libgo: update to Go 1.15.4 release
[official-gcc.git] / gcc / dwarf2out.c
blobbc32a17efcd9a64551a6557fb9f7be6a64e9c623
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2020 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 "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "print-rtl.h"
87 #include "debug.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "lra.h"
91 #include "dumpfile.h"
92 #include "opts.h"
93 #include "tree-dfa.h"
94 #include "gdb/gdb-index.h"
95 #include "rtl-iter.h"
96 #include "stringpool.h"
97 #include "attribs.h"
98 #include "file-prefix-map.h" /* remap_debug_filename() */
100 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
101 int, bool);
102 static rtx_insn *last_var_location_insn;
103 static rtx_insn *cached_next_real_insn;
104 static void dwarf2out_decl (tree);
105 static bool is_redundant_typedef (const_tree);
107 #ifndef XCOFF_DEBUGGING_INFO
108 #define XCOFF_DEBUGGING_INFO 0
109 #endif
111 #ifndef HAVE_XCOFF_DWARF_EXTRAS
112 #define HAVE_XCOFF_DWARF_EXTRAS 0
113 #endif
115 #ifdef VMS_DEBUGGING_INFO
116 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
118 /* Define this macro to be a nonzero value if the directory specifications
119 which are output in the debug info should end with a separator. */
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
121 /* Define this macro to evaluate to a nonzero value if GCC should refrain
122 from generating indirect strings in DWARF2 debug information, for instance
123 if your target is stuck with an old version of GDB that is unable to
124 process them properly or uses VMS Debug. */
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
126 #else
127 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
128 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
129 #endif
131 /* ??? Poison these here until it can be done generically. They've been
132 totally replaced in this file; make sure it stays that way. */
133 #undef DWARF2_UNWIND_INFO
134 #undef DWARF2_FRAME_INFO
135 #if (GCC_VERSION >= 3000)
136 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
137 #endif
139 /* The size of the target's pointer type. */
140 #ifndef PTR_SIZE
141 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
142 #endif
144 /* Array of RTXes referenced by the debugging information, which therefore
145 must be kept around forever. */
146 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
148 /* A pointer to the base of a list of incomplete types which might be
149 completed at some later time. incomplete_types_list needs to be a
150 vec<tree, va_gc> *because we want to tell the garbage collector about
151 it. */
152 static GTY(()) vec<tree, va_gc> *incomplete_types;
154 /* Pointers to various DWARF2 sections. */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static const char *debug_macinfo_section_name;
163 static unsigned macinfo_label_base = 1;
164 static GTY(()) section *debug_line_section;
165 static GTY(()) section *debug_skeleton_line_section;
166 static GTY(()) section *debug_loc_section;
167 static GTY(()) section *debug_pubnames_section;
168 static GTY(()) section *debug_pubtypes_section;
169 static GTY(()) section *debug_str_section;
170 static GTY(()) section *debug_line_str_section;
171 static GTY(()) section *debug_str_dwo_section;
172 static GTY(()) section *debug_str_offsets_section;
173 static GTY(()) section *debug_ranges_section;
174 static GTY(()) section *debug_frame_section;
176 /* Maximum size (in bytes) of an artificially generated label. */
177 #define MAX_ARTIFICIAL_LABEL_BYTES 40
179 /* According to the (draft) DWARF 3 specification, the initial length
180 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
181 bytes are 0xffffffff, followed by the length stored in the next 8
182 bytes.
184 However, the SGI/MIPS ABI uses an initial length which is equal to
185 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
187 #ifndef DWARF_INITIAL_LENGTH_SIZE
188 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
189 #endif
191 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
192 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
193 #endif
195 /* Round SIZE up to the nearest BOUNDARY. */
196 #define DWARF_ROUND(SIZE,BOUNDARY) \
197 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
199 /* CIE identifier. */
200 #if HOST_BITS_PER_WIDE_INT >= 64
201 #define DWARF_CIE_ID \
202 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
203 #else
204 #define DWARF_CIE_ID DW_CIE_ID
205 #endif
208 /* A vector for a table that contains frame description
209 information for each routine. */
210 #define NOT_INDEXED (-1U)
211 #define NO_INDEX_ASSIGNED (-2U)
213 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
215 struct GTY((for_user)) indirect_string_node {
216 const char *str;
217 unsigned int refcount;
218 enum dwarf_form form;
219 char *label;
220 unsigned int index;
223 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
225 typedef const char *compare_type;
227 static hashval_t hash (indirect_string_node *);
228 static bool equal (indirect_string_node *, const char *);
231 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
233 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
235 /* With split_debug_info, both the comp_dir and dwo_name go in the
236 main object file, rather than the dwo, similar to the force_direct
237 parameter elsewhere but with additional complications:
239 1) The string is needed in both the main object file and the dwo.
240 That is, the comp_dir and dwo_name will appear in both places.
242 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
243 DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
245 3) GCC chooses the form to use late, depending on the size and
246 reference count.
248 Rather than forcing the all debug string handling functions and
249 callers to deal with these complications, simply use a separate,
250 special-cased string table for any attribute that should go in the
251 main object file. This limits the complexity to just the places
252 that need it. */
254 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
256 static GTY(()) int dw2_string_counter;
258 /* True if the compilation unit places functions in more than one section. */
259 static GTY(()) bool have_multiple_function_sections = false;
261 /* Whether the default text and cold text sections have been used at all. */
262 static GTY(()) bool text_section_used = false;
263 static GTY(()) bool cold_text_section_used = false;
265 /* The default cold text section. */
266 static GTY(()) section *cold_text_section;
268 /* The DIE for C++14 'auto' in a function return type. */
269 static GTY(()) dw_die_ref auto_die;
271 /* The DIE for C++14 'decltype(auto)' in a function return type. */
272 static GTY(()) dw_die_ref decltype_auto_die;
274 /* Forward declarations for functions defined in this file. */
276 static void output_call_frame_info (int);
277 static void dwarf2out_note_section_used (void);
279 /* Personality decl of current unit. Used only when assembler does not support
280 personality CFI. */
281 static GTY(()) rtx current_unit_personality;
283 /* Whether an eh_frame section is required. */
284 static GTY(()) bool do_eh_frame = false;
286 /* .debug_rnglists next index. */
287 static unsigned int rnglist_idx;
289 /* Data and reference forms for relocatable data. */
290 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
291 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
293 #ifndef DEBUG_FRAME_SECTION
294 #define DEBUG_FRAME_SECTION ".debug_frame"
295 #endif
297 #ifndef FUNC_BEGIN_LABEL
298 #define FUNC_BEGIN_LABEL "LFB"
299 #endif
301 #ifndef FUNC_SECOND_SECT_LABEL
302 #define FUNC_SECOND_SECT_LABEL "LFSB"
303 #endif
305 #ifndef FUNC_END_LABEL
306 #define FUNC_END_LABEL "LFE"
307 #endif
309 #ifndef PROLOGUE_END_LABEL
310 #define PROLOGUE_END_LABEL "LPE"
311 #endif
313 #ifndef EPILOGUE_BEGIN_LABEL
314 #define EPILOGUE_BEGIN_LABEL "LEB"
315 #endif
317 #ifndef FRAME_BEGIN_LABEL
318 #define FRAME_BEGIN_LABEL "Lframe"
319 #endif
320 #define CIE_AFTER_SIZE_LABEL "LSCIE"
321 #define CIE_END_LABEL "LECIE"
322 #define FDE_LABEL "LSFDE"
323 #define FDE_AFTER_SIZE_LABEL "LASFDE"
324 #define FDE_END_LABEL "LEFDE"
325 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
326 #define LINE_NUMBER_END_LABEL "LELT"
327 #define LN_PROLOG_AS_LABEL "LASLTP"
328 #define LN_PROLOG_END_LABEL "LELTP"
329 #define DIE_LABEL_PREFIX "DW"
331 /* Match the base name of a file to the base name of a compilation unit. */
333 static int
334 matches_main_base (const char *path)
336 /* Cache the last query. */
337 static const char *last_path = NULL;
338 static int last_match = 0;
339 if (path != last_path)
341 const char *base;
342 int length = base_of_path (path, &base);
343 last_path = path;
344 last_match = (length == main_input_baselength
345 && memcmp (base, main_input_basename, length) == 0);
347 return last_match;
350 #ifdef DEBUG_DEBUG_STRUCT
352 static int
353 dump_struct_debug (tree type, enum debug_info_usage usage,
354 enum debug_struct_file criterion, int generic,
355 int matches, int result)
357 /* Find the type name. */
358 tree type_decl = TYPE_STUB_DECL (type);
359 tree t = type_decl;
360 const char *name = 0;
361 if (TREE_CODE (t) == TYPE_DECL)
362 t = DECL_NAME (t);
363 if (t)
364 name = IDENTIFIER_POINTER (t);
366 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
367 criterion,
368 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
369 matches ? "bas" : "hdr",
370 generic ? "gen" : "ord",
371 usage == DINFO_USAGE_DFN ? ";" :
372 usage == DINFO_USAGE_DIR_USE ? "." : "*",
373 result,
374 (void*) type_decl, name);
375 return result;
377 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
378 dump_struct_debug (type, usage, criterion, generic, matches, result)
380 #else
382 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
383 (result)
385 #endif
387 /* Get the number of HOST_WIDE_INTs needed to represent the precision
388 of the number. Some constants have a large uniform precision, so
389 we get the precision needed for the actual value of the number. */
391 static unsigned int
392 get_full_len (const wide_int &op)
394 int prec = wi::min_precision (op, UNSIGNED);
395 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
396 / HOST_BITS_PER_WIDE_INT);
399 static bool
400 should_emit_struct_debug (tree type, enum debug_info_usage usage)
402 if (debug_info_level <= DINFO_LEVEL_TERSE)
403 return false;
405 enum debug_struct_file criterion;
406 tree type_decl;
407 bool generic = lang_hooks.types.generic_p (type);
409 if (generic)
410 criterion = debug_struct_generic[usage];
411 else
412 criterion = debug_struct_ordinary[usage];
414 if (criterion == DINFO_STRUCT_FILE_NONE)
415 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
416 if (criterion == DINFO_STRUCT_FILE_ANY)
417 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
419 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
421 if (type_decl != NULL)
423 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
424 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
426 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
427 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
430 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
433 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
434 switch to the data section instead, and write out a synthetic start label
435 for collect2 the first time around. */
437 static void
438 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
440 if (eh_frame_section == 0)
442 int flags;
444 if (EH_TABLES_CAN_BE_READ_ONLY)
446 int fde_encoding;
447 int per_encoding;
448 int lsda_encoding;
450 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
451 /*global=*/0);
452 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
453 /*global=*/1);
454 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
455 /*global=*/0);
456 flags = ((! flag_pic
457 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
458 && (fde_encoding & 0x70) != DW_EH_PE_aligned
459 && (per_encoding & 0x70) != DW_EH_PE_absptr
460 && (per_encoding & 0x70) != DW_EH_PE_aligned
461 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
462 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
463 ? 0 : SECTION_WRITE);
465 else
466 flags = SECTION_WRITE;
468 #ifdef EH_FRAME_SECTION_NAME
469 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
470 #else
471 eh_frame_section = ((flags == SECTION_WRITE)
472 ? data_section : readonly_data_section);
473 #endif /* EH_FRAME_SECTION_NAME */
476 switch_to_section (eh_frame_section);
478 #ifdef EH_FRAME_THROUGH_COLLECT2
479 /* We have no special eh_frame section. Emit special labels to guide
480 collect2. */
481 if (!back)
483 tree label = get_file_function_name ("F");
484 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
485 targetm.asm_out.globalize_label (asm_out_file,
486 IDENTIFIER_POINTER (label));
487 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
489 #endif
492 /* Switch [BACK] to the eh or debug frame table section, depending on
493 FOR_EH. */
495 static void
496 switch_to_frame_table_section (int for_eh, bool back)
498 if (for_eh)
499 switch_to_eh_frame_section (back);
500 else
502 if (!debug_frame_section)
503 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
504 SECTION_DEBUG, NULL);
505 switch_to_section (debug_frame_section);
509 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
511 enum dw_cfi_oprnd_type
512 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
514 switch (cfi)
516 case DW_CFA_nop:
517 case DW_CFA_GNU_window_save:
518 case DW_CFA_remember_state:
519 case DW_CFA_restore_state:
520 return dw_cfi_oprnd_unused;
522 case DW_CFA_set_loc:
523 case DW_CFA_advance_loc1:
524 case DW_CFA_advance_loc2:
525 case DW_CFA_advance_loc4:
526 case DW_CFA_MIPS_advance_loc8:
527 return dw_cfi_oprnd_addr;
529 case DW_CFA_offset:
530 case DW_CFA_offset_extended:
531 case DW_CFA_def_cfa:
532 case DW_CFA_offset_extended_sf:
533 case DW_CFA_def_cfa_sf:
534 case DW_CFA_restore:
535 case DW_CFA_restore_extended:
536 case DW_CFA_undefined:
537 case DW_CFA_same_value:
538 case DW_CFA_def_cfa_register:
539 case DW_CFA_register:
540 case DW_CFA_expression:
541 case DW_CFA_val_expression:
542 return dw_cfi_oprnd_reg_num;
544 case DW_CFA_def_cfa_offset:
545 case DW_CFA_GNU_args_size:
546 case DW_CFA_def_cfa_offset_sf:
547 return dw_cfi_oprnd_offset;
549 case DW_CFA_def_cfa_expression:
550 return dw_cfi_oprnd_loc;
552 default:
553 gcc_unreachable ();
557 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
559 enum dw_cfi_oprnd_type
560 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
562 switch (cfi)
564 case DW_CFA_def_cfa:
565 case DW_CFA_def_cfa_sf:
566 case DW_CFA_offset:
567 case DW_CFA_offset_extended_sf:
568 case DW_CFA_offset_extended:
569 return dw_cfi_oprnd_offset;
571 case DW_CFA_register:
572 return dw_cfi_oprnd_reg_num;
574 case DW_CFA_expression:
575 case DW_CFA_val_expression:
576 return dw_cfi_oprnd_loc;
578 case DW_CFA_def_cfa_expression:
579 return dw_cfi_oprnd_cfa_loc;
581 default:
582 return dw_cfi_oprnd_unused;
586 /* Output one FDE. */
588 static void
589 output_fde (dw_fde_ref fde, bool for_eh, bool second,
590 char *section_start_label, int fde_encoding, char *augmentation,
591 bool any_lsda_needed, int lsda_encoding)
593 const char *begin, *end;
594 static unsigned int j;
595 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
597 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
598 /* empty */ 0);
599 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
600 for_eh + j);
601 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
602 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
603 if (!XCOFF_DEBUGGING_INFO || for_eh)
605 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
606 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
607 " indicating 64-bit DWARF extension");
608 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
609 "FDE Length");
611 ASM_OUTPUT_LABEL (asm_out_file, l1);
613 if (for_eh)
614 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
615 else
616 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
617 debug_frame_section, "FDE CIE offset");
619 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
620 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
622 if (for_eh)
624 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
625 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
626 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
627 "FDE initial location");
628 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
629 end, begin, "FDE address range");
631 else
633 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
634 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
637 if (augmentation[0])
639 if (any_lsda_needed)
641 int size = size_of_encoded_value (lsda_encoding);
643 if (lsda_encoding == DW_EH_PE_aligned)
645 int offset = ( 4 /* Length */
646 + 4 /* CIE offset */
647 + 2 * size_of_encoded_value (fde_encoding)
648 + 1 /* Augmentation size */ );
649 int pad = -offset & (PTR_SIZE - 1);
651 size += pad;
652 gcc_assert (size_of_uleb128 (size) == 1);
655 dw2_asm_output_data_uleb128 (size, "Augmentation size");
657 if (fde->uses_eh_lsda)
659 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
660 fde->funcdef_number);
661 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
662 gen_rtx_SYMBOL_REF (Pmode, l1),
663 false,
664 "Language Specific Data Area");
666 else
668 if (lsda_encoding == DW_EH_PE_aligned)
669 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
670 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
671 "Language Specific Data Area (none)");
674 else
675 dw2_asm_output_data_uleb128 (0, "Augmentation size");
678 /* Loop through the Call Frame Instructions associated with this FDE. */
679 fde->dw_fde_current_label = begin;
681 size_t from, until, i;
683 from = 0;
684 until = vec_safe_length (fde->dw_fde_cfi);
686 if (fde->dw_fde_second_begin == NULL)
688 else if (!second)
689 until = fde->dw_fde_switch_cfi_index;
690 else
691 from = fde->dw_fde_switch_cfi_index;
693 for (i = from; i < until; i++)
694 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
697 /* If we are to emit a ref/link from function bodies to their frame tables,
698 do it now. This is typically performed to make sure that tables
699 associated with functions are dragged with them and not discarded in
700 garbage collecting links. We need to do this on a per function basis to
701 cope with -ffunction-sections. */
703 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
704 /* Switch to the function section, emit the ref to the tables, and
705 switch *back* into the table section. */
706 switch_to_section (function_section (fde->decl));
707 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
708 switch_to_frame_table_section (for_eh, true);
709 #endif
711 /* Pad the FDE out to an address sized boundary. */
712 ASM_OUTPUT_ALIGN (asm_out_file,
713 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
714 ASM_OUTPUT_LABEL (asm_out_file, l2);
716 j += 2;
719 /* Return true if frame description entry FDE is needed for EH. */
721 static bool
722 fde_needed_for_eh_p (dw_fde_ref fde)
724 if (flag_asynchronous_unwind_tables)
725 return true;
727 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
728 return true;
730 if (fde->uses_eh_lsda)
731 return true;
733 /* If exceptions are enabled, we have collected nothrow info. */
734 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
735 return false;
737 return true;
740 /* Output the call frame information used to record information
741 that relates to calculating the frame pointer, and records the
742 location of saved registers. */
744 static void
745 output_call_frame_info (int for_eh)
747 unsigned int i;
748 dw_fde_ref fde;
749 dw_cfi_ref cfi;
750 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
751 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
752 bool any_lsda_needed = false;
753 char augmentation[6];
754 int augmentation_size;
755 int fde_encoding = DW_EH_PE_absptr;
756 int per_encoding = DW_EH_PE_absptr;
757 int lsda_encoding = DW_EH_PE_absptr;
758 int return_reg;
759 rtx personality = NULL;
760 int dw_cie_version;
762 /* Don't emit a CIE if there won't be any FDEs. */
763 if (!fde_vec)
764 return;
766 /* Nothing to do if the assembler's doing it all. */
767 if (dwarf2out_do_cfi_asm ())
768 return;
770 /* If we don't have any functions we'll want to unwind out of, don't emit
771 any EH unwind information. If we make FDEs linkonce, we may have to
772 emit an empty label for an FDE that wouldn't otherwise be emitted. We
773 want to avoid having an FDE kept around when the function it refers to
774 is discarded. Example where this matters: a primary function template
775 in C++ requires EH information, an explicit specialization doesn't. */
776 if (for_eh)
778 bool any_eh_needed = false;
780 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
782 if (fde->uses_eh_lsda)
783 any_eh_needed = any_lsda_needed = true;
784 else if (fde_needed_for_eh_p (fde))
785 any_eh_needed = true;
786 else if (TARGET_USES_WEAK_UNWIND_INFO)
787 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
790 if (!any_eh_needed)
791 return;
794 /* We're going to be generating comments, so turn on app. */
795 if (flag_debug_asm)
796 app_enable ();
798 /* Switch to the proper frame section, first time. */
799 switch_to_frame_table_section (for_eh, false);
801 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
802 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
804 /* Output the CIE. */
805 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
806 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
807 if (!XCOFF_DEBUGGING_INFO || for_eh)
809 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
810 dw2_asm_output_data (4, 0xffffffff,
811 "Initial length escape value indicating 64-bit DWARF extension");
812 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
813 "Length of Common Information Entry");
815 ASM_OUTPUT_LABEL (asm_out_file, l1);
817 /* Now that the CIE pointer is PC-relative for EH,
818 use 0 to identify the CIE. */
819 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
820 (for_eh ? 0 : DWARF_CIE_ID),
821 "CIE Identifier Tag");
823 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
824 use CIE version 1, unless that would produce incorrect results
825 due to overflowing the return register column. */
826 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
827 dw_cie_version = 1;
828 if (return_reg >= 256 || dwarf_version > 2)
829 dw_cie_version = 3;
830 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
832 augmentation[0] = 0;
833 augmentation_size = 0;
835 personality = current_unit_personality;
836 if (for_eh)
838 char *p;
840 /* Augmentation:
841 z Indicates that a uleb128 is present to size the
842 augmentation section.
843 L Indicates the encoding (and thus presence) of
844 an LSDA pointer in the FDE augmentation.
845 R Indicates a non-default pointer encoding for
846 FDE code pointers.
847 P Indicates the presence of an encoding + language
848 personality routine in the CIE augmentation. */
850 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
851 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
852 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
854 p = augmentation + 1;
855 if (personality)
857 *p++ = 'P';
858 augmentation_size += 1 + size_of_encoded_value (per_encoding);
859 assemble_external_libcall (personality);
861 if (any_lsda_needed)
863 *p++ = 'L';
864 augmentation_size += 1;
866 if (fde_encoding != DW_EH_PE_absptr)
868 *p++ = 'R';
869 augmentation_size += 1;
871 if (p > augmentation + 1)
873 augmentation[0] = 'z';
874 *p = '\0';
877 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
878 if (personality && per_encoding == DW_EH_PE_aligned)
880 int offset = ( 4 /* Length */
881 + 4 /* CIE Id */
882 + 1 /* CIE version */
883 + strlen (augmentation) + 1 /* Augmentation */
884 + size_of_uleb128 (1) /* Code alignment */
885 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
886 + 1 /* RA column */
887 + 1 /* Augmentation size */
888 + 1 /* Personality encoding */ );
889 int pad = -offset & (PTR_SIZE - 1);
891 augmentation_size += pad;
893 /* Augmentations should be small, so there's scarce need to
894 iterate for a solution. Die if we exceed one uleb128 byte. */
895 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
899 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
900 if (dw_cie_version >= 4)
902 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
903 dw2_asm_output_data (1, 0, "CIE Segment Size");
905 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
906 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
907 "CIE Data Alignment Factor");
909 if (dw_cie_version == 1)
910 dw2_asm_output_data (1, return_reg, "CIE RA Column");
911 else
912 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
914 if (augmentation[0])
916 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
917 if (personality)
919 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
920 eh_data_format_name (per_encoding));
921 dw2_asm_output_encoded_addr_rtx (per_encoding,
922 personality,
923 true, NULL);
926 if (any_lsda_needed)
927 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
928 eh_data_format_name (lsda_encoding));
930 if (fde_encoding != DW_EH_PE_absptr)
931 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
932 eh_data_format_name (fde_encoding));
935 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
936 output_cfi (cfi, NULL, for_eh);
938 /* Pad the CIE out to an address sized boundary. */
939 ASM_OUTPUT_ALIGN (asm_out_file,
940 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
941 ASM_OUTPUT_LABEL (asm_out_file, l2);
943 /* Loop through all of the FDE's. */
944 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
946 unsigned int k;
948 /* Don't emit EH unwind info for leaf functions that don't need it. */
949 if (for_eh && !fde_needed_for_eh_p (fde))
950 continue;
952 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
953 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
954 augmentation, any_lsda_needed, lsda_encoding);
957 if (for_eh && targetm.terminate_dw2_eh_frame_info)
958 dw2_asm_output_data (4, 0, "End of Table");
960 /* Turn off app to make assembly quicker. */
961 if (flag_debug_asm)
962 app_disable ();
965 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
967 static void
968 dwarf2out_do_cfi_startproc (bool second)
970 int enc;
971 rtx ref;
973 fprintf (asm_out_file, "\t.cfi_startproc\n");
975 targetm.asm_out.post_cfi_startproc (asm_out_file, current_function_decl);
977 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
978 eh unwinders. */
979 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
980 return;
982 rtx personality = get_personality_function (current_function_decl);
984 if (personality)
986 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
987 ref = personality;
989 /* ??? The GAS support isn't entirely consistent. We have to
990 handle indirect support ourselves, but PC-relative is done
991 in the assembler. Further, the assembler can't handle any
992 of the weirder relocation types. */
993 if (enc & DW_EH_PE_indirect)
994 ref = dw2_force_const_mem (ref, true);
996 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
997 output_addr_const (asm_out_file, ref);
998 fputc ('\n', asm_out_file);
1001 if (crtl->uses_eh_lsda)
1003 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1005 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1006 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1007 current_function_funcdef_no);
1008 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1009 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1011 if (enc & DW_EH_PE_indirect)
1012 ref = dw2_force_const_mem (ref, true);
1014 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1015 output_addr_const (asm_out_file, ref);
1016 fputc ('\n', asm_out_file);
1020 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1021 this allocation may be done before pass_final. */
1023 dw_fde_ref
1024 dwarf2out_alloc_current_fde (void)
1026 dw_fde_ref fde;
1028 fde = ggc_cleared_alloc<dw_fde_node> ();
1029 fde->decl = current_function_decl;
1030 fde->funcdef_number = current_function_funcdef_no;
1031 fde->fde_index = vec_safe_length (fde_vec);
1032 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1033 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1034 fde->nothrow = crtl->nothrow;
1035 fde->drap_reg = INVALID_REGNUM;
1036 fde->vdrap_reg = INVALID_REGNUM;
1038 /* Record the FDE associated with this function. */
1039 cfun->fde = fde;
1040 vec_safe_push (fde_vec, fde);
1042 return fde;
1045 /* Output a marker (i.e. a label) for the beginning of a function, before
1046 the prologue. */
1048 void
1049 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1050 unsigned int column ATTRIBUTE_UNUSED,
1051 const char *file ATTRIBUTE_UNUSED)
1053 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1054 char * dup_label;
1055 dw_fde_ref fde;
1056 section *fnsec;
1057 bool do_frame;
1059 current_function_func_begin_label = NULL;
1061 do_frame = dwarf2out_do_frame ();
1063 /* ??? current_function_func_begin_label is also used by except.c for
1064 call-site information. We must emit this label if it might be used. */
1065 if (!do_frame
1066 && (!flag_exceptions
1067 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1068 return;
1070 fnsec = function_section (current_function_decl);
1071 switch_to_section (fnsec);
1072 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1073 current_function_funcdef_no);
1074 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1075 current_function_funcdef_no);
1076 dup_label = xstrdup (label);
1077 current_function_func_begin_label = dup_label;
1079 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1080 if (!do_frame)
1081 return;
1083 /* Unlike the debug version, the EH version of frame unwind info is a per-
1084 function setting so we need to record whether we need it for the unit. */
1085 do_eh_frame |= dwarf2out_do_eh_frame ();
1087 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1088 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1089 would include pass_dwarf2_frame. If we've not created the FDE yet,
1090 do so now. */
1091 fde = cfun->fde;
1092 if (fde == NULL)
1093 fde = dwarf2out_alloc_current_fde ();
1095 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1096 fde->dw_fde_begin = dup_label;
1097 fde->dw_fde_current_label = dup_label;
1098 fde->in_std_section = (fnsec == text_section
1099 || (cold_text_section && fnsec == cold_text_section));
1101 /* We only want to output line number information for the genuine dwarf2
1102 prologue case, not the eh frame case. */
1103 #ifdef DWARF2_DEBUGGING_INFO
1104 if (file)
1105 dwarf2out_source_line (line, column, file, 0, true);
1106 #endif
1108 if (dwarf2out_do_cfi_asm ())
1109 dwarf2out_do_cfi_startproc (false);
1110 else
1112 rtx personality = get_personality_function (current_function_decl);
1113 if (!current_unit_personality)
1114 current_unit_personality = personality;
1116 /* We cannot keep a current personality per function as without CFI
1117 asm, at the point where we emit the CFI data, there is no current
1118 function anymore. */
1119 if (personality && current_unit_personality != personality)
1120 sorry ("multiple EH personalities are supported only with assemblers "
1121 "supporting %<.cfi_personality%> directive");
1125 /* Output a marker (i.e. a label) for the end of the generated code
1126 for a function prologue. This gets called *after* the prologue code has
1127 been generated. */
1129 void
1130 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1131 const char *file ATTRIBUTE_UNUSED)
1133 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1135 /* Output a label to mark the endpoint of the code generated for this
1136 function. */
1137 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1138 current_function_funcdef_no);
1139 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1140 current_function_funcdef_no);
1141 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1144 /* Output a marker (i.e. a label) for the beginning of the generated code
1145 for a function epilogue. This gets called *before* the prologue code has
1146 been generated. */
1148 void
1149 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1150 const char *file ATTRIBUTE_UNUSED)
1152 dw_fde_ref fde = cfun->fde;
1153 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1155 if (fde->dw_fde_vms_begin_epilogue)
1156 return;
1158 /* Output a label to mark the endpoint of the code generated for this
1159 function. */
1160 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1161 current_function_funcdef_no);
1162 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1163 current_function_funcdef_no);
1164 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1167 /* Output a marker (i.e. a label) for the absolute end of the generated code
1168 for a function definition. This gets called *after* the epilogue code has
1169 been generated. */
1171 void
1172 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1173 const char *file ATTRIBUTE_UNUSED)
1175 dw_fde_ref fde;
1176 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1178 last_var_location_insn = NULL;
1179 cached_next_real_insn = NULL;
1181 if (dwarf2out_do_cfi_asm ())
1182 fprintf (asm_out_file, "\t.cfi_endproc\n");
1184 /* Output a label to mark the endpoint of the code generated for this
1185 function. */
1186 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1187 current_function_funcdef_no);
1188 ASM_OUTPUT_LABEL (asm_out_file, label);
1189 fde = cfun->fde;
1190 gcc_assert (fde != NULL);
1191 if (fde->dw_fde_second_begin == NULL)
1192 fde->dw_fde_end = xstrdup (label);
1195 void
1196 dwarf2out_frame_finish (void)
1198 /* Output call frame information. */
1199 if (targetm.debug_unwind_info () == UI_DWARF2)
1200 output_call_frame_info (0);
1202 /* Output another copy for the unwinder. */
1203 if (do_eh_frame)
1204 output_call_frame_info (1);
1207 /* Note that the current function section is being used for code. */
1209 static void
1210 dwarf2out_note_section_used (void)
1212 section *sec = current_function_section ();
1213 if (sec == text_section)
1214 text_section_used = true;
1215 else if (sec == cold_text_section)
1216 cold_text_section_used = true;
1219 static void var_location_switch_text_section (void);
1220 static void set_cur_line_info_table (section *);
1222 void
1223 dwarf2out_switch_text_section (void)
1225 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1226 section *sect;
1227 dw_fde_ref fde = cfun->fde;
1229 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1231 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1232 current_function_funcdef_no);
1234 fde->dw_fde_second_begin = ggc_strdup (label);
1235 if (!in_cold_section_p)
1237 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1238 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1240 else
1242 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1243 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1245 have_multiple_function_sections = true;
1247 /* There is no need to mark used sections when not debugging. */
1248 if (cold_text_section != NULL)
1249 dwarf2out_note_section_used ();
1251 if (dwarf2out_do_cfi_asm ())
1252 fprintf (asm_out_file, "\t.cfi_endproc\n");
1254 /* Now do the real section switch. */
1255 sect = current_function_section ();
1256 switch_to_section (sect);
1258 fde->second_in_std_section
1259 = (sect == text_section
1260 || (cold_text_section && sect == cold_text_section));
1262 if (dwarf2out_do_cfi_asm ())
1263 dwarf2out_do_cfi_startproc (true);
1265 var_location_switch_text_section ();
1267 if (cold_text_section != NULL)
1268 set_cur_line_info_table (sect);
1271 /* And now, the subset of the debugging information support code necessary
1272 for emitting location expressions. */
1274 /* Data about a single source file. */
1275 struct GTY((for_user)) dwarf_file_data {
1276 const char * filename;
1277 int emitted_number;
1280 /* Describe an entry into the .debug_addr section. */
1282 enum ate_kind {
1283 ate_kind_rtx,
1284 ate_kind_rtx_dtprel,
1285 ate_kind_label
1288 struct GTY((for_user)) addr_table_entry {
1289 enum ate_kind kind;
1290 unsigned int refcount;
1291 unsigned int index;
1292 union addr_table_entry_struct_union
1294 rtx GTY ((tag ("0"))) rtl;
1295 char * GTY ((tag ("1"))) label;
1297 GTY ((desc ("%1.kind"))) addr;
1300 typedef unsigned int var_loc_view;
1302 /* Location lists are ranges + location descriptions for that range,
1303 so you can track variables that are in different places over
1304 their entire life. */
1305 typedef struct GTY(()) dw_loc_list_struct {
1306 dw_loc_list_ref dw_loc_next;
1307 const char *begin; /* Label and addr_entry for start of range */
1308 addr_table_entry *begin_entry;
1309 const char *end; /* Label for end of range */
1310 char *ll_symbol; /* Label for beginning of location list.
1311 Only on head of list. */
1312 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1313 const char *section; /* Section this loclist is relative to */
1314 dw_loc_descr_ref expr;
1315 var_loc_view vbegin, vend;
1316 hashval_t hash;
1317 /* True if all addresses in this and subsequent lists are known to be
1318 resolved. */
1319 bool resolved_addr;
1320 /* True if this list has been replaced by dw_loc_next. */
1321 bool replaced;
1322 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1323 section. */
1324 unsigned char emitted : 1;
1325 /* True if hash field is index rather than hash value. */
1326 unsigned char num_assigned : 1;
1327 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1328 unsigned char offset_emitted : 1;
1329 /* True if note_variable_value_in_expr has been called on it. */
1330 unsigned char noted_variable_value : 1;
1331 /* True if the range should be emitted even if begin and end
1332 are the same. */
1333 bool force;
1334 } dw_loc_list_node;
1336 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1337 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1339 /* Convert a DWARF stack opcode into its string name. */
1341 static const char *
1342 dwarf_stack_op_name (unsigned int op)
1344 const char *name = get_DW_OP_name (op);
1346 if (name != NULL)
1347 return name;
1349 return "OP_<unknown>";
1352 /* Return TRUE iff we're to output location view lists as a separate
1353 attribute next to the location lists, as an extension compatible
1354 with DWARF 2 and above. */
1356 static inline bool
1357 dwarf2out_locviews_in_attribute ()
1359 return debug_variable_location_views == 1;
1362 /* Return TRUE iff we're to output location view lists as part of the
1363 location lists, as proposed for standardization after DWARF 5. */
1365 static inline bool
1366 dwarf2out_locviews_in_loclist ()
1368 #ifndef DW_LLE_view_pair
1369 return false;
1370 #else
1371 return debug_variable_location_views == -1;
1372 #endif
1375 /* Return a pointer to a newly allocated location description. Location
1376 descriptions are simple expression terms that can be strung
1377 together to form more complicated location (address) descriptions. */
1379 static inline dw_loc_descr_ref
1380 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1381 unsigned HOST_WIDE_INT oprnd2)
1383 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1385 descr->dw_loc_opc = op;
1386 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1387 descr->dw_loc_oprnd1.val_entry = NULL;
1388 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1389 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1390 descr->dw_loc_oprnd2.val_entry = NULL;
1391 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1393 return descr;
1396 /* Add a location description term to a location description expression. */
1398 static inline void
1399 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1401 dw_loc_descr_ref *d;
1403 /* Find the end of the chain. */
1404 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1407 *d = descr;
1410 /* Compare two location operands for exact equality. */
1412 static bool
1413 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1415 if (a->val_class != b->val_class)
1416 return false;
1417 switch (a->val_class)
1419 case dw_val_class_none:
1420 return true;
1421 case dw_val_class_addr:
1422 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1424 case dw_val_class_offset:
1425 case dw_val_class_unsigned_const:
1426 case dw_val_class_const:
1427 case dw_val_class_unsigned_const_implicit:
1428 case dw_val_class_const_implicit:
1429 case dw_val_class_range_list:
1430 /* These are all HOST_WIDE_INT, signed or unsigned. */
1431 return a->v.val_unsigned == b->v.val_unsigned;
1433 case dw_val_class_loc:
1434 return a->v.val_loc == b->v.val_loc;
1435 case dw_val_class_loc_list:
1436 return a->v.val_loc_list == b->v.val_loc_list;
1437 case dw_val_class_view_list:
1438 return a->v.val_view_list == b->v.val_view_list;
1439 case dw_val_class_die_ref:
1440 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1441 case dw_val_class_fde_ref:
1442 return a->v.val_fde_index == b->v.val_fde_index;
1443 case dw_val_class_symview:
1444 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1445 case dw_val_class_lbl_id:
1446 case dw_val_class_lineptr:
1447 case dw_val_class_macptr:
1448 case dw_val_class_loclistsptr:
1449 case dw_val_class_high_pc:
1450 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1451 case dw_val_class_str:
1452 return a->v.val_str == b->v.val_str;
1453 case dw_val_class_flag:
1454 return a->v.val_flag == b->v.val_flag;
1455 case dw_val_class_file:
1456 case dw_val_class_file_implicit:
1457 return a->v.val_file == b->v.val_file;
1458 case dw_val_class_decl_ref:
1459 return a->v.val_decl_ref == b->v.val_decl_ref;
1461 case dw_val_class_const_double:
1462 return (a->v.val_double.high == b->v.val_double.high
1463 && a->v.val_double.low == b->v.val_double.low);
1465 case dw_val_class_wide_int:
1466 return *a->v.val_wide == *b->v.val_wide;
1468 case dw_val_class_vec:
1470 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1471 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1473 return (a_len == b_len
1474 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1477 case dw_val_class_data8:
1478 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1480 case dw_val_class_vms_delta:
1481 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1482 && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1484 case dw_val_class_discr_value:
1485 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1486 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1487 case dw_val_class_discr_list:
1488 /* It makes no sense comparing two discriminant value lists. */
1489 return false;
1491 gcc_unreachable ();
1494 /* Compare two location atoms for exact equality. */
1496 static bool
1497 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1499 if (a->dw_loc_opc != b->dw_loc_opc)
1500 return false;
1502 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1503 address size, but since we always allocate cleared storage it
1504 should be zero for other types of locations. */
1505 if (a->dtprel != b->dtprel)
1506 return false;
1508 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1509 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1512 /* Compare two complete location expressions for exact equality. */
1514 bool
1515 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1517 while (1)
1519 if (a == b)
1520 return true;
1521 if (a == NULL || b == NULL)
1522 return false;
1523 if (!loc_descr_equal_p_1 (a, b))
1524 return false;
1526 a = a->dw_loc_next;
1527 b = b->dw_loc_next;
1532 /* Add a constant POLY_OFFSET to a location expression. */
1534 static void
1535 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1537 dw_loc_descr_ref loc;
1538 HOST_WIDE_INT *p;
1540 gcc_assert (*list_head != NULL);
1542 if (known_eq (poly_offset, 0))
1543 return;
1545 /* Find the end of the chain. */
1546 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1549 HOST_WIDE_INT offset;
1550 if (!poly_offset.is_constant (&offset))
1552 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1553 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1554 return;
1557 p = NULL;
1558 if (loc->dw_loc_opc == DW_OP_fbreg
1559 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1560 p = &loc->dw_loc_oprnd1.v.val_int;
1561 else if (loc->dw_loc_opc == DW_OP_bregx)
1562 p = &loc->dw_loc_oprnd2.v.val_int;
1564 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1565 offset. Don't optimize if an signed integer overflow would happen. */
1566 if (p != NULL
1567 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1568 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1569 *p += offset;
1571 else if (offset > 0)
1572 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1574 else
1576 loc->dw_loc_next
1577 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1578 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1582 /* Return a pointer to a newly allocated location description for
1583 REG and OFFSET. */
1585 static inline dw_loc_descr_ref
1586 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1588 HOST_WIDE_INT const_offset;
1589 if (offset.is_constant (&const_offset))
1591 if (reg <= 31)
1592 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1593 const_offset, 0);
1594 else
1595 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1597 else
1599 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1600 loc_descr_plus_const (&ret, offset);
1601 return ret;
1605 /* Add a constant OFFSET to a location list. */
1607 static void
1608 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1610 dw_loc_list_ref d;
1611 for (d = list_head; d != NULL; d = d->dw_loc_next)
1612 loc_descr_plus_const (&d->expr, offset);
1615 #define DWARF_REF_SIZE \
1616 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1618 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1619 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1620 DW_FORM_data16 with 128 bits. */
1621 #define DWARF_LARGEST_DATA_FORM_BITS \
1622 (dwarf_version >= 5 ? 128 : 64)
1624 /* Utility inline function for construction of ops that were GNU extension
1625 before DWARF 5. */
1626 static inline enum dwarf_location_atom
1627 dwarf_OP (enum dwarf_location_atom op)
1629 switch (op)
1631 case DW_OP_implicit_pointer:
1632 if (dwarf_version < 5)
1633 return DW_OP_GNU_implicit_pointer;
1634 break;
1636 case DW_OP_entry_value:
1637 if (dwarf_version < 5)
1638 return DW_OP_GNU_entry_value;
1639 break;
1641 case DW_OP_const_type:
1642 if (dwarf_version < 5)
1643 return DW_OP_GNU_const_type;
1644 break;
1646 case DW_OP_regval_type:
1647 if (dwarf_version < 5)
1648 return DW_OP_GNU_regval_type;
1649 break;
1651 case DW_OP_deref_type:
1652 if (dwarf_version < 5)
1653 return DW_OP_GNU_deref_type;
1654 break;
1656 case DW_OP_convert:
1657 if (dwarf_version < 5)
1658 return DW_OP_GNU_convert;
1659 break;
1661 case DW_OP_reinterpret:
1662 if (dwarf_version < 5)
1663 return DW_OP_GNU_reinterpret;
1664 break;
1666 case DW_OP_addrx:
1667 if (dwarf_version < 5)
1668 return DW_OP_GNU_addr_index;
1669 break;
1671 case DW_OP_constx:
1672 if (dwarf_version < 5)
1673 return DW_OP_GNU_const_index;
1674 break;
1676 default:
1677 break;
1679 return op;
1682 /* Similarly for attributes. */
1683 static inline enum dwarf_attribute
1684 dwarf_AT (enum dwarf_attribute at)
1686 switch (at)
1688 case DW_AT_call_return_pc:
1689 if (dwarf_version < 5)
1690 return DW_AT_low_pc;
1691 break;
1693 case DW_AT_call_tail_call:
1694 if (dwarf_version < 5)
1695 return DW_AT_GNU_tail_call;
1696 break;
1698 case DW_AT_call_origin:
1699 if (dwarf_version < 5)
1700 return DW_AT_abstract_origin;
1701 break;
1703 case DW_AT_call_target:
1704 if (dwarf_version < 5)
1705 return DW_AT_GNU_call_site_target;
1706 break;
1708 case DW_AT_call_target_clobbered:
1709 if (dwarf_version < 5)
1710 return DW_AT_GNU_call_site_target_clobbered;
1711 break;
1713 case DW_AT_call_parameter:
1714 if (dwarf_version < 5)
1715 return DW_AT_abstract_origin;
1716 break;
1718 case DW_AT_call_value:
1719 if (dwarf_version < 5)
1720 return DW_AT_GNU_call_site_value;
1721 break;
1723 case DW_AT_call_data_value:
1724 if (dwarf_version < 5)
1725 return DW_AT_GNU_call_site_data_value;
1726 break;
1728 case DW_AT_call_all_calls:
1729 if (dwarf_version < 5)
1730 return DW_AT_GNU_all_call_sites;
1731 break;
1733 case DW_AT_call_all_tail_calls:
1734 if (dwarf_version < 5)
1735 return DW_AT_GNU_all_tail_call_sites;
1736 break;
1738 case DW_AT_dwo_name:
1739 if (dwarf_version < 5)
1740 return DW_AT_GNU_dwo_name;
1741 break;
1743 case DW_AT_addr_base:
1744 if (dwarf_version < 5)
1745 return DW_AT_GNU_addr_base;
1746 break;
1748 default:
1749 break;
1751 return at;
1754 /* And similarly for tags. */
1755 static inline enum dwarf_tag
1756 dwarf_TAG (enum dwarf_tag tag)
1758 switch (tag)
1760 case DW_TAG_call_site:
1761 if (dwarf_version < 5)
1762 return DW_TAG_GNU_call_site;
1763 break;
1765 case DW_TAG_call_site_parameter:
1766 if (dwarf_version < 5)
1767 return DW_TAG_GNU_call_site_parameter;
1768 break;
1770 default:
1771 break;
1773 return tag;
1776 /* And similarly for forms. */
1777 static inline enum dwarf_form
1778 dwarf_FORM (enum dwarf_form form)
1780 switch (form)
1782 case DW_FORM_addrx:
1783 if (dwarf_version < 5)
1784 return DW_FORM_GNU_addr_index;
1785 break;
1787 case DW_FORM_strx:
1788 if (dwarf_version < 5)
1789 return DW_FORM_GNU_str_index;
1790 break;
1792 default:
1793 break;
1795 return form;
1798 static unsigned long int get_base_type_offset (dw_die_ref);
1800 /* Return the size of a location descriptor. */
1802 static unsigned long
1803 size_of_loc_descr (dw_loc_descr_ref loc)
1805 unsigned long size = 1;
1807 switch (loc->dw_loc_opc)
1809 case DW_OP_addr:
1810 size += DWARF2_ADDR_SIZE;
1811 break;
1812 case DW_OP_GNU_addr_index:
1813 case DW_OP_addrx:
1814 case DW_OP_GNU_const_index:
1815 case DW_OP_constx:
1816 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1817 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1818 break;
1819 case DW_OP_const1u:
1820 case DW_OP_const1s:
1821 size += 1;
1822 break;
1823 case DW_OP_const2u:
1824 case DW_OP_const2s:
1825 size += 2;
1826 break;
1827 case DW_OP_const4u:
1828 case DW_OP_const4s:
1829 size += 4;
1830 break;
1831 case DW_OP_const8u:
1832 case DW_OP_const8s:
1833 size += 8;
1834 break;
1835 case DW_OP_constu:
1836 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1837 break;
1838 case DW_OP_consts:
1839 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1840 break;
1841 case DW_OP_pick:
1842 size += 1;
1843 break;
1844 case DW_OP_plus_uconst:
1845 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1846 break;
1847 case DW_OP_skip:
1848 case DW_OP_bra:
1849 size += 2;
1850 break;
1851 case DW_OP_breg0:
1852 case DW_OP_breg1:
1853 case DW_OP_breg2:
1854 case DW_OP_breg3:
1855 case DW_OP_breg4:
1856 case DW_OP_breg5:
1857 case DW_OP_breg6:
1858 case DW_OP_breg7:
1859 case DW_OP_breg8:
1860 case DW_OP_breg9:
1861 case DW_OP_breg10:
1862 case DW_OP_breg11:
1863 case DW_OP_breg12:
1864 case DW_OP_breg13:
1865 case DW_OP_breg14:
1866 case DW_OP_breg15:
1867 case DW_OP_breg16:
1868 case DW_OP_breg17:
1869 case DW_OP_breg18:
1870 case DW_OP_breg19:
1871 case DW_OP_breg20:
1872 case DW_OP_breg21:
1873 case DW_OP_breg22:
1874 case DW_OP_breg23:
1875 case DW_OP_breg24:
1876 case DW_OP_breg25:
1877 case DW_OP_breg26:
1878 case DW_OP_breg27:
1879 case DW_OP_breg28:
1880 case DW_OP_breg29:
1881 case DW_OP_breg30:
1882 case DW_OP_breg31:
1883 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1884 break;
1885 case DW_OP_regx:
1886 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1887 break;
1888 case DW_OP_fbreg:
1889 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1890 break;
1891 case DW_OP_bregx:
1892 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1893 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1894 break;
1895 case DW_OP_piece:
1896 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1897 break;
1898 case DW_OP_bit_piece:
1899 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1900 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1901 break;
1902 case DW_OP_deref_size:
1903 case DW_OP_xderef_size:
1904 size += 1;
1905 break;
1906 case DW_OP_call2:
1907 size += 2;
1908 break;
1909 case DW_OP_call4:
1910 size += 4;
1911 break;
1912 case DW_OP_call_ref:
1913 case DW_OP_GNU_variable_value:
1914 size += DWARF_REF_SIZE;
1915 break;
1916 case DW_OP_implicit_value:
1917 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1918 + loc->dw_loc_oprnd1.v.val_unsigned;
1919 break;
1920 case DW_OP_implicit_pointer:
1921 case DW_OP_GNU_implicit_pointer:
1922 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1923 break;
1924 case DW_OP_entry_value:
1925 case DW_OP_GNU_entry_value:
1927 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1928 size += size_of_uleb128 (op_size) + op_size;
1929 break;
1931 case DW_OP_const_type:
1932 case DW_OP_GNU_const_type:
1934 unsigned long o
1935 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1936 size += size_of_uleb128 (o) + 1;
1937 switch (loc->dw_loc_oprnd2.val_class)
1939 case dw_val_class_vec:
1940 size += loc->dw_loc_oprnd2.v.val_vec.length
1941 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1942 break;
1943 case dw_val_class_const:
1944 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1945 break;
1946 case dw_val_class_const_double:
1947 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1948 break;
1949 case dw_val_class_wide_int:
1950 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1951 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1952 break;
1953 default:
1954 gcc_unreachable ();
1956 break;
1958 case DW_OP_regval_type:
1959 case DW_OP_GNU_regval_type:
1961 unsigned long o
1962 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1963 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1964 + size_of_uleb128 (o);
1966 break;
1967 case DW_OP_deref_type:
1968 case DW_OP_GNU_deref_type:
1970 unsigned long o
1971 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1972 size += 1 + size_of_uleb128 (o);
1974 break;
1975 case DW_OP_convert:
1976 case DW_OP_reinterpret:
1977 case DW_OP_GNU_convert:
1978 case DW_OP_GNU_reinterpret:
1979 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1980 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1981 else
1983 unsigned long o
1984 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1985 size += size_of_uleb128 (o);
1987 break;
1988 case DW_OP_GNU_parameter_ref:
1989 size += 4;
1990 break;
1991 default:
1992 break;
1995 return size;
1998 /* Return the size of a series of location descriptors. */
2000 unsigned long
2001 size_of_locs (dw_loc_descr_ref loc)
2003 dw_loc_descr_ref l;
2004 unsigned long size;
2006 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2007 field, to avoid writing to a PCH file. */
2008 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2010 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2011 break;
2012 size += size_of_loc_descr (l);
2014 if (! l)
2015 return size;
2017 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2019 l->dw_loc_addr = size;
2020 size += size_of_loc_descr (l);
2023 return size;
2026 /* Return the size of the value in a DW_AT_discr_value attribute. */
2028 static int
2029 size_of_discr_value (dw_discr_value *discr_value)
2031 if (discr_value->pos)
2032 return size_of_uleb128 (discr_value->v.uval);
2033 else
2034 return size_of_sleb128 (discr_value->v.sval);
2037 /* Return the size of the value in a DW_AT_discr_list attribute. */
2039 static int
2040 size_of_discr_list (dw_discr_list_ref discr_list)
2042 int size = 0;
2044 for (dw_discr_list_ref list = discr_list;
2045 list != NULL;
2046 list = list->dw_discr_next)
2048 /* One byte for the discriminant value descriptor, and then one or two
2049 LEB128 numbers, depending on whether it's a single case label or a
2050 range label. */
2051 size += 1;
2052 size += size_of_discr_value (&list->dw_discr_lower_bound);
2053 if (list->dw_discr_range != 0)
2054 size += size_of_discr_value (&list->dw_discr_upper_bound);
2056 return size;
2059 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2060 static void get_ref_die_offset_label (char *, dw_die_ref);
2061 static unsigned long int get_ref_die_offset (dw_die_ref);
2063 /* Output location description stack opcode's operands (if any).
2064 The for_eh_or_skip parameter controls whether register numbers are
2065 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2066 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2067 info). This should be suppressed for the cases that have not been converted
2068 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2070 static void
2071 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2073 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2074 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2076 switch (loc->dw_loc_opc)
2078 #ifdef DWARF2_DEBUGGING_INFO
2079 case DW_OP_const2u:
2080 case DW_OP_const2s:
2081 dw2_asm_output_data (2, val1->v.val_int, NULL);
2082 break;
2083 case DW_OP_const4u:
2084 if (loc->dtprel)
2086 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2087 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2088 val1->v.val_addr);
2089 fputc ('\n', asm_out_file);
2090 break;
2092 /* FALLTHRU */
2093 case DW_OP_const4s:
2094 dw2_asm_output_data (4, val1->v.val_int, NULL);
2095 break;
2096 case DW_OP_const8u:
2097 if (loc->dtprel)
2099 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2100 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2101 val1->v.val_addr);
2102 fputc ('\n', asm_out_file);
2103 break;
2105 /* FALLTHRU */
2106 case DW_OP_const8s:
2107 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2108 dw2_asm_output_data (8, val1->v.val_int, NULL);
2109 break;
2110 case DW_OP_skip:
2111 case DW_OP_bra:
2113 int offset;
2115 gcc_assert (val1->val_class == dw_val_class_loc);
2116 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2118 dw2_asm_output_data (2, offset, NULL);
2120 break;
2121 case DW_OP_implicit_value:
2122 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2123 switch (val2->val_class)
2125 case dw_val_class_const:
2126 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2127 break;
2128 case dw_val_class_vec:
2130 unsigned int elt_size = val2->v.val_vec.elt_size;
2131 unsigned int len = val2->v.val_vec.length;
2132 unsigned int i;
2133 unsigned char *p;
2135 if (elt_size > sizeof (HOST_WIDE_INT))
2137 elt_size /= 2;
2138 len *= 2;
2140 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2141 i < len;
2142 i++, p += elt_size)
2143 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2144 "fp or vector constant word %u", i);
2146 break;
2147 case dw_val_class_const_double:
2149 unsigned HOST_WIDE_INT first, second;
2151 if (WORDS_BIG_ENDIAN)
2153 first = val2->v.val_double.high;
2154 second = val2->v.val_double.low;
2156 else
2158 first = val2->v.val_double.low;
2159 second = val2->v.val_double.high;
2161 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2162 first, NULL);
2163 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2164 second, NULL);
2166 break;
2167 case dw_val_class_wide_int:
2169 int i;
2170 int len = get_full_len (*val2->v.val_wide);
2171 if (WORDS_BIG_ENDIAN)
2172 for (i = len - 1; i >= 0; --i)
2173 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2174 val2->v.val_wide->elt (i), NULL);
2175 else
2176 for (i = 0; i < len; ++i)
2177 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2178 val2->v.val_wide->elt (i), NULL);
2180 break;
2181 case dw_val_class_addr:
2182 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2183 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2184 break;
2185 default:
2186 gcc_unreachable ();
2188 break;
2189 #else
2190 case DW_OP_const2u:
2191 case DW_OP_const2s:
2192 case DW_OP_const4u:
2193 case DW_OP_const4s:
2194 case DW_OP_const8u:
2195 case DW_OP_const8s:
2196 case DW_OP_skip:
2197 case DW_OP_bra:
2198 case DW_OP_implicit_value:
2199 /* We currently don't make any attempt to make sure these are
2200 aligned properly like we do for the main unwind info, so
2201 don't support emitting things larger than a byte if we're
2202 only doing unwinding. */
2203 gcc_unreachable ();
2204 #endif
2205 case DW_OP_const1u:
2206 case DW_OP_const1s:
2207 dw2_asm_output_data (1, val1->v.val_int, NULL);
2208 break;
2209 case DW_OP_constu:
2210 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2211 break;
2212 case DW_OP_consts:
2213 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2214 break;
2215 case DW_OP_pick:
2216 dw2_asm_output_data (1, val1->v.val_int, NULL);
2217 break;
2218 case DW_OP_plus_uconst:
2219 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2220 break;
2221 case DW_OP_breg0:
2222 case DW_OP_breg1:
2223 case DW_OP_breg2:
2224 case DW_OP_breg3:
2225 case DW_OP_breg4:
2226 case DW_OP_breg5:
2227 case DW_OP_breg6:
2228 case DW_OP_breg7:
2229 case DW_OP_breg8:
2230 case DW_OP_breg9:
2231 case DW_OP_breg10:
2232 case DW_OP_breg11:
2233 case DW_OP_breg12:
2234 case DW_OP_breg13:
2235 case DW_OP_breg14:
2236 case DW_OP_breg15:
2237 case DW_OP_breg16:
2238 case DW_OP_breg17:
2239 case DW_OP_breg18:
2240 case DW_OP_breg19:
2241 case DW_OP_breg20:
2242 case DW_OP_breg21:
2243 case DW_OP_breg22:
2244 case DW_OP_breg23:
2245 case DW_OP_breg24:
2246 case DW_OP_breg25:
2247 case DW_OP_breg26:
2248 case DW_OP_breg27:
2249 case DW_OP_breg28:
2250 case DW_OP_breg29:
2251 case DW_OP_breg30:
2252 case DW_OP_breg31:
2253 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2254 break;
2255 case DW_OP_regx:
2257 unsigned r = val1->v.val_unsigned;
2258 if (for_eh_or_skip >= 0)
2259 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2260 gcc_assert (size_of_uleb128 (r)
2261 == size_of_uleb128 (val1->v.val_unsigned));
2262 dw2_asm_output_data_uleb128 (r, NULL);
2264 break;
2265 case DW_OP_fbreg:
2266 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2267 break;
2268 case DW_OP_bregx:
2270 unsigned r = val1->v.val_unsigned;
2271 if (for_eh_or_skip >= 0)
2272 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2273 gcc_assert (size_of_uleb128 (r)
2274 == size_of_uleb128 (val1->v.val_unsigned));
2275 dw2_asm_output_data_uleb128 (r, NULL);
2276 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2278 break;
2279 case DW_OP_piece:
2280 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2281 break;
2282 case DW_OP_bit_piece:
2283 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2284 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2285 break;
2286 case DW_OP_deref_size:
2287 case DW_OP_xderef_size:
2288 dw2_asm_output_data (1, val1->v.val_int, NULL);
2289 break;
2291 case DW_OP_addr:
2292 if (loc->dtprel)
2294 if (targetm.asm_out.output_dwarf_dtprel)
2296 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2297 DWARF2_ADDR_SIZE,
2298 val1->v.val_addr);
2299 fputc ('\n', asm_out_file);
2301 else
2302 gcc_unreachable ();
2304 else
2306 #ifdef DWARF2_DEBUGGING_INFO
2307 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2308 #else
2309 gcc_unreachable ();
2310 #endif
2312 break;
2314 case DW_OP_GNU_addr_index:
2315 case DW_OP_addrx:
2316 case DW_OP_GNU_const_index:
2317 case DW_OP_constx:
2318 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2319 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2320 "(index into .debug_addr)");
2321 break;
2323 case DW_OP_call2:
2324 case DW_OP_call4:
2326 unsigned long die_offset
2327 = get_ref_die_offset (val1->v.val_die_ref.die);
2328 /* Make sure the offset has been computed and that we can encode it as
2329 an operand. */
2330 gcc_assert (die_offset > 0
2331 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2332 ? 0xffff
2333 : 0xffffffff));
2334 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2335 die_offset, NULL);
2337 break;
2339 case DW_OP_call_ref:
2340 case DW_OP_GNU_variable_value:
2342 char label[MAX_ARTIFICIAL_LABEL_BYTES
2343 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2344 gcc_assert (val1->val_class == dw_val_class_die_ref);
2345 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2346 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2348 break;
2350 case DW_OP_implicit_pointer:
2351 case DW_OP_GNU_implicit_pointer:
2353 char label[MAX_ARTIFICIAL_LABEL_BYTES
2354 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2355 gcc_assert (val1->val_class == dw_val_class_die_ref);
2356 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2357 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2358 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2360 break;
2362 case DW_OP_entry_value:
2363 case DW_OP_GNU_entry_value:
2364 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2365 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2366 break;
2368 case DW_OP_const_type:
2369 case DW_OP_GNU_const_type:
2371 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2372 gcc_assert (o);
2373 dw2_asm_output_data_uleb128 (o, NULL);
2374 switch (val2->val_class)
2376 case dw_val_class_const:
2377 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2378 dw2_asm_output_data (1, l, NULL);
2379 dw2_asm_output_data (l, val2->v.val_int, NULL);
2380 break;
2381 case dw_val_class_vec:
2383 unsigned int elt_size = val2->v.val_vec.elt_size;
2384 unsigned int len = val2->v.val_vec.length;
2385 unsigned int i;
2386 unsigned char *p;
2388 l = len * elt_size;
2389 dw2_asm_output_data (1, l, NULL);
2390 if (elt_size > sizeof (HOST_WIDE_INT))
2392 elt_size /= 2;
2393 len *= 2;
2395 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2396 i < len;
2397 i++, p += elt_size)
2398 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2399 "fp or vector constant word %u", i);
2401 break;
2402 case dw_val_class_const_double:
2404 unsigned HOST_WIDE_INT first, second;
2405 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2407 dw2_asm_output_data (1, 2 * l, NULL);
2408 if (WORDS_BIG_ENDIAN)
2410 first = val2->v.val_double.high;
2411 second = val2->v.val_double.low;
2413 else
2415 first = val2->v.val_double.low;
2416 second = val2->v.val_double.high;
2418 dw2_asm_output_data (l, first, NULL);
2419 dw2_asm_output_data (l, second, NULL);
2421 break;
2422 case dw_val_class_wide_int:
2424 int i;
2425 int len = get_full_len (*val2->v.val_wide);
2426 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2428 dw2_asm_output_data (1, len * l, NULL);
2429 if (WORDS_BIG_ENDIAN)
2430 for (i = len - 1; i >= 0; --i)
2431 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2432 else
2433 for (i = 0; i < len; ++i)
2434 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2436 break;
2437 default:
2438 gcc_unreachable ();
2441 break;
2442 case DW_OP_regval_type:
2443 case DW_OP_GNU_regval_type:
2445 unsigned r = val1->v.val_unsigned;
2446 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2447 gcc_assert (o);
2448 if (for_eh_or_skip >= 0)
2450 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2451 gcc_assert (size_of_uleb128 (r)
2452 == size_of_uleb128 (val1->v.val_unsigned));
2454 dw2_asm_output_data_uleb128 (r, NULL);
2455 dw2_asm_output_data_uleb128 (o, NULL);
2457 break;
2458 case DW_OP_deref_type:
2459 case DW_OP_GNU_deref_type:
2461 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2462 gcc_assert (o);
2463 dw2_asm_output_data (1, val1->v.val_int, NULL);
2464 dw2_asm_output_data_uleb128 (o, NULL);
2466 break;
2467 case DW_OP_convert:
2468 case DW_OP_reinterpret:
2469 case DW_OP_GNU_convert:
2470 case DW_OP_GNU_reinterpret:
2471 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2472 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2473 else
2475 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2476 gcc_assert (o);
2477 dw2_asm_output_data_uleb128 (o, NULL);
2479 break;
2481 case DW_OP_GNU_parameter_ref:
2483 unsigned long o;
2484 gcc_assert (val1->val_class == dw_val_class_die_ref);
2485 o = get_ref_die_offset (val1->v.val_die_ref.die);
2486 dw2_asm_output_data (4, o, NULL);
2488 break;
2490 default:
2491 /* Other codes have no operands. */
2492 break;
2496 /* Output a sequence of location operations.
2497 The for_eh_or_skip parameter controls whether register numbers are
2498 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2499 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2500 info). This should be suppressed for the cases that have not been converted
2501 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2503 void
2504 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2506 for (; loc != NULL; loc = loc->dw_loc_next)
2508 enum dwarf_location_atom opc = loc->dw_loc_opc;
2509 /* Output the opcode. */
2510 if (for_eh_or_skip >= 0
2511 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2513 unsigned r = (opc - DW_OP_breg0);
2514 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2515 gcc_assert (r <= 31);
2516 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2518 else if (for_eh_or_skip >= 0
2519 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2521 unsigned r = (opc - DW_OP_reg0);
2522 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2523 gcc_assert (r <= 31);
2524 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2527 dw2_asm_output_data (1, opc,
2528 "%s", dwarf_stack_op_name (opc));
2530 /* Output the operand(s) (if any). */
2531 output_loc_operands (loc, for_eh_or_skip);
2535 /* Output location description stack opcode's operands (if any).
2536 The output is single bytes on a line, suitable for .cfi_escape. */
2538 static void
2539 output_loc_operands_raw (dw_loc_descr_ref loc)
2541 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2542 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2544 switch (loc->dw_loc_opc)
2546 case DW_OP_addr:
2547 case DW_OP_GNU_addr_index:
2548 case DW_OP_addrx:
2549 case DW_OP_GNU_const_index:
2550 case DW_OP_constx:
2551 case DW_OP_implicit_value:
2552 /* We cannot output addresses in .cfi_escape, only bytes. */
2553 gcc_unreachable ();
2555 case DW_OP_const1u:
2556 case DW_OP_const1s:
2557 case DW_OP_pick:
2558 case DW_OP_deref_size:
2559 case DW_OP_xderef_size:
2560 fputc (',', asm_out_file);
2561 dw2_asm_output_data_raw (1, val1->v.val_int);
2562 break;
2564 case DW_OP_const2u:
2565 case DW_OP_const2s:
2566 fputc (',', asm_out_file);
2567 dw2_asm_output_data_raw (2, val1->v.val_int);
2568 break;
2570 case DW_OP_const4u:
2571 case DW_OP_const4s:
2572 fputc (',', asm_out_file);
2573 dw2_asm_output_data_raw (4, val1->v.val_int);
2574 break;
2576 case DW_OP_const8u:
2577 case DW_OP_const8s:
2578 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2579 fputc (',', asm_out_file);
2580 dw2_asm_output_data_raw (8, val1->v.val_int);
2581 break;
2583 case DW_OP_skip:
2584 case DW_OP_bra:
2586 int offset;
2588 gcc_assert (val1->val_class == dw_val_class_loc);
2589 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2591 fputc (',', asm_out_file);
2592 dw2_asm_output_data_raw (2, offset);
2594 break;
2596 case DW_OP_regx:
2598 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2599 gcc_assert (size_of_uleb128 (r)
2600 == size_of_uleb128 (val1->v.val_unsigned));
2601 fputc (',', asm_out_file);
2602 dw2_asm_output_data_uleb128_raw (r);
2604 break;
2606 case DW_OP_constu:
2607 case DW_OP_plus_uconst:
2608 case DW_OP_piece:
2609 fputc (',', asm_out_file);
2610 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2611 break;
2613 case DW_OP_bit_piece:
2614 fputc (',', asm_out_file);
2615 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2616 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2617 break;
2619 case DW_OP_consts:
2620 case DW_OP_breg0:
2621 case DW_OP_breg1:
2622 case DW_OP_breg2:
2623 case DW_OP_breg3:
2624 case DW_OP_breg4:
2625 case DW_OP_breg5:
2626 case DW_OP_breg6:
2627 case DW_OP_breg7:
2628 case DW_OP_breg8:
2629 case DW_OP_breg9:
2630 case DW_OP_breg10:
2631 case DW_OP_breg11:
2632 case DW_OP_breg12:
2633 case DW_OP_breg13:
2634 case DW_OP_breg14:
2635 case DW_OP_breg15:
2636 case DW_OP_breg16:
2637 case DW_OP_breg17:
2638 case DW_OP_breg18:
2639 case DW_OP_breg19:
2640 case DW_OP_breg20:
2641 case DW_OP_breg21:
2642 case DW_OP_breg22:
2643 case DW_OP_breg23:
2644 case DW_OP_breg24:
2645 case DW_OP_breg25:
2646 case DW_OP_breg26:
2647 case DW_OP_breg27:
2648 case DW_OP_breg28:
2649 case DW_OP_breg29:
2650 case DW_OP_breg30:
2651 case DW_OP_breg31:
2652 case DW_OP_fbreg:
2653 fputc (',', asm_out_file);
2654 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2655 break;
2657 case DW_OP_bregx:
2659 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2660 gcc_assert (size_of_uleb128 (r)
2661 == size_of_uleb128 (val1->v.val_unsigned));
2662 fputc (',', asm_out_file);
2663 dw2_asm_output_data_uleb128_raw (r);
2664 fputc (',', asm_out_file);
2665 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2667 break;
2669 case DW_OP_implicit_pointer:
2670 case DW_OP_entry_value:
2671 case DW_OP_const_type:
2672 case DW_OP_regval_type:
2673 case DW_OP_deref_type:
2674 case DW_OP_convert:
2675 case DW_OP_reinterpret:
2676 case DW_OP_GNU_implicit_pointer:
2677 case DW_OP_GNU_entry_value:
2678 case DW_OP_GNU_const_type:
2679 case DW_OP_GNU_regval_type:
2680 case DW_OP_GNU_deref_type:
2681 case DW_OP_GNU_convert:
2682 case DW_OP_GNU_reinterpret:
2683 case DW_OP_GNU_parameter_ref:
2684 gcc_unreachable ();
2685 break;
2687 default:
2688 /* Other codes have no operands. */
2689 break;
2693 void
2694 output_loc_sequence_raw (dw_loc_descr_ref loc)
2696 while (1)
2698 enum dwarf_location_atom opc = loc->dw_loc_opc;
2699 /* Output the opcode. */
2700 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2702 unsigned r = (opc - DW_OP_breg0);
2703 r = DWARF2_FRAME_REG_OUT (r, 1);
2704 gcc_assert (r <= 31);
2705 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2707 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2709 unsigned r = (opc - DW_OP_reg0);
2710 r = DWARF2_FRAME_REG_OUT (r, 1);
2711 gcc_assert (r <= 31);
2712 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2714 /* Output the opcode. */
2715 fprintf (asm_out_file, "%#x", opc);
2716 output_loc_operands_raw (loc);
2718 if (!loc->dw_loc_next)
2719 break;
2720 loc = loc->dw_loc_next;
2722 fputc (',', asm_out_file);
2726 /* This function builds a dwarf location descriptor sequence from a
2727 dw_cfa_location, adding the given OFFSET to the result of the
2728 expression. */
2730 struct dw_loc_descr_node *
2731 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2733 struct dw_loc_descr_node *head, *tmp;
2735 offset += cfa->offset;
2737 if (cfa->indirect)
2739 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2740 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2741 head->dw_loc_oprnd1.val_entry = NULL;
2742 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2743 add_loc_descr (&head, tmp);
2744 loc_descr_plus_const (&head, offset);
2746 else
2747 head = new_reg_loc_descr (cfa->reg, offset);
2749 return head;
2752 /* This function builds a dwarf location descriptor sequence for
2753 the address at OFFSET from the CFA when stack is aligned to
2754 ALIGNMENT byte. */
2756 struct dw_loc_descr_node *
2757 build_cfa_aligned_loc (dw_cfa_location *cfa,
2758 poly_int64 offset, HOST_WIDE_INT alignment)
2760 struct dw_loc_descr_node *head;
2761 unsigned int dwarf_fp
2762 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2764 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2765 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2767 head = new_reg_loc_descr (dwarf_fp, 0);
2768 add_loc_descr (&head, int_loc_descriptor (alignment));
2769 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2770 loc_descr_plus_const (&head, offset);
2772 else
2773 head = new_reg_loc_descr (dwarf_fp, offset);
2774 return head;
2777 /* And now, the support for symbolic debugging information. */
2779 /* .debug_str support. */
2781 static void dwarf2out_init (const char *);
2782 static void dwarf2out_finish (const char *);
2783 static void dwarf2out_early_finish (const char *);
2784 static void dwarf2out_assembly_start (void);
2785 static void dwarf2out_define (unsigned int, const char *);
2786 static void dwarf2out_undef (unsigned int, const char *);
2787 static void dwarf2out_start_source_file (unsigned, const char *);
2788 static void dwarf2out_end_source_file (unsigned);
2789 static void dwarf2out_function_decl (tree);
2790 static void dwarf2out_begin_block (unsigned, unsigned);
2791 static void dwarf2out_end_block (unsigned, unsigned);
2792 static bool dwarf2out_ignore_block (const_tree);
2793 static void dwarf2out_early_global_decl (tree);
2794 static void dwarf2out_late_global_decl (tree);
2795 static void dwarf2out_type_decl (tree, int);
2796 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2797 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2798 dw_die_ref);
2799 static void dwarf2out_abstract_function (tree);
2800 static void dwarf2out_var_location (rtx_insn *);
2801 static void dwarf2out_inline_entry (tree);
2802 static void dwarf2out_size_function (tree);
2803 static void dwarf2out_begin_function (tree);
2804 static void dwarf2out_end_function (unsigned int);
2805 static void dwarf2out_register_main_translation_unit (tree unit);
2806 static void dwarf2out_set_name (tree, tree);
2807 static void dwarf2out_register_external_die (tree decl, const char *sym,
2808 unsigned HOST_WIDE_INT off);
2809 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2810 unsigned HOST_WIDE_INT *off);
2812 /* The debug hooks structure. */
2814 const struct gcc_debug_hooks dwarf2_debug_hooks =
2816 dwarf2out_init,
2817 dwarf2out_finish,
2818 dwarf2out_early_finish,
2819 dwarf2out_assembly_start,
2820 dwarf2out_define,
2821 dwarf2out_undef,
2822 dwarf2out_start_source_file,
2823 dwarf2out_end_source_file,
2824 dwarf2out_begin_block,
2825 dwarf2out_end_block,
2826 dwarf2out_ignore_block,
2827 dwarf2out_source_line,
2828 dwarf2out_begin_prologue,
2829 #if VMS_DEBUGGING_INFO
2830 dwarf2out_vms_end_prologue,
2831 dwarf2out_vms_begin_epilogue,
2832 #else
2833 debug_nothing_int_charstar,
2834 debug_nothing_int_charstar,
2835 #endif
2836 dwarf2out_end_epilogue,
2837 dwarf2out_begin_function,
2838 dwarf2out_end_function, /* end_function */
2839 dwarf2out_register_main_translation_unit,
2840 dwarf2out_function_decl, /* function_decl */
2841 dwarf2out_early_global_decl,
2842 dwarf2out_late_global_decl,
2843 dwarf2out_type_decl, /* type_decl */
2844 dwarf2out_imported_module_or_decl,
2845 dwarf2out_die_ref_for_decl,
2846 dwarf2out_register_external_die,
2847 debug_nothing_tree, /* deferred_inline_function */
2848 /* The DWARF 2 backend tries to reduce debugging bloat by not
2849 emitting the abstract description of inline functions until
2850 something tries to reference them. */
2851 dwarf2out_abstract_function, /* outlining_inline_function */
2852 debug_nothing_rtx_code_label, /* label */
2853 debug_nothing_int, /* handle_pch */
2854 dwarf2out_var_location,
2855 dwarf2out_inline_entry, /* inline_entry */
2856 dwarf2out_size_function, /* size_function */
2857 dwarf2out_switch_text_section,
2858 dwarf2out_set_name,
2859 1, /* start_end_main_source_file */
2860 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2863 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2865 dwarf2out_init,
2866 debug_nothing_charstar,
2867 debug_nothing_charstar,
2868 dwarf2out_assembly_start,
2869 debug_nothing_int_charstar,
2870 debug_nothing_int_charstar,
2871 debug_nothing_int_charstar,
2872 debug_nothing_int,
2873 debug_nothing_int_int, /* begin_block */
2874 debug_nothing_int_int, /* end_block */
2875 debug_true_const_tree, /* ignore_block */
2876 dwarf2out_source_line, /* source_line */
2877 debug_nothing_int_int_charstar, /* begin_prologue */
2878 debug_nothing_int_charstar, /* end_prologue */
2879 debug_nothing_int_charstar, /* begin_epilogue */
2880 debug_nothing_int_charstar, /* end_epilogue */
2881 debug_nothing_tree, /* begin_function */
2882 debug_nothing_int, /* end_function */
2883 debug_nothing_tree, /* register_main_translation_unit */
2884 debug_nothing_tree, /* function_decl */
2885 debug_nothing_tree, /* early_global_decl */
2886 debug_nothing_tree, /* late_global_decl */
2887 debug_nothing_tree_int, /* type_decl */
2888 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2889 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2890 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2891 debug_nothing_tree, /* deferred_inline_function */
2892 debug_nothing_tree, /* outlining_inline_function */
2893 debug_nothing_rtx_code_label, /* label */
2894 debug_nothing_int, /* handle_pch */
2895 debug_nothing_rtx_insn, /* var_location */
2896 debug_nothing_tree, /* inline_entry */
2897 debug_nothing_tree, /* size_function */
2898 debug_nothing_void, /* switch_text_section */
2899 debug_nothing_tree_tree, /* set_name */
2900 0, /* start_end_main_source_file */
2901 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2904 /* NOTE: In the comments in this file, many references are made to
2905 "Debugging Information Entries". This term is abbreviated as `DIE'
2906 throughout the remainder of this file. */
2908 /* An internal representation of the DWARF output is built, and then
2909 walked to generate the DWARF debugging info. The walk of the internal
2910 representation is done after the entire program has been compiled.
2911 The types below are used to describe the internal representation. */
2913 /* Whether to put type DIEs into their own section .debug_types instead
2914 of making them part of the .debug_info section. Only supported for
2915 Dwarf V4 or higher and the user didn't disable them through
2916 -fno-debug-types-section. It is more efficient to put them in a
2917 separate comdat sections since the linker will then be able to
2918 remove duplicates. But not all tools support .debug_types sections
2919 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2920 it is DW_UT_type unit type in .debug_info section. For late LTO
2921 debug there should be almost no types emitted so avoid enabling
2922 -fdebug-types-section there. */
2924 #define use_debug_types (dwarf_version >= 4 \
2925 && flag_debug_types_section \
2926 && !in_lto_p)
2928 /* Various DIE's use offsets relative to the beginning of the
2929 .debug_info section to refer to each other. */
2931 typedef long int dw_offset;
2933 struct comdat_type_node;
2935 /* The entries in the line_info table more-or-less mirror the opcodes
2936 that are used in the real dwarf line table. Arrays of these entries
2937 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2938 supported. */
2940 enum dw_line_info_opcode {
2941 /* Emit DW_LNE_set_address; the operand is the label index. */
2942 LI_set_address,
2944 /* Emit a row to the matrix with the given line. This may be done
2945 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2946 special opcodes. */
2947 LI_set_line,
2949 /* Emit a DW_LNS_set_file. */
2950 LI_set_file,
2952 /* Emit a DW_LNS_set_column. */
2953 LI_set_column,
2955 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2956 LI_negate_stmt,
2958 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2959 LI_set_prologue_end,
2960 LI_set_epilogue_begin,
2962 /* Emit a DW_LNE_set_discriminator. */
2963 LI_set_discriminator,
2965 /* Output a Fixed Advance PC; the target PC is the label index; the
2966 base PC is the previous LI_adv_address or LI_set_address entry.
2967 We only use this when emitting debug views without assembler
2968 support, at explicit user request. Ideally, we should only use
2969 it when the offset might be zero but we can't tell: it's the only
2970 way to maybe change the PC without resetting the view number. */
2971 LI_adv_address
2974 typedef struct GTY(()) dw_line_info_struct {
2975 enum dw_line_info_opcode opcode;
2976 unsigned int val;
2977 } dw_line_info_entry;
2980 struct GTY(()) dw_line_info_table {
2981 /* The label that marks the end of this section. */
2982 const char *end_label;
2984 /* The values for the last row of the matrix, as collected in the table.
2985 These are used to minimize the changes to the next row. */
2986 unsigned int file_num;
2987 unsigned int line_num;
2988 unsigned int column_num;
2989 int discrim_num;
2990 bool is_stmt;
2991 bool in_use;
2993 /* This denotes the NEXT view number.
2995 If it is 0, it is known that the NEXT view will be the first view
2996 at the given PC.
2998 If it is -1, we're forcing the view number to be reset, e.g. at a
2999 function entry.
3001 The meaning of other nonzero values depends on whether we're
3002 computing views internally or leaving it for the assembler to do
3003 so. If we're emitting them internally, view denotes the view
3004 number since the last known advance of PC. If we're leaving it
3005 for the assembler, it denotes the LVU label number that we're
3006 going to ask the assembler to assign. */
3007 var_loc_view view;
3009 /* This counts the number of symbolic views emitted in this table
3010 since the latest view reset. Its max value, over all tables,
3011 sets symview_upper_bound. */
3012 var_loc_view symviews_since_reset;
3014 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3015 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3016 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3017 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3019 vec<dw_line_info_entry, va_gc> *entries;
3022 /* This is an upper bound for view numbers that the assembler may
3023 assign to symbolic views output in this translation. It is used to
3024 decide how big a field to use to represent view numbers in
3025 symview-classed attributes. */
3027 static var_loc_view symview_upper_bound;
3029 /* If we're keep track of location views and their reset points, and
3030 INSN is a reset point (i.e., it necessarily advances the PC), mark
3031 the next view in TABLE as reset. */
3033 static void
3034 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3036 if (!debug_internal_reset_location_views)
3037 return;
3039 /* Maybe turn (part of?) this test into a default target hook. */
3040 int reset = 0;
3042 if (targetm.reset_location_view)
3043 reset = targetm.reset_location_view (insn);
3045 if (reset)
3047 else if (JUMP_TABLE_DATA_P (insn))
3048 reset = 1;
3049 else if (GET_CODE (insn) == USE
3050 || GET_CODE (insn) == CLOBBER
3051 || GET_CODE (insn) == ASM_INPUT
3052 || asm_noperands (insn) >= 0)
3054 else if (get_attr_min_length (insn) > 0)
3055 reset = 1;
3057 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3058 RESET_NEXT_VIEW (table->view);
3061 /* Each DIE attribute has a field specifying the attribute kind,
3062 a link to the next attribute in the chain, and an attribute value.
3063 Attributes are typically linked below the DIE they modify. */
3065 typedef struct GTY(()) dw_attr_struct {
3066 enum dwarf_attribute dw_attr;
3067 dw_val_node dw_attr_val;
3069 dw_attr_node;
3072 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3073 The children of each node form a circular list linked by
3074 die_sib. die_child points to the node *before* the "first" child node. */
3076 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3077 union die_symbol_or_type_node
3079 const char * GTY ((tag ("0"))) die_symbol;
3080 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3082 GTY ((desc ("%0.comdat_type_p"))) die_id;
3083 vec<dw_attr_node, va_gc> *die_attr;
3084 dw_die_ref die_parent;
3085 dw_die_ref die_child;
3086 dw_die_ref die_sib;
3087 dw_die_ref die_definition; /* ref from a specification to its definition */
3088 dw_offset die_offset;
3089 unsigned long die_abbrev;
3090 int die_mark;
3091 unsigned int decl_id;
3092 enum dwarf_tag die_tag;
3093 /* Die is used and must not be pruned as unused. */
3094 BOOL_BITFIELD die_perennial_p : 1;
3095 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3096 /* For an external ref to die_symbol if die_offset contains an extra
3097 offset to that symbol. */
3098 BOOL_BITFIELD with_offset : 1;
3099 /* Whether this DIE was removed from the DIE tree, for example via
3100 prune_unused_types. We don't consider those present from the
3101 DIE lookup routines. */
3102 BOOL_BITFIELD removed : 1;
3103 /* Lots of spare bits. */
3105 die_node;
3107 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3108 static bool early_dwarf;
3109 static bool early_dwarf_finished;
3110 class set_early_dwarf {
3111 public:
3112 bool saved;
3113 set_early_dwarf () : saved(early_dwarf)
3115 gcc_assert (! early_dwarf_finished);
3116 early_dwarf = true;
3118 ~set_early_dwarf () { early_dwarf = saved; }
3121 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3122 #define FOR_EACH_CHILD(die, c, expr) do { \
3123 c = die->die_child; \
3124 if (c) do { \
3125 c = c->die_sib; \
3126 expr; \
3127 } while (c != die->die_child); \
3128 } while (0)
3130 /* The pubname structure */
3132 typedef struct GTY(()) pubname_struct {
3133 dw_die_ref die;
3134 const char *name;
3136 pubname_entry;
3139 struct GTY(()) dw_ranges {
3140 const char *label;
3141 /* If this is positive, it's a block number, otherwise it's a
3142 bitwise-negated index into dw_ranges_by_label. */
3143 int num;
3144 /* Index for the range list for DW_FORM_rnglistx. */
3145 unsigned int idx : 31;
3146 /* True if this range might be possibly in a different section
3147 from previous entry. */
3148 unsigned int maybe_new_sec : 1;
3151 /* A structure to hold a macinfo entry. */
3153 typedef struct GTY(()) macinfo_struct {
3154 unsigned char code;
3155 unsigned HOST_WIDE_INT lineno;
3156 const char *info;
3158 macinfo_entry;
3161 struct GTY(()) dw_ranges_by_label {
3162 const char *begin;
3163 const char *end;
3166 /* The comdat type node structure. */
3167 struct GTY(()) comdat_type_node
3169 dw_die_ref root_die;
3170 dw_die_ref type_die;
3171 dw_die_ref skeleton_die;
3172 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3173 comdat_type_node *next;
3176 /* A list of DIEs for which we can't determine ancestry (parent_die
3177 field) just yet. Later in dwarf2out_finish we will fill in the
3178 missing bits. */
3179 typedef struct GTY(()) limbo_die_struct {
3180 dw_die_ref die;
3181 /* The tree for which this DIE was created. We use this to
3182 determine ancestry later. */
3183 tree created_for;
3184 struct limbo_die_struct *next;
3186 limbo_die_node;
3188 typedef struct skeleton_chain_struct
3190 dw_die_ref old_die;
3191 dw_die_ref new_die;
3192 struct skeleton_chain_struct *parent;
3194 skeleton_chain_node;
3196 /* Define a macro which returns nonzero for a TYPE_DECL which was
3197 implicitly generated for a type.
3199 Note that, unlike the C front-end (which generates a NULL named
3200 TYPE_DECL node for each complete tagged type, each array type,
3201 and each function type node created) the C++ front-end generates
3202 a _named_ TYPE_DECL node for each tagged type node created.
3203 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3204 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3205 front-end, but for each type, tagged or not. */
3207 #define TYPE_DECL_IS_STUB(decl) \
3208 (DECL_NAME (decl) == NULL_TREE \
3209 || (DECL_ARTIFICIAL (decl) \
3210 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3211 /* This is necessary for stub decls that \
3212 appear in nested inline functions. */ \
3213 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3214 && (decl_ultimate_origin (decl) \
3215 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3217 /* Information concerning the compilation unit's programming
3218 language, and compiler version. */
3220 /* Fixed size portion of the DWARF compilation unit header. */
3221 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3222 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3223 + (dwarf_version >= 5 ? 4 : 3))
3225 /* Fixed size portion of the DWARF comdat type unit header. */
3226 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3227 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3228 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3230 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3231 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3232 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3234 /* Fixed size portion of public names info. */
3235 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3237 /* Fixed size portion of the address range info. */
3238 #define DWARF_ARANGES_HEADER_SIZE \
3239 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3240 DWARF2_ADDR_SIZE * 2) \
3241 - DWARF_INITIAL_LENGTH_SIZE)
3243 /* Size of padding portion in the address range info. It must be
3244 aligned to twice the pointer size. */
3245 #define DWARF_ARANGES_PAD_SIZE \
3246 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3247 DWARF2_ADDR_SIZE * 2) \
3248 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3250 /* Use assembler line directives if available. */
3251 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3252 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3253 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3254 #else
3255 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3256 #endif
3257 #endif
3259 /* Use assembler views in line directives if available. */
3260 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3261 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3262 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3263 #else
3264 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3265 #endif
3266 #endif
3268 /* Return true if GCC configure detected assembler support for .loc. */
3270 bool
3271 dwarf2out_default_as_loc_support (void)
3273 return DWARF2_ASM_LINE_DEBUG_INFO;
3274 #if (GCC_VERSION >= 3000)
3275 # undef DWARF2_ASM_LINE_DEBUG_INFO
3276 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3277 #endif
3280 /* Return true if GCC configure detected assembler support for views
3281 in .loc directives. */
3283 bool
3284 dwarf2out_default_as_locview_support (void)
3286 return DWARF2_ASM_VIEW_DEBUG_INFO;
3287 #if (GCC_VERSION >= 3000)
3288 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3289 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3290 #endif
3293 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3294 view computation, and it refers to a view identifier for which we
3295 will not emit a label because it is known to map to a view number
3296 zero. We won't allocate the bitmap if we're not using assembler
3297 support for location views, but we have to make the variable
3298 visible for GGC and for code that will be optimized out for lack of
3299 support but that's still parsed and compiled. We could abstract it
3300 out with macros, but it's not worth it. */
3301 static GTY(()) bitmap zero_view_p;
3303 /* Evaluate to TRUE iff N is known to identify the first location view
3304 at its PC. When not using assembler location view computation,
3305 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3306 and views label numbers recorded in it are the ones known to be
3307 zero. */
3308 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3309 || (N) == (var_loc_view)-1 \
3310 || (zero_view_p \
3311 && bitmap_bit_p (zero_view_p, (N))))
3313 /* Return true iff we're to emit .loc directives for the assembler to
3314 generate line number sections.
3316 When we're not emitting views, all we need from the assembler is
3317 support for .loc directives.
3319 If we are emitting views, we can only use the assembler's .loc
3320 support if it also supports views.
3322 When the compiler is emitting the line number programs and
3323 computing view numbers itself, it resets view numbers at known PC
3324 changes and counts from that, and then it emits view numbers as
3325 literal constants in locviewlists. There are cases in which the
3326 compiler is not sure about PC changes, e.g. when extra alignment is
3327 requested for a label. In these cases, the compiler may not reset
3328 the view counter, and the potential PC advance in the line number
3329 program will use an opcode that does not reset the view counter
3330 even if the PC actually changes, so that compiler and debug info
3331 consumer can keep view numbers in sync.
3333 When the compiler defers view computation to the assembler, it
3334 emits symbolic view numbers in locviewlists, with the exception of
3335 views known to be zero (forced resets, or reset after
3336 compiler-visible PC changes): instead of emitting symbols for
3337 these, we emit literal zero and assert the assembler agrees with
3338 the compiler's assessment. We could use symbolic views everywhere,
3339 instead of special-casing zero views, but then we'd be unable to
3340 optimize out locviewlists that contain only zeros. */
3342 static bool
3343 output_asm_line_debug_info (void)
3345 return (dwarf2out_as_loc_support
3346 && (dwarf2out_as_locview_support
3347 || !debug_variable_location_views));
3350 static bool asm_outputs_debug_line_str (void);
3352 /* Minimum line offset in a special line info. opcode.
3353 This value was chosen to give a reasonable range of values. */
3354 #define DWARF_LINE_BASE -10
3356 /* First special line opcode - leave room for the standard opcodes. */
3357 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3359 /* Range of line offsets in a special line info. opcode. */
3360 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3362 /* Flag that indicates the initial value of the is_stmt_start flag.
3363 In the present implementation, we do not mark any lines as
3364 the beginning of a source statement, because that information
3365 is not made available by the GCC front-end. */
3366 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3368 /* Maximum number of operations per instruction bundle. */
3369 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3370 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3371 #endif
3373 /* This location is used by calc_die_sizes() to keep track
3374 the offset of each DIE within the .debug_info section. */
3375 static unsigned long next_die_offset;
3377 /* Record the root of the DIE's built for the current compilation unit. */
3378 static GTY(()) dw_die_ref single_comp_unit_die;
3380 /* A list of type DIEs that have been separated into comdat sections. */
3381 static GTY(()) comdat_type_node *comdat_type_list;
3383 /* A list of CU DIEs that have been separated. */
3384 static GTY(()) limbo_die_node *cu_die_list;
3386 /* A list of DIEs with a NULL parent waiting to be relocated. */
3387 static GTY(()) limbo_die_node *limbo_die_list;
3389 /* A list of DIEs for which we may have to generate
3390 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3391 static GTY(()) limbo_die_node *deferred_asm_name;
3393 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3395 typedef const char *compare_type;
3397 static hashval_t hash (dwarf_file_data *);
3398 static bool equal (dwarf_file_data *, const char *);
3401 /* Filenames referenced by this compilation unit. */
3402 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3404 struct decl_die_hasher : ggc_ptr_hash<die_node>
3406 typedef tree compare_type;
3408 static hashval_t hash (die_node *);
3409 static bool equal (die_node *, tree);
3411 /* A hash table of references to DIE's that describe declarations.
3412 The key is a DECL_UID() which is a unique number identifying each decl. */
3413 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3415 struct GTY ((for_user)) variable_value_struct {
3416 unsigned int decl_id;
3417 vec<dw_die_ref, va_gc> *dies;
3420 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3422 typedef tree compare_type;
3424 static hashval_t hash (variable_value_struct *);
3425 static bool equal (variable_value_struct *, tree);
3427 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3428 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3429 DECL_CONTEXT of the referenced VAR_DECLs. */
3430 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3432 struct block_die_hasher : ggc_ptr_hash<die_struct>
3434 static hashval_t hash (die_struct *);
3435 static bool equal (die_struct *, die_struct *);
3438 /* A hash table of references to DIE's that describe COMMON blocks.
3439 The key is DECL_UID() ^ die_parent. */
3440 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3442 typedef struct GTY(()) die_arg_entry_struct {
3443 dw_die_ref die;
3444 tree arg;
3445 } die_arg_entry;
3448 /* Node of the variable location list. */
3449 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3450 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3451 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3452 in mode of the EXPR_LIST node and first EXPR_LIST operand
3453 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3454 location or NULL for padding. For larger bitsizes,
3455 mode is 0 and first operand is a CONCAT with bitsize
3456 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3457 NULL as second operand. */
3458 rtx GTY (()) loc;
3459 const char * GTY (()) label;
3460 struct var_loc_node * GTY (()) next;
3461 var_loc_view view;
3464 /* Variable location list. */
3465 struct GTY ((for_user)) var_loc_list_def {
3466 struct var_loc_node * GTY (()) first;
3468 /* Pointer to the last but one or last element of the
3469 chained list. If the list is empty, both first and
3470 last are NULL, if the list contains just one node
3471 or the last node certainly is not redundant, it points
3472 to the last node, otherwise points to the last but one.
3473 Do not mark it for GC because it is marked through the chain. */
3474 struct var_loc_node * GTY ((skip ("%h"))) last;
3476 /* Pointer to the last element before section switch,
3477 if NULL, either sections weren't switched or first
3478 is after section switch. */
3479 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3481 /* DECL_UID of the variable decl. */
3482 unsigned int decl_id;
3484 typedef struct var_loc_list_def var_loc_list;
3486 /* Call argument location list. */
3487 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3488 rtx GTY (()) call_arg_loc_note;
3489 const char * GTY (()) label;
3490 tree GTY (()) block;
3491 bool tail_call_p;
3492 rtx GTY (()) symbol_ref;
3493 struct call_arg_loc_node * GTY (()) next;
3497 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3499 typedef const_tree compare_type;
3501 static hashval_t hash (var_loc_list *);
3502 static bool equal (var_loc_list *, const_tree);
3505 /* Table of decl location linked lists. */
3506 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3508 /* Head and tail of call_arg_loc chain. */
3509 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3510 static struct call_arg_loc_node *call_arg_loc_last;
3512 /* Number of call sites in the current function. */
3513 static int call_site_count = -1;
3514 /* Number of tail call sites in the current function. */
3515 static int tail_call_site_count = -1;
3517 /* A cached location list. */
3518 struct GTY ((for_user)) cached_dw_loc_list_def {
3519 /* The DECL_UID of the decl that this entry describes. */
3520 unsigned int decl_id;
3522 /* The cached location list. */
3523 dw_loc_list_ref loc_list;
3525 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3527 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3530 typedef const_tree compare_type;
3532 static hashval_t hash (cached_dw_loc_list *);
3533 static bool equal (cached_dw_loc_list *, const_tree);
3536 /* Table of cached location lists. */
3537 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3539 /* A vector of references to DIE's that are uniquely identified by their tag,
3540 presence/absence of children DIE's, and list of attribute/value pairs. */
3541 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3543 /* A hash map to remember the stack usage for DWARF procedures. The value
3544 stored is the stack size difference between before the DWARF procedure
3545 invokation and after it returned. In other words, for a DWARF procedure
3546 that consumes N stack slots and that pushes M ones, this stores M - N. */
3547 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3549 /* A global counter for generating labels for line number data. */
3550 static unsigned int line_info_label_num;
3552 /* The current table to which we should emit line number information
3553 for the current function. This will be set up at the beginning of
3554 assembly for the function. */
3555 static GTY(()) dw_line_info_table *cur_line_info_table;
3557 /* The two default tables of line number info. */
3558 static GTY(()) dw_line_info_table *text_section_line_info;
3559 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3561 /* The set of all non-default tables of line number info. */
3562 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3564 /* A flag to tell pubnames/types export if there is an info section to
3565 refer to. */
3566 static bool info_section_emitted;
3568 /* A pointer to the base of a table that contains a list of publicly
3569 accessible names. */
3570 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3572 /* A pointer to the base of a table that contains a list of publicly
3573 accessible types. */
3574 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3576 /* A pointer to the base of a table that contains a list of macro
3577 defines/undefines (and file start/end markers). */
3578 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3580 /* True if .debug_macinfo or .debug_macros section is going to be
3581 emitted. */
3582 #define have_macinfo \
3583 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3584 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3585 && !macinfo_table->is_empty ())
3587 /* Vector of dies for which we should generate .debug_ranges info. */
3588 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3590 /* Vector of pairs of labels referenced in ranges_table. */
3591 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3593 /* Whether we have location lists that need outputting */
3594 static GTY(()) bool have_location_lists;
3596 /* Unique label counter. */
3597 static GTY(()) unsigned int loclabel_num;
3599 /* Unique label counter for point-of-call tables. */
3600 static GTY(()) unsigned int poc_label_num;
3602 /* The last file entry emitted by maybe_emit_file(). */
3603 static GTY(()) struct dwarf_file_data * last_emitted_file;
3605 /* Number of internal labels generated by gen_internal_sym(). */
3606 static GTY(()) int label_num;
3608 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3610 /* Instances of generic types for which we need to generate debug
3611 info that describe their generic parameters and arguments. That
3612 generation needs to happen once all types are properly laid out so
3613 we do it at the end of compilation. */
3614 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3616 /* Offset from the "steady-state frame pointer" to the frame base,
3617 within the current function. */
3618 static poly_int64 frame_pointer_fb_offset;
3619 static bool frame_pointer_fb_offset_valid;
3621 static vec<dw_die_ref> base_types;
3623 /* Flags to represent a set of attribute classes for attributes that represent
3624 a scalar value (bounds, pointers, ...). */
3625 enum dw_scalar_form
3627 dw_scalar_form_constant = 0x01,
3628 dw_scalar_form_exprloc = 0x02,
3629 dw_scalar_form_reference = 0x04
3632 /* Forward declarations for functions defined in this file. */
3634 static int is_pseudo_reg (const_rtx);
3635 static tree type_main_variant (tree);
3636 static int is_tagged_type (const_tree);
3637 static const char *dwarf_tag_name (unsigned);
3638 static const char *dwarf_attr_name (unsigned);
3639 static const char *dwarf_form_name (unsigned);
3640 static tree decl_ultimate_origin (const_tree);
3641 static tree decl_class_context (tree);
3642 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3643 static inline enum dw_val_class AT_class (dw_attr_node *);
3644 static inline unsigned int AT_index (dw_attr_node *);
3645 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3646 static inline unsigned AT_flag (dw_attr_node *);
3647 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3648 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3649 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3650 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3651 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3652 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3653 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3654 unsigned int, unsigned char *);
3655 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3656 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3657 static inline const char *AT_string (dw_attr_node *);
3658 static enum dwarf_form AT_string_form (dw_attr_node *);
3659 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3660 static void add_AT_specification (dw_die_ref, dw_die_ref);
3661 static inline dw_die_ref AT_ref (dw_attr_node *);
3662 static inline int AT_ref_external (dw_attr_node *);
3663 static inline void set_AT_ref_external (dw_attr_node *, int);
3664 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3665 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3666 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3667 dw_loc_list_ref);
3668 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3669 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3670 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3671 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3672 static void remove_addr_table_entry (addr_table_entry *);
3673 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3674 static inline rtx AT_addr (dw_attr_node *);
3675 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3676 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3677 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3678 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3679 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3680 unsigned long, bool);
3681 static inline const char *AT_lbl (dw_attr_node *);
3682 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3683 static const char *get_AT_low_pc (dw_die_ref);
3684 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3685 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3686 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3687 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3688 static bool is_c (void);
3689 static bool is_cxx (void);
3690 static bool is_cxx (const_tree);
3691 static bool is_fortran (void);
3692 static bool is_ada (void);
3693 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3694 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3695 static void add_child_die (dw_die_ref, dw_die_ref);
3696 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3697 static dw_die_ref lookup_type_die (tree);
3698 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3699 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3700 static void equate_type_number_to_die (tree, dw_die_ref);
3701 static dw_die_ref lookup_decl_die (tree);
3702 static var_loc_list *lookup_decl_loc (const_tree);
3703 static void equate_decl_number_to_die (tree, dw_die_ref);
3704 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3705 static void print_spaces (FILE *);
3706 static void print_die (dw_die_ref, FILE *);
3707 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3708 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3709 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3710 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3711 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3712 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3713 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3714 struct md5_ctx *, int *);
3715 struct checksum_attributes;
3716 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3717 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3718 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3719 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3720 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3721 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3722 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3723 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3724 static int is_type_die (dw_die_ref);
3725 static inline bool is_template_instantiation (dw_die_ref);
3726 static int is_declaration_die (dw_die_ref);
3727 static int should_move_die_to_comdat (dw_die_ref);
3728 static dw_die_ref clone_as_declaration (dw_die_ref);
3729 static dw_die_ref clone_die (dw_die_ref);
3730 static dw_die_ref clone_tree (dw_die_ref);
3731 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3732 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3733 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3734 static dw_die_ref generate_skeleton (dw_die_ref);
3735 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3736 dw_die_ref,
3737 dw_die_ref);
3738 static void break_out_comdat_types (dw_die_ref);
3739 static void copy_decls_for_unworthy_types (dw_die_ref);
3741 static void add_sibling_attributes (dw_die_ref);
3742 static void output_location_lists (dw_die_ref);
3743 static int constant_size (unsigned HOST_WIDE_INT);
3744 static unsigned long size_of_die (dw_die_ref);
3745 static void calc_die_sizes (dw_die_ref);
3746 static void calc_base_type_die_sizes (void);
3747 static void mark_dies (dw_die_ref);
3748 static void unmark_dies (dw_die_ref);
3749 static void unmark_all_dies (dw_die_ref);
3750 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3751 static unsigned long size_of_aranges (void);
3752 static enum dwarf_form value_format (dw_attr_node *);
3753 static void output_value_format (dw_attr_node *);
3754 static void output_abbrev_section (void);
3755 static void output_die_abbrevs (unsigned long, dw_die_ref);
3756 static void output_die (dw_die_ref);
3757 static void output_compilation_unit_header (enum dwarf_unit_type);
3758 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3759 static void output_comdat_type_unit (comdat_type_node *, bool);
3760 static const char *dwarf2_name (tree, int);
3761 static void add_pubname (tree, dw_die_ref);
3762 static void add_enumerator_pubname (const char *, dw_die_ref);
3763 static void add_pubname_string (const char *, dw_die_ref);
3764 static void add_pubtype (tree, dw_die_ref);
3765 static void output_pubnames (vec<pubname_entry, va_gc> *);
3766 static void output_aranges (void);
3767 static unsigned int add_ranges (const_tree, bool = false);
3768 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3769 bool *, bool);
3770 static void output_ranges (void);
3771 static dw_line_info_table *new_line_info_table (void);
3772 static void output_line_info (bool);
3773 static void output_file_names (void);
3774 static dw_die_ref base_type_die (tree, bool);
3775 static int is_base_type (tree);
3776 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3777 static int decl_quals (const_tree);
3778 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3779 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3780 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3781 static unsigned int dbx_reg_number (const_rtx);
3782 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3783 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3784 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3785 enum var_init_status);
3786 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3787 enum var_init_status);
3788 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3789 enum var_init_status);
3790 static int is_based_loc (const_rtx);
3791 static bool resolve_one_addr (rtx *);
3792 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3793 enum var_init_status);
3794 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3795 enum var_init_status);
3796 struct loc_descr_context;
3797 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3798 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3799 static dw_loc_list_ref loc_list_from_tree (tree, int,
3800 struct loc_descr_context *);
3801 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3802 struct loc_descr_context *);
3803 static tree field_type (const_tree);
3804 static unsigned int simple_type_align_in_bits (const_tree);
3805 static unsigned int simple_decl_align_in_bits (const_tree);
3806 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3807 struct vlr_context;
3808 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3809 HOST_WIDE_INT *);
3810 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3811 dw_loc_list_ref);
3812 static void add_data_member_location_attribute (dw_die_ref, tree,
3813 struct vlr_context *);
3814 static bool add_const_value_attribute (dw_die_ref, rtx);
3815 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3816 static void insert_wide_int (const wide_int &, unsigned char *, int);
3817 static void insert_float (const_rtx, unsigned char *);
3818 static rtx rtl_for_decl_location (tree);
3819 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3820 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3821 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3822 static void add_name_attribute (dw_die_ref, const char *);
3823 static void add_desc_attribute (dw_die_ref, tree);
3824 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3825 static void add_comp_dir_attribute (dw_die_ref);
3826 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3827 struct loc_descr_context *);
3828 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3829 struct loc_descr_context *);
3830 static void add_subscript_info (dw_die_ref, tree, bool);
3831 static void add_byte_size_attribute (dw_die_ref, tree);
3832 static void add_alignment_attribute (dw_die_ref, tree);
3833 static void add_bit_offset_attribute (dw_die_ref, tree);
3834 static void add_bit_size_attribute (dw_die_ref, tree);
3835 static void add_prototyped_attribute (dw_die_ref, tree);
3836 static void add_abstract_origin_attribute (dw_die_ref, tree);
3837 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3838 static void add_src_coords_attributes (dw_die_ref, tree);
3839 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3840 static void add_discr_value (dw_die_ref, dw_discr_value *);
3841 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3842 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3843 static dw_die_ref scope_die_for (tree, dw_die_ref);
3844 static inline int local_scope_p (dw_die_ref);
3845 static inline int class_scope_p (dw_die_ref);
3846 static inline int class_or_namespace_scope_p (dw_die_ref);
3847 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3848 static void add_calling_convention_attribute (dw_die_ref, tree);
3849 static const char *type_tag (const_tree);
3850 static tree member_declared_type (const_tree);
3851 #if 0
3852 static const char *decl_start_label (tree);
3853 #endif
3854 static void gen_array_type_die (tree, dw_die_ref);
3855 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3856 #if 0
3857 static void gen_entry_point_die (tree, dw_die_ref);
3858 #endif
3859 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3860 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3861 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3862 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3863 static void gen_formal_types_die (tree, dw_die_ref);
3864 static void gen_subprogram_die (tree, dw_die_ref);
3865 static void gen_variable_die (tree, tree, dw_die_ref);
3866 static void gen_const_die (tree, dw_die_ref);
3867 static void gen_label_die (tree, dw_die_ref);
3868 static void gen_lexical_block_die (tree, dw_die_ref);
3869 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3870 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3871 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3872 static dw_die_ref gen_compile_unit_die (const char *);
3873 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3874 static void gen_member_die (tree, dw_die_ref);
3875 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3876 enum debug_info_usage);
3877 static void gen_subroutine_type_die (tree, dw_die_ref);
3878 static void gen_typedef_die (tree, dw_die_ref);
3879 static void gen_type_die (tree, dw_die_ref);
3880 static void gen_block_die (tree, dw_die_ref);
3881 static void decls_for_scope (tree, dw_die_ref, bool = true);
3882 static bool is_naming_typedef_decl (const_tree);
3883 static inline dw_die_ref get_context_die (tree);
3884 static void gen_namespace_die (tree, dw_die_ref);
3885 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3886 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3887 static dw_die_ref force_decl_die (tree);
3888 static dw_die_ref force_type_die (tree);
3889 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3890 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3891 static struct dwarf_file_data * lookup_filename (const char *);
3892 static void retry_incomplete_types (void);
3893 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3894 static void gen_generic_params_dies (tree);
3895 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3896 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3897 static void splice_child_die (dw_die_ref, dw_die_ref);
3898 static int file_info_cmp (const void *, const void *);
3899 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3900 const char *, var_loc_view, const char *);
3901 static void output_loc_list (dw_loc_list_ref);
3902 static char *gen_internal_sym (const char *);
3903 static bool want_pubnames (void);
3905 static void prune_unmark_dies (dw_die_ref);
3906 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3907 static void prune_unused_types_mark (dw_die_ref, int);
3908 static void prune_unused_types_walk (dw_die_ref);
3909 static void prune_unused_types_walk_attribs (dw_die_ref);
3910 static void prune_unused_types_prune (dw_die_ref);
3911 static void prune_unused_types (void);
3912 static int maybe_emit_file (struct dwarf_file_data *fd);
3913 static inline const char *AT_vms_delta1 (dw_attr_node *);
3914 static inline const char *AT_vms_delta2 (dw_attr_node *);
3915 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3916 const char *, const char *);
3917 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3918 static void gen_remaining_tmpl_value_param_die_attribute (void);
3919 static bool generic_type_p (tree);
3920 static void schedule_generic_params_dies_gen (tree t);
3921 static void gen_scheduled_generic_parms_dies (void);
3922 static void resolve_variable_values (void);
3924 static const char *comp_dir_string (void);
3926 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3928 /* enum for tracking thread-local variables whose address is really an offset
3929 relative to the TLS pointer, which will need link-time relocation, but will
3930 not need relocation by the DWARF consumer. */
3932 enum dtprel_bool
3934 dtprel_false = 0,
3935 dtprel_true = 1
3938 /* Return the operator to use for an address of a variable. For dtprel_true, we
3939 use DW_OP_const*. For regular variables, which need both link-time
3940 relocation and consumer-level relocation (e.g., to account for shared objects
3941 loaded at a random address), we use DW_OP_addr*. */
3943 static inline enum dwarf_location_atom
3944 dw_addr_op (enum dtprel_bool dtprel)
3946 if (dtprel == dtprel_true)
3947 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
3948 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3949 else
3950 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
3953 /* Return a pointer to a newly allocated address location description. If
3954 dwarf_split_debug_info is true, then record the address with the appropriate
3955 relocation. */
3956 static inline dw_loc_descr_ref
3957 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3959 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3961 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3962 ref->dw_loc_oprnd1.v.val_addr = addr;
3963 ref->dtprel = dtprel;
3964 if (dwarf_split_debug_info)
3965 ref->dw_loc_oprnd1.val_entry
3966 = add_addr_table_entry (addr,
3967 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3968 else
3969 ref->dw_loc_oprnd1.val_entry = NULL;
3971 return ref;
3974 /* Section names used to hold DWARF debugging information. */
3976 #ifndef DEBUG_INFO_SECTION
3977 #define DEBUG_INFO_SECTION ".debug_info"
3978 #endif
3979 #ifndef DEBUG_DWO_INFO_SECTION
3980 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3981 #endif
3982 #ifndef DEBUG_LTO_INFO_SECTION
3983 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3984 #endif
3985 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3986 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3987 #endif
3988 #ifndef DEBUG_ABBREV_SECTION
3989 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3990 #endif
3991 #ifndef DEBUG_LTO_ABBREV_SECTION
3992 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3993 #endif
3994 #ifndef DEBUG_DWO_ABBREV_SECTION
3995 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3996 #endif
3997 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3998 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3999 #endif
4000 #ifndef DEBUG_ARANGES_SECTION
4001 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4002 #endif
4003 #ifndef DEBUG_ADDR_SECTION
4004 #define DEBUG_ADDR_SECTION ".debug_addr"
4005 #endif
4006 #ifndef DEBUG_MACINFO_SECTION
4007 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4008 #endif
4009 #ifndef DEBUG_LTO_MACINFO_SECTION
4010 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4011 #endif
4012 #ifndef DEBUG_DWO_MACINFO_SECTION
4013 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4014 #endif
4015 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4016 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4017 #endif
4018 #ifndef DEBUG_MACRO_SECTION
4019 #define DEBUG_MACRO_SECTION ".debug_macro"
4020 #endif
4021 #ifndef DEBUG_LTO_MACRO_SECTION
4022 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4023 #endif
4024 #ifndef DEBUG_DWO_MACRO_SECTION
4025 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4026 #endif
4027 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4028 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4029 #endif
4030 #ifndef DEBUG_LINE_SECTION
4031 #define DEBUG_LINE_SECTION ".debug_line"
4032 #endif
4033 #ifndef DEBUG_LTO_LINE_SECTION
4034 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4035 #endif
4036 #ifndef DEBUG_DWO_LINE_SECTION
4037 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4038 #endif
4039 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4040 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4041 #endif
4042 #ifndef DEBUG_LOC_SECTION
4043 #define DEBUG_LOC_SECTION ".debug_loc"
4044 #endif
4045 #ifndef DEBUG_DWO_LOC_SECTION
4046 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4047 #endif
4048 #ifndef DEBUG_LOCLISTS_SECTION
4049 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4050 #endif
4051 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4052 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4053 #endif
4054 #ifndef DEBUG_PUBNAMES_SECTION
4055 #define DEBUG_PUBNAMES_SECTION \
4056 ((debug_generate_pub_sections == 2) \
4057 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4058 #endif
4059 #ifndef DEBUG_PUBTYPES_SECTION
4060 #define DEBUG_PUBTYPES_SECTION \
4061 ((debug_generate_pub_sections == 2) \
4062 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4063 #endif
4064 #ifndef DEBUG_STR_OFFSETS_SECTION
4065 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4066 #endif
4067 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4068 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4069 #endif
4070 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4071 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4072 #endif
4073 #ifndef DEBUG_STR_SECTION
4074 #define DEBUG_STR_SECTION ".debug_str"
4075 #endif
4076 #ifndef DEBUG_LTO_STR_SECTION
4077 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4078 #endif
4079 #ifndef DEBUG_STR_DWO_SECTION
4080 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4081 #endif
4082 #ifndef DEBUG_LTO_STR_DWO_SECTION
4083 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4084 #endif
4085 #ifndef DEBUG_RANGES_SECTION
4086 #define DEBUG_RANGES_SECTION ".debug_ranges"
4087 #endif
4088 #ifndef DEBUG_RNGLISTS_SECTION
4089 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4090 #endif
4091 #ifndef DEBUG_LINE_STR_SECTION
4092 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4093 #endif
4094 #ifndef DEBUG_LTO_LINE_STR_SECTION
4095 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4096 #endif
4098 /* Standard ELF section names for compiled code and data. */
4099 #ifndef TEXT_SECTION_NAME
4100 #define TEXT_SECTION_NAME ".text"
4101 #endif
4103 /* Section flags for .debug_str section. */
4104 #define DEBUG_STR_SECTION_FLAGS \
4105 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4106 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4107 : SECTION_DEBUG)
4109 /* Section flags for .debug_str.dwo section. */
4110 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4112 /* Attribute used to refer to the macro section. */
4113 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4114 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4116 /* Labels we insert at beginning sections we can reference instead of
4117 the section names themselves. */
4119 #ifndef TEXT_SECTION_LABEL
4120 #define TEXT_SECTION_LABEL "Ltext"
4121 #endif
4122 #ifndef COLD_TEXT_SECTION_LABEL
4123 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4124 #endif
4125 #ifndef DEBUG_LINE_SECTION_LABEL
4126 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4127 #endif
4128 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4129 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4130 #endif
4131 #ifndef DEBUG_INFO_SECTION_LABEL
4132 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4133 #endif
4134 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4135 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4136 #endif
4137 #ifndef DEBUG_ABBREV_SECTION_LABEL
4138 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4139 #endif
4140 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4141 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4142 #endif
4143 #ifndef DEBUG_ADDR_SECTION_LABEL
4144 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4145 #endif
4146 #ifndef DEBUG_LOC_SECTION_LABEL
4147 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4148 #endif
4149 #ifndef DEBUG_RANGES_SECTION_LABEL
4150 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4151 #endif
4152 #ifndef DEBUG_MACINFO_SECTION_LABEL
4153 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4154 #endif
4155 #ifndef DEBUG_MACRO_SECTION_LABEL
4156 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4157 #endif
4158 #define SKELETON_COMP_DIE_ABBREV 1
4159 #define SKELETON_TYPE_DIE_ABBREV 2
4161 /* Definitions of defaults for formats and names of various special
4162 (artificial) labels which may be generated within this file (when the -g
4163 options is used and DWARF2_DEBUGGING_INFO is in effect.
4164 If necessary, these may be overridden from within the tm.h file, but
4165 typically, overriding these defaults is unnecessary. */
4167 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4168 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4169 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4170 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4171 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4172 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4173 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4174 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4175 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4176 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4177 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4178 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4179 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4180 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4181 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4183 #ifndef TEXT_END_LABEL
4184 #define TEXT_END_LABEL "Letext"
4185 #endif
4186 #ifndef COLD_END_LABEL
4187 #define COLD_END_LABEL "Letext_cold"
4188 #endif
4189 #ifndef BLOCK_BEGIN_LABEL
4190 #define BLOCK_BEGIN_LABEL "LBB"
4191 #endif
4192 #ifndef BLOCK_INLINE_ENTRY_LABEL
4193 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4194 #endif
4195 #ifndef BLOCK_END_LABEL
4196 #define BLOCK_END_LABEL "LBE"
4197 #endif
4198 #ifndef LINE_CODE_LABEL
4199 #define LINE_CODE_LABEL "LM"
4200 #endif
4203 /* Return the root of the DIE's built for the current compilation unit. */
4204 static dw_die_ref
4205 comp_unit_die (void)
4207 if (!single_comp_unit_die)
4208 single_comp_unit_die = gen_compile_unit_die (NULL);
4209 return single_comp_unit_die;
4212 /* We allow a language front-end to designate a function that is to be
4213 called to "demangle" any name before it is put into a DIE. */
4215 static const char *(*demangle_name_func) (const char *);
4217 void
4218 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4220 demangle_name_func = func;
4223 /* Test if rtl node points to a pseudo register. */
4225 static inline int
4226 is_pseudo_reg (const_rtx rtl)
4228 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4229 || (GET_CODE (rtl) == SUBREG
4230 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4233 /* Return a reference to a type, with its const and volatile qualifiers
4234 removed. */
4236 static inline tree
4237 type_main_variant (tree type)
4239 type = TYPE_MAIN_VARIANT (type);
4241 /* ??? There really should be only one main variant among any group of
4242 variants of a given type (and all of the MAIN_VARIANT values for all
4243 members of the group should point to that one type) but sometimes the C
4244 front-end messes this up for array types, so we work around that bug
4245 here. */
4246 if (TREE_CODE (type) == ARRAY_TYPE)
4247 while (type != TYPE_MAIN_VARIANT (type))
4248 type = TYPE_MAIN_VARIANT (type);
4250 return type;
4253 /* Return nonzero if the given type node represents a tagged type. */
4255 static inline int
4256 is_tagged_type (const_tree type)
4258 enum tree_code code = TREE_CODE (type);
4260 return (code == RECORD_TYPE || code == UNION_TYPE
4261 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4264 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4266 static void
4267 get_ref_die_offset_label (char *label, dw_die_ref ref)
4269 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4272 /* Return die_offset of a DIE reference to a base type. */
4274 static unsigned long int
4275 get_base_type_offset (dw_die_ref ref)
4277 if (ref->die_offset)
4278 return ref->die_offset;
4279 if (comp_unit_die ()->die_abbrev)
4281 calc_base_type_die_sizes ();
4282 gcc_assert (ref->die_offset);
4284 return ref->die_offset;
4287 /* Return die_offset of a DIE reference other than base type. */
4289 static unsigned long int
4290 get_ref_die_offset (dw_die_ref ref)
4292 gcc_assert (ref->die_offset);
4293 return ref->die_offset;
4296 /* Convert a DIE tag into its string name. */
4298 static const char *
4299 dwarf_tag_name (unsigned int tag)
4301 const char *name = get_DW_TAG_name (tag);
4303 if (name != NULL)
4304 return name;
4306 return "DW_TAG_<unknown>";
4309 /* Convert a DWARF attribute code into its string name. */
4311 static const char *
4312 dwarf_attr_name (unsigned int attr)
4314 const char *name;
4316 switch (attr)
4318 #if VMS_DEBUGGING_INFO
4319 case DW_AT_HP_prologue:
4320 return "DW_AT_HP_prologue";
4321 #else
4322 case DW_AT_MIPS_loop_unroll_factor:
4323 return "DW_AT_MIPS_loop_unroll_factor";
4324 #endif
4326 #if VMS_DEBUGGING_INFO
4327 case DW_AT_HP_epilogue:
4328 return "DW_AT_HP_epilogue";
4329 #else
4330 case DW_AT_MIPS_stride:
4331 return "DW_AT_MIPS_stride";
4332 #endif
4335 name = get_DW_AT_name (attr);
4337 if (name != NULL)
4338 return name;
4340 return "DW_AT_<unknown>";
4343 /* Convert a DWARF value form code into its string name. */
4345 static const char *
4346 dwarf_form_name (unsigned int form)
4348 const char *name = get_DW_FORM_name (form);
4350 if (name != NULL)
4351 return name;
4353 return "DW_FORM_<unknown>";
4356 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4357 instance of an inlined instance of a decl which is local to an inline
4358 function, so we have to trace all of the way back through the origin chain
4359 to find out what sort of node actually served as the original seed for the
4360 given block. */
4362 static tree
4363 decl_ultimate_origin (const_tree decl)
4365 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4366 return NULL_TREE;
4368 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4369 we're trying to output the abstract instance of this function. */
4370 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4371 return NULL_TREE;
4373 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4374 most distant ancestor, this should never happen. */
4375 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4377 return DECL_ABSTRACT_ORIGIN (decl);
4380 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4381 of a virtual function may refer to a base class, so we check the 'this'
4382 parameter. */
4384 static tree
4385 decl_class_context (tree decl)
4387 tree context = NULL_TREE;
4389 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4390 context = DECL_CONTEXT (decl);
4391 else
4392 context = TYPE_MAIN_VARIANT
4393 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4395 if (context && !TYPE_P (context))
4396 context = NULL_TREE;
4398 return context;
4401 /* Add an attribute/value pair to a DIE. */
4403 static inline void
4404 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4406 /* Maybe this should be an assert? */
4407 if (die == NULL)
4408 return;
4410 if (flag_checking)
4412 /* Check we do not add duplicate attrs. Can't use get_AT here
4413 because that recurses to the specification/abstract origin DIE. */
4414 dw_attr_node *a;
4415 unsigned ix;
4416 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4417 gcc_assert (a->dw_attr != attr->dw_attr);
4420 vec_safe_reserve (die->die_attr, 1);
4421 vec_safe_push (die->die_attr, *attr);
4424 static inline enum dw_val_class
4425 AT_class (dw_attr_node *a)
4427 return a->dw_attr_val.val_class;
4430 /* Return the index for any attribute that will be referenced with a
4431 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4432 indices are stored in dw_attr_val.v.val_str for reference counting
4433 pruning. */
4435 static inline unsigned int
4436 AT_index (dw_attr_node *a)
4438 if (AT_class (a) == dw_val_class_str)
4439 return a->dw_attr_val.v.val_str->index;
4440 else if (a->dw_attr_val.val_entry != NULL)
4441 return a->dw_attr_val.val_entry->index;
4442 return NOT_INDEXED;
4445 /* Add a flag value attribute to a DIE. */
4447 static inline void
4448 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4450 dw_attr_node attr;
4452 attr.dw_attr = attr_kind;
4453 attr.dw_attr_val.val_class = dw_val_class_flag;
4454 attr.dw_attr_val.val_entry = NULL;
4455 attr.dw_attr_val.v.val_flag = flag;
4456 add_dwarf_attr (die, &attr);
4459 static inline unsigned
4460 AT_flag (dw_attr_node *a)
4462 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4463 return a->dw_attr_val.v.val_flag;
4466 /* Add a signed integer attribute value to a DIE. */
4468 static inline void
4469 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4471 dw_attr_node attr;
4473 attr.dw_attr = attr_kind;
4474 attr.dw_attr_val.val_class = dw_val_class_const;
4475 attr.dw_attr_val.val_entry = NULL;
4476 attr.dw_attr_val.v.val_int = int_val;
4477 add_dwarf_attr (die, &attr);
4480 static inline HOST_WIDE_INT
4481 AT_int (dw_attr_node *a)
4483 gcc_assert (a && (AT_class (a) == dw_val_class_const
4484 || AT_class (a) == dw_val_class_const_implicit));
4485 return a->dw_attr_val.v.val_int;
4488 /* Add an unsigned integer attribute value to a DIE. */
4490 static inline void
4491 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4492 unsigned HOST_WIDE_INT unsigned_val)
4494 dw_attr_node attr;
4496 attr.dw_attr = attr_kind;
4497 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4498 attr.dw_attr_val.val_entry = NULL;
4499 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4500 add_dwarf_attr (die, &attr);
4503 static inline unsigned HOST_WIDE_INT
4504 AT_unsigned (dw_attr_node *a)
4506 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4507 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4508 return a->dw_attr_val.v.val_unsigned;
4511 /* Add an unsigned wide integer attribute value to a DIE. */
4513 static inline void
4514 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4515 const wide_int& w)
4517 dw_attr_node attr;
4519 attr.dw_attr = attr_kind;
4520 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4521 attr.dw_attr_val.val_entry = NULL;
4522 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4523 *attr.dw_attr_val.v.val_wide = w;
4524 add_dwarf_attr (die, &attr);
4527 /* Add an unsigned double integer attribute value to a DIE. */
4529 static inline void
4530 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4531 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4533 dw_attr_node attr;
4535 attr.dw_attr = attr_kind;
4536 attr.dw_attr_val.val_class = dw_val_class_const_double;
4537 attr.dw_attr_val.val_entry = NULL;
4538 attr.dw_attr_val.v.val_double.high = high;
4539 attr.dw_attr_val.v.val_double.low = low;
4540 add_dwarf_attr (die, &attr);
4543 /* Add a floating point attribute value to a DIE and return it. */
4545 static inline void
4546 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4547 unsigned int length, unsigned int elt_size, unsigned char *array)
4549 dw_attr_node attr;
4551 attr.dw_attr = attr_kind;
4552 attr.dw_attr_val.val_class = dw_val_class_vec;
4553 attr.dw_attr_val.val_entry = NULL;
4554 attr.dw_attr_val.v.val_vec.length = length;
4555 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4556 attr.dw_attr_val.v.val_vec.array = array;
4557 add_dwarf_attr (die, &attr);
4560 /* Add an 8-byte data attribute value to a DIE. */
4562 static inline void
4563 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4564 unsigned char data8[8])
4566 dw_attr_node attr;
4568 attr.dw_attr = attr_kind;
4569 attr.dw_attr_val.val_class = dw_val_class_data8;
4570 attr.dw_attr_val.val_entry = NULL;
4571 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4572 add_dwarf_attr (die, &attr);
4575 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4576 dwarf_split_debug_info, address attributes in dies destined for the
4577 final executable have force_direct set to avoid using indexed
4578 references. */
4580 static inline void
4581 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4582 bool force_direct)
4584 dw_attr_node attr;
4585 char * lbl_id;
4587 lbl_id = xstrdup (lbl_low);
4588 attr.dw_attr = DW_AT_low_pc;
4589 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4590 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4591 if (dwarf_split_debug_info && !force_direct)
4592 attr.dw_attr_val.val_entry
4593 = add_addr_table_entry (lbl_id, ate_kind_label);
4594 else
4595 attr.dw_attr_val.val_entry = NULL;
4596 add_dwarf_attr (die, &attr);
4598 attr.dw_attr = DW_AT_high_pc;
4599 if (dwarf_version < 4)
4600 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4601 else
4602 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4603 lbl_id = xstrdup (lbl_high);
4604 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4605 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4606 && dwarf_split_debug_info && !force_direct)
4607 attr.dw_attr_val.val_entry
4608 = add_addr_table_entry (lbl_id, ate_kind_label);
4609 else
4610 attr.dw_attr_val.val_entry = NULL;
4611 add_dwarf_attr (die, &attr);
4614 /* Hash and equality functions for debug_str_hash. */
4616 hashval_t
4617 indirect_string_hasher::hash (indirect_string_node *x)
4619 return htab_hash_string (x->str);
4622 bool
4623 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4625 return strcmp (x1->str, x2) == 0;
4628 /* Add STR to the given string hash table. */
4630 static struct indirect_string_node *
4631 find_AT_string_in_table (const char *str,
4632 hash_table<indirect_string_hasher> *table,
4633 enum insert_option insert = INSERT)
4635 struct indirect_string_node *node;
4637 indirect_string_node **slot
4638 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4639 if (*slot == NULL)
4641 node = ggc_cleared_alloc<indirect_string_node> ();
4642 node->str = ggc_strdup (str);
4643 *slot = node;
4645 else
4646 node = *slot;
4648 node->refcount++;
4649 return node;
4652 /* Add STR to the indirect string hash table. */
4654 static struct indirect_string_node *
4655 find_AT_string (const char *str, enum insert_option insert = INSERT)
4657 if (! debug_str_hash)
4658 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4660 return find_AT_string_in_table (str, debug_str_hash, insert);
4663 /* Add a string attribute value to a DIE. */
4665 static inline void
4666 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4668 dw_attr_node attr;
4669 struct indirect_string_node *node;
4671 node = find_AT_string (str);
4673 attr.dw_attr = attr_kind;
4674 attr.dw_attr_val.val_class = dw_val_class_str;
4675 attr.dw_attr_val.val_entry = NULL;
4676 attr.dw_attr_val.v.val_str = node;
4677 add_dwarf_attr (die, &attr);
4680 static inline const char *
4681 AT_string (dw_attr_node *a)
4683 gcc_assert (a && AT_class (a) == dw_val_class_str);
4684 return a->dw_attr_val.v.val_str->str;
4687 /* Call this function directly to bypass AT_string_form's logic to put
4688 the string inline in the die. */
4690 static void
4691 set_indirect_string (struct indirect_string_node *node)
4693 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4694 /* Already indirect is a no op. */
4695 if (node->form == DW_FORM_strp
4696 || node->form == DW_FORM_line_strp
4697 || node->form == dwarf_FORM (DW_FORM_strx))
4699 gcc_assert (node->label);
4700 return;
4702 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4703 ++dw2_string_counter;
4704 node->label = xstrdup (label);
4706 if (!dwarf_split_debug_info)
4708 node->form = DW_FORM_strp;
4709 node->index = NOT_INDEXED;
4711 else
4713 node->form = dwarf_FORM (DW_FORM_strx);
4714 node->index = NO_INDEX_ASSIGNED;
4718 /* A helper function for dwarf2out_finish, called to reset indirect
4719 string decisions done for early LTO dwarf output before fat object
4720 dwarf output. */
4723 reset_indirect_string (indirect_string_node **h, void *)
4725 struct indirect_string_node *node = *h;
4726 if (node->form == DW_FORM_strp || node->form == dwarf_FORM (DW_FORM_strx))
4728 free (node->label);
4729 node->label = NULL;
4730 node->form = (dwarf_form) 0;
4731 node->index = 0;
4733 return 1;
4736 /* Add a string representing a file or filepath attribute value to a DIE. */
4738 static inline void
4739 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4740 const char *str)
4742 if (! asm_outputs_debug_line_str ())
4743 add_AT_string (die, attr_kind, str);
4744 else
4746 dw_attr_node attr;
4747 struct indirect_string_node *node;
4749 if (!debug_line_str_hash)
4750 debug_line_str_hash
4751 = hash_table<indirect_string_hasher>::create_ggc (10);
4753 node = find_AT_string_in_table (str, debug_line_str_hash);
4754 set_indirect_string (node);
4755 node->form = DW_FORM_line_strp;
4757 attr.dw_attr = attr_kind;
4758 attr.dw_attr_val.val_class = dw_val_class_str;
4759 attr.dw_attr_val.val_entry = NULL;
4760 attr.dw_attr_val.v.val_str = node;
4761 add_dwarf_attr (die, &attr);
4765 /* Find out whether a string should be output inline in DIE
4766 or out-of-line in .debug_str section. */
4768 static enum dwarf_form
4769 find_string_form (struct indirect_string_node *node)
4771 unsigned int len;
4773 if (node->form)
4774 return node->form;
4776 len = strlen (node->str) + 1;
4778 /* If the string is shorter or equal to the size of the reference, it is
4779 always better to put it inline. */
4780 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4781 return node->form = DW_FORM_string;
4783 /* If we cannot expect the linker to merge strings in .debug_str
4784 section, only put it into .debug_str if it is worth even in this
4785 single module. */
4786 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4787 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4788 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4789 return node->form = DW_FORM_string;
4791 set_indirect_string (node);
4793 return node->form;
4796 /* Find out whether the string referenced from the attribute should be
4797 output inline in DIE or out-of-line in .debug_str section. */
4799 static enum dwarf_form
4800 AT_string_form (dw_attr_node *a)
4802 gcc_assert (a && AT_class (a) == dw_val_class_str);
4803 return find_string_form (a->dw_attr_val.v.val_str);
4806 /* Add a DIE reference attribute value to a DIE. */
4808 static inline void
4809 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4811 dw_attr_node attr;
4812 gcc_checking_assert (targ_die != NULL);
4814 /* With LTO we can end up trying to reference something we didn't create
4815 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4816 if (targ_die == NULL)
4817 return;
4819 attr.dw_attr = attr_kind;
4820 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4821 attr.dw_attr_val.val_entry = NULL;
4822 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4823 attr.dw_attr_val.v.val_die_ref.external = 0;
4824 add_dwarf_attr (die, &attr);
4827 /* Change DIE reference REF to point to NEW_DIE instead. */
4829 static inline void
4830 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4832 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4833 ref->dw_attr_val.v.val_die_ref.die = new_die;
4834 ref->dw_attr_val.v.val_die_ref.external = 0;
4837 /* Add an AT_specification attribute to a DIE, and also make the back
4838 pointer from the specification to the definition. */
4840 static inline void
4841 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4843 add_AT_die_ref (die, DW_AT_specification, targ_die);
4844 gcc_assert (!targ_die->die_definition);
4845 targ_die->die_definition = die;
4848 static inline dw_die_ref
4849 AT_ref (dw_attr_node *a)
4851 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4852 return a->dw_attr_val.v.val_die_ref.die;
4855 static inline int
4856 AT_ref_external (dw_attr_node *a)
4858 if (a && AT_class (a) == dw_val_class_die_ref)
4859 return a->dw_attr_val.v.val_die_ref.external;
4861 return 0;
4864 static inline void
4865 set_AT_ref_external (dw_attr_node *a, int i)
4867 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4868 a->dw_attr_val.v.val_die_ref.external = i;
4871 /* Add a location description attribute value to a DIE. */
4873 static inline void
4874 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4876 dw_attr_node attr;
4878 attr.dw_attr = attr_kind;
4879 attr.dw_attr_val.val_class = dw_val_class_loc;
4880 attr.dw_attr_val.val_entry = NULL;
4881 attr.dw_attr_val.v.val_loc = loc;
4882 add_dwarf_attr (die, &attr);
4885 static inline dw_loc_descr_ref
4886 AT_loc (dw_attr_node *a)
4888 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4889 return a->dw_attr_val.v.val_loc;
4892 static inline void
4893 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4895 dw_attr_node attr;
4897 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4898 return;
4900 attr.dw_attr = attr_kind;
4901 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4902 attr.dw_attr_val.val_entry = NULL;
4903 attr.dw_attr_val.v.val_loc_list = loc_list;
4904 add_dwarf_attr (die, &attr);
4905 have_location_lists = true;
4908 static inline dw_loc_list_ref
4909 AT_loc_list (dw_attr_node *a)
4911 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4912 return a->dw_attr_val.v.val_loc_list;
4915 /* Add a view list attribute to DIE. It must have a DW_AT_location
4916 attribute, because the view list complements the location list. */
4918 static inline void
4919 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
4921 dw_attr_node attr;
4923 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4924 return;
4926 attr.dw_attr = attr_kind;
4927 attr.dw_attr_val.val_class = dw_val_class_view_list;
4928 attr.dw_attr_val.val_entry = NULL;
4929 attr.dw_attr_val.v.val_view_list = die;
4930 add_dwarf_attr (die, &attr);
4931 gcc_checking_assert (get_AT (die, DW_AT_location));
4932 gcc_assert (have_location_lists);
4935 /* Return a pointer to the location list referenced by the attribute.
4936 If the named attribute is a view list, look up the corresponding
4937 DW_AT_location attribute and return its location list. */
4939 static inline dw_loc_list_ref *
4940 AT_loc_list_ptr (dw_attr_node *a)
4942 gcc_assert (a);
4943 switch (AT_class (a))
4945 case dw_val_class_loc_list:
4946 return &a->dw_attr_val.v.val_loc_list;
4947 case dw_val_class_view_list:
4949 dw_attr_node *l;
4950 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
4951 if (!l)
4952 return NULL;
4953 gcc_checking_assert (l + 1 == a);
4954 return AT_loc_list_ptr (l);
4956 default:
4957 gcc_unreachable ();
4961 /* Return the location attribute value associated with a view list
4962 attribute value. */
4964 static inline dw_val_node *
4965 view_list_to_loc_list_val_node (dw_val_node *val)
4967 gcc_assert (val->val_class == dw_val_class_view_list);
4968 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
4969 if (!loc)
4970 return NULL;
4971 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
4972 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
4973 return &loc->dw_attr_val;
4976 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4978 static hashval_t hash (addr_table_entry *);
4979 static bool equal (addr_table_entry *, addr_table_entry *);
4982 /* Table of entries into the .debug_addr section. */
4984 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4986 /* Hash an address_table_entry. */
4988 hashval_t
4989 addr_hasher::hash (addr_table_entry *a)
4991 inchash::hash hstate;
4992 switch (a->kind)
4994 case ate_kind_rtx:
4995 hstate.add_int (0);
4996 break;
4997 case ate_kind_rtx_dtprel:
4998 hstate.add_int (1);
4999 break;
5000 case ate_kind_label:
5001 return htab_hash_string (a->addr.label);
5002 default:
5003 gcc_unreachable ();
5005 inchash::add_rtx (a->addr.rtl, hstate);
5006 return hstate.end ();
5009 /* Determine equality for two address_table_entries. */
5011 bool
5012 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5014 if (a1->kind != a2->kind)
5015 return 0;
5016 switch (a1->kind)
5018 case ate_kind_rtx:
5019 case ate_kind_rtx_dtprel:
5020 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5021 case ate_kind_label:
5022 return strcmp (a1->addr.label, a2->addr.label) == 0;
5023 default:
5024 gcc_unreachable ();
5028 /* Initialize an addr_table_entry. */
5030 void
5031 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5033 e->kind = kind;
5034 switch (kind)
5036 case ate_kind_rtx:
5037 case ate_kind_rtx_dtprel:
5038 e->addr.rtl = (rtx) addr;
5039 break;
5040 case ate_kind_label:
5041 e->addr.label = (char *) addr;
5042 break;
5044 e->refcount = 0;
5045 e->index = NO_INDEX_ASSIGNED;
5048 /* Add attr to the address table entry to the table. Defer setting an
5049 index until output time. */
5051 static addr_table_entry *
5052 add_addr_table_entry (void *addr, enum ate_kind kind)
5054 addr_table_entry *node;
5055 addr_table_entry finder;
5057 gcc_assert (dwarf_split_debug_info);
5058 if (! addr_index_table)
5059 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5060 init_addr_table_entry (&finder, kind, addr);
5061 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5063 if (*slot == HTAB_EMPTY_ENTRY)
5065 node = ggc_cleared_alloc<addr_table_entry> ();
5066 init_addr_table_entry (node, kind, addr);
5067 *slot = node;
5069 else
5070 node = *slot;
5072 node->refcount++;
5073 return node;
5076 /* Remove an entry from the addr table by decrementing its refcount.
5077 Strictly, decrementing the refcount would be enough, but the
5078 assertion that the entry is actually in the table has found
5079 bugs. */
5081 static void
5082 remove_addr_table_entry (addr_table_entry *entry)
5084 gcc_assert (dwarf_split_debug_info && addr_index_table);
5085 /* After an index is assigned, the table is frozen. */
5086 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5087 entry->refcount--;
5090 /* Given a location list, remove all addresses it refers to from the
5091 address_table. */
5093 static void
5094 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5096 for (; descr; descr = descr->dw_loc_next)
5097 if (descr->dw_loc_oprnd1.val_entry != NULL)
5099 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5100 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5104 /* A helper function for dwarf2out_finish called through
5105 htab_traverse. Assign an addr_table_entry its index. All entries
5106 must be collected into the table when this function is called,
5107 because the indexing code relies on htab_traverse to traverse nodes
5108 in the same order for each run. */
5111 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5113 addr_table_entry *node = *h;
5115 /* Don't index unreferenced nodes. */
5116 if (node->refcount == 0)
5117 return 1;
5119 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5120 node->index = *index;
5121 *index += 1;
5123 return 1;
5126 /* Add an address constant attribute value to a DIE. When using
5127 dwarf_split_debug_info, address attributes in dies destined for the
5128 final executable should be direct references--setting the parameter
5129 force_direct ensures this behavior. */
5131 static inline void
5132 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5133 bool force_direct)
5135 dw_attr_node attr;
5137 attr.dw_attr = attr_kind;
5138 attr.dw_attr_val.val_class = dw_val_class_addr;
5139 attr.dw_attr_val.v.val_addr = addr;
5140 if (dwarf_split_debug_info && !force_direct)
5141 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5142 else
5143 attr.dw_attr_val.val_entry = NULL;
5144 add_dwarf_attr (die, &attr);
5147 /* Get the RTX from to an address DIE attribute. */
5149 static inline rtx
5150 AT_addr (dw_attr_node *a)
5152 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5153 return a->dw_attr_val.v.val_addr;
5156 /* Add a file attribute value to a DIE. */
5158 static inline void
5159 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5160 struct dwarf_file_data *fd)
5162 dw_attr_node attr;
5164 attr.dw_attr = attr_kind;
5165 attr.dw_attr_val.val_class = dw_val_class_file;
5166 attr.dw_attr_val.val_entry = NULL;
5167 attr.dw_attr_val.v.val_file = fd;
5168 add_dwarf_attr (die, &attr);
5171 /* Get the dwarf_file_data from a file DIE attribute. */
5173 static inline struct dwarf_file_data *
5174 AT_file (dw_attr_node *a)
5176 gcc_assert (a && (AT_class (a) == dw_val_class_file
5177 || AT_class (a) == dw_val_class_file_implicit));
5178 return a->dw_attr_val.v.val_file;
5181 /* Add a vms delta attribute value to a DIE. */
5183 static inline void
5184 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5185 const char *lbl1, const char *lbl2)
5187 dw_attr_node attr;
5189 attr.dw_attr = attr_kind;
5190 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5191 attr.dw_attr_val.val_entry = NULL;
5192 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5193 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5194 add_dwarf_attr (die, &attr);
5197 /* Add a symbolic view identifier attribute value to a DIE. */
5199 static inline void
5200 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5201 const char *view_label)
5203 dw_attr_node attr;
5205 attr.dw_attr = attr_kind;
5206 attr.dw_attr_val.val_class = dw_val_class_symview;
5207 attr.dw_attr_val.val_entry = NULL;
5208 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5209 add_dwarf_attr (die, &attr);
5212 /* Add a label identifier attribute value to a DIE. */
5214 static inline void
5215 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5216 const char *lbl_id)
5218 dw_attr_node attr;
5220 attr.dw_attr = attr_kind;
5221 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5222 attr.dw_attr_val.val_entry = NULL;
5223 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5224 if (dwarf_split_debug_info)
5225 attr.dw_attr_val.val_entry
5226 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5227 ate_kind_label);
5228 add_dwarf_attr (die, &attr);
5231 /* Add a section offset attribute value to a DIE, an offset into the
5232 debug_line section. */
5234 static inline void
5235 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5236 const char *label)
5238 dw_attr_node attr;
5240 attr.dw_attr = attr_kind;
5241 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5242 attr.dw_attr_val.val_entry = NULL;
5243 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5244 add_dwarf_attr (die, &attr);
5247 /* Add a section offset attribute value to a DIE, an offset into the
5248 debug_macinfo section. */
5250 static inline void
5251 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5252 const char *label)
5254 dw_attr_node attr;
5256 attr.dw_attr = attr_kind;
5257 attr.dw_attr_val.val_class = dw_val_class_macptr;
5258 attr.dw_attr_val.val_entry = NULL;
5259 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5260 add_dwarf_attr (die, &attr);
5263 /* Add a range_list attribute value to a DIE. When using
5264 dwarf_split_debug_info, address attributes in dies destined for the
5265 final executable should be direct references--setting the parameter
5266 force_direct ensures this behavior. */
5268 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5269 #define RELOCATED_OFFSET (NULL)
5271 static void
5272 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5273 long unsigned int offset, bool force_direct)
5275 dw_attr_node attr;
5277 attr.dw_attr = attr_kind;
5278 attr.dw_attr_val.val_class = dw_val_class_range_list;
5279 /* For the range_list attribute, use val_entry to store whether the
5280 offset should follow split-debug-info or normal semantics. This
5281 value is read in output_range_list_offset. */
5282 if (dwarf_split_debug_info && !force_direct)
5283 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5284 else
5285 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5286 attr.dw_attr_val.v.val_offset = offset;
5287 add_dwarf_attr (die, &attr);
5290 /* Return the start label of a delta attribute. */
5292 static inline const char *
5293 AT_vms_delta1 (dw_attr_node *a)
5295 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5296 return a->dw_attr_val.v.val_vms_delta.lbl1;
5299 /* Return the end label of a delta attribute. */
5301 static inline const char *
5302 AT_vms_delta2 (dw_attr_node *a)
5304 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5305 return a->dw_attr_val.v.val_vms_delta.lbl2;
5308 static inline const char *
5309 AT_lbl (dw_attr_node *a)
5311 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5312 || AT_class (a) == dw_val_class_lineptr
5313 || AT_class (a) == dw_val_class_macptr
5314 || AT_class (a) == dw_val_class_loclistsptr
5315 || AT_class (a) == dw_val_class_high_pc));
5316 return a->dw_attr_val.v.val_lbl_id;
5319 /* Get the attribute of type attr_kind. */
5321 static dw_attr_node *
5322 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5324 dw_attr_node *a;
5325 unsigned ix;
5326 dw_die_ref spec = NULL;
5328 if (! die)
5329 return NULL;
5331 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5332 if (a->dw_attr == attr_kind)
5333 return a;
5334 else if (a->dw_attr == DW_AT_specification
5335 || a->dw_attr == DW_AT_abstract_origin)
5336 spec = AT_ref (a);
5338 if (spec)
5339 return get_AT (spec, attr_kind);
5341 return NULL;
5344 /* Returns the parent of the declaration of DIE. */
5346 static dw_die_ref
5347 get_die_parent (dw_die_ref die)
5349 dw_die_ref t;
5351 if (!die)
5352 return NULL;
5354 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5355 || (t = get_AT_ref (die, DW_AT_specification)))
5356 die = t;
5358 return die->die_parent;
5361 /* Return the "low pc" attribute value, typically associated with a subprogram
5362 DIE. Return null if the "low pc" attribute is either not present, or if it
5363 cannot be represented as an assembler label identifier. */
5365 static inline const char *
5366 get_AT_low_pc (dw_die_ref die)
5368 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5370 return a ? AT_lbl (a) : NULL;
5373 /* Return the value of the string attribute designated by ATTR_KIND, or
5374 NULL if it is not present. */
5376 static inline const char *
5377 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5379 dw_attr_node *a = get_AT (die, attr_kind);
5381 return a ? AT_string (a) : NULL;
5384 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5385 if it is not present. */
5387 static inline int
5388 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5390 dw_attr_node *a = get_AT (die, attr_kind);
5392 return a ? AT_flag (a) : 0;
5395 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5396 if it is not present. */
5398 static inline unsigned
5399 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5401 dw_attr_node *a = get_AT (die, attr_kind);
5403 return a ? AT_unsigned (a) : 0;
5406 static inline dw_die_ref
5407 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5409 dw_attr_node *a = get_AT (die, attr_kind);
5411 return a ? AT_ref (a) : NULL;
5414 static inline struct dwarf_file_data *
5415 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5417 dw_attr_node *a = get_AT (die, attr_kind);
5419 return a ? AT_file (a) : NULL;
5422 /* Return TRUE if the language is C. */
5424 static inline bool
5425 is_c (void)
5427 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5429 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5430 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5435 /* Return TRUE if the language is C++. */
5437 static inline bool
5438 is_cxx (void)
5440 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5442 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5443 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5446 /* Return TRUE if DECL was created by the C++ frontend. */
5448 static bool
5449 is_cxx (const_tree decl)
5451 if (in_lto_p)
5453 const_tree context = get_ultimate_context (decl);
5454 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5455 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5457 return is_cxx ();
5460 /* Return TRUE if the language is Fortran. */
5462 static inline bool
5463 is_fortran (void)
5465 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5467 return (lang == DW_LANG_Fortran77
5468 || lang == DW_LANG_Fortran90
5469 || lang == DW_LANG_Fortran95
5470 || lang == DW_LANG_Fortran03
5471 || lang == DW_LANG_Fortran08);
5474 static inline bool
5475 is_fortran (const_tree decl)
5477 if (in_lto_p)
5479 const_tree context = get_ultimate_context (decl);
5480 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5481 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5482 "GNU Fortran", 11) == 0
5483 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5484 "GNU F77") == 0);
5486 return is_fortran ();
5489 /* Return TRUE if the language is Ada. */
5491 static inline bool
5492 is_ada (void)
5494 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5496 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5499 /* Return TRUE if the language is D. */
5501 static inline bool
5502 is_dlang (void)
5504 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5506 return lang == DW_LANG_D;
5509 /* Remove the specified attribute if present. Return TRUE if removal
5510 was successful. */
5512 static bool
5513 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5515 dw_attr_node *a;
5516 unsigned ix;
5518 if (! die)
5519 return false;
5521 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5522 if (a->dw_attr == attr_kind)
5524 if (AT_class (a) == dw_val_class_str)
5525 if (a->dw_attr_val.v.val_str->refcount)
5526 a->dw_attr_val.v.val_str->refcount--;
5528 /* vec::ordered_remove should help reduce the number of abbrevs
5529 that are needed. */
5530 die->die_attr->ordered_remove (ix);
5531 return true;
5533 return false;
5536 /* Remove CHILD from its parent. PREV must have the property that
5537 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5539 static void
5540 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5542 gcc_assert (child->die_parent == prev->die_parent);
5543 gcc_assert (prev->die_sib == child);
5544 if (prev == child)
5546 gcc_assert (child->die_parent->die_child == child);
5547 prev = NULL;
5549 else
5550 prev->die_sib = child->die_sib;
5551 if (child->die_parent->die_child == child)
5552 child->die_parent->die_child = prev;
5553 child->die_sib = NULL;
5556 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5557 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5559 static void
5560 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5562 dw_die_ref parent = old_child->die_parent;
5564 gcc_assert (parent == prev->die_parent);
5565 gcc_assert (prev->die_sib == old_child);
5567 new_child->die_parent = parent;
5568 if (prev == old_child)
5570 gcc_assert (parent->die_child == old_child);
5571 new_child->die_sib = new_child;
5573 else
5575 prev->die_sib = new_child;
5576 new_child->die_sib = old_child->die_sib;
5578 if (old_child->die_parent->die_child == old_child)
5579 old_child->die_parent->die_child = new_child;
5580 old_child->die_sib = NULL;
5583 /* Move all children from OLD_PARENT to NEW_PARENT. */
5585 static void
5586 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5588 dw_die_ref c;
5589 new_parent->die_child = old_parent->die_child;
5590 old_parent->die_child = NULL;
5591 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5594 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5595 matches TAG. */
5597 static void
5598 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5600 dw_die_ref c;
5602 c = die->die_child;
5603 if (c) do {
5604 dw_die_ref prev = c;
5605 c = c->die_sib;
5606 while (c->die_tag == tag)
5608 remove_child_with_prev (c, prev);
5609 c->die_parent = NULL;
5610 /* Might have removed every child. */
5611 if (die->die_child == NULL)
5612 return;
5613 c = prev->die_sib;
5615 } while (c != die->die_child);
5618 /* Add a CHILD_DIE as the last child of DIE. */
5620 static void
5621 add_child_die (dw_die_ref die, dw_die_ref child_die)
5623 /* FIXME this should probably be an assert. */
5624 if (! die || ! child_die)
5625 return;
5626 gcc_assert (die != child_die);
5628 child_die->die_parent = die;
5629 if (die->die_child)
5631 child_die->die_sib = die->die_child->die_sib;
5632 die->die_child->die_sib = child_die;
5634 else
5635 child_die->die_sib = child_die;
5636 die->die_child = child_die;
5639 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5641 static void
5642 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5643 dw_die_ref after_die)
5645 gcc_assert (die
5646 && child_die
5647 && after_die
5648 && die->die_child
5649 && die != child_die);
5651 child_die->die_parent = die;
5652 child_die->die_sib = after_die->die_sib;
5653 after_die->die_sib = child_die;
5654 if (die->die_child == after_die)
5655 die->die_child = child_die;
5658 /* Unassociate CHILD from its parent, and make its parent be
5659 NEW_PARENT. */
5661 static void
5662 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5664 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5665 if (p->die_sib == child)
5667 remove_child_with_prev (child, p);
5668 break;
5670 add_child_die (new_parent, child);
5673 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5674 is the specification, to the end of PARENT's list of children.
5675 This is done by removing and re-adding it. */
5677 static void
5678 splice_child_die (dw_die_ref parent, dw_die_ref child)
5680 /* We want the declaration DIE from inside the class, not the
5681 specification DIE at toplevel. */
5682 if (child->die_parent != parent)
5684 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5686 if (tmp)
5687 child = tmp;
5690 gcc_assert (child->die_parent == parent
5691 || (child->die_parent
5692 == get_AT_ref (parent, DW_AT_specification)));
5694 reparent_child (child, parent);
5697 /* Create and return a new die with TAG_VALUE as tag. */
5699 static inline dw_die_ref
5700 new_die_raw (enum dwarf_tag tag_value)
5702 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5703 die->die_tag = tag_value;
5704 return die;
5707 /* Create and return a new die with a parent of PARENT_DIE. If
5708 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5709 associated tree T must be supplied to determine parenthood
5710 later. */
5712 static inline dw_die_ref
5713 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5715 dw_die_ref die = new_die_raw (tag_value);
5717 if (parent_die != NULL)
5718 add_child_die (parent_die, die);
5719 else
5721 limbo_die_node *limbo_node;
5723 /* No DIEs created after early dwarf should end up in limbo,
5724 because the limbo list should not persist past LTO
5725 streaming. */
5726 if (tag_value != DW_TAG_compile_unit
5727 /* These are allowed because they're generated while
5728 breaking out COMDAT units late. */
5729 && tag_value != DW_TAG_type_unit
5730 && tag_value != DW_TAG_skeleton_unit
5731 && !early_dwarf
5732 /* Allow nested functions to live in limbo because they will
5733 only temporarily live there, as decls_for_scope will fix
5734 them up. */
5735 && (TREE_CODE (t) != FUNCTION_DECL
5736 || !decl_function_context (t))
5737 /* Same as nested functions above but for types. Types that
5738 are local to a function will be fixed in
5739 decls_for_scope. */
5740 && (!RECORD_OR_UNION_TYPE_P (t)
5741 || !TYPE_CONTEXT (t)
5742 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5743 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5744 especially in the ltrans stage, but once we implement LTO
5745 dwarf streaming, we should remove this exception. */
5746 && !in_lto_p)
5748 fprintf (stderr, "symbol ended up in limbo too late:");
5749 debug_generic_stmt (t);
5750 gcc_unreachable ();
5753 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5754 limbo_node->die = die;
5755 limbo_node->created_for = t;
5756 limbo_node->next = limbo_die_list;
5757 limbo_die_list = limbo_node;
5760 return die;
5763 /* Return the DIE associated with the given type specifier. */
5765 static inline dw_die_ref
5766 lookup_type_die (tree type)
5768 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5769 if (die && die->removed)
5771 TYPE_SYMTAB_DIE (type) = NULL;
5772 return NULL;
5774 return die;
5777 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5778 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5779 anonymous type instead the one of the naming typedef. */
5781 static inline dw_die_ref
5782 strip_naming_typedef (tree type, dw_die_ref type_die)
5784 if (type
5785 && TREE_CODE (type) == RECORD_TYPE
5786 && type_die
5787 && type_die->die_tag == DW_TAG_typedef
5788 && is_naming_typedef_decl (TYPE_NAME (type)))
5789 type_die = get_AT_ref (type_die, DW_AT_type);
5790 return type_die;
5793 /* Like lookup_type_die, but if type is an anonymous type named by a
5794 typedef[1], return the DIE of the anonymous type instead the one of
5795 the naming typedef. This is because in gen_typedef_die, we did
5796 equate the anonymous struct named by the typedef with the DIE of
5797 the naming typedef. So by default, lookup_type_die on an anonymous
5798 struct yields the DIE of the naming typedef.
5800 [1]: Read the comment of is_naming_typedef_decl to learn about what
5801 a naming typedef is. */
5803 static inline dw_die_ref
5804 lookup_type_die_strip_naming_typedef (tree type)
5806 dw_die_ref die = lookup_type_die (type);
5807 return strip_naming_typedef (type, die);
5810 /* Equate a DIE to a given type specifier. */
5812 static inline void
5813 equate_type_number_to_die (tree type, dw_die_ref type_die)
5815 TYPE_SYMTAB_DIE (type) = type_die;
5818 static dw_die_ref maybe_create_die_with_external_ref (tree);
5819 struct GTY(()) sym_off_pair
5821 const char * GTY((skip)) sym;
5822 unsigned HOST_WIDE_INT off;
5824 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5826 /* Returns a hash value for X (which really is a die_struct). */
5828 inline hashval_t
5829 decl_die_hasher::hash (die_node *x)
5831 return (hashval_t) x->decl_id;
5834 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5836 inline bool
5837 decl_die_hasher::equal (die_node *x, tree y)
5839 return (x->decl_id == DECL_UID (y));
5842 /* Return the DIE associated with a given declaration. */
5844 static inline dw_die_ref
5845 lookup_decl_die (tree decl)
5847 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5848 NO_INSERT);
5849 if (!die)
5851 if (in_lto_p)
5852 return maybe_create_die_with_external_ref (decl);
5853 return NULL;
5855 if ((*die)->removed)
5857 decl_die_table->clear_slot (die);
5858 return NULL;
5860 return *die;
5864 /* Return the DIE associated with BLOCK. */
5866 static inline dw_die_ref
5867 lookup_block_die (tree block)
5869 dw_die_ref die = BLOCK_DIE (block);
5870 if (!die && in_lto_p)
5871 return maybe_create_die_with_external_ref (block);
5872 return die;
5875 /* Associate DIE with BLOCK. */
5877 static inline void
5878 equate_block_to_die (tree block, dw_die_ref die)
5880 BLOCK_DIE (block) = die;
5882 #undef BLOCK_DIE
5885 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5886 style reference. Return true if we found one refering to a DIE for
5887 DECL, otherwise return false. */
5889 static bool
5890 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5891 unsigned HOST_WIDE_INT *off)
5893 dw_die_ref die;
5895 if (in_lto_p)
5897 /* During WPA stage and incremental linking we use a hash-map
5898 to store the decl <-> label + offset map. */
5899 if (!external_die_map)
5900 return false;
5901 sym_off_pair *desc = external_die_map->get (decl);
5902 if (!desc)
5903 return false;
5904 *sym = desc->sym;
5905 *off = desc->off;
5906 return true;
5909 if (TREE_CODE (decl) == BLOCK)
5910 die = lookup_block_die (decl);
5911 else
5912 die = lookup_decl_die (decl);
5913 if (!die)
5914 return false;
5916 /* Similar to get_ref_die_offset_label, but using the "correct"
5917 label. */
5918 *off = die->die_offset;
5919 while (die->die_parent)
5920 die = die->die_parent;
5921 /* For the containing CU DIE we compute a die_symbol in
5922 compute_comp_unit_symbol. */
5923 gcc_assert (die->die_tag == DW_TAG_compile_unit
5924 && die->die_id.die_symbol != NULL);
5925 *sym = die->die_id.die_symbol;
5926 return true;
5929 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5931 static void
5932 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5933 const char *symbol, HOST_WIDE_INT offset)
5935 /* Create a fake DIE that contains the reference. Don't use
5936 new_die because we don't want to end up in the limbo list. */
5937 /* ??? We probably want to share these, thus put a ref to the DIE
5938 we create here to the external_die_map entry. */
5939 dw_die_ref ref = new_die_raw (die->die_tag);
5940 ref->die_id.die_symbol = symbol;
5941 ref->die_offset = offset;
5942 ref->with_offset = 1;
5943 add_AT_die_ref (die, attr_kind, ref);
5946 /* Create a DIE for DECL if required and add a reference to a DIE
5947 at SYMBOL + OFFSET which contains attributes dumped early. */
5949 static void
5950 dwarf2out_register_external_die (tree decl, const char *sym,
5951 unsigned HOST_WIDE_INT off)
5953 if (debug_info_level == DINFO_LEVEL_NONE)
5954 return;
5956 if (!external_die_map)
5957 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
5958 gcc_checking_assert (!external_die_map->get (decl));
5959 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
5960 external_die_map->put (decl, p);
5963 /* If we have a registered external DIE for DECL return a new DIE for
5964 the concrete instance with an appropriate abstract origin. */
5966 static dw_die_ref
5967 maybe_create_die_with_external_ref (tree decl)
5969 if (!external_die_map)
5970 return NULL;
5971 sym_off_pair *desc = external_die_map->get (decl);
5972 if (!desc)
5973 return NULL;
5975 const char *sym = desc->sym;
5976 unsigned HOST_WIDE_INT off = desc->off;
5977 external_die_map->remove (decl);
5979 in_lto_p = false;
5980 dw_die_ref die = (TREE_CODE (decl) == BLOCK
5981 ? lookup_block_die (decl) : lookup_decl_die (decl));
5982 gcc_assert (!die);
5983 in_lto_p = true;
5985 tree ctx;
5986 dw_die_ref parent = NULL;
5987 /* Need to lookup a DIE for the decls context - the containing
5988 function or translation unit. */
5989 if (TREE_CODE (decl) == BLOCK)
5991 ctx = BLOCK_SUPERCONTEXT (decl);
5992 /* ??? We do not output DIEs for all scopes thus skip as
5993 many DIEs as needed. */
5994 while (TREE_CODE (ctx) == BLOCK
5995 && !lookup_block_die (ctx))
5996 ctx = BLOCK_SUPERCONTEXT (ctx);
5998 else
5999 ctx = DECL_CONTEXT (decl);
6000 /* Peel types in the context stack. */
6001 while (ctx && TYPE_P (ctx))
6002 ctx = TYPE_CONTEXT (ctx);
6003 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6004 if (debug_info_level <= DINFO_LEVEL_TERSE)
6005 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6006 ctx = DECL_CONTEXT (ctx);
6007 if (ctx)
6009 if (TREE_CODE (ctx) == BLOCK)
6010 parent = lookup_block_die (ctx);
6011 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6012 /* Keep the 1:1 association during WPA. */
6013 && !flag_wpa
6014 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6015 /* Otherwise all late annotations go to the main CU which
6016 imports the original CUs. */
6017 parent = comp_unit_die ();
6018 else if (TREE_CODE (ctx) == FUNCTION_DECL
6019 && TREE_CODE (decl) != FUNCTION_DECL
6020 && TREE_CODE (decl) != PARM_DECL
6021 && TREE_CODE (decl) != RESULT_DECL
6022 && TREE_CODE (decl) != BLOCK)
6023 /* Leave function local entities parent determination to when
6024 we process scope vars. */
6026 else
6027 parent = lookup_decl_die (ctx);
6029 else
6030 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6031 Handle this case gracefully by globalizing stuff. */
6032 parent = comp_unit_die ();
6033 /* Create a DIE "stub". */
6034 switch (TREE_CODE (decl))
6036 case TRANSLATION_UNIT_DECL:
6038 die = comp_unit_die ();
6039 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6040 to create a DIE for the original CUs. */
6041 return die;
6043 case NAMESPACE_DECL:
6044 if (is_fortran (decl))
6045 die = new_die (DW_TAG_module, parent, decl);
6046 else
6047 die = new_die (DW_TAG_namespace, parent, decl);
6048 break;
6049 case FUNCTION_DECL:
6050 die = new_die (DW_TAG_subprogram, parent, decl);
6051 break;
6052 case VAR_DECL:
6053 die = new_die (DW_TAG_variable, parent, decl);
6054 break;
6055 case RESULT_DECL:
6056 die = new_die (DW_TAG_variable, parent, decl);
6057 break;
6058 case PARM_DECL:
6059 die = new_die (DW_TAG_formal_parameter, parent, decl);
6060 break;
6061 case CONST_DECL:
6062 die = new_die (DW_TAG_constant, parent, decl);
6063 break;
6064 case LABEL_DECL:
6065 die = new_die (DW_TAG_label, parent, decl);
6066 break;
6067 case BLOCK:
6068 die = new_die (DW_TAG_lexical_block, parent, decl);
6069 break;
6070 default:
6071 gcc_unreachable ();
6073 if (TREE_CODE (decl) == BLOCK)
6074 equate_block_to_die (decl, die);
6075 else
6076 equate_decl_number_to_die (decl, die);
6078 add_desc_attribute (die, decl);
6080 /* Add a reference to the DIE providing early debug at $sym + off. */
6081 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6083 return die;
6086 /* Returns a hash value for X (which really is a var_loc_list). */
6088 inline hashval_t
6089 decl_loc_hasher::hash (var_loc_list *x)
6091 return (hashval_t) x->decl_id;
6094 /* Return nonzero if decl_id of var_loc_list X is the same as
6095 UID of decl *Y. */
6097 inline bool
6098 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6100 return (x->decl_id == DECL_UID (y));
6103 /* Return the var_loc list associated with a given declaration. */
6105 static inline var_loc_list *
6106 lookup_decl_loc (const_tree decl)
6108 if (!decl_loc_table)
6109 return NULL;
6110 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6113 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6115 inline hashval_t
6116 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6118 return (hashval_t) x->decl_id;
6121 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6122 UID of decl *Y. */
6124 inline bool
6125 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6127 return (x->decl_id == DECL_UID (y));
6130 /* Equate a DIE to a particular declaration. */
6132 static void
6133 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6135 unsigned int decl_id = DECL_UID (decl);
6137 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6138 decl_die->decl_id = decl_id;
6141 /* Return how many bits covers PIECE EXPR_LIST. */
6143 static HOST_WIDE_INT
6144 decl_piece_bitsize (rtx piece)
6146 int ret = (int) GET_MODE (piece);
6147 if (ret)
6148 return ret;
6149 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6150 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6151 return INTVAL (XEXP (XEXP (piece, 0), 0));
6154 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6156 static rtx *
6157 decl_piece_varloc_ptr (rtx piece)
6159 if ((int) GET_MODE (piece))
6160 return &XEXP (piece, 0);
6161 else
6162 return &XEXP (XEXP (piece, 0), 1);
6165 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6166 Next is the chain of following piece nodes. */
6168 static rtx_expr_list *
6169 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6171 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6172 return alloc_EXPR_LIST (bitsize, loc_note, next);
6173 else
6174 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6175 GEN_INT (bitsize),
6176 loc_note), next);
6179 /* Return rtx that should be stored into loc field for
6180 LOC_NOTE and BITPOS/BITSIZE. */
6182 static rtx
6183 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6184 HOST_WIDE_INT bitsize)
6186 if (bitsize != -1)
6188 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6189 if (bitpos != 0)
6190 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6192 return loc_note;
6195 /* This function either modifies location piece list *DEST in
6196 place (if SRC and INNER is NULL), or copies location piece list
6197 *SRC to *DEST while modifying it. Location BITPOS is modified
6198 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6199 not copied and if needed some padding around it is added.
6200 When modifying in place, DEST should point to EXPR_LIST where
6201 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6202 to the start of the whole list and INNER points to the EXPR_LIST
6203 where earlier pieces cover PIECE_BITPOS bits. */
6205 static void
6206 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6207 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6208 HOST_WIDE_INT bitsize, rtx loc_note)
6210 HOST_WIDE_INT diff;
6211 bool copy = inner != NULL;
6213 if (copy)
6215 /* First copy all nodes preceding the current bitpos. */
6216 while (src != inner)
6218 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6219 decl_piece_bitsize (*src), NULL_RTX);
6220 dest = &XEXP (*dest, 1);
6221 src = &XEXP (*src, 1);
6224 /* Add padding if needed. */
6225 if (bitpos != piece_bitpos)
6227 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6228 copy ? NULL_RTX : *dest);
6229 dest = &XEXP (*dest, 1);
6231 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6233 gcc_assert (!copy);
6234 /* A piece with correct bitpos and bitsize already exist,
6235 just update the location for it and return. */
6236 *decl_piece_varloc_ptr (*dest) = loc_note;
6237 return;
6239 /* Add the piece that changed. */
6240 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6241 dest = &XEXP (*dest, 1);
6242 /* Skip over pieces that overlap it. */
6243 diff = bitpos - piece_bitpos + bitsize;
6244 if (!copy)
6245 src = dest;
6246 while (diff > 0 && *src)
6248 rtx piece = *src;
6249 diff -= decl_piece_bitsize (piece);
6250 if (copy)
6251 src = &XEXP (piece, 1);
6252 else
6254 *src = XEXP (piece, 1);
6255 free_EXPR_LIST_node (piece);
6258 /* Add padding if needed. */
6259 if (diff < 0 && *src)
6261 if (!copy)
6262 dest = src;
6263 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6264 dest = &XEXP (*dest, 1);
6266 if (!copy)
6267 return;
6268 /* Finally copy all nodes following it. */
6269 while (*src)
6271 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6272 decl_piece_bitsize (*src), NULL_RTX);
6273 dest = &XEXP (*dest, 1);
6274 src = &XEXP (*src, 1);
6278 /* Add a variable location node to the linked list for DECL. */
6280 static struct var_loc_node *
6281 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6283 unsigned int decl_id;
6284 var_loc_list *temp;
6285 struct var_loc_node *loc = NULL;
6286 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6288 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6290 tree realdecl = DECL_DEBUG_EXPR (decl);
6291 if (handled_component_p (realdecl)
6292 || (TREE_CODE (realdecl) == MEM_REF
6293 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6295 bool reverse;
6296 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6297 &bitsize, &reverse);
6298 if (!innerdecl
6299 || !DECL_P (innerdecl)
6300 || DECL_IGNORED_P (innerdecl)
6301 || TREE_STATIC (innerdecl)
6302 || bitsize == 0
6303 || bitpos + bitsize > 256)
6304 return NULL;
6305 decl = innerdecl;
6309 decl_id = DECL_UID (decl);
6310 var_loc_list **slot
6311 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6312 if (*slot == NULL)
6314 temp = ggc_cleared_alloc<var_loc_list> ();
6315 temp->decl_id = decl_id;
6316 *slot = temp;
6318 else
6319 temp = *slot;
6321 /* For PARM_DECLs try to keep around the original incoming value,
6322 even if that means we'll emit a zero-range .debug_loc entry. */
6323 if (temp->last
6324 && temp->first == temp->last
6325 && TREE_CODE (decl) == PARM_DECL
6326 && NOTE_P (temp->first->loc)
6327 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6328 && DECL_INCOMING_RTL (decl)
6329 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6330 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6331 == GET_CODE (DECL_INCOMING_RTL (decl))
6332 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6333 && (bitsize != -1
6334 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6335 NOTE_VAR_LOCATION_LOC (loc_note))
6336 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6337 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6339 loc = ggc_cleared_alloc<var_loc_node> ();
6340 temp->first->next = loc;
6341 temp->last = loc;
6342 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6344 else if (temp->last)
6346 struct var_loc_node *last = temp->last, *unused = NULL;
6347 rtx *piece_loc = NULL, last_loc_note;
6348 HOST_WIDE_INT piece_bitpos = 0;
6349 if (last->next)
6351 last = last->next;
6352 gcc_assert (last->next == NULL);
6354 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6356 piece_loc = &last->loc;
6359 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6360 if (piece_bitpos + cur_bitsize > bitpos)
6361 break;
6362 piece_bitpos += cur_bitsize;
6363 piece_loc = &XEXP (*piece_loc, 1);
6365 while (*piece_loc);
6367 /* TEMP->LAST here is either pointer to the last but one or
6368 last element in the chained list, LAST is pointer to the
6369 last element. */
6370 if (label && strcmp (last->label, label) == 0 && last->view == view)
6372 /* For SRA optimized variables if there weren't any real
6373 insns since last note, just modify the last node. */
6374 if (piece_loc != NULL)
6376 adjust_piece_list (piece_loc, NULL, NULL,
6377 bitpos, piece_bitpos, bitsize, loc_note);
6378 return NULL;
6380 /* If the last note doesn't cover any instructions, remove it. */
6381 if (temp->last != last)
6383 temp->last->next = NULL;
6384 unused = last;
6385 last = temp->last;
6386 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6388 else
6390 gcc_assert (temp->first == temp->last
6391 || (temp->first->next == temp->last
6392 && TREE_CODE (decl) == PARM_DECL));
6393 memset (temp->last, '\0', sizeof (*temp->last));
6394 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6395 return temp->last;
6398 if (bitsize == -1 && NOTE_P (last->loc))
6399 last_loc_note = last->loc;
6400 else if (piece_loc != NULL
6401 && *piece_loc != NULL_RTX
6402 && piece_bitpos == bitpos
6403 && decl_piece_bitsize (*piece_loc) == bitsize)
6404 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6405 else
6406 last_loc_note = NULL_RTX;
6407 /* If the current location is the same as the end of the list,
6408 and either both or neither of the locations is uninitialized,
6409 we have nothing to do. */
6410 if (last_loc_note == NULL_RTX
6411 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6412 NOTE_VAR_LOCATION_LOC (loc_note)))
6413 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6414 != NOTE_VAR_LOCATION_STATUS (loc_note))
6415 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6416 == VAR_INIT_STATUS_UNINITIALIZED)
6417 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6418 == VAR_INIT_STATUS_UNINITIALIZED))))
6420 /* Add LOC to the end of list and update LAST. If the last
6421 element of the list has been removed above, reuse its
6422 memory for the new node, otherwise allocate a new one. */
6423 if (unused)
6425 loc = unused;
6426 memset (loc, '\0', sizeof (*loc));
6428 else
6429 loc = ggc_cleared_alloc<var_loc_node> ();
6430 if (bitsize == -1 || piece_loc == NULL)
6431 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6432 else
6433 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6434 bitpos, piece_bitpos, bitsize, loc_note);
6435 last->next = loc;
6436 /* Ensure TEMP->LAST will point either to the new last but one
6437 element of the chain, or to the last element in it. */
6438 if (last != temp->last)
6439 temp->last = last;
6441 else if (unused)
6442 ggc_free (unused);
6444 else
6446 loc = ggc_cleared_alloc<var_loc_node> ();
6447 temp->first = loc;
6448 temp->last = loc;
6449 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6451 return loc;
6454 /* Keep track of the number of spaces used to indent the
6455 output of the debugging routines that print the structure of
6456 the DIE internal representation. */
6457 static int print_indent;
6459 /* Indent the line the number of spaces given by print_indent. */
6461 static inline void
6462 print_spaces (FILE *outfile)
6464 fprintf (outfile, "%*s", print_indent, "");
6467 /* Print a type signature in hex. */
6469 static inline void
6470 print_signature (FILE *outfile, char *sig)
6472 int i;
6474 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6475 fprintf (outfile, "%02x", sig[i] & 0xff);
6478 static inline void
6479 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6481 if (discr_value->pos)
6482 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6483 else
6484 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6487 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6489 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6490 RECURSE, output location descriptor operations. */
6492 static void
6493 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6495 switch (val->val_class)
6497 case dw_val_class_addr:
6498 fprintf (outfile, "address");
6499 break;
6500 case dw_val_class_offset:
6501 fprintf (outfile, "offset");
6502 break;
6503 case dw_val_class_loc:
6504 fprintf (outfile, "location descriptor");
6505 if (val->v.val_loc == NULL)
6506 fprintf (outfile, " -> <null>\n");
6507 else if (recurse)
6509 fprintf (outfile, ":\n");
6510 print_indent += 4;
6511 print_loc_descr (val->v.val_loc, outfile);
6512 print_indent -= 4;
6514 else
6516 if (flag_dump_noaddr || flag_dump_unnumbered)
6517 fprintf (outfile, " #\n");
6518 else
6519 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6521 break;
6522 case dw_val_class_loc_list:
6523 fprintf (outfile, "location list -> label:%s",
6524 val->v.val_loc_list->ll_symbol);
6525 break;
6526 case dw_val_class_view_list:
6527 val = view_list_to_loc_list_val_node (val);
6528 fprintf (outfile, "location list with views -> labels:%s and %s",
6529 val->v.val_loc_list->ll_symbol,
6530 val->v.val_loc_list->vl_symbol);
6531 break;
6532 case dw_val_class_range_list:
6533 fprintf (outfile, "range list");
6534 break;
6535 case dw_val_class_const:
6536 case dw_val_class_const_implicit:
6537 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6538 break;
6539 case dw_val_class_unsigned_const:
6540 case dw_val_class_unsigned_const_implicit:
6541 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6542 break;
6543 case dw_val_class_const_double:
6544 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6545 HOST_WIDE_INT_PRINT_UNSIGNED")",
6546 val->v.val_double.high,
6547 val->v.val_double.low);
6548 break;
6549 case dw_val_class_wide_int:
6551 int i = val->v.val_wide->get_len ();
6552 fprintf (outfile, "constant (");
6553 gcc_assert (i > 0);
6554 if (val->v.val_wide->elt (i - 1) == 0)
6555 fprintf (outfile, "0x");
6556 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6557 val->v.val_wide->elt (--i));
6558 while (--i >= 0)
6559 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6560 val->v.val_wide->elt (i));
6561 fprintf (outfile, ")");
6562 break;
6564 case dw_val_class_vec:
6565 fprintf (outfile, "floating-point or vector constant");
6566 break;
6567 case dw_val_class_flag:
6568 fprintf (outfile, "%u", val->v.val_flag);
6569 break;
6570 case dw_val_class_die_ref:
6571 if (val->v.val_die_ref.die != NULL)
6573 dw_die_ref die = val->v.val_die_ref.die;
6575 if (die->comdat_type_p)
6577 fprintf (outfile, "die -> signature: ");
6578 print_signature (outfile,
6579 die->die_id.die_type_node->signature);
6581 else if (die->die_id.die_symbol)
6583 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6584 if (die->with_offset)
6585 fprintf (outfile, " + %ld", die->die_offset);
6587 else
6588 fprintf (outfile, "die -> %ld", die->die_offset);
6589 if (flag_dump_noaddr || flag_dump_unnumbered)
6590 fprintf (outfile, " #");
6591 else
6592 fprintf (outfile, " (%p)", (void *) die);
6594 else
6595 fprintf (outfile, "die -> <null>");
6596 break;
6597 case dw_val_class_vms_delta:
6598 fprintf (outfile, "delta: @slotcount(%s-%s)",
6599 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6600 break;
6601 case dw_val_class_symview:
6602 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6603 break;
6604 case dw_val_class_lbl_id:
6605 case dw_val_class_lineptr:
6606 case dw_val_class_macptr:
6607 case dw_val_class_loclistsptr:
6608 case dw_val_class_high_pc:
6609 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6610 break;
6611 case dw_val_class_str:
6612 if (val->v.val_str->str != NULL)
6613 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6614 else
6615 fprintf (outfile, "<null>");
6616 break;
6617 case dw_val_class_file:
6618 case dw_val_class_file_implicit:
6619 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6620 val->v.val_file->emitted_number);
6621 break;
6622 case dw_val_class_data8:
6624 int i;
6626 for (i = 0; i < 8; i++)
6627 fprintf (outfile, "%02x", val->v.val_data8[i]);
6628 break;
6630 case dw_val_class_discr_value:
6631 print_discr_value (outfile, &val->v.val_discr_value);
6632 break;
6633 case dw_val_class_discr_list:
6634 for (dw_discr_list_ref node = val->v.val_discr_list;
6635 node != NULL;
6636 node = node->dw_discr_next)
6638 if (node->dw_discr_range)
6640 fprintf (outfile, " .. ");
6641 print_discr_value (outfile, &node->dw_discr_lower_bound);
6642 print_discr_value (outfile, &node->dw_discr_upper_bound);
6644 else
6645 print_discr_value (outfile, &node->dw_discr_lower_bound);
6647 if (node->dw_discr_next != NULL)
6648 fprintf (outfile, " | ");
6650 default:
6651 break;
6655 /* Likewise, for a DIE attribute. */
6657 static void
6658 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6660 print_dw_val (&a->dw_attr_val, recurse, outfile);
6664 /* Print the list of operands in the LOC location description to OUTFILE. This
6665 routine is a debugging aid only. */
6667 static void
6668 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6670 dw_loc_descr_ref l = loc;
6672 if (loc == NULL)
6674 print_spaces (outfile);
6675 fprintf (outfile, "<null>\n");
6676 return;
6679 for (l = loc; l != NULL; l = l->dw_loc_next)
6681 print_spaces (outfile);
6682 if (flag_dump_noaddr || flag_dump_unnumbered)
6683 fprintf (outfile, "#");
6684 else
6685 fprintf (outfile, "(%p)", (void *) l);
6686 fprintf (outfile, " %s",
6687 dwarf_stack_op_name (l->dw_loc_opc));
6688 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6690 fprintf (outfile, " ");
6691 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6693 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6695 fprintf (outfile, ", ");
6696 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6698 fprintf (outfile, "\n");
6702 /* Print the information associated with a given DIE, and its children.
6703 This routine is a debugging aid only. */
6705 static void
6706 print_die (dw_die_ref die, FILE *outfile)
6708 dw_attr_node *a;
6709 dw_die_ref c;
6710 unsigned ix;
6712 print_spaces (outfile);
6713 fprintf (outfile, "DIE %4ld: %s ",
6714 die->die_offset, dwarf_tag_name (die->die_tag));
6715 if (flag_dump_noaddr || flag_dump_unnumbered)
6716 fprintf (outfile, "#\n");
6717 else
6718 fprintf (outfile, "(%p)\n", (void*) die);
6719 print_spaces (outfile);
6720 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6721 fprintf (outfile, " offset: %ld", die->die_offset);
6722 fprintf (outfile, " mark: %d\n", die->die_mark);
6724 if (die->comdat_type_p)
6726 print_spaces (outfile);
6727 fprintf (outfile, " signature: ");
6728 print_signature (outfile, die->die_id.die_type_node->signature);
6729 fprintf (outfile, "\n");
6732 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6734 print_spaces (outfile);
6735 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6737 print_attribute (a, true, outfile);
6738 fprintf (outfile, "\n");
6741 if (die->die_child != NULL)
6743 print_indent += 4;
6744 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6745 print_indent -= 4;
6747 if (print_indent == 0)
6748 fprintf (outfile, "\n");
6751 /* Print the list of operations in the LOC location description. */
6753 DEBUG_FUNCTION void
6754 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6756 print_loc_descr (loc, stderr);
6759 /* Print the information collected for a given DIE. */
6761 DEBUG_FUNCTION void
6762 debug_dwarf_die (dw_die_ref die)
6764 print_die (die, stderr);
6767 DEBUG_FUNCTION void
6768 debug (die_struct &ref)
6770 print_die (&ref, stderr);
6773 DEBUG_FUNCTION void
6774 debug (die_struct *ptr)
6776 if (ptr)
6777 debug (*ptr);
6778 else
6779 fprintf (stderr, "<nil>\n");
6783 /* Print all DWARF information collected for the compilation unit.
6784 This routine is a debugging aid only. */
6786 DEBUG_FUNCTION void
6787 debug_dwarf (void)
6789 print_indent = 0;
6790 print_die (comp_unit_die (), stderr);
6793 /* Verify the DIE tree structure. */
6795 DEBUG_FUNCTION void
6796 verify_die (dw_die_ref die)
6798 gcc_assert (!die->die_mark);
6799 if (die->die_parent == NULL
6800 && die->die_sib == NULL)
6801 return;
6802 /* Verify the die_sib list is cyclic. */
6803 dw_die_ref x = die;
6806 x->die_mark = 1;
6807 x = x->die_sib;
6809 while (x && !x->die_mark);
6810 gcc_assert (x == die);
6811 x = die;
6814 /* Verify all dies have the same parent. */
6815 gcc_assert (x->die_parent == die->die_parent);
6816 if (x->die_child)
6818 /* Verify the child has the proper parent and recurse. */
6819 gcc_assert (x->die_child->die_parent == x);
6820 verify_die (x->die_child);
6822 x->die_mark = 0;
6823 x = x->die_sib;
6825 while (x && x->die_mark);
6828 /* Sanity checks on DIEs. */
6830 static void
6831 check_die (dw_die_ref die)
6833 unsigned ix;
6834 dw_attr_node *a;
6835 bool inline_found = false;
6836 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6837 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6838 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6840 switch (a->dw_attr)
6842 case DW_AT_inline:
6843 if (a->dw_attr_val.v.val_unsigned)
6844 inline_found = true;
6845 break;
6846 case DW_AT_location:
6847 ++n_location;
6848 break;
6849 case DW_AT_low_pc:
6850 ++n_low_pc;
6851 break;
6852 case DW_AT_high_pc:
6853 ++n_high_pc;
6854 break;
6855 case DW_AT_artificial:
6856 ++n_artificial;
6857 break;
6858 case DW_AT_decl_column:
6859 ++n_decl_column;
6860 break;
6861 case DW_AT_decl_line:
6862 ++n_decl_line;
6863 break;
6864 case DW_AT_decl_file:
6865 ++n_decl_file;
6866 break;
6867 default:
6868 break;
6871 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6872 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6874 fprintf (stderr, "Duplicate attributes in DIE:\n");
6875 debug_dwarf_die (die);
6876 gcc_unreachable ();
6878 if (inline_found)
6880 /* A debugging information entry that is a member of an abstract
6881 instance tree [that has DW_AT_inline] should not contain any
6882 attributes which describe aspects of the subroutine which vary
6883 between distinct inlined expansions or distinct out-of-line
6884 expansions. */
6885 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6886 gcc_assert (a->dw_attr != DW_AT_low_pc
6887 && a->dw_attr != DW_AT_high_pc
6888 && a->dw_attr != DW_AT_location
6889 && a->dw_attr != DW_AT_frame_base
6890 && a->dw_attr != DW_AT_call_all_calls
6891 && a->dw_attr != DW_AT_GNU_all_call_sites);
6895 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6896 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6897 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6899 /* Calculate the checksum of a location expression. */
6901 static inline void
6902 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6904 int tem;
6905 inchash::hash hstate;
6906 hashval_t hash;
6908 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6909 CHECKSUM (tem);
6910 hash_loc_operands (loc, hstate);
6911 hash = hstate.end();
6912 CHECKSUM (hash);
6915 /* Calculate the checksum of an attribute. */
6917 static void
6918 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6920 dw_loc_descr_ref loc;
6921 rtx r;
6923 CHECKSUM (at->dw_attr);
6925 /* We don't care that this was compiled with a different compiler
6926 snapshot; if the output is the same, that's what matters. */
6927 if (at->dw_attr == DW_AT_producer)
6928 return;
6930 switch (AT_class (at))
6932 case dw_val_class_const:
6933 case dw_val_class_const_implicit:
6934 CHECKSUM (at->dw_attr_val.v.val_int);
6935 break;
6936 case dw_val_class_unsigned_const:
6937 case dw_val_class_unsigned_const_implicit:
6938 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6939 break;
6940 case dw_val_class_const_double:
6941 CHECKSUM (at->dw_attr_val.v.val_double);
6942 break;
6943 case dw_val_class_wide_int:
6944 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6945 get_full_len (*at->dw_attr_val.v.val_wide)
6946 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6947 break;
6948 case dw_val_class_vec:
6949 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6950 (at->dw_attr_val.v.val_vec.length
6951 * at->dw_attr_val.v.val_vec.elt_size));
6952 break;
6953 case dw_val_class_flag:
6954 CHECKSUM (at->dw_attr_val.v.val_flag);
6955 break;
6956 case dw_val_class_str:
6957 CHECKSUM_STRING (AT_string (at));
6958 break;
6960 case dw_val_class_addr:
6961 r = AT_addr (at);
6962 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6963 CHECKSUM_STRING (XSTR (r, 0));
6964 break;
6966 case dw_val_class_offset:
6967 CHECKSUM (at->dw_attr_val.v.val_offset);
6968 break;
6970 case dw_val_class_loc:
6971 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6972 loc_checksum (loc, ctx);
6973 break;
6975 case dw_val_class_die_ref:
6976 die_checksum (AT_ref (at), ctx, mark);
6977 break;
6979 case dw_val_class_fde_ref:
6980 case dw_val_class_vms_delta:
6981 case dw_val_class_symview:
6982 case dw_val_class_lbl_id:
6983 case dw_val_class_lineptr:
6984 case dw_val_class_macptr:
6985 case dw_val_class_loclistsptr:
6986 case dw_val_class_high_pc:
6987 break;
6989 case dw_val_class_file:
6990 case dw_val_class_file_implicit:
6991 CHECKSUM_STRING (AT_file (at)->filename);
6992 break;
6994 case dw_val_class_data8:
6995 CHECKSUM (at->dw_attr_val.v.val_data8);
6996 break;
6998 default:
6999 break;
7003 /* Calculate the checksum of a DIE. */
7005 static void
7006 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7008 dw_die_ref c;
7009 dw_attr_node *a;
7010 unsigned ix;
7012 /* To avoid infinite recursion. */
7013 if (die->die_mark)
7015 CHECKSUM (die->die_mark);
7016 return;
7018 die->die_mark = ++(*mark);
7020 CHECKSUM (die->die_tag);
7022 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7023 attr_checksum (a, ctx, mark);
7025 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7028 #undef CHECKSUM
7029 #undef CHECKSUM_BLOCK
7030 #undef CHECKSUM_STRING
7032 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7033 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7034 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7035 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7036 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7037 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7038 #define CHECKSUM_ATTR(FOO) \
7039 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7041 /* Calculate the checksum of a number in signed LEB128 format. */
7043 static void
7044 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7046 unsigned char byte;
7047 bool more;
7049 while (1)
7051 byte = (value & 0x7f);
7052 value >>= 7;
7053 more = !((value == 0 && (byte & 0x40) == 0)
7054 || (value == -1 && (byte & 0x40) != 0));
7055 if (more)
7056 byte |= 0x80;
7057 CHECKSUM (byte);
7058 if (!more)
7059 break;
7063 /* Calculate the checksum of a number in unsigned LEB128 format. */
7065 static void
7066 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7068 while (1)
7070 unsigned char byte = (value & 0x7f);
7071 value >>= 7;
7072 if (value != 0)
7073 /* More bytes to follow. */
7074 byte |= 0x80;
7075 CHECKSUM (byte);
7076 if (value == 0)
7077 break;
7081 /* Checksum the context of the DIE. This adds the names of any
7082 surrounding namespaces or structures to the checksum. */
7084 static void
7085 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7087 const char *name;
7088 dw_die_ref spec;
7089 int tag = die->die_tag;
7091 if (tag != DW_TAG_namespace
7092 && tag != DW_TAG_structure_type
7093 && tag != DW_TAG_class_type)
7094 return;
7096 name = get_AT_string (die, DW_AT_name);
7098 spec = get_AT_ref (die, DW_AT_specification);
7099 if (spec != NULL)
7100 die = spec;
7102 if (die->die_parent != NULL)
7103 checksum_die_context (die->die_parent, ctx);
7105 CHECKSUM_ULEB128 ('C');
7106 CHECKSUM_ULEB128 (tag);
7107 if (name != NULL)
7108 CHECKSUM_STRING (name);
7111 /* Calculate the checksum of a location expression. */
7113 static inline void
7114 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7116 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7117 were emitted as a DW_FORM_sdata instead of a location expression. */
7118 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7120 CHECKSUM_ULEB128 (DW_FORM_sdata);
7121 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7122 return;
7125 /* Otherwise, just checksum the raw location expression. */
7126 while (loc != NULL)
7128 inchash::hash hstate;
7129 hashval_t hash;
7131 CHECKSUM_ULEB128 (loc->dtprel);
7132 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7133 hash_loc_operands (loc, hstate);
7134 hash = hstate.end ();
7135 CHECKSUM (hash);
7136 loc = loc->dw_loc_next;
7140 /* Calculate the checksum of an attribute. */
7142 static void
7143 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7144 struct md5_ctx *ctx, int *mark)
7146 dw_loc_descr_ref loc;
7147 rtx r;
7149 if (AT_class (at) == dw_val_class_die_ref)
7151 dw_die_ref target_die = AT_ref (at);
7153 /* For pointer and reference types, we checksum only the (qualified)
7154 name of the target type (if there is a name). For friend entries,
7155 we checksum only the (qualified) name of the target type or function.
7156 This allows the checksum to remain the same whether the target type
7157 is complete or not. */
7158 if ((at->dw_attr == DW_AT_type
7159 && (tag == DW_TAG_pointer_type
7160 || tag == DW_TAG_reference_type
7161 || tag == DW_TAG_rvalue_reference_type
7162 || tag == DW_TAG_ptr_to_member_type))
7163 || (at->dw_attr == DW_AT_friend
7164 && tag == DW_TAG_friend))
7166 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7168 if (name_attr != NULL)
7170 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7172 if (decl == NULL)
7173 decl = target_die;
7174 CHECKSUM_ULEB128 ('N');
7175 CHECKSUM_ULEB128 (at->dw_attr);
7176 if (decl->die_parent != NULL)
7177 checksum_die_context (decl->die_parent, ctx);
7178 CHECKSUM_ULEB128 ('E');
7179 CHECKSUM_STRING (AT_string (name_attr));
7180 return;
7184 /* For all other references to another DIE, we check to see if the
7185 target DIE has already been visited. If it has, we emit a
7186 backward reference; if not, we descend recursively. */
7187 if (target_die->die_mark > 0)
7189 CHECKSUM_ULEB128 ('R');
7190 CHECKSUM_ULEB128 (at->dw_attr);
7191 CHECKSUM_ULEB128 (target_die->die_mark);
7193 else
7195 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7197 if (decl == NULL)
7198 decl = target_die;
7199 target_die->die_mark = ++(*mark);
7200 CHECKSUM_ULEB128 ('T');
7201 CHECKSUM_ULEB128 (at->dw_attr);
7202 if (decl->die_parent != NULL)
7203 checksum_die_context (decl->die_parent, ctx);
7204 die_checksum_ordered (target_die, ctx, mark);
7206 return;
7209 CHECKSUM_ULEB128 ('A');
7210 CHECKSUM_ULEB128 (at->dw_attr);
7212 switch (AT_class (at))
7214 case dw_val_class_const:
7215 case dw_val_class_const_implicit:
7216 CHECKSUM_ULEB128 (DW_FORM_sdata);
7217 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7218 break;
7220 case dw_val_class_unsigned_const:
7221 case dw_val_class_unsigned_const_implicit:
7222 CHECKSUM_ULEB128 (DW_FORM_sdata);
7223 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7224 break;
7226 case dw_val_class_const_double:
7227 CHECKSUM_ULEB128 (DW_FORM_block);
7228 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7229 CHECKSUM (at->dw_attr_val.v.val_double);
7230 break;
7232 case dw_val_class_wide_int:
7233 CHECKSUM_ULEB128 (DW_FORM_block);
7234 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7235 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7236 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7237 get_full_len (*at->dw_attr_val.v.val_wide)
7238 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7239 break;
7241 case dw_val_class_vec:
7242 CHECKSUM_ULEB128 (DW_FORM_block);
7243 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7244 * at->dw_attr_val.v.val_vec.elt_size);
7245 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7246 (at->dw_attr_val.v.val_vec.length
7247 * at->dw_attr_val.v.val_vec.elt_size));
7248 break;
7250 case dw_val_class_flag:
7251 CHECKSUM_ULEB128 (DW_FORM_flag);
7252 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7253 break;
7255 case dw_val_class_str:
7256 CHECKSUM_ULEB128 (DW_FORM_string);
7257 CHECKSUM_STRING (AT_string (at));
7258 break;
7260 case dw_val_class_addr:
7261 r = AT_addr (at);
7262 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7263 CHECKSUM_ULEB128 (DW_FORM_string);
7264 CHECKSUM_STRING (XSTR (r, 0));
7265 break;
7267 case dw_val_class_offset:
7268 CHECKSUM_ULEB128 (DW_FORM_sdata);
7269 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7270 break;
7272 case dw_val_class_loc:
7273 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7274 loc_checksum_ordered (loc, ctx);
7275 break;
7277 case dw_val_class_fde_ref:
7278 case dw_val_class_symview:
7279 case dw_val_class_lbl_id:
7280 case dw_val_class_lineptr:
7281 case dw_val_class_macptr:
7282 case dw_val_class_loclistsptr:
7283 case dw_val_class_high_pc:
7284 break;
7286 case dw_val_class_file:
7287 case dw_val_class_file_implicit:
7288 CHECKSUM_ULEB128 (DW_FORM_string);
7289 CHECKSUM_STRING (AT_file (at)->filename);
7290 break;
7292 case dw_val_class_data8:
7293 CHECKSUM (at->dw_attr_val.v.val_data8);
7294 break;
7296 default:
7297 break;
7301 struct checksum_attributes
7303 dw_attr_node *at_name;
7304 dw_attr_node *at_type;
7305 dw_attr_node *at_friend;
7306 dw_attr_node *at_accessibility;
7307 dw_attr_node *at_address_class;
7308 dw_attr_node *at_alignment;
7309 dw_attr_node *at_allocated;
7310 dw_attr_node *at_artificial;
7311 dw_attr_node *at_associated;
7312 dw_attr_node *at_binary_scale;
7313 dw_attr_node *at_bit_offset;
7314 dw_attr_node *at_bit_size;
7315 dw_attr_node *at_bit_stride;
7316 dw_attr_node *at_byte_size;
7317 dw_attr_node *at_byte_stride;
7318 dw_attr_node *at_const_value;
7319 dw_attr_node *at_containing_type;
7320 dw_attr_node *at_count;
7321 dw_attr_node *at_data_location;
7322 dw_attr_node *at_data_member_location;
7323 dw_attr_node *at_decimal_scale;
7324 dw_attr_node *at_decimal_sign;
7325 dw_attr_node *at_default_value;
7326 dw_attr_node *at_digit_count;
7327 dw_attr_node *at_discr;
7328 dw_attr_node *at_discr_list;
7329 dw_attr_node *at_discr_value;
7330 dw_attr_node *at_encoding;
7331 dw_attr_node *at_endianity;
7332 dw_attr_node *at_explicit;
7333 dw_attr_node *at_is_optional;
7334 dw_attr_node *at_location;
7335 dw_attr_node *at_lower_bound;
7336 dw_attr_node *at_mutable;
7337 dw_attr_node *at_ordering;
7338 dw_attr_node *at_picture_string;
7339 dw_attr_node *at_prototyped;
7340 dw_attr_node *at_small;
7341 dw_attr_node *at_segment;
7342 dw_attr_node *at_string_length;
7343 dw_attr_node *at_string_length_bit_size;
7344 dw_attr_node *at_string_length_byte_size;
7345 dw_attr_node *at_threads_scaled;
7346 dw_attr_node *at_upper_bound;
7347 dw_attr_node *at_use_location;
7348 dw_attr_node *at_use_UTF8;
7349 dw_attr_node *at_variable_parameter;
7350 dw_attr_node *at_virtuality;
7351 dw_attr_node *at_visibility;
7352 dw_attr_node *at_vtable_elem_location;
7355 /* Collect the attributes that we will want to use for the checksum. */
7357 static void
7358 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7360 dw_attr_node *a;
7361 unsigned ix;
7363 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7365 switch (a->dw_attr)
7367 case DW_AT_name:
7368 attrs->at_name = a;
7369 break;
7370 case DW_AT_type:
7371 attrs->at_type = a;
7372 break;
7373 case DW_AT_friend:
7374 attrs->at_friend = a;
7375 break;
7376 case DW_AT_accessibility:
7377 attrs->at_accessibility = a;
7378 break;
7379 case DW_AT_address_class:
7380 attrs->at_address_class = a;
7381 break;
7382 case DW_AT_alignment:
7383 attrs->at_alignment = a;
7384 break;
7385 case DW_AT_allocated:
7386 attrs->at_allocated = a;
7387 break;
7388 case DW_AT_artificial:
7389 attrs->at_artificial = a;
7390 break;
7391 case DW_AT_associated:
7392 attrs->at_associated = a;
7393 break;
7394 case DW_AT_binary_scale:
7395 attrs->at_binary_scale = a;
7396 break;
7397 case DW_AT_bit_offset:
7398 attrs->at_bit_offset = a;
7399 break;
7400 case DW_AT_bit_size:
7401 attrs->at_bit_size = a;
7402 break;
7403 case DW_AT_bit_stride:
7404 attrs->at_bit_stride = a;
7405 break;
7406 case DW_AT_byte_size:
7407 attrs->at_byte_size = a;
7408 break;
7409 case DW_AT_byte_stride:
7410 attrs->at_byte_stride = a;
7411 break;
7412 case DW_AT_const_value:
7413 attrs->at_const_value = a;
7414 break;
7415 case DW_AT_containing_type:
7416 attrs->at_containing_type = a;
7417 break;
7418 case DW_AT_count:
7419 attrs->at_count = a;
7420 break;
7421 case DW_AT_data_location:
7422 attrs->at_data_location = a;
7423 break;
7424 case DW_AT_data_member_location:
7425 attrs->at_data_member_location = a;
7426 break;
7427 case DW_AT_decimal_scale:
7428 attrs->at_decimal_scale = a;
7429 break;
7430 case DW_AT_decimal_sign:
7431 attrs->at_decimal_sign = a;
7432 break;
7433 case DW_AT_default_value:
7434 attrs->at_default_value = a;
7435 break;
7436 case DW_AT_digit_count:
7437 attrs->at_digit_count = a;
7438 break;
7439 case DW_AT_discr:
7440 attrs->at_discr = a;
7441 break;
7442 case DW_AT_discr_list:
7443 attrs->at_discr_list = a;
7444 break;
7445 case DW_AT_discr_value:
7446 attrs->at_discr_value = a;
7447 break;
7448 case DW_AT_encoding:
7449 attrs->at_encoding = a;
7450 break;
7451 case DW_AT_endianity:
7452 attrs->at_endianity = a;
7453 break;
7454 case DW_AT_explicit:
7455 attrs->at_explicit = a;
7456 break;
7457 case DW_AT_is_optional:
7458 attrs->at_is_optional = a;
7459 break;
7460 case DW_AT_location:
7461 attrs->at_location = a;
7462 break;
7463 case DW_AT_lower_bound:
7464 attrs->at_lower_bound = a;
7465 break;
7466 case DW_AT_mutable:
7467 attrs->at_mutable = a;
7468 break;
7469 case DW_AT_ordering:
7470 attrs->at_ordering = a;
7471 break;
7472 case DW_AT_picture_string:
7473 attrs->at_picture_string = a;
7474 break;
7475 case DW_AT_prototyped:
7476 attrs->at_prototyped = a;
7477 break;
7478 case DW_AT_small:
7479 attrs->at_small = a;
7480 break;
7481 case DW_AT_segment:
7482 attrs->at_segment = a;
7483 break;
7484 case DW_AT_string_length:
7485 attrs->at_string_length = a;
7486 break;
7487 case DW_AT_string_length_bit_size:
7488 attrs->at_string_length_bit_size = a;
7489 break;
7490 case DW_AT_string_length_byte_size:
7491 attrs->at_string_length_byte_size = a;
7492 break;
7493 case DW_AT_threads_scaled:
7494 attrs->at_threads_scaled = a;
7495 break;
7496 case DW_AT_upper_bound:
7497 attrs->at_upper_bound = a;
7498 break;
7499 case DW_AT_use_location:
7500 attrs->at_use_location = a;
7501 break;
7502 case DW_AT_use_UTF8:
7503 attrs->at_use_UTF8 = a;
7504 break;
7505 case DW_AT_variable_parameter:
7506 attrs->at_variable_parameter = a;
7507 break;
7508 case DW_AT_virtuality:
7509 attrs->at_virtuality = a;
7510 break;
7511 case DW_AT_visibility:
7512 attrs->at_visibility = a;
7513 break;
7514 case DW_AT_vtable_elem_location:
7515 attrs->at_vtable_elem_location = a;
7516 break;
7517 default:
7518 break;
7523 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7525 static void
7526 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7528 dw_die_ref c;
7529 dw_die_ref decl;
7530 struct checksum_attributes attrs;
7532 CHECKSUM_ULEB128 ('D');
7533 CHECKSUM_ULEB128 (die->die_tag);
7535 memset (&attrs, 0, sizeof (attrs));
7537 decl = get_AT_ref (die, DW_AT_specification);
7538 if (decl != NULL)
7539 collect_checksum_attributes (&attrs, decl);
7540 collect_checksum_attributes (&attrs, die);
7542 CHECKSUM_ATTR (attrs.at_name);
7543 CHECKSUM_ATTR (attrs.at_accessibility);
7544 CHECKSUM_ATTR (attrs.at_address_class);
7545 CHECKSUM_ATTR (attrs.at_allocated);
7546 CHECKSUM_ATTR (attrs.at_artificial);
7547 CHECKSUM_ATTR (attrs.at_associated);
7548 CHECKSUM_ATTR (attrs.at_binary_scale);
7549 CHECKSUM_ATTR (attrs.at_bit_offset);
7550 CHECKSUM_ATTR (attrs.at_bit_size);
7551 CHECKSUM_ATTR (attrs.at_bit_stride);
7552 CHECKSUM_ATTR (attrs.at_byte_size);
7553 CHECKSUM_ATTR (attrs.at_byte_stride);
7554 CHECKSUM_ATTR (attrs.at_const_value);
7555 CHECKSUM_ATTR (attrs.at_containing_type);
7556 CHECKSUM_ATTR (attrs.at_count);
7557 CHECKSUM_ATTR (attrs.at_data_location);
7558 CHECKSUM_ATTR (attrs.at_data_member_location);
7559 CHECKSUM_ATTR (attrs.at_decimal_scale);
7560 CHECKSUM_ATTR (attrs.at_decimal_sign);
7561 CHECKSUM_ATTR (attrs.at_default_value);
7562 CHECKSUM_ATTR (attrs.at_digit_count);
7563 CHECKSUM_ATTR (attrs.at_discr);
7564 CHECKSUM_ATTR (attrs.at_discr_list);
7565 CHECKSUM_ATTR (attrs.at_discr_value);
7566 CHECKSUM_ATTR (attrs.at_encoding);
7567 CHECKSUM_ATTR (attrs.at_endianity);
7568 CHECKSUM_ATTR (attrs.at_explicit);
7569 CHECKSUM_ATTR (attrs.at_is_optional);
7570 CHECKSUM_ATTR (attrs.at_location);
7571 CHECKSUM_ATTR (attrs.at_lower_bound);
7572 CHECKSUM_ATTR (attrs.at_mutable);
7573 CHECKSUM_ATTR (attrs.at_ordering);
7574 CHECKSUM_ATTR (attrs.at_picture_string);
7575 CHECKSUM_ATTR (attrs.at_prototyped);
7576 CHECKSUM_ATTR (attrs.at_small);
7577 CHECKSUM_ATTR (attrs.at_segment);
7578 CHECKSUM_ATTR (attrs.at_string_length);
7579 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7580 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7581 CHECKSUM_ATTR (attrs.at_threads_scaled);
7582 CHECKSUM_ATTR (attrs.at_upper_bound);
7583 CHECKSUM_ATTR (attrs.at_use_location);
7584 CHECKSUM_ATTR (attrs.at_use_UTF8);
7585 CHECKSUM_ATTR (attrs.at_variable_parameter);
7586 CHECKSUM_ATTR (attrs.at_virtuality);
7587 CHECKSUM_ATTR (attrs.at_visibility);
7588 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7589 CHECKSUM_ATTR (attrs.at_type);
7590 CHECKSUM_ATTR (attrs.at_friend);
7591 CHECKSUM_ATTR (attrs.at_alignment);
7593 /* Checksum the child DIEs. */
7594 c = die->die_child;
7595 if (c) do {
7596 dw_attr_node *name_attr;
7598 c = c->die_sib;
7599 name_attr = get_AT (c, DW_AT_name);
7600 if (is_template_instantiation (c))
7602 /* Ignore instantiations of member type and function templates. */
7604 else if (name_attr != NULL
7605 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7607 /* Use a shallow checksum for named nested types and member
7608 functions. */
7609 CHECKSUM_ULEB128 ('S');
7610 CHECKSUM_ULEB128 (c->die_tag);
7611 CHECKSUM_STRING (AT_string (name_attr));
7613 else
7615 /* Use a deep checksum for other children. */
7616 /* Mark this DIE so it gets processed when unmarking. */
7617 if (c->die_mark == 0)
7618 c->die_mark = -1;
7619 die_checksum_ordered (c, ctx, mark);
7621 } while (c != die->die_child);
7623 CHECKSUM_ULEB128 (0);
7626 /* Add a type name and tag to a hash. */
7627 static void
7628 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7630 CHECKSUM_ULEB128 (tag);
7631 CHECKSUM_STRING (name);
7634 #undef CHECKSUM
7635 #undef CHECKSUM_STRING
7636 #undef CHECKSUM_ATTR
7637 #undef CHECKSUM_LEB128
7638 #undef CHECKSUM_ULEB128
7640 /* Generate the type signature for DIE. This is computed by generating an
7641 MD5 checksum over the DIE's tag, its relevant attributes, and its
7642 children. Attributes that are references to other DIEs are processed
7643 by recursion, using the MARK field to prevent infinite recursion.
7644 If the DIE is nested inside a namespace or another type, we also
7645 need to include that context in the signature. The lower 64 bits
7646 of the resulting MD5 checksum comprise the signature. */
7648 static void
7649 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7651 int mark;
7652 const char *name;
7653 unsigned char checksum[16];
7654 struct md5_ctx ctx;
7655 dw_die_ref decl;
7656 dw_die_ref parent;
7658 name = get_AT_string (die, DW_AT_name);
7659 decl = get_AT_ref (die, DW_AT_specification);
7660 parent = get_die_parent (die);
7662 /* First, compute a signature for just the type name (and its surrounding
7663 context, if any. This is stored in the type unit DIE for link-time
7664 ODR (one-definition rule) checking. */
7666 if (is_cxx () && name != NULL)
7668 md5_init_ctx (&ctx);
7670 /* Checksum the names of surrounding namespaces and structures. */
7671 if (parent != NULL)
7672 checksum_die_context (parent, &ctx);
7674 /* Checksum the current DIE. */
7675 die_odr_checksum (die->die_tag, name, &ctx);
7676 md5_finish_ctx (&ctx, checksum);
7678 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7681 /* Next, compute the complete type signature. */
7683 md5_init_ctx (&ctx);
7684 mark = 1;
7685 die->die_mark = mark;
7687 /* Checksum the names of surrounding namespaces and structures. */
7688 if (parent != NULL)
7689 checksum_die_context (parent, &ctx);
7691 /* Checksum the DIE and its children. */
7692 die_checksum_ordered (die, &ctx, &mark);
7693 unmark_all_dies (die);
7694 md5_finish_ctx (&ctx, checksum);
7696 /* Store the signature in the type node and link the type DIE and the
7697 type node together. */
7698 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7699 DWARF_TYPE_SIGNATURE_SIZE);
7700 die->comdat_type_p = true;
7701 die->die_id.die_type_node = type_node;
7702 type_node->type_die = die;
7704 /* If the DIE is a specification, link its declaration to the type node
7705 as well. */
7706 if (decl != NULL)
7708 decl->comdat_type_p = true;
7709 decl->die_id.die_type_node = type_node;
7713 /* Do the location expressions look same? */
7714 static inline int
7715 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7717 return loc1->dw_loc_opc == loc2->dw_loc_opc
7718 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7719 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7722 /* Do the values look the same? */
7723 static int
7724 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7726 dw_loc_descr_ref loc1, loc2;
7727 rtx r1, r2;
7729 if (v1->val_class != v2->val_class)
7730 return 0;
7732 switch (v1->val_class)
7734 case dw_val_class_const:
7735 case dw_val_class_const_implicit:
7736 return v1->v.val_int == v2->v.val_int;
7737 case dw_val_class_unsigned_const:
7738 case dw_val_class_unsigned_const_implicit:
7739 return v1->v.val_unsigned == v2->v.val_unsigned;
7740 case dw_val_class_const_double:
7741 return v1->v.val_double.high == v2->v.val_double.high
7742 && v1->v.val_double.low == v2->v.val_double.low;
7743 case dw_val_class_wide_int:
7744 return *v1->v.val_wide == *v2->v.val_wide;
7745 case dw_val_class_vec:
7746 if (v1->v.val_vec.length != v2->v.val_vec.length
7747 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7748 return 0;
7749 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7750 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7751 return 0;
7752 return 1;
7753 case dw_val_class_flag:
7754 return v1->v.val_flag == v2->v.val_flag;
7755 case dw_val_class_str:
7756 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7758 case dw_val_class_addr:
7759 r1 = v1->v.val_addr;
7760 r2 = v2->v.val_addr;
7761 if (GET_CODE (r1) != GET_CODE (r2))
7762 return 0;
7763 return !rtx_equal_p (r1, r2);
7765 case dw_val_class_offset:
7766 return v1->v.val_offset == v2->v.val_offset;
7768 case dw_val_class_loc:
7769 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7770 loc1 && loc2;
7771 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7772 if (!same_loc_p (loc1, loc2, mark))
7773 return 0;
7774 return !loc1 && !loc2;
7776 case dw_val_class_die_ref:
7777 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7779 case dw_val_class_symview:
7780 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7782 case dw_val_class_fde_ref:
7783 case dw_val_class_vms_delta:
7784 case dw_val_class_lbl_id:
7785 case dw_val_class_lineptr:
7786 case dw_val_class_macptr:
7787 case dw_val_class_loclistsptr:
7788 case dw_val_class_high_pc:
7789 return 1;
7791 case dw_val_class_file:
7792 case dw_val_class_file_implicit:
7793 return v1->v.val_file == v2->v.val_file;
7795 case dw_val_class_data8:
7796 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7798 default:
7799 return 1;
7803 /* Do the attributes look the same? */
7805 static int
7806 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7808 if (at1->dw_attr != at2->dw_attr)
7809 return 0;
7811 /* We don't care that this was compiled with a different compiler
7812 snapshot; if the output is the same, that's what matters. */
7813 if (at1->dw_attr == DW_AT_producer)
7814 return 1;
7816 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7819 /* Do the dies look the same? */
7821 static int
7822 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7824 dw_die_ref c1, c2;
7825 dw_attr_node *a1;
7826 unsigned ix;
7828 /* To avoid infinite recursion. */
7829 if (die1->die_mark)
7830 return die1->die_mark == die2->die_mark;
7831 die1->die_mark = die2->die_mark = ++(*mark);
7833 if (die1->die_tag != die2->die_tag)
7834 return 0;
7836 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7837 return 0;
7839 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7840 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7841 return 0;
7843 c1 = die1->die_child;
7844 c2 = die2->die_child;
7845 if (! c1)
7847 if (c2)
7848 return 0;
7850 else
7851 for (;;)
7853 if (!same_die_p (c1, c2, mark))
7854 return 0;
7855 c1 = c1->die_sib;
7856 c2 = c2->die_sib;
7857 if (c1 == die1->die_child)
7859 if (c2 == die2->die_child)
7860 break;
7861 else
7862 return 0;
7866 return 1;
7869 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7870 children, and set die_symbol. */
7872 static void
7873 compute_comp_unit_symbol (dw_die_ref unit_die)
7875 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7876 const char *base = die_name ? lbasename (die_name) : "anonymous";
7877 char *name = XALLOCAVEC (char, strlen (base) + 64);
7878 char *p;
7879 int i, mark;
7880 unsigned char checksum[16];
7881 struct md5_ctx ctx;
7883 /* Compute the checksum of the DIE, then append part of it as hex digits to
7884 the name filename of the unit. */
7886 md5_init_ctx (&ctx);
7887 mark = 0;
7888 die_checksum (unit_die, &ctx, &mark);
7889 unmark_all_dies (unit_die);
7890 md5_finish_ctx (&ctx, checksum);
7892 /* When we this for comp_unit_die () we have a DW_AT_name that might
7893 not start with a letter but with anything valid for filenames and
7894 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7895 character is not a letter. */
7896 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7897 clean_symbol_name (name);
7899 p = name + strlen (name);
7900 for (i = 0; i < 4; i++)
7902 sprintf (p, "%.2x", checksum[i]);
7903 p += 2;
7906 unit_die->die_id.die_symbol = xstrdup (name);
7909 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7911 static int
7912 is_type_die (dw_die_ref die)
7914 switch (die->die_tag)
7916 case DW_TAG_array_type:
7917 case DW_TAG_class_type:
7918 case DW_TAG_interface_type:
7919 case DW_TAG_enumeration_type:
7920 case DW_TAG_pointer_type:
7921 case DW_TAG_reference_type:
7922 case DW_TAG_rvalue_reference_type:
7923 case DW_TAG_string_type:
7924 case DW_TAG_structure_type:
7925 case DW_TAG_subroutine_type:
7926 case DW_TAG_union_type:
7927 case DW_TAG_ptr_to_member_type:
7928 case DW_TAG_set_type:
7929 case DW_TAG_subrange_type:
7930 case DW_TAG_base_type:
7931 case DW_TAG_const_type:
7932 case DW_TAG_file_type:
7933 case DW_TAG_packed_type:
7934 case DW_TAG_volatile_type:
7935 case DW_TAG_typedef:
7936 return 1;
7937 default:
7938 return 0;
7942 /* Returns true iff C is a compile-unit DIE. */
7944 static inline bool
7945 is_cu_die (dw_die_ref c)
7947 return c && (c->die_tag == DW_TAG_compile_unit
7948 || c->die_tag == DW_TAG_skeleton_unit);
7951 /* Returns true iff C is a unit DIE of some sort. */
7953 static inline bool
7954 is_unit_die (dw_die_ref c)
7956 return c && (c->die_tag == DW_TAG_compile_unit
7957 || c->die_tag == DW_TAG_partial_unit
7958 || c->die_tag == DW_TAG_type_unit
7959 || c->die_tag == DW_TAG_skeleton_unit);
7962 /* Returns true iff C is a namespace DIE. */
7964 static inline bool
7965 is_namespace_die (dw_die_ref c)
7967 return c && c->die_tag == DW_TAG_namespace;
7970 /* Return non-zero if this DIE is a template parameter. */
7972 static inline bool
7973 is_template_parameter (dw_die_ref die)
7975 switch (die->die_tag)
7977 case DW_TAG_template_type_param:
7978 case DW_TAG_template_value_param:
7979 case DW_TAG_GNU_template_template_param:
7980 case DW_TAG_GNU_template_parameter_pack:
7981 return true;
7982 default:
7983 return false;
7987 /* Return non-zero if this DIE represents a template instantiation. */
7989 static inline bool
7990 is_template_instantiation (dw_die_ref die)
7992 dw_die_ref c;
7994 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7995 return false;
7996 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7997 return false;
8000 static char *
8001 gen_internal_sym (const char *prefix)
8003 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8005 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8006 return xstrdup (buf);
8009 /* Return non-zero if this DIE is a declaration. */
8011 static int
8012 is_declaration_die (dw_die_ref die)
8014 dw_attr_node *a;
8015 unsigned ix;
8017 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8018 if (a->dw_attr == DW_AT_declaration)
8019 return 1;
8021 return 0;
8024 /* Return non-zero if this DIE is nested inside a subprogram. */
8026 static int
8027 is_nested_in_subprogram (dw_die_ref die)
8029 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8031 if (decl == NULL)
8032 decl = die;
8033 return local_scope_p (decl);
8036 /* Return non-zero if this DIE contains a defining declaration of a
8037 subprogram. */
8039 static int
8040 contains_subprogram_definition (dw_die_ref die)
8042 dw_die_ref c;
8044 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8045 return 1;
8046 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8047 return 0;
8050 /* Return non-zero if this is a type DIE that should be moved to a
8051 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8052 unit type. */
8054 static int
8055 should_move_die_to_comdat (dw_die_ref die)
8057 switch (die->die_tag)
8059 case DW_TAG_class_type:
8060 case DW_TAG_structure_type:
8061 case DW_TAG_enumeration_type:
8062 case DW_TAG_union_type:
8063 /* Don't move declarations, inlined instances, types nested in a
8064 subprogram, or types that contain subprogram definitions. */
8065 if (is_declaration_die (die)
8066 || get_AT (die, DW_AT_abstract_origin)
8067 || is_nested_in_subprogram (die)
8068 || contains_subprogram_definition (die))
8069 return 0;
8070 return 1;
8071 case DW_TAG_array_type:
8072 case DW_TAG_interface_type:
8073 case DW_TAG_pointer_type:
8074 case DW_TAG_reference_type:
8075 case DW_TAG_rvalue_reference_type:
8076 case DW_TAG_string_type:
8077 case DW_TAG_subroutine_type:
8078 case DW_TAG_ptr_to_member_type:
8079 case DW_TAG_set_type:
8080 case DW_TAG_subrange_type:
8081 case DW_TAG_base_type:
8082 case DW_TAG_const_type:
8083 case DW_TAG_file_type:
8084 case DW_TAG_packed_type:
8085 case DW_TAG_volatile_type:
8086 case DW_TAG_typedef:
8087 default:
8088 return 0;
8092 /* Make a clone of DIE. */
8094 static dw_die_ref
8095 clone_die (dw_die_ref die)
8097 dw_die_ref clone = new_die_raw (die->die_tag);
8098 dw_attr_node *a;
8099 unsigned ix;
8101 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8102 add_dwarf_attr (clone, a);
8104 return clone;
8107 /* Make a clone of the tree rooted at DIE. */
8109 static dw_die_ref
8110 clone_tree (dw_die_ref die)
8112 dw_die_ref c;
8113 dw_die_ref clone = clone_die (die);
8115 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8117 return clone;
8120 /* Make a clone of DIE as a declaration. */
8122 static dw_die_ref
8123 clone_as_declaration (dw_die_ref die)
8125 dw_die_ref clone;
8126 dw_die_ref decl;
8127 dw_attr_node *a;
8128 unsigned ix;
8130 /* If the DIE is already a declaration, just clone it. */
8131 if (is_declaration_die (die))
8132 return clone_die (die);
8134 /* If the DIE is a specification, just clone its declaration DIE. */
8135 decl = get_AT_ref (die, DW_AT_specification);
8136 if (decl != NULL)
8138 clone = clone_die (decl);
8139 if (die->comdat_type_p)
8140 add_AT_die_ref (clone, DW_AT_signature, die);
8141 return clone;
8144 clone = new_die_raw (die->die_tag);
8146 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8148 /* We don't want to copy over all attributes.
8149 For example we don't want DW_AT_byte_size because otherwise we will no
8150 longer have a declaration and GDB will treat it as a definition. */
8152 switch (a->dw_attr)
8154 case DW_AT_abstract_origin:
8155 case DW_AT_artificial:
8156 case DW_AT_containing_type:
8157 case DW_AT_external:
8158 case DW_AT_name:
8159 case DW_AT_type:
8160 case DW_AT_virtuality:
8161 case DW_AT_linkage_name:
8162 case DW_AT_MIPS_linkage_name:
8163 add_dwarf_attr (clone, a);
8164 break;
8165 case DW_AT_byte_size:
8166 case DW_AT_alignment:
8167 default:
8168 break;
8172 if (die->comdat_type_p)
8173 add_AT_die_ref (clone, DW_AT_signature, die);
8175 add_AT_flag (clone, DW_AT_declaration, 1);
8176 return clone;
8180 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8182 struct decl_table_entry
8184 dw_die_ref orig;
8185 dw_die_ref copy;
8188 /* Helpers to manipulate hash table of copied declarations. */
8190 /* Hashtable helpers. */
8192 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8194 typedef die_struct *compare_type;
8195 static inline hashval_t hash (const decl_table_entry *);
8196 static inline bool equal (const decl_table_entry *, const die_struct *);
8199 inline hashval_t
8200 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8202 return htab_hash_pointer (entry->orig);
8205 inline bool
8206 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8207 const die_struct *entry2)
8209 return entry1->orig == entry2;
8212 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8214 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8215 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8216 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8217 to check if the ancestor has already been copied into UNIT. */
8219 static dw_die_ref
8220 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8221 decl_hash_type *decl_table)
8223 dw_die_ref parent = die->die_parent;
8224 dw_die_ref new_parent = unit;
8225 dw_die_ref copy;
8226 decl_table_entry **slot = NULL;
8227 struct decl_table_entry *entry = NULL;
8229 /* If DIE refers to a stub unfold that so we get the appropriate
8230 DIE registered as orig in decl_table. */
8231 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8232 die = c;
8234 if (decl_table)
8236 /* Check if the entry has already been copied to UNIT. */
8237 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8238 INSERT);
8239 if (*slot != HTAB_EMPTY_ENTRY)
8241 entry = *slot;
8242 return entry->copy;
8245 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8246 entry = XCNEW (struct decl_table_entry);
8247 entry->orig = die;
8248 entry->copy = NULL;
8249 *slot = entry;
8252 if (parent != NULL)
8254 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8255 if (spec != NULL)
8256 parent = spec;
8257 if (!is_unit_die (parent))
8258 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8261 copy = clone_as_declaration (die);
8262 add_child_die (new_parent, copy);
8264 if (decl_table)
8266 /* Record the pointer to the copy. */
8267 entry->copy = copy;
8270 return copy;
8272 /* Copy the declaration context to the new type unit DIE. This includes
8273 any surrounding namespace or type declarations. If the DIE has an
8274 AT_specification attribute, it also includes attributes and children
8275 attached to the specification, and returns a pointer to the original
8276 parent of the declaration DIE. Returns NULL otherwise. */
8278 static dw_die_ref
8279 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8281 dw_die_ref decl;
8282 dw_die_ref new_decl;
8283 dw_die_ref orig_parent = NULL;
8285 decl = get_AT_ref (die, DW_AT_specification);
8286 if (decl == NULL)
8287 decl = die;
8288 else
8290 unsigned ix;
8291 dw_die_ref c;
8292 dw_attr_node *a;
8294 /* The original DIE will be changed to a declaration, and must
8295 be moved to be a child of the original declaration DIE. */
8296 orig_parent = decl->die_parent;
8298 /* Copy the type node pointer from the new DIE to the original
8299 declaration DIE so we can forward references later. */
8300 decl->comdat_type_p = true;
8301 decl->die_id.die_type_node = die->die_id.die_type_node;
8303 remove_AT (die, DW_AT_specification);
8305 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8307 if (a->dw_attr != DW_AT_name
8308 && a->dw_attr != DW_AT_declaration
8309 && a->dw_attr != DW_AT_external)
8310 add_dwarf_attr (die, a);
8313 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8316 if (decl->die_parent != NULL
8317 && !is_unit_die (decl->die_parent))
8319 new_decl = copy_ancestor_tree (unit, decl, NULL);
8320 if (new_decl != NULL)
8322 remove_AT (new_decl, DW_AT_signature);
8323 add_AT_specification (die, new_decl);
8327 return orig_parent;
8330 /* Generate the skeleton ancestor tree for the given NODE, then clone
8331 the DIE and add the clone into the tree. */
8333 static void
8334 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8336 if (node->new_die != NULL)
8337 return;
8339 node->new_die = clone_as_declaration (node->old_die);
8341 if (node->parent != NULL)
8343 generate_skeleton_ancestor_tree (node->parent);
8344 add_child_die (node->parent->new_die, node->new_die);
8348 /* Generate a skeleton tree of DIEs containing any declarations that are
8349 found in the original tree. We traverse the tree looking for declaration
8350 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8352 static void
8353 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8355 skeleton_chain_node node;
8356 dw_die_ref c;
8357 dw_die_ref first;
8358 dw_die_ref prev = NULL;
8359 dw_die_ref next = NULL;
8361 node.parent = parent;
8363 first = c = parent->old_die->die_child;
8364 if (c)
8365 next = c->die_sib;
8366 if (c) do {
8367 if (prev == NULL || prev->die_sib == c)
8368 prev = c;
8369 c = next;
8370 next = (c == first ? NULL : c->die_sib);
8371 node.old_die = c;
8372 node.new_die = NULL;
8373 if (is_declaration_die (c))
8375 if (is_template_instantiation (c))
8377 /* Instantiated templates do not need to be cloned into the
8378 type unit. Just move the DIE and its children back to
8379 the skeleton tree (in the main CU). */
8380 remove_child_with_prev (c, prev);
8381 add_child_die (parent->new_die, c);
8382 c = prev;
8384 else if (c->comdat_type_p)
8386 /* This is the skeleton of earlier break_out_comdat_types
8387 type. Clone the existing DIE, but keep the children
8388 under the original (which is in the main CU). */
8389 dw_die_ref clone = clone_die (c);
8391 replace_child (c, clone, prev);
8392 generate_skeleton_ancestor_tree (parent);
8393 add_child_die (parent->new_die, c);
8394 c = clone;
8395 continue;
8397 else
8399 /* Clone the existing DIE, move the original to the skeleton
8400 tree (which is in the main CU), and put the clone, with
8401 all the original's children, where the original came from
8402 (which is about to be moved to the type unit). */
8403 dw_die_ref clone = clone_die (c);
8404 move_all_children (c, clone);
8406 /* If the original has a DW_AT_object_pointer attribute,
8407 it would now point to a child DIE just moved to the
8408 cloned tree, so we need to remove that attribute from
8409 the original. */
8410 remove_AT (c, DW_AT_object_pointer);
8412 replace_child (c, clone, prev);
8413 generate_skeleton_ancestor_tree (parent);
8414 add_child_die (parent->new_die, c);
8415 node.old_die = clone;
8416 node.new_die = c;
8417 c = clone;
8420 generate_skeleton_bottom_up (&node);
8421 } while (next != NULL);
8424 /* Wrapper function for generate_skeleton_bottom_up. */
8426 static dw_die_ref
8427 generate_skeleton (dw_die_ref die)
8429 skeleton_chain_node node;
8431 node.old_die = die;
8432 node.new_die = NULL;
8433 node.parent = NULL;
8435 /* If this type definition is nested inside another type,
8436 and is not an instantiation of a template, always leave
8437 at least a declaration in its place. */
8438 if (die->die_parent != NULL
8439 && is_type_die (die->die_parent)
8440 && !is_template_instantiation (die))
8441 node.new_die = clone_as_declaration (die);
8443 generate_skeleton_bottom_up (&node);
8444 return node.new_die;
8447 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8448 declaration. The original DIE is moved to a new compile unit so that
8449 existing references to it follow it to the new location. If any of the
8450 original DIE's descendants is a declaration, we need to replace the
8451 original DIE with a skeleton tree and move the declarations back into the
8452 skeleton tree. */
8454 static dw_die_ref
8455 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8456 dw_die_ref prev)
8458 dw_die_ref skeleton, orig_parent;
8460 /* Copy the declaration context to the type unit DIE. If the returned
8461 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8462 that DIE. */
8463 orig_parent = copy_declaration_context (unit, child);
8465 skeleton = generate_skeleton (child);
8466 if (skeleton == NULL)
8467 remove_child_with_prev (child, prev);
8468 else
8470 skeleton->comdat_type_p = true;
8471 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8473 /* If the original DIE was a specification, we need to put
8474 the skeleton under the parent DIE of the declaration.
8475 This leaves the original declaration in the tree, but
8476 it will be pruned later since there are no longer any
8477 references to it. */
8478 if (orig_parent != NULL)
8480 remove_child_with_prev (child, prev);
8481 add_child_die (orig_parent, skeleton);
8483 else
8484 replace_child (child, skeleton, prev);
8487 return skeleton;
8490 static void
8491 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8492 comdat_type_node *type_node,
8493 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8495 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8496 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8497 DWARF procedure references in the DW_AT_location attribute. */
8499 static dw_die_ref
8500 copy_dwarf_procedure (dw_die_ref die,
8501 comdat_type_node *type_node,
8502 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8504 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8506 /* DWARF procedures are not supposed to have children... */
8507 gcc_assert (die->die_child == NULL);
8509 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8510 gcc_assert (vec_safe_length (die->die_attr) == 1
8511 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8513 /* Do not copy more than once DWARF procedures. */
8514 bool existed;
8515 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8516 if (existed)
8517 return die_copy;
8519 die_copy = clone_die (die);
8520 add_child_die (type_node->root_die, die_copy);
8521 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8522 return die_copy;
8525 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8526 procedures in DIE's attributes. */
8528 static void
8529 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8530 comdat_type_node *type_node,
8531 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8533 dw_attr_node *a;
8534 unsigned i;
8536 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8538 dw_loc_descr_ref loc;
8540 if (a->dw_attr_val.val_class != dw_val_class_loc)
8541 continue;
8543 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8545 switch (loc->dw_loc_opc)
8547 case DW_OP_call2:
8548 case DW_OP_call4:
8549 case DW_OP_call_ref:
8550 gcc_assert (loc->dw_loc_oprnd1.val_class
8551 == dw_val_class_die_ref);
8552 loc->dw_loc_oprnd1.v.val_die_ref.die
8553 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8554 type_node,
8555 copied_dwarf_procs);
8557 default:
8558 break;
8564 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8565 rewrite references to point to the copies.
8567 References are looked for in DIE's attributes and recursively in all its
8568 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8569 mapping from old DWARF procedures to their copy. It is used not to copy
8570 twice the same DWARF procedure under TYPE_NODE. */
8572 static void
8573 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8574 comdat_type_node *type_node,
8575 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8577 dw_die_ref c;
8579 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8580 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8581 type_node,
8582 copied_dwarf_procs));
8585 /* Traverse the DIE and set up additional .debug_types or .debug_info
8586 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8587 section. */
8589 static void
8590 break_out_comdat_types (dw_die_ref die)
8592 dw_die_ref c;
8593 dw_die_ref first;
8594 dw_die_ref prev = NULL;
8595 dw_die_ref next = NULL;
8596 dw_die_ref unit = NULL;
8598 first = c = die->die_child;
8599 if (c)
8600 next = c->die_sib;
8601 if (c) do {
8602 if (prev == NULL || prev->die_sib == c)
8603 prev = c;
8604 c = next;
8605 next = (c == first ? NULL : c->die_sib);
8606 if (should_move_die_to_comdat (c))
8608 dw_die_ref replacement;
8609 comdat_type_node *type_node;
8611 /* Break out nested types into their own type units. */
8612 break_out_comdat_types (c);
8614 /* Create a new type unit DIE as the root for the new tree. */
8615 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8616 add_AT_unsigned (unit, DW_AT_language,
8617 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8619 /* Add the new unit's type DIE into the comdat type list. */
8620 type_node = ggc_cleared_alloc<comdat_type_node> ();
8621 type_node->root_die = unit;
8622 type_node->next = comdat_type_list;
8623 comdat_type_list = type_node;
8625 /* Generate the type signature. */
8626 generate_type_signature (c, type_node);
8628 /* Copy the declaration context, attributes, and children of the
8629 declaration into the new type unit DIE, then remove this DIE
8630 from the main CU (or replace it with a skeleton if necessary). */
8631 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8632 type_node->skeleton_die = replacement;
8634 /* Add the DIE to the new compunit. */
8635 add_child_die (unit, c);
8637 /* Types can reference DWARF procedures for type size or data location
8638 expressions. Calls in DWARF expressions cannot target procedures
8639 that are not in the same section. So we must copy DWARF procedures
8640 along with this type and then rewrite references to them. */
8641 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8642 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8644 if (replacement != NULL)
8645 c = replacement;
8647 else if (c->die_tag == DW_TAG_namespace
8648 || c->die_tag == DW_TAG_class_type
8649 || c->die_tag == DW_TAG_structure_type
8650 || c->die_tag == DW_TAG_union_type)
8652 /* Look for nested types that can be broken out. */
8653 break_out_comdat_types (c);
8655 } while (next != NULL);
8658 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8659 Enter all the cloned children into the hash table decl_table. */
8661 static dw_die_ref
8662 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8664 dw_die_ref c;
8665 dw_die_ref clone;
8666 struct decl_table_entry *entry;
8667 decl_table_entry **slot;
8669 if (die->die_tag == DW_TAG_subprogram)
8670 clone = clone_as_declaration (die);
8671 else
8672 clone = clone_die (die);
8674 slot = decl_table->find_slot_with_hash (die,
8675 htab_hash_pointer (die), INSERT);
8677 /* Assert that DIE isn't in the hash table yet. If it would be there
8678 before, the ancestors would be necessarily there as well, therefore
8679 clone_tree_partial wouldn't be called. */
8680 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8682 entry = XCNEW (struct decl_table_entry);
8683 entry->orig = die;
8684 entry->copy = clone;
8685 *slot = entry;
8687 if (die->die_tag != DW_TAG_subprogram)
8688 FOR_EACH_CHILD (die, c,
8689 add_child_die (clone, clone_tree_partial (c, decl_table)));
8691 return clone;
8694 /* Walk the DIE and its children, looking for references to incomplete
8695 or trivial types that are unmarked (i.e., that are not in the current
8696 type_unit). */
8698 static void
8699 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8701 dw_die_ref c;
8702 dw_attr_node *a;
8703 unsigned ix;
8705 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8707 if (AT_class (a) == dw_val_class_die_ref)
8709 dw_die_ref targ = AT_ref (a);
8710 decl_table_entry **slot;
8711 struct decl_table_entry *entry;
8713 if (targ->die_mark != 0 || targ->comdat_type_p)
8714 continue;
8716 slot = decl_table->find_slot_with_hash (targ,
8717 htab_hash_pointer (targ),
8718 INSERT);
8720 if (*slot != HTAB_EMPTY_ENTRY)
8722 /* TARG has already been copied, so we just need to
8723 modify the reference to point to the copy. */
8724 entry = *slot;
8725 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8727 else
8729 dw_die_ref parent = unit;
8730 dw_die_ref copy = clone_die (targ);
8732 /* Record in DECL_TABLE that TARG has been copied.
8733 Need to do this now, before the recursive call,
8734 because DECL_TABLE may be expanded and SLOT
8735 would no longer be a valid pointer. */
8736 entry = XCNEW (struct decl_table_entry);
8737 entry->orig = targ;
8738 entry->copy = copy;
8739 *slot = entry;
8741 /* If TARG is not a declaration DIE, we need to copy its
8742 children. */
8743 if (!is_declaration_die (targ))
8745 FOR_EACH_CHILD (
8746 targ, c,
8747 add_child_die (copy,
8748 clone_tree_partial (c, decl_table)));
8751 /* Make sure the cloned tree is marked as part of the
8752 type unit. */
8753 mark_dies (copy);
8755 /* If TARG has surrounding context, copy its ancestor tree
8756 into the new type unit. */
8757 if (targ->die_parent != NULL
8758 && !is_unit_die (targ->die_parent))
8759 parent = copy_ancestor_tree (unit, targ->die_parent,
8760 decl_table);
8762 add_child_die (parent, copy);
8763 a->dw_attr_val.v.val_die_ref.die = copy;
8765 /* Make sure the newly-copied DIE is walked. If it was
8766 installed in a previously-added context, it won't
8767 get visited otherwise. */
8768 if (parent != unit)
8770 /* Find the highest point of the newly-added tree,
8771 mark each node along the way, and walk from there. */
8772 parent->die_mark = 1;
8773 while (parent->die_parent
8774 && parent->die_parent->die_mark == 0)
8776 parent = parent->die_parent;
8777 parent->die_mark = 1;
8779 copy_decls_walk (unit, parent, decl_table);
8785 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8788 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8789 and record them in DECL_TABLE. */
8791 static void
8792 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8794 dw_die_ref c;
8796 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8798 dw_die_ref targ = AT_ref (a);
8799 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8800 decl_table_entry **slot
8801 = decl_table->find_slot_with_hash (targ,
8802 htab_hash_pointer (targ),
8803 INSERT);
8804 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8805 /* Record in DECL_TABLE that TARG has been already copied
8806 by remove_child_or_replace_with_skeleton. */
8807 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8808 entry->orig = targ;
8809 entry->copy = die;
8810 *slot = entry;
8812 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8815 /* Copy declarations for "unworthy" types into the new comdat section.
8816 Incomplete types, modified types, and certain other types aren't broken
8817 out into comdat sections of their own, so they don't have a signature,
8818 and we need to copy the declaration into the same section so that we
8819 don't have an external reference. */
8821 static void
8822 copy_decls_for_unworthy_types (dw_die_ref unit)
8824 mark_dies (unit);
8825 decl_hash_type decl_table (10);
8826 collect_skeleton_dies (unit, &decl_table);
8827 copy_decls_walk (unit, unit, &decl_table);
8828 unmark_dies (unit);
8831 /* Traverse the DIE and add a sibling attribute if it may have the
8832 effect of speeding up access to siblings. To save some space,
8833 avoid generating sibling attributes for DIE's without children. */
8835 static void
8836 add_sibling_attributes (dw_die_ref die)
8838 dw_die_ref c;
8840 if (! die->die_child)
8841 return;
8843 if (die->die_parent && die != die->die_parent->die_child)
8844 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8846 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8849 /* Output all location lists for the DIE and its children. */
8851 static void
8852 output_location_lists (dw_die_ref die)
8854 dw_die_ref c;
8855 dw_attr_node *a;
8856 unsigned ix;
8858 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8859 if (AT_class (a) == dw_val_class_loc_list)
8860 output_loc_list (AT_loc_list (a));
8862 FOR_EACH_CHILD (die, c, output_location_lists (c));
8865 /* During assign_location_list_indexes and output_loclists_offset the
8866 current index, after it the number of assigned indexes (i.e. how
8867 large the .debug_loclists* offset table should be). */
8868 static unsigned int loc_list_idx;
8870 /* Output all location list offsets for the DIE and its children. */
8872 static void
8873 output_loclists_offsets (dw_die_ref die)
8875 dw_die_ref c;
8876 dw_attr_node *a;
8877 unsigned ix;
8879 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8880 if (AT_class (a) == dw_val_class_loc_list)
8882 dw_loc_list_ref l = AT_loc_list (a);
8883 if (l->offset_emitted)
8884 continue;
8885 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8886 loc_section_label, NULL);
8887 gcc_assert (l->hash == loc_list_idx);
8888 loc_list_idx++;
8889 l->offset_emitted = true;
8892 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8895 /* Recursively set indexes of location lists. */
8897 static void
8898 assign_location_list_indexes (dw_die_ref die)
8900 dw_die_ref c;
8901 dw_attr_node *a;
8902 unsigned ix;
8904 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8905 if (AT_class (a) == dw_val_class_loc_list)
8907 dw_loc_list_ref list = AT_loc_list (a);
8908 if (!list->num_assigned)
8910 list->num_assigned = true;
8911 list->hash = loc_list_idx++;
8915 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8918 /* We want to limit the number of external references, because they are
8919 larger than local references: a relocation takes multiple words, and
8920 even a sig8 reference is always eight bytes, whereas a local reference
8921 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8922 So if we encounter multiple external references to the same type DIE, we
8923 make a local typedef stub for it and redirect all references there.
8925 This is the element of the hash table for keeping track of these
8926 references. */
8928 struct external_ref
8930 dw_die_ref type;
8931 dw_die_ref stub;
8932 unsigned n_refs;
8935 /* Hashtable helpers. */
8937 struct external_ref_hasher : free_ptr_hash <external_ref>
8939 static inline hashval_t hash (const external_ref *);
8940 static inline bool equal (const external_ref *, const external_ref *);
8943 inline hashval_t
8944 external_ref_hasher::hash (const external_ref *r)
8946 dw_die_ref die = r->type;
8947 hashval_t h = 0;
8949 /* We can't use the address of the DIE for hashing, because
8950 that will make the order of the stub DIEs non-deterministic. */
8951 if (! die->comdat_type_p)
8952 /* We have a symbol; use it to compute a hash. */
8953 h = htab_hash_string (die->die_id.die_symbol);
8954 else
8956 /* We have a type signature; use a subset of the bits as the hash.
8957 The 8-byte signature is at least as large as hashval_t. */
8958 comdat_type_node *type_node = die->die_id.die_type_node;
8959 memcpy (&h, type_node->signature, sizeof (h));
8961 return h;
8964 inline bool
8965 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8967 return r1->type == r2->type;
8970 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8972 /* Return a pointer to the external_ref for references to DIE. */
8974 static struct external_ref *
8975 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8977 struct external_ref ref, *ref_p;
8978 external_ref **slot;
8980 ref.type = die;
8981 slot = map->find_slot (&ref, INSERT);
8982 if (*slot != HTAB_EMPTY_ENTRY)
8983 return *slot;
8985 ref_p = XCNEW (struct external_ref);
8986 ref_p->type = die;
8987 *slot = ref_p;
8988 return ref_p;
8991 /* Subroutine of optimize_external_refs, below.
8993 If we see a type skeleton, record it as our stub. If we see external
8994 references, remember how many we've seen. */
8996 static void
8997 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8999 dw_die_ref c;
9000 dw_attr_node *a;
9001 unsigned ix;
9002 struct external_ref *ref_p;
9004 if (is_type_die (die)
9005 && (c = get_AT_ref (die, DW_AT_signature)))
9007 /* This is a local skeleton; use it for local references. */
9008 ref_p = lookup_external_ref (map, c);
9009 ref_p->stub = die;
9012 /* Scan the DIE references, and remember any that refer to DIEs from
9013 other CUs (i.e. those which are not marked). */
9014 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9015 if (AT_class (a) == dw_val_class_die_ref
9016 && (c = AT_ref (a))->die_mark == 0
9017 && is_type_die (c))
9019 ref_p = lookup_external_ref (map, c);
9020 ref_p->n_refs++;
9023 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9026 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9027 points to an external_ref, DATA is the CU we're processing. If we don't
9028 already have a local stub, and we have multiple refs, build a stub. */
9031 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9033 struct external_ref *ref_p = *slot;
9035 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9037 /* We have multiple references to this type, so build a small stub.
9038 Both of these forms are a bit dodgy from the perspective of the
9039 DWARF standard, since technically they should have names. */
9040 dw_die_ref cu = data;
9041 dw_die_ref type = ref_p->type;
9042 dw_die_ref stub = NULL;
9044 if (type->comdat_type_p)
9046 /* If we refer to this type via sig8, use AT_signature. */
9047 stub = new_die (type->die_tag, cu, NULL_TREE);
9048 add_AT_die_ref (stub, DW_AT_signature, type);
9050 else
9052 /* Otherwise, use a typedef with no name. */
9053 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9054 add_AT_die_ref (stub, DW_AT_type, type);
9057 stub->die_mark++;
9058 ref_p->stub = stub;
9060 return 1;
9063 /* DIE is a unit; look through all the DIE references to see if there are
9064 any external references to types, and if so, create local stubs for
9065 them which will be applied in build_abbrev_table. This is useful because
9066 references to local DIEs are smaller. */
9068 static external_ref_hash_type *
9069 optimize_external_refs (dw_die_ref die)
9071 external_ref_hash_type *map = new external_ref_hash_type (10);
9072 optimize_external_refs_1 (die, map);
9073 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9074 return map;
9077 /* The following 3 variables are temporaries that are computed only during the
9078 build_abbrev_table call and used and released during the following
9079 optimize_abbrev_table call. */
9081 /* First abbrev_id that can be optimized based on usage. */
9082 static unsigned int abbrev_opt_start;
9084 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9085 abbrev_id smaller than this, because they must be already sized
9086 during build_abbrev_table). */
9087 static unsigned int abbrev_opt_base_type_end;
9089 /* Vector of usage counts during build_abbrev_table. Indexed by
9090 abbrev_id - abbrev_opt_start. */
9091 static vec<unsigned int> abbrev_usage_count;
9093 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9094 static vec<dw_die_ref> sorted_abbrev_dies;
9096 /* The format of each DIE (and its attribute value pairs) is encoded in an
9097 abbreviation table. This routine builds the abbreviation table and assigns
9098 a unique abbreviation id for each abbreviation entry. The children of each
9099 die are visited recursively. */
9101 static void
9102 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9104 unsigned int abbrev_id = 0;
9105 dw_die_ref c;
9106 dw_attr_node *a;
9107 unsigned ix;
9108 dw_die_ref abbrev;
9110 /* Scan the DIE references, and replace any that refer to
9111 DIEs from other CUs (i.e. those which are not marked) with
9112 the local stubs we built in optimize_external_refs. */
9113 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9114 if (AT_class (a) == dw_val_class_die_ref
9115 && (c = AT_ref (a))->die_mark == 0)
9117 struct external_ref *ref_p;
9118 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9120 if (is_type_die (c)
9121 && (ref_p = lookup_external_ref (extern_map, c))
9122 && ref_p->stub && ref_p->stub != die)
9124 gcc_assert (a->dw_attr != DW_AT_signature);
9125 change_AT_die_ref (a, ref_p->stub);
9127 else
9128 /* We aren't changing this reference, so mark it external. */
9129 set_AT_ref_external (a, 1);
9132 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9134 dw_attr_node *die_a, *abbrev_a;
9135 unsigned ix;
9136 bool ok = true;
9138 if (abbrev_id == 0)
9139 continue;
9140 if (abbrev->die_tag != die->die_tag)
9141 continue;
9142 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9143 continue;
9145 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9146 continue;
9148 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9150 abbrev_a = &(*abbrev->die_attr)[ix];
9151 if ((abbrev_a->dw_attr != die_a->dw_attr)
9152 || (value_format (abbrev_a) != value_format (die_a)))
9154 ok = false;
9155 break;
9158 if (ok)
9159 break;
9162 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9164 vec_safe_push (abbrev_die_table, die);
9165 if (abbrev_opt_start)
9166 abbrev_usage_count.safe_push (0);
9168 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9170 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9171 sorted_abbrev_dies.safe_push (die);
9174 die->die_abbrev = abbrev_id;
9175 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9178 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9179 by die_abbrev's usage count, from the most commonly used
9180 abbreviation to the least. */
9182 static int
9183 die_abbrev_cmp (const void *p1, const void *p2)
9185 dw_die_ref die1 = *(const dw_die_ref *) p1;
9186 dw_die_ref die2 = *(const dw_die_ref *) p2;
9188 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9189 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9191 if (die1->die_abbrev >= abbrev_opt_base_type_end
9192 && die2->die_abbrev >= abbrev_opt_base_type_end)
9194 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9195 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9196 return -1;
9197 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9198 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9199 return 1;
9202 /* Stabilize the sort. */
9203 if (die1->die_abbrev < die2->die_abbrev)
9204 return -1;
9205 if (die1->die_abbrev > die2->die_abbrev)
9206 return 1;
9208 return 0;
9211 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9212 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9213 into dw_val_class_const_implicit or
9214 dw_val_class_unsigned_const_implicit. */
9216 static void
9217 optimize_implicit_const (unsigned int first_id, unsigned int end,
9218 vec<bool> &implicit_consts)
9220 /* It never makes sense if there is just one DIE using the abbreviation. */
9221 if (end < first_id + 2)
9222 return;
9224 dw_attr_node *a;
9225 unsigned ix, i;
9226 dw_die_ref die = sorted_abbrev_dies[first_id];
9227 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9228 if (implicit_consts[ix])
9230 enum dw_val_class new_class = dw_val_class_none;
9231 switch (AT_class (a))
9233 case dw_val_class_unsigned_const:
9234 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9235 continue;
9237 /* The .debug_abbrev section will grow by
9238 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9239 in all the DIEs using that abbreviation. */
9240 if (constant_size (AT_unsigned (a)) * (end - first_id)
9241 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9242 continue;
9244 new_class = dw_val_class_unsigned_const_implicit;
9245 break;
9247 case dw_val_class_const:
9248 new_class = dw_val_class_const_implicit;
9249 break;
9251 case dw_val_class_file:
9252 new_class = dw_val_class_file_implicit;
9253 break;
9255 default:
9256 continue;
9258 for (i = first_id; i < end; i++)
9259 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9260 = new_class;
9264 /* Attempt to optimize abbreviation table from abbrev_opt_start
9265 abbreviation above. */
9267 static void
9268 optimize_abbrev_table (void)
9270 if (abbrev_opt_start
9271 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9272 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9274 auto_vec<bool, 32> implicit_consts;
9275 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9277 unsigned int abbrev_id = abbrev_opt_start - 1;
9278 unsigned int first_id = ~0U;
9279 unsigned int last_abbrev_id = 0;
9280 unsigned int i;
9281 dw_die_ref die;
9282 if (abbrev_opt_base_type_end > abbrev_opt_start)
9283 abbrev_id = abbrev_opt_base_type_end - 1;
9284 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9285 most commonly used abbreviations come first. */
9286 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9288 dw_attr_node *a;
9289 unsigned ix;
9291 /* If calc_base_type_die_sizes has been called, the CU and
9292 base types after it can't be optimized, because we've already
9293 calculated their DIE offsets. We've sorted them first. */
9294 if (die->die_abbrev < abbrev_opt_base_type_end)
9295 continue;
9296 if (die->die_abbrev != last_abbrev_id)
9298 last_abbrev_id = die->die_abbrev;
9299 if (dwarf_version >= 5 && first_id != ~0U)
9300 optimize_implicit_const (first_id, i, implicit_consts);
9301 abbrev_id++;
9302 (*abbrev_die_table)[abbrev_id] = die;
9303 if (dwarf_version >= 5)
9305 first_id = i;
9306 implicit_consts.truncate (0);
9308 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9309 switch (AT_class (a))
9311 case dw_val_class_const:
9312 case dw_val_class_unsigned_const:
9313 case dw_val_class_file:
9314 implicit_consts.safe_push (true);
9315 break;
9316 default:
9317 implicit_consts.safe_push (false);
9318 break;
9322 else if (dwarf_version >= 5)
9324 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9325 if (!implicit_consts[ix])
9326 continue;
9327 else
9329 dw_attr_node *other_a
9330 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9331 if (!dw_val_equal_p (&a->dw_attr_val,
9332 &other_a->dw_attr_val))
9333 implicit_consts[ix] = false;
9336 die->die_abbrev = abbrev_id;
9338 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9339 if (dwarf_version >= 5 && first_id != ~0U)
9340 optimize_implicit_const (first_id, i, implicit_consts);
9343 abbrev_opt_start = 0;
9344 abbrev_opt_base_type_end = 0;
9345 abbrev_usage_count.release ();
9346 sorted_abbrev_dies.release ();
9349 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9351 static int
9352 constant_size (unsigned HOST_WIDE_INT value)
9354 int log;
9356 if (value == 0)
9357 log = 0;
9358 else
9359 log = floor_log2 (value);
9361 log = log / 8;
9362 log = 1 << (floor_log2 (log) + 1);
9364 return log;
9367 /* Return the size of a DIE as it is represented in the
9368 .debug_info section. */
9370 static unsigned long
9371 size_of_die (dw_die_ref die)
9373 unsigned long size = 0;
9374 dw_attr_node *a;
9375 unsigned ix;
9376 enum dwarf_form form;
9378 size += size_of_uleb128 (die->die_abbrev);
9379 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9381 switch (AT_class (a))
9383 case dw_val_class_addr:
9384 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9386 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9387 size += size_of_uleb128 (AT_index (a));
9389 else
9390 size += DWARF2_ADDR_SIZE;
9391 break;
9392 case dw_val_class_offset:
9393 size += DWARF_OFFSET_SIZE;
9394 break;
9395 case dw_val_class_loc:
9397 unsigned long lsize = size_of_locs (AT_loc (a));
9399 /* Block length. */
9400 if (dwarf_version >= 4)
9401 size += size_of_uleb128 (lsize);
9402 else
9403 size += constant_size (lsize);
9404 size += lsize;
9406 break;
9407 case dw_val_class_loc_list:
9408 if (dwarf_split_debug_info && dwarf_version >= 5)
9410 gcc_assert (AT_loc_list (a)->num_assigned);
9411 size += size_of_uleb128 (AT_loc_list (a)->hash);
9413 else
9414 size += DWARF_OFFSET_SIZE;
9415 break;
9416 case dw_val_class_view_list:
9417 size += DWARF_OFFSET_SIZE;
9418 break;
9419 case dw_val_class_range_list:
9420 if (value_format (a) == DW_FORM_rnglistx)
9422 gcc_assert (rnglist_idx);
9423 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9424 size += size_of_uleb128 (r->idx);
9426 else
9427 size += DWARF_OFFSET_SIZE;
9428 break;
9429 case dw_val_class_const:
9430 size += size_of_sleb128 (AT_int (a));
9431 break;
9432 case dw_val_class_unsigned_const:
9434 int csize = constant_size (AT_unsigned (a));
9435 if (dwarf_version == 3
9436 && a->dw_attr == DW_AT_data_member_location
9437 && csize >= 4)
9438 size += size_of_uleb128 (AT_unsigned (a));
9439 else
9440 size += csize;
9442 break;
9443 case dw_val_class_symview:
9444 if (symview_upper_bound <= 0xff)
9445 size += 1;
9446 else if (symview_upper_bound <= 0xffff)
9447 size += 2;
9448 else if (symview_upper_bound <= 0xffffffff)
9449 size += 4;
9450 else
9451 size += 8;
9452 break;
9453 case dw_val_class_const_implicit:
9454 case dw_val_class_unsigned_const_implicit:
9455 case dw_val_class_file_implicit:
9456 /* These occupy no size in the DIE, just an extra sleb128 in
9457 .debug_abbrev. */
9458 break;
9459 case dw_val_class_const_double:
9460 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9461 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9462 size++; /* block */
9463 break;
9464 case dw_val_class_wide_int:
9465 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9466 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9467 if (get_full_len (*a->dw_attr_val.v.val_wide)
9468 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9469 size++; /* block */
9470 break;
9471 case dw_val_class_vec:
9472 size += constant_size (a->dw_attr_val.v.val_vec.length
9473 * a->dw_attr_val.v.val_vec.elt_size)
9474 + a->dw_attr_val.v.val_vec.length
9475 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9476 break;
9477 case dw_val_class_flag:
9478 if (dwarf_version >= 4)
9479 /* Currently all add_AT_flag calls pass in 1 as last argument,
9480 so DW_FORM_flag_present can be used. If that ever changes,
9481 we'll need to use DW_FORM_flag and have some optimization
9482 in build_abbrev_table that will change those to
9483 DW_FORM_flag_present if it is set to 1 in all DIEs using
9484 the same abbrev entry. */
9485 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9486 else
9487 size += 1;
9488 break;
9489 case dw_val_class_die_ref:
9490 if (AT_ref_external (a))
9492 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9493 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9494 is sized by target address length, whereas in DWARF3
9495 it's always sized as an offset. */
9496 if (AT_ref (a)->comdat_type_p)
9497 size += DWARF_TYPE_SIGNATURE_SIZE;
9498 else if (dwarf_version == 2)
9499 size += DWARF2_ADDR_SIZE;
9500 else
9501 size += DWARF_OFFSET_SIZE;
9503 else
9504 size += DWARF_OFFSET_SIZE;
9505 break;
9506 case dw_val_class_fde_ref:
9507 size += DWARF_OFFSET_SIZE;
9508 break;
9509 case dw_val_class_lbl_id:
9510 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9512 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9513 size += size_of_uleb128 (AT_index (a));
9515 else
9516 size += DWARF2_ADDR_SIZE;
9517 break;
9518 case dw_val_class_lineptr:
9519 case dw_val_class_macptr:
9520 case dw_val_class_loclistsptr:
9521 size += DWARF_OFFSET_SIZE;
9522 break;
9523 case dw_val_class_str:
9524 form = AT_string_form (a);
9525 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9526 size += DWARF_OFFSET_SIZE;
9527 else if (form == dwarf_FORM (DW_FORM_strx))
9528 size += size_of_uleb128 (AT_index (a));
9529 else
9530 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9531 break;
9532 case dw_val_class_file:
9533 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9534 break;
9535 case dw_val_class_data8:
9536 size += 8;
9537 break;
9538 case dw_val_class_vms_delta:
9539 size += DWARF_OFFSET_SIZE;
9540 break;
9541 case dw_val_class_high_pc:
9542 size += DWARF2_ADDR_SIZE;
9543 break;
9544 case dw_val_class_discr_value:
9545 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9546 break;
9547 case dw_val_class_discr_list:
9549 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9551 /* This is a block, so we have the block length and then its
9552 data. */
9553 size += constant_size (block_size) + block_size;
9555 break;
9556 default:
9557 gcc_unreachable ();
9561 return size;
9564 /* Size the debugging information associated with a given DIE. Visits the
9565 DIE's children recursively. Updates the global variable next_die_offset, on
9566 each time through. Uses the current value of next_die_offset to update the
9567 die_offset field in each DIE. */
9569 static void
9570 calc_die_sizes (dw_die_ref die)
9572 dw_die_ref c;
9574 gcc_assert (die->die_offset == 0
9575 || (unsigned long int) die->die_offset == next_die_offset);
9576 die->die_offset = next_die_offset;
9577 next_die_offset += size_of_die (die);
9579 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9581 if (die->die_child != NULL)
9582 /* Count the null byte used to terminate sibling lists. */
9583 next_die_offset += 1;
9586 /* Size just the base type children at the start of the CU.
9587 This is needed because build_abbrev needs to size locs
9588 and sizing of type based stack ops needs to know die_offset
9589 values for the base types. */
9591 static void
9592 calc_base_type_die_sizes (void)
9594 unsigned long die_offset = (dwarf_split_debug_info
9595 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9596 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9597 unsigned int i;
9598 dw_die_ref base_type;
9599 #if ENABLE_ASSERT_CHECKING
9600 dw_die_ref prev = comp_unit_die ()->die_child;
9601 #endif
9603 die_offset += size_of_die (comp_unit_die ());
9604 for (i = 0; base_types.iterate (i, &base_type); i++)
9606 #if ENABLE_ASSERT_CHECKING
9607 gcc_assert (base_type->die_offset == 0
9608 && prev->die_sib == base_type
9609 && base_type->die_child == NULL
9610 && base_type->die_abbrev);
9611 prev = base_type;
9612 #endif
9613 if (abbrev_opt_start
9614 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9615 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9616 base_type->die_offset = die_offset;
9617 die_offset += size_of_die (base_type);
9621 /* Set the marks for a die and its children. We do this so
9622 that we know whether or not a reference needs to use FORM_ref_addr; only
9623 DIEs in the same CU will be marked. We used to clear out the offset
9624 and use that as the flag, but ran into ordering problems. */
9626 static void
9627 mark_dies (dw_die_ref die)
9629 dw_die_ref c;
9631 gcc_assert (!die->die_mark);
9633 die->die_mark = 1;
9634 FOR_EACH_CHILD (die, c, mark_dies (c));
9637 /* Clear the marks for a die and its children. */
9639 static void
9640 unmark_dies (dw_die_ref die)
9642 dw_die_ref c;
9644 if (! use_debug_types)
9645 gcc_assert (die->die_mark);
9647 die->die_mark = 0;
9648 FOR_EACH_CHILD (die, c, unmark_dies (c));
9651 /* Clear the marks for a die, its children and referred dies. */
9653 static void
9654 unmark_all_dies (dw_die_ref die)
9656 dw_die_ref c;
9657 dw_attr_node *a;
9658 unsigned ix;
9660 if (!die->die_mark)
9661 return;
9662 die->die_mark = 0;
9664 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9666 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9667 if (AT_class (a) == dw_val_class_die_ref)
9668 unmark_all_dies (AT_ref (a));
9671 /* Calculate if the entry should appear in the final output file. It may be
9672 from a pruned a type. */
9674 static bool
9675 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9677 /* By limiting gnu pubnames to definitions only, gold can generate a
9678 gdb index without entries for declarations, which don't include
9679 enough information to be useful. */
9680 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9681 return false;
9683 if (table == pubname_table)
9685 /* Enumerator names are part of the pubname table, but the
9686 parent DW_TAG_enumeration_type die may have been pruned.
9687 Don't output them if that is the case. */
9688 if (p->die->die_tag == DW_TAG_enumerator &&
9689 (p->die->die_parent == NULL
9690 || !p->die->die_parent->die_perennial_p))
9691 return false;
9693 /* Everything else in the pubname table is included. */
9694 return true;
9697 /* The pubtypes table shouldn't include types that have been
9698 pruned. */
9699 return (p->die->die_offset != 0
9700 || !flag_eliminate_unused_debug_types);
9703 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9704 generated for the compilation unit. */
9706 static unsigned long
9707 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9709 unsigned long size;
9710 unsigned i;
9711 pubname_entry *p;
9712 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9714 size = DWARF_PUBNAMES_HEADER_SIZE;
9715 FOR_EACH_VEC_ELT (*names, i, p)
9716 if (include_pubname_in_output (names, p))
9717 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9719 size += DWARF_OFFSET_SIZE;
9720 return size;
9723 /* Return the size of the information in the .debug_aranges section. */
9725 static unsigned long
9726 size_of_aranges (void)
9728 unsigned long size;
9730 size = DWARF_ARANGES_HEADER_SIZE;
9732 /* Count the address/length pair for this compilation unit. */
9733 if (text_section_used)
9734 size += 2 * DWARF2_ADDR_SIZE;
9735 if (cold_text_section_used)
9736 size += 2 * DWARF2_ADDR_SIZE;
9737 if (have_multiple_function_sections)
9739 unsigned fde_idx;
9740 dw_fde_ref fde;
9742 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9744 if (DECL_IGNORED_P (fde->decl))
9745 continue;
9746 if (!fde->in_std_section)
9747 size += 2 * DWARF2_ADDR_SIZE;
9748 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9749 size += 2 * DWARF2_ADDR_SIZE;
9753 /* Count the two zero words used to terminated the address range table. */
9754 size += 2 * DWARF2_ADDR_SIZE;
9755 return size;
9758 /* Select the encoding of an attribute value. */
9760 static enum dwarf_form
9761 value_format (dw_attr_node *a)
9763 switch (AT_class (a))
9765 case dw_val_class_addr:
9766 /* Only very few attributes allow DW_FORM_addr. */
9767 switch (a->dw_attr)
9769 case DW_AT_low_pc:
9770 case DW_AT_high_pc:
9771 case DW_AT_entry_pc:
9772 case DW_AT_trampoline:
9773 return (AT_index (a) == NOT_INDEXED
9774 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9775 default:
9776 break;
9778 switch (DWARF2_ADDR_SIZE)
9780 case 1:
9781 return DW_FORM_data1;
9782 case 2:
9783 return DW_FORM_data2;
9784 case 4:
9785 return DW_FORM_data4;
9786 case 8:
9787 return DW_FORM_data8;
9788 default:
9789 gcc_unreachable ();
9791 case dw_val_class_loc_list:
9792 if (dwarf_split_debug_info
9793 && dwarf_version >= 5
9794 && AT_loc_list (a)->num_assigned)
9795 return DW_FORM_loclistx;
9796 /* FALLTHRU */
9797 case dw_val_class_view_list:
9798 case dw_val_class_range_list:
9799 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9800 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9801 care about sizes of .debug* sections in shared libraries and
9802 executables and don't take into account relocations that affect just
9803 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9804 table in the .debug_rnglists section. */
9805 if (dwarf_split_debug_info
9806 && dwarf_version >= 5
9807 && AT_class (a) == dw_val_class_range_list
9808 && rnglist_idx
9809 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9810 return DW_FORM_rnglistx;
9811 if (dwarf_version >= 4)
9812 return DW_FORM_sec_offset;
9813 /* FALLTHRU */
9814 case dw_val_class_vms_delta:
9815 case dw_val_class_offset:
9816 switch (DWARF_OFFSET_SIZE)
9818 case 4:
9819 return DW_FORM_data4;
9820 case 8:
9821 return DW_FORM_data8;
9822 default:
9823 gcc_unreachable ();
9825 case dw_val_class_loc:
9826 if (dwarf_version >= 4)
9827 return DW_FORM_exprloc;
9828 switch (constant_size (size_of_locs (AT_loc (a))))
9830 case 1:
9831 return DW_FORM_block1;
9832 case 2:
9833 return DW_FORM_block2;
9834 case 4:
9835 return DW_FORM_block4;
9836 default:
9837 gcc_unreachable ();
9839 case dw_val_class_const:
9840 return DW_FORM_sdata;
9841 case dw_val_class_unsigned_const:
9842 switch (constant_size (AT_unsigned (a)))
9844 case 1:
9845 return DW_FORM_data1;
9846 case 2:
9847 return DW_FORM_data2;
9848 case 4:
9849 /* In DWARF3 DW_AT_data_member_location with
9850 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9851 constant, so we need to use DW_FORM_udata if we need
9852 a large constant. */
9853 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9854 return DW_FORM_udata;
9855 return DW_FORM_data4;
9856 case 8:
9857 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9858 return DW_FORM_udata;
9859 return DW_FORM_data8;
9860 default:
9861 gcc_unreachable ();
9863 case dw_val_class_const_implicit:
9864 case dw_val_class_unsigned_const_implicit:
9865 case dw_val_class_file_implicit:
9866 return DW_FORM_implicit_const;
9867 case dw_val_class_const_double:
9868 switch (HOST_BITS_PER_WIDE_INT)
9870 case 8:
9871 return DW_FORM_data2;
9872 case 16:
9873 return DW_FORM_data4;
9874 case 32:
9875 return DW_FORM_data8;
9876 case 64:
9877 if (dwarf_version >= 5)
9878 return DW_FORM_data16;
9879 /* FALLTHRU */
9880 default:
9881 return DW_FORM_block1;
9883 case dw_val_class_wide_int:
9884 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9886 case 8:
9887 return DW_FORM_data1;
9888 case 16:
9889 return DW_FORM_data2;
9890 case 32:
9891 return DW_FORM_data4;
9892 case 64:
9893 return DW_FORM_data8;
9894 case 128:
9895 if (dwarf_version >= 5)
9896 return DW_FORM_data16;
9897 /* FALLTHRU */
9898 default:
9899 return DW_FORM_block1;
9901 case dw_val_class_symview:
9902 /* ??? We might use uleb128, but then we'd have to compute
9903 .debug_info offsets in the assembler. */
9904 if (symview_upper_bound <= 0xff)
9905 return DW_FORM_data1;
9906 else if (symview_upper_bound <= 0xffff)
9907 return DW_FORM_data2;
9908 else if (symview_upper_bound <= 0xffffffff)
9909 return DW_FORM_data4;
9910 else
9911 return DW_FORM_data8;
9912 case dw_val_class_vec:
9913 switch (constant_size (a->dw_attr_val.v.val_vec.length
9914 * a->dw_attr_val.v.val_vec.elt_size))
9916 case 1:
9917 return DW_FORM_block1;
9918 case 2:
9919 return DW_FORM_block2;
9920 case 4:
9921 return DW_FORM_block4;
9922 default:
9923 gcc_unreachable ();
9925 case dw_val_class_flag:
9926 if (dwarf_version >= 4)
9928 /* Currently all add_AT_flag calls pass in 1 as last argument,
9929 so DW_FORM_flag_present can be used. If that ever changes,
9930 we'll need to use DW_FORM_flag and have some optimization
9931 in build_abbrev_table that will change those to
9932 DW_FORM_flag_present if it is set to 1 in all DIEs using
9933 the same abbrev entry. */
9934 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9935 return DW_FORM_flag_present;
9937 return DW_FORM_flag;
9938 case dw_val_class_die_ref:
9939 if (AT_ref_external (a))
9941 if (AT_ref (a)->comdat_type_p)
9942 return DW_FORM_ref_sig8;
9943 else
9944 return DW_FORM_ref_addr;
9946 else
9947 return DW_FORM_ref;
9948 case dw_val_class_fde_ref:
9949 return DW_FORM_data;
9950 case dw_val_class_lbl_id:
9951 return (AT_index (a) == NOT_INDEXED
9952 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9953 case dw_val_class_lineptr:
9954 case dw_val_class_macptr:
9955 case dw_val_class_loclistsptr:
9956 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9957 case dw_val_class_str:
9958 return AT_string_form (a);
9959 case dw_val_class_file:
9960 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9962 case 1:
9963 return DW_FORM_data1;
9964 case 2:
9965 return DW_FORM_data2;
9966 case 4:
9967 return DW_FORM_data4;
9968 default:
9969 gcc_unreachable ();
9972 case dw_val_class_data8:
9973 return DW_FORM_data8;
9975 case dw_val_class_high_pc:
9976 switch (DWARF2_ADDR_SIZE)
9978 case 1:
9979 return DW_FORM_data1;
9980 case 2:
9981 return DW_FORM_data2;
9982 case 4:
9983 return DW_FORM_data4;
9984 case 8:
9985 return DW_FORM_data8;
9986 default:
9987 gcc_unreachable ();
9990 case dw_val_class_discr_value:
9991 return (a->dw_attr_val.v.val_discr_value.pos
9992 ? DW_FORM_udata
9993 : DW_FORM_sdata);
9994 case dw_val_class_discr_list:
9995 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9997 case 1:
9998 return DW_FORM_block1;
9999 case 2:
10000 return DW_FORM_block2;
10001 case 4:
10002 return DW_FORM_block4;
10003 default:
10004 gcc_unreachable ();
10007 default:
10008 gcc_unreachable ();
10012 /* Output the encoding of an attribute value. */
10014 static void
10015 output_value_format (dw_attr_node *a)
10017 enum dwarf_form form = value_format (a);
10019 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10022 /* Given a die and id, produce the appropriate abbreviations. */
10024 static void
10025 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10027 unsigned ix;
10028 dw_attr_node *a_attr;
10030 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10031 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10032 dwarf_tag_name (abbrev->die_tag));
10034 if (abbrev->die_child != NULL)
10035 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10036 else
10037 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10039 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10041 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10042 dwarf_attr_name (a_attr->dw_attr));
10043 output_value_format (a_attr);
10044 if (value_format (a_attr) == DW_FORM_implicit_const)
10046 if (AT_class (a_attr) == dw_val_class_file_implicit)
10048 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10049 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10050 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10052 else
10053 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10057 dw2_asm_output_data (1, 0, NULL);
10058 dw2_asm_output_data (1, 0, NULL);
10062 /* Output the .debug_abbrev section which defines the DIE abbreviation
10063 table. */
10065 static void
10066 output_abbrev_section (void)
10068 unsigned int abbrev_id;
10069 dw_die_ref abbrev;
10071 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10072 if (abbrev_id != 0)
10073 output_die_abbrevs (abbrev_id, abbrev);
10075 /* Terminate the table. */
10076 dw2_asm_output_data (1, 0, NULL);
10079 /* Return a new location list, given the begin and end range, and the
10080 expression. */
10082 static inline dw_loc_list_ref
10083 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10084 const char *end, var_loc_view vend,
10085 const char *section)
10087 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10089 retlist->begin = begin;
10090 retlist->begin_entry = NULL;
10091 retlist->end = end;
10092 retlist->expr = expr;
10093 retlist->section = section;
10094 retlist->vbegin = vbegin;
10095 retlist->vend = vend;
10097 return retlist;
10100 /* Return true iff there's any nonzero view number in the loc list.
10102 ??? When views are not enabled, we'll often extend a single range
10103 to the entire function, so that we emit a single location
10104 expression rather than a location list. With views, even with a
10105 single range, we'll output a list if start or end have a nonzero
10106 view. If we change this, we may want to stop splitting a single
10107 range in dw_loc_list just because of a nonzero view, even if it
10108 straddles across hot/cold partitions. */
10110 static bool
10111 loc_list_has_views (dw_loc_list_ref list)
10113 if (!debug_variable_location_views)
10114 return false;
10116 for (dw_loc_list_ref loc = list;
10117 loc != NULL; loc = loc->dw_loc_next)
10118 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10119 return true;
10121 return false;
10124 /* Generate a new internal symbol for this location list node, if it
10125 hasn't got one yet. */
10127 static inline void
10128 gen_llsym (dw_loc_list_ref list)
10130 gcc_assert (!list->ll_symbol);
10131 list->ll_symbol = gen_internal_sym ("LLST");
10133 if (!loc_list_has_views (list))
10134 return;
10136 if (dwarf2out_locviews_in_attribute ())
10138 /* Use the same label_num for the view list. */
10139 label_num--;
10140 list->vl_symbol = gen_internal_sym ("LVUS");
10142 else
10143 list->vl_symbol = list->ll_symbol;
10146 /* Generate a symbol for the list, but only if we really want to emit
10147 it as a list. */
10149 static inline void
10150 maybe_gen_llsym (dw_loc_list_ref list)
10152 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10153 return;
10155 gen_llsym (list);
10158 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10159 NULL, don't consider size of the location expression. If we're not
10160 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10161 representation in *SIZEP. */
10163 static bool
10164 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10166 /* Don't output an entry that starts and ends at the same address. */
10167 if (strcmp (curr->begin, curr->end) == 0
10168 && curr->vbegin == curr->vend && !curr->force)
10169 return true;
10171 if (!sizep)
10172 return false;
10174 unsigned long size = size_of_locs (curr->expr);
10176 /* If the expression is too large, drop it on the floor. We could
10177 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10178 in the expression, but >= 64KB expressions for a single value
10179 in a single range are unlikely very useful. */
10180 if (dwarf_version < 5 && size > 0xffff)
10181 return true;
10183 *sizep = size;
10185 return false;
10188 /* Output a view pair loclist entry for CURR, if it requires one. */
10190 static void
10191 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10193 if (!dwarf2out_locviews_in_loclist ())
10194 return;
10196 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10197 return;
10199 #ifdef DW_LLE_view_pair
10200 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10202 if (dwarf2out_as_locview_support)
10204 if (ZERO_VIEW_P (curr->vbegin))
10205 dw2_asm_output_data_uleb128 (0, "Location view begin");
10206 else
10208 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10209 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10210 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10213 if (ZERO_VIEW_P (curr->vend))
10214 dw2_asm_output_data_uleb128 (0, "Location view end");
10215 else
10217 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10218 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10219 dw2_asm_output_symname_uleb128 (label, "Location view end");
10222 else
10224 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10225 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10227 #endif /* DW_LLE_view_pair */
10229 return;
10232 /* Output the location list given to us. */
10234 static void
10235 output_loc_list (dw_loc_list_ref list_head)
10237 int vcount = 0, lcount = 0;
10239 if (list_head->emitted)
10240 return;
10241 list_head->emitted = true;
10243 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10245 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10247 for (dw_loc_list_ref curr = list_head; curr != NULL;
10248 curr = curr->dw_loc_next)
10250 unsigned long size;
10252 if (skip_loc_list_entry (curr, &size))
10253 continue;
10255 vcount++;
10257 /* ?? dwarf_split_debug_info? */
10258 if (dwarf2out_as_locview_support)
10260 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10262 if (!ZERO_VIEW_P (curr->vbegin))
10264 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10265 dw2_asm_output_symname_uleb128 (label,
10266 "View list begin (%s)",
10267 list_head->vl_symbol);
10269 else
10270 dw2_asm_output_data_uleb128 (0,
10271 "View list begin (%s)",
10272 list_head->vl_symbol);
10274 if (!ZERO_VIEW_P (curr->vend))
10276 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10277 dw2_asm_output_symname_uleb128 (label,
10278 "View list end (%s)",
10279 list_head->vl_symbol);
10281 else
10282 dw2_asm_output_data_uleb128 (0,
10283 "View list end (%s)",
10284 list_head->vl_symbol);
10286 else
10288 dw2_asm_output_data_uleb128 (curr->vbegin,
10289 "View list begin (%s)",
10290 list_head->vl_symbol);
10291 dw2_asm_output_data_uleb128 (curr->vend,
10292 "View list end (%s)",
10293 list_head->vl_symbol);
10298 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10300 const char *last_section = NULL;
10301 const char *base_label = NULL;
10303 /* Walk the location list, and output each range + expression. */
10304 for (dw_loc_list_ref curr = list_head; curr != NULL;
10305 curr = curr->dw_loc_next)
10307 unsigned long size;
10309 /* Skip this entry? If we skip it here, we must skip it in the
10310 view list above as well. */
10311 if (skip_loc_list_entry (curr, &size))
10312 continue;
10314 lcount++;
10316 if (dwarf_version >= 5)
10318 if (dwarf_split_debug_info)
10320 dwarf2out_maybe_output_loclist_view_pair (curr);
10321 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
10322 uleb128 index into .debug_addr and uleb128 length. */
10323 dw2_asm_output_data (1, DW_LLE_startx_length,
10324 "DW_LLE_startx_length (%s)",
10325 list_head->ll_symbol);
10326 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10327 "Location list range start index "
10328 "(%s)", curr->begin);
10329 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
10330 For that case we probably need to emit DW_LLE_startx_endx,
10331 but we'd need 2 .debug_addr entries rather than just one. */
10332 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10333 "Location list length (%s)",
10334 list_head->ll_symbol);
10336 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10338 dwarf2out_maybe_output_loclist_view_pair (curr);
10339 /* If all code is in .text section, the base address is
10340 already provided by the CU attributes. Use
10341 DW_LLE_offset_pair where both addresses are uleb128 encoded
10342 offsets against that base. */
10343 dw2_asm_output_data (1, DW_LLE_offset_pair,
10344 "DW_LLE_offset_pair (%s)",
10345 list_head->ll_symbol);
10346 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10347 "Location list begin address (%s)",
10348 list_head->ll_symbol);
10349 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10350 "Location list end address (%s)",
10351 list_head->ll_symbol);
10353 else if (HAVE_AS_LEB128)
10355 /* Otherwise, find out how many consecutive entries could share
10356 the same base entry. If just one, emit DW_LLE_start_length,
10357 otherwise emit DW_LLE_base_address for the base address
10358 followed by a series of DW_LLE_offset_pair. */
10359 if (last_section == NULL || curr->section != last_section)
10361 dw_loc_list_ref curr2;
10362 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10363 curr2 = curr2->dw_loc_next)
10365 if (strcmp (curr2->begin, curr2->end) == 0
10366 && !curr2->force)
10367 continue;
10368 break;
10370 if (curr2 == NULL || curr->section != curr2->section)
10371 last_section = NULL;
10372 else
10374 last_section = curr->section;
10375 base_label = curr->begin;
10376 dw2_asm_output_data (1, DW_LLE_base_address,
10377 "DW_LLE_base_address (%s)",
10378 list_head->ll_symbol);
10379 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10380 "Base address (%s)",
10381 list_head->ll_symbol);
10384 /* Only one entry with the same base address. Use
10385 DW_LLE_start_length with absolute address and uleb128
10386 length. */
10387 if (last_section == NULL)
10389 dwarf2out_maybe_output_loclist_view_pair (curr);
10390 dw2_asm_output_data (1, DW_LLE_start_length,
10391 "DW_LLE_start_length (%s)",
10392 list_head->ll_symbol);
10393 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10394 "Location list begin address (%s)",
10395 list_head->ll_symbol);
10396 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10397 "Location list length "
10398 "(%s)", list_head->ll_symbol);
10400 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10401 DW_LLE_base_address. */
10402 else
10404 dwarf2out_maybe_output_loclist_view_pair (curr);
10405 dw2_asm_output_data (1, DW_LLE_offset_pair,
10406 "DW_LLE_offset_pair (%s)",
10407 list_head->ll_symbol);
10408 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10409 "Location list begin address "
10410 "(%s)", list_head->ll_symbol);
10411 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10412 "Location list end address "
10413 "(%s)", list_head->ll_symbol);
10416 /* The assembler does not support .uleb128 directive. Emit
10417 DW_LLE_start_end with a pair of absolute addresses. */
10418 else
10420 dwarf2out_maybe_output_loclist_view_pair (curr);
10421 dw2_asm_output_data (1, DW_LLE_start_end,
10422 "DW_LLE_start_end (%s)",
10423 list_head->ll_symbol);
10424 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10425 "Location list begin address (%s)",
10426 list_head->ll_symbol);
10427 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10428 "Location list end address (%s)",
10429 list_head->ll_symbol);
10432 else if (dwarf_split_debug_info)
10434 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10435 and 4 byte length. */
10436 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10437 "Location list start/length entry (%s)",
10438 list_head->ll_symbol);
10439 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10440 "Location list range start index (%s)",
10441 curr->begin);
10442 /* The length field is 4 bytes. If we ever need to support
10443 an 8-byte length, we can add a new DW_LLE code or fall back
10444 to DW_LLE_GNU_start_end_entry. */
10445 dw2_asm_output_delta (4, curr->end, curr->begin,
10446 "Location list range length (%s)",
10447 list_head->ll_symbol);
10449 else if (!have_multiple_function_sections)
10451 /* Pair of relative addresses against start of text section. */
10452 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10453 "Location list begin address (%s)",
10454 list_head->ll_symbol);
10455 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10456 "Location list end address (%s)",
10457 list_head->ll_symbol);
10459 else
10461 /* Pair of absolute addresses. */
10462 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10463 "Location list begin address (%s)",
10464 list_head->ll_symbol);
10465 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10466 "Location list end address (%s)",
10467 list_head->ll_symbol);
10470 /* Output the block length for this list of location operations. */
10471 if (dwarf_version >= 5)
10472 dw2_asm_output_data_uleb128 (size, "Location expression size");
10473 else
10475 gcc_assert (size <= 0xffff);
10476 dw2_asm_output_data (2, size, "Location expression size");
10479 output_loc_sequence (curr->expr, -1);
10482 /* And finally list termination. */
10483 if (dwarf_version >= 5)
10484 dw2_asm_output_data (1, DW_LLE_end_of_list,
10485 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10486 else if (dwarf_split_debug_info)
10487 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10488 "Location list terminator (%s)",
10489 list_head->ll_symbol);
10490 else
10492 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10493 "Location list terminator begin (%s)",
10494 list_head->ll_symbol);
10495 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10496 "Location list terminator end (%s)",
10497 list_head->ll_symbol);
10500 gcc_assert (!list_head->vl_symbol
10501 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10504 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10505 section. Emit a relocated reference if val_entry is NULL, otherwise,
10506 emit an indirect reference. */
10508 static void
10509 output_range_list_offset (dw_attr_node *a)
10511 const char *name = dwarf_attr_name (a->dw_attr);
10513 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10515 if (dwarf_version >= 5)
10517 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10518 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
10519 debug_ranges_section, "%s", name);
10521 else
10523 char *p = strchr (ranges_section_label, '\0');
10524 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10525 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10526 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10527 debug_ranges_section, "%s", name);
10528 *p = '\0';
10531 else if (dwarf_version >= 5)
10533 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10534 gcc_assert (rnglist_idx);
10535 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10537 else
10538 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10539 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10540 "%s (offset from %s)", name, ranges_section_label);
10543 /* Output the offset into the debug_loc section. */
10545 static void
10546 output_loc_list_offset (dw_attr_node *a)
10548 char *sym = AT_loc_list (a)->ll_symbol;
10550 gcc_assert (sym);
10551 if (!dwarf_split_debug_info)
10552 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10553 "%s", dwarf_attr_name (a->dw_attr));
10554 else if (dwarf_version >= 5)
10556 gcc_assert (AT_loc_list (a)->num_assigned);
10557 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10558 dwarf_attr_name (a->dw_attr),
10559 sym);
10561 else
10562 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10563 "%s", dwarf_attr_name (a->dw_attr));
10566 /* Output the offset into the debug_loc section. */
10568 static void
10569 output_view_list_offset (dw_attr_node *a)
10571 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10573 gcc_assert (sym);
10574 if (dwarf_split_debug_info)
10575 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10576 "%s", dwarf_attr_name (a->dw_attr));
10577 else
10578 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10579 "%s", dwarf_attr_name (a->dw_attr));
10582 /* Output an attribute's index or value appropriately. */
10584 static void
10585 output_attr_index_or_value (dw_attr_node *a)
10587 const char *name = dwarf_attr_name (a->dw_attr);
10589 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10591 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10592 return;
10594 switch (AT_class (a))
10596 case dw_val_class_addr:
10597 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10598 break;
10599 case dw_val_class_high_pc:
10600 case dw_val_class_lbl_id:
10601 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10602 break;
10603 default:
10604 gcc_unreachable ();
10608 /* Output a type signature. */
10610 static inline void
10611 output_signature (const char *sig, const char *name)
10613 int i;
10615 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10616 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10619 /* Output a discriminant value. */
10621 static inline void
10622 output_discr_value (dw_discr_value *discr_value, const char *name)
10624 if (discr_value->pos)
10625 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10626 else
10627 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10630 /* Output the DIE and its attributes. Called recursively to generate
10631 the definitions of each child DIE. */
10633 static void
10634 output_die (dw_die_ref die)
10636 dw_attr_node *a;
10637 dw_die_ref c;
10638 unsigned long size;
10639 unsigned ix;
10641 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10642 (unsigned long)die->die_offset,
10643 dwarf_tag_name (die->die_tag));
10645 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10647 const char *name = dwarf_attr_name (a->dw_attr);
10649 switch (AT_class (a))
10651 case dw_val_class_addr:
10652 output_attr_index_or_value (a);
10653 break;
10655 case dw_val_class_offset:
10656 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10657 "%s", name);
10658 break;
10660 case dw_val_class_range_list:
10661 output_range_list_offset (a);
10662 break;
10664 case dw_val_class_loc:
10665 size = size_of_locs (AT_loc (a));
10667 /* Output the block length for this list of location operations. */
10668 if (dwarf_version >= 4)
10669 dw2_asm_output_data_uleb128 (size, "%s", name);
10670 else
10671 dw2_asm_output_data (constant_size (size), size, "%s", name);
10673 output_loc_sequence (AT_loc (a), -1);
10674 break;
10676 case dw_val_class_const:
10677 /* ??? It would be slightly more efficient to use a scheme like is
10678 used for unsigned constants below, but gdb 4.x does not sign
10679 extend. Gdb 5.x does sign extend. */
10680 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10681 break;
10683 case dw_val_class_unsigned_const:
10685 int csize = constant_size (AT_unsigned (a));
10686 if (dwarf_version == 3
10687 && a->dw_attr == DW_AT_data_member_location
10688 && csize >= 4)
10689 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10690 else
10691 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10693 break;
10695 case dw_val_class_symview:
10697 int vsize;
10698 if (symview_upper_bound <= 0xff)
10699 vsize = 1;
10700 else if (symview_upper_bound <= 0xffff)
10701 vsize = 2;
10702 else if (symview_upper_bound <= 0xffffffff)
10703 vsize = 4;
10704 else
10705 vsize = 8;
10706 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10707 "%s", name);
10709 break;
10711 case dw_val_class_const_implicit:
10712 if (flag_debug_asm)
10713 fprintf (asm_out_file, "\t\t\t%s %s ("
10714 HOST_WIDE_INT_PRINT_DEC ")\n",
10715 ASM_COMMENT_START, name, AT_int (a));
10716 break;
10718 case dw_val_class_unsigned_const_implicit:
10719 if (flag_debug_asm)
10720 fprintf (asm_out_file, "\t\t\t%s %s ("
10721 HOST_WIDE_INT_PRINT_HEX ")\n",
10722 ASM_COMMENT_START, name, AT_unsigned (a));
10723 break;
10725 case dw_val_class_const_double:
10727 unsigned HOST_WIDE_INT first, second;
10729 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10730 dw2_asm_output_data (1,
10731 HOST_BITS_PER_DOUBLE_INT
10732 / HOST_BITS_PER_CHAR,
10733 NULL);
10735 if (WORDS_BIG_ENDIAN)
10737 first = a->dw_attr_val.v.val_double.high;
10738 second = a->dw_attr_val.v.val_double.low;
10740 else
10742 first = a->dw_attr_val.v.val_double.low;
10743 second = a->dw_attr_val.v.val_double.high;
10746 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10747 first, "%s", name);
10748 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10749 second, NULL);
10751 break;
10753 case dw_val_class_wide_int:
10755 int i;
10756 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10757 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10758 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10759 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10760 * l, NULL);
10762 if (WORDS_BIG_ENDIAN)
10763 for (i = len - 1; i >= 0; --i)
10765 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10766 "%s", name);
10767 name = "";
10769 else
10770 for (i = 0; i < len; ++i)
10772 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10773 "%s", name);
10774 name = "";
10777 break;
10779 case dw_val_class_vec:
10781 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10782 unsigned int len = a->dw_attr_val.v.val_vec.length;
10783 unsigned int i;
10784 unsigned char *p;
10786 dw2_asm_output_data (constant_size (len * elt_size),
10787 len * elt_size, "%s", name);
10788 if (elt_size > sizeof (HOST_WIDE_INT))
10790 elt_size /= 2;
10791 len *= 2;
10793 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10794 i < len;
10795 i++, p += elt_size)
10796 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10797 "fp or vector constant word %u", i);
10798 break;
10801 case dw_val_class_flag:
10802 if (dwarf_version >= 4)
10804 /* Currently all add_AT_flag calls pass in 1 as last argument,
10805 so DW_FORM_flag_present can be used. If that ever changes,
10806 we'll need to use DW_FORM_flag and have some optimization
10807 in build_abbrev_table that will change those to
10808 DW_FORM_flag_present if it is set to 1 in all DIEs using
10809 the same abbrev entry. */
10810 gcc_assert (AT_flag (a) == 1);
10811 if (flag_debug_asm)
10812 fprintf (asm_out_file, "\t\t\t%s %s\n",
10813 ASM_COMMENT_START, name);
10814 break;
10816 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10817 break;
10819 case dw_val_class_loc_list:
10820 output_loc_list_offset (a);
10821 break;
10823 case dw_val_class_view_list:
10824 output_view_list_offset (a);
10825 break;
10827 case dw_val_class_die_ref:
10828 if (AT_ref_external (a))
10830 if (AT_ref (a)->comdat_type_p)
10832 comdat_type_node *type_node
10833 = AT_ref (a)->die_id.die_type_node;
10835 gcc_assert (type_node);
10836 output_signature (type_node->signature, name);
10838 else
10840 const char *sym = AT_ref (a)->die_id.die_symbol;
10841 int size;
10843 gcc_assert (sym);
10844 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10845 length, whereas in DWARF3 it's always sized as an
10846 offset. */
10847 if (dwarf_version == 2)
10848 size = DWARF2_ADDR_SIZE;
10849 else
10850 size = DWARF_OFFSET_SIZE;
10851 /* ??? We cannot unconditionally output die_offset if
10852 non-zero - others might create references to those
10853 DIEs via symbols.
10854 And we do not clear its DIE offset after outputting it
10855 (and the label refers to the actual DIEs, not the
10856 DWARF CU unit header which is when using label + offset
10857 would be the correct thing to do).
10858 ??? This is the reason for the with_offset flag. */
10859 if (AT_ref (a)->with_offset)
10860 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10861 debug_info_section, "%s", name);
10862 else
10863 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10864 name);
10867 else
10869 gcc_assert (AT_ref (a)->die_offset);
10870 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10871 "%s", name);
10873 break;
10875 case dw_val_class_fde_ref:
10877 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10879 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10880 a->dw_attr_val.v.val_fde_index * 2);
10881 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10882 "%s", name);
10884 break;
10886 case dw_val_class_vms_delta:
10887 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10888 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10889 AT_vms_delta2 (a), AT_vms_delta1 (a),
10890 "%s", name);
10891 #else
10892 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10893 AT_vms_delta2 (a), AT_vms_delta1 (a),
10894 "%s", name);
10895 #endif
10896 break;
10898 case dw_val_class_lbl_id:
10899 output_attr_index_or_value (a);
10900 break;
10902 case dw_val_class_lineptr:
10903 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10904 debug_line_section, "%s", name);
10905 break;
10907 case dw_val_class_macptr:
10908 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10909 debug_macinfo_section, "%s", name);
10910 break;
10912 case dw_val_class_loclistsptr:
10913 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10914 debug_loc_section, "%s", name);
10915 break;
10917 case dw_val_class_str:
10918 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10919 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10920 a->dw_attr_val.v.val_str->label,
10921 debug_str_section,
10922 "%s: \"%s\"", name, AT_string (a));
10923 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10924 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10925 a->dw_attr_val.v.val_str->label,
10926 debug_line_str_section,
10927 "%s: \"%s\"", name, AT_string (a));
10928 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
10929 dw2_asm_output_data_uleb128 (AT_index (a),
10930 "%s: \"%s\"", name, AT_string (a));
10931 else
10932 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10933 break;
10935 case dw_val_class_file:
10937 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10939 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10940 a->dw_attr_val.v.val_file->filename);
10941 break;
10944 case dw_val_class_file_implicit:
10945 if (flag_debug_asm)
10946 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10947 ASM_COMMENT_START, name,
10948 maybe_emit_file (a->dw_attr_val.v.val_file),
10949 a->dw_attr_val.v.val_file->filename);
10950 break;
10952 case dw_val_class_data8:
10954 int i;
10956 for (i = 0; i < 8; i++)
10957 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10958 i == 0 ? "%s" : NULL, name);
10959 break;
10962 case dw_val_class_high_pc:
10963 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10964 get_AT_low_pc (die), "DW_AT_high_pc");
10965 break;
10967 case dw_val_class_discr_value:
10968 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10969 break;
10971 case dw_val_class_discr_list:
10973 dw_discr_list_ref list = AT_discr_list (a);
10974 const int size = size_of_discr_list (list);
10976 /* This is a block, so output its length first. */
10977 dw2_asm_output_data (constant_size (size), size,
10978 "%s: block size", name);
10980 for (; list != NULL; list = list->dw_discr_next)
10982 /* One byte for the discriminant value descriptor, and then as
10983 many LEB128 numbers as required. */
10984 if (list->dw_discr_range)
10985 dw2_asm_output_data (1, DW_DSC_range,
10986 "%s: DW_DSC_range", name);
10987 else
10988 dw2_asm_output_data (1, DW_DSC_label,
10989 "%s: DW_DSC_label", name);
10991 output_discr_value (&list->dw_discr_lower_bound, name);
10992 if (list->dw_discr_range)
10993 output_discr_value (&list->dw_discr_upper_bound, name);
10995 break;
10998 default:
10999 gcc_unreachable ();
11003 FOR_EACH_CHILD (die, c, output_die (c));
11005 /* Add null byte to terminate sibling list. */
11006 if (die->die_child != NULL)
11007 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11008 (unsigned long) die->die_offset);
11011 /* Output the dwarf version number. */
11013 static void
11014 output_dwarf_version ()
11016 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11017 views in loclist. That will change eventually. */
11018 if (dwarf_version == 6)
11020 static bool once;
11021 if (!once)
11023 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11024 "incompatibilities");
11025 once = true;
11027 dw2_asm_output_data (2, 5, "DWARF version number");
11029 else
11030 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11033 /* Output the compilation unit that appears at the beginning of the
11034 .debug_info section, and precedes the DIE descriptions. */
11036 static void
11037 output_compilation_unit_header (enum dwarf_unit_type ut)
11039 if (!XCOFF_DEBUGGING_INFO)
11041 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11042 dw2_asm_output_data (4, 0xffffffff,
11043 "Initial length escape value indicating 64-bit DWARF extension");
11044 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11045 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11046 "Length of Compilation Unit Info");
11049 output_dwarf_version ();
11050 if (dwarf_version >= 5)
11052 const char *name;
11053 switch (ut)
11055 case DW_UT_compile: name = "DW_UT_compile"; break;
11056 case DW_UT_type: name = "DW_UT_type"; break;
11057 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11058 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11059 default: gcc_unreachable ();
11061 dw2_asm_output_data (1, ut, "%s", name);
11062 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11064 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11065 debug_abbrev_section,
11066 "Offset Into Abbrev. Section");
11067 if (dwarf_version < 5)
11068 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11071 /* Output the compilation unit DIE and its children. */
11073 static void
11074 output_comp_unit (dw_die_ref die, int output_if_empty,
11075 const unsigned char *dwo_id)
11077 const char *secname, *oldsym;
11078 char *tmp;
11080 /* Unless we are outputting main CU, we may throw away empty ones. */
11081 if (!output_if_empty && die->die_child == NULL)
11082 return;
11084 /* Even if there are no children of this DIE, we must output the information
11085 about the compilation unit. Otherwise, on an empty translation unit, we
11086 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11087 will then complain when examining the file. First mark all the DIEs in
11088 this CU so we know which get local refs. */
11089 mark_dies (die);
11091 external_ref_hash_type *extern_map = optimize_external_refs (die);
11093 /* For now, optimize only the main CU, in order to optimize the rest
11094 we'd need to see all of them earlier. Leave the rest for post-linking
11095 tools like DWZ. */
11096 if (die == comp_unit_die ())
11097 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11099 build_abbrev_table (die, extern_map);
11101 optimize_abbrev_table ();
11103 delete extern_map;
11105 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11106 next_die_offset = (dwo_id
11107 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11108 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11109 calc_die_sizes (die);
11111 oldsym = die->die_id.die_symbol;
11112 if (oldsym && die->comdat_type_p)
11114 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11116 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11117 secname = tmp;
11118 die->die_id.die_symbol = NULL;
11119 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11121 else
11123 switch_to_section (debug_info_section);
11124 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11125 info_section_emitted = true;
11128 /* For LTO cross unit DIE refs we want a symbol on the start of the
11129 debuginfo section, not on the CU DIE. */
11130 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11132 /* ??? No way to get visibility assembled without a decl. */
11133 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11134 get_identifier (oldsym), char_type_node);
11135 TREE_PUBLIC (decl) = true;
11136 TREE_STATIC (decl) = true;
11137 DECL_ARTIFICIAL (decl) = true;
11138 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11139 DECL_VISIBILITY_SPECIFIED (decl) = true;
11140 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11141 #ifdef ASM_WEAKEN_LABEL
11142 /* We prefer a .weak because that handles duplicates from duplicate
11143 archive members in a graceful way. */
11144 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11145 #else
11146 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11147 #endif
11148 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11151 /* Output debugging information. */
11152 output_compilation_unit_header (dwo_id
11153 ? DW_UT_split_compile : DW_UT_compile);
11154 if (dwarf_version >= 5)
11156 if (dwo_id != NULL)
11157 for (int i = 0; i < 8; i++)
11158 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11160 output_die (die);
11162 /* Leave the marks on the main CU, so we can check them in
11163 output_pubnames. */
11164 if (oldsym)
11166 unmark_dies (die);
11167 die->die_id.die_symbol = oldsym;
11171 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11172 and .debug_pubtypes. This is configured per-target, but can be
11173 overridden by the -gpubnames or -gno-pubnames options. */
11175 static inline bool
11176 want_pubnames (void)
11178 if (debug_info_level <= DINFO_LEVEL_TERSE
11179 /* Names and types go to the early debug part only. */
11180 || in_lto_p)
11181 return false;
11182 if (debug_generate_pub_sections != -1)
11183 return debug_generate_pub_sections;
11184 return targetm.want_debug_pub_sections;
11187 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11189 static void
11190 add_AT_pubnames (dw_die_ref die)
11192 if (want_pubnames ())
11193 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11196 /* Add a string attribute value to a skeleton DIE. */
11198 static inline void
11199 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11200 const char *str)
11202 dw_attr_node attr;
11203 struct indirect_string_node *node;
11205 if (! skeleton_debug_str_hash)
11206 skeleton_debug_str_hash
11207 = hash_table<indirect_string_hasher>::create_ggc (10);
11209 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11210 find_string_form (node);
11211 if (node->form == dwarf_FORM (DW_FORM_strx))
11212 node->form = DW_FORM_strp;
11214 attr.dw_attr = attr_kind;
11215 attr.dw_attr_val.val_class = dw_val_class_str;
11216 attr.dw_attr_val.val_entry = NULL;
11217 attr.dw_attr_val.v.val_str = node;
11218 add_dwarf_attr (die, &attr);
11221 /* Helper function to generate top-level dies for skeleton debug_info and
11222 debug_types. */
11224 static void
11225 add_top_level_skeleton_die_attrs (dw_die_ref die)
11227 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11228 const char *comp_dir = comp_dir_string ();
11230 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11231 if (comp_dir != NULL)
11232 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11233 add_AT_pubnames (die);
11234 if (addr_index_table != NULL && addr_index_table->size () > 0)
11235 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11238 /* Output skeleton debug sections that point to the dwo file. */
11240 static void
11241 output_skeleton_debug_sections (dw_die_ref comp_unit,
11242 const unsigned char *dwo_id)
11244 /* These attributes will be found in the full debug_info section. */
11245 remove_AT (comp_unit, DW_AT_producer);
11246 remove_AT (comp_unit, DW_AT_language);
11248 switch_to_section (debug_skeleton_info_section);
11249 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11251 /* Produce the skeleton compilation-unit header. This one differs enough from
11252 a normal CU header that it's better not to call output_compilation_unit
11253 header. */
11254 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11255 dw2_asm_output_data (4, 0xffffffff,
11256 "Initial length escape value indicating 64-bit "
11257 "DWARF extension");
11259 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11260 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11261 - DWARF_INITIAL_LENGTH_SIZE
11262 + size_of_die (comp_unit),
11263 "Length of Compilation Unit Info");
11264 output_dwarf_version ();
11265 if (dwarf_version >= 5)
11267 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11268 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11270 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
11271 debug_skeleton_abbrev_section,
11272 "Offset Into Abbrev. Section");
11273 if (dwarf_version < 5)
11274 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11275 else
11276 for (int i = 0; i < 8; i++)
11277 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11279 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11280 output_die (comp_unit);
11282 /* Build the skeleton debug_abbrev section. */
11283 switch_to_section (debug_skeleton_abbrev_section);
11284 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11286 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11288 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11291 /* Output a comdat type unit DIE and its children. */
11293 static void
11294 output_comdat_type_unit (comdat_type_node *node,
11295 bool early_lto_debug ATTRIBUTE_UNUSED)
11297 const char *secname;
11298 char *tmp;
11299 int i;
11300 #if defined (OBJECT_FORMAT_ELF)
11301 tree comdat_key;
11302 #endif
11304 /* First mark all the DIEs in this CU so we know which get local refs. */
11305 mark_dies (node->root_die);
11307 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11309 build_abbrev_table (node->root_die, extern_map);
11311 delete extern_map;
11312 extern_map = NULL;
11314 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11315 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11316 calc_die_sizes (node->root_die);
11318 #if defined (OBJECT_FORMAT_ELF)
11319 if (dwarf_version >= 5)
11321 if (!dwarf_split_debug_info)
11322 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11323 else
11324 secname = (early_lto_debug
11325 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11327 else if (!dwarf_split_debug_info)
11328 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11329 else
11330 secname = (early_lto_debug
11331 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11333 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11334 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11335 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11336 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11337 comdat_key = get_identifier (tmp);
11338 targetm.asm_out.named_section (secname,
11339 SECTION_DEBUG | SECTION_LINKONCE,
11340 comdat_key);
11341 #else
11342 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11343 sprintf (tmp, (dwarf_version >= 5
11344 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11345 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11346 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11347 secname = tmp;
11348 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11349 #endif
11351 /* Output debugging information. */
11352 output_compilation_unit_header (dwarf_split_debug_info
11353 ? DW_UT_split_type : DW_UT_type);
11354 output_signature (node->signature, "Type Signature");
11355 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11356 "Offset to Type DIE");
11357 output_die (node->root_die);
11359 unmark_dies (node->root_die);
11362 /* Return the DWARF2/3 pubname associated with a decl. */
11364 static const char *
11365 dwarf2_name (tree decl, int scope)
11367 if (DECL_NAMELESS (decl))
11368 return NULL;
11369 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11372 /* Add a new entry to .debug_pubnames if appropriate. */
11374 static void
11375 add_pubname_string (const char *str, dw_die_ref die)
11377 pubname_entry e;
11379 e.die = die;
11380 e.name = xstrdup (str);
11381 vec_safe_push (pubname_table, e);
11384 static void
11385 add_pubname (tree decl, dw_die_ref die)
11387 if (!want_pubnames ())
11388 return;
11390 /* Don't add items to the table when we expect that the consumer will have
11391 just read the enclosing die. For example, if the consumer is looking at a
11392 class_member, it will either be inside the class already, or will have just
11393 looked up the class to find the member. Either way, searching the class is
11394 faster than searching the index. */
11395 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11396 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11398 const char *name = dwarf2_name (decl, 1);
11400 if (name)
11401 add_pubname_string (name, die);
11405 /* Add an enumerator to the pubnames section. */
11407 static void
11408 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11410 pubname_entry e;
11412 gcc_assert (scope_name);
11413 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11414 e.die = die;
11415 vec_safe_push (pubname_table, e);
11418 /* Add a new entry to .debug_pubtypes if appropriate. */
11420 static void
11421 add_pubtype (tree decl, dw_die_ref die)
11423 pubname_entry e;
11425 if (!want_pubnames ())
11426 return;
11428 if ((TREE_PUBLIC (decl)
11429 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11430 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11432 tree scope = NULL;
11433 const char *scope_name = "";
11434 const char *sep = is_cxx () ? "::" : ".";
11435 const char *name;
11437 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11438 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11440 scope_name = lang_hooks.dwarf_name (scope, 1);
11441 if (scope_name != NULL && scope_name[0] != '\0')
11442 scope_name = concat (scope_name, sep, NULL);
11443 else
11444 scope_name = "";
11447 if (TYPE_P (decl))
11448 name = type_tag (decl);
11449 else
11450 name = lang_hooks.dwarf_name (decl, 1);
11452 /* If we don't have a name for the type, there's no point in adding
11453 it to the table. */
11454 if (name != NULL && name[0] != '\0')
11456 e.die = die;
11457 e.name = concat (scope_name, name, NULL);
11458 vec_safe_push (pubtype_table, e);
11461 /* Although it might be more consistent to add the pubinfo for the
11462 enumerators as their dies are created, they should only be added if the
11463 enum type meets the criteria above. So rather than re-check the parent
11464 enum type whenever an enumerator die is created, just output them all
11465 here. This isn't protected by the name conditional because anonymous
11466 enums don't have names. */
11467 if (die->die_tag == DW_TAG_enumeration_type)
11469 dw_die_ref c;
11471 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11476 /* Output a single entry in the pubnames table. */
11478 static void
11479 output_pubname (dw_offset die_offset, pubname_entry *entry)
11481 dw_die_ref die = entry->die;
11482 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11484 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
11486 if (debug_generate_pub_sections == 2)
11488 /* This logic follows gdb's method for determining the value of the flag
11489 byte. */
11490 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11491 switch (die->die_tag)
11493 case DW_TAG_typedef:
11494 case DW_TAG_base_type:
11495 case DW_TAG_subrange_type:
11496 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11497 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11498 break;
11499 case DW_TAG_enumerator:
11500 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11501 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11502 if (!is_cxx ())
11503 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11504 break;
11505 case DW_TAG_subprogram:
11506 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11507 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11508 if (!is_ada ())
11509 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11510 break;
11511 case DW_TAG_constant:
11512 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11513 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11514 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11515 break;
11516 case DW_TAG_variable:
11517 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11518 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11519 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11520 break;
11521 case DW_TAG_namespace:
11522 case DW_TAG_imported_declaration:
11523 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11524 break;
11525 case DW_TAG_class_type:
11526 case DW_TAG_interface_type:
11527 case DW_TAG_structure_type:
11528 case DW_TAG_union_type:
11529 case DW_TAG_enumeration_type:
11530 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11531 if (!is_cxx ())
11532 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11533 break;
11534 default:
11535 /* An unusual tag. Leave the flag-byte empty. */
11536 break;
11538 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11539 "GDB-index flags");
11542 dw2_asm_output_nstring (entry->name, -1, "external name");
11546 /* Output the public names table used to speed up access to externally
11547 visible names; or the public types table used to find type definitions. */
11549 static void
11550 output_pubnames (vec<pubname_entry, va_gc> *names)
11552 unsigned i;
11553 unsigned long pubnames_length = size_of_pubnames (names);
11554 pubname_entry *pub;
11556 if (!XCOFF_DEBUGGING_INFO)
11558 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11559 dw2_asm_output_data (4, 0xffffffff,
11560 "Initial length escape value indicating 64-bit DWARF extension");
11561 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11562 "Pub Info Length");
11565 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11566 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11568 if (dwarf_split_debug_info)
11569 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11570 debug_skeleton_info_section,
11571 "Offset of Compilation Unit Info");
11572 else
11573 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11574 debug_info_section,
11575 "Offset of Compilation Unit Info");
11576 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11577 "Compilation Unit Length");
11579 FOR_EACH_VEC_ELT (*names, i, pub)
11581 if (include_pubname_in_output (names, pub))
11583 dw_offset die_offset = pub->die->die_offset;
11585 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11586 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11587 gcc_assert (pub->die->die_mark);
11589 /* If we're putting types in their own .debug_types sections,
11590 the .debug_pubtypes table will still point to the compile
11591 unit (not the type unit), so we want to use the offset of
11592 the skeleton DIE (if there is one). */
11593 if (pub->die->comdat_type_p && names == pubtype_table)
11595 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11597 if (type_node != NULL)
11598 die_offset = (type_node->skeleton_die != NULL
11599 ? type_node->skeleton_die->die_offset
11600 : comp_unit_die ()->die_offset);
11603 output_pubname (die_offset, pub);
11607 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11610 /* Output public names and types tables if necessary. */
11612 static void
11613 output_pubtables (void)
11615 if (!want_pubnames () || !info_section_emitted)
11616 return;
11618 switch_to_section (debug_pubnames_section);
11619 output_pubnames (pubname_table);
11620 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11621 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11622 simply won't look for the section. */
11623 switch_to_section (debug_pubtypes_section);
11624 output_pubnames (pubtype_table);
11628 /* Output the information that goes into the .debug_aranges table.
11629 Namely, define the beginning and ending address range of the
11630 text section generated for this compilation unit. */
11632 static void
11633 output_aranges (void)
11635 unsigned i;
11636 unsigned long aranges_length = size_of_aranges ();
11638 if (!XCOFF_DEBUGGING_INFO)
11640 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11641 dw2_asm_output_data (4, 0xffffffff,
11642 "Initial length escape value indicating 64-bit DWARF extension");
11643 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11644 "Length of Address Ranges Info");
11647 /* Version number for aranges is still 2, even up to DWARF5. */
11648 dw2_asm_output_data (2, 2, "DWARF aranges version");
11649 if (dwarf_split_debug_info)
11650 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11651 debug_skeleton_info_section,
11652 "Offset of Compilation Unit Info");
11653 else
11654 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11655 debug_info_section,
11656 "Offset of Compilation Unit Info");
11657 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11658 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11660 /* We need to align to twice the pointer size here. */
11661 if (DWARF_ARANGES_PAD_SIZE)
11663 /* Pad using a 2 byte words so that padding is correct for any
11664 pointer size. */
11665 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11666 2 * DWARF2_ADDR_SIZE);
11667 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11668 dw2_asm_output_data (2, 0, NULL);
11671 /* It is necessary not to output these entries if the sections were
11672 not used; if the sections were not used, the length will be 0 and
11673 the address may end up as 0 if the section is discarded by ld
11674 --gc-sections, leaving an invalid (0, 0) entry that can be
11675 confused with the terminator. */
11676 if (text_section_used)
11678 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11679 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11680 text_section_label, "Length");
11682 if (cold_text_section_used)
11684 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11685 "Address");
11686 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11687 cold_text_section_label, "Length");
11690 if (have_multiple_function_sections)
11692 unsigned fde_idx;
11693 dw_fde_ref fde;
11695 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11697 if (DECL_IGNORED_P (fde->decl))
11698 continue;
11699 if (!fde->in_std_section)
11701 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11702 "Address");
11703 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11704 fde->dw_fde_begin, "Length");
11706 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11708 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11709 "Address");
11710 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11711 fde->dw_fde_second_begin, "Length");
11716 /* Output the terminator words. */
11717 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11718 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11721 /* Add a new entry to .debug_ranges. Return its index into
11722 ranges_table vector. */
11724 static unsigned int
11725 add_ranges_num (int num, bool maybe_new_sec)
11727 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11728 vec_safe_push (ranges_table, r);
11729 return vec_safe_length (ranges_table) - 1;
11732 /* Add a new entry to .debug_ranges corresponding to a block, or a
11733 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11734 this entry might be in a different section from previous range. */
11736 static unsigned int
11737 add_ranges (const_tree block, bool maybe_new_sec)
11739 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11742 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11743 chain, or middle entry of a chain that will be directly referred to. */
11745 static void
11746 note_rnglist_head (unsigned int offset)
11748 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11749 return;
11750 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11753 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11754 When using dwarf_split_debug_info, address attributes in dies destined
11755 for the final executable should be direct references--setting the
11756 parameter force_direct ensures this behavior. */
11758 static void
11759 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11760 bool *added, bool force_direct)
11762 unsigned int in_use = vec_safe_length (ranges_by_label);
11763 unsigned int offset;
11764 dw_ranges_by_label rbl = { begin, end };
11765 vec_safe_push (ranges_by_label, rbl);
11766 offset = add_ranges_num (-(int)in_use - 1, true);
11767 if (!*added)
11769 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11770 *added = true;
11771 note_rnglist_head (offset);
11775 /* Emit .debug_ranges section. */
11777 static void
11778 output_ranges (void)
11780 unsigned i;
11781 static const char *const start_fmt = "Offset %#x";
11782 const char *fmt = start_fmt;
11783 dw_ranges *r;
11785 switch_to_section (debug_ranges_section);
11786 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11787 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11789 int block_num = r->num;
11791 if (block_num > 0)
11793 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11794 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11796 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11797 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11799 /* If all code is in the text section, then the compilation
11800 unit base address defaults to DW_AT_low_pc, which is the
11801 base of the text section. */
11802 if (!have_multiple_function_sections)
11804 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11805 text_section_label,
11806 fmt, i * 2 * DWARF2_ADDR_SIZE);
11807 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11808 text_section_label, NULL);
11811 /* Otherwise, the compilation unit base address is zero,
11812 which allows us to use absolute addresses, and not worry
11813 about whether the target supports cross-section
11814 arithmetic. */
11815 else
11817 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11818 fmt, i * 2 * DWARF2_ADDR_SIZE);
11819 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11822 fmt = NULL;
11825 /* Negative block_num stands for an index into ranges_by_label. */
11826 else if (block_num < 0)
11828 int lab_idx = - block_num - 1;
11830 if (!have_multiple_function_sections)
11832 gcc_unreachable ();
11833 #if 0
11834 /* If we ever use add_ranges_by_labels () for a single
11835 function section, all we have to do is to take out
11836 the #if 0 above. */
11837 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11838 (*ranges_by_label)[lab_idx].begin,
11839 text_section_label,
11840 fmt, i * 2 * DWARF2_ADDR_SIZE);
11841 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11842 (*ranges_by_label)[lab_idx].end,
11843 text_section_label, NULL);
11844 #endif
11846 else
11848 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11849 (*ranges_by_label)[lab_idx].begin,
11850 fmt, i * 2 * DWARF2_ADDR_SIZE);
11851 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11852 (*ranges_by_label)[lab_idx].end,
11853 NULL);
11856 else
11858 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11859 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11860 fmt = start_fmt;
11865 /* Non-zero if .debug_line_str should be used for .debug_line section
11866 strings or strings that are likely shareable with those. */
11867 #define DWARF5_USE_DEBUG_LINE_STR \
11868 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11869 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11870 /* FIXME: there is no .debug_line_str.dwo section, \
11871 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11872 && !dwarf_split_debug_info)
11875 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
11876 DWARF5 .debug_line tables using .debug_line_str or we generate
11877 it ourselves, except for split-dwarf which doesn't have a
11878 .debug_line_str. */
11879 static bool
11880 asm_outputs_debug_line_str (void)
11882 if (dwarf_version >= 5
11883 && ! output_asm_line_debug_info ()
11884 && DWARF5_USE_DEBUG_LINE_STR)
11885 return true;
11886 else
11888 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
11889 return !dwarf_split_debug_info && dwarf_version >= 5;
11890 #else
11891 return false;
11892 #endif
11897 /* Assign .debug_rnglists indexes. */
11899 static void
11900 index_rnglists (void)
11902 unsigned i;
11903 dw_ranges *r;
11905 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11906 if (r->label)
11907 r->idx = rnglist_idx++;
11910 /* Emit .debug_rnglists section. */
11912 static void
11913 output_rnglists (unsigned generation)
11915 unsigned i;
11916 dw_ranges *r;
11917 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11918 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11919 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11921 switch_to_section (debug_ranges_section);
11922 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11923 /* There are up to 4 unique ranges labels per generation.
11924 See also init_sections_and_labels. */
11925 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11926 2 + generation * 4);
11927 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11928 3 + generation * 4);
11929 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11930 dw2_asm_output_data (4, 0xffffffff,
11931 "Initial length escape value indicating "
11932 "64-bit DWARF extension");
11933 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11934 "Length of Range Lists");
11935 ASM_OUTPUT_LABEL (asm_out_file, l1);
11936 output_dwarf_version ();
11937 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11938 dw2_asm_output_data (1, 0, "Segment Size");
11939 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11940 about relocation sizes and primarily care about the size of .debug*
11941 sections in linked shared libraries and executables, then
11942 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11943 into it are usually larger than just DW_FORM_sec_offset offsets
11944 into the .debug_rnglists section. */
11945 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11946 "Offset Entry Count");
11947 if (dwarf_split_debug_info)
11949 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11950 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11951 if (r->label)
11952 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11953 ranges_base_label, NULL);
11956 const char *lab = "";
11957 unsigned int len = vec_safe_length (ranges_table);
11958 const char *base = NULL;
11959 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11961 int block_num = r->num;
11963 if (r->label)
11965 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11966 lab = r->label;
11968 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11969 base = NULL;
11970 if (block_num > 0)
11972 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11973 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11975 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11976 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11978 if (HAVE_AS_LEB128)
11980 /* If all code is in the text section, then the compilation
11981 unit base address defaults to DW_AT_low_pc, which is the
11982 base of the text section. */
11983 if (!have_multiple_function_sections)
11985 dw2_asm_output_data (1, DW_RLE_offset_pair,
11986 "DW_RLE_offset_pair (%s)", lab);
11987 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11988 "Range begin address (%s)", lab);
11989 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11990 "Range end address (%s)", lab);
11991 continue;
11993 if (base == NULL)
11995 dw_ranges *r2 = NULL;
11996 if (i < len - 1)
11997 r2 = &(*ranges_table)[i + 1];
11998 if (r2
11999 && r2->num != 0
12000 && r2->label == NULL
12001 && !r2->maybe_new_sec)
12003 dw2_asm_output_data (1, DW_RLE_base_address,
12004 "DW_RLE_base_address (%s)", lab);
12005 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12006 "Base address (%s)", lab);
12007 strcpy (basebuf, blabel);
12008 base = basebuf;
12011 if (base)
12013 dw2_asm_output_data (1, DW_RLE_offset_pair,
12014 "DW_RLE_offset_pair (%s)", lab);
12015 dw2_asm_output_delta_uleb128 (blabel, base,
12016 "Range begin address (%s)", lab);
12017 dw2_asm_output_delta_uleb128 (elabel, base,
12018 "Range end address (%s)", lab);
12019 continue;
12021 dw2_asm_output_data (1, DW_RLE_start_length,
12022 "DW_RLE_start_length (%s)", lab);
12023 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12024 "Range begin address (%s)", lab);
12025 dw2_asm_output_delta_uleb128 (elabel, blabel,
12026 "Range length (%s)", lab);
12028 else
12030 dw2_asm_output_data (1, DW_RLE_start_end,
12031 "DW_RLE_start_end (%s)", lab);
12032 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12033 "Range begin address (%s)", lab);
12034 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12035 "Range end address (%s)", lab);
12039 /* Negative block_num stands for an index into ranges_by_label. */
12040 else if (block_num < 0)
12042 int lab_idx = - block_num - 1;
12043 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12044 const char *elabel = (*ranges_by_label)[lab_idx].end;
12046 if (!have_multiple_function_sections)
12047 gcc_unreachable ();
12048 if (HAVE_AS_LEB128)
12050 dw2_asm_output_data (1, DW_RLE_start_length,
12051 "DW_RLE_start_length (%s)", lab);
12052 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12053 "Range begin address (%s)", lab);
12054 dw2_asm_output_delta_uleb128 (elabel, blabel,
12055 "Range length (%s)", lab);
12057 else
12059 dw2_asm_output_data (1, DW_RLE_start_end,
12060 "DW_RLE_start_end (%s)", lab);
12061 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12062 "Range begin address (%s)", lab);
12063 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12064 "Range end address (%s)", lab);
12067 else
12068 dw2_asm_output_data (1, DW_RLE_end_of_list,
12069 "DW_RLE_end_of_list (%s)", lab);
12071 ASM_OUTPUT_LABEL (asm_out_file, l2);
12074 /* Data structure containing information about input files. */
12075 struct file_info
12077 const char *path; /* Complete file name. */
12078 const char *fname; /* File name part. */
12079 int length; /* Length of entire string. */
12080 struct dwarf_file_data * file_idx; /* Index in input file table. */
12081 int dir_idx; /* Index in directory table. */
12084 /* Data structure containing information about directories with source
12085 files. */
12086 struct dir_info
12088 const char *path; /* Path including directory name. */
12089 int length; /* Path length. */
12090 int prefix; /* Index of directory entry which is a prefix. */
12091 int count; /* Number of files in this directory. */
12092 int dir_idx; /* Index of directory used as base. */
12095 /* Callback function for file_info comparison. We sort by looking at
12096 the directories in the path. */
12098 static int
12099 file_info_cmp (const void *p1, const void *p2)
12101 const struct file_info *const s1 = (const struct file_info *) p1;
12102 const struct file_info *const s2 = (const struct file_info *) p2;
12103 const unsigned char *cp1;
12104 const unsigned char *cp2;
12106 /* Take care of file names without directories. We need to make sure that
12107 we return consistent values to qsort since some will get confused if
12108 we return the same value when identical operands are passed in opposite
12109 orders. So if neither has a directory, return 0 and otherwise return
12110 1 or -1 depending on which one has the directory. We want the one with
12111 the directory to sort after the one without, so all no directory files
12112 are at the start (normally only the compilation unit file). */
12113 if ((s1->path == s1->fname || s2->path == s2->fname))
12114 return (s2->path == s2->fname) - (s1->path == s1->fname);
12116 cp1 = (const unsigned char *) s1->path;
12117 cp2 = (const unsigned char *) s2->path;
12119 while (1)
12121 ++cp1;
12122 ++cp2;
12123 /* Reached the end of the first path? If so, handle like above,
12124 but now we want longer directory prefixes before shorter ones. */
12125 if ((cp1 == (const unsigned char *) s1->fname)
12126 || (cp2 == (const unsigned char *) s2->fname))
12127 return ((cp1 == (const unsigned char *) s1->fname)
12128 - (cp2 == (const unsigned char *) s2->fname));
12130 /* Character of current path component the same? */
12131 else if (*cp1 != *cp2)
12132 return *cp1 - *cp2;
12136 struct file_name_acquire_data
12138 struct file_info *files;
12139 int used_files;
12140 int max_files;
12143 /* Traversal function for the hash table. */
12146 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12148 struct dwarf_file_data *d = *slot;
12149 struct file_info *fi;
12150 const char *f;
12152 gcc_assert (fnad->max_files >= d->emitted_number);
12154 if (! d->emitted_number)
12155 return 1;
12157 gcc_assert (fnad->max_files != fnad->used_files);
12159 fi = fnad->files + fnad->used_files++;
12161 f = remap_debug_filename (d->filename);
12163 /* Skip all leading "./". */
12164 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12165 f += 2;
12167 /* Create a new array entry. */
12168 fi->path = f;
12169 fi->length = strlen (f);
12170 fi->file_idx = d;
12172 /* Search for the file name part. */
12173 f = strrchr (f, DIR_SEPARATOR);
12174 #if defined (DIR_SEPARATOR_2)
12176 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12178 if (g != NULL)
12180 if (f == NULL || f < g)
12181 f = g;
12184 #endif
12186 fi->fname = f == NULL ? fi->path : f + 1;
12187 return 1;
12190 /* Helper function for output_file_names. Emit a FORM encoded
12191 string STR, with assembly comment start ENTRY_KIND and
12192 index IDX */
12194 static void
12195 output_line_string (enum dwarf_form form, const char *str,
12196 const char *entry_kind, unsigned int idx)
12198 switch (form)
12200 case DW_FORM_string:
12201 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12202 break;
12203 case DW_FORM_line_strp:
12204 if (!debug_line_str_hash)
12205 debug_line_str_hash
12206 = hash_table<indirect_string_hasher>::create_ggc (10);
12208 struct indirect_string_node *node;
12209 node = find_AT_string_in_table (str, debug_line_str_hash);
12210 set_indirect_string (node);
12211 node->form = form;
12212 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
12213 debug_line_str_section, "%s: %#x: \"%s\"",
12214 entry_kind, 0, node->str);
12215 break;
12216 default:
12217 gcc_unreachable ();
12221 /* Output the directory table and the file name table. We try to minimize
12222 the total amount of memory needed. A heuristic is used to avoid large
12223 slowdowns with many input files. */
12225 static void
12226 output_file_names (void)
12228 struct file_name_acquire_data fnad;
12229 int numfiles;
12230 struct file_info *files;
12231 struct dir_info *dirs;
12232 int *saved;
12233 int *savehere;
12234 int *backmap;
12235 int ndirs;
12236 int idx_offset;
12237 int i;
12239 if (!last_emitted_file)
12241 if (dwarf_version >= 5)
12243 dw2_asm_output_data (1, 0, "Directory entry format count");
12244 dw2_asm_output_data_uleb128 (0, "Directories count");
12245 dw2_asm_output_data (1, 0, "File name entry format count");
12246 dw2_asm_output_data_uleb128 (0, "File names count");
12248 else
12250 dw2_asm_output_data (1, 0, "End directory table");
12251 dw2_asm_output_data (1, 0, "End file name table");
12253 return;
12256 numfiles = last_emitted_file->emitted_number;
12258 /* Allocate the various arrays we need. */
12259 files = XALLOCAVEC (struct file_info, numfiles);
12260 dirs = XALLOCAVEC (struct dir_info, numfiles);
12262 fnad.files = files;
12263 fnad.used_files = 0;
12264 fnad.max_files = numfiles;
12265 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12266 gcc_assert (fnad.used_files == fnad.max_files);
12268 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12270 /* Find all the different directories used. */
12271 dirs[0].path = files[0].path;
12272 dirs[0].length = files[0].fname - files[0].path;
12273 dirs[0].prefix = -1;
12274 dirs[0].count = 1;
12275 dirs[0].dir_idx = 0;
12276 files[0].dir_idx = 0;
12277 ndirs = 1;
12279 for (i = 1; i < numfiles; i++)
12280 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12281 && memcmp (dirs[ndirs - 1].path, files[i].path,
12282 dirs[ndirs - 1].length) == 0)
12284 /* Same directory as last entry. */
12285 files[i].dir_idx = ndirs - 1;
12286 ++dirs[ndirs - 1].count;
12288 else
12290 int j;
12292 /* This is a new directory. */
12293 dirs[ndirs].path = files[i].path;
12294 dirs[ndirs].length = files[i].fname - files[i].path;
12295 dirs[ndirs].count = 1;
12296 dirs[ndirs].dir_idx = ndirs;
12297 files[i].dir_idx = ndirs;
12299 /* Search for a prefix. */
12300 dirs[ndirs].prefix = -1;
12301 for (j = 0; j < ndirs; j++)
12302 if (dirs[j].length < dirs[ndirs].length
12303 && dirs[j].length > 1
12304 && (dirs[ndirs].prefix == -1
12305 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12306 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12307 dirs[ndirs].prefix = j;
12309 ++ndirs;
12312 /* Now to the actual work. We have to find a subset of the directories which
12313 allow expressing the file name using references to the directory table
12314 with the least amount of characters. We do not do an exhaustive search
12315 where we would have to check out every combination of every single
12316 possible prefix. Instead we use a heuristic which provides nearly optimal
12317 results in most cases and never is much off. */
12318 saved = XALLOCAVEC (int, ndirs);
12319 savehere = XALLOCAVEC (int, ndirs);
12321 memset (saved, '\0', ndirs * sizeof (saved[0]));
12322 for (i = 0; i < ndirs; i++)
12324 int j;
12325 int total;
12327 /* We can always save some space for the current directory. But this
12328 does not mean it will be enough to justify adding the directory. */
12329 savehere[i] = dirs[i].length;
12330 total = (savehere[i] - saved[i]) * dirs[i].count;
12332 for (j = i + 1; j < ndirs; j++)
12334 savehere[j] = 0;
12335 if (saved[j] < dirs[i].length)
12337 /* Determine whether the dirs[i] path is a prefix of the
12338 dirs[j] path. */
12339 int k;
12341 k = dirs[j].prefix;
12342 while (k != -1 && k != (int) i)
12343 k = dirs[k].prefix;
12345 if (k == (int) i)
12347 /* Yes it is. We can possibly save some memory by
12348 writing the filenames in dirs[j] relative to
12349 dirs[i]. */
12350 savehere[j] = dirs[i].length;
12351 total += (savehere[j] - saved[j]) * dirs[j].count;
12356 /* Check whether we can save enough to justify adding the dirs[i]
12357 directory. */
12358 if (total > dirs[i].length + 1)
12360 /* It's worthwhile adding. */
12361 for (j = i; j < ndirs; j++)
12362 if (savehere[j] > 0)
12364 /* Remember how much we saved for this directory so far. */
12365 saved[j] = savehere[j];
12367 /* Remember the prefix directory. */
12368 dirs[j].dir_idx = i;
12373 /* Emit the directory name table. */
12374 idx_offset = dirs[0].length > 0 ? 1 : 0;
12375 enum dwarf_form str_form = DW_FORM_string;
12376 enum dwarf_form idx_form = DW_FORM_udata;
12377 if (dwarf_version >= 5)
12379 const char *comp_dir = comp_dir_string ();
12380 if (comp_dir == NULL)
12381 comp_dir = "";
12382 dw2_asm_output_data (1, 1, "Directory entry format count");
12383 if (DWARF5_USE_DEBUG_LINE_STR)
12384 str_form = DW_FORM_line_strp;
12385 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12386 dw2_asm_output_data_uleb128 (str_form, "%s",
12387 get_DW_FORM_name (str_form));
12388 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12389 if (str_form == DW_FORM_string)
12391 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12392 for (i = 1 - idx_offset; i < ndirs; i++)
12393 dw2_asm_output_nstring (dirs[i].path,
12394 dirs[i].length
12395 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12396 "Directory Entry: %#x", i + idx_offset);
12398 else
12400 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12401 for (i = 1 - idx_offset; i < ndirs; i++)
12403 const char *str
12404 = ggc_alloc_string (dirs[i].path,
12405 dirs[i].length
12406 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12407 output_line_string (str_form, str, "Directory Entry",
12408 (unsigned) i + idx_offset);
12412 else
12414 for (i = 1 - idx_offset; i < ndirs; i++)
12415 dw2_asm_output_nstring (dirs[i].path,
12416 dirs[i].length
12417 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12418 "Directory Entry: %#x", i + idx_offset);
12420 dw2_asm_output_data (1, 0, "End directory table");
12423 /* We have to emit them in the order of emitted_number since that's
12424 used in the debug info generation. To do this efficiently we
12425 generate a back-mapping of the indices first. */
12426 backmap = XALLOCAVEC (int, numfiles);
12427 for (i = 0; i < numfiles; i++)
12428 backmap[files[i].file_idx->emitted_number - 1] = i;
12430 if (dwarf_version >= 5)
12432 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12433 if (filename0 == NULL)
12434 filename0 = "";
12435 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12436 DW_FORM_data2. Choose one based on the number of directories
12437 and how much space would they occupy in each encoding.
12438 If we have at most 256 directories, all indexes fit into
12439 a single byte, so DW_FORM_data1 is most compact (if there
12440 are at most 128 directories, DW_FORM_udata would be as
12441 compact as that, but not shorter and slower to decode). */
12442 if (ndirs + idx_offset <= 256)
12443 idx_form = DW_FORM_data1;
12444 /* If there are more than 65536 directories, we have to use
12445 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12446 Otherwise, compute what space would occupy if all the indexes
12447 used DW_FORM_udata - sum - and compare that to how large would
12448 be DW_FORM_data2 encoding, and pick the more efficient one. */
12449 else if (ndirs + idx_offset <= 65536)
12451 unsigned HOST_WIDE_INT sum = 1;
12452 for (i = 0; i < numfiles; i++)
12454 int file_idx = backmap[i];
12455 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12456 sum += size_of_uleb128 (dir_idx);
12458 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12459 idx_form = DW_FORM_data2;
12461 #ifdef VMS_DEBUGGING_INFO
12462 dw2_asm_output_data (1, 4, "File name entry format count");
12463 #else
12464 dw2_asm_output_data (1, 2, "File name entry format count");
12465 #endif
12466 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12467 dw2_asm_output_data_uleb128 (str_form, "%s",
12468 get_DW_FORM_name (str_form));
12469 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12470 "DW_LNCT_directory_index");
12471 dw2_asm_output_data_uleb128 (idx_form, "%s",
12472 get_DW_FORM_name (idx_form));
12473 #ifdef VMS_DEBUGGING_INFO
12474 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12475 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12476 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12477 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12478 #endif
12479 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12481 output_line_string (str_form, filename0, "File Entry", 0);
12483 /* Include directory index. */
12484 if (idx_form != DW_FORM_udata)
12485 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12486 0, NULL);
12487 else
12488 dw2_asm_output_data_uleb128 (0, NULL);
12490 #ifdef VMS_DEBUGGING_INFO
12491 dw2_asm_output_data_uleb128 (0, NULL);
12492 dw2_asm_output_data_uleb128 (0, NULL);
12493 #endif
12496 /* Now write all the file names. */
12497 for (i = 0; i < numfiles; i++)
12499 int file_idx = backmap[i];
12500 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12502 #ifdef VMS_DEBUGGING_INFO
12503 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12505 /* Setting these fields can lead to debugger miscomparisons,
12506 but VMS Debug requires them to be set correctly. */
12508 int ver;
12509 long long cdt;
12510 long siz;
12511 int maxfilelen = (strlen (files[file_idx].path)
12512 + dirs[dir_idx].length
12513 + MAX_VMS_VERSION_LEN + 1);
12514 char *filebuf = XALLOCAVEC (char, maxfilelen);
12516 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12517 snprintf (filebuf, maxfilelen, "%s;%d",
12518 files[file_idx].path + dirs[dir_idx].length, ver);
12520 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12522 /* Include directory index. */
12523 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12524 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12525 dir_idx + idx_offset, NULL);
12526 else
12527 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12529 /* Modification time. */
12530 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12531 &cdt, 0, 0, 0) == 0)
12532 ? cdt : 0, NULL);
12534 /* File length in bytes. */
12535 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12536 0, &siz, 0, 0) == 0)
12537 ? siz : 0, NULL);
12538 #else
12539 output_line_string (str_form,
12540 files[file_idx].path + dirs[dir_idx].length,
12541 "File Entry", (unsigned) i + 1);
12543 /* Include directory index. */
12544 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12545 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12546 dir_idx + idx_offset, NULL);
12547 else
12548 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12550 if (dwarf_version >= 5)
12551 continue;
12553 /* Modification time. */
12554 dw2_asm_output_data_uleb128 (0, NULL);
12556 /* File length in bytes. */
12557 dw2_asm_output_data_uleb128 (0, NULL);
12558 #endif /* VMS_DEBUGGING_INFO */
12561 if (dwarf_version < 5)
12562 dw2_asm_output_data (1, 0, "End file name table");
12566 /* Output one line number table into the .debug_line section. */
12568 static void
12569 output_one_line_info_table (dw_line_info_table *table)
12571 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12572 unsigned int current_line = 1;
12573 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12574 dw_line_info_entry *ent, *prev_addr;
12575 size_t i;
12576 unsigned int view;
12578 view = 0;
12580 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12582 switch (ent->opcode)
12584 case LI_set_address:
12585 /* ??? Unfortunately, we have little choice here currently, and
12586 must always use the most general form. GCC does not know the
12587 address delta itself, so we can't use DW_LNS_advance_pc. Many
12588 ports do have length attributes which will give an upper bound
12589 on the address range. We could perhaps use length attributes
12590 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12591 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12593 view = 0;
12595 /* This can handle any delta. This takes
12596 4+DWARF2_ADDR_SIZE bytes. */
12597 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12598 debug_variable_location_views
12599 ? ", reset view to 0" : "");
12600 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12601 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12602 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12604 prev_addr = ent;
12605 break;
12607 case LI_adv_address:
12609 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12610 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12611 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12613 view++;
12615 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12616 dw2_asm_output_delta (2, line_label, prev_label,
12617 "from %s to %s", prev_label, line_label);
12619 prev_addr = ent;
12620 break;
12623 case LI_set_line:
12624 if (ent->val == current_line)
12626 /* We still need to start a new row, so output a copy insn. */
12627 dw2_asm_output_data (1, DW_LNS_copy,
12628 "copy line %u", current_line);
12630 else
12632 int line_offset = ent->val - current_line;
12633 int line_delta = line_offset - DWARF_LINE_BASE;
12635 current_line = ent->val;
12636 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12638 /* This can handle deltas from -10 to 234, using the current
12639 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12640 This takes 1 byte. */
12641 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12642 "line %u", current_line);
12644 else
12646 /* This can handle any delta. This takes at least 4 bytes,
12647 depending on the value being encoded. */
12648 dw2_asm_output_data (1, DW_LNS_advance_line,
12649 "advance to line %u", current_line);
12650 dw2_asm_output_data_sleb128 (line_offset, NULL);
12651 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12654 break;
12656 case LI_set_file:
12657 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12658 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12659 break;
12661 case LI_set_column:
12662 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12663 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12664 break;
12666 case LI_negate_stmt:
12667 current_is_stmt = !current_is_stmt;
12668 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12669 "is_stmt %d", current_is_stmt);
12670 break;
12672 case LI_set_prologue_end:
12673 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12674 "set prologue end");
12675 break;
12677 case LI_set_epilogue_begin:
12678 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12679 "set epilogue begin");
12680 break;
12682 case LI_set_discriminator:
12683 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12684 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12685 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12686 dw2_asm_output_data_uleb128 (ent->val, NULL);
12687 break;
12691 /* Emit debug info for the address of the end of the table. */
12692 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12693 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12694 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12695 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12697 dw2_asm_output_data (1, 0, "end sequence");
12698 dw2_asm_output_data_uleb128 (1, NULL);
12699 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12702 /* Output the source line number correspondence information. This
12703 information goes into the .debug_line section. */
12705 static void
12706 output_line_info (bool prologue_only)
12708 static unsigned int generation;
12709 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
12710 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
12711 bool saw_one = false;
12712 int opc;
12714 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
12715 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
12716 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
12717 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
12719 if (!XCOFF_DEBUGGING_INFO)
12721 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12722 dw2_asm_output_data (4, 0xffffffff,
12723 "Initial length escape value indicating 64-bit DWARF extension");
12724 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12725 "Length of Source Line Info");
12728 ASM_OUTPUT_LABEL (asm_out_file, l1);
12730 output_dwarf_version ();
12731 if (dwarf_version >= 5)
12733 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12734 dw2_asm_output_data (1, 0, "Segment Size");
12736 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12737 ASM_OUTPUT_LABEL (asm_out_file, p1);
12739 /* Define the architecture-dependent minimum instruction length (in bytes).
12740 In this implementation of DWARF, this field is used for information
12741 purposes only. Since GCC generates assembly language, we have no
12742 a priori knowledge of how many instruction bytes are generated for each
12743 source line, and therefore can use only the DW_LNE_set_address and
12744 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12745 this as '1', which is "correct enough" for all architectures,
12746 and don't let the target override. */
12747 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12749 if (dwarf_version >= 4)
12750 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12751 "Maximum Operations Per Instruction");
12752 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12753 "Default is_stmt_start flag");
12754 dw2_asm_output_data (1, DWARF_LINE_BASE,
12755 "Line Base Value (Special Opcodes)");
12756 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12757 "Line Range Value (Special Opcodes)");
12758 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12759 "Special Opcode Base");
12761 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12763 int n_op_args;
12764 switch (opc)
12766 case DW_LNS_advance_pc:
12767 case DW_LNS_advance_line:
12768 case DW_LNS_set_file:
12769 case DW_LNS_set_column:
12770 case DW_LNS_fixed_advance_pc:
12771 case DW_LNS_set_isa:
12772 n_op_args = 1;
12773 break;
12774 default:
12775 n_op_args = 0;
12776 break;
12779 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12780 opc, n_op_args);
12783 /* Write out the information about the files we use. */
12784 output_file_names ();
12785 ASM_OUTPUT_LABEL (asm_out_file, p2);
12786 if (prologue_only)
12788 /* Output the marker for the end of the line number info. */
12789 ASM_OUTPUT_LABEL (asm_out_file, l2);
12790 return;
12793 if (separate_line_info)
12795 dw_line_info_table *table;
12796 size_t i;
12798 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12799 if (table->in_use)
12801 output_one_line_info_table (table);
12802 saw_one = true;
12805 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12807 output_one_line_info_table (cold_text_section_line_info);
12808 saw_one = true;
12811 /* ??? Some Darwin linkers crash on a .debug_line section with no
12812 sequences. Further, merely a DW_LNE_end_sequence entry is not
12813 sufficient -- the address column must also be initialized.
12814 Make sure to output at least one set_address/end_sequence pair,
12815 choosing .text since that section is always present. */
12816 if (text_section_line_info->in_use || !saw_one)
12817 output_one_line_info_table (text_section_line_info);
12819 /* Output the marker for the end of the line number info. */
12820 ASM_OUTPUT_LABEL (asm_out_file, l2);
12823 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12825 static inline bool
12826 need_endianity_attribute_p (bool reverse)
12828 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12831 /* Given a pointer to a tree node for some base type, return a pointer to
12832 a DIE that describes the given type. REVERSE is true if the type is
12833 to be interpreted in the reverse storage order wrt the target order.
12835 This routine must only be called for GCC type nodes that correspond to
12836 Dwarf base (fundamental) types. */
12838 static dw_die_ref
12839 base_type_die (tree type, bool reverse)
12841 dw_die_ref base_type_result;
12842 enum dwarf_type encoding;
12843 bool fpt_used = false;
12844 struct fixed_point_type_info fpt_info;
12845 tree type_bias = NULL_TREE;
12847 /* If this is a subtype that should not be emitted as a subrange type,
12848 use the base type. See subrange_type_for_debug_p. */
12849 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12850 type = TREE_TYPE (type);
12852 switch (TREE_CODE (type))
12854 case INTEGER_TYPE:
12855 if ((dwarf_version >= 4 || !dwarf_strict)
12856 && TYPE_NAME (type)
12857 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12858 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
12859 && DECL_NAME (TYPE_NAME (type)))
12861 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12862 if (strcmp (name, "char16_t") == 0
12863 || strcmp (name, "char32_t") == 0)
12865 encoding = DW_ATE_UTF;
12866 break;
12869 if ((dwarf_version >= 3 || !dwarf_strict)
12870 && lang_hooks.types.get_fixed_point_type_info)
12872 memset (&fpt_info, 0, sizeof (fpt_info));
12873 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12875 fpt_used = true;
12876 encoding = ((TYPE_UNSIGNED (type))
12877 ? DW_ATE_unsigned_fixed
12878 : DW_ATE_signed_fixed);
12879 break;
12882 if (TYPE_STRING_FLAG (type))
12884 if (TYPE_UNSIGNED (type))
12885 encoding = DW_ATE_unsigned_char;
12886 else
12887 encoding = DW_ATE_signed_char;
12889 else if (TYPE_UNSIGNED (type))
12890 encoding = DW_ATE_unsigned;
12891 else
12892 encoding = DW_ATE_signed;
12894 if (!dwarf_strict
12895 && lang_hooks.types.get_type_bias)
12896 type_bias = lang_hooks.types.get_type_bias (type);
12897 break;
12899 case REAL_TYPE:
12900 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12902 if (dwarf_version >= 3 || !dwarf_strict)
12903 encoding = DW_ATE_decimal_float;
12904 else
12905 encoding = DW_ATE_lo_user;
12907 else
12908 encoding = DW_ATE_float;
12909 break;
12911 case FIXED_POINT_TYPE:
12912 if (!(dwarf_version >= 3 || !dwarf_strict))
12913 encoding = DW_ATE_lo_user;
12914 else if (TYPE_UNSIGNED (type))
12915 encoding = DW_ATE_unsigned_fixed;
12916 else
12917 encoding = DW_ATE_signed_fixed;
12918 break;
12920 /* Dwarf2 doesn't know anything about complex ints, so use
12921 a user defined type for it. */
12922 case COMPLEX_TYPE:
12923 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12924 encoding = DW_ATE_complex_float;
12925 else
12926 encoding = DW_ATE_lo_user;
12927 break;
12929 case BOOLEAN_TYPE:
12930 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12931 encoding = DW_ATE_boolean;
12932 break;
12934 default:
12935 /* No other TREE_CODEs are Dwarf fundamental types. */
12936 gcc_unreachable ();
12939 base_type_result = new_die_raw (DW_TAG_base_type);
12941 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12942 int_size_in_bytes (type));
12943 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12945 if (need_endianity_attribute_p (reverse))
12946 add_AT_unsigned (base_type_result, DW_AT_endianity,
12947 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12949 add_alignment_attribute (base_type_result, type);
12951 if (fpt_used)
12953 switch (fpt_info.scale_factor_kind)
12955 case fixed_point_scale_factor_binary:
12956 add_AT_int (base_type_result, DW_AT_binary_scale,
12957 fpt_info.scale_factor.binary);
12958 break;
12960 case fixed_point_scale_factor_decimal:
12961 add_AT_int (base_type_result, DW_AT_decimal_scale,
12962 fpt_info.scale_factor.decimal);
12963 break;
12965 case fixed_point_scale_factor_arbitrary:
12966 /* Arbitrary scale factors cannot be described in standard DWARF,
12967 yet. */
12968 if (!dwarf_strict)
12970 /* Describe the scale factor as a rational constant. */
12971 const dw_die_ref scale_factor
12972 = new_die (DW_TAG_constant, comp_unit_die (), type);
12974 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12975 fpt_info.scale_factor.arbitrary.numerator);
12976 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12977 fpt_info.scale_factor.arbitrary.denominator);
12979 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12981 break;
12983 default:
12984 gcc_unreachable ();
12988 if (type_bias)
12989 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12990 dw_scalar_form_constant
12991 | dw_scalar_form_exprloc
12992 | dw_scalar_form_reference,
12993 NULL);
12995 return base_type_result;
12998 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12999 named 'auto' in its type: return true for it, false otherwise. */
13001 static inline bool
13002 is_cxx_auto (tree type)
13004 if (is_cxx ())
13006 tree name = TYPE_IDENTIFIER (type);
13007 if (name == get_identifier ("auto")
13008 || name == get_identifier ("decltype(auto)"))
13009 return true;
13011 return false;
13014 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
13015 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13017 static inline int
13018 is_base_type (tree type)
13020 switch (TREE_CODE (type))
13022 case INTEGER_TYPE:
13023 case REAL_TYPE:
13024 case FIXED_POINT_TYPE:
13025 case COMPLEX_TYPE:
13026 case BOOLEAN_TYPE:
13027 return 1;
13029 case VOID_TYPE:
13030 case ARRAY_TYPE:
13031 case RECORD_TYPE:
13032 case UNION_TYPE:
13033 case QUAL_UNION_TYPE:
13034 case ENUMERAL_TYPE:
13035 case FUNCTION_TYPE:
13036 case METHOD_TYPE:
13037 case POINTER_TYPE:
13038 case REFERENCE_TYPE:
13039 case NULLPTR_TYPE:
13040 case OFFSET_TYPE:
13041 case LANG_TYPE:
13042 case VECTOR_TYPE:
13043 return 0;
13045 default:
13046 if (is_cxx_auto (type))
13047 return 0;
13048 gcc_unreachable ();
13051 return 0;
13054 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13055 node, return the size in bits for the type if it is a constant, or else
13056 return the alignment for the type if the type's size is not constant, or
13057 else return BITS_PER_WORD if the type actually turns out to be an
13058 ERROR_MARK node. */
13060 static inline unsigned HOST_WIDE_INT
13061 simple_type_size_in_bits (const_tree type)
13063 if (TREE_CODE (type) == ERROR_MARK)
13064 return BITS_PER_WORD;
13065 else if (TYPE_SIZE (type) == NULL_TREE)
13066 return 0;
13067 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13068 return tree_to_uhwi (TYPE_SIZE (type));
13069 else
13070 return TYPE_ALIGN (type);
13073 /* Similarly, but return an offset_int instead of UHWI. */
13075 static inline offset_int
13076 offset_int_type_size_in_bits (const_tree type)
13078 if (TREE_CODE (type) == ERROR_MARK)
13079 return BITS_PER_WORD;
13080 else if (TYPE_SIZE (type) == NULL_TREE)
13081 return 0;
13082 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13083 return wi::to_offset (TYPE_SIZE (type));
13084 else
13085 return TYPE_ALIGN (type);
13088 /* Given a pointer to a tree node for a subrange type, return a pointer
13089 to a DIE that describes the given type. */
13091 static dw_die_ref
13092 subrange_type_die (tree type, tree low, tree high, tree bias,
13093 dw_die_ref context_die)
13095 dw_die_ref subrange_die;
13096 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13098 if (context_die == NULL)
13099 context_die = comp_unit_die ();
13101 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13103 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13105 /* The size of the subrange type and its base type do not match,
13106 so we need to generate a size attribute for the subrange type. */
13107 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13110 add_alignment_attribute (subrange_die, type);
13112 if (low)
13113 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13114 if (high)
13115 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13116 if (bias && !dwarf_strict)
13117 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13118 dw_scalar_form_constant
13119 | dw_scalar_form_exprloc
13120 | dw_scalar_form_reference,
13121 NULL);
13123 return subrange_die;
13126 /* Returns the (const and/or volatile) cv_qualifiers associated with
13127 the decl node. This will normally be augmented with the
13128 cv_qualifiers of the underlying type in add_type_attribute. */
13130 static int
13131 decl_quals (const_tree decl)
13133 return ((TREE_READONLY (decl)
13134 /* The C++ front-end correctly marks reference-typed
13135 variables as readonly, but from a language (and debug
13136 info) standpoint they are not const-qualified. */
13137 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13138 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13139 | (TREE_THIS_VOLATILE (decl)
13140 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13143 /* Determine the TYPE whose qualifiers match the largest strict subset
13144 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13145 qualifiers outside QUAL_MASK. */
13147 static int
13148 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13150 tree t;
13151 int best_rank = 0, best_qual = 0, max_rank;
13153 type_quals &= qual_mask;
13154 max_rank = popcount_hwi (type_quals) - 1;
13156 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13157 t = TYPE_NEXT_VARIANT (t))
13159 int q = TYPE_QUALS (t) & qual_mask;
13161 if ((q & type_quals) == q && q != type_quals
13162 && check_base_type (t, type))
13164 int rank = popcount_hwi (q);
13166 if (rank > best_rank)
13168 best_rank = rank;
13169 best_qual = q;
13174 return best_qual;
13177 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13178 static const dwarf_qual_info_t dwarf_qual_info[] =
13180 { TYPE_QUAL_CONST, DW_TAG_const_type },
13181 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13182 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13183 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13185 static const unsigned int dwarf_qual_info_size
13186 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13188 /* If DIE is a qualified DIE of some base DIE with the same parent,
13189 return the base DIE, otherwise return NULL. Set MASK to the
13190 qualifiers added compared to the returned DIE. */
13192 static dw_die_ref
13193 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13195 unsigned int i;
13196 for (i = 0; i < dwarf_qual_info_size; i++)
13197 if (die->die_tag == dwarf_qual_info[i].t)
13198 break;
13199 if (i == dwarf_qual_info_size)
13200 return NULL;
13201 if (vec_safe_length (die->die_attr) != 1)
13202 return NULL;
13203 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13204 if (type == NULL || type->die_parent != die->die_parent)
13205 return NULL;
13206 *mask |= dwarf_qual_info[i].q;
13207 if (depth)
13209 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13210 if (ret)
13211 return ret;
13213 return type;
13216 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13217 entry that chains the modifiers specified by CV_QUALS in front of the
13218 given type. REVERSE is true if the type is to be interpreted in the
13219 reverse storage order wrt the target order. */
13221 static dw_die_ref
13222 modified_type_die (tree type, int cv_quals, bool reverse,
13223 dw_die_ref context_die)
13225 enum tree_code code = TREE_CODE (type);
13226 dw_die_ref mod_type_die;
13227 dw_die_ref sub_die = NULL;
13228 tree item_type = NULL;
13229 tree qualified_type;
13230 tree name, low, high;
13231 dw_die_ref mod_scope;
13232 /* Only these cv-qualifiers are currently handled. */
13233 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13234 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13235 ENCODE_QUAL_ADDR_SPACE(~0U));
13236 const bool reverse_base_type
13237 = need_endianity_attribute_p (reverse) && is_base_type (type);
13239 if (code == ERROR_MARK)
13240 return NULL;
13242 if (lang_hooks.types.get_debug_type)
13244 tree debug_type = lang_hooks.types.get_debug_type (type);
13246 if (debug_type != NULL_TREE && debug_type != type)
13247 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13250 cv_quals &= cv_qual_mask;
13252 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13253 tag modifier (and not an attribute) old consumers won't be able
13254 to handle it. */
13255 if (dwarf_version < 3)
13256 cv_quals &= ~TYPE_QUAL_RESTRICT;
13258 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13259 if (dwarf_version < 5)
13260 cv_quals &= ~TYPE_QUAL_ATOMIC;
13262 /* See if we already have the appropriately qualified variant of
13263 this type. */
13264 qualified_type = get_qualified_type (type, cv_quals);
13266 if (qualified_type == sizetype)
13268 /* Try not to expose the internal sizetype type's name. */
13269 if (TYPE_NAME (qualified_type)
13270 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13272 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13274 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13275 && (TYPE_PRECISION (t)
13276 == TYPE_PRECISION (qualified_type))
13277 && (TYPE_UNSIGNED (t)
13278 == TYPE_UNSIGNED (qualified_type)));
13279 qualified_type = t;
13281 else if (qualified_type == sizetype
13282 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13283 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13284 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13285 qualified_type = size_type_node;
13286 if (type == sizetype)
13287 type = qualified_type;
13290 /* If we do, then we can just use its DIE, if it exists. */
13291 if (qualified_type)
13293 mod_type_die = lookup_type_die (qualified_type);
13295 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13296 dealt with specially: the DIE with the attribute, if it exists, is
13297 placed immediately after the regular DIE for the same base type. */
13298 if (mod_type_die
13299 && (!reverse_base_type
13300 || ((mod_type_die = mod_type_die->die_sib) != NULL
13301 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13302 return mod_type_die;
13305 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13307 /* Handle C typedef types. */
13308 if (name
13309 && TREE_CODE (name) == TYPE_DECL
13310 && DECL_ORIGINAL_TYPE (name)
13311 && !DECL_ARTIFICIAL (name))
13313 tree dtype = TREE_TYPE (name);
13315 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13316 if (qualified_type == dtype && !reverse_base_type)
13318 tree origin = decl_ultimate_origin (name);
13320 /* Typedef variants that have an abstract origin don't get their own
13321 type DIE (see gen_typedef_die), so fall back on the ultimate
13322 abstract origin instead. */
13323 if (origin != NULL && origin != name)
13324 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13325 context_die);
13327 /* For a named type, use the typedef. */
13328 gen_type_die (qualified_type, context_die);
13329 return lookup_type_die (qualified_type);
13331 else
13333 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13334 dquals &= cv_qual_mask;
13335 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13336 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13337 /* cv-unqualified version of named type. Just use
13338 the unnamed type to which it refers. */
13339 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13340 reverse, context_die);
13341 /* Else cv-qualified version of named type; fall through. */
13345 mod_scope = scope_die_for (type, context_die);
13347 if (cv_quals)
13349 int sub_quals = 0, first_quals = 0;
13350 unsigned i;
13351 dw_die_ref first = NULL, last = NULL;
13353 /* Determine a lesser qualified type that most closely matches
13354 this one. Then generate DW_TAG_* entries for the remaining
13355 qualifiers. */
13356 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13357 cv_qual_mask);
13358 if (sub_quals && use_debug_types)
13360 bool needed = false;
13361 /* If emitting type units, make sure the order of qualifiers
13362 is canonical. Thus, start from unqualified type if
13363 an earlier qualifier is missing in sub_quals, but some later
13364 one is present there. */
13365 for (i = 0; i < dwarf_qual_info_size; i++)
13366 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13367 needed = true;
13368 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13370 sub_quals = 0;
13371 break;
13374 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13375 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13377 /* As not all intermediate qualified DIEs have corresponding
13378 tree types, ensure that qualified DIEs in the same scope
13379 as their DW_AT_type are emitted after their DW_AT_type,
13380 only with other qualified DIEs for the same type possibly
13381 in between them. Determine the range of such qualified
13382 DIEs now (first being the base type, last being corresponding
13383 last qualified DIE for it). */
13384 unsigned int count = 0;
13385 first = qualified_die_p (mod_type_die, &first_quals,
13386 dwarf_qual_info_size);
13387 if (first == NULL)
13388 first = mod_type_die;
13389 gcc_assert ((first_quals & ~sub_quals) == 0);
13390 for (count = 0, last = first;
13391 count < (1U << dwarf_qual_info_size);
13392 count++, last = last->die_sib)
13394 int quals = 0;
13395 if (last == mod_scope->die_child)
13396 break;
13397 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13398 != first)
13399 break;
13403 for (i = 0; i < dwarf_qual_info_size; i++)
13404 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13406 dw_die_ref d;
13407 if (first && first != last)
13409 for (d = first->die_sib; ; d = d->die_sib)
13411 int quals = 0;
13412 qualified_die_p (d, &quals, dwarf_qual_info_size);
13413 if (quals == (first_quals | dwarf_qual_info[i].q))
13414 break;
13415 if (d == last)
13417 d = NULL;
13418 break;
13421 if (d)
13423 mod_type_die = d;
13424 continue;
13427 if (first)
13429 d = new_die_raw (dwarf_qual_info[i].t);
13430 add_child_die_after (mod_scope, d, last);
13431 last = d;
13433 else
13434 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13435 if (mod_type_die)
13436 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13437 mod_type_die = d;
13438 first_quals |= dwarf_qual_info[i].q;
13441 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13443 dwarf_tag tag = DW_TAG_pointer_type;
13444 if (code == REFERENCE_TYPE)
13446 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13447 tag = DW_TAG_rvalue_reference_type;
13448 else
13449 tag = DW_TAG_reference_type;
13451 mod_type_die = new_die (tag, mod_scope, type);
13453 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13454 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13455 add_alignment_attribute (mod_type_die, type);
13456 item_type = TREE_TYPE (type);
13458 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13459 if (!ADDR_SPACE_GENERIC_P (as))
13461 int action = targetm.addr_space.debug (as);
13462 if (action >= 0)
13464 /* Positive values indicate an address_class. */
13465 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13467 else
13469 /* Negative values indicate an (inverted) segment base reg. */
13470 dw_loc_descr_ref d
13471 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13472 add_AT_loc (mod_type_die, DW_AT_segment, d);
13476 else if (code == INTEGER_TYPE
13477 && TREE_TYPE (type) != NULL_TREE
13478 && subrange_type_for_debug_p (type, &low, &high))
13480 tree bias = NULL_TREE;
13481 if (lang_hooks.types.get_type_bias)
13482 bias = lang_hooks.types.get_type_bias (type);
13483 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13484 item_type = TREE_TYPE (type);
13486 else if (is_base_type (type))
13488 mod_type_die = base_type_die (type, reverse);
13490 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13491 if (reverse_base_type)
13493 dw_die_ref after_die
13494 = modified_type_die (type, cv_quals, false, context_die);
13495 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13497 else
13498 add_child_die (comp_unit_die (), mod_type_die);
13500 add_pubtype (type, mod_type_die);
13502 else
13504 gen_type_die (type, context_die);
13506 /* We have to get the type_main_variant here (and pass that to the
13507 `lookup_type_die' routine) because the ..._TYPE node we have
13508 might simply be a *copy* of some original type node (where the
13509 copy was created to help us keep track of typedef names) and
13510 that copy might have a different TYPE_UID from the original
13511 ..._TYPE node. */
13512 if (TREE_CODE (type) == FUNCTION_TYPE
13513 || TREE_CODE (type) == METHOD_TYPE)
13515 /* For function/method types, can't just use type_main_variant here,
13516 because that can have different ref-qualifiers for C++,
13517 but try to canonicalize. */
13518 tree main = TYPE_MAIN_VARIANT (type);
13519 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13520 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13521 && check_base_type (t, main)
13522 && check_lang_type (t, type))
13523 return lookup_type_die (t);
13524 return lookup_type_die (type);
13526 else if (TREE_CODE (type) != VECTOR_TYPE
13527 && TREE_CODE (type) != ARRAY_TYPE)
13528 return lookup_type_die (type_main_variant (type));
13529 else
13530 /* Vectors have the debugging information in the type,
13531 not the main variant. */
13532 return lookup_type_die (type);
13535 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13536 don't output a DW_TAG_typedef, since there isn't one in the
13537 user's program; just attach a DW_AT_name to the type.
13538 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13539 if the base type already has the same name. */
13540 if (name
13541 && ((TREE_CODE (name) != TYPE_DECL
13542 && (qualified_type == TYPE_MAIN_VARIANT (type)
13543 || (cv_quals == TYPE_UNQUALIFIED)))
13544 || (TREE_CODE (name) == TYPE_DECL
13545 && TREE_TYPE (name) == qualified_type
13546 && DECL_NAME (name))))
13548 if (TREE_CODE (name) == TYPE_DECL)
13549 /* Could just call add_name_and_src_coords_attributes here,
13550 but since this is a builtin type it doesn't have any
13551 useful source coordinates anyway. */
13552 name = DECL_NAME (name);
13553 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13555 /* This probably indicates a bug. */
13556 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13558 name = TYPE_IDENTIFIER (type);
13559 add_name_attribute (mod_type_die,
13560 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13563 if (qualified_type && !reverse_base_type)
13564 equate_type_number_to_die (qualified_type, mod_type_die);
13566 if (item_type)
13567 /* We must do this after the equate_type_number_to_die call, in case
13568 this is a recursive type. This ensures that the modified_type_die
13569 recursion will terminate even if the type is recursive. Recursive
13570 types are possible in Ada. */
13571 sub_die = modified_type_die (item_type,
13572 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13573 reverse,
13574 context_die);
13576 if (sub_die != NULL)
13577 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13579 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13580 if (TYPE_ARTIFICIAL (type))
13581 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13583 return mod_type_die;
13586 /* Generate DIEs for the generic parameters of T.
13587 T must be either a generic type or a generic function.
13588 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13590 static void
13591 gen_generic_params_dies (tree t)
13593 tree parms, args;
13594 int parms_num, i;
13595 dw_die_ref die = NULL;
13596 int non_default;
13598 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13599 return;
13601 if (TYPE_P (t))
13602 die = lookup_type_die (t);
13603 else if (DECL_P (t))
13604 die = lookup_decl_die (t);
13606 gcc_assert (die);
13608 parms = lang_hooks.get_innermost_generic_parms (t);
13609 if (!parms)
13610 /* T has no generic parameter. It means T is neither a generic type
13611 or function. End of story. */
13612 return;
13614 parms_num = TREE_VEC_LENGTH (parms);
13615 args = lang_hooks.get_innermost_generic_args (t);
13616 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13617 non_default = int_cst_value (TREE_CHAIN (args));
13618 else
13619 non_default = TREE_VEC_LENGTH (args);
13620 for (i = 0; i < parms_num; i++)
13622 tree parm, arg, arg_pack_elems;
13623 dw_die_ref parm_die;
13625 parm = TREE_VEC_ELT (parms, i);
13626 arg = TREE_VEC_ELT (args, i);
13627 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13628 gcc_assert (parm && TREE_VALUE (parm) && arg);
13630 if (parm && TREE_VALUE (parm) && arg)
13632 /* If PARM represents a template parameter pack,
13633 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13634 by DW_TAG_template_*_parameter DIEs for the argument
13635 pack elements of ARG. Note that ARG would then be
13636 an argument pack. */
13637 if (arg_pack_elems)
13638 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13639 arg_pack_elems,
13640 die);
13641 else
13642 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13643 true /* emit name */, die);
13644 if (i >= non_default)
13645 add_AT_flag (parm_die, DW_AT_default_value, 1);
13650 /* Create and return a DIE for PARM which should be
13651 the representation of a generic type parameter.
13652 For instance, in the C++ front end, PARM would be a template parameter.
13653 ARG is the argument to PARM.
13654 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13655 name of the PARM.
13656 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13657 as a child node. */
13659 static dw_die_ref
13660 generic_parameter_die (tree parm, tree arg,
13661 bool emit_name_p,
13662 dw_die_ref parent_die)
13664 dw_die_ref tmpl_die = NULL;
13665 const char *name = NULL;
13667 /* C++20 accepts class literals as template parameters, and var
13668 decls with initializers represent them. The VAR_DECLs would be
13669 rejected, but we can take the DECL_INITIAL constructor and
13670 attempt to expand it. */
13671 if (arg && VAR_P (arg))
13672 arg = DECL_INITIAL (arg);
13674 if (!parm || !DECL_NAME (parm) || !arg)
13675 return NULL;
13677 /* We support non-type generic parameters and arguments,
13678 type generic parameters and arguments, as well as
13679 generic generic parameters (a.k.a. template template parameters in C++)
13680 and arguments. */
13681 if (TREE_CODE (parm) == PARM_DECL)
13682 /* PARM is a nontype generic parameter */
13683 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13684 else if (TREE_CODE (parm) == TYPE_DECL)
13685 /* PARM is a type generic parameter. */
13686 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13687 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13688 /* PARM is a generic generic parameter.
13689 Its DIE is a GNU extension. It shall have a
13690 DW_AT_name attribute to represent the name of the template template
13691 parameter, and a DW_AT_GNU_template_name attribute to represent the
13692 name of the template template argument. */
13693 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13694 parent_die, parm);
13695 else
13696 gcc_unreachable ();
13698 if (tmpl_die)
13700 tree tmpl_type;
13702 /* If PARM is a generic parameter pack, it means we are
13703 emitting debug info for a template argument pack element.
13704 In other terms, ARG is a template argument pack element.
13705 In that case, we don't emit any DW_AT_name attribute for
13706 the die. */
13707 if (emit_name_p)
13709 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13710 gcc_assert (name);
13711 add_AT_string (tmpl_die, DW_AT_name, name);
13714 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13716 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13717 TMPL_DIE should have a child DW_AT_type attribute that is set
13718 to the type of the argument to PARM, which is ARG.
13719 If PARM is a type generic parameter, TMPL_DIE should have a
13720 child DW_AT_type that is set to ARG. */
13721 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13722 add_type_attribute (tmpl_die, tmpl_type,
13723 (TREE_THIS_VOLATILE (tmpl_type)
13724 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
13725 false, parent_die);
13727 else
13729 /* So TMPL_DIE is a DIE representing a
13730 a generic generic template parameter, a.k.a template template
13731 parameter in C++ and arg is a template. */
13733 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13734 to the name of the argument. */
13735 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13736 if (name)
13737 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13740 if (TREE_CODE (parm) == PARM_DECL)
13741 /* So PARM is a non-type generic parameter.
13742 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13743 attribute of TMPL_DIE which value represents the value
13744 of ARG.
13745 We must be careful here:
13746 The value of ARG might reference some function decls.
13747 We might currently be emitting debug info for a generic
13748 type and types are emitted before function decls, we don't
13749 know if the function decls referenced by ARG will actually be
13750 emitted after cgraph computations.
13751 So must defer the generation of the DW_AT_const_value to
13752 after cgraph is ready. */
13753 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13756 return tmpl_die;
13759 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13760 PARM_PACK must be a template parameter pack. The returned DIE
13761 will be child DIE of PARENT_DIE. */
13763 static dw_die_ref
13764 template_parameter_pack_die (tree parm_pack,
13765 tree parm_pack_args,
13766 dw_die_ref parent_die)
13768 dw_die_ref die;
13769 int j;
13771 gcc_assert (parent_die && parm_pack);
13773 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13774 add_name_and_src_coords_attributes (die, parm_pack);
13775 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13776 generic_parameter_die (parm_pack,
13777 TREE_VEC_ELT (parm_pack_args, j),
13778 false /* Don't emit DW_AT_name */,
13779 die);
13780 return die;
13783 /* Return the DBX register number described by a given RTL node. */
13785 static unsigned int
13786 dbx_reg_number (const_rtx rtl)
13788 unsigned regno = REGNO (rtl);
13790 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13792 #ifdef LEAF_REG_REMAP
13793 if (crtl->uses_only_leaf_regs)
13795 int leaf_reg = LEAF_REG_REMAP (regno);
13796 if (leaf_reg != -1)
13797 regno = (unsigned) leaf_reg;
13799 #endif
13801 regno = DBX_REGISTER_NUMBER (regno);
13802 gcc_assert (regno != INVALID_REGNUM);
13803 return regno;
13806 /* Optionally add a DW_OP_piece term to a location description expression.
13807 DW_OP_piece is only added if the location description expression already
13808 doesn't end with DW_OP_piece. */
13810 static void
13811 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13813 dw_loc_descr_ref loc;
13815 if (*list_head != NULL)
13817 /* Find the end of the chain. */
13818 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13821 if (loc->dw_loc_opc != DW_OP_piece)
13822 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13826 /* Return a location descriptor that designates a machine register or
13827 zero if there is none. */
13829 static dw_loc_descr_ref
13830 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13832 rtx regs;
13834 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13835 return 0;
13837 /* We only use "frame base" when we're sure we're talking about the
13838 post-prologue local stack frame. We do this by *not* running
13839 register elimination until this point, and recognizing the special
13840 argument pointer and soft frame pointer rtx's.
13841 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13842 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13843 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13845 dw_loc_descr_ref result = NULL;
13847 if (dwarf_version >= 4 || !dwarf_strict)
13849 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13850 initialized);
13851 if (result)
13852 add_loc_descr (&result,
13853 new_loc_descr (DW_OP_stack_value, 0, 0));
13855 return result;
13858 regs = targetm.dwarf_register_span (rtl);
13860 if (REG_NREGS (rtl) > 1 || regs)
13861 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13862 else
13864 unsigned int dbx_regnum = dbx_reg_number (rtl);
13865 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13866 return 0;
13867 return one_reg_loc_descriptor (dbx_regnum, initialized);
13871 /* Return a location descriptor that designates a machine register for
13872 a given hard register number. */
13874 static dw_loc_descr_ref
13875 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13877 dw_loc_descr_ref reg_loc_descr;
13879 if (regno <= 31)
13880 reg_loc_descr
13881 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13882 else
13883 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13885 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13886 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13888 return reg_loc_descr;
13891 /* Given an RTL of a register, return a location descriptor that
13892 designates a value that spans more than one register. */
13894 static dw_loc_descr_ref
13895 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13896 enum var_init_status initialized)
13898 int size, i;
13899 dw_loc_descr_ref loc_result = NULL;
13901 /* Simple, contiguous registers. */
13902 if (regs == NULL_RTX)
13904 unsigned reg = REGNO (rtl);
13905 int nregs;
13907 #ifdef LEAF_REG_REMAP
13908 if (crtl->uses_only_leaf_regs)
13910 int leaf_reg = LEAF_REG_REMAP (reg);
13911 if (leaf_reg != -1)
13912 reg = (unsigned) leaf_reg;
13914 #endif
13916 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13917 nregs = REG_NREGS (rtl);
13919 /* At present we only track constant-sized pieces. */
13920 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
13921 return NULL;
13922 size /= nregs;
13924 loc_result = NULL;
13925 while (nregs--)
13927 dw_loc_descr_ref t;
13929 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13930 VAR_INIT_STATUS_INITIALIZED);
13931 add_loc_descr (&loc_result, t);
13932 add_loc_descr_op_piece (&loc_result, size);
13933 ++reg;
13935 return loc_result;
13938 /* Now onto stupid register sets in non contiguous locations. */
13940 gcc_assert (GET_CODE (regs) == PARALLEL);
13942 /* At present we only track constant-sized pieces. */
13943 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
13944 return NULL;
13945 loc_result = NULL;
13947 for (i = 0; i < XVECLEN (regs, 0); ++i)
13949 dw_loc_descr_ref t;
13951 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13952 VAR_INIT_STATUS_INITIALIZED);
13953 add_loc_descr (&loc_result, t);
13954 add_loc_descr_op_piece (&loc_result, size);
13957 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13958 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13959 return loc_result;
13962 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13964 /* Return a location descriptor that designates a constant i,
13965 as a compound operation from constant (i >> shift), constant shift
13966 and DW_OP_shl. */
13968 static dw_loc_descr_ref
13969 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13971 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13972 add_loc_descr (&ret, int_loc_descriptor (shift));
13973 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13974 return ret;
13977 /* Return a location descriptor that designates constant POLY_I. */
13979 static dw_loc_descr_ref
13980 int_loc_descriptor (poly_int64 poly_i)
13982 enum dwarf_location_atom op;
13984 HOST_WIDE_INT i;
13985 if (!poly_i.is_constant (&i))
13987 /* Create location descriptions for the non-constant part and
13988 add any constant offset at the end. */
13989 dw_loc_descr_ref ret = NULL;
13990 HOST_WIDE_INT constant = poly_i.coeffs[0];
13991 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
13993 HOST_WIDE_INT coeff = poly_i.coeffs[j];
13994 if (coeff != 0)
13996 dw_loc_descr_ref start = ret;
13997 unsigned int factor;
13998 int bias;
13999 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14000 (j, &factor, &bias);
14002 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14003 add COEFF * (REGNO / FACTOR) now and subtract
14004 COEFF * BIAS from the final constant part. */
14005 constant -= coeff * bias;
14006 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14007 if (coeff % factor == 0)
14008 coeff /= factor;
14009 else
14011 int amount = exact_log2 (factor);
14012 gcc_assert (amount >= 0);
14013 add_loc_descr (&ret, int_loc_descriptor (amount));
14014 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14016 if (coeff != 1)
14018 add_loc_descr (&ret, int_loc_descriptor (coeff));
14019 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14021 if (start)
14022 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14025 loc_descr_plus_const (&ret, constant);
14026 return ret;
14029 /* Pick the smallest representation of a constant, rather than just
14030 defaulting to the LEB encoding. */
14031 if (i >= 0)
14033 int clz = clz_hwi (i);
14034 int ctz = ctz_hwi (i);
14035 if (i <= 31)
14036 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14037 else if (i <= 0xff)
14038 op = DW_OP_const1u;
14039 else if (i <= 0xffff)
14040 op = DW_OP_const2u;
14041 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14042 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14043 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14044 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14045 while DW_OP_const4u is 5 bytes. */
14046 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14047 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14048 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14049 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14050 while DW_OP_const4u is 5 bytes. */
14051 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14053 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14054 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14055 <= 4)
14057 /* As i >= 2**31, the double cast above will yield a negative number.
14058 Since wrapping is defined in DWARF expressions we can output big
14059 positive integers as small negative ones, regardless of the size
14060 of host wide ints.
14062 Here, since the evaluator will handle 32-bit values and since i >=
14063 2**31, we know it's going to be interpreted as a negative literal:
14064 store it this way if we can do better than 5 bytes this way. */
14065 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14067 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14068 op = DW_OP_const4u;
14070 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14071 least 6 bytes: see if we can do better before falling back to it. */
14072 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14073 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14074 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14075 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14076 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14077 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14078 >= HOST_BITS_PER_WIDE_INT)
14079 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14080 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14081 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14082 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14083 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14084 && size_of_uleb128 (i) > 6)
14085 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14086 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14087 else
14088 op = DW_OP_constu;
14090 else
14092 if (i >= -0x80)
14093 op = DW_OP_const1s;
14094 else if (i >= -0x8000)
14095 op = DW_OP_const2s;
14096 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14098 if (size_of_int_loc_descriptor (i) < 5)
14100 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14101 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14102 return ret;
14104 op = DW_OP_const4s;
14106 else
14108 if (size_of_int_loc_descriptor (i)
14109 < (unsigned long) 1 + size_of_sleb128 (i))
14111 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14112 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14113 return ret;
14115 op = DW_OP_consts;
14119 return new_loc_descr (op, i, 0);
14122 /* Likewise, for unsigned constants. */
14124 static dw_loc_descr_ref
14125 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14127 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14128 const unsigned HOST_WIDE_INT max_uint
14129 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14131 /* If possible, use the clever signed constants handling. */
14132 if (i <= max_int)
14133 return int_loc_descriptor ((HOST_WIDE_INT) i);
14135 /* Here, we are left with positive numbers that cannot be represented as
14136 HOST_WIDE_INT, i.e.:
14137 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14139 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14140 whereas may be better to output a negative integer: thanks to integer
14141 wrapping, we know that:
14142 x = x - 2 ** DWARF2_ADDR_SIZE
14143 = x - 2 * (max (HOST_WIDE_INT) + 1)
14144 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14145 small negative integers. Let's try that in cases it will clearly improve
14146 the encoding: there is no gain turning DW_OP_const4u into
14147 DW_OP_const4s. */
14148 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14149 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14150 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14152 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14154 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14155 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14156 const HOST_WIDE_INT second_shift
14157 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14159 /* So we finally have:
14160 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14161 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14162 return int_loc_descriptor (second_shift);
14165 /* Last chance: fallback to a simple constant operation. */
14166 return new_loc_descr
14167 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14168 ? DW_OP_const4u
14169 : DW_OP_const8u,
14170 i, 0);
14173 /* Generate and return a location description that computes the unsigned
14174 comparison of the two stack top entries (a OP b where b is the top-most
14175 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14176 LE_EXPR, GT_EXPR or GE_EXPR. */
14178 static dw_loc_descr_ref
14179 uint_comparison_loc_list (enum tree_code kind)
14181 enum dwarf_location_atom op, flip_op;
14182 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14184 switch (kind)
14186 case LT_EXPR:
14187 op = DW_OP_lt;
14188 break;
14189 case LE_EXPR:
14190 op = DW_OP_le;
14191 break;
14192 case GT_EXPR:
14193 op = DW_OP_gt;
14194 break;
14195 case GE_EXPR:
14196 op = DW_OP_ge;
14197 break;
14198 default:
14199 gcc_unreachable ();
14202 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14203 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14205 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14206 possible to perform unsigned comparisons: we just have to distinguish
14207 three cases:
14209 1. when a and b have the same sign (as signed integers); then we should
14210 return: a OP(signed) b;
14212 2. when a is a negative signed integer while b is a positive one, then a
14213 is a greater unsigned integer than b; likewise when a and b's roles
14214 are flipped.
14216 So first, compare the sign of the two operands. */
14217 ret = new_loc_descr (DW_OP_over, 0, 0);
14218 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14219 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14220 /* If they have different signs (i.e. they have different sign bits), then
14221 the stack top value has now the sign bit set and thus it's smaller than
14222 zero. */
14223 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14224 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14225 add_loc_descr (&ret, bra_node);
14227 /* We are in case 1. At this point, we know both operands have the same
14228 sign, to it's safe to use the built-in signed comparison. */
14229 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14230 add_loc_descr (&ret, jmp_node);
14232 /* We are in case 2. Here, we know both operands do not have the same sign,
14233 so we have to flip the signed comparison. */
14234 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14235 tmp = new_loc_descr (flip_op, 0, 0);
14236 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14237 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14238 add_loc_descr (&ret, tmp);
14240 /* This dummy operation is necessary to make the two branches join. */
14241 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14242 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14243 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14244 add_loc_descr (&ret, tmp);
14246 return ret;
14249 /* Likewise, but takes the location description lists (might be destructive on
14250 them). Return NULL if either is NULL or if concatenation fails. */
14252 static dw_loc_list_ref
14253 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14254 enum tree_code kind)
14256 if (left == NULL || right == NULL)
14257 return NULL;
14259 add_loc_list (&left, right);
14260 if (left == NULL)
14261 return NULL;
14263 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14264 return left;
14267 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14268 without actually allocating it. */
14270 static unsigned long
14271 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14273 return size_of_int_loc_descriptor (i >> shift)
14274 + size_of_int_loc_descriptor (shift)
14275 + 1;
14278 /* Return size_of_locs (int_loc_descriptor (i)) without
14279 actually allocating it. */
14281 static unsigned long
14282 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14284 unsigned long s;
14286 if (i >= 0)
14288 int clz, ctz;
14289 if (i <= 31)
14290 return 1;
14291 else if (i <= 0xff)
14292 return 2;
14293 else if (i <= 0xffff)
14294 return 3;
14295 clz = clz_hwi (i);
14296 ctz = ctz_hwi (i);
14297 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14298 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14299 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14300 - clz - 5);
14301 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14302 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14303 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14304 - clz - 8);
14305 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14306 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14307 <= 4)
14308 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14309 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14310 return 5;
14311 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14312 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14313 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14314 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14315 - clz - 8);
14316 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14317 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14318 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14319 - clz - 16);
14320 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14321 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14322 && s > 6)
14323 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14324 - clz - 32);
14325 else
14326 return 1 + s;
14328 else
14330 if (i >= -0x80)
14331 return 2;
14332 else if (i >= -0x8000)
14333 return 3;
14334 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14336 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14338 s = size_of_int_loc_descriptor (-i) + 1;
14339 if (s < 5)
14340 return s;
14342 return 5;
14344 else
14346 unsigned long r = 1 + size_of_sleb128 (i);
14347 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14349 s = size_of_int_loc_descriptor (-i) + 1;
14350 if (s < r)
14351 return s;
14353 return r;
14358 /* Return loc description representing "address" of integer value.
14359 This can appear only as toplevel expression. */
14361 static dw_loc_descr_ref
14362 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14364 int litsize;
14365 dw_loc_descr_ref loc_result = NULL;
14367 if (!(dwarf_version >= 4 || !dwarf_strict))
14368 return NULL;
14370 litsize = size_of_int_loc_descriptor (i);
14371 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14372 is more compact. For DW_OP_stack_value we need:
14373 litsize + 1 (DW_OP_stack_value)
14374 and for DW_OP_implicit_value:
14375 1 (DW_OP_implicit_value) + 1 (length) + size. */
14376 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14378 loc_result = int_loc_descriptor (i);
14379 add_loc_descr (&loc_result,
14380 new_loc_descr (DW_OP_stack_value, 0, 0));
14381 return loc_result;
14384 loc_result = new_loc_descr (DW_OP_implicit_value,
14385 size, 0);
14386 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14387 loc_result->dw_loc_oprnd2.v.val_int = i;
14388 return loc_result;
14391 /* Return a location descriptor that designates a base+offset location. */
14393 static dw_loc_descr_ref
14394 based_loc_descr (rtx reg, poly_int64 offset,
14395 enum var_init_status initialized)
14397 unsigned int regno;
14398 dw_loc_descr_ref result;
14399 dw_fde_ref fde = cfun->fde;
14401 /* We only use "frame base" when we're sure we're talking about the
14402 post-prologue local stack frame. We do this by *not* running
14403 register elimination until this point, and recognizing the special
14404 argument pointer and soft frame pointer rtx's. */
14405 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14407 rtx elim = (ira_use_lra_p
14408 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14409 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14411 if (elim != reg)
14413 /* Allow hard frame pointer here even if frame pointer
14414 isn't used since hard frame pointer is encoded with
14415 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14416 not hard frame pointer directly. */
14417 elim = strip_offset_and_add (elim, &offset);
14418 gcc_assert (elim == hard_frame_pointer_rtx
14419 || elim == stack_pointer_rtx);
14421 /* If drap register is used to align stack, use frame
14422 pointer + offset to access stack variables. If stack
14423 is aligned without drap, use stack pointer + offset to
14424 access stack variables. */
14425 if (crtl->stack_realign_tried
14426 && reg == frame_pointer_rtx)
14428 int base_reg
14429 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14430 ? HARD_FRAME_POINTER_REGNUM
14431 : REGNO (elim));
14432 return new_reg_loc_descr (base_reg, offset);
14435 gcc_assert (frame_pointer_fb_offset_valid);
14436 offset += frame_pointer_fb_offset;
14437 HOST_WIDE_INT const_offset;
14438 if (offset.is_constant (&const_offset))
14439 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14440 else
14442 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14443 loc_descr_plus_const (&ret, offset);
14444 return ret;
14449 regno = REGNO (reg);
14450 #ifdef LEAF_REG_REMAP
14451 if (crtl->uses_only_leaf_regs)
14453 int leaf_reg = LEAF_REG_REMAP (regno);
14454 if (leaf_reg != -1)
14455 regno = (unsigned) leaf_reg;
14457 #endif
14458 regno = DWARF_FRAME_REGNUM (regno);
14460 HOST_WIDE_INT const_offset;
14461 if (!optimize && fde
14462 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14463 && offset.is_constant (&const_offset))
14465 /* Use cfa+offset to represent the location of arguments passed
14466 on the stack when drap is used to align stack.
14467 Only do this when not optimizing, for optimized code var-tracking
14468 is supposed to track where the arguments live and the register
14469 used as vdrap or drap in some spot might be used for something
14470 else in other part of the routine. */
14471 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14474 result = new_reg_loc_descr (regno, offset);
14476 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14477 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14479 return result;
14482 /* Return true if this RTL expression describes a base+offset calculation. */
14484 static inline int
14485 is_based_loc (const_rtx rtl)
14487 return (GET_CODE (rtl) == PLUS
14488 && ((REG_P (XEXP (rtl, 0))
14489 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14490 && CONST_INT_P (XEXP (rtl, 1)))));
14493 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14494 failed. */
14496 static dw_loc_descr_ref
14497 tls_mem_loc_descriptor (rtx mem)
14499 tree base;
14500 dw_loc_descr_ref loc_result;
14502 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14503 return NULL;
14505 base = get_base_address (MEM_EXPR (mem));
14506 if (base == NULL
14507 || !VAR_P (base)
14508 || !DECL_THREAD_LOCAL_P (base))
14509 return NULL;
14511 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14512 if (loc_result == NULL)
14513 return NULL;
14515 if (maybe_ne (MEM_OFFSET (mem), 0))
14516 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14518 return loc_result;
14521 /* Output debug info about reason why we failed to expand expression as dwarf
14522 expression. */
14524 static void
14525 expansion_failed (tree expr, rtx rtl, char const *reason)
14527 if (dump_file && (dump_flags & TDF_DETAILS))
14529 fprintf (dump_file, "Failed to expand as dwarf: ");
14530 if (expr)
14531 print_generic_expr (dump_file, expr, dump_flags);
14532 if (rtl)
14534 fprintf (dump_file, "\n");
14535 print_rtl (dump_file, rtl);
14537 fprintf (dump_file, "\nReason: %s\n", reason);
14541 /* Helper function for const_ok_for_output. */
14543 static bool
14544 const_ok_for_output_1 (rtx rtl)
14546 if (targetm.const_not_ok_for_debug_p (rtl))
14548 if (GET_CODE (rtl) != UNSPEC)
14550 expansion_failed (NULL_TREE, rtl,
14551 "Expression rejected for debug by the backend.\n");
14552 return false;
14555 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14556 the target hook doesn't explicitly allow it in debug info, assume
14557 we can't express it in the debug info. */
14558 /* Don't complain about TLS UNSPECs, those are just too hard to
14559 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14560 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14561 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14562 if (flag_checking
14563 && (XVECLEN (rtl, 0) == 0
14564 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14565 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14566 inform (current_function_decl
14567 ? DECL_SOURCE_LOCATION (current_function_decl)
14568 : UNKNOWN_LOCATION,
14569 #if NUM_UNSPEC_VALUES > 0
14570 "non-delegitimized UNSPEC %s (%d) found in variable location",
14571 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14572 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14573 #else
14574 "non-delegitimized UNSPEC %d found in variable location",
14575 #endif
14576 XINT (rtl, 1));
14577 expansion_failed (NULL_TREE, rtl,
14578 "UNSPEC hasn't been delegitimized.\n");
14579 return false;
14582 if (CONST_POLY_INT_P (rtl))
14583 return false;
14585 /* FIXME: Refer to PR60655. It is possible for simplification
14586 of rtl expressions in var tracking to produce such expressions.
14587 We should really identify / validate expressions
14588 enclosed in CONST that can be handled by assemblers on various
14589 targets and only handle legitimate cases here. */
14590 switch (GET_CODE (rtl))
14592 case SYMBOL_REF:
14593 break;
14594 case NOT:
14595 case NEG:
14596 return false;
14597 case PLUS:
14599 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
14600 operands. */
14601 subrtx_var_iterator::array_type array;
14602 bool first = false;
14603 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14604 if (SYMBOL_REF_P (*iter)
14605 || LABEL_P (*iter)
14606 || GET_CODE (*iter) == UNSPEC)
14608 first = true;
14609 break;
14611 if (!first)
14612 return true;
14613 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14614 if (SYMBOL_REF_P (*iter)
14615 || LABEL_P (*iter)
14616 || GET_CODE (*iter) == UNSPEC)
14617 return false;
14618 return true;
14620 case MINUS:
14622 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
14623 appear in the second operand of MINUS. */
14624 subrtx_var_iterator::array_type array;
14625 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14626 if (SYMBOL_REF_P (*iter)
14627 || LABEL_P (*iter)
14628 || GET_CODE (*iter) == UNSPEC)
14629 return false;
14630 return true;
14632 default:
14633 return true;
14636 if (CONSTANT_POOL_ADDRESS_P (rtl))
14638 bool marked;
14639 get_pool_constant_mark (rtl, &marked);
14640 /* If all references to this pool constant were optimized away,
14641 it was not output and thus we can't represent it. */
14642 if (!marked)
14644 expansion_failed (NULL_TREE, rtl,
14645 "Constant was removed from constant pool.\n");
14646 return false;
14650 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14651 return false;
14653 /* Avoid references to external symbols in debug info, on several targets
14654 the linker might even refuse to link when linking a shared library,
14655 and in many other cases the relocations for .debug_info/.debug_loc are
14656 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14657 to be defined within the same shared library or executable are fine. */
14658 if (SYMBOL_REF_EXTERNAL_P (rtl))
14660 tree decl = SYMBOL_REF_DECL (rtl);
14662 if (decl == NULL || !targetm.binds_local_p (decl))
14664 expansion_failed (NULL_TREE, rtl,
14665 "Symbol not defined in current TU.\n");
14666 return false;
14670 return true;
14673 /* Return true if constant RTL can be emitted in DW_OP_addr or
14674 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14675 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14677 static bool
14678 const_ok_for_output (rtx rtl)
14680 if (GET_CODE (rtl) == SYMBOL_REF)
14681 return const_ok_for_output_1 (rtl);
14683 if (GET_CODE (rtl) == CONST)
14685 subrtx_var_iterator::array_type array;
14686 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14687 if (!const_ok_for_output_1 (*iter))
14688 return false;
14689 return true;
14692 return true;
14695 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
14696 if possible, NULL otherwise. */
14698 static dw_die_ref
14699 base_type_for_mode (machine_mode mode, bool unsignedp)
14701 dw_die_ref type_die;
14702 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
14704 if (type == NULL)
14705 return NULL;
14706 switch (TREE_CODE (type))
14708 case INTEGER_TYPE:
14709 case REAL_TYPE:
14710 break;
14711 default:
14712 return NULL;
14714 type_die = lookup_type_die (type);
14715 if (!type_die)
14716 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
14717 comp_unit_die ());
14718 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
14719 return NULL;
14720 return type_die;
14723 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
14724 type matching MODE, or, if MODE is narrower than or as wide as
14725 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
14726 possible. */
14728 static dw_loc_descr_ref
14729 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
14731 machine_mode outer_mode = mode;
14732 dw_die_ref type_die;
14733 dw_loc_descr_ref cvt;
14735 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14737 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
14738 return op;
14740 type_die = base_type_for_mode (outer_mode, 1);
14741 if (type_die == NULL)
14742 return NULL;
14743 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14744 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14745 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14746 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14747 add_loc_descr (&op, cvt);
14748 return op;
14751 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
14753 static dw_loc_descr_ref
14754 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
14755 dw_loc_descr_ref op1)
14757 dw_loc_descr_ref ret = op0;
14758 add_loc_descr (&ret, op1);
14759 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14760 if (STORE_FLAG_VALUE != 1)
14762 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14763 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14765 return ret;
14768 /* Subroutine of scompare_loc_descriptor for the case in which we're
14769 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14770 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
14772 static dw_loc_descr_ref
14773 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14774 scalar_int_mode op_mode,
14775 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14777 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14778 dw_loc_descr_ref cvt;
14780 if (type_die == NULL)
14781 return NULL;
14782 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14783 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14784 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14785 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14786 add_loc_descr (&op0, cvt);
14787 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14788 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14789 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14790 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14791 add_loc_descr (&op1, cvt);
14792 return compare_loc_descriptor (op, op0, op1);
14795 /* Subroutine of scompare_loc_descriptor for the case in which we're
14796 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14797 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
14799 static dw_loc_descr_ref
14800 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
14801 scalar_int_mode op_mode,
14802 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14804 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14805 /* For eq/ne, if the operands are known to be zero-extended,
14806 there is no need to do the fancy shifting up. */
14807 if (op == DW_OP_eq || op == DW_OP_ne)
14809 dw_loc_descr_ref last0, last1;
14810 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14812 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14814 /* deref_size zero extends, and for constants we can check
14815 whether they are zero extended or not. */
14816 if (((last0->dw_loc_opc == DW_OP_deref_size
14817 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14818 || (CONST_INT_P (XEXP (rtl, 0))
14819 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14820 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14821 && ((last1->dw_loc_opc == DW_OP_deref_size
14822 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14823 || (CONST_INT_P (XEXP (rtl, 1))
14824 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14825 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14826 return compare_loc_descriptor (op, op0, op1);
14828 /* EQ/NE comparison against constant in narrower type than
14829 DWARF2_ADDR_SIZE can be performed either as
14830 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14831 DW_OP_{eq,ne}
14833 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14834 DW_OP_{eq,ne}. Pick whatever is shorter. */
14835 if (CONST_INT_P (XEXP (rtl, 1))
14836 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14837 && (size_of_int_loc_descriptor (shift) + 1
14838 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14839 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14840 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14841 & GET_MODE_MASK (op_mode))))
14843 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14844 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14845 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14846 & GET_MODE_MASK (op_mode));
14847 return compare_loc_descriptor (op, op0, op1);
14850 add_loc_descr (&op0, int_loc_descriptor (shift));
14851 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14852 if (CONST_INT_P (XEXP (rtl, 1)))
14853 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14854 else
14856 add_loc_descr (&op1, int_loc_descriptor (shift));
14857 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14859 return compare_loc_descriptor (op, op0, op1);
14862 /* Return location descriptor for unsigned comparison OP RTL. */
14864 static dw_loc_descr_ref
14865 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14866 machine_mode mem_mode)
14868 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14869 dw_loc_descr_ref op0, op1;
14871 if (op_mode == VOIDmode)
14872 op_mode = GET_MODE (XEXP (rtl, 1));
14873 if (op_mode == VOIDmode)
14874 return NULL;
14876 scalar_int_mode int_op_mode;
14877 if (dwarf_strict
14878 && dwarf_version < 5
14879 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14880 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14881 return NULL;
14883 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14884 VAR_INIT_STATUS_INITIALIZED);
14885 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14886 VAR_INIT_STATUS_INITIALIZED);
14888 if (op0 == NULL || op1 == NULL)
14889 return NULL;
14891 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14893 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14894 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14896 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14897 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14899 return compare_loc_descriptor (op, op0, op1);
14902 /* Return location descriptor for unsigned comparison OP RTL. */
14904 static dw_loc_descr_ref
14905 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14906 machine_mode mem_mode)
14908 dw_loc_descr_ref op0, op1;
14910 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14911 if (test_op_mode == VOIDmode)
14912 test_op_mode = GET_MODE (XEXP (rtl, 1));
14914 scalar_int_mode op_mode;
14915 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14916 return NULL;
14918 if (dwarf_strict
14919 && dwarf_version < 5
14920 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14921 return NULL;
14923 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14924 VAR_INIT_STATUS_INITIALIZED);
14925 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14926 VAR_INIT_STATUS_INITIALIZED);
14928 if (op0 == NULL || op1 == NULL)
14929 return NULL;
14931 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14933 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14934 dw_loc_descr_ref last0, last1;
14935 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14937 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14939 if (CONST_INT_P (XEXP (rtl, 0)))
14940 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14941 /* deref_size zero extends, so no need to mask it again. */
14942 else if (last0->dw_loc_opc != DW_OP_deref_size
14943 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14945 add_loc_descr (&op0, int_loc_descriptor (mask));
14946 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14948 if (CONST_INT_P (XEXP (rtl, 1)))
14949 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14950 /* deref_size zero extends, so no need to mask it again. */
14951 else if (last1->dw_loc_opc != DW_OP_deref_size
14952 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14954 add_loc_descr (&op1, int_loc_descriptor (mask));
14955 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14958 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14960 HOST_WIDE_INT bias = 1;
14961 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14962 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14963 if (CONST_INT_P (XEXP (rtl, 1)))
14964 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14965 + INTVAL (XEXP (rtl, 1)));
14966 else
14967 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14968 bias, 0));
14970 return compare_loc_descriptor (op, op0, op1);
14973 /* Return location descriptor for {U,S}{MIN,MAX}. */
14975 static dw_loc_descr_ref
14976 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14977 machine_mode mem_mode)
14979 enum dwarf_location_atom op;
14980 dw_loc_descr_ref op0, op1, ret;
14981 dw_loc_descr_ref bra_node, drop_node;
14983 scalar_int_mode int_mode;
14984 if (dwarf_strict
14985 && dwarf_version < 5
14986 && (!is_a <scalar_int_mode> (mode, &int_mode)
14987 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14988 return NULL;
14990 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14991 VAR_INIT_STATUS_INITIALIZED);
14992 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14993 VAR_INIT_STATUS_INITIALIZED);
14995 if (op0 == NULL || op1 == NULL)
14996 return NULL;
14998 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14999 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15000 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15001 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15003 /* Checked by the caller. */
15004 int_mode = as_a <scalar_int_mode> (mode);
15005 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15007 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15008 add_loc_descr (&op0, int_loc_descriptor (mask));
15009 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15010 add_loc_descr (&op1, int_loc_descriptor (mask));
15011 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15013 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15015 HOST_WIDE_INT bias = 1;
15016 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15017 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15018 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15021 else if (is_a <scalar_int_mode> (mode, &int_mode)
15022 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15024 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15025 add_loc_descr (&op0, int_loc_descriptor (shift));
15026 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15027 add_loc_descr (&op1, int_loc_descriptor (shift));
15028 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15030 else if (is_a <scalar_int_mode> (mode, &int_mode)
15031 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15033 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15034 dw_loc_descr_ref cvt;
15035 if (type_die == NULL)
15036 return NULL;
15037 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15038 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15039 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15040 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15041 add_loc_descr (&op0, cvt);
15042 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15043 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15044 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15045 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15046 add_loc_descr (&op1, cvt);
15049 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15050 op = DW_OP_lt;
15051 else
15052 op = DW_OP_gt;
15053 ret = op0;
15054 add_loc_descr (&ret, op1);
15055 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15056 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15057 add_loc_descr (&ret, bra_node);
15058 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15059 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15060 add_loc_descr (&ret, drop_node);
15061 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15062 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15063 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15064 && is_a <scalar_int_mode> (mode, &int_mode)
15065 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15066 ret = convert_descriptor_to_mode (int_mode, ret);
15067 return ret;
15070 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15071 but after converting arguments to type_die, afterwards
15072 convert back to unsigned. */
15074 static dw_loc_descr_ref
15075 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15076 scalar_int_mode mode, machine_mode mem_mode)
15078 dw_loc_descr_ref cvt, op0, op1;
15080 if (type_die == NULL)
15081 return NULL;
15082 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15083 VAR_INIT_STATUS_INITIALIZED);
15084 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15085 VAR_INIT_STATUS_INITIALIZED);
15086 if (op0 == NULL || op1 == NULL)
15087 return NULL;
15088 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15089 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15090 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15091 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15092 add_loc_descr (&op0, cvt);
15093 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15094 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15095 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15096 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15097 add_loc_descr (&op1, cvt);
15098 add_loc_descr (&op0, op1);
15099 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15100 return convert_descriptor_to_mode (mode, op0);
15103 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15104 const0 is DW_OP_lit0 or corresponding typed constant,
15105 const1 is DW_OP_lit1 or corresponding typed constant
15106 and constMSB is constant with just the MSB bit set
15107 for the mode):
15108 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15109 L1: const0 DW_OP_swap
15110 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15111 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15112 L3: DW_OP_drop
15113 L4: DW_OP_nop
15115 CTZ is similar:
15116 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15117 L1: const0 DW_OP_swap
15118 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15119 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15120 L3: DW_OP_drop
15121 L4: DW_OP_nop
15123 FFS is similar:
15124 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15125 L1: const1 DW_OP_swap
15126 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15127 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15128 L3: DW_OP_drop
15129 L4: DW_OP_nop */
15131 static dw_loc_descr_ref
15132 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15133 machine_mode mem_mode)
15135 dw_loc_descr_ref op0, ret, tmp;
15136 HOST_WIDE_INT valv;
15137 dw_loc_descr_ref l1jump, l1label;
15138 dw_loc_descr_ref l2jump, l2label;
15139 dw_loc_descr_ref l3jump, l3label;
15140 dw_loc_descr_ref l4jump, l4label;
15141 rtx msb;
15143 if (GET_MODE (XEXP (rtl, 0)) != mode)
15144 return NULL;
15146 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15147 VAR_INIT_STATUS_INITIALIZED);
15148 if (op0 == NULL)
15149 return NULL;
15150 ret = op0;
15151 if (GET_CODE (rtl) == CLZ)
15153 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15154 valv = GET_MODE_BITSIZE (mode);
15156 else if (GET_CODE (rtl) == FFS)
15157 valv = 0;
15158 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15159 valv = GET_MODE_BITSIZE (mode);
15160 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15161 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15162 add_loc_descr (&ret, l1jump);
15163 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15164 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15165 VAR_INIT_STATUS_INITIALIZED);
15166 if (tmp == NULL)
15167 return NULL;
15168 add_loc_descr (&ret, tmp);
15169 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15170 add_loc_descr (&ret, l4jump);
15171 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15172 ? const1_rtx : const0_rtx,
15173 mode, mem_mode,
15174 VAR_INIT_STATUS_INITIALIZED);
15175 if (l1label == NULL)
15176 return NULL;
15177 add_loc_descr (&ret, l1label);
15178 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15179 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15180 add_loc_descr (&ret, l2label);
15181 if (GET_CODE (rtl) != CLZ)
15182 msb = const1_rtx;
15183 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15184 msb = GEN_INT (HOST_WIDE_INT_1U
15185 << (GET_MODE_BITSIZE (mode) - 1));
15186 else
15187 msb = immed_wide_int_const
15188 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15189 GET_MODE_PRECISION (mode)), mode);
15190 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15191 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15192 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15193 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15194 else
15195 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15196 VAR_INIT_STATUS_INITIALIZED);
15197 if (tmp == NULL)
15198 return NULL;
15199 add_loc_descr (&ret, tmp);
15200 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15201 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15202 add_loc_descr (&ret, l3jump);
15203 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15204 VAR_INIT_STATUS_INITIALIZED);
15205 if (tmp == NULL)
15206 return NULL;
15207 add_loc_descr (&ret, tmp);
15208 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15209 ? DW_OP_shl : DW_OP_shr, 0, 0));
15210 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15211 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15212 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15213 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15214 add_loc_descr (&ret, l2jump);
15215 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15216 add_loc_descr (&ret, l3label);
15217 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15218 add_loc_descr (&ret, l4label);
15219 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15220 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15221 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15222 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15223 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15224 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15225 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15226 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15227 return ret;
15230 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15231 const1 is DW_OP_lit1 or corresponding typed constant):
15232 const0 DW_OP_swap
15233 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15234 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15235 L2: DW_OP_drop
15237 PARITY is similar:
15238 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15239 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15240 L2: DW_OP_drop */
15242 static dw_loc_descr_ref
15243 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15244 machine_mode mem_mode)
15246 dw_loc_descr_ref op0, ret, tmp;
15247 dw_loc_descr_ref l1jump, l1label;
15248 dw_loc_descr_ref l2jump, l2label;
15250 if (GET_MODE (XEXP (rtl, 0)) != mode)
15251 return NULL;
15253 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15254 VAR_INIT_STATUS_INITIALIZED);
15255 if (op0 == NULL)
15256 return NULL;
15257 ret = op0;
15258 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15259 VAR_INIT_STATUS_INITIALIZED);
15260 if (tmp == NULL)
15261 return NULL;
15262 add_loc_descr (&ret, tmp);
15263 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15264 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15265 add_loc_descr (&ret, l1label);
15266 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15267 add_loc_descr (&ret, l2jump);
15268 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15269 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15270 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15271 VAR_INIT_STATUS_INITIALIZED);
15272 if (tmp == NULL)
15273 return NULL;
15274 add_loc_descr (&ret, tmp);
15275 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15276 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15277 ? DW_OP_plus : DW_OP_xor, 0, 0));
15278 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15279 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15280 VAR_INIT_STATUS_INITIALIZED);
15281 add_loc_descr (&ret, tmp);
15282 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15283 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15284 add_loc_descr (&ret, l1jump);
15285 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15286 add_loc_descr (&ret, l2label);
15287 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15288 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15289 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15290 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15291 return ret;
15294 /* BSWAP (constS is initial shift count, either 56 or 24):
15295 constS const0
15296 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15297 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15298 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15299 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15300 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15302 static dw_loc_descr_ref
15303 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15304 machine_mode mem_mode)
15306 dw_loc_descr_ref op0, ret, tmp;
15307 dw_loc_descr_ref l1jump, l1label;
15308 dw_loc_descr_ref l2jump, l2label;
15310 if (BITS_PER_UNIT != 8
15311 || (GET_MODE_BITSIZE (mode) != 32
15312 && GET_MODE_BITSIZE (mode) != 64))
15313 return NULL;
15315 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15316 VAR_INIT_STATUS_INITIALIZED);
15317 if (op0 == NULL)
15318 return NULL;
15320 ret = op0;
15321 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15322 mode, mem_mode,
15323 VAR_INIT_STATUS_INITIALIZED);
15324 if (tmp == NULL)
15325 return NULL;
15326 add_loc_descr (&ret, tmp);
15327 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15328 VAR_INIT_STATUS_INITIALIZED);
15329 if (tmp == NULL)
15330 return NULL;
15331 add_loc_descr (&ret, tmp);
15332 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15333 add_loc_descr (&ret, l1label);
15334 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15335 mode, mem_mode,
15336 VAR_INIT_STATUS_INITIALIZED);
15337 add_loc_descr (&ret, tmp);
15338 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15339 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15340 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15341 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15342 VAR_INIT_STATUS_INITIALIZED);
15343 if (tmp == NULL)
15344 return NULL;
15345 add_loc_descr (&ret, tmp);
15346 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15347 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15348 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15349 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15350 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15351 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15352 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15353 VAR_INIT_STATUS_INITIALIZED);
15354 add_loc_descr (&ret, tmp);
15355 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15356 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15357 add_loc_descr (&ret, l2jump);
15358 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15359 VAR_INIT_STATUS_INITIALIZED);
15360 add_loc_descr (&ret, tmp);
15361 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15362 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15363 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15364 add_loc_descr (&ret, l1jump);
15365 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15366 add_loc_descr (&ret, l2label);
15367 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15368 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15369 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15370 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15371 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15372 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15373 return ret;
15376 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15377 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15378 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15379 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15381 ROTATERT is similar:
15382 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15383 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15384 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15386 static dw_loc_descr_ref
15387 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15388 machine_mode mem_mode)
15390 rtx rtlop1 = XEXP (rtl, 1);
15391 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15392 int i;
15394 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15395 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15396 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15397 VAR_INIT_STATUS_INITIALIZED);
15398 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15399 VAR_INIT_STATUS_INITIALIZED);
15400 if (op0 == NULL || op1 == NULL)
15401 return NULL;
15402 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15403 for (i = 0; i < 2; i++)
15405 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15406 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15407 mode, mem_mode,
15408 VAR_INIT_STATUS_INITIALIZED);
15409 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15410 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15411 ? DW_OP_const4u
15412 : HOST_BITS_PER_WIDE_INT == 64
15413 ? DW_OP_const8u : DW_OP_constu,
15414 GET_MODE_MASK (mode), 0);
15415 else
15416 mask[i] = NULL;
15417 if (mask[i] == NULL)
15418 return NULL;
15419 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15421 ret = op0;
15422 add_loc_descr (&ret, op1);
15423 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15424 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15425 if (GET_CODE (rtl) == ROTATERT)
15427 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15428 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15429 GET_MODE_BITSIZE (mode), 0));
15431 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15432 if (mask[0] != NULL)
15433 add_loc_descr (&ret, mask[0]);
15434 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15435 if (mask[1] != NULL)
15437 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15438 add_loc_descr (&ret, mask[1]);
15439 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15441 if (GET_CODE (rtl) == ROTATE)
15443 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15444 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15445 GET_MODE_BITSIZE (mode), 0));
15447 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15448 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15449 return ret;
15452 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15453 for DEBUG_PARAMETER_REF RTL. */
15455 static dw_loc_descr_ref
15456 parameter_ref_descriptor (rtx rtl)
15458 dw_loc_descr_ref ret;
15459 dw_die_ref ref;
15461 if (dwarf_strict)
15462 return NULL;
15463 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15464 /* With LTO during LTRANS we get the late DIE that refers to the early
15465 DIE, thus we add another indirection here. This seems to confuse
15466 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15467 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15468 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15469 if (ref)
15471 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15472 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15473 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15475 else
15477 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15478 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15480 return ret;
15483 /* The following routine converts the RTL for a variable or parameter
15484 (resident in memory) into an equivalent Dwarf representation of a
15485 mechanism for getting the address of that same variable onto the top of a
15486 hypothetical "address evaluation" stack.
15488 When creating memory location descriptors, we are effectively transforming
15489 the RTL for a memory-resident object into its Dwarf postfix expression
15490 equivalent. This routine recursively descends an RTL tree, turning
15491 it into Dwarf postfix code as it goes.
15493 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15495 MEM_MODE is the mode of the memory reference, needed to handle some
15496 autoincrement addressing modes.
15498 Return 0 if we can't represent the location. */
15500 dw_loc_descr_ref
15501 mem_loc_descriptor (rtx rtl, machine_mode mode,
15502 machine_mode mem_mode,
15503 enum var_init_status initialized)
15505 dw_loc_descr_ref mem_loc_result = NULL;
15506 enum dwarf_location_atom op;
15507 dw_loc_descr_ref op0, op1;
15508 rtx inner = NULL_RTX;
15509 poly_int64 offset;
15511 if (mode == VOIDmode)
15512 mode = GET_MODE (rtl);
15514 /* Note that for a dynamically sized array, the location we will generate a
15515 description of here will be the lowest numbered location which is
15516 actually within the array. That's *not* necessarily the same as the
15517 zeroth element of the array. */
15519 rtl = targetm.delegitimize_address (rtl);
15521 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15522 return NULL;
15524 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
15525 switch (GET_CODE (rtl))
15527 case POST_INC:
15528 case POST_DEC:
15529 case POST_MODIFY:
15530 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15532 case SUBREG:
15533 /* The case of a subreg may arise when we have a local (register)
15534 variable or a formal (register) parameter which doesn't quite fill
15535 up an entire register. For now, just assume that it is
15536 legitimate to make the Dwarf info refer to the whole register which
15537 contains the given subreg. */
15538 if (!subreg_lowpart_p (rtl))
15539 break;
15540 inner = SUBREG_REG (rtl);
15541 /* FALLTHRU */
15542 case TRUNCATE:
15543 if (inner == NULL_RTX)
15544 inner = XEXP (rtl, 0);
15545 if (is_a <scalar_int_mode> (mode, &int_mode)
15546 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15547 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15548 #ifdef POINTERS_EXTEND_UNSIGNED
15549 || (int_mode == Pmode && mem_mode != VOIDmode)
15550 #endif
15552 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15554 mem_loc_result = mem_loc_descriptor (inner,
15555 inner_mode,
15556 mem_mode, initialized);
15557 break;
15559 if (dwarf_strict && dwarf_version < 5)
15560 break;
15561 if (is_a <scalar_int_mode> (mode, &int_mode)
15562 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15563 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15564 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15566 dw_die_ref type_die;
15567 dw_loc_descr_ref cvt;
15569 mem_loc_result = mem_loc_descriptor (inner,
15570 GET_MODE (inner),
15571 mem_mode, initialized);
15572 if (mem_loc_result == NULL)
15573 break;
15574 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15575 if (type_die == NULL)
15577 mem_loc_result = NULL;
15578 break;
15580 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15581 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15582 else
15583 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15584 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15585 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15586 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15587 add_loc_descr (&mem_loc_result, cvt);
15588 if (is_a <scalar_int_mode> (mode, &int_mode)
15589 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15591 /* Convert it to untyped afterwards. */
15592 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15593 add_loc_descr (&mem_loc_result, cvt);
15596 break;
15598 case REG:
15599 if (!is_a <scalar_int_mode> (mode, &int_mode)
15600 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15601 && rtl != arg_pointer_rtx
15602 && rtl != frame_pointer_rtx
15603 #ifdef POINTERS_EXTEND_UNSIGNED
15604 && (int_mode != Pmode || mem_mode == VOIDmode)
15605 #endif
15608 dw_die_ref type_die;
15609 unsigned int dbx_regnum;
15611 if (dwarf_strict && dwarf_version < 5)
15612 break;
15613 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
15614 break;
15615 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15616 if (type_die == NULL)
15617 break;
15619 dbx_regnum = dbx_reg_number (rtl);
15620 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15621 break;
15622 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15623 dbx_regnum, 0);
15624 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15625 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15626 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15627 break;
15629 /* Whenever a register number forms a part of the description of the
15630 method for calculating the (dynamic) address of a memory resident
15631 object, DWARF rules require the register number be referred to as
15632 a "base register". This distinction is not based in any way upon
15633 what category of register the hardware believes the given register
15634 belongs to. This is strictly DWARF terminology we're dealing with
15635 here. Note that in cases where the location of a memory-resident
15636 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15637 OP_CONST (0)) the actual DWARF location descriptor that we generate
15638 may just be OP_BASEREG (basereg). This may look deceptively like
15639 the object in question was allocated to a register (rather than in
15640 memory) so DWARF consumers need to be aware of the subtle
15641 distinction between OP_REG and OP_BASEREG. */
15642 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15643 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15644 else if (stack_realign_drap
15645 && crtl->drap_reg
15646 && crtl->args.internal_arg_pointer == rtl
15647 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15649 /* If RTL is internal_arg_pointer, which has been optimized
15650 out, use DRAP instead. */
15651 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15652 VAR_INIT_STATUS_INITIALIZED);
15654 break;
15656 case SIGN_EXTEND:
15657 case ZERO_EXTEND:
15658 if (!is_a <scalar_int_mode> (mode, &int_mode)
15659 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15660 break;
15661 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15662 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15663 if (op0 == 0)
15664 break;
15665 else if (GET_CODE (rtl) == ZERO_EXTEND
15666 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15667 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15668 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15669 to expand zero extend as two shifts instead of
15670 masking. */
15671 && GET_MODE_SIZE (inner_mode) <= 4)
15673 mem_loc_result = op0;
15674 add_loc_descr (&mem_loc_result,
15675 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15676 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15678 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15680 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15681 shift *= BITS_PER_UNIT;
15682 if (GET_CODE (rtl) == SIGN_EXTEND)
15683 op = DW_OP_shra;
15684 else
15685 op = DW_OP_shr;
15686 mem_loc_result = op0;
15687 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15688 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15689 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15690 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15692 else if (!dwarf_strict || dwarf_version >= 5)
15694 dw_die_ref type_die1, type_die2;
15695 dw_loc_descr_ref cvt;
15697 type_die1 = base_type_for_mode (inner_mode,
15698 GET_CODE (rtl) == ZERO_EXTEND);
15699 if (type_die1 == NULL)
15700 break;
15701 type_die2 = base_type_for_mode (int_mode, 1);
15702 if (type_die2 == NULL)
15703 break;
15704 mem_loc_result = op0;
15705 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15706 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15707 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
15708 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15709 add_loc_descr (&mem_loc_result, cvt);
15710 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15711 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15712 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
15713 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15714 add_loc_descr (&mem_loc_result, cvt);
15716 break;
15718 case MEM:
15720 rtx new_rtl = avoid_constant_pool_reference (rtl);
15721 if (new_rtl != rtl)
15723 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
15724 initialized);
15725 if (mem_loc_result != NULL)
15726 return mem_loc_result;
15729 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
15730 get_address_mode (rtl), mode,
15731 VAR_INIT_STATUS_INITIALIZED);
15732 if (mem_loc_result == NULL)
15733 mem_loc_result = tls_mem_loc_descriptor (rtl);
15734 if (mem_loc_result != NULL)
15736 if (!is_a <scalar_int_mode> (mode, &int_mode)
15737 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15739 dw_die_ref type_die;
15740 dw_loc_descr_ref deref;
15741 HOST_WIDE_INT size;
15743 if (dwarf_strict && dwarf_version < 5)
15744 return NULL;
15745 if (!GET_MODE_SIZE (mode).is_constant (&size))
15746 return NULL;
15747 type_die
15748 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15749 if (type_die == NULL)
15750 return NULL;
15751 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
15752 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15753 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15754 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
15755 add_loc_descr (&mem_loc_result, deref);
15757 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15758 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15759 else
15760 add_loc_descr (&mem_loc_result,
15761 new_loc_descr (DW_OP_deref_size,
15762 GET_MODE_SIZE (int_mode), 0));
15764 break;
15766 case LO_SUM:
15767 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15769 case LABEL_REF:
15770 /* Some ports can transform a symbol ref into a label ref, because
15771 the symbol ref is too far away and has to be dumped into a constant
15772 pool. */
15773 case CONST:
15774 case SYMBOL_REF:
15775 case UNSPEC:
15776 if (!is_a <scalar_int_mode> (mode, &int_mode)
15777 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15778 #ifdef POINTERS_EXTEND_UNSIGNED
15779 && (int_mode != Pmode || mem_mode == VOIDmode)
15780 #endif
15782 break;
15784 if (GET_CODE (rtl) == UNSPEC)
15786 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15787 can't express it in the debug info. This can happen e.g. with some
15788 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
15789 approves. */
15790 bool not_ok = false;
15791 subrtx_var_iterator::array_type array;
15792 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
15793 if (*iter != rtl && !CONSTANT_P (*iter))
15795 not_ok = true;
15796 break;
15799 if (not_ok)
15800 break;
15802 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
15803 if (!const_ok_for_output_1 (*iter))
15805 not_ok = true;
15806 break;
15809 if (not_ok)
15810 break;
15812 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
15813 goto symref;
15816 if (GET_CODE (rtl) == SYMBOL_REF
15817 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15819 dw_loc_descr_ref temp;
15821 /* If this is not defined, we have no way to emit the data. */
15822 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15823 break;
15825 temp = new_addr_loc_descr (rtl, dtprel_true);
15827 /* We check for DWARF 5 here because gdb did not implement
15828 DW_OP_form_tls_address until after 7.12. */
15829 mem_loc_result = new_loc_descr ((dwarf_version >= 5
15830 ? DW_OP_form_tls_address
15831 : DW_OP_GNU_push_tls_address),
15832 0, 0);
15833 add_loc_descr (&mem_loc_result, temp);
15835 break;
15838 if (!const_ok_for_output (rtl))
15840 if (GET_CODE (rtl) == CONST)
15841 switch (GET_CODE (XEXP (rtl, 0)))
15843 case NOT:
15844 op = DW_OP_not;
15845 goto try_const_unop;
15846 case NEG:
15847 op = DW_OP_neg;
15848 goto try_const_unop;
15849 try_const_unop:
15850 rtx arg;
15851 arg = XEXP (XEXP (rtl, 0), 0);
15852 if (!CONSTANT_P (arg))
15853 arg = gen_rtx_CONST (int_mode, arg);
15854 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
15855 initialized);
15856 if (op0)
15858 mem_loc_result = op0;
15859 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15861 break;
15862 default:
15863 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
15864 mem_mode, initialized);
15865 break;
15867 break;
15870 symref:
15871 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15872 vec_safe_push (used_rtx_array, rtl);
15873 break;
15875 case CONCAT:
15876 case CONCATN:
15877 case VAR_LOCATION:
15878 case DEBUG_IMPLICIT_PTR:
15879 expansion_failed (NULL_TREE, rtl,
15880 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15881 return 0;
15883 case ENTRY_VALUE:
15884 if (dwarf_strict && dwarf_version < 5)
15885 return NULL;
15886 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15888 if (!is_a <scalar_int_mode> (mode, &int_mode)
15889 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15890 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15891 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15892 else
15894 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15895 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15896 return NULL;
15897 op0 = one_reg_loc_descriptor (dbx_regnum,
15898 VAR_INIT_STATUS_INITIALIZED);
15901 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15902 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15904 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15905 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15906 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15907 return NULL;
15909 else
15910 gcc_unreachable ();
15911 if (op0 == NULL)
15912 return NULL;
15913 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15914 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15915 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15916 break;
15918 case DEBUG_PARAMETER_REF:
15919 mem_loc_result = parameter_ref_descriptor (rtl);
15920 break;
15922 case PRE_MODIFY:
15923 /* Extract the PLUS expression nested inside and fall into
15924 PLUS code below. */
15925 rtl = XEXP (rtl, 1);
15926 goto plus;
15928 case PRE_INC:
15929 case PRE_DEC:
15930 /* Turn these into a PLUS expression and fall into the PLUS code
15931 below. */
15932 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15933 gen_int_mode (GET_CODE (rtl) == PRE_INC
15934 ? GET_MODE_UNIT_SIZE (mem_mode)
15935 : -GET_MODE_UNIT_SIZE (mem_mode),
15936 mode));
15938 /* fall through */
15940 case PLUS:
15941 plus:
15942 if (is_based_loc (rtl)
15943 && is_a <scalar_int_mode> (mode, &int_mode)
15944 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15945 || XEXP (rtl, 0) == arg_pointer_rtx
15946 || XEXP (rtl, 0) == frame_pointer_rtx))
15947 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15948 INTVAL (XEXP (rtl, 1)),
15949 VAR_INIT_STATUS_INITIALIZED);
15950 else
15952 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15953 VAR_INIT_STATUS_INITIALIZED);
15954 if (mem_loc_result == 0)
15955 break;
15957 if (CONST_INT_P (XEXP (rtl, 1))
15958 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15959 <= DWARF2_ADDR_SIZE))
15960 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15961 else
15963 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15964 VAR_INIT_STATUS_INITIALIZED);
15965 if (op1 == 0)
15966 return NULL;
15967 add_loc_descr (&mem_loc_result, op1);
15968 add_loc_descr (&mem_loc_result,
15969 new_loc_descr (DW_OP_plus, 0, 0));
15972 break;
15974 /* If a pseudo-reg is optimized away, it is possible for it to
15975 be replaced with a MEM containing a multiply or shift. */
15976 case MINUS:
15977 op = DW_OP_minus;
15978 goto do_binop;
15980 case MULT:
15981 op = DW_OP_mul;
15982 goto do_binop;
15984 case DIV:
15985 if ((!dwarf_strict || dwarf_version >= 5)
15986 && is_a <scalar_int_mode> (mode, &int_mode)
15987 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15989 mem_loc_result = typed_binop (DW_OP_div, rtl,
15990 base_type_for_mode (mode, 0),
15991 int_mode, mem_mode);
15992 break;
15994 op = DW_OP_div;
15995 goto do_binop;
15997 case UMOD:
15998 op = DW_OP_mod;
15999 goto do_binop;
16001 case ASHIFT:
16002 op = DW_OP_shl;
16003 goto do_shift;
16005 case ASHIFTRT:
16006 op = DW_OP_shra;
16007 goto do_shift;
16009 case LSHIFTRT:
16010 op = DW_OP_shr;
16011 goto do_shift;
16013 do_shift:
16014 if (!is_a <scalar_int_mode> (mode, &int_mode))
16015 break;
16016 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16017 VAR_INIT_STATUS_INITIALIZED);
16019 rtx rtlop1 = XEXP (rtl, 1);
16020 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16021 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16022 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16023 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16024 VAR_INIT_STATUS_INITIALIZED);
16027 if (op0 == 0 || op1 == 0)
16028 break;
16030 mem_loc_result = op0;
16031 add_loc_descr (&mem_loc_result, op1);
16032 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16033 break;
16035 case AND:
16036 op = DW_OP_and;
16037 goto do_binop;
16039 case IOR:
16040 op = DW_OP_or;
16041 goto do_binop;
16043 case XOR:
16044 op = DW_OP_xor;
16045 goto do_binop;
16047 do_binop:
16048 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16049 VAR_INIT_STATUS_INITIALIZED);
16050 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16051 VAR_INIT_STATUS_INITIALIZED);
16053 if (op0 == 0 || op1 == 0)
16054 break;
16056 mem_loc_result = op0;
16057 add_loc_descr (&mem_loc_result, op1);
16058 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16059 break;
16061 case MOD:
16062 if ((!dwarf_strict || dwarf_version >= 5)
16063 && is_a <scalar_int_mode> (mode, &int_mode)
16064 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16066 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16067 base_type_for_mode (mode, 0),
16068 int_mode, mem_mode);
16069 break;
16072 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16073 VAR_INIT_STATUS_INITIALIZED);
16074 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16075 VAR_INIT_STATUS_INITIALIZED);
16077 if (op0 == 0 || op1 == 0)
16078 break;
16080 mem_loc_result = op0;
16081 add_loc_descr (&mem_loc_result, op1);
16082 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16083 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16084 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16085 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16086 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16087 break;
16089 case UDIV:
16090 if ((!dwarf_strict || dwarf_version >= 5)
16091 && is_a <scalar_int_mode> (mode, &int_mode))
16093 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16095 op = DW_OP_div;
16096 goto do_binop;
16098 mem_loc_result = typed_binop (DW_OP_div, rtl,
16099 base_type_for_mode (int_mode, 1),
16100 int_mode, mem_mode);
16102 break;
16104 case NOT:
16105 op = DW_OP_not;
16106 goto do_unop;
16108 case ABS:
16109 op = DW_OP_abs;
16110 goto do_unop;
16112 case NEG:
16113 op = DW_OP_neg;
16114 goto do_unop;
16116 do_unop:
16117 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16118 VAR_INIT_STATUS_INITIALIZED);
16120 if (op0 == 0)
16121 break;
16123 mem_loc_result = op0;
16124 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16125 break;
16127 case CONST_INT:
16128 if (!is_a <scalar_int_mode> (mode, &int_mode)
16129 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16130 #ifdef POINTERS_EXTEND_UNSIGNED
16131 || (int_mode == Pmode
16132 && mem_mode != VOIDmode
16133 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16134 #endif
16137 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16138 break;
16140 if ((!dwarf_strict || dwarf_version >= 5)
16141 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16142 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16144 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16145 scalar_int_mode amode;
16146 if (type_die == NULL)
16147 return NULL;
16148 if (INTVAL (rtl) >= 0
16149 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16150 .exists (&amode))
16151 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16152 /* const DW_OP_convert <XXX> vs.
16153 DW_OP_const_type <XXX, 1, const>. */
16154 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16155 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16157 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16158 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16159 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16160 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16161 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16162 add_loc_descr (&mem_loc_result, op0);
16163 return mem_loc_result;
16165 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16166 INTVAL (rtl));
16167 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16168 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16169 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16170 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16171 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16172 else
16174 mem_loc_result->dw_loc_oprnd2.val_class
16175 = dw_val_class_const_double;
16176 mem_loc_result->dw_loc_oprnd2.v.val_double
16177 = double_int::from_shwi (INTVAL (rtl));
16180 break;
16182 case CONST_DOUBLE:
16183 if (!dwarf_strict || dwarf_version >= 5)
16185 dw_die_ref type_die;
16187 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16188 CONST_DOUBLE rtx could represent either a large integer
16189 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16190 the value is always a floating point constant.
16192 When it is an integer, a CONST_DOUBLE is used whenever
16193 the constant requires 2 HWIs to be adequately represented.
16194 We output CONST_DOUBLEs as blocks. */
16195 if (mode == VOIDmode
16196 || (GET_MODE (rtl) == VOIDmode
16197 && maybe_ne (GET_MODE_BITSIZE (mode),
16198 HOST_BITS_PER_DOUBLE_INT)))
16199 break;
16200 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16201 if (type_die == NULL)
16202 return NULL;
16203 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16204 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16205 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16206 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16207 #if TARGET_SUPPORTS_WIDE_INT == 0
16208 if (!SCALAR_FLOAT_MODE_P (mode))
16210 mem_loc_result->dw_loc_oprnd2.val_class
16211 = dw_val_class_const_double;
16212 mem_loc_result->dw_loc_oprnd2.v.val_double
16213 = rtx_to_double_int (rtl);
16215 else
16216 #endif
16218 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16219 unsigned int length = GET_MODE_SIZE (float_mode);
16220 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16222 insert_float (rtl, array);
16223 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16224 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16225 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16226 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16229 break;
16231 case CONST_WIDE_INT:
16232 if (!dwarf_strict || dwarf_version >= 5)
16234 dw_die_ref type_die;
16236 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16237 if (type_die == NULL)
16238 return NULL;
16239 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16240 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16241 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16242 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16243 mem_loc_result->dw_loc_oprnd2.val_class
16244 = dw_val_class_wide_int;
16245 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16246 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16248 break;
16250 case CONST_POLY_INT:
16251 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16252 break;
16254 case EQ:
16255 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16256 break;
16258 case GE:
16259 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16260 break;
16262 case GT:
16263 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16264 break;
16266 case LE:
16267 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16268 break;
16270 case LT:
16271 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16272 break;
16274 case NE:
16275 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16276 break;
16278 case GEU:
16279 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16280 break;
16282 case GTU:
16283 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16284 break;
16286 case LEU:
16287 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16288 break;
16290 case LTU:
16291 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16292 break;
16294 case UMIN:
16295 case UMAX:
16296 if (!SCALAR_INT_MODE_P (mode))
16297 break;
16298 /* FALLTHRU */
16299 case SMIN:
16300 case SMAX:
16301 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16302 break;
16304 case ZERO_EXTRACT:
16305 case SIGN_EXTRACT:
16306 if (CONST_INT_P (XEXP (rtl, 1))
16307 && CONST_INT_P (XEXP (rtl, 2))
16308 && is_a <scalar_int_mode> (mode, &int_mode)
16309 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16310 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16311 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16312 && ((unsigned) INTVAL (XEXP (rtl, 1))
16313 + (unsigned) INTVAL (XEXP (rtl, 2))
16314 <= GET_MODE_BITSIZE (int_mode)))
16316 int shift, size;
16317 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16318 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16319 if (op0 == 0)
16320 break;
16321 if (GET_CODE (rtl) == SIGN_EXTRACT)
16322 op = DW_OP_shra;
16323 else
16324 op = DW_OP_shr;
16325 mem_loc_result = op0;
16326 size = INTVAL (XEXP (rtl, 1));
16327 shift = INTVAL (XEXP (rtl, 2));
16328 if (BITS_BIG_ENDIAN)
16329 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16330 if (shift + size != (int) DWARF2_ADDR_SIZE)
16332 add_loc_descr (&mem_loc_result,
16333 int_loc_descriptor (DWARF2_ADDR_SIZE
16334 - shift - size));
16335 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16337 if (size != (int) DWARF2_ADDR_SIZE)
16339 add_loc_descr (&mem_loc_result,
16340 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16341 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16344 break;
16346 case IF_THEN_ELSE:
16348 dw_loc_descr_ref op2, bra_node, drop_node;
16349 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16350 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16351 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16352 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16353 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16354 VAR_INIT_STATUS_INITIALIZED);
16355 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16356 VAR_INIT_STATUS_INITIALIZED);
16357 if (op0 == NULL || op1 == NULL || op2 == NULL)
16358 break;
16360 mem_loc_result = op1;
16361 add_loc_descr (&mem_loc_result, op2);
16362 add_loc_descr (&mem_loc_result, op0);
16363 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16364 add_loc_descr (&mem_loc_result, bra_node);
16365 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16366 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16367 add_loc_descr (&mem_loc_result, drop_node);
16368 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16369 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16371 break;
16373 case FLOAT_EXTEND:
16374 case FLOAT_TRUNCATE:
16375 case FLOAT:
16376 case UNSIGNED_FLOAT:
16377 case FIX:
16378 case UNSIGNED_FIX:
16379 if (!dwarf_strict || dwarf_version >= 5)
16381 dw_die_ref type_die;
16382 dw_loc_descr_ref cvt;
16384 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16385 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16386 if (op0 == NULL)
16387 break;
16388 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16389 && (GET_CODE (rtl) == FLOAT
16390 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16392 type_die = base_type_for_mode (int_mode,
16393 GET_CODE (rtl) == UNSIGNED_FLOAT);
16394 if (type_die == NULL)
16395 break;
16396 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16397 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16398 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16399 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16400 add_loc_descr (&op0, cvt);
16402 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16403 if (type_die == NULL)
16404 break;
16405 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16406 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16407 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16408 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16409 add_loc_descr (&op0, cvt);
16410 if (is_a <scalar_int_mode> (mode, &int_mode)
16411 && (GET_CODE (rtl) == FIX
16412 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16414 op0 = convert_descriptor_to_mode (int_mode, op0);
16415 if (op0 == NULL)
16416 break;
16418 mem_loc_result = op0;
16420 break;
16422 case CLZ:
16423 case CTZ:
16424 case FFS:
16425 if (is_a <scalar_int_mode> (mode, &int_mode))
16426 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16427 break;
16429 case POPCOUNT:
16430 case PARITY:
16431 if (is_a <scalar_int_mode> (mode, &int_mode))
16432 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16433 break;
16435 case BSWAP:
16436 if (is_a <scalar_int_mode> (mode, &int_mode))
16437 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16438 break;
16440 case ROTATE:
16441 case ROTATERT:
16442 if (is_a <scalar_int_mode> (mode, &int_mode))
16443 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16444 break;
16446 case COMPARE:
16447 /* In theory, we could implement the above. */
16448 /* DWARF cannot represent the unsigned compare operations
16449 natively. */
16450 case SS_MULT:
16451 case US_MULT:
16452 case SS_DIV:
16453 case US_DIV:
16454 case SS_PLUS:
16455 case US_PLUS:
16456 case SS_MINUS:
16457 case US_MINUS:
16458 case SS_NEG:
16459 case US_NEG:
16460 case SS_ABS:
16461 case SS_ASHIFT:
16462 case US_ASHIFT:
16463 case SS_TRUNCATE:
16464 case US_TRUNCATE:
16465 case UNORDERED:
16466 case ORDERED:
16467 case UNEQ:
16468 case UNGE:
16469 case UNGT:
16470 case UNLE:
16471 case UNLT:
16472 case LTGT:
16473 case FRACT_CONVERT:
16474 case UNSIGNED_FRACT_CONVERT:
16475 case SAT_FRACT:
16476 case UNSIGNED_SAT_FRACT:
16477 case SQRT:
16478 case ASM_OPERANDS:
16479 case VEC_MERGE:
16480 case VEC_SELECT:
16481 case VEC_CONCAT:
16482 case VEC_DUPLICATE:
16483 case VEC_SERIES:
16484 case HIGH:
16485 case FMA:
16486 case STRICT_LOW_PART:
16487 case CONST_VECTOR:
16488 case CONST_FIXED:
16489 case CLRSB:
16490 case CLOBBER:
16491 break;
16493 case CONST_STRING:
16494 resolve_one_addr (&rtl);
16495 goto symref;
16497 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16498 the expression. An UNSPEC rtx represents a raw DWARF operation,
16499 new_loc_descr is called for it to build the operation directly.
16500 Otherwise mem_loc_descriptor is called recursively. */
16501 case PARALLEL:
16503 int index = 0;
16504 dw_loc_descr_ref exp_result = NULL;
16506 for (; index < XVECLEN (rtl, 0); index++)
16508 rtx elem = XVECEXP (rtl, 0, index);
16509 if (GET_CODE (elem) == UNSPEC)
16511 /* Each DWARF operation UNSPEC contain two operands, if
16512 one operand is not used for the operation, const0_rtx is
16513 passed. */
16514 gcc_assert (XVECLEN (elem, 0) == 2);
16516 HOST_WIDE_INT dw_op = XINT (elem, 1);
16517 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16518 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16519 exp_result
16520 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16521 oprnd2);
16523 else
16524 exp_result
16525 = mem_loc_descriptor (elem, mode, mem_mode,
16526 VAR_INIT_STATUS_INITIALIZED);
16528 if (!mem_loc_result)
16529 mem_loc_result = exp_result;
16530 else
16531 add_loc_descr (&mem_loc_result, exp_result);
16534 break;
16537 default:
16538 if (flag_checking)
16540 print_rtl (stderr, rtl);
16541 gcc_unreachable ();
16543 break;
16546 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16547 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16549 return mem_loc_result;
16552 /* Return a descriptor that describes the concatenation of two locations.
16553 This is typically a complex variable. */
16555 static dw_loc_descr_ref
16556 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16558 /* At present we only track constant-sized pieces. */
16559 unsigned int size0, size1;
16560 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16561 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16562 return 0;
16564 dw_loc_descr_ref cc_loc_result = NULL;
16565 dw_loc_descr_ref x0_ref
16566 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16567 dw_loc_descr_ref x1_ref
16568 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16570 if (x0_ref == 0 || x1_ref == 0)
16571 return 0;
16573 cc_loc_result = x0_ref;
16574 add_loc_descr_op_piece (&cc_loc_result, size0);
16576 add_loc_descr (&cc_loc_result, x1_ref);
16577 add_loc_descr_op_piece (&cc_loc_result, size1);
16579 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16580 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16582 return cc_loc_result;
16585 /* Return a descriptor that describes the concatenation of N
16586 locations. */
16588 static dw_loc_descr_ref
16589 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16591 unsigned int i;
16592 dw_loc_descr_ref cc_loc_result = NULL;
16593 unsigned int n = XVECLEN (concatn, 0);
16594 unsigned int size;
16596 for (i = 0; i < n; ++i)
16598 dw_loc_descr_ref ref;
16599 rtx x = XVECEXP (concatn, 0, i);
16601 /* At present we only track constant-sized pieces. */
16602 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16603 return NULL;
16605 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16606 if (ref == NULL)
16607 return NULL;
16609 add_loc_descr (&cc_loc_result, ref);
16610 add_loc_descr_op_piece (&cc_loc_result, size);
16613 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16614 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16616 return cc_loc_result;
16619 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16620 for DEBUG_IMPLICIT_PTR RTL. */
16622 static dw_loc_descr_ref
16623 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16625 dw_loc_descr_ref ret;
16626 dw_die_ref ref;
16628 if (dwarf_strict && dwarf_version < 5)
16629 return NULL;
16630 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16631 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16632 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16633 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16634 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16635 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16636 if (ref)
16638 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16639 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16640 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16642 else
16644 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16645 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16647 return ret;
16650 /* Output a proper Dwarf location descriptor for a variable or parameter
16651 which is either allocated in a register or in a memory location. For a
16652 register, we just generate an OP_REG and the register number. For a
16653 memory location we provide a Dwarf postfix expression describing how to
16654 generate the (dynamic) address of the object onto the address stack.
16656 MODE is mode of the decl if this loc_descriptor is going to be used in
16657 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16658 allowed, VOIDmode otherwise.
16660 If we don't know how to describe it, return 0. */
16662 static dw_loc_descr_ref
16663 loc_descriptor (rtx rtl, machine_mode mode,
16664 enum var_init_status initialized)
16666 dw_loc_descr_ref loc_result = NULL;
16667 scalar_int_mode int_mode;
16669 switch (GET_CODE (rtl))
16671 case SUBREG:
16672 /* The case of a subreg may arise when we have a local (register)
16673 variable or a formal (register) parameter which doesn't quite fill
16674 up an entire register. For now, just assume that it is
16675 legitimate to make the Dwarf info refer to the whole register which
16676 contains the given subreg. */
16677 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16678 loc_result = loc_descriptor (SUBREG_REG (rtl),
16679 GET_MODE (SUBREG_REG (rtl)), initialized);
16680 else
16681 goto do_default;
16682 break;
16684 case REG:
16685 loc_result = reg_loc_descriptor (rtl, initialized);
16686 break;
16688 case MEM:
16689 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16690 GET_MODE (rtl), initialized);
16691 if (loc_result == NULL)
16692 loc_result = tls_mem_loc_descriptor (rtl);
16693 if (loc_result == NULL)
16695 rtx new_rtl = avoid_constant_pool_reference (rtl);
16696 if (new_rtl != rtl)
16697 loc_result = loc_descriptor (new_rtl, mode, initialized);
16699 break;
16701 case CONCAT:
16702 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
16703 initialized);
16704 break;
16706 case CONCATN:
16707 loc_result = concatn_loc_descriptor (rtl, initialized);
16708 break;
16710 case VAR_LOCATION:
16711 /* Single part. */
16712 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
16714 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
16715 if (GET_CODE (loc) == EXPR_LIST)
16716 loc = XEXP (loc, 0);
16717 loc_result = loc_descriptor (loc, mode, initialized);
16718 break;
16721 rtl = XEXP (rtl, 1);
16722 /* FALLTHRU */
16724 case PARALLEL:
16726 rtvec par_elems = XVEC (rtl, 0);
16727 int num_elem = GET_NUM_ELEM (par_elems);
16728 machine_mode mode;
16729 int i, size;
16731 /* Create the first one, so we have something to add to. */
16732 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
16733 VOIDmode, initialized);
16734 if (loc_result == NULL)
16735 return NULL;
16736 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
16737 /* At present we only track constant-sized pieces. */
16738 if (!GET_MODE_SIZE (mode).is_constant (&size))
16739 return NULL;
16740 add_loc_descr_op_piece (&loc_result, size);
16741 for (i = 1; i < num_elem; i++)
16743 dw_loc_descr_ref temp;
16745 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
16746 VOIDmode, initialized);
16747 if (temp == NULL)
16748 return NULL;
16749 add_loc_descr (&loc_result, temp);
16750 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
16751 /* At present we only track constant-sized pieces. */
16752 if (!GET_MODE_SIZE (mode).is_constant (&size))
16753 return NULL;
16754 add_loc_descr_op_piece (&loc_result, size);
16757 break;
16759 case CONST_INT:
16760 if (mode != VOIDmode && mode != BLKmode)
16762 int_mode = as_a <scalar_int_mode> (mode);
16763 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
16764 INTVAL (rtl));
16766 break;
16768 case CONST_DOUBLE:
16769 if (mode == VOIDmode)
16770 mode = GET_MODE (rtl);
16772 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16774 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16776 /* Note that a CONST_DOUBLE rtx could represent either an integer
16777 or a floating-point constant. A CONST_DOUBLE is used whenever
16778 the constant requires more than one word in order to be
16779 adequately represented. We output CONST_DOUBLEs as blocks. */
16780 scalar_mode smode = as_a <scalar_mode> (mode);
16781 loc_result = new_loc_descr (DW_OP_implicit_value,
16782 GET_MODE_SIZE (smode), 0);
16783 #if TARGET_SUPPORTS_WIDE_INT == 0
16784 if (!SCALAR_FLOAT_MODE_P (smode))
16786 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
16787 loc_result->dw_loc_oprnd2.v.val_double
16788 = rtx_to_double_int (rtl);
16790 else
16791 #endif
16793 unsigned int length = GET_MODE_SIZE (smode);
16794 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16796 insert_float (rtl, array);
16797 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16798 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16799 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16800 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16803 break;
16805 case CONST_WIDE_INT:
16806 if (mode == VOIDmode)
16807 mode = GET_MODE (rtl);
16809 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16811 int_mode = as_a <scalar_int_mode> (mode);
16812 loc_result = new_loc_descr (DW_OP_implicit_value,
16813 GET_MODE_SIZE (int_mode), 0);
16814 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16815 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16816 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
16818 break;
16820 case CONST_VECTOR:
16821 if (mode == VOIDmode)
16822 mode = GET_MODE (rtl);
16824 if (mode != VOIDmode
16825 /* The combination of a length and byte elt_size doesn't extend
16826 naturally to boolean vectors, where several elements are packed
16827 into the same byte. */
16828 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
16829 && (dwarf_version >= 4 || !dwarf_strict))
16831 unsigned int length;
16832 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
16833 return NULL;
16835 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16836 unsigned char *array
16837 = ggc_vec_alloc<unsigned char> (length * elt_size);
16838 unsigned int i;
16839 unsigned char *p;
16840 machine_mode imode = GET_MODE_INNER (mode);
16842 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16843 switch (GET_MODE_CLASS (mode))
16845 case MODE_VECTOR_INT:
16846 for (i = 0, p = array; i < length; i++, p += elt_size)
16848 rtx elt = CONST_VECTOR_ELT (rtl, i);
16849 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16851 break;
16853 case MODE_VECTOR_FLOAT:
16854 for (i = 0, p = array; i < length; i++, p += elt_size)
16856 rtx elt = CONST_VECTOR_ELT (rtl, i);
16857 insert_float (elt, p);
16859 break;
16861 default:
16862 gcc_unreachable ();
16865 loc_result = new_loc_descr (DW_OP_implicit_value,
16866 length * elt_size, 0);
16867 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16868 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16869 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16870 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16872 break;
16874 case CONST:
16875 if (mode == VOIDmode
16876 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16877 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16878 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16880 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16881 break;
16883 /* FALLTHROUGH */
16884 case SYMBOL_REF:
16885 if (!const_ok_for_output (rtl))
16886 break;
16887 /* FALLTHROUGH */
16888 case LABEL_REF:
16889 if (is_a <scalar_int_mode> (mode, &int_mode)
16890 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
16891 && (dwarf_version >= 4 || !dwarf_strict))
16893 loc_result = new_addr_loc_descr (rtl, dtprel_false);
16894 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16895 vec_safe_push (used_rtx_array, rtl);
16897 break;
16899 case DEBUG_IMPLICIT_PTR:
16900 loc_result = implicit_ptr_descriptor (rtl, 0);
16901 break;
16903 case PLUS:
16904 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16905 && CONST_INT_P (XEXP (rtl, 1)))
16907 loc_result
16908 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16909 break;
16911 /* FALLTHRU */
16912 do_default:
16913 default:
16914 if ((is_a <scalar_int_mode> (mode, &int_mode)
16915 && GET_MODE (rtl) == int_mode
16916 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16917 && dwarf_version >= 4)
16918 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16920 /* Value expression. */
16921 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16922 if (loc_result)
16923 add_loc_descr (&loc_result,
16924 new_loc_descr (DW_OP_stack_value, 0, 0));
16926 break;
16929 return loc_result;
16932 /* We need to figure out what section we should use as the base for the
16933 address ranges where a given location is valid.
16934 1. If this particular DECL has a section associated with it, use that.
16935 2. If this function has a section associated with it, use that.
16936 3. Otherwise, use the text section.
16937 XXX: If you split a variable across multiple sections, we won't notice. */
16939 static const char *
16940 secname_for_decl (const_tree decl)
16942 const char *secname;
16944 if (VAR_OR_FUNCTION_DECL_P (decl)
16945 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16946 && DECL_SECTION_NAME (decl))
16947 secname = DECL_SECTION_NAME (decl);
16948 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16950 if (in_cold_section_p)
16952 section *sec = current_function_section ();
16953 if (sec->common.flags & SECTION_NAMED)
16954 return sec->named.name;
16956 secname = DECL_SECTION_NAME (current_function_decl);
16958 else if (cfun && in_cold_section_p)
16959 secname = crtl->subsections.cold_section_label;
16960 else
16961 secname = text_section_label;
16963 return secname;
16966 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16968 static bool
16969 decl_by_reference_p (tree decl)
16971 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16972 || VAR_P (decl))
16973 && DECL_BY_REFERENCE (decl));
16976 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16977 for VARLOC. */
16979 static dw_loc_descr_ref
16980 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16981 enum var_init_status initialized)
16983 int have_address = 0;
16984 dw_loc_descr_ref descr;
16985 machine_mode mode;
16987 if (want_address != 2)
16989 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16990 /* Single part. */
16991 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16993 varloc = PAT_VAR_LOCATION_LOC (varloc);
16994 if (GET_CODE (varloc) == EXPR_LIST)
16995 varloc = XEXP (varloc, 0);
16996 mode = GET_MODE (varloc);
16997 if (MEM_P (varloc))
16999 rtx addr = XEXP (varloc, 0);
17000 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17001 mode, initialized);
17002 if (descr)
17003 have_address = 1;
17004 else
17006 rtx x = avoid_constant_pool_reference (varloc);
17007 if (x != varloc)
17008 descr = mem_loc_descriptor (x, mode, VOIDmode,
17009 initialized);
17012 else
17013 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17015 else
17016 return 0;
17018 else
17020 if (GET_CODE (varloc) == VAR_LOCATION)
17021 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17022 else
17023 mode = DECL_MODE (loc);
17024 descr = loc_descriptor (varloc, mode, initialized);
17025 have_address = 1;
17028 if (!descr)
17029 return 0;
17031 if (want_address == 2 && !have_address
17032 && (dwarf_version >= 4 || !dwarf_strict))
17034 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17036 expansion_failed (loc, NULL_RTX,
17037 "DWARF address size mismatch");
17038 return 0;
17040 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17041 have_address = 1;
17043 /* Show if we can't fill the request for an address. */
17044 if (want_address && !have_address)
17046 expansion_failed (loc, NULL_RTX,
17047 "Want address and only have value");
17048 return 0;
17051 /* If we've got an address and don't want one, dereference. */
17052 if (!want_address && have_address)
17054 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17055 enum dwarf_location_atom op;
17057 if (size > DWARF2_ADDR_SIZE || size == -1)
17059 expansion_failed (loc, NULL_RTX,
17060 "DWARF address size mismatch");
17061 return 0;
17063 else if (size == DWARF2_ADDR_SIZE)
17064 op = DW_OP_deref;
17065 else
17066 op = DW_OP_deref_size;
17068 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17071 return descr;
17074 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17075 if it is not possible. */
17077 static dw_loc_descr_ref
17078 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17080 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17081 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17082 else if (dwarf_version >= 3 || !dwarf_strict)
17083 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17084 else
17085 return NULL;
17088 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17089 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17091 static dw_loc_descr_ref
17092 dw_sra_loc_expr (tree decl, rtx loc)
17094 rtx p;
17095 unsigned HOST_WIDE_INT padsize = 0;
17096 dw_loc_descr_ref descr, *descr_tail;
17097 unsigned HOST_WIDE_INT decl_size;
17098 rtx varloc;
17099 enum var_init_status initialized;
17101 if (DECL_SIZE (decl) == NULL
17102 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17103 return NULL;
17105 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17106 descr = NULL;
17107 descr_tail = &descr;
17109 for (p = loc; p; p = XEXP (p, 1))
17111 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17112 rtx loc_note = *decl_piece_varloc_ptr (p);
17113 dw_loc_descr_ref cur_descr;
17114 dw_loc_descr_ref *tail, last = NULL;
17115 unsigned HOST_WIDE_INT opsize = 0;
17117 if (loc_note == NULL_RTX
17118 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17120 padsize += bitsize;
17121 continue;
17123 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17124 varloc = NOTE_VAR_LOCATION (loc_note);
17125 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17126 if (cur_descr == NULL)
17128 padsize += bitsize;
17129 continue;
17132 /* Check that cur_descr either doesn't use
17133 DW_OP_*piece operations, or their sum is equal
17134 to bitsize. Otherwise we can't embed it. */
17135 for (tail = &cur_descr; *tail != NULL;
17136 tail = &(*tail)->dw_loc_next)
17137 if ((*tail)->dw_loc_opc == DW_OP_piece)
17139 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17140 * BITS_PER_UNIT;
17141 last = *tail;
17143 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17145 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17146 last = *tail;
17149 if (last != NULL && opsize != bitsize)
17151 padsize += bitsize;
17152 /* Discard the current piece of the descriptor and release any
17153 addr_table entries it uses. */
17154 remove_loc_list_addr_table_entries (cur_descr);
17155 continue;
17158 /* If there is a hole, add DW_OP_*piece after empty DWARF
17159 expression, which means that those bits are optimized out. */
17160 if (padsize)
17162 if (padsize > decl_size)
17164 remove_loc_list_addr_table_entries (cur_descr);
17165 goto discard_descr;
17167 decl_size -= padsize;
17168 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17169 if (*descr_tail == NULL)
17171 remove_loc_list_addr_table_entries (cur_descr);
17172 goto discard_descr;
17174 descr_tail = &(*descr_tail)->dw_loc_next;
17175 padsize = 0;
17177 *descr_tail = cur_descr;
17178 descr_tail = tail;
17179 if (bitsize > decl_size)
17180 goto discard_descr;
17181 decl_size -= bitsize;
17182 if (last == NULL)
17184 HOST_WIDE_INT offset = 0;
17185 if (GET_CODE (varloc) == VAR_LOCATION
17186 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17188 varloc = PAT_VAR_LOCATION_LOC (varloc);
17189 if (GET_CODE (varloc) == EXPR_LIST)
17190 varloc = XEXP (varloc, 0);
17194 if (GET_CODE (varloc) == CONST
17195 || GET_CODE (varloc) == SIGN_EXTEND
17196 || GET_CODE (varloc) == ZERO_EXTEND)
17197 varloc = XEXP (varloc, 0);
17198 else if (GET_CODE (varloc) == SUBREG)
17199 varloc = SUBREG_REG (varloc);
17200 else
17201 break;
17203 while (1);
17204 /* DW_OP_bit_size offset should be zero for register
17205 or implicit location descriptions and empty location
17206 descriptions, but for memory addresses needs big endian
17207 adjustment. */
17208 if (MEM_P (varloc))
17210 unsigned HOST_WIDE_INT memsize;
17211 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17212 goto discard_descr;
17213 memsize *= BITS_PER_UNIT;
17214 if (memsize != bitsize)
17216 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17217 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17218 goto discard_descr;
17219 if (memsize < bitsize)
17220 goto discard_descr;
17221 if (BITS_BIG_ENDIAN)
17222 offset = memsize - bitsize;
17226 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17227 if (*descr_tail == NULL)
17228 goto discard_descr;
17229 descr_tail = &(*descr_tail)->dw_loc_next;
17233 /* If there were any non-empty expressions, add padding till the end of
17234 the decl. */
17235 if (descr != NULL && decl_size != 0)
17237 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17238 if (*descr_tail == NULL)
17239 goto discard_descr;
17241 return descr;
17243 discard_descr:
17244 /* Discard the descriptor and release any addr_table entries it uses. */
17245 remove_loc_list_addr_table_entries (descr);
17246 return NULL;
17249 /* Return the dwarf representation of the location list LOC_LIST of
17250 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17251 function. */
17253 static dw_loc_list_ref
17254 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17256 const char *endname, *secname;
17257 var_loc_view endview;
17258 rtx varloc;
17259 enum var_init_status initialized;
17260 struct var_loc_node *node;
17261 dw_loc_descr_ref descr;
17262 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17263 dw_loc_list_ref list = NULL;
17264 dw_loc_list_ref *listp = &list;
17266 /* Now that we know what section we are using for a base,
17267 actually construct the list of locations.
17268 The first location information is what is passed to the
17269 function that creates the location list, and the remaining
17270 locations just get added on to that list.
17271 Note that we only know the start address for a location
17272 (IE location changes), so to build the range, we use
17273 the range [current location start, next location start].
17274 This means we have to special case the last node, and generate
17275 a range of [last location start, end of function label]. */
17277 if (cfun && crtl->has_bb_partition)
17279 bool save_in_cold_section_p = in_cold_section_p;
17280 in_cold_section_p = first_function_block_is_cold;
17281 if (loc_list->last_before_switch == NULL)
17282 in_cold_section_p = !in_cold_section_p;
17283 secname = secname_for_decl (decl);
17284 in_cold_section_p = save_in_cold_section_p;
17286 else
17287 secname = secname_for_decl (decl);
17289 for (node = loc_list->first; node; node = node->next)
17291 bool range_across_switch = false;
17292 if (GET_CODE (node->loc) == EXPR_LIST
17293 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17295 if (GET_CODE (node->loc) == EXPR_LIST)
17297 descr = NULL;
17298 /* This requires DW_OP_{,bit_}piece, which is not usable
17299 inside DWARF expressions. */
17300 if (want_address == 2)
17301 descr = dw_sra_loc_expr (decl, node->loc);
17303 else
17305 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17306 varloc = NOTE_VAR_LOCATION (node->loc);
17307 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17309 if (descr)
17311 /* If section switch happens in between node->label
17312 and node->next->label (or end of function) and
17313 we can't emit it as a single entry list,
17314 emit two ranges, first one ending at the end
17315 of first partition and second one starting at the
17316 beginning of second partition. */
17317 if (node == loc_list->last_before_switch
17318 && (node != loc_list->first || loc_list->first->next
17319 /* If we are to emit a view number, we will emit
17320 a loclist rather than a single location
17321 expression for the entire function (see
17322 loc_list_has_views), so we have to split the
17323 range that straddles across partitions. */
17324 || !ZERO_VIEW_P (node->view))
17325 && current_function_decl)
17327 endname = cfun->fde->dw_fde_end;
17328 endview = 0;
17329 range_across_switch = true;
17331 /* The variable has a location between NODE->LABEL and
17332 NODE->NEXT->LABEL. */
17333 else if (node->next)
17334 endname = node->next->label, endview = node->next->view;
17335 /* If the variable has a location at the last label
17336 it keeps its location until the end of function. */
17337 else if (!current_function_decl)
17338 endname = text_end_label, endview = 0;
17339 else
17341 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17342 current_function_funcdef_no);
17343 endname = ggc_strdup (label_id);
17344 endview = 0;
17347 *listp = new_loc_list (descr, node->label, node->view,
17348 endname, endview, secname);
17349 if (TREE_CODE (decl) == PARM_DECL
17350 && node == loc_list->first
17351 && NOTE_P (node->loc)
17352 && strcmp (node->label, endname) == 0)
17353 (*listp)->force = true;
17354 listp = &(*listp)->dw_loc_next;
17358 if (cfun
17359 && crtl->has_bb_partition
17360 && node == loc_list->last_before_switch)
17362 bool save_in_cold_section_p = in_cold_section_p;
17363 in_cold_section_p = !first_function_block_is_cold;
17364 secname = secname_for_decl (decl);
17365 in_cold_section_p = save_in_cold_section_p;
17368 if (range_across_switch)
17370 if (GET_CODE (node->loc) == EXPR_LIST)
17371 descr = dw_sra_loc_expr (decl, node->loc);
17372 else
17374 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17375 varloc = NOTE_VAR_LOCATION (node->loc);
17376 descr = dw_loc_list_1 (decl, varloc, want_address,
17377 initialized);
17379 gcc_assert (descr);
17380 /* The variable has a location between NODE->LABEL and
17381 NODE->NEXT->LABEL. */
17382 if (node->next)
17383 endname = node->next->label, endview = node->next->view;
17384 else
17385 endname = cfun->fde->dw_fde_second_end, endview = 0;
17386 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17387 endname, endview, secname);
17388 listp = &(*listp)->dw_loc_next;
17392 /* Try to avoid the overhead of a location list emitting a location
17393 expression instead, but only if we didn't have more than one
17394 location entry in the first place. If some entries were not
17395 representable, we don't want to pretend a single entry that was
17396 applies to the entire scope in which the variable is
17397 available. */
17398 if (list && loc_list->first->next)
17399 gen_llsym (list);
17400 else
17401 maybe_gen_llsym (list);
17403 return list;
17406 /* Return if the loc_list has only single element and thus can be represented
17407 as location description. */
17409 static bool
17410 single_element_loc_list_p (dw_loc_list_ref list)
17412 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17413 return !list->ll_symbol;
17416 /* Duplicate a single element of location list. */
17418 static inline dw_loc_descr_ref
17419 copy_loc_descr (dw_loc_descr_ref ref)
17421 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17422 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17423 return copy;
17426 /* To each location in list LIST append loc descr REF. */
17428 static void
17429 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17431 dw_loc_descr_ref copy;
17432 add_loc_descr (&list->expr, ref);
17433 list = list->dw_loc_next;
17434 while (list)
17436 copy = copy_loc_descr (ref);
17437 add_loc_descr (&list->expr, copy);
17438 while (copy->dw_loc_next)
17439 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17440 list = list->dw_loc_next;
17444 /* To each location in list LIST prepend loc descr REF. */
17446 static void
17447 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17449 dw_loc_descr_ref copy;
17450 dw_loc_descr_ref ref_end = list->expr;
17451 add_loc_descr (&ref, list->expr);
17452 list->expr = ref;
17453 list = list->dw_loc_next;
17454 while (list)
17456 dw_loc_descr_ref end = list->expr;
17457 list->expr = copy = copy_loc_descr (ref);
17458 while (copy->dw_loc_next != ref_end)
17459 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17460 copy->dw_loc_next = end;
17461 list = list->dw_loc_next;
17465 /* Given two lists RET and LIST
17466 produce location list that is result of adding expression in LIST
17467 to expression in RET on each position in program.
17468 Might be destructive on both RET and LIST.
17470 TODO: We handle only simple cases of RET or LIST having at most one
17471 element. General case would involve sorting the lists in program order
17472 and merging them that will need some additional work.
17473 Adding that will improve quality of debug info especially for SRA-ed
17474 structures. */
17476 static void
17477 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17479 if (!list)
17480 return;
17481 if (!*ret)
17483 *ret = list;
17484 return;
17486 if (!list->dw_loc_next)
17488 add_loc_descr_to_each (*ret, list->expr);
17489 return;
17491 if (!(*ret)->dw_loc_next)
17493 prepend_loc_descr_to_each (list, (*ret)->expr);
17494 *ret = list;
17495 return;
17497 expansion_failed (NULL_TREE, NULL_RTX,
17498 "Don't know how to merge two non-trivial"
17499 " location lists.\n");
17500 *ret = NULL;
17501 return;
17504 /* LOC is constant expression. Try a luck, look it up in constant
17505 pool and return its loc_descr of its address. */
17507 static dw_loc_descr_ref
17508 cst_pool_loc_descr (tree loc)
17510 /* Get an RTL for this, if something has been emitted. */
17511 rtx rtl = lookup_constant_def (loc);
17513 if (!rtl || !MEM_P (rtl))
17515 gcc_assert (!rtl);
17516 return 0;
17518 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17520 /* TODO: We might get more coverage if we was actually delaying expansion
17521 of all expressions till end of compilation when constant pools are fully
17522 populated. */
17523 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17525 expansion_failed (loc, NULL_RTX,
17526 "CST value in contant pool but not marked.");
17527 return 0;
17529 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17530 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17533 /* Return dw_loc_list representing address of addr_expr LOC
17534 by looking for inner INDIRECT_REF expression and turning
17535 it into simple arithmetics.
17537 See loc_list_from_tree for the meaning of CONTEXT. */
17539 static dw_loc_list_ref
17540 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17541 loc_descr_context *context)
17543 tree obj, offset;
17544 poly_int64 bitsize, bitpos, bytepos;
17545 machine_mode mode;
17546 int unsignedp, reversep, volatilep = 0;
17547 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17549 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17550 &bitsize, &bitpos, &offset, &mode,
17551 &unsignedp, &reversep, &volatilep);
17552 STRIP_NOPS (obj);
17553 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17555 expansion_failed (loc, NULL_RTX, "bitfield access");
17556 return 0;
17558 if (!INDIRECT_REF_P (obj))
17560 expansion_failed (obj,
17561 NULL_RTX, "no indirect ref in inner refrence");
17562 return 0;
17564 if (!offset && known_eq (bitpos, 0))
17565 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17566 context);
17567 else if (toplev
17568 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17569 && (dwarf_version >= 4 || !dwarf_strict))
17571 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17572 if (!list_ret)
17573 return 0;
17574 if (offset)
17576 /* Variable offset. */
17577 list_ret1 = loc_list_from_tree (offset, 0, context);
17578 if (list_ret1 == 0)
17579 return 0;
17580 add_loc_list (&list_ret, list_ret1);
17581 if (!list_ret)
17582 return 0;
17583 add_loc_descr_to_each (list_ret,
17584 new_loc_descr (DW_OP_plus, 0, 0));
17586 HOST_WIDE_INT value;
17587 if (bytepos.is_constant (&value) && value > 0)
17588 add_loc_descr_to_each (list_ret,
17589 new_loc_descr (DW_OP_plus_uconst, value, 0));
17590 else if (maybe_ne (bytepos, 0))
17591 loc_list_plus_const (list_ret, bytepos);
17592 add_loc_descr_to_each (list_ret,
17593 new_loc_descr (DW_OP_stack_value, 0, 0));
17595 return list_ret;
17598 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17599 all operations from LOC are nops, move to the last one. Insert in NOPS all
17600 operations that are skipped. */
17602 static void
17603 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17604 hash_set<dw_loc_descr_ref> &nops)
17606 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17608 nops.add (loc);
17609 loc = loc->dw_loc_next;
17613 /* Helper for loc_descr_without_nops: free the location description operation
17614 P. */
17616 bool
17617 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17619 ggc_free (loc);
17620 return true;
17623 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17624 finishes LOC. */
17626 static void
17627 loc_descr_without_nops (dw_loc_descr_ref &loc)
17629 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17630 return;
17632 /* Set of all DW_OP_nop operations we remove. */
17633 hash_set<dw_loc_descr_ref> nops;
17635 /* First, strip all prefix NOP operations in order to keep the head of the
17636 operations list. */
17637 loc_descr_to_next_no_nop (loc, nops);
17639 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17641 /* For control flow operations: strip "prefix" nops in destination
17642 labels. */
17643 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17644 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17645 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17646 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17648 /* Do the same for the operations that follow, then move to the next
17649 iteration. */
17650 if (cur->dw_loc_next != NULL)
17651 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17652 cur = cur->dw_loc_next;
17655 nops.traverse<void *, free_loc_descr> (NULL);
17659 struct dwarf_procedure_info;
17661 /* Helper structure for location descriptions generation. */
17662 struct loc_descr_context
17664 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17665 NULL_TREE if DW_OP_push_object_address in invalid for this location
17666 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17667 tree context_type;
17668 /* The ..._DECL node that should be translated as a
17669 DW_OP_push_object_address operation. */
17670 tree base_decl;
17671 /* Information about the DWARF procedure we are currently generating. NULL if
17672 we are not generating a DWARF procedure. */
17673 struct dwarf_procedure_info *dpi;
17674 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17675 by consumer. Used for DW_TAG_generic_subrange attributes. */
17676 bool placeholder_arg;
17677 /* True if PLACEHOLDER_EXPR has been seen. */
17678 bool placeholder_seen;
17681 /* DWARF procedures generation
17683 DWARF expressions (aka. location descriptions) are used to encode variable
17684 things such as sizes or offsets. Such computations can have redundant parts
17685 that can be factorized in order to reduce the size of the output debug
17686 information. This is the whole point of DWARF procedures.
17688 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
17689 already factorized into functions ("size functions") in order to handle very
17690 big and complex types. Such functions are quite simple: they have integral
17691 arguments, they return an integral result and their body contains only a
17692 return statement with arithmetic expressions. This is the only kind of
17693 function we are interested in translating into DWARF procedures, here.
17695 DWARF expressions and DWARF procedure are executed using a stack, so we have
17696 to define some calling convention for them to interact. Let's say that:
17698 - Before calling a DWARF procedure, DWARF expressions must push on the stack
17699 all arguments in reverse order (right-to-left) so that when the DWARF
17700 procedure execution starts, the first argument is the top of the stack.
17702 - Then, when returning, the DWARF procedure must have consumed all arguments
17703 on the stack, must have pushed the result and touched nothing else.
17705 - Each integral argument and the result are integral types can be hold in a
17706 single stack slot.
17708 - We call "frame offset" the number of stack slots that are "under DWARF
17709 procedure control": it includes the arguments slots, the temporaries and
17710 the result slot. Thus, it is equal to the number of arguments when the
17711 procedure execution starts and must be equal to one (the result) when it
17712 returns. */
17714 /* Helper structure used when generating operations for a DWARF procedure. */
17715 struct dwarf_procedure_info
17717 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
17718 currently translated. */
17719 tree fndecl;
17720 /* The number of arguments FNDECL takes. */
17721 unsigned args_count;
17724 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
17725 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
17726 equate it to this DIE. */
17728 static dw_die_ref
17729 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
17730 dw_die_ref parent_die)
17732 dw_die_ref dwarf_proc_die;
17734 if ((dwarf_version < 3 && dwarf_strict)
17735 || location == NULL)
17736 return NULL;
17738 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
17739 if (fndecl)
17740 equate_decl_number_to_die (fndecl, dwarf_proc_die);
17741 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
17742 return dwarf_proc_die;
17745 /* Return whether TYPE is a supported type as a DWARF procedure argument
17746 type or return type (we handle only scalar types and pointer types that
17747 aren't wider than the DWARF expression evaluation stack. */
17749 static bool
17750 is_handled_procedure_type (tree type)
17752 return ((INTEGRAL_TYPE_P (type)
17753 || TREE_CODE (type) == OFFSET_TYPE
17754 || TREE_CODE (type) == POINTER_TYPE)
17755 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
17758 /* Helper for resolve_args_picking: do the same but stop when coming across
17759 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
17760 offset *before* evaluating the corresponding operation. */
17762 static bool
17763 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17764 struct dwarf_procedure_info *dpi,
17765 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
17767 /* The "frame_offset" identifier is already used to name a macro... */
17768 unsigned frame_offset_ = initial_frame_offset;
17769 dw_loc_descr_ref l;
17771 for (l = loc; l != NULL;)
17773 bool existed;
17774 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
17776 /* If we already met this node, there is nothing to compute anymore. */
17777 if (existed)
17779 /* Make sure that the stack size is consistent wherever the execution
17780 flow comes from. */
17781 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
17782 break;
17784 l_frame_offset = frame_offset_;
17786 /* If needed, relocate the picking offset with respect to the frame
17787 offset. */
17788 if (l->frame_offset_rel)
17790 unsigned HOST_WIDE_INT off;
17791 switch (l->dw_loc_opc)
17793 case DW_OP_pick:
17794 off = l->dw_loc_oprnd1.v.val_unsigned;
17795 break;
17796 case DW_OP_dup:
17797 off = 0;
17798 break;
17799 case DW_OP_over:
17800 off = 1;
17801 break;
17802 default:
17803 gcc_unreachable ();
17805 /* frame_offset_ is the size of the current stack frame, including
17806 incoming arguments. Besides, the arguments are pushed
17807 right-to-left. Thus, in order to access the Nth argument from
17808 this operation node, the picking has to skip temporaries *plus*
17809 one stack slot per argument (0 for the first one, 1 for the second
17810 one, etc.).
17812 The targetted argument number (N) is already set as the operand,
17813 and the number of temporaries can be computed with:
17814 frame_offsets_ - dpi->args_count */
17815 off += frame_offset_ - dpi->args_count;
17817 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
17818 if (off > 255)
17819 return false;
17821 if (off == 0)
17823 l->dw_loc_opc = DW_OP_dup;
17824 l->dw_loc_oprnd1.v.val_unsigned = 0;
17826 else if (off == 1)
17828 l->dw_loc_opc = DW_OP_over;
17829 l->dw_loc_oprnd1.v.val_unsigned = 0;
17831 else
17833 l->dw_loc_opc = DW_OP_pick;
17834 l->dw_loc_oprnd1.v.val_unsigned = off;
17838 /* Update frame_offset according to the effect the current operation has
17839 on the stack. */
17840 switch (l->dw_loc_opc)
17842 case DW_OP_deref:
17843 case DW_OP_swap:
17844 case DW_OP_rot:
17845 case DW_OP_abs:
17846 case DW_OP_neg:
17847 case DW_OP_not:
17848 case DW_OP_plus_uconst:
17849 case DW_OP_skip:
17850 case DW_OP_reg0:
17851 case DW_OP_reg1:
17852 case DW_OP_reg2:
17853 case DW_OP_reg3:
17854 case DW_OP_reg4:
17855 case DW_OP_reg5:
17856 case DW_OP_reg6:
17857 case DW_OP_reg7:
17858 case DW_OP_reg8:
17859 case DW_OP_reg9:
17860 case DW_OP_reg10:
17861 case DW_OP_reg11:
17862 case DW_OP_reg12:
17863 case DW_OP_reg13:
17864 case DW_OP_reg14:
17865 case DW_OP_reg15:
17866 case DW_OP_reg16:
17867 case DW_OP_reg17:
17868 case DW_OP_reg18:
17869 case DW_OP_reg19:
17870 case DW_OP_reg20:
17871 case DW_OP_reg21:
17872 case DW_OP_reg22:
17873 case DW_OP_reg23:
17874 case DW_OP_reg24:
17875 case DW_OP_reg25:
17876 case DW_OP_reg26:
17877 case DW_OP_reg27:
17878 case DW_OP_reg28:
17879 case DW_OP_reg29:
17880 case DW_OP_reg30:
17881 case DW_OP_reg31:
17882 case DW_OP_bregx:
17883 case DW_OP_piece:
17884 case DW_OP_deref_size:
17885 case DW_OP_nop:
17886 case DW_OP_bit_piece:
17887 case DW_OP_implicit_value:
17888 case DW_OP_stack_value:
17889 break;
17891 case DW_OP_addr:
17892 case DW_OP_const1u:
17893 case DW_OP_const1s:
17894 case DW_OP_const2u:
17895 case DW_OP_const2s:
17896 case DW_OP_const4u:
17897 case DW_OP_const4s:
17898 case DW_OP_const8u:
17899 case DW_OP_const8s:
17900 case DW_OP_constu:
17901 case DW_OP_consts:
17902 case DW_OP_dup:
17903 case DW_OP_over:
17904 case DW_OP_pick:
17905 case DW_OP_lit0:
17906 case DW_OP_lit1:
17907 case DW_OP_lit2:
17908 case DW_OP_lit3:
17909 case DW_OP_lit4:
17910 case DW_OP_lit5:
17911 case DW_OP_lit6:
17912 case DW_OP_lit7:
17913 case DW_OP_lit8:
17914 case DW_OP_lit9:
17915 case DW_OP_lit10:
17916 case DW_OP_lit11:
17917 case DW_OP_lit12:
17918 case DW_OP_lit13:
17919 case DW_OP_lit14:
17920 case DW_OP_lit15:
17921 case DW_OP_lit16:
17922 case DW_OP_lit17:
17923 case DW_OP_lit18:
17924 case DW_OP_lit19:
17925 case DW_OP_lit20:
17926 case DW_OP_lit21:
17927 case DW_OP_lit22:
17928 case DW_OP_lit23:
17929 case DW_OP_lit24:
17930 case DW_OP_lit25:
17931 case DW_OP_lit26:
17932 case DW_OP_lit27:
17933 case DW_OP_lit28:
17934 case DW_OP_lit29:
17935 case DW_OP_lit30:
17936 case DW_OP_lit31:
17937 case DW_OP_breg0:
17938 case DW_OP_breg1:
17939 case DW_OP_breg2:
17940 case DW_OP_breg3:
17941 case DW_OP_breg4:
17942 case DW_OP_breg5:
17943 case DW_OP_breg6:
17944 case DW_OP_breg7:
17945 case DW_OP_breg8:
17946 case DW_OP_breg9:
17947 case DW_OP_breg10:
17948 case DW_OP_breg11:
17949 case DW_OP_breg12:
17950 case DW_OP_breg13:
17951 case DW_OP_breg14:
17952 case DW_OP_breg15:
17953 case DW_OP_breg16:
17954 case DW_OP_breg17:
17955 case DW_OP_breg18:
17956 case DW_OP_breg19:
17957 case DW_OP_breg20:
17958 case DW_OP_breg21:
17959 case DW_OP_breg22:
17960 case DW_OP_breg23:
17961 case DW_OP_breg24:
17962 case DW_OP_breg25:
17963 case DW_OP_breg26:
17964 case DW_OP_breg27:
17965 case DW_OP_breg28:
17966 case DW_OP_breg29:
17967 case DW_OP_breg30:
17968 case DW_OP_breg31:
17969 case DW_OP_fbreg:
17970 case DW_OP_push_object_address:
17971 case DW_OP_call_frame_cfa:
17972 case DW_OP_GNU_variable_value:
17973 case DW_OP_GNU_addr_index:
17974 case DW_OP_GNU_const_index:
17975 ++frame_offset_;
17976 break;
17978 case DW_OP_drop:
17979 case DW_OP_xderef:
17980 case DW_OP_and:
17981 case DW_OP_div:
17982 case DW_OP_minus:
17983 case DW_OP_mod:
17984 case DW_OP_mul:
17985 case DW_OP_or:
17986 case DW_OP_plus:
17987 case DW_OP_shl:
17988 case DW_OP_shr:
17989 case DW_OP_shra:
17990 case DW_OP_xor:
17991 case DW_OP_bra:
17992 case DW_OP_eq:
17993 case DW_OP_ge:
17994 case DW_OP_gt:
17995 case DW_OP_le:
17996 case DW_OP_lt:
17997 case DW_OP_ne:
17998 case DW_OP_regx:
17999 case DW_OP_xderef_size:
18000 --frame_offset_;
18001 break;
18003 case DW_OP_call2:
18004 case DW_OP_call4:
18005 case DW_OP_call_ref:
18007 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18008 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18010 if (stack_usage == NULL)
18011 return false;
18012 frame_offset_ += *stack_usage;
18013 break;
18016 case DW_OP_implicit_pointer:
18017 case DW_OP_entry_value:
18018 case DW_OP_const_type:
18019 case DW_OP_regval_type:
18020 case DW_OP_deref_type:
18021 case DW_OP_convert:
18022 case DW_OP_reinterpret:
18023 case DW_OP_form_tls_address:
18024 case DW_OP_GNU_push_tls_address:
18025 case DW_OP_GNU_uninit:
18026 case DW_OP_GNU_encoded_addr:
18027 case DW_OP_GNU_implicit_pointer:
18028 case DW_OP_GNU_entry_value:
18029 case DW_OP_GNU_const_type:
18030 case DW_OP_GNU_regval_type:
18031 case DW_OP_GNU_deref_type:
18032 case DW_OP_GNU_convert:
18033 case DW_OP_GNU_reinterpret:
18034 case DW_OP_GNU_parameter_ref:
18035 /* loc_list_from_tree will probably not output these operations for
18036 size functions, so assume they will not appear here. */
18037 /* Fall through... */
18039 default:
18040 gcc_unreachable ();
18043 /* Now, follow the control flow (except subroutine calls). */
18044 switch (l->dw_loc_opc)
18046 case DW_OP_bra:
18047 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18048 frame_offsets))
18049 return false;
18050 /* Fall through. */
18052 case DW_OP_skip:
18053 l = l->dw_loc_oprnd1.v.val_loc;
18054 break;
18056 case DW_OP_stack_value:
18057 return true;
18059 default:
18060 l = l->dw_loc_next;
18061 break;
18065 return true;
18068 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18069 operations) in order to resolve the operand of DW_OP_pick operations that
18070 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18071 offset *before* LOC is executed. Return if all relocations were
18072 successful. */
18074 static bool
18075 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18076 struct dwarf_procedure_info *dpi)
18078 /* Associate to all visited operations the frame offset *before* evaluating
18079 this operation. */
18080 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18082 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
18083 frame_offsets);
18086 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18087 Return NULL if it is not possible. */
18089 static dw_die_ref
18090 function_to_dwarf_procedure (tree fndecl)
18092 struct loc_descr_context ctx;
18093 struct dwarf_procedure_info dpi;
18094 dw_die_ref dwarf_proc_die;
18095 tree tree_body = DECL_SAVED_TREE (fndecl);
18096 dw_loc_descr_ref loc_body, epilogue;
18098 tree cursor;
18099 unsigned i;
18101 /* Do not generate multiple DWARF procedures for the same function
18102 declaration. */
18103 dwarf_proc_die = lookup_decl_die (fndecl);
18104 if (dwarf_proc_die != NULL)
18105 return dwarf_proc_die;
18107 /* DWARF procedures are available starting with the DWARFv3 standard. */
18108 if (dwarf_version < 3 && dwarf_strict)
18109 return NULL;
18111 /* We handle only functions for which we still have a body, that return a
18112 supported type and that takes arguments with supported types. Note that
18113 there is no point translating functions that return nothing. */
18114 if (tree_body == NULL_TREE
18115 || DECL_RESULT (fndecl) == NULL_TREE
18116 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18117 return NULL;
18119 for (cursor = DECL_ARGUMENTS (fndecl);
18120 cursor != NULL_TREE;
18121 cursor = TREE_CHAIN (cursor))
18122 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18123 return NULL;
18125 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18126 if (TREE_CODE (tree_body) != RETURN_EXPR)
18127 return NULL;
18128 tree_body = TREE_OPERAND (tree_body, 0);
18129 if (TREE_CODE (tree_body) != MODIFY_EXPR
18130 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18131 return NULL;
18132 tree_body = TREE_OPERAND (tree_body, 1);
18134 /* Try to translate the body expression itself. Note that this will probably
18135 cause an infinite recursion if its call graph has a cycle. This is very
18136 unlikely for size functions, however, so don't bother with such things at
18137 the moment. */
18138 ctx.context_type = NULL_TREE;
18139 ctx.base_decl = NULL_TREE;
18140 ctx.dpi = &dpi;
18141 ctx.placeholder_arg = false;
18142 ctx.placeholder_seen = false;
18143 dpi.fndecl = fndecl;
18144 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18145 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18146 if (!loc_body)
18147 return NULL;
18149 /* After evaluating all operands in "loc_body", we should still have on the
18150 stack all arguments plus the desired function result (top of the stack).
18151 Generate code in order to keep only the result in our stack frame. */
18152 epilogue = NULL;
18153 for (i = 0; i < dpi.args_count; ++i)
18155 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18156 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18157 op_couple->dw_loc_next->dw_loc_next = epilogue;
18158 epilogue = op_couple;
18160 add_loc_descr (&loc_body, epilogue);
18161 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18162 return NULL;
18164 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18165 because they are considered useful. Now there is an epilogue, they are
18166 not anymore, so give it another try. */
18167 loc_descr_without_nops (loc_body);
18169 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18170 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18171 though, given that size functions do not come from source, so they should
18172 not have a dedicated DW_TAG_subprogram DIE. */
18173 dwarf_proc_die
18174 = new_dwarf_proc_die (loc_body, fndecl,
18175 get_context_die (DECL_CONTEXT (fndecl)));
18177 /* The called DWARF procedure consumes one stack slot per argument and
18178 returns one stack slot. */
18179 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18181 return dwarf_proc_die;
18185 /* Generate Dwarf location list representing LOC.
18186 If WANT_ADDRESS is false, expression computing LOC will be computed
18187 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18188 if WANT_ADDRESS is 2, expression computing address useable in location
18189 will be returned (i.e. DW_OP_reg can be used
18190 to refer to register values).
18192 CONTEXT provides information to customize the location descriptions
18193 generation. Its context_type field specifies what type is implicitly
18194 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18195 will not be generated.
18197 Its DPI field determines whether we are generating a DWARF expression for a
18198 DWARF procedure, so PARM_DECL references are processed specifically.
18200 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18201 and dpi fields were null. */
18203 static dw_loc_list_ref
18204 loc_list_from_tree_1 (tree loc, int want_address,
18205 struct loc_descr_context *context)
18207 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18208 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18209 int have_address = 0;
18210 enum dwarf_location_atom op;
18212 /* ??? Most of the time we do not take proper care for sign/zero
18213 extending the values properly. Hopefully this won't be a real
18214 problem... */
18216 if (context != NULL
18217 && context->base_decl == loc
18218 && want_address == 0)
18220 if (dwarf_version >= 3 || !dwarf_strict)
18221 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18222 NULL, 0, NULL, 0, NULL);
18223 else
18224 return NULL;
18227 switch (TREE_CODE (loc))
18229 case ERROR_MARK:
18230 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18231 return 0;
18233 case PLACEHOLDER_EXPR:
18234 /* This case involves extracting fields from an object to determine the
18235 position of other fields. It is supposed to appear only as the first
18236 operand of COMPONENT_REF nodes and to reference precisely the type
18237 that the context allows. */
18238 if (context != NULL
18239 && TREE_TYPE (loc) == context->context_type
18240 && want_address >= 1)
18242 if (dwarf_version >= 3 || !dwarf_strict)
18244 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18245 have_address = 1;
18246 break;
18248 else
18249 return NULL;
18251 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18252 the single argument passed by consumer. */
18253 else if (context != NULL
18254 && context->placeholder_arg
18255 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18256 && want_address == 0)
18258 ret = new_loc_descr (DW_OP_pick, 0, 0);
18259 ret->frame_offset_rel = 1;
18260 context->placeholder_seen = true;
18261 break;
18263 else
18264 expansion_failed (loc, NULL_RTX,
18265 "PLACEHOLDER_EXPR for an unexpected type");
18266 break;
18268 case CALL_EXPR:
18270 const int nargs = call_expr_nargs (loc);
18271 tree callee = get_callee_fndecl (loc);
18272 int i;
18273 dw_die_ref dwarf_proc;
18275 if (callee == NULL_TREE)
18276 goto call_expansion_failed;
18278 /* We handle only functions that return an integer. */
18279 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
18280 goto call_expansion_failed;
18282 dwarf_proc = function_to_dwarf_procedure (callee);
18283 if (dwarf_proc == NULL)
18284 goto call_expansion_failed;
18286 /* Evaluate arguments right-to-left so that the first argument will
18287 be the top-most one on the stack. */
18288 for (i = nargs - 1; i >= 0; --i)
18290 dw_loc_descr_ref loc_descr
18291 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
18292 context);
18294 if (loc_descr == NULL)
18295 goto call_expansion_failed;
18297 add_loc_descr (&ret, loc_descr);
18300 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18301 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18302 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18303 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18304 add_loc_descr (&ret, ret1);
18305 break;
18307 call_expansion_failed:
18308 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
18309 /* There are no opcodes for these operations. */
18310 return 0;
18313 case PREINCREMENT_EXPR:
18314 case PREDECREMENT_EXPR:
18315 case POSTINCREMENT_EXPR:
18316 case POSTDECREMENT_EXPR:
18317 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18318 /* There are no opcodes for these operations. */
18319 return 0;
18321 case ADDR_EXPR:
18322 /* If we already want an address, see if there is INDIRECT_REF inside
18323 e.g. for &this->field. */
18324 if (want_address)
18326 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18327 (loc, want_address == 2, context);
18328 if (list_ret)
18329 have_address = 1;
18330 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18331 && (ret = cst_pool_loc_descr (loc)))
18332 have_address = 1;
18334 /* Otherwise, process the argument and look for the address. */
18335 if (!list_ret && !ret)
18336 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18337 else
18339 if (want_address)
18340 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18341 return NULL;
18343 break;
18345 case VAR_DECL:
18346 if (DECL_THREAD_LOCAL_P (loc))
18348 rtx rtl;
18349 enum dwarf_location_atom tls_op;
18350 enum dtprel_bool dtprel = dtprel_false;
18352 if (targetm.have_tls)
18354 /* If this is not defined, we have no way to emit the
18355 data. */
18356 if (!targetm.asm_out.output_dwarf_dtprel)
18357 return 0;
18359 /* The way DW_OP_GNU_push_tls_address is specified, we
18360 can only look up addresses of objects in the current
18361 module. We used DW_OP_addr as first op, but that's
18362 wrong, because DW_OP_addr is relocated by the debug
18363 info consumer, while DW_OP_GNU_push_tls_address
18364 operand shouldn't be. */
18365 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18366 return 0;
18367 dtprel = dtprel_true;
18368 /* We check for DWARF 5 here because gdb did not implement
18369 DW_OP_form_tls_address until after 7.12. */
18370 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18371 : DW_OP_GNU_push_tls_address);
18373 else
18375 if (!targetm.emutls.debug_form_tls_address
18376 || !(dwarf_version >= 3 || !dwarf_strict))
18377 return 0;
18378 /* We stuffed the control variable into the DECL_VALUE_EXPR
18379 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18380 no longer appear in gimple code. We used the control
18381 variable in specific so that we could pick it up here. */
18382 loc = DECL_VALUE_EXPR (loc);
18383 tls_op = DW_OP_form_tls_address;
18386 rtl = rtl_for_decl_location (loc);
18387 if (rtl == NULL_RTX)
18388 return 0;
18390 if (!MEM_P (rtl))
18391 return 0;
18392 rtl = XEXP (rtl, 0);
18393 if (! CONSTANT_P (rtl))
18394 return 0;
18396 ret = new_addr_loc_descr (rtl, dtprel);
18397 ret1 = new_loc_descr (tls_op, 0, 0);
18398 add_loc_descr (&ret, ret1);
18400 have_address = 1;
18401 break;
18403 /* FALLTHRU */
18405 case PARM_DECL:
18406 if (context != NULL && context->dpi != NULL
18407 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18409 /* We are generating code for a DWARF procedure and we want to access
18410 one of its arguments: find the appropriate argument offset and let
18411 the resolve_args_picking pass compute the offset that complies
18412 with the stack frame size. */
18413 unsigned i = 0;
18414 tree cursor;
18416 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18417 cursor != NULL_TREE && cursor != loc;
18418 cursor = TREE_CHAIN (cursor), ++i)
18420 /* If we are translating a DWARF procedure, all referenced parameters
18421 must belong to the current function. */
18422 gcc_assert (cursor != NULL_TREE);
18424 ret = new_loc_descr (DW_OP_pick, i, 0);
18425 ret->frame_offset_rel = 1;
18426 break;
18428 /* FALLTHRU */
18430 case RESULT_DECL:
18431 if (DECL_HAS_VALUE_EXPR_P (loc))
18432 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
18433 want_address, context);
18434 /* FALLTHRU */
18436 case FUNCTION_DECL:
18438 rtx rtl;
18439 var_loc_list *loc_list = lookup_decl_loc (loc);
18441 if (loc_list && loc_list->first)
18443 list_ret = dw_loc_list (loc_list, loc, want_address);
18444 have_address = want_address != 0;
18445 break;
18447 rtl = rtl_for_decl_location (loc);
18448 if (rtl == NULL_RTX)
18450 if (TREE_CODE (loc) != FUNCTION_DECL
18451 && early_dwarf
18452 && current_function_decl
18453 && want_address != 1
18454 && ! DECL_IGNORED_P (loc)
18455 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18456 || POINTER_TYPE_P (TREE_TYPE (loc)))
18457 && DECL_CONTEXT (loc) == current_function_decl
18458 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18459 <= DWARF2_ADDR_SIZE))
18461 dw_die_ref ref = lookup_decl_die (loc);
18462 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18463 if (ref)
18465 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18466 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18467 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18469 else
18471 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18472 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18474 break;
18476 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18477 return 0;
18479 else if (CONST_INT_P (rtl))
18481 HOST_WIDE_INT val = INTVAL (rtl);
18482 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18483 val &= GET_MODE_MASK (DECL_MODE (loc));
18484 ret = int_loc_descriptor (val);
18486 else if (GET_CODE (rtl) == CONST_STRING)
18488 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18489 return 0;
18491 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18492 ret = new_addr_loc_descr (rtl, dtprel_false);
18493 else
18495 machine_mode mode, mem_mode;
18497 /* Certain constructs can only be represented at top-level. */
18498 if (want_address == 2)
18500 ret = loc_descriptor (rtl, VOIDmode,
18501 VAR_INIT_STATUS_INITIALIZED);
18502 have_address = 1;
18504 else
18506 mode = GET_MODE (rtl);
18507 mem_mode = VOIDmode;
18508 if (MEM_P (rtl))
18510 mem_mode = mode;
18511 mode = get_address_mode (rtl);
18512 rtl = XEXP (rtl, 0);
18513 have_address = 1;
18515 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18516 VAR_INIT_STATUS_INITIALIZED);
18518 if (!ret)
18519 expansion_failed (loc, rtl,
18520 "failed to produce loc descriptor for rtl");
18523 break;
18525 case MEM_REF:
18526 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18528 have_address = 1;
18529 goto do_plus;
18531 /* Fallthru. */
18532 case INDIRECT_REF:
18533 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18534 have_address = 1;
18535 break;
18537 case TARGET_MEM_REF:
18538 case SSA_NAME:
18539 case DEBUG_EXPR_DECL:
18540 return NULL;
18542 case COMPOUND_EXPR:
18543 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18544 context);
18546 CASE_CONVERT:
18547 case VIEW_CONVERT_EXPR:
18548 case SAVE_EXPR:
18549 case MODIFY_EXPR:
18550 case NON_LVALUE_EXPR:
18551 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18552 context);
18554 case COMPONENT_REF:
18555 case BIT_FIELD_REF:
18556 case ARRAY_REF:
18557 case ARRAY_RANGE_REF:
18558 case REALPART_EXPR:
18559 case IMAGPART_EXPR:
18561 tree obj, offset;
18562 poly_int64 bitsize, bitpos, bytepos;
18563 machine_mode mode;
18564 int unsignedp, reversep, volatilep = 0;
18566 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18567 &unsignedp, &reversep, &volatilep);
18569 gcc_assert (obj != loc);
18571 list_ret = loc_list_from_tree_1 (obj,
18572 want_address == 2
18573 && known_eq (bitpos, 0)
18574 && !offset ? 2 : 1,
18575 context);
18576 /* TODO: We can extract value of the small expression via shifting even
18577 for nonzero bitpos. */
18578 if (list_ret == 0)
18579 return 0;
18580 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18581 || !multiple_p (bitsize, BITS_PER_UNIT))
18583 expansion_failed (loc, NULL_RTX,
18584 "bitfield access");
18585 return 0;
18588 if (offset != NULL_TREE)
18590 /* Variable offset. */
18591 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18592 if (list_ret1 == 0)
18593 return 0;
18594 add_loc_list (&list_ret, list_ret1);
18595 if (!list_ret)
18596 return 0;
18597 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18600 HOST_WIDE_INT value;
18601 if (bytepos.is_constant (&value) && value > 0)
18602 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18603 value, 0));
18604 else if (maybe_ne (bytepos, 0))
18605 loc_list_plus_const (list_ret, bytepos);
18607 have_address = 1;
18608 break;
18611 case INTEGER_CST:
18612 if ((want_address || !tree_fits_shwi_p (loc))
18613 && (ret = cst_pool_loc_descr (loc)))
18614 have_address = 1;
18615 else if (want_address == 2
18616 && tree_fits_shwi_p (loc)
18617 && (ret = address_of_int_loc_descriptor
18618 (int_size_in_bytes (TREE_TYPE (loc)),
18619 tree_to_shwi (loc))))
18620 have_address = 1;
18621 else if (tree_fits_shwi_p (loc))
18622 ret = int_loc_descriptor (tree_to_shwi (loc));
18623 else if (tree_fits_uhwi_p (loc))
18624 ret = uint_loc_descriptor (tree_to_uhwi (loc));
18625 else
18627 expansion_failed (loc, NULL_RTX,
18628 "Integer operand is not host integer");
18629 return 0;
18631 break;
18633 case POLY_INT_CST:
18635 if (want_address)
18637 expansion_failed (loc, NULL_RTX,
18638 "constant address with a runtime component");
18639 return 0;
18641 poly_int64 value;
18642 if (!poly_int_tree_p (loc, &value))
18644 expansion_failed (loc, NULL_RTX, "constant too big");
18645 return 0;
18647 ret = int_loc_descriptor (value);
18649 break;
18651 case CONSTRUCTOR:
18652 case REAL_CST:
18653 case STRING_CST:
18654 case COMPLEX_CST:
18655 if ((ret = cst_pool_loc_descr (loc)))
18656 have_address = 1;
18657 else if (TREE_CODE (loc) == CONSTRUCTOR)
18659 tree type = TREE_TYPE (loc);
18660 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
18661 unsigned HOST_WIDE_INT offset = 0;
18662 unsigned HOST_WIDE_INT cnt;
18663 constructor_elt *ce;
18665 if (TREE_CODE (type) == RECORD_TYPE)
18667 /* This is very limited, but it's enough to output
18668 pointers to member functions, as long as the
18669 referenced function is defined in the current
18670 translation unit. */
18671 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
18673 tree val = ce->value;
18675 tree field = ce->index;
18677 if (val)
18678 STRIP_NOPS (val);
18680 if (!field || DECL_BIT_FIELD (field))
18682 expansion_failed (loc, NULL_RTX,
18683 "bitfield in record type constructor");
18684 size = offset = (unsigned HOST_WIDE_INT)-1;
18685 ret = NULL;
18686 break;
18689 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
18690 unsigned HOST_WIDE_INT pos = int_byte_position (field);
18691 gcc_assert (pos + fieldsize <= size);
18692 if (pos < offset)
18694 expansion_failed (loc, NULL_RTX,
18695 "out-of-order fields in record constructor");
18696 size = offset = (unsigned HOST_WIDE_INT)-1;
18697 ret = NULL;
18698 break;
18700 if (pos > offset)
18702 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
18703 add_loc_descr (&ret, ret1);
18704 offset = pos;
18706 if (val && fieldsize != 0)
18708 ret1 = loc_descriptor_from_tree (val, want_address, context);
18709 if (!ret1)
18711 expansion_failed (loc, NULL_RTX,
18712 "unsupported expression in field");
18713 size = offset = (unsigned HOST_WIDE_INT)-1;
18714 ret = NULL;
18715 break;
18717 add_loc_descr (&ret, ret1);
18719 if (fieldsize)
18721 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
18722 add_loc_descr (&ret, ret1);
18723 offset = pos + fieldsize;
18727 if (offset != size)
18729 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
18730 add_loc_descr (&ret, ret1);
18731 offset = size;
18734 have_address = !!want_address;
18736 else
18737 expansion_failed (loc, NULL_RTX,
18738 "constructor of non-record type");
18740 else
18741 /* We can construct small constants here using int_loc_descriptor. */
18742 expansion_failed (loc, NULL_RTX,
18743 "constructor or constant not in constant pool");
18744 break;
18746 case TRUTH_AND_EXPR:
18747 case TRUTH_ANDIF_EXPR:
18748 case BIT_AND_EXPR:
18749 op = DW_OP_and;
18750 goto do_binop;
18752 case TRUTH_XOR_EXPR:
18753 case BIT_XOR_EXPR:
18754 op = DW_OP_xor;
18755 goto do_binop;
18757 case TRUTH_OR_EXPR:
18758 case TRUTH_ORIF_EXPR:
18759 case BIT_IOR_EXPR:
18760 op = DW_OP_or;
18761 goto do_binop;
18763 case FLOOR_DIV_EXPR:
18764 case CEIL_DIV_EXPR:
18765 case ROUND_DIV_EXPR:
18766 case TRUNC_DIV_EXPR:
18767 case EXACT_DIV_EXPR:
18768 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18769 return 0;
18770 op = DW_OP_div;
18771 goto do_binop;
18773 case MINUS_EXPR:
18774 op = DW_OP_minus;
18775 goto do_binop;
18777 case FLOOR_MOD_EXPR:
18778 case CEIL_MOD_EXPR:
18779 case ROUND_MOD_EXPR:
18780 case TRUNC_MOD_EXPR:
18781 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18783 op = DW_OP_mod;
18784 goto do_binop;
18786 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18787 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18788 if (list_ret == 0 || list_ret1 == 0)
18789 return 0;
18791 add_loc_list (&list_ret, list_ret1);
18792 if (list_ret == 0)
18793 return 0;
18794 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18795 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18796 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
18797 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
18798 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
18799 break;
18801 case MULT_EXPR:
18802 op = DW_OP_mul;
18803 goto do_binop;
18805 case LSHIFT_EXPR:
18806 op = DW_OP_shl;
18807 goto do_binop;
18809 case RSHIFT_EXPR:
18810 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
18811 goto do_binop;
18813 case POINTER_PLUS_EXPR:
18814 case PLUS_EXPR:
18815 do_plus:
18816 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
18818 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
18819 smarter to encode their opposite. The DW_OP_plus_uconst operation
18820 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
18821 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
18822 bytes, Y being the size of the operation that pushes the opposite
18823 of the addend. So let's choose the smallest representation. */
18824 const tree tree_addend = TREE_OPERAND (loc, 1);
18825 offset_int wi_addend;
18826 HOST_WIDE_INT shwi_addend;
18827 dw_loc_descr_ref loc_naddend;
18829 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18830 if (list_ret == 0)
18831 return 0;
18833 /* Try to get the literal to push. It is the opposite of the addend,
18834 so as we rely on wrapping during DWARF evaluation, first decode
18835 the literal as a "DWARF-sized" signed number. */
18836 wi_addend = wi::to_offset (tree_addend);
18837 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
18838 shwi_addend = wi_addend.to_shwi ();
18839 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
18840 ? int_loc_descriptor (-shwi_addend)
18841 : NULL;
18843 if (loc_naddend != NULL
18844 && ((unsigned) size_of_uleb128 (shwi_addend)
18845 > size_of_loc_descr (loc_naddend)))
18847 add_loc_descr_to_each (list_ret, loc_naddend);
18848 add_loc_descr_to_each (list_ret,
18849 new_loc_descr (DW_OP_minus, 0, 0));
18851 else
18853 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
18855 loc_naddend = loc_cur;
18856 loc_cur = loc_cur->dw_loc_next;
18857 ggc_free (loc_naddend);
18859 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
18861 break;
18864 op = DW_OP_plus;
18865 goto do_binop;
18867 case LE_EXPR:
18868 op = DW_OP_le;
18869 goto do_comp_binop;
18871 case GE_EXPR:
18872 op = DW_OP_ge;
18873 goto do_comp_binop;
18875 case LT_EXPR:
18876 op = DW_OP_lt;
18877 goto do_comp_binop;
18879 case GT_EXPR:
18880 op = DW_OP_gt;
18881 goto do_comp_binop;
18883 do_comp_binop:
18884 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18886 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18887 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18888 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18889 TREE_CODE (loc));
18890 break;
18892 else
18893 goto do_binop;
18895 case EQ_EXPR:
18896 op = DW_OP_eq;
18897 goto do_binop;
18899 case NE_EXPR:
18900 op = DW_OP_ne;
18901 goto do_binop;
18903 do_binop:
18904 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18905 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18906 if (list_ret == 0 || list_ret1 == 0)
18907 return 0;
18909 add_loc_list (&list_ret, list_ret1);
18910 if (list_ret == 0)
18911 return 0;
18912 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18913 break;
18915 case TRUTH_NOT_EXPR:
18916 case BIT_NOT_EXPR:
18917 op = DW_OP_not;
18918 goto do_unop;
18920 case ABS_EXPR:
18921 op = DW_OP_abs;
18922 goto do_unop;
18924 case NEGATE_EXPR:
18925 op = DW_OP_neg;
18926 goto do_unop;
18928 do_unop:
18929 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18930 if (list_ret == 0)
18931 return 0;
18933 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18934 break;
18936 case MIN_EXPR:
18937 case MAX_EXPR:
18939 const enum tree_code code =
18940 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18942 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18943 build2 (code, integer_type_node,
18944 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18945 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18948 /* fall through */
18950 case COND_EXPR:
18952 dw_loc_descr_ref lhs
18953 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18954 dw_loc_list_ref rhs
18955 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18956 dw_loc_descr_ref bra_node, jump_node, tmp;
18958 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18959 if (list_ret == 0 || lhs == 0 || rhs == 0)
18960 return 0;
18962 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18963 add_loc_descr_to_each (list_ret, bra_node);
18965 add_loc_list (&list_ret, rhs);
18966 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18967 add_loc_descr_to_each (list_ret, jump_node);
18969 add_loc_descr_to_each (list_ret, lhs);
18970 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18971 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18973 /* ??? Need a node to point the skip at. Use a nop. */
18974 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18975 add_loc_descr_to_each (list_ret, tmp);
18976 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18977 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18979 break;
18981 case FIX_TRUNC_EXPR:
18982 return 0;
18984 default:
18985 /* Leave front-end specific codes as simply unknown. This comes
18986 up, for instance, with the C STMT_EXPR. */
18987 if ((unsigned int) TREE_CODE (loc)
18988 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18990 expansion_failed (loc, NULL_RTX,
18991 "language specific tree node");
18992 return 0;
18995 /* Otherwise this is a generic code; we should just lists all of
18996 these explicitly. We forgot one. */
18997 if (flag_checking)
18998 gcc_unreachable ();
19000 /* In a release build, we want to degrade gracefully: better to
19001 generate incomplete debugging information than to crash. */
19002 return NULL;
19005 if (!ret && !list_ret)
19006 return 0;
19008 if (want_address == 2 && !have_address
19009 && (dwarf_version >= 4 || !dwarf_strict))
19011 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19013 expansion_failed (loc, NULL_RTX,
19014 "DWARF address size mismatch");
19015 return 0;
19017 if (ret)
19018 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19019 else
19020 add_loc_descr_to_each (list_ret,
19021 new_loc_descr (DW_OP_stack_value, 0, 0));
19022 have_address = 1;
19024 /* Show if we can't fill the request for an address. */
19025 if (want_address && !have_address)
19027 expansion_failed (loc, NULL_RTX,
19028 "Want address and only have value");
19029 return 0;
19032 gcc_assert (!ret || !list_ret);
19034 /* If we've got an address and don't want one, dereference. */
19035 if (!want_address && have_address)
19037 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19039 if (size > DWARF2_ADDR_SIZE || size == -1)
19041 expansion_failed (loc, NULL_RTX,
19042 "DWARF address size mismatch");
19043 return 0;
19045 else if (size == DWARF2_ADDR_SIZE)
19046 op = DW_OP_deref;
19047 else
19048 op = DW_OP_deref_size;
19050 if (ret)
19051 add_loc_descr (&ret, new_loc_descr (op, size, 0));
19052 else
19053 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
19055 if (ret)
19056 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19058 return list_ret;
19061 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19062 expressions. */
19064 static dw_loc_list_ref
19065 loc_list_from_tree (tree loc, int want_address,
19066 struct loc_descr_context *context)
19068 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19070 for (dw_loc_list_ref loc_cur = result;
19071 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19072 loc_descr_without_nops (loc_cur->expr);
19073 return result;
19076 /* Same as above but return only single location expression. */
19077 static dw_loc_descr_ref
19078 loc_descriptor_from_tree (tree loc, int want_address,
19079 struct loc_descr_context *context)
19081 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19082 if (!ret)
19083 return NULL;
19084 if (ret->dw_loc_next)
19086 expansion_failed (loc, NULL_RTX,
19087 "Location list where only loc descriptor needed");
19088 return NULL;
19090 return ret->expr;
19093 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19094 pointer to the declared type for the relevant field variable, or return
19095 `integer_type_node' if the given node turns out to be an
19096 ERROR_MARK node. */
19098 static inline tree
19099 field_type (const_tree decl)
19101 tree type;
19103 if (TREE_CODE (decl) == ERROR_MARK)
19104 return integer_type_node;
19106 type = DECL_BIT_FIELD_TYPE (decl);
19107 if (type == NULL_TREE)
19108 type = TREE_TYPE (decl);
19110 return type;
19113 /* Given a pointer to a tree node, return the alignment in bits for
19114 it, or else return BITS_PER_WORD if the node actually turns out to
19115 be an ERROR_MARK node. */
19117 static inline unsigned
19118 simple_type_align_in_bits (const_tree type)
19120 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19123 static inline unsigned
19124 simple_decl_align_in_bits (const_tree decl)
19126 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19129 /* Return the result of rounding T up to ALIGN. */
19131 static inline offset_int
19132 round_up_to_align (const offset_int &t, unsigned int align)
19134 return wi::udiv_trunc (t + align - 1, align) * align;
19137 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
19138 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
19139 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
19140 if we fail to return the size in one of these two forms. */
19142 static dw_loc_descr_ref
19143 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
19145 tree tree_size;
19146 struct loc_descr_context ctx;
19148 /* Return a constant integer in priority, if possible. */
19149 *cst_size = int_size_in_bytes (type);
19150 if (*cst_size != -1)
19151 return NULL;
19153 ctx.context_type = const_cast<tree> (type);
19154 ctx.base_decl = NULL_TREE;
19155 ctx.dpi = NULL;
19156 ctx.placeholder_arg = false;
19157 ctx.placeholder_seen = false;
19159 type = TYPE_MAIN_VARIANT (type);
19160 tree_size = TYPE_SIZE_UNIT (type);
19161 return ((tree_size != NULL_TREE)
19162 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
19163 : NULL);
19166 /* Helper structure for RECORD_TYPE processing. */
19167 struct vlr_context
19169 /* Root RECORD_TYPE. It is needed to generate data member location
19170 descriptions in variable-length records (VLR), but also to cope with
19171 variants, which are composed of nested structures multiplexed with
19172 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19173 function processing a FIELD_DECL, it is required to be non null. */
19174 tree struct_type;
19176 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19177 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19178 this variant part as part of the root record (in storage units). For
19179 regular records, it must be NULL_TREE. */
19180 tree variant_part_offset;
19183 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19184 addressed byte of the "containing object" for the given FIELD_DECL. If
19185 possible, return a native constant through CST_OFFSET (in which case NULL is
19186 returned); otherwise return a DWARF expression that computes the offset.
19188 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19189 that offset is, either because the argument turns out to be a pointer to an
19190 ERROR_MARK node, or because the offset expression is too complex for us.
19192 CTX is required: see the comment for VLR_CONTEXT. */
19194 static dw_loc_descr_ref
19195 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19196 HOST_WIDE_INT *cst_offset)
19198 tree tree_result;
19199 dw_loc_list_ref loc_result;
19201 *cst_offset = 0;
19203 if (TREE_CODE (decl) == ERROR_MARK)
19204 return NULL;
19205 else
19206 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19208 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19209 case. */
19210 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19211 return NULL;
19213 /* We used to handle only constant offsets in all cases. Now, we handle
19214 properly dynamic byte offsets only when PCC bitfield type doesn't
19215 matter. */
19216 if (PCC_BITFIELD_TYPE_MATTERS
19217 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19219 offset_int object_offset_in_bits;
19220 offset_int object_offset_in_bytes;
19221 offset_int bitpos_int;
19222 tree type;
19223 tree field_size_tree;
19224 offset_int deepest_bitpos;
19225 offset_int field_size_in_bits;
19226 unsigned int type_align_in_bits;
19227 unsigned int decl_align_in_bits;
19228 offset_int type_size_in_bits;
19230 bitpos_int = wi::to_offset (bit_position (decl));
19231 type = field_type (decl);
19232 type_size_in_bits = offset_int_type_size_in_bits (type);
19233 type_align_in_bits = simple_type_align_in_bits (type);
19235 field_size_tree = DECL_SIZE (decl);
19237 /* The size could be unspecified if there was an error, or for
19238 a flexible array member. */
19239 if (!field_size_tree)
19240 field_size_tree = bitsize_zero_node;
19242 /* If the size of the field is not constant, use the type size. */
19243 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19244 field_size_in_bits = wi::to_offset (field_size_tree);
19245 else
19246 field_size_in_bits = type_size_in_bits;
19248 decl_align_in_bits = simple_decl_align_in_bits (decl);
19250 /* The GCC front-end doesn't make any attempt to keep track of the
19251 starting bit offset (relative to the start of the containing
19252 structure type) of the hypothetical "containing object" for a
19253 bit-field. Thus, when computing the byte offset value for the
19254 start of the "containing object" of a bit-field, we must deduce
19255 this information on our own. This can be rather tricky to do in
19256 some cases. For example, handling the following structure type
19257 definition when compiling for an i386/i486 target (which only
19258 aligns long long's to 32-bit boundaries) can be very tricky:
19260 struct S { int field1; long long field2:31; };
19262 Fortunately, there is a simple rule-of-thumb which can be used
19263 in such cases. When compiling for an i386/i486, GCC will
19264 allocate 8 bytes for the structure shown above. It decides to
19265 do this based upon one simple rule for bit-field allocation.
19266 GCC allocates each "containing object" for each bit-field at
19267 the first (i.e. lowest addressed) legitimate alignment boundary
19268 (based upon the required minimum alignment for the declared
19269 type of the field) which it can possibly use, subject to the
19270 condition that there is still enough available space remaining
19271 in the containing object (when allocated at the selected point)
19272 to fully accommodate all of the bits of the bit-field itself.
19274 This simple rule makes it obvious why GCC allocates 8 bytes for
19275 each object of the structure type shown above. When looking
19276 for a place to allocate the "containing object" for `field2',
19277 the compiler simply tries to allocate a 64-bit "containing
19278 object" at each successive 32-bit boundary (starting at zero)
19279 until it finds a place to allocate that 64- bit field such that
19280 at least 31 contiguous (and previously unallocated) bits remain
19281 within that selected 64 bit field. (As it turns out, for the
19282 example above, the compiler finds it is OK to allocate the
19283 "containing object" 64-bit field at bit-offset zero within the
19284 structure type.)
19286 Here we attempt to work backwards from the limited set of facts
19287 we're given, and we try to deduce from those facts, where GCC
19288 must have believed that the containing object started (within
19289 the structure type). The value we deduce is then used (by the
19290 callers of this routine) to generate DW_AT_location and
19291 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19292 the case of DW_AT_location, regular fields as well). */
19294 /* Figure out the bit-distance from the start of the structure to
19295 the "deepest" bit of the bit-field. */
19296 deepest_bitpos = bitpos_int + field_size_in_bits;
19298 /* This is the tricky part. Use some fancy footwork to deduce
19299 where the lowest addressed bit of the containing object must
19300 be. */
19301 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19303 /* Round up to type_align by default. This works best for
19304 bitfields. */
19305 object_offset_in_bits
19306 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19308 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19310 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19312 /* Round up to decl_align instead. */
19313 object_offset_in_bits
19314 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19317 object_offset_in_bytes
19318 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19319 if (ctx->variant_part_offset == NULL_TREE)
19321 *cst_offset = object_offset_in_bytes.to_shwi ();
19322 return NULL;
19324 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19326 else
19327 tree_result = byte_position (decl);
19329 if (ctx->variant_part_offset != NULL_TREE)
19330 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19331 ctx->variant_part_offset, tree_result);
19333 /* If the byte offset is a constant, it's simplier to handle a native
19334 constant rather than a DWARF expression. */
19335 if (TREE_CODE (tree_result) == INTEGER_CST)
19337 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19338 return NULL;
19340 struct loc_descr_context loc_ctx = {
19341 ctx->struct_type, /* context_type */
19342 NULL_TREE, /* base_decl */
19343 NULL, /* dpi */
19344 false, /* placeholder_arg */
19345 false /* placeholder_seen */
19347 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19349 /* We want a DWARF expression: abort if we only have a location list with
19350 multiple elements. */
19351 if (!loc_result || !single_element_loc_list_p (loc_result))
19352 return NULL;
19353 else
19354 return loc_result->expr;
19357 /* The following routines define various Dwarf attributes and any data
19358 associated with them. */
19360 /* Add a location description attribute value to a DIE.
19362 This emits location attributes suitable for whole variables and
19363 whole parameters. Note that the location attributes for struct fields are
19364 generated by the routine `data_member_location_attribute' below. */
19366 static inline void
19367 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19368 dw_loc_list_ref descr)
19370 bool check_no_locviews = true;
19371 if (descr == 0)
19372 return;
19373 if (single_element_loc_list_p (descr))
19374 add_AT_loc (die, attr_kind, descr->expr);
19375 else
19377 add_AT_loc_list (die, attr_kind, descr);
19378 gcc_assert (descr->ll_symbol);
19379 if (attr_kind == DW_AT_location && descr->vl_symbol
19380 && dwarf2out_locviews_in_attribute ())
19382 add_AT_view_list (die, DW_AT_GNU_locviews);
19383 check_no_locviews = false;
19387 if (check_no_locviews)
19388 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19391 /* Add DW_AT_accessibility attribute to DIE if needed. */
19393 static void
19394 add_accessibility_attribute (dw_die_ref die, tree decl)
19396 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19397 children, otherwise the default is DW_ACCESS_public. In DWARF2
19398 the default has always been DW_ACCESS_public. */
19399 if (TREE_PROTECTED (decl))
19400 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19401 else if (TREE_PRIVATE (decl))
19403 if (dwarf_version == 2
19404 || die->die_parent == NULL
19405 || die->die_parent->die_tag != DW_TAG_class_type)
19406 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19408 else if (dwarf_version > 2
19409 && die->die_parent
19410 && die->die_parent->die_tag == DW_TAG_class_type)
19411 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19414 /* Attach the specialized form of location attribute used for data members of
19415 struct and union types. In the special case of a FIELD_DECL node which
19416 represents a bit-field, the "offset" part of this special location
19417 descriptor must indicate the distance in bytes from the lowest-addressed
19418 byte of the containing struct or union type to the lowest-addressed byte of
19419 the "containing object" for the bit-field. (See the `field_byte_offset'
19420 function above).
19422 For any given bit-field, the "containing object" is a hypothetical object
19423 (of some integral or enum type) within which the given bit-field lives. The
19424 type of this hypothetical "containing object" is always the same as the
19425 declared type of the individual bit-field itself (for GCC anyway... the
19426 DWARF spec doesn't actually mandate this). Note that it is the size (in
19427 bytes) of the hypothetical "containing object" which will be given in the
19428 DW_AT_byte_size attribute for this bit-field. (See the
19429 `byte_size_attribute' function below.) It is also used when calculating the
19430 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19431 function below.)
19433 CTX is required: see the comment for VLR_CONTEXT. */
19435 static void
19436 add_data_member_location_attribute (dw_die_ref die,
19437 tree decl,
19438 struct vlr_context *ctx)
19440 HOST_WIDE_INT offset;
19441 dw_loc_descr_ref loc_descr = 0;
19443 if (TREE_CODE (decl) == TREE_BINFO)
19445 /* We're working on the TAG_inheritance for a base class. */
19446 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19448 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19449 aren't at a fixed offset from all (sub)objects of the same
19450 type. We need to extract the appropriate offset from our
19451 vtable. The following dwarf expression means
19453 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19455 This is specific to the V3 ABI, of course. */
19457 dw_loc_descr_ref tmp;
19459 /* Make a copy of the object address. */
19460 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19461 add_loc_descr (&loc_descr, tmp);
19463 /* Extract the vtable address. */
19464 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19465 add_loc_descr (&loc_descr, tmp);
19467 /* Calculate the address of the offset. */
19468 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19469 gcc_assert (offset < 0);
19471 tmp = int_loc_descriptor (-offset);
19472 add_loc_descr (&loc_descr, tmp);
19473 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19474 add_loc_descr (&loc_descr, tmp);
19476 /* Extract the offset. */
19477 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19478 add_loc_descr (&loc_descr, tmp);
19480 /* Add it to the object address. */
19481 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19482 add_loc_descr (&loc_descr, tmp);
19484 else
19485 offset = tree_to_shwi (BINFO_OFFSET (decl));
19487 else
19489 loc_descr = field_byte_offset (decl, ctx, &offset);
19491 /* If loc_descr is available then we know the field offset is dynamic.
19492 However, GDB does not handle dynamic field offsets very well at the
19493 moment. */
19494 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
19496 loc_descr = NULL;
19497 offset = 0;
19500 /* Data member location evalutation starts with the base address on the
19501 stack. Compute the field offset and add it to this base address. */
19502 else if (loc_descr != NULL)
19503 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19506 if (! loc_descr)
19508 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19509 e.g. GDB only added support to it in November 2016. For DWARF5
19510 we need newer debug info consumers anyway. We might change this
19511 to dwarf_version >= 4 once most consumers catched up. */
19512 if (dwarf_version >= 5
19513 && TREE_CODE (decl) == FIELD_DECL
19514 && DECL_BIT_FIELD_TYPE (decl)
19515 && (ctx->variant_part_offset == NULL_TREE
19516 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
19518 tree off = bit_position (decl);
19519 if (ctx->variant_part_offset)
19520 off = bit_from_pos (ctx->variant_part_offset, off);
19521 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19523 remove_AT (die, DW_AT_byte_size);
19524 remove_AT (die, DW_AT_bit_offset);
19525 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19526 return;
19529 if (dwarf_version > 2)
19531 /* Don't need to output a location expression, just the constant. */
19532 if (offset < 0)
19533 add_AT_int (die, DW_AT_data_member_location, offset);
19534 else
19535 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19536 return;
19538 else
19540 enum dwarf_location_atom op;
19542 /* The DWARF2 standard says that we should assume that the structure
19543 address is already on the stack, so we can specify a structure
19544 field address by using DW_OP_plus_uconst. */
19545 op = DW_OP_plus_uconst;
19546 loc_descr = new_loc_descr (op, offset, 0);
19550 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19553 /* Writes integer values to dw_vec_const array. */
19555 static void
19556 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19558 while (size != 0)
19560 *dest++ = val & 0xff;
19561 val >>= 8;
19562 --size;
19566 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
19568 static HOST_WIDE_INT
19569 extract_int (const unsigned char *src, unsigned int size)
19571 HOST_WIDE_INT val = 0;
19573 src += size;
19574 while (size != 0)
19576 val <<= 8;
19577 val |= *--src & 0xff;
19578 --size;
19580 return val;
19583 /* Writes wide_int values to dw_vec_const array. */
19585 static void
19586 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
19588 int i;
19590 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
19592 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
19593 return;
19596 /* We'd have to extend this code to support odd sizes. */
19597 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
19599 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
19601 if (WORDS_BIG_ENDIAN)
19602 for (i = n - 1; i >= 0; i--)
19604 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19605 dest += sizeof (HOST_WIDE_INT);
19607 else
19608 for (i = 0; i < n; i++)
19610 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19611 dest += sizeof (HOST_WIDE_INT);
19615 /* Writes floating point values to dw_vec_const array. */
19617 static void
19618 insert_float (const_rtx rtl, unsigned char *array)
19620 long val[4];
19621 int i;
19622 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19624 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
19626 /* real_to_target puts 32-bit pieces in each long. Pack them. */
19627 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
19629 insert_int (val[i], 4, array);
19630 array += 4;
19634 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
19635 does not have a "location" either in memory or in a register. These
19636 things can arise in GNU C when a constant is passed as an actual parameter
19637 to an inlined function. They can also arise in C++ where declared
19638 constants do not necessarily get memory "homes". */
19640 static bool
19641 add_const_value_attribute (dw_die_ref die, rtx rtl)
19643 switch (GET_CODE (rtl))
19645 case CONST_INT:
19647 HOST_WIDE_INT val = INTVAL (rtl);
19649 if (val < 0)
19650 add_AT_int (die, DW_AT_const_value, val);
19651 else
19652 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
19654 return true;
19656 case CONST_WIDE_INT:
19658 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
19659 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
19660 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
19661 wide_int w = wi::zext (w1, prec);
19662 add_AT_wide (die, DW_AT_const_value, w);
19664 return true;
19666 case CONST_DOUBLE:
19667 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
19668 floating-point constant. A CONST_DOUBLE is used whenever the
19669 constant requires more than one word in order to be adequately
19670 represented. */
19671 if (TARGET_SUPPORTS_WIDE_INT == 0
19672 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
19673 add_AT_double (die, DW_AT_const_value,
19674 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
19675 else
19677 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19678 unsigned int length = GET_MODE_SIZE (mode);
19679 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
19681 insert_float (rtl, array);
19682 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
19684 return true;
19686 case CONST_VECTOR:
19688 unsigned int length;
19689 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
19690 return false;
19692 machine_mode mode = GET_MODE (rtl);
19693 /* The combination of a length and byte elt_size doesn't extend
19694 naturally to boolean vectors, where several elements are packed
19695 into the same byte. */
19696 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
19697 return false;
19699 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
19700 unsigned char *array
19701 = ggc_vec_alloc<unsigned char> (length * elt_size);
19702 unsigned int i;
19703 unsigned char *p;
19704 machine_mode imode = GET_MODE_INNER (mode);
19706 switch (GET_MODE_CLASS (mode))
19708 case MODE_VECTOR_INT:
19709 for (i = 0, p = array; i < length; i++, p += elt_size)
19711 rtx elt = CONST_VECTOR_ELT (rtl, i);
19712 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
19714 break;
19716 case MODE_VECTOR_FLOAT:
19717 for (i = 0, p = array; i < length; i++, p += elt_size)
19719 rtx elt = CONST_VECTOR_ELT (rtl, i);
19720 insert_float (elt, p);
19722 break;
19724 default:
19725 gcc_unreachable ();
19728 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
19730 return true;
19732 case CONST_STRING:
19733 if (dwarf_version >= 4 || !dwarf_strict)
19735 dw_loc_descr_ref loc_result;
19736 resolve_one_addr (&rtl);
19737 rtl_addr:
19738 loc_result = new_addr_loc_descr (rtl, dtprel_false);
19739 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
19740 add_AT_loc (die, DW_AT_location, loc_result);
19741 vec_safe_push (used_rtx_array, rtl);
19742 return true;
19744 return false;
19746 case CONST:
19747 if (CONSTANT_P (XEXP (rtl, 0)))
19748 return add_const_value_attribute (die, XEXP (rtl, 0));
19749 /* FALLTHROUGH */
19750 case SYMBOL_REF:
19751 if (!const_ok_for_output (rtl))
19752 return false;
19753 /* FALLTHROUGH */
19754 case LABEL_REF:
19755 if (dwarf_version >= 4 || !dwarf_strict)
19756 goto rtl_addr;
19757 return false;
19759 case PLUS:
19760 /* In cases where an inlined instance of an inline function is passed
19761 the address of an `auto' variable (which is local to the caller) we
19762 can get a situation where the DECL_RTL of the artificial local
19763 variable (for the inlining) which acts as a stand-in for the
19764 corresponding formal parameter (of the inline function) will look
19765 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
19766 exactly a compile-time constant expression, but it isn't the address
19767 of the (artificial) local variable either. Rather, it represents the
19768 *value* which the artificial local variable always has during its
19769 lifetime. We currently have no way to represent such quasi-constant
19770 values in Dwarf, so for now we just punt and generate nothing. */
19771 return false;
19773 case HIGH:
19774 case CONST_FIXED:
19775 case MINUS:
19776 case SIGN_EXTEND:
19777 case ZERO_EXTEND:
19778 case CONST_POLY_INT:
19779 return false;
19781 case MEM:
19782 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
19783 && MEM_READONLY_P (rtl)
19784 && GET_MODE (rtl) == BLKmode)
19786 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
19787 return true;
19789 return false;
19791 default:
19792 /* No other kinds of rtx should be possible here. */
19793 gcc_unreachable ();
19795 return false;
19798 /* Determine whether the evaluation of EXPR references any variables
19799 or functions which aren't otherwise used (and therefore may not be
19800 output). */
19801 static tree
19802 reference_to_unused (tree * tp, int * walk_subtrees,
19803 void * data ATTRIBUTE_UNUSED)
19805 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
19806 *walk_subtrees = 0;
19808 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
19809 && ! TREE_ASM_WRITTEN (*tp))
19810 return *tp;
19811 /* ??? The C++ FE emits debug information for using decls, so
19812 putting gcc_unreachable here falls over. See PR31899. For now
19813 be conservative. */
19814 else if (!symtab->global_info_ready && VAR_P (*tp))
19815 return *tp;
19816 else if (VAR_P (*tp))
19818 varpool_node *node = varpool_node::get (*tp);
19819 if (!node || !node->definition)
19820 return *tp;
19822 else if (TREE_CODE (*tp) == FUNCTION_DECL
19823 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
19825 /* The call graph machinery must have finished analyzing,
19826 optimizing and gimplifying the CU by now.
19827 So if *TP has no call graph node associated
19828 to it, it means *TP will not be emitted. */
19829 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
19830 return *tp;
19832 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
19833 return *tp;
19835 return NULL_TREE;
19838 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
19839 for use in a later add_const_value_attribute call. */
19841 static rtx
19842 rtl_for_decl_init (tree init, tree type)
19844 rtx rtl = NULL_RTX;
19846 STRIP_NOPS (init);
19848 /* If a variable is initialized with a string constant without embedded
19849 zeros, build CONST_STRING. */
19850 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
19852 tree enttype = TREE_TYPE (type);
19853 tree domain = TYPE_DOMAIN (type);
19854 scalar_int_mode mode;
19856 if (is_int_mode (TYPE_MODE (enttype), &mode)
19857 && GET_MODE_SIZE (mode) == 1
19858 && domain
19859 && TYPE_MAX_VALUE (domain)
19860 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
19861 && integer_zerop (TYPE_MIN_VALUE (domain))
19862 && compare_tree_int (TYPE_MAX_VALUE (domain),
19863 TREE_STRING_LENGTH (init) - 1) == 0
19864 && ((size_t) TREE_STRING_LENGTH (init)
19865 == strlen (TREE_STRING_POINTER (init)) + 1))
19867 rtl = gen_rtx_CONST_STRING (VOIDmode,
19868 ggc_strdup (TREE_STRING_POINTER (init)));
19869 rtl = gen_rtx_MEM (BLKmode, rtl);
19870 MEM_READONLY_P (rtl) = 1;
19873 /* Other aggregates, and complex values, could be represented using
19874 CONCAT: FIXME! */
19875 else if (AGGREGATE_TYPE_P (type)
19876 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
19877 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
19878 || TREE_CODE (type) == COMPLEX_TYPE)
19880 /* Vectors only work if their mode is supported by the target.
19881 FIXME: generic vectors ought to work too. */
19882 else if (TREE_CODE (type) == VECTOR_TYPE
19883 && !VECTOR_MODE_P (TYPE_MODE (type)))
19885 /* If the initializer is something that we know will expand into an
19886 immediate RTL constant, expand it now. We must be careful not to
19887 reference variables which won't be output. */
19888 else if (initializer_constant_valid_p (init, type)
19889 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19891 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19892 possible. */
19893 if (TREE_CODE (type) == VECTOR_TYPE)
19894 switch (TREE_CODE (init))
19896 case VECTOR_CST:
19897 break;
19898 case CONSTRUCTOR:
19899 if (TREE_CONSTANT (init))
19901 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19902 bool constant_p = true;
19903 tree value;
19904 unsigned HOST_WIDE_INT ix;
19906 /* Even when ctor is constant, it might contain non-*_CST
19907 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19908 belong into VECTOR_CST nodes. */
19909 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19910 if (!CONSTANT_CLASS_P (value))
19912 constant_p = false;
19913 break;
19916 if (constant_p)
19918 init = build_vector_from_ctor (type, elts);
19919 break;
19922 /* FALLTHRU */
19924 default:
19925 return NULL;
19928 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19930 /* If expand_expr returns a MEM, it wasn't immediate. */
19931 gcc_assert (!rtl || !MEM_P (rtl));
19934 return rtl;
19937 /* Generate RTL for the variable DECL to represent its location. */
19939 static rtx
19940 rtl_for_decl_location (tree decl)
19942 rtx rtl;
19944 /* Here we have to decide where we are going to say the parameter "lives"
19945 (as far as the debugger is concerned). We only have a couple of
19946 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19948 DECL_RTL normally indicates where the parameter lives during most of the
19949 activation of the function. If optimization is enabled however, this
19950 could be either NULL or else a pseudo-reg. Both of those cases indicate
19951 that the parameter doesn't really live anywhere (as far as the code
19952 generation parts of GCC are concerned) during most of the function's
19953 activation. That will happen (for example) if the parameter is never
19954 referenced within the function.
19956 We could just generate a location descriptor here for all non-NULL
19957 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19958 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19959 where DECL_RTL is NULL or is a pseudo-reg.
19961 Note however that we can only get away with using DECL_INCOMING_RTL as
19962 a backup substitute for DECL_RTL in certain limited cases. In cases
19963 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19964 we can be sure that the parameter was passed using the same type as it is
19965 declared to have within the function, and that its DECL_INCOMING_RTL
19966 points us to a place where a value of that type is passed.
19968 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19969 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19970 because in these cases DECL_INCOMING_RTL points us to a value of some
19971 type which is *different* from the type of the parameter itself. Thus,
19972 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19973 such cases, the debugger would end up (for example) trying to fetch a
19974 `float' from a place which actually contains the first part of a
19975 `double'. That would lead to really incorrect and confusing
19976 output at debug-time.
19978 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19979 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19980 are a couple of exceptions however. On little-endian machines we can
19981 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19982 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19983 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19984 when (on a little-endian machine) a non-prototyped function has a
19985 parameter declared to be of type `short' or `char'. In such cases,
19986 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19987 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19988 passed `int' value. If the debugger then uses that address to fetch
19989 a `short' or a `char' (on a little-endian machine) the result will be
19990 the correct data, so we allow for such exceptional cases below.
19992 Note that our goal here is to describe the place where the given formal
19993 parameter lives during most of the function's activation (i.e. between the
19994 end of the prologue and the start of the epilogue). We'll do that as best
19995 as we can. Note however that if the given formal parameter is modified
19996 sometime during the execution of the function, then a stack backtrace (at
19997 debug-time) will show the function as having been called with the *new*
19998 value rather than the value which was originally passed in. This happens
19999 rarely enough that it is not a major problem, but it *is* a problem, and
20000 I'd like to fix it.
20002 A future version of dwarf2out.c may generate two additional attributes for
20003 any given DW_TAG_formal_parameter DIE which will describe the "passed
20004 type" and the "passed location" for the given formal parameter in addition
20005 to the attributes we now generate to indicate the "declared type" and the
20006 "active location" for each parameter. This additional set of attributes
20007 could be used by debuggers for stack backtraces. Separately, note that
20008 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20009 This happens (for example) for inlined-instances of inline function formal
20010 parameters which are never referenced. This really shouldn't be
20011 happening. All PARM_DECL nodes should get valid non-NULL
20012 DECL_INCOMING_RTL values. FIXME. */
20014 /* Use DECL_RTL as the "location" unless we find something better. */
20015 rtl = DECL_RTL_IF_SET (decl);
20017 /* When generating abstract instances, ignore everything except
20018 constants, symbols living in memory, and symbols living in
20019 fixed registers. */
20020 if (! reload_completed)
20022 if (rtl
20023 && (CONSTANT_P (rtl)
20024 || (MEM_P (rtl)
20025 && CONSTANT_P (XEXP (rtl, 0)))
20026 || (REG_P (rtl)
20027 && VAR_P (decl)
20028 && TREE_STATIC (decl))))
20030 rtl = targetm.delegitimize_address (rtl);
20031 return rtl;
20033 rtl = NULL_RTX;
20035 else if (TREE_CODE (decl) == PARM_DECL)
20037 if (rtl == NULL_RTX
20038 || is_pseudo_reg (rtl)
20039 || (MEM_P (rtl)
20040 && is_pseudo_reg (XEXP (rtl, 0))
20041 && DECL_INCOMING_RTL (decl)
20042 && MEM_P (DECL_INCOMING_RTL (decl))
20043 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20045 tree declared_type = TREE_TYPE (decl);
20046 tree passed_type = DECL_ARG_TYPE (decl);
20047 machine_mode dmode = TYPE_MODE (declared_type);
20048 machine_mode pmode = TYPE_MODE (passed_type);
20050 /* This decl represents a formal parameter which was optimized out.
20051 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20052 all cases where (rtl == NULL_RTX) just below. */
20053 if (dmode == pmode)
20054 rtl = DECL_INCOMING_RTL (decl);
20055 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20056 && SCALAR_INT_MODE_P (dmode)
20057 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20058 && DECL_INCOMING_RTL (decl))
20060 rtx inc = DECL_INCOMING_RTL (decl);
20061 if (REG_P (inc))
20062 rtl = inc;
20063 else if (MEM_P (inc))
20065 if (BYTES_BIG_ENDIAN)
20066 rtl = adjust_address_nv (inc, dmode,
20067 GET_MODE_SIZE (pmode)
20068 - GET_MODE_SIZE (dmode));
20069 else
20070 rtl = inc;
20075 /* If the parm was passed in registers, but lives on the stack, then
20076 make a big endian correction if the mode of the type of the
20077 parameter is not the same as the mode of the rtl. */
20078 /* ??? This is the same series of checks that are made in dbxout.c before
20079 we reach the big endian correction code there. It isn't clear if all
20080 of these checks are necessary here, but keeping them all is the safe
20081 thing to do. */
20082 else if (MEM_P (rtl)
20083 && XEXP (rtl, 0) != const0_rtx
20084 && ! CONSTANT_P (XEXP (rtl, 0))
20085 /* Not passed in memory. */
20086 && !MEM_P (DECL_INCOMING_RTL (decl))
20087 /* Not passed by invisible reference. */
20088 && (!REG_P (XEXP (rtl, 0))
20089 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20090 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20091 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20092 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20093 #endif
20095 /* Big endian correction check. */
20096 && BYTES_BIG_ENDIAN
20097 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20098 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20099 UNITS_PER_WORD))
20101 machine_mode addr_mode = get_address_mode (rtl);
20102 poly_int64 offset = (UNITS_PER_WORD
20103 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20105 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20106 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20109 else if (VAR_P (decl)
20110 && rtl
20111 && MEM_P (rtl)
20112 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20114 machine_mode addr_mode = get_address_mode (rtl);
20115 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20116 GET_MODE (rtl));
20118 /* If a variable is declared "register" yet is smaller than
20119 a register, then if we store the variable to memory, it
20120 looks like we're storing a register-sized value, when in
20121 fact we are not. We need to adjust the offset of the
20122 storage location to reflect the actual value's bytes,
20123 else gdb will not be able to display it. */
20124 if (maybe_ne (offset, 0))
20125 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20126 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20129 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20130 and will have been substituted directly into all expressions that use it.
20131 C does not have such a concept, but C++ and other languages do. */
20132 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20133 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20135 if (rtl)
20136 rtl = targetm.delegitimize_address (rtl);
20138 /* If we don't look past the constant pool, we risk emitting a
20139 reference to a constant pool entry that isn't referenced from
20140 code, and thus is not emitted. */
20141 if (rtl)
20142 rtl = avoid_constant_pool_reference (rtl);
20144 /* Try harder to get a rtl. If this symbol ends up not being emitted
20145 in the current CU, resolve_addr will remove the expression referencing
20146 it. */
20147 if (rtl == NULL_RTX
20148 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20149 && VAR_P (decl)
20150 && !DECL_EXTERNAL (decl)
20151 && TREE_STATIC (decl)
20152 && DECL_NAME (decl)
20153 && !DECL_HARD_REGISTER (decl)
20154 && DECL_MODE (decl) != VOIDmode)
20156 rtl = make_decl_rtl_for_debug (decl);
20157 if (!MEM_P (rtl)
20158 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20159 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20160 rtl = NULL_RTX;
20163 return rtl;
20166 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20167 returned. If so, the decl for the COMMON block is returned, and the
20168 value is the offset into the common block for the symbol. */
20170 static tree
20171 fortran_common (tree decl, HOST_WIDE_INT *value)
20173 tree val_expr, cvar;
20174 machine_mode mode;
20175 poly_int64 bitsize, bitpos;
20176 tree offset;
20177 HOST_WIDE_INT cbitpos;
20178 int unsignedp, reversep, volatilep = 0;
20180 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20181 it does not have a value (the offset into the common area), or if it
20182 is thread local (as opposed to global) then it isn't common, and shouldn't
20183 be handled as such. */
20184 if (!VAR_P (decl)
20185 || !TREE_STATIC (decl)
20186 || !DECL_HAS_VALUE_EXPR_P (decl)
20187 || !is_fortran ())
20188 return NULL_TREE;
20190 val_expr = DECL_VALUE_EXPR (decl);
20191 if (TREE_CODE (val_expr) != COMPONENT_REF)
20192 return NULL_TREE;
20194 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20195 &unsignedp, &reversep, &volatilep);
20197 if (cvar == NULL_TREE
20198 || !VAR_P (cvar)
20199 || DECL_ARTIFICIAL (cvar)
20200 || !TREE_PUBLIC (cvar)
20201 /* We don't expect to have to cope with variable offsets,
20202 since at present all static data must have a constant size. */
20203 || !bitpos.is_constant (&cbitpos))
20204 return NULL_TREE;
20206 *value = 0;
20207 if (offset != NULL)
20209 if (!tree_fits_shwi_p (offset))
20210 return NULL_TREE;
20211 *value = tree_to_shwi (offset);
20213 if (cbitpos != 0)
20214 *value += cbitpos / BITS_PER_UNIT;
20216 return cvar;
20219 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20220 data attribute for a variable or a parameter. We generate the
20221 DW_AT_const_value attribute only in those cases where the given variable
20222 or parameter does not have a true "location" either in memory or in a
20223 register. This can happen (for example) when a constant is passed as an
20224 actual argument in a call to an inline function. (It's possible that
20225 these things can crop up in other ways also.) Note that one type of
20226 constant value which can be passed into an inlined function is a constant
20227 pointer. This can happen for example if an actual argument in an inlined
20228 function call evaluates to a compile-time constant address.
20230 CACHE_P is true if it is worth caching the location list for DECL,
20231 so that future calls can reuse it rather than regenerate it from scratch.
20232 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20233 since we will need to refer to them each time the function is inlined. */
20235 static bool
20236 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20238 rtx rtl;
20239 dw_loc_list_ref list;
20240 var_loc_list *loc_list;
20241 cached_dw_loc_list *cache;
20243 if (early_dwarf)
20244 return false;
20246 if (TREE_CODE (decl) == ERROR_MARK)
20247 return false;
20249 if (get_AT (die, DW_AT_location)
20250 || get_AT (die, DW_AT_const_value))
20251 return true;
20253 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20254 || TREE_CODE (decl) == RESULT_DECL);
20256 /* Try to get some constant RTL for this decl, and use that as the value of
20257 the location. */
20259 rtl = rtl_for_decl_location (decl);
20260 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20261 && add_const_value_attribute (die, rtl))
20262 return true;
20264 /* See if we have single element location list that is equivalent to
20265 a constant value. That way we are better to use add_const_value_attribute
20266 rather than expanding constant value equivalent. */
20267 loc_list = lookup_decl_loc (decl);
20268 if (loc_list
20269 && loc_list->first
20270 && loc_list->first->next == NULL
20271 && NOTE_P (loc_list->first->loc)
20272 && NOTE_VAR_LOCATION (loc_list->first->loc)
20273 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20275 struct var_loc_node *node;
20277 node = loc_list->first;
20278 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20279 if (GET_CODE (rtl) == EXPR_LIST)
20280 rtl = XEXP (rtl, 0);
20281 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20282 && add_const_value_attribute (die, rtl))
20283 return true;
20285 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20286 list several times. See if we've already cached the contents. */
20287 list = NULL;
20288 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20289 cache_p = false;
20290 if (cache_p)
20292 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20293 if (cache)
20294 list = cache->loc_list;
20296 if (list == NULL)
20298 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20299 NULL);
20300 /* It is usually worth caching this result if the decl is from
20301 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20302 if (cache_p && list && list->dw_loc_next)
20304 cached_dw_loc_list **slot
20305 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20306 DECL_UID (decl),
20307 INSERT);
20308 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20309 cache->decl_id = DECL_UID (decl);
20310 cache->loc_list = list;
20311 *slot = cache;
20314 if (list)
20316 add_AT_location_description (die, DW_AT_location, list);
20317 return true;
20319 /* None of that worked, so it must not really have a location;
20320 try adding a constant value attribute from the DECL_INITIAL. */
20321 return tree_add_const_value_attribute_for_decl (die, decl);
20324 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20325 attribute is the const value T. */
20327 static bool
20328 tree_add_const_value_attribute (dw_die_ref die, tree t)
20330 tree init;
20331 tree type = TREE_TYPE (t);
20332 rtx rtl;
20334 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20335 return false;
20337 init = t;
20338 gcc_assert (!DECL_P (init));
20340 if (TREE_CODE (init) == INTEGER_CST)
20342 if (tree_fits_uhwi_p (init))
20344 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20345 return true;
20347 if (tree_fits_shwi_p (init))
20349 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20350 return true;
20353 /* Generate the RTL even if early_dwarf to force mangling of all refered to
20354 symbols. */
20355 rtl = rtl_for_decl_init (init, type);
20356 if (rtl && !early_dwarf)
20357 return add_const_value_attribute (die, rtl);
20358 /* If the host and target are sane, try harder. */
20359 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20360 && initializer_constant_valid_p (init, type))
20362 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20363 if (size > 0 && (int) size == size)
20365 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20367 if (native_encode_initializer (init, array, size) == size)
20369 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20370 return true;
20372 ggc_free (array);
20375 return false;
20378 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20379 attribute is the const value of T, where T is an integral constant
20380 variable with static storage duration
20381 (so it can't be a PARM_DECL or a RESULT_DECL). */
20383 static bool
20384 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20387 if (!decl
20388 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20389 || (VAR_P (decl) && !TREE_STATIC (decl)))
20390 return false;
20392 if (TREE_READONLY (decl)
20393 && ! TREE_THIS_VOLATILE (decl)
20394 && DECL_INITIAL (decl))
20395 /* OK */;
20396 else
20397 return false;
20399 /* Don't add DW_AT_const_value if abstract origin already has one. */
20400 if (get_AT (var_die, DW_AT_const_value))
20401 return false;
20403 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20406 /* Convert the CFI instructions for the current function into a
20407 location list. This is used for DW_AT_frame_base when we targeting
20408 a dwarf2 consumer that does not support the dwarf3
20409 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20410 expressions. */
20412 static dw_loc_list_ref
20413 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20415 int ix;
20416 dw_fde_ref fde;
20417 dw_loc_list_ref list, *list_tail;
20418 dw_cfi_ref cfi;
20419 dw_cfa_location last_cfa, next_cfa;
20420 const char *start_label, *last_label, *section;
20421 dw_cfa_location remember;
20423 fde = cfun->fde;
20424 gcc_assert (fde != NULL);
20426 section = secname_for_decl (current_function_decl);
20427 list_tail = &list;
20428 list = NULL;
20430 memset (&next_cfa, 0, sizeof (next_cfa));
20431 next_cfa.reg = INVALID_REGNUM;
20432 remember = next_cfa;
20434 start_label = fde->dw_fde_begin;
20436 /* ??? Bald assumption that the CIE opcode list does not contain
20437 advance opcodes. */
20438 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20439 lookup_cfa_1 (cfi, &next_cfa, &remember);
20441 last_cfa = next_cfa;
20442 last_label = start_label;
20444 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20446 /* If the first partition contained no CFI adjustments, the
20447 CIE opcodes apply to the whole first partition. */
20448 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20449 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20450 list_tail =&(*list_tail)->dw_loc_next;
20451 start_label = last_label = fde->dw_fde_second_begin;
20454 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20456 switch (cfi->dw_cfi_opc)
20458 case DW_CFA_set_loc:
20459 case DW_CFA_advance_loc1:
20460 case DW_CFA_advance_loc2:
20461 case DW_CFA_advance_loc4:
20462 if (!cfa_equal_p (&last_cfa, &next_cfa))
20464 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20465 start_label, 0, last_label, 0, section);
20467 list_tail = &(*list_tail)->dw_loc_next;
20468 last_cfa = next_cfa;
20469 start_label = last_label;
20471 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20472 break;
20474 case DW_CFA_advance_loc:
20475 /* The encoding is complex enough that we should never emit this. */
20476 gcc_unreachable ();
20478 default:
20479 lookup_cfa_1 (cfi, &next_cfa, &remember);
20480 break;
20482 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20484 if (!cfa_equal_p (&last_cfa, &next_cfa))
20486 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20487 start_label, 0, last_label, 0, section);
20489 list_tail = &(*list_tail)->dw_loc_next;
20490 last_cfa = next_cfa;
20491 start_label = last_label;
20493 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20494 start_label, 0, fde->dw_fde_end, 0, section);
20495 list_tail = &(*list_tail)->dw_loc_next;
20496 start_label = last_label = fde->dw_fde_second_begin;
20500 if (!cfa_equal_p (&last_cfa, &next_cfa))
20502 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20503 start_label, 0, last_label, 0, section);
20504 list_tail = &(*list_tail)->dw_loc_next;
20505 start_label = last_label;
20508 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20509 start_label, 0,
20510 fde->dw_fde_second_begin
20511 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20512 section);
20514 maybe_gen_llsym (list);
20516 return list;
20519 /* Compute a displacement from the "steady-state frame pointer" to the
20520 frame base (often the same as the CFA), and store it in
20521 frame_pointer_fb_offset. OFFSET is added to the displacement
20522 before the latter is negated. */
20524 static void
20525 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20527 rtx reg, elim;
20529 #ifdef FRAME_POINTER_CFA_OFFSET
20530 reg = frame_pointer_rtx;
20531 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20532 #else
20533 reg = arg_pointer_rtx;
20534 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20535 #endif
20537 elim = (ira_use_lra_p
20538 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20539 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20540 elim = strip_offset_and_add (elim, &offset);
20542 frame_pointer_fb_offset = -offset;
20544 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20545 in which to eliminate. This is because it's stack pointer isn't
20546 directly accessible as a register within the ISA. To work around
20547 this, assume that while we cannot provide a proper value for
20548 frame_pointer_fb_offset, we won't need one either. We can use
20549 hard frame pointer in debug info even if frame pointer isn't used
20550 since hard frame pointer in debug info is encoded with DW_OP_fbreg
20551 which uses the DW_AT_frame_base attribute, not hard frame pointer
20552 directly. */
20553 frame_pointer_fb_offset_valid
20554 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
20557 /* Generate a DW_AT_name attribute given some string value to be included as
20558 the value of the attribute. */
20560 static void
20561 add_name_attribute (dw_die_ref die, const char *name_string)
20563 if (name_string != NULL && *name_string != 0)
20565 if (demangle_name_func)
20566 name_string = (*demangle_name_func) (name_string);
20568 add_AT_string (die, DW_AT_name, name_string);
20572 /* Generate a DW_AT_name attribute given some string value representing a
20573 file or filepath to be included as value of the attribute. */
20574 static void
20575 add_filename_attribute (dw_die_ref die, const char *name_string)
20577 if (name_string != NULL && *name_string != 0)
20578 add_filepath_AT_string (die, DW_AT_name, name_string);
20581 /* Generate a DW_AT_description attribute given some string value to be included
20582 as the value of the attribute. */
20584 static void
20585 add_desc_attribute (dw_die_ref die, const char *name_string)
20587 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20588 return;
20590 if (name_string == NULL || *name_string == 0)
20591 return;
20593 if (demangle_name_func)
20594 name_string = (*demangle_name_func) (name_string);
20596 add_AT_string (die, DW_AT_description, name_string);
20599 /* Generate a DW_AT_description attribute given some decl to be included
20600 as the value of the attribute. */
20602 static void
20603 add_desc_attribute (dw_die_ref die, tree decl)
20605 tree decl_name;
20607 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20608 return;
20610 if (decl == NULL_TREE || !DECL_P (decl))
20611 return;
20612 decl_name = DECL_NAME (decl);
20614 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20616 const char *name = dwarf2_name (decl, 0);
20617 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
20619 else
20621 char *desc = print_generic_expr_to_str (decl);
20622 add_desc_attribute (die, desc);
20623 free (desc);
20627 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
20628 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
20629 of TYPE accordingly.
20631 ??? This is a temporary measure until after we're able to generate
20632 regular DWARF for the complex Ada type system. */
20634 static void
20635 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
20636 dw_die_ref context_die)
20638 tree dtype;
20639 dw_die_ref dtype_die;
20641 if (!lang_hooks.types.descriptive_type)
20642 return;
20644 dtype = lang_hooks.types.descriptive_type (type);
20645 if (!dtype)
20646 return;
20648 dtype_die = lookup_type_die (dtype);
20649 if (!dtype_die)
20651 gen_type_die (dtype, context_die);
20652 dtype_die = lookup_type_die (dtype);
20653 gcc_assert (dtype_die);
20656 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
20659 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
20661 static const char *
20662 comp_dir_string (void)
20664 const char *wd;
20665 char *wd_plus_sep = NULL;
20666 static const char *cached_wd = NULL;
20668 if (cached_wd != NULL)
20669 return cached_wd;
20671 wd = get_src_pwd ();
20672 if (wd == NULL)
20673 return NULL;
20675 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
20677 size_t wdlen = strlen (wd);
20678 wd_plus_sep = XNEWVEC (char, wdlen + 2);
20679 strcpy (wd_plus_sep, wd);
20680 wd_plus_sep [wdlen] = DIR_SEPARATOR;
20681 wd_plus_sep [wdlen + 1] = 0;
20682 wd = wd_plus_sep;
20685 cached_wd = remap_debug_filename (wd);
20687 /* remap_debug_filename can just pass through wd or return a new gc string.
20688 These two types can't be both stored in a GTY(())-tagged string, but since
20689 the cached value lives forever just copy it if needed. */
20690 if (cached_wd != wd)
20692 cached_wd = xstrdup (cached_wd);
20693 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
20694 free (wd_plus_sep);
20697 return cached_wd;
20700 /* Generate a DW_AT_comp_dir attribute for DIE. */
20702 static void
20703 add_comp_dir_attribute (dw_die_ref die)
20705 const char * wd = comp_dir_string ();
20706 if (wd != NULL)
20707 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
20710 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
20711 pointer computation, ...), output a representation for that bound according
20712 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
20713 loc_list_from_tree for the meaning of CONTEXT. */
20715 static void
20716 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
20717 int forms, struct loc_descr_context *context)
20719 dw_die_ref context_die, decl_die = NULL;
20720 dw_loc_list_ref list;
20721 bool strip_conversions = true;
20722 bool placeholder_seen = false;
20724 while (strip_conversions)
20725 switch (TREE_CODE (value))
20727 case ERROR_MARK:
20728 case SAVE_EXPR:
20729 return;
20731 CASE_CONVERT:
20732 case VIEW_CONVERT_EXPR:
20733 value = TREE_OPERAND (value, 0);
20734 break;
20736 default:
20737 strip_conversions = false;
20738 break;
20741 /* If possible and permitted, output the attribute as a constant. */
20742 if ((forms & dw_scalar_form_constant) != 0
20743 && TREE_CODE (value) == INTEGER_CST)
20745 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
20747 /* If HOST_WIDE_INT is big enough then represent the bound as
20748 a constant value. We need to choose a form based on
20749 whether the type is signed or unsigned. We cannot just
20750 call add_AT_unsigned if the value itself is positive
20751 (add_AT_unsigned might add the unsigned value encoded as
20752 DW_FORM_data[1248]). Some DWARF consumers will lookup the
20753 bounds type and then sign extend any unsigned values found
20754 for signed types. This is needed only for
20755 DW_AT_{lower,upper}_bound, since for most other attributes,
20756 consumers will treat DW_FORM_data[1248] as unsigned values,
20757 regardless of the underlying type. */
20758 if (prec <= HOST_BITS_PER_WIDE_INT
20759 || tree_fits_uhwi_p (value))
20761 if (TYPE_UNSIGNED (TREE_TYPE (value)))
20762 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
20763 else
20764 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
20766 else
20767 /* Otherwise represent the bound as an unsigned value with
20768 the precision of its type. The precision and signedness
20769 of the type will be necessary to re-interpret it
20770 unambiguously. */
20771 add_AT_wide (die, attr, wi::to_wide (value));
20772 return;
20775 /* Otherwise, if it's possible and permitted too, output a reference to
20776 another DIE. */
20777 if ((forms & dw_scalar_form_reference) != 0)
20779 tree decl = NULL_TREE;
20781 /* Some type attributes reference an outer type. For instance, the upper
20782 bound of an array may reference an embedding record (this happens in
20783 Ada). */
20784 if (TREE_CODE (value) == COMPONENT_REF
20785 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
20786 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
20787 decl = TREE_OPERAND (value, 1);
20789 else if (VAR_P (value)
20790 || TREE_CODE (value) == PARM_DECL
20791 || TREE_CODE (value) == RESULT_DECL)
20792 decl = value;
20794 if (decl != NULL_TREE)
20796 decl_die = lookup_decl_die (decl);
20798 /* ??? Can this happen, or should the variable have been bound
20799 first? Probably it can, since I imagine that we try to create
20800 the types of parameters in the order in which they exist in
20801 the list, and won't have created a forward reference to a
20802 later parameter. */
20803 if (decl_die != NULL)
20805 if (get_AT (decl_die, DW_AT_location)
20806 || get_AT (decl_die, DW_AT_data_member_location)
20807 || get_AT (decl_die, DW_AT_const_value))
20809 add_AT_die_ref (die, attr, decl_die);
20810 return;
20816 /* Last chance: try to create a stack operation procedure to evaluate the
20817 value. Do nothing if even that is not possible or permitted. */
20818 if ((forms & dw_scalar_form_exprloc) == 0)
20819 return;
20821 list = loc_list_from_tree (value, 2, context);
20822 if (context && context->placeholder_arg)
20824 placeholder_seen = context->placeholder_seen;
20825 context->placeholder_seen = false;
20827 if (list == NULL || single_element_loc_list_p (list))
20829 /* If this attribute is not a reference nor constant, it is
20830 a DWARF expression rather than location description. For that
20831 loc_list_from_tree (value, 0, &context) is needed. */
20832 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20833 if (list2 && single_element_loc_list_p (list2))
20835 if (placeholder_seen)
20837 struct dwarf_procedure_info dpi;
20838 dpi.fndecl = NULL_TREE;
20839 dpi.args_count = 1;
20840 if (!resolve_args_picking (list2->expr, 1, &dpi))
20841 return;
20843 add_AT_loc (die, attr, list2->expr);
20844 return;
20848 /* If that failed to give a single element location list, fall back to
20849 outputting this as a reference... still if permitted. */
20850 if (list == NULL
20851 || (forms & dw_scalar_form_reference) == 0
20852 || placeholder_seen)
20853 return;
20855 if (!decl_die)
20857 if (current_function_decl == 0)
20858 context_die = comp_unit_die ();
20859 else
20860 context_die = lookup_decl_die (current_function_decl);
20862 decl_die = new_die (DW_TAG_variable, context_die, value);
20863 add_AT_flag (decl_die, DW_AT_artificial, 1);
20864 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20865 context_die);
20868 add_AT_location_description (decl_die, DW_AT_location, list);
20869 add_AT_die_ref (die, attr, decl_die);
20872 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20873 default. */
20875 static int
20876 lower_bound_default (void)
20878 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20880 case DW_LANG_C:
20881 case DW_LANG_C89:
20882 case DW_LANG_C99:
20883 case DW_LANG_C11:
20884 case DW_LANG_C_plus_plus:
20885 case DW_LANG_C_plus_plus_11:
20886 case DW_LANG_C_plus_plus_14:
20887 case DW_LANG_ObjC:
20888 case DW_LANG_ObjC_plus_plus:
20889 return 0;
20890 case DW_LANG_Fortran77:
20891 case DW_LANG_Fortran90:
20892 case DW_LANG_Fortran95:
20893 case DW_LANG_Fortran03:
20894 case DW_LANG_Fortran08:
20895 return 1;
20896 case DW_LANG_UPC:
20897 case DW_LANG_D:
20898 case DW_LANG_Python:
20899 return dwarf_version >= 4 ? 0 : -1;
20900 case DW_LANG_Ada95:
20901 case DW_LANG_Ada83:
20902 case DW_LANG_Cobol74:
20903 case DW_LANG_Cobol85:
20904 case DW_LANG_Modula2:
20905 case DW_LANG_PLI:
20906 return dwarf_version >= 4 ? 1 : -1;
20907 default:
20908 return -1;
20912 /* Given a tree node describing an array bound (either lower or upper) output
20913 a representation for that bound. */
20915 static void
20916 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20917 tree bound, struct loc_descr_context *context)
20919 int dflt;
20921 while (1)
20922 switch (TREE_CODE (bound))
20924 /* Strip all conversions. */
20925 CASE_CONVERT:
20926 case VIEW_CONVERT_EXPR:
20927 bound = TREE_OPERAND (bound, 0);
20928 break;
20930 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20931 are even omitted when they are the default. */
20932 case INTEGER_CST:
20933 /* If the value for this bound is the default one, we can even omit the
20934 attribute. */
20935 if (bound_attr == DW_AT_lower_bound
20936 && tree_fits_shwi_p (bound)
20937 && (dflt = lower_bound_default ()) != -1
20938 && tree_to_shwi (bound) == dflt)
20939 return;
20941 /* FALLTHRU */
20943 default:
20944 /* Because of the complex interaction there can be with other GNAT
20945 encodings, GDB isn't ready yet to handle proper DWARF description
20946 for self-referencial subrange bounds: let GNAT encodings do the
20947 magic in such a case. */
20948 if (is_ada ()
20949 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20950 && contains_placeholder_p (bound))
20951 return;
20953 add_scalar_info (subrange_die, bound_attr, bound,
20954 dw_scalar_form_constant
20955 | dw_scalar_form_exprloc
20956 | dw_scalar_form_reference,
20957 context);
20958 return;
20962 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20963 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20964 Note that the block of subscript information for an array type also
20965 includes information about the element type of the given array type.
20967 This function reuses previously set type and bound information if
20968 available. */
20970 static void
20971 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20973 unsigned dimension_number;
20974 tree lower, upper;
20975 dw_die_ref child = type_die->die_child;
20977 for (dimension_number = 0;
20978 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20979 type = TREE_TYPE (type), dimension_number++)
20981 tree domain = TYPE_DOMAIN (type);
20983 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20984 break;
20986 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20987 and (in GNU C only) variable bounds. Handle all three forms
20988 here. */
20990 /* Find and reuse a previously generated DW_TAG_subrange_type if
20991 available.
20993 For multi-dimensional arrays, as we iterate through the
20994 various dimensions in the enclosing for loop above, we also
20995 iterate through the DIE children and pick at each
20996 DW_TAG_subrange_type previously generated (if available).
20997 Each child DW_TAG_subrange_type DIE describes the range of
20998 the current dimension. At this point we should have as many
20999 DW_TAG_subrange_type's as we have dimensions in the
21000 array. */
21001 dw_die_ref subrange_die = NULL;
21002 if (child)
21003 while (1)
21005 child = child->die_sib;
21006 if (child->die_tag == DW_TAG_subrange_type)
21007 subrange_die = child;
21008 if (child == type_die->die_child)
21010 /* If we wrapped around, stop looking next time. */
21011 child = NULL;
21012 break;
21014 if (child->die_tag == DW_TAG_subrange_type)
21015 break;
21017 if (!subrange_die)
21018 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21020 if (domain)
21022 /* We have an array type with specified bounds. */
21023 lower = TYPE_MIN_VALUE (domain);
21024 upper = TYPE_MAX_VALUE (domain);
21026 /* Define the index type. */
21027 if (TREE_TYPE (domain)
21028 && !get_AT (subrange_die, DW_AT_type))
21030 /* ??? This is probably an Ada unnamed subrange type. Ignore the
21031 TREE_TYPE field. We can't emit debug info for this
21032 because it is an unnamed integral type. */
21033 if (TREE_CODE (domain) == INTEGER_TYPE
21034 && TYPE_NAME (domain) == NULL_TREE
21035 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
21036 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
21038 else
21039 add_type_attribute (subrange_die, TREE_TYPE (domain),
21040 TYPE_UNQUALIFIED, false, type_die);
21043 /* ??? If upper is NULL, the array has unspecified length,
21044 but it does have a lower bound. This happens with Fortran
21045 dimension arr(N:*)
21046 Since the debugger is definitely going to need to know N
21047 to produce useful results, go ahead and output the lower
21048 bound solo, and hope the debugger can cope. */
21050 if (!get_AT (subrange_die, DW_AT_lower_bound))
21051 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21052 if (!get_AT (subrange_die, DW_AT_upper_bound)
21053 && !get_AT (subrange_die, DW_AT_count))
21055 if (upper)
21056 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21057 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21058 /* Zero-length array. */
21059 add_bound_info (subrange_die, DW_AT_count,
21060 build_int_cst (TREE_TYPE (lower), 0), NULL);
21064 /* Otherwise we have an array type with an unspecified length. The
21065 DWARF-2 spec does not say how to handle this; let's just leave out the
21066 bounds. */
21070 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21072 static void
21073 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21075 dw_die_ref decl_die;
21076 HOST_WIDE_INT size;
21077 dw_loc_descr_ref size_expr = NULL;
21079 switch (TREE_CODE (tree_node))
21081 case ERROR_MARK:
21082 size = 0;
21083 break;
21084 case ENUMERAL_TYPE:
21085 case RECORD_TYPE:
21086 case UNION_TYPE:
21087 case QUAL_UNION_TYPE:
21088 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21089 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21091 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21092 return;
21094 size_expr = type_byte_size (tree_node, &size);
21095 break;
21096 case FIELD_DECL:
21097 /* For a data member of a struct or union, the DW_AT_byte_size is
21098 generally given as the number of bytes normally allocated for an
21099 object of the *declared* type of the member itself. This is true
21100 even for bit-fields. */
21101 size = int_size_in_bytes (field_type (tree_node));
21102 break;
21103 default:
21104 gcc_unreachable ();
21107 /* Support for dynamically-sized objects was introduced by DWARFv3.
21108 At the moment, GDB does not handle variable byte sizes very well,
21109 though. */
21110 if ((dwarf_version >= 3 || !dwarf_strict)
21111 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
21112 && size_expr != NULL)
21113 add_AT_loc (die, DW_AT_byte_size, size_expr);
21115 /* Note that `size' might be -1 when we get to this point. If it is, that
21116 indicates that the byte size of the entity in question is variable and
21117 that we could not generate a DWARF expression that computes it. */
21118 if (size >= 0)
21119 add_AT_unsigned (die, DW_AT_byte_size, size);
21122 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21123 alignment. */
21125 static void
21126 add_alignment_attribute (dw_die_ref die, tree tree_node)
21128 if (dwarf_version < 5 && dwarf_strict)
21129 return;
21131 unsigned align;
21133 if (DECL_P (tree_node))
21135 if (!DECL_USER_ALIGN (tree_node))
21136 return;
21138 align = DECL_ALIGN_UNIT (tree_node);
21140 else if (TYPE_P (tree_node))
21142 if (!TYPE_USER_ALIGN (tree_node))
21143 return;
21145 align = TYPE_ALIGN_UNIT (tree_node);
21147 else
21148 gcc_unreachable ();
21150 add_AT_unsigned (die, DW_AT_alignment, align);
21153 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21154 which specifies the distance in bits from the highest order bit of the
21155 "containing object" for the bit-field to the highest order bit of the
21156 bit-field itself.
21158 For any given bit-field, the "containing object" is a hypothetical object
21159 (of some integral or enum type) within which the given bit-field lives. The
21160 type of this hypothetical "containing object" is always the same as the
21161 declared type of the individual bit-field itself. The determination of the
21162 exact location of the "containing object" for a bit-field is rather
21163 complicated. It's handled by the `field_byte_offset' function (above).
21165 Note that it is the size (in bytes) of the hypothetical "containing object"
21166 which will be given in the DW_AT_byte_size attribute for this bit-field.
21167 (See `byte_size_attribute' above). */
21169 static inline void
21170 add_bit_offset_attribute (dw_die_ref die, tree decl)
21172 HOST_WIDE_INT object_offset_in_bytes;
21173 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21174 HOST_WIDE_INT bitpos_int;
21175 HOST_WIDE_INT highest_order_object_bit_offset;
21176 HOST_WIDE_INT highest_order_field_bit_offset;
21177 HOST_WIDE_INT bit_offset;
21179 /* The containing object is within the DECL_CONTEXT. */
21180 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21182 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21184 /* Must be a field and a bit field. */
21185 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21187 /* We can't yet handle bit-fields whose offsets are variable, so if we
21188 encounter such things, just return without generating any attribute
21189 whatsoever. Likewise for variable or too large size. */
21190 if (! tree_fits_shwi_p (bit_position (decl))
21191 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21192 return;
21194 bitpos_int = int_bit_position (decl);
21196 /* Note that the bit offset is always the distance (in bits) from the
21197 highest-order bit of the "containing object" to the highest-order bit of
21198 the bit-field itself. Since the "high-order end" of any object or field
21199 is different on big-endian and little-endian machines, the computation
21200 below must take account of these differences. */
21201 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21202 highest_order_field_bit_offset = bitpos_int;
21204 if (! BYTES_BIG_ENDIAN)
21206 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21207 highest_order_object_bit_offset +=
21208 simple_type_size_in_bits (original_type);
21211 bit_offset
21212 = (! BYTES_BIG_ENDIAN
21213 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21214 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21216 if (bit_offset < 0)
21217 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21218 else
21219 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21222 /* For a FIELD_DECL node which represents a bit field, output an attribute
21223 which specifies the length in bits of the given field. */
21225 static inline void
21226 add_bit_size_attribute (dw_die_ref die, tree decl)
21228 /* Must be a field and a bit field. */
21229 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21230 && DECL_BIT_FIELD_TYPE (decl));
21232 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21233 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21236 /* If the compiled language is ANSI C, then add a 'prototyped'
21237 attribute, if arg types are given for the parameters of a function. */
21239 static inline void
21240 add_prototyped_attribute (dw_die_ref die, tree func_type)
21242 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21244 case DW_LANG_C:
21245 case DW_LANG_C89:
21246 case DW_LANG_C99:
21247 case DW_LANG_C11:
21248 case DW_LANG_ObjC:
21249 if (prototype_p (func_type))
21250 add_AT_flag (die, DW_AT_prototyped, 1);
21251 break;
21252 default:
21253 break;
21257 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21258 by looking in the type declaration, the object declaration equate table or
21259 the block mapping. */
21261 static inline void
21262 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21264 dw_die_ref origin_die = NULL;
21266 /* For late LTO debug output we want to refer directly to the abstract
21267 DIE in the early debug rather to the possibly existing concrete
21268 instance and avoid creating that just for this purpose. */
21269 sym_off_pair *desc;
21270 if (in_lto_p
21271 && external_die_map
21272 && (desc = external_die_map->get (origin)))
21274 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21275 desc->sym, desc->off);
21276 return;
21279 if (DECL_P (origin))
21280 origin_die = lookup_decl_die (origin);
21281 else if (TYPE_P (origin))
21282 origin_die = lookup_type_die (origin);
21283 else if (TREE_CODE (origin) == BLOCK)
21284 origin_die = lookup_block_die (origin);
21286 /* XXX: Functions that are never lowered don't always have correct block
21287 trees (in the case of java, they simply have no block tree, in some other
21288 languages). For these functions, there is nothing we can really do to
21289 output correct debug info for inlined functions in all cases. Rather
21290 than die, we'll just produce deficient debug info now, in that we will
21291 have variables without a proper abstract origin. In the future, when all
21292 functions are lowered, we should re-add a gcc_assert (origin_die)
21293 here. */
21295 if (origin_die)
21297 dw_attr_node *a;
21298 /* Like above, if we already created a concrete instance DIE
21299 do not use that for the abstract origin but the early DIE
21300 if present. */
21301 if (in_lto_p
21302 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
21303 origin_die = AT_ref (a);
21304 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21308 /* We do not currently support the pure_virtual attribute. */
21310 static inline void
21311 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21313 if (DECL_VINDEX (func_decl))
21315 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21317 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21318 add_AT_loc (die, DW_AT_vtable_elem_location,
21319 new_loc_descr (DW_OP_constu,
21320 tree_to_shwi (DECL_VINDEX (func_decl)),
21321 0));
21323 /* GNU extension: Record what type this method came from originally. */
21324 if (debug_info_level > DINFO_LEVEL_TERSE
21325 && DECL_CONTEXT (func_decl))
21326 add_AT_die_ref (die, DW_AT_containing_type,
21327 lookup_type_die (DECL_CONTEXT (func_decl)));
21331 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21332 given decl. This used to be a vendor extension until after DWARF 4
21333 standardized it. */
21335 static void
21336 add_linkage_attr (dw_die_ref die, tree decl)
21338 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21340 /* Mimic what assemble_name_raw does with a leading '*'. */
21341 if (name[0] == '*')
21342 name = &name[1];
21344 if (dwarf_version >= 4)
21345 add_AT_string (die, DW_AT_linkage_name, name);
21346 else
21347 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21350 /* Add source coordinate attributes for the given decl. */
21352 static void
21353 add_src_coords_attributes (dw_die_ref die, tree decl)
21355 expanded_location s;
21357 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21358 return;
21359 s = expand_location (DECL_SOURCE_LOCATION (decl));
21360 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21361 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21362 if (debug_column_info && s.column)
21363 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21366 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21368 static void
21369 add_linkage_name_raw (dw_die_ref die, tree decl)
21371 /* Defer until we have an assembler name set. */
21372 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21374 limbo_die_node *asm_name;
21376 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21377 asm_name->die = die;
21378 asm_name->created_for = decl;
21379 asm_name->next = deferred_asm_name;
21380 deferred_asm_name = asm_name;
21382 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21383 add_linkage_attr (die, decl);
21386 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21388 static void
21389 add_linkage_name (dw_die_ref die, tree decl)
21391 if (debug_info_level > DINFO_LEVEL_NONE
21392 && VAR_OR_FUNCTION_DECL_P (decl)
21393 && TREE_PUBLIC (decl)
21394 && !(VAR_P (decl) && DECL_REGISTER (decl))
21395 && die->die_tag != DW_TAG_member)
21396 add_linkage_name_raw (die, decl);
21399 /* Add a DW_AT_name attribute and source coordinate attribute for the
21400 given decl, but only if it actually has a name. */
21402 static void
21403 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21404 bool no_linkage_name)
21406 tree decl_name;
21408 decl_name = DECL_NAME (decl);
21409 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21411 const char *name = dwarf2_name (decl, 0);
21412 if (name)
21413 add_name_attribute (die, name);
21414 else
21415 add_desc_attribute (die, decl);
21417 if (! DECL_ARTIFICIAL (decl))
21418 add_src_coords_attributes (die, decl);
21420 if (!no_linkage_name)
21421 add_linkage_name (die, decl);
21423 else
21424 add_desc_attribute (die, decl);
21426 #ifdef VMS_DEBUGGING_INFO
21427 /* Get the function's name, as described by its RTL. This may be different
21428 from the DECL_NAME name used in the source file. */
21429 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21431 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21432 XEXP (DECL_RTL (decl), 0), false);
21433 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21435 #endif /* VMS_DEBUGGING_INFO */
21438 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21440 static void
21441 add_discr_value (dw_die_ref die, dw_discr_value *value)
21443 dw_attr_node attr;
21445 attr.dw_attr = DW_AT_discr_value;
21446 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21447 attr.dw_attr_val.val_entry = NULL;
21448 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21449 if (value->pos)
21450 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21451 else
21452 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21453 add_dwarf_attr (die, &attr);
21456 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21458 static void
21459 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21461 dw_attr_node attr;
21463 attr.dw_attr = DW_AT_discr_list;
21464 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21465 attr.dw_attr_val.val_entry = NULL;
21466 attr.dw_attr_val.v.val_discr_list = discr_list;
21467 add_dwarf_attr (die, &attr);
21470 static inline dw_discr_list_ref
21471 AT_discr_list (dw_attr_node *attr)
21473 return attr->dw_attr_val.v.val_discr_list;
21476 #ifdef VMS_DEBUGGING_INFO
21477 /* Output the debug main pointer die for VMS */
21479 void
21480 dwarf2out_vms_debug_main_pointer (void)
21482 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21483 dw_die_ref die;
21485 /* Allocate the VMS debug main subprogram die. */
21486 die = new_die_raw (DW_TAG_subprogram);
21487 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21488 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21489 current_function_funcdef_no);
21490 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21492 /* Make it the first child of comp_unit_die (). */
21493 die->die_parent = comp_unit_die ();
21494 if (comp_unit_die ()->die_child)
21496 die->die_sib = comp_unit_die ()->die_child->die_sib;
21497 comp_unit_die ()->die_child->die_sib = die;
21499 else
21501 die->die_sib = die;
21502 comp_unit_die ()->die_child = die;
21505 #endif /* VMS_DEBUGGING_INFO */
21507 /* walk_tree helper function for uses_local_type, below. */
21509 static tree
21510 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21512 if (!TYPE_P (*tp))
21513 *walk_subtrees = 0;
21514 else
21516 tree name = TYPE_NAME (*tp);
21517 if (name && DECL_P (name) && decl_function_context (name))
21518 return *tp;
21520 return NULL_TREE;
21523 /* If TYPE involves a function-local type (including a local typedef to a
21524 non-local type), returns that type; otherwise returns NULL_TREE. */
21526 static tree
21527 uses_local_type (tree type)
21529 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21530 return used;
21533 /* Return the DIE for the scope that immediately contains this type.
21534 Non-named types that do not involve a function-local type get global
21535 scope. Named types nested in namespaces or other types get their
21536 containing scope. All other types (i.e. function-local named types) get
21537 the current active scope. */
21539 static dw_die_ref
21540 scope_die_for (tree t, dw_die_ref context_die)
21542 dw_die_ref scope_die = NULL;
21543 tree containing_scope;
21545 /* Non-types always go in the current scope. */
21546 gcc_assert (TYPE_P (t));
21548 /* Use the scope of the typedef, rather than the scope of the type
21549 it refers to. */
21550 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
21551 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
21552 else
21553 containing_scope = TYPE_CONTEXT (t);
21555 /* Use the containing namespace if there is one. */
21556 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
21558 if (context_die == lookup_decl_die (containing_scope))
21559 /* OK */;
21560 else if (debug_info_level > DINFO_LEVEL_TERSE)
21561 context_die = get_context_die (containing_scope);
21562 else
21563 containing_scope = NULL_TREE;
21566 /* Ignore function type "scopes" from the C frontend. They mean that
21567 a tagged type is local to a parmlist of a function declarator, but
21568 that isn't useful to DWARF. */
21569 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
21570 containing_scope = NULL_TREE;
21572 if (SCOPE_FILE_SCOPE_P (containing_scope))
21574 /* If T uses a local type keep it local as well, to avoid references
21575 to function-local DIEs from outside the function. */
21576 if (current_function_decl && uses_local_type (t))
21577 scope_die = context_die;
21578 else
21579 scope_die = comp_unit_die ();
21581 else if (TYPE_P (containing_scope))
21583 /* For types, we can just look up the appropriate DIE. */
21584 if (debug_info_level > DINFO_LEVEL_TERSE)
21585 scope_die = get_context_die (containing_scope);
21586 else
21588 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
21589 if (scope_die == NULL)
21590 scope_die = comp_unit_die ();
21593 else
21594 scope_die = context_die;
21596 return scope_die;
21599 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
21601 static inline int
21602 local_scope_p (dw_die_ref context_die)
21604 for (; context_die; context_die = context_die->die_parent)
21605 if (context_die->die_tag == DW_TAG_inlined_subroutine
21606 || context_die->die_tag == DW_TAG_subprogram)
21607 return 1;
21609 return 0;
21612 /* Returns nonzero if CONTEXT_DIE is a class. */
21614 static inline int
21615 class_scope_p (dw_die_ref context_die)
21617 return (context_die
21618 && (context_die->die_tag == DW_TAG_structure_type
21619 || context_die->die_tag == DW_TAG_class_type
21620 || context_die->die_tag == DW_TAG_interface_type
21621 || context_die->die_tag == DW_TAG_union_type));
21624 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
21625 whether or not to treat a DIE in this context as a declaration. */
21627 static inline int
21628 class_or_namespace_scope_p (dw_die_ref context_die)
21630 return (class_scope_p (context_die)
21631 || (context_die && context_die->die_tag == DW_TAG_namespace));
21634 /* Many forms of DIEs require a "type description" attribute. This
21635 routine locates the proper "type descriptor" die for the type given
21636 by 'type' plus any additional qualifiers given by 'cv_quals', and
21637 adds a DW_AT_type attribute below the given die. */
21639 static void
21640 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
21641 bool reverse, dw_die_ref context_die)
21643 enum tree_code code = TREE_CODE (type);
21644 dw_die_ref type_die = NULL;
21646 if (debug_info_level <= DINFO_LEVEL_TERSE)
21647 return;
21649 /* ??? If this type is an unnamed subrange type of an integral, floating-point
21650 or fixed-point type, use the inner type. This is because we have no
21651 support for unnamed types in base_type_die. This can happen if this is
21652 an Ada subrange type. Correct solution is emit a subrange type die. */
21653 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
21654 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
21655 type = TREE_TYPE (type), code = TREE_CODE (type);
21657 if (code == ERROR_MARK
21658 /* Handle a special case. For functions whose return type is void, we
21659 generate *no* type attribute. (Note that no object may have type
21660 `void', so this only applies to function return types). */
21661 || code == VOID_TYPE)
21662 return;
21664 type_die = modified_type_die (type,
21665 cv_quals | TYPE_QUALS (type),
21666 reverse,
21667 context_die);
21669 if (type_die != NULL)
21670 add_AT_die_ref (object_die, DW_AT_type, type_die);
21673 /* Given an object die, add the calling convention attribute for the
21674 function call type. */
21675 static void
21676 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
21678 enum dwarf_calling_convention value = DW_CC_normal;
21680 value = ((enum dwarf_calling_convention)
21681 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
21683 if (is_fortran ()
21684 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
21686 /* DWARF 2 doesn't provide a way to identify a program's source-level
21687 entry point. DW_AT_calling_convention attributes are only meant
21688 to describe functions' calling conventions. However, lacking a
21689 better way to signal the Fortran main program, we used this for
21690 a long time, following existing custom. Now, DWARF 4 has
21691 DW_AT_main_subprogram, which we add below, but some tools still
21692 rely on the old way, which we thus keep. */
21693 value = DW_CC_program;
21695 if (dwarf_version >= 4 || !dwarf_strict)
21696 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
21699 /* Only add the attribute if the backend requests it, and
21700 is not DW_CC_normal. */
21701 if (value && (value != DW_CC_normal))
21702 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
21705 /* Given a tree pointer to a struct, class, union, or enum type node, return
21706 a pointer to the (string) tag name for the given type, or zero if the type
21707 was declared without a tag. */
21709 static const char *
21710 type_tag (const_tree type)
21712 const char *name = 0;
21714 if (TYPE_NAME (type) != 0)
21716 tree t = 0;
21718 /* Find the IDENTIFIER_NODE for the type name. */
21719 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
21720 && !TYPE_NAMELESS (type))
21721 t = TYPE_NAME (type);
21723 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
21724 a TYPE_DECL node, regardless of whether or not a `typedef' was
21725 involved. */
21726 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21727 && ! DECL_IGNORED_P (TYPE_NAME (type)))
21729 /* We want to be extra verbose. Don't call dwarf_name if
21730 DECL_NAME isn't set. The default hook for decl_printable_name
21731 doesn't like that, and in this context it's correct to return
21732 0, instead of "<anonymous>" or the like. */
21733 if (DECL_NAME (TYPE_NAME (type))
21734 && !DECL_NAMELESS (TYPE_NAME (type)))
21735 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
21738 /* Now get the name as a string, or invent one. */
21739 if (!name && t != 0)
21740 name = IDENTIFIER_POINTER (t);
21743 return (name == 0 || *name == '\0') ? 0 : name;
21746 /* Return the type associated with a data member, make a special check
21747 for bit field types. */
21749 static inline tree
21750 member_declared_type (const_tree member)
21752 return (DECL_BIT_FIELD_TYPE (member)
21753 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
21756 /* Get the decl's label, as described by its RTL. This may be different
21757 from the DECL_NAME name used in the source file. */
21759 #if 0
21760 static const char *
21761 decl_start_label (tree decl)
21763 rtx x;
21764 const char *fnname;
21766 x = DECL_RTL (decl);
21767 gcc_assert (MEM_P (x));
21769 x = XEXP (x, 0);
21770 gcc_assert (GET_CODE (x) == SYMBOL_REF);
21772 fnname = XSTR (x, 0);
21773 return fnname;
21775 #endif
21777 /* For variable-length arrays that have been previously generated, but
21778 may be incomplete due to missing subscript info, fill the subscript
21779 info. Return TRUE if this is one of those cases. */
21780 static bool
21781 fill_variable_array_bounds (tree type)
21783 if (TREE_ASM_WRITTEN (type)
21784 && TREE_CODE (type) == ARRAY_TYPE
21785 && variably_modified_type_p (type, NULL))
21787 dw_die_ref array_die = lookup_type_die (type);
21788 if (!array_die)
21789 return false;
21790 add_subscript_info (array_die, type, !is_ada ());
21791 return true;
21793 return false;
21796 /* These routines generate the internal representation of the DIE's for
21797 the compilation unit. Debugging information is collected by walking
21798 the declaration trees passed in from dwarf2out_decl(). */
21800 static void
21801 gen_array_type_die (tree type, dw_die_ref context_die)
21803 dw_die_ref array_die;
21805 /* GNU compilers represent multidimensional array types as sequences of one
21806 dimensional array types whose element types are themselves array types.
21807 We sometimes squish that down to a single array_type DIE with multiple
21808 subscripts in the Dwarf debugging info. The draft Dwarf specification
21809 say that we are allowed to do this kind of compression in C, because
21810 there is no difference between an array of arrays and a multidimensional
21811 array. We don't do this for Ada to remain as close as possible to the
21812 actual representation, which is especially important against the language
21813 flexibilty wrt arrays of variable size. */
21815 bool collapse_nested_arrays = !is_ada ();
21817 if (fill_variable_array_bounds (type))
21818 return;
21820 dw_die_ref scope_die = scope_die_for (type, context_die);
21821 tree element_type;
21823 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
21824 DW_TAG_string_type doesn't have DW_AT_type attribute). */
21825 if (TREE_CODE (type) == ARRAY_TYPE
21826 && TYPE_STRING_FLAG (type)
21827 && is_fortran ()
21828 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21830 HOST_WIDE_INT size;
21832 array_die = new_die (DW_TAG_string_type, scope_die, type);
21833 add_name_attribute (array_die, type_tag (type));
21834 equate_type_number_to_die (type, array_die);
21835 size = int_size_in_bytes (type);
21836 if (size >= 0)
21837 add_AT_unsigned (array_die, DW_AT_byte_size, size);
21838 /* ??? We can't annotate types late, but for LTO we may not
21839 generate a location early either (gfortran.dg/save_6.f90). */
21840 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
21841 && TYPE_DOMAIN (type) != NULL_TREE
21842 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21844 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21845 tree rszdecl = szdecl;
21847 size = int_size_in_bytes (TREE_TYPE (szdecl));
21848 if (!DECL_P (szdecl))
21850 if (TREE_CODE (szdecl) == INDIRECT_REF
21851 && DECL_P (TREE_OPERAND (szdecl, 0)))
21853 rszdecl = TREE_OPERAND (szdecl, 0);
21854 if (int_size_in_bytes (TREE_TYPE (rszdecl))
21855 != DWARF2_ADDR_SIZE)
21856 size = 0;
21858 else
21859 size = 0;
21861 if (size > 0)
21863 dw_loc_list_ref loc
21864 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21865 NULL);
21866 if (loc)
21868 add_AT_location_description (array_die, DW_AT_string_length,
21869 loc);
21870 if (size != DWARF2_ADDR_SIZE)
21871 add_AT_unsigned (array_die, dwarf_version >= 5
21872 ? DW_AT_string_length_byte_size
21873 : DW_AT_byte_size, size);
21877 return;
21880 array_die = new_die (DW_TAG_array_type, scope_die, type);
21881 add_name_attribute (array_die, type_tag (type));
21882 equate_type_number_to_die (type, array_die);
21884 if (TREE_CODE (type) == VECTOR_TYPE)
21885 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21887 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
21888 if (is_fortran ()
21889 && TREE_CODE (type) == ARRAY_TYPE
21890 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21891 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21892 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21894 #if 0
21895 /* We default the array ordering. Debuggers will probably do the right
21896 things even if DW_AT_ordering is not present. It's not even an issue
21897 until we start to get into multidimensional arrays anyway. If a debugger
21898 is ever caught doing the Wrong Thing for multi-dimensional arrays,
21899 then we'll have to put the DW_AT_ordering attribute back in. (But if
21900 and when we find out that we need to put these in, we will only do so
21901 for multidimensional arrays. */
21902 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21903 #endif
21905 if (TREE_CODE (type) == VECTOR_TYPE)
21907 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21908 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21909 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21910 add_bound_info (subrange_die, DW_AT_upper_bound,
21911 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21913 else
21914 add_subscript_info (array_die, type, collapse_nested_arrays);
21916 /* Add representation of the type of the elements of this array type and
21917 emit the corresponding DIE if we haven't done it already. */
21918 element_type = TREE_TYPE (type);
21919 if (collapse_nested_arrays)
21920 while (TREE_CODE (element_type) == ARRAY_TYPE)
21922 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21923 break;
21924 element_type = TREE_TYPE (element_type);
21927 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21928 TREE_CODE (type) == ARRAY_TYPE
21929 && TYPE_REVERSE_STORAGE_ORDER (type),
21930 context_die);
21932 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21933 if (TYPE_ARTIFICIAL (type))
21934 add_AT_flag (array_die, DW_AT_artificial, 1);
21936 if (get_AT (array_die, DW_AT_name))
21937 add_pubtype (type, array_die);
21939 add_alignment_attribute (array_die, type);
21942 /* This routine generates DIE for array with hidden descriptor, details
21943 are filled into *info by a langhook. */
21945 static void
21946 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21947 dw_die_ref context_die)
21949 const dw_die_ref scope_die = scope_die_for (type, context_die);
21950 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21951 struct loc_descr_context context = { type, info->base_decl, NULL,
21952 false, false };
21953 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21954 int dim;
21956 add_name_attribute (array_die, type_tag (type));
21957 equate_type_number_to_die (type, array_die);
21959 if (info->ndimensions > 1)
21960 switch (info->ordering)
21962 case array_descr_ordering_row_major:
21963 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21964 break;
21965 case array_descr_ordering_column_major:
21966 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21967 break;
21968 default:
21969 break;
21972 if (dwarf_version >= 3 || !dwarf_strict)
21974 if (info->data_location)
21975 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21976 dw_scalar_form_exprloc, &context);
21977 if (info->associated)
21978 add_scalar_info (array_die, DW_AT_associated, info->associated,
21979 dw_scalar_form_constant
21980 | dw_scalar_form_exprloc
21981 | dw_scalar_form_reference, &context);
21982 if (info->allocated)
21983 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21984 dw_scalar_form_constant
21985 | dw_scalar_form_exprloc
21986 | dw_scalar_form_reference, &context);
21987 if (info->stride)
21989 const enum dwarf_attribute attr
21990 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21991 const int forms
21992 = (info->stride_in_bits)
21993 ? dw_scalar_form_constant
21994 : (dw_scalar_form_constant
21995 | dw_scalar_form_exprloc
21996 | dw_scalar_form_reference);
21998 add_scalar_info (array_die, attr, info->stride, forms, &context);
22001 if (dwarf_version >= 5)
22003 if (info->rank)
22005 add_scalar_info (array_die, DW_AT_rank, info->rank,
22006 dw_scalar_form_constant
22007 | dw_scalar_form_exprloc, &context);
22008 subrange_tag = DW_TAG_generic_subrange;
22009 context.placeholder_arg = true;
22013 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22015 for (dim = 0; dim < info->ndimensions; dim++)
22017 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22019 if (info->dimen[dim].bounds_type)
22020 add_type_attribute (subrange_die,
22021 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22022 false, context_die);
22023 if (info->dimen[dim].lower_bound)
22024 add_bound_info (subrange_die, DW_AT_lower_bound,
22025 info->dimen[dim].lower_bound, &context);
22026 if (info->dimen[dim].upper_bound)
22027 add_bound_info (subrange_die, DW_AT_upper_bound,
22028 info->dimen[dim].upper_bound, &context);
22029 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22030 add_scalar_info (subrange_die, DW_AT_byte_stride,
22031 info->dimen[dim].stride,
22032 dw_scalar_form_constant
22033 | dw_scalar_form_exprloc
22034 | dw_scalar_form_reference,
22035 &context);
22038 gen_type_die (info->element_type, context_die);
22039 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22040 TREE_CODE (type) == ARRAY_TYPE
22041 && TYPE_REVERSE_STORAGE_ORDER (type),
22042 context_die);
22044 if (get_AT (array_die, DW_AT_name))
22045 add_pubtype (type, array_die);
22047 add_alignment_attribute (array_die, type);
22050 #if 0
22051 static void
22052 gen_entry_point_die (tree decl, dw_die_ref context_die)
22054 tree origin = decl_ultimate_origin (decl);
22055 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22057 if (origin != NULL)
22058 add_abstract_origin_attribute (decl_die, origin);
22059 else
22061 add_name_and_src_coords_attributes (decl_die, decl);
22062 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22063 TYPE_UNQUALIFIED, false, context_die);
22066 if (DECL_ABSTRACT_P (decl))
22067 equate_decl_number_to_die (decl, decl_die);
22068 else
22069 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22071 #endif
22073 /* Walk through the list of incomplete types again, trying once more to
22074 emit full debugging info for them. */
22076 static void
22077 retry_incomplete_types (void)
22079 set_early_dwarf s;
22080 int i;
22082 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22083 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22084 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22085 vec_safe_truncate (incomplete_types, 0);
22088 /* Determine what tag to use for a record type. */
22090 static enum dwarf_tag
22091 record_type_tag (tree type)
22093 if (! lang_hooks.types.classify_record)
22094 return DW_TAG_structure_type;
22096 switch (lang_hooks.types.classify_record (type))
22098 case RECORD_IS_STRUCT:
22099 return DW_TAG_structure_type;
22101 case RECORD_IS_CLASS:
22102 return DW_TAG_class_type;
22104 case RECORD_IS_INTERFACE:
22105 if (dwarf_version >= 3 || !dwarf_strict)
22106 return DW_TAG_interface_type;
22107 return DW_TAG_structure_type;
22109 default:
22110 gcc_unreachable ();
22114 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22115 include all of the information about the enumeration values also. Each
22116 enumerated type name/value is listed as a child of the enumerated type
22117 DIE. */
22119 static dw_die_ref
22120 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22122 dw_die_ref type_die = lookup_type_die (type);
22123 dw_die_ref orig_type_die = type_die;
22125 if (type_die == NULL)
22127 type_die = new_die (DW_TAG_enumeration_type,
22128 scope_die_for (type, context_die), type);
22129 equate_type_number_to_die (type, type_die);
22130 add_name_attribute (type_die, type_tag (type));
22131 if ((dwarf_version >= 4 || !dwarf_strict)
22132 && ENUM_IS_SCOPED (type))
22133 add_AT_flag (type_die, DW_AT_enum_class, 1);
22134 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22135 add_AT_flag (type_die, DW_AT_declaration, 1);
22136 if (!dwarf_strict)
22137 add_AT_unsigned (type_die, DW_AT_encoding,
22138 TYPE_UNSIGNED (type)
22139 ? DW_ATE_unsigned
22140 : DW_ATE_signed);
22142 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22143 return type_die;
22144 else
22145 remove_AT (type_die, DW_AT_declaration);
22147 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22148 given enum type is incomplete, do not generate the DW_AT_byte_size
22149 attribute or the DW_AT_element_list attribute. */
22150 if (TYPE_SIZE (type))
22152 tree link;
22154 if (!ENUM_IS_OPAQUE (type))
22155 TREE_ASM_WRITTEN (type) = 1;
22156 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22157 add_byte_size_attribute (type_die, type);
22158 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22159 add_alignment_attribute (type_die, type);
22160 if ((dwarf_version >= 3 || !dwarf_strict)
22161 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22163 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22164 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22165 context_die);
22167 if (TYPE_STUB_DECL (type) != NULL_TREE)
22169 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22170 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22171 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22172 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22175 /* If the first reference to this type was as the return type of an
22176 inline function, then it may not have a parent. Fix this now. */
22177 if (type_die->die_parent == NULL)
22178 add_child_die (scope_die_for (type, context_die), type_die);
22180 for (link = TYPE_VALUES (type);
22181 link != NULL; link = TREE_CHAIN (link))
22183 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22184 tree value = TREE_VALUE (link);
22186 gcc_assert (!ENUM_IS_OPAQUE (type));
22187 add_name_attribute (enum_die,
22188 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22190 if (TREE_CODE (value) == CONST_DECL)
22191 value = DECL_INITIAL (value);
22193 if (simple_type_size_in_bits (TREE_TYPE (value))
22194 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22196 /* For constant forms created by add_AT_unsigned DWARF
22197 consumers (GDB, elfutils, etc.) always zero extend
22198 the value. Only when the actual value is negative
22199 do we need to use add_AT_int to generate a constant
22200 form that can represent negative values. */
22201 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22202 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22203 add_AT_unsigned (enum_die, DW_AT_const_value,
22204 (unsigned HOST_WIDE_INT) val);
22205 else
22206 add_AT_int (enum_die, DW_AT_const_value, val);
22208 else
22209 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22210 that here. TODO: This should be re-worked to use correct
22211 signed/unsigned double tags for all cases. */
22212 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22215 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22216 if (TYPE_ARTIFICIAL (type)
22217 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22218 add_AT_flag (type_die, DW_AT_artificial, 1);
22220 else
22221 add_AT_flag (type_die, DW_AT_declaration, 1);
22223 add_pubtype (type, type_die);
22225 return type_die;
22228 /* Generate a DIE to represent either a real live formal parameter decl or to
22229 represent just the type of some formal parameter position in some function
22230 type.
22232 Note that this routine is a bit unusual because its argument may be a
22233 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22234 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22235 node. If it's the former then this function is being called to output a
22236 DIE to represent a formal parameter object (or some inlining thereof). If
22237 it's the latter, then this function is only being called to output a
22238 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22239 argument type of some subprogram type.
22240 If EMIT_NAME_P is true, name and source coordinate attributes
22241 are emitted. */
22243 static dw_die_ref
22244 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22245 dw_die_ref context_die)
22247 tree node_or_origin = node ? node : origin;
22248 tree ultimate_origin;
22249 dw_die_ref parm_die = NULL;
22251 if (DECL_P (node_or_origin))
22253 parm_die = lookup_decl_die (node);
22255 /* If the contexts differ, we may not be talking about the same
22256 thing.
22257 ??? When in LTO the DIE parent is the "abstract" copy and the
22258 context_die is the specification "copy". */
22259 if (parm_die
22260 && parm_die->die_parent != context_die
22261 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22262 || parm_die->die_parent->die_parent != context_die)
22263 && !in_lto_p)
22265 gcc_assert (!DECL_ABSTRACT_P (node));
22266 /* This can happen when creating a concrete instance, in
22267 which case we need to create a new DIE that will get
22268 annotated with DW_AT_abstract_origin. */
22269 parm_die = NULL;
22272 if (parm_die && parm_die->die_parent == NULL)
22274 /* Check that parm_die already has the right attributes that
22275 we would have added below. If any attributes are
22276 missing, fall through to add them. */
22277 if (! DECL_ABSTRACT_P (node_or_origin)
22278 && !get_AT (parm_die, DW_AT_location)
22279 && !get_AT (parm_die, DW_AT_const_value))
22280 /* We are missing location info, and are about to add it. */
22282 else
22284 add_child_die (context_die, parm_die);
22285 return parm_die;
22290 /* If we have a previously generated DIE, use it, unless this is an
22291 concrete instance (origin != NULL), in which case we need a new
22292 DIE with a corresponding DW_AT_abstract_origin. */
22293 bool reusing_die;
22294 if (parm_die && origin == NULL)
22295 reusing_die = true;
22296 else
22298 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22299 reusing_die = false;
22302 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22304 case tcc_declaration:
22305 ultimate_origin = decl_ultimate_origin (node_or_origin);
22306 if (node || ultimate_origin)
22307 origin = ultimate_origin;
22309 if (reusing_die)
22310 goto add_location;
22312 if (origin != NULL)
22313 add_abstract_origin_attribute (parm_die, origin);
22314 else if (emit_name_p)
22315 add_name_and_src_coords_attributes (parm_die, node);
22316 if (origin == NULL
22317 || (! DECL_ABSTRACT_P (node_or_origin)
22318 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22319 decl_function_context
22320 (node_or_origin))))
22322 tree type = TREE_TYPE (node_or_origin);
22323 if (decl_by_reference_p (node_or_origin))
22324 add_type_attribute (parm_die, TREE_TYPE (type),
22325 TYPE_UNQUALIFIED,
22326 false, context_die);
22327 else
22328 add_type_attribute (parm_die, type,
22329 decl_quals (node_or_origin),
22330 false, context_die);
22332 if (origin == NULL && DECL_ARTIFICIAL (node))
22333 add_AT_flag (parm_die, DW_AT_artificial, 1);
22334 add_location:
22335 if (node && node != origin)
22336 equate_decl_number_to_die (node, parm_die);
22337 if (! DECL_ABSTRACT_P (node_or_origin))
22338 add_location_or_const_value_attribute (parm_die, node_or_origin,
22339 node == NULL);
22341 break;
22343 case tcc_type:
22344 /* We were called with some kind of a ..._TYPE node. */
22345 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22346 context_die);
22347 break;
22349 default:
22350 gcc_unreachable ();
22353 return parm_die;
22356 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22357 children DW_TAG_formal_parameter DIEs representing the arguments of the
22358 parameter pack.
22360 PARM_PACK must be a function parameter pack.
22361 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22362 must point to the subsequent arguments of the function PACK_ARG belongs to.
22363 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22364 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22365 following the last one for which a DIE was generated. */
22367 static dw_die_ref
22368 gen_formal_parameter_pack_die (tree parm_pack,
22369 tree pack_arg,
22370 dw_die_ref subr_die,
22371 tree *next_arg)
22373 tree arg;
22374 dw_die_ref parm_pack_die;
22376 gcc_assert (parm_pack
22377 && lang_hooks.function_parameter_pack_p (parm_pack)
22378 && subr_die);
22380 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22381 add_src_coords_attributes (parm_pack_die, parm_pack);
22383 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22385 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22386 parm_pack))
22387 break;
22388 gen_formal_parameter_die (arg, NULL,
22389 false /* Don't emit name attribute. */,
22390 parm_pack_die);
22392 if (next_arg)
22393 *next_arg = arg;
22394 return parm_pack_die;
22397 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22398 at the end of an (ANSI prototyped) formal parameters list. */
22400 static void
22401 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22403 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22406 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22407 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22408 parameters as specified in some function type specification (except for
22409 those which appear as part of a function *definition*). */
22411 static void
22412 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22414 tree link;
22415 tree formal_type = NULL;
22416 tree first_parm_type;
22417 tree arg;
22419 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22421 arg = DECL_ARGUMENTS (function_or_method_type);
22422 function_or_method_type = TREE_TYPE (function_or_method_type);
22424 else
22425 arg = NULL_TREE;
22427 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22429 /* Make our first pass over the list of formal parameter types and output a
22430 DW_TAG_formal_parameter DIE for each one. */
22431 for (link = first_parm_type; link; )
22433 dw_die_ref parm_die;
22435 formal_type = TREE_VALUE (link);
22436 if (formal_type == void_type_node)
22437 break;
22439 /* Output a (nameless) DIE to represent the formal parameter itself. */
22440 parm_die = gen_formal_parameter_die (formal_type, NULL,
22441 true /* Emit name attribute. */,
22442 context_die);
22443 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22444 && link == first_parm_type)
22446 add_AT_flag (parm_die, DW_AT_artificial, 1);
22447 if (dwarf_version >= 3 || !dwarf_strict)
22448 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22450 else if (arg && DECL_ARTIFICIAL (arg))
22451 add_AT_flag (parm_die, DW_AT_artificial, 1);
22453 link = TREE_CHAIN (link);
22454 if (arg)
22455 arg = DECL_CHAIN (arg);
22458 /* If this function type has an ellipsis, add a
22459 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22460 if (formal_type != void_type_node)
22461 gen_unspecified_parameters_die (function_or_method_type, context_die);
22463 /* Make our second (and final) pass over the list of formal parameter types
22464 and output DIEs to represent those types (as necessary). */
22465 for (link = TYPE_ARG_TYPES (function_or_method_type);
22466 link && TREE_VALUE (link);
22467 link = TREE_CHAIN (link))
22468 gen_type_die (TREE_VALUE (link), context_die);
22471 /* We want to generate the DIE for TYPE so that we can generate the
22472 die for MEMBER, which has been defined; we will need to refer back
22473 to the member declaration nested within TYPE. If we're trying to
22474 generate minimal debug info for TYPE, processing TYPE won't do the
22475 trick; we need to attach the member declaration by hand. */
22477 static void
22478 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22480 gen_type_die (type, context_die);
22482 /* If we're trying to avoid duplicate debug info, we may not have
22483 emitted the member decl for this function. Emit it now. */
22484 if (TYPE_STUB_DECL (type)
22485 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22486 && ! lookup_decl_die (member))
22488 dw_die_ref type_die;
22489 gcc_assert (!decl_ultimate_origin (member));
22491 type_die = lookup_type_die_strip_naming_typedef (type);
22492 if (TREE_CODE (member) == FUNCTION_DECL)
22493 gen_subprogram_die (member, type_die);
22494 else if (TREE_CODE (member) == FIELD_DECL)
22496 /* Ignore the nameless fields that are used to skip bits but handle
22497 C++ anonymous unions and structs. */
22498 if (DECL_NAME (member) != NULL_TREE
22499 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22500 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22502 struct vlr_context vlr_ctx = {
22503 DECL_CONTEXT (member), /* struct_type */
22504 NULL_TREE /* variant_part_offset */
22506 gen_type_die (member_declared_type (member), type_die);
22507 gen_field_die (member, &vlr_ctx, type_die);
22510 else
22511 gen_variable_die (member, NULL_TREE, type_die);
22515 /* Forward declare these functions, because they are mutually recursive
22516 with their set_block_* pairing functions. */
22517 static void set_decl_origin_self (tree);
22519 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22520 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22521 that it points to the node itself, thus indicating that the node is its
22522 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
22523 the given node is NULL, recursively descend the decl/block tree which
22524 it is the root of, and for each other ..._DECL or BLOCK node contained
22525 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
22526 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
22527 values to point to themselves. */
22529 static void
22530 set_block_origin_self (tree stmt)
22532 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
22534 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
22537 tree local_decl;
22539 for (local_decl = BLOCK_VARS (stmt);
22540 local_decl != NULL_TREE;
22541 local_decl = DECL_CHAIN (local_decl))
22542 /* Do not recurse on nested functions since the inlining status
22543 of parent and child can be different as per the DWARF spec. */
22544 if (TREE_CODE (local_decl) != FUNCTION_DECL
22545 && !DECL_EXTERNAL (local_decl))
22546 set_decl_origin_self (local_decl);
22550 tree subblock;
22552 for (subblock = BLOCK_SUBBLOCKS (stmt);
22553 subblock != NULL_TREE;
22554 subblock = BLOCK_CHAIN (subblock))
22555 set_block_origin_self (subblock); /* Recurse. */
22560 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
22561 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
22562 node to so that it points to the node itself, thus indicating that the
22563 node represents its own (abstract) origin. Additionally, if the
22564 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
22565 the decl/block tree of which the given node is the root of, and for
22566 each other ..._DECL or BLOCK node contained therein whose
22567 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
22568 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
22569 point to themselves. */
22571 static void
22572 set_decl_origin_self (tree decl)
22574 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
22576 DECL_ABSTRACT_ORIGIN (decl) = decl;
22577 if (TREE_CODE (decl) == FUNCTION_DECL)
22579 tree arg;
22581 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
22582 DECL_ABSTRACT_ORIGIN (arg) = arg;
22583 if (DECL_INITIAL (decl) != NULL_TREE
22584 && DECL_INITIAL (decl) != error_mark_node)
22585 set_block_origin_self (DECL_INITIAL (decl));
22590 /* Mark the early DIE for DECL as the abstract instance. */
22592 static void
22593 dwarf2out_abstract_function (tree decl)
22595 dw_die_ref old_die;
22597 /* Make sure we have the actual abstract inline, not a clone. */
22598 decl = DECL_ORIGIN (decl);
22600 if (DECL_IGNORED_P (decl))
22601 return;
22603 /* In LTO we're all set. We already created abstract instances
22604 early and we want to avoid creating a concrete instance of that
22605 if we don't output it. */
22606 if (in_lto_p)
22607 return;
22609 old_die = lookup_decl_die (decl);
22610 gcc_assert (old_die != NULL);
22611 if (get_AT (old_die, DW_AT_inline))
22612 /* We've already generated the abstract instance. */
22613 return;
22615 /* Go ahead and put DW_AT_inline on the DIE. */
22616 if (DECL_DECLARED_INLINE_P (decl))
22618 if (cgraph_function_possibly_inlined_p (decl))
22619 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
22620 else
22621 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
22623 else
22625 if (cgraph_function_possibly_inlined_p (decl))
22626 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
22627 else
22628 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
22631 if (DECL_DECLARED_INLINE_P (decl)
22632 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22633 add_AT_flag (old_die, DW_AT_artificial, 1);
22635 set_decl_origin_self (decl);
22638 /* Helper function of premark_used_types() which gets called through
22639 htab_traverse.
22641 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22642 marked as unused by prune_unused_types. */
22644 bool
22645 premark_used_types_helper (tree const &type, void *)
22647 dw_die_ref die;
22649 die = lookup_type_die (type);
22650 if (die != NULL)
22651 die->die_perennial_p = 1;
22652 return true;
22655 /* Helper function of premark_types_used_by_global_vars which gets called
22656 through htab_traverse.
22658 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22659 marked as unused by prune_unused_types. The DIE of the type is marked
22660 only if the global variable using the type will actually be emitted. */
22663 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
22664 void *)
22666 struct types_used_by_vars_entry *entry;
22667 dw_die_ref die;
22669 entry = (struct types_used_by_vars_entry *) *slot;
22670 gcc_assert (entry->type != NULL
22671 && entry->var_decl != NULL);
22672 die = lookup_type_die (entry->type);
22673 if (die)
22675 /* Ask cgraph if the global variable really is to be emitted.
22676 If yes, then we'll keep the DIE of ENTRY->TYPE. */
22677 varpool_node *node = varpool_node::get (entry->var_decl);
22678 if (node && node->definition)
22680 die->die_perennial_p = 1;
22681 /* Keep the parent DIEs as well. */
22682 while ((die = die->die_parent) && die->die_perennial_p == 0)
22683 die->die_perennial_p = 1;
22686 return 1;
22689 /* Mark all members of used_types_hash as perennial. */
22691 static void
22692 premark_used_types (struct function *fun)
22694 if (fun && fun->used_types_hash)
22695 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
22698 /* Mark all members of types_used_by_vars_entry as perennial. */
22700 static void
22701 premark_types_used_by_global_vars (void)
22703 if (types_used_by_vars_hash)
22704 types_used_by_vars_hash
22705 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
22708 /* Mark all variables used by the symtab as perennial. */
22710 static void
22711 premark_used_variables (void)
22713 /* Mark DIEs in the symtab as used. */
22714 varpool_node *var;
22715 FOR_EACH_VARIABLE (var)
22717 dw_die_ref die = lookup_decl_die (var->decl);
22718 if (die)
22719 die->die_perennial_p = 1;
22723 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
22724 for CA_LOC call arg loc node. */
22726 static dw_die_ref
22727 gen_call_site_die (tree decl, dw_die_ref subr_die,
22728 struct call_arg_loc_node *ca_loc)
22730 dw_die_ref stmt_die = NULL, die;
22731 tree block = ca_loc->block;
22733 while (block
22734 && block != DECL_INITIAL (decl)
22735 && TREE_CODE (block) == BLOCK)
22737 stmt_die = lookup_block_die (block);
22738 if (stmt_die)
22739 break;
22740 block = BLOCK_SUPERCONTEXT (block);
22742 if (stmt_die == NULL)
22743 stmt_die = subr_die;
22744 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
22745 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
22746 if (ca_loc->tail_call_p)
22747 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
22748 if (ca_loc->symbol_ref)
22750 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
22751 if (tdie)
22752 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
22753 else
22754 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
22755 false);
22757 return die;
22760 /* Generate a DIE to represent a declared function (either file-scope or
22761 block-local). */
22763 static void
22764 gen_subprogram_die (tree decl, dw_die_ref context_die)
22766 tree origin = decl_ultimate_origin (decl);
22767 dw_die_ref subr_die;
22768 dw_die_ref old_die = lookup_decl_die (decl);
22770 /* This function gets called multiple times for different stages of
22771 the debug process. For example, for func() in this code:
22773 namespace S
22775 void func() { ... }
22778 ...we get called 4 times. Twice in early debug and twice in
22779 late debug:
22781 Early debug
22782 -----------
22784 1. Once while generating func() within the namespace. This is
22785 the declaration. The declaration bit below is set, as the
22786 context is the namespace.
22788 A new DIE will be generated with DW_AT_declaration set.
22790 2. Once for func() itself. This is the specification. The
22791 declaration bit below is clear as the context is the CU.
22793 We will use the cached DIE from (1) to create a new DIE with
22794 DW_AT_specification pointing to the declaration in (1).
22796 Late debug via rest_of_handle_final()
22797 -------------------------------------
22799 3. Once generating func() within the namespace. This is also the
22800 declaration, as in (1), but this time we will early exit below
22801 as we have a cached DIE and a declaration needs no additional
22802 annotations (no locations), as the source declaration line
22803 info is enough.
22805 4. Once for func() itself. As in (2), this is the specification,
22806 but this time we will re-use the cached DIE, and just annotate
22807 it with the location information that should now be available.
22809 For something without namespaces, but with abstract instances, we
22810 are also called a multiple times:
22812 class Base
22814 public:
22815 Base (); // constructor declaration (1)
22818 Base::Base () { } // constructor specification (2)
22820 Early debug
22821 -----------
22823 1. Once for the Base() constructor by virtue of it being a
22824 member of the Base class. This is done via
22825 rest_of_type_compilation.
22827 This is a declaration, so a new DIE will be created with
22828 DW_AT_declaration.
22830 2. Once for the Base() constructor definition, but this time
22831 while generating the abstract instance of the base
22832 constructor (__base_ctor) which is being generated via early
22833 debug of reachable functions.
22835 Even though we have a cached version of the declaration (1),
22836 we will create a DW_AT_specification of the declaration DIE
22837 in (1).
22839 3. Once for the __base_ctor itself, but this time, we generate
22840 an DW_AT_abstract_origin version of the DW_AT_specification in
22841 (2).
22843 Late debug via rest_of_handle_final
22844 -----------------------------------
22846 4. One final time for the __base_ctor (which will have a cached
22847 DIE with DW_AT_abstract_origin created in (3). This time,
22848 we will just annotate the location information now
22849 available.
22851 int declaration = (current_function_decl != decl
22852 || class_or_namespace_scope_p (context_die));
22854 /* A declaration that has been previously dumped needs no
22855 additional information. */
22856 if (old_die && declaration)
22857 return;
22859 /* Now that the C++ front end lazily declares artificial member fns, we
22860 might need to retrofit the declaration into its class. */
22861 if (!declaration && !origin && !old_die
22862 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22863 && !class_or_namespace_scope_p (context_die)
22864 && debug_info_level > DINFO_LEVEL_TERSE)
22865 old_die = force_decl_die (decl);
22867 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
22868 if (origin != NULL)
22870 gcc_assert (!declaration || local_scope_p (context_die));
22872 /* Fixup die_parent for the abstract instance of a nested
22873 inline function. */
22874 if (old_die && old_die->die_parent == NULL)
22875 add_child_die (context_die, old_die);
22877 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22879 /* If we have a DW_AT_abstract_origin we have a working
22880 cached version. */
22881 subr_die = old_die;
22883 else
22885 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22886 add_abstract_origin_attribute (subr_die, origin);
22887 /* This is where the actual code for a cloned function is.
22888 Let's emit linkage name attribute for it. This helps
22889 debuggers to e.g, set breakpoints into
22890 constructors/destructors when the user asks "break
22891 K::K". */
22892 add_linkage_name (subr_die, decl);
22895 /* A cached copy, possibly from early dwarf generation. Reuse as
22896 much as possible. */
22897 else if (old_die)
22899 if (!get_AT_flag (old_die, DW_AT_declaration)
22900 /* We can have a normal definition following an inline one in the
22901 case of redefinition of GNU C extern inlines.
22902 It seems reasonable to use AT_specification in this case. */
22903 && !get_AT (old_die, DW_AT_inline))
22905 /* Detect and ignore this case, where we are trying to output
22906 something we have already output. */
22907 if (get_AT (old_die, DW_AT_low_pc)
22908 || get_AT (old_die, DW_AT_ranges))
22909 return;
22911 /* If we have no location information, this must be a
22912 partially generated DIE from early dwarf generation.
22913 Fall through and generate it. */
22916 /* If the definition comes from the same place as the declaration,
22917 maybe use the old DIE. We always want the DIE for this function
22918 that has the *_pc attributes to be under comp_unit_die so the
22919 debugger can find it. We also need to do this for abstract
22920 instances of inlines, since the spec requires the out-of-line copy
22921 to have the same parent. For local class methods, this doesn't
22922 apply; we just use the old DIE. */
22923 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22924 struct dwarf_file_data * file_index = lookup_filename (s.file);
22925 if (((is_unit_die (old_die->die_parent)
22926 /* This condition fixes the inconsistency/ICE with the
22927 following Fortran test (or some derivative thereof) while
22928 building libgfortran:
22930 module some_m
22931 contains
22932 logical function funky (FLAG)
22933 funky = .true.
22934 end function
22935 end module
22937 || (old_die->die_parent
22938 && old_die->die_parent->die_tag == DW_TAG_module)
22939 || local_scope_p (old_die->die_parent)
22940 || context_die == NULL)
22941 && (DECL_ARTIFICIAL (decl)
22942 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22943 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22944 == (unsigned) s.line)
22945 && (!debug_column_info
22946 || s.column == 0
22947 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22948 == (unsigned) s.column)))))
22949 /* With LTO if there's an abstract instance for
22950 the old DIE, this is a concrete instance and
22951 thus re-use the DIE. */
22952 || get_AT (old_die, DW_AT_abstract_origin))
22954 subr_die = old_die;
22956 /* Clear out the declaration attribute, but leave the
22957 parameters so they can be augmented with location
22958 information later. Unless this was a declaration, in
22959 which case, wipe out the nameless parameters and recreate
22960 them further down. */
22961 if (remove_AT (subr_die, DW_AT_declaration))
22964 remove_AT (subr_die, DW_AT_object_pointer);
22965 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22968 /* Make a specification pointing to the previously built
22969 declaration. */
22970 else
22972 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22973 add_AT_specification (subr_die, old_die);
22974 add_pubname (decl, subr_die);
22975 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22976 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22977 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22978 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22979 if (debug_column_info
22980 && s.column
22981 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22982 != (unsigned) s.column))
22983 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22985 /* If the prototype had an 'auto' or 'decltype(auto)' in
22986 the return type, emit the real type on the definition die. */
22987 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22989 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22990 while (die
22991 && (die->die_tag == DW_TAG_reference_type
22992 || die->die_tag == DW_TAG_rvalue_reference_type
22993 || die->die_tag == DW_TAG_pointer_type
22994 || die->die_tag == DW_TAG_const_type
22995 || die->die_tag == DW_TAG_volatile_type
22996 || die->die_tag == DW_TAG_restrict_type
22997 || die->die_tag == DW_TAG_array_type
22998 || die->die_tag == DW_TAG_ptr_to_member_type
22999 || die->die_tag == DW_TAG_subroutine_type))
23000 die = get_AT_ref (die, DW_AT_type);
23001 if (die == auto_die || die == decltype_auto_die)
23002 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23003 TYPE_UNQUALIFIED, false, context_die);
23006 /* When we process the method declaration, we haven't seen
23007 the out-of-class defaulted definition yet, so we have to
23008 recheck now. */
23009 if ((dwarf_version >= 5 || ! dwarf_strict)
23010 && !get_AT (subr_die, DW_AT_defaulted))
23012 int defaulted
23013 = lang_hooks.decls.decl_dwarf_attribute (decl,
23014 DW_AT_defaulted);
23015 if (defaulted != -1)
23017 /* Other values must have been handled before. */
23018 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23019 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23024 /* Create a fresh DIE for anything else. */
23025 else
23027 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23029 if (TREE_PUBLIC (decl))
23030 add_AT_flag (subr_die, DW_AT_external, 1);
23032 add_name_and_src_coords_attributes (subr_die, decl);
23033 add_pubname (decl, subr_die);
23034 if (debug_info_level > DINFO_LEVEL_TERSE)
23036 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23037 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23038 TYPE_UNQUALIFIED, false, context_die);
23041 add_pure_or_virtual_attribute (subr_die, decl);
23042 if (DECL_ARTIFICIAL (decl))
23043 add_AT_flag (subr_die, DW_AT_artificial, 1);
23045 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23046 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23048 add_alignment_attribute (subr_die, decl);
23050 add_accessibility_attribute (subr_die, decl);
23053 /* Unless we have an existing non-declaration DIE, equate the new
23054 DIE. */
23055 if (!old_die || is_declaration_die (old_die))
23056 equate_decl_number_to_die (decl, subr_die);
23058 if (declaration)
23060 if (!old_die || !get_AT (old_die, DW_AT_inline))
23062 add_AT_flag (subr_die, DW_AT_declaration, 1);
23064 /* If this is an explicit function declaration then generate
23065 a DW_AT_explicit attribute. */
23066 if ((dwarf_version >= 3 || !dwarf_strict)
23067 && lang_hooks.decls.decl_dwarf_attribute (decl,
23068 DW_AT_explicit) == 1)
23069 add_AT_flag (subr_die, DW_AT_explicit, 1);
23071 /* If this is a C++11 deleted special function member then generate
23072 a DW_AT_deleted attribute. */
23073 if ((dwarf_version >= 5 || !dwarf_strict)
23074 && lang_hooks.decls.decl_dwarf_attribute (decl,
23075 DW_AT_deleted) == 1)
23076 add_AT_flag (subr_die, DW_AT_deleted, 1);
23078 /* If this is a C++11 defaulted special function member then
23079 generate a DW_AT_defaulted attribute. */
23080 if (dwarf_version >= 5 || !dwarf_strict)
23082 int defaulted
23083 = lang_hooks.decls.decl_dwarf_attribute (decl,
23084 DW_AT_defaulted);
23085 if (defaulted != -1)
23086 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23089 /* If this is a C++11 non-static member function with & ref-qualifier
23090 then generate a DW_AT_reference attribute. */
23091 if ((dwarf_version >= 5 || !dwarf_strict)
23092 && lang_hooks.decls.decl_dwarf_attribute (decl,
23093 DW_AT_reference) == 1)
23094 add_AT_flag (subr_die, DW_AT_reference, 1);
23096 /* If this is a C++11 non-static member function with &&
23097 ref-qualifier then generate a DW_AT_reference attribute. */
23098 if ((dwarf_version >= 5 || !dwarf_strict)
23099 && lang_hooks.decls.decl_dwarf_attribute (decl,
23100 DW_AT_rvalue_reference)
23101 == 1)
23102 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23105 /* For non DECL_EXTERNALs, if range information is available, fill
23106 the DIE with it. */
23107 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23109 HOST_WIDE_INT cfa_fb_offset;
23111 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23113 if (!crtl->has_bb_partition)
23115 dw_fde_ref fde = fun->fde;
23116 if (fde->dw_fde_begin)
23118 /* We have already generated the labels. */
23119 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23120 fde->dw_fde_end, false);
23122 else
23124 /* Create start/end labels and add the range. */
23125 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23126 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23127 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23128 current_function_funcdef_no);
23129 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23130 current_function_funcdef_no);
23131 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23132 false);
23135 #if VMS_DEBUGGING_INFO
23136 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23137 Section 2.3 Prologue and Epilogue Attributes:
23138 When a breakpoint is set on entry to a function, it is generally
23139 desirable for execution to be suspended, not on the very first
23140 instruction of the function, but rather at a point after the
23141 function's frame has been set up, after any language defined local
23142 declaration processing has been completed, and before execution of
23143 the first statement of the function begins. Debuggers generally
23144 cannot properly determine where this point is. Similarly for a
23145 breakpoint set on exit from a function. The prologue and epilogue
23146 attributes allow a compiler to communicate the location(s) to use. */
23149 if (fde->dw_fde_vms_end_prologue)
23150 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23151 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23153 if (fde->dw_fde_vms_begin_epilogue)
23154 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23155 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23157 #endif
23160 else
23162 /* Generate pubnames entries for the split function code ranges. */
23163 dw_fde_ref fde = fun->fde;
23165 if (fde->dw_fde_second_begin)
23167 if (dwarf_version >= 3 || !dwarf_strict)
23169 /* We should use ranges for non-contiguous code section
23170 addresses. Use the actual code range for the initial
23171 section, since the HOT/COLD labels might precede an
23172 alignment offset. */
23173 bool range_list_added = false;
23174 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23175 fde->dw_fde_end, &range_list_added,
23176 false);
23177 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23178 fde->dw_fde_second_end,
23179 &range_list_added, false);
23180 if (range_list_added)
23181 add_ranges (NULL);
23183 else
23185 /* There is no real support in DW2 for this .. so we make
23186 a work-around. First, emit the pub name for the segment
23187 containing the function label. Then make and emit a
23188 simplified subprogram DIE for the second segment with the
23189 name pre-fixed by __hot/cold_sect_of_. We use the same
23190 linkage name for the second die so that gdb will find both
23191 sections when given "b foo". */
23192 const char *name = NULL;
23193 tree decl_name = DECL_NAME (decl);
23194 dw_die_ref seg_die;
23196 /* Do the 'primary' section. */
23197 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23198 fde->dw_fde_end, false);
23200 /* Build a minimal DIE for the secondary section. */
23201 seg_die = new_die (DW_TAG_subprogram,
23202 subr_die->die_parent, decl);
23204 if (TREE_PUBLIC (decl))
23205 add_AT_flag (seg_die, DW_AT_external, 1);
23207 if (decl_name != NULL
23208 && IDENTIFIER_POINTER (decl_name) != NULL)
23210 name = dwarf2_name (decl, 1);
23211 if (! DECL_ARTIFICIAL (decl))
23212 add_src_coords_attributes (seg_die, decl);
23214 add_linkage_name (seg_die, decl);
23216 gcc_assert (name != NULL);
23217 add_pure_or_virtual_attribute (seg_die, decl);
23218 if (DECL_ARTIFICIAL (decl))
23219 add_AT_flag (seg_die, DW_AT_artificial, 1);
23221 name = concat ("__second_sect_of_", name, NULL);
23222 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23223 fde->dw_fde_second_end, false);
23224 add_name_attribute (seg_die, name);
23225 if (want_pubnames ())
23226 add_pubname_string (name, seg_die);
23229 else
23230 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23231 false);
23234 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23236 /* We define the "frame base" as the function's CFA. This is more
23237 convenient for several reasons: (1) It's stable across the prologue
23238 and epilogue, which makes it better than just a frame pointer,
23239 (2) With dwarf3, there exists a one-byte encoding that allows us
23240 to reference the .debug_frame data by proxy, but failing that,
23241 (3) We can at least reuse the code inspection and interpretation
23242 code that determines the CFA position at various points in the
23243 function. */
23244 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23246 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23247 add_AT_loc (subr_die, DW_AT_frame_base, op);
23249 else
23251 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23252 if (list->dw_loc_next)
23253 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23254 else
23255 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23258 /* Compute a displacement from the "steady-state frame pointer" to
23259 the CFA. The former is what all stack slots and argument slots
23260 will reference in the rtl; the latter is what we've told the
23261 debugger about. We'll need to adjust all frame_base references
23262 by this displacement. */
23263 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23265 if (fun->static_chain_decl)
23267 /* DWARF requires here a location expression that computes the
23268 address of the enclosing subprogram's frame base. The machinery
23269 in tree-nested.c is supposed to store this specific address in the
23270 last field of the FRAME record. */
23271 const tree frame_type
23272 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23273 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23275 tree fb_expr
23276 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23277 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23278 fb_expr, fb_decl, NULL_TREE);
23280 add_AT_location_description (subr_die, DW_AT_static_link,
23281 loc_list_from_tree (fb_expr, 0, NULL));
23284 resolve_variable_values ();
23287 /* Generate child dies for template paramaters. */
23288 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23289 gen_generic_params_dies (decl);
23291 /* Now output descriptions of the arguments for this function. This gets
23292 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23293 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23294 `...' at the end of the formal parameter list. In order to find out if
23295 there was a trailing ellipsis or not, we must instead look at the type
23296 associated with the FUNCTION_DECL. This will be a node of type
23297 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23298 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23299 an ellipsis at the end. */
23301 /* In the case where we are describing a mere function declaration, all we
23302 need to do here (and all we *can* do here) is to describe the *types* of
23303 its formal parameters. */
23304 if (debug_info_level <= DINFO_LEVEL_TERSE)
23306 else if (declaration)
23307 gen_formal_types_die (decl, subr_die);
23308 else
23310 /* Generate DIEs to represent all known formal parameters. */
23311 tree parm = DECL_ARGUMENTS (decl);
23312 tree generic_decl = early_dwarf
23313 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23314 tree generic_decl_parm = generic_decl
23315 ? DECL_ARGUMENTS (generic_decl)
23316 : NULL;
23318 /* Now we want to walk the list of parameters of the function and
23319 emit their relevant DIEs.
23321 We consider the case of DECL being an instance of a generic function
23322 as well as it being a normal function.
23324 If DECL is an instance of a generic function we walk the
23325 parameters of the generic function declaration _and_ the parameters of
23326 DECL itself. This is useful because we want to emit specific DIEs for
23327 function parameter packs and those are declared as part of the
23328 generic function declaration. In that particular case,
23329 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23330 That DIE has children DIEs representing the set of arguments
23331 of the pack. Note that the set of pack arguments can be empty.
23332 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23333 children DIE.
23335 Otherwise, we just consider the parameters of DECL. */
23336 while (generic_decl_parm || parm)
23338 if (generic_decl_parm
23339 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23340 gen_formal_parameter_pack_die (generic_decl_parm,
23341 parm, subr_die,
23342 &parm);
23343 else if (parm)
23345 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23347 if (early_dwarf
23348 && parm == DECL_ARGUMENTS (decl)
23349 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23350 && parm_die
23351 && (dwarf_version >= 3 || !dwarf_strict))
23352 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23354 parm = DECL_CHAIN (parm);
23357 if (generic_decl_parm)
23358 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23361 /* Decide whether we need an unspecified_parameters DIE at the end.
23362 There are 2 more cases to do this for: 1) the ansi ... declaration -
23363 this is detectable when the end of the arg list is not a
23364 void_type_node 2) an unprototyped function declaration (not a
23365 definition). This just means that we have no info about the
23366 parameters at all. */
23367 if (early_dwarf)
23369 if (prototype_p (TREE_TYPE (decl)))
23371 /* This is the prototyped case, check for.... */
23372 if (stdarg_p (TREE_TYPE (decl)))
23373 gen_unspecified_parameters_die (decl, subr_die);
23375 else if (DECL_INITIAL (decl) == NULL_TREE)
23376 gen_unspecified_parameters_die (decl, subr_die);
23380 if (subr_die != old_die)
23381 /* Add the calling convention attribute if requested. */
23382 add_calling_convention_attribute (subr_die, decl);
23384 /* Output Dwarf info for all of the stuff within the body of the function
23385 (if it has one - it may be just a declaration).
23387 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23388 a function. This BLOCK actually represents the outermost binding contour
23389 for the function, i.e. the contour in which the function's formal
23390 parameters and labels get declared. Curiously, it appears that the front
23391 end doesn't actually put the PARM_DECL nodes for the current function onto
23392 the BLOCK_VARS list for this outer scope, but are strung off of the
23393 DECL_ARGUMENTS list for the function instead.
23395 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23396 the LABEL_DECL nodes for the function however, and we output DWARF info
23397 for those in decls_for_scope. Just within the `outer_scope' there will be
23398 a BLOCK node representing the function's outermost pair of curly braces,
23399 and any blocks used for the base and member initializers of a C++
23400 constructor function. */
23401 tree outer_scope = DECL_INITIAL (decl);
23402 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23404 int call_site_note_count = 0;
23405 int tail_call_site_note_count = 0;
23407 /* Emit a DW_TAG_variable DIE for a named return value. */
23408 if (DECL_NAME (DECL_RESULT (decl)))
23409 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23411 /* The first time through decls_for_scope we will generate the
23412 DIEs for the locals. The second time, we fill in the
23413 location info. */
23414 decls_for_scope (outer_scope, subr_die);
23416 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23418 struct call_arg_loc_node *ca_loc;
23419 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23421 dw_die_ref die = NULL;
23422 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23423 rtx arg, next_arg;
23424 tree arg_decl = NULL_TREE;
23426 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23427 ? XEXP (ca_loc->call_arg_loc_note, 0)
23428 : NULL_RTX);
23429 arg; arg = next_arg)
23431 dw_loc_descr_ref reg, val;
23432 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23433 dw_die_ref cdie, tdie = NULL;
23435 next_arg = XEXP (arg, 1);
23436 if (REG_P (XEXP (XEXP (arg, 0), 0))
23437 && next_arg
23438 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23439 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23440 && REGNO (XEXP (XEXP (arg, 0), 0))
23441 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23442 next_arg = XEXP (next_arg, 1);
23443 if (mode == VOIDmode)
23445 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23446 if (mode == VOIDmode)
23447 mode = GET_MODE (XEXP (arg, 0));
23449 if (mode == VOIDmode || mode == BLKmode)
23450 continue;
23451 /* Get dynamic information about call target only if we
23452 have no static information: we cannot generate both
23453 DW_AT_call_origin and DW_AT_call_target
23454 attributes. */
23455 if (ca_loc->symbol_ref == NULL_RTX)
23457 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23459 tloc = XEXP (XEXP (arg, 0), 1);
23460 continue;
23462 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23463 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23465 tlocc = XEXP (XEXP (arg, 0), 1);
23466 continue;
23469 reg = NULL;
23470 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23471 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23472 VAR_INIT_STATUS_INITIALIZED);
23473 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23475 rtx mem = XEXP (XEXP (arg, 0), 0);
23476 reg = mem_loc_descriptor (XEXP (mem, 0),
23477 get_address_mode (mem),
23478 GET_MODE (mem),
23479 VAR_INIT_STATUS_INITIALIZED);
23481 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23482 == DEBUG_PARAMETER_REF)
23484 tree tdecl
23485 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23486 tdie = lookup_decl_die (tdecl);
23487 if (tdie == NULL)
23488 continue;
23489 arg_decl = tdecl;
23491 else
23492 continue;
23493 if (reg == NULL
23494 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23495 != DEBUG_PARAMETER_REF)
23496 continue;
23497 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23498 VOIDmode,
23499 VAR_INIT_STATUS_INITIALIZED);
23500 if (val == NULL)
23501 continue;
23502 if (die == NULL)
23503 die = gen_call_site_die (decl, subr_die, ca_loc);
23504 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23505 NULL_TREE);
23506 add_desc_attribute (cdie, arg_decl);
23507 if (reg != NULL)
23508 add_AT_loc (cdie, DW_AT_location, reg);
23509 else if (tdie != NULL)
23510 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23511 tdie);
23512 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23513 if (next_arg != XEXP (arg, 1))
23515 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
23516 if (mode == VOIDmode)
23517 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
23518 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
23519 0), 1),
23520 mode, VOIDmode,
23521 VAR_INIT_STATUS_INITIALIZED);
23522 if (val != NULL)
23523 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
23524 val);
23527 if (die == NULL
23528 && (ca_loc->symbol_ref || tloc))
23529 die = gen_call_site_die (decl, subr_die, ca_loc);
23530 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
23532 dw_loc_descr_ref tval = NULL;
23534 if (tloc != NULL_RTX)
23535 tval = mem_loc_descriptor (tloc,
23536 GET_MODE (tloc) == VOIDmode
23537 ? Pmode : GET_MODE (tloc),
23538 VOIDmode,
23539 VAR_INIT_STATUS_INITIALIZED);
23540 if (tval)
23541 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
23542 else if (tlocc != NULL_RTX)
23544 tval = mem_loc_descriptor (tlocc,
23545 GET_MODE (tlocc) == VOIDmode
23546 ? Pmode : GET_MODE (tlocc),
23547 VOIDmode,
23548 VAR_INIT_STATUS_INITIALIZED);
23549 if (tval)
23550 add_AT_loc (die,
23551 dwarf_AT (DW_AT_call_target_clobbered),
23552 tval);
23555 if (die != NULL)
23557 call_site_note_count++;
23558 if (ca_loc->tail_call_p)
23559 tail_call_site_note_count++;
23563 call_arg_locations = NULL;
23564 call_arg_loc_last = NULL;
23565 if (tail_call_site_count >= 0
23566 && tail_call_site_count == tail_call_site_note_count
23567 && (!dwarf_strict || dwarf_version >= 5))
23569 if (call_site_count >= 0
23570 && call_site_count == call_site_note_count)
23571 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
23572 else
23573 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
23575 call_site_count = -1;
23576 tail_call_site_count = -1;
23579 /* Mark used types after we have created DIEs for the functions scopes. */
23580 premark_used_types (DECL_STRUCT_FUNCTION (decl));
23583 /* Returns a hash value for X (which really is a die_struct). */
23585 hashval_t
23586 block_die_hasher::hash (die_struct *d)
23588 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
23591 /* Return nonzero if decl_id and die_parent of die_struct X is the same
23592 as decl_id and die_parent of die_struct Y. */
23594 bool
23595 block_die_hasher::equal (die_struct *x, die_struct *y)
23597 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
23600 /* Hold information about markers for inlined entry points. */
23601 struct GTY ((for_user)) inline_entry_data
23603 /* The block that's the inlined_function_outer_scope for an inlined
23604 function. */
23605 tree block;
23607 /* The label at the inlined entry point. */
23608 const char *label_pfx;
23609 unsigned int label_num;
23611 /* The view number to be used as the inlined entry point. */
23612 var_loc_view view;
23615 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
23617 typedef tree compare_type;
23618 static inline hashval_t hash (const inline_entry_data *);
23619 static inline bool equal (const inline_entry_data *, const_tree);
23622 /* Hash table routines for inline_entry_data. */
23624 inline hashval_t
23625 inline_entry_data_hasher::hash (const inline_entry_data *data)
23627 return htab_hash_pointer (data->block);
23630 inline bool
23631 inline_entry_data_hasher::equal (const inline_entry_data *data,
23632 const_tree block)
23634 return data->block == block;
23637 /* Inlined entry points pending DIE creation in this compilation unit. */
23639 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
23642 /* Return TRUE if DECL, which may have been previously generated as
23643 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
23644 true if decl (or its origin) is either an extern declaration or a
23645 class/namespace scoped declaration.
23647 The declare_in_namespace support causes us to get two DIEs for one
23648 variable, both of which are declarations. We want to avoid
23649 considering one to be a specification, so we must test for
23650 DECLARATION and DW_AT_declaration. */
23651 static inline bool
23652 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
23654 return (old_die && TREE_STATIC (decl) && !declaration
23655 && get_AT_flag (old_die, DW_AT_declaration) == 1);
23658 /* Return true if DECL is a local static. */
23660 static inline bool
23661 local_function_static (tree decl)
23663 gcc_assert (VAR_P (decl));
23664 return TREE_STATIC (decl)
23665 && DECL_CONTEXT (decl)
23666 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
23669 /* Return true iff DECL overrides (presumably completes) the type of
23670 OLD_DIE within CONTEXT_DIE. */
23672 static bool
23673 override_type_for_decl_p (tree decl, dw_die_ref old_die,
23674 dw_die_ref context_die)
23676 tree type = TREE_TYPE (decl);
23677 int cv_quals;
23679 if (decl_by_reference_p (decl))
23681 type = TREE_TYPE (type);
23682 cv_quals = TYPE_UNQUALIFIED;
23684 else
23685 cv_quals = decl_quals (decl);
23687 dw_die_ref type_die = modified_type_die (type,
23688 cv_quals | TYPE_QUALS (type),
23689 false,
23690 context_die);
23692 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
23694 return type_die != old_type_die;
23697 /* Generate a DIE to represent a declared data object.
23698 Either DECL or ORIGIN must be non-null. */
23700 static void
23701 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
23703 HOST_WIDE_INT off = 0;
23704 tree com_decl;
23705 tree decl_or_origin = decl ? decl : origin;
23706 tree ultimate_origin;
23707 dw_die_ref var_die;
23708 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
23709 bool declaration = (DECL_EXTERNAL (decl_or_origin)
23710 || class_or_namespace_scope_p (context_die));
23711 bool specialization_p = false;
23712 bool no_linkage_name = false;
23714 /* While C++ inline static data members have definitions inside of the
23715 class, force the first DIE to be a declaration, then let gen_member_die
23716 reparent it to the class context and call gen_variable_die again
23717 to create the outside of the class DIE for the definition. */
23718 if (!declaration
23719 && old_die == NULL
23720 && decl
23721 && DECL_CONTEXT (decl)
23722 && TYPE_P (DECL_CONTEXT (decl))
23723 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
23725 declaration = true;
23726 if (dwarf_version < 5)
23727 no_linkage_name = true;
23730 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23731 if (decl || ultimate_origin)
23732 origin = ultimate_origin;
23733 com_decl = fortran_common (decl_or_origin, &off);
23735 /* Symbol in common gets emitted as a child of the common block, in the form
23736 of a data member. */
23737 if (com_decl)
23739 dw_die_ref com_die;
23740 dw_loc_list_ref loc = NULL;
23741 die_node com_die_arg;
23743 var_die = lookup_decl_die (decl_or_origin);
23744 if (var_die)
23746 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
23748 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
23749 if (loc)
23751 if (off)
23753 /* Optimize the common case. */
23754 if (single_element_loc_list_p (loc)
23755 && loc->expr->dw_loc_opc == DW_OP_addr
23756 && loc->expr->dw_loc_next == NULL
23757 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
23758 == SYMBOL_REF)
23760 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23761 loc->expr->dw_loc_oprnd1.v.val_addr
23762 = plus_constant (GET_MODE (x), x , off);
23764 else
23765 loc_list_plus_const (loc, off);
23767 add_AT_location_description (var_die, DW_AT_location, loc);
23768 remove_AT (var_die, DW_AT_declaration);
23771 return;
23774 if (common_block_die_table == NULL)
23775 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
23777 com_die_arg.decl_id = DECL_UID (com_decl);
23778 com_die_arg.die_parent = context_die;
23779 com_die = common_block_die_table->find (&com_die_arg);
23780 if (! early_dwarf)
23781 loc = loc_list_from_tree (com_decl, 2, NULL);
23782 if (com_die == NULL)
23784 const char *cnam
23785 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
23786 die_node **slot;
23788 com_die = new_die (DW_TAG_common_block, context_die, decl);
23789 add_name_and_src_coords_attributes (com_die, com_decl);
23790 if (loc)
23792 add_AT_location_description (com_die, DW_AT_location, loc);
23793 /* Avoid sharing the same loc descriptor between
23794 DW_TAG_common_block and DW_TAG_variable. */
23795 loc = loc_list_from_tree (com_decl, 2, NULL);
23797 else if (DECL_EXTERNAL (decl_or_origin))
23798 add_AT_flag (com_die, DW_AT_declaration, 1);
23799 if (want_pubnames ())
23800 add_pubname_string (cnam, com_die); /* ??? needed? */
23801 com_die->decl_id = DECL_UID (com_decl);
23802 slot = common_block_die_table->find_slot (com_die, INSERT);
23803 *slot = com_die;
23805 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
23807 add_AT_location_description (com_die, DW_AT_location, loc);
23808 loc = loc_list_from_tree (com_decl, 2, NULL);
23809 remove_AT (com_die, DW_AT_declaration);
23811 var_die = new_die (DW_TAG_variable, com_die, decl);
23812 add_name_and_src_coords_attributes (var_die, decl_or_origin);
23813 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
23814 decl_quals (decl_or_origin), false,
23815 context_die);
23816 add_alignment_attribute (var_die, decl);
23817 add_AT_flag (var_die, DW_AT_external, 1);
23818 if (loc)
23820 if (off)
23822 /* Optimize the common case. */
23823 if (single_element_loc_list_p (loc)
23824 && loc->expr->dw_loc_opc == DW_OP_addr
23825 && loc->expr->dw_loc_next == NULL
23826 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
23828 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23829 loc->expr->dw_loc_oprnd1.v.val_addr
23830 = plus_constant (GET_MODE (x), x, off);
23832 else
23833 loc_list_plus_const (loc, off);
23835 add_AT_location_description (var_die, DW_AT_location, loc);
23837 else if (DECL_EXTERNAL (decl_or_origin))
23838 add_AT_flag (var_die, DW_AT_declaration, 1);
23839 if (decl)
23840 equate_decl_number_to_die (decl, var_die);
23841 return;
23844 if (old_die)
23846 if (declaration)
23848 /* A declaration that has been previously dumped, needs no
23849 further annotations, since it doesn't need location on
23850 the second pass. */
23851 return;
23853 else if (decl_will_get_specification_p (old_die, decl, declaration)
23854 && !get_AT (old_die, DW_AT_specification))
23856 /* Fall-thru so we can make a new variable die along with a
23857 DW_AT_specification. */
23859 else if (origin && old_die->die_parent != context_die)
23861 /* If we will be creating an inlined instance, we need a
23862 new DIE that will get annotated with
23863 DW_AT_abstract_origin. */
23864 gcc_assert (!DECL_ABSTRACT_P (decl));
23866 else
23868 /* If a DIE was dumped early, it still needs location info.
23869 Skip to where we fill the location bits. */
23870 var_die = old_die;
23872 /* ??? In LTRANS we cannot annotate early created variably
23873 modified type DIEs without copying them and adjusting all
23874 references to them. Thus we dumped them again. Also add a
23875 reference to them but beware of -g0 compile and -g link
23876 in which case the reference will be already present. */
23877 tree type = TREE_TYPE (decl_or_origin);
23878 if (in_lto_p
23879 && ! get_AT (var_die, DW_AT_type)
23880 && variably_modified_type_p
23881 (type, decl_function_context (decl_or_origin)))
23883 if (decl_by_reference_p (decl_or_origin))
23884 add_type_attribute (var_die, TREE_TYPE (type),
23885 TYPE_UNQUALIFIED, false, context_die);
23886 else
23887 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
23888 false, context_die);
23891 goto gen_variable_die_location;
23895 /* For static data members, the declaration in the class is supposed
23896 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
23897 also in DWARF2; the specification should still be DW_TAG_variable
23898 referencing the DW_TAG_member DIE. */
23899 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
23900 var_die = new_die (DW_TAG_member, context_die, decl);
23901 else
23902 var_die = new_die (DW_TAG_variable, context_die, decl);
23904 if (origin != NULL)
23905 add_abstract_origin_attribute (var_die, origin);
23907 /* Loop unrolling can create multiple blocks that refer to the same
23908 static variable, so we must test for the DW_AT_declaration flag.
23910 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
23911 copy decls and set the DECL_ABSTRACT_P flag on them instead of
23912 sharing them.
23914 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
23915 else if (decl_will_get_specification_p (old_die, decl, declaration))
23917 /* This is a definition of a C++ class level static. */
23918 add_AT_specification (var_die, old_die);
23919 specialization_p = true;
23920 if (DECL_NAME (decl))
23922 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23923 struct dwarf_file_data * file_index = lookup_filename (s.file);
23925 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23926 add_AT_file (var_die, DW_AT_decl_file, file_index);
23928 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23929 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23931 if (debug_column_info
23932 && s.column
23933 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23934 != (unsigned) s.column))
23935 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23937 if (old_die->die_tag == DW_TAG_member)
23938 add_linkage_name (var_die, decl);
23941 else
23942 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23944 if ((origin == NULL && !specialization_p)
23945 || (origin != NULL
23946 && !DECL_ABSTRACT_P (decl_or_origin)
23947 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23948 decl_function_context
23949 (decl_or_origin)))
23950 || (old_die && specialization_p
23951 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
23953 tree type = TREE_TYPE (decl_or_origin);
23955 if (decl_by_reference_p (decl_or_origin))
23956 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23957 context_die);
23958 else
23959 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23960 context_die);
23963 if (origin == NULL && !specialization_p)
23965 if (TREE_PUBLIC (decl))
23966 add_AT_flag (var_die, DW_AT_external, 1);
23968 if (DECL_ARTIFICIAL (decl))
23969 add_AT_flag (var_die, DW_AT_artificial, 1);
23971 add_alignment_attribute (var_die, decl);
23973 add_accessibility_attribute (var_die, decl);
23976 if (declaration)
23977 add_AT_flag (var_die, DW_AT_declaration, 1);
23979 if (decl && (DECL_ABSTRACT_P (decl)
23980 || !old_die || is_declaration_die (old_die)))
23981 equate_decl_number_to_die (decl, var_die);
23983 gen_variable_die_location:
23984 if (! declaration
23985 && (! DECL_ABSTRACT_P (decl_or_origin)
23986 /* Local static vars are shared between all clones/inlines,
23987 so emit DW_AT_location on the abstract DIE if DECL_RTL is
23988 already set. */
23989 || (VAR_P (decl_or_origin)
23990 && TREE_STATIC (decl_or_origin)
23991 && DECL_RTL_SET_P (decl_or_origin))))
23993 if (early_dwarf)
23994 add_pubname (decl_or_origin, var_die);
23995 else
23996 add_location_or_const_value_attribute (var_die, decl_or_origin,
23997 decl == NULL);
23999 else
24000 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24002 if ((dwarf_version >= 4 || !dwarf_strict)
24003 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24004 DW_AT_const_expr) == 1
24005 && !get_AT (var_die, DW_AT_const_expr)
24006 && !specialization_p)
24007 add_AT_flag (var_die, DW_AT_const_expr, 1);
24009 if (!dwarf_strict)
24011 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24012 DW_AT_inline);
24013 if (inl != -1
24014 && !get_AT (var_die, DW_AT_inline)
24015 && !specialization_p)
24016 add_AT_unsigned (var_die, DW_AT_inline, inl);
24020 /* Generate a DIE to represent a named constant. */
24022 static void
24023 gen_const_die (tree decl, dw_die_ref context_die)
24025 dw_die_ref const_die;
24026 tree type = TREE_TYPE (decl);
24028 const_die = lookup_decl_die (decl);
24029 if (const_die)
24030 return;
24032 const_die = new_die (DW_TAG_constant, context_die, decl);
24033 equate_decl_number_to_die (decl, const_die);
24034 add_name_and_src_coords_attributes (const_die, decl);
24035 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24036 if (TREE_PUBLIC (decl))
24037 add_AT_flag (const_die, DW_AT_external, 1);
24038 if (DECL_ARTIFICIAL (decl))
24039 add_AT_flag (const_die, DW_AT_artificial, 1);
24040 tree_add_const_value_attribute_for_decl (const_die, decl);
24043 /* Generate a DIE to represent a label identifier. */
24045 static void
24046 gen_label_die (tree decl, dw_die_ref context_die)
24048 tree origin = decl_ultimate_origin (decl);
24049 dw_die_ref lbl_die = lookup_decl_die (decl);
24050 rtx insn;
24051 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24053 if (!lbl_die)
24055 lbl_die = new_die (DW_TAG_label, context_die, decl);
24056 equate_decl_number_to_die (decl, lbl_die);
24058 if (origin != NULL)
24059 add_abstract_origin_attribute (lbl_die, origin);
24060 else
24061 add_name_and_src_coords_attributes (lbl_die, decl);
24064 if (DECL_ABSTRACT_P (decl))
24065 equate_decl_number_to_die (decl, lbl_die);
24066 else if (! early_dwarf)
24068 insn = DECL_RTL_IF_SET (decl);
24070 /* Deleted labels are programmer specified labels which have been
24071 eliminated because of various optimizations. We still emit them
24072 here so that it is possible to put breakpoints on them. */
24073 if (insn
24074 && (LABEL_P (insn)
24075 || ((NOTE_P (insn)
24076 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24078 /* When optimization is enabled (via -O) some parts of the compiler
24079 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
24080 represent source-level labels which were explicitly declared by
24081 the user. This really shouldn't be happening though, so catch
24082 it if it ever does happen. */
24083 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24085 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24086 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24088 else if (insn
24089 && NOTE_P (insn)
24090 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24091 && CODE_LABEL_NUMBER (insn) != -1)
24093 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24094 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24099 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24100 attributes to the DIE for a block STMT, to describe where the inlined
24101 function was called from. This is similar to add_src_coords_attributes. */
24103 static inline void
24104 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24106 /* We can end up with BUILTINS_LOCATION here. */
24107 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24108 return;
24110 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
24112 if (dwarf_version >= 3 || !dwarf_strict)
24114 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24115 add_AT_unsigned (die, DW_AT_call_line, s.line);
24116 if (debug_column_info && s.column)
24117 add_AT_unsigned (die, DW_AT_call_column, s.column);
24122 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24123 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24125 static inline void
24126 add_high_low_attributes (tree stmt, dw_die_ref die)
24128 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24130 if (inline_entry_data **iedp
24131 = !inline_entry_data_table ? NULL
24132 : inline_entry_data_table->find_slot_with_hash (stmt,
24133 htab_hash_pointer (stmt),
24134 NO_INSERT))
24136 inline_entry_data *ied = *iedp;
24137 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24138 gcc_assert (debug_inline_points);
24139 gcc_assert (inlined_function_outer_scope_p (stmt));
24141 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24142 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24144 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24145 && !dwarf_strict)
24147 if (!output_asm_line_debug_info ())
24148 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24149 else
24151 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24152 /* FIXME: this will resolve to a small number. Could we
24153 possibly emit smaller data? Ideally we'd emit a
24154 uleb128, but that would make the size of DIEs
24155 impossible for the compiler to compute, since it's
24156 the assembler that computes the value of the view
24157 label in this case. Ideally, we'd have a single form
24158 encompassing both the address and the view, and
24159 indirecting them through a table might make things
24160 easier, but even that would be more wasteful,
24161 space-wise, than what we have now. */
24162 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24166 inline_entry_data_table->clear_slot (iedp);
24169 if (BLOCK_FRAGMENT_CHAIN (stmt)
24170 && (dwarf_version >= 3 || !dwarf_strict))
24172 tree chain, superblock = NULL_TREE;
24173 dw_die_ref pdie;
24174 dw_attr_node *attr = NULL;
24176 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24178 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24179 BLOCK_NUMBER (stmt));
24180 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24183 /* Optimize duplicate .debug_ranges lists or even tails of
24184 lists. If this BLOCK has same ranges as its supercontext,
24185 lookup DW_AT_ranges attribute in the supercontext (and
24186 recursively so), verify that the ranges_table contains the
24187 right values and use it instead of adding a new .debug_range. */
24188 for (chain = stmt, pdie = die;
24189 BLOCK_SAME_RANGE (chain);
24190 chain = BLOCK_SUPERCONTEXT (chain))
24192 dw_attr_node *new_attr;
24194 pdie = pdie->die_parent;
24195 if (pdie == NULL)
24196 break;
24197 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24198 break;
24199 new_attr = get_AT (pdie, DW_AT_ranges);
24200 if (new_attr == NULL
24201 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24202 break;
24203 attr = new_attr;
24204 superblock = BLOCK_SUPERCONTEXT (chain);
24206 if (attr != NULL
24207 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24208 == (int)BLOCK_NUMBER (superblock))
24209 && BLOCK_FRAGMENT_CHAIN (superblock))
24211 unsigned long off = attr->dw_attr_val.v.val_offset;
24212 unsigned long supercnt = 0, thiscnt = 0;
24213 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24214 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24216 ++supercnt;
24217 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24218 == (int)BLOCK_NUMBER (chain));
24220 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24221 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24222 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24223 ++thiscnt;
24224 gcc_assert (supercnt >= thiscnt);
24225 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24226 false);
24227 note_rnglist_head (off + supercnt - thiscnt);
24228 return;
24231 unsigned int offset = add_ranges (stmt, true);
24232 add_AT_range_list (die, DW_AT_ranges, offset, false);
24233 note_rnglist_head (offset);
24235 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24236 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24239 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24240 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24241 chain = BLOCK_FRAGMENT_CHAIN (chain);
24243 while (chain);
24244 add_ranges (NULL);
24246 else
24248 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24249 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24250 BLOCK_NUMBER (stmt));
24251 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24252 BLOCK_NUMBER (stmt));
24253 add_AT_low_high_pc (die, label, label_high, false);
24257 /* Generate a DIE for a lexical block. */
24259 static void
24260 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24262 dw_die_ref old_die = lookup_block_die (stmt);
24263 dw_die_ref stmt_die = NULL;
24264 if (!old_die)
24266 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24267 equate_block_to_die (stmt, stmt_die);
24270 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24272 /* If this is an inlined or conrecte instance, create a new lexical
24273 die for anything below to attach DW_AT_abstract_origin to. */
24274 if (old_die)
24275 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24277 tree origin = block_ultimate_origin (stmt);
24278 if (origin != NULL_TREE && (origin != stmt || old_die))
24279 add_abstract_origin_attribute (stmt_die, origin);
24281 old_die = NULL;
24284 if (old_die)
24285 stmt_die = old_die;
24287 /* A non abstract block whose blocks have already been reordered
24288 should have the instruction range for this block. If so, set the
24289 high/low attributes. */
24290 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24292 gcc_assert (stmt_die);
24293 add_high_low_attributes (stmt, stmt_die);
24296 decls_for_scope (stmt, stmt_die);
24299 /* Generate a DIE for an inlined subprogram. */
24301 static void
24302 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24304 tree decl = block_ultimate_origin (stmt);
24306 /* Make sure any inlined functions are known to be inlineable. */
24307 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24308 || cgraph_function_possibly_inlined_p (decl));
24310 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24312 if (call_arg_locations || debug_inline_points)
24313 equate_block_to_die (stmt, subr_die);
24314 add_abstract_origin_attribute (subr_die, decl);
24315 if (TREE_ASM_WRITTEN (stmt))
24316 add_high_low_attributes (stmt, subr_die);
24317 add_call_src_coords_attributes (stmt, subr_die);
24319 /* The inliner creates an extra BLOCK for the parameter setup,
24320 we want to merge that with the actual outermost BLOCK of the
24321 inlined function to avoid duplicate locals in consumers.
24322 Do that by doing the recursion to subblocks on the single subblock
24323 of STMT. */
24324 bool unwrap_one = false;
24325 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24327 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24328 if (origin
24329 && TREE_CODE (origin) == BLOCK
24330 && BLOCK_SUPERCONTEXT (origin) == decl)
24331 unwrap_one = true;
24333 decls_for_scope (stmt, subr_die, !unwrap_one);
24334 if (unwrap_one)
24335 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24338 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24339 the comment for VLR_CONTEXT. */
24341 static void
24342 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24344 dw_die_ref decl_die;
24346 if (TREE_TYPE (decl) == error_mark_node)
24347 return;
24349 decl_die = new_die (DW_TAG_member, context_die, decl);
24350 add_name_and_src_coords_attributes (decl_die, decl);
24351 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24352 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24353 context_die);
24355 if (DECL_BIT_FIELD_TYPE (decl))
24357 add_byte_size_attribute (decl_die, decl);
24358 add_bit_size_attribute (decl_die, decl);
24359 add_bit_offset_attribute (decl_die, decl);
24362 add_alignment_attribute (decl_die, decl);
24364 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24365 add_data_member_location_attribute (decl_die, decl, ctx);
24367 if (DECL_ARTIFICIAL (decl))
24368 add_AT_flag (decl_die, DW_AT_artificial, 1);
24370 add_accessibility_attribute (decl_die, decl);
24372 /* Equate decl number to die, so that we can look up this decl later on. */
24373 equate_decl_number_to_die (decl, decl_die);
24376 /* Generate a DIE for a pointer to a member type. TYPE can be an
24377 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24378 pointer to member function. */
24380 static void
24381 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24383 if (lookup_type_die (type))
24384 return;
24386 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24387 scope_die_for (type, context_die), type);
24389 equate_type_number_to_die (type, ptr_die);
24390 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24391 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24392 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24393 context_die);
24394 add_alignment_attribute (ptr_die, type);
24396 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24397 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24399 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24400 add_AT_loc (ptr_die, DW_AT_use_location, op);
24404 static char *producer_string;
24406 /* Return a heap allocated producer string including command line options
24407 if -grecord-gcc-switches. */
24409 static char *
24410 gen_producer_string (void)
24412 size_t j;
24413 auto_vec<const char *> switches;
24414 const char *language_string = lang_hooks.name;
24415 char *producer, *tail;
24416 const char *p;
24417 size_t len = dwarf_record_gcc_switches ? 0 : 3;
24418 size_t plen = strlen (language_string) + 1 + strlen (version_string);
24420 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
24421 switch (save_decoded_options[j].opt_index)
24423 case OPT_o:
24424 case OPT_d:
24425 case OPT_dumpbase:
24426 case OPT_dumpbase_ext:
24427 case OPT_dumpdir:
24428 case OPT_quiet:
24429 case OPT_version:
24430 case OPT_v:
24431 case OPT_w:
24432 case OPT_L:
24433 case OPT_D:
24434 case OPT_I:
24435 case OPT_U:
24436 case OPT_SPECIAL_unknown:
24437 case OPT_SPECIAL_ignore:
24438 case OPT_SPECIAL_warn_removed:
24439 case OPT_SPECIAL_program_name:
24440 case OPT_SPECIAL_input_file:
24441 case OPT_grecord_gcc_switches:
24442 case OPT__output_pch_:
24443 case OPT_fdiagnostics_show_location_:
24444 case OPT_fdiagnostics_show_option:
24445 case OPT_fdiagnostics_show_caret:
24446 case OPT_fdiagnostics_show_labels:
24447 case OPT_fdiagnostics_show_line_numbers:
24448 case OPT_fdiagnostics_color_:
24449 case OPT_fdiagnostics_format_:
24450 case OPT_fverbose_asm:
24451 case OPT____:
24452 case OPT__sysroot_:
24453 case OPT_nostdinc:
24454 case OPT_nostdinc__:
24455 case OPT_fpreprocessed:
24456 case OPT_fltrans_output_list_:
24457 case OPT_fresolution_:
24458 case OPT_fdebug_prefix_map_:
24459 case OPT_fmacro_prefix_map_:
24460 case OPT_ffile_prefix_map_:
24461 case OPT_fcompare_debug:
24462 case OPT_fchecking:
24463 case OPT_fchecking_:
24464 /* Ignore these. */
24465 continue;
24466 case OPT_flto_:
24468 const char *lto_canonical = "-flto";
24469 switches.safe_push (lto_canonical);
24470 len += strlen (lto_canonical) + 1;
24471 break;
24473 default:
24474 if (cl_options[save_decoded_options[j].opt_index].flags
24475 & CL_NO_DWARF_RECORD)
24476 continue;
24477 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
24478 == '-');
24479 switch (save_decoded_options[j].canonical_option[0][1])
24481 case 'M':
24482 case 'i':
24483 case 'W':
24484 continue;
24485 case 'f':
24486 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
24487 "dump", 4) == 0)
24488 continue;
24489 break;
24490 default:
24491 break;
24493 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
24494 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
24495 break;
24498 producer = XNEWVEC (char, plen + 1 + len + 1);
24499 tail = producer;
24500 sprintf (tail, "%s %s", language_string, version_string);
24501 tail += plen;
24503 FOR_EACH_VEC_ELT (switches, j, p)
24505 len = strlen (p);
24506 *tail = ' ';
24507 memcpy (tail + 1, p, len);
24508 tail += len + 1;
24511 *tail = '\0';
24512 return producer;
24515 /* Given a C and/or C++ language/version string return the "highest".
24516 C++ is assumed to be "higher" than C in this case. Used for merging
24517 LTO translation unit languages. */
24518 static const char *
24519 highest_c_language (const char *lang1, const char *lang2)
24521 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24522 return "GNU C++17";
24523 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24524 return "GNU C++14";
24525 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24526 return "GNU C++11";
24527 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24528 return "GNU C++98";
24530 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24531 return "GNU C2X";
24532 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24533 return "GNU C17";
24534 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24535 return "GNU C11";
24536 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24537 return "GNU C99";
24538 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24539 return "GNU C89";
24541 gcc_unreachable ();
24545 /* Generate the DIE for the compilation unit. */
24547 static dw_die_ref
24548 gen_compile_unit_die (const char *filename)
24550 dw_die_ref die;
24551 const char *language_string = lang_hooks.name;
24552 int language;
24554 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24556 if (filename)
24558 add_filename_attribute (die, filename);
24559 /* Don't add cwd for <built-in>. */
24560 if (filename[0] != '<')
24561 add_comp_dir_attribute (die);
24564 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24566 /* If our producer is LTO try to figure out a common language to use
24567 from the global list of translation units. */
24568 if (strcmp (language_string, "GNU GIMPLE") == 0)
24570 unsigned i;
24571 tree t;
24572 const char *common_lang = NULL;
24574 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24576 if (!TRANSLATION_UNIT_LANGUAGE (t))
24577 continue;
24578 if (!common_lang)
24579 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24580 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24582 else if (strncmp (common_lang, "GNU C", 5) == 0
24583 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
24584 /* Mixing C and C++ is ok, use C++ in that case. */
24585 common_lang = highest_c_language (common_lang,
24586 TRANSLATION_UNIT_LANGUAGE (t));
24587 else
24589 /* Fall back to C. */
24590 common_lang = NULL;
24591 break;
24595 if (common_lang)
24596 language_string = common_lang;
24599 language = DW_LANG_C;
24600 if (strncmp (language_string, "GNU C", 5) == 0
24601 && ISDIGIT (language_string[5]))
24603 language = DW_LANG_C89;
24604 if (dwarf_version >= 3 || !dwarf_strict)
24606 if (strcmp (language_string, "GNU C89") != 0)
24607 language = DW_LANG_C99;
24609 if (dwarf_version >= 5 /* || !dwarf_strict */)
24610 if (strcmp (language_string, "GNU C11") == 0
24611 || strcmp (language_string, "GNU C17") == 0
24612 || strcmp (language_string, "GNU C2X"))
24613 language = DW_LANG_C11;
24616 else if (strncmp (language_string, "GNU C++", 7) == 0)
24618 language = DW_LANG_C_plus_plus;
24619 if (dwarf_version >= 5 /* || !dwarf_strict */)
24621 if (strcmp (language_string, "GNU C++11") == 0)
24622 language = DW_LANG_C_plus_plus_11;
24623 else if (strcmp (language_string, "GNU C++14") == 0)
24624 language = DW_LANG_C_plus_plus_14;
24625 else if (strcmp (language_string, "GNU C++17") == 0)
24626 /* For now. */
24627 language = DW_LANG_C_plus_plus_14;
24630 else if (strcmp (language_string, "GNU F77") == 0)
24631 language = DW_LANG_Fortran77;
24632 else if (dwarf_version >= 3 || !dwarf_strict)
24634 if (strcmp (language_string, "GNU Ada") == 0)
24635 language = DW_LANG_Ada95;
24636 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24638 language = DW_LANG_Fortran95;
24639 if (dwarf_version >= 5 /* || !dwarf_strict */)
24641 if (strcmp (language_string, "GNU Fortran2003") == 0)
24642 language = DW_LANG_Fortran03;
24643 else if (strcmp (language_string, "GNU Fortran2008") == 0)
24644 language = DW_LANG_Fortran08;
24647 else if (strcmp (language_string, "GNU Objective-C") == 0)
24648 language = DW_LANG_ObjC;
24649 else if (strcmp (language_string, "GNU Objective-C++") == 0)
24650 language = DW_LANG_ObjC_plus_plus;
24651 else if (strcmp (language_string, "GNU D") == 0)
24652 language = DW_LANG_D;
24653 else if (dwarf_version >= 5 || !dwarf_strict)
24655 if (strcmp (language_string, "GNU Go") == 0)
24656 language = DW_LANG_Go;
24659 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
24660 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24661 language = DW_LANG_Fortran90;
24662 /* Likewise for Ada. */
24663 else if (strcmp (language_string, "GNU Ada") == 0)
24664 language = DW_LANG_Ada83;
24666 add_AT_unsigned (die, DW_AT_language, language);
24668 switch (language)
24670 case DW_LANG_Fortran77:
24671 case DW_LANG_Fortran90:
24672 case DW_LANG_Fortran95:
24673 case DW_LANG_Fortran03:
24674 case DW_LANG_Fortran08:
24675 /* Fortran has case insensitive identifiers and the front-end
24676 lowercases everything. */
24677 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
24678 break;
24679 default:
24680 /* The default DW_ID_case_sensitive doesn't need to be specified. */
24681 break;
24683 return die;
24686 /* Generate the DIE for a base class. */
24688 static void
24689 gen_inheritance_die (tree binfo, tree access, tree type,
24690 dw_die_ref context_die)
24692 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
24693 struct vlr_context ctx = { type, NULL };
24695 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
24696 context_die);
24697 add_data_member_location_attribute (die, binfo, &ctx);
24699 if (BINFO_VIRTUAL_P (binfo))
24700 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
24702 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
24703 children, otherwise the default is DW_ACCESS_public. In DWARF2
24704 the default has always been DW_ACCESS_private. */
24705 if (access == access_public_node)
24707 if (dwarf_version == 2
24708 || context_die->die_tag == DW_TAG_class_type)
24709 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
24711 else if (access == access_protected_node)
24712 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
24713 else if (dwarf_version > 2
24714 && context_die->die_tag != DW_TAG_class_type)
24715 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
24718 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
24719 structure. */
24721 static bool
24722 is_variant_part (tree decl)
24724 return (TREE_CODE (decl) == FIELD_DECL
24725 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
24728 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
24729 return the FIELD_DECL. Return NULL_TREE otherwise. */
24731 static tree
24732 analyze_discr_in_predicate (tree operand, tree struct_type)
24734 while (CONVERT_EXPR_P (operand))
24735 operand = TREE_OPERAND (operand, 0);
24737 /* Match field access to members of struct_type only. */
24738 if (TREE_CODE (operand) == COMPONENT_REF
24739 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
24740 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
24741 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
24742 return TREE_OPERAND (operand, 1);
24743 else
24744 return NULL_TREE;
24747 /* Check that SRC is a constant integer that can be represented as a native
24748 integer constant (either signed or unsigned). If so, store it into DEST and
24749 return true. Return false otherwise. */
24751 static bool
24752 get_discr_value (tree src, dw_discr_value *dest)
24754 tree discr_type = TREE_TYPE (src);
24756 if (lang_hooks.types.get_debug_type)
24758 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
24759 if (debug_type != NULL)
24760 discr_type = debug_type;
24763 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
24764 return false;
24766 /* Signedness can vary between the original type and the debug type. This
24767 can happen for character types in Ada for instance: the character type
24768 used for code generation can be signed, to be compatible with the C one,
24769 but from a debugger point of view, it must be unsigned. */
24770 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
24771 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
24773 if (is_orig_unsigned != is_debug_unsigned)
24774 src = fold_convert (discr_type, src);
24776 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
24777 return false;
24779 dest->pos = is_debug_unsigned;
24780 if (is_debug_unsigned)
24781 dest->v.uval = tree_to_uhwi (src);
24782 else
24783 dest->v.sval = tree_to_shwi (src);
24785 return true;
24788 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
24789 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
24790 store NULL_TREE in DISCR_DECL. Otherwise:
24792 - store the discriminant field in STRUCT_TYPE that controls the variant
24793 part to *DISCR_DECL
24795 - put in *DISCR_LISTS_P an array where for each variant, the item
24796 represents the corresponding matching list of discriminant values.
24798 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
24799 the above array.
24801 Note that when the array is allocated (i.e. when the analysis is
24802 successful), it is up to the caller to free the array. */
24804 static void
24805 analyze_variants_discr (tree variant_part_decl,
24806 tree struct_type,
24807 tree *discr_decl,
24808 dw_discr_list_ref **discr_lists_p,
24809 unsigned *discr_lists_length)
24811 tree variant_part_type = TREE_TYPE (variant_part_decl);
24812 tree variant;
24813 dw_discr_list_ref *discr_lists;
24814 unsigned i;
24816 /* Compute how many variants there are in this variant part. */
24817 *discr_lists_length = 0;
24818 for (variant = TYPE_FIELDS (variant_part_type);
24819 variant != NULL_TREE;
24820 variant = DECL_CHAIN (variant))
24821 ++*discr_lists_length;
24823 *discr_decl = NULL_TREE;
24824 *discr_lists_p
24825 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
24826 sizeof (**discr_lists_p));
24827 discr_lists = *discr_lists_p;
24829 /* And then analyze all variants to extract discriminant information for all
24830 of them. This analysis is conservative: as soon as we detect something we
24831 do not support, abort everything and pretend we found nothing. */
24832 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
24833 variant != NULL_TREE;
24834 variant = DECL_CHAIN (variant), ++i)
24836 tree match_expr = DECL_QUALIFIER (variant);
24838 /* Now, try to analyze the predicate and deduce a discriminant for
24839 it. */
24840 if (match_expr == boolean_true_node)
24841 /* Typically happens for the default variant: it matches all cases that
24842 previous variants rejected. Don't output any matching value for
24843 this one. */
24844 continue;
24846 /* The following loop tries to iterate over each discriminant
24847 possibility: single values or ranges. */
24848 while (match_expr != NULL_TREE)
24850 tree next_round_match_expr;
24851 tree candidate_discr = NULL_TREE;
24852 dw_discr_list_ref new_node = NULL;
24854 /* Possibilities are matched one after the other by nested
24855 TRUTH_ORIF_EXPR expressions. Process the current possibility and
24856 continue with the rest at next iteration. */
24857 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
24859 next_round_match_expr = TREE_OPERAND (match_expr, 0);
24860 match_expr = TREE_OPERAND (match_expr, 1);
24862 else
24863 next_round_match_expr = NULL_TREE;
24865 if (match_expr == boolean_false_node)
24866 /* This sub-expression matches nothing: just wait for the next
24867 one. */
24870 else if (TREE_CODE (match_expr) == EQ_EXPR)
24872 /* We are matching: <discr_field> == <integer_cst>
24873 This sub-expression matches a single value. */
24874 tree integer_cst = TREE_OPERAND (match_expr, 1);
24876 candidate_discr
24877 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
24878 struct_type);
24880 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24881 if (!get_discr_value (integer_cst,
24882 &new_node->dw_discr_lower_bound))
24883 goto abort;
24884 new_node->dw_discr_range = false;
24887 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
24889 /* We are matching:
24890 <discr_field> > <integer_cst>
24891 && <discr_field> < <integer_cst>.
24892 This sub-expression matches the range of values between the
24893 two matched integer constants. Note that comparisons can be
24894 inclusive or exclusive. */
24895 tree candidate_discr_1, candidate_discr_2;
24896 tree lower_cst, upper_cst;
24897 bool lower_cst_included, upper_cst_included;
24898 tree lower_op = TREE_OPERAND (match_expr, 0);
24899 tree upper_op = TREE_OPERAND (match_expr, 1);
24901 /* When the comparison is exclusive, the integer constant is not
24902 the discriminant range bound we are looking for: we will have
24903 to increment or decrement it. */
24904 if (TREE_CODE (lower_op) == GE_EXPR)
24905 lower_cst_included = true;
24906 else if (TREE_CODE (lower_op) == GT_EXPR)
24907 lower_cst_included = false;
24908 else
24909 goto abort;
24911 if (TREE_CODE (upper_op) == LE_EXPR)
24912 upper_cst_included = true;
24913 else if (TREE_CODE (upper_op) == LT_EXPR)
24914 upper_cst_included = false;
24915 else
24916 goto abort;
24918 /* Extract the discriminant from the first operand and check it
24919 is consistant with the same analysis in the second
24920 operand. */
24921 candidate_discr_1
24922 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
24923 struct_type);
24924 candidate_discr_2
24925 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
24926 struct_type);
24927 if (candidate_discr_1 == candidate_discr_2)
24928 candidate_discr = candidate_discr_1;
24929 else
24930 goto abort;
24932 /* Extract bounds from both. */
24933 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24934 lower_cst = TREE_OPERAND (lower_op, 1);
24935 upper_cst = TREE_OPERAND (upper_op, 1);
24937 if (!lower_cst_included)
24938 lower_cst
24939 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
24940 build_int_cst (TREE_TYPE (lower_cst), 1));
24941 if (!upper_cst_included)
24942 upper_cst
24943 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
24944 build_int_cst (TREE_TYPE (upper_cst), 1));
24946 if (!get_discr_value (lower_cst,
24947 &new_node->dw_discr_lower_bound)
24948 || !get_discr_value (upper_cst,
24949 &new_node->dw_discr_upper_bound))
24950 goto abort;
24952 new_node->dw_discr_range = true;
24955 else if ((candidate_discr
24956 = analyze_discr_in_predicate (match_expr, struct_type))
24957 && (TREE_TYPE (candidate_discr) == boolean_type_node
24958 || TREE_TYPE (TREE_TYPE (candidate_discr))
24959 == boolean_type_node))
24961 /* We are matching: <discr_field> for a boolean discriminant.
24962 This sub-expression matches boolean_true_node. */
24963 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24964 if (!get_discr_value (boolean_true_node,
24965 &new_node->dw_discr_lower_bound))
24966 goto abort;
24967 new_node->dw_discr_range = false;
24970 else
24971 /* Unsupported sub-expression: we cannot determine the set of
24972 matching discriminant values. Abort everything. */
24973 goto abort;
24975 /* If the discriminant info is not consistant with what we saw so
24976 far, consider the analysis failed and abort everything. */
24977 if (candidate_discr == NULL_TREE
24978 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
24979 goto abort;
24980 else
24981 *discr_decl = candidate_discr;
24983 if (new_node != NULL)
24985 new_node->dw_discr_next = discr_lists[i];
24986 discr_lists[i] = new_node;
24988 match_expr = next_round_match_expr;
24992 /* If we reach this point, we could match everything we were interested
24993 in. */
24994 return;
24996 abort:
24997 /* Clean all data structure and return no result. */
24998 free (*discr_lists_p);
24999 *discr_lists_p = NULL;
25000 *discr_decl = NULL_TREE;
25003 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25004 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25005 under CONTEXT_DIE.
25007 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25008 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25009 this type, which are record types, represent the available variants and each
25010 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25011 values are inferred from these attributes.
25013 In trees, the offsets for the fields inside these sub-records are relative
25014 to the variant part itself, whereas the corresponding DIEs should have
25015 offset attributes that are relative to the embedding record base address.
25016 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25017 must be an expression that computes the offset of the variant part to
25018 describe in DWARF. */
25020 static void
25021 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25022 dw_die_ref context_die)
25024 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25025 tree variant_part_offset = vlr_ctx->variant_part_offset;
25026 struct loc_descr_context ctx = {
25027 vlr_ctx->struct_type, /* context_type */
25028 NULL_TREE, /* base_decl */
25029 NULL, /* dpi */
25030 false, /* placeholder_arg */
25031 false /* placeholder_seen */
25034 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25035 NULL_TREE if there is no such field. */
25036 tree discr_decl = NULL_TREE;
25037 dw_discr_list_ref *discr_lists;
25038 unsigned discr_lists_length = 0;
25039 unsigned i;
25041 dw_die_ref dwarf_proc_die = NULL;
25042 dw_die_ref variant_part_die
25043 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25045 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25047 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25048 &discr_decl, &discr_lists, &discr_lists_length);
25050 if (discr_decl != NULL_TREE)
25052 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25054 if (discr_die)
25055 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25056 else
25057 /* We have no DIE for the discriminant, so just discard all
25058 discrimimant information in the output. */
25059 discr_decl = NULL_TREE;
25062 /* If the offset for this variant part is more complex than a constant,
25063 create a DWARF procedure for it so that we will not have to generate DWARF
25064 expressions for it for each member. */
25065 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25066 && (dwarf_version >= 3 || !dwarf_strict))
25068 const tree dwarf_proc_fndecl
25069 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25070 build_function_type (TREE_TYPE (variant_part_offset),
25071 NULL_TREE));
25072 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25073 const dw_loc_descr_ref dwarf_proc_body
25074 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25076 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25077 dwarf_proc_fndecl, context_die);
25078 if (dwarf_proc_die != NULL)
25079 variant_part_offset = dwarf_proc_call;
25082 /* Output DIEs for all variants. */
25083 i = 0;
25084 for (tree variant = TYPE_FIELDS (variant_part_type);
25085 variant != NULL_TREE;
25086 variant = DECL_CHAIN (variant), ++i)
25088 tree variant_type = TREE_TYPE (variant);
25089 dw_die_ref variant_die;
25091 /* All variants (i.e. members of a variant part) are supposed to be
25092 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25093 under these records. */
25094 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25096 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25097 equate_decl_number_to_die (variant, variant_die);
25099 /* Output discriminant values this variant matches, if any. */
25100 if (discr_decl == NULL || discr_lists[i] == NULL)
25101 /* In the case we have discriminant information at all, this is
25102 probably the default variant: as the standard says, don't
25103 output any discriminant value/list attribute. */
25105 else if (discr_lists[i]->dw_discr_next == NULL
25106 && !discr_lists[i]->dw_discr_range)
25107 /* If there is only one accepted value, don't bother outputting a
25108 list. */
25109 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25110 else
25111 add_discr_list (variant_die, discr_lists[i]);
25113 for (tree member = TYPE_FIELDS (variant_type);
25114 member != NULL_TREE;
25115 member = DECL_CHAIN (member))
25117 struct vlr_context vlr_sub_ctx = {
25118 vlr_ctx->struct_type, /* struct_type */
25119 NULL /* variant_part_offset */
25121 if (is_variant_part (member))
25123 /* All offsets for fields inside variant parts are relative to
25124 the top-level embedding RECORD_TYPE's base address. On the
25125 other hand, offsets in GCC's types are relative to the
25126 nested-most variant part. So we have to sum offsets each time
25127 we recurse. */
25129 vlr_sub_ctx.variant_part_offset
25130 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25131 variant_part_offset, byte_position (member));
25132 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25134 else
25136 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25137 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25142 free (discr_lists);
25145 /* Generate a DIE for a class member. */
25147 static void
25148 gen_member_die (tree type, dw_die_ref context_die)
25150 tree member;
25151 tree binfo = TYPE_BINFO (type);
25153 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25155 /* If this is not an incomplete type, output descriptions of each of its
25156 members. Note that as we output the DIEs necessary to represent the
25157 members of this record or union type, we will also be trying to output
25158 DIEs to represent the *types* of those members. However the `type'
25159 function (above) will specifically avoid generating type DIEs for member
25160 types *within* the list of member DIEs for this (containing) type except
25161 for those types (of members) which are explicitly marked as also being
25162 members of this (containing) type themselves. The g++ front- end can
25163 force any given type to be treated as a member of some other (containing)
25164 type by setting the TYPE_CONTEXT of the given (member) type to point to
25165 the TREE node representing the appropriate (containing) type. */
25167 /* First output info about the base classes. */
25168 if (binfo && early_dwarf)
25170 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25171 int i;
25172 tree base;
25174 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25175 gen_inheritance_die (base,
25176 (accesses ? (*accesses)[i] : access_public_node),
25177 type,
25178 context_die);
25181 /* Now output info about the members. */
25182 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25184 /* Ignore clones. */
25185 if (DECL_ABSTRACT_ORIGIN (member))
25186 continue;
25188 struct vlr_context vlr_ctx = { type, NULL_TREE };
25189 bool static_inline_p
25190 = (VAR_P (member)
25191 && TREE_STATIC (member)
25192 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25193 != -1));
25195 /* If we thought we were generating minimal debug info for TYPE
25196 and then changed our minds, some of the member declarations
25197 may have already been defined. Don't define them again, but
25198 do put them in the right order. */
25200 if (dw_die_ref child = lookup_decl_die (member))
25202 /* Handle inline static data members, which only have in-class
25203 declarations. */
25204 bool splice = true;
25206 dw_die_ref ref = NULL;
25207 if (child->die_tag == DW_TAG_variable
25208 && child->die_parent == comp_unit_die ())
25210 ref = get_AT_ref (child, DW_AT_specification);
25212 /* For C++17 inline static data members followed by redundant
25213 out of class redeclaration, we might get here with
25214 child being the DIE created for the out of class
25215 redeclaration and with its DW_AT_specification being
25216 the DIE created for in-class definition. We want to
25217 reparent the latter, and don't want to create another
25218 DIE with DW_AT_specification in that case, because
25219 we already have one. */
25220 if (ref
25221 && static_inline_p
25222 && ref->die_tag == DW_TAG_variable
25223 && ref->die_parent == comp_unit_die ()
25224 && get_AT (ref, DW_AT_specification) == NULL)
25226 child = ref;
25227 ref = NULL;
25228 static_inline_p = false;
25231 if (!ref)
25233 reparent_child (child, context_die);
25234 if (dwarf_version < 5)
25235 child->die_tag = DW_TAG_member;
25236 splice = false;
25240 if (splice)
25241 splice_child_die (context_die, child);
25244 /* Do not generate standard DWARF for variant parts if we are generating
25245 the corresponding GNAT encodings: DIEs generated for both would
25246 conflict in our mappings. */
25247 else if (is_variant_part (member)
25248 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
25250 vlr_ctx.variant_part_offset = byte_position (member);
25251 gen_variant_part (member, &vlr_ctx, context_die);
25253 else
25255 vlr_ctx.variant_part_offset = NULL_TREE;
25256 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25259 /* For C++ inline static data members emit immediately a DW_TAG_variable
25260 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25261 DW_AT_specification. */
25262 if (static_inline_p)
25264 int old_extern = DECL_EXTERNAL (member);
25265 DECL_EXTERNAL (member) = 0;
25266 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25267 DECL_EXTERNAL (member) = old_extern;
25272 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25273 is set, we pretend that the type was never defined, so we only get the
25274 member DIEs needed by later specification DIEs. */
25276 static void
25277 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25278 enum debug_info_usage usage)
25280 if (TREE_ASM_WRITTEN (type))
25282 /* Fill in the bound of variable-length fields in late dwarf if
25283 still incomplete. */
25284 if (!early_dwarf && variably_modified_type_p (type, NULL))
25285 for (tree member = TYPE_FIELDS (type);
25286 member;
25287 member = DECL_CHAIN (member))
25288 fill_variable_array_bounds (TREE_TYPE (member));
25289 return;
25292 dw_die_ref type_die = lookup_type_die (type);
25293 dw_die_ref scope_die = 0;
25294 int nested = 0;
25295 int complete = (TYPE_SIZE (type)
25296 && (! TYPE_STUB_DECL (type)
25297 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25298 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25299 complete = complete && should_emit_struct_debug (type, usage);
25301 if (type_die && ! complete)
25302 return;
25304 if (TYPE_CONTEXT (type) != NULL_TREE
25305 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25306 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25307 nested = 1;
25309 scope_die = scope_die_for (type, context_die);
25311 /* Generate child dies for template paramaters. */
25312 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25313 schedule_generic_params_dies_gen (type);
25315 if (! type_die || (nested && is_cu_die (scope_die)))
25316 /* First occurrence of type or toplevel definition of nested class. */
25318 dw_die_ref old_die = type_die;
25320 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25321 ? record_type_tag (type) : DW_TAG_union_type,
25322 scope_die, type);
25323 equate_type_number_to_die (type, type_die);
25324 if (old_die)
25325 add_AT_specification (type_die, old_die);
25326 else
25327 add_name_attribute (type_die, type_tag (type));
25329 else
25330 remove_AT (type_die, DW_AT_declaration);
25332 /* If this type has been completed, then give it a byte_size attribute and
25333 then give a list of members. */
25334 if (complete && !ns_decl)
25336 /* Prevent infinite recursion in cases where the type of some member of
25337 this type is expressed in terms of this type itself. */
25338 TREE_ASM_WRITTEN (type) = 1;
25339 add_byte_size_attribute (type_die, type);
25340 add_alignment_attribute (type_die, type);
25341 if (TYPE_STUB_DECL (type) != NULL_TREE)
25343 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25344 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25347 /* If the first reference to this type was as the return type of an
25348 inline function, then it may not have a parent. Fix this now. */
25349 if (type_die->die_parent == NULL)
25350 add_child_die (scope_die, type_die);
25352 gen_member_die (type, type_die);
25354 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25355 if (TYPE_ARTIFICIAL (type))
25356 add_AT_flag (type_die, DW_AT_artificial, 1);
25358 /* GNU extension: Record what type our vtable lives in. */
25359 if (TYPE_VFIELD (type))
25361 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25363 gen_type_die (vtype, context_die);
25364 add_AT_die_ref (type_die, DW_AT_containing_type,
25365 lookup_type_die (vtype));
25368 else
25370 add_AT_flag (type_die, DW_AT_declaration, 1);
25372 /* We don't need to do this for function-local types. */
25373 if (TYPE_STUB_DECL (type)
25374 && ! decl_function_context (TYPE_STUB_DECL (type)))
25375 vec_safe_push (incomplete_types, type);
25378 if (get_AT (type_die, DW_AT_name))
25379 add_pubtype (type, type_die);
25382 /* Generate a DIE for a subroutine _type_. */
25384 static void
25385 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25387 tree return_type = TREE_TYPE (type);
25388 dw_die_ref subr_die
25389 = new_die (DW_TAG_subroutine_type,
25390 scope_die_for (type, context_die), type);
25392 equate_type_number_to_die (type, subr_die);
25393 add_prototyped_attribute (subr_die, type);
25394 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25395 context_die);
25396 add_alignment_attribute (subr_die, type);
25397 gen_formal_types_die (type, subr_die);
25399 if (get_AT (subr_die, DW_AT_name))
25400 add_pubtype (type, subr_die);
25401 if ((dwarf_version >= 5 || !dwarf_strict)
25402 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25403 add_AT_flag (subr_die, DW_AT_reference, 1);
25404 if ((dwarf_version >= 5 || !dwarf_strict)
25405 && lang_hooks.types.type_dwarf_attribute (type,
25406 DW_AT_rvalue_reference) != -1)
25407 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25410 /* Generate a DIE for a type definition. */
25412 static void
25413 gen_typedef_die (tree decl, dw_die_ref context_die)
25415 dw_die_ref type_die;
25416 tree type;
25418 if (TREE_ASM_WRITTEN (decl))
25420 if (DECL_ORIGINAL_TYPE (decl))
25421 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25422 return;
25425 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25426 checks in process_scope_var and modified_type_die), this should be called
25427 only for original types. */
25428 gcc_assert (decl_ultimate_origin (decl) == NULL
25429 || decl_ultimate_origin (decl) == decl);
25431 TREE_ASM_WRITTEN (decl) = 1;
25432 type_die = new_die (DW_TAG_typedef, context_die, decl);
25434 add_name_and_src_coords_attributes (type_die, decl);
25435 if (DECL_ORIGINAL_TYPE (decl))
25437 type = DECL_ORIGINAL_TYPE (decl);
25438 if (type == error_mark_node)
25439 return;
25441 gcc_assert (type != TREE_TYPE (decl));
25442 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25444 else
25446 type = TREE_TYPE (decl);
25447 if (type == error_mark_node)
25448 return;
25450 if (is_naming_typedef_decl (TYPE_NAME (type)))
25452 /* Here, we are in the case of decl being a typedef naming
25453 an anonymous type, e.g:
25454 typedef struct {...} foo;
25455 In that case TREE_TYPE (decl) is not a typedef variant
25456 type and TYPE_NAME of the anonymous type is set to the
25457 TYPE_DECL of the typedef. This construct is emitted by
25458 the C++ FE.
25460 TYPE is the anonymous struct named by the typedef
25461 DECL. As we need the DW_AT_type attribute of the
25462 DW_TAG_typedef to point to the DIE of TYPE, let's
25463 generate that DIE right away. add_type_attribute
25464 called below will then pick (via lookup_type_die) that
25465 anonymous struct DIE. */
25466 if (!TREE_ASM_WRITTEN (type))
25467 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25469 /* This is a GNU Extension. We are adding a
25470 DW_AT_linkage_name attribute to the DIE of the
25471 anonymous struct TYPE. The value of that attribute
25472 is the name of the typedef decl naming the anonymous
25473 struct. This greatly eases the work of consumers of
25474 this debug info. */
25475 add_linkage_name_raw (lookup_type_die (type), decl);
25479 add_type_attribute (type_die, type, decl_quals (decl), false,
25480 context_die);
25482 if (is_naming_typedef_decl (decl))
25483 /* We want that all subsequent calls to lookup_type_die with
25484 TYPE in argument yield the DW_TAG_typedef we have just
25485 created. */
25486 equate_type_number_to_die (type, type_die);
25488 add_alignment_attribute (type_die, TREE_TYPE (decl));
25490 add_accessibility_attribute (type_die, decl);
25492 if (DECL_ABSTRACT_P (decl))
25493 equate_decl_number_to_die (decl, type_die);
25495 if (get_AT (type_die, DW_AT_name))
25496 add_pubtype (decl, type_die);
25499 /* Generate a DIE for a struct, class, enum or union type. */
25501 static void
25502 gen_tagged_type_die (tree type,
25503 dw_die_ref context_die,
25504 enum debug_info_usage usage)
25506 if (type == NULL_TREE
25507 || !is_tagged_type (type))
25508 return;
25510 if (TREE_ASM_WRITTEN (type))
25512 /* If this is a nested type whose containing class hasn't been written
25513 out yet, writing it out will cover this one, too. This does not apply
25514 to instantiations of member class templates; they need to be added to
25515 the containing class as they are generated. FIXME: This hurts the
25516 idea of combining type decls from multiple TUs, since we can't predict
25517 what set of template instantiations we'll get. */
25518 else if (TYPE_CONTEXT (type)
25519 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25520 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25522 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25524 if (TREE_ASM_WRITTEN (type))
25525 return;
25527 /* If that failed, attach ourselves to the stub. */
25528 context_die = lookup_type_die (TYPE_CONTEXT (type));
25530 else if (TYPE_CONTEXT (type) != NULL_TREE
25531 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25533 /* If this type is local to a function that hasn't been written
25534 out yet, use a NULL context for now; it will be fixed up in
25535 decls_for_scope. */
25536 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25537 /* A declaration DIE doesn't count; nested types need to go in the
25538 specification. */
25539 if (context_die && is_declaration_die (context_die))
25540 context_die = NULL;
25542 else
25543 context_die = declare_in_namespace (type, context_die);
25545 if (TREE_CODE (type) == ENUMERAL_TYPE)
25547 /* This might have been written out by the call to
25548 declare_in_namespace. */
25549 if (!TREE_ASM_WRITTEN (type))
25550 gen_enumeration_type_die (type, context_die);
25552 else
25553 gen_struct_or_union_type_die (type, context_die, usage);
25555 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25556 it up if it is ever completed. gen_*_type_die will set it for us
25557 when appropriate. */
25560 /* Generate a type description DIE. */
25562 static void
25563 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25564 enum debug_info_usage usage)
25566 struct array_descr_info info;
25568 if (type == NULL_TREE || type == error_mark_node)
25569 return;
25571 if (flag_checking && type)
25572 verify_type (type);
25574 if (TYPE_NAME (type) != NULL_TREE
25575 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25576 && is_redundant_typedef (TYPE_NAME (type))
25577 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25578 /* The DECL of this type is a typedef we don't want to emit debug
25579 info for but we want debug info for its underlying typedef.
25580 This can happen for e.g, the injected-class-name of a C++
25581 type. */
25582 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25584 /* If TYPE is a typedef type variant, let's generate debug info
25585 for the parent typedef which TYPE is a type of. */
25586 if (typedef_variant_p (type))
25588 if (TREE_ASM_WRITTEN (type))
25589 return;
25591 tree name = TYPE_NAME (type);
25592 tree origin = decl_ultimate_origin (name);
25593 if (origin != NULL && origin != name)
25595 gen_decl_die (origin, NULL, NULL, context_die);
25596 return;
25599 /* Prevent broken recursion; we can't hand off to the same type. */
25600 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25602 /* Give typedefs the right scope. */
25603 context_die = scope_die_for (type, context_die);
25605 TREE_ASM_WRITTEN (type) = 1;
25607 gen_decl_die (name, NULL, NULL, context_die);
25608 return;
25611 /* If type is an anonymous tagged type named by a typedef, let's
25612 generate debug info for the typedef. */
25613 if (is_naming_typedef_decl (TYPE_NAME (type)))
25615 /* Give typedefs the right scope. */
25616 context_die = scope_die_for (type, context_die);
25618 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
25619 return;
25622 if (lang_hooks.types.get_debug_type)
25624 tree debug_type = lang_hooks.types.get_debug_type (type);
25626 if (debug_type != NULL_TREE && debug_type != type)
25628 gen_type_die_with_usage (debug_type, context_die, usage);
25629 return;
25633 /* We are going to output a DIE to represent the unqualified version
25634 of this type (i.e. without any const or volatile qualifiers) so
25635 get the main variant (i.e. the unqualified version) of this type
25636 now. (Vectors and arrays are special because the debugging info is in the
25637 cloned type itself. Similarly function/method types can contain extra
25638 ref-qualification). */
25639 if (TREE_CODE (type) == FUNCTION_TYPE
25640 || TREE_CODE (type) == METHOD_TYPE)
25642 /* For function/method types, can't use type_main_variant here,
25643 because that can have different ref-qualifiers for C++,
25644 but try to canonicalize. */
25645 tree main = TYPE_MAIN_VARIANT (type);
25646 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
25647 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
25648 && check_base_type (t, main)
25649 && check_lang_type (t, type))
25651 type = t;
25652 break;
25655 else if (TREE_CODE (type) != VECTOR_TYPE
25656 && TREE_CODE (type) != ARRAY_TYPE)
25657 type = type_main_variant (type);
25659 /* If this is an array type with hidden descriptor, handle it first. */
25660 if (!TREE_ASM_WRITTEN (type)
25661 && lang_hooks.types.get_array_descr_info)
25663 memset (&info, 0, sizeof (info));
25664 if (lang_hooks.types.get_array_descr_info (type, &info))
25666 /* Fortran sometimes emits array types with no dimension. */
25667 gcc_assert (info.ndimensions >= 0
25668 && (info.ndimensions
25669 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
25670 gen_descr_array_type_die (type, &info, context_die);
25671 TREE_ASM_WRITTEN (type) = 1;
25672 return;
25676 if (TREE_ASM_WRITTEN (type))
25678 /* Variable-length types may be incomplete even if
25679 TREE_ASM_WRITTEN. For such types, fall through to
25680 gen_array_type_die() and possibly fill in
25681 DW_AT_{upper,lower}_bound attributes. */
25682 if ((TREE_CODE (type) != ARRAY_TYPE
25683 && TREE_CODE (type) != RECORD_TYPE
25684 && TREE_CODE (type) != UNION_TYPE
25685 && TREE_CODE (type) != QUAL_UNION_TYPE)
25686 || !variably_modified_type_p (type, NULL))
25687 return;
25690 switch (TREE_CODE (type))
25692 case ERROR_MARK:
25693 break;
25695 case POINTER_TYPE:
25696 case REFERENCE_TYPE:
25697 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
25698 ensures that the gen_type_die recursion will terminate even if the
25699 type is recursive. Recursive types are possible in Ada. */
25700 /* ??? We could perhaps do this for all types before the switch
25701 statement. */
25702 TREE_ASM_WRITTEN (type) = 1;
25704 /* For these types, all that is required is that we output a DIE (or a
25705 set of DIEs) to represent the "basis" type. */
25706 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25707 DINFO_USAGE_IND_USE);
25708 break;
25710 case OFFSET_TYPE:
25711 /* This code is used for C++ pointer-to-data-member types.
25712 Output a description of the relevant class type. */
25713 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
25714 DINFO_USAGE_IND_USE);
25716 /* Output a description of the type of the object pointed to. */
25717 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25718 DINFO_USAGE_IND_USE);
25720 /* Now output a DIE to represent this pointer-to-data-member type
25721 itself. */
25722 gen_ptr_to_mbr_type_die (type, context_die);
25723 break;
25725 case FUNCTION_TYPE:
25726 /* Force out return type (in case it wasn't forced out already). */
25727 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25728 DINFO_USAGE_DIR_USE);
25729 gen_subroutine_type_die (type, context_die);
25730 break;
25732 case METHOD_TYPE:
25733 /* Force out return type (in case it wasn't forced out already). */
25734 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25735 DINFO_USAGE_DIR_USE);
25736 gen_subroutine_type_die (type, context_die);
25737 break;
25739 case ARRAY_TYPE:
25740 case VECTOR_TYPE:
25741 gen_array_type_die (type, context_die);
25742 break;
25744 case ENUMERAL_TYPE:
25745 case RECORD_TYPE:
25746 case UNION_TYPE:
25747 case QUAL_UNION_TYPE:
25748 gen_tagged_type_die (type, context_die, usage);
25749 return;
25751 case VOID_TYPE:
25752 case INTEGER_TYPE:
25753 case REAL_TYPE:
25754 case FIXED_POINT_TYPE:
25755 case COMPLEX_TYPE:
25756 case BOOLEAN_TYPE:
25757 /* No DIEs needed for fundamental types. */
25758 break;
25760 case NULLPTR_TYPE:
25761 case LANG_TYPE:
25762 /* Just use DW_TAG_unspecified_type. */
25764 dw_die_ref type_die = lookup_type_die (type);
25765 if (type_die == NULL)
25767 tree name = TYPE_IDENTIFIER (type);
25768 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
25769 type);
25770 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
25771 equate_type_number_to_die (type, type_die);
25774 break;
25776 default:
25777 if (is_cxx_auto (type))
25779 tree name = TYPE_IDENTIFIER (type);
25780 dw_die_ref *die = (name == get_identifier ("auto")
25781 ? &auto_die : &decltype_auto_die);
25782 if (!*die)
25784 *die = new_die (DW_TAG_unspecified_type,
25785 comp_unit_die (), NULL_TREE);
25786 add_name_attribute (*die, IDENTIFIER_POINTER (name));
25788 equate_type_number_to_die (type, *die);
25789 break;
25791 gcc_unreachable ();
25794 TREE_ASM_WRITTEN (type) = 1;
25797 static void
25798 gen_type_die (tree type, dw_die_ref context_die)
25800 if (type != error_mark_node)
25802 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
25803 if (flag_checking)
25805 dw_die_ref die = lookup_type_die (type);
25806 if (die)
25807 check_die (die);
25812 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
25813 things which are local to the given block. */
25815 static void
25816 gen_block_die (tree stmt, dw_die_ref context_die)
25818 int must_output_die = 0;
25819 bool inlined_func;
25821 /* Ignore blocks that are NULL. */
25822 if (stmt == NULL_TREE)
25823 return;
25825 inlined_func = inlined_function_outer_scope_p (stmt);
25827 /* If the block is one fragment of a non-contiguous block, do not
25828 process the variables, since they will have been done by the
25829 origin block. Do process subblocks. */
25830 if (BLOCK_FRAGMENT_ORIGIN (stmt))
25832 tree sub;
25834 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
25835 gen_block_die (sub, context_die);
25837 return;
25840 /* Determine if we need to output any Dwarf DIEs at all to represent this
25841 block. */
25842 if (inlined_func)
25843 /* The outer scopes for inlinings *must* always be represented. We
25844 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
25845 must_output_die = 1;
25846 else if (lookup_block_die (stmt))
25847 /* If we already have a DIE then it was filled early. Meanwhile
25848 we might have pruned all BLOCK_VARS as optimized out but we
25849 still want to generate high/low PC attributes so output it. */
25850 must_output_die = 1;
25851 else if (TREE_USED (stmt)
25852 || TREE_ASM_WRITTEN (stmt))
25854 /* Determine if this block directly contains any "significant"
25855 local declarations which we will need to output DIEs for. */
25856 if (debug_info_level > DINFO_LEVEL_TERSE)
25858 /* We are not in terse mode so any local declaration that
25859 is not ignored for debug purposes counts as being a
25860 "significant" one. */
25861 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
25862 must_output_die = 1;
25863 else
25864 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
25865 if (!DECL_IGNORED_P (var))
25867 must_output_die = 1;
25868 break;
25871 else if (!dwarf2out_ignore_block (stmt))
25872 must_output_die = 1;
25875 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
25876 DIE for any block which contains no significant local declarations at
25877 all. Rather, in such cases we just call `decls_for_scope' so that any
25878 needed Dwarf info for any sub-blocks will get properly generated. Note
25879 that in terse mode, our definition of what constitutes a "significant"
25880 local declaration gets restricted to include only inlined function
25881 instances and local (nested) function definitions. */
25882 if (must_output_die)
25884 if (inlined_func)
25885 gen_inlined_subroutine_die (stmt, context_die);
25886 else
25887 gen_lexical_block_die (stmt, context_die);
25889 else
25890 decls_for_scope (stmt, context_die);
25893 /* Process variable DECL (or variable with origin ORIGIN) within
25894 block STMT and add it to CONTEXT_DIE. */
25895 static void
25896 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
25898 dw_die_ref die;
25899 tree decl_or_origin = decl ? decl : origin;
25901 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
25902 die = lookup_decl_die (decl_or_origin);
25903 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
25905 if (TYPE_DECL_IS_STUB (decl_or_origin))
25906 die = lookup_type_die (TREE_TYPE (decl_or_origin));
25907 else
25908 die = lookup_decl_die (decl_or_origin);
25909 /* Avoid re-creating the DIE late if it was optimized as unused early. */
25910 if (! die && ! early_dwarf)
25911 return;
25913 else
25914 die = NULL;
25916 /* Avoid creating DIEs for local typedefs and concrete static variables that
25917 will only be pruned later. */
25918 if ((origin || decl_ultimate_origin (decl))
25919 && (TREE_CODE (decl_or_origin) == TYPE_DECL
25920 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
25922 origin = decl_ultimate_origin (decl_or_origin);
25923 if (decl && VAR_P (decl) && die != NULL)
25925 die = lookup_decl_die (origin);
25926 if (die != NULL)
25927 equate_decl_number_to_die (decl, die);
25929 return;
25932 if (die != NULL && die->die_parent == NULL)
25933 add_child_die (context_die, die);
25934 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
25936 if (early_dwarf)
25937 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
25938 stmt, context_die);
25940 else
25942 if (decl && DECL_P (decl))
25944 die = lookup_decl_die (decl);
25946 /* Early created DIEs do not have a parent as the decls refer
25947 to the function as DECL_CONTEXT rather than the BLOCK. */
25948 if (die && die->die_parent == NULL)
25950 gcc_assert (in_lto_p);
25951 add_child_die (context_die, die);
25955 gen_decl_die (decl, origin, NULL, context_die);
25959 /* Generate all of the decls declared within a given scope and (recursively)
25960 all of its sub-blocks. */
25962 static void
25963 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
25965 tree decl;
25966 unsigned int i;
25967 tree subblocks;
25969 /* Ignore NULL blocks. */
25970 if (stmt == NULL_TREE)
25971 return;
25973 /* Output the DIEs to represent all of the data objects and typedefs
25974 declared directly within this block but not within any nested
25975 sub-blocks. Also, nested function and tag DIEs have been
25976 generated with a parent of NULL; fix that up now. We don't
25977 have to do this if we're at -g1. */
25978 if (debug_info_level > DINFO_LEVEL_TERSE)
25980 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25981 process_scope_var (stmt, decl, NULL_TREE, context_die);
25982 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25983 origin - avoid doing this twice as we have no good way to see
25984 if we've done it once already. */
25985 if (! early_dwarf)
25986 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25988 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25989 if (decl == current_function_decl)
25990 /* Ignore declarations of the current function, while they
25991 are declarations, gen_subprogram_die would treat them
25992 as definitions again, because they are equal to
25993 current_function_decl and endlessly recurse. */;
25994 else if (TREE_CODE (decl) == FUNCTION_DECL)
25995 process_scope_var (stmt, decl, NULL_TREE, context_die);
25996 else
25997 process_scope_var (stmt, NULL_TREE, decl, context_die);
26001 /* Even if we're at -g1, we need to process the subblocks in order to get
26002 inlined call information. */
26004 /* Output the DIEs to represent all sub-blocks (and the items declared
26005 therein) of this block. */
26006 if (recurse)
26007 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26008 subblocks != NULL;
26009 subblocks = BLOCK_CHAIN (subblocks))
26010 gen_block_die (subblocks, context_die);
26013 /* Is this a typedef we can avoid emitting? */
26015 static bool
26016 is_redundant_typedef (const_tree decl)
26018 if (TYPE_DECL_IS_STUB (decl))
26019 return true;
26021 if (DECL_ARTIFICIAL (decl)
26022 && DECL_CONTEXT (decl)
26023 && is_tagged_type (DECL_CONTEXT (decl))
26024 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26025 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26026 /* Also ignore the artificial member typedef for the class name. */
26027 return true;
26029 return false;
26032 /* Return TRUE if TYPE is a typedef that names a type for linkage
26033 purposes. This kind of typedefs is produced by the C++ FE for
26034 constructs like:
26036 typedef struct {...} foo;
26038 In that case, there is no typedef variant type produced for foo.
26039 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26040 struct type. */
26042 static bool
26043 is_naming_typedef_decl (const_tree decl)
26045 if (decl == NULL_TREE
26046 || TREE_CODE (decl) != TYPE_DECL
26047 || DECL_NAMELESS (decl)
26048 || !is_tagged_type (TREE_TYPE (decl))
26049 || DECL_IS_UNDECLARED_BUILTIN (decl)
26050 || is_redundant_typedef (decl)
26051 /* It looks like Ada produces TYPE_DECLs that are very similar
26052 to C++ naming typedefs but that have different
26053 semantics. Let's be specific to c++ for now. */
26054 || !is_cxx (decl))
26055 return FALSE;
26057 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26058 && TYPE_NAME (TREE_TYPE (decl)) == decl
26059 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26060 != TYPE_NAME (TREE_TYPE (decl))));
26063 /* Looks up the DIE for a context. */
26065 static inline dw_die_ref
26066 lookup_context_die (tree context)
26068 if (context)
26070 /* Find die that represents this context. */
26071 if (TYPE_P (context))
26073 context = TYPE_MAIN_VARIANT (context);
26074 dw_die_ref ctx = lookup_type_die (context);
26075 if (!ctx)
26076 return NULL;
26077 return strip_naming_typedef (context, ctx);
26079 else
26080 return lookup_decl_die (context);
26082 return comp_unit_die ();
26085 /* Returns the DIE for a context. */
26087 static inline dw_die_ref
26088 get_context_die (tree context)
26090 if (context)
26092 /* Find die that represents this context. */
26093 if (TYPE_P (context))
26095 context = TYPE_MAIN_VARIANT (context);
26096 return strip_naming_typedef (context, force_type_die (context));
26098 else
26099 return force_decl_die (context);
26101 return comp_unit_die ();
26104 /* Returns the DIE for decl. A DIE will always be returned. */
26106 static dw_die_ref
26107 force_decl_die (tree decl)
26109 dw_die_ref decl_die;
26110 unsigned saved_external_flag;
26111 tree save_fn = NULL_TREE;
26112 decl_die = lookup_decl_die (decl);
26113 if (!decl_die)
26115 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26117 decl_die = lookup_decl_die (decl);
26118 if (decl_die)
26119 return decl_die;
26121 switch (TREE_CODE (decl))
26123 case FUNCTION_DECL:
26124 /* Clear current_function_decl, so that gen_subprogram_die thinks
26125 that this is a declaration. At this point, we just want to force
26126 declaration die. */
26127 save_fn = current_function_decl;
26128 current_function_decl = NULL_TREE;
26129 gen_subprogram_die (decl, context_die);
26130 current_function_decl = save_fn;
26131 break;
26133 case VAR_DECL:
26134 /* Set external flag to force declaration die. Restore it after
26135 gen_decl_die() call. */
26136 saved_external_flag = DECL_EXTERNAL (decl);
26137 DECL_EXTERNAL (decl) = 1;
26138 gen_decl_die (decl, NULL, NULL, context_die);
26139 DECL_EXTERNAL (decl) = saved_external_flag;
26140 break;
26142 case NAMESPACE_DECL:
26143 if (dwarf_version >= 3 || !dwarf_strict)
26144 dwarf2out_decl (decl);
26145 else
26146 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26147 decl_die = comp_unit_die ();
26148 break;
26150 case TRANSLATION_UNIT_DECL:
26151 decl_die = comp_unit_die ();
26152 break;
26154 default:
26155 gcc_unreachable ();
26158 /* We should be able to find the DIE now. */
26159 if (!decl_die)
26160 decl_die = lookup_decl_die (decl);
26161 gcc_assert (decl_die);
26164 return decl_die;
26167 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26168 always returned. */
26170 static dw_die_ref
26171 force_type_die (tree type)
26173 dw_die_ref type_die;
26175 type_die = lookup_type_die (type);
26176 if (!type_die)
26178 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26180 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26181 false, context_die);
26182 gcc_assert (type_die);
26184 return type_die;
26187 /* Force out any required namespaces to be able to output DECL,
26188 and return the new context_die for it, if it's changed. */
26190 static dw_die_ref
26191 setup_namespace_context (tree thing, dw_die_ref context_die)
26193 tree context = (DECL_P (thing)
26194 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26195 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26196 /* Force out the namespace. */
26197 context_die = force_decl_die (context);
26199 return context_die;
26202 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26203 type) within its namespace, if appropriate.
26205 For compatibility with older debuggers, namespace DIEs only contain
26206 declarations; all definitions are emitted at CU scope, with
26207 DW_AT_specification pointing to the declaration (like with class
26208 members). */
26210 static dw_die_ref
26211 declare_in_namespace (tree thing, dw_die_ref context_die)
26213 dw_die_ref ns_context;
26215 if (debug_info_level <= DINFO_LEVEL_TERSE)
26216 return context_die;
26218 /* External declarations in the local scope only need to be emitted
26219 once, not once in the namespace and once in the scope.
26221 This avoids declaring the `extern' below in the
26222 namespace DIE as well as in the innermost scope:
26224 namespace S
26226 int i=5;
26227 int foo()
26229 int i=8;
26230 extern int i;
26231 return i;
26235 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26236 return context_die;
26238 /* If this decl is from an inlined function, then don't try to emit it in its
26239 namespace, as we will get confused. It would have already been emitted
26240 when the abstract instance of the inline function was emitted anyways. */
26241 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26242 return context_die;
26244 ns_context = setup_namespace_context (thing, context_die);
26246 if (ns_context != context_die)
26248 if (is_fortran () || is_dlang ())
26249 return ns_context;
26250 if (DECL_P (thing))
26251 gen_decl_die (thing, NULL, NULL, ns_context);
26252 else
26253 gen_type_die (thing, ns_context);
26255 return context_die;
26258 /* Generate a DIE for a namespace or namespace alias. */
26260 static void
26261 gen_namespace_die (tree decl, dw_die_ref context_die)
26263 dw_die_ref namespace_die;
26265 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26266 they are an alias of. */
26267 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26269 /* Output a real namespace or module. */
26270 context_die = setup_namespace_context (decl, comp_unit_die ());
26271 namespace_die = new_die (is_fortran () || is_dlang ()
26272 ? DW_TAG_module : DW_TAG_namespace,
26273 context_die, decl);
26274 /* For Fortran modules defined in different CU don't add src coords. */
26275 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26277 const char *name = dwarf2_name (decl, 0);
26278 if (name)
26279 add_name_attribute (namespace_die, name);
26281 else
26282 add_name_and_src_coords_attributes (namespace_die, decl);
26283 if (DECL_EXTERNAL (decl))
26284 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26285 equate_decl_number_to_die (decl, namespace_die);
26287 else
26289 /* Output a namespace alias. */
26291 /* Force out the namespace we are an alias of, if necessary. */
26292 dw_die_ref origin_die
26293 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26295 if (DECL_FILE_SCOPE_P (decl)
26296 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26297 context_die = setup_namespace_context (decl, comp_unit_die ());
26298 /* Now create the namespace alias DIE. */
26299 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26300 add_name_and_src_coords_attributes (namespace_die, decl);
26301 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26302 equate_decl_number_to_die (decl, namespace_die);
26304 if ((dwarf_version >= 5 || !dwarf_strict)
26305 && lang_hooks.decls.decl_dwarf_attribute (decl,
26306 DW_AT_export_symbols) == 1)
26307 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26309 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26310 if (want_pubnames ())
26311 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26314 /* Generate Dwarf debug information for a decl described by DECL.
26315 The return value is currently only meaningful for PARM_DECLs,
26316 for all other decls it returns NULL.
26318 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26319 It can be NULL otherwise. */
26321 static dw_die_ref
26322 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26323 dw_die_ref context_die)
26325 tree decl_or_origin = decl ? decl : origin;
26326 tree class_origin = NULL, ultimate_origin;
26328 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26329 return NULL;
26331 switch (TREE_CODE (decl_or_origin))
26333 case ERROR_MARK:
26334 break;
26336 case CONST_DECL:
26337 if (!is_fortran () && !is_ada () && !is_dlang ())
26339 /* The individual enumerators of an enum type get output when we output
26340 the Dwarf representation of the relevant enum type itself. */
26341 break;
26344 /* Emit its type. */
26345 gen_type_die (TREE_TYPE (decl), context_die);
26347 /* And its containing namespace. */
26348 context_die = declare_in_namespace (decl, context_die);
26350 gen_const_die (decl, context_die);
26351 break;
26353 case FUNCTION_DECL:
26354 #if 0
26355 /* FIXME */
26356 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26357 on local redeclarations of global functions. That seems broken. */
26358 if (current_function_decl != decl)
26359 /* This is only a declaration. */;
26360 #endif
26362 /* We should have abstract copies already and should not generate
26363 stray type DIEs in late LTO dumping. */
26364 if (! early_dwarf)
26367 /* If we're emitting a clone, emit info for the abstract instance. */
26368 else if (origin || DECL_ORIGIN (decl) != decl)
26369 dwarf2out_abstract_function (origin
26370 ? DECL_ORIGIN (origin)
26371 : DECL_ABSTRACT_ORIGIN (decl));
26373 /* If we're emitting a possibly inlined function emit it as
26374 abstract instance. */
26375 else if (cgraph_function_possibly_inlined_p (decl)
26376 && ! DECL_ABSTRACT_P (decl)
26377 && ! class_or_namespace_scope_p (context_die)
26378 /* dwarf2out_abstract_function won't emit a die if this is just
26379 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26380 that case, because that works only if we have a die. */
26381 && DECL_INITIAL (decl) != NULL_TREE)
26382 dwarf2out_abstract_function (decl);
26384 /* Otherwise we're emitting the primary DIE for this decl. */
26385 else if (debug_info_level > DINFO_LEVEL_TERSE)
26387 /* Before we describe the FUNCTION_DECL itself, make sure that we
26388 have its containing type. */
26389 if (!origin)
26390 origin = decl_class_context (decl);
26391 if (origin != NULL_TREE)
26392 gen_type_die (origin, context_die);
26394 /* And its return type. */
26395 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26397 /* And its virtual context. */
26398 if (DECL_VINDEX (decl) != NULL_TREE)
26399 gen_type_die (DECL_CONTEXT (decl), context_die);
26401 /* Make sure we have a member DIE for decl. */
26402 if (origin != NULL_TREE)
26403 gen_type_die_for_member (origin, decl, context_die);
26405 /* And its containing namespace. */
26406 context_die = declare_in_namespace (decl, context_die);
26409 /* Now output a DIE to represent the function itself. */
26410 if (decl)
26411 gen_subprogram_die (decl, context_die);
26412 break;
26414 case TYPE_DECL:
26415 /* If we are in terse mode, don't generate any DIEs to represent any
26416 actual typedefs. */
26417 if (debug_info_level <= DINFO_LEVEL_TERSE)
26418 break;
26420 /* In the special case of a TYPE_DECL node representing the declaration
26421 of some type tag, if the given TYPE_DECL is marked as having been
26422 instantiated from some other (original) TYPE_DECL node (e.g. one which
26423 was generated within the original definition of an inline function) we
26424 used to generate a special (abbreviated) DW_TAG_structure_type,
26425 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26426 should be actually referencing those DIEs, as variable DIEs with that
26427 type would be emitted already in the abstract origin, so it was always
26428 removed during unused type prunning. Don't add anything in this
26429 case. */
26430 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26431 break;
26433 if (is_redundant_typedef (decl))
26434 gen_type_die (TREE_TYPE (decl), context_die);
26435 else
26436 /* Output a DIE to represent the typedef itself. */
26437 gen_typedef_die (decl, context_die);
26438 break;
26440 case LABEL_DECL:
26441 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26442 gen_label_die (decl, context_die);
26443 break;
26445 case VAR_DECL:
26446 case RESULT_DECL:
26447 /* If we are in terse mode, don't generate any DIEs to represent any
26448 variable declarations or definitions unless it is external. */
26449 if (debug_info_level < DINFO_LEVEL_TERSE
26450 || (debug_info_level == DINFO_LEVEL_TERSE
26451 && !TREE_PUBLIC (decl_or_origin)))
26452 break;
26454 if (debug_info_level > DINFO_LEVEL_TERSE)
26456 /* Avoid generating stray type DIEs during late dwarf dumping.
26457 All types have been dumped early. */
26458 if (early_dwarf
26459 /* ??? But in LTRANS we cannot annotate early created variably
26460 modified type DIEs without copying them and adjusting all
26461 references to them. Dump them again as happens for inlining
26462 which copies both the decl and the types. */
26463 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26464 in VLA bound information for example. */
26465 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26466 current_function_decl)))
26468 /* Output any DIEs that are needed to specify the type of this data
26469 object. */
26470 if (decl_by_reference_p (decl_or_origin))
26471 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26472 else
26473 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26476 if (early_dwarf)
26478 /* And its containing type. */
26479 class_origin = decl_class_context (decl_or_origin);
26480 if (class_origin != NULL_TREE)
26481 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26483 /* And its containing namespace. */
26484 context_die = declare_in_namespace (decl_or_origin, context_die);
26488 /* Now output the DIE to represent the data object itself. This gets
26489 complicated because of the possibility that the VAR_DECL really
26490 represents an inlined instance of a formal parameter for an inline
26491 function. */
26492 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26493 if (ultimate_origin != NULL_TREE
26494 && TREE_CODE (ultimate_origin) == PARM_DECL)
26495 gen_formal_parameter_die (decl, origin,
26496 true /* Emit name attribute. */,
26497 context_die);
26498 else
26499 gen_variable_die (decl, origin, context_die);
26500 break;
26502 case FIELD_DECL:
26503 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26504 /* Ignore the nameless fields that are used to skip bits but handle C++
26505 anonymous unions and structs. */
26506 if (DECL_NAME (decl) != NULL_TREE
26507 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26508 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26510 gen_type_die (member_declared_type (decl), context_die);
26511 gen_field_die (decl, ctx, context_die);
26513 break;
26515 case PARM_DECL:
26516 /* Avoid generating stray type DIEs during late dwarf dumping.
26517 All types have been dumped early. */
26518 if (early_dwarf
26519 /* ??? But in LTRANS we cannot annotate early created variably
26520 modified type DIEs without copying them and adjusting all
26521 references to them. Dump them again as happens for inlining
26522 which copies both the decl and the types. */
26523 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26524 in VLA bound information for example. */
26525 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26526 current_function_decl)))
26528 if (DECL_BY_REFERENCE (decl_or_origin))
26529 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26530 else
26531 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26533 return gen_formal_parameter_die (decl, origin,
26534 true /* Emit name attribute. */,
26535 context_die);
26537 case NAMESPACE_DECL:
26538 if (dwarf_version >= 3 || !dwarf_strict)
26539 gen_namespace_die (decl, context_die);
26540 break;
26542 case IMPORTED_DECL:
26543 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26544 DECL_CONTEXT (decl), context_die);
26545 break;
26547 case NAMELIST_DECL:
26548 gen_namelist_decl (DECL_NAME (decl), context_die,
26549 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26550 break;
26552 default:
26553 /* Probably some frontend-internal decl. Assume we don't care. */
26554 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26555 break;
26558 return NULL;
26561 /* Output initial debug information for global DECL. Called at the
26562 end of the parsing process.
26564 This is the initial debug generation process. As such, the DIEs
26565 generated may be incomplete. A later debug generation pass
26566 (dwarf2out_late_global_decl) will augment the information generated
26567 in this pass (e.g., with complete location info). */
26569 static void
26570 dwarf2out_early_global_decl (tree decl)
26572 set_early_dwarf s;
26574 /* gen_decl_die() will set DECL_ABSTRACT because
26575 cgraph_function_possibly_inlined_p() returns true. This is in
26576 turn will cause DW_AT_inline attributes to be set.
26578 This happens because at early dwarf generation, there is no
26579 cgraph information, causing cgraph_function_possibly_inlined_p()
26580 to return true. Trick cgraph_function_possibly_inlined_p()
26581 while we generate dwarf early. */
26582 bool save = symtab->global_info_ready;
26583 symtab->global_info_ready = true;
26585 /* We don't handle TYPE_DECLs. If required, they'll be reached via
26586 other DECLs and they can point to template types or other things
26587 that dwarf2out can't handle when done via dwarf2out_decl. */
26588 if (TREE_CODE (decl) != TYPE_DECL
26589 && TREE_CODE (decl) != PARM_DECL)
26591 if (TREE_CODE (decl) == FUNCTION_DECL)
26593 tree save_fndecl = current_function_decl;
26595 /* For nested functions, make sure we have DIEs for the parents first
26596 so that all nested DIEs are generated at the proper scope in the
26597 first shot. */
26598 tree context = decl_function_context (decl);
26599 if (context != NULL)
26601 dw_die_ref context_die = lookup_decl_die (context);
26602 current_function_decl = context;
26604 /* Avoid emitting DIEs multiple times, but still process CONTEXT
26605 enough so that it lands in its own context. This avoids type
26606 pruning issues later on. */
26607 if (context_die == NULL || is_declaration_die (context_die))
26608 dwarf2out_early_global_decl (context);
26611 /* Emit an abstract origin of a function first. This happens
26612 with C++ constructor clones for example and makes
26613 dwarf2out_abstract_function happy which requires the early
26614 DIE of the abstract instance to be present. */
26615 tree origin = DECL_ABSTRACT_ORIGIN (decl);
26616 dw_die_ref origin_die;
26617 if (origin != NULL
26618 /* Do not emit the DIE multiple times but make sure to
26619 process it fully here in case we just saw a declaration. */
26620 && ((origin_die = lookup_decl_die (origin)) == NULL
26621 || is_declaration_die (origin_die)))
26623 current_function_decl = origin;
26624 dwarf2out_decl (origin);
26627 /* Emit the DIE for decl but avoid doing that multiple times. */
26628 dw_die_ref old_die;
26629 if ((old_die = lookup_decl_die (decl)) == NULL
26630 || is_declaration_die (old_die))
26632 current_function_decl = decl;
26633 dwarf2out_decl (decl);
26636 current_function_decl = save_fndecl;
26638 else
26639 dwarf2out_decl (decl);
26641 symtab->global_info_ready = save;
26644 /* Return whether EXPR is an expression with the following pattern:
26645 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
26647 static bool
26648 is_trivial_indirect_ref (tree expr)
26650 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
26651 return false;
26653 tree nop = TREE_OPERAND (expr, 0);
26654 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
26655 return false;
26657 tree int_cst = TREE_OPERAND (nop, 0);
26658 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
26661 /* Output debug information for global decl DECL. Called from
26662 toplev.c after compilation proper has finished. */
26664 static void
26665 dwarf2out_late_global_decl (tree decl)
26667 /* Fill-in any location information we were unable to determine
26668 on the first pass. */
26669 if (VAR_P (decl))
26671 dw_die_ref die = lookup_decl_die (decl);
26673 /* We may have to generate full debug late for LTO in case debug
26674 was not enabled at compile-time or the target doesn't support
26675 the LTO early debug scheme. */
26676 if (! die && in_lto_p)
26677 dwarf2out_decl (decl);
26678 else if (die)
26680 /* We get called via the symtab code invoking late_global_decl
26681 for symbols that are optimized out.
26683 Do not add locations for those, except if they have a
26684 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
26685 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
26686 INDIRECT_REF expression, as this could generate relocations to
26687 text symbols in LTO object files, which is invalid. */
26688 varpool_node *node = varpool_node::get (decl);
26689 if ((! node || ! node->definition)
26690 && ! (DECL_HAS_VALUE_EXPR_P (decl)
26691 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
26692 tree_add_const_value_attribute_for_decl (die, decl);
26693 else
26694 add_location_or_const_value_attribute (die, decl, false);
26699 /* Output debug information for type decl DECL. Called from toplev.c
26700 and from language front ends (to record built-in types). */
26701 static void
26702 dwarf2out_type_decl (tree decl, int local)
26704 if (!local)
26706 set_early_dwarf s;
26707 dwarf2out_decl (decl);
26711 /* Output debug information for imported module or decl DECL.
26712 NAME is non-NULL name in the lexical block if the decl has been renamed.
26713 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
26714 that DECL belongs to.
26715 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
26716 static void
26717 dwarf2out_imported_module_or_decl_1 (tree decl,
26718 tree name,
26719 tree lexical_block,
26720 dw_die_ref lexical_block_die)
26722 expanded_location xloc;
26723 dw_die_ref imported_die = NULL;
26724 dw_die_ref at_import_die;
26726 if (TREE_CODE (decl) == IMPORTED_DECL)
26728 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
26729 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
26730 gcc_assert (decl);
26732 else
26733 xloc = expand_location (input_location);
26735 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
26737 at_import_die = force_type_die (TREE_TYPE (decl));
26738 /* For namespace N { typedef void T; } using N::T; base_type_die
26739 returns NULL, but DW_TAG_imported_declaration requires
26740 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
26741 if (!at_import_die)
26743 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
26744 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
26745 at_import_die = lookup_type_die (TREE_TYPE (decl));
26746 gcc_assert (at_import_die);
26749 else
26751 at_import_die = lookup_decl_die (decl);
26752 if (!at_import_die)
26754 /* If we're trying to avoid duplicate debug info, we may not have
26755 emitted the member decl for this field. Emit it now. */
26756 if (TREE_CODE (decl) == FIELD_DECL)
26758 tree type = DECL_CONTEXT (decl);
26760 if (TYPE_CONTEXT (type)
26761 && TYPE_P (TYPE_CONTEXT (type))
26762 && !should_emit_struct_debug (TYPE_CONTEXT (type),
26763 DINFO_USAGE_DIR_USE))
26764 return;
26765 gen_type_die_for_member (type, decl,
26766 get_context_die (TYPE_CONTEXT (type)));
26768 if (TREE_CODE (decl) == NAMELIST_DECL)
26769 at_import_die = gen_namelist_decl (DECL_NAME (decl),
26770 get_context_die (DECL_CONTEXT (decl)),
26771 NULL_TREE);
26772 else
26773 at_import_die = force_decl_die (decl);
26777 if (TREE_CODE (decl) == NAMESPACE_DECL)
26779 if (dwarf_version >= 3 || !dwarf_strict)
26780 imported_die = new_die (DW_TAG_imported_module,
26781 lexical_block_die,
26782 lexical_block);
26783 else
26784 return;
26786 else
26787 imported_die = new_die (DW_TAG_imported_declaration,
26788 lexical_block_die,
26789 lexical_block);
26791 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
26792 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
26793 if (debug_column_info && xloc.column)
26794 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
26795 if (name)
26796 add_AT_string (imported_die, DW_AT_name,
26797 IDENTIFIER_POINTER (name));
26798 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
26801 /* Output debug information for imported module or decl DECL.
26802 NAME is non-NULL name in context if the decl has been renamed.
26803 CHILD is true if decl is one of the renamed decls as part of
26804 importing whole module.
26805 IMPLICIT is set if this hook is called for an implicit import
26806 such as inline namespace. */
26808 static void
26809 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
26810 bool child, bool implicit)
26812 /* dw_die_ref at_import_die; */
26813 dw_die_ref scope_die;
26815 if (debug_info_level <= DINFO_LEVEL_TERSE)
26816 return;
26818 gcc_assert (decl);
26820 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
26821 should be enough, for DWARF4 and older even if we emit as extension
26822 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
26823 for the benefit of consumers unaware of DW_AT_export_symbols. */
26824 if (implicit
26825 && dwarf_version >= 5
26826 && lang_hooks.decls.decl_dwarf_attribute (decl,
26827 DW_AT_export_symbols) == 1)
26828 return;
26830 set_early_dwarf s;
26832 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
26833 We need decl DIE for reference and scope die. First, get DIE for the decl
26834 itself. */
26836 /* Get the scope die for decl context. Use comp_unit_die for global module
26837 or decl. If die is not found for non globals, force new die. */
26838 if (context
26839 && TYPE_P (context)
26840 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
26841 return;
26843 scope_die = get_context_die (context);
26845 if (child)
26847 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
26848 there is nothing we can do, here. */
26849 if (dwarf_version < 3 && dwarf_strict)
26850 return;
26852 gcc_assert (scope_die->die_child);
26853 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
26854 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
26855 scope_die = scope_die->die_child;
26858 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
26859 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
26862 /* Output debug information for namelists. */
26864 static dw_die_ref
26865 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
26867 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
26868 tree value;
26869 unsigned i;
26871 if (debug_info_level <= DINFO_LEVEL_TERSE)
26872 return NULL;
26874 gcc_assert (scope_die != NULL);
26875 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
26876 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
26878 /* If there are no item_decls, we have a nondefining namelist, e.g.
26879 with USE association; hence, set DW_AT_declaration. */
26880 if (item_decls == NULL_TREE)
26882 add_AT_flag (nml_die, DW_AT_declaration, 1);
26883 return nml_die;
26886 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
26888 nml_item_ref_die = lookup_decl_die (value);
26889 if (!nml_item_ref_die)
26890 nml_item_ref_die = force_decl_die (value);
26892 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
26893 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
26895 return nml_die;
26899 /* Write the debugging output for DECL and return the DIE. */
26901 static void
26902 dwarf2out_decl (tree decl)
26904 dw_die_ref context_die = comp_unit_die ();
26906 switch (TREE_CODE (decl))
26908 case ERROR_MARK:
26909 return;
26911 case FUNCTION_DECL:
26912 /* If we're a nested function, initially use a parent of NULL; if we're
26913 a plain function, this will be fixed up in decls_for_scope. If
26914 we're a method, it will be ignored, since we already have a DIE.
26915 Avoid doing this late though since clones of class methods may
26916 otherwise end up in limbo and create type DIEs late. */
26917 if (early_dwarf
26918 && decl_function_context (decl)
26919 /* But if we're in terse mode, we don't care about scope. */
26920 && debug_info_level > DINFO_LEVEL_TERSE)
26921 context_die = NULL;
26922 break;
26924 case VAR_DECL:
26925 /* For local statics lookup proper context die. */
26926 if (local_function_static (decl))
26927 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26929 /* If we are in terse mode, don't generate any DIEs to represent any
26930 variable declarations or definitions unless it is external. */
26931 if (debug_info_level < DINFO_LEVEL_TERSE
26932 || (debug_info_level == DINFO_LEVEL_TERSE
26933 && !TREE_PUBLIC (decl)))
26934 return;
26935 break;
26937 case CONST_DECL:
26938 if (debug_info_level <= DINFO_LEVEL_TERSE)
26939 return;
26940 if (!is_fortran () && !is_ada () && !is_dlang ())
26941 return;
26942 if (TREE_STATIC (decl) && decl_function_context (decl))
26943 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26944 break;
26946 case NAMESPACE_DECL:
26947 case IMPORTED_DECL:
26948 if (debug_info_level <= DINFO_LEVEL_TERSE)
26949 return;
26950 if (lookup_decl_die (decl) != NULL)
26951 return;
26952 break;
26954 case TYPE_DECL:
26955 /* Don't emit stubs for types unless they are needed by other DIEs. */
26956 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
26957 return;
26959 /* Don't bother trying to generate any DIEs to represent any of the
26960 normal built-in types for the language we are compiling. */
26961 if (DECL_IS_UNDECLARED_BUILTIN (decl))
26962 return;
26964 /* If we are in terse mode, don't generate any DIEs for types. */
26965 if (debug_info_level <= DINFO_LEVEL_TERSE)
26966 return;
26968 /* If we're a function-scope tag, initially use a parent of NULL;
26969 this will be fixed up in decls_for_scope. */
26970 if (decl_function_context (decl))
26971 context_die = NULL;
26973 break;
26975 case NAMELIST_DECL:
26976 break;
26978 default:
26979 return;
26982 gen_decl_die (decl, NULL, NULL, context_die);
26984 if (flag_checking)
26986 dw_die_ref die = lookup_decl_die (decl);
26987 if (die)
26988 check_die (die);
26992 /* Write the debugging output for DECL. */
26994 static void
26995 dwarf2out_function_decl (tree decl)
26997 dwarf2out_decl (decl);
26998 call_arg_locations = NULL;
26999 call_arg_loc_last = NULL;
27000 call_site_count = -1;
27001 tail_call_site_count = -1;
27002 decl_loc_table->empty ();
27003 cached_dw_loc_list_table->empty ();
27006 /* Output a marker (i.e. a label) for the beginning of the generated code for
27007 a lexical block. */
27009 static void
27010 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27011 unsigned int blocknum)
27013 switch_to_section (current_function_section ());
27014 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27017 /* Output a marker (i.e. a label) for the end of the generated code for a
27018 lexical block. */
27020 static void
27021 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27023 switch_to_section (current_function_section ());
27024 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27027 /* Returns nonzero if it is appropriate not to emit any debugging
27028 information for BLOCK, because it doesn't contain any instructions.
27030 Don't allow this for blocks with nested functions or local classes
27031 as we would end up with orphans, and in the presence of scheduling
27032 we may end up calling them anyway. */
27034 static bool
27035 dwarf2out_ignore_block (const_tree block)
27037 tree decl;
27038 unsigned int i;
27040 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27041 if (TREE_CODE (decl) == FUNCTION_DECL
27042 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27043 return 0;
27044 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27046 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27047 if (TREE_CODE (decl) == FUNCTION_DECL
27048 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27049 return 0;
27052 return 1;
27055 /* Hash table routines for file_hash. */
27057 bool
27058 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27060 return filename_cmp (p1->filename, p2) == 0;
27063 hashval_t
27064 dwarf_file_hasher::hash (dwarf_file_data *p)
27066 return htab_hash_string (p->filename);
27069 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27070 dwarf2out.c) and return its "index". The index of each (known) filename is
27071 just a unique number which is associated with only that one filename. We
27072 need such numbers for the sake of generating labels (in the .debug_sfnames
27073 section) and references to those files numbers (in the .debug_srcinfo
27074 and .debug_macinfo sections). If the filename given as an argument is not
27075 found in our current list, add it to the list and assign it the next
27076 available unique index number. */
27078 static struct dwarf_file_data *
27079 lookup_filename (const char *file_name)
27081 struct dwarf_file_data * created;
27083 if (!file_name)
27084 return NULL;
27086 if (!file_name[0])
27087 file_name = "<stdin>";
27089 dwarf_file_data **slot
27090 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27091 INSERT);
27092 if (*slot)
27093 return *slot;
27095 created = ggc_alloc<dwarf_file_data> ();
27096 created->filename = file_name;
27097 created->emitted_number = 0;
27098 *slot = created;
27099 return created;
27102 /* If the assembler will construct the file table, then translate the compiler
27103 internal file table number into the assembler file table number, and emit
27104 a .file directive if we haven't already emitted one yet. The file table
27105 numbers are different because we prune debug info for unused variables and
27106 types, which may include filenames. */
27108 static int
27109 maybe_emit_file (struct dwarf_file_data * fd)
27111 if (! fd->emitted_number)
27113 if (last_emitted_file)
27114 fd->emitted_number = last_emitted_file->emitted_number + 1;
27115 else
27116 fd->emitted_number = 1;
27117 last_emitted_file = fd;
27119 if (output_asm_line_debug_info ())
27121 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27122 output_quoted_string (asm_out_file,
27123 remap_debug_filename (fd->filename));
27124 fputc ('\n', asm_out_file);
27128 return fd->emitted_number;
27131 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27132 That generation should happen after function debug info has been
27133 generated. The value of the attribute is the constant value of ARG. */
27135 static void
27136 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27138 die_arg_entry entry;
27140 if (!die || !arg)
27141 return;
27143 gcc_assert (early_dwarf);
27145 if (!tmpl_value_parm_die_table)
27146 vec_alloc (tmpl_value_parm_die_table, 32);
27148 entry.die = die;
27149 entry.arg = arg;
27150 vec_safe_push (tmpl_value_parm_die_table, entry);
27153 /* Return TRUE if T is an instance of generic type, FALSE
27154 otherwise. */
27156 static bool
27157 generic_type_p (tree t)
27159 if (t == NULL_TREE || !TYPE_P (t))
27160 return false;
27161 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27164 /* Schedule the generation of the generic parameter dies for the
27165 instance of generic type T. The proper generation itself is later
27166 done by gen_scheduled_generic_parms_dies. */
27168 static void
27169 schedule_generic_params_dies_gen (tree t)
27171 if (!generic_type_p (t))
27172 return;
27174 gcc_assert (early_dwarf);
27176 if (!generic_type_instances)
27177 vec_alloc (generic_type_instances, 256);
27179 vec_safe_push (generic_type_instances, t);
27182 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27183 by append_entry_to_tmpl_value_parm_die_table. This function must
27184 be called after function DIEs have been generated. */
27186 static void
27187 gen_remaining_tmpl_value_param_die_attribute (void)
27189 if (tmpl_value_parm_die_table)
27191 unsigned i, j;
27192 die_arg_entry *e;
27194 /* We do this in two phases - first get the cases we can
27195 handle during early-finish, preserving those we cannot
27196 (containing symbolic constants where we don't yet know
27197 whether we are going to output the referenced symbols).
27198 For those we try again at late-finish. */
27199 j = 0;
27200 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27202 if (!e->die->removed
27203 && !tree_add_const_value_attribute (e->die, e->arg))
27205 dw_loc_descr_ref loc = NULL;
27206 if (! early_dwarf
27207 && (dwarf_version >= 5 || !dwarf_strict))
27208 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27209 if (loc)
27210 add_AT_loc (e->die, DW_AT_location, loc);
27211 else
27212 (*tmpl_value_parm_die_table)[j++] = *e;
27215 tmpl_value_parm_die_table->truncate (j);
27219 /* Generate generic parameters DIEs for instances of generic types
27220 that have been previously scheduled by
27221 schedule_generic_params_dies_gen. This function must be called
27222 after all the types of the CU have been laid out. */
27224 static void
27225 gen_scheduled_generic_parms_dies (void)
27227 unsigned i;
27228 tree t;
27230 if (!generic_type_instances)
27231 return;
27233 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27234 if (COMPLETE_TYPE_P (t))
27235 gen_generic_params_dies (t);
27237 generic_type_instances = NULL;
27241 /* Replace DW_AT_name for the decl with name. */
27243 static void
27244 dwarf2out_set_name (tree decl, tree name)
27246 dw_die_ref die;
27247 dw_attr_node *attr;
27248 const char *dname;
27250 die = TYPE_SYMTAB_DIE (decl);
27251 if (!die)
27252 return;
27254 dname = dwarf2_name (name, 0);
27255 if (!dname)
27256 return;
27258 attr = get_AT (die, DW_AT_name);
27259 if (attr)
27261 struct indirect_string_node *node;
27263 node = find_AT_string (dname);
27264 /* replace the string. */
27265 attr->dw_attr_val.v.val_str = node;
27268 else
27269 add_name_attribute (die, dname);
27272 /* True if before or during processing of the first function being emitted. */
27273 static bool in_first_function_p = true;
27274 /* True if loc_note during dwarf2out_var_location call might still be
27275 before first real instruction at address equal to .Ltext0. */
27276 static bool maybe_at_text_label_p = true;
27277 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27278 static unsigned int first_loclabel_num_not_at_text_label;
27280 /* Look ahead for a real insn. */
27282 static rtx_insn *
27283 dwarf2out_next_real_insn (rtx_insn *loc_note)
27285 rtx_insn *next_real = NEXT_INSN (loc_note);
27287 while (next_real)
27288 if (INSN_P (next_real))
27289 break;
27290 else
27291 next_real = NEXT_INSN (next_real);
27293 return next_real;
27296 /* Called by the final INSN scan whenever we see a var location. We
27297 use it to drop labels in the right places, and throw the location in
27298 our lookup table. */
27300 static void
27301 dwarf2out_var_location (rtx_insn *loc_note)
27303 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27304 struct var_loc_node *newloc;
27305 rtx_insn *next_real;
27306 rtx_insn *call_insn = NULL;
27307 static const char *last_label;
27308 static const char *last_postcall_label;
27309 static bool last_in_cold_section_p;
27310 static rtx_insn *expected_next_loc_note;
27311 tree decl;
27312 bool var_loc_p;
27313 var_loc_view view = 0;
27315 if (!NOTE_P (loc_note))
27317 if (CALL_P (loc_note))
27319 maybe_reset_location_view (loc_note, cur_line_info_table);
27320 call_site_count++;
27321 if (SIBLING_CALL_P (loc_note))
27322 tail_call_site_count++;
27323 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27325 call_insn = loc_note;
27326 loc_note = NULL;
27327 var_loc_p = false;
27329 next_real = dwarf2out_next_real_insn (call_insn);
27330 cached_next_real_insn = NULL;
27331 goto create_label;
27333 if (optimize == 0 && !flag_var_tracking)
27335 /* When the var-tracking pass is not running, there is no note
27336 for indirect calls whose target is compile-time known. In this
27337 case, process such calls specifically so that we generate call
27338 sites for them anyway. */
27339 rtx x = PATTERN (loc_note);
27340 if (GET_CODE (x) == PARALLEL)
27341 x = XVECEXP (x, 0, 0);
27342 if (GET_CODE (x) == SET)
27343 x = SET_SRC (x);
27344 if (GET_CODE (x) == CALL)
27345 x = XEXP (x, 0);
27346 if (!MEM_P (x)
27347 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27348 || !SYMBOL_REF_DECL (XEXP (x, 0))
27349 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27350 != FUNCTION_DECL))
27352 call_insn = loc_note;
27353 loc_note = NULL;
27354 var_loc_p = false;
27356 next_real = dwarf2out_next_real_insn (call_insn);
27357 cached_next_real_insn = NULL;
27358 goto create_label;
27362 else if (!debug_variable_location_views)
27363 gcc_unreachable ();
27364 else
27365 maybe_reset_location_view (loc_note, cur_line_info_table);
27367 return;
27370 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27371 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27372 return;
27374 /* Optimize processing a large consecutive sequence of location
27375 notes so we don't spend too much time in next_real_insn. If the
27376 next insn is another location note, remember the next_real_insn
27377 calculation for next time. */
27378 next_real = cached_next_real_insn;
27379 if (next_real)
27381 if (expected_next_loc_note != loc_note)
27382 next_real = NULL;
27385 if (! next_real)
27386 next_real = dwarf2out_next_real_insn (loc_note);
27388 if (next_real)
27390 rtx_insn *next_note = NEXT_INSN (loc_note);
27391 while (next_note != next_real)
27393 if (! next_note->deleted ()
27394 && NOTE_P (next_note)
27395 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
27396 break;
27397 next_note = NEXT_INSN (next_note);
27400 if (next_note == next_real)
27401 cached_next_real_insn = NULL;
27402 else
27404 expected_next_loc_note = next_note;
27405 cached_next_real_insn = next_real;
27408 else
27409 cached_next_real_insn = NULL;
27411 /* If there are no instructions which would be affected by this note,
27412 don't do anything. */
27413 if (var_loc_p
27414 && next_real == NULL_RTX
27415 && !NOTE_DURING_CALL_P (loc_note))
27416 return;
27418 create_label:
27420 if (next_real == NULL_RTX)
27421 next_real = get_last_insn ();
27423 /* If there were any real insns between note we processed last time
27424 and this note (or if it is the first note), clear
27425 last_{,postcall_}label so that they are not reused this time. */
27426 if (last_var_location_insn == NULL_RTX
27427 || last_var_location_insn != next_real
27428 || last_in_cold_section_p != in_cold_section_p)
27430 last_label = NULL;
27431 last_postcall_label = NULL;
27434 if (var_loc_p)
27436 const char *label
27437 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27438 view = cur_line_info_table->view;
27439 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27440 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27441 if (newloc == NULL)
27442 return;
27444 else
27446 decl = NULL_TREE;
27447 newloc = NULL;
27450 /* If there were no real insns between note we processed last time
27451 and this note, use the label we emitted last time. Otherwise
27452 create a new label and emit it. */
27453 if (last_label == NULL)
27455 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27456 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27457 loclabel_num++;
27458 last_label = ggc_strdup (loclabel);
27459 /* See if loclabel might be equal to .Ltext0. If yes,
27460 bump first_loclabel_num_not_at_text_label. */
27461 if (!have_multiple_function_sections
27462 && in_first_function_p
27463 && maybe_at_text_label_p)
27465 static rtx_insn *last_start;
27466 rtx_insn *insn;
27467 for (insn = loc_note; insn; insn = previous_insn (insn))
27468 if (insn == last_start)
27469 break;
27470 else if (!NONDEBUG_INSN_P (insn))
27471 continue;
27472 else
27474 rtx body = PATTERN (insn);
27475 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27476 continue;
27477 /* Inline asm could occupy zero bytes. */
27478 else if (GET_CODE (body) == ASM_INPUT
27479 || asm_noperands (body) >= 0)
27480 continue;
27481 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27482 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27483 continue;
27484 #endif
27485 else
27487 /* Assume insn has non-zero length. */
27488 maybe_at_text_label_p = false;
27489 break;
27492 if (maybe_at_text_label_p)
27494 last_start = loc_note;
27495 first_loclabel_num_not_at_text_label = loclabel_num;
27500 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27501 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27503 if (!var_loc_p)
27505 struct call_arg_loc_node *ca_loc
27506 = ggc_cleared_alloc<call_arg_loc_node> ();
27507 rtx_insn *prev = call_insn;
27509 ca_loc->call_arg_loc_note
27510 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27511 ca_loc->next = NULL;
27512 ca_loc->label = last_label;
27513 gcc_assert (prev
27514 && (CALL_P (prev)
27515 || (NONJUMP_INSN_P (prev)
27516 && GET_CODE (PATTERN (prev)) == SEQUENCE
27517 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27518 if (!CALL_P (prev))
27519 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27520 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27522 /* Look for a SYMBOL_REF in the "prev" instruction. */
27523 rtx x = get_call_rtx_from (prev);
27524 if (x)
27526 /* Try to get the call symbol, if any. */
27527 if (MEM_P (XEXP (x, 0)))
27528 x = XEXP (x, 0);
27529 /* First, look for a memory access to a symbol_ref. */
27530 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27531 && SYMBOL_REF_DECL (XEXP (x, 0))
27532 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27533 ca_loc->symbol_ref = XEXP (x, 0);
27534 /* Otherwise, look at a compile-time known user-level function
27535 declaration. */
27536 else if (MEM_P (x)
27537 && MEM_EXPR (x)
27538 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27539 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27542 ca_loc->block = insn_scope (prev);
27543 if (call_arg_locations)
27544 call_arg_loc_last->next = ca_loc;
27545 else
27546 call_arg_locations = ca_loc;
27547 call_arg_loc_last = ca_loc;
27549 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27551 newloc->label = last_label;
27552 newloc->view = view;
27554 else
27556 if (!last_postcall_label)
27558 sprintf (loclabel, "%s-1", last_label);
27559 last_postcall_label = ggc_strdup (loclabel);
27561 newloc->label = last_postcall_label;
27562 /* ??? This view is at last_label, not last_label-1, but we
27563 could only assume view at last_label-1 is zero if we could
27564 assume calls always have length greater than one. This is
27565 probably true in general, though there might be a rare
27566 exception to this rule, e.g. if a call insn is optimized out
27567 by target magic. Then, even the -1 in the label will be
27568 wrong, which might invalidate the range. Anyway, using view,
27569 though technically possibly incorrect, will work as far as
27570 ranges go: since L-1 is in the middle of the call insn,
27571 (L-1).0 and (L-1).V shouldn't make any difference, and having
27572 the loclist entry refer to the .loc entry might be useful, so
27573 leave it like this. */
27574 newloc->view = view;
27577 if (var_loc_p && flag_debug_asm)
27579 const char *name, *sep, *patstr;
27580 if (decl && DECL_NAME (decl))
27581 name = IDENTIFIER_POINTER (DECL_NAME (decl));
27582 else
27583 name = "";
27584 if (NOTE_VAR_LOCATION_LOC (loc_note))
27586 sep = " => ";
27587 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27589 else
27591 sep = " ";
27592 patstr = "RESET";
27594 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27595 name, sep, patstr);
27598 last_var_location_insn = next_real;
27599 last_in_cold_section_p = in_cold_section_p;
27602 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
27603 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
27604 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
27605 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
27606 BLOCK_FRAGMENT_ORIGIN links. */
27607 static bool
27608 block_within_block_p (tree block, tree outer, bool bothways)
27610 if (block == outer)
27611 return true;
27613 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
27614 for (tree context = BLOCK_SUPERCONTEXT (block);
27615 context != outer;
27616 context = BLOCK_SUPERCONTEXT (context))
27617 if (!context || TREE_CODE (context) != BLOCK)
27618 return false;
27620 if (!bothways)
27621 return true;
27623 /* Now check that each block is actually referenced by its
27624 parent. */
27625 for (tree context = BLOCK_SUPERCONTEXT (block); ;
27626 context = BLOCK_SUPERCONTEXT (context))
27628 if (BLOCK_FRAGMENT_ORIGIN (context))
27630 gcc_assert (!BLOCK_SUBBLOCKS (context));
27631 context = BLOCK_FRAGMENT_ORIGIN (context);
27633 for (tree sub = BLOCK_SUBBLOCKS (context);
27634 sub != block;
27635 sub = BLOCK_CHAIN (sub))
27636 if (!sub)
27637 return false;
27638 if (context == outer)
27639 return true;
27640 else
27641 block = context;
27645 /* Called during final while assembling the marker of the entry point
27646 for an inlined function. */
27648 static void
27649 dwarf2out_inline_entry (tree block)
27651 gcc_assert (debug_inline_points);
27653 /* If we can't represent it, don't bother. */
27654 if (!(dwarf_version >= 3 || !dwarf_strict))
27655 return;
27657 gcc_assert (DECL_P (block_ultimate_origin (block)));
27659 /* Sanity check the block tree. This would catch a case in which
27660 BLOCK got removed from the tree reachable from the outermost
27661 lexical block, but got retained in markers. It would still link
27662 back to its parents, but some ancestor would be missing a link
27663 down the path to the sub BLOCK. If the block got removed, its
27664 BLOCK_NUMBER will not be a usable value. */
27665 if (flag_checking)
27666 gcc_assert (block_within_block_p (block,
27667 DECL_INITIAL (current_function_decl),
27668 true));
27670 gcc_assert (inlined_function_outer_scope_p (block));
27671 gcc_assert (!lookup_block_die (block));
27673 if (BLOCK_FRAGMENT_ORIGIN (block))
27674 block = BLOCK_FRAGMENT_ORIGIN (block);
27675 /* Can the entry point ever not be at the beginning of an
27676 unfragmented lexical block? */
27677 else if (!(BLOCK_FRAGMENT_CHAIN (block)
27678 || (cur_line_info_table
27679 && !ZERO_VIEW_P (cur_line_info_table->view))))
27680 return;
27682 if (!inline_entry_data_table)
27683 inline_entry_data_table
27684 = hash_table<inline_entry_data_hasher>::create_ggc (10);
27687 inline_entry_data **iedp
27688 = inline_entry_data_table->find_slot_with_hash (block,
27689 htab_hash_pointer (block),
27690 INSERT);
27691 if (*iedp)
27692 /* ??? Ideally, we'd record all entry points for the same inlined
27693 function (some may have been duplicated by e.g. unrolling), but
27694 we have no way to represent that ATM. */
27695 return;
27697 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
27698 ied->block = block;
27699 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
27700 ied->label_num = BLOCK_NUMBER (block);
27701 if (cur_line_info_table)
27702 ied->view = cur_line_info_table->view;
27704 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
27705 BLOCK_NUMBER (block));
27708 /* Called from finalize_size_functions for size functions so that their body
27709 can be encoded in the debug info to describe the layout of variable-length
27710 structures. */
27712 static void
27713 dwarf2out_size_function (tree decl)
27715 set_early_dwarf s;
27716 function_to_dwarf_procedure (decl);
27719 /* Note in one location list that text section has changed. */
27722 var_location_switch_text_section_1 (var_loc_list **slot, void *)
27724 var_loc_list *list = *slot;
27725 if (list->first)
27726 list->last_before_switch
27727 = list->last->next ? list->last->next : list->last;
27728 return 1;
27731 /* Note in all location lists that text section has changed. */
27733 static void
27734 var_location_switch_text_section (void)
27736 if (decl_loc_table == NULL)
27737 return;
27739 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
27742 /* Create a new line number table. */
27744 static dw_line_info_table *
27745 new_line_info_table (void)
27747 dw_line_info_table *table;
27749 table = ggc_cleared_alloc<dw_line_info_table> ();
27750 table->file_num = 1;
27751 table->line_num = 1;
27752 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
27753 FORCE_RESET_NEXT_VIEW (table->view);
27754 table->symviews_since_reset = 0;
27756 return table;
27759 /* Lookup the "current" table into which we emit line info, so
27760 that we don't have to do it for every source line. */
27762 static void
27763 set_cur_line_info_table (section *sec)
27765 dw_line_info_table *table;
27767 if (sec == text_section)
27768 table = text_section_line_info;
27769 else if (sec == cold_text_section)
27771 table = cold_text_section_line_info;
27772 if (!table)
27774 cold_text_section_line_info = table = new_line_info_table ();
27775 table->end_label = cold_end_label;
27778 else
27780 const char *end_label;
27782 if (crtl->has_bb_partition)
27784 if (in_cold_section_p)
27785 end_label = crtl->subsections.cold_section_end_label;
27786 else
27787 end_label = crtl->subsections.hot_section_end_label;
27789 else
27791 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27792 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
27793 current_function_funcdef_no);
27794 end_label = ggc_strdup (label);
27797 table = new_line_info_table ();
27798 table->end_label = end_label;
27800 vec_safe_push (separate_line_info, table);
27803 if (output_asm_line_debug_info ())
27804 table->is_stmt = (cur_line_info_table
27805 ? cur_line_info_table->is_stmt
27806 : DWARF_LINE_DEFAULT_IS_STMT_START);
27807 cur_line_info_table = table;
27811 /* We need to reset the locations at the beginning of each
27812 function. We can't do this in the end_function hook, because the
27813 declarations that use the locations won't have been output when
27814 that hook is called. Also compute have_multiple_function_sections here. */
27816 static void
27817 dwarf2out_begin_function (tree fun)
27819 section *sec = function_section (fun);
27821 if (sec != text_section)
27822 have_multiple_function_sections = true;
27824 if (crtl->has_bb_partition && !cold_text_section)
27826 gcc_assert (current_function_decl == fun);
27827 cold_text_section = unlikely_text_section ();
27828 switch_to_section (cold_text_section);
27829 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
27830 switch_to_section (sec);
27833 dwarf2out_note_section_used ();
27834 call_site_count = 0;
27835 tail_call_site_count = 0;
27837 set_cur_line_info_table (sec);
27838 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
27841 /* Helper function of dwarf2out_end_function, called only after emitting
27842 the very first function into assembly. Check if some .debug_loc range
27843 might end with a .LVL* label that could be equal to .Ltext0.
27844 In that case we must force using absolute addresses in .debug_loc ranges,
27845 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
27846 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
27847 list terminator.
27848 Set have_multiple_function_sections to true in that case and
27849 terminate htab traversal. */
27852 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
27854 var_loc_list *entry = *slot;
27855 struct var_loc_node *node;
27857 node = entry->first;
27858 if (node && node->next && node->next->label)
27860 unsigned int i;
27861 const char *label = node->next->label;
27862 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
27864 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
27866 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
27867 if (strcmp (label, loclabel) == 0)
27869 have_multiple_function_sections = true;
27870 return 0;
27874 return 1;
27877 /* Hook called after emitting a function into assembly.
27878 This does something only for the very first function emitted. */
27880 static void
27881 dwarf2out_end_function (unsigned int)
27883 if (in_first_function_p
27884 && !have_multiple_function_sections
27885 && first_loclabel_num_not_at_text_label
27886 && decl_loc_table)
27887 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
27888 in_first_function_p = false;
27889 maybe_at_text_label_p = false;
27892 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
27893 front-ends register a translation unit even before dwarf2out_init is
27894 called. */
27895 static tree main_translation_unit = NULL_TREE;
27897 /* Hook called by front-ends after they built their main translation unit.
27898 Associate comp_unit_die to UNIT. */
27900 static void
27901 dwarf2out_register_main_translation_unit (tree unit)
27903 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
27904 && main_translation_unit == NULL_TREE);
27905 main_translation_unit = unit;
27906 /* If dwarf2out_init has not been called yet, it will perform the association
27907 itself looking at main_translation_unit. */
27908 if (decl_die_table != NULL)
27909 equate_decl_number_to_die (unit, comp_unit_die ());
27912 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
27914 static void
27915 push_dw_line_info_entry (dw_line_info_table *table,
27916 enum dw_line_info_opcode opcode, unsigned int val)
27918 dw_line_info_entry e;
27919 e.opcode = opcode;
27920 e.val = val;
27921 vec_safe_push (table->entries, e);
27924 /* Output a label to mark the beginning of a source code line entry
27925 and record information relating to this source line, in
27926 'line_info_table' for later output of the .debug_line section. */
27927 /* ??? The discriminator parameter ought to be unsigned. */
27929 static void
27930 dwarf2out_source_line (unsigned int line, unsigned int column,
27931 const char *filename,
27932 int discriminator, bool is_stmt)
27934 unsigned int file_num;
27935 dw_line_info_table *table;
27936 static var_loc_view lvugid;
27938 if (debug_info_level < DINFO_LEVEL_TERSE)
27939 return;
27941 table = cur_line_info_table;
27943 if (line == 0)
27945 if (debug_variable_location_views
27946 && output_asm_line_debug_info ()
27947 && table && !RESETTING_VIEW_P (table->view))
27949 /* If we're using the assembler to compute view numbers, we
27950 can't issue a .loc directive for line zero, so we can't
27951 get a view number at this point. We might attempt to
27952 compute it from the previous view, or equate it to a
27953 subsequent view (though it might not be there!), but
27954 since we're omitting the line number entry, we might as
27955 well omit the view number as well. That means pretending
27956 it's a view number zero, which might very well turn out
27957 to be correct. ??? Extend the assembler so that the
27958 compiler could emit e.g. ".locview .LVU#", to output a
27959 view without changing line number information. We'd then
27960 have to count it in symviews_since_reset; when it's omitted,
27961 it doesn't count. */
27962 if (!zero_view_p)
27963 zero_view_p = BITMAP_GGC_ALLOC ();
27964 bitmap_set_bit (zero_view_p, table->view);
27965 if (flag_debug_asm)
27967 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27968 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27969 fprintf (asm_out_file, "\t%s line 0, omitted view ",
27970 ASM_COMMENT_START);
27971 assemble_name (asm_out_file, label);
27972 putc ('\n', asm_out_file);
27974 table->view = ++lvugid;
27976 return;
27979 /* The discriminator column was added in dwarf4. Simplify the below
27980 by simply removing it if we're not supposed to output it. */
27981 if (dwarf_version < 4 && dwarf_strict)
27982 discriminator = 0;
27984 if (!debug_column_info)
27985 column = 0;
27987 file_num = maybe_emit_file (lookup_filename (filename));
27989 /* ??? TODO: Elide duplicate line number entries. Traditionally,
27990 the debugger has used the second (possibly duplicate) line number
27991 at the beginning of the function to mark the end of the prologue.
27992 We could eliminate any other duplicates within the function. For
27993 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
27994 that second line number entry. */
27995 /* Recall that this end-of-prologue indication is *not* the same thing
27996 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
27997 to which the hook corresponds, follows the last insn that was
27998 emitted by gen_prologue. What we need is to precede the first insn
27999 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28000 insn that corresponds to something the user wrote. These may be
28001 very different locations once scheduling is enabled. */
28003 if (0 && file_num == table->file_num
28004 && line == table->line_num
28005 && column == table->column_num
28006 && discriminator == table->discrim_num
28007 && is_stmt == table->is_stmt)
28008 return;
28010 switch_to_section (current_function_section ());
28012 /* If requested, emit something human-readable. */
28013 if (flag_debug_asm)
28015 if (debug_column_info)
28016 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28017 filename, line, column);
28018 else
28019 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28020 filename, line);
28023 if (output_asm_line_debug_info ())
28025 /* Emit the .loc directive understood by GNU as. */
28026 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28027 file_num, line, is_stmt, discriminator */
28028 fputs ("\t.loc ", asm_out_file);
28029 fprint_ul (asm_out_file, file_num);
28030 putc (' ', asm_out_file);
28031 fprint_ul (asm_out_file, line);
28032 putc (' ', asm_out_file);
28033 fprint_ul (asm_out_file, column);
28035 if (is_stmt != table->is_stmt)
28037 #if HAVE_GAS_LOC_STMT
28038 fputs (" is_stmt ", asm_out_file);
28039 putc (is_stmt ? '1' : '0', asm_out_file);
28040 #endif
28042 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28044 gcc_assert (discriminator > 0);
28045 fputs (" discriminator ", asm_out_file);
28046 fprint_ul (asm_out_file, (unsigned long) discriminator);
28048 if (debug_variable_location_views)
28050 if (!RESETTING_VIEW_P (table->view))
28052 table->symviews_since_reset++;
28053 if (table->symviews_since_reset > symview_upper_bound)
28054 symview_upper_bound = table->symviews_since_reset;
28055 /* When we're using the assembler to compute view
28056 numbers, we output symbolic labels after "view" in
28057 .loc directives, and the assembler will set them for
28058 us, so that we can refer to the view numbers in
28059 location lists. The only exceptions are when we know
28060 a view will be zero: "-0" is a forced reset, used
28061 e.g. in the beginning of functions, whereas "0" tells
28062 the assembler to check that there was a PC change
28063 since the previous view, in a way that implicitly
28064 resets the next view. */
28065 fputs (" view ", asm_out_file);
28066 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28067 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28068 assemble_name (asm_out_file, label);
28069 table->view = ++lvugid;
28071 else
28073 table->symviews_since_reset = 0;
28074 if (FORCE_RESETTING_VIEW_P (table->view))
28075 fputs (" view -0", asm_out_file);
28076 else
28077 fputs (" view 0", asm_out_file);
28078 /* Mark the present view as a zero view. Earlier debug
28079 binds may have already added its id to loclists to be
28080 emitted later, so we can't reuse the id for something
28081 else. However, it's good to know whether a view is
28082 known to be zero, because then we may be able to
28083 optimize out locviews that are all zeros, so take
28084 note of it in zero_view_p. */
28085 if (!zero_view_p)
28086 zero_view_p = BITMAP_GGC_ALLOC ();
28087 bitmap_set_bit (zero_view_p, lvugid);
28088 table->view = ++lvugid;
28091 putc ('\n', asm_out_file);
28093 else
28095 unsigned int label_num = ++line_info_label_num;
28097 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28099 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28100 push_dw_line_info_entry (table, LI_adv_address, label_num);
28101 else
28102 push_dw_line_info_entry (table, LI_set_address, label_num);
28103 if (debug_variable_location_views)
28105 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28106 if (resetting)
28107 table->view = 0;
28109 if (flag_debug_asm)
28110 fprintf (asm_out_file, "\t%s view %s%d\n",
28111 ASM_COMMENT_START,
28112 resetting ? "-" : "",
28113 table->view);
28115 table->view++;
28117 if (file_num != table->file_num)
28118 push_dw_line_info_entry (table, LI_set_file, file_num);
28119 if (discriminator != table->discrim_num)
28120 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28121 if (is_stmt != table->is_stmt)
28122 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28123 push_dw_line_info_entry (table, LI_set_line, line);
28124 if (debug_column_info)
28125 push_dw_line_info_entry (table, LI_set_column, column);
28128 table->file_num = file_num;
28129 table->line_num = line;
28130 table->column_num = column;
28131 table->discrim_num = discriminator;
28132 table->is_stmt = is_stmt;
28133 table->in_use = true;
28136 /* Record the beginning of a new source file. */
28138 static void
28139 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28141 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28143 macinfo_entry e;
28144 e.code = DW_MACINFO_start_file;
28145 e.lineno = lineno;
28146 e.info = ggc_strdup (filename);
28147 vec_safe_push (macinfo_table, e);
28151 /* Record the end of a source file. */
28153 static void
28154 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28156 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28158 macinfo_entry e;
28159 e.code = DW_MACINFO_end_file;
28160 e.lineno = lineno;
28161 e.info = NULL;
28162 vec_safe_push (macinfo_table, e);
28166 /* Called from debug_define in toplev.c. The `buffer' parameter contains
28167 the tail part of the directive line, i.e. the part which is past the
28168 initial whitespace, #, whitespace, directive-name, whitespace part. */
28170 static void
28171 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28172 const char *buffer ATTRIBUTE_UNUSED)
28174 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28176 macinfo_entry e;
28177 /* Insert a dummy first entry to be able to optimize the whole
28178 predefined macro block using DW_MACRO_import. */
28179 if (macinfo_table->is_empty () && lineno <= 1)
28181 e.code = 0;
28182 e.lineno = 0;
28183 e.info = NULL;
28184 vec_safe_push (macinfo_table, e);
28186 e.code = DW_MACINFO_define;
28187 e.lineno = lineno;
28188 e.info = ggc_strdup (buffer);
28189 vec_safe_push (macinfo_table, e);
28193 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
28194 the tail part of the directive line, i.e. the part which is past the
28195 initial whitespace, #, whitespace, directive-name, whitespace part. */
28197 static void
28198 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28199 const char *buffer ATTRIBUTE_UNUSED)
28201 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28203 macinfo_entry e;
28204 /* Insert a dummy first entry to be able to optimize the whole
28205 predefined macro block using DW_MACRO_import. */
28206 if (macinfo_table->is_empty () && lineno <= 1)
28208 e.code = 0;
28209 e.lineno = 0;
28210 e.info = NULL;
28211 vec_safe_push (macinfo_table, e);
28213 e.code = DW_MACINFO_undef;
28214 e.lineno = lineno;
28215 e.info = ggc_strdup (buffer);
28216 vec_safe_push (macinfo_table, e);
28220 /* Helpers to manipulate hash table of CUs. */
28222 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28224 static inline hashval_t hash (const macinfo_entry *);
28225 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28228 inline hashval_t
28229 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28231 return htab_hash_string (entry->info);
28234 inline bool
28235 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28236 const macinfo_entry *entry2)
28238 return !strcmp (entry1->info, entry2->info);
28241 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28243 /* Output a single .debug_macinfo entry. */
28245 static void
28246 output_macinfo_op (macinfo_entry *ref)
28248 int file_num;
28249 size_t len;
28250 struct indirect_string_node *node;
28251 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28252 struct dwarf_file_data *fd;
28254 switch (ref->code)
28256 case DW_MACINFO_start_file:
28257 fd = lookup_filename (ref->info);
28258 file_num = maybe_emit_file (fd);
28259 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28260 dw2_asm_output_data_uleb128 (ref->lineno,
28261 "Included from line number %lu",
28262 (unsigned long) ref->lineno);
28263 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28264 break;
28265 case DW_MACINFO_end_file:
28266 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28267 break;
28268 case DW_MACINFO_define:
28269 case DW_MACINFO_undef:
28270 len = strlen (ref->info) + 1;
28271 if (!dwarf_strict
28272 && len > DWARF_OFFSET_SIZE
28273 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28274 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28276 ref->code = ref->code == DW_MACINFO_define
28277 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28278 output_macinfo_op (ref);
28279 return;
28281 dw2_asm_output_data (1, ref->code,
28282 ref->code == DW_MACINFO_define
28283 ? "Define macro" : "Undefine macro");
28284 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28285 (unsigned long) ref->lineno);
28286 dw2_asm_output_nstring (ref->info, -1, "The macro");
28287 break;
28288 case DW_MACRO_define_strp:
28289 case DW_MACRO_undef_strp:
28290 /* NB: dwarf2out_finish performs:
28291 1. save_macinfo_strings
28292 2. hash table traverse of index_string
28293 3. output_macinfo -> output_macinfo_op
28294 4. output_indirect_strings
28295 -> hash table traverse of output_index_string
28297 When output_macinfo_op is called, all index strings have been
28298 added to hash table by save_macinfo_strings and we can't pass
28299 INSERT to find_slot_with_hash which may expand hash table, even
28300 if no insertion is needed, and change hash table traverse order
28301 between index_string and output_index_string. */
28302 node = find_AT_string (ref->info, NO_INSERT);
28303 gcc_assert (node
28304 && (node->form == DW_FORM_strp
28305 || node->form == dwarf_FORM (DW_FORM_strx)));
28306 dw2_asm_output_data (1, ref->code,
28307 ref->code == DW_MACRO_define_strp
28308 ? "Define macro strp"
28309 : "Undefine macro strp");
28310 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28311 (unsigned long) ref->lineno);
28312 if (node->form == DW_FORM_strp)
28313 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
28314 debug_str_section, "The macro: \"%s\"",
28315 ref->info);
28316 else
28317 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28318 ref->info);
28319 break;
28320 case DW_MACRO_import:
28321 dw2_asm_output_data (1, ref->code, "Import");
28322 ASM_GENERATE_INTERNAL_LABEL (label,
28323 DEBUG_MACRO_SECTION_LABEL,
28324 ref->lineno + macinfo_label_base);
28325 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
28326 break;
28327 default:
28328 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28329 ASM_COMMENT_START, (unsigned long) ref->code);
28330 break;
28334 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28335 other compilation unit .debug_macinfo sections. IDX is the first
28336 index of a define/undef, return the number of ops that should be
28337 emitted in a comdat .debug_macinfo section and emit
28338 a DW_MACRO_import entry referencing it.
28339 If the define/undef entry should be emitted normally, return 0. */
28341 static unsigned
28342 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28343 macinfo_hash_type **macinfo_htab)
28345 macinfo_entry *first, *second, *cur, *inc;
28346 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28347 unsigned char checksum[16];
28348 struct md5_ctx ctx;
28349 char *grp_name, *tail;
28350 const char *base;
28351 unsigned int i, count, encoded_filename_len, linebuf_len;
28352 macinfo_entry **slot;
28354 first = &(*macinfo_table)[idx];
28355 second = &(*macinfo_table)[idx + 1];
28357 /* Optimize only if there are at least two consecutive define/undef ops,
28358 and either all of them are before first DW_MACINFO_start_file
28359 with lineno {0,1} (i.e. predefined macro block), or all of them are
28360 in some included header file. */
28361 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28362 return 0;
28363 if (vec_safe_is_empty (files))
28365 if (first->lineno > 1 || second->lineno > 1)
28366 return 0;
28368 else if (first->lineno == 0)
28369 return 0;
28371 /* Find the last define/undef entry that can be grouped together
28372 with first and at the same time compute md5 checksum of their
28373 codes, linenumbers and strings. */
28374 md5_init_ctx (&ctx);
28375 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28376 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28377 break;
28378 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28379 break;
28380 else
28382 unsigned char code = cur->code;
28383 md5_process_bytes (&code, 1, &ctx);
28384 checksum_uleb128 (cur->lineno, &ctx);
28385 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28387 md5_finish_ctx (&ctx, checksum);
28388 count = i - idx;
28390 /* From the containing include filename (if any) pick up just
28391 usable characters from its basename. */
28392 if (vec_safe_is_empty (files))
28393 base = "";
28394 else
28395 base = lbasename (files->last ().info);
28396 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28397 if (ISIDNUM (base[i]) || base[i] == '.')
28398 encoded_filename_len++;
28399 /* Count . at the end. */
28400 if (encoded_filename_len)
28401 encoded_filename_len++;
28403 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28404 linebuf_len = strlen (linebuf);
28406 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28407 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28408 + 16 * 2 + 1);
28409 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
28410 tail = grp_name + 4;
28411 if (encoded_filename_len)
28413 for (i = 0; base[i]; i++)
28414 if (ISIDNUM (base[i]) || base[i] == '.')
28415 *tail++ = base[i];
28416 *tail++ = '.';
28418 memcpy (tail, linebuf, linebuf_len);
28419 tail += linebuf_len;
28420 *tail++ = '.';
28421 for (i = 0; i < 16; i++)
28422 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28424 /* Construct a macinfo_entry for DW_MACRO_import
28425 in the empty vector entry before the first define/undef. */
28426 inc = &(*macinfo_table)[idx - 1];
28427 inc->code = DW_MACRO_import;
28428 inc->lineno = 0;
28429 inc->info = ggc_strdup (grp_name);
28430 if (!*macinfo_htab)
28431 *macinfo_htab = new macinfo_hash_type (10);
28432 /* Avoid emitting duplicates. */
28433 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28434 if (*slot != NULL)
28436 inc->code = 0;
28437 inc->info = NULL;
28438 /* If such an entry has been used before, just emit
28439 a DW_MACRO_import op. */
28440 inc = *slot;
28441 output_macinfo_op (inc);
28442 /* And clear all macinfo_entry in the range to avoid emitting them
28443 in the second pass. */
28444 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28446 cur->code = 0;
28447 cur->info = NULL;
28450 else
28452 *slot = inc;
28453 inc->lineno = (*macinfo_htab)->elements ();
28454 output_macinfo_op (inc);
28456 return count;
28459 /* Save any strings needed by the macinfo table in the debug str
28460 table. All strings must be collected into the table by the time
28461 index_string is called. */
28463 static void
28464 save_macinfo_strings (void)
28466 unsigned len;
28467 unsigned i;
28468 macinfo_entry *ref;
28470 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28472 switch (ref->code)
28474 /* Match the logic in output_macinfo_op to decide on
28475 indirect strings. */
28476 case DW_MACINFO_define:
28477 case DW_MACINFO_undef:
28478 len = strlen (ref->info) + 1;
28479 if (!dwarf_strict
28480 && len > DWARF_OFFSET_SIZE
28481 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28482 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28483 set_indirect_string (find_AT_string (ref->info));
28484 break;
28485 case DW_MACINFO_start_file:
28486 /* -gsplit-dwarf -g3 will also output filename as indirect
28487 string. */
28488 if (!dwarf_split_debug_info)
28489 break;
28490 /* Fall through. */
28491 case DW_MACRO_define_strp:
28492 case DW_MACRO_undef_strp:
28493 set_indirect_string (find_AT_string (ref->info));
28494 break;
28495 default:
28496 break;
28501 /* Output macinfo section(s). */
28503 static void
28504 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28506 unsigned i;
28507 unsigned long length = vec_safe_length (macinfo_table);
28508 macinfo_entry *ref;
28509 vec<macinfo_entry, va_gc> *files = NULL;
28510 macinfo_hash_type *macinfo_htab = NULL;
28511 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28513 if (! length)
28514 return;
28516 /* output_macinfo* uses these interchangeably. */
28517 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28518 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28519 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28520 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28522 /* AIX Assembler inserts the length, so adjust the reference to match the
28523 offset expected by debuggers. */
28524 strcpy (dl_section_ref, debug_line_label);
28525 if (XCOFF_DEBUGGING_INFO)
28526 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28528 /* For .debug_macro emit the section header. */
28529 if (!dwarf_strict || dwarf_version >= 5)
28531 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28532 "DWARF macro version number");
28533 if (DWARF_OFFSET_SIZE == 8)
28534 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28535 else
28536 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28537 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
28538 debug_line_section, NULL);
28541 /* In the first loop, it emits the primary .debug_macinfo section
28542 and after each emitted op the macinfo_entry is cleared.
28543 If a longer range of define/undef ops can be optimized using
28544 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28545 the vector before the first define/undef in the range and the
28546 whole range of define/undef ops is not emitted and kept. */
28547 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28549 switch (ref->code)
28551 case DW_MACINFO_start_file:
28552 vec_safe_push (files, *ref);
28553 break;
28554 case DW_MACINFO_end_file:
28555 if (!vec_safe_is_empty (files))
28556 files->pop ();
28557 break;
28558 case DW_MACINFO_define:
28559 case DW_MACINFO_undef:
28560 if ((!dwarf_strict || dwarf_version >= 5)
28561 && HAVE_COMDAT_GROUP
28562 && vec_safe_length (files) != 1
28563 && i > 0
28564 && i + 1 < length
28565 && (*macinfo_table)[i - 1].code == 0)
28567 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28568 if (count)
28570 i += count - 1;
28571 continue;
28574 break;
28575 case 0:
28576 /* A dummy entry may be inserted at the beginning to be able
28577 to optimize the whole block of predefined macros. */
28578 if (i == 0)
28579 continue;
28580 default:
28581 break;
28583 output_macinfo_op (ref);
28584 ref->info = NULL;
28585 ref->code = 0;
28588 if (!macinfo_htab)
28589 return;
28591 /* Save the number of transparent includes so we can adjust the
28592 label number for the fat LTO object DWARF. */
28593 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
28595 delete macinfo_htab;
28596 macinfo_htab = NULL;
28598 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
28599 terminate the current chain and switch to a new comdat .debug_macinfo
28600 section and emit the define/undef entries within it. */
28601 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28602 switch (ref->code)
28604 case 0:
28605 continue;
28606 case DW_MACRO_import:
28608 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28609 tree comdat_key = get_identifier (ref->info);
28610 /* Terminate the previous .debug_macinfo section. */
28611 dw2_asm_output_data (1, 0, "End compilation unit");
28612 targetm.asm_out.named_section (debug_macinfo_section_name,
28613 SECTION_DEBUG
28614 | SECTION_LINKONCE
28615 | (early_lto_debug
28616 ? SECTION_EXCLUDE : 0),
28617 comdat_key);
28618 ASM_GENERATE_INTERNAL_LABEL (label,
28619 DEBUG_MACRO_SECTION_LABEL,
28620 ref->lineno + macinfo_label_base);
28621 ASM_OUTPUT_LABEL (asm_out_file, label);
28622 ref->code = 0;
28623 ref->info = NULL;
28624 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28625 "DWARF macro version number");
28626 if (DWARF_OFFSET_SIZE == 8)
28627 dw2_asm_output_data (1, 1, "Flags: 64-bit");
28628 else
28629 dw2_asm_output_data (1, 0, "Flags: 32-bit");
28631 break;
28632 case DW_MACINFO_define:
28633 case DW_MACINFO_undef:
28634 output_macinfo_op (ref);
28635 ref->code = 0;
28636 ref->info = NULL;
28637 break;
28638 default:
28639 gcc_unreachable ();
28642 macinfo_label_base += macinfo_label_base_adj;
28645 /* Initialize the various sections and labels for dwarf output and prefix
28646 them with PREFIX if non-NULL. Returns the generation (zero based
28647 number of times function was called). */
28649 static unsigned
28650 init_sections_and_labels (bool early_lto_debug)
28652 /* As we may get called multiple times have a generation count for
28653 labels. */
28654 static unsigned generation = 0;
28656 if (early_lto_debug)
28658 if (!dwarf_split_debug_info)
28660 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28661 SECTION_DEBUG | SECTION_EXCLUDE,
28662 NULL);
28663 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
28664 SECTION_DEBUG | SECTION_EXCLUDE,
28665 NULL);
28666 debug_macinfo_section_name
28667 = ((dwarf_strict && dwarf_version < 5)
28668 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
28669 debug_macinfo_section = get_section (debug_macinfo_section_name,
28670 SECTION_DEBUG
28671 | SECTION_EXCLUDE, NULL);
28673 else
28675 /* ??? Which of the following do we need early? */
28676 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
28677 SECTION_DEBUG | SECTION_EXCLUDE,
28678 NULL);
28679 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
28680 SECTION_DEBUG | SECTION_EXCLUDE,
28681 NULL);
28682 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28683 SECTION_DEBUG
28684 | SECTION_EXCLUDE, NULL);
28685 debug_skeleton_abbrev_section
28686 = get_section (DEBUG_LTO_ABBREV_SECTION,
28687 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28688 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28689 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28690 generation);
28692 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28693 stay in the main .o, but the skeleton_line goes into the split
28694 off dwo. */
28695 debug_skeleton_line_section
28696 = get_section (DEBUG_LTO_LINE_SECTION,
28697 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28698 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28699 DEBUG_SKELETON_LINE_SECTION_LABEL,
28700 generation);
28701 debug_str_offsets_section
28702 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
28703 SECTION_DEBUG | SECTION_EXCLUDE,
28704 NULL);
28705 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28706 DEBUG_SKELETON_INFO_SECTION_LABEL,
28707 generation);
28708 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
28709 DEBUG_STR_DWO_SECTION_FLAGS,
28710 NULL);
28711 debug_macinfo_section_name
28712 = ((dwarf_strict && dwarf_version < 5)
28713 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
28714 debug_macinfo_section = get_section (debug_macinfo_section_name,
28715 SECTION_DEBUG | SECTION_EXCLUDE,
28716 NULL);
28718 /* For macro info and the file table we have to refer to a
28719 debug_line section. */
28720 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
28721 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28722 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28723 DEBUG_LINE_SECTION_LABEL, generation);
28725 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
28726 DEBUG_STR_SECTION_FLAGS
28727 | SECTION_EXCLUDE, NULL);
28728 if (!dwarf_split_debug_info)
28729 debug_line_str_section
28730 = get_section (DEBUG_LTO_LINE_STR_SECTION,
28731 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
28733 else
28735 if (!dwarf_split_debug_info)
28737 debug_info_section = get_section (DEBUG_INFO_SECTION,
28738 SECTION_DEBUG, NULL);
28739 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28740 SECTION_DEBUG, NULL);
28741 debug_loc_section = get_section (dwarf_version >= 5
28742 ? DEBUG_LOCLISTS_SECTION
28743 : DEBUG_LOC_SECTION,
28744 SECTION_DEBUG, NULL);
28745 debug_macinfo_section_name
28746 = ((dwarf_strict && dwarf_version < 5)
28747 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
28748 debug_macinfo_section = get_section (debug_macinfo_section_name,
28749 SECTION_DEBUG, NULL);
28751 else
28753 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
28754 SECTION_DEBUG | SECTION_EXCLUDE,
28755 NULL);
28756 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
28757 SECTION_DEBUG | SECTION_EXCLUDE,
28758 NULL);
28759 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
28760 SECTION_DEBUG, NULL);
28761 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
28762 SECTION_DEBUG, NULL);
28763 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28764 SECTION_DEBUG, NULL);
28765 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28766 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28767 generation);
28769 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28770 stay in the main .o, but the skeleton_line goes into the
28771 split off dwo. */
28772 debug_skeleton_line_section
28773 = get_section (DEBUG_DWO_LINE_SECTION,
28774 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28775 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28776 DEBUG_SKELETON_LINE_SECTION_LABEL,
28777 generation);
28778 debug_str_offsets_section
28779 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
28780 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28781 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28782 DEBUG_SKELETON_INFO_SECTION_LABEL,
28783 generation);
28784 debug_loc_section = get_section (dwarf_version >= 5
28785 ? DEBUG_DWO_LOCLISTS_SECTION
28786 : DEBUG_DWO_LOC_SECTION,
28787 SECTION_DEBUG | SECTION_EXCLUDE,
28788 NULL);
28789 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
28790 DEBUG_STR_DWO_SECTION_FLAGS,
28791 NULL);
28792 debug_macinfo_section_name
28793 = ((dwarf_strict && dwarf_version < 5)
28794 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
28795 debug_macinfo_section = get_section (debug_macinfo_section_name,
28796 SECTION_DEBUG | SECTION_EXCLUDE,
28797 NULL);
28799 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
28800 SECTION_DEBUG, NULL);
28801 debug_line_section = get_section (DEBUG_LINE_SECTION,
28802 SECTION_DEBUG, NULL);
28803 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
28804 SECTION_DEBUG, NULL);
28805 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
28806 SECTION_DEBUG, NULL);
28807 debug_str_section = get_section (DEBUG_STR_SECTION,
28808 DEBUG_STR_SECTION_FLAGS, NULL);
28809 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
28810 || asm_outputs_debug_line_str ())
28811 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
28812 DEBUG_STR_SECTION_FLAGS, NULL);
28814 debug_ranges_section = get_section (dwarf_version >= 5
28815 ? DEBUG_RNGLISTS_SECTION
28816 : DEBUG_RANGES_SECTION,
28817 SECTION_DEBUG, NULL);
28818 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
28819 SECTION_DEBUG, NULL);
28822 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
28823 DEBUG_ABBREV_SECTION_LABEL, generation);
28824 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
28825 DEBUG_INFO_SECTION_LABEL, generation);
28826 info_section_emitted = false;
28827 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28828 DEBUG_LINE_SECTION_LABEL, generation);
28829 /* There are up to 4 unique ranges labels per generation.
28830 See also output_rnglists. */
28831 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
28832 DEBUG_RANGES_SECTION_LABEL, generation * 4);
28833 if (dwarf_version >= 5 && dwarf_split_debug_info)
28834 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
28835 DEBUG_RANGES_SECTION_LABEL,
28836 1 + generation * 4);
28837 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
28838 DEBUG_ADDR_SECTION_LABEL, generation);
28839 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
28840 (dwarf_strict && dwarf_version < 5)
28841 ? DEBUG_MACINFO_SECTION_LABEL
28842 : DEBUG_MACRO_SECTION_LABEL, generation);
28843 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
28844 generation);
28846 ++generation;
28847 return generation - 1;
28850 /* Set up for Dwarf output at the start of compilation. */
28852 static void
28853 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
28855 /* Allocate the file_table. */
28856 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
28858 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28859 /* Allocate the decl_die_table. */
28860 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
28862 /* Allocate the decl_loc_table. */
28863 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
28865 /* Allocate the cached_dw_loc_list_table. */
28866 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
28868 /* Allocate the initial hunk of the abbrev_die_table. */
28869 vec_alloc (abbrev_die_table, 256);
28870 /* Zero-th entry is allocated, but unused. */
28871 abbrev_die_table->quick_push (NULL);
28873 /* Allocate the dwarf_proc_stack_usage_map. */
28874 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
28876 /* Allocate the pubtypes and pubnames vectors. */
28877 vec_alloc (pubname_table, 32);
28878 vec_alloc (pubtype_table, 32);
28880 vec_alloc (incomplete_types, 64);
28882 vec_alloc (used_rtx_array, 32);
28884 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28885 vec_alloc (macinfo_table, 64);
28886 #endif
28888 /* If front-ends already registered a main translation unit but we were not
28889 ready to perform the association, do this now. */
28890 if (main_translation_unit != NULL_TREE)
28891 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
28894 /* Called before compile () starts outputtting functions, variables
28895 and toplevel asms into assembly. */
28897 static void
28898 dwarf2out_assembly_start (void)
28900 if (text_section_line_info)
28901 return;
28903 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28904 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
28905 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
28906 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
28907 COLD_TEXT_SECTION_LABEL, 0);
28908 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
28910 switch_to_section (text_section);
28911 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
28912 #endif
28914 /* Make sure the line number table for .text always exists. */
28915 text_section_line_info = new_line_info_table ();
28916 text_section_line_info->end_label = text_end_label;
28918 #ifdef DWARF2_LINENO_DEBUGGING_INFO
28919 cur_line_info_table = text_section_line_info;
28920 #endif
28922 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
28923 && dwarf2out_do_cfi_asm ()
28924 && !dwarf2out_do_eh_frame ())
28925 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
28928 /* A helper function for dwarf2out_finish called through
28929 htab_traverse. Assign a string its index. All strings must be
28930 collected into the table by the time index_string is called,
28931 because the indexing code relies on htab_traverse to traverse nodes
28932 in the same order for each run. */
28935 index_string (indirect_string_node **h, unsigned int *index)
28937 indirect_string_node *node = *h;
28939 find_string_form (node);
28940 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28942 gcc_assert (node->index == NO_INDEX_ASSIGNED);
28943 node->index = *index;
28944 *index += 1;
28946 return 1;
28949 /* A helper function for output_indirect_strings called through
28950 htab_traverse. Output the offset to a string and update the
28951 current offset. */
28954 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
28956 indirect_string_node *node = *h;
28958 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28960 /* Assert that this node has been assigned an index. */
28961 gcc_assert (node->index != NO_INDEX_ASSIGNED
28962 && node->index != NOT_INDEXED);
28963 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
28964 "indexed string 0x%x: %s", node->index, node->str);
28965 *offset += strlen (node->str) + 1;
28967 return 1;
28970 /* A helper function for dwarf2out_finish called through
28971 htab_traverse. Output the indexed string. */
28974 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
28976 struct indirect_string_node *node = *h;
28978 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28980 /* Assert that the strings are output in the same order as their
28981 indexes were assigned. */
28982 gcc_assert (*cur_idx == node->index);
28983 assemble_string (node->str, strlen (node->str) + 1);
28984 *cur_idx += 1;
28986 return 1;
28989 /* A helper function for output_indirect_strings. Counts the number
28990 of index strings offsets. Must match the logic of the functions
28991 output_index_string[_offsets] above. */
28993 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
28995 struct indirect_string_node *node = *h;
28997 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28998 *last_idx += 1;
28999 return 1;
29002 /* A helper function for dwarf2out_finish called through
29003 htab_traverse. Emit one queued .debug_str string. */
29006 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29008 struct indirect_string_node *node = *h;
29010 node->form = find_string_form (node);
29011 if (node->form == form && node->refcount > 0)
29013 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29014 assemble_string (node->str, strlen (node->str) + 1);
29017 return 1;
29020 /* Output the indexed string table. */
29022 static void
29023 output_indirect_strings (void)
29025 switch_to_section (debug_str_section);
29026 if (!dwarf_split_debug_info)
29027 debug_str_hash->traverse<enum dwarf_form,
29028 output_indirect_string> (DW_FORM_strp);
29029 else
29031 unsigned int offset = 0;
29032 unsigned int cur_idx = 0;
29034 if (skeleton_debug_str_hash)
29035 skeleton_debug_str_hash->traverse<enum dwarf_form,
29036 output_indirect_string> (DW_FORM_strp);
29038 switch_to_section (debug_str_offsets_section);
29039 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29040 header. Note that we don't need to generate a label to the
29041 actual index table following the header here, because this is
29042 for the split dwarf case only. In an .dwo file there is only
29043 one string offsets table (and one debug info section). But
29044 if we would start using string offset tables for the main (or
29045 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29046 pointing to the actual index after the header. Split dwarf
29047 units will never have a string offsets base attribute. When
29048 a split unit is moved into a .dwp file the string offsets can
29049 be found through the .debug_cu_index section table. */
29050 if (dwarf_version >= 5)
29052 unsigned int last_idx = 0;
29053 unsigned long str_offsets_length;
29055 debug_str_hash->traverse_noresize
29056 <unsigned int *, count_index_strings> (&last_idx);
29057 str_offsets_length = last_idx * DWARF_OFFSET_SIZE + 4;
29058 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29059 dw2_asm_output_data (4, 0xffffffff,
29060 "Escape value for 64-bit DWARF extension");
29061 dw2_asm_output_data (DWARF_OFFSET_SIZE, str_offsets_length,
29062 "Length of string offsets unit");
29063 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29064 dw2_asm_output_data (2, 0, "Header zero padding");
29066 debug_str_hash->traverse_noresize
29067 <unsigned int *, output_index_string_offset> (&offset);
29068 switch_to_section (debug_str_dwo_section);
29069 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29070 (&cur_idx);
29074 /* Callback for htab_traverse to assign an index to an entry in the
29075 table, and to write that entry to the .debug_addr section. */
29078 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29080 addr_table_entry *entry = *slot;
29082 if (entry->refcount == 0)
29084 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29085 || entry->index == NOT_INDEXED);
29086 return 1;
29089 gcc_assert (entry->index == *cur_index);
29090 (*cur_index)++;
29092 switch (entry->kind)
29094 case ate_kind_rtx:
29095 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29096 "0x%x", entry->index);
29097 break;
29098 case ate_kind_rtx_dtprel:
29099 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29100 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29101 DWARF2_ADDR_SIZE,
29102 entry->addr.rtl);
29103 fputc ('\n', asm_out_file);
29104 break;
29105 case ate_kind_label:
29106 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29107 "0x%x", entry->index);
29108 break;
29109 default:
29110 gcc_unreachable ();
29112 return 1;
29115 /* A helper function for dwarf2out_finish. Counts the number
29116 of indexed addresses. Must match the logic of the functions
29117 output_addr_table_entry above. */
29119 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29121 addr_table_entry *entry = *slot;
29123 if (entry->refcount > 0)
29124 *last_idx += 1;
29125 return 1;
29128 /* Produce the .debug_addr section. */
29130 static void
29131 output_addr_table (void)
29133 unsigned int index = 0;
29134 if (addr_index_table == NULL || addr_index_table->size () == 0)
29135 return;
29137 switch_to_section (debug_addr_section);
29138 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29139 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29140 before DWARF5, didn't have a header for .debug_addr units.
29141 DWARF5 specifies a small header when address tables are used. */
29142 if (dwarf_version >= 5)
29144 unsigned int last_idx = 0;
29145 unsigned long addrs_length;
29147 addr_index_table->traverse_noresize
29148 <unsigned int *, count_index_addrs> (&last_idx);
29149 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29151 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29152 dw2_asm_output_data (4, 0xffffffff,
29153 "Escape value for 64-bit DWARF extension");
29154 dw2_asm_output_data (DWARF_OFFSET_SIZE, addrs_length,
29155 "Length of Address Unit");
29156 dw2_asm_output_data (2, 5, "DWARF addr version");
29157 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29158 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29160 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29162 addr_index_table
29163 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29166 #if ENABLE_ASSERT_CHECKING
29167 /* Verify that all marks are clear. */
29169 static void
29170 verify_marks_clear (dw_die_ref die)
29172 dw_die_ref c;
29174 gcc_assert (! die->die_mark);
29175 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29177 #endif /* ENABLE_ASSERT_CHECKING */
29179 /* Clear the marks for a die and its children.
29180 Be cool if the mark isn't set. */
29182 static void
29183 prune_unmark_dies (dw_die_ref die)
29185 dw_die_ref c;
29187 if (die->die_mark)
29188 die->die_mark = 0;
29189 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29192 /* Given LOC that is referenced by a DIE we're marking as used, find all
29193 referenced DWARF procedures it references and mark them as used. */
29195 static void
29196 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29198 for (; loc != NULL; loc = loc->dw_loc_next)
29199 switch (loc->dw_loc_opc)
29201 case DW_OP_implicit_pointer:
29202 case DW_OP_convert:
29203 case DW_OP_reinterpret:
29204 case DW_OP_GNU_implicit_pointer:
29205 case DW_OP_GNU_convert:
29206 case DW_OP_GNU_reinterpret:
29207 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29208 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29209 break;
29210 case DW_OP_GNU_variable_value:
29211 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29213 dw_die_ref ref
29214 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29215 if (ref == NULL)
29216 break;
29217 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29218 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29219 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29221 /* FALLTHRU */
29222 case DW_OP_call2:
29223 case DW_OP_call4:
29224 case DW_OP_call_ref:
29225 case DW_OP_const_type:
29226 case DW_OP_GNU_const_type:
29227 case DW_OP_GNU_parameter_ref:
29228 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29229 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29230 break;
29231 case DW_OP_regval_type:
29232 case DW_OP_deref_type:
29233 case DW_OP_GNU_regval_type:
29234 case DW_OP_GNU_deref_type:
29235 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29236 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29237 break;
29238 case DW_OP_entry_value:
29239 case DW_OP_GNU_entry_value:
29240 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29241 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29242 break;
29243 default:
29244 break;
29248 /* Given DIE that we're marking as used, find any other dies
29249 it references as attributes and mark them as used. */
29251 static void
29252 prune_unused_types_walk_attribs (dw_die_ref die)
29254 dw_attr_node *a;
29255 unsigned ix;
29257 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29259 switch (AT_class (a))
29261 /* Make sure DWARF procedures referenced by location descriptions will
29262 get emitted. */
29263 case dw_val_class_loc:
29264 prune_unused_types_walk_loc_descr (AT_loc (a));
29265 break;
29266 case dw_val_class_loc_list:
29267 for (dw_loc_list_ref list = AT_loc_list (a);
29268 list != NULL;
29269 list = list->dw_loc_next)
29270 prune_unused_types_walk_loc_descr (list->expr);
29271 break;
29273 case dw_val_class_view_list:
29274 /* This points to a loc_list in another attribute, so it's
29275 already covered. */
29276 break;
29278 case dw_val_class_die_ref:
29279 /* A reference to another DIE.
29280 Make sure that it will get emitted.
29281 If it was broken out into a comdat group, don't follow it. */
29282 if (! AT_ref (a)->comdat_type_p
29283 || a->dw_attr == DW_AT_specification)
29284 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29285 break;
29287 case dw_val_class_str:
29288 /* Set the string's refcount to 0 so that prune_unused_types_mark
29289 accounts properly for it. */
29290 a->dw_attr_val.v.val_str->refcount = 0;
29291 break;
29293 default:
29294 break;
29299 /* Mark the generic parameters and arguments children DIEs of DIE. */
29301 static void
29302 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29304 dw_die_ref c;
29306 if (die == NULL || die->die_child == NULL)
29307 return;
29308 c = die->die_child;
29311 if (is_template_parameter (c))
29312 prune_unused_types_mark (c, 1);
29313 c = c->die_sib;
29314 } while (c && c != die->die_child);
29317 /* Mark DIE as being used. If DOKIDS is true, then walk down
29318 to DIE's children. */
29320 static void
29321 prune_unused_types_mark (dw_die_ref die, int dokids)
29323 dw_die_ref c;
29325 if (die->die_mark == 0)
29327 /* We haven't done this node yet. Mark it as used. */
29328 die->die_mark = 1;
29329 /* If this is the DIE of a generic type instantiation,
29330 mark the children DIEs that describe its generic parms and
29331 args. */
29332 prune_unused_types_mark_generic_parms_dies (die);
29334 /* We also have to mark its parents as used.
29335 (But we don't want to mark our parent's kids due to this,
29336 unless it is a class.) */
29337 if (die->die_parent)
29338 prune_unused_types_mark (die->die_parent,
29339 class_scope_p (die->die_parent));
29341 /* Mark any referenced nodes. */
29342 prune_unused_types_walk_attribs (die);
29344 /* If this node is a specification,
29345 also mark the definition, if it exists. */
29346 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29347 prune_unused_types_mark (die->die_definition, 1);
29350 if (dokids && die->die_mark != 2)
29352 /* We need to walk the children, but haven't done so yet.
29353 Remember that we've walked the kids. */
29354 die->die_mark = 2;
29356 /* If this is an array type, we need to make sure our
29357 kids get marked, even if they're types. If we're
29358 breaking out types into comdat sections, do this
29359 for all type definitions. */
29360 if (die->die_tag == DW_TAG_array_type
29361 || (use_debug_types
29362 && is_type_die (die) && ! is_declaration_die (die)))
29363 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29364 else
29365 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29369 /* For local classes, look if any static member functions were emitted
29370 and if so, mark them. */
29372 static void
29373 prune_unused_types_walk_local_classes (dw_die_ref die)
29375 dw_die_ref c;
29377 if (die->die_mark == 2)
29378 return;
29380 switch (die->die_tag)
29382 case DW_TAG_structure_type:
29383 case DW_TAG_union_type:
29384 case DW_TAG_class_type:
29385 case DW_TAG_interface_type:
29386 break;
29388 case DW_TAG_subprogram:
29389 if (!get_AT_flag (die, DW_AT_declaration)
29390 || die->die_definition != NULL)
29391 prune_unused_types_mark (die, 1);
29392 return;
29394 default:
29395 return;
29398 /* Mark children. */
29399 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29402 /* Walk the tree DIE and mark types that we actually use. */
29404 static void
29405 prune_unused_types_walk (dw_die_ref die)
29407 dw_die_ref c;
29409 /* Don't do anything if this node is already marked and
29410 children have been marked as well. */
29411 if (die->die_mark == 2)
29412 return;
29414 switch (die->die_tag)
29416 case DW_TAG_structure_type:
29417 case DW_TAG_union_type:
29418 case DW_TAG_class_type:
29419 case DW_TAG_interface_type:
29420 if (die->die_perennial_p)
29421 break;
29423 for (c = die->die_parent; c; c = c->die_parent)
29424 if (c->die_tag == DW_TAG_subprogram)
29425 break;
29427 /* Finding used static member functions inside of classes
29428 is needed just for local classes, because for other classes
29429 static member function DIEs with DW_AT_specification
29430 are emitted outside of the DW_TAG_*_type. If we ever change
29431 it, we'd need to call this even for non-local classes. */
29432 if (c)
29433 prune_unused_types_walk_local_classes (die);
29435 /* It's a type node --- don't mark it. */
29436 return;
29438 case DW_TAG_const_type:
29439 case DW_TAG_packed_type:
29440 case DW_TAG_pointer_type:
29441 case DW_TAG_reference_type:
29442 case DW_TAG_rvalue_reference_type:
29443 case DW_TAG_volatile_type:
29444 case DW_TAG_typedef:
29445 case DW_TAG_array_type:
29446 case DW_TAG_friend:
29447 case DW_TAG_enumeration_type:
29448 case DW_TAG_subroutine_type:
29449 case DW_TAG_string_type:
29450 case DW_TAG_set_type:
29451 case DW_TAG_subrange_type:
29452 case DW_TAG_ptr_to_member_type:
29453 case DW_TAG_file_type:
29454 /* Type nodes are useful only when other DIEs reference them --- don't
29455 mark them. */
29456 /* FALLTHROUGH */
29458 case DW_TAG_dwarf_procedure:
29459 /* Likewise for DWARF procedures. */
29461 if (die->die_perennial_p)
29462 break;
29464 return;
29466 case DW_TAG_variable:
29467 if (flag_debug_only_used_symbols)
29469 if (die->die_perennial_p)
29470 break;
29472 /* premark_used_variables marks external variables --- don't mark
29473 them here. But function-local externals are always considered
29474 used. */
29475 if (get_AT (die, DW_AT_external))
29477 for (c = die->die_parent; c; c = c->die_parent)
29478 if (c->die_tag == DW_TAG_subprogram)
29479 break;
29480 if (!c)
29481 return;
29484 /* FALLTHROUGH */
29486 default:
29487 /* Mark everything else. */
29488 break;
29491 if (die->die_mark == 0)
29493 die->die_mark = 1;
29495 /* Now, mark any dies referenced from here. */
29496 prune_unused_types_walk_attribs (die);
29499 die->die_mark = 2;
29501 /* Mark children. */
29502 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29505 /* Increment the string counts on strings referred to from DIE's
29506 attributes. */
29508 static void
29509 prune_unused_types_update_strings (dw_die_ref die)
29511 dw_attr_node *a;
29512 unsigned ix;
29514 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29515 if (AT_class (a) == dw_val_class_str)
29517 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
29518 s->refcount++;
29519 /* Avoid unnecessarily putting strings that are used less than
29520 twice in the hash table. */
29521 if (s->refcount
29522 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
29524 indirect_string_node **slot
29525 = debug_str_hash->find_slot_with_hash (s->str,
29526 htab_hash_string (s->str),
29527 INSERT);
29528 gcc_assert (*slot == NULL);
29529 *slot = s;
29534 /* Mark DIE and its children as removed. */
29536 static void
29537 mark_removed (dw_die_ref die)
29539 dw_die_ref c;
29540 die->removed = true;
29541 FOR_EACH_CHILD (die, c, mark_removed (c));
29544 /* Remove from the tree DIE any dies that aren't marked. */
29546 static void
29547 prune_unused_types_prune (dw_die_ref die)
29549 dw_die_ref c;
29551 gcc_assert (die->die_mark);
29552 prune_unused_types_update_strings (die);
29554 if (! die->die_child)
29555 return;
29557 c = die->die_child;
29558 do {
29559 dw_die_ref prev = c, next;
29560 for (c = c->die_sib; ! c->die_mark; c = next)
29561 if (c == die->die_child)
29563 /* No marked children between 'prev' and the end of the list. */
29564 if (prev == c)
29565 /* No marked children at all. */
29566 die->die_child = NULL;
29567 else
29569 prev->die_sib = c->die_sib;
29570 die->die_child = prev;
29572 c->die_sib = NULL;
29573 mark_removed (c);
29574 return;
29576 else
29578 next = c->die_sib;
29579 c->die_sib = NULL;
29580 mark_removed (c);
29583 if (c != prev->die_sib)
29584 prev->die_sib = c;
29585 prune_unused_types_prune (c);
29586 } while (c != die->die_child);
29589 /* Remove dies representing declarations that we never use. */
29591 static void
29592 prune_unused_types (void)
29594 unsigned int i;
29595 limbo_die_node *node;
29596 comdat_type_node *ctnode;
29597 pubname_entry *pub;
29598 dw_die_ref base_type;
29600 #if ENABLE_ASSERT_CHECKING
29601 /* All the marks should already be clear. */
29602 verify_marks_clear (comp_unit_die ());
29603 for (node = limbo_die_list; node; node = node->next)
29604 verify_marks_clear (node->die);
29605 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29606 verify_marks_clear (ctnode->root_die);
29607 #endif /* ENABLE_ASSERT_CHECKING */
29609 /* Mark types that are used in global variables. */
29610 premark_types_used_by_global_vars ();
29612 /* Mark variables used in the symtab. */
29613 if (flag_debug_only_used_symbols)
29614 premark_used_variables ();
29616 /* Set the mark on nodes that are actually used. */
29617 prune_unused_types_walk (comp_unit_die ());
29618 for (node = limbo_die_list; node; node = node->next)
29619 prune_unused_types_walk (node->die);
29620 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29622 prune_unused_types_walk (ctnode->root_die);
29623 prune_unused_types_mark (ctnode->type_die, 1);
29626 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
29627 are unusual in that they are pubnames that are the children of pubtypes.
29628 They should only be marked via their parent DW_TAG_enumeration_type die,
29629 not as roots in themselves. */
29630 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
29631 if (pub->die->die_tag != DW_TAG_enumerator)
29632 prune_unused_types_mark (pub->die, 1);
29633 for (i = 0; base_types.iterate (i, &base_type); i++)
29634 prune_unused_types_mark (base_type, 1);
29636 /* Also set the mark on nodes that could be referenced by
29637 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
29638 by DW_TAG_inlined_subroutine origins. */
29639 cgraph_node *cnode;
29640 FOR_EACH_FUNCTION (cnode)
29641 if (cnode->referred_to_p (false))
29643 dw_die_ref die = lookup_decl_die (cnode->decl);
29644 if (die == NULL || die->die_mark)
29645 continue;
29646 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
29647 if (e->caller != cnode)
29649 prune_unused_types_mark (die, 1);
29650 break;
29654 if (debug_str_hash)
29655 debug_str_hash->empty ();
29656 if (skeleton_debug_str_hash)
29657 skeleton_debug_str_hash->empty ();
29658 prune_unused_types_prune (comp_unit_die ());
29659 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
29661 node = *pnode;
29662 if (!node->die->die_mark)
29663 *pnode = node->next;
29664 else
29666 prune_unused_types_prune (node->die);
29667 pnode = &node->next;
29670 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29671 prune_unused_types_prune (ctnode->root_die);
29673 /* Leave the marks clear. */
29674 prune_unmark_dies (comp_unit_die ());
29675 for (node = limbo_die_list; node; node = node->next)
29676 prune_unmark_dies (node->die);
29677 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29678 prune_unmark_dies (ctnode->root_die);
29681 /* Helpers to manipulate hash table of comdat type units. */
29683 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
29685 static inline hashval_t hash (const comdat_type_node *);
29686 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
29689 inline hashval_t
29690 comdat_type_hasher::hash (const comdat_type_node *type_node)
29692 hashval_t h;
29693 memcpy (&h, type_node->signature, sizeof (h));
29694 return h;
29697 inline bool
29698 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
29699 const comdat_type_node *type_node_2)
29701 return (! memcmp (type_node_1->signature, type_node_2->signature,
29702 DWARF_TYPE_SIGNATURE_SIZE));
29705 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
29706 to the location it would have been added, should we know its
29707 DECL_ASSEMBLER_NAME when we added other attributes. This will
29708 probably improve compactness of debug info, removing equivalent
29709 abbrevs, and hide any differences caused by deferring the
29710 computation of the assembler name, triggered by e.g. PCH. */
29712 static inline void
29713 move_linkage_attr (dw_die_ref die)
29715 unsigned ix = vec_safe_length (die->die_attr);
29716 dw_attr_node linkage = (*die->die_attr)[ix - 1];
29718 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
29719 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
29721 while (--ix > 0)
29723 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
29725 if (prev->dw_attr == DW_AT_decl_line
29726 || prev->dw_attr == DW_AT_decl_column
29727 || prev->dw_attr == DW_AT_name)
29728 break;
29731 if (ix != vec_safe_length (die->die_attr) - 1)
29733 die->die_attr->pop ();
29734 die->die_attr->quick_insert (ix, linkage);
29738 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
29739 referenced from typed stack ops and count how often they are used. */
29741 static void
29742 mark_base_types (dw_loc_descr_ref loc)
29744 dw_die_ref base_type = NULL;
29746 for (; loc; loc = loc->dw_loc_next)
29748 switch (loc->dw_loc_opc)
29750 case DW_OP_regval_type:
29751 case DW_OP_deref_type:
29752 case DW_OP_GNU_regval_type:
29753 case DW_OP_GNU_deref_type:
29754 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
29755 break;
29756 case DW_OP_convert:
29757 case DW_OP_reinterpret:
29758 case DW_OP_GNU_convert:
29759 case DW_OP_GNU_reinterpret:
29760 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
29761 continue;
29762 /* FALLTHRU */
29763 case DW_OP_const_type:
29764 case DW_OP_GNU_const_type:
29765 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
29766 break;
29767 case DW_OP_entry_value:
29768 case DW_OP_GNU_entry_value:
29769 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
29770 continue;
29771 default:
29772 continue;
29774 gcc_assert (base_type->die_parent == comp_unit_die ());
29775 if (base_type->die_mark)
29776 base_type->die_mark++;
29777 else
29779 base_types.safe_push (base_type);
29780 base_type->die_mark = 1;
29785 /* Comparison function for sorting marked base types. */
29787 static int
29788 base_type_cmp (const void *x, const void *y)
29790 dw_die_ref dx = *(const dw_die_ref *) x;
29791 dw_die_ref dy = *(const dw_die_ref *) y;
29792 unsigned int byte_size1, byte_size2;
29793 unsigned int encoding1, encoding2;
29794 unsigned int align1, align2;
29795 if (dx->die_mark > dy->die_mark)
29796 return -1;
29797 if (dx->die_mark < dy->die_mark)
29798 return 1;
29799 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
29800 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
29801 if (byte_size1 < byte_size2)
29802 return 1;
29803 if (byte_size1 > byte_size2)
29804 return -1;
29805 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
29806 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
29807 if (encoding1 < encoding2)
29808 return 1;
29809 if (encoding1 > encoding2)
29810 return -1;
29811 align1 = get_AT_unsigned (dx, DW_AT_alignment);
29812 align2 = get_AT_unsigned (dy, DW_AT_alignment);
29813 if (align1 < align2)
29814 return 1;
29815 if (align1 > align2)
29816 return -1;
29817 return 0;
29820 /* Move base types marked by mark_base_types as early as possible
29821 in the CU, sorted by decreasing usage count both to make the
29822 uleb128 references as small as possible and to make sure they
29823 will have die_offset already computed by calc_die_sizes when
29824 sizes of typed stack loc ops is computed. */
29826 static void
29827 move_marked_base_types (void)
29829 unsigned int i;
29830 dw_die_ref base_type, die, c;
29832 if (base_types.is_empty ())
29833 return;
29835 /* Sort by decreasing usage count, they will be added again in that
29836 order later on. */
29837 base_types.qsort (base_type_cmp);
29838 die = comp_unit_die ();
29839 c = die->die_child;
29842 dw_die_ref prev = c;
29843 c = c->die_sib;
29844 while (c->die_mark)
29846 remove_child_with_prev (c, prev);
29847 /* As base types got marked, there must be at least
29848 one node other than DW_TAG_base_type. */
29849 gcc_assert (die->die_child != NULL);
29850 c = prev->die_sib;
29853 while (c != die->die_child);
29854 gcc_assert (die->die_child);
29855 c = die->die_child;
29856 for (i = 0; base_types.iterate (i, &base_type); i++)
29858 base_type->die_mark = 0;
29859 base_type->die_sib = c->die_sib;
29860 c->die_sib = base_type;
29861 c = base_type;
29865 /* Helper function for resolve_addr, attempt to resolve
29866 one CONST_STRING, return true if successful. Similarly verify that
29867 SYMBOL_REFs refer to variables emitted in the current CU. */
29869 static bool
29870 resolve_one_addr (rtx *addr)
29872 rtx rtl = *addr;
29874 if (GET_CODE (rtl) == CONST_STRING)
29876 size_t len = strlen (XSTR (rtl, 0)) + 1;
29877 tree t = build_string (len, XSTR (rtl, 0));
29878 tree tlen = size_int (len - 1);
29879 TREE_TYPE (t)
29880 = build_array_type (char_type_node, build_index_type (tlen));
29881 rtl = lookup_constant_def (t);
29882 if (!rtl || !MEM_P (rtl))
29883 return false;
29884 rtl = XEXP (rtl, 0);
29885 if (GET_CODE (rtl) == SYMBOL_REF
29886 && SYMBOL_REF_DECL (rtl)
29887 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29888 return false;
29889 vec_safe_push (used_rtx_array, rtl);
29890 *addr = rtl;
29891 return true;
29894 if (GET_CODE (rtl) == SYMBOL_REF
29895 && SYMBOL_REF_DECL (rtl))
29897 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
29899 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
29900 return false;
29902 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29903 return false;
29906 if (GET_CODE (rtl) == CONST)
29908 subrtx_ptr_iterator::array_type array;
29909 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
29910 if (!resolve_one_addr (*iter))
29911 return false;
29914 return true;
29917 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
29918 if possible, and create DW_TAG_dwarf_procedure that can be referenced
29919 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
29921 static rtx
29922 string_cst_pool_decl (tree t)
29924 rtx rtl = output_constant_def (t, 1);
29925 unsigned char *array;
29926 dw_loc_descr_ref l;
29927 tree decl;
29928 size_t len;
29929 dw_die_ref ref;
29931 if (!rtl || !MEM_P (rtl))
29932 return NULL_RTX;
29933 rtl = XEXP (rtl, 0);
29934 if (GET_CODE (rtl) != SYMBOL_REF
29935 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
29936 return NULL_RTX;
29938 decl = SYMBOL_REF_DECL (rtl);
29939 if (!lookup_decl_die (decl))
29941 len = TREE_STRING_LENGTH (t);
29942 vec_safe_push (used_rtx_array, rtl);
29943 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
29944 array = ggc_vec_alloc<unsigned char> (len);
29945 memcpy (array, TREE_STRING_POINTER (t), len);
29946 l = new_loc_descr (DW_OP_implicit_value, len, 0);
29947 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
29948 l->dw_loc_oprnd2.v.val_vec.length = len;
29949 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
29950 l->dw_loc_oprnd2.v.val_vec.array = array;
29951 add_AT_loc (ref, DW_AT_location, l);
29952 equate_decl_number_to_die (decl, ref);
29954 return rtl;
29957 /* Helper function of resolve_addr_in_expr. LOC is
29958 a DW_OP_addr followed by DW_OP_stack_value, either at the start
29959 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
29960 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
29961 with DW_OP_implicit_pointer if possible
29962 and return true, if unsuccessful, return false. */
29964 static bool
29965 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
29967 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
29968 HOST_WIDE_INT offset = 0;
29969 dw_die_ref ref = NULL;
29970 tree decl;
29972 if (GET_CODE (rtl) == CONST
29973 && GET_CODE (XEXP (rtl, 0)) == PLUS
29974 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
29976 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
29977 rtl = XEXP (XEXP (rtl, 0), 0);
29979 if (GET_CODE (rtl) == CONST_STRING)
29981 size_t len = strlen (XSTR (rtl, 0)) + 1;
29982 tree t = build_string (len, XSTR (rtl, 0));
29983 tree tlen = size_int (len - 1);
29985 TREE_TYPE (t)
29986 = build_array_type (char_type_node, build_index_type (tlen));
29987 rtl = string_cst_pool_decl (t);
29988 if (!rtl)
29989 return false;
29991 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
29993 decl = SYMBOL_REF_DECL (rtl);
29994 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
29996 ref = lookup_decl_die (decl);
29997 if (ref && (get_AT (ref, DW_AT_location)
29998 || get_AT (ref, DW_AT_const_value)))
30000 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30001 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30002 loc->dw_loc_oprnd1.val_entry = NULL;
30003 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30004 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30005 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30006 loc->dw_loc_oprnd2.v.val_int = offset;
30007 return true;
30011 return false;
30014 /* Helper function for resolve_addr, handle one location
30015 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30016 the location list couldn't be resolved. */
30018 static bool
30019 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30021 dw_loc_descr_ref keep = NULL;
30022 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30023 switch (loc->dw_loc_opc)
30025 case DW_OP_addr:
30026 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30028 if ((prev == NULL
30029 || prev->dw_loc_opc == DW_OP_piece
30030 || prev->dw_loc_opc == DW_OP_bit_piece)
30031 && loc->dw_loc_next
30032 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30033 && (!dwarf_strict || dwarf_version >= 5)
30034 && optimize_one_addr_into_implicit_ptr (loc))
30035 break;
30036 return false;
30038 break;
30039 case DW_OP_GNU_addr_index:
30040 case DW_OP_addrx:
30041 case DW_OP_GNU_const_index:
30042 case DW_OP_constx:
30043 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30044 || loc->dw_loc_opc == DW_OP_addrx)
30045 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30046 || loc->dw_loc_opc == DW_OP_constx)
30047 && loc->dtprel))
30049 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30050 if (!resolve_one_addr (&rtl))
30051 return false;
30052 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30053 loc->dw_loc_oprnd1.val_entry
30054 = add_addr_table_entry (rtl, ate_kind_rtx);
30056 break;
30057 case DW_OP_const4u:
30058 case DW_OP_const8u:
30059 if (loc->dtprel
30060 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30061 return false;
30062 break;
30063 case DW_OP_plus_uconst:
30064 if (size_of_loc_descr (loc)
30065 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30067 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30069 dw_loc_descr_ref repl
30070 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30071 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30072 add_loc_descr (&repl, loc->dw_loc_next);
30073 *loc = *repl;
30075 break;
30076 case DW_OP_implicit_value:
30077 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30078 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30079 return false;
30080 break;
30081 case DW_OP_implicit_pointer:
30082 case DW_OP_GNU_implicit_pointer:
30083 case DW_OP_GNU_parameter_ref:
30084 case DW_OP_GNU_variable_value:
30085 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30087 dw_die_ref ref
30088 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30089 if (ref == NULL)
30090 return false;
30091 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30092 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30093 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30095 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30097 if (prev == NULL
30098 && loc->dw_loc_next == NULL
30099 && AT_class (a) == dw_val_class_loc)
30100 switch (a->dw_attr)
30102 /* Following attributes allow both exprloc and reference,
30103 so if the whole expression is DW_OP_GNU_variable_value
30104 alone we could transform it into reference. */
30105 case DW_AT_byte_size:
30106 case DW_AT_bit_size:
30107 case DW_AT_lower_bound:
30108 case DW_AT_upper_bound:
30109 case DW_AT_bit_stride:
30110 case DW_AT_count:
30111 case DW_AT_allocated:
30112 case DW_AT_associated:
30113 case DW_AT_byte_stride:
30114 a->dw_attr_val.val_class = dw_val_class_die_ref;
30115 a->dw_attr_val.val_entry = NULL;
30116 a->dw_attr_val.v.val_die_ref.die
30117 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30118 a->dw_attr_val.v.val_die_ref.external = 0;
30119 return true;
30120 default:
30121 break;
30123 if (dwarf_strict)
30124 return false;
30126 break;
30127 case DW_OP_const_type:
30128 case DW_OP_regval_type:
30129 case DW_OP_deref_type:
30130 case DW_OP_convert:
30131 case DW_OP_reinterpret:
30132 case DW_OP_GNU_const_type:
30133 case DW_OP_GNU_regval_type:
30134 case DW_OP_GNU_deref_type:
30135 case DW_OP_GNU_convert:
30136 case DW_OP_GNU_reinterpret:
30137 while (loc->dw_loc_next
30138 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30139 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30141 dw_die_ref base1, base2;
30142 unsigned enc1, enc2, size1, size2;
30143 if (loc->dw_loc_opc == DW_OP_regval_type
30144 || loc->dw_loc_opc == DW_OP_deref_type
30145 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30146 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30147 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30148 else if (loc->dw_loc_oprnd1.val_class
30149 == dw_val_class_unsigned_const)
30150 break;
30151 else
30152 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30153 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30154 == dw_val_class_unsigned_const)
30155 break;
30156 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30157 gcc_assert (base1->die_tag == DW_TAG_base_type
30158 && base2->die_tag == DW_TAG_base_type);
30159 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30160 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30161 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30162 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30163 if (size1 == size2
30164 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30165 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30166 && loc != keep)
30167 || enc1 == enc2))
30169 /* Optimize away next DW_OP_convert after
30170 adjusting LOC's base type die reference. */
30171 if (loc->dw_loc_opc == DW_OP_regval_type
30172 || loc->dw_loc_opc == DW_OP_deref_type
30173 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30174 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30175 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30176 else
30177 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30178 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30179 continue;
30181 /* Don't change integer DW_OP_convert after e.g. floating
30182 point typed stack entry. */
30183 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30184 keep = loc->dw_loc_next;
30185 break;
30187 break;
30188 default:
30189 break;
30191 return true;
30194 /* Helper function of resolve_addr. DIE had DW_AT_location of
30195 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30196 and DW_OP_addr couldn't be resolved. resolve_addr has already
30197 removed the DW_AT_location attribute. This function attempts to
30198 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30199 to it or DW_AT_const_value attribute, if possible. */
30201 static void
30202 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30204 if (!VAR_P (decl)
30205 || lookup_decl_die (decl) != die
30206 || DECL_EXTERNAL (decl)
30207 || !TREE_STATIC (decl)
30208 || DECL_INITIAL (decl) == NULL_TREE
30209 || DECL_P (DECL_INITIAL (decl))
30210 || get_AT (die, DW_AT_const_value))
30211 return;
30213 tree init = DECL_INITIAL (decl);
30214 HOST_WIDE_INT offset = 0;
30215 /* For variables that have been optimized away and thus
30216 don't have a memory location, see if we can emit
30217 DW_AT_const_value instead. */
30218 if (tree_add_const_value_attribute (die, init))
30219 return;
30220 if (dwarf_strict && dwarf_version < 5)
30221 return;
30222 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30223 and ADDR_EXPR refers to a decl that has DW_AT_location or
30224 DW_AT_const_value (but isn't addressable, otherwise
30225 resolving the original DW_OP_addr wouldn't fail), see if
30226 we can add DW_OP_implicit_pointer. */
30227 STRIP_NOPS (init);
30228 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30229 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30231 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30232 init = TREE_OPERAND (init, 0);
30233 STRIP_NOPS (init);
30235 if (TREE_CODE (init) != ADDR_EXPR)
30236 return;
30237 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30238 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30239 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30240 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30241 && TREE_OPERAND (init, 0) != decl))
30243 dw_die_ref ref;
30244 dw_loc_descr_ref l;
30246 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30248 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30249 if (!rtl)
30250 return;
30251 decl = SYMBOL_REF_DECL (rtl);
30253 else
30254 decl = TREE_OPERAND (init, 0);
30255 ref = lookup_decl_die (decl);
30256 if (ref == NULL
30257 || (!get_AT (ref, DW_AT_location)
30258 && !get_AT (ref, DW_AT_const_value)))
30259 return;
30260 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30261 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30262 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30263 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30264 add_AT_loc (die, DW_AT_location, l);
30268 /* Return NULL if l is a DWARF expression, or first op that is not
30269 valid DWARF expression. */
30271 static dw_loc_descr_ref
30272 non_dwarf_expression (dw_loc_descr_ref l)
30274 while (l)
30276 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30277 return l;
30278 switch (l->dw_loc_opc)
30280 case DW_OP_regx:
30281 case DW_OP_implicit_value:
30282 case DW_OP_stack_value:
30283 case DW_OP_implicit_pointer:
30284 case DW_OP_GNU_implicit_pointer:
30285 case DW_OP_GNU_parameter_ref:
30286 case DW_OP_piece:
30287 case DW_OP_bit_piece:
30288 return l;
30289 default:
30290 break;
30292 l = l->dw_loc_next;
30294 return NULL;
30297 /* Return adjusted copy of EXPR:
30298 If it is empty DWARF expression, return it.
30299 If it is valid non-empty DWARF expression,
30300 return copy of EXPR with DW_OP_deref appended to it.
30301 If it is DWARF expression followed by DW_OP_reg{N,x}, return
30302 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30303 If it is DWARF expression followed by DW_OP_stack_value, return
30304 copy of the DWARF expression without anything appended.
30305 Otherwise, return NULL. */
30307 static dw_loc_descr_ref
30308 copy_deref_exprloc (dw_loc_descr_ref expr)
30310 dw_loc_descr_ref tail = NULL;
30312 if (expr == NULL)
30313 return NULL;
30315 dw_loc_descr_ref l = non_dwarf_expression (expr);
30316 if (l && l->dw_loc_next)
30317 return NULL;
30319 if (l)
30321 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30322 tail = new_loc_descr ((enum dwarf_location_atom)
30323 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30324 0, 0);
30325 else
30326 switch (l->dw_loc_opc)
30328 case DW_OP_regx:
30329 tail = new_loc_descr (DW_OP_bregx,
30330 l->dw_loc_oprnd1.v.val_unsigned, 0);
30331 break;
30332 case DW_OP_stack_value:
30333 break;
30334 default:
30335 return NULL;
30338 else
30339 tail = new_loc_descr (DW_OP_deref, 0, 0);
30341 dw_loc_descr_ref ret = NULL, *p = &ret;
30342 while (expr != l)
30344 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30345 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30346 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30347 p = &(*p)->dw_loc_next;
30348 expr = expr->dw_loc_next;
30350 *p = tail;
30351 return ret;
30354 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30355 reference to a variable or argument, adjust it if needed and return:
30356 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30357 attribute if present should be removed
30358 0 keep the attribute perhaps with minor modifications, no need to rescan
30359 1 if the attribute has been successfully adjusted. */
30361 static int
30362 optimize_string_length (dw_attr_node *a)
30364 dw_loc_descr_ref l = AT_loc (a), lv;
30365 dw_die_ref die;
30366 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30368 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30369 die = lookup_decl_die (decl);
30370 if (die)
30372 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30373 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30374 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30376 else
30377 return -1;
30379 else
30380 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30382 /* DWARF5 allows reference class, so we can then reference the DIE.
30383 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30384 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30386 a->dw_attr_val.val_class = dw_val_class_die_ref;
30387 a->dw_attr_val.val_entry = NULL;
30388 a->dw_attr_val.v.val_die_ref.die = die;
30389 a->dw_attr_val.v.val_die_ref.external = 0;
30390 return 0;
30393 dw_attr_node *av = get_AT (die, DW_AT_location);
30394 dw_loc_list_ref d;
30395 bool non_dwarf_expr = false;
30397 if (av == NULL)
30398 return dwarf_strict ? -1 : 0;
30399 switch (AT_class (av))
30401 case dw_val_class_loc_list:
30402 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30403 if (d->expr && non_dwarf_expression (d->expr))
30404 non_dwarf_expr = true;
30405 break;
30406 case dw_val_class_view_list:
30407 gcc_unreachable ();
30408 case dw_val_class_loc:
30409 lv = AT_loc (av);
30410 if (lv == NULL)
30411 return dwarf_strict ? -1 : 0;
30412 if (non_dwarf_expression (lv))
30413 non_dwarf_expr = true;
30414 break;
30415 default:
30416 return dwarf_strict ? -1 : 0;
30419 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30420 into DW_OP_call4 or DW_OP_GNU_variable_value into
30421 DW_OP_call4 DW_OP_deref, do so. */
30422 if (!non_dwarf_expr
30423 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30425 l->dw_loc_opc = DW_OP_call4;
30426 if (l->dw_loc_next)
30427 l->dw_loc_next = NULL;
30428 else
30429 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30430 return 0;
30433 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30434 copy over the DW_AT_location attribute from die to a. */
30435 if (l->dw_loc_next != NULL)
30437 a->dw_attr_val = av->dw_attr_val;
30438 return 1;
30441 dw_loc_list_ref list, *p;
30442 switch (AT_class (av))
30444 case dw_val_class_loc_list:
30445 p = &list;
30446 list = NULL;
30447 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30449 lv = copy_deref_exprloc (d->expr);
30450 if (lv)
30452 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
30453 p = &(*p)->dw_loc_next;
30455 else if (!dwarf_strict && d->expr)
30456 return 0;
30458 if (list == NULL)
30459 return dwarf_strict ? -1 : 0;
30460 a->dw_attr_val.val_class = dw_val_class_loc_list;
30461 gen_llsym (list);
30462 *AT_loc_list_ptr (a) = list;
30463 return 1;
30464 case dw_val_class_loc:
30465 lv = copy_deref_exprloc (AT_loc (av));
30466 if (lv == NULL)
30467 return dwarf_strict ? -1 : 0;
30468 a->dw_attr_val.v.val_loc = lv;
30469 return 1;
30470 default:
30471 gcc_unreachable ();
30475 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
30476 an address in .rodata section if the string literal is emitted there,
30477 or remove the containing location list or replace DW_AT_const_value
30478 with DW_AT_location and empty location expression, if it isn't found
30479 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
30480 to something that has been emitted in the current CU. */
30482 static void
30483 resolve_addr (dw_die_ref die)
30485 dw_die_ref c;
30486 dw_attr_node *a;
30487 dw_loc_list_ref *curr, *start, loc;
30488 unsigned ix;
30489 bool remove_AT_byte_size = false;
30491 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30492 switch (AT_class (a))
30494 case dw_val_class_loc_list:
30495 start = curr = AT_loc_list_ptr (a);
30496 loc = *curr;
30497 gcc_assert (loc);
30498 /* The same list can be referenced more than once. See if we have
30499 already recorded the result from a previous pass. */
30500 if (loc->replaced)
30501 *curr = loc->dw_loc_next;
30502 else if (!loc->resolved_addr)
30504 /* As things stand, we do not expect or allow one die to
30505 reference a suffix of another die's location list chain.
30506 References must be identical or completely separate.
30507 There is therefore no need to cache the result of this
30508 pass on any list other than the first; doing so
30509 would lead to unnecessary writes. */
30510 while (*curr)
30512 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
30513 if (!resolve_addr_in_expr (a, (*curr)->expr))
30515 dw_loc_list_ref next = (*curr)->dw_loc_next;
30516 dw_loc_descr_ref l = (*curr)->expr;
30518 if (next && (*curr)->ll_symbol)
30520 gcc_assert (!next->ll_symbol);
30521 next->ll_symbol = (*curr)->ll_symbol;
30522 next->vl_symbol = (*curr)->vl_symbol;
30524 if (dwarf_split_debug_info)
30525 remove_loc_list_addr_table_entries (l);
30526 *curr = next;
30528 else
30530 mark_base_types ((*curr)->expr);
30531 curr = &(*curr)->dw_loc_next;
30534 if (loc == *start)
30535 loc->resolved_addr = 1;
30536 else
30538 loc->replaced = 1;
30539 loc->dw_loc_next = *start;
30542 if (!*start)
30544 remove_AT (die, a->dw_attr);
30545 ix--;
30547 break;
30548 case dw_val_class_view_list:
30550 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30551 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
30552 dw_val_node *llnode
30553 = view_list_to_loc_list_val_node (&a->dw_attr_val);
30554 /* If we no longer have a loclist, or it no longer needs
30555 views, drop this attribute. */
30556 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
30558 remove_AT (die, a->dw_attr);
30559 ix--;
30561 break;
30563 case dw_val_class_loc:
30565 dw_loc_descr_ref l = AT_loc (a);
30566 /* DW_OP_GNU_variable_value DW_OP_stack_value or
30567 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
30568 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
30569 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
30570 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
30571 with DW_FORM_ref referencing the same DIE as
30572 DW_OP_GNU_variable_value used to reference. */
30573 if (a->dw_attr == DW_AT_string_length
30574 && l
30575 && l->dw_loc_opc == DW_OP_GNU_variable_value
30576 && (l->dw_loc_next == NULL
30577 || (l->dw_loc_next->dw_loc_next == NULL
30578 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
30580 switch (optimize_string_length (a))
30582 case -1:
30583 remove_AT (die, a->dw_attr);
30584 ix--;
30585 /* If we drop DW_AT_string_length, we need to drop also
30586 DW_AT_{string_length_,}byte_size. */
30587 remove_AT_byte_size = true;
30588 continue;
30589 default:
30590 break;
30591 case 1:
30592 /* Even if we keep the optimized DW_AT_string_length,
30593 it might have changed AT_class, so process it again. */
30594 ix--;
30595 continue;
30598 /* For -gdwarf-2 don't attempt to optimize
30599 DW_AT_data_member_location containing
30600 DW_OP_plus_uconst - older consumers might
30601 rely on it being that op instead of a more complex,
30602 but shorter, location description. */
30603 if ((dwarf_version > 2
30604 || a->dw_attr != DW_AT_data_member_location
30605 || l == NULL
30606 || l->dw_loc_opc != DW_OP_plus_uconst
30607 || l->dw_loc_next != NULL)
30608 && !resolve_addr_in_expr (a, l))
30610 if (dwarf_split_debug_info)
30611 remove_loc_list_addr_table_entries (l);
30612 if (l != NULL
30613 && l->dw_loc_next == NULL
30614 && l->dw_loc_opc == DW_OP_addr
30615 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
30616 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
30617 && a->dw_attr == DW_AT_location)
30619 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
30620 remove_AT (die, a->dw_attr);
30621 ix--;
30622 optimize_location_into_implicit_ptr (die, decl);
30623 break;
30625 if (a->dw_attr == DW_AT_string_length)
30626 /* If we drop DW_AT_string_length, we need to drop also
30627 DW_AT_{string_length_,}byte_size. */
30628 remove_AT_byte_size = true;
30629 remove_AT (die, a->dw_attr);
30630 ix--;
30632 else
30633 mark_base_types (l);
30635 break;
30636 case dw_val_class_addr:
30637 if (a->dw_attr == DW_AT_const_value
30638 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
30640 if (AT_index (a) != NOT_INDEXED)
30641 remove_addr_table_entry (a->dw_attr_val.val_entry);
30642 remove_AT (die, a->dw_attr);
30643 ix--;
30645 if ((die->die_tag == DW_TAG_call_site
30646 && a->dw_attr == DW_AT_call_origin)
30647 || (die->die_tag == DW_TAG_GNU_call_site
30648 && a->dw_attr == DW_AT_abstract_origin))
30650 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
30651 dw_die_ref tdie = lookup_decl_die (tdecl);
30652 dw_die_ref cdie;
30653 if (tdie == NULL
30654 && DECL_EXTERNAL (tdecl)
30655 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
30656 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
30658 dw_die_ref pdie = cdie;
30659 /* Make sure we don't add these DIEs into type units.
30660 We could emit skeleton DIEs for context (namespaces,
30661 outer structs/classes) and a skeleton DIE for the
30662 innermost context with DW_AT_signature pointing to the
30663 type unit. See PR78835. */
30664 while (pdie && pdie->die_tag != DW_TAG_type_unit)
30665 pdie = pdie->die_parent;
30666 if (pdie == NULL)
30668 /* Creating a full DIE for tdecl is overly expensive and
30669 at this point even wrong when in the LTO phase
30670 as it can end up generating new type DIEs we didn't
30671 output and thus optimize_external_refs will crash. */
30672 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
30673 add_AT_flag (tdie, DW_AT_external, 1);
30674 add_AT_flag (tdie, DW_AT_declaration, 1);
30675 add_linkage_attr (tdie, tdecl);
30676 add_name_and_src_coords_attributes (tdie, tdecl, true);
30677 equate_decl_number_to_die (tdecl, tdie);
30680 if (tdie)
30682 a->dw_attr_val.val_class = dw_val_class_die_ref;
30683 a->dw_attr_val.v.val_die_ref.die = tdie;
30684 a->dw_attr_val.v.val_die_ref.external = 0;
30686 else
30688 if (AT_index (a) != NOT_INDEXED)
30689 remove_addr_table_entry (a->dw_attr_val.val_entry);
30690 remove_AT (die, a->dw_attr);
30691 ix--;
30694 break;
30695 default:
30696 break;
30699 if (remove_AT_byte_size)
30700 remove_AT (die, dwarf_version >= 5
30701 ? DW_AT_string_length_byte_size
30702 : DW_AT_byte_size);
30704 FOR_EACH_CHILD (die, c, resolve_addr (c));
30707 /* Helper routines for optimize_location_lists.
30708 This pass tries to share identical local lists in .debug_loc
30709 section. */
30711 /* Iteratively hash operands of LOC opcode into HSTATE. */
30713 static void
30714 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
30716 dw_val_ref val1 = &loc->dw_loc_oprnd1;
30717 dw_val_ref val2 = &loc->dw_loc_oprnd2;
30719 switch (loc->dw_loc_opc)
30721 case DW_OP_const4u:
30722 case DW_OP_const8u:
30723 if (loc->dtprel)
30724 goto hash_addr;
30725 /* FALLTHRU */
30726 case DW_OP_const1u:
30727 case DW_OP_const1s:
30728 case DW_OP_const2u:
30729 case DW_OP_const2s:
30730 case DW_OP_const4s:
30731 case DW_OP_const8s:
30732 case DW_OP_constu:
30733 case DW_OP_consts:
30734 case DW_OP_pick:
30735 case DW_OP_plus_uconst:
30736 case DW_OP_breg0:
30737 case DW_OP_breg1:
30738 case DW_OP_breg2:
30739 case DW_OP_breg3:
30740 case DW_OP_breg4:
30741 case DW_OP_breg5:
30742 case DW_OP_breg6:
30743 case DW_OP_breg7:
30744 case DW_OP_breg8:
30745 case DW_OP_breg9:
30746 case DW_OP_breg10:
30747 case DW_OP_breg11:
30748 case DW_OP_breg12:
30749 case DW_OP_breg13:
30750 case DW_OP_breg14:
30751 case DW_OP_breg15:
30752 case DW_OP_breg16:
30753 case DW_OP_breg17:
30754 case DW_OP_breg18:
30755 case DW_OP_breg19:
30756 case DW_OP_breg20:
30757 case DW_OP_breg21:
30758 case DW_OP_breg22:
30759 case DW_OP_breg23:
30760 case DW_OP_breg24:
30761 case DW_OP_breg25:
30762 case DW_OP_breg26:
30763 case DW_OP_breg27:
30764 case DW_OP_breg28:
30765 case DW_OP_breg29:
30766 case DW_OP_breg30:
30767 case DW_OP_breg31:
30768 case DW_OP_regx:
30769 case DW_OP_fbreg:
30770 case DW_OP_piece:
30771 case DW_OP_deref_size:
30772 case DW_OP_xderef_size:
30773 hstate.add_object (val1->v.val_int);
30774 break;
30775 case DW_OP_skip:
30776 case DW_OP_bra:
30778 int offset;
30780 gcc_assert (val1->val_class == dw_val_class_loc);
30781 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
30782 hstate.add_object (offset);
30784 break;
30785 case DW_OP_implicit_value:
30786 hstate.add_object (val1->v.val_unsigned);
30787 switch (val2->val_class)
30789 case dw_val_class_const:
30790 hstate.add_object (val2->v.val_int);
30791 break;
30792 case dw_val_class_vec:
30794 unsigned int elt_size = val2->v.val_vec.elt_size;
30795 unsigned int len = val2->v.val_vec.length;
30797 hstate.add_int (elt_size);
30798 hstate.add_int (len);
30799 hstate.add (val2->v.val_vec.array, len * elt_size);
30801 break;
30802 case dw_val_class_const_double:
30803 hstate.add_object (val2->v.val_double.low);
30804 hstate.add_object (val2->v.val_double.high);
30805 break;
30806 case dw_val_class_wide_int:
30807 hstate.add (val2->v.val_wide->get_val (),
30808 get_full_len (*val2->v.val_wide)
30809 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30810 break;
30811 case dw_val_class_addr:
30812 inchash::add_rtx (val2->v.val_addr, hstate);
30813 break;
30814 default:
30815 gcc_unreachable ();
30817 break;
30818 case DW_OP_bregx:
30819 case DW_OP_bit_piece:
30820 hstate.add_object (val1->v.val_int);
30821 hstate.add_object (val2->v.val_int);
30822 break;
30823 case DW_OP_addr:
30824 hash_addr:
30825 if (loc->dtprel)
30827 unsigned char dtprel = 0xd1;
30828 hstate.add_object (dtprel);
30830 inchash::add_rtx (val1->v.val_addr, hstate);
30831 break;
30832 case DW_OP_GNU_addr_index:
30833 case DW_OP_addrx:
30834 case DW_OP_GNU_const_index:
30835 case DW_OP_constx:
30837 if (loc->dtprel)
30839 unsigned char dtprel = 0xd1;
30840 hstate.add_object (dtprel);
30842 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
30844 break;
30845 case DW_OP_implicit_pointer:
30846 case DW_OP_GNU_implicit_pointer:
30847 hstate.add_int (val2->v.val_int);
30848 break;
30849 case DW_OP_entry_value:
30850 case DW_OP_GNU_entry_value:
30851 hstate.add_object (val1->v.val_loc);
30852 break;
30853 case DW_OP_regval_type:
30854 case DW_OP_deref_type:
30855 case DW_OP_GNU_regval_type:
30856 case DW_OP_GNU_deref_type:
30858 unsigned int byte_size
30859 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
30860 unsigned int encoding
30861 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
30862 hstate.add_object (val1->v.val_int);
30863 hstate.add_object (byte_size);
30864 hstate.add_object (encoding);
30866 break;
30867 case DW_OP_convert:
30868 case DW_OP_reinterpret:
30869 case DW_OP_GNU_convert:
30870 case DW_OP_GNU_reinterpret:
30871 if (val1->val_class == dw_val_class_unsigned_const)
30873 hstate.add_object (val1->v.val_unsigned);
30874 break;
30876 /* FALLTHRU */
30877 case DW_OP_const_type:
30878 case DW_OP_GNU_const_type:
30880 unsigned int byte_size
30881 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
30882 unsigned int encoding
30883 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
30884 hstate.add_object (byte_size);
30885 hstate.add_object (encoding);
30886 if (loc->dw_loc_opc != DW_OP_const_type
30887 && loc->dw_loc_opc != DW_OP_GNU_const_type)
30888 break;
30889 hstate.add_object (val2->val_class);
30890 switch (val2->val_class)
30892 case dw_val_class_const:
30893 hstate.add_object (val2->v.val_int);
30894 break;
30895 case dw_val_class_vec:
30897 unsigned int elt_size = val2->v.val_vec.elt_size;
30898 unsigned int len = val2->v.val_vec.length;
30900 hstate.add_object (elt_size);
30901 hstate.add_object (len);
30902 hstate.add (val2->v.val_vec.array, len * elt_size);
30904 break;
30905 case dw_val_class_const_double:
30906 hstate.add_object (val2->v.val_double.low);
30907 hstate.add_object (val2->v.val_double.high);
30908 break;
30909 case dw_val_class_wide_int:
30910 hstate.add (val2->v.val_wide->get_val (),
30911 get_full_len (*val2->v.val_wide)
30912 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30913 break;
30914 default:
30915 gcc_unreachable ();
30918 break;
30920 default:
30921 /* Other codes have no operands. */
30922 break;
30926 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
30928 static inline void
30929 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
30931 dw_loc_descr_ref l;
30932 bool sizes_computed = false;
30933 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
30934 size_of_locs (loc);
30936 for (l = loc; l != NULL; l = l->dw_loc_next)
30938 enum dwarf_location_atom opc = l->dw_loc_opc;
30939 hstate.add_object (opc);
30940 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
30942 size_of_locs (loc);
30943 sizes_computed = true;
30945 hash_loc_operands (l, hstate);
30949 /* Compute hash of the whole location list LIST_HEAD. */
30951 static inline void
30952 hash_loc_list (dw_loc_list_ref list_head)
30954 dw_loc_list_ref curr = list_head;
30955 inchash::hash hstate;
30957 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
30959 hstate.add (curr->begin, strlen (curr->begin) + 1);
30960 hstate.add (curr->end, strlen (curr->end) + 1);
30961 hstate.add_object (curr->vbegin);
30962 hstate.add_object (curr->vend);
30963 if (curr->section)
30964 hstate.add (curr->section, strlen (curr->section) + 1);
30965 hash_locs (curr->expr, hstate);
30967 list_head->hash = hstate.end ();
30970 /* Return true if X and Y opcodes have the same operands. */
30972 static inline bool
30973 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
30975 dw_val_ref valx1 = &x->dw_loc_oprnd1;
30976 dw_val_ref valx2 = &x->dw_loc_oprnd2;
30977 dw_val_ref valy1 = &y->dw_loc_oprnd1;
30978 dw_val_ref valy2 = &y->dw_loc_oprnd2;
30980 switch (x->dw_loc_opc)
30982 case DW_OP_const4u:
30983 case DW_OP_const8u:
30984 if (x->dtprel)
30985 goto hash_addr;
30986 /* FALLTHRU */
30987 case DW_OP_const1u:
30988 case DW_OP_const1s:
30989 case DW_OP_const2u:
30990 case DW_OP_const2s:
30991 case DW_OP_const4s:
30992 case DW_OP_const8s:
30993 case DW_OP_constu:
30994 case DW_OP_consts:
30995 case DW_OP_pick:
30996 case DW_OP_plus_uconst:
30997 case DW_OP_breg0:
30998 case DW_OP_breg1:
30999 case DW_OP_breg2:
31000 case DW_OP_breg3:
31001 case DW_OP_breg4:
31002 case DW_OP_breg5:
31003 case DW_OP_breg6:
31004 case DW_OP_breg7:
31005 case DW_OP_breg8:
31006 case DW_OP_breg9:
31007 case DW_OP_breg10:
31008 case DW_OP_breg11:
31009 case DW_OP_breg12:
31010 case DW_OP_breg13:
31011 case DW_OP_breg14:
31012 case DW_OP_breg15:
31013 case DW_OP_breg16:
31014 case DW_OP_breg17:
31015 case DW_OP_breg18:
31016 case DW_OP_breg19:
31017 case DW_OP_breg20:
31018 case DW_OP_breg21:
31019 case DW_OP_breg22:
31020 case DW_OP_breg23:
31021 case DW_OP_breg24:
31022 case DW_OP_breg25:
31023 case DW_OP_breg26:
31024 case DW_OP_breg27:
31025 case DW_OP_breg28:
31026 case DW_OP_breg29:
31027 case DW_OP_breg30:
31028 case DW_OP_breg31:
31029 case DW_OP_regx:
31030 case DW_OP_fbreg:
31031 case DW_OP_piece:
31032 case DW_OP_deref_size:
31033 case DW_OP_xderef_size:
31034 return valx1->v.val_int == valy1->v.val_int;
31035 case DW_OP_skip:
31036 case DW_OP_bra:
31037 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31038 can cause irrelevant differences in dw_loc_addr. */
31039 gcc_assert (valx1->val_class == dw_val_class_loc
31040 && valy1->val_class == dw_val_class_loc
31041 && (dwarf_split_debug_info
31042 || x->dw_loc_addr == y->dw_loc_addr));
31043 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31044 case DW_OP_implicit_value:
31045 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31046 || valx2->val_class != valy2->val_class)
31047 return false;
31048 switch (valx2->val_class)
31050 case dw_val_class_const:
31051 return valx2->v.val_int == valy2->v.val_int;
31052 case dw_val_class_vec:
31053 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31054 && valx2->v.val_vec.length == valy2->v.val_vec.length
31055 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31056 valx2->v.val_vec.elt_size
31057 * valx2->v.val_vec.length) == 0;
31058 case dw_val_class_const_double:
31059 return valx2->v.val_double.low == valy2->v.val_double.low
31060 && valx2->v.val_double.high == valy2->v.val_double.high;
31061 case dw_val_class_wide_int:
31062 return *valx2->v.val_wide == *valy2->v.val_wide;
31063 case dw_val_class_addr:
31064 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31065 default:
31066 gcc_unreachable ();
31068 case DW_OP_bregx:
31069 case DW_OP_bit_piece:
31070 return valx1->v.val_int == valy1->v.val_int
31071 && valx2->v.val_int == valy2->v.val_int;
31072 case DW_OP_addr:
31073 hash_addr:
31074 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31075 case DW_OP_GNU_addr_index:
31076 case DW_OP_addrx:
31077 case DW_OP_GNU_const_index:
31078 case DW_OP_constx:
31080 rtx ax1 = valx1->val_entry->addr.rtl;
31081 rtx ay1 = valy1->val_entry->addr.rtl;
31082 return rtx_equal_p (ax1, ay1);
31084 case DW_OP_implicit_pointer:
31085 case DW_OP_GNU_implicit_pointer:
31086 return valx1->val_class == dw_val_class_die_ref
31087 && valx1->val_class == valy1->val_class
31088 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31089 && valx2->v.val_int == valy2->v.val_int;
31090 case DW_OP_entry_value:
31091 case DW_OP_GNU_entry_value:
31092 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31093 case DW_OP_const_type:
31094 case DW_OP_GNU_const_type:
31095 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31096 || valx2->val_class != valy2->val_class)
31097 return false;
31098 switch (valx2->val_class)
31100 case dw_val_class_const:
31101 return valx2->v.val_int == valy2->v.val_int;
31102 case dw_val_class_vec:
31103 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31104 && valx2->v.val_vec.length == valy2->v.val_vec.length
31105 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31106 valx2->v.val_vec.elt_size
31107 * valx2->v.val_vec.length) == 0;
31108 case dw_val_class_const_double:
31109 return valx2->v.val_double.low == valy2->v.val_double.low
31110 && valx2->v.val_double.high == valy2->v.val_double.high;
31111 case dw_val_class_wide_int:
31112 return *valx2->v.val_wide == *valy2->v.val_wide;
31113 default:
31114 gcc_unreachable ();
31116 case DW_OP_regval_type:
31117 case DW_OP_deref_type:
31118 case DW_OP_GNU_regval_type:
31119 case DW_OP_GNU_deref_type:
31120 return valx1->v.val_int == valy1->v.val_int
31121 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31122 case DW_OP_convert:
31123 case DW_OP_reinterpret:
31124 case DW_OP_GNU_convert:
31125 case DW_OP_GNU_reinterpret:
31126 if (valx1->val_class != valy1->val_class)
31127 return false;
31128 if (valx1->val_class == dw_val_class_unsigned_const)
31129 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31130 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31131 case DW_OP_GNU_parameter_ref:
31132 return valx1->val_class == dw_val_class_die_ref
31133 && valx1->val_class == valy1->val_class
31134 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31135 default:
31136 /* Other codes have no operands. */
31137 return true;
31141 /* Return true if DWARF location expressions X and Y are the same. */
31143 static inline bool
31144 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31146 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31147 if (x->dw_loc_opc != y->dw_loc_opc
31148 || x->dtprel != y->dtprel
31149 || !compare_loc_operands (x, y))
31150 break;
31151 return x == NULL && y == NULL;
31154 /* Hashtable helpers. */
31156 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31158 static inline hashval_t hash (const dw_loc_list_struct *);
31159 static inline bool equal (const dw_loc_list_struct *,
31160 const dw_loc_list_struct *);
31163 /* Return precomputed hash of location list X. */
31165 inline hashval_t
31166 loc_list_hasher::hash (const dw_loc_list_struct *x)
31168 return x->hash;
31171 /* Return true if location lists A and B are the same. */
31173 inline bool
31174 loc_list_hasher::equal (const dw_loc_list_struct *a,
31175 const dw_loc_list_struct *b)
31177 if (a == b)
31178 return 1;
31179 if (a->hash != b->hash)
31180 return 0;
31181 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31182 if (strcmp (a->begin, b->begin) != 0
31183 || strcmp (a->end, b->end) != 0
31184 || (a->section == NULL) != (b->section == NULL)
31185 || (a->section && strcmp (a->section, b->section) != 0)
31186 || a->vbegin != b->vbegin || a->vend != b->vend
31187 || !compare_locs (a->expr, b->expr))
31188 break;
31189 return a == NULL && b == NULL;
31192 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31195 /* Recursively optimize location lists referenced from DIE
31196 children and share them whenever possible. */
31198 static void
31199 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31201 dw_die_ref c;
31202 dw_attr_node *a;
31203 unsigned ix;
31204 dw_loc_list_struct **slot;
31205 bool drop_locviews = false;
31206 bool has_locviews = false;
31208 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31209 if (AT_class (a) == dw_val_class_loc_list)
31211 dw_loc_list_ref list = AT_loc_list (a);
31212 /* TODO: perform some optimizations here, before hashing
31213 it and storing into the hash table. */
31214 hash_loc_list (list);
31215 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31216 if (*slot == NULL)
31218 *slot = list;
31219 if (loc_list_has_views (list))
31220 gcc_assert (list->vl_symbol);
31221 else if (list->vl_symbol)
31223 drop_locviews = true;
31224 list->vl_symbol = NULL;
31227 else
31229 if (list->vl_symbol && !(*slot)->vl_symbol)
31230 drop_locviews = true;
31231 a->dw_attr_val.v.val_loc_list = *slot;
31234 else if (AT_class (a) == dw_val_class_view_list)
31236 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31237 has_locviews = true;
31241 if (drop_locviews && has_locviews)
31242 remove_AT (die, DW_AT_GNU_locviews);
31244 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31248 /* Recursively assign each location list a unique index into the debug_addr
31249 section. */
31251 static void
31252 index_location_lists (dw_die_ref die)
31254 dw_die_ref c;
31255 dw_attr_node *a;
31256 unsigned ix;
31258 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31259 if (AT_class (a) == dw_val_class_loc_list)
31261 dw_loc_list_ref list = AT_loc_list (a);
31262 dw_loc_list_ref curr;
31263 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31265 /* Don't index an entry that has already been indexed
31266 or won't be output. Make sure skip_loc_list_entry doesn't
31267 call size_of_locs, because that might cause circular dependency,
31268 index_location_lists requiring address table indexes to be
31269 computed, but adding new indexes through add_addr_table_entry
31270 and address table index computation requiring no new additions
31271 to the hash table. In the rare case of DWARF[234] >= 64KB
31272 location expression, we'll just waste unused address table entry
31273 for it. */
31274 if (curr->begin_entry != NULL
31275 || skip_loc_list_entry (curr))
31276 continue;
31278 curr->begin_entry
31279 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31283 FOR_EACH_CHILD (die, c, index_location_lists (c));
31286 /* Optimize location lists referenced from DIE
31287 children and share them whenever possible. */
31289 static void
31290 optimize_location_lists (dw_die_ref die)
31292 loc_list_hash_type htab (500);
31293 optimize_location_lists_1 (die, &htab);
31296 /* Traverse the limbo die list, and add parent/child links. The only
31297 dies without parents that should be here are concrete instances of
31298 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
31299 For concrete instances, we can get the parent die from the abstract
31300 instance. */
31302 static void
31303 flush_limbo_die_list (void)
31305 limbo_die_node *node;
31307 /* get_context_die calls force_decl_die, which can put new DIEs on the
31308 limbo list in LTO mode when nested functions are put in a different
31309 partition than that of their parent function. */
31310 while ((node = limbo_die_list))
31312 dw_die_ref die = node->die;
31313 limbo_die_list = node->next;
31315 if (die->die_parent == NULL)
31317 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31319 if (origin && origin->die_parent)
31320 add_child_die (origin->die_parent, die);
31321 else if (is_cu_die (die))
31323 else if (seen_error ())
31324 /* It's OK to be confused by errors in the input. */
31325 add_child_die (comp_unit_die (), die);
31326 else
31328 /* In certain situations, the lexical block containing a
31329 nested function can be optimized away, which results
31330 in the nested function die being orphaned. Likewise
31331 with the return type of that nested function. Force
31332 this to be a child of the containing function.
31334 It may happen that even the containing function got fully
31335 inlined and optimized out. In that case we are lost and
31336 assign the empty child. This should not be big issue as
31337 the function is likely unreachable too. */
31338 gcc_assert (node->created_for);
31340 if (DECL_P (node->created_for))
31341 origin = get_context_die (DECL_CONTEXT (node->created_for));
31342 else if (TYPE_P (node->created_for))
31343 origin = scope_die_for (node->created_for, comp_unit_die ());
31344 else
31345 origin = comp_unit_die ();
31347 add_child_die (origin, die);
31353 /* Reset DIEs so we can output them again. */
31355 static void
31356 reset_dies (dw_die_ref die)
31358 dw_die_ref c;
31360 /* Remove stuff we re-generate. */
31361 die->die_mark = 0;
31362 die->die_offset = 0;
31363 die->die_abbrev = 0;
31364 remove_AT (die, DW_AT_sibling);
31366 FOR_EACH_CHILD (die, c, reset_dies (c));
31369 /* Output stuff that dwarf requires at the end of every file,
31370 and generate the DWARF-2 debugging info. */
31372 static void
31373 dwarf2out_finish (const char *filename)
31375 comdat_type_node *ctnode;
31376 dw_die_ref main_comp_unit_die;
31377 unsigned char checksum[16];
31378 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31380 /* Flush out any latecomers to the limbo party. */
31381 flush_limbo_die_list ();
31383 if (inline_entry_data_table)
31384 gcc_assert (inline_entry_data_table->is_empty ());
31386 if (flag_checking)
31388 verify_die (comp_unit_die ());
31389 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31390 verify_die (node->die);
31393 /* We shouldn't have any symbols with delayed asm names for
31394 DIEs generated after early finish. */
31395 gcc_assert (deferred_asm_name == NULL);
31397 gen_remaining_tmpl_value_param_die_attribute ();
31399 if (flag_generate_lto || flag_generate_offload)
31401 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31403 /* Prune stuff so that dwarf2out_finish runs successfully
31404 for the fat part of the object. */
31405 reset_dies (comp_unit_die ());
31406 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31407 reset_dies (node->die);
31409 hash_table<comdat_type_hasher> comdat_type_table (100);
31410 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31412 comdat_type_node **slot
31413 = comdat_type_table.find_slot (ctnode, INSERT);
31415 /* Don't reset types twice. */
31416 if (*slot != HTAB_EMPTY_ENTRY)
31417 continue;
31419 /* Remove the pointer to the line table. */
31420 remove_AT (ctnode->root_die, DW_AT_stmt_list);
31422 if (debug_info_level >= DINFO_LEVEL_TERSE)
31423 reset_dies (ctnode->root_die);
31425 *slot = ctnode;
31428 /* Reset die CU symbol so we don't output it twice. */
31429 comp_unit_die ()->die_id.die_symbol = NULL;
31431 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
31432 remove_AT (comp_unit_die (), DW_AT_stmt_list);
31433 if (have_macinfo)
31434 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
31436 /* Remove indirect string decisions. */
31437 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
31438 if (debug_line_str_hash)
31440 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
31441 debug_line_str_hash = NULL;
31445 #if ENABLE_ASSERT_CHECKING
31447 dw_die_ref die = comp_unit_die (), c;
31448 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
31450 #endif
31451 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31452 resolve_addr (ctnode->root_die);
31453 resolve_addr (comp_unit_die ());
31454 move_marked_base_types ();
31456 if (dump_file)
31458 fprintf (dump_file, "DWARF for %s\n", filename);
31459 print_die (comp_unit_die (), dump_file);
31462 /* Initialize sections and labels used for actual assembler output. */
31463 unsigned generation = init_sections_and_labels (false);
31465 /* Traverse the DIE's and add sibling attributes to those DIE's that
31466 have children. */
31467 add_sibling_attributes (comp_unit_die ());
31468 limbo_die_node *node;
31469 for (node = cu_die_list; node; node = node->next)
31470 add_sibling_attributes (node->die);
31471 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31472 add_sibling_attributes (ctnode->root_die);
31474 /* When splitting DWARF info, we put some attributes in the
31475 skeleton compile_unit DIE that remains in the .o, while
31476 most attributes go in the DWO compile_unit_die. */
31477 if (dwarf_split_debug_info)
31479 limbo_die_node *cu;
31480 main_comp_unit_die = gen_compile_unit_die (NULL);
31481 if (dwarf_version >= 5)
31482 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
31483 cu = limbo_die_list;
31484 gcc_assert (cu->die == main_comp_unit_die);
31485 limbo_die_list = limbo_die_list->next;
31486 cu->next = cu_die_list;
31487 cu_die_list = cu;
31489 else
31490 main_comp_unit_die = comp_unit_die ();
31492 /* Output a terminator label for the .text section. */
31493 switch_to_section (text_section);
31494 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
31495 if (cold_text_section)
31497 switch_to_section (cold_text_section);
31498 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
31501 /* We can only use the low/high_pc attributes if all of the code was
31502 in .text. */
31503 if (!have_multiple_function_sections
31504 || (dwarf_version < 3 && dwarf_strict))
31506 /* Don't add if the CU has no associated code. */
31507 if (text_section_used)
31508 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
31509 text_end_label, true);
31511 else
31513 unsigned fde_idx;
31514 dw_fde_ref fde;
31515 bool range_list_added = false;
31517 if (text_section_used)
31518 add_ranges_by_labels (main_comp_unit_die, text_section_label,
31519 text_end_label, &range_list_added, true);
31520 if (cold_text_section_used)
31521 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
31522 cold_end_label, &range_list_added, true);
31524 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
31526 if (DECL_IGNORED_P (fde->decl))
31527 continue;
31528 if (!fde->in_std_section)
31529 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
31530 fde->dw_fde_end, &range_list_added,
31531 true);
31532 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
31533 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
31534 fde->dw_fde_second_end, &range_list_added,
31535 true);
31538 if (range_list_added)
31540 /* We need to give .debug_loc and .debug_ranges an appropriate
31541 "base address". Use zero so that these addresses become
31542 absolute. Historically, we've emitted the unexpected
31543 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
31544 Emit both to give time for other tools to adapt. */
31545 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
31546 if (! dwarf_strict && dwarf_version < 4)
31547 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
31549 add_ranges (NULL);
31553 /* AIX Assembler inserts the length, so adjust the reference to match the
31554 offset expected by debuggers. */
31555 strcpy (dl_section_ref, debug_line_section_label);
31556 if (XCOFF_DEBUGGING_INFO)
31557 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
31559 if (debug_info_level >= DINFO_LEVEL_TERSE)
31560 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
31561 dl_section_ref);
31563 if (have_macinfo)
31564 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31565 macinfo_section_label);
31567 if (dwarf_split_debug_info)
31569 if (have_location_lists)
31571 /* Since we generate the loclists in the split DWARF .dwo
31572 file itself, we don't need to generate a loclists_base
31573 attribute for the split compile unit DIE. That attribute
31574 (and using relocatable sec_offset FORMs) isn't allowed
31575 for a split compile unit. Only if the .debug_loclists
31576 section was in the main file, would we need to generate a
31577 loclists_base attribute here (for the full or skeleton
31578 unit DIE). */
31580 /* optimize_location_lists calculates the size of the lists,
31581 so index them first, and assign indices to the entries.
31582 Although optimize_location_lists will remove entries from
31583 the table, it only does so for duplicates, and therefore
31584 only reduces ref_counts to 1. */
31585 index_location_lists (comp_unit_die ());
31588 if (addr_index_table != NULL)
31590 unsigned int index = 0;
31591 addr_index_table
31592 ->traverse_noresize<unsigned int *, index_addr_table_entry>
31593 (&index);
31597 loc_list_idx = 0;
31598 if (have_location_lists)
31600 optimize_location_lists (comp_unit_die ());
31601 /* And finally assign indexes to the entries for -gsplit-dwarf. */
31602 if (dwarf_version >= 5 && dwarf_split_debug_info)
31603 assign_location_list_indexes (comp_unit_die ());
31606 save_macinfo_strings ();
31608 if (dwarf_split_debug_info)
31610 unsigned int index = 0;
31612 /* Add attributes common to skeleton compile_units and
31613 type_units. Because these attributes include strings, it
31614 must be done before freezing the string table. Top-level
31615 skeleton die attrs are added when the skeleton type unit is
31616 created, so ensure it is created by this point. */
31617 add_top_level_skeleton_die_attrs (main_comp_unit_die);
31618 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31621 /* Output all of the compilation units. We put the main one last so that
31622 the offsets are available to output_pubnames. */
31623 for (node = cu_die_list; node; node = node->next)
31624 output_comp_unit (node->die, 0, NULL);
31626 hash_table<comdat_type_hasher> comdat_type_table (100);
31627 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31629 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31631 /* Don't output duplicate types. */
31632 if (*slot != HTAB_EMPTY_ENTRY)
31633 continue;
31635 /* Add a pointer to the line table for the main compilation unit
31636 so that the debugger can make sense of DW_AT_decl_file
31637 attributes. */
31638 if (debug_info_level >= DINFO_LEVEL_TERSE)
31639 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31640 (!dwarf_split_debug_info
31641 ? dl_section_ref
31642 : debug_skeleton_line_section_label));
31644 output_comdat_type_unit (ctnode, false);
31645 *slot = ctnode;
31648 if (dwarf_split_debug_info)
31650 int mark;
31651 struct md5_ctx ctx;
31653 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
31654 index_rnglists ();
31656 /* Compute a checksum of the comp_unit to use as the dwo_id. */
31657 md5_init_ctx (&ctx);
31658 mark = 0;
31659 die_checksum (comp_unit_die (), &ctx, &mark);
31660 unmark_all_dies (comp_unit_die ());
31661 md5_finish_ctx (&ctx, checksum);
31663 if (dwarf_version < 5)
31665 /* Use the first 8 bytes of the checksum as the dwo_id,
31666 and add it to both comp-unit DIEs. */
31667 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
31668 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
31671 /* Add the base offset of the ranges table to the skeleton
31672 comp-unit DIE. */
31673 if (!vec_safe_is_empty (ranges_table))
31675 if (dwarf_version >= 5)
31676 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
31677 ranges_base_label);
31678 else
31679 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
31680 ranges_section_label);
31683 output_addr_table ();
31686 /* Output the main compilation unit if non-empty or if .debug_macinfo
31687 or .debug_macro will be emitted. */
31688 output_comp_unit (comp_unit_die (), have_macinfo,
31689 dwarf_split_debug_info ? checksum : NULL);
31691 if (dwarf_split_debug_info && info_section_emitted)
31692 output_skeleton_debug_sections (main_comp_unit_die, checksum);
31694 /* Output the abbreviation table. */
31695 if (vec_safe_length (abbrev_die_table) != 1)
31697 switch_to_section (debug_abbrev_section);
31698 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31699 output_abbrev_section ();
31702 /* Output location list section if necessary. */
31703 if (have_location_lists)
31705 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
31706 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
31707 /* Output the location lists info. */
31708 switch_to_section (debug_loc_section);
31709 if (dwarf_version >= 5)
31711 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
31712 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
31713 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31714 dw2_asm_output_data (4, 0xffffffff,
31715 "Initial length escape value indicating "
31716 "64-bit DWARF extension");
31717 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
31718 "Length of Location Lists");
31719 ASM_OUTPUT_LABEL (asm_out_file, l1);
31720 output_dwarf_version ();
31721 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
31722 dw2_asm_output_data (1, 0, "Segment Size");
31723 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
31724 "Offset Entry Count");
31726 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
31727 if (dwarf_version >= 5 && dwarf_split_debug_info)
31729 unsigned int save_loc_list_idx = loc_list_idx;
31730 loc_list_idx = 0;
31731 output_loclists_offsets (comp_unit_die ());
31732 gcc_assert (save_loc_list_idx == loc_list_idx);
31734 output_location_lists (comp_unit_die ());
31735 if (dwarf_version >= 5)
31736 ASM_OUTPUT_LABEL (asm_out_file, l2);
31739 output_pubtables ();
31741 /* Output the address range information if a CU (.debug_info section)
31742 was emitted. We output an empty table even if we had no functions
31743 to put in it. This because the consumer has no way to tell the
31744 difference between an empty table that we omitted and failure to
31745 generate a table that would have contained data. */
31746 if (info_section_emitted)
31748 switch_to_section (debug_aranges_section);
31749 output_aranges ();
31752 /* Output ranges section if necessary. */
31753 if (!vec_safe_is_empty (ranges_table))
31755 if (dwarf_version >= 5)
31756 output_rnglists (generation);
31757 else
31758 output_ranges ();
31761 /* Have to end the macro section. */
31762 if (have_macinfo)
31764 switch_to_section (debug_macinfo_section);
31765 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31766 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
31767 : debug_skeleton_line_section_label, false);
31768 dw2_asm_output_data (1, 0, "End compilation unit");
31771 /* Output the source line correspondence table. We must do this
31772 even if there is no line information. Otherwise, on an empty
31773 translation unit, we will generate a present, but empty,
31774 .debug_info section. IRIX 6.5 `nm' will then complain when
31775 examining the file. This is done late so that any filenames
31776 used by the debug_info section are marked as 'used'. */
31777 switch_to_section (debug_line_section);
31778 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
31779 if (! output_asm_line_debug_info ())
31780 output_line_info (false);
31781 else if (asm_outputs_debug_line_str ())
31783 /* When gas outputs DWARF5 .debug_line[_str] then we have to
31784 tell it the comp_dir and main file name for the zero entry
31785 line table. */
31786 const char *comp_dir, *filename0;
31788 comp_dir = comp_dir_string ();
31789 if (comp_dir == NULL)
31790 comp_dir = "";
31792 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
31793 if (filename0 == NULL)
31794 filename0 = "";
31796 fprintf (asm_out_file, "\t.file 0 ");
31797 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
31798 fputc (' ', asm_out_file);
31799 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
31800 fputc ('\n', asm_out_file);
31803 if (dwarf_split_debug_info && info_section_emitted)
31805 switch_to_section (debug_skeleton_line_section);
31806 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31807 output_line_info (true);
31810 /* If we emitted any indirect strings, output the string table too. */
31811 if (debug_str_hash || skeleton_debug_str_hash)
31812 output_indirect_strings ();
31813 if (debug_line_str_hash)
31815 switch_to_section (debug_line_str_section);
31816 const enum dwarf_form form = DW_FORM_line_strp;
31817 debug_line_str_hash->traverse<enum dwarf_form,
31818 output_indirect_string> (form);
31821 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
31822 symview_upper_bound = 0;
31823 if (zero_view_p)
31824 bitmap_clear (zero_view_p);
31827 /* Returns a hash value for X (which really is a variable_value_struct). */
31829 inline hashval_t
31830 variable_value_hasher::hash (variable_value_struct *x)
31832 return (hashval_t) x->decl_id;
31835 /* Return nonzero if decl_id of variable_value_struct X is the same as
31836 UID of decl Y. */
31838 inline bool
31839 variable_value_hasher::equal (variable_value_struct *x, tree y)
31841 return x->decl_id == DECL_UID (y);
31844 /* Helper function for resolve_variable_value, handle
31845 DW_OP_GNU_variable_value in one location expression.
31846 Return true if exprloc has been changed into loclist. */
31848 static bool
31849 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31851 dw_loc_descr_ref next;
31852 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
31854 next = loc->dw_loc_next;
31855 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
31856 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
31857 continue;
31859 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31860 if (DECL_CONTEXT (decl) != current_function_decl)
31861 continue;
31863 dw_die_ref ref = lookup_decl_die (decl);
31864 if (ref)
31866 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31867 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31868 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31869 continue;
31871 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
31872 if (l == NULL)
31873 continue;
31874 if (l->dw_loc_next)
31876 if (AT_class (a) != dw_val_class_loc)
31877 continue;
31878 switch (a->dw_attr)
31880 /* Following attributes allow both exprloc and loclist
31881 classes, so we can change them into a loclist. */
31882 case DW_AT_location:
31883 case DW_AT_string_length:
31884 case DW_AT_return_addr:
31885 case DW_AT_data_member_location:
31886 case DW_AT_frame_base:
31887 case DW_AT_segment:
31888 case DW_AT_static_link:
31889 case DW_AT_use_location:
31890 case DW_AT_vtable_elem_location:
31891 if (prev)
31893 prev->dw_loc_next = NULL;
31894 prepend_loc_descr_to_each (l, AT_loc (a));
31896 if (next)
31897 add_loc_descr_to_each (l, next);
31898 a->dw_attr_val.val_class = dw_val_class_loc_list;
31899 a->dw_attr_val.val_entry = NULL;
31900 a->dw_attr_val.v.val_loc_list = l;
31901 have_location_lists = true;
31902 return true;
31903 /* Following attributes allow both exprloc and reference,
31904 so if the whole expression is DW_OP_GNU_variable_value alone
31905 we could transform it into reference. */
31906 case DW_AT_byte_size:
31907 case DW_AT_bit_size:
31908 case DW_AT_lower_bound:
31909 case DW_AT_upper_bound:
31910 case DW_AT_bit_stride:
31911 case DW_AT_count:
31912 case DW_AT_allocated:
31913 case DW_AT_associated:
31914 case DW_AT_byte_stride:
31915 if (prev == NULL && next == NULL)
31916 break;
31917 /* FALLTHRU */
31918 default:
31919 if (dwarf_strict)
31920 continue;
31921 break;
31923 /* Create DW_TAG_variable that we can refer to. */
31924 gen_decl_die (decl, NULL_TREE, NULL,
31925 lookup_decl_die (current_function_decl));
31926 ref = lookup_decl_die (decl);
31927 if (ref)
31929 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31930 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31931 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31933 continue;
31935 if (prev)
31937 prev->dw_loc_next = l->expr;
31938 add_loc_descr (&prev->dw_loc_next, next);
31939 free_loc_descr (loc, NULL);
31940 next = prev->dw_loc_next;
31942 else
31944 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
31945 add_loc_descr (&loc, next);
31946 next = loc;
31948 loc = prev;
31950 return false;
31953 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
31955 static void
31956 resolve_variable_value (dw_die_ref die)
31958 dw_attr_node *a;
31959 dw_loc_list_ref loc;
31960 unsigned ix;
31962 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31963 switch (AT_class (a))
31965 case dw_val_class_loc:
31966 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
31967 break;
31968 /* FALLTHRU */
31969 case dw_val_class_loc_list:
31970 loc = AT_loc_list (a);
31971 gcc_assert (loc);
31972 for (; loc; loc = loc->dw_loc_next)
31973 resolve_variable_value_in_expr (a, loc->expr);
31974 break;
31975 default:
31976 break;
31980 /* Attempt to optimize DW_OP_GNU_variable_value refering to
31981 temporaries in the current function. */
31983 static void
31984 resolve_variable_values (void)
31986 if (!variable_value_hash || !current_function_decl)
31987 return;
31989 struct variable_value_struct *node
31990 = variable_value_hash->find_with_hash (current_function_decl,
31991 DECL_UID (current_function_decl));
31993 if (node == NULL)
31994 return;
31996 unsigned int i;
31997 dw_die_ref die;
31998 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
31999 resolve_variable_value (die);
32002 /* Helper function for note_variable_value, handle one location
32003 expression. */
32005 static void
32006 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32008 for (; loc; loc = loc->dw_loc_next)
32009 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32010 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32012 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32013 dw_die_ref ref = lookup_decl_die (decl);
32014 if (! ref && (flag_generate_lto || flag_generate_offload))
32016 /* ??? This is somewhat a hack because we do not create DIEs
32017 for variables not in BLOCK trees early but when generating
32018 early LTO output we need the dw_val_class_decl_ref to be
32019 fully resolved. For fat LTO objects we'd also like to
32020 undo this after LTO dwarf output. */
32021 gcc_assert (DECL_CONTEXT (decl));
32022 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32023 gcc_assert (ctx != NULL);
32024 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32025 ref = lookup_decl_die (decl);
32026 gcc_assert (ref != NULL);
32028 if (ref)
32030 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32031 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32032 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32033 continue;
32035 if (VAR_P (decl)
32036 && DECL_CONTEXT (decl)
32037 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32038 && lookup_decl_die (DECL_CONTEXT (decl)))
32040 if (!variable_value_hash)
32041 variable_value_hash
32042 = hash_table<variable_value_hasher>::create_ggc (10);
32044 tree fndecl = DECL_CONTEXT (decl);
32045 struct variable_value_struct *node;
32046 struct variable_value_struct **slot
32047 = variable_value_hash->find_slot_with_hash (fndecl,
32048 DECL_UID (fndecl),
32049 INSERT);
32050 if (*slot == NULL)
32052 node = ggc_cleared_alloc<variable_value_struct> ();
32053 node->decl_id = DECL_UID (fndecl);
32054 *slot = node;
32056 else
32057 node = *slot;
32059 vec_safe_push (node->dies, die);
32064 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32065 with dw_val_class_decl_ref operand. */
32067 static void
32068 note_variable_value (dw_die_ref die)
32070 dw_die_ref c;
32071 dw_attr_node *a;
32072 dw_loc_list_ref loc;
32073 unsigned ix;
32075 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32076 switch (AT_class (a))
32078 case dw_val_class_loc_list:
32079 loc = AT_loc_list (a);
32080 gcc_assert (loc);
32081 if (!loc->noted_variable_value)
32083 loc->noted_variable_value = 1;
32084 for (; loc; loc = loc->dw_loc_next)
32085 note_variable_value_in_expr (die, loc->expr);
32087 break;
32088 case dw_val_class_loc:
32089 note_variable_value_in_expr (die, AT_loc (a));
32090 break;
32091 default:
32092 break;
32095 /* Mark children. */
32096 FOR_EACH_CHILD (die, c, note_variable_value (c));
32099 /* Perform any cleanups needed after the early debug generation pass
32100 has run. */
32102 static void
32103 dwarf2out_early_finish (const char *filename)
32105 set_early_dwarf s;
32106 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32108 /* PCH might result in DW_AT_producer string being restored from the
32109 header compilation, so always fill it with empty string initially
32110 and overwrite only here. */
32111 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32112 producer_string = gen_producer_string ();
32113 producer->dw_attr_val.v.val_str->refcount--;
32114 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32116 /* Add the name for the main input file now. We delayed this from
32117 dwarf2out_init to avoid complications with PCH. */
32118 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32119 add_comp_dir_attribute (comp_unit_die ());
32121 /* With LTO early dwarf was really finished at compile-time, so make
32122 sure to adjust the phase after annotating the LTRANS CU DIE. */
32123 if (in_lto_p)
32125 early_dwarf_finished = true;
32126 if (dump_file)
32128 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32129 print_die (comp_unit_die (), dump_file);
32131 return;
32134 /* Walk through the list of incomplete types again, trying once more to
32135 emit full debugging info for them. */
32136 retry_incomplete_types ();
32138 /* The point here is to flush out the limbo list so that it is empty
32139 and we don't need to stream it for LTO. */
32140 flush_limbo_die_list ();
32142 gen_scheduled_generic_parms_dies ();
32143 gen_remaining_tmpl_value_param_die_attribute ();
32145 /* Add DW_AT_linkage_name for all deferred DIEs. */
32146 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32148 tree decl = node->created_for;
32149 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32150 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32151 ended up in deferred_asm_name before we knew it was
32152 constant and never written to disk. */
32153 && DECL_ASSEMBLER_NAME (decl))
32155 add_linkage_attr (node->die, decl);
32156 move_linkage_attr (node->die);
32159 deferred_asm_name = NULL;
32161 if (flag_eliminate_unused_debug_types)
32162 prune_unused_types ();
32164 /* Generate separate COMDAT sections for type DIEs. */
32165 if (use_debug_types)
32167 break_out_comdat_types (comp_unit_die ());
32169 /* Each new type_unit DIE was added to the limbo die list when created.
32170 Since these have all been added to comdat_type_list, clear the
32171 limbo die list. */
32172 limbo_die_list = NULL;
32174 /* For each new comdat type unit, copy declarations for incomplete
32175 types to make the new unit self-contained (i.e., no direct
32176 references to the main compile unit). */
32177 for (comdat_type_node *ctnode = comdat_type_list;
32178 ctnode != NULL; ctnode = ctnode->next)
32179 copy_decls_for_unworthy_types (ctnode->root_die);
32180 copy_decls_for_unworthy_types (comp_unit_die ());
32182 /* In the process of copying declarations from one unit to another,
32183 we may have left some declarations behind that are no longer
32184 referenced. Prune them. */
32185 prune_unused_types ();
32188 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
32189 with dw_val_class_decl_ref operand. */
32190 note_variable_value (comp_unit_die ());
32191 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32192 note_variable_value (node->die);
32193 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
32194 ctnode = ctnode->next)
32195 note_variable_value (ctnode->root_die);
32196 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32197 note_variable_value (node->die);
32199 /* The AT_pubnames attribute needs to go in all skeleton dies, including
32200 both the main_cu and all skeleton TUs. Making this call unconditional
32201 would end up either adding a second copy of the AT_pubnames attribute, or
32202 requiring a special case in add_top_level_skeleton_die_attrs. */
32203 if (!dwarf_split_debug_info)
32204 add_AT_pubnames (comp_unit_die ());
32206 /* The early debug phase is now finished. */
32207 early_dwarf_finished = true;
32208 if (dump_file)
32210 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
32211 print_die (comp_unit_die (), dump_file);
32214 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
32215 if ((!flag_generate_lto && !flag_generate_offload)
32216 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
32217 copy_lto_debug_sections operation of the simple object support in
32218 libiberty is not implemented for them yet. */
32219 || TARGET_PECOFF || TARGET_COFF)
32220 return;
32222 /* Now as we are going to output for LTO initialize sections and labels
32223 to the LTO variants. We don't need a random-seed postfix as other
32224 LTO sections as linking the LTO debug sections into one in a partial
32225 link is fine. */
32226 init_sections_and_labels (true);
32228 /* The output below is modeled after dwarf2out_finish with all
32229 location related output removed and some LTO specific changes.
32230 Some refactoring might make both smaller and easier to match up. */
32232 /* Traverse the DIE's and add sibling attributes to those DIE's
32233 that have children. */
32234 add_sibling_attributes (comp_unit_die ());
32235 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32236 add_sibling_attributes (node->die);
32237 for (comdat_type_node *ctnode = comdat_type_list;
32238 ctnode != NULL; ctnode = ctnode->next)
32239 add_sibling_attributes (ctnode->root_die);
32241 /* AIX Assembler inserts the length, so adjust the reference to match the
32242 offset expected by debuggers. */
32243 strcpy (dl_section_ref, debug_line_section_label);
32244 if (XCOFF_DEBUGGING_INFO)
32245 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32247 if (debug_info_level >= DINFO_LEVEL_TERSE)
32248 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32250 if (have_macinfo)
32251 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32252 macinfo_section_label);
32254 save_macinfo_strings ();
32256 if (dwarf_split_debug_info)
32258 unsigned int index = 0;
32259 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32262 /* Output all of the compilation units. We put the main one last so that
32263 the offsets are available to output_pubnames. */
32264 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32265 output_comp_unit (node->die, 0, NULL);
32267 hash_table<comdat_type_hasher> comdat_type_table (100);
32268 for (comdat_type_node *ctnode = comdat_type_list;
32269 ctnode != NULL; ctnode = ctnode->next)
32271 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32273 /* Don't output duplicate types. */
32274 if (*slot != HTAB_EMPTY_ENTRY)
32275 continue;
32277 /* Add a pointer to the line table for the main compilation unit
32278 so that the debugger can make sense of DW_AT_decl_file
32279 attributes. */
32280 if (debug_info_level >= DINFO_LEVEL_TERSE)
32281 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32282 (!dwarf_split_debug_info
32283 ? debug_line_section_label
32284 : debug_skeleton_line_section_label));
32286 output_comdat_type_unit (ctnode, true);
32287 *slot = ctnode;
32290 /* Stick a unique symbol to the main debuginfo section. */
32291 compute_comp_unit_symbol (comp_unit_die ());
32293 /* Output the main compilation unit. We always need it if only for
32294 the CU symbol. */
32295 output_comp_unit (comp_unit_die (), true, NULL);
32297 /* Output the abbreviation table. */
32298 if (vec_safe_length (abbrev_die_table) != 1)
32300 switch_to_section (debug_abbrev_section);
32301 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32302 output_abbrev_section ();
32305 /* Have to end the macro section. */
32306 if (have_macinfo)
32308 /* We have to save macinfo state if we need to output it again
32309 for the FAT part of the object. */
32310 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32311 if (flag_fat_lto_objects)
32312 macinfo_table = macinfo_table->copy ();
32314 switch_to_section (debug_macinfo_section);
32315 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32316 output_macinfo (debug_line_section_label, true);
32317 dw2_asm_output_data (1, 0, "End compilation unit");
32319 if (flag_fat_lto_objects)
32321 vec_free (macinfo_table);
32322 macinfo_table = saved_macinfo_table;
32326 /* Emit a skeleton debug_line section. */
32327 switch_to_section (debug_line_section);
32328 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32329 output_line_info (true);
32331 /* If we emitted any indirect strings, output the string table too. */
32332 if (debug_str_hash || skeleton_debug_str_hash)
32333 output_indirect_strings ();
32334 if (debug_line_str_hash)
32336 switch_to_section (debug_line_str_section);
32337 const enum dwarf_form form = DW_FORM_line_strp;
32338 debug_line_str_hash->traverse<enum dwarf_form,
32339 output_indirect_string> (form);
32342 /* Switch back to the text section. */
32343 switch_to_section (text_section);
32346 /* Reset all state within dwarf2out.c so that we can rerun the compiler
32347 within the same process. For use by toplev::finalize. */
32349 void
32350 dwarf2out_c_finalize (void)
32352 last_var_location_insn = NULL;
32353 cached_next_real_insn = NULL;
32354 used_rtx_array = NULL;
32355 incomplete_types = NULL;
32356 debug_info_section = NULL;
32357 debug_skeleton_info_section = NULL;
32358 debug_abbrev_section = NULL;
32359 debug_skeleton_abbrev_section = NULL;
32360 debug_aranges_section = NULL;
32361 debug_addr_section = NULL;
32362 debug_macinfo_section = NULL;
32363 debug_line_section = NULL;
32364 debug_skeleton_line_section = NULL;
32365 debug_loc_section = NULL;
32366 debug_pubnames_section = NULL;
32367 debug_pubtypes_section = NULL;
32368 debug_str_section = NULL;
32369 debug_line_str_section = NULL;
32370 debug_str_dwo_section = NULL;
32371 debug_str_offsets_section = NULL;
32372 debug_ranges_section = NULL;
32373 debug_frame_section = NULL;
32374 fde_vec = NULL;
32375 debug_str_hash = NULL;
32376 debug_line_str_hash = NULL;
32377 skeleton_debug_str_hash = NULL;
32378 dw2_string_counter = 0;
32379 have_multiple_function_sections = false;
32380 text_section_used = false;
32381 cold_text_section_used = false;
32382 cold_text_section = NULL;
32383 current_unit_personality = NULL;
32385 early_dwarf = false;
32386 early_dwarf_finished = false;
32388 next_die_offset = 0;
32389 single_comp_unit_die = NULL;
32390 comdat_type_list = NULL;
32391 limbo_die_list = NULL;
32392 file_table = NULL;
32393 decl_die_table = NULL;
32394 common_block_die_table = NULL;
32395 decl_loc_table = NULL;
32396 call_arg_locations = NULL;
32397 call_arg_loc_last = NULL;
32398 call_site_count = -1;
32399 tail_call_site_count = -1;
32400 cached_dw_loc_list_table = NULL;
32401 abbrev_die_table = NULL;
32402 delete dwarf_proc_stack_usage_map;
32403 dwarf_proc_stack_usage_map = NULL;
32404 line_info_label_num = 0;
32405 cur_line_info_table = NULL;
32406 text_section_line_info = NULL;
32407 cold_text_section_line_info = NULL;
32408 separate_line_info = NULL;
32409 info_section_emitted = false;
32410 pubname_table = NULL;
32411 pubtype_table = NULL;
32412 macinfo_table = NULL;
32413 ranges_table = NULL;
32414 ranges_by_label = NULL;
32415 rnglist_idx = 0;
32416 have_location_lists = false;
32417 loclabel_num = 0;
32418 poc_label_num = 0;
32419 last_emitted_file = NULL;
32420 label_num = 0;
32421 tmpl_value_parm_die_table = NULL;
32422 generic_type_instances = NULL;
32423 frame_pointer_fb_offset = 0;
32424 frame_pointer_fb_offset_valid = false;
32425 base_types.release ();
32426 XDELETEVEC (producer_string);
32427 producer_string = NULL;
32430 #include "gt-dwarf2out.h"