Default to dwarf version 4 on hppa64-hpux
[official-gcc.git] / gcc / dwarf2out.c
blob9876750e4f9f0f91d44c3b333e8195d10b287729
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 break;
16814 case CONST_STRING:
16815 resolve_one_addr (&rtl);
16816 goto symref;
16818 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16819 the expression. An UNSPEC rtx represents a raw DWARF operation,
16820 new_loc_descr is called for it to build the operation directly.
16821 Otherwise mem_loc_descriptor is called recursively. */
16822 case PARALLEL:
16824 int index = 0;
16825 dw_loc_descr_ref exp_result = NULL;
16827 for (; index < XVECLEN (rtl, 0); index++)
16829 rtx elem = XVECEXP (rtl, 0, index);
16830 if (GET_CODE (elem) == UNSPEC)
16832 /* Each DWARF operation UNSPEC contain two operands, if
16833 one operand is not used for the operation, const0_rtx is
16834 passed. */
16835 gcc_assert (XVECLEN (elem, 0) == 2);
16837 HOST_WIDE_INT dw_op = XINT (elem, 1);
16838 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16839 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16840 exp_result
16841 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16842 oprnd2);
16844 else
16845 exp_result
16846 = mem_loc_descriptor (elem, mode, mem_mode,
16847 VAR_INIT_STATUS_INITIALIZED);
16849 if (!mem_loc_result)
16850 mem_loc_result = exp_result;
16851 else
16852 add_loc_descr (&mem_loc_result, exp_result);
16855 break;
16858 default:
16859 if (flag_checking)
16861 print_rtl (stderr, rtl);
16862 gcc_unreachable ();
16864 break;
16867 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16868 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16870 return mem_loc_result;
16873 /* Return a descriptor that describes the concatenation of two locations.
16874 This is typically a complex variable. */
16876 static dw_loc_descr_ref
16877 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16879 /* At present we only track constant-sized pieces. */
16880 unsigned int size0, size1;
16881 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16882 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16883 return 0;
16885 dw_loc_descr_ref cc_loc_result = NULL;
16886 dw_loc_descr_ref x0_ref
16887 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16888 dw_loc_descr_ref x1_ref
16889 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16891 if (x0_ref == 0 || x1_ref == 0)
16892 return 0;
16894 cc_loc_result = x0_ref;
16895 add_loc_descr_op_piece (&cc_loc_result, size0);
16897 add_loc_descr (&cc_loc_result, x1_ref);
16898 add_loc_descr_op_piece (&cc_loc_result, size1);
16900 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16901 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16903 return cc_loc_result;
16906 /* Return a descriptor that describes the concatenation of N
16907 locations. */
16909 static dw_loc_descr_ref
16910 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16912 unsigned int i;
16913 dw_loc_descr_ref cc_loc_result = NULL;
16914 unsigned int n = XVECLEN (concatn, 0);
16915 unsigned int size;
16917 for (i = 0; i < n; ++i)
16919 dw_loc_descr_ref ref;
16920 rtx x = XVECEXP (concatn, 0, i);
16922 /* At present we only track constant-sized pieces. */
16923 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16924 return NULL;
16926 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16927 if (ref == NULL)
16928 return NULL;
16930 add_loc_descr (&cc_loc_result, ref);
16931 add_loc_descr_op_piece (&cc_loc_result, size);
16934 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16935 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16937 return cc_loc_result;
16940 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16941 for DEBUG_IMPLICIT_PTR RTL. */
16943 static dw_loc_descr_ref
16944 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16946 dw_loc_descr_ref ret;
16947 dw_die_ref ref;
16949 if (dwarf_strict && dwarf_version < 5)
16950 return NULL;
16951 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16952 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16953 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16954 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16955 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16956 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16957 if (ref)
16959 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16960 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16961 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16963 else
16965 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16966 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16968 return ret;
16971 /* Output a proper Dwarf location descriptor for a variable or parameter
16972 which is either allocated in a register or in a memory location. For a
16973 register, we just generate an OP_REG and the register number. For a
16974 memory location we provide a Dwarf postfix expression describing how to
16975 generate the (dynamic) address of the object onto the address stack.
16977 MODE is mode of the decl if this loc_descriptor is going to be used in
16978 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16979 allowed, VOIDmode otherwise.
16981 If we don't know how to describe it, return 0. */
16983 static dw_loc_descr_ref
16984 loc_descriptor (rtx rtl, machine_mode mode,
16985 enum var_init_status initialized)
16987 dw_loc_descr_ref loc_result = NULL;
16988 scalar_int_mode int_mode;
16990 switch (GET_CODE (rtl))
16992 case SUBREG:
16993 /* The case of a subreg may arise when we have a local (register)
16994 variable or a formal (register) parameter which doesn't quite fill
16995 up an entire register. For now, just assume that it is
16996 legitimate to make the Dwarf info refer to the whole register which
16997 contains the given subreg. */
16998 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16999 loc_result = loc_descriptor (SUBREG_REG (rtl),
17000 GET_MODE (SUBREG_REG (rtl)), initialized);
17001 else
17002 goto do_default;
17003 break;
17005 case REG:
17006 loc_result = reg_loc_descriptor (rtl, initialized);
17007 break;
17009 case MEM:
17010 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17011 GET_MODE (rtl), initialized);
17012 if (loc_result == NULL)
17013 loc_result = tls_mem_loc_descriptor (rtl);
17014 if (loc_result == NULL)
17016 rtx new_rtl = avoid_constant_pool_reference (rtl);
17017 if (new_rtl != rtl)
17018 loc_result = loc_descriptor (new_rtl, mode, initialized);
17020 break;
17022 case CONCAT:
17023 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17024 initialized);
17025 break;
17027 case CONCATN:
17028 loc_result = concatn_loc_descriptor (rtl, initialized);
17029 break;
17031 case VAR_LOCATION:
17032 /* Single part. */
17033 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17035 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17036 if (GET_CODE (loc) == EXPR_LIST)
17037 loc = XEXP (loc, 0);
17038 loc_result = loc_descriptor (loc, mode, initialized);
17039 break;
17042 rtl = XEXP (rtl, 1);
17043 /* FALLTHRU */
17045 case PARALLEL:
17047 rtvec par_elems = XVEC (rtl, 0);
17048 int num_elem = GET_NUM_ELEM (par_elems);
17049 machine_mode mode;
17050 int i, size;
17052 /* Create the first one, so we have something to add to. */
17053 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17054 VOIDmode, initialized);
17055 if (loc_result == NULL)
17056 return NULL;
17057 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17058 /* At present we only track constant-sized pieces. */
17059 if (!GET_MODE_SIZE (mode).is_constant (&size))
17060 return NULL;
17061 add_loc_descr_op_piece (&loc_result, size);
17062 for (i = 1; i < num_elem; i++)
17064 dw_loc_descr_ref temp;
17066 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17067 VOIDmode, initialized);
17068 if (temp == NULL)
17069 return NULL;
17070 add_loc_descr (&loc_result, temp);
17071 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17072 /* At present we only track constant-sized pieces. */
17073 if (!GET_MODE_SIZE (mode).is_constant (&size))
17074 return NULL;
17075 add_loc_descr_op_piece (&loc_result, size);
17078 break;
17080 case CONST_INT:
17081 if (mode != VOIDmode && mode != BLKmode)
17083 int_mode = as_a <scalar_int_mode> (mode);
17084 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17085 INTVAL (rtl));
17087 break;
17089 case CONST_DOUBLE:
17090 if (mode == VOIDmode)
17091 mode = GET_MODE (rtl);
17093 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17095 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17097 /* Note that a CONST_DOUBLE rtx could represent either an integer
17098 or a floating-point constant. A CONST_DOUBLE is used whenever
17099 the constant requires more than one word in order to be
17100 adequately represented. We output CONST_DOUBLEs as blocks. */
17101 scalar_mode smode = as_a <scalar_mode> (mode);
17102 loc_result = new_loc_descr (DW_OP_implicit_value,
17103 GET_MODE_SIZE (smode), 0);
17104 #if TARGET_SUPPORTS_WIDE_INT == 0
17105 if (!SCALAR_FLOAT_MODE_P (smode))
17107 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17108 loc_result->dw_loc_oprnd2.v.val_double
17109 = rtx_to_double_int (rtl);
17111 else
17112 #endif
17114 unsigned int length = GET_MODE_SIZE (smode);
17115 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17116 unsigned int elt_size = insert_float (rtl, array);
17118 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17119 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17120 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17121 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17124 break;
17126 case CONST_WIDE_INT:
17127 if (mode == VOIDmode)
17128 mode = GET_MODE (rtl);
17130 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17132 int_mode = as_a <scalar_int_mode> (mode);
17133 loc_result = new_loc_descr (DW_OP_implicit_value,
17134 GET_MODE_SIZE (int_mode), 0);
17135 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17136 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
17137 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
17139 break;
17141 case CONST_VECTOR:
17142 if (mode == VOIDmode)
17143 mode = GET_MODE (rtl);
17145 if (mode != VOIDmode
17146 /* The combination of a length and byte elt_size doesn't extend
17147 naturally to boolean vectors, where several elements are packed
17148 into the same byte. */
17149 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17150 && (dwarf_version >= 4 || !dwarf_strict))
17152 unsigned int length;
17153 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17154 return NULL;
17156 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17157 unsigned char *array
17158 = ggc_vec_alloc<unsigned char> (length * elt_size);
17159 unsigned int i;
17160 unsigned char *p;
17161 machine_mode imode = GET_MODE_INNER (mode);
17163 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17164 switch (GET_MODE_CLASS (mode))
17166 case MODE_VECTOR_INT:
17167 for (i = 0, p = array; i < length; i++, p += elt_size)
17169 rtx elt = CONST_VECTOR_ELT (rtl, i);
17170 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17172 break;
17174 case MODE_VECTOR_FLOAT:
17175 for (i = 0, p = array; i < length; i++, p += elt_size)
17177 rtx elt = CONST_VECTOR_ELT (rtl, i);
17178 insert_float (elt, p);
17180 break;
17182 default:
17183 gcc_unreachable ();
17186 loc_result = new_loc_descr (DW_OP_implicit_value,
17187 length * elt_size, 0);
17188 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17189 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17190 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17191 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17193 break;
17195 case CONST:
17196 if (mode == VOIDmode
17197 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17198 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17199 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17201 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17202 break;
17204 /* FALLTHROUGH */
17205 case SYMBOL_REF:
17206 if (!const_ok_for_output (rtl))
17207 break;
17208 /* FALLTHROUGH */
17209 case LABEL_REF:
17210 if (is_a <scalar_int_mode> (mode, &int_mode)
17211 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17212 && (dwarf_version >= 4 || !dwarf_strict))
17214 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17215 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17216 vec_safe_push (used_rtx_array, rtl);
17218 break;
17220 case DEBUG_IMPLICIT_PTR:
17221 loc_result = implicit_ptr_descriptor (rtl, 0);
17222 break;
17224 case PLUS:
17225 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17226 && CONST_INT_P (XEXP (rtl, 1)))
17228 loc_result
17229 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17230 break;
17232 /* FALLTHRU */
17233 do_default:
17234 default:
17235 if ((is_a <scalar_int_mode> (mode, &int_mode)
17236 && GET_MODE (rtl) == int_mode
17237 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17238 && dwarf_version >= 4)
17239 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17241 /* Value expression. */
17242 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17243 if (loc_result)
17244 add_loc_descr (&loc_result,
17245 new_loc_descr (DW_OP_stack_value, 0, 0));
17247 break;
17250 return loc_result;
17253 /* We need to figure out what section we should use as the base for the
17254 address ranges where a given location is valid.
17255 1. If this particular DECL has a section associated with it, use that.
17256 2. If this function has a section associated with it, use that.
17257 3. Otherwise, use the text section.
17258 XXX: If you split a variable across multiple sections, we won't notice. */
17260 static const char *
17261 secname_for_decl (const_tree decl)
17263 const char *secname;
17265 if (VAR_OR_FUNCTION_DECL_P (decl)
17266 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17267 && DECL_SECTION_NAME (decl))
17268 secname = DECL_SECTION_NAME (decl);
17269 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17271 if (in_cold_section_p)
17273 section *sec = current_function_section ();
17274 if (sec->common.flags & SECTION_NAMED)
17275 return sec->named.name;
17277 secname = DECL_SECTION_NAME (current_function_decl);
17279 else if (cfun && in_cold_section_p)
17280 secname = crtl->subsections.cold_section_label;
17281 else
17282 secname = text_section_label;
17284 return secname;
17287 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17289 static bool
17290 decl_by_reference_p (tree decl)
17292 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17293 || VAR_P (decl))
17294 && DECL_BY_REFERENCE (decl));
17297 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17298 for VARLOC. */
17300 static dw_loc_descr_ref
17301 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17302 enum var_init_status initialized)
17304 int have_address = 0;
17305 dw_loc_descr_ref descr;
17306 machine_mode mode;
17308 if (want_address != 2)
17310 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17311 /* Single part. */
17312 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17314 varloc = PAT_VAR_LOCATION_LOC (varloc);
17315 if (GET_CODE (varloc) == EXPR_LIST)
17316 varloc = XEXP (varloc, 0);
17317 mode = GET_MODE (varloc);
17318 if (MEM_P (varloc))
17320 rtx addr = XEXP (varloc, 0);
17321 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17322 mode, initialized);
17323 if (descr)
17324 have_address = 1;
17325 else
17327 rtx x = avoid_constant_pool_reference (varloc);
17328 if (x != varloc)
17329 descr = mem_loc_descriptor (x, mode, VOIDmode,
17330 initialized);
17333 else
17334 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17336 else
17337 return 0;
17339 else
17341 if (GET_CODE (varloc) == VAR_LOCATION)
17342 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17343 else
17344 mode = DECL_MODE (loc);
17345 descr = loc_descriptor (varloc, mode, initialized);
17346 have_address = 1;
17349 if (!descr)
17350 return 0;
17352 if (want_address == 2 && !have_address
17353 && (dwarf_version >= 4 || !dwarf_strict))
17355 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17357 expansion_failed (loc, NULL_RTX,
17358 "DWARF address size mismatch");
17359 return 0;
17361 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17362 have_address = 1;
17364 /* Show if we can't fill the request for an address. */
17365 if (want_address && !have_address)
17367 expansion_failed (loc, NULL_RTX,
17368 "Want address and only have value");
17369 return 0;
17372 /* If we've got an address and don't want one, dereference. */
17373 if (!want_address && have_address)
17375 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17376 enum dwarf_location_atom op;
17378 if (size > DWARF2_ADDR_SIZE || size == -1)
17380 expansion_failed (loc, NULL_RTX,
17381 "DWARF address size mismatch");
17382 return 0;
17384 else if (size == DWARF2_ADDR_SIZE)
17385 op = DW_OP_deref;
17386 else
17387 op = DW_OP_deref_size;
17389 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17392 return descr;
17395 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17396 if it is not possible. */
17398 static dw_loc_descr_ref
17399 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17401 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17402 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17403 else if (dwarf_version >= 3 || !dwarf_strict)
17404 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17405 else
17406 return NULL;
17409 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17410 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17412 static dw_loc_descr_ref
17413 dw_sra_loc_expr (tree decl, rtx loc)
17415 rtx p;
17416 unsigned HOST_WIDE_INT padsize = 0;
17417 dw_loc_descr_ref descr, *descr_tail;
17418 unsigned HOST_WIDE_INT decl_size;
17419 rtx varloc;
17420 enum var_init_status initialized;
17422 if (DECL_SIZE (decl) == NULL
17423 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17424 return NULL;
17426 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17427 descr = NULL;
17428 descr_tail = &descr;
17430 for (p = loc; p; p = XEXP (p, 1))
17432 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17433 rtx loc_note = *decl_piece_varloc_ptr (p);
17434 dw_loc_descr_ref cur_descr;
17435 dw_loc_descr_ref *tail, last = NULL;
17436 unsigned HOST_WIDE_INT opsize = 0;
17438 if (loc_note == NULL_RTX
17439 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17441 padsize += bitsize;
17442 continue;
17444 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17445 varloc = NOTE_VAR_LOCATION (loc_note);
17446 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17447 if (cur_descr == NULL)
17449 padsize += bitsize;
17450 continue;
17453 /* Check that cur_descr either doesn't use
17454 DW_OP_*piece operations, or their sum is equal
17455 to bitsize. Otherwise we can't embed it. */
17456 for (tail = &cur_descr; *tail != NULL;
17457 tail = &(*tail)->dw_loc_next)
17458 if ((*tail)->dw_loc_opc == DW_OP_piece)
17460 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17461 * BITS_PER_UNIT;
17462 last = *tail;
17464 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17466 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17467 last = *tail;
17470 if (last != NULL && opsize != bitsize)
17472 padsize += bitsize;
17473 /* Discard the current piece of the descriptor and release any
17474 addr_table entries it uses. */
17475 remove_loc_list_addr_table_entries (cur_descr);
17476 continue;
17479 /* If there is a hole, add DW_OP_*piece after empty DWARF
17480 expression, which means that those bits are optimized out. */
17481 if (padsize)
17483 if (padsize > decl_size)
17485 remove_loc_list_addr_table_entries (cur_descr);
17486 goto discard_descr;
17488 decl_size -= padsize;
17489 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17490 if (*descr_tail == NULL)
17492 remove_loc_list_addr_table_entries (cur_descr);
17493 goto discard_descr;
17495 descr_tail = &(*descr_tail)->dw_loc_next;
17496 padsize = 0;
17498 *descr_tail = cur_descr;
17499 descr_tail = tail;
17500 if (bitsize > decl_size)
17501 goto discard_descr;
17502 decl_size -= bitsize;
17503 if (last == NULL)
17505 HOST_WIDE_INT offset = 0;
17506 if (GET_CODE (varloc) == VAR_LOCATION
17507 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17509 varloc = PAT_VAR_LOCATION_LOC (varloc);
17510 if (GET_CODE (varloc) == EXPR_LIST)
17511 varloc = XEXP (varloc, 0);
17515 if (GET_CODE (varloc) == CONST
17516 || GET_CODE (varloc) == SIGN_EXTEND
17517 || GET_CODE (varloc) == ZERO_EXTEND)
17518 varloc = XEXP (varloc, 0);
17519 else if (GET_CODE (varloc) == SUBREG)
17520 varloc = SUBREG_REG (varloc);
17521 else
17522 break;
17524 while (1);
17525 /* DW_OP_bit_size offset should be zero for register
17526 or implicit location descriptions and empty location
17527 descriptions, but for memory addresses needs big endian
17528 adjustment. */
17529 if (MEM_P (varloc))
17531 unsigned HOST_WIDE_INT memsize;
17532 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17533 goto discard_descr;
17534 memsize *= BITS_PER_UNIT;
17535 if (memsize != bitsize)
17537 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17538 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17539 goto discard_descr;
17540 if (memsize < bitsize)
17541 goto discard_descr;
17542 if (BITS_BIG_ENDIAN)
17543 offset = memsize - bitsize;
17547 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17548 if (*descr_tail == NULL)
17549 goto discard_descr;
17550 descr_tail = &(*descr_tail)->dw_loc_next;
17554 /* If there were any non-empty expressions, add padding till the end of
17555 the decl. */
17556 if (descr != NULL && decl_size != 0)
17558 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17559 if (*descr_tail == NULL)
17560 goto discard_descr;
17562 return descr;
17564 discard_descr:
17565 /* Discard the descriptor and release any addr_table entries it uses. */
17566 remove_loc_list_addr_table_entries (descr);
17567 return NULL;
17570 /* Return the dwarf representation of the location list LOC_LIST of
17571 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17572 function. */
17574 static dw_loc_list_ref
17575 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17577 const char *endname, *secname;
17578 var_loc_view endview;
17579 rtx varloc;
17580 enum var_init_status initialized;
17581 struct var_loc_node *node;
17582 dw_loc_descr_ref descr;
17583 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17584 dw_loc_list_ref list = NULL;
17585 dw_loc_list_ref *listp = &list;
17587 /* Now that we know what section we are using for a base,
17588 actually construct the list of locations.
17589 The first location information is what is passed to the
17590 function that creates the location list, and the remaining
17591 locations just get added on to that list.
17592 Note that we only know the start address for a location
17593 (IE location changes), so to build the range, we use
17594 the range [current location start, next location start].
17595 This means we have to special case the last node, and generate
17596 a range of [last location start, end of function label]. */
17598 if (cfun && crtl->has_bb_partition)
17600 bool save_in_cold_section_p = in_cold_section_p;
17601 in_cold_section_p = first_function_block_is_cold;
17602 if (loc_list->last_before_switch == NULL)
17603 in_cold_section_p = !in_cold_section_p;
17604 secname = secname_for_decl (decl);
17605 in_cold_section_p = save_in_cold_section_p;
17607 else
17608 secname = secname_for_decl (decl);
17610 for (node = loc_list->first; node; node = node->next)
17612 bool range_across_switch = false;
17613 if (GET_CODE (node->loc) == EXPR_LIST
17614 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17616 if (GET_CODE (node->loc) == EXPR_LIST)
17618 descr = NULL;
17619 /* This requires DW_OP_{,bit_}piece, which is not usable
17620 inside DWARF expressions. */
17621 if (want_address == 2)
17622 descr = dw_sra_loc_expr (decl, node->loc);
17624 else
17626 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17627 varloc = NOTE_VAR_LOCATION (node->loc);
17628 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17630 if (descr)
17632 /* If section switch happens in between node->label
17633 and node->next->label (or end of function) and
17634 we can't emit it as a single entry list,
17635 emit two ranges, first one ending at the end
17636 of first partition and second one starting at the
17637 beginning of second partition. */
17638 if (node == loc_list->last_before_switch
17639 && (node != loc_list->first || loc_list->first->next
17640 /* If we are to emit a view number, we will emit
17641 a loclist rather than a single location
17642 expression for the entire function (see
17643 loc_list_has_views), so we have to split the
17644 range that straddles across partitions. */
17645 || !ZERO_VIEW_P (node->view))
17646 && current_function_decl)
17648 endname = cfun->fde->dw_fde_end;
17649 endview = 0;
17650 range_across_switch = true;
17652 /* The variable has a location between NODE->LABEL and
17653 NODE->NEXT->LABEL. */
17654 else if (node->next)
17655 endname = node->next->label, endview = node->next->view;
17656 /* If the variable has a location at the last label
17657 it keeps its location until the end of function. */
17658 else if (!current_function_decl)
17659 endname = text_end_label, endview = 0;
17660 else
17662 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17663 current_function_funcdef_no);
17664 endname = ggc_strdup (label_id);
17665 endview = 0;
17668 *listp = new_loc_list (descr, node->label, node->view,
17669 endname, endview, secname);
17670 if (TREE_CODE (decl) == PARM_DECL
17671 && node == loc_list->first
17672 && NOTE_P (node->loc)
17673 && strcmp (node->label, endname) == 0)
17674 (*listp)->force = true;
17675 listp = &(*listp)->dw_loc_next;
17679 if (cfun
17680 && crtl->has_bb_partition
17681 && node == loc_list->last_before_switch)
17683 bool save_in_cold_section_p = in_cold_section_p;
17684 in_cold_section_p = !first_function_block_is_cold;
17685 secname = secname_for_decl (decl);
17686 in_cold_section_p = save_in_cold_section_p;
17689 if (range_across_switch)
17691 if (GET_CODE (node->loc) == EXPR_LIST)
17692 descr = dw_sra_loc_expr (decl, node->loc);
17693 else
17695 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17696 varloc = NOTE_VAR_LOCATION (node->loc);
17697 descr = dw_loc_list_1 (decl, varloc, want_address,
17698 initialized);
17700 gcc_assert (descr);
17701 /* The variable has a location between NODE->LABEL and
17702 NODE->NEXT->LABEL. */
17703 if (node->next)
17704 endname = node->next->label, endview = node->next->view;
17705 else
17706 endname = cfun->fde->dw_fde_second_end, endview = 0;
17707 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17708 endname, endview, secname);
17709 listp = &(*listp)->dw_loc_next;
17713 /* Try to avoid the overhead of a location list emitting a location
17714 expression instead, but only if we didn't have more than one
17715 location entry in the first place. If some entries were not
17716 representable, we don't want to pretend a single entry that was
17717 applies to the entire scope in which the variable is
17718 available. */
17719 if (list && loc_list->first->next)
17720 gen_llsym (list);
17721 else
17722 maybe_gen_llsym (list);
17724 return list;
17727 /* Return if the loc_list has only single element and thus can be represented
17728 as location description. */
17730 static bool
17731 single_element_loc_list_p (dw_loc_list_ref list)
17733 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17734 return !list->ll_symbol;
17737 /* Duplicate a single element of location list. */
17739 static inline dw_loc_descr_ref
17740 copy_loc_descr (dw_loc_descr_ref ref)
17742 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17743 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17744 return copy;
17747 /* To each location in list LIST append loc descr REF. */
17749 static void
17750 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17752 dw_loc_descr_ref copy;
17753 add_loc_descr (&list->expr, ref);
17754 list = list->dw_loc_next;
17755 while (list)
17757 copy = copy_loc_descr (ref);
17758 add_loc_descr (&list->expr, copy);
17759 while (copy->dw_loc_next)
17760 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17761 list = list->dw_loc_next;
17765 /* To each location in list LIST prepend loc descr REF. */
17767 static void
17768 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17770 dw_loc_descr_ref copy;
17771 dw_loc_descr_ref ref_end = list->expr;
17772 add_loc_descr (&ref, list->expr);
17773 list->expr = ref;
17774 list = list->dw_loc_next;
17775 while (list)
17777 dw_loc_descr_ref end = list->expr;
17778 list->expr = copy = copy_loc_descr (ref);
17779 while (copy->dw_loc_next != ref_end)
17780 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17781 copy->dw_loc_next = end;
17782 list = list->dw_loc_next;
17786 /* Given two lists RET and LIST
17787 produce location list that is result of adding expression in LIST
17788 to expression in RET on each position in program.
17789 Might be destructive on both RET and LIST.
17791 TODO: We handle only simple cases of RET or LIST having at most one
17792 element. General case would involve sorting the lists in program order
17793 and merging them that will need some additional work.
17794 Adding that will improve quality of debug info especially for SRA-ed
17795 structures. */
17797 static void
17798 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17800 if (!list)
17801 return;
17802 if (!*ret)
17804 *ret = list;
17805 return;
17807 if (!list->dw_loc_next)
17809 add_loc_descr_to_each (*ret, list->expr);
17810 return;
17812 if (!(*ret)->dw_loc_next)
17814 prepend_loc_descr_to_each (list, (*ret)->expr);
17815 *ret = list;
17816 return;
17818 expansion_failed (NULL_TREE, NULL_RTX,
17819 "Don't know how to merge two non-trivial"
17820 " location lists.\n");
17821 *ret = NULL;
17822 return;
17825 /* LOC is constant expression. Try a luck, look it up in constant
17826 pool and return its loc_descr of its address. */
17828 static dw_loc_descr_ref
17829 cst_pool_loc_descr (tree loc)
17831 /* Get an RTL for this, if something has been emitted. */
17832 rtx rtl = lookup_constant_def (loc);
17834 if (!rtl || !MEM_P (rtl))
17836 gcc_assert (!rtl);
17837 return 0;
17839 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17841 /* TODO: We might get more coverage if we was actually delaying expansion
17842 of all expressions till end of compilation when constant pools are fully
17843 populated. */
17844 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17846 expansion_failed (loc, NULL_RTX,
17847 "CST value in contant pool but not marked.");
17848 return 0;
17850 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17851 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17854 /* Return dw_loc_list representing address of addr_expr LOC
17855 by looking for inner INDIRECT_REF expression and turning
17856 it into simple arithmetics.
17858 See loc_list_from_tree for the meaning of CONTEXT. */
17860 static dw_loc_list_ref
17861 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17862 loc_descr_context *context)
17864 tree obj, offset;
17865 poly_int64 bitsize, bitpos, bytepos;
17866 machine_mode mode;
17867 int unsignedp, reversep, volatilep = 0;
17868 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17870 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17871 &bitsize, &bitpos, &offset, &mode,
17872 &unsignedp, &reversep, &volatilep);
17873 STRIP_NOPS (obj);
17874 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17876 expansion_failed (loc, NULL_RTX, "bitfield access");
17877 return 0;
17879 if (!INDIRECT_REF_P (obj))
17881 expansion_failed (obj,
17882 NULL_RTX, "no indirect ref in inner refrence");
17883 return 0;
17885 if (!offset && known_eq (bitpos, 0))
17886 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17887 context);
17888 else if (toplev
17889 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17890 && (dwarf_version >= 4 || !dwarf_strict))
17892 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17893 if (!list_ret)
17894 return 0;
17895 if (offset)
17897 /* Variable offset. */
17898 list_ret1 = loc_list_from_tree (offset, 0, context);
17899 if (list_ret1 == 0)
17900 return 0;
17901 add_loc_list (&list_ret, list_ret1);
17902 if (!list_ret)
17903 return 0;
17904 add_loc_descr_to_each (list_ret,
17905 new_loc_descr (DW_OP_plus, 0, 0));
17907 HOST_WIDE_INT value;
17908 if (bytepos.is_constant (&value) && value > 0)
17909 add_loc_descr_to_each (list_ret,
17910 new_loc_descr (DW_OP_plus_uconst, value, 0));
17911 else if (maybe_ne (bytepos, 0))
17912 loc_list_plus_const (list_ret, bytepos);
17913 add_loc_descr_to_each (list_ret,
17914 new_loc_descr (DW_OP_stack_value, 0, 0));
17916 return list_ret;
17919 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17920 all operations from LOC are nops, move to the last one. Insert in NOPS all
17921 operations that are skipped. */
17923 static void
17924 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17925 hash_set<dw_loc_descr_ref> &nops)
17927 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17929 nops.add (loc);
17930 loc = loc->dw_loc_next;
17934 /* Helper for loc_descr_without_nops: free the location description operation
17935 P. */
17937 bool
17938 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17940 ggc_free (loc);
17941 return true;
17944 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17945 finishes LOC. */
17947 static void
17948 loc_descr_without_nops (dw_loc_descr_ref &loc)
17950 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17951 return;
17953 /* Set of all DW_OP_nop operations we remove. */
17954 hash_set<dw_loc_descr_ref> nops;
17956 /* First, strip all prefix NOP operations in order to keep the head of the
17957 operations list. */
17958 loc_descr_to_next_no_nop (loc, nops);
17960 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17962 /* For control flow operations: strip "prefix" nops in destination
17963 labels. */
17964 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17965 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17966 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17967 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17969 /* Do the same for the operations that follow, then move to the next
17970 iteration. */
17971 if (cur->dw_loc_next != NULL)
17972 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17973 cur = cur->dw_loc_next;
17976 nops.traverse<void *, free_loc_descr> (NULL);
17980 struct dwarf_procedure_info;
17982 /* Helper structure for location descriptions generation. */
17983 struct loc_descr_context
17985 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17986 NULL_TREE if DW_OP_push_object_address in invalid for this location
17987 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17988 tree context_type;
17989 /* The ..._DECL node that should be translated as a
17990 DW_OP_push_object_address operation. */
17991 tree base_decl;
17992 /* Information about the DWARF procedure we are currently generating. NULL if
17993 we are not generating a DWARF procedure. */
17994 struct dwarf_procedure_info *dpi;
17995 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17996 by consumer. Used for DW_TAG_generic_subrange attributes. */
17997 bool placeholder_arg;
17998 /* True if PLACEHOLDER_EXPR has been seen. */
17999 bool placeholder_seen;
18000 /* True if strict preservation of signedness has been requested. */
18001 bool strict_signedness;
18004 /* DWARF procedures generation
18006 DWARF expressions (aka. location descriptions) are used to encode variable
18007 things such as sizes or offsets. Such computations can have redundant parts
18008 that can be factorized in order to reduce the size of the output debug
18009 information. This is the whole point of DWARF procedures.
18011 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
18012 already factorized into functions ("size functions") in order to handle very
18013 big and complex types. Such functions are quite simple: they have integral
18014 arguments, they return an integral result and their body contains only a
18015 return statement with arithmetic expressions. This is the only kind of
18016 function we are interested in translating into DWARF procedures, here.
18018 DWARF expressions and DWARF procedure are executed using a stack, so we have
18019 to define some calling convention for them to interact. Let's say that:
18021 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18022 all arguments in reverse order (right-to-left) so that when the DWARF
18023 procedure execution starts, the first argument is the top of the stack.
18025 - Then, when returning, the DWARF procedure must have consumed all arguments
18026 on the stack, must have pushed the result and touched nothing else.
18028 - Each integral argument and the result are integral types can be hold in a
18029 single stack slot.
18031 - We call "frame offset" the number of stack slots that are "under DWARF
18032 procedure control": it includes the arguments slots, the temporaries and
18033 the result slot. Thus, it is equal to the number of arguments when the
18034 procedure execution starts and must be equal to one (the result) when it
18035 returns. */
18037 /* Helper structure used when generating operations for a DWARF procedure. */
18038 struct dwarf_procedure_info
18040 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18041 currently translated. */
18042 tree fndecl;
18043 /* The number of arguments FNDECL takes. */
18044 unsigned args_count;
18047 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18048 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18049 equate it to this DIE. */
18051 static dw_die_ref
18052 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18053 dw_die_ref parent_die)
18055 dw_die_ref dwarf_proc_die;
18057 if ((dwarf_version < 3 && dwarf_strict)
18058 || location == NULL)
18059 return NULL;
18061 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18062 if (fndecl)
18063 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18064 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18065 return dwarf_proc_die;
18068 /* Return whether TYPE is a supported type as a DWARF procedure argument
18069 type or return type (we handle only scalar types and pointer types that
18070 aren't wider than the DWARF expression evaluation stack). */
18072 static bool
18073 is_handled_procedure_type (tree type)
18075 return ((INTEGRAL_TYPE_P (type)
18076 || TREE_CODE (type) == OFFSET_TYPE
18077 || TREE_CODE (type) == POINTER_TYPE)
18078 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18081 /* Helper for resolve_args_picking: do the same but stop when coming across
18082 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18083 offset *before* evaluating the corresponding operation. */
18085 static bool
18086 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18087 struct dwarf_procedure_info *dpi,
18088 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18090 /* The "frame_offset" identifier is already used to name a macro... */
18091 unsigned frame_offset_ = initial_frame_offset;
18092 dw_loc_descr_ref l;
18094 for (l = loc; l != NULL;)
18096 bool existed;
18097 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18099 /* If we already met this node, there is nothing to compute anymore. */
18100 if (existed)
18102 /* Make sure that the stack size is consistent wherever the execution
18103 flow comes from. */
18104 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18105 break;
18107 l_frame_offset = frame_offset_;
18109 /* If needed, relocate the picking offset with respect to the frame
18110 offset. */
18111 if (l->frame_offset_rel)
18113 unsigned HOST_WIDE_INT off;
18114 switch (l->dw_loc_opc)
18116 case DW_OP_pick:
18117 off = l->dw_loc_oprnd1.v.val_unsigned;
18118 break;
18119 case DW_OP_dup:
18120 off = 0;
18121 break;
18122 case DW_OP_over:
18123 off = 1;
18124 break;
18125 default:
18126 gcc_unreachable ();
18128 /* frame_offset_ is the size of the current stack frame, including
18129 incoming arguments. Besides, the arguments are pushed
18130 right-to-left. Thus, in order to access the Nth argument from
18131 this operation node, the picking has to skip temporaries *plus*
18132 one stack slot per argument (0 for the first one, 1 for the second
18133 one, etc.).
18135 The targetted argument number (N) is already set as the operand,
18136 and the number of temporaries can be computed with:
18137 frame_offsets_ - dpi->args_count */
18138 off += frame_offset_ - dpi->args_count;
18140 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18141 if (off > 255)
18142 return false;
18144 if (off == 0)
18146 l->dw_loc_opc = DW_OP_dup;
18147 l->dw_loc_oprnd1.v.val_unsigned = 0;
18149 else if (off == 1)
18151 l->dw_loc_opc = DW_OP_over;
18152 l->dw_loc_oprnd1.v.val_unsigned = 0;
18154 else
18156 l->dw_loc_opc = DW_OP_pick;
18157 l->dw_loc_oprnd1.v.val_unsigned = off;
18161 /* Update frame_offset according to the effect the current operation has
18162 on the stack. */
18163 switch (l->dw_loc_opc)
18165 case DW_OP_deref:
18166 case DW_OP_swap:
18167 case DW_OP_rot:
18168 case DW_OP_abs:
18169 case DW_OP_neg:
18170 case DW_OP_not:
18171 case DW_OP_plus_uconst:
18172 case DW_OP_skip:
18173 case DW_OP_reg0:
18174 case DW_OP_reg1:
18175 case DW_OP_reg2:
18176 case DW_OP_reg3:
18177 case DW_OP_reg4:
18178 case DW_OP_reg5:
18179 case DW_OP_reg6:
18180 case DW_OP_reg7:
18181 case DW_OP_reg8:
18182 case DW_OP_reg9:
18183 case DW_OP_reg10:
18184 case DW_OP_reg11:
18185 case DW_OP_reg12:
18186 case DW_OP_reg13:
18187 case DW_OP_reg14:
18188 case DW_OP_reg15:
18189 case DW_OP_reg16:
18190 case DW_OP_reg17:
18191 case DW_OP_reg18:
18192 case DW_OP_reg19:
18193 case DW_OP_reg20:
18194 case DW_OP_reg21:
18195 case DW_OP_reg22:
18196 case DW_OP_reg23:
18197 case DW_OP_reg24:
18198 case DW_OP_reg25:
18199 case DW_OP_reg26:
18200 case DW_OP_reg27:
18201 case DW_OP_reg28:
18202 case DW_OP_reg29:
18203 case DW_OP_reg30:
18204 case DW_OP_reg31:
18205 case DW_OP_bregx:
18206 case DW_OP_piece:
18207 case DW_OP_deref_size:
18208 case DW_OP_nop:
18209 case DW_OP_bit_piece:
18210 case DW_OP_implicit_value:
18211 case DW_OP_stack_value:
18212 case DW_OP_deref_type:
18213 case DW_OP_convert:
18214 case DW_OP_reinterpret:
18215 case DW_OP_GNU_deref_type:
18216 case DW_OP_GNU_convert:
18217 case DW_OP_GNU_reinterpret:
18218 break;
18220 case DW_OP_addr:
18221 case DW_OP_const1u:
18222 case DW_OP_const1s:
18223 case DW_OP_const2u:
18224 case DW_OP_const2s:
18225 case DW_OP_const4u:
18226 case DW_OP_const4s:
18227 case DW_OP_const8u:
18228 case DW_OP_const8s:
18229 case DW_OP_constu:
18230 case DW_OP_consts:
18231 case DW_OP_dup:
18232 case DW_OP_over:
18233 case DW_OP_pick:
18234 case DW_OP_lit0:
18235 case DW_OP_lit1:
18236 case DW_OP_lit2:
18237 case DW_OP_lit3:
18238 case DW_OP_lit4:
18239 case DW_OP_lit5:
18240 case DW_OP_lit6:
18241 case DW_OP_lit7:
18242 case DW_OP_lit8:
18243 case DW_OP_lit9:
18244 case DW_OP_lit10:
18245 case DW_OP_lit11:
18246 case DW_OP_lit12:
18247 case DW_OP_lit13:
18248 case DW_OP_lit14:
18249 case DW_OP_lit15:
18250 case DW_OP_lit16:
18251 case DW_OP_lit17:
18252 case DW_OP_lit18:
18253 case DW_OP_lit19:
18254 case DW_OP_lit20:
18255 case DW_OP_lit21:
18256 case DW_OP_lit22:
18257 case DW_OP_lit23:
18258 case DW_OP_lit24:
18259 case DW_OP_lit25:
18260 case DW_OP_lit26:
18261 case DW_OP_lit27:
18262 case DW_OP_lit28:
18263 case DW_OP_lit29:
18264 case DW_OP_lit30:
18265 case DW_OP_lit31:
18266 case DW_OP_breg0:
18267 case DW_OP_breg1:
18268 case DW_OP_breg2:
18269 case DW_OP_breg3:
18270 case DW_OP_breg4:
18271 case DW_OP_breg5:
18272 case DW_OP_breg6:
18273 case DW_OP_breg7:
18274 case DW_OP_breg8:
18275 case DW_OP_breg9:
18276 case DW_OP_breg10:
18277 case DW_OP_breg11:
18278 case DW_OP_breg12:
18279 case DW_OP_breg13:
18280 case DW_OP_breg14:
18281 case DW_OP_breg15:
18282 case DW_OP_breg16:
18283 case DW_OP_breg17:
18284 case DW_OP_breg18:
18285 case DW_OP_breg19:
18286 case DW_OP_breg20:
18287 case DW_OP_breg21:
18288 case DW_OP_breg22:
18289 case DW_OP_breg23:
18290 case DW_OP_breg24:
18291 case DW_OP_breg25:
18292 case DW_OP_breg26:
18293 case DW_OP_breg27:
18294 case DW_OP_breg28:
18295 case DW_OP_breg29:
18296 case DW_OP_breg30:
18297 case DW_OP_breg31:
18298 case DW_OP_fbreg:
18299 case DW_OP_push_object_address:
18300 case DW_OP_call_frame_cfa:
18301 case DW_OP_GNU_variable_value:
18302 case DW_OP_GNU_addr_index:
18303 case DW_OP_GNU_const_index:
18304 ++frame_offset_;
18305 break;
18307 case DW_OP_drop:
18308 case DW_OP_xderef:
18309 case DW_OP_and:
18310 case DW_OP_div:
18311 case DW_OP_minus:
18312 case DW_OP_mod:
18313 case DW_OP_mul:
18314 case DW_OP_or:
18315 case DW_OP_plus:
18316 case DW_OP_shl:
18317 case DW_OP_shr:
18318 case DW_OP_shra:
18319 case DW_OP_xor:
18320 case DW_OP_bra:
18321 case DW_OP_eq:
18322 case DW_OP_ge:
18323 case DW_OP_gt:
18324 case DW_OP_le:
18325 case DW_OP_lt:
18326 case DW_OP_ne:
18327 case DW_OP_regx:
18328 case DW_OP_xderef_size:
18329 --frame_offset_;
18330 break;
18332 case DW_OP_call2:
18333 case DW_OP_call4:
18334 case DW_OP_call_ref:
18336 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18337 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18339 if (stack_usage == NULL)
18340 return false;
18341 frame_offset_ += *stack_usage;
18342 break;
18345 case DW_OP_implicit_pointer:
18346 case DW_OP_entry_value:
18347 case DW_OP_const_type:
18348 case DW_OP_regval_type:
18349 case DW_OP_form_tls_address:
18350 case DW_OP_GNU_push_tls_address:
18351 case DW_OP_GNU_uninit:
18352 case DW_OP_GNU_encoded_addr:
18353 case DW_OP_GNU_implicit_pointer:
18354 case DW_OP_GNU_entry_value:
18355 case DW_OP_GNU_const_type:
18356 case DW_OP_GNU_regval_type:
18357 case DW_OP_GNU_parameter_ref:
18358 /* loc_list_from_tree will probably not output these operations for
18359 size functions, so assume they will not appear here. */
18360 /* Fall through... */
18362 default:
18363 gcc_unreachable ();
18366 /* Now, follow the control flow (except subroutine calls). */
18367 switch (l->dw_loc_opc)
18369 case DW_OP_bra:
18370 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18371 frame_offsets))
18372 return false;
18373 /* Fall through. */
18375 case DW_OP_skip:
18376 l = l->dw_loc_oprnd1.v.val_loc;
18377 break;
18379 case DW_OP_stack_value:
18380 return true;
18382 default:
18383 l = l->dw_loc_next;
18384 break;
18388 return true;
18391 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18392 operations) in order to resolve the operand of DW_OP_pick operations that
18393 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18394 offset *before* LOC is executed. Return if all relocations were
18395 successful. */
18397 static bool
18398 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18399 struct dwarf_procedure_info *dpi)
18401 /* Associate to all visited operations the frame offset *before* evaluating
18402 this operation. */
18403 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18405 return
18406 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18409 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18410 Return NULL if it is not possible. */
18412 static dw_die_ref
18413 function_to_dwarf_procedure (tree fndecl)
18415 struct dwarf_procedure_info dpi;
18416 struct loc_descr_context ctx = {
18417 NULL_TREE, /* context_type */
18418 NULL_TREE, /* base_decl */
18419 &dpi, /* dpi */
18420 false, /* placeholder_arg */
18421 false, /* placeholder_seen */
18422 true /* strict_signedness */
18424 dw_die_ref dwarf_proc_die;
18425 tree tree_body = DECL_SAVED_TREE (fndecl);
18426 dw_loc_descr_ref loc_body, epilogue;
18428 tree cursor;
18429 unsigned i;
18431 /* Do not generate multiple DWARF procedures for the same function
18432 declaration. */
18433 dwarf_proc_die = lookup_decl_die (fndecl);
18434 if (dwarf_proc_die != NULL)
18435 return dwarf_proc_die;
18437 /* DWARF procedures are available starting with the DWARFv3 standard. */
18438 if (dwarf_version < 3 && dwarf_strict)
18439 return NULL;
18441 /* We handle only functions for which we still have a body, that return a
18442 supported type and that takes arguments with supported types. Note that
18443 there is no point translating functions that return nothing. */
18444 if (tree_body == NULL_TREE
18445 || DECL_RESULT (fndecl) == NULL_TREE
18446 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18447 return NULL;
18449 for (cursor = DECL_ARGUMENTS (fndecl);
18450 cursor != NULL_TREE;
18451 cursor = TREE_CHAIN (cursor))
18452 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18453 return NULL;
18455 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18456 if (TREE_CODE (tree_body) != RETURN_EXPR)
18457 return NULL;
18458 tree_body = TREE_OPERAND (tree_body, 0);
18459 if (TREE_CODE (tree_body) != MODIFY_EXPR
18460 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18461 return NULL;
18462 tree_body = TREE_OPERAND (tree_body, 1);
18464 /* Try to translate the body expression itself. Note that this will probably
18465 cause an infinite recursion if its call graph has a cycle. This is very
18466 unlikely for size functions, however, so don't bother with such things at
18467 the moment. */
18468 dpi.fndecl = fndecl;
18469 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18470 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18471 if (!loc_body)
18472 return NULL;
18474 /* After evaluating all operands in "loc_body", we should still have on the
18475 stack all arguments plus the desired function result (top of the stack).
18476 Generate code in order to keep only the result in our stack frame. */
18477 epilogue = NULL;
18478 for (i = 0; i < dpi.args_count; ++i)
18480 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18481 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18482 op_couple->dw_loc_next->dw_loc_next = epilogue;
18483 epilogue = op_couple;
18485 add_loc_descr (&loc_body, epilogue);
18486 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18487 return NULL;
18489 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18490 because they are considered useful. Now there is an epilogue, they are
18491 not anymore, so give it another try. */
18492 loc_descr_without_nops (loc_body);
18494 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18495 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18496 though, given that size functions do not come from source, so they should
18497 not have a dedicated DW_TAG_subprogram DIE. */
18498 dwarf_proc_die
18499 = new_dwarf_proc_die (loc_body, fndecl,
18500 get_context_die (DECL_CONTEXT (fndecl)));
18502 /* The called DWARF procedure consumes one stack slot per argument and
18503 returns one stack slot. */
18504 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18506 return dwarf_proc_die;
18509 /* Helper function for loc_list_from_tree. Perform OP binary op,
18510 but after converting arguments to type_die, afterwards convert
18511 back to unsigned. */
18513 static dw_loc_list_ref
18514 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18515 dw_die_ref type_die, scalar_int_mode mode,
18516 struct loc_descr_context *context)
18518 dw_loc_list_ref op0, op1;
18519 dw_loc_descr_ref cvt, binop;
18521 if (type_die == NULL)
18522 return NULL;
18524 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18525 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18526 if (op0 == NULL || op1 == NULL)
18527 return NULL;
18529 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18530 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18531 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18532 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18533 add_loc_descr_to_each (op0, cvt);
18535 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18536 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18537 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18538 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18539 add_loc_descr_to_each (op1, cvt);
18541 add_loc_list (&op0, op1);
18542 if (op0 == NULL)
18543 return NULL;
18545 binop = new_loc_descr (op, 0, 0);
18546 convert_descriptor_to_mode (mode, binop);
18547 add_loc_descr_to_each (op0, binop);
18549 return op0;
18552 /* Generate Dwarf location list representing LOC.
18553 If WANT_ADDRESS is false, expression computing LOC will be computed
18554 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18555 if WANT_ADDRESS is 2, expression computing address useable in location
18556 will be returned (i.e. DW_OP_reg can be used
18557 to refer to register values).
18559 CONTEXT provides information to customize the location descriptions
18560 generation. Its context_type field specifies what type is implicitly
18561 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18562 will not be generated.
18564 Its DPI field determines whether we are generating a DWARF expression for a
18565 DWARF procedure, so PARM_DECL references are processed specifically.
18567 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18568 and dpi fields were null. */
18570 static dw_loc_list_ref
18571 loc_list_from_tree_1 (tree loc, int want_address,
18572 struct loc_descr_context *context)
18574 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18575 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18576 int have_address = 0;
18577 enum dwarf_location_atom op;
18579 /* ??? Most of the time we do not take proper care for sign/zero
18580 extending the values properly. Hopefully this won't be a real
18581 problem... */
18583 if (context != NULL
18584 && context->base_decl == loc
18585 && want_address == 0)
18587 if (dwarf_version >= 3 || !dwarf_strict)
18588 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18589 NULL, 0, NULL, 0, NULL);
18590 else
18591 return NULL;
18594 switch (TREE_CODE (loc))
18596 case ERROR_MARK:
18597 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18598 return 0;
18600 case PLACEHOLDER_EXPR:
18601 /* This case involves extracting fields from an object to determine the
18602 position of other fields. It is supposed to appear only as the first
18603 operand of COMPONENT_REF nodes and to reference precisely the type
18604 that the context allows or its enclosing type. */
18605 if (context != NULL
18606 && (TREE_TYPE (loc) == context->context_type
18607 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18608 && want_address >= 1)
18610 if (dwarf_version >= 3 || !dwarf_strict)
18612 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18613 have_address = 1;
18614 break;
18616 else
18617 return NULL;
18619 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18620 the single argument passed by consumer. */
18621 else if (context != NULL
18622 && context->placeholder_arg
18623 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18624 && want_address == 0)
18626 ret = new_loc_descr (DW_OP_pick, 0, 0);
18627 ret->frame_offset_rel = 1;
18628 context->placeholder_seen = true;
18629 break;
18631 else
18632 expansion_failed (loc, NULL_RTX,
18633 "PLACEHOLDER_EXPR for an unexpected type");
18634 break;
18636 case CALL_EXPR:
18638 tree callee = get_callee_fndecl (loc);
18639 dw_die_ref dwarf_proc;
18641 if (callee
18642 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18643 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18645 /* DWARF procedures are used for size functions, which are built
18646 when size expressions contain conditional constructs, so we
18647 request strict preservation of signedness for comparisons. */
18648 bool old_strict_signedness;
18649 if (context)
18651 old_strict_signedness = context->strict_signedness;
18652 context->strict_signedness = true;
18655 /* Evaluate arguments right-to-left so that the first argument
18656 will be the top-most one on the stack. */
18657 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18659 tree arg = CALL_EXPR_ARG (loc, i);
18660 ret1 = loc_descriptor_from_tree (arg, 0, context);
18661 if (!ret1)
18663 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18664 return NULL;
18666 add_loc_descr (&ret, ret1);
18669 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18670 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18671 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18672 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18673 add_loc_descr (&ret, ret1);
18674 if (context)
18675 context->strict_signedness = old_strict_signedness;
18677 else
18678 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18679 break;
18682 case PREINCREMENT_EXPR:
18683 case PREDECREMENT_EXPR:
18684 case POSTINCREMENT_EXPR:
18685 case POSTDECREMENT_EXPR:
18686 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18687 /* There are no opcodes for these operations. */
18688 return 0;
18690 case ADDR_EXPR:
18691 /* If we already want an address, see if there is INDIRECT_REF inside
18692 e.g. for &this->field. */
18693 if (want_address)
18695 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18696 (loc, want_address == 2, context);
18697 if (list_ret)
18698 have_address = 1;
18699 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18700 && (ret = cst_pool_loc_descr (loc)))
18701 have_address = 1;
18703 /* Otherwise, process the argument and look for the address. */
18704 if (!list_ret && !ret)
18705 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18706 else
18708 if (want_address)
18709 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18710 return NULL;
18712 break;
18714 case VAR_DECL:
18715 if (DECL_THREAD_LOCAL_P (loc))
18717 rtx rtl;
18718 enum dwarf_location_atom tls_op;
18719 enum dtprel_bool dtprel = dtprel_false;
18721 if (targetm.have_tls)
18723 /* If this is not defined, we have no way to emit the
18724 data. */
18725 if (!targetm.asm_out.output_dwarf_dtprel)
18726 return 0;
18728 /* The way DW_OP_GNU_push_tls_address is specified, we
18729 can only look up addresses of objects in the current
18730 module. We used DW_OP_addr as first op, but that's
18731 wrong, because DW_OP_addr is relocated by the debug
18732 info consumer, while DW_OP_GNU_push_tls_address
18733 operand shouldn't be. */
18734 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18735 return 0;
18736 dtprel = dtprel_true;
18737 /* We check for DWARF 5 here because gdb did not implement
18738 DW_OP_form_tls_address until after 7.12. */
18739 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18740 : DW_OP_GNU_push_tls_address);
18742 else
18744 if (!targetm.emutls.debug_form_tls_address
18745 || !(dwarf_version >= 3 || !dwarf_strict))
18746 return 0;
18747 /* We stuffed the control variable into the DECL_VALUE_EXPR
18748 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18749 no longer appear in gimple code. We used the control
18750 variable in specific so that we could pick it up here. */
18751 loc = DECL_VALUE_EXPR (loc);
18752 tls_op = DW_OP_form_tls_address;
18755 rtl = rtl_for_decl_location (loc);
18756 if (rtl == NULL_RTX)
18757 return 0;
18759 if (!MEM_P (rtl))
18760 return 0;
18761 rtl = XEXP (rtl, 0);
18762 if (! CONSTANT_P (rtl))
18763 return 0;
18765 ret = new_addr_loc_descr (rtl, dtprel);
18766 ret1 = new_loc_descr (tls_op, 0, 0);
18767 add_loc_descr (&ret, ret1);
18769 have_address = 1;
18770 break;
18772 /* FALLTHRU */
18774 case PARM_DECL:
18775 if (context != NULL && context->dpi != NULL
18776 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18778 /* We are generating code for a DWARF procedure and we want to access
18779 one of its arguments: find the appropriate argument offset and let
18780 the resolve_args_picking pass compute the offset that complies
18781 with the stack frame size. */
18782 unsigned i = 0;
18783 tree cursor;
18785 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18786 cursor != NULL_TREE && cursor != loc;
18787 cursor = TREE_CHAIN (cursor), ++i)
18789 /* If we are translating a DWARF procedure, all referenced parameters
18790 must belong to the current function. */
18791 gcc_assert (cursor != NULL_TREE);
18793 ret = new_loc_descr (DW_OP_pick, i, 0);
18794 ret->frame_offset_rel = 1;
18795 break;
18797 /* FALLTHRU */
18799 case RESULT_DECL:
18800 if (DECL_HAS_VALUE_EXPR_P (loc))
18802 tree value_expr = DECL_VALUE_EXPR (loc);
18804 /* Non-local frame structures are DECL_IGNORED_P variables so we need
18805 to wait until they get an RTX in order to reference them. */
18806 if (early_dwarf
18807 && TREE_CODE (value_expr) == COMPONENT_REF
18808 && VAR_P (TREE_OPERAND (value_expr, 0))
18809 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
18811 else
18812 return loc_list_from_tree_1 (value_expr, want_address, context);
18815 /* FALLTHRU */
18817 case FUNCTION_DECL:
18819 rtx rtl;
18820 var_loc_list *loc_list = lookup_decl_loc (loc);
18822 if (loc_list && loc_list->first)
18824 list_ret = dw_loc_list (loc_list, loc, want_address);
18825 have_address = want_address != 0;
18826 break;
18828 rtl = rtl_for_decl_location (loc);
18829 if (rtl == NULL_RTX)
18831 if (TREE_CODE (loc) != FUNCTION_DECL
18832 && early_dwarf
18833 && want_address != 1
18834 && ! DECL_IGNORED_P (loc)
18835 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18836 || POINTER_TYPE_P (TREE_TYPE (loc)))
18837 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18838 <= DWARF2_ADDR_SIZE))
18840 dw_die_ref ref = lookup_decl_die (loc);
18841 if (ref)
18843 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18844 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18845 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18846 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18848 else if (current_function_decl
18849 && DECL_CONTEXT (loc) == current_function_decl)
18851 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18852 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18853 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18855 break;
18857 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18858 return 0;
18860 else if (CONST_INT_P (rtl))
18862 HOST_WIDE_INT val = INTVAL (rtl);
18863 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18864 val &= GET_MODE_MASK (DECL_MODE (loc));
18865 ret = int_loc_descriptor (val);
18867 else if (GET_CODE (rtl) == CONST_STRING)
18869 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18870 return 0;
18872 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18873 ret = new_addr_loc_descr (rtl, dtprel_false);
18874 else
18876 machine_mode mode, mem_mode;
18878 /* Certain constructs can only be represented at top-level. */
18879 if (want_address == 2)
18881 ret = loc_descriptor (rtl, VOIDmode,
18882 VAR_INIT_STATUS_INITIALIZED);
18883 have_address = 1;
18885 else
18887 mode = GET_MODE (rtl);
18888 mem_mode = VOIDmode;
18889 if (MEM_P (rtl))
18891 mem_mode = mode;
18892 mode = get_address_mode (rtl);
18893 rtl = XEXP (rtl, 0);
18894 have_address = 1;
18896 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18897 VAR_INIT_STATUS_INITIALIZED);
18899 if (!ret)
18900 expansion_failed (loc, rtl,
18901 "failed to produce loc descriptor for rtl");
18904 break;
18906 case MEM_REF:
18907 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18909 have_address = 1;
18910 goto do_plus;
18912 /* Fallthru. */
18913 case INDIRECT_REF:
18914 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18915 have_address = 1;
18916 break;
18918 case TARGET_MEM_REF:
18919 case SSA_NAME:
18920 case DEBUG_EXPR_DECL:
18921 return NULL;
18923 case COMPOUND_EXPR:
18924 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18925 context);
18927 CASE_CONVERT:
18928 case VIEW_CONVERT_EXPR:
18929 case SAVE_EXPR:
18930 case MODIFY_EXPR:
18931 case NON_LVALUE_EXPR:
18932 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18933 context);
18935 case COMPONENT_REF:
18936 case BIT_FIELD_REF:
18937 case ARRAY_REF:
18938 case ARRAY_RANGE_REF:
18939 case REALPART_EXPR:
18940 case IMAGPART_EXPR:
18942 tree obj, offset;
18943 poly_int64 bitsize, bitpos, bytepos;
18944 machine_mode mode;
18945 int unsignedp, reversep, volatilep = 0;
18947 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18948 &unsignedp, &reversep, &volatilep);
18950 gcc_assert (obj != loc);
18952 list_ret = loc_list_from_tree_1 (obj,
18953 want_address == 2
18954 && known_eq (bitpos, 0)
18955 && !offset ? 2 : 1,
18956 context);
18957 /* TODO: We can extract value of the small expression via shifting even
18958 for nonzero bitpos. */
18959 if (list_ret == 0)
18960 return 0;
18961 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18962 || !multiple_p (bitsize, BITS_PER_UNIT))
18964 expansion_failed (loc, NULL_RTX,
18965 "bitfield access");
18966 return 0;
18969 if (offset != NULL_TREE)
18971 /* Variable offset. */
18972 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18973 if (list_ret1 == 0)
18974 return 0;
18975 add_loc_list (&list_ret, list_ret1);
18976 if (!list_ret)
18977 return 0;
18978 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18981 HOST_WIDE_INT value;
18982 if (bytepos.is_constant (&value) && value > 0)
18983 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18984 value, 0));
18985 else if (maybe_ne (bytepos, 0))
18986 loc_list_plus_const (list_ret, bytepos);
18988 have_address = 1;
18989 break;
18992 case INTEGER_CST:
18993 if ((want_address || !tree_fits_shwi_p (loc))
18994 && (ret = cst_pool_loc_descr (loc)))
18995 have_address = 1;
18996 else if (want_address == 2
18997 && tree_fits_shwi_p (loc)
18998 && (ret = address_of_int_loc_descriptor
18999 (int_size_in_bytes (TREE_TYPE (loc)),
19000 tree_to_shwi (loc))))
19001 have_address = 1;
19002 else if (tree_fits_shwi_p (loc))
19003 ret = int_loc_descriptor (tree_to_shwi (loc));
19004 else if (tree_fits_uhwi_p (loc))
19005 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19006 else
19008 expansion_failed (loc, NULL_RTX,
19009 "Integer operand is not host integer");
19010 return 0;
19012 break;
19014 case POLY_INT_CST:
19016 if (want_address)
19018 expansion_failed (loc, NULL_RTX,
19019 "constant address with a runtime component");
19020 return 0;
19022 poly_int64 value;
19023 if (!poly_int_tree_p (loc, &value))
19025 expansion_failed (loc, NULL_RTX, "constant too big");
19026 return 0;
19028 ret = int_loc_descriptor (value);
19030 break;
19032 case CONSTRUCTOR:
19033 case REAL_CST:
19034 case STRING_CST:
19035 case COMPLEX_CST:
19036 if ((ret = cst_pool_loc_descr (loc)))
19037 have_address = 1;
19038 else if (TREE_CODE (loc) == CONSTRUCTOR)
19040 tree type = TREE_TYPE (loc);
19041 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19042 unsigned HOST_WIDE_INT offset = 0;
19043 unsigned HOST_WIDE_INT cnt;
19044 constructor_elt *ce;
19046 if (TREE_CODE (type) == RECORD_TYPE)
19048 /* This is very limited, but it's enough to output
19049 pointers to member functions, as long as the
19050 referenced function is defined in the current
19051 translation unit. */
19052 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19054 tree val = ce->value;
19056 tree field = ce->index;
19058 if (val)
19059 STRIP_NOPS (val);
19061 if (!field || DECL_BIT_FIELD (field))
19063 expansion_failed (loc, NULL_RTX,
19064 "bitfield in record type constructor");
19065 size = offset = (unsigned HOST_WIDE_INT)-1;
19066 ret = NULL;
19067 break;
19070 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19071 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19072 gcc_assert (pos + fieldsize <= size);
19073 if (pos < offset)
19075 expansion_failed (loc, NULL_RTX,
19076 "out-of-order fields in record constructor");
19077 size = offset = (unsigned HOST_WIDE_INT)-1;
19078 ret = NULL;
19079 break;
19081 if (pos > offset)
19083 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19084 add_loc_descr (&ret, ret1);
19085 offset = pos;
19087 if (val && fieldsize != 0)
19089 ret1 = loc_descriptor_from_tree (val, want_address, context);
19090 if (!ret1)
19092 expansion_failed (loc, NULL_RTX,
19093 "unsupported expression in field");
19094 size = offset = (unsigned HOST_WIDE_INT)-1;
19095 ret = NULL;
19096 break;
19098 add_loc_descr (&ret, ret1);
19100 if (fieldsize)
19102 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19103 add_loc_descr (&ret, ret1);
19104 offset = pos + fieldsize;
19108 if (offset != size)
19110 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19111 add_loc_descr (&ret, ret1);
19112 offset = size;
19115 have_address = !!want_address;
19117 else
19118 expansion_failed (loc, NULL_RTX,
19119 "constructor of non-record type");
19121 else
19122 /* We can construct small constants here using int_loc_descriptor. */
19123 expansion_failed (loc, NULL_RTX,
19124 "constructor or constant not in constant pool");
19125 break;
19127 case TRUTH_AND_EXPR:
19128 case TRUTH_ANDIF_EXPR:
19129 case BIT_AND_EXPR:
19130 op = DW_OP_and;
19131 goto do_binop;
19133 case TRUTH_XOR_EXPR:
19134 case BIT_XOR_EXPR:
19135 op = DW_OP_xor;
19136 goto do_binop;
19138 case TRUTH_OR_EXPR:
19139 case TRUTH_ORIF_EXPR:
19140 case BIT_IOR_EXPR:
19141 op = DW_OP_or;
19142 goto do_binop;
19144 case EXACT_DIV_EXPR:
19145 case FLOOR_DIV_EXPR:
19146 case TRUNC_DIV_EXPR:
19147 /* Turn a divide by a power of 2 into a shift when possible. */
19148 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19149 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19151 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19152 if (log2 > 0)
19154 list_ret
19155 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19156 if (list_ret == 0)
19157 return 0;
19159 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19160 add_loc_descr_to_each (list_ret,
19161 new_loc_descr (DW_OP_shr, 0, 0));
19162 break;
19166 /* fall through */
19168 case CEIL_DIV_EXPR:
19169 case ROUND_DIV_EXPR:
19170 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19172 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19173 scalar_int_mode int_mode;
19175 if ((dwarf_strict && dwarf_version < 5)
19176 || !is_a <scalar_int_mode> (mode, &int_mode))
19177 return 0;
19179 /* We can use a signed divide if the sign bit is not set. */
19180 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19182 op = DW_OP_div;
19183 goto do_binop;
19186 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19187 base_type_for_mode (int_mode, 1),
19188 int_mode, context);
19189 break;
19191 op = DW_OP_div;
19192 goto do_binop;
19194 case MINUS_EXPR:
19195 op = DW_OP_minus;
19196 goto do_binop;
19198 case FLOOR_MOD_EXPR:
19199 case CEIL_MOD_EXPR:
19200 case ROUND_MOD_EXPR:
19201 case TRUNC_MOD_EXPR:
19202 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19204 op = DW_OP_mod;
19205 goto do_binop;
19207 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19208 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19209 if (list_ret == 0 || list_ret1 == 0)
19210 return 0;
19212 add_loc_list (&list_ret, list_ret1);
19213 if (list_ret == 0)
19214 return 0;
19215 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19216 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19217 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19218 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19219 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19220 break;
19222 case MULT_EXPR:
19223 op = DW_OP_mul;
19224 goto do_binop;
19226 case LSHIFT_EXPR:
19227 op = DW_OP_shl;
19228 goto do_binop;
19230 case RSHIFT_EXPR:
19231 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19232 goto do_binop;
19234 case POINTER_PLUS_EXPR:
19235 case PLUS_EXPR:
19236 do_plus:
19237 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19239 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19240 smarter to encode their opposite. The DW_OP_plus_uconst operation
19241 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19242 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19243 bytes, Y being the size of the operation that pushes the opposite
19244 of the addend. So let's choose the smallest representation. */
19245 const tree tree_addend = TREE_OPERAND (loc, 1);
19246 offset_int wi_addend;
19247 HOST_WIDE_INT shwi_addend;
19248 dw_loc_descr_ref loc_naddend;
19250 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19251 if (list_ret == 0)
19252 return 0;
19254 /* Try to get the literal to push. It is the opposite of the addend,
19255 so as we rely on wrapping during DWARF evaluation, first decode
19256 the literal as a "DWARF-sized" signed number. */
19257 wi_addend = wi::to_offset (tree_addend);
19258 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19259 shwi_addend = wi_addend.to_shwi ();
19260 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19261 ? int_loc_descriptor (-shwi_addend)
19262 : NULL;
19264 if (loc_naddend != NULL
19265 && ((unsigned) size_of_uleb128 (shwi_addend)
19266 > size_of_loc_descr (loc_naddend)))
19268 add_loc_descr_to_each (list_ret, loc_naddend);
19269 add_loc_descr_to_each (list_ret,
19270 new_loc_descr (DW_OP_minus, 0, 0));
19272 else
19274 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19276 loc_naddend = loc_cur;
19277 loc_cur = loc_cur->dw_loc_next;
19278 ggc_free (loc_naddend);
19280 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19282 break;
19285 op = DW_OP_plus;
19286 goto do_binop;
19288 case LE_EXPR:
19289 op = DW_OP_le;
19290 goto do_comp_binop;
19292 case GE_EXPR:
19293 op = DW_OP_ge;
19294 goto do_comp_binop;
19296 case LT_EXPR:
19297 op = DW_OP_lt;
19298 goto do_comp_binop;
19300 case GT_EXPR:
19301 op = DW_OP_gt;
19302 goto do_comp_binop;
19304 do_comp_binop:
19305 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19307 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19308 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19309 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19310 TREE_CODE (loc));
19311 break;
19313 else
19314 goto do_binop;
19316 case EQ_EXPR:
19317 op = DW_OP_eq;
19318 goto do_binop;
19320 case NE_EXPR:
19321 op = DW_OP_ne;
19322 goto do_binop;
19324 do_binop:
19325 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19326 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19327 if (list_ret == 0 || list_ret1 == 0)
19328 return 0;
19330 add_loc_list (&list_ret, list_ret1);
19331 if (list_ret == 0)
19332 return 0;
19333 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19334 break;
19336 case TRUTH_NOT_EXPR:
19337 case BIT_NOT_EXPR:
19338 op = DW_OP_not;
19339 goto do_unop;
19341 case ABS_EXPR:
19342 op = DW_OP_abs;
19343 goto do_unop;
19345 case NEGATE_EXPR:
19346 op = DW_OP_neg;
19347 goto do_unop;
19349 do_unop:
19350 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19351 if (list_ret == 0)
19352 return 0;
19354 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19355 break;
19357 case MIN_EXPR:
19358 case MAX_EXPR:
19360 const enum tree_code code =
19361 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19363 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19364 build2 (code, integer_type_node,
19365 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19366 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19369 /* fall through */
19371 case COND_EXPR:
19373 dw_loc_descr_ref lhs
19374 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19375 dw_loc_list_ref rhs
19376 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19377 dw_loc_descr_ref bra_node, jump_node, tmp;
19379 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19380 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19381 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19382 list_ret
19383 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19384 0, context);
19385 else
19386 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19387 if (list_ret == 0 || lhs == 0 || rhs == 0)
19388 return 0;
19390 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19391 add_loc_descr_to_each (list_ret, bra_node);
19393 add_loc_list (&list_ret, rhs);
19394 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19395 add_loc_descr_to_each (list_ret, jump_node);
19397 add_loc_descr_to_each (list_ret, lhs);
19398 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19399 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19401 /* ??? Need a node to point the skip at. Use a nop. */
19402 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19403 add_loc_descr_to_each (list_ret, tmp);
19404 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19405 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19407 break;
19409 case FIX_TRUNC_EXPR:
19410 return 0;
19412 case COMPOUND_LITERAL_EXPR:
19413 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19414 0, context);
19416 default:
19417 /* Leave front-end specific codes as simply unknown. This comes
19418 up, for instance, with the C STMT_EXPR. */
19419 if ((unsigned int) TREE_CODE (loc)
19420 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19422 expansion_failed (loc, NULL_RTX,
19423 "language specific tree node");
19424 return 0;
19427 /* Otherwise this is a generic code; we should just lists all of
19428 these explicitly. We forgot one. */
19429 if (flag_checking)
19430 gcc_unreachable ();
19432 /* In a release build, we want to degrade gracefully: better to
19433 generate incomplete debugging information than to crash. */
19434 return NULL;
19437 if (!ret && !list_ret)
19438 return 0;
19440 if (want_address == 2 && !have_address
19441 && (dwarf_version >= 4 || !dwarf_strict))
19443 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19445 expansion_failed (loc, NULL_RTX,
19446 "DWARF address size mismatch");
19447 return 0;
19449 if (ret)
19450 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19451 else
19452 add_loc_descr_to_each (list_ret,
19453 new_loc_descr (DW_OP_stack_value, 0, 0));
19454 have_address = 1;
19456 /* Show if we can't fill the request for an address. */
19457 if (want_address && !have_address)
19459 expansion_failed (loc, NULL_RTX,
19460 "Want address and only have value");
19461 return 0;
19464 gcc_assert (!ret || !list_ret);
19466 /* If we've got an address and don't want one, dereference. */
19467 if (!want_address && have_address)
19469 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19470 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19471 scalar_int_mode int_mode;
19472 dw_die_ref type_die;
19473 dw_loc_descr_ref deref;
19475 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19476 if (size > DWARF2_ADDR_SIZE || size == -1)
19478 expansion_failed (loc, NULL_RTX,
19479 "DWARF address size mismatch");
19480 return 0;
19483 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19484 else if (size == DWARF2_ADDR_SIZE)
19485 deref = new_loc_descr (DW_OP_deref, size, 0);
19487 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19488 extend the value, which is really OK for unsigned types only. */
19489 else if (!(context && context->strict_signedness)
19490 || TYPE_UNSIGNED (TREE_TYPE (loc))
19491 || (dwarf_strict && dwarf_version < 5)
19492 || !is_a <scalar_int_mode> (mode, &int_mode)
19493 || !(type_die = base_type_for_mode (mode, false)))
19494 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19496 /* Use DW_OP_deref_type for signed integral types if possible, but
19497 convert back to the generic type to avoid type mismatches later. */
19498 else
19500 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19501 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19502 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19503 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19504 add_loc_descr (&deref,
19505 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19508 if (ret)
19509 add_loc_descr (&ret, deref);
19510 else
19511 add_loc_descr_to_each (list_ret, deref);
19514 if (ret)
19515 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19517 return list_ret;
19520 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19521 expressions. */
19523 static dw_loc_list_ref
19524 loc_list_from_tree (tree loc, int want_address,
19525 struct loc_descr_context *context)
19527 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19529 for (dw_loc_list_ref loc_cur = result;
19530 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19531 loc_descr_without_nops (loc_cur->expr);
19532 return result;
19535 /* Same as above but return only single location expression. */
19536 static dw_loc_descr_ref
19537 loc_descriptor_from_tree (tree loc, int want_address,
19538 struct loc_descr_context *context)
19540 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19541 if (!ret)
19542 return NULL;
19543 if (ret->dw_loc_next)
19545 expansion_failed (loc, NULL_RTX,
19546 "Location list where only loc descriptor needed");
19547 return NULL;
19549 return ret->expr;
19552 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19553 pointer to the declared type for the relevant field variable, or return
19554 `integer_type_node' if the given node turns out to be an
19555 ERROR_MARK node. */
19557 static inline tree
19558 field_type (const_tree decl)
19560 tree type;
19562 if (TREE_CODE (decl) == ERROR_MARK)
19563 return integer_type_node;
19565 type = DECL_BIT_FIELD_TYPE (decl);
19566 if (type == NULL_TREE)
19567 type = TREE_TYPE (decl);
19569 return type;
19572 /* Given a pointer to a tree node, return the alignment in bits for
19573 it, or else return BITS_PER_WORD if the node actually turns out to
19574 be an ERROR_MARK node. */
19576 static inline unsigned
19577 simple_type_align_in_bits (const_tree type)
19579 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19582 static inline unsigned
19583 simple_decl_align_in_bits (const_tree decl)
19585 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19588 /* Return the result of rounding T up to ALIGN. */
19590 static inline offset_int
19591 round_up_to_align (const offset_int &t, unsigned int align)
19593 return wi::udiv_trunc (t + align - 1, align) * align;
19596 /* Helper structure for RECORD_TYPE processing. */
19597 struct vlr_context
19599 /* Root RECORD_TYPE. It is needed to generate data member location
19600 descriptions in variable-length records (VLR), but also to cope with
19601 variants, which are composed of nested structures multiplexed with
19602 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19603 function processing a FIELD_DECL, it is required to be non null. */
19604 tree struct_type;
19606 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19607 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19608 this variant part as part of the root record (in storage units). For
19609 regular records, it must be NULL_TREE. */
19610 tree variant_part_offset;
19613 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19614 addressed byte of the "containing object" for the given FIELD_DECL. If
19615 possible, return a native constant through CST_OFFSET (in which case NULL is
19616 returned); otherwise return a DWARF expression that computes the offset.
19618 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19619 that offset is, either because the argument turns out to be a pointer to an
19620 ERROR_MARK node, or because the offset expression is too complex for us.
19622 CTX is required: see the comment for VLR_CONTEXT. */
19624 static dw_loc_descr_ref
19625 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19626 HOST_WIDE_INT *cst_offset)
19628 tree tree_result;
19629 dw_loc_list_ref loc_result;
19631 *cst_offset = 0;
19633 if (TREE_CODE (decl) == ERROR_MARK)
19634 return NULL;
19635 else
19636 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19638 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19639 case. */
19640 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19641 return NULL;
19643 /* We used to handle only constant offsets in all cases. Now, we handle
19644 properly dynamic byte offsets only when PCC bitfield type doesn't
19645 matter. */
19646 if (PCC_BITFIELD_TYPE_MATTERS
19647 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19649 offset_int object_offset_in_bits;
19650 offset_int object_offset_in_bytes;
19651 offset_int bitpos_int;
19652 tree type;
19653 tree field_size_tree;
19654 offset_int deepest_bitpos;
19655 offset_int field_size_in_bits;
19656 unsigned int type_align_in_bits;
19657 unsigned int decl_align_in_bits;
19658 offset_int type_size_in_bits;
19660 bitpos_int = wi::to_offset (bit_position (decl));
19661 type = field_type (decl);
19662 type_size_in_bits = offset_int_type_size_in_bits (type);
19663 type_align_in_bits = simple_type_align_in_bits (type);
19665 field_size_tree = DECL_SIZE (decl);
19667 /* The size could be unspecified if there was an error, or for
19668 a flexible array member. */
19669 if (!field_size_tree)
19670 field_size_tree = bitsize_zero_node;
19672 /* If the size of the field is not constant, use the type size. */
19673 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19674 field_size_in_bits = wi::to_offset (field_size_tree);
19675 else
19676 field_size_in_bits = type_size_in_bits;
19678 decl_align_in_bits = simple_decl_align_in_bits (decl);
19680 /* The GCC front-end doesn't make any attempt to keep track of the
19681 starting bit offset (relative to the start of the containing
19682 structure type) of the hypothetical "containing object" for a
19683 bit-field. Thus, when computing the byte offset value for the
19684 start of the "containing object" of a bit-field, we must deduce
19685 this information on our own. This can be rather tricky to do in
19686 some cases. For example, handling the following structure type
19687 definition when compiling for an i386/i486 target (which only
19688 aligns long long's to 32-bit boundaries) can be very tricky:
19690 struct S { int field1; long long field2:31; };
19692 Fortunately, there is a simple rule-of-thumb which can be used
19693 in such cases. When compiling for an i386/i486, GCC will
19694 allocate 8 bytes for the structure shown above. It decides to
19695 do this based upon one simple rule for bit-field allocation.
19696 GCC allocates each "containing object" for each bit-field at
19697 the first (i.e. lowest addressed) legitimate alignment boundary
19698 (based upon the required minimum alignment for the declared
19699 type of the field) which it can possibly use, subject to the
19700 condition that there is still enough available space remaining
19701 in the containing object (when allocated at the selected point)
19702 to fully accommodate all of the bits of the bit-field itself.
19704 This simple rule makes it obvious why GCC allocates 8 bytes for
19705 each object of the structure type shown above. When looking
19706 for a place to allocate the "containing object" for `field2',
19707 the compiler simply tries to allocate a 64-bit "containing
19708 object" at each successive 32-bit boundary (starting at zero)
19709 until it finds a place to allocate that 64- bit field such that
19710 at least 31 contiguous (and previously unallocated) bits remain
19711 within that selected 64 bit field. (As it turns out, for the
19712 example above, the compiler finds it is OK to allocate the
19713 "containing object" 64-bit field at bit-offset zero within the
19714 structure type.)
19716 Here we attempt to work backwards from the limited set of facts
19717 we're given, and we try to deduce from those facts, where GCC
19718 must have believed that the containing object started (within
19719 the structure type). The value we deduce is then used (by the
19720 callers of this routine) to generate DW_AT_location and
19721 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19722 the case of DW_AT_location, regular fields as well). */
19724 /* Figure out the bit-distance from the start of the structure to
19725 the "deepest" bit of the bit-field. */
19726 deepest_bitpos = bitpos_int + field_size_in_bits;
19728 /* This is the tricky part. Use some fancy footwork to deduce
19729 where the lowest addressed bit of the containing object must
19730 be. */
19731 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19733 /* Round up to type_align by default. This works best for
19734 bitfields. */
19735 object_offset_in_bits
19736 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19738 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19740 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19742 /* Round up to decl_align instead. */
19743 object_offset_in_bits
19744 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19747 object_offset_in_bytes
19748 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19749 if (ctx->variant_part_offset == NULL_TREE)
19751 *cst_offset = object_offset_in_bytes.to_shwi ();
19752 return NULL;
19754 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19756 else
19757 tree_result = byte_position (decl);
19759 if (ctx->variant_part_offset != NULL_TREE)
19760 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19761 ctx->variant_part_offset, tree_result);
19763 /* If the byte offset is a constant, it's simplier to handle a native
19764 constant rather than a DWARF expression. */
19765 if (TREE_CODE (tree_result) == INTEGER_CST)
19767 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19768 return NULL;
19771 struct loc_descr_context loc_ctx = {
19772 ctx->struct_type, /* context_type */
19773 NULL_TREE, /* base_decl */
19774 NULL, /* dpi */
19775 false, /* placeholder_arg */
19776 false, /* placeholder_seen */
19777 false /* strict_signedness */
19779 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19781 /* We want a DWARF expression: abort if we only have a location list with
19782 multiple elements. */
19783 if (!loc_result || !single_element_loc_list_p (loc_result))
19784 return NULL;
19785 else
19786 return loc_result->expr;
19789 /* The following routines define various Dwarf attributes and any data
19790 associated with them. */
19792 /* Add a location description attribute value to a DIE.
19794 This emits location attributes suitable for whole variables and
19795 whole parameters. Note that the location attributes for struct fields are
19796 generated by the routine `data_member_location_attribute' below. */
19798 static inline void
19799 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19800 dw_loc_list_ref descr)
19802 bool check_no_locviews = true;
19803 if (descr == 0)
19804 return;
19805 if (single_element_loc_list_p (descr))
19806 add_AT_loc (die, attr_kind, descr->expr);
19807 else
19809 add_AT_loc_list (die, attr_kind, descr);
19810 gcc_assert (descr->ll_symbol);
19811 if (attr_kind == DW_AT_location && descr->vl_symbol
19812 && dwarf2out_locviews_in_attribute ())
19814 add_AT_view_list (die, DW_AT_GNU_locviews);
19815 check_no_locviews = false;
19819 if (check_no_locviews)
19820 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19823 /* Add DW_AT_accessibility attribute to DIE if needed. */
19825 static void
19826 add_accessibility_attribute (dw_die_ref die, tree decl)
19828 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19829 children, otherwise the default is DW_ACCESS_public. In DWARF2
19830 the default has always been DW_ACCESS_public. */
19831 if (TREE_PROTECTED (decl))
19832 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19833 else if (TREE_PRIVATE (decl))
19835 if (dwarf_version == 2
19836 || die->die_parent == NULL
19837 || die->die_parent->die_tag != DW_TAG_class_type)
19838 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19840 else if (dwarf_version > 2
19841 && die->die_parent
19842 && die->die_parent->die_tag == DW_TAG_class_type)
19843 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19846 /* Attach the specialized form of location attribute used for data members of
19847 struct and union types. In the special case of a FIELD_DECL node which
19848 represents a bit-field, the "offset" part of this special location
19849 descriptor must indicate the distance in bytes from the lowest-addressed
19850 byte of the containing struct or union type to the lowest-addressed byte of
19851 the "containing object" for the bit-field. (See the `field_byte_offset'
19852 function above).
19854 For any given bit-field, the "containing object" is a hypothetical object
19855 (of some integral or enum type) within which the given bit-field lives. The
19856 type of this hypothetical "containing object" is always the same as the
19857 declared type of the individual bit-field itself (for GCC anyway... the
19858 DWARF spec doesn't actually mandate this). Note that it is the size (in
19859 bytes) of the hypothetical "containing object" which will be given in the
19860 DW_AT_byte_size attribute for this bit-field. (See the
19861 `byte_size_attribute' function below.) It is also used when calculating the
19862 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19863 function below.)
19865 CTX is required: see the comment for VLR_CONTEXT. */
19867 static void
19868 add_data_member_location_attribute (dw_die_ref die,
19869 tree decl,
19870 struct vlr_context *ctx)
19872 HOST_WIDE_INT offset;
19873 dw_loc_descr_ref loc_descr = 0;
19875 if (TREE_CODE (decl) == TREE_BINFO)
19877 /* We're working on the TAG_inheritance for a base class. */
19878 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19880 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19881 aren't at a fixed offset from all (sub)objects of the same
19882 type. We need to extract the appropriate offset from our
19883 vtable. The following dwarf expression means
19885 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19887 This is specific to the V3 ABI, of course. */
19889 dw_loc_descr_ref tmp;
19891 /* Make a copy of the object address. */
19892 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19893 add_loc_descr (&loc_descr, tmp);
19895 /* Extract the vtable address. */
19896 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19897 add_loc_descr (&loc_descr, tmp);
19899 /* Calculate the address of the offset. */
19900 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19901 gcc_assert (offset < 0);
19903 tmp = int_loc_descriptor (-offset);
19904 add_loc_descr (&loc_descr, tmp);
19905 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19906 add_loc_descr (&loc_descr, tmp);
19908 /* Extract the offset. */
19909 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19910 add_loc_descr (&loc_descr, tmp);
19912 /* Add it to the object address. */
19913 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19914 add_loc_descr (&loc_descr, tmp);
19916 else
19917 offset = tree_to_shwi (BINFO_OFFSET (decl));
19919 else
19921 loc_descr = field_byte_offset (decl, ctx, &offset);
19923 if (!loc_descr)
19926 /* If loc_descr is available, then we know the offset is dynamic. */
19927 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
19929 loc_descr = NULL;
19930 offset = 0;
19933 /* Data member location evaluation starts with the base address on the
19934 stack. Compute the field offset and add it to this base address. */
19935 else
19936 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19939 if (!loc_descr)
19941 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19942 e.g. GDB only added support to it in November 2016. For DWARF5
19943 we need newer debug info consumers anyway. We might change this
19944 to dwarf_version >= 4 once most consumers catched up. */
19945 if (dwarf_version >= 5
19946 && TREE_CODE (decl) == FIELD_DECL
19947 && DECL_BIT_FIELD_TYPE (decl)
19948 && (ctx->variant_part_offset == NULL_TREE
19949 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
19951 tree off = bit_position (decl);
19952 if (ctx->variant_part_offset)
19953 off = bit_from_pos (ctx->variant_part_offset, off);
19954 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19956 remove_AT (die, DW_AT_byte_size);
19957 remove_AT (die, DW_AT_bit_offset);
19958 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19959 return;
19962 if (dwarf_version > 2)
19964 /* Don't need to output a location expression, just the constant. */
19965 if (offset < 0)
19966 add_AT_int (die, DW_AT_data_member_location, offset);
19967 else
19968 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19969 return;
19971 else
19973 enum dwarf_location_atom op;
19975 /* The DWARF2 standard says that we should assume that the structure
19976 address is already on the stack, so we can specify a structure
19977 field address by using DW_OP_plus_uconst. */
19978 op = DW_OP_plus_uconst;
19979 loc_descr = new_loc_descr (op, offset, 0);
19983 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19986 /* Writes integer values to dw_vec_const array. */
19988 static void
19989 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19991 while (size != 0)
19993 *dest++ = val & 0xff;
19994 val >>= 8;
19995 --size;
19999 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20001 static HOST_WIDE_INT
20002 extract_int (const unsigned char *src, unsigned int size)
20004 HOST_WIDE_INT val = 0;
20006 src += size;
20007 while (size != 0)
20009 val <<= 8;
20010 val |= *--src & 0xff;
20011 --size;
20013 return val;
20016 /* Writes wide_int values to dw_vec_const array. */
20018 static void
20019 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
20021 int i;
20023 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20025 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20026 return;
20029 /* We'd have to extend this code to support odd sizes. */
20030 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20032 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20034 if (WORDS_BIG_ENDIAN)
20035 for (i = n - 1; i >= 0; i--)
20037 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20038 dest += sizeof (HOST_WIDE_INT);
20040 else
20041 for (i = 0; i < n; i++)
20043 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20044 dest += sizeof (HOST_WIDE_INT);
20048 /* Writes floating point values to dw_vec_const array. */
20050 static unsigned
20051 insert_float (const_rtx rtl, unsigned char *array)
20053 long val[4];
20054 int i;
20055 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20057 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20059 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20060 if (GET_MODE_SIZE (mode) < 4)
20062 gcc_assert (GET_MODE_SIZE (mode) == 2);
20063 insert_int (val[0], 2, array);
20064 return 2;
20067 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20069 insert_int (val[i], 4, array);
20070 array += 4;
20072 return 4;
20075 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20076 does not have a "location" either in memory or in a register. These
20077 things can arise in GNU C when a constant is passed as an actual parameter
20078 to an inlined function. They can also arise in C++ where declared
20079 constants do not necessarily get memory "homes". */
20081 static bool
20082 add_const_value_attribute (dw_die_ref die, rtx rtl)
20084 switch (GET_CODE (rtl))
20086 case CONST_INT:
20088 HOST_WIDE_INT val = INTVAL (rtl);
20090 if (val < 0)
20091 add_AT_int (die, DW_AT_const_value, val);
20092 else
20093 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20095 return true;
20097 case CONST_WIDE_INT:
20099 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
20100 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
20101 (unsigned int) CONST_WIDE_INT_NUNITS (rtl)
20102 * HOST_BITS_PER_WIDE_INT);
20103 wide_int w = wide_int::from (w1, prec, UNSIGNED);
20104 add_AT_wide (die, DW_AT_const_value, w);
20106 return true;
20108 case CONST_DOUBLE:
20109 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20110 floating-point constant. A CONST_DOUBLE is used whenever the
20111 constant requires more than one word in order to be adequately
20112 represented. */
20113 if (TARGET_SUPPORTS_WIDE_INT == 0
20114 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20115 add_AT_double (die, DW_AT_const_value,
20116 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20117 else
20119 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20120 unsigned int length = GET_MODE_SIZE (mode);
20121 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20122 unsigned int elt_size = insert_float (rtl, array);
20124 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20125 array);
20127 return true;
20129 case CONST_VECTOR:
20131 unsigned int length;
20132 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20133 return false;
20135 machine_mode mode = GET_MODE (rtl);
20136 /* The combination of a length and byte elt_size doesn't extend
20137 naturally to boolean vectors, where several elements are packed
20138 into the same byte. */
20139 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20140 return false;
20142 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20143 unsigned char *array
20144 = ggc_vec_alloc<unsigned char> (length * elt_size);
20145 unsigned int i;
20146 unsigned char *p;
20147 machine_mode imode = GET_MODE_INNER (mode);
20149 switch (GET_MODE_CLASS (mode))
20151 case MODE_VECTOR_INT:
20152 for (i = 0, p = array; i < length; i++, p += elt_size)
20154 rtx elt = CONST_VECTOR_ELT (rtl, i);
20155 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20157 break;
20159 case MODE_VECTOR_FLOAT:
20160 for (i = 0, p = array; i < length; i++, p += elt_size)
20162 rtx elt = CONST_VECTOR_ELT (rtl, i);
20163 insert_float (elt, p);
20165 break;
20167 default:
20168 gcc_unreachable ();
20171 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20173 return true;
20175 case CONST_STRING:
20176 if (dwarf_version >= 4 || !dwarf_strict)
20178 dw_loc_descr_ref loc_result;
20179 resolve_one_addr (&rtl);
20180 rtl_addr:
20181 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20182 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20183 add_AT_loc (die, DW_AT_location, loc_result);
20184 vec_safe_push (used_rtx_array, rtl);
20185 return true;
20187 return false;
20189 case CONST:
20190 if (CONSTANT_P (XEXP (rtl, 0)))
20191 return add_const_value_attribute (die, XEXP (rtl, 0));
20192 /* FALLTHROUGH */
20193 case SYMBOL_REF:
20194 if (!const_ok_for_output (rtl))
20195 return false;
20196 /* FALLTHROUGH */
20197 case LABEL_REF:
20198 if (dwarf_version >= 4 || !dwarf_strict)
20199 goto rtl_addr;
20200 return false;
20202 case PLUS:
20203 /* In cases where an inlined instance of an inline function is passed
20204 the address of an `auto' variable (which is local to the caller) we
20205 can get a situation where the DECL_RTL of the artificial local
20206 variable (for the inlining) which acts as a stand-in for the
20207 corresponding formal parameter (of the inline function) will look
20208 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20209 exactly a compile-time constant expression, but it isn't the address
20210 of the (artificial) local variable either. Rather, it represents the
20211 *value* which the artificial local variable always has during its
20212 lifetime. We currently have no way to represent such quasi-constant
20213 values in Dwarf, so for now we just punt and generate nothing. */
20214 return false;
20216 case HIGH:
20217 case CONST_FIXED:
20218 case MINUS:
20219 case SIGN_EXTEND:
20220 case ZERO_EXTEND:
20221 case CONST_POLY_INT:
20222 return false;
20224 case MEM:
20225 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20226 && MEM_READONLY_P (rtl)
20227 && GET_MODE (rtl) == BLKmode)
20229 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20230 return true;
20232 return false;
20234 default:
20235 /* No other kinds of rtx should be possible here. */
20236 gcc_unreachable ();
20238 return false;
20241 /* Determine whether the evaluation of EXPR references any variables
20242 or functions which aren't otherwise used (and therefore may not be
20243 output). */
20244 static tree
20245 reference_to_unused (tree * tp, int * walk_subtrees,
20246 void * data ATTRIBUTE_UNUSED)
20248 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20249 *walk_subtrees = 0;
20251 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20252 && ! TREE_ASM_WRITTEN (*tp))
20253 return *tp;
20254 /* ??? The C++ FE emits debug information for using decls, so
20255 putting gcc_unreachable here falls over. See PR31899. For now
20256 be conservative. */
20257 else if (!symtab->global_info_ready && VAR_P (*tp))
20258 return *tp;
20259 else if (VAR_P (*tp))
20261 varpool_node *node = varpool_node::get (*tp);
20262 if (!node || !node->definition)
20263 return *tp;
20265 else if (TREE_CODE (*tp) == FUNCTION_DECL
20266 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20268 /* The call graph machinery must have finished analyzing,
20269 optimizing and gimplifying the CU by now.
20270 So if *TP has no call graph node associated
20271 to it, it means *TP will not be emitted. */
20272 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20273 return *tp;
20275 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20276 return *tp;
20278 return NULL_TREE;
20281 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20282 for use in a later add_const_value_attribute call. */
20284 static rtx
20285 rtl_for_decl_init (tree init, tree type)
20287 rtx rtl = NULL_RTX;
20289 STRIP_NOPS (init);
20291 /* If a variable is initialized with a string constant without embedded
20292 zeros, build CONST_STRING. */
20293 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20295 tree enttype = TREE_TYPE (type);
20296 tree domain = TYPE_DOMAIN (type);
20297 scalar_int_mode mode;
20299 if (is_int_mode (TYPE_MODE (enttype), &mode)
20300 && GET_MODE_SIZE (mode) == 1
20301 && domain
20302 && TYPE_MAX_VALUE (domain)
20303 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20304 && integer_zerop (TYPE_MIN_VALUE (domain))
20305 && compare_tree_int (TYPE_MAX_VALUE (domain),
20306 TREE_STRING_LENGTH (init) - 1) == 0
20307 && ((size_t) TREE_STRING_LENGTH (init)
20308 == strlen (TREE_STRING_POINTER (init)) + 1))
20310 rtl = gen_rtx_CONST_STRING (VOIDmode,
20311 ggc_strdup (TREE_STRING_POINTER (init)));
20312 rtl = gen_rtx_MEM (BLKmode, rtl);
20313 MEM_READONLY_P (rtl) = 1;
20316 /* Other aggregates, and complex values, could be represented using
20317 CONCAT: FIXME! */
20318 else if (AGGREGATE_TYPE_P (type)
20319 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20320 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20321 || TREE_CODE (type) == COMPLEX_TYPE)
20323 /* Vectors only work if their mode is supported by the target.
20324 FIXME: generic vectors ought to work too. */
20325 else if (TREE_CODE (type) == VECTOR_TYPE
20326 && !VECTOR_MODE_P (TYPE_MODE (type)))
20328 /* If the initializer is something that we know will expand into an
20329 immediate RTL constant, expand it now. We must be careful not to
20330 reference variables which won't be output. */
20331 else if (initializer_constant_valid_p (init, type)
20332 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20334 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20335 possible. */
20336 if (TREE_CODE (type) == VECTOR_TYPE)
20337 switch (TREE_CODE (init))
20339 case VECTOR_CST:
20340 break;
20341 case CONSTRUCTOR:
20342 if (TREE_CONSTANT (init))
20344 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20345 bool constant_p = true;
20346 tree value;
20347 unsigned HOST_WIDE_INT ix;
20349 /* Even when ctor is constant, it might contain non-*_CST
20350 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20351 belong into VECTOR_CST nodes. */
20352 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20353 if (!CONSTANT_CLASS_P (value))
20355 constant_p = false;
20356 break;
20359 if (constant_p)
20361 init = build_vector_from_ctor (type, elts);
20362 break;
20365 /* FALLTHRU */
20367 default:
20368 return NULL;
20371 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20373 /* If expand_expr returns a MEM, it wasn't immediate. */
20374 gcc_assert (!rtl || !MEM_P (rtl));
20377 return rtl;
20380 /* Generate RTL for the variable DECL to represent its location. */
20382 static rtx
20383 rtl_for_decl_location (tree decl)
20385 rtx rtl;
20387 /* Here we have to decide where we are going to say the parameter "lives"
20388 (as far as the debugger is concerned). We only have a couple of
20389 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20391 DECL_RTL normally indicates where the parameter lives during most of the
20392 activation of the function. If optimization is enabled however, this
20393 could be either NULL or else a pseudo-reg. Both of those cases indicate
20394 that the parameter doesn't really live anywhere (as far as the code
20395 generation parts of GCC are concerned) during most of the function's
20396 activation. That will happen (for example) if the parameter is never
20397 referenced within the function.
20399 We could just generate a location descriptor here for all non-NULL
20400 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20401 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20402 where DECL_RTL is NULL or is a pseudo-reg.
20404 Note however that we can only get away with using DECL_INCOMING_RTL as
20405 a backup substitute for DECL_RTL in certain limited cases. In cases
20406 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20407 we can be sure that the parameter was passed using the same type as it is
20408 declared to have within the function, and that its DECL_INCOMING_RTL
20409 points us to a place where a value of that type is passed.
20411 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20412 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20413 because in these cases DECL_INCOMING_RTL points us to a value of some
20414 type which is *different* from the type of the parameter itself. Thus,
20415 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20416 such cases, the debugger would end up (for example) trying to fetch a
20417 `float' from a place which actually contains the first part of a
20418 `double'. That would lead to really incorrect and confusing
20419 output at debug-time.
20421 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20422 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20423 are a couple of exceptions however. On little-endian machines we can
20424 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20425 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20426 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20427 when (on a little-endian machine) a non-prototyped function has a
20428 parameter declared to be of type `short' or `char'. In such cases,
20429 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20430 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20431 passed `int' value. If the debugger then uses that address to fetch
20432 a `short' or a `char' (on a little-endian machine) the result will be
20433 the correct data, so we allow for such exceptional cases below.
20435 Note that our goal here is to describe the place where the given formal
20436 parameter lives during most of the function's activation (i.e. between the
20437 end of the prologue and the start of the epilogue). We'll do that as best
20438 as we can. Note however that if the given formal parameter is modified
20439 sometime during the execution of the function, then a stack backtrace (at
20440 debug-time) will show the function as having been called with the *new*
20441 value rather than the value which was originally passed in. This happens
20442 rarely enough that it is not a major problem, but it *is* a problem, and
20443 I'd like to fix it.
20445 A future version of dwarf2out.c may generate two additional attributes for
20446 any given DW_TAG_formal_parameter DIE which will describe the "passed
20447 type" and the "passed location" for the given formal parameter in addition
20448 to the attributes we now generate to indicate the "declared type" and the
20449 "active location" for each parameter. This additional set of attributes
20450 could be used by debuggers for stack backtraces. Separately, note that
20451 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20452 This happens (for example) for inlined-instances of inline function formal
20453 parameters which are never referenced. This really shouldn't be
20454 happening. All PARM_DECL nodes should get valid non-NULL
20455 DECL_INCOMING_RTL values. FIXME. */
20457 /* Use DECL_RTL as the "location" unless we find something better. */
20458 rtl = DECL_RTL_IF_SET (decl);
20460 /* When generating abstract instances, ignore everything except
20461 constants, symbols living in memory, and symbols living in
20462 fixed registers. */
20463 if (! reload_completed)
20465 if (rtl
20466 && (CONSTANT_P (rtl)
20467 || (MEM_P (rtl)
20468 && CONSTANT_P (XEXP (rtl, 0)))
20469 || (REG_P (rtl)
20470 && VAR_P (decl)
20471 && TREE_STATIC (decl))))
20473 rtl = targetm.delegitimize_address (rtl);
20474 return rtl;
20476 rtl = NULL_RTX;
20478 else if (TREE_CODE (decl) == PARM_DECL)
20480 if (rtl == NULL_RTX
20481 || is_pseudo_reg (rtl)
20482 || (MEM_P (rtl)
20483 && is_pseudo_reg (XEXP (rtl, 0))
20484 && DECL_INCOMING_RTL (decl)
20485 && MEM_P (DECL_INCOMING_RTL (decl))
20486 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20488 tree declared_type = TREE_TYPE (decl);
20489 tree passed_type = DECL_ARG_TYPE (decl);
20490 machine_mode dmode = TYPE_MODE (declared_type);
20491 machine_mode pmode = TYPE_MODE (passed_type);
20493 /* This decl represents a formal parameter which was optimized out.
20494 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20495 all cases where (rtl == NULL_RTX) just below. */
20496 if (dmode == pmode)
20497 rtl = DECL_INCOMING_RTL (decl);
20498 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20499 && SCALAR_INT_MODE_P (dmode)
20500 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20501 && DECL_INCOMING_RTL (decl))
20503 rtx inc = DECL_INCOMING_RTL (decl);
20504 if (REG_P (inc))
20505 rtl = inc;
20506 else if (MEM_P (inc))
20508 if (BYTES_BIG_ENDIAN)
20509 rtl = adjust_address_nv (inc, dmode,
20510 GET_MODE_SIZE (pmode)
20511 - GET_MODE_SIZE (dmode));
20512 else
20513 rtl = inc;
20518 /* If the parm was passed in registers, but lives on the stack, then
20519 make a big endian correction if the mode of the type of the
20520 parameter is not the same as the mode of the rtl. */
20521 /* ??? This is the same series of checks that are made in dbxout.c before
20522 we reach the big endian correction code there. It isn't clear if all
20523 of these checks are necessary here, but keeping them all is the safe
20524 thing to do. */
20525 else if (MEM_P (rtl)
20526 && XEXP (rtl, 0) != const0_rtx
20527 && ! CONSTANT_P (XEXP (rtl, 0))
20528 /* Not passed in memory. */
20529 && !MEM_P (DECL_INCOMING_RTL (decl))
20530 /* Not passed by invisible reference. */
20531 && (!REG_P (XEXP (rtl, 0))
20532 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20533 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20534 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20535 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20536 #endif
20538 /* Big endian correction check. */
20539 && BYTES_BIG_ENDIAN
20540 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20541 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20542 UNITS_PER_WORD))
20544 machine_mode addr_mode = get_address_mode (rtl);
20545 poly_int64 offset = (UNITS_PER_WORD
20546 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20548 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20549 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20552 else if (VAR_P (decl)
20553 && rtl
20554 && MEM_P (rtl)
20555 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20557 machine_mode addr_mode = get_address_mode (rtl);
20558 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20559 GET_MODE (rtl));
20561 /* If a variable is declared "register" yet is smaller than
20562 a register, then if we store the variable to memory, it
20563 looks like we're storing a register-sized value, when in
20564 fact we are not. We need to adjust the offset of the
20565 storage location to reflect the actual value's bytes,
20566 else gdb will not be able to display it. */
20567 if (maybe_ne (offset, 0))
20568 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20569 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20572 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20573 and will have been substituted directly into all expressions that use it.
20574 C does not have such a concept, but C++ and other languages do. */
20575 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20576 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20578 if (rtl)
20579 rtl = targetm.delegitimize_address (rtl);
20581 /* If we don't look past the constant pool, we risk emitting a
20582 reference to a constant pool entry that isn't referenced from
20583 code, and thus is not emitted. */
20584 if (rtl)
20585 rtl = avoid_constant_pool_reference (rtl);
20587 /* Try harder to get a rtl. If this symbol ends up not being emitted
20588 in the current CU, resolve_addr will remove the expression referencing
20589 it. */
20590 if (rtl == NULL_RTX
20591 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20592 && VAR_P (decl)
20593 && !DECL_EXTERNAL (decl)
20594 && TREE_STATIC (decl)
20595 && DECL_NAME (decl)
20596 && !DECL_HARD_REGISTER (decl)
20597 && DECL_MODE (decl) != VOIDmode)
20599 rtl = make_decl_rtl_for_debug (decl);
20600 if (!MEM_P (rtl)
20601 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20602 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20603 rtl = NULL_RTX;
20606 return rtl;
20609 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20610 returned. If so, the decl for the COMMON block is returned, and the
20611 value is the offset into the common block for the symbol. */
20613 static tree
20614 fortran_common (tree decl, HOST_WIDE_INT *value)
20616 tree val_expr, cvar;
20617 machine_mode mode;
20618 poly_int64 bitsize, bitpos;
20619 tree offset;
20620 HOST_WIDE_INT cbitpos;
20621 int unsignedp, reversep, volatilep = 0;
20623 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20624 it does not have a value (the offset into the common area), or if it
20625 is thread local (as opposed to global) then it isn't common, and shouldn't
20626 be handled as such. */
20627 if (!VAR_P (decl)
20628 || !TREE_STATIC (decl)
20629 || !DECL_HAS_VALUE_EXPR_P (decl)
20630 || !is_fortran ())
20631 return NULL_TREE;
20633 val_expr = DECL_VALUE_EXPR (decl);
20634 if (TREE_CODE (val_expr) != COMPONENT_REF)
20635 return NULL_TREE;
20637 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20638 &unsignedp, &reversep, &volatilep);
20640 if (cvar == NULL_TREE
20641 || !VAR_P (cvar)
20642 || DECL_ARTIFICIAL (cvar)
20643 || !TREE_PUBLIC (cvar)
20644 /* We don't expect to have to cope with variable offsets,
20645 since at present all static data must have a constant size. */
20646 || !bitpos.is_constant (&cbitpos))
20647 return NULL_TREE;
20649 *value = 0;
20650 if (offset != NULL)
20652 if (!tree_fits_shwi_p (offset))
20653 return NULL_TREE;
20654 *value = tree_to_shwi (offset);
20656 if (cbitpos != 0)
20657 *value += cbitpos / BITS_PER_UNIT;
20659 return cvar;
20662 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20663 data attribute for a variable or a parameter. We generate the
20664 DW_AT_const_value attribute only in those cases where the given variable
20665 or parameter does not have a true "location" either in memory or in a
20666 register. This can happen (for example) when a constant is passed as an
20667 actual argument in a call to an inline function. (It's possible that
20668 these things can crop up in other ways also.) Note that one type of
20669 constant value which can be passed into an inlined function is a constant
20670 pointer. This can happen for example if an actual argument in an inlined
20671 function call evaluates to a compile-time constant address.
20673 CACHE_P is true if it is worth caching the location list for DECL,
20674 so that future calls can reuse it rather than regenerate it from scratch.
20675 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20676 since we will need to refer to them each time the function is inlined. */
20678 static bool
20679 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20681 rtx rtl;
20682 dw_loc_list_ref list;
20683 var_loc_list *loc_list;
20684 cached_dw_loc_list *cache;
20686 if (early_dwarf)
20687 return false;
20689 if (TREE_CODE (decl) == ERROR_MARK)
20690 return false;
20692 if (get_AT (die, DW_AT_location)
20693 || get_AT (die, DW_AT_const_value))
20694 return true;
20696 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20697 || TREE_CODE (decl) == RESULT_DECL);
20699 /* Try to get some constant RTL for this decl, and use that as the value of
20700 the location. */
20702 rtl = rtl_for_decl_location (decl);
20703 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20704 && add_const_value_attribute (die, rtl))
20705 return true;
20707 /* See if we have single element location list that is equivalent to
20708 a constant value. That way we are better to use add_const_value_attribute
20709 rather than expanding constant value equivalent. */
20710 loc_list = lookup_decl_loc (decl);
20711 if (loc_list
20712 && loc_list->first
20713 && loc_list->first->next == NULL
20714 && NOTE_P (loc_list->first->loc)
20715 && NOTE_VAR_LOCATION (loc_list->first->loc)
20716 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20718 struct var_loc_node *node;
20720 node = loc_list->first;
20721 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20722 if (GET_CODE (rtl) == EXPR_LIST)
20723 rtl = XEXP (rtl, 0);
20724 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20725 && add_const_value_attribute (die, rtl))
20726 return true;
20728 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20729 list several times. See if we've already cached the contents. */
20730 list = NULL;
20731 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20732 cache_p = false;
20733 if (cache_p)
20735 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20736 if (cache)
20737 list = cache->loc_list;
20739 if (list == NULL)
20741 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20742 NULL);
20743 /* It is usually worth caching this result if the decl is from
20744 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20745 if (cache_p && list && list->dw_loc_next)
20747 cached_dw_loc_list **slot
20748 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20749 DECL_UID (decl),
20750 INSERT);
20751 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20752 cache->decl_id = DECL_UID (decl);
20753 cache->loc_list = list;
20754 *slot = cache;
20757 if (list)
20759 add_AT_location_description (die, DW_AT_location, list);
20760 return true;
20762 /* None of that worked, so it must not really have a location;
20763 try adding a constant value attribute from the DECL_INITIAL. */
20764 return tree_add_const_value_attribute_for_decl (die, decl);
20767 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20768 attribute is the const value T. */
20770 static bool
20771 tree_add_const_value_attribute (dw_die_ref die, tree t)
20773 tree init;
20774 tree type = TREE_TYPE (t);
20775 rtx rtl;
20777 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20778 return false;
20780 init = t;
20781 gcc_assert (!DECL_P (init));
20783 if (TREE_CODE (init) == INTEGER_CST)
20785 if (tree_fits_uhwi_p (init))
20787 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20788 return true;
20790 if (tree_fits_shwi_p (init))
20792 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20793 return true;
20796 /* Generate the RTL even if early_dwarf to force mangling of all refered to
20797 symbols. */
20798 rtl = rtl_for_decl_init (init, type);
20799 if (rtl && !early_dwarf)
20800 return add_const_value_attribute (die, rtl);
20801 /* If the host and target are sane, try harder. */
20802 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20803 && initializer_constant_valid_p (init, type))
20805 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20806 if (size > 0 && (int) size == size)
20808 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20810 if (native_encode_initializer (init, array, size) == size)
20812 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20813 return true;
20815 ggc_free (array);
20818 return false;
20821 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20822 attribute is the const value of T, where T is an integral constant
20823 variable with static storage duration
20824 (so it can't be a PARM_DECL or a RESULT_DECL). */
20826 static bool
20827 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20830 if (!decl
20831 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20832 || (VAR_P (decl) && !TREE_STATIC (decl)))
20833 return false;
20835 if (TREE_READONLY (decl)
20836 && ! TREE_THIS_VOLATILE (decl)
20837 && DECL_INITIAL (decl))
20838 /* OK */;
20839 else
20840 return false;
20842 /* Don't add DW_AT_const_value if abstract origin already has one. */
20843 if (get_AT (var_die, DW_AT_const_value))
20844 return false;
20846 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20849 /* Convert the CFI instructions for the current function into a
20850 location list. This is used for DW_AT_frame_base when we targeting
20851 a dwarf2 consumer that does not support the dwarf3
20852 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20853 expressions. */
20855 static dw_loc_list_ref
20856 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20858 int ix;
20859 dw_fde_ref fde;
20860 dw_loc_list_ref list, *list_tail;
20861 dw_cfi_ref cfi;
20862 dw_cfa_location last_cfa, next_cfa;
20863 const char *start_label, *last_label, *section;
20864 dw_cfa_location remember;
20866 fde = cfun->fde;
20867 gcc_assert (fde != NULL);
20869 section = secname_for_decl (current_function_decl);
20870 list_tail = &list;
20871 list = NULL;
20873 memset (&next_cfa, 0, sizeof (next_cfa));
20874 next_cfa.reg = INVALID_REGNUM;
20875 remember = next_cfa;
20877 start_label = fde->dw_fde_begin;
20879 /* ??? Bald assumption that the CIE opcode list does not contain
20880 advance opcodes. */
20881 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20882 lookup_cfa_1 (cfi, &next_cfa, &remember);
20884 last_cfa = next_cfa;
20885 last_label = start_label;
20887 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20889 /* If the first partition contained no CFI adjustments, the
20890 CIE opcodes apply to the whole first partition. */
20891 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20892 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20893 list_tail =&(*list_tail)->dw_loc_next;
20894 start_label = last_label = fde->dw_fde_second_begin;
20897 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20899 switch (cfi->dw_cfi_opc)
20901 case DW_CFA_set_loc:
20902 case DW_CFA_advance_loc1:
20903 case DW_CFA_advance_loc2:
20904 case DW_CFA_advance_loc4:
20905 if (!cfa_equal_p (&last_cfa, &next_cfa))
20907 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20908 start_label, 0, last_label, 0, section);
20910 list_tail = &(*list_tail)->dw_loc_next;
20911 last_cfa = next_cfa;
20912 start_label = last_label;
20914 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20915 break;
20917 case DW_CFA_advance_loc:
20918 /* The encoding is complex enough that we should never emit this. */
20919 gcc_unreachable ();
20921 default:
20922 lookup_cfa_1 (cfi, &next_cfa, &remember);
20923 break;
20925 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20927 if (!cfa_equal_p (&last_cfa, &next_cfa))
20929 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20930 start_label, 0, last_label, 0, section);
20932 list_tail = &(*list_tail)->dw_loc_next;
20933 last_cfa = next_cfa;
20934 start_label = last_label;
20936 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20937 start_label, 0, fde->dw_fde_end, 0, section);
20938 list_tail = &(*list_tail)->dw_loc_next;
20939 start_label = last_label = fde->dw_fde_second_begin;
20943 if (!cfa_equal_p (&last_cfa, &next_cfa))
20945 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20946 start_label, 0, last_label, 0, section);
20947 list_tail = &(*list_tail)->dw_loc_next;
20948 start_label = last_label;
20951 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20952 start_label, 0,
20953 fde->dw_fde_second_begin
20954 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20955 section);
20957 maybe_gen_llsym (list);
20959 return list;
20962 /* Compute a displacement from the "steady-state frame pointer" to the
20963 frame base (often the same as the CFA), and store it in
20964 frame_pointer_fb_offset. OFFSET is added to the displacement
20965 before the latter is negated. */
20967 static void
20968 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20970 rtx reg, elim;
20972 #ifdef FRAME_POINTER_CFA_OFFSET
20973 reg = frame_pointer_rtx;
20974 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20975 #else
20976 reg = arg_pointer_rtx;
20977 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20978 #endif
20980 elim = (ira_use_lra_p
20981 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20982 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20983 elim = strip_offset_and_add (elim, &offset);
20985 frame_pointer_fb_offset = -offset;
20987 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20988 in which to eliminate. This is because it's stack pointer isn't
20989 directly accessible as a register within the ISA. To work around
20990 this, assume that while we cannot provide a proper value for
20991 frame_pointer_fb_offset, we won't need one either. We can use
20992 hard frame pointer in debug info even if frame pointer isn't used
20993 since hard frame pointer in debug info is encoded with DW_OP_fbreg
20994 which uses the DW_AT_frame_base attribute, not hard frame pointer
20995 directly. */
20996 frame_pointer_fb_offset_valid
20997 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21000 /* Generate a DW_AT_name attribute given some string value to be included as
21001 the value of the attribute. */
21003 void
21004 add_name_attribute (dw_die_ref die, const char *name_string)
21006 if (name_string != NULL && *name_string != 0)
21008 if (demangle_name_func)
21009 name_string = (*demangle_name_func) (name_string);
21011 add_AT_string (die, DW_AT_name, name_string);
21015 /* Generate a DW_AT_name attribute given some string value representing a
21016 file or filepath to be included as value of the attribute. */
21017 static void
21018 add_filename_attribute (dw_die_ref die, const char *name_string)
21020 if (name_string != NULL && *name_string != 0)
21021 add_filepath_AT_string (die, DW_AT_name, name_string);
21024 /* Generate a DW_AT_description attribute given some string value to be included
21025 as the value of the attribute. */
21027 static void
21028 add_desc_attribute (dw_die_ref die, const char *name_string)
21030 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21031 return;
21033 if (name_string == NULL || *name_string == 0)
21034 return;
21036 if (demangle_name_func)
21037 name_string = (*demangle_name_func) (name_string);
21039 add_AT_string (die, DW_AT_description, name_string);
21042 /* Generate a DW_AT_description attribute given some decl to be included
21043 as the value of the attribute. */
21045 static void
21046 add_desc_attribute (dw_die_ref die, tree decl)
21048 tree decl_name;
21050 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21051 return;
21053 if (decl == NULL_TREE || !DECL_P (decl))
21054 return;
21055 decl_name = DECL_NAME (decl);
21057 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21059 const char *name = dwarf2_name (decl, 0);
21060 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21062 else
21064 char *desc = print_generic_expr_to_str (decl);
21065 add_desc_attribute (die, desc);
21066 free (desc);
21070 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21071 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21072 of TYPE accordingly.
21074 ??? This is a temporary measure until after we're able to generate
21075 regular DWARF for the complex Ada type system. */
21077 static void
21078 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21079 dw_die_ref context_die)
21081 tree dtype;
21082 dw_die_ref dtype_die;
21084 if (!lang_hooks.types.descriptive_type)
21085 return;
21087 dtype = lang_hooks.types.descriptive_type (type);
21088 if (!dtype)
21089 return;
21091 dtype_die = lookup_type_die (dtype);
21092 if (!dtype_die)
21094 gen_type_die (dtype, context_die);
21095 dtype_die = lookup_type_die (dtype);
21096 gcc_assert (dtype_die);
21099 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21102 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21104 static const char *
21105 comp_dir_string (void)
21107 const char *wd;
21108 char *wd_plus_sep = NULL;
21109 static const char *cached_wd = NULL;
21111 if (cached_wd != NULL)
21112 return cached_wd;
21114 wd = get_src_pwd ();
21115 if (wd == NULL)
21116 return NULL;
21118 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21120 size_t wdlen = strlen (wd);
21121 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21122 strcpy (wd_plus_sep, wd);
21123 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21124 wd_plus_sep [wdlen + 1] = 0;
21125 wd = wd_plus_sep;
21128 cached_wd = remap_debug_filename (wd);
21130 /* remap_debug_filename can just pass through wd or return a new gc string.
21131 These two types can't be both stored in a GTY(())-tagged string, but since
21132 the cached value lives forever just copy it if needed. */
21133 if (cached_wd != wd)
21135 cached_wd = xstrdup (cached_wd);
21136 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21137 free (wd_plus_sep);
21140 return cached_wd;
21143 /* Generate a DW_AT_comp_dir attribute for DIE. */
21145 static void
21146 add_comp_dir_attribute (dw_die_ref die)
21148 const char * wd = comp_dir_string ();
21149 if (wd != NULL)
21150 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21153 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21154 pointer computation, ...), output a representation for that bound according
21155 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21156 loc_list_from_tree for the meaning of CONTEXT. */
21158 static void
21159 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21160 int forms, struct loc_descr_context *context)
21162 dw_die_ref context_die, decl_die = NULL;
21163 dw_loc_list_ref list;
21164 bool strip_conversions = true;
21165 bool placeholder_seen = false;
21167 while (strip_conversions)
21168 switch (TREE_CODE (value))
21170 case ERROR_MARK:
21171 case SAVE_EXPR:
21172 return;
21174 CASE_CONVERT:
21175 case VIEW_CONVERT_EXPR:
21176 value = TREE_OPERAND (value, 0);
21177 break;
21179 default:
21180 strip_conversions = false;
21181 break;
21184 /* If possible and permitted, output the attribute as a constant. */
21185 if ((forms & dw_scalar_form_constant) != 0
21186 && TREE_CODE (value) == INTEGER_CST)
21188 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21190 /* If HOST_WIDE_INT is big enough then represent the bound as
21191 a constant value. We need to choose a form based on
21192 whether the type is signed or unsigned. We cannot just
21193 call add_AT_unsigned if the value itself is positive
21194 (add_AT_unsigned might add the unsigned value encoded as
21195 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21196 bounds type and then sign extend any unsigned values found
21197 for signed types. This is needed only for
21198 DW_AT_{lower,upper}_bound, since for most other attributes,
21199 consumers will treat DW_FORM_data[1248] as unsigned values,
21200 regardless of the underlying type. */
21201 if (prec <= HOST_BITS_PER_WIDE_INT
21202 || tree_fits_uhwi_p (value))
21204 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21205 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21206 else
21207 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21209 else if (dwarf_version >= 5
21210 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21211 /* Otherwise represent the bound as an unsigned value with
21212 the precision of its type. The precision and signedness
21213 of the type will be necessary to re-interpret it
21214 unambiguously. */
21215 add_AT_wide (die, attr, wi::to_wide (value));
21216 else
21218 rtx v = immed_wide_int_const (wi::to_wide (value),
21219 TYPE_MODE (TREE_TYPE (value)));
21220 dw_loc_descr_ref loc
21221 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21222 VAR_INIT_STATUS_INITIALIZED);
21223 if (loc)
21224 add_AT_loc (die, attr, loc);
21226 return;
21229 /* Otherwise, if it's possible and permitted too, output a reference to
21230 another DIE. */
21231 if ((forms & dw_scalar_form_reference) != 0)
21233 tree decl = NULL_TREE;
21235 /* Some type attributes reference an outer type. For instance, the upper
21236 bound of an array may reference an embedding record (this happens in
21237 Ada). */
21238 if (TREE_CODE (value) == COMPONENT_REF
21239 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21240 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21241 decl = TREE_OPERAND (value, 1);
21243 else if (VAR_P (value)
21244 || TREE_CODE (value) == PARM_DECL
21245 || TREE_CODE (value) == RESULT_DECL)
21246 decl = value;
21248 if (decl != NULL_TREE)
21250 decl_die = lookup_decl_die (decl);
21252 /* ??? Can this happen, or should the variable have been bound
21253 first? Probably it can, since I imagine that we try to create
21254 the types of parameters in the order in which they exist in
21255 the list, and won't have created a forward reference to a
21256 later parameter. */
21257 if (decl_die != NULL)
21259 if (get_AT (decl_die, DW_AT_location)
21260 || get_AT (decl_die, DW_AT_data_member_location)
21261 || get_AT (decl_die, DW_AT_data_bit_offset)
21262 || get_AT (decl_die, DW_AT_const_value))
21264 add_AT_die_ref (die, attr, decl_die);
21265 return;
21271 /* Last chance: try to create a stack operation procedure to evaluate the
21272 value. Do nothing if even that is not possible or permitted. */
21273 if ((forms & dw_scalar_form_exprloc) == 0)
21274 return;
21276 list = loc_list_from_tree (value, 2, context);
21277 if (context && context->placeholder_arg)
21279 placeholder_seen = context->placeholder_seen;
21280 context->placeholder_seen = false;
21282 if (list == NULL || single_element_loc_list_p (list))
21284 /* If this attribute is not a reference nor constant, it is
21285 a DWARF expression rather than location description. For that
21286 loc_list_from_tree (value, 0, &context) is needed. */
21287 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21288 if (list2 && single_element_loc_list_p (list2))
21290 if (placeholder_seen)
21292 struct dwarf_procedure_info dpi;
21293 dpi.fndecl = NULL_TREE;
21294 dpi.args_count = 1;
21295 if (!resolve_args_picking (list2->expr, 1, &dpi))
21296 return;
21298 add_AT_loc (die, attr, list2->expr);
21299 return;
21303 /* If that failed to give a single element location list, fall back to
21304 outputting this as a reference... still if permitted. */
21305 if (list == NULL
21306 || (forms & dw_scalar_form_reference) == 0
21307 || placeholder_seen)
21308 return;
21310 if (!decl_die)
21312 if (current_function_decl == 0)
21313 context_die = comp_unit_die ();
21314 else
21315 context_die = lookup_decl_die (current_function_decl);
21317 decl_die = new_die (DW_TAG_variable, context_die, value);
21318 add_AT_flag (decl_die, DW_AT_artificial, 1);
21319 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21320 context_die);
21323 add_AT_location_description (decl_die, DW_AT_location, list);
21324 add_AT_die_ref (die, attr, decl_die);
21327 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21328 default. */
21330 static int
21331 lower_bound_default (void)
21333 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21335 case DW_LANG_C:
21336 case DW_LANG_C89:
21337 case DW_LANG_C99:
21338 case DW_LANG_C11:
21339 case DW_LANG_C_plus_plus:
21340 case DW_LANG_C_plus_plus_11:
21341 case DW_LANG_C_plus_plus_14:
21342 case DW_LANG_ObjC:
21343 case DW_LANG_ObjC_plus_plus:
21344 return 0;
21345 case DW_LANG_Fortran77:
21346 case DW_LANG_Fortran90:
21347 case DW_LANG_Fortran95:
21348 case DW_LANG_Fortran03:
21349 case DW_LANG_Fortran08:
21350 return 1;
21351 case DW_LANG_UPC:
21352 case DW_LANG_D:
21353 case DW_LANG_Python:
21354 return dwarf_version >= 4 ? 0 : -1;
21355 case DW_LANG_Ada95:
21356 case DW_LANG_Ada83:
21357 case DW_LANG_Cobol74:
21358 case DW_LANG_Cobol85:
21359 case DW_LANG_Modula2:
21360 case DW_LANG_PLI:
21361 return dwarf_version >= 4 ? 1 : -1;
21362 default:
21363 return -1;
21367 /* Given a tree node describing an array bound (either lower or upper) output
21368 a representation for that bound. */
21370 static void
21371 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21372 tree bound, struct loc_descr_context *context)
21374 int dflt;
21376 while (1)
21377 switch (TREE_CODE (bound))
21379 /* Strip all conversions. */
21380 CASE_CONVERT:
21381 case VIEW_CONVERT_EXPR:
21382 bound = TREE_OPERAND (bound, 0);
21383 break;
21385 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21386 are even omitted when they are the default. */
21387 case INTEGER_CST:
21388 /* If the value for this bound is the default one, we can even omit the
21389 attribute. */
21390 if (bound_attr == DW_AT_lower_bound
21391 && tree_fits_shwi_p (bound)
21392 && (dflt = lower_bound_default ()) != -1
21393 && tree_to_shwi (bound) == dflt)
21394 return;
21396 /* FALLTHRU */
21398 default:
21399 /* Let GNAT encodings do the magic for self-referential bounds. */
21400 if (is_ada ()
21401 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21402 && contains_placeholder_p (bound))
21403 return;
21405 add_scalar_info (subrange_die, bound_attr, bound,
21406 dw_scalar_form_constant
21407 | dw_scalar_form_exprloc
21408 | dw_scalar_form_reference,
21409 context);
21410 return;
21414 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21415 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21417 This function reuses previously set type and bound information if
21418 available. */
21420 static void
21421 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21423 dw_die_ref child = type_die->die_child;
21424 struct array_descr_info info;
21425 int dimension_number;
21427 if (lang_hooks.types.get_array_descr_info)
21429 memset (&info, 0, sizeof (info));
21430 if (lang_hooks.types.get_array_descr_info (type, &info))
21431 /* Fortran sometimes emits array types with no dimension. */
21432 gcc_assert (info.ndimensions >= 0
21433 && info.ndimensions
21434 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21436 else
21437 info.ndimensions = 0;
21439 for (dimension_number = 0;
21440 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21441 type = TREE_TYPE (type), dimension_number++)
21443 tree domain = TYPE_DOMAIN (type);
21445 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21446 break;
21448 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21449 and (in GNU C only) variable bounds. Handle all three forms
21450 here. */
21452 /* Find and reuse a previously generated DW_TAG_subrange_type if
21453 available.
21455 For multi-dimensional arrays, as we iterate through the
21456 various dimensions in the enclosing for loop above, we also
21457 iterate through the DIE children and pick at each
21458 DW_TAG_subrange_type previously generated (if available).
21459 Each child DW_TAG_subrange_type DIE describes the range of
21460 the current dimension. At this point we should have as many
21461 DW_TAG_subrange_type's as we have dimensions in the
21462 array. */
21463 dw_die_ref subrange_die = NULL;
21464 if (child)
21465 while (1)
21467 child = child->die_sib;
21468 if (child->die_tag == DW_TAG_subrange_type)
21469 subrange_die = child;
21470 if (child == type_die->die_child)
21472 /* If we wrapped around, stop looking next time. */
21473 child = NULL;
21474 break;
21476 if (child->die_tag == DW_TAG_subrange_type)
21477 break;
21479 if (!subrange_die)
21480 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21482 if (domain)
21484 /* We have an array type with specified bounds. */
21485 tree lower = TYPE_MIN_VALUE (domain);
21486 tree upper = TYPE_MAX_VALUE (domain);
21487 tree index_type = TREE_TYPE (domain);
21489 if (dimension_number <= info.ndimensions - 1)
21491 lower = info.dimen[dimension_number].lower_bound;
21492 upper = info.dimen[dimension_number].upper_bound;
21493 index_type = info.dimen[dimension_number].bounds_type;
21496 /* Define the index type. */
21497 if (index_type && !get_AT (subrange_die, DW_AT_type))
21498 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21499 false, type_die);
21501 /* ??? If upper is NULL, the array has unspecified length,
21502 but it does have a lower bound. This happens with Fortran
21503 dimension arr(N:*)
21504 Since the debugger is definitely going to need to know N
21505 to produce useful results, go ahead and output the lower
21506 bound solo, and hope the debugger can cope. */
21508 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21509 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21511 if (!get_AT (subrange_die, DW_AT_upper_bound)
21512 && !get_AT (subrange_die, DW_AT_count))
21514 if (upper)
21515 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21516 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21517 /* Zero-length array. */
21518 add_bound_info (subrange_die, DW_AT_count,
21519 build_int_cst (TREE_TYPE (lower), 0), NULL);
21523 /* Otherwise we have an array type with an unspecified length. The
21524 DWARF-2 spec does not say how to handle this; let's just leave out the
21525 bounds. */
21529 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21531 static void
21532 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21534 dw_die_ref decl_die;
21535 HOST_WIDE_INT size;
21537 switch (TREE_CODE (tree_node))
21539 case ERROR_MARK:
21540 size = 0;
21541 break;
21542 case ENUMERAL_TYPE:
21543 case RECORD_TYPE:
21544 case UNION_TYPE:
21545 case QUAL_UNION_TYPE:
21546 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21547 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21549 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21550 return;
21552 size = int_size_in_bytes (tree_node);
21553 break;
21554 case FIELD_DECL:
21555 /* For a data member of a struct or union, the DW_AT_byte_size is
21556 generally given as the number of bytes normally allocated for an
21557 object of the *declared* type of the member itself. This is true
21558 even for bit-fields. */
21559 size = int_size_in_bytes (field_type (tree_node));
21560 break;
21561 default:
21562 gcc_unreachable ();
21565 /* Note that `size' might be -1 when we get to this point. If it is, that
21566 indicates that the byte size of the entity in question is variable. */
21567 if (size >= 0)
21568 add_AT_unsigned (die, DW_AT_byte_size, size);
21570 /* Support for dynamically-sized objects was introduced in DWARF3. */
21571 else if (TYPE_P (tree_node)
21572 && (dwarf_version >= 3 || !dwarf_strict)
21573 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21575 struct loc_descr_context ctx = {
21576 const_cast<tree> (tree_node), /* context_type */
21577 NULL_TREE, /* base_decl */
21578 NULL, /* dpi */
21579 false, /* placeholder_arg */
21580 false, /* placeholder_seen */
21581 false /* strict_signedness */
21584 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21585 add_scalar_info (die, DW_AT_byte_size, tree_size,
21586 dw_scalar_form_constant
21587 | dw_scalar_form_exprloc
21588 | dw_scalar_form_reference,
21589 &ctx);
21593 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21594 alignment. */
21596 static void
21597 add_alignment_attribute (dw_die_ref die, tree tree_node)
21599 if (dwarf_version < 5 && dwarf_strict)
21600 return;
21602 unsigned align;
21604 if (DECL_P (tree_node))
21606 if (!DECL_USER_ALIGN (tree_node))
21607 return;
21609 align = DECL_ALIGN_UNIT (tree_node);
21611 else if (TYPE_P (tree_node))
21613 if (!TYPE_USER_ALIGN (tree_node))
21614 return;
21616 align = TYPE_ALIGN_UNIT (tree_node);
21618 else
21619 gcc_unreachable ();
21621 add_AT_unsigned (die, DW_AT_alignment, align);
21624 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21625 which specifies the distance in bits from the highest order bit of the
21626 "containing object" for the bit-field to the highest order bit of the
21627 bit-field itself.
21629 For any given bit-field, the "containing object" is a hypothetical object
21630 (of some integral or enum type) within which the given bit-field lives. The
21631 type of this hypothetical "containing object" is always the same as the
21632 declared type of the individual bit-field itself. The determination of the
21633 exact location of the "containing object" for a bit-field is rather
21634 complicated. It's handled by the `field_byte_offset' function (above).
21636 Note that it is the size (in bytes) of the hypothetical "containing object"
21637 which will be given in the DW_AT_byte_size attribute for this bit-field.
21638 (See `byte_size_attribute' above). */
21640 static inline void
21641 add_bit_offset_attribute (dw_die_ref die, tree decl)
21643 HOST_WIDE_INT object_offset_in_bytes;
21644 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21645 HOST_WIDE_INT bitpos_int;
21646 HOST_WIDE_INT highest_order_object_bit_offset;
21647 HOST_WIDE_INT highest_order_field_bit_offset;
21648 HOST_WIDE_INT bit_offset;
21650 /* The containing object is within the DECL_CONTEXT. */
21651 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21653 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21655 /* Must be a field and a bit field. */
21656 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21658 /* We can't yet handle bit-fields whose offsets are variable, so if we
21659 encounter such things, just return without generating any attribute
21660 whatsoever. Likewise for variable or too large size. */
21661 if (! tree_fits_shwi_p (bit_position (decl))
21662 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21663 return;
21665 bitpos_int = int_bit_position (decl);
21667 /* Note that the bit offset is always the distance (in bits) from the
21668 highest-order bit of the "containing object" to the highest-order bit of
21669 the bit-field itself. Since the "high-order end" of any object or field
21670 is different on big-endian and little-endian machines, the computation
21671 below must take account of these differences. */
21672 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21673 highest_order_field_bit_offset = bitpos_int;
21675 if (! BYTES_BIG_ENDIAN)
21677 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21678 highest_order_object_bit_offset +=
21679 simple_type_size_in_bits (original_type);
21682 bit_offset
21683 = (! BYTES_BIG_ENDIAN
21684 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21685 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21687 if (bit_offset < 0)
21688 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21689 else
21690 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21693 /* For a FIELD_DECL node which represents a bit field, output an attribute
21694 which specifies the length in bits of the given field. */
21696 static inline void
21697 add_bit_size_attribute (dw_die_ref die, tree decl)
21699 /* Must be a field and a bit field. */
21700 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21701 && DECL_BIT_FIELD_TYPE (decl));
21703 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21704 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21707 /* If the compiled language is ANSI C, then add a 'prototyped'
21708 attribute, if arg types are given for the parameters of a function. */
21710 static inline void
21711 add_prototyped_attribute (dw_die_ref die, tree func_type)
21713 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21715 case DW_LANG_C:
21716 case DW_LANG_C89:
21717 case DW_LANG_C99:
21718 case DW_LANG_C11:
21719 case DW_LANG_ObjC:
21720 if (prototype_p (func_type))
21721 add_AT_flag (die, DW_AT_prototyped, 1);
21722 break;
21723 default:
21724 break;
21728 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21729 by looking in the type declaration, the object declaration equate table or
21730 the block mapping. */
21732 static inline void
21733 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21735 dw_die_ref origin_die = NULL;
21737 /* For late LTO debug output we want to refer directly to the abstract
21738 DIE in the early debug rather to the possibly existing concrete
21739 instance and avoid creating that just for this purpose. */
21740 sym_off_pair *desc;
21741 if (in_lto_p
21742 && external_die_map
21743 && (desc = external_die_map->get (origin)))
21745 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21746 desc->sym, desc->off);
21747 return;
21750 if (DECL_P (origin))
21751 origin_die = lookup_decl_die (origin);
21752 else if (TYPE_P (origin))
21753 origin_die = lookup_type_die (origin);
21754 else if (TREE_CODE (origin) == BLOCK)
21755 origin_die = lookup_block_die (origin);
21757 /* XXX: Functions that are never lowered don't always have correct block
21758 trees (in the case of java, they simply have no block tree, in some other
21759 languages). For these functions, there is nothing we can really do to
21760 output correct debug info for inlined functions in all cases. Rather
21761 than die, we'll just produce deficient debug info now, in that we will
21762 have variables without a proper abstract origin. In the future, when all
21763 functions are lowered, we should re-add a gcc_assert (origin_die)
21764 here. */
21766 if (origin_die)
21768 dw_attr_node *a;
21769 /* Like above, if we already created a concrete instance DIE
21770 do not use that for the abstract origin but the early DIE
21771 if present. */
21772 if (in_lto_p
21773 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
21774 origin_die = AT_ref (a);
21775 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21779 /* We do not currently support the pure_virtual attribute. */
21781 static inline void
21782 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21784 if (DECL_VINDEX (func_decl))
21786 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21788 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21789 add_AT_loc (die, DW_AT_vtable_elem_location,
21790 new_loc_descr (DW_OP_constu,
21791 tree_to_shwi (DECL_VINDEX (func_decl)),
21792 0));
21794 /* GNU extension: Record what type this method came from originally. */
21795 if (debug_info_level > DINFO_LEVEL_TERSE
21796 && DECL_CONTEXT (func_decl))
21797 add_AT_die_ref (die, DW_AT_containing_type,
21798 lookup_type_die (DECL_CONTEXT (func_decl)));
21802 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21803 given decl. This used to be a vendor extension until after DWARF 4
21804 standardized it. */
21806 static void
21807 add_linkage_attr (dw_die_ref die, tree decl)
21809 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21811 /* Mimic what assemble_name_raw does with a leading '*'. */
21812 if (name[0] == '*')
21813 name = &name[1];
21815 if (dwarf_version >= 4)
21816 add_AT_string (die, DW_AT_linkage_name, name);
21817 else
21818 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21821 /* Add source coordinate attributes for the given decl. */
21823 static void
21824 add_src_coords_attributes (dw_die_ref die, tree decl)
21826 expanded_location s;
21828 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21829 return;
21830 s = expand_location (DECL_SOURCE_LOCATION (decl));
21831 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21832 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21833 if (debug_column_info && s.column)
21834 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21837 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21839 static void
21840 add_linkage_name_raw (dw_die_ref die, tree decl)
21842 /* Defer until we have an assembler name set. */
21843 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21845 limbo_die_node *asm_name;
21847 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21848 asm_name->die = die;
21849 asm_name->created_for = decl;
21850 asm_name->next = deferred_asm_name;
21851 deferred_asm_name = asm_name;
21853 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21854 add_linkage_attr (die, decl);
21857 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21859 static void
21860 add_linkage_name (dw_die_ref die, tree decl)
21862 if (debug_info_level > DINFO_LEVEL_NONE
21863 && VAR_OR_FUNCTION_DECL_P (decl)
21864 && TREE_PUBLIC (decl)
21865 && !(VAR_P (decl) && DECL_REGISTER (decl))
21866 && die->die_tag != DW_TAG_member)
21867 add_linkage_name_raw (die, decl);
21870 /* Add a DW_AT_name attribute and source coordinate attribute for the
21871 given decl, but only if it actually has a name. */
21873 static void
21874 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21875 bool no_linkage_name)
21877 tree decl_name;
21879 decl_name = DECL_NAME (decl);
21880 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21882 const char *name = dwarf2_name (decl, 0);
21883 if (name)
21884 add_name_attribute (die, name);
21885 else
21886 add_desc_attribute (die, decl);
21888 if (! DECL_ARTIFICIAL (decl))
21889 add_src_coords_attributes (die, decl);
21891 if (!no_linkage_name)
21892 add_linkage_name (die, decl);
21894 else
21895 add_desc_attribute (die, decl);
21897 #ifdef VMS_DEBUGGING_INFO
21898 /* Get the function's name, as described by its RTL. This may be different
21899 from the DECL_NAME name used in the source file. */
21900 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21902 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21903 XEXP (DECL_RTL (decl), 0), false);
21904 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21906 #endif /* VMS_DEBUGGING_INFO */
21909 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21911 static void
21912 add_discr_value (dw_die_ref die, dw_discr_value *value)
21914 dw_attr_node attr;
21916 attr.dw_attr = DW_AT_discr_value;
21917 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21918 attr.dw_attr_val.val_entry = NULL;
21919 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21920 if (value->pos)
21921 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21922 else
21923 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21924 add_dwarf_attr (die, &attr);
21927 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21929 static void
21930 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21932 dw_attr_node attr;
21934 attr.dw_attr = DW_AT_discr_list;
21935 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21936 attr.dw_attr_val.val_entry = NULL;
21937 attr.dw_attr_val.v.val_discr_list = discr_list;
21938 add_dwarf_attr (die, &attr);
21941 static inline dw_discr_list_ref
21942 AT_discr_list (dw_attr_node *attr)
21944 return attr->dw_attr_val.v.val_discr_list;
21947 #ifdef VMS_DEBUGGING_INFO
21948 /* Output the debug main pointer die for VMS */
21950 void
21951 dwarf2out_vms_debug_main_pointer (void)
21953 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21954 dw_die_ref die;
21956 /* Allocate the VMS debug main subprogram die. */
21957 die = new_die_raw (DW_TAG_subprogram);
21958 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21959 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21960 current_function_funcdef_no);
21961 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21963 /* Make it the first child of comp_unit_die (). */
21964 die->die_parent = comp_unit_die ();
21965 if (comp_unit_die ()->die_child)
21967 die->die_sib = comp_unit_die ()->die_child->die_sib;
21968 comp_unit_die ()->die_child->die_sib = die;
21970 else
21972 die->die_sib = die;
21973 comp_unit_die ()->die_child = die;
21976 #endif /* VMS_DEBUGGING_INFO */
21978 /* walk_tree helper function for uses_local_type, below. */
21980 static tree
21981 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21983 if (!TYPE_P (*tp))
21984 *walk_subtrees = 0;
21985 else
21987 tree name = TYPE_NAME (*tp);
21988 if (name && DECL_P (name) && decl_function_context (name))
21989 return *tp;
21991 return NULL_TREE;
21994 /* If TYPE involves a function-local type (including a local typedef to a
21995 non-local type), returns that type; otherwise returns NULL_TREE. */
21997 static tree
21998 uses_local_type (tree type)
22000 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22001 return used;
22004 /* Return the DIE for the scope that immediately contains this type.
22005 Non-named types that do not involve a function-local type get global
22006 scope. Named types nested in namespaces or other types get their
22007 containing scope. All other types (i.e. function-local named types) get
22008 the current active scope. */
22010 static dw_die_ref
22011 scope_die_for (tree t, dw_die_ref context_die)
22013 dw_die_ref scope_die = NULL;
22014 tree containing_scope;
22016 /* Non-types always go in the current scope. */
22017 gcc_assert (TYPE_P (t));
22019 /* Use the scope of the typedef, rather than the scope of the type
22020 it refers to. */
22021 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22022 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22023 else
22024 containing_scope = TYPE_CONTEXT (t);
22026 /* Use the containing namespace if there is one. */
22027 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22029 if (context_die == lookup_decl_die (containing_scope))
22030 /* OK */;
22031 else if (debug_info_level > DINFO_LEVEL_TERSE)
22032 context_die = get_context_die (containing_scope);
22033 else
22034 containing_scope = NULL_TREE;
22037 /* Ignore function type "scopes" from the C frontend. They mean that
22038 a tagged type is local to a parmlist of a function declarator, but
22039 that isn't useful to DWARF. */
22040 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22041 containing_scope = NULL_TREE;
22043 if (SCOPE_FILE_SCOPE_P (containing_scope))
22045 /* If T uses a local type keep it local as well, to avoid references
22046 to function-local DIEs from outside the function. */
22047 if (current_function_decl && uses_local_type (t))
22048 scope_die = context_die;
22049 else
22050 scope_die = comp_unit_die ();
22052 else if (TYPE_P (containing_scope))
22054 /* For types, we can just look up the appropriate DIE. */
22055 if (debug_info_level > DINFO_LEVEL_TERSE)
22056 scope_die = get_context_die (containing_scope);
22057 else
22059 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22060 if (scope_die == NULL)
22061 scope_die = comp_unit_die ();
22064 else
22065 scope_die = context_die;
22067 return scope_die;
22070 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
22072 static inline int
22073 local_scope_p (dw_die_ref context_die)
22075 for (; context_die; context_die = context_die->die_parent)
22076 if (context_die->die_tag == DW_TAG_inlined_subroutine
22077 || context_die->die_tag == DW_TAG_subprogram)
22078 return 1;
22080 return 0;
22083 /* Returns nonzero if CONTEXT_DIE is a class. */
22085 static inline int
22086 class_scope_p (dw_die_ref context_die)
22088 return (context_die
22089 && (context_die->die_tag == DW_TAG_structure_type
22090 || context_die->die_tag == DW_TAG_class_type
22091 || context_die->die_tag == DW_TAG_interface_type
22092 || context_die->die_tag == DW_TAG_union_type));
22095 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
22096 whether or not to treat a DIE in this context as a declaration. */
22098 static inline int
22099 class_or_namespace_scope_p (dw_die_ref context_die)
22101 return (class_scope_p (context_die)
22102 || (context_die && context_die->die_tag == DW_TAG_namespace));
22105 /* Many forms of DIEs require a "type description" attribute. This
22106 routine locates the proper "type descriptor" die for the type given
22107 by 'type' plus any additional qualifiers given by 'cv_quals', and
22108 adds a DW_AT_type attribute below the given die. */
22110 static void
22111 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22112 bool reverse, dw_die_ref context_die)
22114 enum tree_code code = TREE_CODE (type);
22115 dw_die_ref type_die = NULL;
22117 if (debug_info_level <= DINFO_LEVEL_TERSE)
22118 return;
22120 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22121 or fixed-point type, use the inner type. This is because we have no
22122 support for unnamed types in base_type_die. This can happen if this is
22123 an Ada subrange type. Correct solution is emit a subrange type die. */
22124 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22125 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22126 type = TREE_TYPE (type), code = TREE_CODE (type);
22128 if (code == ERROR_MARK
22129 /* Handle a special case. For functions whose return type is void, we
22130 generate *no* type attribute. (Note that no object may have type
22131 `void', so this only applies to function return types). */
22132 || code == VOID_TYPE)
22133 return;
22135 type_die = modified_type_die (type,
22136 cv_quals | TYPE_QUALS (type),
22137 reverse,
22138 context_die);
22140 if (type_die != NULL)
22141 add_AT_die_ref (object_die, DW_AT_type, type_die);
22144 /* Given an object die, add the calling convention attribute for the
22145 function call type. */
22146 static void
22147 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22149 enum dwarf_calling_convention value = DW_CC_normal;
22151 value = ((enum dwarf_calling_convention)
22152 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22154 if (is_fortran ()
22155 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22157 /* DWARF 2 doesn't provide a way to identify a program's source-level
22158 entry point. DW_AT_calling_convention attributes are only meant
22159 to describe functions' calling conventions. However, lacking a
22160 better way to signal the Fortran main program, we used this for
22161 a long time, following existing custom. Now, DWARF 4 has
22162 DW_AT_main_subprogram, which we add below, but some tools still
22163 rely on the old way, which we thus keep. */
22164 value = DW_CC_program;
22166 if (dwarf_version >= 4 || !dwarf_strict)
22167 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22170 /* Only add the attribute if the backend requests it, and
22171 is not DW_CC_normal. */
22172 if (value && (value != DW_CC_normal))
22173 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22176 /* Given a tree pointer to a struct, class, union, or enum type node, return
22177 a pointer to the (string) tag name for the given type, or zero if the type
22178 was declared without a tag. */
22180 static const char *
22181 type_tag (const_tree type)
22183 const char *name = 0;
22185 if (TYPE_NAME (type) != 0)
22187 tree t = 0;
22189 /* Find the IDENTIFIER_NODE for the type name. */
22190 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22191 && !TYPE_NAMELESS (type))
22192 t = TYPE_NAME (type);
22194 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22195 a TYPE_DECL node, regardless of whether or not a `typedef' was
22196 involved. */
22197 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22198 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22200 /* We want to be extra verbose. Don't call dwarf_name if
22201 DECL_NAME isn't set. The default hook for decl_printable_name
22202 doesn't like that, and in this context it's correct to return
22203 0, instead of "<anonymous>" or the like. */
22204 if (DECL_NAME (TYPE_NAME (type))
22205 && !DECL_NAMELESS (TYPE_NAME (type)))
22206 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22209 /* Now get the name as a string, or invent one. */
22210 if (!name && t != 0)
22211 name = IDENTIFIER_POINTER (t);
22214 return (name == 0 || *name == '\0') ? 0 : name;
22217 /* Return the type associated with a data member, make a special check
22218 for bit field types. */
22220 static inline tree
22221 member_declared_type (const_tree member)
22223 return (DECL_BIT_FIELD_TYPE (member)
22224 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22227 /* Get the decl's label, as described by its RTL. This may be different
22228 from the DECL_NAME name used in the source file. */
22230 #if 0
22231 static const char *
22232 decl_start_label (tree decl)
22234 rtx x;
22235 const char *fnname;
22237 x = DECL_RTL (decl);
22238 gcc_assert (MEM_P (x));
22240 x = XEXP (x, 0);
22241 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22243 fnname = XSTR (x, 0);
22244 return fnname;
22246 #endif
22248 /* For variable-length arrays that have been previously generated, but
22249 may be incomplete due to missing subscript info, fill the subscript
22250 info. Return TRUE if this is one of those cases. */
22252 static bool
22253 fill_variable_array_bounds (tree type)
22255 if (TREE_ASM_WRITTEN (type)
22256 && TREE_CODE (type) == ARRAY_TYPE
22257 && variably_modified_type_p (type, NULL))
22259 dw_die_ref array_die = lookup_type_die (type);
22260 if (!array_die)
22261 return false;
22262 add_subscript_info (array_die, type, !is_ada ());
22263 return true;
22265 return false;
22268 /* These routines generate the internal representation of the DIE's for
22269 the compilation unit. Debugging information is collected by walking
22270 the declaration trees passed in from dwarf2out_decl(). */
22272 static void
22273 gen_array_type_die (tree type, dw_die_ref context_die)
22275 dw_die_ref array_die;
22277 /* GNU compilers represent multidimensional array types as sequences of one
22278 dimensional array types whose element types are themselves array types.
22279 We sometimes squish that down to a single array_type DIE with multiple
22280 subscripts in the Dwarf debugging info. The draft Dwarf specification
22281 say that we are allowed to do this kind of compression in C, because
22282 there is no difference between an array of arrays and a multidimensional
22283 array. We don't do this for Ada to remain as close as possible to the
22284 actual representation, which is especially important against the language
22285 flexibilty wrt arrays of variable size. */
22287 bool collapse_nested_arrays = !is_ada ();
22289 if (fill_variable_array_bounds (type))
22290 return;
22292 dw_die_ref scope_die = scope_die_for (type, context_die);
22293 tree element_type;
22295 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22296 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22297 if (TREE_CODE (type) == ARRAY_TYPE
22298 && TYPE_STRING_FLAG (type)
22299 && is_fortran ()
22300 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22302 HOST_WIDE_INT size;
22304 array_die = new_die (DW_TAG_string_type, scope_die, type);
22305 add_name_attribute (array_die, type_tag (type));
22306 equate_type_number_to_die (type, array_die);
22307 size = int_size_in_bytes (type);
22308 if (size >= 0)
22309 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22310 /* ??? We can't annotate types late, but for LTO we may not
22311 generate a location early either (gfortran.dg/save_6.f90). */
22312 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22313 && TYPE_DOMAIN (type) != NULL_TREE
22314 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22316 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22317 tree rszdecl = szdecl;
22319 size = int_size_in_bytes (TREE_TYPE (szdecl));
22320 if (!DECL_P (szdecl))
22322 if (TREE_CODE (szdecl) == INDIRECT_REF
22323 && DECL_P (TREE_OPERAND (szdecl, 0)))
22325 rszdecl = TREE_OPERAND (szdecl, 0);
22326 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22327 != DWARF2_ADDR_SIZE)
22328 size = 0;
22330 else
22331 size = 0;
22333 if (size > 0)
22335 dw_loc_list_ref loc
22336 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22337 NULL);
22338 if (loc)
22340 add_AT_location_description (array_die, DW_AT_string_length,
22341 loc);
22342 if (size != DWARF2_ADDR_SIZE)
22343 add_AT_unsigned (array_die, dwarf_version >= 5
22344 ? DW_AT_string_length_byte_size
22345 : DW_AT_byte_size, size);
22349 return;
22352 array_die = new_die (DW_TAG_array_type, scope_die, type);
22353 add_name_attribute (array_die, type_tag (type));
22354 equate_type_number_to_die (type, array_die);
22356 if (TREE_CODE (type) == VECTOR_TYPE)
22357 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22359 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22360 if (is_fortran ()
22361 && TREE_CODE (type) == ARRAY_TYPE
22362 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22363 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22364 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22366 #if 0
22367 /* We default the array ordering. Debuggers will probably do the right
22368 things even if DW_AT_ordering is not present. It's not even an issue
22369 until we start to get into multidimensional arrays anyway. If a debugger
22370 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22371 then we'll have to put the DW_AT_ordering attribute back in. (But if
22372 and when we find out that we need to put these in, we will only do so
22373 for multidimensional arrays. */
22374 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22375 #endif
22377 if (TREE_CODE (type) == VECTOR_TYPE)
22379 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
22380 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22381 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
22382 add_bound_info (subrange_die, DW_AT_upper_bound,
22383 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22385 else
22386 add_subscript_info (array_die, type, collapse_nested_arrays);
22388 /* Add representation of the type of the elements of this array type and
22389 emit the corresponding DIE if we haven't done it already. */
22390 element_type = TREE_TYPE (type);
22391 if (collapse_nested_arrays)
22392 while (TREE_CODE (element_type) == ARRAY_TYPE)
22394 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22395 break;
22396 element_type = TREE_TYPE (element_type);
22399 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22400 TREE_CODE (type) == ARRAY_TYPE
22401 && TYPE_REVERSE_STORAGE_ORDER (type),
22402 context_die);
22404 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22405 if (TYPE_ARTIFICIAL (type))
22406 add_AT_flag (array_die, DW_AT_artificial, 1);
22408 if (get_AT (array_die, DW_AT_name))
22409 add_pubtype (type, array_die);
22411 add_alignment_attribute (array_die, type);
22414 /* This routine generates DIE for array with hidden descriptor, details
22415 are filled into *info by a langhook. */
22417 static void
22418 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22419 dw_die_ref context_die)
22421 const dw_die_ref scope_die = scope_die_for (type, context_die);
22422 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22423 struct loc_descr_context context = {
22424 type, /* context_type */
22425 info->base_decl, /* base_decl */
22426 NULL, /* dpi */
22427 false, /* placeholder_arg */
22428 false, /* placeholder_seen */
22429 false /* strict_signedness */
22431 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22432 int dim;
22434 add_name_attribute (array_die, type_tag (type));
22435 equate_type_number_to_die (type, array_die);
22437 if (info->ndimensions > 1)
22438 switch (info->ordering)
22440 case array_descr_ordering_row_major:
22441 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22442 break;
22443 case array_descr_ordering_column_major:
22444 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22445 break;
22446 default:
22447 break;
22450 if (dwarf_version >= 3 || !dwarf_strict)
22452 if (info->data_location)
22453 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22454 dw_scalar_form_exprloc, &context);
22455 if (info->associated)
22456 add_scalar_info (array_die, DW_AT_associated, info->associated,
22457 dw_scalar_form_constant
22458 | dw_scalar_form_exprloc
22459 | dw_scalar_form_reference, &context);
22460 if (info->allocated)
22461 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22462 dw_scalar_form_constant
22463 | dw_scalar_form_exprloc
22464 | dw_scalar_form_reference, &context);
22465 if (info->stride)
22467 const enum dwarf_attribute attr
22468 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22469 const int forms
22470 = (info->stride_in_bits)
22471 ? dw_scalar_form_constant
22472 : (dw_scalar_form_constant
22473 | dw_scalar_form_exprloc
22474 | dw_scalar_form_reference);
22476 add_scalar_info (array_die, attr, info->stride, forms, &context);
22479 if (dwarf_version >= 5)
22481 if (info->rank)
22483 add_scalar_info (array_die, DW_AT_rank, info->rank,
22484 dw_scalar_form_constant
22485 | dw_scalar_form_exprloc, &context);
22486 subrange_tag = DW_TAG_generic_subrange;
22487 context.placeholder_arg = true;
22491 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22493 for (dim = 0; dim < info->ndimensions; dim++)
22495 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22497 if (info->dimen[dim].bounds_type)
22498 add_type_attribute (subrange_die,
22499 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22500 false, context_die);
22501 if (info->dimen[dim].lower_bound)
22502 add_bound_info (subrange_die, DW_AT_lower_bound,
22503 info->dimen[dim].lower_bound, &context);
22504 if (info->dimen[dim].upper_bound)
22505 add_bound_info (subrange_die, DW_AT_upper_bound,
22506 info->dimen[dim].upper_bound, &context);
22507 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22508 add_scalar_info (subrange_die, DW_AT_byte_stride,
22509 info->dimen[dim].stride,
22510 dw_scalar_form_constant
22511 | dw_scalar_form_exprloc
22512 | dw_scalar_form_reference,
22513 &context);
22516 gen_type_die (info->element_type, context_die);
22517 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22518 TREE_CODE (type) == ARRAY_TYPE
22519 && TYPE_REVERSE_STORAGE_ORDER (type),
22520 context_die);
22522 if (get_AT (array_die, DW_AT_name))
22523 add_pubtype (type, array_die);
22525 add_alignment_attribute (array_die, type);
22528 #if 0
22529 static void
22530 gen_entry_point_die (tree decl, dw_die_ref context_die)
22532 tree origin = decl_ultimate_origin (decl);
22533 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22535 if (origin != NULL)
22536 add_abstract_origin_attribute (decl_die, origin);
22537 else
22539 add_name_and_src_coords_attributes (decl_die, decl);
22540 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22541 TYPE_UNQUALIFIED, false, context_die);
22544 if (DECL_ABSTRACT_P (decl))
22545 equate_decl_number_to_die (decl, decl_die);
22546 else
22547 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22549 #endif
22551 /* Walk through the list of incomplete types again, trying once more to
22552 emit full debugging info for them. */
22554 static void
22555 retry_incomplete_types (void)
22557 set_early_dwarf s;
22558 int i;
22560 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22561 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22562 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22563 vec_safe_truncate (incomplete_types, 0);
22566 /* Determine what tag to use for a record type. */
22568 static enum dwarf_tag
22569 record_type_tag (tree type)
22571 if (! lang_hooks.types.classify_record)
22572 return DW_TAG_structure_type;
22574 switch (lang_hooks.types.classify_record (type))
22576 case RECORD_IS_STRUCT:
22577 return DW_TAG_structure_type;
22579 case RECORD_IS_CLASS:
22580 return DW_TAG_class_type;
22582 case RECORD_IS_INTERFACE:
22583 if (dwarf_version >= 3 || !dwarf_strict)
22584 return DW_TAG_interface_type;
22585 return DW_TAG_structure_type;
22587 default:
22588 gcc_unreachable ();
22592 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22593 include all of the information about the enumeration values also. Each
22594 enumerated type name/value is listed as a child of the enumerated type
22595 DIE. */
22597 static dw_die_ref
22598 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22600 dw_die_ref type_die = lookup_type_die (type);
22601 dw_die_ref orig_type_die = type_die;
22603 if (type_die == NULL)
22605 type_die = new_die (DW_TAG_enumeration_type,
22606 scope_die_for (type, context_die), type);
22607 equate_type_number_to_die (type, type_die);
22608 add_name_attribute (type_die, type_tag (type));
22609 if ((dwarf_version >= 4 || !dwarf_strict)
22610 && ENUM_IS_SCOPED (type))
22611 add_AT_flag (type_die, DW_AT_enum_class, 1);
22612 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22613 add_AT_flag (type_die, DW_AT_declaration, 1);
22614 if (!dwarf_strict)
22615 add_AT_unsigned (type_die, DW_AT_encoding,
22616 TYPE_UNSIGNED (type)
22617 ? DW_ATE_unsigned
22618 : DW_ATE_signed);
22620 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22621 return type_die;
22622 else
22623 remove_AT (type_die, DW_AT_declaration);
22625 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22626 given enum type is incomplete, do not generate the DW_AT_byte_size
22627 attribute or the DW_AT_element_list attribute. */
22628 if (TYPE_SIZE (type))
22630 tree link;
22632 if (!ENUM_IS_OPAQUE (type))
22633 TREE_ASM_WRITTEN (type) = 1;
22634 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22635 add_byte_size_attribute (type_die, type);
22636 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22637 add_alignment_attribute (type_die, type);
22638 if ((dwarf_version >= 3 || !dwarf_strict)
22639 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22641 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22642 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22643 context_die);
22645 if (TYPE_STUB_DECL (type) != NULL_TREE)
22647 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22648 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22649 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22650 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22653 /* If the first reference to this type was as the return type of an
22654 inline function, then it may not have a parent. Fix this now. */
22655 if (type_die->die_parent == NULL)
22656 add_child_die (scope_die_for (type, context_die), type_die);
22658 for (link = TYPE_VALUES (type);
22659 link != NULL; link = TREE_CHAIN (link))
22661 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22662 tree value = TREE_VALUE (link);
22664 if (DECL_P (value))
22665 equate_decl_number_to_die (value, enum_die);
22667 gcc_assert (!ENUM_IS_OPAQUE (type));
22668 add_name_attribute (enum_die,
22669 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22671 if (TREE_CODE (value) == CONST_DECL)
22672 value = DECL_INITIAL (value);
22674 if (simple_type_size_in_bits (TREE_TYPE (value))
22675 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22677 /* For constant forms created by add_AT_unsigned DWARF
22678 consumers (GDB, elfutils, etc.) always zero extend
22679 the value. Only when the actual value is negative
22680 do we need to use add_AT_int to generate a constant
22681 form that can represent negative values. */
22682 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22683 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22684 add_AT_unsigned (enum_die, DW_AT_const_value,
22685 (unsigned HOST_WIDE_INT) val);
22686 else
22687 add_AT_int (enum_die, DW_AT_const_value, val);
22689 else
22690 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22691 that here. TODO: This should be re-worked to use correct
22692 signed/unsigned double tags for all cases. */
22693 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22696 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22697 if (TYPE_ARTIFICIAL (type)
22698 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22699 add_AT_flag (type_die, DW_AT_artificial, 1);
22701 else
22702 add_AT_flag (type_die, DW_AT_declaration, 1);
22704 add_pubtype (type, type_die);
22706 return type_die;
22709 /* Generate a DIE to represent either a real live formal parameter decl or to
22710 represent just the type of some formal parameter position in some function
22711 type.
22713 Note that this routine is a bit unusual because its argument may be a
22714 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22715 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22716 node. If it's the former then this function is being called to output a
22717 DIE to represent a formal parameter object (or some inlining thereof). If
22718 it's the latter, then this function is only being called to output a
22719 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22720 argument type of some subprogram type.
22721 If EMIT_NAME_P is true, name and source coordinate attributes
22722 are emitted. */
22724 static dw_die_ref
22725 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22726 dw_die_ref context_die)
22728 tree node_or_origin = node ? node : origin;
22729 tree ultimate_origin;
22730 dw_die_ref parm_die = NULL;
22732 if (DECL_P (node_or_origin))
22734 parm_die = lookup_decl_die (node);
22736 /* If the contexts differ, we may not be talking about the same
22737 thing.
22738 ??? When in LTO the DIE parent is the "abstract" copy and the
22739 context_die is the specification "copy". */
22740 if (parm_die
22741 && parm_die->die_parent != context_die
22742 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22743 || parm_die->die_parent->die_parent != context_die)
22744 && !in_lto_p)
22746 gcc_assert (!DECL_ABSTRACT_P (node));
22747 /* This can happen when creating a concrete instance, in
22748 which case we need to create a new DIE that will get
22749 annotated with DW_AT_abstract_origin. */
22750 parm_die = NULL;
22753 if (parm_die && parm_die->die_parent == NULL)
22755 /* Check that parm_die already has the right attributes that
22756 we would have added below. If any attributes are
22757 missing, fall through to add them. */
22758 if (! DECL_ABSTRACT_P (node_or_origin)
22759 && !get_AT (parm_die, DW_AT_location)
22760 && !get_AT (parm_die, DW_AT_const_value))
22761 /* We are missing location info, and are about to add it. */
22763 else
22765 add_child_die (context_die, parm_die);
22766 return parm_die;
22771 /* If we have a previously generated DIE, use it, unless this is an
22772 concrete instance (origin != NULL), in which case we need a new
22773 DIE with a corresponding DW_AT_abstract_origin. */
22774 bool reusing_die;
22775 if (parm_die && origin == NULL)
22776 reusing_die = true;
22777 else
22779 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22780 reusing_die = false;
22783 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22785 case tcc_declaration:
22786 ultimate_origin = decl_ultimate_origin (node_or_origin);
22787 if (node || ultimate_origin)
22788 origin = ultimate_origin;
22790 if (reusing_die)
22791 goto add_location;
22793 if (origin != NULL)
22794 add_abstract_origin_attribute (parm_die, origin);
22795 else if (emit_name_p)
22796 add_name_and_src_coords_attributes (parm_die, node);
22797 if (origin == NULL
22798 || (! DECL_ABSTRACT_P (node_or_origin)
22799 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22800 decl_function_context
22801 (node_or_origin))))
22803 tree type = TREE_TYPE (node_or_origin);
22804 if (decl_by_reference_p (node_or_origin))
22805 add_type_attribute (parm_die, TREE_TYPE (type),
22806 TYPE_UNQUALIFIED,
22807 false, context_die);
22808 else
22809 add_type_attribute (parm_die, type,
22810 decl_quals (node_or_origin),
22811 false, context_die);
22813 if (origin == NULL && DECL_ARTIFICIAL (node))
22814 add_AT_flag (parm_die, DW_AT_artificial, 1);
22815 add_location:
22816 if (node && node != origin)
22817 equate_decl_number_to_die (node, parm_die);
22818 if (! DECL_ABSTRACT_P (node_or_origin))
22819 add_location_or_const_value_attribute (parm_die, node_or_origin,
22820 node == NULL);
22822 break;
22824 case tcc_type:
22825 /* We were called with some kind of a ..._TYPE node. */
22826 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22827 context_die);
22828 break;
22830 default:
22831 gcc_unreachable ();
22834 return parm_die;
22837 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22838 children DW_TAG_formal_parameter DIEs representing the arguments of the
22839 parameter pack.
22841 PARM_PACK must be a function parameter pack.
22842 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22843 must point to the subsequent arguments of the function PACK_ARG belongs to.
22844 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22845 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22846 following the last one for which a DIE was generated. */
22848 static dw_die_ref
22849 gen_formal_parameter_pack_die (tree parm_pack,
22850 tree pack_arg,
22851 dw_die_ref subr_die,
22852 tree *next_arg)
22854 tree arg;
22855 dw_die_ref parm_pack_die;
22857 gcc_assert (parm_pack
22858 && lang_hooks.function_parameter_pack_p (parm_pack)
22859 && subr_die);
22861 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22862 add_src_coords_attributes (parm_pack_die, parm_pack);
22864 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22866 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22867 parm_pack))
22868 break;
22869 gen_formal_parameter_die (arg, NULL,
22870 false /* Don't emit name attribute. */,
22871 parm_pack_die);
22873 if (next_arg)
22874 *next_arg = arg;
22875 return parm_pack_die;
22878 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22879 at the end of an (ANSI prototyped) formal parameters list. */
22881 static void
22882 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22884 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22887 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22888 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22889 parameters as specified in some function type specification (except for
22890 those which appear as part of a function *definition*). */
22892 static void
22893 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22895 tree link;
22896 tree formal_type = NULL;
22897 tree first_parm_type;
22898 tree arg;
22900 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22902 arg = DECL_ARGUMENTS (function_or_method_type);
22903 function_or_method_type = TREE_TYPE (function_or_method_type);
22905 else
22906 arg = NULL_TREE;
22908 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22910 /* Make our first pass over the list of formal parameter types and output a
22911 DW_TAG_formal_parameter DIE for each one. */
22912 for (link = first_parm_type; link; )
22914 dw_die_ref parm_die;
22916 formal_type = TREE_VALUE (link);
22917 if (formal_type == void_type_node)
22918 break;
22920 /* Output a (nameless) DIE to represent the formal parameter itself. */
22921 parm_die = gen_formal_parameter_die (formal_type, NULL,
22922 true /* Emit name attribute. */,
22923 context_die);
22924 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22925 && link == first_parm_type)
22927 add_AT_flag (parm_die, DW_AT_artificial, 1);
22928 if (dwarf_version >= 3 || !dwarf_strict)
22929 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22931 else if (arg && DECL_ARTIFICIAL (arg))
22932 add_AT_flag (parm_die, DW_AT_artificial, 1);
22934 link = TREE_CHAIN (link);
22935 if (arg)
22936 arg = DECL_CHAIN (arg);
22939 /* If this function type has an ellipsis, add a
22940 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22941 if (formal_type != void_type_node)
22942 gen_unspecified_parameters_die (function_or_method_type, context_die);
22944 /* Make our second (and final) pass over the list of formal parameter types
22945 and output DIEs to represent those types (as necessary). */
22946 for (link = TYPE_ARG_TYPES (function_or_method_type);
22947 link && TREE_VALUE (link);
22948 link = TREE_CHAIN (link))
22949 gen_type_die (TREE_VALUE (link), context_die);
22952 /* We want to generate the DIE for TYPE so that we can generate the
22953 die for MEMBER, which has been defined; we will need to refer back
22954 to the member declaration nested within TYPE. If we're trying to
22955 generate minimal debug info for TYPE, processing TYPE won't do the
22956 trick; we need to attach the member declaration by hand. */
22958 static void
22959 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22961 gen_type_die (type, context_die);
22963 /* If we're trying to avoid duplicate debug info, we may not have
22964 emitted the member decl for this function. Emit it now. */
22965 if (TYPE_STUB_DECL (type)
22966 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22967 && ! lookup_decl_die (member))
22969 dw_die_ref type_die;
22970 gcc_assert (!decl_ultimate_origin (member));
22972 type_die = lookup_type_die_strip_naming_typedef (type);
22973 if (TREE_CODE (member) == FUNCTION_DECL)
22974 gen_subprogram_die (member, type_die);
22975 else if (TREE_CODE (member) == FIELD_DECL)
22977 /* Ignore the nameless fields that are used to skip bits but handle
22978 C++ anonymous unions and structs. */
22979 if (DECL_NAME (member) != NULL_TREE
22980 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22981 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22983 struct vlr_context vlr_ctx = {
22984 DECL_CONTEXT (member), /* struct_type */
22985 NULL_TREE /* variant_part_offset */
22987 gen_type_die (member_declared_type (member), type_die);
22988 gen_field_die (member, &vlr_ctx, type_die);
22991 else
22992 gen_variable_die (member, NULL_TREE, type_die);
22996 /* Forward declare these functions, because they are mutually recursive
22997 with their set_block_* pairing functions. */
22998 static void set_decl_origin_self (tree);
23000 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23001 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23002 that it points to the node itself, thus indicating that the node is its
23003 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23004 the given node is NULL, recursively descend the decl/block tree which
23005 it is the root of, and for each other ..._DECL or BLOCK node contained
23006 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23007 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23008 values to point to themselves. */
23010 static void
23011 set_block_origin_self (tree stmt)
23013 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23015 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23018 tree local_decl;
23020 for (local_decl = BLOCK_VARS (stmt);
23021 local_decl != NULL_TREE;
23022 local_decl = DECL_CHAIN (local_decl))
23023 /* Do not recurse on nested functions since the inlining status
23024 of parent and child can be different as per the DWARF spec. */
23025 if (TREE_CODE (local_decl) != FUNCTION_DECL
23026 && !DECL_EXTERNAL (local_decl))
23027 set_decl_origin_self (local_decl);
23031 tree subblock;
23033 for (subblock = BLOCK_SUBBLOCKS (stmt);
23034 subblock != NULL_TREE;
23035 subblock = BLOCK_CHAIN (subblock))
23036 set_block_origin_self (subblock); /* Recurse. */
23041 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23042 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23043 node to so that it points to the node itself, thus indicating that the
23044 node represents its own (abstract) origin. Additionally, if the
23045 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23046 the decl/block tree of which the given node is the root of, and for
23047 each other ..._DECL or BLOCK node contained therein whose
23048 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23049 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23050 point to themselves. */
23052 static void
23053 set_decl_origin_self (tree decl)
23055 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23057 DECL_ABSTRACT_ORIGIN (decl) = decl;
23058 if (TREE_CODE (decl) == FUNCTION_DECL)
23060 tree arg;
23062 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23063 DECL_ABSTRACT_ORIGIN (arg) = arg;
23064 if (DECL_INITIAL (decl) != NULL_TREE
23065 && DECL_INITIAL (decl) != error_mark_node)
23066 set_block_origin_self (DECL_INITIAL (decl));
23071 /* Mark the early DIE for DECL as the abstract instance. */
23073 static void
23074 dwarf2out_abstract_function (tree decl)
23076 dw_die_ref old_die;
23078 /* Make sure we have the actual abstract inline, not a clone. */
23079 decl = DECL_ORIGIN (decl);
23081 if (DECL_IGNORED_P (decl))
23082 return;
23084 /* In LTO we're all set. We already created abstract instances
23085 early and we want to avoid creating a concrete instance of that
23086 if we don't output it. */
23087 if (in_lto_p)
23088 return;
23090 old_die = lookup_decl_die (decl);
23091 gcc_assert (old_die != NULL);
23092 if (get_AT (old_die, DW_AT_inline))
23093 /* We've already generated the abstract instance. */
23094 return;
23096 /* Go ahead and put DW_AT_inline on the DIE. */
23097 if (DECL_DECLARED_INLINE_P (decl))
23099 if (cgraph_function_possibly_inlined_p (decl))
23100 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23101 else
23102 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23104 else
23106 if (cgraph_function_possibly_inlined_p (decl))
23107 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23108 else
23109 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23112 if (DECL_DECLARED_INLINE_P (decl)
23113 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23114 add_AT_flag (old_die, DW_AT_artificial, 1);
23116 set_decl_origin_self (decl);
23119 /* Helper function of premark_used_types() which gets called through
23120 htab_traverse.
23122 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23123 marked as unused by prune_unused_types. */
23125 bool
23126 premark_used_types_helper (tree const &type, void *)
23128 dw_die_ref die;
23130 die = lookup_type_die (type);
23131 if (die != NULL)
23132 die->die_perennial_p = 1;
23133 return true;
23136 /* Helper function of premark_types_used_by_global_vars which gets called
23137 through htab_traverse.
23139 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23140 marked as unused by prune_unused_types. The DIE of the type is marked
23141 only if the global variable using the type will actually be emitted. */
23144 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23145 void *)
23147 struct types_used_by_vars_entry *entry;
23148 dw_die_ref die;
23150 entry = (struct types_used_by_vars_entry *) *slot;
23151 gcc_assert (entry->type != NULL
23152 && entry->var_decl != NULL);
23153 die = lookup_type_die (entry->type);
23154 if (die)
23156 /* Ask cgraph if the global variable really is to be emitted.
23157 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23158 varpool_node *node = varpool_node::get (entry->var_decl);
23159 if (node && node->definition)
23161 die->die_perennial_p = 1;
23162 /* Keep the parent DIEs as well. */
23163 while ((die = die->die_parent) && die->die_perennial_p == 0)
23164 die->die_perennial_p = 1;
23167 return 1;
23170 /* Mark all members of used_types_hash as perennial. */
23172 static void
23173 premark_used_types (struct function *fun)
23175 if (fun && fun->used_types_hash)
23176 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23179 /* Mark all members of types_used_by_vars_entry as perennial. */
23181 static void
23182 premark_types_used_by_global_vars (void)
23184 if (types_used_by_vars_hash)
23185 types_used_by_vars_hash
23186 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23189 /* Mark all variables used by the symtab as perennial. */
23191 static void
23192 premark_used_variables (void)
23194 /* Mark DIEs in the symtab as used. */
23195 varpool_node *var;
23196 FOR_EACH_VARIABLE (var)
23198 dw_die_ref die = lookup_decl_die (var->decl);
23199 if (die)
23200 die->die_perennial_p = 1;
23204 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23205 for CA_LOC call arg loc node. */
23207 static dw_die_ref
23208 gen_call_site_die (tree decl, dw_die_ref subr_die,
23209 struct call_arg_loc_node *ca_loc)
23211 dw_die_ref stmt_die = NULL, die;
23212 tree block = ca_loc->block;
23214 while (block
23215 && block != DECL_INITIAL (decl)
23216 && TREE_CODE (block) == BLOCK)
23218 stmt_die = lookup_block_die (block);
23219 if (stmt_die)
23220 break;
23221 block = BLOCK_SUPERCONTEXT (block);
23223 if (stmt_die == NULL)
23224 stmt_die = subr_die;
23225 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23226 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23227 if (ca_loc->tail_call_p)
23228 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23229 if (ca_loc->symbol_ref)
23231 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23232 if (tdie)
23233 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23234 else
23235 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23236 false);
23238 return die;
23241 /* Generate a DIE to represent a declared function (either file-scope or
23242 block-local). */
23244 static void
23245 gen_subprogram_die (tree decl, dw_die_ref context_die)
23247 tree origin = decl_ultimate_origin (decl);
23248 dw_die_ref subr_die;
23249 dw_die_ref old_die = lookup_decl_die (decl);
23250 bool old_die_had_no_children = false;
23252 /* This function gets called multiple times for different stages of
23253 the debug process. For example, for func() in this code:
23255 namespace S
23257 void func() { ... }
23260 ...we get called 4 times. Twice in early debug and twice in
23261 late debug:
23263 Early debug
23264 -----------
23266 1. Once while generating func() within the namespace. This is
23267 the declaration. The declaration bit below is set, as the
23268 context is the namespace.
23270 A new DIE will be generated with DW_AT_declaration set.
23272 2. Once for func() itself. This is the specification. The
23273 declaration bit below is clear as the context is the CU.
23275 We will use the cached DIE from (1) to create a new DIE with
23276 DW_AT_specification pointing to the declaration in (1).
23278 Late debug via rest_of_handle_final()
23279 -------------------------------------
23281 3. Once generating func() within the namespace. This is also the
23282 declaration, as in (1), but this time we will early exit below
23283 as we have a cached DIE and a declaration needs no additional
23284 annotations (no locations), as the source declaration line
23285 info is enough.
23287 4. Once for func() itself. As in (2), this is the specification,
23288 but this time we will re-use the cached DIE, and just annotate
23289 it with the location information that should now be available.
23291 For something without namespaces, but with abstract instances, we
23292 are also called a multiple times:
23294 class Base
23296 public:
23297 Base (); // constructor declaration (1)
23300 Base::Base () { } // constructor specification (2)
23302 Early debug
23303 -----------
23305 1. Once for the Base() constructor by virtue of it being a
23306 member of the Base class. This is done via
23307 rest_of_type_compilation.
23309 This is a declaration, so a new DIE will be created with
23310 DW_AT_declaration.
23312 2. Once for the Base() constructor definition, but this time
23313 while generating the abstract instance of the base
23314 constructor (__base_ctor) which is being generated via early
23315 debug of reachable functions.
23317 Even though we have a cached version of the declaration (1),
23318 we will create a DW_AT_specification of the declaration DIE
23319 in (1).
23321 3. Once for the __base_ctor itself, but this time, we generate
23322 an DW_AT_abstract_origin version of the DW_AT_specification in
23323 (2).
23325 Late debug via rest_of_handle_final
23326 -----------------------------------
23328 4. One final time for the __base_ctor (which will have a cached
23329 DIE with DW_AT_abstract_origin created in (3). This time,
23330 we will just annotate the location information now
23331 available.
23333 int declaration = (current_function_decl != decl
23334 || (!DECL_INITIAL (decl) && !origin)
23335 || class_or_namespace_scope_p (context_die));
23337 /* A declaration that has been previously dumped needs no
23338 additional information. */
23339 if (old_die && declaration)
23340 return;
23342 if (in_lto_p && old_die && old_die->die_child == NULL)
23343 old_die_had_no_children = true;
23345 /* Now that the C++ front end lazily declares artificial member fns, we
23346 might need to retrofit the declaration into its class. */
23347 if (!declaration && !origin && !old_die
23348 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23349 && !class_or_namespace_scope_p (context_die)
23350 && debug_info_level > DINFO_LEVEL_TERSE)
23351 old_die = force_decl_die (decl);
23353 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23354 if (origin != NULL)
23356 gcc_assert (!declaration || local_scope_p (context_die));
23358 /* Fixup die_parent for the abstract instance of a nested
23359 inline function. */
23360 if (old_die && old_die->die_parent == NULL)
23361 add_child_die (context_die, old_die);
23363 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23365 /* If we have a DW_AT_abstract_origin we have a working
23366 cached version. */
23367 subr_die = old_die;
23369 else
23371 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23372 add_abstract_origin_attribute (subr_die, origin);
23373 /* This is where the actual code for a cloned function is.
23374 Let's emit linkage name attribute for it. This helps
23375 debuggers to e.g, set breakpoints into
23376 constructors/destructors when the user asks "break
23377 K::K". */
23378 add_linkage_name (subr_die, decl);
23381 /* A cached copy, possibly from early dwarf generation. Reuse as
23382 much as possible. */
23383 else if (old_die)
23385 if (!get_AT_flag (old_die, DW_AT_declaration)
23386 /* We can have a normal definition following an inline one in the
23387 case of redefinition of GNU C extern inlines.
23388 It seems reasonable to use AT_specification in this case. */
23389 && !get_AT (old_die, DW_AT_inline))
23391 /* Detect and ignore this case, where we are trying to output
23392 something we have already output. */
23393 if (get_AT (old_die, DW_AT_low_pc)
23394 || get_AT (old_die, DW_AT_ranges))
23395 return;
23397 /* If we have no location information, this must be a
23398 partially generated DIE from early dwarf generation.
23399 Fall through and generate it. */
23402 /* If the definition comes from the same place as the declaration,
23403 maybe use the old DIE. We always want the DIE for this function
23404 that has the *_pc attributes to be under comp_unit_die so the
23405 debugger can find it. We also need to do this for abstract
23406 instances of inlines, since the spec requires the out-of-line copy
23407 to have the same parent. For local class methods, this doesn't
23408 apply; we just use the old DIE. */
23409 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23410 struct dwarf_file_data * file_index = lookup_filename (s.file);
23411 if (((is_unit_die (old_die->die_parent)
23412 /* This condition fixes the inconsistency/ICE with the
23413 following Fortran test (or some derivative thereof) while
23414 building libgfortran:
23416 module some_m
23417 contains
23418 logical function funky (FLAG)
23419 funky = .true.
23420 end function
23421 end module
23423 || (old_die->die_parent
23424 && old_die->die_parent->die_tag == DW_TAG_module)
23425 || local_scope_p (old_die->die_parent)
23426 || context_die == NULL)
23427 && (DECL_ARTIFICIAL (decl)
23428 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23429 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23430 == (unsigned) s.line)
23431 && (!debug_column_info
23432 || s.column == 0
23433 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23434 == (unsigned) s.column)))))
23435 /* With LTO if there's an abstract instance for
23436 the old DIE, this is a concrete instance and
23437 thus re-use the DIE. */
23438 || get_AT (old_die, DW_AT_abstract_origin))
23440 subr_die = old_die;
23442 /* Clear out the declaration attribute, but leave the
23443 parameters so they can be augmented with location
23444 information later. Unless this was a declaration, in
23445 which case, wipe out the nameless parameters and recreate
23446 them further down. */
23447 if (remove_AT (subr_die, DW_AT_declaration))
23450 remove_AT (subr_die, DW_AT_object_pointer);
23451 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23454 /* Make a specification pointing to the previously built
23455 declaration. */
23456 else
23458 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23459 add_AT_specification (subr_die, old_die);
23460 add_pubname (decl, subr_die);
23461 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23462 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23463 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23464 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23465 if (debug_column_info
23466 && s.column
23467 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23468 != (unsigned) s.column))
23469 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23471 /* If the prototype had an 'auto' or 'decltype(auto)' in
23472 the return type, emit the real type on the definition die. */
23473 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23475 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23476 while (die
23477 && (die->die_tag == DW_TAG_reference_type
23478 || die->die_tag == DW_TAG_rvalue_reference_type
23479 || die->die_tag == DW_TAG_pointer_type
23480 || die->die_tag == DW_TAG_const_type
23481 || die->die_tag == DW_TAG_volatile_type
23482 || die->die_tag == DW_TAG_restrict_type
23483 || die->die_tag == DW_TAG_array_type
23484 || die->die_tag == DW_TAG_ptr_to_member_type
23485 || die->die_tag == DW_TAG_subroutine_type))
23486 die = get_AT_ref (die, DW_AT_type);
23487 if (die == auto_die || die == decltype_auto_die)
23488 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23489 TYPE_UNQUALIFIED, false, context_die);
23492 /* When we process the method declaration, we haven't seen
23493 the out-of-class defaulted definition yet, so we have to
23494 recheck now. */
23495 if ((dwarf_version >= 5 || ! dwarf_strict)
23496 && !get_AT (subr_die, DW_AT_defaulted))
23498 int defaulted
23499 = lang_hooks.decls.decl_dwarf_attribute (decl,
23500 DW_AT_defaulted);
23501 if (defaulted != -1)
23503 /* Other values must have been handled before. */
23504 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23505 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23510 /* Create a fresh DIE for anything else. */
23511 else
23513 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23515 if (TREE_PUBLIC (decl))
23516 add_AT_flag (subr_die, DW_AT_external, 1);
23518 add_name_and_src_coords_attributes (subr_die, decl);
23519 add_pubname (decl, subr_die);
23520 if (debug_info_level > DINFO_LEVEL_TERSE)
23522 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23523 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23524 TYPE_UNQUALIFIED, false, context_die);
23527 add_pure_or_virtual_attribute (subr_die, decl);
23528 if (DECL_ARTIFICIAL (decl))
23529 add_AT_flag (subr_die, DW_AT_artificial, 1);
23531 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23532 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23534 add_alignment_attribute (subr_die, decl);
23536 add_accessibility_attribute (subr_die, decl);
23539 /* Unless we have an existing non-declaration DIE, equate the new
23540 DIE. */
23541 if (!old_die || is_declaration_die (old_die))
23542 equate_decl_number_to_die (decl, subr_die);
23544 if (declaration)
23546 if (!old_die || !get_AT (old_die, DW_AT_inline))
23548 add_AT_flag (subr_die, DW_AT_declaration, 1);
23550 /* If this is an explicit function declaration then generate
23551 a DW_AT_explicit attribute. */
23552 if ((dwarf_version >= 3 || !dwarf_strict)
23553 && lang_hooks.decls.decl_dwarf_attribute (decl,
23554 DW_AT_explicit) == 1)
23555 add_AT_flag (subr_die, DW_AT_explicit, 1);
23557 /* If this is a C++11 deleted special function member then generate
23558 a DW_AT_deleted attribute. */
23559 if ((dwarf_version >= 5 || !dwarf_strict)
23560 && lang_hooks.decls.decl_dwarf_attribute (decl,
23561 DW_AT_deleted) == 1)
23562 add_AT_flag (subr_die, DW_AT_deleted, 1);
23564 /* If this is a C++11 defaulted special function member then
23565 generate a DW_AT_defaulted attribute. */
23566 if (dwarf_version >= 5 || !dwarf_strict)
23568 int defaulted
23569 = lang_hooks.decls.decl_dwarf_attribute (decl,
23570 DW_AT_defaulted);
23571 if (defaulted != -1)
23572 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23575 /* If this is a C++11 non-static member function with & ref-qualifier
23576 then generate a DW_AT_reference attribute. */
23577 if ((dwarf_version >= 5 || !dwarf_strict)
23578 && lang_hooks.decls.decl_dwarf_attribute (decl,
23579 DW_AT_reference) == 1)
23580 add_AT_flag (subr_die, DW_AT_reference, 1);
23582 /* If this is a C++11 non-static member function with &&
23583 ref-qualifier then generate a DW_AT_reference attribute. */
23584 if ((dwarf_version >= 5 || !dwarf_strict)
23585 && lang_hooks.decls.decl_dwarf_attribute (decl,
23586 DW_AT_rvalue_reference)
23587 == 1)
23588 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23591 /* For non DECL_EXTERNALs, if range information is available, fill
23592 the DIE with it. */
23593 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23595 HOST_WIDE_INT cfa_fb_offset;
23597 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23599 if (!crtl->has_bb_partition)
23601 dw_fde_ref fde = fun->fde;
23602 if (fde->dw_fde_begin)
23604 /* We have already generated the labels. */
23605 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23606 fde->dw_fde_end, false);
23608 else
23610 /* Create start/end labels and add the range. */
23611 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23612 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23613 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23614 current_function_funcdef_no);
23615 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23616 current_function_funcdef_no);
23617 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23618 false);
23621 #if VMS_DEBUGGING_INFO
23622 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23623 Section 2.3 Prologue and Epilogue Attributes:
23624 When a breakpoint is set on entry to a function, it is generally
23625 desirable for execution to be suspended, not on the very first
23626 instruction of the function, but rather at a point after the
23627 function's frame has been set up, after any language defined local
23628 declaration processing has been completed, and before execution of
23629 the first statement of the function begins. Debuggers generally
23630 cannot properly determine where this point is. Similarly for a
23631 breakpoint set on exit from a function. The prologue and epilogue
23632 attributes allow a compiler to communicate the location(s) to use. */
23635 if (fde->dw_fde_vms_end_prologue)
23636 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23637 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23639 if (fde->dw_fde_vms_begin_epilogue)
23640 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23641 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23643 #endif
23646 else
23648 /* Generate pubnames entries for the split function code ranges. */
23649 dw_fde_ref fde = fun->fde;
23651 if (fde->dw_fde_second_begin)
23653 if (dwarf_version >= 3 || !dwarf_strict)
23655 /* We should use ranges for non-contiguous code section
23656 addresses. Use the actual code range for the initial
23657 section, since the HOT/COLD labels might precede an
23658 alignment offset. */
23659 bool range_list_added = false;
23660 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23661 fde->dw_fde_end, &range_list_added,
23662 false);
23663 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23664 fde->dw_fde_second_end,
23665 &range_list_added, false);
23666 if (range_list_added)
23667 add_ranges (NULL);
23669 else
23671 /* There is no real support in DW2 for this .. so we make
23672 a work-around. First, emit the pub name for the segment
23673 containing the function label. Then make and emit a
23674 simplified subprogram DIE for the second segment with the
23675 name pre-fixed by __hot/cold_sect_of_. We use the same
23676 linkage name for the second die so that gdb will find both
23677 sections when given "b foo". */
23678 const char *name = NULL;
23679 tree decl_name = DECL_NAME (decl);
23680 dw_die_ref seg_die;
23682 /* Do the 'primary' section. */
23683 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23684 fde->dw_fde_end, false);
23686 /* Build a minimal DIE for the secondary section. */
23687 seg_die = new_die (DW_TAG_subprogram,
23688 subr_die->die_parent, decl);
23690 if (TREE_PUBLIC (decl))
23691 add_AT_flag (seg_die, DW_AT_external, 1);
23693 if (decl_name != NULL
23694 && IDENTIFIER_POINTER (decl_name) != NULL)
23696 name = dwarf2_name (decl, 1);
23697 if (! DECL_ARTIFICIAL (decl))
23698 add_src_coords_attributes (seg_die, decl);
23700 add_linkage_name (seg_die, decl);
23702 gcc_assert (name != NULL);
23703 add_pure_or_virtual_attribute (seg_die, decl);
23704 if (DECL_ARTIFICIAL (decl))
23705 add_AT_flag (seg_die, DW_AT_artificial, 1);
23707 name = concat ("__second_sect_of_", name, NULL);
23708 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23709 fde->dw_fde_second_end, false);
23710 add_name_attribute (seg_die, name);
23711 if (want_pubnames ())
23712 add_pubname_string (name, seg_die);
23715 else
23716 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23717 false);
23720 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23722 /* We define the "frame base" as the function's CFA. This is more
23723 convenient for several reasons: (1) It's stable across the prologue
23724 and epilogue, which makes it better than just a frame pointer,
23725 (2) With dwarf3, there exists a one-byte encoding that allows us
23726 to reference the .debug_frame data by proxy, but failing that,
23727 (3) We can at least reuse the code inspection and interpretation
23728 code that determines the CFA position at various points in the
23729 function. */
23730 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23732 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23733 add_AT_loc (subr_die, DW_AT_frame_base, op);
23735 else
23737 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23738 if (list->dw_loc_next)
23739 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23740 else
23741 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23744 /* Compute a displacement from the "steady-state frame pointer" to
23745 the CFA. The former is what all stack slots and argument slots
23746 will reference in the rtl; the latter is what we've told the
23747 debugger about. We'll need to adjust all frame_base references
23748 by this displacement. */
23749 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23751 if (fun->static_chain_decl)
23753 /* DWARF requires here a location expression that computes the
23754 address of the enclosing subprogram's frame base. The machinery
23755 in tree-nested.c is supposed to store this specific address in the
23756 last field of the FRAME record. */
23757 const tree frame_type
23758 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23759 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23761 tree fb_expr
23762 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23763 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23764 fb_expr, fb_decl, NULL_TREE);
23766 add_AT_location_description (subr_die, DW_AT_static_link,
23767 loc_list_from_tree (fb_expr, 0, NULL));
23770 resolve_variable_values ();
23773 /* Generate child dies for template parameters. */
23774 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23775 gen_generic_params_dies (decl);
23777 /* Now output descriptions of the arguments for this function. This gets
23778 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23779 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23780 `...' at the end of the formal parameter list. In order to find out if
23781 there was a trailing ellipsis or not, we must instead look at the type
23782 associated with the FUNCTION_DECL. This will be a node of type
23783 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23784 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23785 an ellipsis at the end. */
23787 /* In the case where we are describing a mere function declaration, all we
23788 need to do here (and all we *can* do here) is to describe the *types* of
23789 its formal parameters. */
23790 if (debug_info_level <= DINFO_LEVEL_TERSE)
23792 else if (declaration)
23793 gen_formal_types_die (decl, subr_die);
23794 else
23796 /* Generate DIEs to represent all known formal parameters. */
23797 tree parm = DECL_ARGUMENTS (decl);
23798 tree generic_decl = early_dwarf
23799 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23800 tree generic_decl_parm = generic_decl
23801 ? DECL_ARGUMENTS (generic_decl)
23802 : NULL;
23804 /* Now we want to walk the list of parameters of the function and
23805 emit their relevant DIEs.
23807 We consider the case of DECL being an instance of a generic function
23808 as well as it being a normal function.
23810 If DECL is an instance of a generic function we walk the
23811 parameters of the generic function declaration _and_ the parameters of
23812 DECL itself. This is useful because we want to emit specific DIEs for
23813 function parameter packs and those are declared as part of the
23814 generic function declaration. In that particular case,
23815 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23816 That DIE has children DIEs representing the set of arguments
23817 of the pack. Note that the set of pack arguments can be empty.
23818 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23819 children DIE.
23821 Otherwise, we just consider the parameters of DECL. */
23822 while (generic_decl_parm || parm)
23824 if (generic_decl_parm
23825 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23826 gen_formal_parameter_pack_die (generic_decl_parm,
23827 parm, subr_die,
23828 &parm);
23829 else if (parm)
23831 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23833 if (early_dwarf
23834 && parm == DECL_ARGUMENTS (decl)
23835 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23836 && parm_die
23837 && (dwarf_version >= 3 || !dwarf_strict))
23838 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23840 parm = DECL_CHAIN (parm);
23843 if (generic_decl_parm)
23844 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23847 /* Decide whether we need an unspecified_parameters DIE at the end.
23848 There are 2 more cases to do this for: 1) the ansi ... declaration -
23849 this is detectable when the end of the arg list is not a
23850 void_type_node 2) an unprototyped function declaration (not a
23851 definition). This just means that we have no info about the
23852 parameters at all. */
23853 if (early_dwarf)
23855 if (prototype_p (TREE_TYPE (decl)))
23857 /* This is the prototyped case, check for.... */
23858 if (stdarg_p (TREE_TYPE (decl)))
23859 gen_unspecified_parameters_die (decl, subr_die);
23861 else if (DECL_INITIAL (decl) == NULL_TREE)
23862 gen_unspecified_parameters_die (decl, subr_die);
23864 else if ((subr_die != old_die || old_die_had_no_children)
23865 && prototype_p (TREE_TYPE (decl))
23866 && stdarg_p (TREE_TYPE (decl)))
23867 gen_unspecified_parameters_die (decl, subr_die);
23870 if (subr_die != old_die)
23871 /* Add the calling convention attribute if requested. */
23872 add_calling_convention_attribute (subr_die, decl);
23874 /* Output Dwarf info for all of the stuff within the body of the function
23875 (if it has one - it may be just a declaration).
23877 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23878 a function. This BLOCK actually represents the outermost binding contour
23879 for the function, i.e. the contour in which the function's formal
23880 parameters and labels get declared. Curiously, it appears that the front
23881 end doesn't actually put the PARM_DECL nodes for the current function onto
23882 the BLOCK_VARS list for this outer scope, but are strung off of the
23883 DECL_ARGUMENTS list for the function instead.
23885 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23886 the LABEL_DECL nodes for the function however, and we output DWARF info
23887 for those in decls_for_scope. Just within the `outer_scope' there will be
23888 a BLOCK node representing the function's outermost pair of curly braces,
23889 and any blocks used for the base and member initializers of a C++
23890 constructor function. */
23891 tree outer_scope = DECL_INITIAL (decl);
23892 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23894 int call_site_note_count = 0;
23895 int tail_call_site_note_count = 0;
23897 /* Emit a DW_TAG_variable DIE for a named return value. */
23898 if (DECL_NAME (DECL_RESULT (decl)))
23899 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23901 /* The first time through decls_for_scope we will generate the
23902 DIEs for the locals. The second time, we fill in the
23903 location info. */
23904 decls_for_scope (outer_scope, subr_die);
23906 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23908 struct call_arg_loc_node *ca_loc;
23909 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23911 dw_die_ref die = NULL;
23912 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23913 rtx arg, next_arg;
23914 tree arg_decl = NULL_TREE;
23916 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23917 ? XEXP (ca_loc->call_arg_loc_note, 0)
23918 : NULL_RTX);
23919 arg; arg = next_arg)
23921 dw_loc_descr_ref reg, val;
23922 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23923 dw_die_ref cdie, tdie = NULL;
23925 next_arg = XEXP (arg, 1);
23926 if (REG_P (XEXP (XEXP (arg, 0), 0))
23927 && next_arg
23928 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23929 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23930 && REGNO (XEXP (XEXP (arg, 0), 0))
23931 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23932 next_arg = XEXP (next_arg, 1);
23933 if (mode == VOIDmode)
23935 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23936 if (mode == VOIDmode)
23937 mode = GET_MODE (XEXP (arg, 0));
23939 if (mode == VOIDmode || mode == BLKmode)
23940 continue;
23941 /* Get dynamic information about call target only if we
23942 have no static information: we cannot generate both
23943 DW_AT_call_origin and DW_AT_call_target
23944 attributes. */
23945 if (ca_loc->symbol_ref == NULL_RTX)
23947 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23949 tloc = XEXP (XEXP (arg, 0), 1);
23950 continue;
23952 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23953 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23955 tlocc = XEXP (XEXP (arg, 0), 1);
23956 continue;
23959 reg = NULL;
23960 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23961 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23962 VAR_INIT_STATUS_INITIALIZED);
23963 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23965 rtx mem = XEXP (XEXP (arg, 0), 0);
23966 reg = mem_loc_descriptor (XEXP (mem, 0),
23967 get_address_mode (mem),
23968 GET_MODE (mem),
23969 VAR_INIT_STATUS_INITIALIZED);
23971 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23972 == DEBUG_PARAMETER_REF)
23974 tree tdecl
23975 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23976 tdie = lookup_decl_die (tdecl);
23977 if (tdie == NULL)
23978 continue;
23979 arg_decl = tdecl;
23981 else
23982 continue;
23983 if (reg == NULL
23984 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23985 != DEBUG_PARAMETER_REF)
23986 continue;
23987 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23988 VOIDmode,
23989 VAR_INIT_STATUS_INITIALIZED);
23990 if (val == NULL)
23991 continue;
23992 if (die == NULL)
23993 die = gen_call_site_die (decl, subr_die, ca_loc);
23994 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23995 NULL_TREE);
23996 add_desc_attribute (cdie, arg_decl);
23997 if (reg != NULL)
23998 add_AT_loc (cdie, DW_AT_location, reg);
23999 else if (tdie != NULL)
24000 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24001 tdie);
24002 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24003 if (next_arg != XEXP (arg, 1))
24005 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24006 if (mode == VOIDmode)
24007 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24008 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24009 0), 1),
24010 mode, VOIDmode,
24011 VAR_INIT_STATUS_INITIALIZED);
24012 if (val != NULL)
24013 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24014 val);
24017 if (die == NULL
24018 && (ca_loc->symbol_ref || tloc))
24019 die = gen_call_site_die (decl, subr_die, ca_loc);
24020 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24022 dw_loc_descr_ref tval = NULL;
24024 if (tloc != NULL_RTX)
24025 tval = mem_loc_descriptor (tloc,
24026 GET_MODE (tloc) == VOIDmode
24027 ? Pmode : GET_MODE (tloc),
24028 VOIDmode,
24029 VAR_INIT_STATUS_INITIALIZED);
24030 if (tval)
24031 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24032 else if (tlocc != NULL_RTX)
24034 tval = mem_loc_descriptor (tlocc,
24035 GET_MODE (tlocc) == VOIDmode
24036 ? Pmode : GET_MODE (tlocc),
24037 VOIDmode,
24038 VAR_INIT_STATUS_INITIALIZED);
24039 if (tval)
24040 add_AT_loc (die,
24041 dwarf_AT (DW_AT_call_target_clobbered),
24042 tval);
24045 if (die != NULL)
24047 call_site_note_count++;
24048 if (ca_loc->tail_call_p)
24049 tail_call_site_note_count++;
24053 call_arg_locations = NULL;
24054 call_arg_loc_last = NULL;
24055 if (tail_call_site_count >= 0
24056 && tail_call_site_count == tail_call_site_note_count
24057 && (!dwarf_strict || dwarf_version >= 5))
24059 if (call_site_count >= 0
24060 && call_site_count == call_site_note_count)
24061 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24062 else
24063 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24065 call_site_count = -1;
24066 tail_call_site_count = -1;
24069 /* Mark used types after we have created DIEs for the functions scopes. */
24070 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24073 /* Returns a hash value for X (which really is a die_struct). */
24075 hashval_t
24076 block_die_hasher::hash (die_struct *d)
24078 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24081 /* Return nonzero if decl_id and die_parent of die_struct X is the same
24082 as decl_id and die_parent of die_struct Y. */
24084 bool
24085 block_die_hasher::equal (die_struct *x, die_struct *y)
24087 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24090 /* Hold information about markers for inlined entry points. */
24091 struct GTY ((for_user)) inline_entry_data
24093 /* The block that's the inlined_function_outer_scope for an inlined
24094 function. */
24095 tree block;
24097 /* The label at the inlined entry point. */
24098 const char *label_pfx;
24099 unsigned int label_num;
24101 /* The view number to be used as the inlined entry point. */
24102 var_loc_view view;
24105 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24107 typedef tree compare_type;
24108 static inline hashval_t hash (const inline_entry_data *);
24109 static inline bool equal (const inline_entry_data *, const_tree);
24112 /* Hash table routines for inline_entry_data. */
24114 inline hashval_t
24115 inline_entry_data_hasher::hash (const inline_entry_data *data)
24117 return htab_hash_pointer (data->block);
24120 inline bool
24121 inline_entry_data_hasher::equal (const inline_entry_data *data,
24122 const_tree block)
24124 return data->block == block;
24127 /* Inlined entry points pending DIE creation in this compilation unit. */
24129 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24132 /* Return TRUE if DECL, which may have been previously generated as
24133 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24134 true if decl (or its origin) is either an extern declaration or a
24135 class/namespace scoped declaration.
24137 The declare_in_namespace support causes us to get two DIEs for one
24138 variable, both of which are declarations. We want to avoid
24139 considering one to be a specification, so we must test for
24140 DECLARATION and DW_AT_declaration. */
24141 static inline bool
24142 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24144 return (old_die && TREE_STATIC (decl) && !declaration
24145 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24148 /* Return true if DECL is a local static. */
24150 static inline bool
24151 local_function_static (tree decl)
24153 gcc_assert (VAR_P (decl));
24154 return TREE_STATIC (decl)
24155 && DECL_CONTEXT (decl)
24156 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24159 /* Return true iff DECL overrides (presumably completes) the type of
24160 OLD_DIE within CONTEXT_DIE. */
24162 static bool
24163 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24164 dw_die_ref context_die)
24166 tree type = TREE_TYPE (decl);
24167 int cv_quals;
24169 if (decl_by_reference_p (decl))
24171 type = TREE_TYPE (type);
24172 cv_quals = TYPE_UNQUALIFIED;
24174 else
24175 cv_quals = decl_quals (decl);
24177 dw_die_ref type_die = modified_type_die (type,
24178 cv_quals | TYPE_QUALS (type),
24179 false,
24180 context_die);
24182 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24184 return type_die != old_type_die;
24187 /* Generate a DIE to represent a declared data object.
24188 Either DECL or ORIGIN must be non-null. */
24190 static void
24191 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24193 HOST_WIDE_INT off = 0;
24194 tree com_decl;
24195 tree decl_or_origin = decl ? decl : origin;
24196 tree ultimate_origin;
24197 dw_die_ref var_die;
24198 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24199 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24200 || class_or_namespace_scope_p (context_die));
24201 bool specialization_p = false;
24202 bool no_linkage_name = false;
24204 /* While C++ inline static data members have definitions inside of the
24205 class, force the first DIE to be a declaration, then let gen_member_die
24206 reparent it to the class context and call gen_variable_die again
24207 to create the outside of the class DIE for the definition. */
24208 if (!declaration
24209 && old_die == NULL
24210 && decl
24211 && DECL_CONTEXT (decl)
24212 && TYPE_P (DECL_CONTEXT (decl))
24213 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24215 declaration = true;
24216 if (dwarf_version < 5)
24217 no_linkage_name = true;
24220 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24221 if (decl || ultimate_origin)
24222 origin = ultimate_origin;
24223 com_decl = fortran_common (decl_or_origin, &off);
24225 /* Symbol in common gets emitted as a child of the common block, in the form
24226 of a data member. */
24227 if (com_decl)
24229 dw_die_ref com_die;
24230 dw_loc_list_ref loc = NULL;
24231 die_node com_die_arg;
24233 var_die = lookup_decl_die (decl_or_origin);
24234 if (var_die)
24236 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24238 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24239 if (loc)
24241 if (off)
24243 /* Optimize the common case. */
24244 if (single_element_loc_list_p (loc)
24245 && loc->expr->dw_loc_opc == DW_OP_addr
24246 && loc->expr->dw_loc_next == NULL
24247 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24248 == SYMBOL_REF)
24250 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24251 loc->expr->dw_loc_oprnd1.v.val_addr
24252 = plus_constant (GET_MODE (x), x , off);
24254 else
24255 loc_list_plus_const (loc, off);
24257 add_AT_location_description (var_die, DW_AT_location, loc);
24258 remove_AT (var_die, DW_AT_declaration);
24261 return;
24264 if (common_block_die_table == NULL)
24265 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24267 com_die_arg.decl_id = DECL_UID (com_decl);
24268 com_die_arg.die_parent = context_die;
24269 com_die = common_block_die_table->find (&com_die_arg);
24270 if (! early_dwarf)
24271 loc = loc_list_from_tree (com_decl, 2, NULL);
24272 if (com_die == NULL)
24274 const char *cnam
24275 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24276 die_node **slot;
24278 com_die = new_die (DW_TAG_common_block, context_die, decl);
24279 add_name_and_src_coords_attributes (com_die, com_decl);
24280 if (loc)
24282 add_AT_location_description (com_die, DW_AT_location, loc);
24283 /* Avoid sharing the same loc descriptor between
24284 DW_TAG_common_block and DW_TAG_variable. */
24285 loc = loc_list_from_tree (com_decl, 2, NULL);
24287 else if (DECL_EXTERNAL (decl_or_origin))
24288 add_AT_flag (com_die, DW_AT_declaration, 1);
24289 if (want_pubnames ())
24290 add_pubname_string (cnam, com_die); /* ??? needed? */
24291 com_die->decl_id = DECL_UID (com_decl);
24292 slot = common_block_die_table->find_slot (com_die, INSERT);
24293 *slot = com_die;
24295 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24297 add_AT_location_description (com_die, DW_AT_location, loc);
24298 loc = loc_list_from_tree (com_decl, 2, NULL);
24299 remove_AT (com_die, DW_AT_declaration);
24301 var_die = new_die (DW_TAG_variable, com_die, decl);
24302 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24303 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24304 decl_quals (decl_or_origin), false,
24305 context_die);
24306 add_alignment_attribute (var_die, decl);
24307 add_AT_flag (var_die, DW_AT_external, 1);
24308 if (loc)
24310 if (off)
24312 /* Optimize the common case. */
24313 if (single_element_loc_list_p (loc)
24314 && loc->expr->dw_loc_opc == DW_OP_addr
24315 && loc->expr->dw_loc_next == NULL
24316 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24318 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24319 loc->expr->dw_loc_oprnd1.v.val_addr
24320 = plus_constant (GET_MODE (x), x, off);
24322 else
24323 loc_list_plus_const (loc, off);
24325 add_AT_location_description (var_die, DW_AT_location, loc);
24327 else if (DECL_EXTERNAL (decl_or_origin))
24328 add_AT_flag (var_die, DW_AT_declaration, 1);
24329 if (decl)
24330 equate_decl_number_to_die (decl, var_die);
24331 return;
24334 if (old_die)
24336 if (declaration)
24338 /* A declaration that has been previously dumped, needs no
24339 further annotations, since it doesn't need location on
24340 the second pass. */
24341 return;
24343 else if (decl_will_get_specification_p (old_die, decl, declaration)
24344 && !get_AT (old_die, DW_AT_specification))
24346 /* Fall-thru so we can make a new variable die along with a
24347 DW_AT_specification. */
24349 else if (origin && old_die->die_parent != context_die)
24351 /* If we will be creating an inlined instance, we need a
24352 new DIE that will get annotated with
24353 DW_AT_abstract_origin. */
24354 gcc_assert (!DECL_ABSTRACT_P (decl));
24356 else
24358 /* If a DIE was dumped early, it still needs location info.
24359 Skip to where we fill the location bits. */
24360 var_die = old_die;
24362 /* ??? In LTRANS we cannot annotate early created variably
24363 modified type DIEs without copying them and adjusting all
24364 references to them. Thus we dumped them again. Also add a
24365 reference to them but beware of -g0 compile and -g link
24366 in which case the reference will be already present. */
24367 tree type = TREE_TYPE (decl_or_origin);
24368 if (in_lto_p
24369 && ! get_AT (var_die, DW_AT_type)
24370 && variably_modified_type_p
24371 (type, decl_function_context (decl_or_origin)))
24373 if (decl_by_reference_p (decl_or_origin))
24374 add_type_attribute (var_die, TREE_TYPE (type),
24375 TYPE_UNQUALIFIED, false, context_die);
24376 else
24377 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24378 false, context_die);
24381 goto gen_variable_die_location;
24385 /* For static data members, the declaration in the class is supposed
24386 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24387 also in DWARF2; the specification should still be DW_TAG_variable
24388 referencing the DW_TAG_member DIE. */
24389 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24390 var_die = new_die (DW_TAG_member, context_die, decl);
24391 else
24392 var_die = new_die (DW_TAG_variable, context_die, decl);
24394 if (origin != NULL)
24395 add_abstract_origin_attribute (var_die, origin);
24397 /* Loop unrolling can create multiple blocks that refer to the same
24398 static variable, so we must test for the DW_AT_declaration flag.
24400 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24401 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24402 sharing them.
24404 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24405 else if (decl_will_get_specification_p (old_die, decl, declaration))
24407 /* This is a definition of a C++ class level static. */
24408 add_AT_specification (var_die, old_die);
24409 specialization_p = true;
24410 if (DECL_NAME (decl))
24412 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24413 struct dwarf_file_data * file_index = lookup_filename (s.file);
24415 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24416 add_AT_file (var_die, DW_AT_decl_file, file_index);
24418 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24419 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24421 if (debug_column_info
24422 && s.column
24423 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24424 != (unsigned) s.column))
24425 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24427 if (old_die->die_tag == DW_TAG_member)
24428 add_linkage_name (var_die, decl);
24431 else
24432 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24434 if ((origin == NULL && !specialization_p)
24435 || (origin != NULL
24436 && !DECL_ABSTRACT_P (decl_or_origin)
24437 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24438 decl_function_context
24439 (decl_or_origin)))
24440 || (old_die && specialization_p
24441 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24443 tree type = TREE_TYPE (decl_or_origin);
24445 if (decl_by_reference_p (decl_or_origin))
24446 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24447 context_die);
24448 else
24449 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24450 context_die);
24453 if (origin == NULL && !specialization_p)
24455 if (TREE_PUBLIC (decl))
24456 add_AT_flag (var_die, DW_AT_external, 1);
24458 if (DECL_ARTIFICIAL (decl))
24459 add_AT_flag (var_die, DW_AT_artificial, 1);
24461 add_alignment_attribute (var_die, decl);
24463 add_accessibility_attribute (var_die, decl);
24466 if (declaration)
24467 add_AT_flag (var_die, DW_AT_declaration, 1);
24469 if (decl && (DECL_ABSTRACT_P (decl)
24470 || !old_die || is_declaration_die (old_die)))
24471 equate_decl_number_to_die (decl, var_die);
24473 gen_variable_die_location:
24474 if (! declaration
24475 && (! DECL_ABSTRACT_P (decl_or_origin)
24476 /* Local static vars are shared between all clones/inlines,
24477 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24478 already set. */
24479 || (VAR_P (decl_or_origin)
24480 && TREE_STATIC (decl_or_origin)
24481 && DECL_RTL_SET_P (decl_or_origin))))
24483 if (early_dwarf)
24485 add_pubname (decl_or_origin, var_die);
24486 /* For global register variables, emit DW_AT_location if possible
24487 already during early_dwarf, as late_global_decl won't be usually
24488 called. */
24489 if (DECL_HARD_REGISTER (decl_or_origin)
24490 && TREE_STATIC (decl_or_origin)
24491 && !decl_by_reference_p (decl_or_origin)
24492 && !get_AT (var_die, DW_AT_location)
24493 && !get_AT (var_die, DW_AT_const_value)
24494 && DECL_RTL_SET_P (decl_or_origin)
24495 && REG_P (DECL_RTL (decl_or_origin)))
24497 dw_loc_descr_ref descr
24498 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24499 VAR_INIT_STATUS_INITIALIZED);
24500 if (descr)
24501 add_AT_loc (var_die, DW_AT_location, descr);
24504 else
24505 add_location_or_const_value_attribute (var_die, decl_or_origin,
24506 decl == NULL);
24508 else
24509 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24511 if ((dwarf_version >= 4 || !dwarf_strict)
24512 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24513 DW_AT_const_expr) == 1
24514 && !get_AT (var_die, DW_AT_const_expr)
24515 && !specialization_p)
24516 add_AT_flag (var_die, DW_AT_const_expr, 1);
24518 if (!dwarf_strict)
24520 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24521 DW_AT_inline);
24522 if (inl != -1
24523 && !get_AT (var_die, DW_AT_inline)
24524 && !specialization_p)
24525 add_AT_unsigned (var_die, DW_AT_inline, inl);
24529 /* Generate a DIE to represent a named constant. */
24531 static void
24532 gen_const_die (tree decl, dw_die_ref context_die)
24534 dw_die_ref const_die;
24535 tree type = TREE_TYPE (decl);
24537 const_die = lookup_decl_die (decl);
24538 if (const_die)
24539 return;
24541 const_die = new_die (DW_TAG_constant, context_die, decl);
24542 equate_decl_number_to_die (decl, const_die);
24543 add_name_and_src_coords_attributes (const_die, decl);
24544 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24545 if (TREE_PUBLIC (decl))
24546 add_AT_flag (const_die, DW_AT_external, 1);
24547 if (DECL_ARTIFICIAL (decl))
24548 add_AT_flag (const_die, DW_AT_artificial, 1);
24549 tree_add_const_value_attribute_for_decl (const_die, decl);
24552 /* Generate a DIE to represent a label identifier. */
24554 static void
24555 gen_label_die (tree decl, dw_die_ref context_die)
24557 tree origin = decl_ultimate_origin (decl);
24558 dw_die_ref lbl_die = lookup_decl_die (decl);
24559 rtx insn;
24560 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24562 if (!lbl_die)
24564 lbl_die = new_die (DW_TAG_label, context_die, decl);
24565 equate_decl_number_to_die (decl, lbl_die);
24567 if (origin != NULL)
24568 add_abstract_origin_attribute (lbl_die, origin);
24569 else
24570 add_name_and_src_coords_attributes (lbl_die, decl);
24573 if (DECL_ABSTRACT_P (decl))
24574 equate_decl_number_to_die (decl, lbl_die);
24575 else if (! early_dwarf)
24577 insn = DECL_RTL_IF_SET (decl);
24579 /* Deleted labels are programmer specified labels which have been
24580 eliminated because of various optimizations. We still emit them
24581 here so that it is possible to put breakpoints on them. */
24582 if (insn
24583 && (LABEL_P (insn)
24584 || ((NOTE_P (insn)
24585 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24587 /* When optimization is enabled (via -O) some parts of the compiler
24588 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
24589 represent source-level labels which were explicitly declared by
24590 the user. This really shouldn't be happening though, so catch
24591 it if it ever does happen. */
24592 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24594 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24595 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24597 else if (insn
24598 && NOTE_P (insn)
24599 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24600 && CODE_LABEL_NUMBER (insn) != -1)
24602 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24603 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24608 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24609 attributes to the DIE for a block STMT, to describe where the inlined
24610 function was called from. This is similar to add_src_coords_attributes. */
24612 static inline void
24613 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24615 /* We can end up with BUILTINS_LOCATION here. */
24616 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24617 return;
24619 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
24621 if (dwarf_version >= 3 || !dwarf_strict)
24623 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24624 add_AT_unsigned (die, DW_AT_call_line, s.line);
24625 if (debug_column_info && s.column)
24626 add_AT_unsigned (die, DW_AT_call_column, s.column);
24631 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24632 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24634 static inline void
24635 add_high_low_attributes (tree stmt, dw_die_ref die)
24637 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24639 if (inline_entry_data **iedp
24640 = !inline_entry_data_table ? NULL
24641 : inline_entry_data_table->find_slot_with_hash (stmt,
24642 htab_hash_pointer (stmt),
24643 NO_INSERT))
24645 inline_entry_data *ied = *iedp;
24646 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24647 gcc_assert (debug_inline_points);
24648 gcc_assert (inlined_function_outer_scope_p (stmt));
24650 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24651 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24653 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24654 && !dwarf_strict)
24656 if (!output_asm_line_debug_info ())
24657 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24658 else
24660 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24661 /* FIXME: this will resolve to a small number. Could we
24662 possibly emit smaller data? Ideally we'd emit a
24663 uleb128, but that would make the size of DIEs
24664 impossible for the compiler to compute, since it's
24665 the assembler that computes the value of the view
24666 label in this case. Ideally, we'd have a single form
24667 encompassing both the address and the view, and
24668 indirecting them through a table might make things
24669 easier, but even that would be more wasteful,
24670 space-wise, than what we have now. */
24671 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24675 inline_entry_data_table->clear_slot (iedp);
24678 if (BLOCK_FRAGMENT_CHAIN (stmt)
24679 && (dwarf_version >= 3 || !dwarf_strict))
24681 tree chain, superblock = NULL_TREE;
24682 dw_die_ref pdie;
24683 dw_attr_node *attr = NULL;
24685 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24687 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24688 BLOCK_NUMBER (stmt));
24689 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24692 /* Optimize duplicate .debug_ranges lists or even tails of
24693 lists. If this BLOCK has same ranges as its supercontext,
24694 lookup DW_AT_ranges attribute in the supercontext (and
24695 recursively so), verify that the ranges_table contains the
24696 right values and use it instead of adding a new .debug_range. */
24697 for (chain = stmt, pdie = die;
24698 BLOCK_SAME_RANGE (chain);
24699 chain = BLOCK_SUPERCONTEXT (chain))
24701 dw_attr_node *new_attr;
24703 pdie = pdie->die_parent;
24704 if (pdie == NULL)
24705 break;
24706 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24707 break;
24708 new_attr = get_AT (pdie, DW_AT_ranges);
24709 if (new_attr == NULL
24710 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24711 break;
24712 attr = new_attr;
24713 superblock = BLOCK_SUPERCONTEXT (chain);
24715 if (attr != NULL
24716 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24717 == (int)BLOCK_NUMBER (superblock))
24718 && BLOCK_FRAGMENT_CHAIN (superblock))
24720 unsigned long off = attr->dw_attr_val.v.val_offset;
24721 unsigned long supercnt = 0, thiscnt = 0;
24722 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24723 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24725 ++supercnt;
24726 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24727 == (int)BLOCK_NUMBER (chain));
24729 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24730 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24731 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24732 ++thiscnt;
24733 gcc_assert (supercnt >= thiscnt);
24734 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24735 false);
24736 note_rnglist_head (off + supercnt - thiscnt);
24737 return;
24740 unsigned int offset = add_ranges (stmt, true);
24741 add_AT_range_list (die, DW_AT_ranges, offset, false);
24742 note_rnglist_head (offset);
24744 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24745 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24748 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24749 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24750 chain = BLOCK_FRAGMENT_CHAIN (chain);
24752 while (chain);
24753 add_ranges (NULL);
24755 else
24757 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24758 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24759 BLOCK_NUMBER (stmt));
24760 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24761 BLOCK_NUMBER (stmt));
24762 add_AT_low_high_pc (die, label, label_high, false);
24766 /* Generate a DIE for a lexical block. */
24768 static void
24769 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24771 dw_die_ref old_die = lookup_block_die (stmt);
24772 dw_die_ref stmt_die = NULL;
24773 if (!old_die)
24775 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24776 equate_block_to_die (stmt, stmt_die);
24779 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24781 /* If this is an inlined or conrecte instance, create a new lexical
24782 die for anything below to attach DW_AT_abstract_origin to. */
24783 if (old_die)
24784 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24786 tree origin = block_ultimate_origin (stmt);
24787 if (origin != NULL_TREE && (origin != stmt || old_die))
24788 add_abstract_origin_attribute (stmt_die, origin);
24790 old_die = NULL;
24793 if (old_die)
24794 stmt_die = old_die;
24796 /* A non abstract block whose blocks have already been reordered
24797 should have the instruction range for this block. If so, set the
24798 high/low attributes. */
24799 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24801 gcc_assert (stmt_die);
24802 add_high_low_attributes (stmt, stmt_die);
24805 decls_for_scope (stmt, stmt_die);
24808 /* Generate a DIE for an inlined subprogram. */
24810 static void
24811 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24813 tree decl = block_ultimate_origin (stmt);
24815 /* Make sure any inlined functions are known to be inlineable. */
24816 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24817 || cgraph_function_possibly_inlined_p (decl));
24819 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24821 if (call_arg_locations || debug_inline_points)
24822 equate_block_to_die (stmt, subr_die);
24823 add_abstract_origin_attribute (subr_die, decl);
24824 if (TREE_ASM_WRITTEN (stmt))
24825 add_high_low_attributes (stmt, subr_die);
24826 add_call_src_coords_attributes (stmt, subr_die);
24828 /* The inliner creates an extra BLOCK for the parameter setup,
24829 we want to merge that with the actual outermost BLOCK of the
24830 inlined function to avoid duplicate locals in consumers.
24831 Do that by doing the recursion to subblocks on the single subblock
24832 of STMT. */
24833 bool unwrap_one = false;
24834 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24836 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24837 if (origin
24838 && TREE_CODE (origin) == BLOCK
24839 && BLOCK_SUPERCONTEXT (origin) == decl)
24840 unwrap_one = true;
24842 decls_for_scope (stmt, subr_die, !unwrap_one);
24843 if (unwrap_one)
24844 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24847 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24848 the comment for VLR_CONTEXT. */
24850 static void
24851 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24853 dw_die_ref decl_die;
24855 if (TREE_TYPE (decl) == error_mark_node)
24856 return;
24858 decl_die = new_die (DW_TAG_member, context_die, decl);
24859 add_name_and_src_coords_attributes (decl_die, decl);
24860 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24861 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24862 context_die);
24864 if (DECL_BIT_FIELD_TYPE (decl))
24866 add_byte_size_attribute (decl_die, decl);
24867 add_bit_size_attribute (decl_die, decl);
24868 add_bit_offset_attribute (decl_die, decl);
24871 add_alignment_attribute (decl_die, decl);
24873 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24874 add_data_member_location_attribute (decl_die, decl, ctx);
24876 if (DECL_ARTIFICIAL (decl))
24877 add_AT_flag (decl_die, DW_AT_artificial, 1);
24879 add_accessibility_attribute (decl_die, decl);
24881 /* Equate decl number to die, so that we can look up this decl later on. */
24882 equate_decl_number_to_die (decl, decl_die);
24885 /* Generate a DIE for a pointer to a member type. TYPE can be an
24886 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24887 pointer to member function. */
24889 static void
24890 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24892 if (lookup_type_die (type))
24893 return;
24895 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24896 scope_die_for (type, context_die), type);
24898 equate_type_number_to_die (type, ptr_die);
24899 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24900 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24901 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24902 context_die);
24903 add_alignment_attribute (ptr_die, type);
24905 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24906 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24908 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24909 add_AT_loc (ptr_die, DW_AT_use_location, op);
24913 static char *producer_string;
24915 /* Given a C and/or C++ language/version string return the "highest".
24916 C++ is assumed to be "higher" than C in this case. Used for merging
24917 LTO translation unit languages. */
24918 static const char *
24919 highest_c_language (const char *lang1, const char *lang2)
24921 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
24922 return "GNU C++23";
24923 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
24924 return "GNU C++20";
24925 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24926 return "GNU C++17";
24927 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24928 return "GNU C++14";
24929 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24930 return "GNU C++11";
24931 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24932 return "GNU C++98";
24934 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24935 return "GNU C2X";
24936 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24937 return "GNU C17";
24938 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24939 return "GNU C11";
24940 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24941 return "GNU C99";
24942 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24943 return "GNU C89";
24945 gcc_unreachable ();
24949 /* Generate the DIE for the compilation unit. */
24951 static dw_die_ref
24952 gen_compile_unit_die (const char *filename)
24954 dw_die_ref die;
24955 const char *language_string = lang_hooks.name;
24956 int language;
24958 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24960 if (filename)
24962 add_filename_attribute (die, filename);
24963 /* Don't add cwd for <built-in>. */
24964 if (filename[0] != '<')
24965 add_comp_dir_attribute (die);
24968 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24970 /* If our producer is LTO try to figure out a common language to use
24971 from the global list of translation units. */
24972 if (strcmp (language_string, "GNU GIMPLE") == 0)
24974 unsigned i;
24975 tree t;
24976 const char *common_lang = NULL;
24978 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24980 if (!TRANSLATION_UNIT_LANGUAGE (t))
24981 continue;
24982 if (!common_lang)
24983 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24984 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24986 else if (startswith (common_lang, "GNU C")
24987 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
24988 /* Mixing C and C++ is ok, use C++ in that case. */
24989 common_lang = highest_c_language (common_lang,
24990 TRANSLATION_UNIT_LANGUAGE (t));
24991 else
24993 /* Fall back to C. */
24994 common_lang = NULL;
24995 break;
24999 if (common_lang)
25000 language_string = common_lang;
25003 language = DW_LANG_C;
25004 if (startswith (language_string, "GNU C")
25005 && ISDIGIT (language_string[5]))
25007 language = DW_LANG_C89;
25008 if (dwarf_version >= 3 || !dwarf_strict)
25010 if (strcmp (language_string, "GNU C89") != 0)
25011 language = DW_LANG_C99;
25013 if (dwarf_version >= 5 /* || !dwarf_strict */)
25014 if (strcmp (language_string, "GNU C11") == 0
25015 || strcmp (language_string, "GNU C17") == 0
25016 || strcmp (language_string, "GNU C2X") == 0)
25017 language = DW_LANG_C11;
25020 else if (startswith (language_string, "GNU C++"))
25022 language = DW_LANG_C_plus_plus;
25023 if (dwarf_version >= 5 /* || !dwarf_strict */)
25025 if (strcmp (language_string, "GNU C++11") == 0)
25026 language = DW_LANG_C_plus_plus_11;
25027 else if (strcmp (language_string, "GNU C++14") == 0)
25028 language = DW_LANG_C_plus_plus_14;
25029 else if (strcmp (language_string, "GNU C++17") == 0
25030 || strcmp (language_string, "GNU C++20") == 0
25031 || strcmp (language_string, "GNU C++23") == 0)
25032 /* For now. */
25033 language = DW_LANG_C_plus_plus_14;
25036 else if (strcmp (language_string, "GNU F77") == 0)
25037 language = DW_LANG_Fortran77;
25038 else if (dwarf_version >= 3 || !dwarf_strict)
25040 if (strcmp (language_string, "GNU Ada") == 0)
25041 language = DW_LANG_Ada95;
25042 else if (startswith (language_string, "GNU Fortran"))
25044 language = DW_LANG_Fortran95;
25045 if (dwarf_version >= 5 /* || !dwarf_strict */)
25047 if (strcmp (language_string, "GNU Fortran2003") == 0)
25048 language = DW_LANG_Fortran03;
25049 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25050 language = DW_LANG_Fortran08;
25053 else if (strcmp (language_string, "GNU Objective-C") == 0)
25054 language = DW_LANG_ObjC;
25055 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25056 language = DW_LANG_ObjC_plus_plus;
25057 else if (strcmp (language_string, "GNU D") == 0)
25058 language = DW_LANG_D;
25059 else if (dwarf_version >= 5 || !dwarf_strict)
25061 if (strcmp (language_string, "GNU Go") == 0)
25062 language = DW_LANG_Go;
25065 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25066 else if (startswith (language_string, "GNU Fortran"))
25067 language = DW_LANG_Fortran90;
25068 /* Likewise for Ada. */
25069 else if (strcmp (language_string, "GNU Ada") == 0)
25070 language = DW_LANG_Ada83;
25072 add_AT_unsigned (die, DW_AT_language, language);
25074 switch (language)
25076 case DW_LANG_Fortran77:
25077 case DW_LANG_Fortran90:
25078 case DW_LANG_Fortran95:
25079 case DW_LANG_Fortran03:
25080 case DW_LANG_Fortran08:
25081 /* Fortran has case insensitive identifiers and the front-end
25082 lowercases everything. */
25083 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25084 break;
25085 default:
25086 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25087 break;
25089 return die;
25092 /* Generate the DIE for a base class. */
25094 static void
25095 gen_inheritance_die (tree binfo, tree access, tree type,
25096 dw_die_ref context_die)
25098 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25099 struct vlr_context ctx = { type, NULL };
25101 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25102 context_die);
25103 add_data_member_location_attribute (die, binfo, &ctx);
25105 if (BINFO_VIRTUAL_P (binfo))
25106 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25108 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25109 children, otherwise the default is DW_ACCESS_public. In DWARF2
25110 the default has always been DW_ACCESS_private. */
25111 if (access == access_public_node)
25113 if (dwarf_version == 2
25114 || context_die->die_tag == DW_TAG_class_type)
25115 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25117 else if (access == access_protected_node)
25118 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25119 else if (dwarf_version > 2
25120 && context_die->die_tag != DW_TAG_class_type)
25121 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25124 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25125 structure. */
25127 static bool
25128 is_variant_part (tree decl)
25130 return (TREE_CODE (decl) == FIELD_DECL
25131 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25134 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25135 return the FIELD_DECL. Return NULL_TREE otherwise. */
25137 static tree
25138 analyze_discr_in_predicate (tree operand, tree struct_type)
25140 while (CONVERT_EXPR_P (operand))
25141 operand = TREE_OPERAND (operand, 0);
25143 /* Match field access to members of struct_type only. */
25144 if (TREE_CODE (operand) == COMPONENT_REF
25145 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25146 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25147 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25148 return TREE_OPERAND (operand, 1);
25149 else
25150 return NULL_TREE;
25153 /* Check that SRC is a constant integer that can be represented as a native
25154 integer constant (either signed or unsigned). If so, store it into DEST and
25155 return true. Return false otherwise. */
25157 static bool
25158 get_discr_value (tree src, dw_discr_value *dest)
25160 tree discr_type = TREE_TYPE (src);
25162 if (lang_hooks.types.get_debug_type)
25164 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25165 if (debug_type != NULL)
25166 discr_type = debug_type;
25169 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25170 return false;
25172 /* Signedness can vary between the original type and the debug type. This
25173 can happen for character types in Ada for instance: the character type
25174 used for code generation can be signed, to be compatible with the C one,
25175 but from a debugger point of view, it must be unsigned. */
25176 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25177 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25179 if (is_orig_unsigned != is_debug_unsigned)
25180 src = fold_convert (discr_type, src);
25182 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25183 return false;
25185 dest->pos = is_debug_unsigned;
25186 if (is_debug_unsigned)
25187 dest->v.uval = tree_to_uhwi (src);
25188 else
25189 dest->v.sval = tree_to_shwi (src);
25191 return true;
25194 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25195 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25196 store NULL_TREE in DISCR_DECL. Otherwise:
25198 - store the discriminant field in STRUCT_TYPE that controls the variant
25199 part to *DISCR_DECL
25201 - put in *DISCR_LISTS_P an array where for each variant, the item
25202 represents the corresponding matching list of discriminant values.
25204 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25205 the above array.
25207 Note that when the array is allocated (i.e. when the analysis is
25208 successful), it is up to the caller to free the array. */
25210 static void
25211 analyze_variants_discr (tree variant_part_decl,
25212 tree struct_type,
25213 tree *discr_decl,
25214 dw_discr_list_ref **discr_lists_p,
25215 unsigned *discr_lists_length)
25217 tree variant_part_type = TREE_TYPE (variant_part_decl);
25218 tree variant;
25219 dw_discr_list_ref *discr_lists;
25220 unsigned i;
25222 /* Compute how many variants there are in this variant part. */
25223 *discr_lists_length = 0;
25224 for (variant = TYPE_FIELDS (variant_part_type);
25225 variant != NULL_TREE;
25226 variant = DECL_CHAIN (variant))
25227 ++*discr_lists_length;
25229 *discr_decl = NULL_TREE;
25230 *discr_lists_p
25231 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25232 sizeof (**discr_lists_p));
25233 discr_lists = *discr_lists_p;
25235 /* And then analyze all variants to extract discriminant information for all
25236 of them. This analysis is conservative: as soon as we detect something we
25237 do not support, abort everything and pretend we found nothing. */
25238 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25239 variant != NULL_TREE;
25240 variant = DECL_CHAIN (variant), ++i)
25242 tree match_expr = DECL_QUALIFIER (variant);
25244 /* Now, try to analyze the predicate and deduce a discriminant for
25245 it. */
25246 if (match_expr == boolean_true_node)
25247 /* Typically happens for the default variant: it matches all cases that
25248 previous variants rejected. Don't output any matching value for
25249 this one. */
25250 continue;
25252 /* The following loop tries to iterate over each discriminant
25253 possibility: single values or ranges. */
25254 while (match_expr != NULL_TREE)
25256 tree next_round_match_expr;
25257 tree candidate_discr = NULL_TREE;
25258 dw_discr_list_ref new_node = NULL;
25260 /* Possibilities are matched one after the other by nested
25261 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25262 continue with the rest at next iteration. */
25263 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25265 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25266 match_expr = TREE_OPERAND (match_expr, 1);
25268 else
25269 next_round_match_expr = NULL_TREE;
25271 if (match_expr == boolean_false_node)
25272 /* This sub-expression matches nothing: just wait for the next
25273 one. */
25276 else if (TREE_CODE (match_expr) == EQ_EXPR)
25278 /* We are matching: <discr_field> == <integer_cst>
25279 This sub-expression matches a single value. */
25280 tree integer_cst = TREE_OPERAND (match_expr, 1);
25282 candidate_discr
25283 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25284 struct_type);
25286 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25287 if (!get_discr_value (integer_cst,
25288 &new_node->dw_discr_lower_bound))
25289 goto abort;
25290 new_node->dw_discr_range = false;
25293 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25295 /* We are matching:
25296 <discr_field> > <integer_cst>
25297 && <discr_field> < <integer_cst>.
25298 This sub-expression matches the range of values between the
25299 two matched integer constants. Note that comparisons can be
25300 inclusive or exclusive. */
25301 tree candidate_discr_1, candidate_discr_2;
25302 tree lower_cst, upper_cst;
25303 bool lower_cst_included, upper_cst_included;
25304 tree lower_op = TREE_OPERAND (match_expr, 0);
25305 tree upper_op = TREE_OPERAND (match_expr, 1);
25307 /* When the comparison is exclusive, the integer constant is not
25308 the discriminant range bound we are looking for: we will have
25309 to increment or decrement it. */
25310 if (TREE_CODE (lower_op) == GE_EXPR)
25311 lower_cst_included = true;
25312 else if (TREE_CODE (lower_op) == GT_EXPR)
25313 lower_cst_included = false;
25314 else
25315 goto abort;
25317 if (TREE_CODE (upper_op) == LE_EXPR)
25318 upper_cst_included = true;
25319 else if (TREE_CODE (upper_op) == LT_EXPR)
25320 upper_cst_included = false;
25321 else
25322 goto abort;
25324 /* Extract the discriminant from the first operand and check it
25325 is consistant with the same analysis in the second
25326 operand. */
25327 candidate_discr_1
25328 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25329 struct_type);
25330 candidate_discr_2
25331 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25332 struct_type);
25333 if (candidate_discr_1 == candidate_discr_2)
25334 candidate_discr = candidate_discr_1;
25335 else
25336 goto abort;
25338 /* Extract bounds from both. */
25339 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25340 lower_cst = TREE_OPERAND (lower_op, 1);
25341 upper_cst = TREE_OPERAND (upper_op, 1);
25343 if (!lower_cst_included)
25344 lower_cst
25345 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25346 build_int_cst (TREE_TYPE (lower_cst), 1));
25347 if (!upper_cst_included)
25348 upper_cst
25349 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25350 build_int_cst (TREE_TYPE (upper_cst), 1));
25352 if (!get_discr_value (lower_cst,
25353 &new_node->dw_discr_lower_bound)
25354 || !get_discr_value (upper_cst,
25355 &new_node->dw_discr_upper_bound))
25356 goto abort;
25358 new_node->dw_discr_range = true;
25361 else if ((candidate_discr
25362 = analyze_discr_in_predicate (match_expr, struct_type))
25363 && (TREE_TYPE (candidate_discr) == boolean_type_node
25364 || TREE_TYPE (TREE_TYPE (candidate_discr))
25365 == boolean_type_node))
25367 /* We are matching: <discr_field> for a boolean discriminant.
25368 This sub-expression matches boolean_true_node. */
25369 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25370 if (!get_discr_value (boolean_true_node,
25371 &new_node->dw_discr_lower_bound))
25372 goto abort;
25373 new_node->dw_discr_range = false;
25376 else
25377 /* Unsupported sub-expression: we cannot determine the set of
25378 matching discriminant values. Abort everything. */
25379 goto abort;
25381 /* If the discriminant info is not consistant with what we saw so
25382 far, consider the analysis failed and abort everything. */
25383 if (candidate_discr == NULL_TREE
25384 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25385 goto abort;
25386 else
25387 *discr_decl = candidate_discr;
25389 if (new_node != NULL)
25391 new_node->dw_discr_next = discr_lists[i];
25392 discr_lists[i] = new_node;
25394 match_expr = next_round_match_expr;
25398 /* If we reach this point, we could match everything we were interested
25399 in. */
25400 return;
25402 abort:
25403 /* Clean all data structure and return no result. */
25404 free (*discr_lists_p);
25405 *discr_lists_p = NULL;
25406 *discr_decl = NULL_TREE;
25409 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25410 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25411 under CONTEXT_DIE.
25413 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25414 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25415 this type, which are record types, represent the available variants and each
25416 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25417 values are inferred from these attributes.
25419 In trees, the offsets for the fields inside these sub-records are relative
25420 to the variant part itself, whereas the corresponding DIEs should have
25421 offset attributes that are relative to the embedding record base address.
25422 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25423 must be an expression that computes the offset of the variant part to
25424 describe in DWARF. */
25426 static void
25427 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25428 dw_die_ref context_die)
25430 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25431 tree variant_part_offset = vlr_ctx->variant_part_offset;
25433 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25434 NULL_TREE if there is no such field. */
25435 tree discr_decl = NULL_TREE;
25436 dw_discr_list_ref *discr_lists;
25437 unsigned discr_lists_length = 0;
25438 unsigned i;
25440 dw_die_ref dwarf_proc_die = NULL;
25441 dw_die_ref variant_part_die
25442 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25444 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25446 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25447 &discr_decl, &discr_lists, &discr_lists_length);
25449 if (discr_decl != NULL_TREE)
25451 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25453 if (discr_die)
25454 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25455 else
25456 /* We have no DIE for the discriminant, so just discard all
25457 discrimimant information in the output. */
25458 discr_decl = NULL_TREE;
25461 /* If the offset for this variant part is more complex than a constant,
25462 create a DWARF procedure for it so that we will not have to generate
25463 DWARF expressions for it for each member. */
25464 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25465 && (dwarf_version >= 3 || !dwarf_strict))
25467 struct loc_descr_context ctx = {
25468 vlr_ctx->struct_type, /* context_type */
25469 NULL_TREE, /* base_decl */
25470 NULL, /* dpi */
25471 false, /* placeholder_arg */
25472 false, /* placeholder_seen */
25473 false /* strict_signedness */
25475 const tree dwarf_proc_fndecl
25476 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25477 build_function_type (TREE_TYPE (variant_part_offset),
25478 NULL_TREE));
25479 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25480 const dw_loc_descr_ref dwarf_proc_body
25481 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25483 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25484 dwarf_proc_fndecl, context_die);
25485 if (dwarf_proc_die != NULL)
25486 variant_part_offset = dwarf_proc_call;
25489 /* Output DIEs for all variants. */
25490 i = 0;
25491 for (tree variant = TYPE_FIELDS (variant_part_type);
25492 variant != NULL_TREE;
25493 variant = DECL_CHAIN (variant), ++i)
25495 tree variant_type = TREE_TYPE (variant);
25496 dw_die_ref variant_die;
25498 /* All variants (i.e. members of a variant part) are supposed to be
25499 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25500 under these records. */
25501 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25503 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25504 equate_decl_number_to_die (variant, variant_die);
25506 /* Output discriminant values this variant matches, if any. */
25507 if (discr_decl == NULL || discr_lists[i] == NULL)
25508 /* In the case we have discriminant information at all, this is
25509 probably the default variant: as the standard says, don't
25510 output any discriminant value/list attribute. */
25512 else if (discr_lists[i]->dw_discr_next == NULL
25513 && !discr_lists[i]->dw_discr_range)
25514 /* If there is only one accepted value, don't bother outputting a
25515 list. */
25516 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25517 else
25518 add_discr_list (variant_die, discr_lists[i]);
25520 for (tree member = TYPE_FIELDS (variant_type);
25521 member != NULL_TREE;
25522 member = DECL_CHAIN (member))
25524 struct vlr_context vlr_sub_ctx = {
25525 vlr_ctx->struct_type, /* struct_type */
25526 NULL /* variant_part_offset */
25528 if (is_variant_part (member))
25530 /* All offsets for fields inside variant parts are relative to
25531 the top-level embedding RECORD_TYPE's base address. On the
25532 other hand, offsets in GCC's types are relative to the
25533 nested-most variant part. So we have to sum offsets each time
25534 we recurse. */
25536 vlr_sub_ctx.variant_part_offset
25537 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25538 variant_part_offset, byte_position (member));
25539 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25541 else
25543 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25544 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25549 free (discr_lists);
25552 /* Generate a DIE for a class member. */
25554 static void
25555 gen_member_die (tree type, dw_die_ref context_die)
25557 tree member;
25558 tree binfo = TYPE_BINFO (type);
25560 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25562 /* If this is not an incomplete type, output descriptions of each of its
25563 members. Note that as we output the DIEs necessary to represent the
25564 members of this record or union type, we will also be trying to output
25565 DIEs to represent the *types* of those members. However the `type'
25566 function (above) will specifically avoid generating type DIEs for member
25567 types *within* the list of member DIEs for this (containing) type except
25568 for those types (of members) which are explicitly marked as also being
25569 members of this (containing) type themselves. The g++ front- end can
25570 force any given type to be treated as a member of some other (containing)
25571 type by setting the TYPE_CONTEXT of the given (member) type to point to
25572 the TREE node representing the appropriate (containing) type. */
25574 /* First output info about the base classes. */
25575 if (binfo && early_dwarf)
25577 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25578 int i;
25579 tree base;
25581 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25582 gen_inheritance_die (base,
25583 (accesses ? (*accesses)[i] : access_public_node),
25584 type,
25585 context_die);
25588 /* Now output info about the members. */
25589 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25591 /* Ignore clones. */
25592 if (DECL_ABSTRACT_ORIGIN (member))
25593 continue;
25595 struct vlr_context vlr_ctx = { type, NULL_TREE };
25596 bool static_inline_p
25597 = (VAR_P (member)
25598 && TREE_STATIC (member)
25599 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25600 != -1));
25602 /* If we thought we were generating minimal debug info for TYPE
25603 and then changed our minds, some of the member declarations
25604 may have already been defined. Don't define them again, but
25605 do put them in the right order. */
25607 if (dw_die_ref child = lookup_decl_die (member))
25609 /* Handle inline static data members, which only have in-class
25610 declarations. */
25611 bool splice = true;
25613 dw_die_ref ref = NULL;
25614 if (child->die_tag == DW_TAG_variable
25615 && child->die_parent == comp_unit_die ())
25617 ref = get_AT_ref (child, DW_AT_specification);
25619 /* For C++17 inline static data members followed by redundant
25620 out of class redeclaration, we might get here with
25621 child being the DIE created for the out of class
25622 redeclaration and with its DW_AT_specification being
25623 the DIE created for in-class definition. We want to
25624 reparent the latter, and don't want to create another
25625 DIE with DW_AT_specification in that case, because
25626 we already have one. */
25627 if (ref
25628 && static_inline_p
25629 && ref->die_tag == DW_TAG_variable
25630 && ref->die_parent == comp_unit_die ()
25631 && get_AT (ref, DW_AT_specification) == NULL)
25633 child = ref;
25634 ref = NULL;
25635 static_inline_p = false;
25638 if (!ref)
25640 reparent_child (child, context_die);
25641 if (dwarf_version < 5)
25642 child->die_tag = DW_TAG_member;
25643 splice = false;
25646 else if (child->die_tag == DW_TAG_enumerator)
25647 /* Enumerators remain under their enumeration even if
25648 their names are introduced in the enclosing scope. */
25649 splice = false;
25651 if (splice)
25652 splice_child_die (context_die, child);
25655 /* Do not generate DWARF for variant parts if we are generating the
25656 corresponding GNAT encodings: DIEs generated for the two schemes
25657 would conflict in our mappings. */
25658 else if (is_variant_part (member)
25659 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
25661 vlr_ctx.variant_part_offset = byte_position (member);
25662 gen_variant_part (member, &vlr_ctx, context_die);
25664 else
25666 vlr_ctx.variant_part_offset = NULL_TREE;
25667 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25670 /* For C++ inline static data members emit immediately a DW_TAG_variable
25671 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25672 DW_AT_specification. */
25673 if (static_inline_p)
25675 int old_extern = DECL_EXTERNAL (member);
25676 DECL_EXTERNAL (member) = 0;
25677 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25678 DECL_EXTERNAL (member) = old_extern;
25683 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25684 is set, we pretend that the type was never defined, so we only get the
25685 member DIEs needed by later specification DIEs. */
25687 static void
25688 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25689 enum debug_info_usage usage)
25691 if (TREE_ASM_WRITTEN (type))
25693 /* Fill in the bound of variable-length fields in late dwarf if
25694 still incomplete. */
25695 if (!early_dwarf && variably_modified_type_p (type, NULL))
25696 for (tree member = TYPE_FIELDS (type);
25697 member;
25698 member = DECL_CHAIN (member))
25699 fill_variable_array_bounds (TREE_TYPE (member));
25700 return;
25703 dw_die_ref type_die = lookup_type_die (type);
25704 dw_die_ref scope_die = 0;
25705 int nested = 0;
25706 int complete = (TYPE_SIZE (type)
25707 && (! TYPE_STUB_DECL (type)
25708 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25709 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25710 complete = complete && should_emit_struct_debug (type, usage);
25712 if (type_die && ! complete)
25713 return;
25715 if (TYPE_CONTEXT (type) != NULL_TREE
25716 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25717 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25718 nested = 1;
25720 scope_die = scope_die_for (type, context_die);
25722 /* Generate child dies for template parameters. */
25723 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25724 schedule_generic_params_dies_gen (type);
25726 if (! type_die || (nested && is_cu_die (scope_die)))
25727 /* First occurrence of type or toplevel definition of nested class. */
25729 dw_die_ref old_die = type_die;
25731 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25732 ? record_type_tag (type) : DW_TAG_union_type,
25733 scope_die, type);
25734 equate_type_number_to_die (type, type_die);
25735 if (old_die)
25736 add_AT_specification (type_die, old_die);
25737 else
25738 add_name_attribute (type_die, type_tag (type));
25740 else
25741 remove_AT (type_die, DW_AT_declaration);
25743 /* If this type has been completed, then give it a byte_size attribute and
25744 then give a list of members. */
25745 if (complete && !ns_decl)
25747 /* Prevent infinite recursion in cases where the type of some member of
25748 this type is expressed in terms of this type itself. */
25749 TREE_ASM_WRITTEN (type) = 1;
25750 add_byte_size_attribute (type_die, type);
25751 add_alignment_attribute (type_die, type);
25752 if (TYPE_STUB_DECL (type) != NULL_TREE)
25754 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25755 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25758 /* If the first reference to this type was as the return type of an
25759 inline function, then it may not have a parent. Fix this now. */
25760 if (type_die->die_parent == NULL)
25761 add_child_die (scope_die, type_die);
25763 gen_member_die (type, type_die);
25765 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25766 if (TYPE_ARTIFICIAL (type))
25767 add_AT_flag (type_die, DW_AT_artificial, 1);
25769 /* GNU extension: Record what type our vtable lives in. */
25770 if (TYPE_VFIELD (type))
25772 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25774 gen_type_die (vtype, context_die);
25775 add_AT_die_ref (type_die, DW_AT_containing_type,
25776 lookup_type_die (vtype));
25779 else
25781 add_AT_flag (type_die, DW_AT_declaration, 1);
25783 /* We don't need to do this for function-local types. */
25784 if (TYPE_STUB_DECL (type)
25785 && ! decl_function_context (TYPE_STUB_DECL (type)))
25786 vec_safe_push (incomplete_types, type);
25789 if (get_AT (type_die, DW_AT_name))
25790 add_pubtype (type, type_die);
25793 /* Generate a DIE for a subroutine _type_. */
25795 static void
25796 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25798 tree return_type = TREE_TYPE (type);
25799 dw_die_ref subr_die
25800 = new_die (DW_TAG_subroutine_type,
25801 scope_die_for (type, context_die), type);
25803 equate_type_number_to_die (type, subr_die);
25804 add_prototyped_attribute (subr_die, type);
25805 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25806 context_die);
25807 add_alignment_attribute (subr_die, type);
25808 gen_formal_types_die (type, subr_die);
25810 if (get_AT (subr_die, DW_AT_name))
25811 add_pubtype (type, subr_die);
25812 if ((dwarf_version >= 5 || !dwarf_strict)
25813 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25814 add_AT_flag (subr_die, DW_AT_reference, 1);
25815 if ((dwarf_version >= 5 || !dwarf_strict)
25816 && lang_hooks.types.type_dwarf_attribute (type,
25817 DW_AT_rvalue_reference) != -1)
25818 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25821 /* Generate a DIE for a type definition. */
25823 static void
25824 gen_typedef_die (tree decl, dw_die_ref context_die)
25826 dw_die_ref type_die;
25827 tree type;
25829 if (TREE_ASM_WRITTEN (decl))
25831 if (DECL_ORIGINAL_TYPE (decl))
25832 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25833 return;
25836 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25837 checks in process_scope_var and modified_type_die), this should be called
25838 only for original types. */
25839 gcc_assert (decl_ultimate_origin (decl) == NULL
25840 || decl_ultimate_origin (decl) == decl);
25842 TREE_ASM_WRITTEN (decl) = 1;
25843 type_die = new_die (DW_TAG_typedef, context_die, decl);
25845 add_name_and_src_coords_attributes (type_die, decl);
25846 if (DECL_ORIGINAL_TYPE (decl))
25848 type = DECL_ORIGINAL_TYPE (decl);
25849 if (type == error_mark_node)
25850 return;
25852 gcc_assert (type != TREE_TYPE (decl));
25853 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25855 else
25857 type = TREE_TYPE (decl);
25858 if (type == error_mark_node)
25859 return;
25861 if (is_naming_typedef_decl (TYPE_NAME (type)))
25863 /* Here, we are in the case of decl being a typedef naming
25864 an anonymous type, e.g:
25865 typedef struct {...} foo;
25866 In that case TREE_TYPE (decl) is not a typedef variant
25867 type and TYPE_NAME of the anonymous type is set to the
25868 TYPE_DECL of the typedef. This construct is emitted by
25869 the C++ FE.
25871 TYPE is the anonymous struct named by the typedef
25872 DECL. As we need the DW_AT_type attribute of the
25873 DW_TAG_typedef to point to the DIE of TYPE, let's
25874 generate that DIE right away. add_type_attribute
25875 called below will then pick (via lookup_type_die) that
25876 anonymous struct DIE. */
25877 if (!TREE_ASM_WRITTEN (type))
25878 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25880 /* This is a GNU Extension. We are adding a
25881 DW_AT_linkage_name attribute to the DIE of the
25882 anonymous struct TYPE. The value of that attribute
25883 is the name of the typedef decl naming the anonymous
25884 struct. This greatly eases the work of consumers of
25885 this debug info. */
25886 add_linkage_name_raw (lookup_type_die (type), decl);
25890 add_type_attribute (type_die, type, decl_quals (decl), false,
25891 context_die);
25893 if (is_naming_typedef_decl (decl))
25894 /* We want that all subsequent calls to lookup_type_die with
25895 TYPE in argument yield the DW_TAG_typedef we have just
25896 created. */
25897 equate_type_number_to_die (type, type_die);
25899 add_alignment_attribute (type_die, TREE_TYPE (decl));
25901 add_accessibility_attribute (type_die, decl);
25903 if (DECL_ABSTRACT_P (decl))
25904 equate_decl_number_to_die (decl, type_die);
25906 if (get_AT (type_die, DW_AT_name))
25907 add_pubtype (decl, type_die);
25910 /* Generate a DIE for a struct, class, enum or union type. */
25912 static void
25913 gen_tagged_type_die (tree type,
25914 dw_die_ref context_die,
25915 enum debug_info_usage usage)
25917 if (type == NULL_TREE
25918 || !is_tagged_type (type))
25919 return;
25921 if (TREE_ASM_WRITTEN (type))
25923 /* If this is a nested type whose containing class hasn't been written
25924 out yet, writing it out will cover this one, too. This does not apply
25925 to instantiations of member class templates; they need to be added to
25926 the containing class as they are generated. FIXME: This hurts the
25927 idea of combining type decls from multiple TUs, since we can't predict
25928 what set of template instantiations we'll get. */
25929 else if (TYPE_CONTEXT (type)
25930 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25931 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25933 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25935 if (TREE_ASM_WRITTEN (type))
25936 return;
25938 /* If that failed, attach ourselves to the stub. */
25939 context_die = lookup_type_die (TYPE_CONTEXT (type));
25941 else if (TYPE_CONTEXT (type) != NULL_TREE
25942 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25944 /* If this type is local to a function that hasn't been written
25945 out yet, use a NULL context for now; it will be fixed up in
25946 decls_for_scope. */
25947 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25948 /* A declaration DIE doesn't count; nested types need to go in the
25949 specification. */
25950 if (context_die && is_declaration_die (context_die))
25951 context_die = NULL;
25953 else
25954 context_die = declare_in_namespace (type, context_die);
25956 if (TREE_CODE (type) == ENUMERAL_TYPE)
25958 /* This might have been written out by the call to
25959 declare_in_namespace. */
25960 if (!TREE_ASM_WRITTEN (type))
25961 gen_enumeration_type_die (type, context_die);
25963 else
25964 gen_struct_or_union_type_die (type, context_die, usage);
25966 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25967 it up if it is ever completed. gen_*_type_die will set it for us
25968 when appropriate. */
25971 /* Generate a type description DIE. */
25973 static void
25974 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25975 enum debug_info_usage usage)
25977 struct array_descr_info info;
25979 if (type == NULL_TREE || type == error_mark_node)
25980 return;
25982 if (flag_checking && type)
25983 verify_type (type);
25985 if (TYPE_NAME (type) != NULL_TREE
25986 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25987 && is_redundant_typedef (TYPE_NAME (type))
25988 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25989 /* The DECL of this type is a typedef we don't want to emit debug
25990 info for but we want debug info for its underlying typedef.
25991 This can happen for e.g, the injected-class-name of a C++
25992 type. */
25993 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25995 /* If TYPE is a typedef type variant, let's generate debug info
25996 for the parent typedef which TYPE is a type of. */
25997 if (typedef_variant_p (type))
25999 if (TREE_ASM_WRITTEN (type))
26000 return;
26002 tree name = TYPE_NAME (type);
26003 tree origin = decl_ultimate_origin (name);
26004 if (origin != NULL && origin != name)
26006 gen_decl_die (origin, NULL, NULL, context_die);
26007 return;
26010 /* Prevent broken recursion; we can't hand off to the same type. */
26011 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26013 /* Give typedefs the right scope. */
26014 context_die = scope_die_for (type, context_die);
26016 TREE_ASM_WRITTEN (type) = 1;
26018 gen_decl_die (name, NULL, NULL, context_die);
26019 return;
26022 /* If type is an anonymous tagged type named by a typedef, let's
26023 generate debug info for the typedef. */
26024 if (is_naming_typedef_decl (TYPE_NAME (type)))
26026 /* Give typedefs the right scope. */
26027 context_die = scope_die_for (type, context_die);
26029 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26030 return;
26033 if (lang_hooks.types.get_debug_type)
26035 tree debug_type = lang_hooks.types.get_debug_type (type);
26037 if (debug_type != NULL_TREE && debug_type != type)
26039 gen_type_die_with_usage (debug_type, context_die, usage);
26040 return;
26044 /* We are going to output a DIE to represent the unqualified version
26045 of this type (i.e. without any const or volatile qualifiers) so
26046 get the main variant (i.e. the unqualified version) of this type
26047 now. (Vectors and arrays are special because the debugging info is in the
26048 cloned type itself. Similarly function/method types can contain extra
26049 ref-qualification). */
26050 if (TREE_CODE (type) == FUNCTION_TYPE
26051 || TREE_CODE (type) == METHOD_TYPE)
26053 /* For function/method types, can't use type_main_variant here,
26054 because that can have different ref-qualifiers for C++,
26055 but try to canonicalize. */
26056 tree main = TYPE_MAIN_VARIANT (type);
26057 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26058 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26059 && check_base_type (t, main)
26060 && check_lang_type (t, type))
26062 type = t;
26063 break;
26066 else if (TREE_CODE (type) != VECTOR_TYPE
26067 && TREE_CODE (type) != ARRAY_TYPE)
26068 type = type_main_variant (type);
26070 /* If this is an array type with hidden descriptor, handle it first. */
26071 if (!TREE_ASM_WRITTEN (type)
26072 && lang_hooks.types.get_array_descr_info)
26074 memset (&info, 0, sizeof (info));
26075 if (lang_hooks.types.get_array_descr_info (type, &info))
26077 /* Fortran sometimes emits array types with no dimension. */
26078 gcc_assert (info.ndimensions >= 0
26079 && (info.ndimensions
26080 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26081 gen_descr_array_type_die (type, &info, context_die);
26082 TREE_ASM_WRITTEN (type) = 1;
26083 return;
26087 if (TREE_ASM_WRITTEN (type))
26089 /* Variable-length types may be incomplete even if
26090 TREE_ASM_WRITTEN. For such types, fall through to
26091 gen_array_type_die() and possibly fill in
26092 DW_AT_{upper,lower}_bound attributes. */
26093 if ((TREE_CODE (type) != ARRAY_TYPE
26094 && TREE_CODE (type) != RECORD_TYPE
26095 && TREE_CODE (type) != UNION_TYPE
26096 && TREE_CODE (type) != QUAL_UNION_TYPE)
26097 || !variably_modified_type_p (type, NULL))
26098 return;
26101 switch (TREE_CODE (type))
26103 case ERROR_MARK:
26104 break;
26106 case POINTER_TYPE:
26107 case REFERENCE_TYPE:
26108 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26109 ensures that the gen_type_die recursion will terminate even if the
26110 type is recursive. Recursive types are possible in Ada. */
26111 /* ??? We could perhaps do this for all types before the switch
26112 statement. */
26113 TREE_ASM_WRITTEN (type) = 1;
26115 /* For these types, all that is required is that we output a DIE (or a
26116 set of DIEs) to represent the "basis" type. */
26117 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26118 DINFO_USAGE_IND_USE);
26119 break;
26121 case OFFSET_TYPE:
26122 /* This code is used for C++ pointer-to-data-member types.
26123 Output a description of the relevant class type. */
26124 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26125 DINFO_USAGE_IND_USE);
26127 /* Output a description of the type of the object pointed to. */
26128 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26129 DINFO_USAGE_IND_USE);
26131 /* Now output a DIE to represent this pointer-to-data-member type
26132 itself. */
26133 gen_ptr_to_mbr_type_die (type, context_die);
26134 break;
26136 case FUNCTION_TYPE:
26137 /* Force out return type (in case it wasn't forced out already). */
26138 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26139 DINFO_USAGE_DIR_USE);
26140 gen_subroutine_type_die (type, context_die);
26141 break;
26143 case METHOD_TYPE:
26144 /* Force out return type (in case it wasn't forced out already). */
26145 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26146 DINFO_USAGE_DIR_USE);
26147 gen_subroutine_type_die (type, context_die);
26148 break;
26150 case ARRAY_TYPE:
26151 case VECTOR_TYPE:
26152 gen_array_type_die (type, context_die);
26153 break;
26155 case ENUMERAL_TYPE:
26156 case RECORD_TYPE:
26157 case UNION_TYPE:
26158 case QUAL_UNION_TYPE:
26159 gen_tagged_type_die (type, context_die, usage);
26160 return;
26162 case VOID_TYPE:
26163 case OPAQUE_TYPE:
26164 case INTEGER_TYPE:
26165 case REAL_TYPE:
26166 case FIXED_POINT_TYPE:
26167 case COMPLEX_TYPE:
26168 case BOOLEAN_TYPE:
26169 /* No DIEs needed for fundamental types. */
26170 break;
26172 case NULLPTR_TYPE:
26173 case LANG_TYPE:
26174 /* Just use DW_TAG_unspecified_type. */
26176 dw_die_ref type_die = lookup_type_die (type);
26177 if (type_die == NULL)
26179 tree name = TYPE_IDENTIFIER (type);
26180 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26181 type);
26182 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26183 equate_type_number_to_die (type, type_die);
26186 break;
26188 default:
26189 if (is_cxx_auto (type))
26191 tree name = TYPE_IDENTIFIER (type);
26192 dw_die_ref *die = (name == get_identifier ("auto")
26193 ? &auto_die : &decltype_auto_die);
26194 if (!*die)
26196 *die = new_die (DW_TAG_unspecified_type,
26197 comp_unit_die (), NULL_TREE);
26198 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26200 equate_type_number_to_die (type, *die);
26201 break;
26203 gcc_unreachable ();
26206 TREE_ASM_WRITTEN (type) = 1;
26209 static void
26210 gen_type_die (tree type, dw_die_ref context_die)
26212 if (type != error_mark_node)
26214 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
26215 if (flag_checking)
26217 dw_die_ref die = lookup_type_die (type);
26218 if (die)
26219 check_die (die);
26224 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26225 things which are local to the given block. */
26227 static void
26228 gen_block_die (tree stmt, dw_die_ref context_die)
26230 int must_output_die = 0;
26231 bool inlined_func;
26233 /* Ignore blocks that are NULL. */
26234 if (stmt == NULL_TREE)
26235 return;
26237 inlined_func = inlined_function_outer_scope_p (stmt);
26239 /* If the block is one fragment of a non-contiguous block, do not
26240 process the variables, since they will have been done by the
26241 origin block. Do process subblocks. */
26242 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26244 tree sub;
26246 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26247 gen_block_die (sub, context_die);
26249 return;
26252 /* Determine if we need to output any Dwarf DIEs at all to represent this
26253 block. */
26254 if (inlined_func)
26255 /* The outer scopes for inlinings *must* always be represented. We
26256 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26257 must_output_die = 1;
26258 else if (lookup_block_die (stmt))
26259 /* If we already have a DIE then it was filled early. Meanwhile
26260 we might have pruned all BLOCK_VARS as optimized out but we
26261 still want to generate high/low PC attributes so output it. */
26262 must_output_die = 1;
26263 else if (TREE_USED (stmt)
26264 || TREE_ASM_WRITTEN (stmt))
26266 /* Determine if this block directly contains any "significant"
26267 local declarations which we will need to output DIEs for. */
26268 if (debug_info_level > DINFO_LEVEL_TERSE)
26270 /* We are not in terse mode so any local declaration that
26271 is not ignored for debug purposes counts as being a
26272 "significant" one. */
26273 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26274 must_output_die = 1;
26275 else
26276 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26277 if (!DECL_IGNORED_P (var))
26279 must_output_die = 1;
26280 break;
26283 else if (!dwarf2out_ignore_block (stmt))
26284 must_output_die = 1;
26287 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26288 DIE for any block which contains no significant local declarations at
26289 all. Rather, in such cases we just call `decls_for_scope' so that any
26290 needed Dwarf info for any sub-blocks will get properly generated. Note
26291 that in terse mode, our definition of what constitutes a "significant"
26292 local declaration gets restricted to include only inlined function
26293 instances and local (nested) function definitions. */
26294 if (must_output_die)
26296 if (inlined_func)
26297 gen_inlined_subroutine_die (stmt, context_die);
26298 else
26299 gen_lexical_block_die (stmt, context_die);
26301 else
26302 decls_for_scope (stmt, context_die);
26305 /* Process variable DECL (or variable with origin ORIGIN) within
26306 block STMT and add it to CONTEXT_DIE. */
26307 static void
26308 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26310 dw_die_ref die;
26311 tree decl_or_origin = decl ? decl : origin;
26313 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26314 die = lookup_decl_die (decl_or_origin);
26315 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26317 if (TYPE_DECL_IS_STUB (decl_or_origin))
26318 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26319 else
26320 die = lookup_decl_die (decl_or_origin);
26321 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26322 if (! die && ! early_dwarf)
26323 return;
26325 else
26326 die = NULL;
26328 /* Avoid creating DIEs for local typedefs and concrete static variables that
26329 will only be pruned later. */
26330 if ((origin || decl_ultimate_origin (decl))
26331 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26332 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26334 origin = decl_ultimate_origin (decl_or_origin);
26335 if (decl && VAR_P (decl) && die != NULL)
26337 die = lookup_decl_die (origin);
26338 if (die != NULL)
26339 equate_decl_number_to_die (decl, die);
26341 return;
26344 if (die != NULL && die->die_parent == NULL)
26345 add_child_die (context_die, die);
26346 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26348 if (early_dwarf)
26349 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26350 stmt, context_die);
26352 else
26354 if (decl && DECL_P (decl))
26356 die = lookup_decl_die (decl);
26358 /* Early created DIEs do not have a parent as the decls refer
26359 to the function as DECL_CONTEXT rather than the BLOCK. */
26360 if (die && die->die_parent == NULL)
26362 gcc_assert (in_lto_p);
26363 add_child_die (context_die, die);
26367 gen_decl_die (decl, origin, NULL, context_die);
26371 /* Generate all of the decls declared within a given scope and (recursively)
26372 all of its sub-blocks. */
26374 static void
26375 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26377 tree decl;
26378 unsigned int i;
26379 tree subblocks;
26381 /* Ignore NULL blocks. */
26382 if (stmt == NULL_TREE)
26383 return;
26385 /* Output the DIEs to represent all of the data objects and typedefs
26386 declared directly within this block but not within any nested
26387 sub-blocks. Also, nested function and tag DIEs have been
26388 generated with a parent of NULL; fix that up now. We don't
26389 have to do this if we're at -g1. */
26390 if (debug_info_level > DINFO_LEVEL_TERSE)
26392 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26393 process_scope_var (stmt, decl, NULL_TREE, context_die);
26394 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26395 origin - avoid doing this twice as we have no good way to see
26396 if we've done it once already. */
26397 if (! early_dwarf)
26398 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26400 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26401 if (decl == current_function_decl)
26402 /* Ignore declarations of the current function, while they
26403 are declarations, gen_subprogram_die would treat them
26404 as definitions again, because they are equal to
26405 current_function_decl and endlessly recurse. */;
26406 else if (TREE_CODE (decl) == FUNCTION_DECL)
26407 process_scope_var (stmt, decl, NULL_TREE, context_die);
26408 else
26409 process_scope_var (stmt, NULL_TREE, decl, context_die);
26413 /* Even if we're at -g1, we need to process the subblocks in order to get
26414 inlined call information. */
26416 /* Output the DIEs to represent all sub-blocks (and the items declared
26417 therein) of this block. */
26418 if (recurse)
26419 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26420 subblocks != NULL;
26421 subblocks = BLOCK_CHAIN (subblocks))
26422 gen_block_die (subblocks, context_die);
26425 /* Is this a typedef we can avoid emitting? */
26427 static bool
26428 is_redundant_typedef (const_tree decl)
26430 if (TYPE_DECL_IS_STUB (decl))
26431 return true;
26433 if (DECL_ARTIFICIAL (decl)
26434 && DECL_CONTEXT (decl)
26435 && is_tagged_type (DECL_CONTEXT (decl))
26436 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26437 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26438 /* Also ignore the artificial member typedef for the class name. */
26439 return true;
26441 return false;
26444 /* Return TRUE if TYPE is a typedef that names a type for linkage
26445 purposes. This kind of typedefs is produced by the C++ FE for
26446 constructs like:
26448 typedef struct {...} foo;
26450 In that case, there is no typedef variant type produced for foo.
26451 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26452 struct type. */
26454 static bool
26455 is_naming_typedef_decl (const_tree decl)
26457 if (decl == NULL_TREE
26458 || TREE_CODE (decl) != TYPE_DECL
26459 || DECL_NAMELESS (decl)
26460 || !is_tagged_type (TREE_TYPE (decl))
26461 || DECL_IS_UNDECLARED_BUILTIN (decl)
26462 || is_redundant_typedef (decl)
26463 /* It looks like Ada produces TYPE_DECLs that are very similar
26464 to C++ naming typedefs but that have different
26465 semantics. Let's be specific to c++ for now. */
26466 || !is_cxx (decl))
26467 return FALSE;
26469 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26470 && TYPE_NAME (TREE_TYPE (decl)) == decl
26471 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26472 != TYPE_NAME (TREE_TYPE (decl))));
26475 /* Looks up the DIE for a context. */
26477 static inline dw_die_ref
26478 lookup_context_die (tree context)
26480 if (context)
26482 /* Find die that represents this context. */
26483 if (TYPE_P (context))
26485 context = TYPE_MAIN_VARIANT (context);
26486 dw_die_ref ctx = lookup_type_die (context);
26487 if (!ctx)
26488 return NULL;
26489 return strip_naming_typedef (context, ctx);
26491 else
26492 return lookup_decl_die (context);
26494 return comp_unit_die ();
26497 /* Returns the DIE for a context. */
26499 static inline dw_die_ref
26500 get_context_die (tree context)
26502 if (context)
26504 /* Find die that represents this context. */
26505 if (TYPE_P (context))
26507 context = TYPE_MAIN_VARIANT (context);
26508 return strip_naming_typedef (context, force_type_die (context));
26510 else
26511 return force_decl_die (context);
26513 return comp_unit_die ();
26516 /* Returns the DIE for decl. A DIE will always be returned. */
26518 static dw_die_ref
26519 force_decl_die (tree decl)
26521 dw_die_ref decl_die;
26522 unsigned saved_external_flag;
26523 tree save_fn = NULL_TREE;
26524 decl_die = lookup_decl_die (decl);
26525 if (!decl_die)
26527 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26529 decl_die = lookup_decl_die (decl);
26530 if (decl_die)
26531 return decl_die;
26533 switch (TREE_CODE (decl))
26535 case FUNCTION_DECL:
26536 /* Clear current_function_decl, so that gen_subprogram_die thinks
26537 that this is a declaration. At this point, we just want to force
26538 declaration die. */
26539 save_fn = current_function_decl;
26540 current_function_decl = NULL_TREE;
26541 gen_subprogram_die (decl, context_die);
26542 current_function_decl = save_fn;
26543 break;
26545 case VAR_DECL:
26546 /* Set external flag to force declaration die. Restore it after
26547 gen_decl_die() call. */
26548 saved_external_flag = DECL_EXTERNAL (decl);
26549 DECL_EXTERNAL (decl) = 1;
26550 gen_decl_die (decl, NULL, NULL, context_die);
26551 DECL_EXTERNAL (decl) = saved_external_flag;
26552 break;
26554 case NAMESPACE_DECL:
26555 if (dwarf_version >= 3 || !dwarf_strict)
26556 dwarf2out_decl (decl);
26557 else
26558 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26559 decl_die = comp_unit_die ();
26560 break;
26562 case CONST_DECL:
26563 /* Enumerators shouldn't need force_decl_die. */
26564 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26565 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26566 gen_decl_die (decl, NULL, NULL, context_die);
26567 break;
26569 case TRANSLATION_UNIT_DECL:
26570 decl_die = comp_unit_die ();
26571 break;
26573 default:
26574 gcc_unreachable ();
26577 /* We should be able to find the DIE now. */
26578 if (!decl_die)
26579 decl_die = lookup_decl_die (decl);
26580 gcc_assert (decl_die);
26583 return decl_die;
26586 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26587 always returned. */
26589 static dw_die_ref
26590 force_type_die (tree type)
26592 dw_die_ref type_die;
26594 type_die = lookup_type_die (type);
26595 if (!type_die)
26597 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26599 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26600 false, context_die);
26601 gcc_assert (type_die);
26603 return type_die;
26606 /* Force out any required namespaces to be able to output DECL,
26607 and return the new context_die for it, if it's changed. */
26609 static dw_die_ref
26610 setup_namespace_context (tree thing, dw_die_ref context_die)
26612 tree context = (DECL_P (thing)
26613 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26614 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26615 /* Force out the namespace. */
26616 context_die = force_decl_die (context);
26618 return context_die;
26621 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26622 type) within its namespace, if appropriate.
26624 For compatibility with older debuggers, namespace DIEs only contain
26625 declarations; all definitions are emitted at CU scope, with
26626 DW_AT_specification pointing to the declaration (like with class
26627 members). */
26629 static dw_die_ref
26630 declare_in_namespace (tree thing, dw_die_ref context_die)
26632 dw_die_ref ns_context;
26634 if (debug_info_level <= DINFO_LEVEL_TERSE)
26635 return context_die;
26637 /* External declarations in the local scope only need to be emitted
26638 once, not once in the namespace and once in the scope.
26640 This avoids declaring the `extern' below in the
26641 namespace DIE as well as in the innermost scope:
26643 namespace S
26645 int i=5;
26646 int foo()
26648 int i=8;
26649 extern int i;
26650 return i;
26654 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26655 return context_die;
26657 /* If this decl is from an inlined function, then don't try to emit it in its
26658 namespace, as we will get confused. It would have already been emitted
26659 when the abstract instance of the inline function was emitted anyways. */
26660 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26661 return context_die;
26663 ns_context = setup_namespace_context (thing, context_die);
26665 if (ns_context != context_die)
26667 if (is_fortran () || is_dlang ())
26668 return ns_context;
26669 if (DECL_P (thing))
26670 gen_decl_die (thing, NULL, NULL, ns_context);
26671 else
26672 gen_type_die (thing, ns_context);
26674 return context_die;
26677 /* Generate a DIE for a namespace or namespace alias. */
26679 static void
26680 gen_namespace_die (tree decl, dw_die_ref context_die)
26682 dw_die_ref namespace_die;
26684 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26685 they are an alias of. */
26686 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26688 /* Output a real namespace or module. */
26689 context_die = setup_namespace_context (decl, comp_unit_die ());
26690 namespace_die = new_die (is_fortran () || is_dlang ()
26691 ? DW_TAG_module : DW_TAG_namespace,
26692 context_die, decl);
26693 /* For Fortran modules defined in different CU don't add src coords. */
26694 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26696 const char *name = dwarf2_name (decl, 0);
26697 if (name)
26698 add_name_attribute (namespace_die, name);
26700 else
26701 add_name_and_src_coords_attributes (namespace_die, decl);
26702 if (DECL_EXTERNAL (decl))
26703 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26704 equate_decl_number_to_die (decl, namespace_die);
26706 else
26708 /* Output a namespace alias. */
26710 /* Force out the namespace we are an alias of, if necessary. */
26711 dw_die_ref origin_die
26712 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26714 if (DECL_FILE_SCOPE_P (decl)
26715 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26716 context_die = setup_namespace_context (decl, comp_unit_die ());
26717 /* Now create the namespace alias DIE. */
26718 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26719 add_name_and_src_coords_attributes (namespace_die, decl);
26720 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26721 equate_decl_number_to_die (decl, namespace_die);
26723 if ((dwarf_version >= 5 || !dwarf_strict)
26724 && lang_hooks.decls.decl_dwarf_attribute (decl,
26725 DW_AT_export_symbols) == 1)
26726 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26728 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26729 if (want_pubnames ())
26730 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26733 /* Generate Dwarf debug information for a decl described by DECL.
26734 The return value is currently only meaningful for PARM_DECLs,
26735 for all other decls it returns NULL.
26737 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26738 It can be NULL otherwise. */
26740 static dw_die_ref
26741 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26742 dw_die_ref context_die)
26744 tree decl_or_origin = decl ? decl : origin;
26745 tree class_origin = NULL, ultimate_origin;
26747 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26748 return NULL;
26750 switch (TREE_CODE (decl_or_origin))
26752 case ERROR_MARK:
26753 break;
26755 case CONST_DECL:
26756 if (!is_fortran () && !is_ada () && !is_dlang ())
26758 /* The individual enumerators of an enum type get output when we output
26759 the Dwarf representation of the relevant enum type itself. */
26760 break;
26763 /* Emit its type. */
26764 gen_type_die (TREE_TYPE (decl), context_die);
26766 /* And its containing namespace. */
26767 context_die = declare_in_namespace (decl, context_die);
26769 gen_const_die (decl, context_die);
26770 break;
26772 case FUNCTION_DECL:
26773 #if 0
26774 /* FIXME */
26775 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26776 on local redeclarations of global functions. That seems broken. */
26777 if (current_function_decl != decl)
26778 /* This is only a declaration. */;
26779 #endif
26781 /* We should have abstract copies already and should not generate
26782 stray type DIEs in late LTO dumping. */
26783 if (! early_dwarf)
26786 /* If we're emitting a clone, emit info for the abstract instance. */
26787 else if (origin || DECL_ORIGIN (decl) != decl)
26788 dwarf2out_abstract_function (origin
26789 ? DECL_ORIGIN (origin)
26790 : DECL_ABSTRACT_ORIGIN (decl));
26792 /* If we're emitting a possibly inlined function emit it as
26793 abstract instance. */
26794 else if (cgraph_function_possibly_inlined_p (decl)
26795 && ! DECL_ABSTRACT_P (decl)
26796 && ! class_or_namespace_scope_p (context_die)
26797 /* dwarf2out_abstract_function won't emit a die if this is just
26798 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26799 that case, because that works only if we have a die. */
26800 && DECL_INITIAL (decl) != NULL_TREE)
26801 dwarf2out_abstract_function (decl);
26803 /* Otherwise we're emitting the primary DIE for this decl. */
26804 else if (debug_info_level > DINFO_LEVEL_TERSE)
26806 /* Before we describe the FUNCTION_DECL itself, make sure that we
26807 have its containing type. */
26808 if (!origin)
26809 origin = decl_class_context (decl);
26810 if (origin != NULL_TREE)
26811 gen_type_die (origin, context_die);
26813 /* And its return type. */
26814 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26816 /* And its virtual context. */
26817 if (DECL_VINDEX (decl) != NULL_TREE)
26818 gen_type_die (DECL_CONTEXT (decl), context_die);
26820 /* Make sure we have a member DIE for decl. */
26821 if (origin != NULL_TREE)
26822 gen_type_die_for_member (origin, decl, context_die);
26824 /* And its containing namespace. */
26825 context_die = declare_in_namespace (decl, context_die);
26828 /* Now output a DIE to represent the function itself. */
26829 if (decl)
26830 gen_subprogram_die (decl, context_die);
26831 break;
26833 case TYPE_DECL:
26834 /* If we are in terse mode, don't generate any DIEs to represent any
26835 actual typedefs. */
26836 if (debug_info_level <= DINFO_LEVEL_TERSE)
26837 break;
26839 /* In the special case of a TYPE_DECL node representing the declaration
26840 of some type tag, if the given TYPE_DECL is marked as having been
26841 instantiated from some other (original) TYPE_DECL node (e.g. one which
26842 was generated within the original definition of an inline function) we
26843 used to generate a special (abbreviated) DW_TAG_structure_type,
26844 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26845 should be actually referencing those DIEs, as variable DIEs with that
26846 type would be emitted already in the abstract origin, so it was always
26847 removed during unused type prunning. Don't add anything in this
26848 case. */
26849 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26850 break;
26852 if (is_redundant_typedef (decl))
26853 gen_type_die (TREE_TYPE (decl), context_die);
26854 else
26855 /* Output a DIE to represent the typedef itself. */
26856 gen_typedef_die (decl, context_die);
26857 break;
26859 case LABEL_DECL:
26860 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26861 gen_label_die (decl, context_die);
26862 break;
26864 case VAR_DECL:
26865 case RESULT_DECL:
26866 /* If we are in terse mode, don't generate any DIEs to represent any
26867 variable declarations or definitions unless it is external. */
26868 if (debug_info_level < DINFO_LEVEL_TERSE
26869 || (debug_info_level == DINFO_LEVEL_TERSE
26870 && !TREE_PUBLIC (decl_or_origin)))
26871 break;
26873 if (debug_info_level > DINFO_LEVEL_TERSE)
26875 /* Avoid generating stray type DIEs during late dwarf dumping.
26876 All types have been dumped early. */
26877 if (early_dwarf
26878 /* ??? But in LTRANS we cannot annotate early created variably
26879 modified type DIEs without copying them and adjusting all
26880 references to them. Dump them again as happens for inlining
26881 which copies both the decl and the types. */
26882 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26883 in VLA bound information for example. */
26884 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26885 current_function_decl)))
26887 /* Output any DIEs that are needed to specify the type of this data
26888 object. */
26889 if (decl_by_reference_p (decl_or_origin))
26890 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26891 else
26892 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26895 if (early_dwarf)
26897 /* And its containing type. */
26898 class_origin = decl_class_context (decl_or_origin);
26899 if (class_origin != NULL_TREE)
26900 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26902 /* And its containing namespace. */
26903 context_die = declare_in_namespace (decl_or_origin, context_die);
26907 /* Now output the DIE to represent the data object itself. This gets
26908 complicated because of the possibility that the VAR_DECL really
26909 represents an inlined instance of a formal parameter for an inline
26910 function. */
26911 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26912 if (ultimate_origin != NULL_TREE
26913 && TREE_CODE (ultimate_origin) == PARM_DECL)
26914 gen_formal_parameter_die (decl, origin,
26915 true /* Emit name attribute. */,
26916 context_die);
26917 else
26918 gen_variable_die (decl, origin, context_die);
26919 break;
26921 case FIELD_DECL:
26922 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26923 /* Ignore the nameless fields that are used to skip bits but handle C++
26924 anonymous unions and structs. */
26925 if (DECL_NAME (decl) != NULL_TREE
26926 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26927 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26929 gen_type_die (member_declared_type (decl), context_die);
26930 gen_field_die (decl, ctx, context_die);
26932 break;
26934 case PARM_DECL:
26935 /* Avoid generating stray type DIEs during late dwarf dumping.
26936 All types have been dumped early. */
26937 if (early_dwarf
26938 /* ??? But in LTRANS we cannot annotate early created variably
26939 modified type DIEs without copying them and adjusting all
26940 references to them. Dump them again as happens for inlining
26941 which copies both the decl and the types. */
26942 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26943 in VLA bound information for example. */
26944 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26945 current_function_decl)))
26947 if (DECL_BY_REFERENCE (decl_or_origin))
26948 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26949 else
26950 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26952 return gen_formal_parameter_die (decl, origin,
26953 true /* Emit name attribute. */,
26954 context_die);
26956 case NAMESPACE_DECL:
26957 if (dwarf_version >= 3 || !dwarf_strict)
26958 gen_namespace_die (decl, context_die);
26959 break;
26961 case IMPORTED_DECL:
26962 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26963 DECL_CONTEXT (decl), context_die);
26964 break;
26966 case NAMELIST_DECL:
26967 gen_namelist_decl (DECL_NAME (decl), context_die,
26968 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26969 break;
26971 default:
26972 /* Probably some frontend-internal decl. Assume we don't care. */
26973 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26974 break;
26977 return NULL;
26980 /* Output initial debug information for global DECL. Called at the
26981 end of the parsing process.
26983 This is the initial debug generation process. As such, the DIEs
26984 generated may be incomplete. A later debug generation pass
26985 (dwarf2out_late_global_decl) will augment the information generated
26986 in this pass (e.g., with complete location info). */
26988 static void
26989 dwarf2out_early_global_decl (tree decl)
26991 set_early_dwarf s;
26993 /* gen_decl_die() will set DECL_ABSTRACT because
26994 cgraph_function_possibly_inlined_p() returns true. This is in
26995 turn will cause DW_AT_inline attributes to be set.
26997 This happens because at early dwarf generation, there is no
26998 cgraph information, causing cgraph_function_possibly_inlined_p()
26999 to return true. Trick cgraph_function_possibly_inlined_p()
27000 while we generate dwarf early. */
27001 bool save = symtab->global_info_ready;
27002 symtab->global_info_ready = true;
27004 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27005 other DECLs and they can point to template types or other things
27006 that dwarf2out can't handle when done via dwarf2out_decl. */
27007 if (TREE_CODE (decl) != TYPE_DECL
27008 && TREE_CODE (decl) != PARM_DECL)
27010 if (TREE_CODE (decl) == FUNCTION_DECL)
27012 tree save_fndecl = current_function_decl;
27014 /* For nested functions, make sure we have DIEs for the parents first
27015 so that all nested DIEs are generated at the proper scope in the
27016 first shot. */
27017 tree context = decl_function_context (decl);
27018 if (context != NULL)
27020 dw_die_ref context_die = lookup_decl_die (context);
27021 current_function_decl = context;
27023 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27024 enough so that it lands in its own context. This avoids type
27025 pruning issues later on. */
27026 if (context_die == NULL || is_declaration_die (context_die))
27027 dwarf2out_early_global_decl (context);
27030 /* Emit an abstract origin of a function first. This happens
27031 with C++ constructor clones for example and makes
27032 dwarf2out_abstract_function happy which requires the early
27033 DIE of the abstract instance to be present. */
27034 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27035 dw_die_ref origin_die;
27036 if (origin != NULL
27037 /* Do not emit the DIE multiple times but make sure to
27038 process it fully here in case we just saw a declaration. */
27039 && ((origin_die = lookup_decl_die (origin)) == NULL
27040 || is_declaration_die (origin_die)))
27042 current_function_decl = origin;
27043 dwarf2out_decl (origin);
27046 /* Emit the DIE for decl but avoid doing that multiple times. */
27047 dw_die_ref old_die;
27048 if ((old_die = lookup_decl_die (decl)) == NULL
27049 || is_declaration_die (old_die))
27051 current_function_decl = decl;
27052 dwarf2out_decl (decl);
27055 current_function_decl = save_fndecl;
27057 else
27058 dwarf2out_decl (decl);
27060 symtab->global_info_ready = save;
27063 /* Return whether EXPR is an expression with the following pattern:
27064 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27066 static bool
27067 is_trivial_indirect_ref (tree expr)
27069 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27070 return false;
27072 tree nop = TREE_OPERAND (expr, 0);
27073 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27074 return false;
27076 tree int_cst = TREE_OPERAND (nop, 0);
27077 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27080 /* Output debug information for global decl DECL. Called from
27081 toplev.c after compilation proper has finished. */
27083 static void
27084 dwarf2out_late_global_decl (tree decl)
27086 /* Fill-in any location information we were unable to determine
27087 on the first pass. */
27088 if (VAR_P (decl))
27090 dw_die_ref die = lookup_decl_die (decl);
27092 /* We may have to generate full debug late for LTO in case debug
27093 was not enabled at compile-time or the target doesn't support
27094 the LTO early debug scheme. */
27095 if (! die && in_lto_p)
27096 dwarf2out_decl (decl);
27097 else if (die)
27099 /* We get called via the symtab code invoking late_global_decl
27100 for symbols that are optimized out.
27102 Do not add locations for those, except if they have a
27103 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27104 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27105 INDIRECT_REF expression, as this could generate relocations to
27106 text symbols in LTO object files, which is invalid. */
27107 varpool_node *node = varpool_node::get (decl);
27108 if ((! node || ! node->definition)
27109 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27110 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27111 tree_add_const_value_attribute_for_decl (die, decl);
27112 else
27113 add_location_or_const_value_attribute (die, decl, false);
27118 /* Output debug information for type decl DECL. Called from toplev.c
27119 and from language front ends (to record built-in types). */
27120 static void
27121 dwarf2out_type_decl (tree decl, int local)
27123 if (!local)
27125 set_early_dwarf s;
27126 dwarf2out_decl (decl);
27130 /* Output debug information for imported module or decl DECL.
27131 NAME is non-NULL name in the lexical block if the decl has been renamed.
27132 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27133 that DECL belongs to.
27134 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27135 static void
27136 dwarf2out_imported_module_or_decl_1 (tree decl,
27137 tree name,
27138 tree lexical_block,
27139 dw_die_ref lexical_block_die)
27141 expanded_location xloc;
27142 dw_die_ref imported_die = NULL;
27143 dw_die_ref at_import_die;
27145 if (TREE_CODE (decl) == IMPORTED_DECL)
27147 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27148 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27149 gcc_assert (decl);
27151 else
27152 xloc = expand_location (input_location);
27154 if (TREE_CODE (decl) == TYPE_DECL)
27156 at_import_die = force_type_die (TREE_TYPE (decl));
27157 /* For namespace N { typedef void T; } using N::T; base_type_die
27158 returns NULL, but DW_TAG_imported_declaration requires
27159 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27160 if (!at_import_die)
27162 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27163 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27164 at_import_die = lookup_type_die (TREE_TYPE (decl));
27165 gcc_assert (at_import_die);
27168 else
27170 at_import_die = lookup_decl_die (decl);
27171 if (!at_import_die)
27173 /* If we're trying to avoid duplicate debug info, we may not have
27174 emitted the member decl for this field. Emit it now. */
27175 if (TREE_CODE (decl) == FIELD_DECL)
27177 tree type = DECL_CONTEXT (decl);
27179 if (TYPE_CONTEXT (type)
27180 && TYPE_P (TYPE_CONTEXT (type))
27181 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27182 DINFO_USAGE_DIR_USE))
27183 return;
27184 gen_type_die_for_member (type, decl,
27185 get_context_die (TYPE_CONTEXT (type)));
27187 if (TREE_CODE (decl) == CONST_DECL)
27189 /* Individual enumerators of an enum type do not get output here
27190 (see gen_decl_die), so we cannot call force_decl_die. */
27191 if (!is_fortran () && !is_ada () && !is_dlang ())
27192 return;
27194 if (TREE_CODE (decl) == NAMELIST_DECL)
27195 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27196 get_context_die (DECL_CONTEXT (decl)),
27197 NULL_TREE);
27198 else
27199 at_import_die = force_decl_die (decl);
27203 if (TREE_CODE (decl) == NAMESPACE_DECL)
27205 if (dwarf_version >= 3 || !dwarf_strict)
27206 imported_die = new_die (DW_TAG_imported_module,
27207 lexical_block_die,
27208 lexical_block);
27209 else
27210 return;
27212 else
27213 imported_die = new_die (DW_TAG_imported_declaration,
27214 lexical_block_die,
27215 lexical_block);
27217 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27218 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27219 if (debug_column_info && xloc.column)
27220 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27221 if (name)
27222 add_AT_string (imported_die, DW_AT_name,
27223 IDENTIFIER_POINTER (name));
27224 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27227 /* Output debug information for imported module or decl DECL.
27228 NAME is non-NULL name in context if the decl has been renamed.
27229 CHILD is true if decl is one of the renamed decls as part of
27230 importing whole module.
27231 IMPLICIT is set if this hook is called for an implicit import
27232 such as inline namespace. */
27234 static void
27235 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27236 bool child, bool implicit)
27238 /* dw_die_ref at_import_die; */
27239 dw_die_ref scope_die;
27241 if (debug_info_level <= DINFO_LEVEL_TERSE)
27242 return;
27244 gcc_assert (decl);
27246 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27247 should be enough, for DWARF4 and older even if we emit as extension
27248 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27249 for the benefit of consumers unaware of DW_AT_export_symbols. */
27250 if (implicit
27251 && dwarf_version >= 5
27252 && lang_hooks.decls.decl_dwarf_attribute (decl,
27253 DW_AT_export_symbols) == 1)
27254 return;
27256 set_early_dwarf s;
27258 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27259 We need decl DIE for reference and scope die. First, get DIE for the decl
27260 itself. */
27262 /* Get the scope die for decl context. Use comp_unit_die for global module
27263 or decl. If die is not found for non globals, force new die. */
27264 if (context
27265 && TYPE_P (context)
27266 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27267 return;
27269 scope_die = get_context_die (context);
27271 if (child)
27273 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27274 there is nothing we can do, here. */
27275 if (dwarf_version < 3 && dwarf_strict)
27276 return;
27278 gcc_assert (scope_die->die_child);
27279 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27280 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27281 scope_die = scope_die->die_child;
27284 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27285 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27288 /* Output debug information for namelists. */
27290 static dw_die_ref
27291 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27293 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27294 tree value;
27295 unsigned i;
27297 if (debug_info_level <= DINFO_LEVEL_TERSE)
27298 return NULL;
27300 gcc_assert (scope_die != NULL);
27301 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27302 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27304 /* If there are no item_decls, we have a nondefining namelist, e.g.
27305 with USE association; hence, set DW_AT_declaration. */
27306 if (item_decls == NULL_TREE)
27308 add_AT_flag (nml_die, DW_AT_declaration, 1);
27309 return nml_die;
27312 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27314 nml_item_ref_die = lookup_decl_die (value);
27315 if (!nml_item_ref_die)
27316 nml_item_ref_die = force_decl_die (value);
27318 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27319 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
27321 return nml_die;
27325 /* Write the debugging output for DECL and return the DIE. */
27327 static void
27328 dwarf2out_decl (tree decl)
27330 dw_die_ref context_die = comp_unit_die ();
27332 switch (TREE_CODE (decl))
27334 case ERROR_MARK:
27335 return;
27337 case FUNCTION_DECL:
27338 /* If we're a nested function, initially use a parent of NULL; if we're
27339 a plain function, this will be fixed up in decls_for_scope. If
27340 we're a method, it will be ignored, since we already have a DIE.
27341 Avoid doing this late though since clones of class methods may
27342 otherwise end up in limbo and create type DIEs late. */
27343 if (early_dwarf
27344 && decl_function_context (decl)
27345 /* But if we're in terse mode, we don't care about scope. */
27346 && debug_info_level > DINFO_LEVEL_TERSE)
27347 context_die = NULL;
27348 break;
27350 case VAR_DECL:
27351 /* For local statics lookup proper context die. */
27352 if (local_function_static (decl))
27353 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27355 /* If we are in terse mode, don't generate any DIEs to represent any
27356 variable declarations or definitions unless it is external. */
27357 if (debug_info_level < DINFO_LEVEL_TERSE
27358 || (debug_info_level == DINFO_LEVEL_TERSE
27359 && !TREE_PUBLIC (decl)))
27360 return;
27361 break;
27363 case CONST_DECL:
27364 if (debug_info_level <= DINFO_LEVEL_TERSE)
27365 return;
27366 if (!is_fortran () && !is_ada () && !is_dlang ())
27367 return;
27368 if (TREE_STATIC (decl) && decl_function_context (decl))
27369 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27370 break;
27372 case NAMESPACE_DECL:
27373 case IMPORTED_DECL:
27374 if (debug_info_level <= DINFO_LEVEL_TERSE)
27375 return;
27376 if (lookup_decl_die (decl) != NULL)
27377 return;
27378 break;
27380 case TYPE_DECL:
27381 /* Don't emit stubs for types unless they are needed by other DIEs. */
27382 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27383 return;
27385 /* Don't bother trying to generate any DIEs to represent any of the
27386 normal built-in types for the language we are compiling. */
27387 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27388 return;
27390 /* If we are in terse mode, don't generate any DIEs for types. */
27391 if (debug_info_level <= DINFO_LEVEL_TERSE)
27392 return;
27394 /* If we're a function-scope tag, initially use a parent of NULL;
27395 this will be fixed up in decls_for_scope. */
27396 if (decl_function_context (decl))
27397 context_die = NULL;
27399 break;
27401 case NAMELIST_DECL:
27402 break;
27404 default:
27405 return;
27408 gen_decl_die (decl, NULL, NULL, context_die);
27410 if (flag_checking)
27412 dw_die_ref die = lookup_decl_die (decl);
27413 if (die)
27414 check_die (die);
27418 /* Write the debugging output for DECL. */
27420 static void
27421 dwarf2out_function_decl (tree decl)
27423 dwarf2out_decl (decl);
27424 call_arg_locations = NULL;
27425 call_arg_loc_last = NULL;
27426 call_site_count = -1;
27427 tail_call_site_count = -1;
27428 decl_loc_table->empty ();
27429 cached_dw_loc_list_table->empty ();
27432 /* Output a marker (i.e. a label) for the beginning of the generated code for
27433 a lexical block. */
27435 static void
27436 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27437 unsigned int blocknum)
27439 switch_to_section (current_function_section ());
27440 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27443 /* Output a marker (i.e. a label) for the end of the generated code for a
27444 lexical block. */
27446 static void
27447 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27449 switch_to_section (current_function_section ());
27450 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27453 /* Returns nonzero if it is appropriate not to emit any debugging
27454 information for BLOCK, because it doesn't contain any instructions.
27456 Don't allow this for blocks with nested functions or local classes
27457 as we would end up with orphans, and in the presence of scheduling
27458 we may end up calling them anyway. */
27460 static bool
27461 dwarf2out_ignore_block (const_tree block)
27463 tree decl;
27464 unsigned int i;
27466 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27467 if (TREE_CODE (decl) == FUNCTION_DECL
27468 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27469 return 0;
27470 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27472 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27473 if (TREE_CODE (decl) == FUNCTION_DECL
27474 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27475 return 0;
27478 return 1;
27481 /* Hash table routines for file_hash. */
27483 bool
27484 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27486 return filename_cmp (p1->key, p2) == 0;
27489 hashval_t
27490 dwarf_file_hasher::hash (dwarf_file_data *p)
27492 return htab_hash_string (p->key);
27495 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27496 dwarf2out.c) and return its "index". The index of each (known) filename is
27497 just a unique number which is associated with only that one filename. We
27498 need such numbers for the sake of generating labels (in the .debug_sfnames
27499 section) and references to those files numbers (in the .debug_srcinfo
27500 and .debug_macinfo sections). If the filename given as an argument is not
27501 found in our current list, add it to the list and assign it the next
27502 available unique index number. */
27504 static struct dwarf_file_data *
27505 lookup_filename (const char *file_name)
27507 struct dwarf_file_data * created;
27509 if (!file_name)
27510 return NULL;
27512 if (!file_name[0])
27513 file_name = "<stdin>";
27515 dwarf_file_data **slot
27516 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27517 INSERT);
27518 if (*slot)
27519 return *slot;
27521 created = ggc_alloc<dwarf_file_data> ();
27522 created->key = file_name;
27523 created->filename = remap_debug_filename (file_name);
27524 created->emitted_number = 0;
27525 *slot = created;
27526 return created;
27529 /* If the assembler will construct the file table, then translate the compiler
27530 internal file table number into the assembler file table number, and emit
27531 a .file directive if we haven't already emitted one yet. The file table
27532 numbers are different because we prune debug info for unused variables and
27533 types, which may include filenames. */
27535 static int
27536 maybe_emit_file (struct dwarf_file_data * fd)
27538 if (! fd->emitted_number)
27540 if (last_emitted_file)
27541 fd->emitted_number = last_emitted_file->emitted_number + 1;
27542 else
27543 fd->emitted_number = 1;
27544 last_emitted_file = fd;
27546 if (output_asm_line_debug_info ())
27548 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27549 output_quoted_string (asm_out_file, fd->filename);
27550 fputc ('\n', asm_out_file);
27554 return fd->emitted_number;
27557 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27558 That generation should happen after function debug info has been
27559 generated. The value of the attribute is the constant value of ARG. */
27561 static void
27562 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27564 die_arg_entry entry;
27566 if (!die || !arg)
27567 return;
27569 gcc_assert (early_dwarf);
27571 if (!tmpl_value_parm_die_table)
27572 vec_alloc (tmpl_value_parm_die_table, 32);
27574 entry.die = die;
27575 entry.arg = arg;
27576 vec_safe_push (tmpl_value_parm_die_table, entry);
27579 /* Return TRUE if T is an instance of generic type, FALSE
27580 otherwise. */
27582 static bool
27583 generic_type_p (tree t)
27585 if (t == NULL_TREE || !TYPE_P (t))
27586 return false;
27587 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27590 /* Schedule the generation of the generic parameter dies for the
27591 instance of generic type T. The proper generation itself is later
27592 done by gen_scheduled_generic_parms_dies. */
27594 static void
27595 schedule_generic_params_dies_gen (tree t)
27597 if (!generic_type_p (t))
27598 return;
27600 gcc_assert (early_dwarf);
27602 if (!generic_type_instances)
27603 vec_alloc (generic_type_instances, 256);
27605 vec_safe_push (generic_type_instances, t);
27608 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27609 by append_entry_to_tmpl_value_parm_die_table. This function must
27610 be called after function DIEs have been generated. */
27612 static void
27613 gen_remaining_tmpl_value_param_die_attribute (void)
27615 if (tmpl_value_parm_die_table)
27617 unsigned i, j;
27618 die_arg_entry *e;
27620 /* We do this in two phases - first get the cases we can
27621 handle during early-finish, preserving those we cannot
27622 (containing symbolic constants where we don't yet know
27623 whether we are going to output the referenced symbols).
27624 For those we try again at late-finish. */
27625 j = 0;
27626 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27628 if (!e->die->removed
27629 && !tree_add_const_value_attribute (e->die, e->arg))
27631 dw_loc_descr_ref loc = NULL;
27632 if (! early_dwarf
27633 && (dwarf_version >= 5 || !dwarf_strict))
27634 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27635 if (loc)
27636 add_AT_loc (e->die, DW_AT_location, loc);
27637 else
27638 (*tmpl_value_parm_die_table)[j++] = *e;
27641 tmpl_value_parm_die_table->truncate (j);
27645 /* Generate generic parameters DIEs for instances of generic types
27646 that have been previously scheduled by
27647 schedule_generic_params_dies_gen. This function must be called
27648 after all the types of the CU have been laid out. */
27650 static void
27651 gen_scheduled_generic_parms_dies (void)
27653 unsigned i;
27654 tree t;
27656 if (!generic_type_instances)
27657 return;
27659 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27660 if (COMPLETE_TYPE_P (t))
27661 gen_generic_params_dies (t);
27663 generic_type_instances = NULL;
27667 /* Replace DW_AT_name for the decl with name. */
27669 static void
27670 dwarf2out_set_name (tree decl, tree name)
27672 dw_die_ref die;
27673 dw_attr_node *attr;
27674 const char *dname;
27676 die = TYPE_SYMTAB_DIE (decl);
27677 if (!die)
27678 return;
27680 dname = dwarf2_name (name, 0);
27681 if (!dname)
27682 return;
27684 attr = get_AT (die, DW_AT_name);
27685 if (attr)
27687 struct indirect_string_node *node;
27689 node = find_AT_string (dname);
27690 /* replace the string. */
27691 attr->dw_attr_val.v.val_str = node;
27694 else
27695 add_name_attribute (die, dname);
27698 /* True if before or during processing of the first function being emitted. */
27699 static bool in_first_function_p = true;
27700 /* True if loc_note during dwarf2out_var_location call might still be
27701 before first real instruction at address equal to .Ltext0. */
27702 static bool maybe_at_text_label_p = true;
27703 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27704 static unsigned int first_loclabel_num_not_at_text_label;
27706 /* Look ahead for a real insn. */
27708 static rtx_insn *
27709 dwarf2out_next_real_insn (rtx_insn *loc_note)
27711 rtx_insn *next_real = NEXT_INSN (loc_note);
27713 while (next_real)
27714 if (INSN_P (next_real))
27715 break;
27716 else
27717 next_real = NEXT_INSN (next_real);
27719 return next_real;
27722 /* Called by the final INSN scan whenever we see a var location. We
27723 use it to drop labels in the right places, and throw the location in
27724 our lookup table. */
27726 static void
27727 dwarf2out_var_location (rtx_insn *loc_note)
27729 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27730 struct var_loc_node *newloc;
27731 rtx_insn *next_real;
27732 rtx_insn *call_insn = NULL;
27733 static const char *last_label;
27734 static const char *last_postcall_label;
27735 static bool last_in_cold_section_p;
27736 static rtx_insn *expected_next_loc_note;
27737 tree decl;
27738 bool var_loc_p;
27739 var_loc_view view = 0;
27741 if (!NOTE_P (loc_note))
27743 if (CALL_P (loc_note))
27745 maybe_reset_location_view (loc_note, cur_line_info_table);
27746 call_site_count++;
27747 if (SIBLING_CALL_P (loc_note))
27748 tail_call_site_count++;
27749 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27751 call_insn = loc_note;
27752 loc_note = NULL;
27753 var_loc_p = false;
27755 next_real = dwarf2out_next_real_insn (call_insn);
27756 cached_next_real_insn = NULL;
27757 goto create_label;
27759 if (optimize == 0 && !flag_var_tracking)
27761 /* When the var-tracking pass is not running, there is no note
27762 for indirect calls whose target is compile-time known. In this
27763 case, process such calls specifically so that we generate call
27764 sites for them anyway. */
27765 rtx x = PATTERN (loc_note);
27766 if (GET_CODE (x) == PARALLEL)
27767 x = XVECEXP (x, 0, 0);
27768 if (GET_CODE (x) == SET)
27769 x = SET_SRC (x);
27770 if (GET_CODE (x) == CALL)
27771 x = XEXP (x, 0);
27772 if (!MEM_P (x)
27773 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27774 || !SYMBOL_REF_DECL (XEXP (x, 0))
27775 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27776 != FUNCTION_DECL))
27778 call_insn = loc_note;
27779 loc_note = NULL;
27780 var_loc_p = false;
27782 next_real = dwarf2out_next_real_insn (call_insn);
27783 cached_next_real_insn = NULL;
27784 goto create_label;
27788 else if (!debug_variable_location_views)
27789 gcc_unreachable ();
27790 else
27791 maybe_reset_location_view (loc_note, cur_line_info_table);
27793 return;
27796 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27797 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27798 return;
27800 /* Optimize processing a large consecutive sequence of location
27801 notes so we don't spend too much time in next_real_insn. If the
27802 next insn is another location note, remember the next_real_insn
27803 calculation for next time. */
27804 next_real = cached_next_real_insn;
27805 if (next_real)
27807 if (expected_next_loc_note != loc_note)
27808 next_real = NULL;
27811 if (! next_real)
27812 next_real = dwarf2out_next_real_insn (loc_note);
27814 if (next_real)
27816 rtx_insn *next_note = NEXT_INSN (loc_note);
27817 while (next_note != next_real)
27819 if (! next_note->deleted ()
27820 && NOTE_P (next_note)
27821 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
27822 break;
27823 next_note = NEXT_INSN (next_note);
27826 if (next_note == next_real)
27827 cached_next_real_insn = NULL;
27828 else
27830 expected_next_loc_note = next_note;
27831 cached_next_real_insn = next_real;
27834 else
27835 cached_next_real_insn = NULL;
27837 /* If there are no instructions which would be affected by this note,
27838 don't do anything. */
27839 if (var_loc_p
27840 && next_real == NULL_RTX
27841 && !NOTE_DURING_CALL_P (loc_note))
27842 return;
27844 create_label:
27846 if (next_real == NULL_RTX)
27847 next_real = get_last_insn ();
27849 /* If there were any real insns between note we processed last time
27850 and this note (or if it is the first note), clear
27851 last_{,postcall_}label so that they are not reused this time. */
27852 if (last_var_location_insn == NULL_RTX
27853 || last_var_location_insn != next_real
27854 || last_in_cold_section_p != in_cold_section_p)
27856 last_label = NULL;
27857 last_postcall_label = NULL;
27860 if (var_loc_p)
27862 const char *label
27863 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27864 view = cur_line_info_table->view;
27865 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27866 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27867 if (newloc == NULL)
27868 return;
27870 else
27872 decl = NULL_TREE;
27873 newloc = NULL;
27876 /* If there were no real insns between note we processed last time
27877 and this note, use the label we emitted last time. Otherwise
27878 create a new label and emit it. */
27879 if (last_label == NULL)
27881 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27882 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27883 loclabel_num++;
27884 last_label = ggc_strdup (loclabel);
27885 /* See if loclabel might be equal to .Ltext0. If yes,
27886 bump first_loclabel_num_not_at_text_label. */
27887 if (!have_multiple_function_sections
27888 && in_first_function_p
27889 && maybe_at_text_label_p)
27891 static rtx_insn *last_start;
27892 rtx_insn *insn;
27893 for (insn = loc_note; insn; insn = previous_insn (insn))
27894 if (insn == last_start)
27895 break;
27896 else if (!NONDEBUG_INSN_P (insn))
27897 continue;
27898 else
27900 rtx body = PATTERN (insn);
27901 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27902 continue;
27903 /* Inline asm could occupy zero bytes. */
27904 else if (GET_CODE (body) == ASM_INPUT
27905 || asm_noperands (body) >= 0)
27906 continue;
27907 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27908 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27909 continue;
27910 #endif
27911 else
27913 /* Assume insn has non-zero length. */
27914 maybe_at_text_label_p = false;
27915 break;
27918 if (maybe_at_text_label_p)
27920 last_start = loc_note;
27921 first_loclabel_num_not_at_text_label = loclabel_num;
27926 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27927 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27929 if (!var_loc_p)
27931 struct call_arg_loc_node *ca_loc
27932 = ggc_cleared_alloc<call_arg_loc_node> ();
27933 rtx_insn *prev = call_insn;
27935 ca_loc->call_arg_loc_note
27936 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27937 ca_loc->next = NULL;
27938 ca_loc->label = last_label;
27939 gcc_assert (prev
27940 && (CALL_P (prev)
27941 || (NONJUMP_INSN_P (prev)
27942 && GET_CODE (PATTERN (prev)) == SEQUENCE
27943 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27944 if (!CALL_P (prev))
27945 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27946 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27948 /* Look for a SYMBOL_REF in the "prev" instruction. */
27949 rtx x = get_call_rtx_from (prev);
27950 if (x)
27952 /* Try to get the call symbol, if any. */
27953 if (MEM_P (XEXP (x, 0)))
27954 x = XEXP (x, 0);
27955 /* First, look for a memory access to a symbol_ref. */
27956 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27957 && SYMBOL_REF_DECL (XEXP (x, 0))
27958 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27959 ca_loc->symbol_ref = XEXP (x, 0);
27960 /* Otherwise, look at a compile-time known user-level function
27961 declaration. */
27962 else if (MEM_P (x)
27963 && MEM_EXPR (x)
27964 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27965 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27968 ca_loc->block = insn_scope (prev);
27969 if (call_arg_locations)
27970 call_arg_loc_last->next = ca_loc;
27971 else
27972 call_arg_locations = ca_loc;
27973 call_arg_loc_last = ca_loc;
27975 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27977 newloc->label = last_label;
27978 newloc->view = view;
27980 else
27982 if (!last_postcall_label)
27984 sprintf (loclabel, "%s-1", last_label);
27985 last_postcall_label = ggc_strdup (loclabel);
27987 newloc->label = last_postcall_label;
27988 /* ??? This view is at last_label, not last_label-1, but we
27989 could only assume view at last_label-1 is zero if we could
27990 assume calls always have length greater than one. This is
27991 probably true in general, though there might be a rare
27992 exception to this rule, e.g. if a call insn is optimized out
27993 by target magic. Then, even the -1 in the label will be
27994 wrong, which might invalidate the range. Anyway, using view,
27995 though technically possibly incorrect, will work as far as
27996 ranges go: since L-1 is in the middle of the call insn,
27997 (L-1).0 and (L-1).V shouldn't make any difference, and having
27998 the loclist entry refer to the .loc entry might be useful, so
27999 leave it like this. */
28000 newloc->view = view;
28003 if (var_loc_p && flag_debug_asm)
28005 const char *name, *sep, *patstr;
28006 if (decl && DECL_NAME (decl))
28007 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28008 else
28009 name = "";
28010 if (NOTE_VAR_LOCATION_LOC (loc_note))
28012 sep = " => ";
28013 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28015 else
28017 sep = " ";
28018 patstr = "RESET";
28020 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28021 name, sep, patstr);
28024 last_var_location_insn = next_real;
28025 last_in_cold_section_p = in_cold_section_p;
28028 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28029 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28030 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28031 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28032 BLOCK_FRAGMENT_ORIGIN links. */
28033 static bool
28034 block_within_block_p (tree block, tree outer, bool bothways)
28036 if (block == outer)
28037 return true;
28039 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28040 for (tree context = BLOCK_SUPERCONTEXT (block);
28041 context != outer;
28042 context = BLOCK_SUPERCONTEXT (context))
28043 if (!context || TREE_CODE (context) != BLOCK)
28044 return false;
28046 if (!bothways)
28047 return true;
28049 /* Now check that each block is actually referenced by its
28050 parent. */
28051 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28052 context = BLOCK_SUPERCONTEXT (context))
28054 if (BLOCK_FRAGMENT_ORIGIN (context))
28056 gcc_assert (!BLOCK_SUBBLOCKS (context));
28057 context = BLOCK_FRAGMENT_ORIGIN (context);
28059 for (tree sub = BLOCK_SUBBLOCKS (context);
28060 sub != block;
28061 sub = BLOCK_CHAIN (sub))
28062 if (!sub)
28063 return false;
28064 if (context == outer)
28065 return true;
28066 else
28067 block = context;
28071 /* Called during final while assembling the marker of the entry point
28072 for an inlined function. */
28074 static void
28075 dwarf2out_inline_entry (tree block)
28077 gcc_assert (debug_inline_points);
28079 /* If we can't represent it, don't bother. */
28080 if (!(dwarf_version >= 3 || !dwarf_strict))
28081 return;
28083 gcc_assert (DECL_P (block_ultimate_origin (block)));
28085 /* Sanity check the block tree. This would catch a case in which
28086 BLOCK got removed from the tree reachable from the outermost
28087 lexical block, but got retained in markers. It would still link
28088 back to its parents, but some ancestor would be missing a link
28089 down the path to the sub BLOCK. If the block got removed, its
28090 BLOCK_NUMBER will not be a usable value. */
28091 if (flag_checking)
28092 gcc_assert (block_within_block_p (block,
28093 DECL_INITIAL (current_function_decl),
28094 true));
28096 gcc_assert (inlined_function_outer_scope_p (block));
28097 gcc_assert (!lookup_block_die (block));
28099 if (BLOCK_FRAGMENT_ORIGIN (block))
28100 block = BLOCK_FRAGMENT_ORIGIN (block);
28101 /* Can the entry point ever not be at the beginning of an
28102 unfragmented lexical block? */
28103 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28104 || (cur_line_info_table
28105 && !ZERO_VIEW_P (cur_line_info_table->view))))
28106 return;
28108 if (!inline_entry_data_table)
28109 inline_entry_data_table
28110 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28113 inline_entry_data **iedp
28114 = inline_entry_data_table->find_slot_with_hash (block,
28115 htab_hash_pointer (block),
28116 INSERT);
28117 if (*iedp)
28118 /* ??? Ideally, we'd record all entry points for the same inlined
28119 function (some may have been duplicated by e.g. unrolling), but
28120 we have no way to represent that ATM. */
28121 return;
28123 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28124 ied->block = block;
28125 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28126 ied->label_num = BLOCK_NUMBER (block);
28127 if (cur_line_info_table)
28128 ied->view = cur_line_info_table->view;
28130 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28131 BLOCK_NUMBER (block));
28134 /* Called from finalize_size_functions for size functions so that their body
28135 can be encoded in the debug info to describe the layout of variable-length
28136 structures. */
28138 static void
28139 dwarf2out_size_function (tree decl)
28141 set_early_dwarf s;
28142 function_to_dwarf_procedure (decl);
28145 /* Note in one location list that text section has changed. */
28148 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28150 var_loc_list *list = *slot;
28151 if (list->first)
28152 list->last_before_switch
28153 = list->last->next ? list->last->next : list->last;
28154 return 1;
28157 /* Note in all location lists that text section has changed. */
28159 static void
28160 var_location_switch_text_section (void)
28162 if (decl_loc_table == NULL)
28163 return;
28165 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28168 /* Create a new line number table. */
28170 static dw_line_info_table *
28171 new_line_info_table (void)
28173 dw_line_info_table *table;
28175 table = ggc_cleared_alloc<dw_line_info_table> ();
28176 table->file_num = 1;
28177 table->line_num = 1;
28178 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28179 FORCE_RESET_NEXT_VIEW (table->view);
28180 table->symviews_since_reset = 0;
28182 return table;
28185 /* Lookup the "current" table into which we emit line info, so
28186 that we don't have to do it for every source line. */
28188 static void
28189 set_cur_line_info_table (section *sec)
28191 dw_line_info_table *table;
28193 if (sec == text_section)
28194 table = text_section_line_info;
28195 else if (sec == cold_text_section)
28197 table = cold_text_section_line_info;
28198 if (!table)
28200 cold_text_section_line_info = table = new_line_info_table ();
28201 table->end_label = cold_end_label;
28204 else
28206 const char *end_label;
28208 if (crtl->has_bb_partition)
28210 if (in_cold_section_p)
28211 end_label = crtl->subsections.cold_section_end_label;
28212 else
28213 end_label = crtl->subsections.hot_section_end_label;
28215 else
28217 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28218 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28219 current_function_funcdef_no);
28220 end_label = ggc_strdup (label);
28223 table = new_line_info_table ();
28224 table->end_label = end_label;
28226 vec_safe_push (separate_line_info, table);
28229 if (output_asm_line_debug_info ())
28230 table->is_stmt = (cur_line_info_table
28231 ? cur_line_info_table->is_stmt
28232 : DWARF_LINE_DEFAULT_IS_STMT_START);
28233 cur_line_info_table = table;
28237 /* We need to reset the locations at the beginning of each
28238 function. We can't do this in the end_function hook, because the
28239 declarations that use the locations won't have been output when
28240 that hook is called. Also compute have_multiple_function_sections here. */
28242 static void
28243 dwarf2out_begin_function (tree fun)
28245 section *sec = function_section (fun);
28247 if (sec != text_section)
28248 have_multiple_function_sections = true;
28250 if (crtl->has_bb_partition && !cold_text_section)
28252 gcc_assert (current_function_decl == fun);
28253 cold_text_section = unlikely_text_section ();
28254 switch_to_section (cold_text_section);
28255 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28256 switch_to_section (sec);
28259 call_site_count = 0;
28260 tail_call_site_count = 0;
28262 set_cur_line_info_table (sec);
28263 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28266 /* Helper function of dwarf2out_end_function, called only after emitting
28267 the very first function into assembly. Check if some .debug_loc range
28268 might end with a .LVL* label that could be equal to .Ltext0.
28269 In that case we must force using absolute addresses in .debug_loc ranges,
28270 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28271 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28272 list terminator.
28273 Set have_multiple_function_sections to true in that case and
28274 terminate htab traversal. */
28277 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28279 var_loc_list *entry = *slot;
28280 struct var_loc_node *node;
28282 node = entry->first;
28283 if (node && node->next && node->next->label)
28285 unsigned int i;
28286 const char *label = node->next->label;
28287 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28289 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28291 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28292 if (strcmp (label, loclabel) == 0)
28294 have_multiple_function_sections = true;
28295 return 0;
28299 return 1;
28302 /* Hook called after emitting a function into assembly.
28303 This does something only for the very first function emitted. */
28305 static void
28306 dwarf2out_end_function (unsigned int)
28308 if (in_first_function_p
28309 && !have_multiple_function_sections
28310 && first_loclabel_num_not_at_text_label
28311 && decl_loc_table)
28312 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28313 in_first_function_p = false;
28314 maybe_at_text_label_p = false;
28317 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28318 front-ends register a translation unit even before dwarf2out_init is
28319 called. */
28320 static tree main_translation_unit = NULL_TREE;
28322 /* Hook called by front-ends after they built their main translation unit.
28323 Associate comp_unit_die to UNIT. */
28325 static void
28326 dwarf2out_register_main_translation_unit (tree unit)
28328 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28329 && main_translation_unit == NULL_TREE);
28330 main_translation_unit = unit;
28331 /* If dwarf2out_init has not been called yet, it will perform the association
28332 itself looking at main_translation_unit. */
28333 if (decl_die_table != NULL)
28334 equate_decl_number_to_die (unit, comp_unit_die ());
28337 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28339 static void
28340 push_dw_line_info_entry (dw_line_info_table *table,
28341 enum dw_line_info_opcode opcode, unsigned int val)
28343 dw_line_info_entry e;
28344 e.opcode = opcode;
28345 e.val = val;
28346 vec_safe_push (table->entries, e);
28349 /* Output a label to mark the beginning of a source code line entry
28350 and record information relating to this source line, in
28351 'line_info_table' for later output of the .debug_line section. */
28352 /* ??? The discriminator parameter ought to be unsigned. */
28354 static void
28355 dwarf2out_source_line (unsigned int line, unsigned int column,
28356 const char *filename,
28357 int discriminator, bool is_stmt)
28359 unsigned int file_num;
28360 dw_line_info_table *table;
28361 static var_loc_view lvugid;
28363 /* 'line_info_table' information gathering is not needed when the debug
28364 info level is set to the lowest value. Also, the current DWARF-based
28365 debug formats do not use this info. */
28366 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28367 return;
28369 table = cur_line_info_table;
28371 if (line == 0)
28373 if (debug_variable_location_views
28374 && output_asm_line_debug_info ()
28375 && table && !RESETTING_VIEW_P (table->view))
28377 /* If we're using the assembler to compute view numbers, we
28378 can't issue a .loc directive for line zero, so we can't
28379 get a view number at this point. We might attempt to
28380 compute it from the previous view, or equate it to a
28381 subsequent view (though it might not be there!), but
28382 since we're omitting the line number entry, we might as
28383 well omit the view number as well. That means pretending
28384 it's a view number zero, which might very well turn out
28385 to be correct. ??? Extend the assembler so that the
28386 compiler could emit e.g. ".locview .LVU#", to output a
28387 view without changing line number information. We'd then
28388 have to count it in symviews_since_reset; when it's omitted,
28389 it doesn't count. */
28390 if (!zero_view_p)
28391 zero_view_p = BITMAP_GGC_ALLOC ();
28392 bitmap_set_bit (zero_view_p, table->view);
28393 if (flag_debug_asm)
28395 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28396 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28397 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28398 ASM_COMMENT_START);
28399 assemble_name (asm_out_file, label);
28400 putc ('\n', asm_out_file);
28402 table->view = ++lvugid;
28404 return;
28407 /* The discriminator column was added in dwarf4. Simplify the below
28408 by simply removing it if we're not supposed to output it. */
28409 if (dwarf_version < 4 && dwarf_strict)
28410 discriminator = 0;
28412 if (!debug_column_info)
28413 column = 0;
28415 file_num = maybe_emit_file (lookup_filename (filename));
28417 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28418 the debugger has used the second (possibly duplicate) line number
28419 at the beginning of the function to mark the end of the prologue.
28420 We could eliminate any other duplicates within the function. For
28421 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28422 that second line number entry. */
28423 /* Recall that this end-of-prologue indication is *not* the same thing
28424 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28425 to which the hook corresponds, follows the last insn that was
28426 emitted by gen_prologue. What we need is to precede the first insn
28427 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28428 insn that corresponds to something the user wrote. These may be
28429 very different locations once scheduling is enabled. */
28431 if (0 && file_num == table->file_num
28432 && line == table->line_num
28433 && column == table->column_num
28434 && discriminator == table->discrim_num
28435 && is_stmt == table->is_stmt)
28436 return;
28438 switch_to_section (current_function_section ());
28440 /* If requested, emit something human-readable. */
28441 if (flag_debug_asm)
28443 if (debug_column_info)
28444 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28445 filename, line, column);
28446 else
28447 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28448 filename, line);
28451 if (output_asm_line_debug_info ())
28453 /* Emit the .loc directive understood by GNU as. */
28454 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28455 file_num, line, is_stmt, discriminator */
28456 fputs ("\t.loc ", asm_out_file);
28457 fprint_ul (asm_out_file, file_num);
28458 putc (' ', asm_out_file);
28459 fprint_ul (asm_out_file, line);
28460 putc (' ', asm_out_file);
28461 fprint_ul (asm_out_file, column);
28463 if (is_stmt != table->is_stmt)
28465 #if HAVE_GAS_LOC_STMT
28466 fputs (" is_stmt ", asm_out_file);
28467 putc (is_stmt ? '1' : '0', asm_out_file);
28468 #endif
28470 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28472 gcc_assert (discriminator > 0);
28473 fputs (" discriminator ", asm_out_file);
28474 fprint_ul (asm_out_file, (unsigned long) discriminator);
28476 if (debug_variable_location_views)
28478 if (!RESETTING_VIEW_P (table->view))
28480 table->symviews_since_reset++;
28481 if (table->symviews_since_reset > symview_upper_bound)
28482 symview_upper_bound = table->symviews_since_reset;
28483 /* When we're using the assembler to compute view
28484 numbers, we output symbolic labels after "view" in
28485 .loc directives, and the assembler will set them for
28486 us, so that we can refer to the view numbers in
28487 location lists. The only exceptions are when we know
28488 a view will be zero: "-0" is a forced reset, used
28489 e.g. in the beginning of functions, whereas "0" tells
28490 the assembler to check that there was a PC change
28491 since the previous view, in a way that implicitly
28492 resets the next view. */
28493 fputs (" view ", asm_out_file);
28494 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28495 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28496 assemble_name (asm_out_file, label);
28497 table->view = ++lvugid;
28499 else
28501 table->symviews_since_reset = 0;
28502 if (FORCE_RESETTING_VIEW_P (table->view))
28503 fputs (" view -0", asm_out_file);
28504 else
28505 fputs (" view 0", asm_out_file);
28506 /* Mark the present view as a zero view. Earlier debug
28507 binds may have already added its id to loclists to be
28508 emitted later, so we can't reuse the id for something
28509 else. However, it's good to know whether a view is
28510 known to be zero, because then we may be able to
28511 optimize out locviews that are all zeros, so take
28512 note of it in zero_view_p. */
28513 if (!zero_view_p)
28514 zero_view_p = BITMAP_GGC_ALLOC ();
28515 bitmap_set_bit (zero_view_p, lvugid);
28516 table->view = ++lvugid;
28519 putc ('\n', asm_out_file);
28521 else
28523 unsigned int label_num = ++line_info_label_num;
28525 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28527 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28528 push_dw_line_info_entry (table, LI_adv_address, label_num);
28529 else
28530 push_dw_line_info_entry (table, LI_set_address, label_num);
28531 if (debug_variable_location_views)
28533 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28534 if (resetting)
28535 table->view = 0;
28537 if (flag_debug_asm)
28538 fprintf (asm_out_file, "\t%s view %s%d\n",
28539 ASM_COMMENT_START,
28540 resetting ? "-" : "",
28541 table->view);
28543 table->view++;
28545 if (file_num != table->file_num)
28546 push_dw_line_info_entry (table, LI_set_file, file_num);
28547 if (discriminator != table->discrim_num)
28548 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28549 if (is_stmt != table->is_stmt)
28550 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28551 push_dw_line_info_entry (table, LI_set_line, line);
28552 if (debug_column_info)
28553 push_dw_line_info_entry (table, LI_set_column, column);
28556 table->file_num = file_num;
28557 table->line_num = line;
28558 table->column_num = column;
28559 table->discrim_num = discriminator;
28560 table->is_stmt = is_stmt;
28561 table->in_use = true;
28564 /* Record a source file location for a DECL_IGNORED_P function. */
28566 static void
28567 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28568 const char *filename)
28570 dw_fde_ref fde = cfun->fde;
28572 fde->ignored_debug = false;
28573 set_cur_line_info_table (function_section (fde->decl));
28575 dwarf2out_source_line (line, column, filename, 0, true);
28578 /* Record the beginning of a new source file. */
28580 static void
28581 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28583 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28585 macinfo_entry e;
28586 e.code = DW_MACINFO_start_file;
28587 e.lineno = lineno;
28588 e.info = ggc_strdup (filename);
28589 vec_safe_push (macinfo_table, e);
28593 /* Record the end of a source file. */
28595 static void
28596 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28598 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28600 macinfo_entry e;
28601 e.code = DW_MACINFO_end_file;
28602 e.lineno = lineno;
28603 e.info = NULL;
28604 vec_safe_push (macinfo_table, e);
28608 /* Called from debug_define in toplev.c. The `buffer' parameter contains
28609 the tail part of the directive line, i.e. the part which is past the
28610 initial whitespace, #, whitespace, directive-name, whitespace part. */
28612 static void
28613 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28614 const char *buffer ATTRIBUTE_UNUSED)
28616 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28618 macinfo_entry e;
28619 /* Insert a dummy first entry to be able to optimize the whole
28620 predefined macro block using DW_MACRO_import. */
28621 if (macinfo_table->is_empty () && lineno <= 1)
28623 e.code = 0;
28624 e.lineno = 0;
28625 e.info = NULL;
28626 vec_safe_push (macinfo_table, e);
28628 e.code = DW_MACINFO_define;
28629 e.lineno = lineno;
28630 e.info = ggc_strdup (buffer);
28631 vec_safe_push (macinfo_table, e);
28635 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
28636 the tail part of the directive line, i.e. the part which is past the
28637 initial whitespace, #, whitespace, directive-name, whitespace part. */
28639 static void
28640 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28641 const char *buffer ATTRIBUTE_UNUSED)
28643 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28645 macinfo_entry e;
28646 /* Insert a dummy first entry to be able to optimize the whole
28647 predefined macro block using DW_MACRO_import. */
28648 if (macinfo_table->is_empty () && lineno <= 1)
28650 e.code = 0;
28651 e.lineno = 0;
28652 e.info = NULL;
28653 vec_safe_push (macinfo_table, e);
28655 e.code = DW_MACINFO_undef;
28656 e.lineno = lineno;
28657 e.info = ggc_strdup (buffer);
28658 vec_safe_push (macinfo_table, e);
28662 /* Helpers to manipulate hash table of CUs. */
28664 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28666 static inline hashval_t hash (const macinfo_entry *);
28667 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28670 inline hashval_t
28671 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28673 return htab_hash_string (entry->info);
28676 inline bool
28677 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28678 const macinfo_entry *entry2)
28680 return !strcmp (entry1->info, entry2->info);
28683 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28685 /* Output a single .debug_macinfo entry. */
28687 static void
28688 output_macinfo_op (macinfo_entry *ref)
28690 int file_num;
28691 size_t len;
28692 struct indirect_string_node *node;
28693 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28694 struct dwarf_file_data *fd;
28696 switch (ref->code)
28698 case DW_MACINFO_start_file:
28699 fd = lookup_filename (ref->info);
28700 file_num = maybe_emit_file (fd);
28701 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28702 dw2_asm_output_data_uleb128 (ref->lineno,
28703 "Included from line number %lu",
28704 (unsigned long) ref->lineno);
28705 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28706 break;
28707 case DW_MACINFO_end_file:
28708 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28709 break;
28710 case DW_MACINFO_define:
28711 case DW_MACINFO_undef:
28712 len = strlen (ref->info) + 1;
28713 if ((!dwarf_strict || dwarf_version >= 5)
28714 && len > (size_t) dwarf_offset_size
28715 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28716 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28718 if (dwarf_split_debug_info && dwarf_version >= 5)
28719 ref->code = ref->code == DW_MACINFO_define
28720 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
28721 else
28722 ref->code = ref->code == DW_MACINFO_define
28723 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28724 output_macinfo_op (ref);
28725 return;
28727 dw2_asm_output_data (1, ref->code,
28728 ref->code == DW_MACINFO_define
28729 ? "Define macro" : "Undefine macro");
28730 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28731 (unsigned long) ref->lineno);
28732 dw2_asm_output_nstring (ref->info, -1, "The macro");
28733 break;
28734 case DW_MACRO_define_strp:
28735 dw2_asm_output_data (1, ref->code, "Define macro strp");
28736 goto do_DW_MACRO_define_strpx;
28737 case DW_MACRO_undef_strp:
28738 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
28739 goto do_DW_MACRO_define_strpx;
28740 case DW_MACRO_define_strx:
28741 dw2_asm_output_data (1, ref->code, "Define macro strx");
28742 goto do_DW_MACRO_define_strpx;
28743 case DW_MACRO_undef_strx:
28744 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
28745 /* FALLTHRU */
28746 do_DW_MACRO_define_strpx:
28747 /* NB: dwarf2out_finish performs:
28748 1. save_macinfo_strings
28749 2. hash table traverse of index_string
28750 3. output_macinfo -> output_macinfo_op
28751 4. output_indirect_strings
28752 -> hash table traverse of output_index_string
28754 When output_macinfo_op is called, all index strings have been
28755 added to hash table by save_macinfo_strings and we can't pass
28756 INSERT to find_slot_with_hash which may expand hash table, even
28757 if no insertion is needed, and change hash table traverse order
28758 between index_string and output_index_string. */
28759 node = find_AT_string (ref->info, NO_INSERT);
28760 gcc_assert (node
28761 && (node->form == DW_FORM_strp
28762 || node->form == dwarf_FORM (DW_FORM_strx)));
28763 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28764 (unsigned long) ref->lineno);
28765 if (node->form == DW_FORM_strp)
28766 dw2_asm_output_offset (dwarf_offset_size, node->label,
28767 debug_str_section, "The macro: \"%s\"",
28768 ref->info);
28769 else
28770 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28771 ref->info);
28772 break;
28773 case DW_MACRO_import:
28774 dw2_asm_output_data (1, ref->code, "Import");
28775 ASM_GENERATE_INTERNAL_LABEL (label,
28776 DEBUG_MACRO_SECTION_LABEL,
28777 ref->lineno + macinfo_label_base);
28778 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
28779 break;
28780 default:
28781 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28782 ASM_COMMENT_START, (unsigned long) ref->code);
28783 break;
28787 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28788 other compilation unit .debug_macinfo sections. IDX is the first
28789 index of a define/undef, return the number of ops that should be
28790 emitted in a comdat .debug_macinfo section and emit
28791 a DW_MACRO_import entry referencing it.
28792 If the define/undef entry should be emitted normally, return 0. */
28794 static unsigned
28795 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28796 macinfo_hash_type **macinfo_htab)
28798 macinfo_entry *first, *second, *cur, *inc;
28799 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28800 unsigned char checksum[16];
28801 struct md5_ctx ctx;
28802 char *grp_name, *tail;
28803 const char *base;
28804 unsigned int i, count, encoded_filename_len, linebuf_len;
28805 macinfo_entry **slot;
28807 first = &(*macinfo_table)[idx];
28808 second = &(*macinfo_table)[idx + 1];
28810 /* Optimize only if there are at least two consecutive define/undef ops,
28811 and either all of them are before first DW_MACINFO_start_file
28812 with lineno {0,1} (i.e. predefined macro block), or all of them are
28813 in some included header file. */
28814 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28815 return 0;
28816 if (vec_safe_is_empty (files))
28818 if (first->lineno > 1 || second->lineno > 1)
28819 return 0;
28821 else if (first->lineno == 0)
28822 return 0;
28824 /* Find the last define/undef entry that can be grouped together
28825 with first and at the same time compute md5 checksum of their
28826 codes, linenumbers and strings. */
28827 md5_init_ctx (&ctx);
28828 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28829 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28830 break;
28831 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28832 break;
28833 else
28835 unsigned char code = cur->code;
28836 md5_process_bytes (&code, 1, &ctx);
28837 checksum_uleb128 (cur->lineno, &ctx);
28838 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28840 md5_finish_ctx (&ctx, checksum);
28841 count = i - idx;
28843 /* From the containing include filename (if any) pick up just
28844 usable characters from its basename. */
28845 if (vec_safe_is_empty (files))
28846 base = "";
28847 else
28848 base = lbasename (files->last ().info);
28849 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28850 if (ISIDNUM (base[i]) || base[i] == '.')
28851 encoded_filename_len++;
28852 /* Count . at the end. */
28853 if (encoded_filename_len)
28854 encoded_filename_len++;
28856 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28857 linebuf_len = strlen (linebuf);
28859 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28860 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28861 + 16 * 2 + 1);
28862 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
28863 tail = grp_name + 4;
28864 if (encoded_filename_len)
28866 for (i = 0; base[i]; i++)
28867 if (ISIDNUM (base[i]) || base[i] == '.')
28868 *tail++ = base[i];
28869 *tail++ = '.';
28871 memcpy (tail, linebuf, linebuf_len);
28872 tail += linebuf_len;
28873 *tail++ = '.';
28874 for (i = 0; i < 16; i++)
28875 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28877 /* Construct a macinfo_entry for DW_MACRO_import
28878 in the empty vector entry before the first define/undef. */
28879 inc = &(*macinfo_table)[idx - 1];
28880 inc->code = DW_MACRO_import;
28881 inc->lineno = 0;
28882 inc->info = ggc_strdup (grp_name);
28883 if (!*macinfo_htab)
28884 *macinfo_htab = new macinfo_hash_type (10);
28885 /* Avoid emitting duplicates. */
28886 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28887 if (*slot != NULL)
28889 inc->code = 0;
28890 inc->info = NULL;
28891 /* If such an entry has been used before, just emit
28892 a DW_MACRO_import op. */
28893 inc = *slot;
28894 output_macinfo_op (inc);
28895 /* And clear all macinfo_entry in the range to avoid emitting them
28896 in the second pass. */
28897 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28899 cur->code = 0;
28900 cur->info = NULL;
28903 else
28905 *slot = inc;
28906 inc->lineno = (*macinfo_htab)->elements ();
28907 output_macinfo_op (inc);
28909 return count;
28912 /* Save any strings needed by the macinfo table in the debug str
28913 table. All strings must be collected into the table by the time
28914 index_string is called. */
28916 static void
28917 save_macinfo_strings (void)
28919 unsigned len;
28920 unsigned i;
28921 macinfo_entry *ref;
28923 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28925 switch (ref->code)
28927 /* Match the logic in output_macinfo_op to decide on
28928 indirect strings. */
28929 case DW_MACINFO_define:
28930 case DW_MACINFO_undef:
28931 len = strlen (ref->info) + 1;
28932 if ((!dwarf_strict || dwarf_version >= 5)
28933 && len > (unsigned) dwarf_offset_size
28934 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28935 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28936 set_indirect_string (find_AT_string (ref->info));
28937 break;
28938 case DW_MACINFO_start_file:
28939 /* -gsplit-dwarf -g3 will also output filename as indirect
28940 string. */
28941 if (!dwarf_split_debug_info)
28942 break;
28943 /* Fall through. */
28944 case DW_MACRO_define_strp:
28945 case DW_MACRO_undef_strp:
28946 case DW_MACRO_define_strx:
28947 case DW_MACRO_undef_strx:
28948 set_indirect_string (find_AT_string (ref->info));
28949 break;
28950 default:
28951 break;
28956 /* Output macinfo section(s). */
28958 static void
28959 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28961 unsigned i;
28962 unsigned long length = vec_safe_length (macinfo_table);
28963 macinfo_entry *ref;
28964 vec<macinfo_entry, va_gc> *files = NULL;
28965 macinfo_hash_type *macinfo_htab = NULL;
28966 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28968 if (! length)
28969 return;
28971 /* output_macinfo* uses these interchangeably. */
28972 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28973 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28974 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28975 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28977 /* AIX Assembler inserts the length, so adjust the reference to match the
28978 offset expected by debuggers. */
28979 strcpy (dl_section_ref, debug_line_label);
28980 if (XCOFF_DEBUGGING_INFO)
28981 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28983 /* For .debug_macro emit the section header. */
28984 if (!dwarf_strict || dwarf_version >= 5)
28986 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28987 "DWARF macro version number");
28988 if (dwarf_offset_size == 8)
28989 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28990 else
28991 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28992 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
28993 debug_line_section, NULL);
28996 /* In the first loop, it emits the primary .debug_macinfo section
28997 and after each emitted op the macinfo_entry is cleared.
28998 If a longer range of define/undef ops can be optimized using
28999 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29000 the vector before the first define/undef in the range and the
29001 whole range of define/undef ops is not emitted and kept. */
29002 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29004 switch (ref->code)
29006 case DW_MACINFO_start_file:
29007 vec_safe_push (files, *ref);
29008 break;
29009 case DW_MACINFO_end_file:
29010 if (!vec_safe_is_empty (files))
29011 files->pop ();
29012 break;
29013 case DW_MACINFO_define:
29014 case DW_MACINFO_undef:
29015 if ((!dwarf_strict || dwarf_version >= 5)
29016 && HAVE_COMDAT_GROUP
29017 && vec_safe_length (files) != 1
29018 && i > 0
29019 && i + 1 < length
29020 && (*macinfo_table)[i - 1].code == 0)
29022 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29023 if (count)
29025 i += count - 1;
29026 continue;
29029 break;
29030 case 0:
29031 /* A dummy entry may be inserted at the beginning to be able
29032 to optimize the whole block of predefined macros. */
29033 if (i == 0)
29034 continue;
29035 default:
29036 break;
29038 output_macinfo_op (ref);
29039 ref->info = NULL;
29040 ref->code = 0;
29043 if (!macinfo_htab)
29044 return;
29046 /* Save the number of transparent includes so we can adjust the
29047 label number for the fat LTO object DWARF. */
29048 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29050 delete macinfo_htab;
29051 macinfo_htab = NULL;
29053 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29054 terminate the current chain and switch to a new comdat .debug_macinfo
29055 section and emit the define/undef entries within it. */
29056 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29057 switch (ref->code)
29059 case 0:
29060 continue;
29061 case DW_MACRO_import:
29063 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29064 tree comdat_key = get_identifier (ref->info);
29065 /* Terminate the previous .debug_macinfo section. */
29066 dw2_asm_output_data (1, 0, "End compilation unit");
29067 targetm.asm_out.named_section (debug_macinfo_section_name,
29068 SECTION_DEBUG
29069 | SECTION_LINKONCE
29070 | (early_lto_debug
29071 ? SECTION_EXCLUDE : 0),
29072 comdat_key);
29073 ASM_GENERATE_INTERNAL_LABEL (label,
29074 DEBUG_MACRO_SECTION_LABEL,
29075 ref->lineno + macinfo_label_base);
29076 ASM_OUTPUT_LABEL (asm_out_file, label);
29077 ref->code = 0;
29078 ref->info = NULL;
29079 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29080 "DWARF macro version number");
29081 if (dwarf_offset_size == 8)
29082 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29083 else
29084 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29086 break;
29087 case DW_MACINFO_define:
29088 case DW_MACINFO_undef:
29089 output_macinfo_op (ref);
29090 ref->code = 0;
29091 ref->info = NULL;
29092 break;
29093 default:
29094 gcc_unreachable ();
29097 macinfo_label_base += macinfo_label_base_adj;
29100 /* As init_sections_and_labels may get called multiple times, have a
29101 generation count for labels. */
29102 static unsigned init_sections_and_labels_generation;
29104 /* Initialize the various sections and labels for dwarf output and prefix
29105 them with PREFIX if non-NULL. Returns the generation (zero based
29106 number of times function was called). */
29108 static unsigned
29109 init_sections_and_labels (bool early_lto_debug)
29111 if (early_lto_debug)
29113 if (!dwarf_split_debug_info)
29115 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29116 SECTION_DEBUG | SECTION_EXCLUDE,
29117 NULL);
29118 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29119 SECTION_DEBUG | SECTION_EXCLUDE,
29120 NULL);
29121 debug_macinfo_section_name
29122 = ((dwarf_strict && dwarf_version < 5)
29123 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29124 debug_macinfo_section = get_section (debug_macinfo_section_name,
29125 SECTION_DEBUG
29126 | SECTION_EXCLUDE, NULL);
29128 else
29130 /* ??? Which of the following do we need early? */
29131 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29132 SECTION_DEBUG | SECTION_EXCLUDE,
29133 NULL);
29134 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29135 SECTION_DEBUG | SECTION_EXCLUDE,
29136 NULL);
29137 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29138 SECTION_DEBUG
29139 | SECTION_EXCLUDE, NULL);
29140 debug_skeleton_abbrev_section
29141 = get_section (DEBUG_LTO_ABBREV_SECTION,
29142 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29143 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29144 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29145 init_sections_and_labels_generation);
29147 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29148 stay in the main .o, but the skeleton_line goes into the split
29149 off dwo. */
29150 debug_skeleton_line_section
29151 = get_section (DEBUG_LTO_LINE_SECTION,
29152 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29153 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29154 DEBUG_SKELETON_LINE_SECTION_LABEL,
29155 init_sections_and_labels_generation);
29156 debug_str_offsets_section
29157 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29158 SECTION_DEBUG | SECTION_EXCLUDE,
29159 NULL);
29160 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29161 DEBUG_SKELETON_INFO_SECTION_LABEL,
29162 init_sections_and_labels_generation);
29163 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29164 DEBUG_STR_DWO_SECTION_FLAGS,
29165 NULL);
29166 debug_macinfo_section_name
29167 = ((dwarf_strict && dwarf_version < 5)
29168 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29169 debug_macinfo_section = get_section (debug_macinfo_section_name,
29170 SECTION_DEBUG | SECTION_EXCLUDE,
29171 NULL);
29173 /* For macro info and the file table we have to refer to a
29174 debug_line section. */
29175 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29176 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29177 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29178 DEBUG_LINE_SECTION_LABEL,
29179 init_sections_and_labels_generation);
29181 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29182 DEBUG_STR_SECTION_FLAGS
29183 | SECTION_EXCLUDE, NULL);
29184 if (!dwarf_split_debug_info)
29185 debug_line_str_section
29186 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29187 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29189 else
29191 if (!dwarf_split_debug_info)
29193 debug_info_section = get_section (DEBUG_INFO_SECTION,
29194 SECTION_DEBUG, NULL);
29195 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29196 SECTION_DEBUG, NULL);
29197 debug_loc_section = get_section (dwarf_version >= 5
29198 ? DEBUG_LOCLISTS_SECTION
29199 : DEBUG_LOC_SECTION,
29200 SECTION_DEBUG, NULL);
29201 debug_macinfo_section_name
29202 = ((dwarf_strict && dwarf_version < 5)
29203 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29204 debug_macinfo_section = get_section (debug_macinfo_section_name,
29205 SECTION_DEBUG, NULL);
29207 else
29209 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29210 SECTION_DEBUG | SECTION_EXCLUDE,
29211 NULL);
29212 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29213 SECTION_DEBUG | SECTION_EXCLUDE,
29214 NULL);
29215 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29216 SECTION_DEBUG, NULL);
29217 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29218 SECTION_DEBUG, NULL);
29219 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29220 SECTION_DEBUG, NULL);
29221 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29222 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29223 init_sections_and_labels_generation);
29225 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29226 stay in the main .o, but the skeleton_line goes into the
29227 split off dwo. */
29228 debug_skeleton_line_section
29229 = get_section (DEBUG_DWO_LINE_SECTION,
29230 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29231 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29232 DEBUG_SKELETON_LINE_SECTION_LABEL,
29233 init_sections_and_labels_generation);
29234 debug_str_offsets_section
29235 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29236 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29237 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29238 DEBUG_SKELETON_INFO_SECTION_LABEL,
29239 init_sections_and_labels_generation);
29240 debug_loc_section = get_section (dwarf_version >= 5
29241 ? DEBUG_DWO_LOCLISTS_SECTION
29242 : DEBUG_DWO_LOC_SECTION,
29243 SECTION_DEBUG | SECTION_EXCLUDE,
29244 NULL);
29245 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29246 DEBUG_STR_DWO_SECTION_FLAGS,
29247 NULL);
29248 debug_macinfo_section_name
29249 = ((dwarf_strict && dwarf_version < 5)
29250 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29251 debug_macinfo_section = get_section (debug_macinfo_section_name,
29252 SECTION_DEBUG | SECTION_EXCLUDE,
29253 NULL);
29254 if (dwarf_version >= 5)
29255 debug_ranges_dwo_section
29256 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29257 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29259 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29260 SECTION_DEBUG, NULL);
29261 debug_line_section = get_section (DEBUG_LINE_SECTION,
29262 SECTION_DEBUG, NULL);
29263 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29264 SECTION_DEBUG, NULL);
29265 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29266 SECTION_DEBUG, NULL);
29267 debug_str_section = get_section (DEBUG_STR_SECTION,
29268 DEBUG_STR_SECTION_FLAGS, NULL);
29269 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29270 || asm_outputs_debug_line_str ())
29271 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29272 DEBUG_STR_SECTION_FLAGS, NULL);
29274 debug_ranges_section = get_section (dwarf_version >= 5
29275 ? DEBUG_RNGLISTS_SECTION
29276 : DEBUG_RANGES_SECTION,
29277 SECTION_DEBUG, NULL);
29278 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29279 SECTION_DEBUG, NULL);
29282 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29283 DEBUG_ABBREV_SECTION_LABEL,
29284 init_sections_and_labels_generation);
29285 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29286 DEBUG_INFO_SECTION_LABEL,
29287 init_sections_and_labels_generation);
29288 info_section_emitted = false;
29289 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29290 DEBUG_LINE_SECTION_LABEL,
29291 init_sections_and_labels_generation);
29292 /* There are up to 6 unique ranges labels per generation.
29293 See also output_rnglists. */
29294 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29295 DEBUG_RANGES_SECTION_LABEL,
29296 init_sections_and_labels_generation * 6);
29297 if (dwarf_version >= 5 && dwarf_split_debug_info)
29298 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29299 DEBUG_RANGES_SECTION_LABEL,
29300 1 + init_sections_and_labels_generation * 6);
29301 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29302 DEBUG_ADDR_SECTION_LABEL,
29303 init_sections_and_labels_generation);
29304 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29305 (dwarf_strict && dwarf_version < 5)
29306 ? DEBUG_MACINFO_SECTION_LABEL
29307 : DEBUG_MACRO_SECTION_LABEL,
29308 init_sections_and_labels_generation);
29309 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29310 init_sections_and_labels_generation);
29312 ++init_sections_and_labels_generation;
29313 return init_sections_and_labels_generation - 1;
29316 /* Set up for Dwarf output at the start of compilation. */
29318 static void
29319 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29321 /* Allocate the file_table. */
29322 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29324 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29325 /* Allocate the decl_die_table. */
29326 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29328 /* Allocate the decl_loc_table. */
29329 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29331 /* Allocate the cached_dw_loc_list_table. */
29332 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29334 /* Allocate the initial hunk of the abbrev_die_table. */
29335 vec_alloc (abbrev_die_table, 256);
29336 /* Zero-th entry is allocated, but unused. */
29337 abbrev_die_table->quick_push (NULL);
29339 /* Allocate the dwarf_proc_stack_usage_map. */
29340 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29342 /* Allocate the pubtypes and pubnames vectors. */
29343 vec_alloc (pubname_table, 32);
29344 vec_alloc (pubtype_table, 32);
29346 vec_alloc (incomplete_types, 64);
29348 vec_alloc (used_rtx_array, 32);
29350 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29351 vec_alloc (macinfo_table, 64);
29352 #endif
29354 /* If front-ends already registered a main translation unit but we were not
29355 ready to perform the association, do this now. */
29356 if (main_translation_unit != NULL_TREE)
29357 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29360 /* Called before compile () starts outputtting functions, variables
29361 and toplevel asms into assembly. */
29363 static void
29364 dwarf2out_assembly_start (void)
29366 if (text_section_line_info)
29367 return;
29369 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29370 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29371 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29372 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29373 COLD_TEXT_SECTION_LABEL, 0);
29374 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29376 switch_to_section (text_section);
29377 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29378 #endif
29380 /* Make sure the line number table for .text always exists. */
29381 text_section_line_info = new_line_info_table ();
29382 text_section_line_info->end_label = text_end_label;
29384 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29385 cur_line_info_table = text_section_line_info;
29386 #endif
29388 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29389 && dwarf2out_do_cfi_asm ()
29390 && !dwarf2out_do_eh_frame ())
29391 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29393 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29394 if (output_asm_line_debug_info () && dwarf_version >= 5)
29396 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29397 tell it the comp_dir and main file name for the zero entry
29398 line table. */
29399 const char *comp_dir, *filename0;
29401 comp_dir = comp_dir_string ();
29402 if (comp_dir == NULL)
29403 comp_dir = "";
29405 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29406 if (filename0 == NULL)
29407 filename0 = "";
29409 fprintf (asm_out_file, "\t.file 0 ");
29410 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29411 fputc (' ', asm_out_file);
29412 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29413 fputc ('\n', asm_out_file);
29415 else
29416 #endif
29417 /* Work around for PR101575: output a dummy .file directive. */
29418 if (!last_emitted_file && dwarf_debuginfo_p ()
29419 && debug_info_level >= DINFO_LEVEL_TERSE)
29421 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29423 if (filename0 == NULL)
29424 filename0 = "<dummy>";
29425 maybe_emit_file (lookup_filename (filename0));
29429 /* A helper function for dwarf2out_finish called through
29430 htab_traverse. Assign a string its index. All strings must be
29431 collected into the table by the time index_string is called,
29432 because the indexing code relies on htab_traverse to traverse nodes
29433 in the same order for each run. */
29436 index_string (indirect_string_node **h, unsigned int *index)
29438 indirect_string_node *node = *h;
29440 find_string_form (node);
29441 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29443 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29444 node->index = *index;
29445 *index += 1;
29447 return 1;
29450 /* A helper function for output_indirect_strings called through
29451 htab_traverse. Output the offset to a string and update the
29452 current offset. */
29455 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29457 indirect_string_node *node = *h;
29459 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29461 /* Assert that this node has been assigned an index. */
29462 gcc_assert (node->index != NO_INDEX_ASSIGNED
29463 && node->index != NOT_INDEXED);
29464 dw2_asm_output_data (dwarf_offset_size, *offset,
29465 "indexed string 0x%x: %s", node->index, node->str);
29466 *offset += strlen (node->str) + 1;
29468 return 1;
29471 /* A helper function for dwarf2out_finish called through
29472 htab_traverse. Output the indexed string. */
29475 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29477 struct indirect_string_node *node = *h;
29479 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29481 /* Assert that the strings are output in the same order as their
29482 indexes were assigned. */
29483 gcc_assert (*cur_idx == node->index);
29484 assemble_string (node->str, strlen (node->str) + 1);
29485 *cur_idx += 1;
29487 return 1;
29490 /* A helper function for output_indirect_strings. Counts the number
29491 of index strings offsets. Must match the logic of the functions
29492 output_index_string[_offsets] above. */
29494 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29496 struct indirect_string_node *node = *h;
29498 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29499 *last_idx += 1;
29500 return 1;
29503 /* A helper function for dwarf2out_finish called through
29504 htab_traverse. Emit one queued .debug_str string. */
29507 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29509 struct indirect_string_node *node = *h;
29511 node->form = find_string_form (node);
29512 if (node->form == form && node->refcount > 0)
29514 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29515 assemble_string (node->str, strlen (node->str) + 1);
29518 return 1;
29521 /* Output the indexed string table. */
29523 static void
29524 output_indirect_strings (void)
29526 switch_to_section (debug_str_section);
29527 if (!dwarf_split_debug_info)
29528 debug_str_hash->traverse<enum dwarf_form,
29529 output_indirect_string> (DW_FORM_strp);
29530 else
29532 unsigned int offset = 0;
29533 unsigned int cur_idx = 0;
29535 if (skeleton_debug_str_hash)
29536 skeleton_debug_str_hash->traverse<enum dwarf_form,
29537 output_indirect_string> (DW_FORM_strp);
29539 switch_to_section (debug_str_offsets_section);
29540 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29541 header. Note that we don't need to generate a label to the
29542 actual index table following the header here, because this is
29543 for the split dwarf case only. In an .dwo file there is only
29544 one string offsets table (and one debug info section). But
29545 if we would start using string offset tables for the main (or
29546 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29547 pointing to the actual index after the header. Split dwarf
29548 units will never have a string offsets base attribute. When
29549 a split unit is moved into a .dwp file the string offsets can
29550 be found through the .debug_cu_index section table. */
29551 if (dwarf_version >= 5)
29553 unsigned int last_idx = 0;
29554 unsigned long str_offsets_length;
29556 debug_str_hash->traverse_noresize
29557 <unsigned int *, count_index_strings> (&last_idx);
29558 str_offsets_length = last_idx * dwarf_offset_size + 4;
29559 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29560 dw2_asm_output_data (4, 0xffffffff,
29561 "Escape value for 64-bit DWARF extension");
29562 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29563 "Length of string offsets unit");
29564 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29565 dw2_asm_output_data (2, 0, "Header zero padding");
29567 debug_str_hash->traverse_noresize
29568 <unsigned int *, output_index_string_offset> (&offset);
29569 switch_to_section (debug_str_dwo_section);
29570 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29571 (&cur_idx);
29575 /* Callback for htab_traverse to assign an index to an entry in the
29576 table, and to write that entry to the .debug_addr section. */
29579 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29581 addr_table_entry *entry = *slot;
29583 if (entry->refcount == 0)
29585 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29586 || entry->index == NOT_INDEXED);
29587 return 1;
29590 gcc_assert (entry->index == *cur_index);
29591 (*cur_index)++;
29593 switch (entry->kind)
29595 case ate_kind_rtx:
29596 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29597 "0x%x", entry->index);
29598 break;
29599 case ate_kind_rtx_dtprel:
29600 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29601 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29602 DWARF2_ADDR_SIZE,
29603 entry->addr.rtl);
29604 fputc ('\n', asm_out_file);
29605 break;
29606 case ate_kind_label:
29607 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29608 "0x%x", entry->index);
29609 break;
29610 default:
29611 gcc_unreachable ();
29613 return 1;
29616 /* A helper function for dwarf2out_finish. Counts the number
29617 of indexed addresses. Must match the logic of the functions
29618 output_addr_table_entry above. */
29620 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29622 addr_table_entry *entry = *slot;
29624 if (entry->refcount > 0)
29625 *last_idx += 1;
29626 return 1;
29629 /* Produce the .debug_addr section. */
29631 static void
29632 output_addr_table (void)
29634 unsigned int index = 0;
29635 if (addr_index_table == NULL || addr_index_table->size () == 0)
29636 return;
29638 switch_to_section (debug_addr_section);
29639 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29640 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29641 before DWARF5, didn't have a header for .debug_addr units.
29642 DWARF5 specifies a small header when address tables are used. */
29643 if (dwarf_version >= 5)
29645 unsigned int last_idx = 0;
29646 unsigned long addrs_length;
29648 addr_index_table->traverse_noresize
29649 <unsigned int *, count_index_addrs> (&last_idx);
29650 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29652 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29653 dw2_asm_output_data (4, 0xffffffff,
29654 "Escape value for 64-bit DWARF extension");
29655 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29656 "Length of Address Unit");
29657 dw2_asm_output_data (2, 5, "DWARF addr version");
29658 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29659 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29661 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29663 addr_index_table
29664 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29667 #if ENABLE_ASSERT_CHECKING
29668 /* Verify that all marks are clear. */
29670 static void
29671 verify_marks_clear (dw_die_ref die)
29673 dw_die_ref c;
29675 gcc_assert (! die->die_mark);
29676 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29678 #endif /* ENABLE_ASSERT_CHECKING */
29680 /* Clear the marks for a die and its children.
29681 Be cool if the mark isn't set. */
29683 static void
29684 prune_unmark_dies (dw_die_ref die)
29686 dw_die_ref c;
29688 if (die->die_mark)
29689 die->die_mark = 0;
29690 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29693 /* Given LOC that is referenced by a DIE we're marking as used, find all
29694 referenced DWARF procedures it references and mark them as used. */
29696 static void
29697 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29699 for (; loc != NULL; loc = loc->dw_loc_next)
29700 switch (loc->dw_loc_opc)
29702 case DW_OP_implicit_pointer:
29703 case DW_OP_convert:
29704 case DW_OP_reinterpret:
29705 case DW_OP_GNU_implicit_pointer:
29706 case DW_OP_GNU_convert:
29707 case DW_OP_GNU_reinterpret:
29708 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29709 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29710 break;
29711 case DW_OP_GNU_variable_value:
29712 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29714 dw_die_ref ref
29715 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29716 if (ref == NULL)
29717 break;
29718 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29719 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29720 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29722 /* FALLTHRU */
29723 case DW_OP_call2:
29724 case DW_OP_call4:
29725 case DW_OP_call_ref:
29726 case DW_OP_const_type:
29727 case DW_OP_GNU_const_type:
29728 case DW_OP_GNU_parameter_ref:
29729 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29730 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29731 break;
29732 case DW_OP_regval_type:
29733 case DW_OP_deref_type:
29734 case DW_OP_GNU_regval_type:
29735 case DW_OP_GNU_deref_type:
29736 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29737 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29738 break;
29739 case DW_OP_entry_value:
29740 case DW_OP_GNU_entry_value:
29741 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29742 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29743 break;
29744 default:
29745 break;
29749 /* Given DIE that we're marking as used, find any other dies
29750 it references as attributes and mark them as used. */
29752 static void
29753 prune_unused_types_walk_attribs (dw_die_ref die)
29755 dw_attr_node *a;
29756 unsigned ix;
29758 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29760 switch (AT_class (a))
29762 /* Make sure DWARF procedures referenced by location descriptions will
29763 get emitted. */
29764 case dw_val_class_loc:
29765 prune_unused_types_walk_loc_descr (AT_loc (a));
29766 break;
29767 case dw_val_class_loc_list:
29768 for (dw_loc_list_ref list = AT_loc_list (a);
29769 list != NULL;
29770 list = list->dw_loc_next)
29771 prune_unused_types_walk_loc_descr (list->expr);
29772 break;
29774 case dw_val_class_view_list:
29775 /* This points to a loc_list in another attribute, so it's
29776 already covered. */
29777 break;
29779 case dw_val_class_die_ref:
29780 /* A reference to another DIE.
29781 Make sure that it will get emitted.
29782 If it was broken out into a comdat group, don't follow it. */
29783 if (! AT_ref (a)->comdat_type_p
29784 || a->dw_attr == DW_AT_specification)
29785 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29786 break;
29788 case dw_val_class_str:
29789 /* Set the string's refcount to 0 so that prune_unused_types_mark
29790 accounts properly for it. */
29791 a->dw_attr_val.v.val_str->refcount = 0;
29792 break;
29794 default:
29795 break;
29800 /* Mark the generic parameters and arguments children DIEs of DIE. */
29802 static void
29803 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29805 dw_die_ref c;
29807 if (die == NULL || die->die_child == NULL)
29808 return;
29809 c = die->die_child;
29812 if (is_template_parameter (c))
29813 prune_unused_types_mark (c, 1);
29814 c = c->die_sib;
29815 } while (c && c != die->die_child);
29818 /* Mark DIE as being used. If DOKIDS is true, then walk down
29819 to DIE's children. */
29821 static void
29822 prune_unused_types_mark (dw_die_ref die, int dokids)
29824 dw_die_ref c;
29826 if (die->die_mark == 0)
29828 /* We haven't done this node yet. Mark it as used. */
29829 die->die_mark = 1;
29830 /* If this is the DIE of a generic type instantiation,
29831 mark the children DIEs that describe its generic parms and
29832 args. */
29833 prune_unused_types_mark_generic_parms_dies (die);
29835 /* We also have to mark its parents as used.
29836 (But we don't want to mark our parent's kids due to this,
29837 unless it is a class.) */
29838 if (die->die_parent)
29839 prune_unused_types_mark (die->die_parent,
29840 class_scope_p (die->die_parent));
29842 /* Mark any referenced nodes. */
29843 prune_unused_types_walk_attribs (die);
29845 /* If this node is a specification,
29846 also mark the definition, if it exists. */
29847 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29848 prune_unused_types_mark (die->die_definition, 1);
29851 if (dokids && die->die_mark != 2)
29853 /* We need to walk the children, but haven't done so yet.
29854 Remember that we've walked the kids. */
29855 die->die_mark = 2;
29857 /* If this is an array type, we need to make sure our
29858 kids get marked, even if they're types. If we're
29859 breaking out types into comdat sections, do this
29860 for all type definitions. */
29861 if (die->die_tag == DW_TAG_array_type
29862 || (use_debug_types
29863 && is_type_die (die) && ! is_declaration_die (die)))
29864 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29865 else
29866 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29870 /* For local classes, look if any static member functions were emitted
29871 and if so, mark them. */
29873 static void
29874 prune_unused_types_walk_local_classes (dw_die_ref die)
29876 dw_die_ref c;
29878 if (die->die_mark == 2)
29879 return;
29881 switch (die->die_tag)
29883 case DW_TAG_structure_type:
29884 case DW_TAG_union_type:
29885 case DW_TAG_class_type:
29886 case DW_TAG_interface_type:
29887 break;
29889 case DW_TAG_subprogram:
29890 if (!get_AT_flag (die, DW_AT_declaration)
29891 || die->die_definition != NULL)
29892 prune_unused_types_mark (die, 1);
29893 return;
29895 default:
29896 return;
29899 /* Mark children. */
29900 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29903 /* Walk the tree DIE and mark types that we actually use. */
29905 static void
29906 prune_unused_types_walk (dw_die_ref die)
29908 dw_die_ref c;
29910 /* Don't do anything if this node is already marked and
29911 children have been marked as well. */
29912 if (die->die_mark == 2)
29913 return;
29915 switch (die->die_tag)
29917 case DW_TAG_structure_type:
29918 case DW_TAG_union_type:
29919 case DW_TAG_class_type:
29920 case DW_TAG_interface_type:
29921 if (die->die_perennial_p)
29922 break;
29924 for (c = die->die_parent; c; c = c->die_parent)
29925 if (c->die_tag == DW_TAG_subprogram)
29926 break;
29928 /* Finding used static member functions inside of classes
29929 is needed just for local classes, because for other classes
29930 static member function DIEs with DW_AT_specification
29931 are emitted outside of the DW_TAG_*_type. If we ever change
29932 it, we'd need to call this even for non-local classes. */
29933 if (c)
29934 prune_unused_types_walk_local_classes (die);
29936 /* It's a type node --- don't mark it. */
29937 return;
29939 case DW_TAG_const_type:
29940 case DW_TAG_packed_type:
29941 case DW_TAG_pointer_type:
29942 case DW_TAG_reference_type:
29943 case DW_TAG_rvalue_reference_type:
29944 case DW_TAG_volatile_type:
29945 case DW_TAG_typedef:
29946 case DW_TAG_array_type:
29947 case DW_TAG_friend:
29948 case DW_TAG_enumeration_type:
29949 case DW_TAG_subroutine_type:
29950 case DW_TAG_string_type:
29951 case DW_TAG_set_type:
29952 case DW_TAG_subrange_type:
29953 case DW_TAG_ptr_to_member_type:
29954 case DW_TAG_file_type:
29955 /* Type nodes are useful only when other DIEs reference them --- don't
29956 mark them. */
29957 /* FALLTHROUGH */
29959 case DW_TAG_dwarf_procedure:
29960 /* Likewise for DWARF procedures. */
29962 if (die->die_perennial_p)
29963 break;
29965 return;
29967 case DW_TAG_variable:
29968 if (flag_debug_only_used_symbols)
29970 if (die->die_perennial_p)
29971 break;
29973 /* For static data members, the declaration in the class is supposed
29974 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
29975 DWARF5. DW_TAG_member will be marked, so mark even such
29976 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
29977 attribute. */
29978 if (dwarf_version >= 5
29979 && class_scope_p (die->die_parent)
29980 && get_AT (die, DW_AT_const_value))
29981 break;
29983 /* premark_used_variables marks external variables --- don't mark
29984 them here. But function-local externals are always considered
29985 used. */
29986 if (get_AT (die, DW_AT_external))
29988 for (c = die->die_parent; c; c = c->die_parent)
29989 if (c->die_tag == DW_TAG_subprogram)
29990 break;
29991 if (!c)
29992 return;
29995 /* FALLTHROUGH */
29997 default:
29998 /* Mark everything else. */
29999 break;
30002 if (die->die_mark == 0)
30004 die->die_mark = 1;
30006 /* Now, mark any dies referenced from here. */
30007 prune_unused_types_walk_attribs (die);
30010 die->die_mark = 2;
30012 /* Mark children. */
30013 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30016 /* Increment the string counts on strings referred to from DIE's
30017 attributes. */
30019 static void
30020 prune_unused_types_update_strings (dw_die_ref die)
30022 dw_attr_node *a;
30023 unsigned ix;
30025 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30026 if (AT_class (a) == dw_val_class_str)
30028 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30029 s->refcount++;
30030 /* Avoid unnecessarily putting strings that are used less than
30031 twice in the hash table. */
30032 if (s->form != DW_FORM_line_strp
30033 && (s->refcount
30034 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30036 indirect_string_node **slot
30037 = debug_str_hash->find_slot_with_hash (s->str,
30038 htab_hash_string (s->str),
30039 INSERT);
30040 gcc_assert (*slot == NULL);
30041 *slot = s;
30046 /* Mark DIE and its children as removed. */
30048 static void
30049 mark_removed (dw_die_ref die)
30051 dw_die_ref c;
30052 die->removed = true;
30053 FOR_EACH_CHILD (die, c, mark_removed (c));
30056 /* Remove from the tree DIE any dies that aren't marked. */
30058 static void
30059 prune_unused_types_prune (dw_die_ref die)
30061 dw_die_ref c;
30063 gcc_assert (die->die_mark);
30064 prune_unused_types_update_strings (die);
30066 if (! die->die_child)
30067 return;
30069 c = die->die_child;
30070 do {
30071 dw_die_ref prev = c, next;
30072 for (c = c->die_sib; ! c->die_mark; c = next)
30073 if (c == die->die_child)
30075 /* No marked children between 'prev' and the end of the list. */
30076 if (prev == c)
30077 /* No marked children at all. */
30078 die->die_child = NULL;
30079 else
30081 prev->die_sib = c->die_sib;
30082 die->die_child = prev;
30084 c->die_sib = NULL;
30085 mark_removed (c);
30086 return;
30088 else
30090 next = c->die_sib;
30091 c->die_sib = NULL;
30092 mark_removed (c);
30095 if (c != prev->die_sib)
30096 prev->die_sib = c;
30097 prune_unused_types_prune (c);
30098 } while (c != die->die_child);
30101 /* Remove dies representing declarations that we never use. */
30103 static void
30104 prune_unused_types (void)
30106 unsigned int i;
30107 limbo_die_node *node;
30108 comdat_type_node *ctnode;
30109 pubname_entry *pub;
30110 dw_die_ref base_type;
30112 #if ENABLE_ASSERT_CHECKING
30113 /* All the marks should already be clear. */
30114 verify_marks_clear (comp_unit_die ());
30115 for (node = limbo_die_list; node; node = node->next)
30116 verify_marks_clear (node->die);
30117 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30118 verify_marks_clear (ctnode->root_die);
30119 #endif /* ENABLE_ASSERT_CHECKING */
30121 /* Mark types that are used in global variables. */
30122 premark_types_used_by_global_vars ();
30124 /* Mark variables used in the symtab. */
30125 if (flag_debug_only_used_symbols)
30126 premark_used_variables ();
30128 /* Set the mark on nodes that are actually used. */
30129 prune_unused_types_walk (comp_unit_die ());
30130 for (node = limbo_die_list; node; node = node->next)
30131 prune_unused_types_walk (node->die);
30132 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30134 prune_unused_types_walk (ctnode->root_die);
30135 prune_unused_types_mark (ctnode->type_die, 1);
30138 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30139 are unusual in that they are pubnames that are the children of pubtypes.
30140 They should only be marked via their parent DW_TAG_enumeration_type die,
30141 not as roots in themselves. */
30142 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30143 if (pub->die->die_tag != DW_TAG_enumerator)
30144 prune_unused_types_mark (pub->die, 1);
30145 for (i = 0; base_types.iterate (i, &base_type); i++)
30146 prune_unused_types_mark (base_type, 1);
30148 /* Also set the mark on nodes that could be referenced by
30149 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30150 by DW_TAG_inlined_subroutine origins. */
30151 cgraph_node *cnode;
30152 FOR_EACH_FUNCTION (cnode)
30153 if (cnode->referred_to_p (false))
30155 dw_die_ref die = lookup_decl_die (cnode->decl);
30156 if (die == NULL || die->die_mark)
30157 continue;
30158 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30159 if (e->caller != cnode)
30161 prune_unused_types_mark (die, 1);
30162 break;
30166 if (debug_str_hash)
30167 debug_str_hash->empty ();
30168 if (skeleton_debug_str_hash)
30169 skeleton_debug_str_hash->empty ();
30170 prune_unused_types_prune (comp_unit_die ());
30171 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30173 node = *pnode;
30174 if (!node->die->die_mark)
30175 *pnode = node->next;
30176 else
30178 prune_unused_types_prune (node->die);
30179 pnode = &node->next;
30182 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30183 prune_unused_types_prune (ctnode->root_die);
30185 /* Leave the marks clear. */
30186 prune_unmark_dies (comp_unit_die ());
30187 for (node = limbo_die_list; node; node = node->next)
30188 prune_unmark_dies (node->die);
30189 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30190 prune_unmark_dies (ctnode->root_die);
30193 /* Helpers to manipulate hash table of comdat type units. */
30195 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30197 static inline hashval_t hash (const comdat_type_node *);
30198 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30201 inline hashval_t
30202 comdat_type_hasher::hash (const comdat_type_node *type_node)
30204 hashval_t h;
30205 memcpy (&h, type_node->signature, sizeof (h));
30206 return h;
30209 inline bool
30210 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30211 const comdat_type_node *type_node_2)
30213 return (! memcmp (type_node_1->signature, type_node_2->signature,
30214 DWARF_TYPE_SIGNATURE_SIZE));
30217 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30218 to the location it would have been added, should we know its
30219 DECL_ASSEMBLER_NAME when we added other attributes. This will
30220 probably improve compactness of debug info, removing equivalent
30221 abbrevs, and hide any differences caused by deferring the
30222 computation of the assembler name, triggered by e.g. PCH. */
30224 static inline void
30225 move_linkage_attr (dw_die_ref die)
30227 unsigned ix = vec_safe_length (die->die_attr);
30228 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30230 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30231 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30233 while (--ix > 0)
30235 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30237 if (prev->dw_attr == DW_AT_decl_line
30238 || prev->dw_attr == DW_AT_decl_column
30239 || prev->dw_attr == DW_AT_name)
30240 break;
30243 if (ix != vec_safe_length (die->die_attr) - 1)
30245 die->die_attr->pop ();
30246 die->die_attr->quick_insert (ix, linkage);
30250 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30251 referenced from typed stack ops and count how often they are used. */
30253 static void
30254 mark_base_types (dw_loc_descr_ref loc)
30256 dw_die_ref base_type = NULL;
30258 for (; loc; loc = loc->dw_loc_next)
30260 switch (loc->dw_loc_opc)
30262 case DW_OP_regval_type:
30263 case DW_OP_deref_type:
30264 case DW_OP_GNU_regval_type:
30265 case DW_OP_GNU_deref_type:
30266 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30267 break;
30268 case DW_OP_convert:
30269 case DW_OP_reinterpret:
30270 case DW_OP_GNU_convert:
30271 case DW_OP_GNU_reinterpret:
30272 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30273 continue;
30274 /* FALLTHRU */
30275 case DW_OP_const_type:
30276 case DW_OP_GNU_const_type:
30277 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30278 break;
30279 case DW_OP_entry_value:
30280 case DW_OP_GNU_entry_value:
30281 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30282 continue;
30283 default:
30284 continue;
30286 gcc_assert (base_type->die_parent == comp_unit_die ());
30287 if (base_type->die_mark)
30288 base_type->die_mark++;
30289 else
30291 base_types.safe_push (base_type);
30292 base_type->die_mark = 1;
30297 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30298 referenced from typed stack ops and count how often they are used. */
30300 static void
30301 mark_base_types (dw_die_ref die)
30303 dw_die_ref c;
30304 dw_attr_node *a;
30305 dw_loc_list_ref *curr;
30306 unsigned ix;
30308 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30309 switch (AT_class (a))
30311 case dw_val_class_loc_list:
30312 curr = AT_loc_list_ptr (a);
30313 while (*curr)
30315 mark_base_types ((*curr)->expr);
30316 curr = &(*curr)->dw_loc_next;
30318 break;
30320 case dw_val_class_loc:
30321 mark_base_types (AT_loc (a));
30322 break;
30324 default:
30325 break;
30328 FOR_EACH_CHILD (die, c, mark_base_types (c));
30331 /* Comparison function for sorting marked base types. */
30333 static int
30334 base_type_cmp (const void *x, const void *y)
30336 dw_die_ref dx = *(const dw_die_ref *) x;
30337 dw_die_ref dy = *(const dw_die_ref *) y;
30338 unsigned int byte_size1, byte_size2;
30339 unsigned int encoding1, encoding2;
30340 unsigned int align1, align2;
30341 if (dx->die_mark > dy->die_mark)
30342 return -1;
30343 if (dx->die_mark < dy->die_mark)
30344 return 1;
30345 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30346 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30347 if (byte_size1 < byte_size2)
30348 return 1;
30349 if (byte_size1 > byte_size2)
30350 return -1;
30351 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30352 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30353 if (encoding1 < encoding2)
30354 return 1;
30355 if (encoding1 > encoding2)
30356 return -1;
30357 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30358 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30359 if (align1 < align2)
30360 return 1;
30361 if (align1 > align2)
30362 return -1;
30363 return 0;
30366 /* Move base types marked by mark_base_types as early as possible
30367 in the CU, sorted by decreasing usage count both to make the
30368 uleb128 references as small as possible and to make sure they
30369 will have die_offset already computed by calc_die_sizes when
30370 sizes of typed stack loc ops is computed. */
30372 static void
30373 move_marked_base_types (void)
30375 unsigned int i;
30376 dw_die_ref base_type, die, c;
30378 if (base_types.is_empty ())
30379 return;
30381 /* Sort by decreasing usage count, they will be added again in that
30382 order later on. */
30383 base_types.qsort (base_type_cmp);
30384 die = comp_unit_die ();
30385 c = die->die_child;
30388 dw_die_ref prev = c;
30389 c = c->die_sib;
30390 while (c->die_mark)
30392 remove_child_with_prev (c, prev);
30393 /* As base types got marked, there must be at least
30394 one node other than DW_TAG_base_type. */
30395 gcc_assert (die->die_child != NULL);
30396 c = prev->die_sib;
30399 while (c != die->die_child);
30400 gcc_assert (die->die_child);
30401 c = die->die_child;
30402 for (i = 0; base_types.iterate (i, &base_type); i++)
30404 base_type->die_mark = 0;
30405 base_type->die_sib = c->die_sib;
30406 c->die_sib = base_type;
30407 c = base_type;
30411 /* Helper function for resolve_addr, attempt to resolve
30412 one CONST_STRING, return true if successful. Similarly verify that
30413 SYMBOL_REFs refer to variables emitted in the current CU. */
30415 static bool
30416 resolve_one_addr (rtx *addr)
30418 rtx rtl = *addr;
30420 if (GET_CODE (rtl) == CONST_STRING)
30422 size_t len = strlen (XSTR (rtl, 0)) + 1;
30423 tree t = build_string (len, XSTR (rtl, 0));
30424 tree tlen = size_int (len - 1);
30425 TREE_TYPE (t)
30426 = build_array_type (char_type_node, build_index_type (tlen));
30427 rtl = lookup_constant_def (t);
30428 if (!rtl || !MEM_P (rtl))
30429 return false;
30430 rtl = XEXP (rtl, 0);
30431 if (GET_CODE (rtl) == SYMBOL_REF
30432 && SYMBOL_REF_DECL (rtl)
30433 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30434 return false;
30435 vec_safe_push (used_rtx_array, rtl);
30436 *addr = rtl;
30437 return true;
30440 if (GET_CODE (rtl) == SYMBOL_REF
30441 && SYMBOL_REF_DECL (rtl))
30443 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30445 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30446 return false;
30448 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30449 return false;
30452 if (GET_CODE (rtl) == CONST)
30454 subrtx_ptr_iterator::array_type array;
30455 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30456 if (!resolve_one_addr (*iter))
30457 return false;
30460 return true;
30463 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30464 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30465 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30467 static rtx
30468 string_cst_pool_decl (tree t)
30470 rtx rtl = output_constant_def (t, 1);
30471 unsigned char *array;
30472 dw_loc_descr_ref l;
30473 tree decl;
30474 size_t len;
30475 dw_die_ref ref;
30477 if (!rtl || !MEM_P (rtl))
30478 return NULL_RTX;
30479 rtl = XEXP (rtl, 0);
30480 if (GET_CODE (rtl) != SYMBOL_REF
30481 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30482 return NULL_RTX;
30484 decl = SYMBOL_REF_DECL (rtl);
30485 if (!lookup_decl_die (decl))
30487 len = TREE_STRING_LENGTH (t);
30488 vec_safe_push (used_rtx_array, rtl);
30489 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30490 array = ggc_vec_alloc<unsigned char> (len);
30491 memcpy (array, TREE_STRING_POINTER (t), len);
30492 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30493 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30494 l->dw_loc_oprnd2.v.val_vec.length = len;
30495 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30496 l->dw_loc_oprnd2.v.val_vec.array = array;
30497 add_AT_loc (ref, DW_AT_location, l);
30498 equate_decl_number_to_die (decl, ref);
30500 return rtl;
30503 /* Helper function of resolve_addr_in_expr. LOC is
30504 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30505 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30506 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30507 with DW_OP_implicit_pointer if possible
30508 and return true, if unsuccessful, return false. */
30510 static bool
30511 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30513 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30514 HOST_WIDE_INT offset = 0;
30515 dw_die_ref ref = NULL;
30516 tree decl;
30518 if (GET_CODE (rtl) == CONST
30519 && GET_CODE (XEXP (rtl, 0)) == PLUS
30520 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30522 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30523 rtl = XEXP (XEXP (rtl, 0), 0);
30525 if (GET_CODE (rtl) == CONST_STRING)
30527 size_t len = strlen (XSTR (rtl, 0)) + 1;
30528 tree t = build_string (len, XSTR (rtl, 0));
30529 tree tlen = size_int (len - 1);
30531 TREE_TYPE (t)
30532 = build_array_type (char_type_node, build_index_type (tlen));
30533 rtl = string_cst_pool_decl (t);
30534 if (!rtl)
30535 return false;
30537 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30539 decl = SYMBOL_REF_DECL (rtl);
30540 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30542 ref = lookup_decl_die (decl);
30543 if (ref && (get_AT (ref, DW_AT_location)
30544 || get_AT (ref, DW_AT_const_value)))
30546 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30547 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30548 loc->dw_loc_oprnd1.val_entry = NULL;
30549 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30550 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30551 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30552 loc->dw_loc_oprnd2.v.val_int = offset;
30553 return true;
30557 return false;
30560 /* Helper function for resolve_addr, handle one location
30561 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30562 the location list couldn't be resolved. */
30564 static bool
30565 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30567 dw_loc_descr_ref keep = NULL;
30568 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30569 switch (loc->dw_loc_opc)
30571 case DW_OP_addr:
30572 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30574 if ((prev == NULL
30575 || prev->dw_loc_opc == DW_OP_piece
30576 || prev->dw_loc_opc == DW_OP_bit_piece)
30577 && loc->dw_loc_next
30578 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30579 && (!dwarf_strict || dwarf_version >= 5)
30580 && optimize_one_addr_into_implicit_ptr (loc))
30581 break;
30582 return false;
30584 break;
30585 case DW_OP_GNU_addr_index:
30586 case DW_OP_addrx:
30587 case DW_OP_GNU_const_index:
30588 case DW_OP_constx:
30589 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30590 || loc->dw_loc_opc == DW_OP_addrx)
30591 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30592 || loc->dw_loc_opc == DW_OP_constx)
30593 && loc->dtprel))
30595 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30596 if (!resolve_one_addr (&rtl))
30597 return false;
30598 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30599 loc->dw_loc_oprnd1.val_entry
30600 = add_addr_table_entry (rtl, ate_kind_rtx);
30602 break;
30603 case DW_OP_const4u:
30604 case DW_OP_const8u:
30605 if (loc->dtprel
30606 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30607 return false;
30608 break;
30609 case DW_OP_plus_uconst:
30610 if (size_of_loc_descr (loc)
30611 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30613 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30615 dw_loc_descr_ref repl
30616 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30617 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30618 add_loc_descr (&repl, loc->dw_loc_next);
30619 *loc = *repl;
30621 break;
30622 case DW_OP_implicit_value:
30623 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30624 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30625 return false;
30626 break;
30627 case DW_OP_implicit_pointer:
30628 case DW_OP_GNU_implicit_pointer:
30629 case DW_OP_GNU_parameter_ref:
30630 case DW_OP_GNU_variable_value:
30631 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30633 dw_die_ref ref
30634 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30635 if (ref == NULL)
30636 return false;
30637 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30638 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30639 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30641 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30643 if (prev == NULL
30644 && loc->dw_loc_next == NULL
30645 && AT_class (a) == dw_val_class_loc)
30646 switch (a->dw_attr)
30648 /* Following attributes allow both exprloc and reference,
30649 so if the whole expression is DW_OP_GNU_variable_value
30650 alone we could transform it into reference. */
30651 case DW_AT_byte_size:
30652 case DW_AT_bit_size:
30653 case DW_AT_lower_bound:
30654 case DW_AT_upper_bound:
30655 case DW_AT_bit_stride:
30656 case DW_AT_count:
30657 case DW_AT_allocated:
30658 case DW_AT_associated:
30659 case DW_AT_byte_stride:
30660 a->dw_attr_val.val_class = dw_val_class_die_ref;
30661 a->dw_attr_val.val_entry = NULL;
30662 a->dw_attr_val.v.val_die_ref.die
30663 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30664 a->dw_attr_val.v.val_die_ref.external = 0;
30665 return true;
30666 default:
30667 break;
30669 if (dwarf_strict)
30670 return false;
30672 break;
30673 case DW_OP_const_type:
30674 case DW_OP_regval_type:
30675 case DW_OP_deref_type:
30676 case DW_OP_convert:
30677 case DW_OP_reinterpret:
30678 case DW_OP_GNU_const_type:
30679 case DW_OP_GNU_regval_type:
30680 case DW_OP_GNU_deref_type:
30681 case DW_OP_GNU_convert:
30682 case DW_OP_GNU_reinterpret:
30683 while (loc->dw_loc_next
30684 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30685 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30687 dw_die_ref base1, base2;
30688 unsigned enc1, enc2, size1, size2;
30689 if (loc->dw_loc_opc == DW_OP_regval_type
30690 || loc->dw_loc_opc == DW_OP_deref_type
30691 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30692 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30693 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30694 else if (loc->dw_loc_oprnd1.val_class
30695 == dw_val_class_unsigned_const)
30696 break;
30697 else
30698 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30699 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30700 == dw_val_class_unsigned_const)
30701 break;
30702 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30703 gcc_assert (base1->die_tag == DW_TAG_base_type
30704 && base2->die_tag == DW_TAG_base_type);
30705 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30706 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30707 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30708 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30709 if (size1 == size2
30710 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30711 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30712 && loc != keep)
30713 || enc1 == enc2))
30715 /* Optimize away next DW_OP_convert after
30716 adjusting LOC's base type die reference. */
30717 if (loc->dw_loc_opc == DW_OP_regval_type
30718 || loc->dw_loc_opc == DW_OP_deref_type
30719 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30720 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30721 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30722 else
30723 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30724 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30725 continue;
30727 /* Don't change integer DW_OP_convert after e.g. floating
30728 point typed stack entry. */
30729 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30730 keep = loc->dw_loc_next;
30731 break;
30733 break;
30734 default:
30735 break;
30737 return true;
30740 /* Helper function of resolve_addr. DIE had DW_AT_location of
30741 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30742 and DW_OP_addr couldn't be resolved. resolve_addr has already
30743 removed the DW_AT_location attribute. This function attempts to
30744 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30745 to it or DW_AT_const_value attribute, if possible. */
30747 static void
30748 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30750 if (!VAR_P (decl)
30751 || lookup_decl_die (decl) != die
30752 || DECL_EXTERNAL (decl)
30753 || !TREE_STATIC (decl)
30754 || DECL_INITIAL (decl) == NULL_TREE
30755 || DECL_P (DECL_INITIAL (decl))
30756 || get_AT (die, DW_AT_const_value))
30757 return;
30759 tree init = DECL_INITIAL (decl);
30760 HOST_WIDE_INT offset = 0;
30761 /* For variables that have been optimized away and thus
30762 don't have a memory location, see if we can emit
30763 DW_AT_const_value instead. */
30764 if (tree_add_const_value_attribute (die, init))
30765 return;
30766 if (dwarf_strict && dwarf_version < 5)
30767 return;
30768 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30769 and ADDR_EXPR refers to a decl that has DW_AT_location or
30770 DW_AT_const_value (but isn't addressable, otherwise
30771 resolving the original DW_OP_addr wouldn't fail), see if
30772 we can add DW_OP_implicit_pointer. */
30773 STRIP_NOPS (init);
30774 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30775 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30777 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30778 init = TREE_OPERAND (init, 0);
30779 STRIP_NOPS (init);
30781 if (TREE_CODE (init) != ADDR_EXPR)
30782 return;
30783 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30784 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30785 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30786 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30787 && TREE_OPERAND (init, 0) != decl))
30789 dw_die_ref ref;
30790 dw_loc_descr_ref l;
30792 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30794 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30795 if (!rtl)
30796 return;
30797 decl = SYMBOL_REF_DECL (rtl);
30799 else
30800 decl = TREE_OPERAND (init, 0);
30801 ref = lookup_decl_die (decl);
30802 if (ref == NULL
30803 || (!get_AT (ref, DW_AT_location)
30804 && !get_AT (ref, DW_AT_const_value)))
30805 return;
30806 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30807 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30808 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30809 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30810 add_AT_loc (die, DW_AT_location, l);
30814 /* Return NULL if l is a DWARF expression, or first op that is not
30815 valid DWARF expression. */
30817 static dw_loc_descr_ref
30818 non_dwarf_expression (dw_loc_descr_ref l)
30820 while (l)
30822 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30823 return l;
30824 switch (l->dw_loc_opc)
30826 case DW_OP_regx:
30827 case DW_OP_implicit_value:
30828 case DW_OP_stack_value:
30829 case DW_OP_implicit_pointer:
30830 case DW_OP_GNU_implicit_pointer:
30831 case DW_OP_GNU_parameter_ref:
30832 case DW_OP_piece:
30833 case DW_OP_bit_piece:
30834 return l;
30835 default:
30836 break;
30838 l = l->dw_loc_next;
30840 return NULL;
30843 /* Return adjusted copy of EXPR:
30844 If it is empty DWARF expression, return it.
30845 If it is valid non-empty DWARF expression,
30846 return copy of EXPR with DW_OP_deref appended to it.
30847 If it is DWARF expression followed by DW_OP_reg{N,x}, return
30848 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30849 If it is DWARF expression followed by DW_OP_stack_value, return
30850 copy of the DWARF expression without anything appended.
30851 Otherwise, return NULL. */
30853 static dw_loc_descr_ref
30854 copy_deref_exprloc (dw_loc_descr_ref expr)
30856 dw_loc_descr_ref tail = NULL;
30858 if (expr == NULL)
30859 return NULL;
30861 dw_loc_descr_ref l = non_dwarf_expression (expr);
30862 if (l && l->dw_loc_next)
30863 return NULL;
30865 if (l)
30867 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30868 tail = new_loc_descr ((enum dwarf_location_atom)
30869 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30870 0, 0);
30871 else
30872 switch (l->dw_loc_opc)
30874 case DW_OP_regx:
30875 tail = new_loc_descr (DW_OP_bregx,
30876 l->dw_loc_oprnd1.v.val_unsigned, 0);
30877 break;
30878 case DW_OP_stack_value:
30879 break;
30880 default:
30881 return NULL;
30884 else
30885 tail = new_loc_descr (DW_OP_deref, 0, 0);
30887 dw_loc_descr_ref ret = NULL, *p = &ret;
30888 while (expr != l)
30890 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30891 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30892 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30893 p = &(*p)->dw_loc_next;
30894 expr = expr->dw_loc_next;
30896 *p = tail;
30897 return ret;
30900 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30901 reference to a variable or argument, adjust it if needed and return:
30902 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30903 attribute if present should be removed
30904 0 keep the attribute perhaps with minor modifications, no need to rescan
30905 1 if the attribute has been successfully adjusted. */
30907 static int
30908 optimize_string_length (dw_attr_node *a)
30910 dw_loc_descr_ref l = AT_loc (a), lv;
30911 dw_die_ref die;
30912 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30914 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30915 die = lookup_decl_die (decl);
30916 if (die)
30918 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30919 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30920 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30922 else
30923 return -1;
30925 else
30926 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30928 /* DWARF5 allows reference class, so we can then reference the DIE.
30929 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30930 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30932 a->dw_attr_val.val_class = dw_val_class_die_ref;
30933 a->dw_attr_val.val_entry = NULL;
30934 a->dw_attr_val.v.val_die_ref.die = die;
30935 a->dw_attr_val.v.val_die_ref.external = 0;
30936 return 0;
30939 dw_attr_node *av = get_AT (die, DW_AT_location);
30940 dw_loc_list_ref d;
30941 bool non_dwarf_expr = false;
30943 if (av == NULL)
30944 return dwarf_strict ? -1 : 0;
30945 switch (AT_class (av))
30947 case dw_val_class_loc_list:
30948 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30949 if (d->expr && non_dwarf_expression (d->expr))
30950 non_dwarf_expr = true;
30951 break;
30952 case dw_val_class_view_list:
30953 gcc_unreachable ();
30954 case dw_val_class_loc:
30955 lv = AT_loc (av);
30956 if (lv == NULL)
30957 return dwarf_strict ? -1 : 0;
30958 if (non_dwarf_expression (lv))
30959 non_dwarf_expr = true;
30960 break;
30961 default:
30962 return dwarf_strict ? -1 : 0;
30965 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30966 into DW_OP_call4 or DW_OP_GNU_variable_value into
30967 DW_OP_call4 DW_OP_deref, do so. */
30968 if (!non_dwarf_expr
30969 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30971 l->dw_loc_opc = DW_OP_call4;
30972 if (l->dw_loc_next)
30973 l->dw_loc_next = NULL;
30974 else
30975 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30976 return 0;
30979 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30980 copy over the DW_AT_location attribute from die to a. */
30981 if (l->dw_loc_next != NULL)
30983 a->dw_attr_val = av->dw_attr_val;
30984 return 1;
30987 dw_loc_list_ref list, *p;
30988 switch (AT_class (av))
30990 case dw_val_class_loc_list:
30991 p = &list;
30992 list = NULL;
30993 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30995 lv = copy_deref_exprloc (d->expr);
30996 if (lv)
30998 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
30999 p = &(*p)->dw_loc_next;
31001 else if (!dwarf_strict && d->expr)
31002 return 0;
31004 if (list == NULL)
31005 return dwarf_strict ? -1 : 0;
31006 a->dw_attr_val.val_class = dw_val_class_loc_list;
31007 gen_llsym (list);
31008 *AT_loc_list_ptr (a) = list;
31009 return 1;
31010 case dw_val_class_loc:
31011 lv = copy_deref_exprloc (AT_loc (av));
31012 if (lv == NULL)
31013 return dwarf_strict ? -1 : 0;
31014 a->dw_attr_val.v.val_loc = lv;
31015 return 1;
31016 default:
31017 gcc_unreachable ();
31021 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31022 an address in .rodata section if the string literal is emitted there,
31023 or remove the containing location list or replace DW_AT_const_value
31024 with DW_AT_location and empty location expression, if it isn't found
31025 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31026 to something that has been emitted in the current CU. */
31028 static void
31029 resolve_addr (dw_die_ref die)
31031 dw_die_ref c;
31032 dw_attr_node *a;
31033 dw_loc_list_ref *curr, *start, loc;
31034 unsigned ix;
31035 bool remove_AT_byte_size = false;
31037 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31038 switch (AT_class (a))
31040 case dw_val_class_loc_list:
31041 start = curr = AT_loc_list_ptr (a);
31042 loc = *curr;
31043 gcc_assert (loc);
31044 /* The same list can be referenced more than once. See if we have
31045 already recorded the result from a previous pass. */
31046 if (loc->replaced)
31047 *curr = loc->dw_loc_next;
31048 else if (!loc->resolved_addr)
31050 /* As things stand, we do not expect or allow one die to
31051 reference a suffix of another die's location list chain.
31052 References must be identical or completely separate.
31053 There is therefore no need to cache the result of this
31054 pass on any list other than the first; doing so
31055 would lead to unnecessary writes. */
31056 while (*curr)
31058 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31059 if (!resolve_addr_in_expr (a, (*curr)->expr))
31061 dw_loc_list_ref next = (*curr)->dw_loc_next;
31062 dw_loc_descr_ref l = (*curr)->expr;
31064 if (next && (*curr)->ll_symbol)
31066 gcc_assert (!next->ll_symbol);
31067 next->ll_symbol = (*curr)->ll_symbol;
31068 next->vl_symbol = (*curr)->vl_symbol;
31070 if (dwarf_split_debug_info)
31071 remove_loc_list_addr_table_entries (l);
31072 *curr = next;
31074 else
31076 mark_base_types ((*curr)->expr);
31077 curr = &(*curr)->dw_loc_next;
31080 if (loc == *start)
31081 loc->resolved_addr = 1;
31082 else
31084 loc->replaced = 1;
31085 loc->dw_loc_next = *start;
31088 if (!*start)
31090 remove_AT (die, a->dw_attr);
31091 ix--;
31093 break;
31094 case dw_val_class_view_list:
31096 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31097 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31098 dw_val_node *llnode
31099 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31100 /* If we no longer have a loclist, or it no longer needs
31101 views, drop this attribute. */
31102 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31104 remove_AT (die, a->dw_attr);
31105 ix--;
31107 break;
31109 case dw_val_class_loc:
31111 dw_loc_descr_ref l = AT_loc (a);
31112 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31113 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31114 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31115 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31116 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31117 with DW_FORM_ref referencing the same DIE as
31118 DW_OP_GNU_variable_value used to reference. */
31119 if (a->dw_attr == DW_AT_string_length
31120 && l
31121 && l->dw_loc_opc == DW_OP_GNU_variable_value
31122 && (l->dw_loc_next == NULL
31123 || (l->dw_loc_next->dw_loc_next == NULL
31124 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31126 switch (optimize_string_length (a))
31128 case -1:
31129 remove_AT (die, a->dw_attr);
31130 ix--;
31131 /* If we drop DW_AT_string_length, we need to drop also
31132 DW_AT_{string_length_,}byte_size. */
31133 remove_AT_byte_size = true;
31134 continue;
31135 default:
31136 break;
31137 case 1:
31138 /* Even if we keep the optimized DW_AT_string_length,
31139 it might have changed AT_class, so process it again. */
31140 ix--;
31141 continue;
31144 /* For -gdwarf-2 don't attempt to optimize
31145 DW_AT_data_member_location containing
31146 DW_OP_plus_uconst - older consumers might
31147 rely on it being that op instead of a more complex,
31148 but shorter, location description. */
31149 if ((dwarf_version > 2
31150 || a->dw_attr != DW_AT_data_member_location
31151 || l == NULL
31152 || l->dw_loc_opc != DW_OP_plus_uconst
31153 || l->dw_loc_next != NULL)
31154 && !resolve_addr_in_expr (a, l))
31156 if (dwarf_split_debug_info)
31157 remove_loc_list_addr_table_entries (l);
31158 if (l != NULL
31159 && l->dw_loc_next == NULL
31160 && l->dw_loc_opc == DW_OP_addr
31161 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31162 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31163 && a->dw_attr == DW_AT_location)
31165 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31166 remove_AT (die, a->dw_attr);
31167 ix--;
31168 optimize_location_into_implicit_ptr (die, decl);
31169 break;
31171 if (a->dw_attr == DW_AT_string_length)
31172 /* If we drop DW_AT_string_length, we need to drop also
31173 DW_AT_{string_length_,}byte_size. */
31174 remove_AT_byte_size = true;
31175 remove_AT (die, a->dw_attr);
31176 ix--;
31178 else
31179 mark_base_types (l);
31181 break;
31182 case dw_val_class_addr:
31183 if (a->dw_attr == DW_AT_const_value
31184 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31186 if (AT_index (a) != NOT_INDEXED)
31187 remove_addr_table_entry (a->dw_attr_val.val_entry);
31188 remove_AT (die, a->dw_attr);
31189 ix--;
31191 if ((die->die_tag == DW_TAG_call_site
31192 && a->dw_attr == DW_AT_call_origin)
31193 || (die->die_tag == DW_TAG_GNU_call_site
31194 && a->dw_attr == DW_AT_abstract_origin))
31196 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31197 dw_die_ref tdie = lookup_decl_die (tdecl);
31198 dw_die_ref cdie;
31199 if (tdie == NULL
31200 && DECL_EXTERNAL (tdecl)
31201 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31202 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31204 dw_die_ref pdie = cdie;
31205 /* Make sure we don't add these DIEs into type units.
31206 We could emit skeleton DIEs for context (namespaces,
31207 outer structs/classes) and a skeleton DIE for the
31208 innermost context with DW_AT_signature pointing to the
31209 type unit. See PR78835. */
31210 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31211 pdie = pdie->die_parent;
31212 if (pdie == NULL)
31214 /* Creating a full DIE for tdecl is overly expensive and
31215 at this point even wrong when in the LTO phase
31216 as it can end up generating new type DIEs we didn't
31217 output and thus optimize_external_refs will crash. */
31218 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31219 add_AT_flag (tdie, DW_AT_external, 1);
31220 add_AT_flag (tdie, DW_AT_declaration, 1);
31221 add_linkage_attr (tdie, tdecl);
31222 add_name_and_src_coords_attributes (tdie, tdecl, true);
31223 equate_decl_number_to_die (tdecl, tdie);
31226 if (tdie)
31228 a->dw_attr_val.val_class = dw_val_class_die_ref;
31229 a->dw_attr_val.v.val_die_ref.die = tdie;
31230 a->dw_attr_val.v.val_die_ref.external = 0;
31232 else
31234 if (AT_index (a) != NOT_INDEXED)
31235 remove_addr_table_entry (a->dw_attr_val.val_entry);
31236 remove_AT (die, a->dw_attr);
31237 ix--;
31240 break;
31241 default:
31242 break;
31245 if (remove_AT_byte_size)
31246 remove_AT (die, dwarf_version >= 5
31247 ? DW_AT_string_length_byte_size
31248 : DW_AT_byte_size);
31250 FOR_EACH_CHILD (die, c, resolve_addr (c));
31253 /* Helper routines for optimize_location_lists.
31254 This pass tries to share identical local lists in .debug_loc
31255 section. */
31257 /* Iteratively hash operands of LOC opcode into HSTATE. */
31259 static void
31260 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31262 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31263 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31265 switch (loc->dw_loc_opc)
31267 case DW_OP_const4u:
31268 case DW_OP_const8u:
31269 if (loc->dtprel)
31270 goto hash_addr;
31271 /* FALLTHRU */
31272 case DW_OP_const1u:
31273 case DW_OP_const1s:
31274 case DW_OP_const2u:
31275 case DW_OP_const2s:
31276 case DW_OP_const4s:
31277 case DW_OP_const8s:
31278 case DW_OP_constu:
31279 case DW_OP_consts:
31280 case DW_OP_pick:
31281 case DW_OP_plus_uconst:
31282 case DW_OP_breg0:
31283 case DW_OP_breg1:
31284 case DW_OP_breg2:
31285 case DW_OP_breg3:
31286 case DW_OP_breg4:
31287 case DW_OP_breg5:
31288 case DW_OP_breg6:
31289 case DW_OP_breg7:
31290 case DW_OP_breg8:
31291 case DW_OP_breg9:
31292 case DW_OP_breg10:
31293 case DW_OP_breg11:
31294 case DW_OP_breg12:
31295 case DW_OP_breg13:
31296 case DW_OP_breg14:
31297 case DW_OP_breg15:
31298 case DW_OP_breg16:
31299 case DW_OP_breg17:
31300 case DW_OP_breg18:
31301 case DW_OP_breg19:
31302 case DW_OP_breg20:
31303 case DW_OP_breg21:
31304 case DW_OP_breg22:
31305 case DW_OP_breg23:
31306 case DW_OP_breg24:
31307 case DW_OP_breg25:
31308 case DW_OP_breg26:
31309 case DW_OP_breg27:
31310 case DW_OP_breg28:
31311 case DW_OP_breg29:
31312 case DW_OP_breg30:
31313 case DW_OP_breg31:
31314 case DW_OP_regx:
31315 case DW_OP_fbreg:
31316 case DW_OP_piece:
31317 case DW_OP_deref_size:
31318 case DW_OP_xderef_size:
31319 hstate.add_object (val1->v.val_int);
31320 break;
31321 case DW_OP_skip:
31322 case DW_OP_bra:
31324 int offset;
31326 gcc_assert (val1->val_class == dw_val_class_loc);
31327 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31328 hstate.add_object (offset);
31330 break;
31331 case DW_OP_implicit_value:
31332 hstate.add_object (val1->v.val_unsigned);
31333 switch (val2->val_class)
31335 case dw_val_class_const:
31336 hstate.add_object (val2->v.val_int);
31337 break;
31338 case dw_val_class_vec:
31340 unsigned int elt_size = val2->v.val_vec.elt_size;
31341 unsigned int len = val2->v.val_vec.length;
31343 hstate.add_int (elt_size);
31344 hstate.add_int (len);
31345 hstate.add (val2->v.val_vec.array, len * elt_size);
31347 break;
31348 case dw_val_class_const_double:
31349 hstate.add_object (val2->v.val_double.low);
31350 hstate.add_object (val2->v.val_double.high);
31351 break;
31352 case dw_val_class_wide_int:
31353 hstate.add (val2->v.val_wide->get_val (),
31354 get_full_len (*val2->v.val_wide)
31355 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31356 break;
31357 case dw_val_class_addr:
31358 inchash::add_rtx (val2->v.val_addr, hstate);
31359 break;
31360 default:
31361 gcc_unreachable ();
31363 break;
31364 case DW_OP_bregx:
31365 case DW_OP_bit_piece:
31366 hstate.add_object (val1->v.val_int);
31367 hstate.add_object (val2->v.val_int);
31368 break;
31369 case DW_OP_addr:
31370 hash_addr:
31371 if (loc->dtprel)
31373 unsigned char dtprel = 0xd1;
31374 hstate.add_object (dtprel);
31376 inchash::add_rtx (val1->v.val_addr, hstate);
31377 break;
31378 case DW_OP_GNU_addr_index:
31379 case DW_OP_addrx:
31380 case DW_OP_GNU_const_index:
31381 case DW_OP_constx:
31383 if (loc->dtprel)
31385 unsigned char dtprel = 0xd1;
31386 hstate.add_object (dtprel);
31388 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31390 break;
31391 case DW_OP_implicit_pointer:
31392 case DW_OP_GNU_implicit_pointer:
31393 hstate.add_int (val2->v.val_int);
31394 break;
31395 case DW_OP_entry_value:
31396 case DW_OP_GNU_entry_value:
31397 hstate.add_object (val1->v.val_loc);
31398 break;
31399 case DW_OP_regval_type:
31400 case DW_OP_deref_type:
31401 case DW_OP_GNU_regval_type:
31402 case DW_OP_GNU_deref_type:
31404 unsigned int byte_size
31405 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31406 unsigned int encoding
31407 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31408 hstate.add_object (val1->v.val_int);
31409 hstate.add_object (byte_size);
31410 hstate.add_object (encoding);
31412 break;
31413 case DW_OP_convert:
31414 case DW_OP_reinterpret:
31415 case DW_OP_GNU_convert:
31416 case DW_OP_GNU_reinterpret:
31417 if (val1->val_class == dw_val_class_unsigned_const)
31419 hstate.add_object (val1->v.val_unsigned);
31420 break;
31422 /* FALLTHRU */
31423 case DW_OP_const_type:
31424 case DW_OP_GNU_const_type:
31426 unsigned int byte_size
31427 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31428 unsigned int encoding
31429 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31430 hstate.add_object (byte_size);
31431 hstate.add_object (encoding);
31432 if (loc->dw_loc_opc != DW_OP_const_type
31433 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31434 break;
31435 hstate.add_object (val2->val_class);
31436 switch (val2->val_class)
31438 case dw_val_class_const:
31439 hstate.add_object (val2->v.val_int);
31440 break;
31441 case dw_val_class_vec:
31443 unsigned int elt_size = val2->v.val_vec.elt_size;
31444 unsigned int len = val2->v.val_vec.length;
31446 hstate.add_object (elt_size);
31447 hstate.add_object (len);
31448 hstate.add (val2->v.val_vec.array, len * elt_size);
31450 break;
31451 case dw_val_class_const_double:
31452 hstate.add_object (val2->v.val_double.low);
31453 hstate.add_object (val2->v.val_double.high);
31454 break;
31455 case dw_val_class_wide_int:
31456 hstate.add (val2->v.val_wide->get_val (),
31457 get_full_len (*val2->v.val_wide)
31458 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31459 break;
31460 default:
31461 gcc_unreachable ();
31464 break;
31466 default:
31467 /* Other codes have no operands. */
31468 break;
31472 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31474 static inline void
31475 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31477 dw_loc_descr_ref l;
31478 bool sizes_computed = false;
31479 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31480 size_of_locs (loc);
31482 for (l = loc; l != NULL; l = l->dw_loc_next)
31484 enum dwarf_location_atom opc = l->dw_loc_opc;
31485 hstate.add_object (opc);
31486 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31488 size_of_locs (loc);
31489 sizes_computed = true;
31491 hash_loc_operands (l, hstate);
31495 /* Compute hash of the whole location list LIST_HEAD. */
31497 static inline void
31498 hash_loc_list (dw_loc_list_ref list_head)
31500 dw_loc_list_ref curr = list_head;
31501 inchash::hash hstate;
31503 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31505 hstate.add (curr->begin, strlen (curr->begin) + 1);
31506 hstate.add (curr->end, strlen (curr->end) + 1);
31507 hstate.add_object (curr->vbegin);
31508 hstate.add_object (curr->vend);
31509 if (curr->section)
31510 hstate.add (curr->section, strlen (curr->section) + 1);
31511 hash_locs (curr->expr, hstate);
31513 list_head->hash = hstate.end ();
31516 /* Return true if X and Y opcodes have the same operands. */
31518 static inline bool
31519 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31521 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31522 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31523 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31524 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31526 switch (x->dw_loc_opc)
31528 case DW_OP_const4u:
31529 case DW_OP_const8u:
31530 if (x->dtprel)
31531 goto hash_addr;
31532 /* FALLTHRU */
31533 case DW_OP_const1u:
31534 case DW_OP_const1s:
31535 case DW_OP_const2u:
31536 case DW_OP_const2s:
31537 case DW_OP_const4s:
31538 case DW_OP_const8s:
31539 case DW_OP_constu:
31540 case DW_OP_consts:
31541 case DW_OP_pick:
31542 case DW_OP_plus_uconst:
31543 case DW_OP_breg0:
31544 case DW_OP_breg1:
31545 case DW_OP_breg2:
31546 case DW_OP_breg3:
31547 case DW_OP_breg4:
31548 case DW_OP_breg5:
31549 case DW_OP_breg6:
31550 case DW_OP_breg7:
31551 case DW_OP_breg8:
31552 case DW_OP_breg9:
31553 case DW_OP_breg10:
31554 case DW_OP_breg11:
31555 case DW_OP_breg12:
31556 case DW_OP_breg13:
31557 case DW_OP_breg14:
31558 case DW_OP_breg15:
31559 case DW_OP_breg16:
31560 case DW_OP_breg17:
31561 case DW_OP_breg18:
31562 case DW_OP_breg19:
31563 case DW_OP_breg20:
31564 case DW_OP_breg21:
31565 case DW_OP_breg22:
31566 case DW_OP_breg23:
31567 case DW_OP_breg24:
31568 case DW_OP_breg25:
31569 case DW_OP_breg26:
31570 case DW_OP_breg27:
31571 case DW_OP_breg28:
31572 case DW_OP_breg29:
31573 case DW_OP_breg30:
31574 case DW_OP_breg31:
31575 case DW_OP_regx:
31576 case DW_OP_fbreg:
31577 case DW_OP_piece:
31578 case DW_OP_deref_size:
31579 case DW_OP_xderef_size:
31580 return valx1->v.val_int == valy1->v.val_int;
31581 case DW_OP_skip:
31582 case DW_OP_bra:
31583 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31584 can cause irrelevant differences in dw_loc_addr. */
31585 gcc_assert (valx1->val_class == dw_val_class_loc
31586 && valy1->val_class == dw_val_class_loc
31587 && (dwarf_split_debug_info
31588 || x->dw_loc_addr == y->dw_loc_addr));
31589 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31590 case DW_OP_implicit_value:
31591 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31592 || valx2->val_class != valy2->val_class)
31593 return false;
31594 switch (valx2->val_class)
31596 case dw_val_class_const:
31597 return valx2->v.val_int == valy2->v.val_int;
31598 case dw_val_class_vec:
31599 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31600 && valx2->v.val_vec.length == valy2->v.val_vec.length
31601 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31602 valx2->v.val_vec.elt_size
31603 * valx2->v.val_vec.length) == 0;
31604 case dw_val_class_const_double:
31605 return valx2->v.val_double.low == valy2->v.val_double.low
31606 && valx2->v.val_double.high == valy2->v.val_double.high;
31607 case dw_val_class_wide_int:
31608 return *valx2->v.val_wide == *valy2->v.val_wide;
31609 case dw_val_class_addr:
31610 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31611 default:
31612 gcc_unreachable ();
31614 case DW_OP_bregx:
31615 case DW_OP_bit_piece:
31616 return valx1->v.val_int == valy1->v.val_int
31617 && valx2->v.val_int == valy2->v.val_int;
31618 case DW_OP_addr:
31619 hash_addr:
31620 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31621 case DW_OP_GNU_addr_index:
31622 case DW_OP_addrx:
31623 case DW_OP_GNU_const_index:
31624 case DW_OP_constx:
31626 rtx ax1 = valx1->val_entry->addr.rtl;
31627 rtx ay1 = valy1->val_entry->addr.rtl;
31628 return rtx_equal_p (ax1, ay1);
31630 case DW_OP_implicit_pointer:
31631 case DW_OP_GNU_implicit_pointer:
31632 return valx1->val_class == dw_val_class_die_ref
31633 && valx1->val_class == valy1->val_class
31634 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31635 && valx2->v.val_int == valy2->v.val_int;
31636 case DW_OP_entry_value:
31637 case DW_OP_GNU_entry_value:
31638 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31639 case DW_OP_const_type:
31640 case DW_OP_GNU_const_type:
31641 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31642 || valx2->val_class != valy2->val_class)
31643 return false;
31644 switch (valx2->val_class)
31646 case dw_val_class_const:
31647 return valx2->v.val_int == valy2->v.val_int;
31648 case dw_val_class_vec:
31649 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31650 && valx2->v.val_vec.length == valy2->v.val_vec.length
31651 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31652 valx2->v.val_vec.elt_size
31653 * valx2->v.val_vec.length) == 0;
31654 case dw_val_class_const_double:
31655 return valx2->v.val_double.low == valy2->v.val_double.low
31656 && valx2->v.val_double.high == valy2->v.val_double.high;
31657 case dw_val_class_wide_int:
31658 return *valx2->v.val_wide == *valy2->v.val_wide;
31659 default:
31660 gcc_unreachable ();
31662 case DW_OP_regval_type:
31663 case DW_OP_deref_type:
31664 case DW_OP_GNU_regval_type:
31665 case DW_OP_GNU_deref_type:
31666 return valx1->v.val_int == valy1->v.val_int
31667 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31668 case DW_OP_convert:
31669 case DW_OP_reinterpret:
31670 case DW_OP_GNU_convert:
31671 case DW_OP_GNU_reinterpret:
31672 if (valx1->val_class != valy1->val_class)
31673 return false;
31674 if (valx1->val_class == dw_val_class_unsigned_const)
31675 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31676 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31677 case DW_OP_GNU_parameter_ref:
31678 return valx1->val_class == dw_val_class_die_ref
31679 && valx1->val_class == valy1->val_class
31680 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31681 default:
31682 /* Other codes have no operands. */
31683 return true;
31687 /* Return true if DWARF location expressions X and Y are the same. */
31689 static inline bool
31690 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31692 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31693 if (x->dw_loc_opc != y->dw_loc_opc
31694 || x->dtprel != y->dtprel
31695 || !compare_loc_operands (x, y))
31696 break;
31697 return x == NULL && y == NULL;
31700 /* Hashtable helpers. */
31702 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31704 static inline hashval_t hash (const dw_loc_list_struct *);
31705 static inline bool equal (const dw_loc_list_struct *,
31706 const dw_loc_list_struct *);
31709 /* Return precomputed hash of location list X. */
31711 inline hashval_t
31712 loc_list_hasher::hash (const dw_loc_list_struct *x)
31714 return x->hash;
31717 /* Return true if location lists A and B are the same. */
31719 inline bool
31720 loc_list_hasher::equal (const dw_loc_list_struct *a,
31721 const dw_loc_list_struct *b)
31723 if (a == b)
31724 return 1;
31725 if (a->hash != b->hash)
31726 return 0;
31727 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31728 if (strcmp (a->begin, b->begin) != 0
31729 || strcmp (a->end, b->end) != 0
31730 || (a->section == NULL) != (b->section == NULL)
31731 || (a->section && strcmp (a->section, b->section) != 0)
31732 || a->vbegin != b->vbegin || a->vend != b->vend
31733 || !compare_locs (a->expr, b->expr))
31734 break;
31735 return a == NULL && b == NULL;
31738 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31741 /* Recursively optimize location lists referenced from DIE
31742 children and share them whenever possible. */
31744 static void
31745 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31747 dw_die_ref c;
31748 dw_attr_node *a;
31749 unsigned ix;
31750 dw_loc_list_struct **slot;
31751 bool drop_locviews = false;
31752 bool has_locviews = false;
31754 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31755 if (AT_class (a) == dw_val_class_loc_list)
31757 dw_loc_list_ref list = AT_loc_list (a);
31758 /* TODO: perform some optimizations here, before hashing
31759 it and storing into the hash table. */
31760 hash_loc_list (list);
31761 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31762 if (*slot == NULL)
31764 *slot = list;
31765 if (loc_list_has_views (list))
31766 gcc_assert (list->vl_symbol);
31767 else if (list->vl_symbol)
31769 drop_locviews = true;
31770 list->vl_symbol = NULL;
31773 else
31775 if (list->vl_symbol && !(*slot)->vl_symbol)
31776 drop_locviews = true;
31777 a->dw_attr_val.v.val_loc_list = *slot;
31780 else if (AT_class (a) == dw_val_class_view_list)
31782 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31783 has_locviews = true;
31787 if (drop_locviews && has_locviews)
31788 remove_AT (die, DW_AT_GNU_locviews);
31790 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31794 /* Recursively assign each location list a unique index into the debug_addr
31795 section. */
31797 static void
31798 index_location_lists (dw_die_ref die)
31800 dw_die_ref c;
31801 dw_attr_node *a;
31802 unsigned ix;
31804 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31805 if (AT_class (a) == dw_val_class_loc_list)
31807 dw_loc_list_ref list = AT_loc_list (a);
31808 dw_loc_list_ref curr;
31809 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31811 /* Don't index an entry that has already been indexed
31812 or won't be output. Make sure skip_loc_list_entry doesn't
31813 call size_of_locs, because that might cause circular dependency,
31814 index_location_lists requiring address table indexes to be
31815 computed, but adding new indexes through add_addr_table_entry
31816 and address table index computation requiring no new additions
31817 to the hash table. In the rare case of DWARF[234] >= 64KB
31818 location expression, we'll just waste unused address table entry
31819 for it. */
31820 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
31821 continue;
31823 curr->begin_entry
31824 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31825 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
31826 curr->end_entry
31827 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
31831 FOR_EACH_CHILD (die, c, index_location_lists (c));
31834 /* Optimize location lists referenced from DIE
31835 children and share them whenever possible. */
31837 static void
31838 optimize_location_lists (dw_die_ref die)
31840 loc_list_hash_type htab (500);
31841 optimize_location_lists_1 (die, &htab);
31844 /* Traverse the limbo die list, and add parent/child links. The only
31845 dies without parents that should be here are concrete instances of
31846 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
31847 For concrete instances, we can get the parent die from the abstract
31848 instance. */
31850 static void
31851 flush_limbo_die_list (void)
31853 limbo_die_node *node;
31855 /* get_context_die calls force_decl_die, which can put new DIEs on the
31856 limbo list in LTO mode when nested functions are put in a different
31857 partition than that of their parent function. */
31858 while ((node = limbo_die_list))
31860 dw_die_ref die = node->die;
31861 limbo_die_list = node->next;
31863 if (die->die_parent == NULL)
31865 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31867 if (origin && origin->die_parent)
31868 add_child_die (origin->die_parent, die);
31869 else if (is_cu_die (die))
31871 else if (seen_error ())
31872 /* It's OK to be confused by errors in the input. */
31873 add_child_die (comp_unit_die (), die);
31874 else
31876 /* In certain situations, the lexical block containing a
31877 nested function can be optimized away, which results
31878 in the nested function die being orphaned. Likewise
31879 with the return type of that nested function. Force
31880 this to be a child of the containing function.
31882 It may happen that even the containing function got fully
31883 inlined and optimized out. In that case we are lost and
31884 assign the empty child. This should not be big issue as
31885 the function is likely unreachable too. */
31886 gcc_assert (node->created_for);
31888 if (DECL_P (node->created_for))
31889 origin = get_context_die (DECL_CONTEXT (node->created_for));
31890 else if (TYPE_P (node->created_for))
31891 origin = scope_die_for (node->created_for, comp_unit_die ());
31892 else
31893 origin = comp_unit_die ();
31895 add_child_die (origin, die);
31901 /* Reset DIEs so we can output them again. */
31903 static void
31904 reset_dies (dw_die_ref die)
31906 dw_die_ref c;
31908 /* Remove stuff we re-generate. */
31909 die->die_mark = 0;
31910 die->die_offset = 0;
31911 die->die_abbrev = 0;
31912 remove_AT (die, DW_AT_sibling);
31914 FOR_EACH_CHILD (die, c, reset_dies (c));
31917 /* reset_indirect_string removed the references coming from DW_AT_name
31918 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
31919 .debug_line_str strings again. */
31921 static void
31922 adjust_name_comp_dir (dw_die_ref die)
31924 for (int i = 0; i < 2; i++)
31926 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
31927 dw_attr_node *a = get_AT (die, attr_kind);
31928 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
31929 continue;
31931 if (!debug_line_str_hash)
31932 debug_line_str_hash
31933 = hash_table<indirect_string_hasher>::create_ggc (10);
31935 struct indirect_string_node *node
31936 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
31937 debug_line_str_hash);
31938 set_indirect_string (node);
31939 node->form = DW_FORM_line_strp;
31940 a->dw_attr_val.v.val_str = node;
31944 /* Output stuff that dwarf requires at the end of every file,
31945 and generate the DWARF-2 debugging info. */
31947 static void
31948 dwarf2out_finish (const char *filename)
31950 comdat_type_node *ctnode;
31951 dw_die_ref main_comp_unit_die;
31952 unsigned char checksum[16];
31953 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31955 /* Generate CTF/BTF debug info. */
31956 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
31957 || btf_debuginfo_p ()) && lang_GNU_C ())
31958 ctf_debug_finish (filename);
31960 /* Skip emitting DWARF if not required. */
31961 if (!dwarf_debuginfo_p ())
31962 return;
31964 /* Flush out any latecomers to the limbo party. */
31965 flush_limbo_die_list ();
31967 if (inline_entry_data_table)
31968 gcc_assert (inline_entry_data_table->is_empty ());
31970 if (flag_checking)
31972 verify_die (comp_unit_die ());
31973 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31974 verify_die (node->die);
31977 /* We shouldn't have any symbols with delayed asm names for
31978 DIEs generated after early finish. */
31979 gcc_assert (deferred_asm_name == NULL);
31981 gen_remaining_tmpl_value_param_die_attribute ();
31983 if (flag_generate_lto || flag_generate_offload)
31985 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31987 /* Prune stuff so that dwarf2out_finish runs successfully
31988 for the fat part of the object. */
31989 reset_dies (comp_unit_die ());
31990 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31991 reset_dies (node->die);
31993 hash_table<comdat_type_hasher> comdat_type_table (100);
31994 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31996 comdat_type_node **slot
31997 = comdat_type_table.find_slot (ctnode, INSERT);
31999 /* Don't reset types twice. */
32000 if (*slot != HTAB_EMPTY_ENTRY)
32001 continue;
32003 /* Remove the pointer to the line table. */
32004 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32006 if (debug_info_level >= DINFO_LEVEL_TERSE)
32007 reset_dies (ctnode->root_die);
32009 *slot = ctnode;
32012 /* Reset die CU symbol so we don't output it twice. */
32013 comp_unit_die ()->die_id.die_symbol = NULL;
32015 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32016 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32017 if (have_macinfo)
32018 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32020 /* Remove indirect string decisions. */
32021 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32022 if (debug_line_str_hash)
32024 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32025 debug_line_str_hash = NULL;
32026 if (asm_outputs_debug_line_str ())
32028 adjust_name_comp_dir (comp_unit_die ());
32029 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32030 adjust_name_comp_dir (node->die);
32035 #if ENABLE_ASSERT_CHECKING
32037 dw_die_ref die = comp_unit_die (), c;
32038 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32040 #endif
32041 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32042 resolve_addr (ctnode->root_die);
32043 resolve_addr (comp_unit_die ());
32044 move_marked_base_types ();
32046 if (dump_file)
32048 fprintf (dump_file, "DWARF for %s\n", filename);
32049 print_die (comp_unit_die (), dump_file);
32052 /* Initialize sections and labels used for actual assembler output. */
32053 unsigned generation = init_sections_and_labels (false);
32055 /* Traverse the DIE's and add sibling attributes to those DIE's that
32056 have children. */
32057 add_sibling_attributes (comp_unit_die ());
32058 limbo_die_node *node;
32059 for (node = cu_die_list; node; node = node->next)
32060 add_sibling_attributes (node->die);
32061 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32062 add_sibling_attributes (ctnode->root_die);
32064 /* When splitting DWARF info, we put some attributes in the
32065 skeleton compile_unit DIE that remains in the .o, while
32066 most attributes go in the DWO compile_unit_die. */
32067 if (dwarf_split_debug_info)
32069 limbo_die_node *cu;
32070 main_comp_unit_die = gen_compile_unit_die (NULL);
32071 if (dwarf_version >= 5)
32072 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32073 cu = limbo_die_list;
32074 gcc_assert (cu->die == main_comp_unit_die);
32075 limbo_die_list = limbo_die_list->next;
32076 cu->next = cu_die_list;
32077 cu_die_list = cu;
32079 else
32080 main_comp_unit_die = comp_unit_die ();
32082 /* Output a terminator label for the .text section. */
32083 switch_to_section (text_section);
32084 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32085 if (cold_text_section)
32087 switch_to_section (cold_text_section);
32088 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32091 /* We can only use the low/high_pc attributes if all of the code was
32092 in .text. */
32093 if ((!have_multiple_function_sections
32094 && vec_safe_length (switch_text_ranges) < 2)
32095 || (dwarf_version < 3 && dwarf_strict))
32097 const char *end_label = text_end_label;
32098 if (vec_safe_length (switch_text_ranges) == 1)
32099 end_label = (*switch_text_ranges)[0];
32100 /* Don't add if the CU has no associated code. */
32101 if (switch_text_ranges)
32102 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32103 end_label, true);
32105 else
32107 unsigned fde_idx;
32108 dw_fde_ref fde;
32109 bool range_list_added = false;
32110 if (switch_text_ranges)
32112 const char *prev_loc = text_section_label;
32113 const char *loc;
32114 unsigned idx;
32116 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32117 if (prev_loc)
32119 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32120 loc, &range_list_added, true);
32121 prev_loc = NULL;
32123 else
32124 prev_loc = loc;
32126 if (prev_loc)
32127 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32128 text_end_label, &range_list_added, true);
32131 if (switch_cold_ranges)
32133 const char *prev_loc = cold_text_section_label;
32134 const char *loc;
32135 unsigned idx;
32137 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32138 if (prev_loc)
32140 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32141 loc, &range_list_added, true);
32142 prev_loc = NULL;
32144 else
32145 prev_loc = loc;
32147 if (prev_loc)
32148 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32149 cold_end_label, &range_list_added, true);
32152 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32154 if (fde->ignored_debug)
32155 continue;
32156 if (!fde->in_std_section)
32157 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32158 fde->dw_fde_end, &range_list_added,
32159 true);
32160 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32161 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32162 fde->dw_fde_second_end, &range_list_added,
32163 true);
32166 if (range_list_added)
32168 /* We need to give .debug_loc and .debug_ranges an appropriate
32169 "base address". Use zero so that these addresses become
32170 absolute. Historically, we've emitted the unexpected
32171 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32172 Emit both to give time for other tools to adapt. */
32173 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32174 if (! dwarf_strict && dwarf_version < 4)
32175 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32177 add_ranges (NULL);
32178 have_multiple_function_sections = true;
32182 /* AIX Assembler inserts the length, so adjust the reference to match the
32183 offset expected by debuggers. */
32184 strcpy (dl_section_ref, debug_line_section_label);
32185 if (XCOFF_DEBUGGING_INFO)
32186 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32188 if (debug_info_level >= DINFO_LEVEL_TERSE)
32189 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32190 dl_section_ref);
32192 if (have_macinfo)
32193 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32194 macinfo_section_label);
32196 if (dwarf_split_debug_info)
32198 if (have_location_lists)
32200 /* Since we generate the loclists in the split DWARF .dwo
32201 file itself, we don't need to generate a loclists_base
32202 attribute for the split compile unit DIE. That attribute
32203 (and using relocatable sec_offset FORMs) isn't allowed
32204 for a split compile unit. Only if the .debug_loclists
32205 section was in the main file, would we need to generate a
32206 loclists_base attribute here (for the full or skeleton
32207 unit DIE). */
32209 /* optimize_location_lists calculates the size of the lists,
32210 so index them first, and assign indices to the entries.
32211 Although optimize_location_lists will remove entries from
32212 the table, it only does so for duplicates, and therefore
32213 only reduces ref_counts to 1. */
32214 index_location_lists (comp_unit_die ());
32217 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32218 index_rnglists ();
32220 if (addr_index_table != NULL)
32222 unsigned int index = 0;
32223 addr_index_table
32224 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32225 (&index);
32229 loc_list_idx = 0;
32230 if (have_location_lists)
32232 optimize_location_lists (comp_unit_die ());
32233 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32234 if (dwarf_version >= 5 && dwarf_split_debug_info)
32235 assign_location_list_indexes (comp_unit_die ());
32238 save_macinfo_strings ();
32240 if (dwarf_split_debug_info)
32242 unsigned int index = 0;
32244 /* Add attributes common to skeleton compile_units and
32245 type_units. Because these attributes include strings, it
32246 must be done before freezing the string table. Top-level
32247 skeleton die attrs are added when the skeleton type unit is
32248 created, so ensure it is created by this point. */
32249 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32250 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32253 /* Output all of the compilation units. We put the main one last so that
32254 the offsets are available to output_pubnames. */
32255 for (node = cu_die_list; node; node = node->next)
32256 output_comp_unit (node->die, 0, NULL);
32258 hash_table<comdat_type_hasher> comdat_type_table (100);
32259 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32261 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32263 /* Don't output duplicate types. */
32264 if (*slot != HTAB_EMPTY_ENTRY)
32265 continue;
32267 /* Add a pointer to the line table for the main compilation unit
32268 so that the debugger can make sense of DW_AT_decl_file
32269 attributes. */
32270 if (debug_info_level >= DINFO_LEVEL_TERSE)
32271 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32272 (!dwarf_split_debug_info
32273 ? dl_section_ref
32274 : debug_skeleton_line_section_label));
32276 output_comdat_type_unit (ctnode, false);
32277 *slot = ctnode;
32280 if (dwarf_split_debug_info)
32282 int mark;
32283 struct md5_ctx ctx;
32285 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32286 md5_init_ctx (&ctx);
32287 mark = 0;
32288 die_checksum (comp_unit_die (), &ctx, &mark);
32289 unmark_all_dies (comp_unit_die ());
32290 md5_finish_ctx (&ctx, checksum);
32292 if (dwarf_version < 5)
32294 /* Use the first 8 bytes of the checksum as the dwo_id,
32295 and add it to both comp-unit DIEs. */
32296 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32297 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32300 /* Add the base offset of the ranges table to the skeleton
32301 comp-unit DIE. */
32302 if (!vec_safe_is_empty (ranges_table))
32304 if (dwarf_version < 5)
32305 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32306 ranges_section_label);
32309 output_addr_table ();
32312 /* Output the main compilation unit if non-empty or if .debug_macinfo
32313 or .debug_macro will be emitted. */
32314 output_comp_unit (comp_unit_die (), have_macinfo,
32315 dwarf_split_debug_info ? checksum : NULL);
32317 if (dwarf_split_debug_info && info_section_emitted)
32318 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32320 /* Output the abbreviation table. */
32321 if (vec_safe_length (abbrev_die_table) != 1)
32323 switch_to_section (debug_abbrev_section);
32324 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32325 output_abbrev_section ();
32328 /* Output location list section if necessary. */
32329 if (have_location_lists)
32331 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32332 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32333 /* Output the location lists info. */
32334 switch_to_section (debug_loc_section);
32335 if (dwarf_version >= 5)
32337 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32338 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32339 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32340 dw2_asm_output_data (4, 0xffffffff,
32341 "Initial length escape value indicating "
32342 "64-bit DWARF extension");
32343 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32344 "Length of Location Lists");
32345 ASM_OUTPUT_LABEL (asm_out_file, l1);
32346 output_dwarf_version ();
32347 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32348 dw2_asm_output_data (1, 0, "Segment Size");
32349 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32350 "Offset Entry Count");
32352 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32353 if (dwarf_version >= 5 && dwarf_split_debug_info)
32355 unsigned int save_loc_list_idx = loc_list_idx;
32356 loc_list_idx = 0;
32357 output_loclists_offsets (comp_unit_die ());
32358 gcc_assert (save_loc_list_idx == loc_list_idx);
32360 output_location_lists (comp_unit_die ());
32361 if (dwarf_version >= 5)
32362 ASM_OUTPUT_LABEL (asm_out_file, l2);
32365 output_pubtables ();
32367 /* Output the address range information if a CU (.debug_info section)
32368 was emitted. We output an empty table even if we had no functions
32369 to put in it. This because the consumer has no way to tell the
32370 difference between an empty table that we omitted and failure to
32371 generate a table that would have contained data. */
32372 if (info_section_emitted)
32374 switch_to_section (debug_aranges_section);
32375 output_aranges ();
32378 /* Output ranges section if necessary. */
32379 if (!vec_safe_is_empty (ranges_table))
32381 if (dwarf_version >= 5)
32383 if (dwarf_split_debug_info)
32385 /* We don't know right now whether there are any
32386 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32387 Depending on into which of those two belongs the first
32388 ranges_table entry, emit that section first and that
32389 output_rnglists call will return true if the other kind of
32390 ranges needs to be emitted as well. */
32391 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32392 if (output_rnglists (generation, dwo))
32393 output_rnglists (generation, !dwo);
32395 else
32396 output_rnglists (generation, false);
32398 else
32399 output_ranges ();
32402 /* Have to end the macro section. */
32403 if (have_macinfo)
32405 switch_to_section (debug_macinfo_section);
32406 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32407 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32408 : debug_skeleton_line_section_label, false);
32409 dw2_asm_output_data (1, 0, "End compilation unit");
32412 /* Output the source line correspondence table. We must do this
32413 even if there is no line information. Otherwise, on an empty
32414 translation unit, we will generate a present, but empty,
32415 .debug_info section. IRIX 6.5 `nm' will then complain when
32416 examining the file. This is done late so that any filenames
32417 used by the debug_info section are marked as 'used'. */
32418 switch_to_section (debug_line_section);
32419 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32420 if (! output_asm_line_debug_info ())
32421 output_line_info (false);
32423 if (dwarf_split_debug_info && info_section_emitted)
32425 switch_to_section (debug_skeleton_line_section);
32426 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32427 output_line_info (true);
32430 /* If we emitted any indirect strings, output the string table too. */
32431 if (debug_str_hash || skeleton_debug_str_hash)
32432 output_indirect_strings ();
32433 if (debug_line_str_hash)
32435 switch_to_section (debug_line_str_section);
32436 const enum dwarf_form form = DW_FORM_line_strp;
32437 debug_line_str_hash->traverse<enum dwarf_form,
32438 output_indirect_string> (form);
32441 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32442 symview_upper_bound = 0;
32443 if (zero_view_p)
32444 bitmap_clear (zero_view_p);
32447 /* Returns a hash value for X (which really is a variable_value_struct). */
32449 inline hashval_t
32450 variable_value_hasher::hash (variable_value_struct *x)
32452 return (hashval_t) x->decl_id;
32455 /* Return nonzero if decl_id of variable_value_struct X is the same as
32456 UID of decl Y. */
32458 inline bool
32459 variable_value_hasher::equal (variable_value_struct *x, tree y)
32461 return x->decl_id == DECL_UID (y);
32464 /* Helper function for resolve_variable_value, handle
32465 DW_OP_GNU_variable_value in one location expression.
32466 Return true if exprloc has been changed into loclist. */
32468 static bool
32469 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32471 dw_loc_descr_ref next;
32472 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32474 next = loc->dw_loc_next;
32475 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32476 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32477 continue;
32479 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32480 if (DECL_CONTEXT (decl) != current_function_decl)
32481 continue;
32483 dw_die_ref ref = lookup_decl_die (decl);
32484 if (ref)
32486 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32487 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32488 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32489 continue;
32491 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32492 if (l == NULL)
32493 continue;
32494 if (l->dw_loc_next)
32496 if (AT_class (a) != dw_val_class_loc)
32497 continue;
32498 switch (a->dw_attr)
32500 /* Following attributes allow both exprloc and loclist
32501 classes, so we can change them into a loclist. */
32502 case DW_AT_location:
32503 case DW_AT_string_length:
32504 case DW_AT_return_addr:
32505 case DW_AT_data_member_location:
32506 case DW_AT_frame_base:
32507 case DW_AT_segment:
32508 case DW_AT_static_link:
32509 case DW_AT_use_location:
32510 case DW_AT_vtable_elem_location:
32511 if (prev)
32513 prev->dw_loc_next = NULL;
32514 prepend_loc_descr_to_each (l, AT_loc (a));
32516 if (next)
32517 add_loc_descr_to_each (l, next);
32518 a->dw_attr_val.val_class = dw_val_class_loc_list;
32519 a->dw_attr_val.val_entry = NULL;
32520 a->dw_attr_val.v.val_loc_list = l;
32521 have_location_lists = true;
32522 return true;
32523 /* Following attributes allow both exprloc and reference,
32524 so if the whole expression is DW_OP_GNU_variable_value alone
32525 we could transform it into reference. */
32526 case DW_AT_byte_size:
32527 case DW_AT_bit_size:
32528 case DW_AT_lower_bound:
32529 case DW_AT_upper_bound:
32530 case DW_AT_bit_stride:
32531 case DW_AT_count:
32532 case DW_AT_allocated:
32533 case DW_AT_associated:
32534 case DW_AT_byte_stride:
32535 if (prev == NULL && next == NULL)
32536 break;
32537 /* FALLTHRU */
32538 default:
32539 if (dwarf_strict)
32540 continue;
32541 break;
32543 /* Create DW_TAG_variable that we can refer to. */
32544 gen_decl_die (decl, NULL_TREE, NULL,
32545 lookup_decl_die (current_function_decl));
32546 ref = lookup_decl_die (decl);
32547 if (ref)
32549 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32550 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32551 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32553 continue;
32555 if (prev)
32557 prev->dw_loc_next = l->expr;
32558 add_loc_descr (&prev->dw_loc_next, next);
32559 free_loc_descr (loc, NULL);
32560 next = prev->dw_loc_next;
32562 else
32564 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32565 add_loc_descr (&loc, next);
32566 next = loc;
32568 loc = prev;
32570 return false;
32573 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32575 static void
32576 resolve_variable_value (dw_die_ref die)
32578 dw_attr_node *a;
32579 dw_loc_list_ref loc;
32580 unsigned ix;
32582 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32583 switch (AT_class (a))
32585 case dw_val_class_loc:
32586 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32587 break;
32588 /* FALLTHRU */
32589 case dw_val_class_loc_list:
32590 loc = AT_loc_list (a);
32591 gcc_assert (loc);
32592 for (; loc; loc = loc->dw_loc_next)
32593 resolve_variable_value_in_expr (a, loc->expr);
32594 break;
32595 default:
32596 break;
32600 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32601 temporaries in the current function. */
32603 static void
32604 resolve_variable_values (void)
32606 if (!variable_value_hash || !current_function_decl)
32607 return;
32609 struct variable_value_struct *node
32610 = variable_value_hash->find_with_hash (current_function_decl,
32611 DECL_UID (current_function_decl));
32613 if (node == NULL)
32614 return;
32616 unsigned int i;
32617 dw_die_ref die;
32618 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32619 resolve_variable_value (die);
32622 /* Helper function for note_variable_value, handle one location
32623 expression. */
32625 static void
32626 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32628 for (; loc; loc = loc->dw_loc_next)
32629 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32630 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32632 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32633 dw_die_ref ref = lookup_decl_die (decl);
32634 if (! ref && (flag_generate_lto || flag_generate_offload))
32636 /* ??? This is somewhat a hack because we do not create DIEs
32637 for variables not in BLOCK trees early but when generating
32638 early LTO output we need the dw_val_class_decl_ref to be
32639 fully resolved. For fat LTO objects we'd also like to
32640 undo this after LTO dwarf output. */
32641 gcc_assert (DECL_CONTEXT (decl));
32642 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32643 gcc_assert (ctx != NULL);
32644 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32645 ref = lookup_decl_die (decl);
32646 gcc_assert (ref != NULL);
32648 if (ref)
32650 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32651 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32652 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32653 continue;
32655 if (VAR_P (decl)
32656 && DECL_CONTEXT (decl)
32657 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32658 && lookup_decl_die (DECL_CONTEXT (decl)))
32660 if (!variable_value_hash)
32661 variable_value_hash
32662 = hash_table<variable_value_hasher>::create_ggc (10);
32664 tree fndecl = DECL_CONTEXT (decl);
32665 struct variable_value_struct *node;
32666 struct variable_value_struct **slot
32667 = variable_value_hash->find_slot_with_hash (fndecl,
32668 DECL_UID (fndecl),
32669 INSERT);
32670 if (*slot == NULL)
32672 node = ggc_cleared_alloc<variable_value_struct> ();
32673 node->decl_id = DECL_UID (fndecl);
32674 *slot = node;
32676 else
32677 node = *slot;
32679 vec_safe_push (node->dies, die);
32684 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32685 with dw_val_class_decl_ref operand. */
32687 static void
32688 note_variable_value (dw_die_ref die)
32690 dw_die_ref c;
32691 dw_attr_node *a;
32692 dw_loc_list_ref loc;
32693 unsigned ix;
32695 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32696 switch (AT_class (a))
32698 case dw_val_class_loc_list:
32699 loc = AT_loc_list (a);
32700 gcc_assert (loc);
32701 if (!loc->noted_variable_value)
32703 loc->noted_variable_value = 1;
32704 for (; loc; loc = loc->dw_loc_next)
32705 note_variable_value_in_expr (die, loc->expr);
32707 break;
32708 case dw_val_class_loc:
32709 note_variable_value_in_expr (die, AT_loc (a));
32710 break;
32711 default:
32712 break;
32715 /* Mark children. */
32716 FOR_EACH_CHILD (die, c, note_variable_value (c));
32719 /* Process DWARF dies for CTF generation. */
32721 static void
32722 ctf_debug_do_cu (dw_die_ref die)
32724 dw_die_ref c;
32726 if (!ctf_do_die (die))
32727 return;
32729 FOR_EACH_CHILD (die, c, ctf_do_die (c));
32732 /* Perform any cleanups needed after the early debug generation pass
32733 has run. */
32735 static void
32736 dwarf2out_early_finish (const char *filename)
32738 comdat_type_node *ctnode;
32739 set_early_dwarf s;
32740 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32742 /* PCH might result in DW_AT_producer string being restored from the
32743 header compilation, so always fill it with empty string initially
32744 and overwrite only here. */
32745 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32747 if (dwarf_record_gcc_switches)
32748 producer_string = gen_producer_string (lang_hooks.name,
32749 save_decoded_options,
32750 save_decoded_options_count);
32751 else
32752 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
32754 producer->dw_attr_val.v.val_str->refcount--;
32755 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32757 /* Add the name for the main input file now. We delayed this from
32758 dwarf2out_init to avoid complications with PCH. */
32759 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32760 add_comp_dir_attribute (comp_unit_die ());
32762 /* With LTO early dwarf was really finished at compile-time, so make
32763 sure to adjust the phase after annotating the LTRANS CU DIE. */
32764 if (in_lto_p)
32766 early_dwarf_finished = true;
32767 if (dump_file)
32769 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32770 print_die (comp_unit_die (), dump_file);
32772 return;
32775 /* Walk through the list of incomplete types again, trying once more to
32776 emit full debugging info for them. */
32777 retry_incomplete_types ();
32779 gen_scheduled_generic_parms_dies ();
32780 gen_remaining_tmpl_value_param_die_attribute ();
32782 /* The point here is to flush out the limbo list so that it is empty
32783 and we don't need to stream it for LTO. */
32784 flush_limbo_die_list ();
32786 /* Add DW_AT_linkage_name for all deferred DIEs. */
32787 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32789 tree decl = node->created_for;
32790 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32791 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32792 ended up in deferred_asm_name before we knew it was
32793 constant and never written to disk. */
32794 && DECL_ASSEMBLER_NAME (decl))
32796 add_linkage_attr (node->die, decl);
32797 move_linkage_attr (node->die);
32800 deferred_asm_name = NULL;
32802 if (flag_eliminate_unused_debug_types)
32803 prune_unused_types ();
32805 /* Generate separate COMDAT sections for type DIEs. */
32806 if (use_debug_types)
32808 break_out_comdat_types (comp_unit_die ());
32810 /* Each new type_unit DIE was added to the limbo die list when created.
32811 Since these have all been added to comdat_type_list, clear the
32812 limbo die list. */
32813 limbo_die_list = NULL;
32815 /* For each new comdat type unit, copy declarations for incomplete
32816 types to make the new unit self-contained (i.e., no direct
32817 references to the main compile unit). */
32818 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32819 copy_decls_for_unworthy_types (ctnode->root_die);
32820 copy_decls_for_unworthy_types (comp_unit_die ());
32822 /* In the process of copying declarations from one unit to another,
32823 we may have left some declarations behind that are no longer
32824 referenced. Prune them. */
32825 prune_unused_types ();
32828 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
32829 with dw_val_class_decl_ref operand. */
32830 note_variable_value (comp_unit_die ());
32831 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32832 note_variable_value (node->die);
32833 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32834 note_variable_value (ctnode->root_die);
32835 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32836 note_variable_value (node->die);
32838 /* The AT_pubnames attribute needs to go in all skeleton dies, including
32839 both the main_cu and all skeleton TUs. Making this call unconditional
32840 would end up either adding a second copy of the AT_pubnames attribute, or
32841 requiring a special case in add_top_level_skeleton_die_attrs. */
32842 if (!dwarf_split_debug_info)
32843 add_AT_pubnames (comp_unit_die ());
32845 /* The early debug phase is now finished. */
32846 early_dwarf_finished = true;
32847 if (dump_file)
32849 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
32850 print_die (comp_unit_die (), dump_file);
32853 /* Generate CTF/BTF debug info. */
32854 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32855 || btf_debuginfo_p ()) && lang_GNU_C ())
32857 ctf_debug_init ();
32858 ctf_debug_do_cu (comp_unit_die ());
32859 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32860 ctf_debug_do_cu (node->die);
32861 /* Post process the debug data in the CTF container if necessary. */
32862 ctf_debug_init_postprocess (btf_debuginfo_p ());
32864 ctf_debug_early_finish (filename);
32867 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
32868 if ((!flag_generate_lto && !flag_generate_offload)
32869 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
32870 copy_lto_debug_sections operation of the simple object support in
32871 libiberty is not implemented for them yet. */
32872 || TARGET_PECOFF || TARGET_COFF)
32873 return;
32875 /* Now as we are going to output for LTO initialize sections and labels
32876 to the LTO variants. We don't need a random-seed postfix as other
32877 LTO sections as linking the LTO debug sections into one in a partial
32878 link is fine. */
32879 init_sections_and_labels (true);
32881 /* The output below is modeled after dwarf2out_finish with all
32882 location related output removed and some LTO specific changes.
32883 Some refactoring might make both smaller and easier to match up. */
32885 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32886 mark_base_types (ctnode->root_die);
32887 mark_base_types (comp_unit_die ());
32888 move_marked_base_types ();
32890 /* Traverse the DIE's and add sibling attributes to those DIE's
32891 that have children. */
32892 add_sibling_attributes (comp_unit_die ());
32893 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32894 add_sibling_attributes (node->die);
32895 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32896 add_sibling_attributes (ctnode->root_die);
32898 /* AIX Assembler inserts the length, so adjust the reference to match the
32899 offset expected by debuggers. */
32900 strcpy (dl_section_ref, debug_line_section_label);
32901 if (XCOFF_DEBUGGING_INFO)
32902 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32904 if (debug_info_level >= DINFO_LEVEL_TERSE)
32905 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32907 if (have_macinfo)
32908 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32909 macinfo_section_label);
32911 save_macinfo_strings ();
32913 if (dwarf_split_debug_info)
32915 unsigned int index = 0;
32916 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32919 /* Output all of the compilation units. We put the main one last so that
32920 the offsets are available to output_pubnames. */
32921 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32922 output_comp_unit (node->die, 0, NULL);
32924 hash_table<comdat_type_hasher> comdat_type_table (100);
32925 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32927 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32929 /* Don't output duplicate types. */
32930 if (*slot != HTAB_EMPTY_ENTRY)
32931 continue;
32933 /* Add a pointer to the line table for the main compilation unit
32934 so that the debugger can make sense of DW_AT_decl_file
32935 attributes. */
32936 if (debug_info_level >= DINFO_LEVEL_TERSE)
32937 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32938 (!dwarf_split_debug_info
32939 ? debug_line_section_label
32940 : debug_skeleton_line_section_label));
32942 output_comdat_type_unit (ctnode, true);
32943 *slot = ctnode;
32946 /* Stick a unique symbol to the main debuginfo section. */
32947 compute_comp_unit_symbol (comp_unit_die ());
32949 /* Output the main compilation unit. We always need it if only for
32950 the CU symbol. */
32951 output_comp_unit (comp_unit_die (), true, NULL);
32953 /* Output the abbreviation table. */
32954 if (vec_safe_length (abbrev_die_table) != 1)
32956 switch_to_section (debug_abbrev_section);
32957 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32958 output_abbrev_section ();
32961 /* Have to end the macro section. */
32962 if (have_macinfo)
32964 /* We have to save macinfo state if we need to output it again
32965 for the FAT part of the object. */
32966 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32967 if (flag_fat_lto_objects)
32968 macinfo_table = macinfo_table->copy ();
32970 switch_to_section (debug_macinfo_section);
32971 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32972 output_macinfo (debug_line_section_label, true);
32973 dw2_asm_output_data (1, 0, "End compilation unit");
32975 if (flag_fat_lto_objects)
32977 vec_free (macinfo_table);
32978 macinfo_table = saved_macinfo_table;
32982 /* Emit a skeleton debug_line section. */
32983 switch_to_section (debug_line_section);
32984 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32985 output_line_info (true);
32987 /* If we emitted any indirect strings, output the string table too. */
32988 if (debug_str_hash || skeleton_debug_str_hash)
32989 output_indirect_strings ();
32990 if (debug_line_str_hash)
32992 switch_to_section (debug_line_str_section);
32993 const enum dwarf_form form = DW_FORM_line_strp;
32994 debug_line_str_hash->traverse<enum dwarf_form,
32995 output_indirect_string> (form);
32998 /* Switch back to the text section. */
32999 switch_to_section (text_section);
33002 /* Reset all state within dwarf2out.c so that we can rerun the compiler
33003 within the same process. For use by toplev::finalize. */
33005 void
33006 dwarf2out_c_finalize (void)
33008 last_var_location_insn = NULL;
33009 cached_next_real_insn = NULL;
33010 used_rtx_array = NULL;
33011 incomplete_types = NULL;
33012 debug_info_section = NULL;
33013 debug_skeleton_info_section = NULL;
33014 debug_abbrev_section = NULL;
33015 debug_skeleton_abbrev_section = NULL;
33016 debug_aranges_section = NULL;
33017 debug_addr_section = NULL;
33018 debug_macinfo_section = NULL;
33019 debug_line_section = NULL;
33020 debug_skeleton_line_section = NULL;
33021 debug_loc_section = NULL;
33022 debug_pubnames_section = NULL;
33023 debug_pubtypes_section = NULL;
33024 debug_str_section = NULL;
33025 debug_line_str_section = NULL;
33026 debug_str_dwo_section = NULL;
33027 debug_str_offsets_section = NULL;
33028 debug_ranges_section = NULL;
33029 debug_ranges_dwo_section = NULL;
33030 debug_frame_section = NULL;
33031 fde_vec = NULL;
33032 debug_str_hash = NULL;
33033 debug_line_str_hash = NULL;
33034 skeleton_debug_str_hash = NULL;
33035 dw2_string_counter = 0;
33036 have_multiple_function_sections = false;
33037 in_text_section_p = false;
33038 cold_text_section = NULL;
33039 last_text_label = NULL;
33040 last_cold_label = NULL;
33041 switch_text_ranges = NULL;
33042 switch_cold_ranges = NULL;
33043 current_unit_personality = NULL;
33045 early_dwarf = false;
33046 early_dwarf_finished = false;
33048 next_die_offset = 0;
33049 single_comp_unit_die = NULL;
33050 comdat_type_list = NULL;
33051 limbo_die_list = NULL;
33052 file_table = NULL;
33053 decl_die_table = NULL;
33054 common_block_die_table = NULL;
33055 decl_loc_table = NULL;
33056 call_arg_locations = NULL;
33057 call_arg_loc_last = NULL;
33058 call_site_count = -1;
33059 tail_call_site_count = -1;
33060 cached_dw_loc_list_table = NULL;
33061 abbrev_die_table = NULL;
33062 delete dwarf_proc_stack_usage_map;
33063 dwarf_proc_stack_usage_map = NULL;
33064 line_info_label_num = 0;
33065 cur_line_info_table = NULL;
33066 text_section_line_info = NULL;
33067 cold_text_section_line_info = NULL;
33068 separate_line_info = NULL;
33069 info_section_emitted = false;
33070 pubname_table = NULL;
33071 pubtype_table = NULL;
33072 macinfo_table = NULL;
33073 ranges_table = NULL;
33074 ranges_by_label = NULL;
33075 rnglist_idx = 0;
33076 have_location_lists = false;
33077 loclabel_num = 0;
33078 poc_label_num = 0;
33079 last_emitted_file = NULL;
33080 label_num = 0;
33081 tmpl_value_parm_die_table = NULL;
33082 generic_type_instances = NULL;
33083 frame_pointer_fb_offset = 0;
33084 frame_pointer_fb_offset_valid = false;
33085 base_types.release ();
33086 XDELETEVEC (producer_string);
33087 producer_string = NULL;
33088 output_line_info_generation = 0;
33089 init_sections_and_labels_generation = 0;
33092 #include "gt-dwarf2out.h"