[RS6000] dg-do !compile and scan-assembler
[official-gcc.git] / gcc / dwarf2out.c
blob534877babfbe5c0df67a266c753866e56797d84a
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;
5978 in_lto_p = false;
5979 dw_die_ref die = (TREE_CODE (decl) == BLOCK
5980 ? lookup_block_die (decl) : lookup_decl_die (decl));
5981 gcc_assert (!die);
5982 in_lto_p = true;
5984 tree ctx;
5985 dw_die_ref parent = NULL;
5986 /* Need to lookup a DIE for the decls context - the containing
5987 function or translation unit. */
5988 if (TREE_CODE (decl) == BLOCK)
5990 ctx = BLOCK_SUPERCONTEXT (decl);
5991 /* ??? We do not output DIEs for all scopes thus skip as
5992 many DIEs as needed. */
5993 while (TREE_CODE (ctx) == BLOCK
5994 && !lookup_block_die (ctx))
5995 ctx = BLOCK_SUPERCONTEXT (ctx);
5997 else
5998 ctx = DECL_CONTEXT (decl);
5999 /* Peel types in the context stack. */
6000 while (ctx && TYPE_P (ctx))
6001 ctx = TYPE_CONTEXT (ctx);
6002 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6003 if (debug_info_level <= DINFO_LEVEL_TERSE)
6004 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6005 ctx = DECL_CONTEXT (ctx);
6006 if (ctx)
6008 if (TREE_CODE (ctx) == BLOCK)
6009 parent = lookup_block_die (ctx);
6010 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6011 /* Keep the 1:1 association during WPA. */
6012 && !flag_wpa
6013 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6014 /* Otherwise all late annotations go to the main CU which
6015 imports the original CUs. */
6016 parent = comp_unit_die ();
6017 else if (TREE_CODE (ctx) == FUNCTION_DECL
6018 && TREE_CODE (decl) != FUNCTION_DECL
6019 && TREE_CODE (decl) != PARM_DECL
6020 && TREE_CODE (decl) != RESULT_DECL
6021 && TREE_CODE (decl) != BLOCK)
6022 /* Leave function local entities parent determination to when
6023 we process scope vars. */
6025 else
6026 parent = lookup_decl_die (ctx);
6028 else
6029 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6030 Handle this case gracefully by globalizing stuff. */
6031 parent = comp_unit_die ();
6032 /* Create a DIE "stub". */
6033 switch (TREE_CODE (decl))
6035 case TRANSLATION_UNIT_DECL:
6037 die = comp_unit_die ();
6038 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6039 to create a DIE for the original CUs. */
6040 return die;
6042 case NAMESPACE_DECL:
6043 if (is_fortran (decl))
6044 die = new_die (DW_TAG_module, parent, decl);
6045 else
6046 die = new_die (DW_TAG_namespace, parent, decl);
6047 break;
6048 case FUNCTION_DECL:
6049 die = new_die (DW_TAG_subprogram, parent, decl);
6050 break;
6051 case VAR_DECL:
6052 die = new_die (DW_TAG_variable, parent, decl);
6053 break;
6054 case RESULT_DECL:
6055 die = new_die (DW_TAG_variable, parent, decl);
6056 break;
6057 case PARM_DECL:
6058 die = new_die (DW_TAG_formal_parameter, parent, decl);
6059 break;
6060 case CONST_DECL:
6061 die = new_die (DW_TAG_constant, parent, decl);
6062 break;
6063 case LABEL_DECL:
6064 die = new_die (DW_TAG_label, parent, decl);
6065 break;
6066 case BLOCK:
6067 die = new_die (DW_TAG_lexical_block, parent, decl);
6068 break;
6069 default:
6070 gcc_unreachable ();
6072 if (TREE_CODE (decl) == BLOCK)
6073 equate_block_to_die (decl, die);
6074 else
6075 equate_decl_number_to_die (decl, die);
6077 add_desc_attribute (die, decl);
6079 /* Add a reference to the DIE providing early debug at $sym + off. */
6080 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6082 return die;
6085 /* Returns a hash value for X (which really is a var_loc_list). */
6087 inline hashval_t
6088 decl_loc_hasher::hash (var_loc_list *x)
6090 return (hashval_t) x->decl_id;
6093 /* Return nonzero if decl_id of var_loc_list X is the same as
6094 UID of decl *Y. */
6096 inline bool
6097 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6099 return (x->decl_id == DECL_UID (y));
6102 /* Return the var_loc list associated with a given declaration. */
6104 static inline var_loc_list *
6105 lookup_decl_loc (const_tree decl)
6107 if (!decl_loc_table)
6108 return NULL;
6109 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6112 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6114 inline hashval_t
6115 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6117 return (hashval_t) x->decl_id;
6120 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6121 UID of decl *Y. */
6123 inline bool
6124 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6126 return (x->decl_id == DECL_UID (y));
6129 /* Equate a DIE to a particular declaration. */
6131 static void
6132 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6134 unsigned int decl_id = DECL_UID (decl);
6136 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6137 decl_die->decl_id = decl_id;
6140 /* Return how many bits covers PIECE EXPR_LIST. */
6142 static HOST_WIDE_INT
6143 decl_piece_bitsize (rtx piece)
6145 int ret = (int) GET_MODE (piece);
6146 if (ret)
6147 return ret;
6148 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6149 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6150 return INTVAL (XEXP (XEXP (piece, 0), 0));
6153 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6155 static rtx *
6156 decl_piece_varloc_ptr (rtx piece)
6158 if ((int) GET_MODE (piece))
6159 return &XEXP (piece, 0);
6160 else
6161 return &XEXP (XEXP (piece, 0), 1);
6164 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6165 Next is the chain of following piece nodes. */
6167 static rtx_expr_list *
6168 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6170 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6171 return alloc_EXPR_LIST (bitsize, loc_note, next);
6172 else
6173 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6174 GEN_INT (bitsize),
6175 loc_note), next);
6178 /* Return rtx that should be stored into loc field for
6179 LOC_NOTE and BITPOS/BITSIZE. */
6181 static rtx
6182 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6183 HOST_WIDE_INT bitsize)
6185 if (bitsize != -1)
6187 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6188 if (bitpos != 0)
6189 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6191 return loc_note;
6194 /* This function either modifies location piece list *DEST in
6195 place (if SRC and INNER is NULL), or copies location piece list
6196 *SRC to *DEST while modifying it. Location BITPOS is modified
6197 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6198 not copied and if needed some padding around it is added.
6199 When modifying in place, DEST should point to EXPR_LIST where
6200 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6201 to the start of the whole list and INNER points to the EXPR_LIST
6202 where earlier pieces cover PIECE_BITPOS bits. */
6204 static void
6205 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6206 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6207 HOST_WIDE_INT bitsize, rtx loc_note)
6209 HOST_WIDE_INT diff;
6210 bool copy = inner != NULL;
6212 if (copy)
6214 /* First copy all nodes preceding the current bitpos. */
6215 while (src != inner)
6217 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6218 decl_piece_bitsize (*src), NULL_RTX);
6219 dest = &XEXP (*dest, 1);
6220 src = &XEXP (*src, 1);
6223 /* Add padding if needed. */
6224 if (bitpos != piece_bitpos)
6226 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6227 copy ? NULL_RTX : *dest);
6228 dest = &XEXP (*dest, 1);
6230 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6232 gcc_assert (!copy);
6233 /* A piece with correct bitpos and bitsize already exist,
6234 just update the location for it and return. */
6235 *decl_piece_varloc_ptr (*dest) = loc_note;
6236 return;
6238 /* Add the piece that changed. */
6239 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6240 dest = &XEXP (*dest, 1);
6241 /* Skip over pieces that overlap it. */
6242 diff = bitpos - piece_bitpos + bitsize;
6243 if (!copy)
6244 src = dest;
6245 while (diff > 0 && *src)
6247 rtx piece = *src;
6248 diff -= decl_piece_bitsize (piece);
6249 if (copy)
6250 src = &XEXP (piece, 1);
6251 else
6253 *src = XEXP (piece, 1);
6254 free_EXPR_LIST_node (piece);
6257 /* Add padding if needed. */
6258 if (diff < 0 && *src)
6260 if (!copy)
6261 dest = src;
6262 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6263 dest = &XEXP (*dest, 1);
6265 if (!copy)
6266 return;
6267 /* Finally copy all nodes following it. */
6268 while (*src)
6270 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6271 decl_piece_bitsize (*src), NULL_RTX);
6272 dest = &XEXP (*dest, 1);
6273 src = &XEXP (*src, 1);
6277 /* Add a variable location node to the linked list for DECL. */
6279 static struct var_loc_node *
6280 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6282 unsigned int decl_id;
6283 var_loc_list *temp;
6284 struct var_loc_node *loc = NULL;
6285 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6287 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6289 tree realdecl = DECL_DEBUG_EXPR (decl);
6290 if (handled_component_p (realdecl)
6291 || (TREE_CODE (realdecl) == MEM_REF
6292 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6294 bool reverse;
6295 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6296 &bitsize, &reverse);
6297 if (!innerdecl
6298 || !DECL_P (innerdecl)
6299 || DECL_IGNORED_P (innerdecl)
6300 || TREE_STATIC (innerdecl)
6301 || bitsize == 0
6302 || bitpos + bitsize > 256)
6303 return NULL;
6304 decl = innerdecl;
6308 decl_id = DECL_UID (decl);
6309 var_loc_list **slot
6310 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6311 if (*slot == NULL)
6313 temp = ggc_cleared_alloc<var_loc_list> ();
6314 temp->decl_id = decl_id;
6315 *slot = temp;
6317 else
6318 temp = *slot;
6320 /* For PARM_DECLs try to keep around the original incoming value,
6321 even if that means we'll emit a zero-range .debug_loc entry. */
6322 if (temp->last
6323 && temp->first == temp->last
6324 && TREE_CODE (decl) == PARM_DECL
6325 && NOTE_P (temp->first->loc)
6326 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6327 && DECL_INCOMING_RTL (decl)
6328 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6329 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6330 == GET_CODE (DECL_INCOMING_RTL (decl))
6331 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6332 && (bitsize != -1
6333 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6334 NOTE_VAR_LOCATION_LOC (loc_note))
6335 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6336 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6338 loc = ggc_cleared_alloc<var_loc_node> ();
6339 temp->first->next = loc;
6340 temp->last = loc;
6341 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6343 else if (temp->last)
6345 struct var_loc_node *last = temp->last, *unused = NULL;
6346 rtx *piece_loc = NULL, last_loc_note;
6347 HOST_WIDE_INT piece_bitpos = 0;
6348 if (last->next)
6350 last = last->next;
6351 gcc_assert (last->next == NULL);
6353 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6355 piece_loc = &last->loc;
6358 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6359 if (piece_bitpos + cur_bitsize > bitpos)
6360 break;
6361 piece_bitpos += cur_bitsize;
6362 piece_loc = &XEXP (*piece_loc, 1);
6364 while (*piece_loc);
6366 /* TEMP->LAST here is either pointer to the last but one or
6367 last element in the chained list, LAST is pointer to the
6368 last element. */
6369 if (label && strcmp (last->label, label) == 0 && last->view == view)
6371 /* For SRA optimized variables if there weren't any real
6372 insns since last note, just modify the last node. */
6373 if (piece_loc != NULL)
6375 adjust_piece_list (piece_loc, NULL, NULL,
6376 bitpos, piece_bitpos, bitsize, loc_note);
6377 return NULL;
6379 /* If the last note doesn't cover any instructions, remove it. */
6380 if (temp->last != last)
6382 temp->last->next = NULL;
6383 unused = last;
6384 last = temp->last;
6385 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6387 else
6389 gcc_assert (temp->first == temp->last
6390 || (temp->first->next == temp->last
6391 && TREE_CODE (decl) == PARM_DECL));
6392 memset (temp->last, '\0', sizeof (*temp->last));
6393 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6394 return temp->last;
6397 if (bitsize == -1 && NOTE_P (last->loc))
6398 last_loc_note = last->loc;
6399 else if (piece_loc != NULL
6400 && *piece_loc != NULL_RTX
6401 && piece_bitpos == bitpos
6402 && decl_piece_bitsize (*piece_loc) == bitsize)
6403 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6404 else
6405 last_loc_note = NULL_RTX;
6406 /* If the current location is the same as the end of the list,
6407 and either both or neither of the locations is uninitialized,
6408 we have nothing to do. */
6409 if (last_loc_note == NULL_RTX
6410 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6411 NOTE_VAR_LOCATION_LOC (loc_note)))
6412 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6413 != NOTE_VAR_LOCATION_STATUS (loc_note))
6414 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6415 == VAR_INIT_STATUS_UNINITIALIZED)
6416 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6417 == VAR_INIT_STATUS_UNINITIALIZED))))
6419 /* Add LOC to the end of list and update LAST. If the last
6420 element of the list has been removed above, reuse its
6421 memory for the new node, otherwise allocate a new one. */
6422 if (unused)
6424 loc = unused;
6425 memset (loc, '\0', sizeof (*loc));
6427 else
6428 loc = ggc_cleared_alloc<var_loc_node> ();
6429 if (bitsize == -1 || piece_loc == NULL)
6430 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6431 else
6432 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6433 bitpos, piece_bitpos, bitsize, loc_note);
6434 last->next = loc;
6435 /* Ensure TEMP->LAST will point either to the new last but one
6436 element of the chain, or to the last element in it. */
6437 if (last != temp->last)
6438 temp->last = last;
6440 else if (unused)
6441 ggc_free (unused);
6443 else
6445 loc = ggc_cleared_alloc<var_loc_node> ();
6446 temp->first = loc;
6447 temp->last = loc;
6448 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6450 return loc;
6453 /* Keep track of the number of spaces used to indent the
6454 output of the debugging routines that print the structure of
6455 the DIE internal representation. */
6456 static int print_indent;
6458 /* Indent the line the number of spaces given by print_indent. */
6460 static inline void
6461 print_spaces (FILE *outfile)
6463 fprintf (outfile, "%*s", print_indent, "");
6466 /* Print a type signature in hex. */
6468 static inline void
6469 print_signature (FILE *outfile, char *sig)
6471 int i;
6473 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6474 fprintf (outfile, "%02x", sig[i] & 0xff);
6477 static inline void
6478 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6480 if (discr_value->pos)
6481 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6482 else
6483 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6486 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6488 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6489 RECURSE, output location descriptor operations. */
6491 static void
6492 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6494 switch (val->val_class)
6496 case dw_val_class_addr:
6497 fprintf (outfile, "address");
6498 break;
6499 case dw_val_class_offset:
6500 fprintf (outfile, "offset");
6501 break;
6502 case dw_val_class_loc:
6503 fprintf (outfile, "location descriptor");
6504 if (val->v.val_loc == NULL)
6505 fprintf (outfile, " -> <null>\n");
6506 else if (recurse)
6508 fprintf (outfile, ":\n");
6509 print_indent += 4;
6510 print_loc_descr (val->v.val_loc, outfile);
6511 print_indent -= 4;
6513 else
6515 if (flag_dump_noaddr || flag_dump_unnumbered)
6516 fprintf (outfile, " #\n");
6517 else
6518 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6520 break;
6521 case dw_val_class_loc_list:
6522 fprintf (outfile, "location list -> label:%s",
6523 val->v.val_loc_list->ll_symbol);
6524 break;
6525 case dw_val_class_view_list:
6526 val = view_list_to_loc_list_val_node (val);
6527 fprintf (outfile, "location list with views -> labels:%s and %s",
6528 val->v.val_loc_list->ll_symbol,
6529 val->v.val_loc_list->vl_symbol);
6530 break;
6531 case dw_val_class_range_list:
6532 fprintf (outfile, "range list");
6533 break;
6534 case dw_val_class_const:
6535 case dw_val_class_const_implicit:
6536 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6537 break;
6538 case dw_val_class_unsigned_const:
6539 case dw_val_class_unsigned_const_implicit:
6540 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6541 break;
6542 case dw_val_class_const_double:
6543 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6544 HOST_WIDE_INT_PRINT_UNSIGNED")",
6545 val->v.val_double.high,
6546 val->v.val_double.low);
6547 break;
6548 case dw_val_class_wide_int:
6550 int i = val->v.val_wide->get_len ();
6551 fprintf (outfile, "constant (");
6552 gcc_assert (i > 0);
6553 if (val->v.val_wide->elt (i - 1) == 0)
6554 fprintf (outfile, "0x");
6555 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6556 val->v.val_wide->elt (--i));
6557 while (--i >= 0)
6558 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6559 val->v.val_wide->elt (i));
6560 fprintf (outfile, ")");
6561 break;
6563 case dw_val_class_vec:
6564 fprintf (outfile, "floating-point or vector constant");
6565 break;
6566 case dw_val_class_flag:
6567 fprintf (outfile, "%u", val->v.val_flag);
6568 break;
6569 case dw_val_class_die_ref:
6570 if (val->v.val_die_ref.die != NULL)
6572 dw_die_ref die = val->v.val_die_ref.die;
6574 if (die->comdat_type_p)
6576 fprintf (outfile, "die -> signature: ");
6577 print_signature (outfile,
6578 die->die_id.die_type_node->signature);
6580 else if (die->die_id.die_symbol)
6582 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6583 if (die->with_offset)
6584 fprintf (outfile, " + %ld", die->die_offset);
6586 else
6587 fprintf (outfile, "die -> %ld", die->die_offset);
6588 if (flag_dump_noaddr || flag_dump_unnumbered)
6589 fprintf (outfile, " #");
6590 else
6591 fprintf (outfile, " (%p)", (void *) die);
6593 else
6594 fprintf (outfile, "die -> <null>");
6595 break;
6596 case dw_val_class_vms_delta:
6597 fprintf (outfile, "delta: @slotcount(%s-%s)",
6598 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6599 break;
6600 case dw_val_class_symview:
6601 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6602 break;
6603 case dw_val_class_lbl_id:
6604 case dw_val_class_lineptr:
6605 case dw_val_class_macptr:
6606 case dw_val_class_loclistsptr:
6607 case dw_val_class_high_pc:
6608 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6609 break;
6610 case dw_val_class_str:
6611 if (val->v.val_str->str != NULL)
6612 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6613 else
6614 fprintf (outfile, "<null>");
6615 break;
6616 case dw_val_class_file:
6617 case dw_val_class_file_implicit:
6618 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6619 val->v.val_file->emitted_number);
6620 break;
6621 case dw_val_class_data8:
6623 int i;
6625 for (i = 0; i < 8; i++)
6626 fprintf (outfile, "%02x", val->v.val_data8[i]);
6627 break;
6629 case dw_val_class_discr_value:
6630 print_discr_value (outfile, &val->v.val_discr_value);
6631 break;
6632 case dw_val_class_discr_list:
6633 for (dw_discr_list_ref node = val->v.val_discr_list;
6634 node != NULL;
6635 node = node->dw_discr_next)
6637 if (node->dw_discr_range)
6639 fprintf (outfile, " .. ");
6640 print_discr_value (outfile, &node->dw_discr_lower_bound);
6641 print_discr_value (outfile, &node->dw_discr_upper_bound);
6643 else
6644 print_discr_value (outfile, &node->dw_discr_lower_bound);
6646 if (node->dw_discr_next != NULL)
6647 fprintf (outfile, " | ");
6649 default:
6650 break;
6654 /* Likewise, for a DIE attribute. */
6656 static void
6657 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6659 print_dw_val (&a->dw_attr_val, recurse, outfile);
6663 /* Print the list of operands in the LOC location description to OUTFILE. This
6664 routine is a debugging aid only. */
6666 static void
6667 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6669 dw_loc_descr_ref l = loc;
6671 if (loc == NULL)
6673 print_spaces (outfile);
6674 fprintf (outfile, "<null>\n");
6675 return;
6678 for (l = loc; l != NULL; l = l->dw_loc_next)
6680 print_spaces (outfile);
6681 if (flag_dump_noaddr || flag_dump_unnumbered)
6682 fprintf (outfile, "#");
6683 else
6684 fprintf (outfile, "(%p)", (void *) l);
6685 fprintf (outfile, " %s",
6686 dwarf_stack_op_name (l->dw_loc_opc));
6687 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6689 fprintf (outfile, " ");
6690 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6692 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6694 fprintf (outfile, ", ");
6695 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6697 fprintf (outfile, "\n");
6701 /* Print the information associated with a given DIE, and its children.
6702 This routine is a debugging aid only. */
6704 static void
6705 print_die (dw_die_ref die, FILE *outfile)
6707 dw_attr_node *a;
6708 dw_die_ref c;
6709 unsigned ix;
6711 print_spaces (outfile);
6712 fprintf (outfile, "DIE %4ld: %s ",
6713 die->die_offset, dwarf_tag_name (die->die_tag));
6714 if (flag_dump_noaddr || flag_dump_unnumbered)
6715 fprintf (outfile, "#\n");
6716 else
6717 fprintf (outfile, "(%p)\n", (void*) die);
6718 print_spaces (outfile);
6719 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6720 fprintf (outfile, " offset: %ld", die->die_offset);
6721 fprintf (outfile, " mark: %d\n", die->die_mark);
6723 if (die->comdat_type_p)
6725 print_spaces (outfile);
6726 fprintf (outfile, " signature: ");
6727 print_signature (outfile, die->die_id.die_type_node->signature);
6728 fprintf (outfile, "\n");
6731 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6733 print_spaces (outfile);
6734 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6736 print_attribute (a, true, outfile);
6737 fprintf (outfile, "\n");
6740 if (die->die_child != NULL)
6742 print_indent += 4;
6743 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6744 print_indent -= 4;
6746 if (print_indent == 0)
6747 fprintf (outfile, "\n");
6750 /* Print the list of operations in the LOC location description. */
6752 DEBUG_FUNCTION void
6753 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6755 print_loc_descr (loc, stderr);
6758 /* Print the information collected for a given DIE. */
6760 DEBUG_FUNCTION void
6761 debug_dwarf_die (dw_die_ref die)
6763 print_die (die, stderr);
6766 DEBUG_FUNCTION void
6767 debug (die_struct &ref)
6769 print_die (&ref, stderr);
6772 DEBUG_FUNCTION void
6773 debug (die_struct *ptr)
6775 if (ptr)
6776 debug (*ptr);
6777 else
6778 fprintf (stderr, "<nil>\n");
6782 /* Print all DWARF information collected for the compilation unit.
6783 This routine is a debugging aid only. */
6785 DEBUG_FUNCTION void
6786 debug_dwarf (void)
6788 print_indent = 0;
6789 print_die (comp_unit_die (), stderr);
6792 /* Verify the DIE tree structure. */
6794 DEBUG_FUNCTION void
6795 verify_die (dw_die_ref die)
6797 gcc_assert (!die->die_mark);
6798 if (die->die_parent == NULL
6799 && die->die_sib == NULL)
6800 return;
6801 /* Verify the die_sib list is cyclic. */
6802 dw_die_ref x = die;
6805 x->die_mark = 1;
6806 x = x->die_sib;
6808 while (x && !x->die_mark);
6809 gcc_assert (x == die);
6810 x = die;
6813 /* Verify all dies have the same parent. */
6814 gcc_assert (x->die_parent == die->die_parent);
6815 if (x->die_child)
6817 /* Verify the child has the proper parent and recurse. */
6818 gcc_assert (x->die_child->die_parent == x);
6819 verify_die (x->die_child);
6821 x->die_mark = 0;
6822 x = x->die_sib;
6824 while (x && x->die_mark);
6827 /* Sanity checks on DIEs. */
6829 static void
6830 check_die (dw_die_ref die)
6832 unsigned ix;
6833 dw_attr_node *a;
6834 bool inline_found = false;
6835 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6836 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6837 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6839 switch (a->dw_attr)
6841 case DW_AT_inline:
6842 if (a->dw_attr_val.v.val_unsigned)
6843 inline_found = true;
6844 break;
6845 case DW_AT_location:
6846 ++n_location;
6847 break;
6848 case DW_AT_low_pc:
6849 ++n_low_pc;
6850 break;
6851 case DW_AT_high_pc:
6852 ++n_high_pc;
6853 break;
6854 case DW_AT_artificial:
6855 ++n_artificial;
6856 break;
6857 case DW_AT_decl_column:
6858 ++n_decl_column;
6859 break;
6860 case DW_AT_decl_line:
6861 ++n_decl_line;
6862 break;
6863 case DW_AT_decl_file:
6864 ++n_decl_file;
6865 break;
6866 default:
6867 break;
6870 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6871 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6873 fprintf (stderr, "Duplicate attributes in DIE:\n");
6874 debug_dwarf_die (die);
6875 gcc_unreachable ();
6877 if (inline_found)
6879 /* A debugging information entry that is a member of an abstract
6880 instance tree [that has DW_AT_inline] should not contain any
6881 attributes which describe aspects of the subroutine which vary
6882 between distinct inlined expansions or distinct out-of-line
6883 expansions. */
6884 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6885 gcc_assert (a->dw_attr != DW_AT_low_pc
6886 && a->dw_attr != DW_AT_high_pc
6887 && a->dw_attr != DW_AT_location
6888 && a->dw_attr != DW_AT_frame_base
6889 && a->dw_attr != DW_AT_call_all_calls
6890 && a->dw_attr != DW_AT_GNU_all_call_sites);
6894 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6895 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6896 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6898 /* Calculate the checksum of a location expression. */
6900 static inline void
6901 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6903 int tem;
6904 inchash::hash hstate;
6905 hashval_t hash;
6907 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6908 CHECKSUM (tem);
6909 hash_loc_operands (loc, hstate);
6910 hash = hstate.end();
6911 CHECKSUM (hash);
6914 /* Calculate the checksum of an attribute. */
6916 static void
6917 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6919 dw_loc_descr_ref loc;
6920 rtx r;
6922 CHECKSUM (at->dw_attr);
6924 /* We don't care that this was compiled with a different compiler
6925 snapshot; if the output is the same, that's what matters. */
6926 if (at->dw_attr == DW_AT_producer)
6927 return;
6929 switch (AT_class (at))
6931 case dw_val_class_const:
6932 case dw_val_class_const_implicit:
6933 CHECKSUM (at->dw_attr_val.v.val_int);
6934 break;
6935 case dw_val_class_unsigned_const:
6936 case dw_val_class_unsigned_const_implicit:
6937 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6938 break;
6939 case dw_val_class_const_double:
6940 CHECKSUM (at->dw_attr_val.v.val_double);
6941 break;
6942 case dw_val_class_wide_int:
6943 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6944 get_full_len (*at->dw_attr_val.v.val_wide)
6945 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6946 break;
6947 case dw_val_class_vec:
6948 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6949 (at->dw_attr_val.v.val_vec.length
6950 * at->dw_attr_val.v.val_vec.elt_size));
6951 break;
6952 case dw_val_class_flag:
6953 CHECKSUM (at->dw_attr_val.v.val_flag);
6954 break;
6955 case dw_val_class_str:
6956 CHECKSUM_STRING (AT_string (at));
6957 break;
6959 case dw_val_class_addr:
6960 r = AT_addr (at);
6961 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6962 CHECKSUM_STRING (XSTR (r, 0));
6963 break;
6965 case dw_val_class_offset:
6966 CHECKSUM (at->dw_attr_val.v.val_offset);
6967 break;
6969 case dw_val_class_loc:
6970 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6971 loc_checksum (loc, ctx);
6972 break;
6974 case dw_val_class_die_ref:
6975 die_checksum (AT_ref (at), ctx, mark);
6976 break;
6978 case dw_val_class_fde_ref:
6979 case dw_val_class_vms_delta:
6980 case dw_val_class_symview:
6981 case dw_val_class_lbl_id:
6982 case dw_val_class_lineptr:
6983 case dw_val_class_macptr:
6984 case dw_val_class_loclistsptr:
6985 case dw_val_class_high_pc:
6986 break;
6988 case dw_val_class_file:
6989 case dw_val_class_file_implicit:
6990 CHECKSUM_STRING (AT_file (at)->filename);
6991 break;
6993 case dw_val_class_data8:
6994 CHECKSUM (at->dw_attr_val.v.val_data8);
6995 break;
6997 default:
6998 break;
7002 /* Calculate the checksum of a DIE. */
7004 static void
7005 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7007 dw_die_ref c;
7008 dw_attr_node *a;
7009 unsigned ix;
7011 /* To avoid infinite recursion. */
7012 if (die->die_mark)
7014 CHECKSUM (die->die_mark);
7015 return;
7017 die->die_mark = ++(*mark);
7019 CHECKSUM (die->die_tag);
7021 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7022 attr_checksum (a, ctx, mark);
7024 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7027 #undef CHECKSUM
7028 #undef CHECKSUM_BLOCK
7029 #undef CHECKSUM_STRING
7031 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7032 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7033 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7034 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7035 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7036 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7037 #define CHECKSUM_ATTR(FOO) \
7038 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7040 /* Calculate the checksum of a number in signed LEB128 format. */
7042 static void
7043 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7045 unsigned char byte;
7046 bool more;
7048 while (1)
7050 byte = (value & 0x7f);
7051 value >>= 7;
7052 more = !((value == 0 && (byte & 0x40) == 0)
7053 || (value == -1 && (byte & 0x40) != 0));
7054 if (more)
7055 byte |= 0x80;
7056 CHECKSUM (byte);
7057 if (!more)
7058 break;
7062 /* Calculate the checksum of a number in unsigned LEB128 format. */
7064 static void
7065 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7067 while (1)
7069 unsigned char byte = (value & 0x7f);
7070 value >>= 7;
7071 if (value != 0)
7072 /* More bytes to follow. */
7073 byte |= 0x80;
7074 CHECKSUM (byte);
7075 if (value == 0)
7076 break;
7080 /* Checksum the context of the DIE. This adds the names of any
7081 surrounding namespaces or structures to the checksum. */
7083 static void
7084 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7086 const char *name;
7087 dw_die_ref spec;
7088 int tag = die->die_tag;
7090 if (tag != DW_TAG_namespace
7091 && tag != DW_TAG_structure_type
7092 && tag != DW_TAG_class_type)
7093 return;
7095 name = get_AT_string (die, DW_AT_name);
7097 spec = get_AT_ref (die, DW_AT_specification);
7098 if (spec != NULL)
7099 die = spec;
7101 if (die->die_parent != NULL)
7102 checksum_die_context (die->die_parent, ctx);
7104 CHECKSUM_ULEB128 ('C');
7105 CHECKSUM_ULEB128 (tag);
7106 if (name != NULL)
7107 CHECKSUM_STRING (name);
7110 /* Calculate the checksum of a location expression. */
7112 static inline void
7113 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7115 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7116 were emitted as a DW_FORM_sdata instead of a location expression. */
7117 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7119 CHECKSUM_ULEB128 (DW_FORM_sdata);
7120 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7121 return;
7124 /* Otherwise, just checksum the raw location expression. */
7125 while (loc != NULL)
7127 inchash::hash hstate;
7128 hashval_t hash;
7130 CHECKSUM_ULEB128 (loc->dtprel);
7131 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7132 hash_loc_operands (loc, hstate);
7133 hash = hstate.end ();
7134 CHECKSUM (hash);
7135 loc = loc->dw_loc_next;
7139 /* Calculate the checksum of an attribute. */
7141 static void
7142 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7143 struct md5_ctx *ctx, int *mark)
7145 dw_loc_descr_ref loc;
7146 rtx r;
7148 if (AT_class (at) == dw_val_class_die_ref)
7150 dw_die_ref target_die = AT_ref (at);
7152 /* For pointer and reference types, we checksum only the (qualified)
7153 name of the target type (if there is a name). For friend entries,
7154 we checksum only the (qualified) name of the target type or function.
7155 This allows the checksum to remain the same whether the target type
7156 is complete or not. */
7157 if ((at->dw_attr == DW_AT_type
7158 && (tag == DW_TAG_pointer_type
7159 || tag == DW_TAG_reference_type
7160 || tag == DW_TAG_rvalue_reference_type
7161 || tag == DW_TAG_ptr_to_member_type))
7162 || (at->dw_attr == DW_AT_friend
7163 && tag == DW_TAG_friend))
7165 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7167 if (name_attr != NULL)
7169 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7171 if (decl == NULL)
7172 decl = target_die;
7173 CHECKSUM_ULEB128 ('N');
7174 CHECKSUM_ULEB128 (at->dw_attr);
7175 if (decl->die_parent != NULL)
7176 checksum_die_context (decl->die_parent, ctx);
7177 CHECKSUM_ULEB128 ('E');
7178 CHECKSUM_STRING (AT_string (name_attr));
7179 return;
7183 /* For all other references to another DIE, we check to see if the
7184 target DIE has already been visited. If it has, we emit a
7185 backward reference; if not, we descend recursively. */
7186 if (target_die->die_mark > 0)
7188 CHECKSUM_ULEB128 ('R');
7189 CHECKSUM_ULEB128 (at->dw_attr);
7190 CHECKSUM_ULEB128 (target_die->die_mark);
7192 else
7194 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7196 if (decl == NULL)
7197 decl = target_die;
7198 target_die->die_mark = ++(*mark);
7199 CHECKSUM_ULEB128 ('T');
7200 CHECKSUM_ULEB128 (at->dw_attr);
7201 if (decl->die_parent != NULL)
7202 checksum_die_context (decl->die_parent, ctx);
7203 die_checksum_ordered (target_die, ctx, mark);
7205 return;
7208 CHECKSUM_ULEB128 ('A');
7209 CHECKSUM_ULEB128 (at->dw_attr);
7211 switch (AT_class (at))
7213 case dw_val_class_const:
7214 case dw_val_class_const_implicit:
7215 CHECKSUM_ULEB128 (DW_FORM_sdata);
7216 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7217 break;
7219 case dw_val_class_unsigned_const:
7220 case dw_val_class_unsigned_const_implicit:
7221 CHECKSUM_ULEB128 (DW_FORM_sdata);
7222 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7223 break;
7225 case dw_val_class_const_double:
7226 CHECKSUM_ULEB128 (DW_FORM_block);
7227 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7228 CHECKSUM (at->dw_attr_val.v.val_double);
7229 break;
7231 case dw_val_class_wide_int:
7232 CHECKSUM_ULEB128 (DW_FORM_block);
7233 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7234 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7235 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7236 get_full_len (*at->dw_attr_val.v.val_wide)
7237 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7238 break;
7240 case dw_val_class_vec:
7241 CHECKSUM_ULEB128 (DW_FORM_block);
7242 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7243 * at->dw_attr_val.v.val_vec.elt_size);
7244 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7245 (at->dw_attr_val.v.val_vec.length
7246 * at->dw_attr_val.v.val_vec.elt_size));
7247 break;
7249 case dw_val_class_flag:
7250 CHECKSUM_ULEB128 (DW_FORM_flag);
7251 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7252 break;
7254 case dw_val_class_str:
7255 CHECKSUM_ULEB128 (DW_FORM_string);
7256 CHECKSUM_STRING (AT_string (at));
7257 break;
7259 case dw_val_class_addr:
7260 r = AT_addr (at);
7261 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7262 CHECKSUM_ULEB128 (DW_FORM_string);
7263 CHECKSUM_STRING (XSTR (r, 0));
7264 break;
7266 case dw_val_class_offset:
7267 CHECKSUM_ULEB128 (DW_FORM_sdata);
7268 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7269 break;
7271 case dw_val_class_loc:
7272 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7273 loc_checksum_ordered (loc, ctx);
7274 break;
7276 case dw_val_class_fde_ref:
7277 case dw_val_class_symview:
7278 case dw_val_class_lbl_id:
7279 case dw_val_class_lineptr:
7280 case dw_val_class_macptr:
7281 case dw_val_class_loclistsptr:
7282 case dw_val_class_high_pc:
7283 break;
7285 case dw_val_class_file:
7286 case dw_val_class_file_implicit:
7287 CHECKSUM_ULEB128 (DW_FORM_string);
7288 CHECKSUM_STRING (AT_file (at)->filename);
7289 break;
7291 case dw_val_class_data8:
7292 CHECKSUM (at->dw_attr_val.v.val_data8);
7293 break;
7295 default:
7296 break;
7300 struct checksum_attributes
7302 dw_attr_node *at_name;
7303 dw_attr_node *at_type;
7304 dw_attr_node *at_friend;
7305 dw_attr_node *at_accessibility;
7306 dw_attr_node *at_address_class;
7307 dw_attr_node *at_alignment;
7308 dw_attr_node *at_allocated;
7309 dw_attr_node *at_artificial;
7310 dw_attr_node *at_associated;
7311 dw_attr_node *at_binary_scale;
7312 dw_attr_node *at_bit_offset;
7313 dw_attr_node *at_bit_size;
7314 dw_attr_node *at_bit_stride;
7315 dw_attr_node *at_byte_size;
7316 dw_attr_node *at_byte_stride;
7317 dw_attr_node *at_const_value;
7318 dw_attr_node *at_containing_type;
7319 dw_attr_node *at_count;
7320 dw_attr_node *at_data_location;
7321 dw_attr_node *at_data_member_location;
7322 dw_attr_node *at_decimal_scale;
7323 dw_attr_node *at_decimal_sign;
7324 dw_attr_node *at_default_value;
7325 dw_attr_node *at_digit_count;
7326 dw_attr_node *at_discr;
7327 dw_attr_node *at_discr_list;
7328 dw_attr_node *at_discr_value;
7329 dw_attr_node *at_encoding;
7330 dw_attr_node *at_endianity;
7331 dw_attr_node *at_explicit;
7332 dw_attr_node *at_is_optional;
7333 dw_attr_node *at_location;
7334 dw_attr_node *at_lower_bound;
7335 dw_attr_node *at_mutable;
7336 dw_attr_node *at_ordering;
7337 dw_attr_node *at_picture_string;
7338 dw_attr_node *at_prototyped;
7339 dw_attr_node *at_small;
7340 dw_attr_node *at_segment;
7341 dw_attr_node *at_string_length;
7342 dw_attr_node *at_string_length_bit_size;
7343 dw_attr_node *at_string_length_byte_size;
7344 dw_attr_node *at_threads_scaled;
7345 dw_attr_node *at_upper_bound;
7346 dw_attr_node *at_use_location;
7347 dw_attr_node *at_use_UTF8;
7348 dw_attr_node *at_variable_parameter;
7349 dw_attr_node *at_virtuality;
7350 dw_attr_node *at_visibility;
7351 dw_attr_node *at_vtable_elem_location;
7354 /* Collect the attributes that we will want to use for the checksum. */
7356 static void
7357 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7359 dw_attr_node *a;
7360 unsigned ix;
7362 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7364 switch (a->dw_attr)
7366 case DW_AT_name:
7367 attrs->at_name = a;
7368 break;
7369 case DW_AT_type:
7370 attrs->at_type = a;
7371 break;
7372 case DW_AT_friend:
7373 attrs->at_friend = a;
7374 break;
7375 case DW_AT_accessibility:
7376 attrs->at_accessibility = a;
7377 break;
7378 case DW_AT_address_class:
7379 attrs->at_address_class = a;
7380 break;
7381 case DW_AT_alignment:
7382 attrs->at_alignment = a;
7383 break;
7384 case DW_AT_allocated:
7385 attrs->at_allocated = a;
7386 break;
7387 case DW_AT_artificial:
7388 attrs->at_artificial = a;
7389 break;
7390 case DW_AT_associated:
7391 attrs->at_associated = a;
7392 break;
7393 case DW_AT_binary_scale:
7394 attrs->at_binary_scale = a;
7395 break;
7396 case DW_AT_bit_offset:
7397 attrs->at_bit_offset = a;
7398 break;
7399 case DW_AT_bit_size:
7400 attrs->at_bit_size = a;
7401 break;
7402 case DW_AT_bit_stride:
7403 attrs->at_bit_stride = a;
7404 break;
7405 case DW_AT_byte_size:
7406 attrs->at_byte_size = a;
7407 break;
7408 case DW_AT_byte_stride:
7409 attrs->at_byte_stride = a;
7410 break;
7411 case DW_AT_const_value:
7412 attrs->at_const_value = a;
7413 break;
7414 case DW_AT_containing_type:
7415 attrs->at_containing_type = a;
7416 break;
7417 case DW_AT_count:
7418 attrs->at_count = a;
7419 break;
7420 case DW_AT_data_location:
7421 attrs->at_data_location = a;
7422 break;
7423 case DW_AT_data_member_location:
7424 attrs->at_data_member_location = a;
7425 break;
7426 case DW_AT_decimal_scale:
7427 attrs->at_decimal_scale = a;
7428 break;
7429 case DW_AT_decimal_sign:
7430 attrs->at_decimal_sign = a;
7431 break;
7432 case DW_AT_default_value:
7433 attrs->at_default_value = a;
7434 break;
7435 case DW_AT_digit_count:
7436 attrs->at_digit_count = a;
7437 break;
7438 case DW_AT_discr:
7439 attrs->at_discr = a;
7440 break;
7441 case DW_AT_discr_list:
7442 attrs->at_discr_list = a;
7443 break;
7444 case DW_AT_discr_value:
7445 attrs->at_discr_value = a;
7446 break;
7447 case DW_AT_encoding:
7448 attrs->at_encoding = a;
7449 break;
7450 case DW_AT_endianity:
7451 attrs->at_endianity = a;
7452 break;
7453 case DW_AT_explicit:
7454 attrs->at_explicit = a;
7455 break;
7456 case DW_AT_is_optional:
7457 attrs->at_is_optional = a;
7458 break;
7459 case DW_AT_location:
7460 attrs->at_location = a;
7461 break;
7462 case DW_AT_lower_bound:
7463 attrs->at_lower_bound = a;
7464 break;
7465 case DW_AT_mutable:
7466 attrs->at_mutable = a;
7467 break;
7468 case DW_AT_ordering:
7469 attrs->at_ordering = a;
7470 break;
7471 case DW_AT_picture_string:
7472 attrs->at_picture_string = a;
7473 break;
7474 case DW_AT_prototyped:
7475 attrs->at_prototyped = a;
7476 break;
7477 case DW_AT_small:
7478 attrs->at_small = a;
7479 break;
7480 case DW_AT_segment:
7481 attrs->at_segment = a;
7482 break;
7483 case DW_AT_string_length:
7484 attrs->at_string_length = a;
7485 break;
7486 case DW_AT_string_length_bit_size:
7487 attrs->at_string_length_bit_size = a;
7488 break;
7489 case DW_AT_string_length_byte_size:
7490 attrs->at_string_length_byte_size = a;
7491 break;
7492 case DW_AT_threads_scaled:
7493 attrs->at_threads_scaled = a;
7494 break;
7495 case DW_AT_upper_bound:
7496 attrs->at_upper_bound = a;
7497 break;
7498 case DW_AT_use_location:
7499 attrs->at_use_location = a;
7500 break;
7501 case DW_AT_use_UTF8:
7502 attrs->at_use_UTF8 = a;
7503 break;
7504 case DW_AT_variable_parameter:
7505 attrs->at_variable_parameter = a;
7506 break;
7507 case DW_AT_virtuality:
7508 attrs->at_virtuality = a;
7509 break;
7510 case DW_AT_visibility:
7511 attrs->at_visibility = a;
7512 break;
7513 case DW_AT_vtable_elem_location:
7514 attrs->at_vtable_elem_location = a;
7515 break;
7516 default:
7517 break;
7522 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7524 static void
7525 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7527 dw_die_ref c;
7528 dw_die_ref decl;
7529 struct checksum_attributes attrs;
7531 CHECKSUM_ULEB128 ('D');
7532 CHECKSUM_ULEB128 (die->die_tag);
7534 memset (&attrs, 0, sizeof (attrs));
7536 decl = get_AT_ref (die, DW_AT_specification);
7537 if (decl != NULL)
7538 collect_checksum_attributes (&attrs, decl);
7539 collect_checksum_attributes (&attrs, die);
7541 CHECKSUM_ATTR (attrs.at_name);
7542 CHECKSUM_ATTR (attrs.at_accessibility);
7543 CHECKSUM_ATTR (attrs.at_address_class);
7544 CHECKSUM_ATTR (attrs.at_allocated);
7545 CHECKSUM_ATTR (attrs.at_artificial);
7546 CHECKSUM_ATTR (attrs.at_associated);
7547 CHECKSUM_ATTR (attrs.at_binary_scale);
7548 CHECKSUM_ATTR (attrs.at_bit_offset);
7549 CHECKSUM_ATTR (attrs.at_bit_size);
7550 CHECKSUM_ATTR (attrs.at_bit_stride);
7551 CHECKSUM_ATTR (attrs.at_byte_size);
7552 CHECKSUM_ATTR (attrs.at_byte_stride);
7553 CHECKSUM_ATTR (attrs.at_const_value);
7554 CHECKSUM_ATTR (attrs.at_containing_type);
7555 CHECKSUM_ATTR (attrs.at_count);
7556 CHECKSUM_ATTR (attrs.at_data_location);
7557 CHECKSUM_ATTR (attrs.at_data_member_location);
7558 CHECKSUM_ATTR (attrs.at_decimal_scale);
7559 CHECKSUM_ATTR (attrs.at_decimal_sign);
7560 CHECKSUM_ATTR (attrs.at_default_value);
7561 CHECKSUM_ATTR (attrs.at_digit_count);
7562 CHECKSUM_ATTR (attrs.at_discr);
7563 CHECKSUM_ATTR (attrs.at_discr_list);
7564 CHECKSUM_ATTR (attrs.at_discr_value);
7565 CHECKSUM_ATTR (attrs.at_encoding);
7566 CHECKSUM_ATTR (attrs.at_endianity);
7567 CHECKSUM_ATTR (attrs.at_explicit);
7568 CHECKSUM_ATTR (attrs.at_is_optional);
7569 CHECKSUM_ATTR (attrs.at_location);
7570 CHECKSUM_ATTR (attrs.at_lower_bound);
7571 CHECKSUM_ATTR (attrs.at_mutable);
7572 CHECKSUM_ATTR (attrs.at_ordering);
7573 CHECKSUM_ATTR (attrs.at_picture_string);
7574 CHECKSUM_ATTR (attrs.at_prototyped);
7575 CHECKSUM_ATTR (attrs.at_small);
7576 CHECKSUM_ATTR (attrs.at_segment);
7577 CHECKSUM_ATTR (attrs.at_string_length);
7578 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7579 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7580 CHECKSUM_ATTR (attrs.at_threads_scaled);
7581 CHECKSUM_ATTR (attrs.at_upper_bound);
7582 CHECKSUM_ATTR (attrs.at_use_location);
7583 CHECKSUM_ATTR (attrs.at_use_UTF8);
7584 CHECKSUM_ATTR (attrs.at_variable_parameter);
7585 CHECKSUM_ATTR (attrs.at_virtuality);
7586 CHECKSUM_ATTR (attrs.at_visibility);
7587 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7588 CHECKSUM_ATTR (attrs.at_type);
7589 CHECKSUM_ATTR (attrs.at_friend);
7590 CHECKSUM_ATTR (attrs.at_alignment);
7592 /* Checksum the child DIEs. */
7593 c = die->die_child;
7594 if (c) do {
7595 dw_attr_node *name_attr;
7597 c = c->die_sib;
7598 name_attr = get_AT (c, DW_AT_name);
7599 if (is_template_instantiation (c))
7601 /* Ignore instantiations of member type and function templates. */
7603 else if (name_attr != NULL
7604 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7606 /* Use a shallow checksum for named nested types and member
7607 functions. */
7608 CHECKSUM_ULEB128 ('S');
7609 CHECKSUM_ULEB128 (c->die_tag);
7610 CHECKSUM_STRING (AT_string (name_attr));
7612 else
7614 /* Use a deep checksum for other children. */
7615 /* Mark this DIE so it gets processed when unmarking. */
7616 if (c->die_mark == 0)
7617 c->die_mark = -1;
7618 die_checksum_ordered (c, ctx, mark);
7620 } while (c != die->die_child);
7622 CHECKSUM_ULEB128 (0);
7625 /* Add a type name and tag to a hash. */
7626 static void
7627 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7629 CHECKSUM_ULEB128 (tag);
7630 CHECKSUM_STRING (name);
7633 #undef CHECKSUM
7634 #undef CHECKSUM_STRING
7635 #undef CHECKSUM_ATTR
7636 #undef CHECKSUM_LEB128
7637 #undef CHECKSUM_ULEB128
7639 /* Generate the type signature for DIE. This is computed by generating an
7640 MD5 checksum over the DIE's tag, its relevant attributes, and its
7641 children. Attributes that are references to other DIEs are processed
7642 by recursion, using the MARK field to prevent infinite recursion.
7643 If the DIE is nested inside a namespace or another type, we also
7644 need to include that context in the signature. The lower 64 bits
7645 of the resulting MD5 checksum comprise the signature. */
7647 static void
7648 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7650 int mark;
7651 const char *name;
7652 unsigned char checksum[16];
7653 struct md5_ctx ctx;
7654 dw_die_ref decl;
7655 dw_die_ref parent;
7657 name = get_AT_string (die, DW_AT_name);
7658 decl = get_AT_ref (die, DW_AT_specification);
7659 parent = get_die_parent (die);
7661 /* First, compute a signature for just the type name (and its surrounding
7662 context, if any. This is stored in the type unit DIE for link-time
7663 ODR (one-definition rule) checking. */
7665 if (is_cxx () && name != NULL)
7667 md5_init_ctx (&ctx);
7669 /* Checksum the names of surrounding namespaces and structures. */
7670 if (parent != NULL)
7671 checksum_die_context (parent, &ctx);
7673 /* Checksum the current DIE. */
7674 die_odr_checksum (die->die_tag, name, &ctx);
7675 md5_finish_ctx (&ctx, checksum);
7677 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7680 /* Next, compute the complete type signature. */
7682 md5_init_ctx (&ctx);
7683 mark = 1;
7684 die->die_mark = mark;
7686 /* Checksum the names of surrounding namespaces and structures. */
7687 if (parent != NULL)
7688 checksum_die_context (parent, &ctx);
7690 /* Checksum the DIE and its children. */
7691 die_checksum_ordered (die, &ctx, &mark);
7692 unmark_all_dies (die);
7693 md5_finish_ctx (&ctx, checksum);
7695 /* Store the signature in the type node and link the type DIE and the
7696 type node together. */
7697 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7698 DWARF_TYPE_SIGNATURE_SIZE);
7699 die->comdat_type_p = true;
7700 die->die_id.die_type_node = type_node;
7701 type_node->type_die = die;
7703 /* If the DIE is a specification, link its declaration to the type node
7704 as well. */
7705 if (decl != NULL)
7707 decl->comdat_type_p = true;
7708 decl->die_id.die_type_node = type_node;
7712 /* Do the location expressions look same? */
7713 static inline int
7714 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7716 return loc1->dw_loc_opc == loc2->dw_loc_opc
7717 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7718 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7721 /* Do the values look the same? */
7722 static int
7723 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7725 dw_loc_descr_ref loc1, loc2;
7726 rtx r1, r2;
7728 if (v1->val_class != v2->val_class)
7729 return 0;
7731 switch (v1->val_class)
7733 case dw_val_class_const:
7734 case dw_val_class_const_implicit:
7735 return v1->v.val_int == v2->v.val_int;
7736 case dw_val_class_unsigned_const:
7737 case dw_val_class_unsigned_const_implicit:
7738 return v1->v.val_unsigned == v2->v.val_unsigned;
7739 case dw_val_class_const_double:
7740 return v1->v.val_double.high == v2->v.val_double.high
7741 && v1->v.val_double.low == v2->v.val_double.low;
7742 case dw_val_class_wide_int:
7743 return *v1->v.val_wide == *v2->v.val_wide;
7744 case dw_val_class_vec:
7745 if (v1->v.val_vec.length != v2->v.val_vec.length
7746 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7747 return 0;
7748 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7749 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7750 return 0;
7751 return 1;
7752 case dw_val_class_flag:
7753 return v1->v.val_flag == v2->v.val_flag;
7754 case dw_val_class_str:
7755 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7757 case dw_val_class_addr:
7758 r1 = v1->v.val_addr;
7759 r2 = v2->v.val_addr;
7760 if (GET_CODE (r1) != GET_CODE (r2))
7761 return 0;
7762 return !rtx_equal_p (r1, r2);
7764 case dw_val_class_offset:
7765 return v1->v.val_offset == v2->v.val_offset;
7767 case dw_val_class_loc:
7768 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7769 loc1 && loc2;
7770 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7771 if (!same_loc_p (loc1, loc2, mark))
7772 return 0;
7773 return !loc1 && !loc2;
7775 case dw_val_class_die_ref:
7776 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7778 case dw_val_class_symview:
7779 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7781 case dw_val_class_fde_ref:
7782 case dw_val_class_vms_delta:
7783 case dw_val_class_lbl_id:
7784 case dw_val_class_lineptr:
7785 case dw_val_class_macptr:
7786 case dw_val_class_loclistsptr:
7787 case dw_val_class_high_pc:
7788 return 1;
7790 case dw_val_class_file:
7791 case dw_val_class_file_implicit:
7792 return v1->v.val_file == v2->v.val_file;
7794 case dw_val_class_data8:
7795 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7797 default:
7798 return 1;
7802 /* Do the attributes look the same? */
7804 static int
7805 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7807 if (at1->dw_attr != at2->dw_attr)
7808 return 0;
7810 /* We don't care that this was compiled with a different compiler
7811 snapshot; if the output is the same, that's what matters. */
7812 if (at1->dw_attr == DW_AT_producer)
7813 return 1;
7815 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7818 /* Do the dies look the same? */
7820 static int
7821 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7823 dw_die_ref c1, c2;
7824 dw_attr_node *a1;
7825 unsigned ix;
7827 /* To avoid infinite recursion. */
7828 if (die1->die_mark)
7829 return die1->die_mark == die2->die_mark;
7830 die1->die_mark = die2->die_mark = ++(*mark);
7832 if (die1->die_tag != die2->die_tag)
7833 return 0;
7835 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7836 return 0;
7838 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7839 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7840 return 0;
7842 c1 = die1->die_child;
7843 c2 = die2->die_child;
7844 if (! c1)
7846 if (c2)
7847 return 0;
7849 else
7850 for (;;)
7852 if (!same_die_p (c1, c2, mark))
7853 return 0;
7854 c1 = c1->die_sib;
7855 c2 = c2->die_sib;
7856 if (c1 == die1->die_child)
7858 if (c2 == die2->die_child)
7859 break;
7860 else
7861 return 0;
7865 return 1;
7868 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7869 children, and set die_symbol. */
7871 static void
7872 compute_comp_unit_symbol (dw_die_ref unit_die)
7874 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7875 const char *base = die_name ? lbasename (die_name) : "anonymous";
7876 char *name = XALLOCAVEC (char, strlen (base) + 64);
7877 char *p;
7878 int i, mark;
7879 unsigned char checksum[16];
7880 struct md5_ctx ctx;
7882 /* Compute the checksum of the DIE, then append part of it as hex digits to
7883 the name filename of the unit. */
7885 md5_init_ctx (&ctx);
7886 mark = 0;
7887 die_checksum (unit_die, &ctx, &mark);
7888 unmark_all_dies (unit_die);
7889 md5_finish_ctx (&ctx, checksum);
7891 /* When we this for comp_unit_die () we have a DW_AT_name that might
7892 not start with a letter but with anything valid for filenames and
7893 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7894 character is not a letter. */
7895 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7896 clean_symbol_name (name);
7898 p = name + strlen (name);
7899 for (i = 0; i < 4; i++)
7901 sprintf (p, "%.2x", checksum[i]);
7902 p += 2;
7905 unit_die->die_id.die_symbol = xstrdup (name);
7908 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7910 static int
7911 is_type_die (dw_die_ref die)
7913 switch (die->die_tag)
7915 case DW_TAG_array_type:
7916 case DW_TAG_class_type:
7917 case DW_TAG_interface_type:
7918 case DW_TAG_enumeration_type:
7919 case DW_TAG_pointer_type:
7920 case DW_TAG_reference_type:
7921 case DW_TAG_rvalue_reference_type:
7922 case DW_TAG_string_type:
7923 case DW_TAG_structure_type:
7924 case DW_TAG_subroutine_type:
7925 case DW_TAG_union_type:
7926 case DW_TAG_ptr_to_member_type:
7927 case DW_TAG_set_type:
7928 case DW_TAG_subrange_type:
7929 case DW_TAG_base_type:
7930 case DW_TAG_const_type:
7931 case DW_TAG_file_type:
7932 case DW_TAG_packed_type:
7933 case DW_TAG_volatile_type:
7934 case DW_TAG_typedef:
7935 return 1;
7936 default:
7937 return 0;
7941 /* Returns true iff C is a compile-unit DIE. */
7943 static inline bool
7944 is_cu_die (dw_die_ref c)
7946 return c && (c->die_tag == DW_TAG_compile_unit
7947 || c->die_tag == DW_TAG_skeleton_unit);
7950 /* Returns true iff C is a unit DIE of some sort. */
7952 static inline bool
7953 is_unit_die (dw_die_ref c)
7955 return c && (c->die_tag == DW_TAG_compile_unit
7956 || c->die_tag == DW_TAG_partial_unit
7957 || c->die_tag == DW_TAG_type_unit
7958 || c->die_tag == DW_TAG_skeleton_unit);
7961 /* Returns true iff C is a namespace DIE. */
7963 static inline bool
7964 is_namespace_die (dw_die_ref c)
7966 return c && c->die_tag == DW_TAG_namespace;
7969 /* Return non-zero if this DIE is a template parameter. */
7971 static inline bool
7972 is_template_parameter (dw_die_ref die)
7974 switch (die->die_tag)
7976 case DW_TAG_template_type_param:
7977 case DW_TAG_template_value_param:
7978 case DW_TAG_GNU_template_template_param:
7979 case DW_TAG_GNU_template_parameter_pack:
7980 return true;
7981 default:
7982 return false;
7986 /* Return non-zero if this DIE represents a template instantiation. */
7988 static inline bool
7989 is_template_instantiation (dw_die_ref die)
7991 dw_die_ref c;
7993 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7994 return false;
7995 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7996 return false;
7999 static char *
8000 gen_internal_sym (const char *prefix)
8002 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8004 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8005 return xstrdup (buf);
8008 /* Return non-zero if this DIE is a declaration. */
8010 static int
8011 is_declaration_die (dw_die_ref die)
8013 dw_attr_node *a;
8014 unsigned ix;
8016 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8017 if (a->dw_attr == DW_AT_declaration)
8018 return 1;
8020 return 0;
8023 /* Return non-zero if this DIE is nested inside a subprogram. */
8025 static int
8026 is_nested_in_subprogram (dw_die_ref die)
8028 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8030 if (decl == NULL)
8031 decl = die;
8032 return local_scope_p (decl);
8035 /* Return non-zero if this DIE contains a defining declaration of a
8036 subprogram. */
8038 static int
8039 contains_subprogram_definition (dw_die_ref die)
8041 dw_die_ref c;
8043 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8044 return 1;
8045 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8046 return 0;
8049 /* Return non-zero if this is a type DIE that should be moved to a
8050 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8051 unit type. */
8053 static int
8054 should_move_die_to_comdat (dw_die_ref die)
8056 switch (die->die_tag)
8058 case DW_TAG_class_type:
8059 case DW_TAG_structure_type:
8060 case DW_TAG_enumeration_type:
8061 case DW_TAG_union_type:
8062 /* Don't move declarations, inlined instances, types nested in a
8063 subprogram, or types that contain subprogram definitions. */
8064 if (is_declaration_die (die)
8065 || get_AT (die, DW_AT_abstract_origin)
8066 || is_nested_in_subprogram (die)
8067 || contains_subprogram_definition (die))
8068 return 0;
8069 return 1;
8070 case DW_TAG_array_type:
8071 case DW_TAG_interface_type:
8072 case DW_TAG_pointer_type:
8073 case DW_TAG_reference_type:
8074 case DW_TAG_rvalue_reference_type:
8075 case DW_TAG_string_type:
8076 case DW_TAG_subroutine_type:
8077 case DW_TAG_ptr_to_member_type:
8078 case DW_TAG_set_type:
8079 case DW_TAG_subrange_type:
8080 case DW_TAG_base_type:
8081 case DW_TAG_const_type:
8082 case DW_TAG_file_type:
8083 case DW_TAG_packed_type:
8084 case DW_TAG_volatile_type:
8085 case DW_TAG_typedef:
8086 default:
8087 return 0;
8091 /* Make a clone of DIE. */
8093 static dw_die_ref
8094 clone_die (dw_die_ref die)
8096 dw_die_ref clone = new_die_raw (die->die_tag);
8097 dw_attr_node *a;
8098 unsigned ix;
8100 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8101 add_dwarf_attr (clone, a);
8103 return clone;
8106 /* Make a clone of the tree rooted at DIE. */
8108 static dw_die_ref
8109 clone_tree (dw_die_ref die)
8111 dw_die_ref c;
8112 dw_die_ref clone = clone_die (die);
8114 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8116 return clone;
8119 /* Make a clone of DIE as a declaration. */
8121 static dw_die_ref
8122 clone_as_declaration (dw_die_ref die)
8124 dw_die_ref clone;
8125 dw_die_ref decl;
8126 dw_attr_node *a;
8127 unsigned ix;
8129 /* If the DIE is already a declaration, just clone it. */
8130 if (is_declaration_die (die))
8131 return clone_die (die);
8133 /* If the DIE is a specification, just clone its declaration DIE. */
8134 decl = get_AT_ref (die, DW_AT_specification);
8135 if (decl != NULL)
8137 clone = clone_die (decl);
8138 if (die->comdat_type_p)
8139 add_AT_die_ref (clone, DW_AT_signature, die);
8140 return clone;
8143 clone = new_die_raw (die->die_tag);
8145 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8147 /* We don't want to copy over all attributes.
8148 For example we don't want DW_AT_byte_size because otherwise we will no
8149 longer have a declaration and GDB will treat it as a definition. */
8151 switch (a->dw_attr)
8153 case DW_AT_abstract_origin:
8154 case DW_AT_artificial:
8155 case DW_AT_containing_type:
8156 case DW_AT_external:
8157 case DW_AT_name:
8158 case DW_AT_type:
8159 case DW_AT_virtuality:
8160 case DW_AT_linkage_name:
8161 case DW_AT_MIPS_linkage_name:
8162 add_dwarf_attr (clone, a);
8163 break;
8164 case DW_AT_byte_size:
8165 case DW_AT_alignment:
8166 default:
8167 break;
8171 if (die->comdat_type_p)
8172 add_AT_die_ref (clone, DW_AT_signature, die);
8174 add_AT_flag (clone, DW_AT_declaration, 1);
8175 return clone;
8179 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8181 struct decl_table_entry
8183 dw_die_ref orig;
8184 dw_die_ref copy;
8187 /* Helpers to manipulate hash table of copied declarations. */
8189 /* Hashtable helpers. */
8191 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8193 typedef die_struct *compare_type;
8194 static inline hashval_t hash (const decl_table_entry *);
8195 static inline bool equal (const decl_table_entry *, const die_struct *);
8198 inline hashval_t
8199 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8201 return htab_hash_pointer (entry->orig);
8204 inline bool
8205 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8206 const die_struct *entry2)
8208 return entry1->orig == entry2;
8211 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8213 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8214 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8215 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8216 to check if the ancestor has already been copied into UNIT. */
8218 static dw_die_ref
8219 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8220 decl_hash_type *decl_table)
8222 dw_die_ref parent = die->die_parent;
8223 dw_die_ref new_parent = unit;
8224 dw_die_ref copy;
8225 decl_table_entry **slot = NULL;
8226 struct decl_table_entry *entry = NULL;
8228 /* If DIE refers to a stub unfold that so we get the appropriate
8229 DIE registered as orig in decl_table. */
8230 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8231 die = c;
8233 if (decl_table)
8235 /* Check if the entry has already been copied to UNIT. */
8236 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8237 INSERT);
8238 if (*slot != HTAB_EMPTY_ENTRY)
8240 entry = *slot;
8241 return entry->copy;
8244 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8245 entry = XCNEW (struct decl_table_entry);
8246 entry->orig = die;
8247 entry->copy = NULL;
8248 *slot = entry;
8251 if (parent != NULL)
8253 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8254 if (spec != NULL)
8255 parent = spec;
8256 if (!is_unit_die (parent))
8257 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8260 copy = clone_as_declaration (die);
8261 add_child_die (new_parent, copy);
8263 if (decl_table)
8265 /* Record the pointer to the copy. */
8266 entry->copy = copy;
8269 return copy;
8271 /* Copy the declaration context to the new type unit DIE. This includes
8272 any surrounding namespace or type declarations. If the DIE has an
8273 AT_specification attribute, it also includes attributes and children
8274 attached to the specification, and returns a pointer to the original
8275 parent of the declaration DIE. Returns NULL otherwise. */
8277 static dw_die_ref
8278 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8280 dw_die_ref decl;
8281 dw_die_ref new_decl;
8282 dw_die_ref orig_parent = NULL;
8284 decl = get_AT_ref (die, DW_AT_specification);
8285 if (decl == NULL)
8286 decl = die;
8287 else
8289 unsigned ix;
8290 dw_die_ref c;
8291 dw_attr_node *a;
8293 /* The original DIE will be changed to a declaration, and must
8294 be moved to be a child of the original declaration DIE. */
8295 orig_parent = decl->die_parent;
8297 /* Copy the type node pointer from the new DIE to the original
8298 declaration DIE so we can forward references later. */
8299 decl->comdat_type_p = true;
8300 decl->die_id.die_type_node = die->die_id.die_type_node;
8302 remove_AT (die, DW_AT_specification);
8304 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8306 if (a->dw_attr != DW_AT_name
8307 && a->dw_attr != DW_AT_declaration
8308 && a->dw_attr != DW_AT_external)
8309 add_dwarf_attr (die, a);
8312 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8315 if (decl->die_parent != NULL
8316 && !is_unit_die (decl->die_parent))
8318 new_decl = copy_ancestor_tree (unit, decl, NULL);
8319 if (new_decl != NULL)
8321 remove_AT (new_decl, DW_AT_signature);
8322 add_AT_specification (die, new_decl);
8326 return orig_parent;
8329 /* Generate the skeleton ancestor tree for the given NODE, then clone
8330 the DIE and add the clone into the tree. */
8332 static void
8333 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8335 if (node->new_die != NULL)
8336 return;
8338 node->new_die = clone_as_declaration (node->old_die);
8340 if (node->parent != NULL)
8342 generate_skeleton_ancestor_tree (node->parent);
8343 add_child_die (node->parent->new_die, node->new_die);
8347 /* Generate a skeleton tree of DIEs containing any declarations that are
8348 found in the original tree. We traverse the tree looking for declaration
8349 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8351 static void
8352 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8354 skeleton_chain_node node;
8355 dw_die_ref c;
8356 dw_die_ref first;
8357 dw_die_ref prev = NULL;
8358 dw_die_ref next = NULL;
8360 node.parent = parent;
8362 first = c = parent->old_die->die_child;
8363 if (c)
8364 next = c->die_sib;
8365 if (c) do {
8366 if (prev == NULL || prev->die_sib == c)
8367 prev = c;
8368 c = next;
8369 next = (c == first ? NULL : c->die_sib);
8370 node.old_die = c;
8371 node.new_die = NULL;
8372 if (is_declaration_die (c))
8374 if (is_template_instantiation (c))
8376 /* Instantiated templates do not need to be cloned into the
8377 type unit. Just move the DIE and its children back to
8378 the skeleton tree (in the main CU). */
8379 remove_child_with_prev (c, prev);
8380 add_child_die (parent->new_die, c);
8381 c = prev;
8383 else if (c->comdat_type_p)
8385 /* This is the skeleton of earlier break_out_comdat_types
8386 type. Clone the existing DIE, but keep the children
8387 under the original (which is in the main CU). */
8388 dw_die_ref clone = clone_die (c);
8390 replace_child (c, clone, prev);
8391 generate_skeleton_ancestor_tree (parent);
8392 add_child_die (parent->new_die, c);
8393 c = clone;
8394 continue;
8396 else
8398 /* Clone the existing DIE, move the original to the skeleton
8399 tree (which is in the main CU), and put the clone, with
8400 all the original's children, where the original came from
8401 (which is about to be moved to the type unit). */
8402 dw_die_ref clone = clone_die (c);
8403 move_all_children (c, clone);
8405 /* If the original has a DW_AT_object_pointer attribute,
8406 it would now point to a child DIE just moved to the
8407 cloned tree, so we need to remove that attribute from
8408 the original. */
8409 remove_AT (c, DW_AT_object_pointer);
8411 replace_child (c, clone, prev);
8412 generate_skeleton_ancestor_tree (parent);
8413 add_child_die (parent->new_die, c);
8414 node.old_die = clone;
8415 node.new_die = c;
8416 c = clone;
8419 generate_skeleton_bottom_up (&node);
8420 } while (next != NULL);
8423 /* Wrapper function for generate_skeleton_bottom_up. */
8425 static dw_die_ref
8426 generate_skeleton (dw_die_ref die)
8428 skeleton_chain_node node;
8430 node.old_die = die;
8431 node.new_die = NULL;
8432 node.parent = NULL;
8434 /* If this type definition is nested inside another type,
8435 and is not an instantiation of a template, always leave
8436 at least a declaration in its place. */
8437 if (die->die_parent != NULL
8438 && is_type_die (die->die_parent)
8439 && !is_template_instantiation (die))
8440 node.new_die = clone_as_declaration (die);
8442 generate_skeleton_bottom_up (&node);
8443 return node.new_die;
8446 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8447 declaration. The original DIE is moved to a new compile unit so that
8448 existing references to it follow it to the new location. If any of the
8449 original DIE's descendants is a declaration, we need to replace the
8450 original DIE with a skeleton tree and move the declarations back into the
8451 skeleton tree. */
8453 static dw_die_ref
8454 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8455 dw_die_ref prev)
8457 dw_die_ref skeleton, orig_parent;
8459 /* Copy the declaration context to the type unit DIE. If the returned
8460 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8461 that DIE. */
8462 orig_parent = copy_declaration_context (unit, child);
8464 skeleton = generate_skeleton (child);
8465 if (skeleton == NULL)
8466 remove_child_with_prev (child, prev);
8467 else
8469 skeleton->comdat_type_p = true;
8470 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8472 /* If the original DIE was a specification, we need to put
8473 the skeleton under the parent DIE of the declaration.
8474 This leaves the original declaration in the tree, but
8475 it will be pruned later since there are no longer any
8476 references to it. */
8477 if (orig_parent != NULL)
8479 remove_child_with_prev (child, prev);
8480 add_child_die (orig_parent, skeleton);
8482 else
8483 replace_child (child, skeleton, prev);
8486 return skeleton;
8489 static void
8490 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8491 comdat_type_node *type_node,
8492 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8494 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8495 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8496 DWARF procedure references in the DW_AT_location attribute. */
8498 static dw_die_ref
8499 copy_dwarf_procedure (dw_die_ref die,
8500 comdat_type_node *type_node,
8501 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8503 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8505 /* DWARF procedures are not supposed to have children... */
8506 gcc_assert (die->die_child == NULL);
8508 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8509 gcc_assert (vec_safe_length (die->die_attr) == 1
8510 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8512 /* Do not copy more than once DWARF procedures. */
8513 bool existed;
8514 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8515 if (existed)
8516 return die_copy;
8518 die_copy = clone_die (die);
8519 add_child_die (type_node->root_die, die_copy);
8520 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8521 return die_copy;
8524 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8525 procedures in DIE's attributes. */
8527 static void
8528 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8529 comdat_type_node *type_node,
8530 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8532 dw_attr_node *a;
8533 unsigned i;
8535 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8537 dw_loc_descr_ref loc;
8539 if (a->dw_attr_val.val_class != dw_val_class_loc)
8540 continue;
8542 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8544 switch (loc->dw_loc_opc)
8546 case DW_OP_call2:
8547 case DW_OP_call4:
8548 case DW_OP_call_ref:
8549 gcc_assert (loc->dw_loc_oprnd1.val_class
8550 == dw_val_class_die_ref);
8551 loc->dw_loc_oprnd1.v.val_die_ref.die
8552 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8553 type_node,
8554 copied_dwarf_procs);
8556 default:
8557 break;
8563 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8564 rewrite references to point to the copies.
8566 References are looked for in DIE's attributes and recursively in all its
8567 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8568 mapping from old DWARF procedures to their copy. It is used not to copy
8569 twice the same DWARF procedure under TYPE_NODE. */
8571 static void
8572 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8573 comdat_type_node *type_node,
8574 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8576 dw_die_ref c;
8578 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8579 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8580 type_node,
8581 copied_dwarf_procs));
8584 /* Traverse the DIE and set up additional .debug_types or .debug_info
8585 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8586 section. */
8588 static void
8589 break_out_comdat_types (dw_die_ref die)
8591 dw_die_ref c;
8592 dw_die_ref first;
8593 dw_die_ref prev = NULL;
8594 dw_die_ref next = NULL;
8595 dw_die_ref unit = NULL;
8597 first = c = die->die_child;
8598 if (c)
8599 next = c->die_sib;
8600 if (c) do {
8601 if (prev == NULL || prev->die_sib == c)
8602 prev = c;
8603 c = next;
8604 next = (c == first ? NULL : c->die_sib);
8605 if (should_move_die_to_comdat (c))
8607 dw_die_ref replacement;
8608 comdat_type_node *type_node;
8610 /* Break out nested types into their own type units. */
8611 break_out_comdat_types (c);
8613 /* Create a new type unit DIE as the root for the new tree. */
8614 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8615 add_AT_unsigned (unit, DW_AT_language,
8616 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8618 /* Add the new unit's type DIE into the comdat type list. */
8619 type_node = ggc_cleared_alloc<comdat_type_node> ();
8620 type_node->root_die = unit;
8621 type_node->next = comdat_type_list;
8622 comdat_type_list = type_node;
8624 /* Generate the type signature. */
8625 generate_type_signature (c, type_node);
8627 /* Copy the declaration context, attributes, and children of the
8628 declaration into the new type unit DIE, then remove this DIE
8629 from the main CU (or replace it with a skeleton if necessary). */
8630 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8631 type_node->skeleton_die = replacement;
8633 /* Add the DIE to the new compunit. */
8634 add_child_die (unit, c);
8636 /* Types can reference DWARF procedures for type size or data location
8637 expressions. Calls in DWARF expressions cannot target procedures
8638 that are not in the same section. So we must copy DWARF procedures
8639 along with this type and then rewrite references to them. */
8640 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8641 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8643 if (replacement != NULL)
8644 c = replacement;
8646 else if (c->die_tag == DW_TAG_namespace
8647 || c->die_tag == DW_TAG_class_type
8648 || c->die_tag == DW_TAG_structure_type
8649 || c->die_tag == DW_TAG_union_type)
8651 /* Look for nested types that can be broken out. */
8652 break_out_comdat_types (c);
8654 } while (next != NULL);
8657 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8658 Enter all the cloned children into the hash table decl_table. */
8660 static dw_die_ref
8661 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8663 dw_die_ref c;
8664 dw_die_ref clone;
8665 struct decl_table_entry *entry;
8666 decl_table_entry **slot;
8668 if (die->die_tag == DW_TAG_subprogram)
8669 clone = clone_as_declaration (die);
8670 else
8671 clone = clone_die (die);
8673 slot = decl_table->find_slot_with_hash (die,
8674 htab_hash_pointer (die), INSERT);
8676 /* Assert that DIE isn't in the hash table yet. If it would be there
8677 before, the ancestors would be necessarily there as well, therefore
8678 clone_tree_partial wouldn't be called. */
8679 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8681 entry = XCNEW (struct decl_table_entry);
8682 entry->orig = die;
8683 entry->copy = clone;
8684 *slot = entry;
8686 if (die->die_tag != DW_TAG_subprogram)
8687 FOR_EACH_CHILD (die, c,
8688 add_child_die (clone, clone_tree_partial (c, decl_table)));
8690 return clone;
8693 /* Walk the DIE and its children, looking for references to incomplete
8694 or trivial types that are unmarked (i.e., that are not in the current
8695 type_unit). */
8697 static void
8698 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8700 dw_die_ref c;
8701 dw_attr_node *a;
8702 unsigned ix;
8704 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8706 if (AT_class (a) == dw_val_class_die_ref)
8708 dw_die_ref targ = AT_ref (a);
8709 decl_table_entry **slot;
8710 struct decl_table_entry *entry;
8712 if (targ->die_mark != 0 || targ->comdat_type_p)
8713 continue;
8715 slot = decl_table->find_slot_with_hash (targ,
8716 htab_hash_pointer (targ),
8717 INSERT);
8719 if (*slot != HTAB_EMPTY_ENTRY)
8721 /* TARG has already been copied, so we just need to
8722 modify the reference to point to the copy. */
8723 entry = *slot;
8724 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8726 else
8728 dw_die_ref parent = unit;
8729 dw_die_ref copy = clone_die (targ);
8731 /* Record in DECL_TABLE that TARG has been copied.
8732 Need to do this now, before the recursive call,
8733 because DECL_TABLE may be expanded and SLOT
8734 would no longer be a valid pointer. */
8735 entry = XCNEW (struct decl_table_entry);
8736 entry->orig = targ;
8737 entry->copy = copy;
8738 *slot = entry;
8740 /* If TARG is not a declaration DIE, we need to copy its
8741 children. */
8742 if (!is_declaration_die (targ))
8744 FOR_EACH_CHILD (
8745 targ, c,
8746 add_child_die (copy,
8747 clone_tree_partial (c, decl_table)));
8750 /* Make sure the cloned tree is marked as part of the
8751 type unit. */
8752 mark_dies (copy);
8754 /* If TARG has surrounding context, copy its ancestor tree
8755 into the new type unit. */
8756 if (targ->die_parent != NULL
8757 && !is_unit_die (targ->die_parent))
8758 parent = copy_ancestor_tree (unit, targ->die_parent,
8759 decl_table);
8761 add_child_die (parent, copy);
8762 a->dw_attr_val.v.val_die_ref.die = copy;
8764 /* Make sure the newly-copied DIE is walked. If it was
8765 installed in a previously-added context, it won't
8766 get visited otherwise. */
8767 if (parent != unit)
8769 /* Find the highest point of the newly-added tree,
8770 mark each node along the way, and walk from there. */
8771 parent->die_mark = 1;
8772 while (parent->die_parent
8773 && parent->die_parent->die_mark == 0)
8775 parent = parent->die_parent;
8776 parent->die_mark = 1;
8778 copy_decls_walk (unit, parent, decl_table);
8784 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8787 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8788 and record them in DECL_TABLE. */
8790 static void
8791 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8793 dw_die_ref c;
8795 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8797 dw_die_ref targ = AT_ref (a);
8798 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8799 decl_table_entry **slot
8800 = decl_table->find_slot_with_hash (targ,
8801 htab_hash_pointer (targ),
8802 INSERT);
8803 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8804 /* Record in DECL_TABLE that TARG has been already copied
8805 by remove_child_or_replace_with_skeleton. */
8806 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8807 entry->orig = targ;
8808 entry->copy = die;
8809 *slot = entry;
8811 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8814 /* Copy declarations for "unworthy" types into the new comdat section.
8815 Incomplete types, modified types, and certain other types aren't broken
8816 out into comdat sections of their own, so they don't have a signature,
8817 and we need to copy the declaration into the same section so that we
8818 don't have an external reference. */
8820 static void
8821 copy_decls_for_unworthy_types (dw_die_ref unit)
8823 mark_dies (unit);
8824 decl_hash_type decl_table (10);
8825 collect_skeleton_dies (unit, &decl_table);
8826 copy_decls_walk (unit, unit, &decl_table);
8827 unmark_dies (unit);
8830 /* Traverse the DIE and add a sibling attribute if it may have the
8831 effect of speeding up access to siblings. To save some space,
8832 avoid generating sibling attributes for DIE's without children. */
8834 static void
8835 add_sibling_attributes (dw_die_ref die)
8837 dw_die_ref c;
8839 if (! die->die_child)
8840 return;
8842 if (die->die_parent && die != die->die_parent->die_child)
8843 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8845 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8848 /* Output all location lists for the DIE and its children. */
8850 static void
8851 output_location_lists (dw_die_ref die)
8853 dw_die_ref c;
8854 dw_attr_node *a;
8855 unsigned ix;
8857 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8858 if (AT_class (a) == dw_val_class_loc_list)
8859 output_loc_list (AT_loc_list (a));
8861 FOR_EACH_CHILD (die, c, output_location_lists (c));
8864 /* During assign_location_list_indexes and output_loclists_offset the
8865 current index, after it the number of assigned indexes (i.e. how
8866 large the .debug_loclists* offset table should be). */
8867 static unsigned int loc_list_idx;
8869 /* Output all location list offsets for the DIE and its children. */
8871 static void
8872 output_loclists_offsets (dw_die_ref die)
8874 dw_die_ref c;
8875 dw_attr_node *a;
8876 unsigned ix;
8878 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8879 if (AT_class (a) == dw_val_class_loc_list)
8881 dw_loc_list_ref l = AT_loc_list (a);
8882 if (l->offset_emitted)
8883 continue;
8884 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8885 loc_section_label, NULL);
8886 gcc_assert (l->hash == loc_list_idx);
8887 loc_list_idx++;
8888 l->offset_emitted = true;
8891 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8894 /* Recursively set indexes of location lists. */
8896 static void
8897 assign_location_list_indexes (dw_die_ref die)
8899 dw_die_ref c;
8900 dw_attr_node *a;
8901 unsigned ix;
8903 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8904 if (AT_class (a) == dw_val_class_loc_list)
8906 dw_loc_list_ref list = AT_loc_list (a);
8907 if (!list->num_assigned)
8909 list->num_assigned = true;
8910 list->hash = loc_list_idx++;
8914 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8917 /* We want to limit the number of external references, because they are
8918 larger than local references: a relocation takes multiple words, and
8919 even a sig8 reference is always eight bytes, whereas a local reference
8920 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8921 So if we encounter multiple external references to the same type DIE, we
8922 make a local typedef stub for it and redirect all references there.
8924 This is the element of the hash table for keeping track of these
8925 references. */
8927 struct external_ref
8929 dw_die_ref type;
8930 dw_die_ref stub;
8931 unsigned n_refs;
8934 /* Hashtable helpers. */
8936 struct external_ref_hasher : free_ptr_hash <external_ref>
8938 static inline hashval_t hash (const external_ref *);
8939 static inline bool equal (const external_ref *, const external_ref *);
8942 inline hashval_t
8943 external_ref_hasher::hash (const external_ref *r)
8945 dw_die_ref die = r->type;
8946 hashval_t h = 0;
8948 /* We can't use the address of the DIE for hashing, because
8949 that will make the order of the stub DIEs non-deterministic. */
8950 if (! die->comdat_type_p)
8951 /* We have a symbol; use it to compute a hash. */
8952 h = htab_hash_string (die->die_id.die_symbol);
8953 else
8955 /* We have a type signature; use a subset of the bits as the hash.
8956 The 8-byte signature is at least as large as hashval_t. */
8957 comdat_type_node *type_node = die->die_id.die_type_node;
8958 memcpy (&h, type_node->signature, sizeof (h));
8960 return h;
8963 inline bool
8964 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8966 return r1->type == r2->type;
8969 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8971 /* Return a pointer to the external_ref for references to DIE. */
8973 static struct external_ref *
8974 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8976 struct external_ref ref, *ref_p;
8977 external_ref **slot;
8979 ref.type = die;
8980 slot = map->find_slot (&ref, INSERT);
8981 if (*slot != HTAB_EMPTY_ENTRY)
8982 return *slot;
8984 ref_p = XCNEW (struct external_ref);
8985 ref_p->type = die;
8986 *slot = ref_p;
8987 return ref_p;
8990 /* Subroutine of optimize_external_refs, below.
8992 If we see a type skeleton, record it as our stub. If we see external
8993 references, remember how many we've seen. */
8995 static void
8996 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8998 dw_die_ref c;
8999 dw_attr_node *a;
9000 unsigned ix;
9001 struct external_ref *ref_p;
9003 if (is_type_die (die)
9004 && (c = get_AT_ref (die, DW_AT_signature)))
9006 /* This is a local skeleton; use it for local references. */
9007 ref_p = lookup_external_ref (map, c);
9008 ref_p->stub = die;
9011 /* Scan the DIE references, and remember any that refer to DIEs from
9012 other CUs (i.e. those which are not marked). */
9013 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9014 if (AT_class (a) == dw_val_class_die_ref
9015 && (c = AT_ref (a))->die_mark == 0
9016 && is_type_die (c))
9018 ref_p = lookup_external_ref (map, c);
9019 ref_p->n_refs++;
9022 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9025 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9026 points to an external_ref, DATA is the CU we're processing. If we don't
9027 already have a local stub, and we have multiple refs, build a stub. */
9030 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9032 struct external_ref *ref_p = *slot;
9034 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9036 /* We have multiple references to this type, so build a small stub.
9037 Both of these forms are a bit dodgy from the perspective of the
9038 DWARF standard, since technically they should have names. */
9039 dw_die_ref cu = data;
9040 dw_die_ref type = ref_p->type;
9041 dw_die_ref stub = NULL;
9043 if (type->comdat_type_p)
9045 /* If we refer to this type via sig8, use AT_signature. */
9046 stub = new_die (type->die_tag, cu, NULL_TREE);
9047 add_AT_die_ref (stub, DW_AT_signature, type);
9049 else
9051 /* Otherwise, use a typedef with no name. */
9052 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9053 add_AT_die_ref (stub, DW_AT_type, type);
9056 stub->die_mark++;
9057 ref_p->stub = stub;
9059 return 1;
9062 /* DIE is a unit; look through all the DIE references to see if there are
9063 any external references to types, and if so, create local stubs for
9064 them which will be applied in build_abbrev_table. This is useful because
9065 references to local DIEs are smaller. */
9067 static external_ref_hash_type *
9068 optimize_external_refs (dw_die_ref die)
9070 external_ref_hash_type *map = new external_ref_hash_type (10);
9071 optimize_external_refs_1 (die, map);
9072 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9073 return map;
9076 /* The following 3 variables are temporaries that are computed only during the
9077 build_abbrev_table call and used and released during the following
9078 optimize_abbrev_table call. */
9080 /* First abbrev_id that can be optimized based on usage. */
9081 static unsigned int abbrev_opt_start;
9083 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9084 abbrev_id smaller than this, because they must be already sized
9085 during build_abbrev_table). */
9086 static unsigned int abbrev_opt_base_type_end;
9088 /* Vector of usage counts during build_abbrev_table. Indexed by
9089 abbrev_id - abbrev_opt_start. */
9090 static vec<unsigned int> abbrev_usage_count;
9092 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9093 static vec<dw_die_ref> sorted_abbrev_dies;
9095 /* The format of each DIE (and its attribute value pairs) is encoded in an
9096 abbreviation table. This routine builds the abbreviation table and assigns
9097 a unique abbreviation id for each abbreviation entry. The children of each
9098 die are visited recursively. */
9100 static void
9101 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9103 unsigned int abbrev_id = 0;
9104 dw_die_ref c;
9105 dw_attr_node *a;
9106 unsigned ix;
9107 dw_die_ref abbrev;
9109 /* Scan the DIE references, and replace any that refer to
9110 DIEs from other CUs (i.e. those which are not marked) with
9111 the local stubs we built in optimize_external_refs. */
9112 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9113 if (AT_class (a) == dw_val_class_die_ref
9114 && (c = AT_ref (a))->die_mark == 0)
9116 struct external_ref *ref_p;
9117 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9119 if (is_type_die (c)
9120 && (ref_p = lookup_external_ref (extern_map, c))
9121 && ref_p->stub && ref_p->stub != die)
9123 gcc_assert (a->dw_attr != DW_AT_signature);
9124 change_AT_die_ref (a, ref_p->stub);
9126 else
9127 /* We aren't changing this reference, so mark it external. */
9128 set_AT_ref_external (a, 1);
9131 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9133 dw_attr_node *die_a, *abbrev_a;
9134 unsigned ix;
9135 bool ok = true;
9137 if (abbrev_id == 0)
9138 continue;
9139 if (abbrev->die_tag != die->die_tag)
9140 continue;
9141 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9142 continue;
9144 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9145 continue;
9147 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9149 abbrev_a = &(*abbrev->die_attr)[ix];
9150 if ((abbrev_a->dw_attr != die_a->dw_attr)
9151 || (value_format (abbrev_a) != value_format (die_a)))
9153 ok = false;
9154 break;
9157 if (ok)
9158 break;
9161 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9163 vec_safe_push (abbrev_die_table, die);
9164 if (abbrev_opt_start)
9165 abbrev_usage_count.safe_push (0);
9167 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9169 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9170 sorted_abbrev_dies.safe_push (die);
9173 die->die_abbrev = abbrev_id;
9174 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9177 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9178 by die_abbrev's usage count, from the most commonly used
9179 abbreviation to the least. */
9181 static int
9182 die_abbrev_cmp (const void *p1, const void *p2)
9184 dw_die_ref die1 = *(const dw_die_ref *) p1;
9185 dw_die_ref die2 = *(const dw_die_ref *) p2;
9187 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9188 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9190 if (die1->die_abbrev >= abbrev_opt_base_type_end
9191 && die2->die_abbrev >= abbrev_opt_base_type_end)
9193 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9194 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9195 return -1;
9196 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9197 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9198 return 1;
9201 /* Stabilize the sort. */
9202 if (die1->die_abbrev < die2->die_abbrev)
9203 return -1;
9204 if (die1->die_abbrev > die2->die_abbrev)
9205 return 1;
9207 return 0;
9210 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9211 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9212 into dw_val_class_const_implicit or
9213 dw_val_class_unsigned_const_implicit. */
9215 static void
9216 optimize_implicit_const (unsigned int first_id, unsigned int end,
9217 vec<bool> &implicit_consts)
9219 /* It never makes sense if there is just one DIE using the abbreviation. */
9220 if (end < first_id + 2)
9221 return;
9223 dw_attr_node *a;
9224 unsigned ix, i;
9225 dw_die_ref die = sorted_abbrev_dies[first_id];
9226 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9227 if (implicit_consts[ix])
9229 enum dw_val_class new_class = dw_val_class_none;
9230 switch (AT_class (a))
9232 case dw_val_class_unsigned_const:
9233 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9234 continue;
9236 /* The .debug_abbrev section will grow by
9237 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9238 in all the DIEs using that abbreviation. */
9239 if (constant_size (AT_unsigned (a)) * (end - first_id)
9240 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9241 continue;
9243 new_class = dw_val_class_unsigned_const_implicit;
9244 break;
9246 case dw_val_class_const:
9247 new_class = dw_val_class_const_implicit;
9248 break;
9250 case dw_val_class_file:
9251 new_class = dw_val_class_file_implicit;
9252 break;
9254 default:
9255 continue;
9257 for (i = first_id; i < end; i++)
9258 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9259 = new_class;
9263 /* Attempt to optimize abbreviation table from abbrev_opt_start
9264 abbreviation above. */
9266 static void
9267 optimize_abbrev_table (void)
9269 if (abbrev_opt_start
9270 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9271 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9273 auto_vec<bool, 32> implicit_consts;
9274 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9276 unsigned int abbrev_id = abbrev_opt_start - 1;
9277 unsigned int first_id = ~0U;
9278 unsigned int last_abbrev_id = 0;
9279 unsigned int i;
9280 dw_die_ref die;
9281 if (abbrev_opt_base_type_end > abbrev_opt_start)
9282 abbrev_id = abbrev_opt_base_type_end - 1;
9283 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9284 most commonly used abbreviations come first. */
9285 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9287 dw_attr_node *a;
9288 unsigned ix;
9290 /* If calc_base_type_die_sizes has been called, the CU and
9291 base types after it can't be optimized, because we've already
9292 calculated their DIE offsets. We've sorted them first. */
9293 if (die->die_abbrev < abbrev_opt_base_type_end)
9294 continue;
9295 if (die->die_abbrev != last_abbrev_id)
9297 last_abbrev_id = die->die_abbrev;
9298 if (dwarf_version >= 5 && first_id != ~0U)
9299 optimize_implicit_const (first_id, i, implicit_consts);
9300 abbrev_id++;
9301 (*abbrev_die_table)[abbrev_id] = die;
9302 if (dwarf_version >= 5)
9304 first_id = i;
9305 implicit_consts.truncate (0);
9307 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9308 switch (AT_class (a))
9310 case dw_val_class_const:
9311 case dw_val_class_unsigned_const:
9312 case dw_val_class_file:
9313 implicit_consts.safe_push (true);
9314 break;
9315 default:
9316 implicit_consts.safe_push (false);
9317 break;
9321 else if (dwarf_version >= 5)
9323 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9324 if (!implicit_consts[ix])
9325 continue;
9326 else
9328 dw_attr_node *other_a
9329 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9330 if (!dw_val_equal_p (&a->dw_attr_val,
9331 &other_a->dw_attr_val))
9332 implicit_consts[ix] = false;
9335 die->die_abbrev = abbrev_id;
9337 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9338 if (dwarf_version >= 5 && first_id != ~0U)
9339 optimize_implicit_const (first_id, i, implicit_consts);
9342 abbrev_opt_start = 0;
9343 abbrev_opt_base_type_end = 0;
9344 abbrev_usage_count.release ();
9345 sorted_abbrev_dies.release ();
9348 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9350 static int
9351 constant_size (unsigned HOST_WIDE_INT value)
9353 int log;
9355 if (value == 0)
9356 log = 0;
9357 else
9358 log = floor_log2 (value);
9360 log = log / 8;
9361 log = 1 << (floor_log2 (log) + 1);
9363 return log;
9366 /* Return the size of a DIE as it is represented in the
9367 .debug_info section. */
9369 static unsigned long
9370 size_of_die (dw_die_ref die)
9372 unsigned long size = 0;
9373 dw_attr_node *a;
9374 unsigned ix;
9375 enum dwarf_form form;
9377 size += size_of_uleb128 (die->die_abbrev);
9378 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9380 switch (AT_class (a))
9382 case dw_val_class_addr:
9383 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9385 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9386 size += size_of_uleb128 (AT_index (a));
9388 else
9389 size += DWARF2_ADDR_SIZE;
9390 break;
9391 case dw_val_class_offset:
9392 size += DWARF_OFFSET_SIZE;
9393 break;
9394 case dw_val_class_loc:
9396 unsigned long lsize = size_of_locs (AT_loc (a));
9398 /* Block length. */
9399 if (dwarf_version >= 4)
9400 size += size_of_uleb128 (lsize);
9401 else
9402 size += constant_size (lsize);
9403 size += lsize;
9405 break;
9406 case dw_val_class_loc_list:
9407 if (dwarf_split_debug_info && dwarf_version >= 5)
9409 gcc_assert (AT_loc_list (a)->num_assigned);
9410 size += size_of_uleb128 (AT_loc_list (a)->hash);
9412 else
9413 size += DWARF_OFFSET_SIZE;
9414 break;
9415 case dw_val_class_view_list:
9416 size += DWARF_OFFSET_SIZE;
9417 break;
9418 case dw_val_class_range_list:
9419 if (value_format (a) == DW_FORM_rnglistx)
9421 gcc_assert (rnglist_idx);
9422 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9423 size += size_of_uleb128 (r->idx);
9425 else
9426 size += DWARF_OFFSET_SIZE;
9427 break;
9428 case dw_val_class_const:
9429 size += size_of_sleb128 (AT_int (a));
9430 break;
9431 case dw_val_class_unsigned_const:
9433 int csize = constant_size (AT_unsigned (a));
9434 if (dwarf_version == 3
9435 && a->dw_attr == DW_AT_data_member_location
9436 && csize >= 4)
9437 size += size_of_uleb128 (AT_unsigned (a));
9438 else
9439 size += csize;
9441 break;
9442 case dw_val_class_symview:
9443 if (symview_upper_bound <= 0xff)
9444 size += 1;
9445 else if (symview_upper_bound <= 0xffff)
9446 size += 2;
9447 else if (symview_upper_bound <= 0xffffffff)
9448 size += 4;
9449 else
9450 size += 8;
9451 break;
9452 case dw_val_class_const_implicit:
9453 case dw_val_class_unsigned_const_implicit:
9454 case dw_val_class_file_implicit:
9455 /* These occupy no size in the DIE, just an extra sleb128 in
9456 .debug_abbrev. */
9457 break;
9458 case dw_val_class_const_double:
9459 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9460 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9461 size++; /* block */
9462 break;
9463 case dw_val_class_wide_int:
9464 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9465 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9466 if (get_full_len (*a->dw_attr_val.v.val_wide)
9467 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9468 size++; /* block */
9469 break;
9470 case dw_val_class_vec:
9471 size += constant_size (a->dw_attr_val.v.val_vec.length
9472 * a->dw_attr_val.v.val_vec.elt_size)
9473 + a->dw_attr_val.v.val_vec.length
9474 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9475 break;
9476 case dw_val_class_flag:
9477 if (dwarf_version >= 4)
9478 /* Currently all add_AT_flag calls pass in 1 as last argument,
9479 so DW_FORM_flag_present can be used. If that ever changes,
9480 we'll need to use DW_FORM_flag and have some optimization
9481 in build_abbrev_table that will change those to
9482 DW_FORM_flag_present if it is set to 1 in all DIEs using
9483 the same abbrev entry. */
9484 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9485 else
9486 size += 1;
9487 break;
9488 case dw_val_class_die_ref:
9489 if (AT_ref_external (a))
9491 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9492 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9493 is sized by target address length, whereas in DWARF3
9494 it's always sized as an offset. */
9495 if (AT_ref (a)->comdat_type_p)
9496 size += DWARF_TYPE_SIGNATURE_SIZE;
9497 else if (dwarf_version == 2)
9498 size += DWARF2_ADDR_SIZE;
9499 else
9500 size += DWARF_OFFSET_SIZE;
9502 else
9503 size += DWARF_OFFSET_SIZE;
9504 break;
9505 case dw_val_class_fde_ref:
9506 size += DWARF_OFFSET_SIZE;
9507 break;
9508 case dw_val_class_lbl_id:
9509 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9511 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9512 size += size_of_uleb128 (AT_index (a));
9514 else
9515 size += DWARF2_ADDR_SIZE;
9516 break;
9517 case dw_val_class_lineptr:
9518 case dw_val_class_macptr:
9519 case dw_val_class_loclistsptr:
9520 size += DWARF_OFFSET_SIZE;
9521 break;
9522 case dw_val_class_str:
9523 form = AT_string_form (a);
9524 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9525 size += DWARF_OFFSET_SIZE;
9526 else if (form == dwarf_FORM (DW_FORM_strx))
9527 size += size_of_uleb128 (AT_index (a));
9528 else
9529 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9530 break;
9531 case dw_val_class_file:
9532 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9533 break;
9534 case dw_val_class_data8:
9535 size += 8;
9536 break;
9537 case dw_val_class_vms_delta:
9538 size += DWARF_OFFSET_SIZE;
9539 break;
9540 case dw_val_class_high_pc:
9541 size += DWARF2_ADDR_SIZE;
9542 break;
9543 case dw_val_class_discr_value:
9544 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9545 break;
9546 case dw_val_class_discr_list:
9548 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9550 /* This is a block, so we have the block length and then its
9551 data. */
9552 size += constant_size (block_size) + block_size;
9554 break;
9555 default:
9556 gcc_unreachable ();
9560 return size;
9563 /* Size the debugging information associated with a given DIE. Visits the
9564 DIE's children recursively. Updates the global variable next_die_offset, on
9565 each time through. Uses the current value of next_die_offset to update the
9566 die_offset field in each DIE. */
9568 static void
9569 calc_die_sizes (dw_die_ref die)
9571 dw_die_ref c;
9573 gcc_assert (die->die_offset == 0
9574 || (unsigned long int) die->die_offset == next_die_offset);
9575 die->die_offset = next_die_offset;
9576 next_die_offset += size_of_die (die);
9578 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9580 if (die->die_child != NULL)
9581 /* Count the null byte used to terminate sibling lists. */
9582 next_die_offset += 1;
9585 /* Size just the base type children at the start of the CU.
9586 This is needed because build_abbrev needs to size locs
9587 and sizing of type based stack ops needs to know die_offset
9588 values for the base types. */
9590 static void
9591 calc_base_type_die_sizes (void)
9593 unsigned long die_offset = (dwarf_split_debug_info
9594 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9595 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9596 unsigned int i;
9597 dw_die_ref base_type;
9598 #if ENABLE_ASSERT_CHECKING
9599 dw_die_ref prev = comp_unit_die ()->die_child;
9600 #endif
9602 die_offset += size_of_die (comp_unit_die ());
9603 for (i = 0; base_types.iterate (i, &base_type); i++)
9605 #if ENABLE_ASSERT_CHECKING
9606 gcc_assert (base_type->die_offset == 0
9607 && prev->die_sib == base_type
9608 && base_type->die_child == NULL
9609 && base_type->die_abbrev);
9610 prev = base_type;
9611 #endif
9612 if (abbrev_opt_start
9613 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9614 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9615 base_type->die_offset = die_offset;
9616 die_offset += size_of_die (base_type);
9620 /* Set the marks for a die and its children. We do this so
9621 that we know whether or not a reference needs to use FORM_ref_addr; only
9622 DIEs in the same CU will be marked. We used to clear out the offset
9623 and use that as the flag, but ran into ordering problems. */
9625 static void
9626 mark_dies (dw_die_ref die)
9628 dw_die_ref c;
9630 gcc_assert (!die->die_mark);
9632 die->die_mark = 1;
9633 FOR_EACH_CHILD (die, c, mark_dies (c));
9636 /* Clear the marks for a die and its children. */
9638 static void
9639 unmark_dies (dw_die_ref die)
9641 dw_die_ref c;
9643 if (! use_debug_types)
9644 gcc_assert (die->die_mark);
9646 die->die_mark = 0;
9647 FOR_EACH_CHILD (die, c, unmark_dies (c));
9650 /* Clear the marks for a die, its children and referred dies. */
9652 static void
9653 unmark_all_dies (dw_die_ref die)
9655 dw_die_ref c;
9656 dw_attr_node *a;
9657 unsigned ix;
9659 if (!die->die_mark)
9660 return;
9661 die->die_mark = 0;
9663 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9665 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9666 if (AT_class (a) == dw_val_class_die_ref)
9667 unmark_all_dies (AT_ref (a));
9670 /* Calculate if the entry should appear in the final output file. It may be
9671 from a pruned a type. */
9673 static bool
9674 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9676 /* By limiting gnu pubnames to definitions only, gold can generate a
9677 gdb index without entries for declarations, which don't include
9678 enough information to be useful. */
9679 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9680 return false;
9682 if (table == pubname_table)
9684 /* Enumerator names are part of the pubname table, but the
9685 parent DW_TAG_enumeration_type die may have been pruned.
9686 Don't output them if that is the case. */
9687 if (p->die->die_tag == DW_TAG_enumerator &&
9688 (p->die->die_parent == NULL
9689 || !p->die->die_parent->die_perennial_p))
9690 return false;
9692 /* Everything else in the pubname table is included. */
9693 return true;
9696 /* The pubtypes table shouldn't include types that have been
9697 pruned. */
9698 return (p->die->die_offset != 0
9699 || !flag_eliminate_unused_debug_types);
9702 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9703 generated for the compilation unit. */
9705 static unsigned long
9706 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9708 unsigned long size;
9709 unsigned i;
9710 pubname_entry *p;
9711 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9713 size = DWARF_PUBNAMES_HEADER_SIZE;
9714 FOR_EACH_VEC_ELT (*names, i, p)
9715 if (include_pubname_in_output (names, p))
9716 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9718 size += DWARF_OFFSET_SIZE;
9719 return size;
9722 /* Return the size of the information in the .debug_aranges section. */
9724 static unsigned long
9725 size_of_aranges (void)
9727 unsigned long size;
9729 size = DWARF_ARANGES_HEADER_SIZE;
9731 /* Count the address/length pair for this compilation unit. */
9732 if (text_section_used)
9733 size += 2 * DWARF2_ADDR_SIZE;
9734 if (cold_text_section_used)
9735 size += 2 * DWARF2_ADDR_SIZE;
9736 if (have_multiple_function_sections)
9738 unsigned fde_idx;
9739 dw_fde_ref fde;
9741 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9743 if (DECL_IGNORED_P (fde->decl))
9744 continue;
9745 if (!fde->in_std_section)
9746 size += 2 * DWARF2_ADDR_SIZE;
9747 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9748 size += 2 * DWARF2_ADDR_SIZE;
9752 /* Count the two zero words used to terminated the address range table. */
9753 size += 2 * DWARF2_ADDR_SIZE;
9754 return size;
9757 /* Select the encoding of an attribute value. */
9759 static enum dwarf_form
9760 value_format (dw_attr_node *a)
9762 switch (AT_class (a))
9764 case dw_val_class_addr:
9765 /* Only very few attributes allow DW_FORM_addr. */
9766 switch (a->dw_attr)
9768 case DW_AT_low_pc:
9769 case DW_AT_high_pc:
9770 case DW_AT_entry_pc:
9771 case DW_AT_trampoline:
9772 return (AT_index (a) == NOT_INDEXED
9773 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9774 default:
9775 break;
9777 switch (DWARF2_ADDR_SIZE)
9779 case 1:
9780 return DW_FORM_data1;
9781 case 2:
9782 return DW_FORM_data2;
9783 case 4:
9784 return DW_FORM_data4;
9785 case 8:
9786 return DW_FORM_data8;
9787 default:
9788 gcc_unreachable ();
9790 case dw_val_class_loc_list:
9791 if (dwarf_split_debug_info
9792 && dwarf_version >= 5
9793 && AT_loc_list (a)->num_assigned)
9794 return DW_FORM_loclistx;
9795 /* FALLTHRU */
9796 case dw_val_class_view_list:
9797 case dw_val_class_range_list:
9798 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9799 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9800 care about sizes of .debug* sections in shared libraries and
9801 executables and don't take into account relocations that affect just
9802 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9803 table in the .debug_rnglists section. */
9804 if (dwarf_split_debug_info
9805 && dwarf_version >= 5
9806 && AT_class (a) == dw_val_class_range_list
9807 && rnglist_idx
9808 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9809 return DW_FORM_rnglistx;
9810 if (dwarf_version >= 4)
9811 return DW_FORM_sec_offset;
9812 /* FALLTHRU */
9813 case dw_val_class_vms_delta:
9814 case dw_val_class_offset:
9815 switch (DWARF_OFFSET_SIZE)
9817 case 4:
9818 return DW_FORM_data4;
9819 case 8:
9820 return DW_FORM_data8;
9821 default:
9822 gcc_unreachable ();
9824 case dw_val_class_loc:
9825 if (dwarf_version >= 4)
9826 return DW_FORM_exprloc;
9827 switch (constant_size (size_of_locs (AT_loc (a))))
9829 case 1:
9830 return DW_FORM_block1;
9831 case 2:
9832 return DW_FORM_block2;
9833 case 4:
9834 return DW_FORM_block4;
9835 default:
9836 gcc_unreachable ();
9838 case dw_val_class_const:
9839 return DW_FORM_sdata;
9840 case dw_val_class_unsigned_const:
9841 switch (constant_size (AT_unsigned (a)))
9843 case 1:
9844 return DW_FORM_data1;
9845 case 2:
9846 return DW_FORM_data2;
9847 case 4:
9848 /* In DWARF3 DW_AT_data_member_location with
9849 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9850 constant, so we need to use DW_FORM_udata if we need
9851 a large constant. */
9852 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9853 return DW_FORM_udata;
9854 return DW_FORM_data4;
9855 case 8:
9856 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9857 return DW_FORM_udata;
9858 return DW_FORM_data8;
9859 default:
9860 gcc_unreachable ();
9862 case dw_val_class_const_implicit:
9863 case dw_val_class_unsigned_const_implicit:
9864 case dw_val_class_file_implicit:
9865 return DW_FORM_implicit_const;
9866 case dw_val_class_const_double:
9867 switch (HOST_BITS_PER_WIDE_INT)
9869 case 8:
9870 return DW_FORM_data2;
9871 case 16:
9872 return DW_FORM_data4;
9873 case 32:
9874 return DW_FORM_data8;
9875 case 64:
9876 if (dwarf_version >= 5)
9877 return DW_FORM_data16;
9878 /* FALLTHRU */
9879 default:
9880 return DW_FORM_block1;
9882 case dw_val_class_wide_int:
9883 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9885 case 8:
9886 return DW_FORM_data1;
9887 case 16:
9888 return DW_FORM_data2;
9889 case 32:
9890 return DW_FORM_data4;
9891 case 64:
9892 return DW_FORM_data8;
9893 case 128:
9894 if (dwarf_version >= 5)
9895 return DW_FORM_data16;
9896 /* FALLTHRU */
9897 default:
9898 return DW_FORM_block1;
9900 case dw_val_class_symview:
9901 /* ??? We might use uleb128, but then we'd have to compute
9902 .debug_info offsets in the assembler. */
9903 if (symview_upper_bound <= 0xff)
9904 return DW_FORM_data1;
9905 else if (symview_upper_bound <= 0xffff)
9906 return DW_FORM_data2;
9907 else if (symview_upper_bound <= 0xffffffff)
9908 return DW_FORM_data4;
9909 else
9910 return DW_FORM_data8;
9911 case dw_val_class_vec:
9912 switch (constant_size (a->dw_attr_val.v.val_vec.length
9913 * a->dw_attr_val.v.val_vec.elt_size))
9915 case 1:
9916 return DW_FORM_block1;
9917 case 2:
9918 return DW_FORM_block2;
9919 case 4:
9920 return DW_FORM_block4;
9921 default:
9922 gcc_unreachable ();
9924 case dw_val_class_flag:
9925 if (dwarf_version >= 4)
9927 /* Currently all add_AT_flag calls pass in 1 as last argument,
9928 so DW_FORM_flag_present can be used. If that ever changes,
9929 we'll need to use DW_FORM_flag and have some optimization
9930 in build_abbrev_table that will change those to
9931 DW_FORM_flag_present if it is set to 1 in all DIEs using
9932 the same abbrev entry. */
9933 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9934 return DW_FORM_flag_present;
9936 return DW_FORM_flag;
9937 case dw_val_class_die_ref:
9938 if (AT_ref_external (a))
9940 if (AT_ref (a)->comdat_type_p)
9941 return DW_FORM_ref_sig8;
9942 else
9943 return DW_FORM_ref_addr;
9945 else
9946 return DW_FORM_ref;
9947 case dw_val_class_fde_ref:
9948 return DW_FORM_data;
9949 case dw_val_class_lbl_id:
9950 return (AT_index (a) == NOT_INDEXED
9951 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9952 case dw_val_class_lineptr:
9953 case dw_val_class_macptr:
9954 case dw_val_class_loclistsptr:
9955 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9956 case dw_val_class_str:
9957 return AT_string_form (a);
9958 case dw_val_class_file:
9959 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9961 case 1:
9962 return DW_FORM_data1;
9963 case 2:
9964 return DW_FORM_data2;
9965 case 4:
9966 return DW_FORM_data4;
9967 default:
9968 gcc_unreachable ();
9971 case dw_val_class_data8:
9972 return DW_FORM_data8;
9974 case dw_val_class_high_pc:
9975 switch (DWARF2_ADDR_SIZE)
9977 case 1:
9978 return DW_FORM_data1;
9979 case 2:
9980 return DW_FORM_data2;
9981 case 4:
9982 return DW_FORM_data4;
9983 case 8:
9984 return DW_FORM_data8;
9985 default:
9986 gcc_unreachable ();
9989 case dw_val_class_discr_value:
9990 return (a->dw_attr_val.v.val_discr_value.pos
9991 ? DW_FORM_udata
9992 : DW_FORM_sdata);
9993 case dw_val_class_discr_list:
9994 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9996 case 1:
9997 return DW_FORM_block1;
9998 case 2:
9999 return DW_FORM_block2;
10000 case 4:
10001 return DW_FORM_block4;
10002 default:
10003 gcc_unreachable ();
10006 default:
10007 gcc_unreachable ();
10011 /* Output the encoding of an attribute value. */
10013 static void
10014 output_value_format (dw_attr_node *a)
10016 enum dwarf_form form = value_format (a);
10018 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10021 /* Given a die and id, produce the appropriate abbreviations. */
10023 static void
10024 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10026 unsigned ix;
10027 dw_attr_node *a_attr;
10029 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10030 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10031 dwarf_tag_name (abbrev->die_tag));
10033 if (abbrev->die_child != NULL)
10034 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10035 else
10036 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10038 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10040 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10041 dwarf_attr_name (a_attr->dw_attr));
10042 output_value_format (a_attr);
10043 if (value_format (a_attr) == DW_FORM_implicit_const)
10045 if (AT_class (a_attr) == dw_val_class_file_implicit)
10047 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10048 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10049 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10051 else
10052 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10056 dw2_asm_output_data (1, 0, NULL);
10057 dw2_asm_output_data (1, 0, NULL);
10061 /* Output the .debug_abbrev section which defines the DIE abbreviation
10062 table. */
10064 static void
10065 output_abbrev_section (void)
10067 unsigned int abbrev_id;
10068 dw_die_ref abbrev;
10070 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10071 if (abbrev_id != 0)
10072 output_die_abbrevs (abbrev_id, abbrev);
10074 /* Terminate the table. */
10075 dw2_asm_output_data (1, 0, NULL);
10078 /* Return a new location list, given the begin and end range, and the
10079 expression. */
10081 static inline dw_loc_list_ref
10082 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10083 const char *end, var_loc_view vend,
10084 const char *section)
10086 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10088 retlist->begin = begin;
10089 retlist->begin_entry = NULL;
10090 retlist->end = end;
10091 retlist->expr = expr;
10092 retlist->section = section;
10093 retlist->vbegin = vbegin;
10094 retlist->vend = vend;
10096 return retlist;
10099 /* Return true iff there's any nonzero view number in the loc list.
10101 ??? When views are not enabled, we'll often extend a single range
10102 to the entire function, so that we emit a single location
10103 expression rather than a location list. With views, even with a
10104 single range, we'll output a list if start or end have a nonzero
10105 view. If we change this, we may want to stop splitting a single
10106 range in dw_loc_list just because of a nonzero view, even if it
10107 straddles across hot/cold partitions. */
10109 static bool
10110 loc_list_has_views (dw_loc_list_ref list)
10112 if (!debug_variable_location_views)
10113 return false;
10115 for (dw_loc_list_ref loc = list;
10116 loc != NULL; loc = loc->dw_loc_next)
10117 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10118 return true;
10120 return false;
10123 /* Generate a new internal symbol for this location list node, if it
10124 hasn't got one yet. */
10126 static inline void
10127 gen_llsym (dw_loc_list_ref list)
10129 gcc_assert (!list->ll_symbol);
10130 list->ll_symbol = gen_internal_sym ("LLST");
10132 if (!loc_list_has_views (list))
10133 return;
10135 if (dwarf2out_locviews_in_attribute ())
10137 /* Use the same label_num for the view list. */
10138 label_num--;
10139 list->vl_symbol = gen_internal_sym ("LVUS");
10141 else
10142 list->vl_symbol = list->ll_symbol;
10145 /* Generate a symbol for the list, but only if we really want to emit
10146 it as a list. */
10148 static inline void
10149 maybe_gen_llsym (dw_loc_list_ref list)
10151 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10152 return;
10154 gen_llsym (list);
10157 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10158 NULL, don't consider size of the location expression. If we're not
10159 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10160 representation in *SIZEP. */
10162 static bool
10163 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10165 /* Don't output an entry that starts and ends at the same address. */
10166 if (strcmp (curr->begin, curr->end) == 0
10167 && curr->vbegin == curr->vend && !curr->force)
10168 return true;
10170 if (!sizep)
10171 return false;
10173 unsigned long size = size_of_locs (curr->expr);
10175 /* If the expression is too large, drop it on the floor. We could
10176 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10177 in the expression, but >= 64KB expressions for a single value
10178 in a single range are unlikely very useful. */
10179 if (dwarf_version < 5 && size > 0xffff)
10180 return true;
10182 *sizep = size;
10184 return false;
10187 /* Output a view pair loclist entry for CURR, if it requires one. */
10189 static void
10190 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10192 if (!dwarf2out_locviews_in_loclist ())
10193 return;
10195 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10196 return;
10198 #ifdef DW_LLE_view_pair
10199 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10201 if (dwarf2out_as_locview_support)
10203 if (ZERO_VIEW_P (curr->vbegin))
10204 dw2_asm_output_data_uleb128 (0, "Location view begin");
10205 else
10207 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10208 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10209 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10212 if (ZERO_VIEW_P (curr->vend))
10213 dw2_asm_output_data_uleb128 (0, "Location view end");
10214 else
10216 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10217 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10218 dw2_asm_output_symname_uleb128 (label, "Location view end");
10221 else
10223 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10224 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10226 #endif /* DW_LLE_view_pair */
10228 return;
10231 /* Output the location list given to us. */
10233 static void
10234 output_loc_list (dw_loc_list_ref list_head)
10236 int vcount = 0, lcount = 0;
10238 if (list_head->emitted)
10239 return;
10240 list_head->emitted = true;
10242 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10244 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10246 for (dw_loc_list_ref curr = list_head; curr != NULL;
10247 curr = curr->dw_loc_next)
10249 unsigned long size;
10251 if (skip_loc_list_entry (curr, &size))
10252 continue;
10254 vcount++;
10256 /* ?? dwarf_split_debug_info? */
10257 if (dwarf2out_as_locview_support)
10259 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10261 if (!ZERO_VIEW_P (curr->vbegin))
10263 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10264 dw2_asm_output_symname_uleb128 (label,
10265 "View list begin (%s)",
10266 list_head->vl_symbol);
10268 else
10269 dw2_asm_output_data_uleb128 (0,
10270 "View list begin (%s)",
10271 list_head->vl_symbol);
10273 if (!ZERO_VIEW_P (curr->vend))
10275 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10276 dw2_asm_output_symname_uleb128 (label,
10277 "View list end (%s)",
10278 list_head->vl_symbol);
10280 else
10281 dw2_asm_output_data_uleb128 (0,
10282 "View list end (%s)",
10283 list_head->vl_symbol);
10285 else
10287 dw2_asm_output_data_uleb128 (curr->vbegin,
10288 "View list begin (%s)",
10289 list_head->vl_symbol);
10290 dw2_asm_output_data_uleb128 (curr->vend,
10291 "View list end (%s)",
10292 list_head->vl_symbol);
10297 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10299 const char *last_section = NULL;
10300 const char *base_label = NULL;
10302 /* Walk the location list, and output each range + expression. */
10303 for (dw_loc_list_ref curr = list_head; curr != NULL;
10304 curr = curr->dw_loc_next)
10306 unsigned long size;
10308 /* Skip this entry? If we skip it here, we must skip it in the
10309 view list above as well. */
10310 if (skip_loc_list_entry (curr, &size))
10311 continue;
10313 lcount++;
10315 if (dwarf_version >= 5)
10317 if (dwarf_split_debug_info)
10319 dwarf2out_maybe_output_loclist_view_pair (curr);
10320 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
10321 uleb128 index into .debug_addr and uleb128 length. */
10322 dw2_asm_output_data (1, DW_LLE_startx_length,
10323 "DW_LLE_startx_length (%s)",
10324 list_head->ll_symbol);
10325 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10326 "Location list range start index "
10327 "(%s)", curr->begin);
10328 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
10329 For that case we probably need to emit DW_LLE_startx_endx,
10330 but we'd need 2 .debug_addr entries rather than just one. */
10331 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10332 "Location list length (%s)",
10333 list_head->ll_symbol);
10335 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10337 dwarf2out_maybe_output_loclist_view_pair (curr);
10338 /* If all code is in .text section, the base address is
10339 already provided by the CU attributes. Use
10340 DW_LLE_offset_pair where both addresses are uleb128 encoded
10341 offsets against that base. */
10342 dw2_asm_output_data (1, DW_LLE_offset_pair,
10343 "DW_LLE_offset_pair (%s)",
10344 list_head->ll_symbol);
10345 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10346 "Location list begin address (%s)",
10347 list_head->ll_symbol);
10348 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10349 "Location list end address (%s)",
10350 list_head->ll_symbol);
10352 else if (HAVE_AS_LEB128)
10354 /* Otherwise, find out how many consecutive entries could share
10355 the same base entry. If just one, emit DW_LLE_start_length,
10356 otherwise emit DW_LLE_base_address for the base address
10357 followed by a series of DW_LLE_offset_pair. */
10358 if (last_section == NULL || curr->section != last_section)
10360 dw_loc_list_ref curr2;
10361 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10362 curr2 = curr2->dw_loc_next)
10364 if (strcmp (curr2->begin, curr2->end) == 0
10365 && !curr2->force)
10366 continue;
10367 break;
10369 if (curr2 == NULL || curr->section != curr2->section)
10370 last_section = NULL;
10371 else
10373 last_section = curr->section;
10374 base_label = curr->begin;
10375 dw2_asm_output_data (1, DW_LLE_base_address,
10376 "DW_LLE_base_address (%s)",
10377 list_head->ll_symbol);
10378 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10379 "Base address (%s)",
10380 list_head->ll_symbol);
10383 /* Only one entry with the same base address. Use
10384 DW_LLE_start_length with absolute address and uleb128
10385 length. */
10386 if (last_section == NULL)
10388 dwarf2out_maybe_output_loclist_view_pair (curr);
10389 dw2_asm_output_data (1, DW_LLE_start_length,
10390 "DW_LLE_start_length (%s)",
10391 list_head->ll_symbol);
10392 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10393 "Location list begin address (%s)",
10394 list_head->ll_symbol);
10395 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10396 "Location list length "
10397 "(%s)", list_head->ll_symbol);
10399 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10400 DW_LLE_base_address. */
10401 else
10403 dwarf2out_maybe_output_loclist_view_pair (curr);
10404 dw2_asm_output_data (1, DW_LLE_offset_pair,
10405 "DW_LLE_offset_pair (%s)",
10406 list_head->ll_symbol);
10407 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10408 "Location list begin address "
10409 "(%s)", list_head->ll_symbol);
10410 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10411 "Location list end address "
10412 "(%s)", list_head->ll_symbol);
10415 /* The assembler does not support .uleb128 directive. Emit
10416 DW_LLE_start_end with a pair of absolute addresses. */
10417 else
10419 dwarf2out_maybe_output_loclist_view_pair (curr);
10420 dw2_asm_output_data (1, DW_LLE_start_end,
10421 "DW_LLE_start_end (%s)",
10422 list_head->ll_symbol);
10423 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10424 "Location list begin address (%s)",
10425 list_head->ll_symbol);
10426 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10427 "Location list end address (%s)",
10428 list_head->ll_symbol);
10431 else if (dwarf_split_debug_info)
10433 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10434 and 4 byte length. */
10435 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10436 "Location list start/length entry (%s)",
10437 list_head->ll_symbol);
10438 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10439 "Location list range start index (%s)",
10440 curr->begin);
10441 /* The length field is 4 bytes. If we ever need to support
10442 an 8-byte length, we can add a new DW_LLE code or fall back
10443 to DW_LLE_GNU_start_end_entry. */
10444 dw2_asm_output_delta (4, curr->end, curr->begin,
10445 "Location list range length (%s)",
10446 list_head->ll_symbol);
10448 else if (!have_multiple_function_sections)
10450 /* Pair of relative addresses against start of text section. */
10451 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10452 "Location list begin address (%s)",
10453 list_head->ll_symbol);
10454 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10455 "Location list end address (%s)",
10456 list_head->ll_symbol);
10458 else
10460 /* Pair of absolute addresses. */
10461 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10462 "Location list begin address (%s)",
10463 list_head->ll_symbol);
10464 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10465 "Location list end address (%s)",
10466 list_head->ll_symbol);
10469 /* Output the block length for this list of location operations. */
10470 if (dwarf_version >= 5)
10471 dw2_asm_output_data_uleb128 (size, "Location expression size");
10472 else
10474 gcc_assert (size <= 0xffff);
10475 dw2_asm_output_data (2, size, "Location expression size");
10478 output_loc_sequence (curr->expr, -1);
10481 /* And finally list termination. */
10482 if (dwarf_version >= 5)
10483 dw2_asm_output_data (1, DW_LLE_end_of_list,
10484 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10485 else if (dwarf_split_debug_info)
10486 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10487 "Location list terminator (%s)",
10488 list_head->ll_symbol);
10489 else
10491 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10492 "Location list terminator begin (%s)",
10493 list_head->ll_symbol);
10494 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10495 "Location list terminator end (%s)",
10496 list_head->ll_symbol);
10499 gcc_assert (!list_head->vl_symbol
10500 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10503 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10504 section. Emit a relocated reference if val_entry is NULL, otherwise,
10505 emit an indirect reference. */
10507 static void
10508 output_range_list_offset (dw_attr_node *a)
10510 const char *name = dwarf_attr_name (a->dw_attr);
10512 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10514 if (dwarf_version >= 5)
10516 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10517 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
10518 debug_ranges_section, "%s", name);
10520 else
10522 char *p = strchr (ranges_section_label, '\0');
10523 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10524 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10525 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10526 debug_ranges_section, "%s", name);
10527 *p = '\0';
10530 else if (dwarf_version >= 5)
10532 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10533 gcc_assert (rnglist_idx);
10534 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10536 else
10537 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10538 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10539 "%s (offset from %s)", name, ranges_section_label);
10542 /* Output the offset into the debug_loc section. */
10544 static void
10545 output_loc_list_offset (dw_attr_node *a)
10547 char *sym = AT_loc_list (a)->ll_symbol;
10549 gcc_assert (sym);
10550 if (!dwarf_split_debug_info)
10551 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10552 "%s", dwarf_attr_name (a->dw_attr));
10553 else if (dwarf_version >= 5)
10555 gcc_assert (AT_loc_list (a)->num_assigned);
10556 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10557 dwarf_attr_name (a->dw_attr),
10558 sym);
10560 else
10561 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10562 "%s", dwarf_attr_name (a->dw_attr));
10565 /* Output the offset into the debug_loc section. */
10567 static void
10568 output_view_list_offset (dw_attr_node *a)
10570 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10572 gcc_assert (sym);
10573 if (dwarf_split_debug_info)
10574 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10575 "%s", dwarf_attr_name (a->dw_attr));
10576 else
10577 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10578 "%s", dwarf_attr_name (a->dw_attr));
10581 /* Output an attribute's index or value appropriately. */
10583 static void
10584 output_attr_index_or_value (dw_attr_node *a)
10586 const char *name = dwarf_attr_name (a->dw_attr);
10588 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10590 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10591 return;
10593 switch (AT_class (a))
10595 case dw_val_class_addr:
10596 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10597 break;
10598 case dw_val_class_high_pc:
10599 case dw_val_class_lbl_id:
10600 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10601 break;
10602 default:
10603 gcc_unreachable ();
10607 /* Output a type signature. */
10609 static inline void
10610 output_signature (const char *sig, const char *name)
10612 int i;
10614 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10615 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10618 /* Output a discriminant value. */
10620 static inline void
10621 output_discr_value (dw_discr_value *discr_value, const char *name)
10623 if (discr_value->pos)
10624 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10625 else
10626 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10629 /* Output the DIE and its attributes. Called recursively to generate
10630 the definitions of each child DIE. */
10632 static void
10633 output_die (dw_die_ref die)
10635 dw_attr_node *a;
10636 dw_die_ref c;
10637 unsigned long size;
10638 unsigned ix;
10640 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10641 (unsigned long)die->die_offset,
10642 dwarf_tag_name (die->die_tag));
10644 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10646 const char *name = dwarf_attr_name (a->dw_attr);
10648 switch (AT_class (a))
10650 case dw_val_class_addr:
10651 output_attr_index_or_value (a);
10652 break;
10654 case dw_val_class_offset:
10655 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10656 "%s", name);
10657 break;
10659 case dw_val_class_range_list:
10660 output_range_list_offset (a);
10661 break;
10663 case dw_val_class_loc:
10664 size = size_of_locs (AT_loc (a));
10666 /* Output the block length for this list of location operations. */
10667 if (dwarf_version >= 4)
10668 dw2_asm_output_data_uleb128 (size, "%s", name);
10669 else
10670 dw2_asm_output_data (constant_size (size), size, "%s", name);
10672 output_loc_sequence (AT_loc (a), -1);
10673 break;
10675 case dw_val_class_const:
10676 /* ??? It would be slightly more efficient to use a scheme like is
10677 used for unsigned constants below, but gdb 4.x does not sign
10678 extend. Gdb 5.x does sign extend. */
10679 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10680 break;
10682 case dw_val_class_unsigned_const:
10684 int csize = constant_size (AT_unsigned (a));
10685 if (dwarf_version == 3
10686 && a->dw_attr == DW_AT_data_member_location
10687 && csize >= 4)
10688 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10689 else
10690 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10692 break;
10694 case dw_val_class_symview:
10696 int vsize;
10697 if (symview_upper_bound <= 0xff)
10698 vsize = 1;
10699 else if (symview_upper_bound <= 0xffff)
10700 vsize = 2;
10701 else if (symview_upper_bound <= 0xffffffff)
10702 vsize = 4;
10703 else
10704 vsize = 8;
10705 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10706 "%s", name);
10708 break;
10710 case dw_val_class_const_implicit:
10711 if (flag_debug_asm)
10712 fprintf (asm_out_file, "\t\t\t%s %s ("
10713 HOST_WIDE_INT_PRINT_DEC ")\n",
10714 ASM_COMMENT_START, name, AT_int (a));
10715 break;
10717 case dw_val_class_unsigned_const_implicit:
10718 if (flag_debug_asm)
10719 fprintf (asm_out_file, "\t\t\t%s %s ("
10720 HOST_WIDE_INT_PRINT_HEX ")\n",
10721 ASM_COMMENT_START, name, AT_unsigned (a));
10722 break;
10724 case dw_val_class_const_double:
10726 unsigned HOST_WIDE_INT first, second;
10728 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10729 dw2_asm_output_data (1,
10730 HOST_BITS_PER_DOUBLE_INT
10731 / HOST_BITS_PER_CHAR,
10732 NULL);
10734 if (WORDS_BIG_ENDIAN)
10736 first = a->dw_attr_val.v.val_double.high;
10737 second = a->dw_attr_val.v.val_double.low;
10739 else
10741 first = a->dw_attr_val.v.val_double.low;
10742 second = a->dw_attr_val.v.val_double.high;
10745 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10746 first, "%s", name);
10747 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10748 second, NULL);
10750 break;
10752 case dw_val_class_wide_int:
10754 int i;
10755 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10756 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10757 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10758 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10759 * l, NULL);
10761 if (WORDS_BIG_ENDIAN)
10762 for (i = len - 1; i >= 0; --i)
10764 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10765 "%s", name);
10766 name = "";
10768 else
10769 for (i = 0; i < len; ++i)
10771 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10772 "%s", name);
10773 name = "";
10776 break;
10778 case dw_val_class_vec:
10780 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10781 unsigned int len = a->dw_attr_val.v.val_vec.length;
10782 unsigned int i;
10783 unsigned char *p;
10785 dw2_asm_output_data (constant_size (len * elt_size),
10786 len * elt_size, "%s", name);
10787 if (elt_size > sizeof (HOST_WIDE_INT))
10789 elt_size /= 2;
10790 len *= 2;
10792 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10793 i < len;
10794 i++, p += elt_size)
10795 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10796 "fp or vector constant word %u", i);
10797 break;
10800 case dw_val_class_flag:
10801 if (dwarf_version >= 4)
10803 /* Currently all add_AT_flag calls pass in 1 as last argument,
10804 so DW_FORM_flag_present can be used. If that ever changes,
10805 we'll need to use DW_FORM_flag and have some optimization
10806 in build_abbrev_table that will change those to
10807 DW_FORM_flag_present if it is set to 1 in all DIEs using
10808 the same abbrev entry. */
10809 gcc_assert (AT_flag (a) == 1);
10810 if (flag_debug_asm)
10811 fprintf (asm_out_file, "\t\t\t%s %s\n",
10812 ASM_COMMENT_START, name);
10813 break;
10815 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10816 break;
10818 case dw_val_class_loc_list:
10819 output_loc_list_offset (a);
10820 break;
10822 case dw_val_class_view_list:
10823 output_view_list_offset (a);
10824 break;
10826 case dw_val_class_die_ref:
10827 if (AT_ref_external (a))
10829 if (AT_ref (a)->comdat_type_p)
10831 comdat_type_node *type_node
10832 = AT_ref (a)->die_id.die_type_node;
10834 gcc_assert (type_node);
10835 output_signature (type_node->signature, name);
10837 else
10839 const char *sym = AT_ref (a)->die_id.die_symbol;
10840 int size;
10842 gcc_assert (sym);
10843 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10844 length, whereas in DWARF3 it's always sized as an
10845 offset. */
10846 if (dwarf_version == 2)
10847 size = DWARF2_ADDR_SIZE;
10848 else
10849 size = DWARF_OFFSET_SIZE;
10850 /* ??? We cannot unconditionally output die_offset if
10851 non-zero - others might create references to those
10852 DIEs via symbols.
10853 And we do not clear its DIE offset after outputting it
10854 (and the label refers to the actual DIEs, not the
10855 DWARF CU unit header which is when using label + offset
10856 would be the correct thing to do).
10857 ??? This is the reason for the with_offset flag. */
10858 if (AT_ref (a)->with_offset)
10859 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10860 debug_info_section, "%s", name);
10861 else
10862 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10863 name);
10866 else
10868 gcc_assert (AT_ref (a)->die_offset);
10869 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10870 "%s", name);
10872 break;
10874 case dw_val_class_fde_ref:
10876 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10878 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10879 a->dw_attr_val.v.val_fde_index * 2);
10880 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10881 "%s", name);
10883 break;
10885 case dw_val_class_vms_delta:
10886 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10887 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10888 AT_vms_delta2 (a), AT_vms_delta1 (a),
10889 "%s", name);
10890 #else
10891 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10892 AT_vms_delta2 (a), AT_vms_delta1 (a),
10893 "%s", name);
10894 #endif
10895 break;
10897 case dw_val_class_lbl_id:
10898 output_attr_index_or_value (a);
10899 break;
10901 case dw_val_class_lineptr:
10902 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10903 debug_line_section, "%s", name);
10904 break;
10906 case dw_val_class_macptr:
10907 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10908 debug_macinfo_section, "%s", name);
10909 break;
10911 case dw_val_class_loclistsptr:
10912 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10913 debug_loc_section, "%s", name);
10914 break;
10916 case dw_val_class_str:
10917 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10918 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10919 a->dw_attr_val.v.val_str->label,
10920 debug_str_section,
10921 "%s: \"%s\"", name, AT_string (a));
10922 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10923 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10924 a->dw_attr_val.v.val_str->label,
10925 debug_line_str_section,
10926 "%s: \"%s\"", name, AT_string (a));
10927 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
10928 dw2_asm_output_data_uleb128 (AT_index (a),
10929 "%s: \"%s\"", name, AT_string (a));
10930 else
10931 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10932 break;
10934 case dw_val_class_file:
10936 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10938 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10939 a->dw_attr_val.v.val_file->filename);
10940 break;
10943 case dw_val_class_file_implicit:
10944 if (flag_debug_asm)
10945 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10946 ASM_COMMENT_START, name,
10947 maybe_emit_file (a->dw_attr_val.v.val_file),
10948 a->dw_attr_val.v.val_file->filename);
10949 break;
10951 case dw_val_class_data8:
10953 int i;
10955 for (i = 0; i < 8; i++)
10956 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10957 i == 0 ? "%s" : NULL, name);
10958 break;
10961 case dw_val_class_high_pc:
10962 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10963 get_AT_low_pc (die), "DW_AT_high_pc");
10964 break;
10966 case dw_val_class_discr_value:
10967 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10968 break;
10970 case dw_val_class_discr_list:
10972 dw_discr_list_ref list = AT_discr_list (a);
10973 const int size = size_of_discr_list (list);
10975 /* This is a block, so output its length first. */
10976 dw2_asm_output_data (constant_size (size), size,
10977 "%s: block size", name);
10979 for (; list != NULL; list = list->dw_discr_next)
10981 /* One byte for the discriminant value descriptor, and then as
10982 many LEB128 numbers as required. */
10983 if (list->dw_discr_range)
10984 dw2_asm_output_data (1, DW_DSC_range,
10985 "%s: DW_DSC_range", name);
10986 else
10987 dw2_asm_output_data (1, DW_DSC_label,
10988 "%s: DW_DSC_label", name);
10990 output_discr_value (&list->dw_discr_lower_bound, name);
10991 if (list->dw_discr_range)
10992 output_discr_value (&list->dw_discr_upper_bound, name);
10994 break;
10997 default:
10998 gcc_unreachable ();
11002 FOR_EACH_CHILD (die, c, output_die (c));
11004 /* Add null byte to terminate sibling list. */
11005 if (die->die_child != NULL)
11006 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11007 (unsigned long) die->die_offset);
11010 /* Output the dwarf version number. */
11012 static void
11013 output_dwarf_version ()
11015 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11016 views in loclist. That will change eventually. */
11017 if (dwarf_version == 6)
11019 static bool once;
11020 if (!once)
11022 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11023 "incompatibilities");
11024 once = true;
11026 dw2_asm_output_data (2, 5, "DWARF version number");
11028 else
11029 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11032 /* Output the compilation unit that appears at the beginning of the
11033 .debug_info section, and precedes the DIE descriptions. */
11035 static void
11036 output_compilation_unit_header (enum dwarf_unit_type ut)
11038 if (!XCOFF_DEBUGGING_INFO)
11040 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11041 dw2_asm_output_data (4, 0xffffffff,
11042 "Initial length escape value indicating 64-bit DWARF extension");
11043 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11044 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11045 "Length of Compilation Unit Info");
11048 output_dwarf_version ();
11049 if (dwarf_version >= 5)
11051 const char *name;
11052 switch (ut)
11054 case DW_UT_compile: name = "DW_UT_compile"; break;
11055 case DW_UT_type: name = "DW_UT_type"; break;
11056 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11057 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11058 default: gcc_unreachable ();
11060 dw2_asm_output_data (1, ut, "%s", name);
11061 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11063 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11064 debug_abbrev_section,
11065 "Offset Into Abbrev. Section");
11066 if (dwarf_version < 5)
11067 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11070 /* Output the compilation unit DIE and its children. */
11072 static void
11073 output_comp_unit (dw_die_ref die, int output_if_empty,
11074 const unsigned char *dwo_id)
11076 const char *secname, *oldsym;
11077 char *tmp;
11079 /* Unless we are outputting main CU, we may throw away empty ones. */
11080 if (!output_if_empty && die->die_child == NULL)
11081 return;
11083 /* Even if there are no children of this DIE, we must output the information
11084 about the compilation unit. Otherwise, on an empty translation unit, we
11085 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11086 will then complain when examining the file. First mark all the DIEs in
11087 this CU so we know which get local refs. */
11088 mark_dies (die);
11090 external_ref_hash_type *extern_map = optimize_external_refs (die);
11092 /* For now, optimize only the main CU, in order to optimize the rest
11093 we'd need to see all of them earlier. Leave the rest for post-linking
11094 tools like DWZ. */
11095 if (die == comp_unit_die ())
11096 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11098 build_abbrev_table (die, extern_map);
11100 optimize_abbrev_table ();
11102 delete extern_map;
11104 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11105 next_die_offset = (dwo_id
11106 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11107 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11108 calc_die_sizes (die);
11110 oldsym = die->die_id.die_symbol;
11111 if (oldsym && die->comdat_type_p)
11113 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11115 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11116 secname = tmp;
11117 die->die_id.die_symbol = NULL;
11118 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11120 else
11122 switch_to_section (debug_info_section);
11123 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11124 info_section_emitted = true;
11127 /* For LTO cross unit DIE refs we want a symbol on the start of the
11128 debuginfo section, not on the CU DIE. */
11129 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11131 /* ??? No way to get visibility assembled without a decl. */
11132 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11133 get_identifier (oldsym), char_type_node);
11134 TREE_PUBLIC (decl) = true;
11135 TREE_STATIC (decl) = true;
11136 DECL_ARTIFICIAL (decl) = true;
11137 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11138 DECL_VISIBILITY_SPECIFIED (decl) = true;
11139 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11140 #ifdef ASM_WEAKEN_LABEL
11141 /* We prefer a .weak because that handles duplicates from duplicate
11142 archive members in a graceful way. */
11143 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11144 #else
11145 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11146 #endif
11147 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11150 /* Output debugging information. */
11151 output_compilation_unit_header (dwo_id
11152 ? DW_UT_split_compile : DW_UT_compile);
11153 if (dwarf_version >= 5)
11155 if (dwo_id != NULL)
11156 for (int i = 0; i < 8; i++)
11157 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11159 output_die (die);
11161 /* Leave the marks on the main CU, so we can check them in
11162 output_pubnames. */
11163 if (oldsym)
11165 unmark_dies (die);
11166 die->die_id.die_symbol = oldsym;
11170 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11171 and .debug_pubtypes. This is configured per-target, but can be
11172 overridden by the -gpubnames or -gno-pubnames options. */
11174 static inline bool
11175 want_pubnames (void)
11177 if (debug_info_level <= DINFO_LEVEL_TERSE
11178 /* Names and types go to the early debug part only. */
11179 || in_lto_p)
11180 return false;
11181 if (debug_generate_pub_sections != -1)
11182 return debug_generate_pub_sections;
11183 return targetm.want_debug_pub_sections;
11186 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11188 static void
11189 add_AT_pubnames (dw_die_ref die)
11191 if (want_pubnames ())
11192 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11195 /* Add a string attribute value to a skeleton DIE. */
11197 static inline void
11198 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11199 const char *str)
11201 dw_attr_node attr;
11202 struct indirect_string_node *node;
11204 if (! skeleton_debug_str_hash)
11205 skeleton_debug_str_hash
11206 = hash_table<indirect_string_hasher>::create_ggc (10);
11208 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11209 find_string_form (node);
11210 if (node->form == dwarf_FORM (DW_FORM_strx))
11211 node->form = DW_FORM_strp;
11213 attr.dw_attr = attr_kind;
11214 attr.dw_attr_val.val_class = dw_val_class_str;
11215 attr.dw_attr_val.val_entry = NULL;
11216 attr.dw_attr_val.v.val_str = node;
11217 add_dwarf_attr (die, &attr);
11220 /* Helper function to generate top-level dies for skeleton debug_info and
11221 debug_types. */
11223 static void
11224 add_top_level_skeleton_die_attrs (dw_die_ref die)
11226 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11227 const char *comp_dir = comp_dir_string ();
11229 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11230 if (comp_dir != NULL)
11231 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11232 add_AT_pubnames (die);
11233 if (addr_index_table != NULL && addr_index_table->size () > 0)
11234 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11237 /* Output skeleton debug sections that point to the dwo file. */
11239 static void
11240 output_skeleton_debug_sections (dw_die_ref comp_unit,
11241 const unsigned char *dwo_id)
11243 /* These attributes will be found in the full debug_info section. */
11244 remove_AT (comp_unit, DW_AT_producer);
11245 remove_AT (comp_unit, DW_AT_language);
11247 switch_to_section (debug_skeleton_info_section);
11248 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11250 /* Produce the skeleton compilation-unit header. This one differs enough from
11251 a normal CU header that it's better not to call output_compilation_unit
11252 header. */
11253 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11254 dw2_asm_output_data (4, 0xffffffff,
11255 "Initial length escape value indicating 64-bit "
11256 "DWARF extension");
11258 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11259 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11260 - DWARF_INITIAL_LENGTH_SIZE
11261 + size_of_die (comp_unit),
11262 "Length of Compilation Unit Info");
11263 output_dwarf_version ();
11264 if (dwarf_version >= 5)
11266 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11267 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11269 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
11270 debug_skeleton_abbrev_section,
11271 "Offset Into Abbrev. Section");
11272 if (dwarf_version < 5)
11273 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11274 else
11275 for (int i = 0; i < 8; i++)
11276 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11278 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11279 output_die (comp_unit);
11281 /* Build the skeleton debug_abbrev section. */
11282 switch_to_section (debug_skeleton_abbrev_section);
11283 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11285 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11287 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11290 /* Output a comdat type unit DIE and its children. */
11292 static void
11293 output_comdat_type_unit (comdat_type_node *node,
11294 bool early_lto_debug ATTRIBUTE_UNUSED)
11296 const char *secname;
11297 char *tmp;
11298 int i;
11299 #if defined (OBJECT_FORMAT_ELF)
11300 tree comdat_key;
11301 #endif
11303 /* First mark all the DIEs in this CU so we know which get local refs. */
11304 mark_dies (node->root_die);
11306 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11308 build_abbrev_table (node->root_die, extern_map);
11310 delete extern_map;
11311 extern_map = NULL;
11313 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11314 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11315 calc_die_sizes (node->root_die);
11317 #if defined (OBJECT_FORMAT_ELF)
11318 if (dwarf_version >= 5)
11320 if (!dwarf_split_debug_info)
11321 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11322 else
11323 secname = (early_lto_debug
11324 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11326 else if (!dwarf_split_debug_info)
11327 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11328 else
11329 secname = (early_lto_debug
11330 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11332 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11333 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11334 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11335 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11336 comdat_key = get_identifier (tmp);
11337 targetm.asm_out.named_section (secname,
11338 SECTION_DEBUG | SECTION_LINKONCE,
11339 comdat_key);
11340 #else
11341 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11342 sprintf (tmp, (dwarf_version >= 5
11343 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11344 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11345 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11346 secname = tmp;
11347 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11348 #endif
11350 /* Output debugging information. */
11351 output_compilation_unit_header (dwarf_split_debug_info
11352 ? DW_UT_split_type : DW_UT_type);
11353 output_signature (node->signature, "Type Signature");
11354 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11355 "Offset to Type DIE");
11356 output_die (node->root_die);
11358 unmark_dies (node->root_die);
11361 /* Return the DWARF2/3 pubname associated with a decl. */
11363 static const char *
11364 dwarf2_name (tree decl, int scope)
11366 if (DECL_NAMELESS (decl))
11367 return NULL;
11368 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11371 /* Add a new entry to .debug_pubnames if appropriate. */
11373 static void
11374 add_pubname_string (const char *str, dw_die_ref die)
11376 pubname_entry e;
11378 e.die = die;
11379 e.name = xstrdup (str);
11380 vec_safe_push (pubname_table, e);
11383 static void
11384 add_pubname (tree decl, dw_die_ref die)
11386 if (!want_pubnames ())
11387 return;
11389 /* Don't add items to the table when we expect that the consumer will have
11390 just read the enclosing die. For example, if the consumer is looking at a
11391 class_member, it will either be inside the class already, or will have just
11392 looked up the class to find the member. Either way, searching the class is
11393 faster than searching the index. */
11394 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11395 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11397 const char *name = dwarf2_name (decl, 1);
11399 if (name)
11400 add_pubname_string (name, die);
11404 /* Add an enumerator to the pubnames section. */
11406 static void
11407 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11409 pubname_entry e;
11411 gcc_assert (scope_name);
11412 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11413 e.die = die;
11414 vec_safe_push (pubname_table, e);
11417 /* Add a new entry to .debug_pubtypes if appropriate. */
11419 static void
11420 add_pubtype (tree decl, dw_die_ref die)
11422 pubname_entry e;
11424 if (!want_pubnames ())
11425 return;
11427 if ((TREE_PUBLIC (decl)
11428 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11429 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11431 tree scope = NULL;
11432 const char *scope_name = "";
11433 const char *sep = is_cxx () ? "::" : ".";
11434 const char *name;
11436 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11437 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11439 scope_name = lang_hooks.dwarf_name (scope, 1);
11440 if (scope_name != NULL && scope_name[0] != '\0')
11441 scope_name = concat (scope_name, sep, NULL);
11442 else
11443 scope_name = "";
11446 if (TYPE_P (decl))
11447 name = type_tag (decl);
11448 else
11449 name = lang_hooks.dwarf_name (decl, 1);
11451 /* If we don't have a name for the type, there's no point in adding
11452 it to the table. */
11453 if (name != NULL && name[0] != '\0')
11455 e.die = die;
11456 e.name = concat (scope_name, name, NULL);
11457 vec_safe_push (pubtype_table, e);
11460 /* Although it might be more consistent to add the pubinfo for the
11461 enumerators as their dies are created, they should only be added if the
11462 enum type meets the criteria above. So rather than re-check the parent
11463 enum type whenever an enumerator die is created, just output them all
11464 here. This isn't protected by the name conditional because anonymous
11465 enums don't have names. */
11466 if (die->die_tag == DW_TAG_enumeration_type)
11468 dw_die_ref c;
11470 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11475 /* Output a single entry in the pubnames table. */
11477 static void
11478 output_pubname (dw_offset die_offset, pubname_entry *entry)
11480 dw_die_ref die = entry->die;
11481 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11483 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
11485 if (debug_generate_pub_sections == 2)
11487 /* This logic follows gdb's method for determining the value of the flag
11488 byte. */
11489 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11490 switch (die->die_tag)
11492 case DW_TAG_typedef:
11493 case DW_TAG_base_type:
11494 case DW_TAG_subrange_type:
11495 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11496 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11497 break;
11498 case DW_TAG_enumerator:
11499 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11500 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11501 if (!is_cxx ())
11502 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11503 break;
11504 case DW_TAG_subprogram:
11505 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11506 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11507 if (!is_ada ())
11508 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11509 break;
11510 case DW_TAG_constant:
11511 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11512 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11513 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11514 break;
11515 case DW_TAG_variable:
11516 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11517 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11518 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11519 break;
11520 case DW_TAG_namespace:
11521 case DW_TAG_imported_declaration:
11522 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11523 break;
11524 case DW_TAG_class_type:
11525 case DW_TAG_interface_type:
11526 case DW_TAG_structure_type:
11527 case DW_TAG_union_type:
11528 case DW_TAG_enumeration_type:
11529 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11530 if (!is_cxx ())
11531 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11532 break;
11533 default:
11534 /* An unusual tag. Leave the flag-byte empty. */
11535 break;
11537 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11538 "GDB-index flags");
11541 dw2_asm_output_nstring (entry->name, -1, "external name");
11545 /* Output the public names table used to speed up access to externally
11546 visible names; or the public types table used to find type definitions. */
11548 static void
11549 output_pubnames (vec<pubname_entry, va_gc> *names)
11551 unsigned i;
11552 unsigned long pubnames_length = size_of_pubnames (names);
11553 pubname_entry *pub;
11555 if (!XCOFF_DEBUGGING_INFO)
11557 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11558 dw2_asm_output_data (4, 0xffffffff,
11559 "Initial length escape value indicating 64-bit DWARF extension");
11560 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11561 "Pub Info Length");
11564 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11565 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11567 if (dwarf_split_debug_info)
11568 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11569 debug_skeleton_info_section,
11570 "Offset of Compilation Unit Info");
11571 else
11572 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11573 debug_info_section,
11574 "Offset of Compilation Unit Info");
11575 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11576 "Compilation Unit Length");
11578 FOR_EACH_VEC_ELT (*names, i, pub)
11580 if (include_pubname_in_output (names, pub))
11582 dw_offset die_offset = pub->die->die_offset;
11584 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11585 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11586 gcc_assert (pub->die->die_mark);
11588 /* If we're putting types in their own .debug_types sections,
11589 the .debug_pubtypes table will still point to the compile
11590 unit (not the type unit), so we want to use the offset of
11591 the skeleton DIE (if there is one). */
11592 if (pub->die->comdat_type_p && names == pubtype_table)
11594 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11596 if (type_node != NULL)
11597 die_offset = (type_node->skeleton_die != NULL
11598 ? type_node->skeleton_die->die_offset
11599 : comp_unit_die ()->die_offset);
11602 output_pubname (die_offset, pub);
11606 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11609 /* Output public names and types tables if necessary. */
11611 static void
11612 output_pubtables (void)
11614 if (!want_pubnames () || !info_section_emitted)
11615 return;
11617 switch_to_section (debug_pubnames_section);
11618 output_pubnames (pubname_table);
11619 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11620 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11621 simply won't look for the section. */
11622 switch_to_section (debug_pubtypes_section);
11623 output_pubnames (pubtype_table);
11627 /* Output the information that goes into the .debug_aranges table.
11628 Namely, define the beginning and ending address range of the
11629 text section generated for this compilation unit. */
11631 static void
11632 output_aranges (void)
11634 unsigned i;
11635 unsigned long aranges_length = size_of_aranges ();
11637 if (!XCOFF_DEBUGGING_INFO)
11639 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11640 dw2_asm_output_data (4, 0xffffffff,
11641 "Initial length escape value indicating 64-bit DWARF extension");
11642 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11643 "Length of Address Ranges Info");
11646 /* Version number for aranges is still 2, even up to DWARF5. */
11647 dw2_asm_output_data (2, 2, "DWARF aranges version");
11648 if (dwarf_split_debug_info)
11649 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11650 debug_skeleton_info_section,
11651 "Offset of Compilation Unit Info");
11652 else
11653 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11654 debug_info_section,
11655 "Offset of Compilation Unit Info");
11656 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11657 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11659 /* We need to align to twice the pointer size here. */
11660 if (DWARF_ARANGES_PAD_SIZE)
11662 /* Pad using a 2 byte words so that padding is correct for any
11663 pointer size. */
11664 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11665 2 * DWARF2_ADDR_SIZE);
11666 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11667 dw2_asm_output_data (2, 0, NULL);
11670 /* It is necessary not to output these entries if the sections were
11671 not used; if the sections were not used, the length will be 0 and
11672 the address may end up as 0 if the section is discarded by ld
11673 --gc-sections, leaving an invalid (0, 0) entry that can be
11674 confused with the terminator. */
11675 if (text_section_used)
11677 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11678 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11679 text_section_label, "Length");
11681 if (cold_text_section_used)
11683 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11684 "Address");
11685 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11686 cold_text_section_label, "Length");
11689 if (have_multiple_function_sections)
11691 unsigned fde_idx;
11692 dw_fde_ref fde;
11694 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11696 if (DECL_IGNORED_P (fde->decl))
11697 continue;
11698 if (!fde->in_std_section)
11700 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11701 "Address");
11702 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11703 fde->dw_fde_begin, "Length");
11705 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11707 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11708 "Address");
11709 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11710 fde->dw_fde_second_begin, "Length");
11715 /* Output the terminator words. */
11716 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11717 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11720 /* Add a new entry to .debug_ranges. Return its index into
11721 ranges_table vector. */
11723 static unsigned int
11724 add_ranges_num (int num, bool maybe_new_sec)
11726 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11727 vec_safe_push (ranges_table, r);
11728 return vec_safe_length (ranges_table) - 1;
11731 /* Add a new entry to .debug_ranges corresponding to a block, or a
11732 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11733 this entry might be in a different section from previous range. */
11735 static unsigned int
11736 add_ranges (const_tree block, bool maybe_new_sec)
11738 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11741 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11742 chain, or middle entry of a chain that will be directly referred to. */
11744 static void
11745 note_rnglist_head (unsigned int offset)
11747 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11748 return;
11749 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11752 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11753 When using dwarf_split_debug_info, address attributes in dies destined
11754 for the final executable should be direct references--setting the
11755 parameter force_direct ensures this behavior. */
11757 static void
11758 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11759 bool *added, bool force_direct)
11761 unsigned int in_use = vec_safe_length (ranges_by_label);
11762 unsigned int offset;
11763 dw_ranges_by_label rbl = { begin, end };
11764 vec_safe_push (ranges_by_label, rbl);
11765 offset = add_ranges_num (-(int)in_use - 1, true);
11766 if (!*added)
11768 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11769 *added = true;
11770 note_rnglist_head (offset);
11774 /* Emit .debug_ranges section. */
11776 static void
11777 output_ranges (void)
11779 unsigned i;
11780 static const char *const start_fmt = "Offset %#x";
11781 const char *fmt = start_fmt;
11782 dw_ranges *r;
11784 switch_to_section (debug_ranges_section);
11785 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11786 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11788 int block_num = r->num;
11790 if (block_num > 0)
11792 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11793 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11795 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11796 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11798 /* If all code is in the text section, then the compilation
11799 unit base address defaults to DW_AT_low_pc, which is the
11800 base of the text section. */
11801 if (!have_multiple_function_sections)
11803 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11804 text_section_label,
11805 fmt, i * 2 * DWARF2_ADDR_SIZE);
11806 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11807 text_section_label, NULL);
11810 /* Otherwise, the compilation unit base address is zero,
11811 which allows us to use absolute addresses, and not worry
11812 about whether the target supports cross-section
11813 arithmetic. */
11814 else
11816 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11817 fmt, i * 2 * DWARF2_ADDR_SIZE);
11818 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11821 fmt = NULL;
11824 /* Negative block_num stands for an index into ranges_by_label. */
11825 else if (block_num < 0)
11827 int lab_idx = - block_num - 1;
11829 if (!have_multiple_function_sections)
11831 gcc_unreachable ();
11832 #if 0
11833 /* If we ever use add_ranges_by_labels () for a single
11834 function section, all we have to do is to take out
11835 the #if 0 above. */
11836 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11837 (*ranges_by_label)[lab_idx].begin,
11838 text_section_label,
11839 fmt, i * 2 * DWARF2_ADDR_SIZE);
11840 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11841 (*ranges_by_label)[lab_idx].end,
11842 text_section_label, NULL);
11843 #endif
11845 else
11847 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11848 (*ranges_by_label)[lab_idx].begin,
11849 fmt, i * 2 * DWARF2_ADDR_SIZE);
11850 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11851 (*ranges_by_label)[lab_idx].end,
11852 NULL);
11855 else
11857 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11858 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11859 fmt = start_fmt;
11864 /* Non-zero if .debug_line_str should be used for .debug_line section
11865 strings or strings that are likely shareable with those. */
11866 #define DWARF5_USE_DEBUG_LINE_STR \
11867 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11868 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11869 /* FIXME: there is no .debug_line_str.dwo section, \
11870 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11871 && !dwarf_split_debug_info)
11874 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
11875 DWARF5 .debug_line tables using .debug_line_str or we generate
11876 it ourselves, except for split-dwarf which doesn't have a
11877 .debug_line_str. */
11878 static bool
11879 asm_outputs_debug_line_str (void)
11881 if (dwarf_version >= 5
11882 && ! output_asm_line_debug_info ()
11883 && DWARF5_USE_DEBUG_LINE_STR)
11884 return true;
11885 else
11887 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
11888 return !dwarf_split_debug_info && dwarf_version >= 5;
11889 #else
11890 return false;
11891 #endif
11896 /* Assign .debug_rnglists indexes. */
11898 static void
11899 index_rnglists (void)
11901 unsigned i;
11902 dw_ranges *r;
11904 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11905 if (r->label)
11906 r->idx = rnglist_idx++;
11909 /* Emit .debug_rnglists section. */
11911 static void
11912 output_rnglists (unsigned generation)
11914 unsigned i;
11915 dw_ranges *r;
11916 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11917 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11918 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11920 switch_to_section (debug_ranges_section);
11921 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11922 /* There are up to 4 unique ranges labels per generation.
11923 See also init_sections_and_labels. */
11924 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11925 2 + generation * 4);
11926 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11927 3 + generation * 4);
11928 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11929 dw2_asm_output_data (4, 0xffffffff,
11930 "Initial length escape value indicating "
11931 "64-bit DWARF extension");
11932 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11933 "Length of Range Lists");
11934 ASM_OUTPUT_LABEL (asm_out_file, l1);
11935 output_dwarf_version ();
11936 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11937 dw2_asm_output_data (1, 0, "Segment Size");
11938 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11939 about relocation sizes and primarily care about the size of .debug*
11940 sections in linked shared libraries and executables, then
11941 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11942 into it are usually larger than just DW_FORM_sec_offset offsets
11943 into the .debug_rnglists section. */
11944 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11945 "Offset Entry Count");
11946 if (dwarf_split_debug_info)
11948 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11949 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11950 if (r->label)
11951 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11952 ranges_base_label, NULL);
11955 const char *lab = "";
11956 unsigned int len = vec_safe_length (ranges_table);
11957 const char *base = NULL;
11958 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11960 int block_num = r->num;
11962 if (r->label)
11964 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11965 lab = r->label;
11967 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11968 base = NULL;
11969 if (block_num > 0)
11971 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11972 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11974 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11975 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11977 if (HAVE_AS_LEB128)
11979 /* If all code is in the text section, then the compilation
11980 unit base address defaults to DW_AT_low_pc, which is the
11981 base of the text section. */
11982 if (!have_multiple_function_sections)
11984 dw2_asm_output_data (1, DW_RLE_offset_pair,
11985 "DW_RLE_offset_pair (%s)", lab);
11986 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11987 "Range begin address (%s)", lab);
11988 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11989 "Range end address (%s)", lab);
11990 continue;
11992 if (base == NULL)
11994 dw_ranges *r2 = NULL;
11995 if (i < len - 1)
11996 r2 = &(*ranges_table)[i + 1];
11997 if (r2
11998 && r2->num != 0
11999 && r2->label == NULL
12000 && !r2->maybe_new_sec)
12002 dw2_asm_output_data (1, DW_RLE_base_address,
12003 "DW_RLE_base_address (%s)", lab);
12004 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12005 "Base address (%s)", lab);
12006 strcpy (basebuf, blabel);
12007 base = basebuf;
12010 if (base)
12012 dw2_asm_output_data (1, DW_RLE_offset_pair,
12013 "DW_RLE_offset_pair (%s)", lab);
12014 dw2_asm_output_delta_uleb128 (blabel, base,
12015 "Range begin address (%s)", lab);
12016 dw2_asm_output_delta_uleb128 (elabel, base,
12017 "Range end address (%s)", lab);
12018 continue;
12020 dw2_asm_output_data (1, DW_RLE_start_length,
12021 "DW_RLE_start_length (%s)", lab);
12022 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12023 "Range begin address (%s)", lab);
12024 dw2_asm_output_delta_uleb128 (elabel, blabel,
12025 "Range length (%s)", lab);
12027 else
12029 dw2_asm_output_data (1, DW_RLE_start_end,
12030 "DW_RLE_start_end (%s)", lab);
12031 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12032 "Range begin address (%s)", lab);
12033 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12034 "Range end address (%s)", lab);
12038 /* Negative block_num stands for an index into ranges_by_label. */
12039 else if (block_num < 0)
12041 int lab_idx = - block_num - 1;
12042 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12043 const char *elabel = (*ranges_by_label)[lab_idx].end;
12045 if (!have_multiple_function_sections)
12046 gcc_unreachable ();
12047 if (HAVE_AS_LEB128)
12049 dw2_asm_output_data (1, DW_RLE_start_length,
12050 "DW_RLE_start_length (%s)", lab);
12051 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12052 "Range begin address (%s)", lab);
12053 dw2_asm_output_delta_uleb128 (elabel, blabel,
12054 "Range length (%s)", lab);
12056 else
12058 dw2_asm_output_data (1, DW_RLE_start_end,
12059 "DW_RLE_start_end (%s)", lab);
12060 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12061 "Range begin address (%s)", lab);
12062 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12063 "Range end address (%s)", lab);
12066 else
12067 dw2_asm_output_data (1, DW_RLE_end_of_list,
12068 "DW_RLE_end_of_list (%s)", lab);
12070 ASM_OUTPUT_LABEL (asm_out_file, l2);
12073 /* Data structure containing information about input files. */
12074 struct file_info
12076 const char *path; /* Complete file name. */
12077 const char *fname; /* File name part. */
12078 int length; /* Length of entire string. */
12079 struct dwarf_file_data * file_idx; /* Index in input file table. */
12080 int dir_idx; /* Index in directory table. */
12083 /* Data structure containing information about directories with source
12084 files. */
12085 struct dir_info
12087 const char *path; /* Path including directory name. */
12088 int length; /* Path length. */
12089 int prefix; /* Index of directory entry which is a prefix. */
12090 int count; /* Number of files in this directory. */
12091 int dir_idx; /* Index of directory used as base. */
12094 /* Callback function for file_info comparison. We sort by looking at
12095 the directories in the path. */
12097 static int
12098 file_info_cmp (const void *p1, const void *p2)
12100 const struct file_info *const s1 = (const struct file_info *) p1;
12101 const struct file_info *const s2 = (const struct file_info *) p2;
12102 const unsigned char *cp1;
12103 const unsigned char *cp2;
12105 /* Take care of file names without directories. We need to make sure that
12106 we return consistent values to qsort since some will get confused if
12107 we return the same value when identical operands are passed in opposite
12108 orders. So if neither has a directory, return 0 and otherwise return
12109 1 or -1 depending on which one has the directory. We want the one with
12110 the directory to sort after the one without, so all no directory files
12111 are at the start (normally only the compilation unit file). */
12112 if ((s1->path == s1->fname || s2->path == s2->fname))
12113 return (s2->path == s2->fname) - (s1->path == s1->fname);
12115 cp1 = (const unsigned char *) s1->path;
12116 cp2 = (const unsigned char *) s2->path;
12118 while (1)
12120 ++cp1;
12121 ++cp2;
12122 /* Reached the end of the first path? If so, handle like above,
12123 but now we want longer directory prefixes before shorter ones. */
12124 if ((cp1 == (const unsigned char *) s1->fname)
12125 || (cp2 == (const unsigned char *) s2->fname))
12126 return ((cp1 == (const unsigned char *) s1->fname)
12127 - (cp2 == (const unsigned char *) s2->fname));
12129 /* Character of current path component the same? */
12130 else if (*cp1 != *cp2)
12131 return *cp1 - *cp2;
12135 struct file_name_acquire_data
12137 struct file_info *files;
12138 int used_files;
12139 int max_files;
12142 /* Traversal function for the hash table. */
12145 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12147 struct dwarf_file_data *d = *slot;
12148 struct file_info *fi;
12149 const char *f;
12151 gcc_assert (fnad->max_files >= d->emitted_number);
12153 if (! d->emitted_number)
12154 return 1;
12156 gcc_assert (fnad->max_files != fnad->used_files);
12158 fi = fnad->files + fnad->used_files++;
12160 f = remap_debug_filename (d->filename);
12162 /* Skip all leading "./". */
12163 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12164 f += 2;
12166 /* Create a new array entry. */
12167 fi->path = f;
12168 fi->length = strlen (f);
12169 fi->file_idx = d;
12171 /* Search for the file name part. */
12172 f = strrchr (f, DIR_SEPARATOR);
12173 #if defined (DIR_SEPARATOR_2)
12175 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12177 if (g != NULL)
12179 if (f == NULL || f < g)
12180 f = g;
12183 #endif
12185 fi->fname = f == NULL ? fi->path : f + 1;
12186 return 1;
12189 /* Helper function for output_file_names. Emit a FORM encoded
12190 string STR, with assembly comment start ENTRY_KIND and
12191 index IDX */
12193 static void
12194 output_line_string (enum dwarf_form form, const char *str,
12195 const char *entry_kind, unsigned int idx)
12197 switch (form)
12199 case DW_FORM_string:
12200 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12201 break;
12202 case DW_FORM_line_strp:
12203 if (!debug_line_str_hash)
12204 debug_line_str_hash
12205 = hash_table<indirect_string_hasher>::create_ggc (10);
12207 struct indirect_string_node *node;
12208 node = find_AT_string_in_table (str, debug_line_str_hash);
12209 set_indirect_string (node);
12210 node->form = form;
12211 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
12212 debug_line_str_section, "%s: %#x: \"%s\"",
12213 entry_kind, 0, node->str);
12214 break;
12215 default:
12216 gcc_unreachable ();
12220 /* Output the directory table and the file name table. We try to minimize
12221 the total amount of memory needed. A heuristic is used to avoid large
12222 slowdowns with many input files. */
12224 static void
12225 output_file_names (void)
12227 struct file_name_acquire_data fnad;
12228 int numfiles;
12229 struct file_info *files;
12230 struct dir_info *dirs;
12231 int *saved;
12232 int *savehere;
12233 int *backmap;
12234 int ndirs;
12235 int idx_offset;
12236 int i;
12238 if (!last_emitted_file)
12240 if (dwarf_version >= 5)
12242 dw2_asm_output_data (1, 0, "Directory entry format count");
12243 dw2_asm_output_data_uleb128 (0, "Directories count");
12244 dw2_asm_output_data (1, 0, "File name entry format count");
12245 dw2_asm_output_data_uleb128 (0, "File names count");
12247 else
12249 dw2_asm_output_data (1, 0, "End directory table");
12250 dw2_asm_output_data (1, 0, "End file name table");
12252 return;
12255 numfiles = last_emitted_file->emitted_number;
12257 /* Allocate the various arrays we need. */
12258 files = XALLOCAVEC (struct file_info, numfiles);
12259 dirs = XALLOCAVEC (struct dir_info, numfiles);
12261 fnad.files = files;
12262 fnad.used_files = 0;
12263 fnad.max_files = numfiles;
12264 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12265 gcc_assert (fnad.used_files == fnad.max_files);
12267 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12269 /* Find all the different directories used. */
12270 dirs[0].path = files[0].path;
12271 dirs[0].length = files[0].fname - files[0].path;
12272 dirs[0].prefix = -1;
12273 dirs[0].count = 1;
12274 dirs[0].dir_idx = 0;
12275 files[0].dir_idx = 0;
12276 ndirs = 1;
12278 for (i = 1; i < numfiles; i++)
12279 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12280 && memcmp (dirs[ndirs - 1].path, files[i].path,
12281 dirs[ndirs - 1].length) == 0)
12283 /* Same directory as last entry. */
12284 files[i].dir_idx = ndirs - 1;
12285 ++dirs[ndirs - 1].count;
12287 else
12289 int j;
12291 /* This is a new directory. */
12292 dirs[ndirs].path = files[i].path;
12293 dirs[ndirs].length = files[i].fname - files[i].path;
12294 dirs[ndirs].count = 1;
12295 dirs[ndirs].dir_idx = ndirs;
12296 files[i].dir_idx = ndirs;
12298 /* Search for a prefix. */
12299 dirs[ndirs].prefix = -1;
12300 for (j = 0; j < ndirs; j++)
12301 if (dirs[j].length < dirs[ndirs].length
12302 && dirs[j].length > 1
12303 && (dirs[ndirs].prefix == -1
12304 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12305 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12306 dirs[ndirs].prefix = j;
12308 ++ndirs;
12311 /* Now to the actual work. We have to find a subset of the directories which
12312 allow expressing the file name using references to the directory table
12313 with the least amount of characters. We do not do an exhaustive search
12314 where we would have to check out every combination of every single
12315 possible prefix. Instead we use a heuristic which provides nearly optimal
12316 results in most cases and never is much off. */
12317 saved = XALLOCAVEC (int, ndirs);
12318 savehere = XALLOCAVEC (int, ndirs);
12320 memset (saved, '\0', ndirs * sizeof (saved[0]));
12321 for (i = 0; i < ndirs; i++)
12323 int j;
12324 int total;
12326 /* We can always save some space for the current directory. But this
12327 does not mean it will be enough to justify adding the directory. */
12328 savehere[i] = dirs[i].length;
12329 total = (savehere[i] - saved[i]) * dirs[i].count;
12331 for (j = i + 1; j < ndirs; j++)
12333 savehere[j] = 0;
12334 if (saved[j] < dirs[i].length)
12336 /* Determine whether the dirs[i] path is a prefix of the
12337 dirs[j] path. */
12338 int k;
12340 k = dirs[j].prefix;
12341 while (k != -1 && k != (int) i)
12342 k = dirs[k].prefix;
12344 if (k == (int) i)
12346 /* Yes it is. We can possibly save some memory by
12347 writing the filenames in dirs[j] relative to
12348 dirs[i]. */
12349 savehere[j] = dirs[i].length;
12350 total += (savehere[j] - saved[j]) * dirs[j].count;
12355 /* Check whether we can save enough to justify adding the dirs[i]
12356 directory. */
12357 if (total > dirs[i].length + 1)
12359 /* It's worthwhile adding. */
12360 for (j = i; j < ndirs; j++)
12361 if (savehere[j] > 0)
12363 /* Remember how much we saved for this directory so far. */
12364 saved[j] = savehere[j];
12366 /* Remember the prefix directory. */
12367 dirs[j].dir_idx = i;
12372 /* Emit the directory name table. */
12373 idx_offset = dirs[0].length > 0 ? 1 : 0;
12374 enum dwarf_form str_form = DW_FORM_string;
12375 enum dwarf_form idx_form = DW_FORM_udata;
12376 if (dwarf_version >= 5)
12378 const char *comp_dir = comp_dir_string ();
12379 if (comp_dir == NULL)
12380 comp_dir = "";
12381 dw2_asm_output_data (1, 1, "Directory entry format count");
12382 if (DWARF5_USE_DEBUG_LINE_STR)
12383 str_form = DW_FORM_line_strp;
12384 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12385 dw2_asm_output_data_uleb128 (str_form, "%s",
12386 get_DW_FORM_name (str_form));
12387 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12388 if (str_form == DW_FORM_string)
12390 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12391 for (i = 1 - idx_offset; i < ndirs; i++)
12392 dw2_asm_output_nstring (dirs[i].path,
12393 dirs[i].length
12394 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12395 "Directory Entry: %#x", i + idx_offset);
12397 else
12399 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12400 for (i = 1 - idx_offset; i < ndirs; i++)
12402 const char *str
12403 = ggc_alloc_string (dirs[i].path,
12404 dirs[i].length
12405 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12406 output_line_string (str_form, str, "Directory Entry",
12407 (unsigned) i + idx_offset);
12411 else
12413 for (i = 1 - idx_offset; i < ndirs; i++)
12414 dw2_asm_output_nstring (dirs[i].path,
12415 dirs[i].length
12416 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12417 "Directory Entry: %#x", i + idx_offset);
12419 dw2_asm_output_data (1, 0, "End directory table");
12422 /* We have to emit them in the order of emitted_number since that's
12423 used in the debug info generation. To do this efficiently we
12424 generate a back-mapping of the indices first. */
12425 backmap = XALLOCAVEC (int, numfiles);
12426 for (i = 0; i < numfiles; i++)
12427 backmap[files[i].file_idx->emitted_number - 1] = i;
12429 if (dwarf_version >= 5)
12431 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12432 if (filename0 == NULL)
12433 filename0 = "";
12434 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12435 DW_FORM_data2. Choose one based on the number of directories
12436 and how much space would they occupy in each encoding.
12437 If we have at most 256 directories, all indexes fit into
12438 a single byte, so DW_FORM_data1 is most compact (if there
12439 are at most 128 directories, DW_FORM_udata would be as
12440 compact as that, but not shorter and slower to decode). */
12441 if (ndirs + idx_offset <= 256)
12442 idx_form = DW_FORM_data1;
12443 /* If there are more than 65536 directories, we have to use
12444 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12445 Otherwise, compute what space would occupy if all the indexes
12446 used DW_FORM_udata - sum - and compare that to how large would
12447 be DW_FORM_data2 encoding, and pick the more efficient one. */
12448 else if (ndirs + idx_offset <= 65536)
12450 unsigned HOST_WIDE_INT sum = 1;
12451 for (i = 0; i < numfiles; i++)
12453 int file_idx = backmap[i];
12454 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12455 sum += size_of_uleb128 (dir_idx);
12457 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12458 idx_form = DW_FORM_data2;
12460 #ifdef VMS_DEBUGGING_INFO
12461 dw2_asm_output_data (1, 4, "File name entry format count");
12462 #else
12463 dw2_asm_output_data (1, 2, "File name entry format count");
12464 #endif
12465 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12466 dw2_asm_output_data_uleb128 (str_form, "%s",
12467 get_DW_FORM_name (str_form));
12468 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12469 "DW_LNCT_directory_index");
12470 dw2_asm_output_data_uleb128 (idx_form, "%s",
12471 get_DW_FORM_name (idx_form));
12472 #ifdef VMS_DEBUGGING_INFO
12473 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12474 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12475 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12476 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12477 #endif
12478 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12480 output_line_string (str_form, filename0, "File Entry", 0);
12482 /* Include directory index. */
12483 if (idx_form != DW_FORM_udata)
12484 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12485 0, NULL);
12486 else
12487 dw2_asm_output_data_uleb128 (0, NULL);
12489 #ifdef VMS_DEBUGGING_INFO
12490 dw2_asm_output_data_uleb128 (0, NULL);
12491 dw2_asm_output_data_uleb128 (0, NULL);
12492 #endif
12495 /* Now write all the file names. */
12496 for (i = 0; i < numfiles; i++)
12498 int file_idx = backmap[i];
12499 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12501 #ifdef VMS_DEBUGGING_INFO
12502 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12504 /* Setting these fields can lead to debugger miscomparisons,
12505 but VMS Debug requires them to be set correctly. */
12507 int ver;
12508 long long cdt;
12509 long siz;
12510 int maxfilelen = (strlen (files[file_idx].path)
12511 + dirs[dir_idx].length
12512 + MAX_VMS_VERSION_LEN + 1);
12513 char *filebuf = XALLOCAVEC (char, maxfilelen);
12515 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12516 snprintf (filebuf, maxfilelen, "%s;%d",
12517 files[file_idx].path + dirs[dir_idx].length, ver);
12519 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12521 /* Include directory index. */
12522 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12523 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12524 dir_idx + idx_offset, NULL);
12525 else
12526 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12528 /* Modification time. */
12529 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12530 &cdt, 0, 0, 0) == 0)
12531 ? cdt : 0, NULL);
12533 /* File length in bytes. */
12534 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12535 0, &siz, 0, 0) == 0)
12536 ? siz : 0, NULL);
12537 #else
12538 output_line_string (str_form,
12539 files[file_idx].path + dirs[dir_idx].length,
12540 "File Entry", (unsigned) i + 1);
12542 /* Include directory index. */
12543 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12544 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12545 dir_idx + idx_offset, NULL);
12546 else
12547 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12549 if (dwarf_version >= 5)
12550 continue;
12552 /* Modification time. */
12553 dw2_asm_output_data_uleb128 (0, NULL);
12555 /* File length in bytes. */
12556 dw2_asm_output_data_uleb128 (0, NULL);
12557 #endif /* VMS_DEBUGGING_INFO */
12560 if (dwarf_version < 5)
12561 dw2_asm_output_data (1, 0, "End file name table");
12565 /* Output one line number table into the .debug_line section. */
12567 static void
12568 output_one_line_info_table (dw_line_info_table *table)
12570 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12571 unsigned int current_line = 1;
12572 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12573 dw_line_info_entry *ent, *prev_addr;
12574 size_t i;
12575 unsigned int view;
12577 view = 0;
12579 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12581 switch (ent->opcode)
12583 case LI_set_address:
12584 /* ??? Unfortunately, we have little choice here currently, and
12585 must always use the most general form. GCC does not know the
12586 address delta itself, so we can't use DW_LNS_advance_pc. Many
12587 ports do have length attributes which will give an upper bound
12588 on the address range. We could perhaps use length attributes
12589 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12590 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12592 view = 0;
12594 /* This can handle any delta. This takes
12595 4+DWARF2_ADDR_SIZE bytes. */
12596 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12597 debug_variable_location_views
12598 ? ", reset view to 0" : "");
12599 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12600 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12601 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12603 prev_addr = ent;
12604 break;
12606 case LI_adv_address:
12608 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12609 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12610 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12612 view++;
12614 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12615 dw2_asm_output_delta (2, line_label, prev_label,
12616 "from %s to %s", prev_label, line_label);
12618 prev_addr = ent;
12619 break;
12622 case LI_set_line:
12623 if (ent->val == current_line)
12625 /* We still need to start a new row, so output a copy insn. */
12626 dw2_asm_output_data (1, DW_LNS_copy,
12627 "copy line %u", current_line);
12629 else
12631 int line_offset = ent->val - current_line;
12632 int line_delta = line_offset - DWARF_LINE_BASE;
12634 current_line = ent->val;
12635 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12637 /* This can handle deltas from -10 to 234, using the current
12638 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12639 This takes 1 byte. */
12640 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12641 "line %u", current_line);
12643 else
12645 /* This can handle any delta. This takes at least 4 bytes,
12646 depending on the value being encoded. */
12647 dw2_asm_output_data (1, DW_LNS_advance_line,
12648 "advance to line %u", current_line);
12649 dw2_asm_output_data_sleb128 (line_offset, NULL);
12650 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12653 break;
12655 case LI_set_file:
12656 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12657 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12658 break;
12660 case LI_set_column:
12661 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12662 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12663 break;
12665 case LI_negate_stmt:
12666 current_is_stmt = !current_is_stmt;
12667 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12668 "is_stmt %d", current_is_stmt);
12669 break;
12671 case LI_set_prologue_end:
12672 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12673 "set prologue end");
12674 break;
12676 case LI_set_epilogue_begin:
12677 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12678 "set epilogue begin");
12679 break;
12681 case LI_set_discriminator:
12682 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12683 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12684 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12685 dw2_asm_output_data_uleb128 (ent->val, NULL);
12686 break;
12690 /* Emit debug info for the address of the end of the table. */
12691 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12692 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12693 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12694 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12696 dw2_asm_output_data (1, 0, "end sequence");
12697 dw2_asm_output_data_uleb128 (1, NULL);
12698 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12701 /* Output the source line number correspondence information. This
12702 information goes into the .debug_line section. */
12704 static void
12705 output_line_info (bool prologue_only)
12707 static unsigned int generation;
12708 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
12709 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
12710 bool saw_one = false;
12711 int opc;
12713 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
12714 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
12715 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
12716 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
12718 if (!XCOFF_DEBUGGING_INFO)
12720 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12721 dw2_asm_output_data (4, 0xffffffff,
12722 "Initial length escape value indicating 64-bit DWARF extension");
12723 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12724 "Length of Source Line Info");
12727 ASM_OUTPUT_LABEL (asm_out_file, l1);
12729 output_dwarf_version ();
12730 if (dwarf_version >= 5)
12732 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12733 dw2_asm_output_data (1, 0, "Segment Size");
12735 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12736 ASM_OUTPUT_LABEL (asm_out_file, p1);
12738 /* Define the architecture-dependent minimum instruction length (in bytes).
12739 In this implementation of DWARF, this field is used for information
12740 purposes only. Since GCC generates assembly language, we have no
12741 a priori knowledge of how many instruction bytes are generated for each
12742 source line, and therefore can use only the DW_LNE_set_address and
12743 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12744 this as '1', which is "correct enough" for all architectures,
12745 and don't let the target override. */
12746 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12748 if (dwarf_version >= 4)
12749 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12750 "Maximum Operations Per Instruction");
12751 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12752 "Default is_stmt_start flag");
12753 dw2_asm_output_data (1, DWARF_LINE_BASE,
12754 "Line Base Value (Special Opcodes)");
12755 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12756 "Line Range Value (Special Opcodes)");
12757 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12758 "Special Opcode Base");
12760 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12762 int n_op_args;
12763 switch (opc)
12765 case DW_LNS_advance_pc:
12766 case DW_LNS_advance_line:
12767 case DW_LNS_set_file:
12768 case DW_LNS_set_column:
12769 case DW_LNS_fixed_advance_pc:
12770 case DW_LNS_set_isa:
12771 n_op_args = 1;
12772 break;
12773 default:
12774 n_op_args = 0;
12775 break;
12778 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12779 opc, n_op_args);
12782 /* Write out the information about the files we use. */
12783 output_file_names ();
12784 ASM_OUTPUT_LABEL (asm_out_file, p2);
12785 if (prologue_only)
12787 /* Output the marker for the end of the line number info. */
12788 ASM_OUTPUT_LABEL (asm_out_file, l2);
12789 return;
12792 if (separate_line_info)
12794 dw_line_info_table *table;
12795 size_t i;
12797 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12798 if (table->in_use)
12800 output_one_line_info_table (table);
12801 saw_one = true;
12804 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12806 output_one_line_info_table (cold_text_section_line_info);
12807 saw_one = true;
12810 /* ??? Some Darwin linkers crash on a .debug_line section with no
12811 sequences. Further, merely a DW_LNE_end_sequence entry is not
12812 sufficient -- the address column must also be initialized.
12813 Make sure to output at least one set_address/end_sequence pair,
12814 choosing .text since that section is always present. */
12815 if (text_section_line_info->in_use || !saw_one)
12816 output_one_line_info_table (text_section_line_info);
12818 /* Output the marker for the end of the line number info. */
12819 ASM_OUTPUT_LABEL (asm_out_file, l2);
12822 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12824 static inline bool
12825 need_endianity_attribute_p (bool reverse)
12827 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12830 /* Given a pointer to a tree node for some base type, return a pointer to
12831 a DIE that describes the given type. REVERSE is true if the type is
12832 to be interpreted in the reverse storage order wrt the target order.
12834 This routine must only be called for GCC type nodes that correspond to
12835 Dwarf base (fundamental) types. */
12837 static dw_die_ref
12838 base_type_die (tree type, bool reverse)
12840 dw_die_ref base_type_result;
12841 enum dwarf_type encoding;
12842 bool fpt_used = false;
12843 struct fixed_point_type_info fpt_info;
12844 tree type_bias = NULL_TREE;
12846 /* If this is a subtype that should not be emitted as a subrange type,
12847 use the base type. See subrange_type_for_debug_p. */
12848 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12849 type = TREE_TYPE (type);
12851 switch (TREE_CODE (type))
12853 case INTEGER_TYPE:
12854 if ((dwarf_version >= 4 || !dwarf_strict)
12855 && TYPE_NAME (type)
12856 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12857 && DECL_IS_BUILTIN (TYPE_NAME (type))
12858 && DECL_NAME (TYPE_NAME (type)))
12860 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12861 if (strcmp (name, "char16_t") == 0
12862 || strcmp (name, "char32_t") == 0)
12864 encoding = DW_ATE_UTF;
12865 break;
12868 if ((dwarf_version >= 3 || !dwarf_strict)
12869 && lang_hooks.types.get_fixed_point_type_info)
12871 memset (&fpt_info, 0, sizeof (fpt_info));
12872 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12874 fpt_used = true;
12875 encoding = ((TYPE_UNSIGNED (type))
12876 ? DW_ATE_unsigned_fixed
12877 : DW_ATE_signed_fixed);
12878 break;
12881 if (TYPE_STRING_FLAG (type))
12883 if (TYPE_UNSIGNED (type))
12884 encoding = DW_ATE_unsigned_char;
12885 else
12886 encoding = DW_ATE_signed_char;
12888 else if (TYPE_UNSIGNED (type))
12889 encoding = DW_ATE_unsigned;
12890 else
12891 encoding = DW_ATE_signed;
12893 if (!dwarf_strict
12894 && lang_hooks.types.get_type_bias)
12895 type_bias = lang_hooks.types.get_type_bias (type);
12896 break;
12898 case REAL_TYPE:
12899 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12901 if (dwarf_version >= 3 || !dwarf_strict)
12902 encoding = DW_ATE_decimal_float;
12903 else
12904 encoding = DW_ATE_lo_user;
12906 else
12907 encoding = DW_ATE_float;
12908 break;
12910 case FIXED_POINT_TYPE:
12911 if (!(dwarf_version >= 3 || !dwarf_strict))
12912 encoding = DW_ATE_lo_user;
12913 else if (TYPE_UNSIGNED (type))
12914 encoding = DW_ATE_unsigned_fixed;
12915 else
12916 encoding = DW_ATE_signed_fixed;
12917 break;
12919 /* Dwarf2 doesn't know anything about complex ints, so use
12920 a user defined type for it. */
12921 case COMPLEX_TYPE:
12922 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12923 encoding = DW_ATE_complex_float;
12924 else
12925 encoding = DW_ATE_lo_user;
12926 break;
12928 case BOOLEAN_TYPE:
12929 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12930 encoding = DW_ATE_boolean;
12931 break;
12933 default:
12934 /* No other TREE_CODEs are Dwarf fundamental types. */
12935 gcc_unreachable ();
12938 base_type_result = new_die_raw (DW_TAG_base_type);
12940 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12941 int_size_in_bytes (type));
12942 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12944 if (need_endianity_attribute_p (reverse))
12945 add_AT_unsigned (base_type_result, DW_AT_endianity,
12946 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12948 add_alignment_attribute (base_type_result, type);
12950 if (fpt_used)
12952 switch (fpt_info.scale_factor_kind)
12954 case fixed_point_scale_factor_binary:
12955 add_AT_int (base_type_result, DW_AT_binary_scale,
12956 fpt_info.scale_factor.binary);
12957 break;
12959 case fixed_point_scale_factor_decimal:
12960 add_AT_int (base_type_result, DW_AT_decimal_scale,
12961 fpt_info.scale_factor.decimal);
12962 break;
12964 case fixed_point_scale_factor_arbitrary:
12965 /* Arbitrary scale factors cannot be described in standard DWARF,
12966 yet. */
12967 if (!dwarf_strict)
12969 /* Describe the scale factor as a rational constant. */
12970 const dw_die_ref scale_factor
12971 = new_die (DW_TAG_constant, comp_unit_die (), type);
12973 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12974 fpt_info.scale_factor.arbitrary.numerator);
12975 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12976 fpt_info.scale_factor.arbitrary.denominator);
12978 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12980 break;
12982 default:
12983 gcc_unreachable ();
12987 if (type_bias)
12988 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12989 dw_scalar_form_constant
12990 | dw_scalar_form_exprloc
12991 | dw_scalar_form_reference,
12992 NULL);
12994 return base_type_result;
12997 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12998 named 'auto' in its type: return true for it, false otherwise. */
13000 static inline bool
13001 is_cxx_auto (tree type)
13003 if (is_cxx ())
13005 tree name = TYPE_IDENTIFIER (type);
13006 if (name == get_identifier ("auto")
13007 || name == get_identifier ("decltype(auto)"))
13008 return true;
13010 return false;
13013 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
13014 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13016 static inline int
13017 is_base_type (tree type)
13019 switch (TREE_CODE (type))
13021 case INTEGER_TYPE:
13022 case REAL_TYPE:
13023 case FIXED_POINT_TYPE:
13024 case COMPLEX_TYPE:
13025 case BOOLEAN_TYPE:
13026 return 1;
13028 case VOID_TYPE:
13029 case ARRAY_TYPE:
13030 case RECORD_TYPE:
13031 case UNION_TYPE:
13032 case QUAL_UNION_TYPE:
13033 case ENUMERAL_TYPE:
13034 case FUNCTION_TYPE:
13035 case METHOD_TYPE:
13036 case POINTER_TYPE:
13037 case REFERENCE_TYPE:
13038 case NULLPTR_TYPE:
13039 case OFFSET_TYPE:
13040 case LANG_TYPE:
13041 case VECTOR_TYPE:
13042 return 0;
13044 default:
13045 if (is_cxx_auto (type))
13046 return 0;
13047 gcc_unreachable ();
13050 return 0;
13053 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13054 node, return the size in bits for the type if it is a constant, or else
13055 return the alignment for the type if the type's size is not constant, or
13056 else return BITS_PER_WORD if the type actually turns out to be an
13057 ERROR_MARK node. */
13059 static inline unsigned HOST_WIDE_INT
13060 simple_type_size_in_bits (const_tree type)
13062 if (TREE_CODE (type) == ERROR_MARK)
13063 return BITS_PER_WORD;
13064 else if (TYPE_SIZE (type) == NULL_TREE)
13065 return 0;
13066 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13067 return tree_to_uhwi (TYPE_SIZE (type));
13068 else
13069 return TYPE_ALIGN (type);
13072 /* Similarly, but return an offset_int instead of UHWI. */
13074 static inline offset_int
13075 offset_int_type_size_in_bits (const_tree type)
13077 if (TREE_CODE (type) == ERROR_MARK)
13078 return BITS_PER_WORD;
13079 else if (TYPE_SIZE (type) == NULL_TREE)
13080 return 0;
13081 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13082 return wi::to_offset (TYPE_SIZE (type));
13083 else
13084 return TYPE_ALIGN (type);
13087 /* Given a pointer to a tree node for a subrange type, return a pointer
13088 to a DIE that describes the given type. */
13090 static dw_die_ref
13091 subrange_type_die (tree type, tree low, tree high, tree bias,
13092 dw_die_ref context_die)
13094 dw_die_ref subrange_die;
13095 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13097 if (context_die == NULL)
13098 context_die = comp_unit_die ();
13100 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13102 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13104 /* The size of the subrange type and its base type do not match,
13105 so we need to generate a size attribute for the subrange type. */
13106 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13109 add_alignment_attribute (subrange_die, type);
13111 if (low)
13112 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13113 if (high)
13114 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13115 if (bias && !dwarf_strict)
13116 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13117 dw_scalar_form_constant
13118 | dw_scalar_form_exprloc
13119 | dw_scalar_form_reference,
13120 NULL);
13122 return subrange_die;
13125 /* Returns the (const and/or volatile) cv_qualifiers associated with
13126 the decl node. This will normally be augmented with the
13127 cv_qualifiers of the underlying type in add_type_attribute. */
13129 static int
13130 decl_quals (const_tree decl)
13132 return ((TREE_READONLY (decl)
13133 /* The C++ front-end correctly marks reference-typed
13134 variables as readonly, but from a language (and debug
13135 info) standpoint they are not const-qualified. */
13136 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13137 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13138 | (TREE_THIS_VOLATILE (decl)
13139 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13142 /* Determine the TYPE whose qualifiers match the largest strict subset
13143 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13144 qualifiers outside QUAL_MASK. */
13146 static int
13147 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13149 tree t;
13150 int best_rank = 0, best_qual = 0, max_rank;
13152 type_quals &= qual_mask;
13153 max_rank = popcount_hwi (type_quals) - 1;
13155 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13156 t = TYPE_NEXT_VARIANT (t))
13158 int q = TYPE_QUALS (t) & qual_mask;
13160 if ((q & type_quals) == q && q != type_quals
13161 && check_base_type (t, type))
13163 int rank = popcount_hwi (q);
13165 if (rank > best_rank)
13167 best_rank = rank;
13168 best_qual = q;
13173 return best_qual;
13176 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13177 static const dwarf_qual_info_t dwarf_qual_info[] =
13179 { TYPE_QUAL_CONST, DW_TAG_const_type },
13180 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13181 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13182 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13184 static const unsigned int dwarf_qual_info_size
13185 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13187 /* If DIE is a qualified DIE of some base DIE with the same parent,
13188 return the base DIE, otherwise return NULL. Set MASK to the
13189 qualifiers added compared to the returned DIE. */
13191 static dw_die_ref
13192 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13194 unsigned int i;
13195 for (i = 0; i < dwarf_qual_info_size; i++)
13196 if (die->die_tag == dwarf_qual_info[i].t)
13197 break;
13198 if (i == dwarf_qual_info_size)
13199 return NULL;
13200 if (vec_safe_length (die->die_attr) != 1)
13201 return NULL;
13202 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13203 if (type == NULL || type->die_parent != die->die_parent)
13204 return NULL;
13205 *mask |= dwarf_qual_info[i].q;
13206 if (depth)
13208 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13209 if (ret)
13210 return ret;
13212 return type;
13215 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13216 entry that chains the modifiers specified by CV_QUALS in front of the
13217 given type. REVERSE is true if the type is to be interpreted in the
13218 reverse storage order wrt the target order. */
13220 static dw_die_ref
13221 modified_type_die (tree type, int cv_quals, bool reverse,
13222 dw_die_ref context_die)
13224 enum tree_code code = TREE_CODE (type);
13225 dw_die_ref mod_type_die;
13226 dw_die_ref sub_die = NULL;
13227 tree item_type = NULL;
13228 tree qualified_type;
13229 tree name, low, high;
13230 dw_die_ref mod_scope;
13231 /* Only these cv-qualifiers are currently handled. */
13232 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13233 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13234 ENCODE_QUAL_ADDR_SPACE(~0U));
13235 const bool reverse_base_type
13236 = need_endianity_attribute_p (reverse) && is_base_type (type);
13238 if (code == ERROR_MARK)
13239 return NULL;
13241 if (lang_hooks.types.get_debug_type)
13243 tree debug_type = lang_hooks.types.get_debug_type (type);
13245 if (debug_type != NULL_TREE && debug_type != type)
13246 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13249 cv_quals &= cv_qual_mask;
13251 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13252 tag modifier (and not an attribute) old consumers won't be able
13253 to handle it. */
13254 if (dwarf_version < 3)
13255 cv_quals &= ~TYPE_QUAL_RESTRICT;
13257 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13258 if (dwarf_version < 5)
13259 cv_quals &= ~TYPE_QUAL_ATOMIC;
13261 /* See if we already have the appropriately qualified variant of
13262 this type. */
13263 qualified_type = get_qualified_type (type, cv_quals);
13265 if (qualified_type == sizetype)
13267 /* Try not to expose the internal sizetype type's name. */
13268 if (TYPE_NAME (qualified_type)
13269 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13271 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13273 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13274 && (TYPE_PRECISION (t)
13275 == TYPE_PRECISION (qualified_type))
13276 && (TYPE_UNSIGNED (t)
13277 == TYPE_UNSIGNED (qualified_type)));
13278 qualified_type = t;
13280 else if (qualified_type == sizetype
13281 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13282 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13283 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13284 qualified_type = size_type_node;
13285 if (type == sizetype)
13286 type = qualified_type;
13289 /* If we do, then we can just use its DIE, if it exists. */
13290 if (qualified_type)
13292 mod_type_die = lookup_type_die (qualified_type);
13294 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13295 dealt with specially: the DIE with the attribute, if it exists, is
13296 placed immediately after the regular DIE for the same base type. */
13297 if (mod_type_die
13298 && (!reverse_base_type
13299 || ((mod_type_die = mod_type_die->die_sib) != NULL
13300 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13301 return mod_type_die;
13304 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13306 /* Handle C typedef types. */
13307 if (name
13308 && TREE_CODE (name) == TYPE_DECL
13309 && DECL_ORIGINAL_TYPE (name)
13310 && !DECL_ARTIFICIAL (name))
13312 tree dtype = TREE_TYPE (name);
13314 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13315 if (qualified_type == dtype && !reverse_base_type)
13317 tree origin = decl_ultimate_origin (name);
13319 /* Typedef variants that have an abstract origin don't get their own
13320 type DIE (see gen_typedef_die), so fall back on the ultimate
13321 abstract origin instead. */
13322 if (origin != NULL && origin != name)
13323 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13324 context_die);
13326 /* For a named type, use the typedef. */
13327 gen_type_die (qualified_type, context_die);
13328 return lookup_type_die (qualified_type);
13330 else
13332 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13333 dquals &= cv_qual_mask;
13334 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13335 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13336 /* cv-unqualified version of named type. Just use
13337 the unnamed type to which it refers. */
13338 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13339 reverse, context_die);
13340 /* Else cv-qualified version of named type; fall through. */
13344 mod_scope = scope_die_for (type, context_die);
13346 if (cv_quals)
13348 int sub_quals = 0, first_quals = 0;
13349 unsigned i;
13350 dw_die_ref first = NULL, last = NULL;
13352 /* Determine a lesser qualified type that most closely matches
13353 this one. Then generate DW_TAG_* entries for the remaining
13354 qualifiers. */
13355 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13356 cv_qual_mask);
13357 if (sub_quals && use_debug_types)
13359 bool needed = false;
13360 /* If emitting type units, make sure the order of qualifiers
13361 is canonical. Thus, start from unqualified type if
13362 an earlier qualifier is missing in sub_quals, but some later
13363 one is present there. */
13364 for (i = 0; i < dwarf_qual_info_size; i++)
13365 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13366 needed = true;
13367 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13369 sub_quals = 0;
13370 break;
13373 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13374 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13376 /* As not all intermediate qualified DIEs have corresponding
13377 tree types, ensure that qualified DIEs in the same scope
13378 as their DW_AT_type are emitted after their DW_AT_type,
13379 only with other qualified DIEs for the same type possibly
13380 in between them. Determine the range of such qualified
13381 DIEs now (first being the base type, last being corresponding
13382 last qualified DIE for it). */
13383 unsigned int count = 0;
13384 first = qualified_die_p (mod_type_die, &first_quals,
13385 dwarf_qual_info_size);
13386 if (first == NULL)
13387 first = mod_type_die;
13388 gcc_assert ((first_quals & ~sub_quals) == 0);
13389 for (count = 0, last = first;
13390 count < (1U << dwarf_qual_info_size);
13391 count++, last = last->die_sib)
13393 int quals = 0;
13394 if (last == mod_scope->die_child)
13395 break;
13396 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13397 != first)
13398 break;
13402 for (i = 0; i < dwarf_qual_info_size; i++)
13403 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13405 dw_die_ref d;
13406 if (first && first != last)
13408 for (d = first->die_sib; ; d = d->die_sib)
13410 int quals = 0;
13411 qualified_die_p (d, &quals, dwarf_qual_info_size);
13412 if (quals == (first_quals | dwarf_qual_info[i].q))
13413 break;
13414 if (d == last)
13416 d = NULL;
13417 break;
13420 if (d)
13422 mod_type_die = d;
13423 continue;
13426 if (first)
13428 d = new_die_raw (dwarf_qual_info[i].t);
13429 add_child_die_after (mod_scope, d, last);
13430 last = d;
13432 else
13433 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13434 if (mod_type_die)
13435 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13436 mod_type_die = d;
13437 first_quals |= dwarf_qual_info[i].q;
13440 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13442 dwarf_tag tag = DW_TAG_pointer_type;
13443 if (code == REFERENCE_TYPE)
13445 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13446 tag = DW_TAG_rvalue_reference_type;
13447 else
13448 tag = DW_TAG_reference_type;
13450 mod_type_die = new_die (tag, mod_scope, type);
13452 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13453 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13454 add_alignment_attribute (mod_type_die, type);
13455 item_type = TREE_TYPE (type);
13457 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13458 if (!ADDR_SPACE_GENERIC_P (as))
13460 int action = targetm.addr_space.debug (as);
13461 if (action >= 0)
13463 /* Positive values indicate an address_class. */
13464 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13466 else
13468 /* Negative values indicate an (inverted) segment base reg. */
13469 dw_loc_descr_ref d
13470 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13471 add_AT_loc (mod_type_die, DW_AT_segment, d);
13475 else if (code == INTEGER_TYPE
13476 && TREE_TYPE (type) != NULL_TREE
13477 && subrange_type_for_debug_p (type, &low, &high))
13479 tree bias = NULL_TREE;
13480 if (lang_hooks.types.get_type_bias)
13481 bias = lang_hooks.types.get_type_bias (type);
13482 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13483 item_type = TREE_TYPE (type);
13485 else if (is_base_type (type))
13487 mod_type_die = base_type_die (type, reverse);
13489 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13490 if (reverse_base_type)
13492 dw_die_ref after_die
13493 = modified_type_die (type, cv_quals, false, context_die);
13494 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13496 else
13497 add_child_die (comp_unit_die (), mod_type_die);
13499 add_pubtype (type, mod_type_die);
13501 else
13503 gen_type_die (type, context_die);
13505 /* We have to get the type_main_variant here (and pass that to the
13506 `lookup_type_die' routine) because the ..._TYPE node we have
13507 might simply be a *copy* of some original type node (where the
13508 copy was created to help us keep track of typedef names) and
13509 that copy might have a different TYPE_UID from the original
13510 ..._TYPE node. */
13511 if (TREE_CODE (type) == FUNCTION_TYPE
13512 || TREE_CODE (type) == METHOD_TYPE)
13514 /* For function/method types, can't just use type_main_variant here,
13515 because that can have different ref-qualifiers for C++,
13516 but try to canonicalize. */
13517 tree main = TYPE_MAIN_VARIANT (type);
13518 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13519 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13520 && check_base_type (t, main)
13521 && check_lang_type (t, type))
13522 return lookup_type_die (t);
13523 return lookup_type_die (type);
13525 else if (TREE_CODE (type) != VECTOR_TYPE
13526 && TREE_CODE (type) != ARRAY_TYPE)
13527 return lookup_type_die (type_main_variant (type));
13528 else
13529 /* Vectors have the debugging information in the type,
13530 not the main variant. */
13531 return lookup_type_die (type);
13534 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13535 don't output a DW_TAG_typedef, since there isn't one in the
13536 user's program; just attach a DW_AT_name to the type.
13537 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13538 if the base type already has the same name. */
13539 if (name
13540 && ((TREE_CODE (name) != TYPE_DECL
13541 && (qualified_type == TYPE_MAIN_VARIANT (type)
13542 || (cv_quals == TYPE_UNQUALIFIED)))
13543 || (TREE_CODE (name) == TYPE_DECL
13544 && TREE_TYPE (name) == qualified_type
13545 && DECL_NAME (name))))
13547 if (TREE_CODE (name) == TYPE_DECL)
13548 /* Could just call add_name_and_src_coords_attributes here,
13549 but since this is a builtin type it doesn't have any
13550 useful source coordinates anyway. */
13551 name = DECL_NAME (name);
13552 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13554 /* This probably indicates a bug. */
13555 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13557 name = TYPE_IDENTIFIER (type);
13558 add_name_attribute (mod_type_die,
13559 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13562 if (qualified_type && !reverse_base_type)
13563 equate_type_number_to_die (qualified_type, mod_type_die);
13565 if (item_type)
13566 /* We must do this after the equate_type_number_to_die call, in case
13567 this is a recursive type. This ensures that the modified_type_die
13568 recursion will terminate even if the type is recursive. Recursive
13569 types are possible in Ada. */
13570 sub_die = modified_type_die (item_type,
13571 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13572 reverse,
13573 context_die);
13575 if (sub_die != NULL)
13576 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13578 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13579 if (TYPE_ARTIFICIAL (type))
13580 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13582 return mod_type_die;
13585 /* Generate DIEs for the generic parameters of T.
13586 T must be either a generic type or a generic function.
13587 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13589 static void
13590 gen_generic_params_dies (tree t)
13592 tree parms, args;
13593 int parms_num, i;
13594 dw_die_ref die = NULL;
13595 int non_default;
13597 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13598 return;
13600 if (TYPE_P (t))
13601 die = lookup_type_die (t);
13602 else if (DECL_P (t))
13603 die = lookup_decl_die (t);
13605 gcc_assert (die);
13607 parms = lang_hooks.get_innermost_generic_parms (t);
13608 if (!parms)
13609 /* T has no generic parameter. It means T is neither a generic type
13610 or function. End of story. */
13611 return;
13613 parms_num = TREE_VEC_LENGTH (parms);
13614 args = lang_hooks.get_innermost_generic_args (t);
13615 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13616 non_default = int_cst_value (TREE_CHAIN (args));
13617 else
13618 non_default = TREE_VEC_LENGTH (args);
13619 for (i = 0; i < parms_num; i++)
13621 tree parm, arg, arg_pack_elems;
13622 dw_die_ref parm_die;
13624 parm = TREE_VEC_ELT (parms, i);
13625 arg = TREE_VEC_ELT (args, i);
13626 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13627 gcc_assert (parm && TREE_VALUE (parm) && arg);
13629 if (parm && TREE_VALUE (parm) && arg)
13631 /* If PARM represents a template parameter pack,
13632 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13633 by DW_TAG_template_*_parameter DIEs for the argument
13634 pack elements of ARG. Note that ARG would then be
13635 an argument pack. */
13636 if (arg_pack_elems)
13637 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13638 arg_pack_elems,
13639 die);
13640 else
13641 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13642 true /* emit name */, die);
13643 if (i >= non_default)
13644 add_AT_flag (parm_die, DW_AT_default_value, 1);
13649 /* Create and return a DIE for PARM which should be
13650 the representation of a generic type parameter.
13651 For instance, in the C++ front end, PARM would be a template parameter.
13652 ARG is the argument to PARM.
13653 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13654 name of the PARM.
13655 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13656 as a child node. */
13658 static dw_die_ref
13659 generic_parameter_die (tree parm, tree arg,
13660 bool emit_name_p,
13661 dw_die_ref parent_die)
13663 dw_die_ref tmpl_die = NULL;
13664 const char *name = NULL;
13666 /* C++20 accepts class literals as template parameters, and var
13667 decls with initializers represent them. The VAR_DECLs would be
13668 rejected, but we can take the DECL_INITIAL constructor and
13669 attempt to expand it. */
13670 if (arg && VAR_P (arg))
13671 arg = DECL_INITIAL (arg);
13673 if (!parm || !DECL_NAME (parm) || !arg)
13674 return NULL;
13676 /* We support non-type generic parameters and arguments,
13677 type generic parameters and arguments, as well as
13678 generic generic parameters (a.k.a. template template parameters in C++)
13679 and arguments. */
13680 if (TREE_CODE (parm) == PARM_DECL)
13681 /* PARM is a nontype generic parameter */
13682 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13683 else if (TREE_CODE (parm) == TYPE_DECL)
13684 /* PARM is a type generic parameter. */
13685 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13686 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13687 /* PARM is a generic generic parameter.
13688 Its DIE is a GNU extension. It shall have a
13689 DW_AT_name attribute to represent the name of the template template
13690 parameter, and a DW_AT_GNU_template_name attribute to represent the
13691 name of the template template argument. */
13692 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13693 parent_die, parm);
13694 else
13695 gcc_unreachable ();
13697 if (tmpl_die)
13699 tree tmpl_type;
13701 /* If PARM is a generic parameter pack, it means we are
13702 emitting debug info for a template argument pack element.
13703 In other terms, ARG is a template argument pack element.
13704 In that case, we don't emit any DW_AT_name attribute for
13705 the die. */
13706 if (emit_name_p)
13708 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13709 gcc_assert (name);
13710 add_AT_string (tmpl_die, DW_AT_name, name);
13713 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13715 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13716 TMPL_DIE should have a child DW_AT_type attribute that is set
13717 to the type of the argument to PARM, which is ARG.
13718 If PARM is a type generic parameter, TMPL_DIE should have a
13719 child DW_AT_type that is set to ARG. */
13720 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13721 add_type_attribute (tmpl_die, tmpl_type,
13722 (TREE_THIS_VOLATILE (tmpl_type)
13723 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
13724 false, parent_die);
13726 else
13728 /* So TMPL_DIE is a DIE representing a
13729 a generic generic template parameter, a.k.a template template
13730 parameter in C++ and arg is a template. */
13732 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13733 to the name of the argument. */
13734 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13735 if (name)
13736 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13739 if (TREE_CODE (parm) == PARM_DECL)
13740 /* So PARM is a non-type generic parameter.
13741 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13742 attribute of TMPL_DIE which value represents the value
13743 of ARG.
13744 We must be careful here:
13745 The value of ARG might reference some function decls.
13746 We might currently be emitting debug info for a generic
13747 type and types are emitted before function decls, we don't
13748 know if the function decls referenced by ARG will actually be
13749 emitted after cgraph computations.
13750 So must defer the generation of the DW_AT_const_value to
13751 after cgraph is ready. */
13752 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13755 return tmpl_die;
13758 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13759 PARM_PACK must be a template parameter pack. The returned DIE
13760 will be child DIE of PARENT_DIE. */
13762 static dw_die_ref
13763 template_parameter_pack_die (tree parm_pack,
13764 tree parm_pack_args,
13765 dw_die_ref parent_die)
13767 dw_die_ref die;
13768 int j;
13770 gcc_assert (parent_die && parm_pack);
13772 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13773 add_name_and_src_coords_attributes (die, parm_pack);
13774 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13775 generic_parameter_die (parm_pack,
13776 TREE_VEC_ELT (parm_pack_args, j),
13777 false /* Don't emit DW_AT_name */,
13778 die);
13779 return die;
13782 /* Return the DBX register number described by a given RTL node. */
13784 static unsigned int
13785 dbx_reg_number (const_rtx rtl)
13787 unsigned regno = REGNO (rtl);
13789 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13791 #ifdef LEAF_REG_REMAP
13792 if (crtl->uses_only_leaf_regs)
13794 int leaf_reg = LEAF_REG_REMAP (regno);
13795 if (leaf_reg != -1)
13796 regno = (unsigned) leaf_reg;
13798 #endif
13800 regno = DBX_REGISTER_NUMBER (regno);
13801 gcc_assert (regno != INVALID_REGNUM);
13802 return regno;
13805 /* Optionally add a DW_OP_piece term to a location description expression.
13806 DW_OP_piece is only added if the location description expression already
13807 doesn't end with DW_OP_piece. */
13809 static void
13810 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13812 dw_loc_descr_ref loc;
13814 if (*list_head != NULL)
13816 /* Find the end of the chain. */
13817 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13820 if (loc->dw_loc_opc != DW_OP_piece)
13821 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13825 /* Return a location descriptor that designates a machine register or
13826 zero if there is none. */
13828 static dw_loc_descr_ref
13829 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13831 rtx regs;
13833 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13834 return 0;
13836 /* We only use "frame base" when we're sure we're talking about the
13837 post-prologue local stack frame. We do this by *not* running
13838 register elimination until this point, and recognizing the special
13839 argument pointer and soft frame pointer rtx's.
13840 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13841 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13842 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13844 dw_loc_descr_ref result = NULL;
13846 if (dwarf_version >= 4 || !dwarf_strict)
13848 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13849 initialized);
13850 if (result)
13851 add_loc_descr (&result,
13852 new_loc_descr (DW_OP_stack_value, 0, 0));
13854 return result;
13857 regs = targetm.dwarf_register_span (rtl);
13859 if (REG_NREGS (rtl) > 1 || regs)
13860 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13861 else
13863 unsigned int dbx_regnum = dbx_reg_number (rtl);
13864 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13865 return 0;
13866 return one_reg_loc_descriptor (dbx_regnum, initialized);
13870 /* Return a location descriptor that designates a machine register for
13871 a given hard register number. */
13873 static dw_loc_descr_ref
13874 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13876 dw_loc_descr_ref reg_loc_descr;
13878 if (regno <= 31)
13879 reg_loc_descr
13880 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13881 else
13882 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13884 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13885 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13887 return reg_loc_descr;
13890 /* Given an RTL of a register, return a location descriptor that
13891 designates a value that spans more than one register. */
13893 static dw_loc_descr_ref
13894 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13895 enum var_init_status initialized)
13897 int size, i;
13898 dw_loc_descr_ref loc_result = NULL;
13900 /* Simple, contiguous registers. */
13901 if (regs == NULL_RTX)
13903 unsigned reg = REGNO (rtl);
13904 int nregs;
13906 #ifdef LEAF_REG_REMAP
13907 if (crtl->uses_only_leaf_regs)
13909 int leaf_reg = LEAF_REG_REMAP (reg);
13910 if (leaf_reg != -1)
13911 reg = (unsigned) leaf_reg;
13913 #endif
13915 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13916 nregs = REG_NREGS (rtl);
13918 /* At present we only track constant-sized pieces. */
13919 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
13920 return NULL;
13921 size /= nregs;
13923 loc_result = NULL;
13924 while (nregs--)
13926 dw_loc_descr_ref t;
13928 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13929 VAR_INIT_STATUS_INITIALIZED);
13930 add_loc_descr (&loc_result, t);
13931 add_loc_descr_op_piece (&loc_result, size);
13932 ++reg;
13934 return loc_result;
13937 /* Now onto stupid register sets in non contiguous locations. */
13939 gcc_assert (GET_CODE (regs) == PARALLEL);
13941 /* At present we only track constant-sized pieces. */
13942 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
13943 return NULL;
13944 loc_result = NULL;
13946 for (i = 0; i < XVECLEN (regs, 0); ++i)
13948 dw_loc_descr_ref t;
13950 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13951 VAR_INIT_STATUS_INITIALIZED);
13952 add_loc_descr (&loc_result, t);
13953 add_loc_descr_op_piece (&loc_result, size);
13956 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13957 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13958 return loc_result;
13961 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13963 /* Return a location descriptor that designates a constant i,
13964 as a compound operation from constant (i >> shift), constant shift
13965 and DW_OP_shl. */
13967 static dw_loc_descr_ref
13968 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13970 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13971 add_loc_descr (&ret, int_loc_descriptor (shift));
13972 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13973 return ret;
13976 /* Return a location descriptor that designates constant POLY_I. */
13978 static dw_loc_descr_ref
13979 int_loc_descriptor (poly_int64 poly_i)
13981 enum dwarf_location_atom op;
13983 HOST_WIDE_INT i;
13984 if (!poly_i.is_constant (&i))
13986 /* Create location descriptions for the non-constant part and
13987 add any constant offset at the end. */
13988 dw_loc_descr_ref ret = NULL;
13989 HOST_WIDE_INT constant = poly_i.coeffs[0];
13990 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
13992 HOST_WIDE_INT coeff = poly_i.coeffs[j];
13993 if (coeff != 0)
13995 dw_loc_descr_ref start = ret;
13996 unsigned int factor;
13997 int bias;
13998 unsigned int regno = targetm.dwarf_poly_indeterminate_value
13999 (j, &factor, &bias);
14001 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14002 add COEFF * (REGNO / FACTOR) now and subtract
14003 COEFF * BIAS from the final constant part. */
14004 constant -= coeff * bias;
14005 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14006 if (coeff % factor == 0)
14007 coeff /= factor;
14008 else
14010 int amount = exact_log2 (factor);
14011 gcc_assert (amount >= 0);
14012 add_loc_descr (&ret, int_loc_descriptor (amount));
14013 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14015 if (coeff != 1)
14017 add_loc_descr (&ret, int_loc_descriptor (coeff));
14018 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14020 if (start)
14021 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14024 loc_descr_plus_const (&ret, constant);
14025 return ret;
14028 /* Pick the smallest representation of a constant, rather than just
14029 defaulting to the LEB encoding. */
14030 if (i >= 0)
14032 int clz = clz_hwi (i);
14033 int ctz = ctz_hwi (i);
14034 if (i <= 31)
14035 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14036 else if (i <= 0xff)
14037 op = DW_OP_const1u;
14038 else if (i <= 0xffff)
14039 op = DW_OP_const2u;
14040 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14041 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14042 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14043 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14044 while DW_OP_const4u is 5 bytes. */
14045 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14046 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14047 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14048 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14049 while DW_OP_const4u is 5 bytes. */
14050 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14052 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14053 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14054 <= 4)
14056 /* As i >= 2**31, the double cast above will yield a negative number.
14057 Since wrapping is defined in DWARF expressions we can output big
14058 positive integers as small negative ones, regardless of the size
14059 of host wide ints.
14061 Here, since the evaluator will handle 32-bit values and since i >=
14062 2**31, we know it's going to be interpreted as a negative literal:
14063 store it this way if we can do better than 5 bytes this way. */
14064 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14066 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14067 op = DW_OP_const4u;
14069 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14070 least 6 bytes: see if we can do better before falling back to it. */
14071 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14072 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14073 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14074 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14075 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14076 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14077 >= HOST_BITS_PER_WIDE_INT)
14078 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14079 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14080 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14081 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14082 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14083 && size_of_uleb128 (i) > 6)
14084 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14085 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14086 else
14087 op = DW_OP_constu;
14089 else
14091 if (i >= -0x80)
14092 op = DW_OP_const1s;
14093 else if (i >= -0x8000)
14094 op = DW_OP_const2s;
14095 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14097 if (size_of_int_loc_descriptor (i) < 5)
14099 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14100 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14101 return ret;
14103 op = DW_OP_const4s;
14105 else
14107 if (size_of_int_loc_descriptor (i)
14108 < (unsigned long) 1 + size_of_sleb128 (i))
14110 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14111 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14112 return ret;
14114 op = DW_OP_consts;
14118 return new_loc_descr (op, i, 0);
14121 /* Likewise, for unsigned constants. */
14123 static dw_loc_descr_ref
14124 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14126 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14127 const unsigned HOST_WIDE_INT max_uint
14128 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14130 /* If possible, use the clever signed constants handling. */
14131 if (i <= max_int)
14132 return int_loc_descriptor ((HOST_WIDE_INT) i);
14134 /* Here, we are left with positive numbers that cannot be represented as
14135 HOST_WIDE_INT, i.e.:
14136 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14138 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14139 whereas may be better to output a negative integer: thanks to integer
14140 wrapping, we know that:
14141 x = x - 2 ** DWARF2_ADDR_SIZE
14142 = x - 2 * (max (HOST_WIDE_INT) + 1)
14143 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14144 small negative integers. Let's try that in cases it will clearly improve
14145 the encoding: there is no gain turning DW_OP_const4u into
14146 DW_OP_const4s. */
14147 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14148 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14149 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14151 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14153 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14154 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14155 const HOST_WIDE_INT second_shift
14156 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14158 /* So we finally have:
14159 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14160 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14161 return int_loc_descriptor (second_shift);
14164 /* Last chance: fallback to a simple constant operation. */
14165 return new_loc_descr
14166 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14167 ? DW_OP_const4u
14168 : DW_OP_const8u,
14169 i, 0);
14172 /* Generate and return a location description that computes the unsigned
14173 comparison of the two stack top entries (a OP b where b is the top-most
14174 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14175 LE_EXPR, GT_EXPR or GE_EXPR. */
14177 static dw_loc_descr_ref
14178 uint_comparison_loc_list (enum tree_code kind)
14180 enum dwarf_location_atom op, flip_op;
14181 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14183 switch (kind)
14185 case LT_EXPR:
14186 op = DW_OP_lt;
14187 break;
14188 case LE_EXPR:
14189 op = DW_OP_le;
14190 break;
14191 case GT_EXPR:
14192 op = DW_OP_gt;
14193 break;
14194 case GE_EXPR:
14195 op = DW_OP_ge;
14196 break;
14197 default:
14198 gcc_unreachable ();
14201 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14202 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14204 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14205 possible to perform unsigned comparisons: we just have to distinguish
14206 three cases:
14208 1. when a and b have the same sign (as signed integers); then we should
14209 return: a OP(signed) b;
14211 2. when a is a negative signed integer while b is a positive one, then a
14212 is a greater unsigned integer than b; likewise when a and b's roles
14213 are flipped.
14215 So first, compare the sign of the two operands. */
14216 ret = new_loc_descr (DW_OP_over, 0, 0);
14217 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14218 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14219 /* If they have different signs (i.e. they have different sign bits), then
14220 the stack top value has now the sign bit set and thus it's smaller than
14221 zero. */
14222 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14223 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14224 add_loc_descr (&ret, bra_node);
14226 /* We are in case 1. At this point, we know both operands have the same
14227 sign, to it's safe to use the built-in signed comparison. */
14228 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14229 add_loc_descr (&ret, jmp_node);
14231 /* We are in case 2. Here, we know both operands do not have the same sign,
14232 so we have to flip the signed comparison. */
14233 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14234 tmp = new_loc_descr (flip_op, 0, 0);
14235 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14236 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14237 add_loc_descr (&ret, tmp);
14239 /* This dummy operation is necessary to make the two branches join. */
14240 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14241 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14242 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14243 add_loc_descr (&ret, tmp);
14245 return ret;
14248 /* Likewise, but takes the location description lists (might be destructive on
14249 them). Return NULL if either is NULL or if concatenation fails. */
14251 static dw_loc_list_ref
14252 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14253 enum tree_code kind)
14255 if (left == NULL || right == NULL)
14256 return NULL;
14258 add_loc_list (&left, right);
14259 if (left == NULL)
14260 return NULL;
14262 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14263 return left;
14266 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14267 without actually allocating it. */
14269 static unsigned long
14270 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14272 return size_of_int_loc_descriptor (i >> shift)
14273 + size_of_int_loc_descriptor (shift)
14274 + 1;
14277 /* Return size_of_locs (int_loc_descriptor (i)) without
14278 actually allocating it. */
14280 static unsigned long
14281 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14283 unsigned long s;
14285 if (i >= 0)
14287 int clz, ctz;
14288 if (i <= 31)
14289 return 1;
14290 else if (i <= 0xff)
14291 return 2;
14292 else if (i <= 0xffff)
14293 return 3;
14294 clz = clz_hwi (i);
14295 ctz = ctz_hwi (i);
14296 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14297 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14298 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14299 - clz - 5);
14300 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14301 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14302 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14303 - clz - 8);
14304 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14305 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14306 <= 4)
14307 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14308 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14309 return 5;
14310 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14311 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14312 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14313 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14314 - clz - 8);
14315 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14316 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14317 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14318 - clz - 16);
14319 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14320 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14321 && s > 6)
14322 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14323 - clz - 32);
14324 else
14325 return 1 + s;
14327 else
14329 if (i >= -0x80)
14330 return 2;
14331 else if (i >= -0x8000)
14332 return 3;
14333 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14335 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14337 s = size_of_int_loc_descriptor (-i) + 1;
14338 if (s < 5)
14339 return s;
14341 return 5;
14343 else
14345 unsigned long r = 1 + size_of_sleb128 (i);
14346 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14348 s = size_of_int_loc_descriptor (-i) + 1;
14349 if (s < r)
14350 return s;
14352 return r;
14357 /* Return loc description representing "address" of integer value.
14358 This can appear only as toplevel expression. */
14360 static dw_loc_descr_ref
14361 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14363 int litsize;
14364 dw_loc_descr_ref loc_result = NULL;
14366 if (!(dwarf_version >= 4 || !dwarf_strict))
14367 return NULL;
14369 litsize = size_of_int_loc_descriptor (i);
14370 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14371 is more compact. For DW_OP_stack_value we need:
14372 litsize + 1 (DW_OP_stack_value)
14373 and for DW_OP_implicit_value:
14374 1 (DW_OP_implicit_value) + 1 (length) + size. */
14375 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14377 loc_result = int_loc_descriptor (i);
14378 add_loc_descr (&loc_result,
14379 new_loc_descr (DW_OP_stack_value, 0, 0));
14380 return loc_result;
14383 loc_result = new_loc_descr (DW_OP_implicit_value,
14384 size, 0);
14385 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14386 loc_result->dw_loc_oprnd2.v.val_int = i;
14387 return loc_result;
14390 /* Return a location descriptor that designates a base+offset location. */
14392 static dw_loc_descr_ref
14393 based_loc_descr (rtx reg, poly_int64 offset,
14394 enum var_init_status initialized)
14396 unsigned int regno;
14397 dw_loc_descr_ref result;
14398 dw_fde_ref fde = cfun->fde;
14400 /* We only use "frame base" when we're sure we're talking about the
14401 post-prologue local stack frame. We do this by *not* running
14402 register elimination until this point, and recognizing the special
14403 argument pointer and soft frame pointer rtx's. */
14404 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14406 rtx elim = (ira_use_lra_p
14407 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14408 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14410 if (elim != reg)
14412 /* Allow hard frame pointer here even if frame pointer
14413 isn't used since hard frame pointer is encoded with
14414 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14415 not hard frame pointer directly. */
14416 elim = strip_offset_and_add (elim, &offset);
14417 gcc_assert (elim == hard_frame_pointer_rtx
14418 || elim == stack_pointer_rtx);
14420 /* If drap register is used to align stack, use frame
14421 pointer + offset to access stack variables. If stack
14422 is aligned without drap, use stack pointer + offset to
14423 access stack variables. */
14424 if (crtl->stack_realign_tried
14425 && reg == frame_pointer_rtx)
14427 int base_reg
14428 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14429 ? HARD_FRAME_POINTER_REGNUM
14430 : REGNO (elim));
14431 return new_reg_loc_descr (base_reg, offset);
14434 gcc_assert (frame_pointer_fb_offset_valid);
14435 offset += frame_pointer_fb_offset;
14436 HOST_WIDE_INT const_offset;
14437 if (offset.is_constant (&const_offset))
14438 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14439 else
14441 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14442 loc_descr_plus_const (&ret, offset);
14443 return ret;
14448 regno = REGNO (reg);
14449 #ifdef LEAF_REG_REMAP
14450 if (crtl->uses_only_leaf_regs)
14452 int leaf_reg = LEAF_REG_REMAP (regno);
14453 if (leaf_reg != -1)
14454 regno = (unsigned) leaf_reg;
14456 #endif
14457 regno = DWARF_FRAME_REGNUM (regno);
14459 HOST_WIDE_INT const_offset;
14460 if (!optimize && fde
14461 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14462 && offset.is_constant (&const_offset))
14464 /* Use cfa+offset to represent the location of arguments passed
14465 on the stack when drap is used to align stack.
14466 Only do this when not optimizing, for optimized code var-tracking
14467 is supposed to track where the arguments live and the register
14468 used as vdrap or drap in some spot might be used for something
14469 else in other part of the routine. */
14470 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14473 result = new_reg_loc_descr (regno, offset);
14475 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14476 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14478 return result;
14481 /* Return true if this RTL expression describes a base+offset calculation. */
14483 static inline int
14484 is_based_loc (const_rtx rtl)
14486 return (GET_CODE (rtl) == PLUS
14487 && ((REG_P (XEXP (rtl, 0))
14488 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14489 && CONST_INT_P (XEXP (rtl, 1)))));
14492 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14493 failed. */
14495 static dw_loc_descr_ref
14496 tls_mem_loc_descriptor (rtx mem)
14498 tree base;
14499 dw_loc_descr_ref loc_result;
14501 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14502 return NULL;
14504 base = get_base_address (MEM_EXPR (mem));
14505 if (base == NULL
14506 || !VAR_P (base)
14507 || !DECL_THREAD_LOCAL_P (base))
14508 return NULL;
14510 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14511 if (loc_result == NULL)
14512 return NULL;
14514 if (maybe_ne (MEM_OFFSET (mem), 0))
14515 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14517 return loc_result;
14520 /* Output debug info about reason why we failed to expand expression as dwarf
14521 expression. */
14523 static void
14524 expansion_failed (tree expr, rtx rtl, char const *reason)
14526 if (dump_file && (dump_flags & TDF_DETAILS))
14528 fprintf (dump_file, "Failed to expand as dwarf: ");
14529 if (expr)
14530 print_generic_expr (dump_file, expr, dump_flags);
14531 if (rtl)
14533 fprintf (dump_file, "\n");
14534 print_rtl (dump_file, rtl);
14536 fprintf (dump_file, "\nReason: %s\n", reason);
14540 /* Helper function for const_ok_for_output. */
14542 static bool
14543 const_ok_for_output_1 (rtx rtl)
14545 if (targetm.const_not_ok_for_debug_p (rtl))
14547 if (GET_CODE (rtl) != UNSPEC)
14549 expansion_failed (NULL_TREE, rtl,
14550 "Expression rejected for debug by the backend.\n");
14551 return false;
14554 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14555 the target hook doesn't explicitly allow it in debug info, assume
14556 we can't express it in the debug info. */
14557 /* Don't complain about TLS UNSPECs, those are just too hard to
14558 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14559 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14560 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14561 if (flag_checking
14562 && (XVECLEN (rtl, 0) == 0
14563 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14564 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14565 inform (current_function_decl
14566 ? DECL_SOURCE_LOCATION (current_function_decl)
14567 : UNKNOWN_LOCATION,
14568 #if NUM_UNSPEC_VALUES > 0
14569 "non-delegitimized UNSPEC %s (%d) found in variable location",
14570 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14571 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14572 #else
14573 "non-delegitimized UNSPEC %d found in variable location",
14574 #endif
14575 XINT (rtl, 1));
14576 expansion_failed (NULL_TREE, rtl,
14577 "UNSPEC hasn't been delegitimized.\n");
14578 return false;
14581 if (CONST_POLY_INT_P (rtl))
14582 return false;
14584 /* FIXME: Refer to PR60655. It is possible for simplification
14585 of rtl expressions in var tracking to produce such expressions.
14586 We should really identify / validate expressions
14587 enclosed in CONST that can be handled by assemblers on various
14588 targets and only handle legitimate cases here. */
14589 switch (GET_CODE (rtl))
14591 case SYMBOL_REF:
14592 break;
14593 case NOT:
14594 case NEG:
14595 return false;
14596 case PLUS:
14598 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
14599 operands. */
14600 subrtx_var_iterator::array_type array;
14601 bool first = false;
14602 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14603 if (SYMBOL_REF_P (*iter)
14604 || LABEL_P (*iter)
14605 || GET_CODE (*iter) == UNSPEC)
14607 first = true;
14608 break;
14610 if (!first)
14611 return true;
14612 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14613 if (SYMBOL_REF_P (*iter)
14614 || LABEL_P (*iter)
14615 || GET_CODE (*iter) == UNSPEC)
14616 return false;
14617 return true;
14619 case MINUS:
14621 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
14622 appear in the second operand of MINUS. */
14623 subrtx_var_iterator::array_type array;
14624 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14625 if (SYMBOL_REF_P (*iter)
14626 || LABEL_P (*iter)
14627 || GET_CODE (*iter) == UNSPEC)
14628 return false;
14629 return true;
14631 default:
14632 return true;
14635 if (CONSTANT_POOL_ADDRESS_P (rtl))
14637 bool marked;
14638 get_pool_constant_mark (rtl, &marked);
14639 /* If all references to this pool constant were optimized away,
14640 it was not output and thus we can't represent it. */
14641 if (!marked)
14643 expansion_failed (NULL_TREE, rtl,
14644 "Constant was removed from constant pool.\n");
14645 return false;
14649 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14650 return false;
14652 /* Avoid references to external symbols in debug info, on several targets
14653 the linker might even refuse to link when linking a shared library,
14654 and in many other cases the relocations for .debug_info/.debug_loc are
14655 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14656 to be defined within the same shared library or executable are fine. */
14657 if (SYMBOL_REF_EXTERNAL_P (rtl))
14659 tree decl = SYMBOL_REF_DECL (rtl);
14661 if (decl == NULL || !targetm.binds_local_p (decl))
14663 expansion_failed (NULL_TREE, rtl,
14664 "Symbol not defined in current TU.\n");
14665 return false;
14669 return true;
14672 /* Return true if constant RTL can be emitted in DW_OP_addr or
14673 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14674 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14676 static bool
14677 const_ok_for_output (rtx rtl)
14679 if (GET_CODE (rtl) == SYMBOL_REF)
14680 return const_ok_for_output_1 (rtl);
14682 if (GET_CODE (rtl) == CONST)
14684 subrtx_var_iterator::array_type array;
14685 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14686 if (!const_ok_for_output_1 (*iter))
14687 return false;
14688 return true;
14691 return true;
14694 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
14695 if possible, NULL otherwise. */
14697 static dw_die_ref
14698 base_type_for_mode (machine_mode mode, bool unsignedp)
14700 dw_die_ref type_die;
14701 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
14703 if (type == NULL)
14704 return NULL;
14705 switch (TREE_CODE (type))
14707 case INTEGER_TYPE:
14708 case REAL_TYPE:
14709 break;
14710 default:
14711 return NULL;
14713 type_die = lookup_type_die (type);
14714 if (!type_die)
14715 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
14716 comp_unit_die ());
14717 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
14718 return NULL;
14719 return type_die;
14722 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
14723 type matching MODE, or, if MODE is narrower than or as wide as
14724 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
14725 possible. */
14727 static dw_loc_descr_ref
14728 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
14730 machine_mode outer_mode = mode;
14731 dw_die_ref type_die;
14732 dw_loc_descr_ref cvt;
14734 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14736 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
14737 return op;
14739 type_die = base_type_for_mode (outer_mode, 1);
14740 if (type_die == NULL)
14741 return NULL;
14742 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14743 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14744 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14745 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14746 add_loc_descr (&op, cvt);
14747 return op;
14750 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
14752 static dw_loc_descr_ref
14753 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
14754 dw_loc_descr_ref op1)
14756 dw_loc_descr_ref ret = op0;
14757 add_loc_descr (&ret, op1);
14758 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14759 if (STORE_FLAG_VALUE != 1)
14761 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14762 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14764 return ret;
14767 /* Subroutine of scompare_loc_descriptor for the case in which we're
14768 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14769 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
14771 static dw_loc_descr_ref
14772 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14773 scalar_int_mode op_mode,
14774 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14776 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14777 dw_loc_descr_ref cvt;
14779 if (type_die == NULL)
14780 return NULL;
14781 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14782 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14783 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14784 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14785 add_loc_descr (&op0, cvt);
14786 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14787 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14788 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14789 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14790 add_loc_descr (&op1, cvt);
14791 return compare_loc_descriptor (op, op0, op1);
14794 /* Subroutine of scompare_loc_descriptor for the case in which we're
14795 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14796 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
14798 static dw_loc_descr_ref
14799 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
14800 scalar_int_mode op_mode,
14801 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14803 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14804 /* For eq/ne, if the operands are known to be zero-extended,
14805 there is no need to do the fancy shifting up. */
14806 if (op == DW_OP_eq || op == DW_OP_ne)
14808 dw_loc_descr_ref last0, last1;
14809 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14811 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14813 /* deref_size zero extends, and for constants we can check
14814 whether they are zero extended or not. */
14815 if (((last0->dw_loc_opc == DW_OP_deref_size
14816 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14817 || (CONST_INT_P (XEXP (rtl, 0))
14818 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14819 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14820 && ((last1->dw_loc_opc == DW_OP_deref_size
14821 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14822 || (CONST_INT_P (XEXP (rtl, 1))
14823 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14824 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14825 return compare_loc_descriptor (op, op0, op1);
14827 /* EQ/NE comparison against constant in narrower type than
14828 DWARF2_ADDR_SIZE can be performed either as
14829 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14830 DW_OP_{eq,ne}
14832 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14833 DW_OP_{eq,ne}. Pick whatever is shorter. */
14834 if (CONST_INT_P (XEXP (rtl, 1))
14835 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14836 && (size_of_int_loc_descriptor (shift) + 1
14837 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14838 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14839 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14840 & GET_MODE_MASK (op_mode))))
14842 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14843 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14844 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14845 & GET_MODE_MASK (op_mode));
14846 return compare_loc_descriptor (op, op0, op1);
14849 add_loc_descr (&op0, int_loc_descriptor (shift));
14850 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14851 if (CONST_INT_P (XEXP (rtl, 1)))
14852 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14853 else
14855 add_loc_descr (&op1, int_loc_descriptor (shift));
14856 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14858 return compare_loc_descriptor (op, op0, op1);
14861 /* Return location descriptor for unsigned comparison OP RTL. */
14863 static dw_loc_descr_ref
14864 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14865 machine_mode mem_mode)
14867 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14868 dw_loc_descr_ref op0, op1;
14870 if (op_mode == VOIDmode)
14871 op_mode = GET_MODE (XEXP (rtl, 1));
14872 if (op_mode == VOIDmode)
14873 return NULL;
14875 scalar_int_mode int_op_mode;
14876 if (dwarf_strict
14877 && dwarf_version < 5
14878 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14879 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14880 return NULL;
14882 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14883 VAR_INIT_STATUS_INITIALIZED);
14884 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14885 VAR_INIT_STATUS_INITIALIZED);
14887 if (op0 == NULL || op1 == NULL)
14888 return NULL;
14890 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14892 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14893 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14895 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14896 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14898 return compare_loc_descriptor (op, op0, op1);
14901 /* Return location descriptor for unsigned comparison OP RTL. */
14903 static dw_loc_descr_ref
14904 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14905 machine_mode mem_mode)
14907 dw_loc_descr_ref op0, op1;
14909 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14910 if (test_op_mode == VOIDmode)
14911 test_op_mode = GET_MODE (XEXP (rtl, 1));
14913 scalar_int_mode op_mode;
14914 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14915 return NULL;
14917 if (dwarf_strict
14918 && dwarf_version < 5
14919 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14920 return NULL;
14922 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14923 VAR_INIT_STATUS_INITIALIZED);
14924 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14925 VAR_INIT_STATUS_INITIALIZED);
14927 if (op0 == NULL || op1 == NULL)
14928 return NULL;
14930 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14932 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14933 dw_loc_descr_ref last0, last1;
14934 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14936 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14938 if (CONST_INT_P (XEXP (rtl, 0)))
14939 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14940 /* deref_size zero extends, so no need to mask it again. */
14941 else if (last0->dw_loc_opc != DW_OP_deref_size
14942 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14944 add_loc_descr (&op0, int_loc_descriptor (mask));
14945 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14947 if (CONST_INT_P (XEXP (rtl, 1)))
14948 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14949 /* deref_size zero extends, so no need to mask it again. */
14950 else if (last1->dw_loc_opc != DW_OP_deref_size
14951 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14953 add_loc_descr (&op1, int_loc_descriptor (mask));
14954 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14957 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14959 HOST_WIDE_INT bias = 1;
14960 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14961 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14962 if (CONST_INT_P (XEXP (rtl, 1)))
14963 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14964 + INTVAL (XEXP (rtl, 1)));
14965 else
14966 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14967 bias, 0));
14969 return compare_loc_descriptor (op, op0, op1);
14972 /* Return location descriptor for {U,S}{MIN,MAX}. */
14974 static dw_loc_descr_ref
14975 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14976 machine_mode mem_mode)
14978 enum dwarf_location_atom op;
14979 dw_loc_descr_ref op0, op1, ret;
14980 dw_loc_descr_ref bra_node, drop_node;
14982 scalar_int_mode int_mode;
14983 if (dwarf_strict
14984 && dwarf_version < 5
14985 && (!is_a <scalar_int_mode> (mode, &int_mode)
14986 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14987 return NULL;
14989 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14990 VAR_INIT_STATUS_INITIALIZED);
14991 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14992 VAR_INIT_STATUS_INITIALIZED);
14994 if (op0 == NULL || op1 == NULL)
14995 return NULL;
14997 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14998 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14999 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15000 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15002 /* Checked by the caller. */
15003 int_mode = as_a <scalar_int_mode> (mode);
15004 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15006 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15007 add_loc_descr (&op0, int_loc_descriptor (mask));
15008 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15009 add_loc_descr (&op1, int_loc_descriptor (mask));
15010 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15012 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15014 HOST_WIDE_INT bias = 1;
15015 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15016 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15017 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15020 else if (is_a <scalar_int_mode> (mode, &int_mode)
15021 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15023 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15024 add_loc_descr (&op0, int_loc_descriptor (shift));
15025 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15026 add_loc_descr (&op1, int_loc_descriptor (shift));
15027 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15029 else if (is_a <scalar_int_mode> (mode, &int_mode)
15030 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15032 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15033 dw_loc_descr_ref cvt;
15034 if (type_die == NULL)
15035 return NULL;
15036 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15037 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15038 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15039 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15040 add_loc_descr (&op0, cvt);
15041 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15042 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15043 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15044 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15045 add_loc_descr (&op1, cvt);
15048 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15049 op = DW_OP_lt;
15050 else
15051 op = DW_OP_gt;
15052 ret = op0;
15053 add_loc_descr (&ret, op1);
15054 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15055 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15056 add_loc_descr (&ret, bra_node);
15057 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15058 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15059 add_loc_descr (&ret, drop_node);
15060 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15061 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15062 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15063 && is_a <scalar_int_mode> (mode, &int_mode)
15064 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15065 ret = convert_descriptor_to_mode (int_mode, ret);
15066 return ret;
15069 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15070 but after converting arguments to type_die, afterwards
15071 convert back to unsigned. */
15073 static dw_loc_descr_ref
15074 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15075 scalar_int_mode mode, machine_mode mem_mode)
15077 dw_loc_descr_ref cvt, op0, op1;
15079 if (type_die == NULL)
15080 return NULL;
15081 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15082 VAR_INIT_STATUS_INITIALIZED);
15083 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15084 VAR_INIT_STATUS_INITIALIZED);
15085 if (op0 == NULL || op1 == NULL)
15086 return NULL;
15087 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15088 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15089 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15090 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15091 add_loc_descr (&op0, cvt);
15092 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15093 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15094 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15095 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15096 add_loc_descr (&op1, cvt);
15097 add_loc_descr (&op0, op1);
15098 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15099 return convert_descriptor_to_mode (mode, op0);
15102 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15103 const0 is DW_OP_lit0 or corresponding typed constant,
15104 const1 is DW_OP_lit1 or corresponding typed constant
15105 and constMSB is constant with just the MSB bit set
15106 for the mode):
15107 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15108 L1: const0 DW_OP_swap
15109 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15110 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15111 L3: DW_OP_drop
15112 L4: DW_OP_nop
15114 CTZ is similar:
15115 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15116 L1: const0 DW_OP_swap
15117 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15118 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15119 L3: DW_OP_drop
15120 L4: DW_OP_nop
15122 FFS is similar:
15123 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15124 L1: const1 DW_OP_swap
15125 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15126 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15127 L3: DW_OP_drop
15128 L4: DW_OP_nop */
15130 static dw_loc_descr_ref
15131 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15132 machine_mode mem_mode)
15134 dw_loc_descr_ref op0, ret, tmp;
15135 HOST_WIDE_INT valv;
15136 dw_loc_descr_ref l1jump, l1label;
15137 dw_loc_descr_ref l2jump, l2label;
15138 dw_loc_descr_ref l3jump, l3label;
15139 dw_loc_descr_ref l4jump, l4label;
15140 rtx msb;
15142 if (GET_MODE (XEXP (rtl, 0)) != mode)
15143 return NULL;
15145 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15146 VAR_INIT_STATUS_INITIALIZED);
15147 if (op0 == NULL)
15148 return NULL;
15149 ret = op0;
15150 if (GET_CODE (rtl) == CLZ)
15152 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15153 valv = GET_MODE_BITSIZE (mode);
15155 else if (GET_CODE (rtl) == FFS)
15156 valv = 0;
15157 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15158 valv = GET_MODE_BITSIZE (mode);
15159 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15160 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15161 add_loc_descr (&ret, l1jump);
15162 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15163 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15164 VAR_INIT_STATUS_INITIALIZED);
15165 if (tmp == NULL)
15166 return NULL;
15167 add_loc_descr (&ret, tmp);
15168 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15169 add_loc_descr (&ret, l4jump);
15170 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15171 ? const1_rtx : const0_rtx,
15172 mode, mem_mode,
15173 VAR_INIT_STATUS_INITIALIZED);
15174 if (l1label == NULL)
15175 return NULL;
15176 add_loc_descr (&ret, l1label);
15177 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15178 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15179 add_loc_descr (&ret, l2label);
15180 if (GET_CODE (rtl) != CLZ)
15181 msb = const1_rtx;
15182 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15183 msb = GEN_INT (HOST_WIDE_INT_1U
15184 << (GET_MODE_BITSIZE (mode) - 1));
15185 else
15186 msb = immed_wide_int_const
15187 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15188 GET_MODE_PRECISION (mode)), mode);
15189 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15190 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15191 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15192 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15193 else
15194 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15195 VAR_INIT_STATUS_INITIALIZED);
15196 if (tmp == NULL)
15197 return NULL;
15198 add_loc_descr (&ret, tmp);
15199 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15200 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15201 add_loc_descr (&ret, l3jump);
15202 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15203 VAR_INIT_STATUS_INITIALIZED);
15204 if (tmp == NULL)
15205 return NULL;
15206 add_loc_descr (&ret, tmp);
15207 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15208 ? DW_OP_shl : DW_OP_shr, 0, 0));
15209 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15210 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15211 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15212 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15213 add_loc_descr (&ret, l2jump);
15214 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15215 add_loc_descr (&ret, l3label);
15216 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15217 add_loc_descr (&ret, l4label);
15218 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15219 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15220 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15221 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15222 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15223 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15224 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15225 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15226 return ret;
15229 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15230 const1 is DW_OP_lit1 or corresponding typed constant):
15231 const0 DW_OP_swap
15232 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15233 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15234 L2: DW_OP_drop
15236 PARITY is similar:
15237 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15238 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15239 L2: DW_OP_drop */
15241 static dw_loc_descr_ref
15242 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15243 machine_mode mem_mode)
15245 dw_loc_descr_ref op0, ret, tmp;
15246 dw_loc_descr_ref l1jump, l1label;
15247 dw_loc_descr_ref l2jump, l2label;
15249 if (GET_MODE (XEXP (rtl, 0)) != mode)
15250 return NULL;
15252 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15253 VAR_INIT_STATUS_INITIALIZED);
15254 if (op0 == NULL)
15255 return NULL;
15256 ret = op0;
15257 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15258 VAR_INIT_STATUS_INITIALIZED);
15259 if (tmp == NULL)
15260 return NULL;
15261 add_loc_descr (&ret, tmp);
15262 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15263 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15264 add_loc_descr (&ret, l1label);
15265 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15266 add_loc_descr (&ret, l2jump);
15267 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15268 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15269 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15270 VAR_INIT_STATUS_INITIALIZED);
15271 if (tmp == NULL)
15272 return NULL;
15273 add_loc_descr (&ret, tmp);
15274 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15275 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15276 ? DW_OP_plus : DW_OP_xor, 0, 0));
15277 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15278 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15279 VAR_INIT_STATUS_INITIALIZED);
15280 add_loc_descr (&ret, tmp);
15281 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15282 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15283 add_loc_descr (&ret, l1jump);
15284 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15285 add_loc_descr (&ret, l2label);
15286 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15287 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15288 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15289 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15290 return ret;
15293 /* BSWAP (constS is initial shift count, either 56 or 24):
15294 constS const0
15295 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15296 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15297 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15298 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15299 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15301 static dw_loc_descr_ref
15302 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15303 machine_mode mem_mode)
15305 dw_loc_descr_ref op0, ret, tmp;
15306 dw_loc_descr_ref l1jump, l1label;
15307 dw_loc_descr_ref l2jump, l2label;
15309 if (BITS_PER_UNIT != 8
15310 || (GET_MODE_BITSIZE (mode) != 32
15311 && GET_MODE_BITSIZE (mode) != 64))
15312 return NULL;
15314 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15315 VAR_INIT_STATUS_INITIALIZED);
15316 if (op0 == NULL)
15317 return NULL;
15319 ret = op0;
15320 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15321 mode, mem_mode,
15322 VAR_INIT_STATUS_INITIALIZED);
15323 if (tmp == NULL)
15324 return NULL;
15325 add_loc_descr (&ret, tmp);
15326 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15327 VAR_INIT_STATUS_INITIALIZED);
15328 if (tmp == NULL)
15329 return NULL;
15330 add_loc_descr (&ret, tmp);
15331 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15332 add_loc_descr (&ret, l1label);
15333 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15334 mode, mem_mode,
15335 VAR_INIT_STATUS_INITIALIZED);
15336 add_loc_descr (&ret, tmp);
15337 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15338 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15339 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15340 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15341 VAR_INIT_STATUS_INITIALIZED);
15342 if (tmp == NULL)
15343 return NULL;
15344 add_loc_descr (&ret, tmp);
15345 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15346 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15347 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15348 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15349 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15350 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15351 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15352 VAR_INIT_STATUS_INITIALIZED);
15353 add_loc_descr (&ret, tmp);
15354 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15355 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15356 add_loc_descr (&ret, l2jump);
15357 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15358 VAR_INIT_STATUS_INITIALIZED);
15359 add_loc_descr (&ret, tmp);
15360 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15361 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15362 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15363 add_loc_descr (&ret, l1jump);
15364 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15365 add_loc_descr (&ret, l2label);
15366 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15367 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15368 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15369 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15370 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15371 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15372 return ret;
15375 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15376 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15377 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15378 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15380 ROTATERT is similar:
15381 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15382 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15383 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15385 static dw_loc_descr_ref
15386 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15387 machine_mode mem_mode)
15389 rtx rtlop1 = XEXP (rtl, 1);
15390 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15391 int i;
15393 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15394 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15395 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15396 VAR_INIT_STATUS_INITIALIZED);
15397 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15398 VAR_INIT_STATUS_INITIALIZED);
15399 if (op0 == NULL || op1 == NULL)
15400 return NULL;
15401 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15402 for (i = 0; i < 2; i++)
15404 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15405 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15406 mode, mem_mode,
15407 VAR_INIT_STATUS_INITIALIZED);
15408 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15409 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15410 ? DW_OP_const4u
15411 : HOST_BITS_PER_WIDE_INT == 64
15412 ? DW_OP_const8u : DW_OP_constu,
15413 GET_MODE_MASK (mode), 0);
15414 else
15415 mask[i] = NULL;
15416 if (mask[i] == NULL)
15417 return NULL;
15418 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15420 ret = op0;
15421 add_loc_descr (&ret, op1);
15422 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15423 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15424 if (GET_CODE (rtl) == ROTATERT)
15426 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15427 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15428 GET_MODE_BITSIZE (mode), 0));
15430 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15431 if (mask[0] != NULL)
15432 add_loc_descr (&ret, mask[0]);
15433 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15434 if (mask[1] != NULL)
15436 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15437 add_loc_descr (&ret, mask[1]);
15438 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15440 if (GET_CODE (rtl) == ROTATE)
15442 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15443 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15444 GET_MODE_BITSIZE (mode), 0));
15446 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15447 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15448 return ret;
15451 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15452 for DEBUG_PARAMETER_REF RTL. */
15454 static dw_loc_descr_ref
15455 parameter_ref_descriptor (rtx rtl)
15457 dw_loc_descr_ref ret;
15458 dw_die_ref ref;
15460 if (dwarf_strict)
15461 return NULL;
15462 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15463 /* With LTO during LTRANS we get the late DIE that refers to the early
15464 DIE, thus we add another indirection here. This seems to confuse
15465 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15466 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15467 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15468 if (ref)
15470 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15471 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15472 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15474 else
15476 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15477 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15479 return ret;
15482 /* The following routine converts the RTL for a variable or parameter
15483 (resident in memory) into an equivalent Dwarf representation of a
15484 mechanism for getting the address of that same variable onto the top of a
15485 hypothetical "address evaluation" stack.
15487 When creating memory location descriptors, we are effectively transforming
15488 the RTL for a memory-resident object into its Dwarf postfix expression
15489 equivalent. This routine recursively descends an RTL tree, turning
15490 it into Dwarf postfix code as it goes.
15492 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15494 MEM_MODE is the mode of the memory reference, needed to handle some
15495 autoincrement addressing modes.
15497 Return 0 if we can't represent the location. */
15499 dw_loc_descr_ref
15500 mem_loc_descriptor (rtx rtl, machine_mode mode,
15501 machine_mode mem_mode,
15502 enum var_init_status initialized)
15504 dw_loc_descr_ref mem_loc_result = NULL;
15505 enum dwarf_location_atom op;
15506 dw_loc_descr_ref op0, op1;
15507 rtx inner = NULL_RTX;
15508 poly_int64 offset;
15510 if (mode == VOIDmode)
15511 mode = GET_MODE (rtl);
15513 /* Note that for a dynamically sized array, the location we will generate a
15514 description of here will be the lowest numbered location which is
15515 actually within the array. That's *not* necessarily the same as the
15516 zeroth element of the array. */
15518 rtl = targetm.delegitimize_address (rtl);
15520 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15521 return NULL;
15523 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
15524 switch (GET_CODE (rtl))
15526 case POST_INC:
15527 case POST_DEC:
15528 case POST_MODIFY:
15529 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15531 case SUBREG:
15532 /* The case of a subreg may arise when we have a local (register)
15533 variable or a formal (register) parameter which doesn't quite fill
15534 up an entire register. For now, just assume that it is
15535 legitimate to make the Dwarf info refer to the whole register which
15536 contains the given subreg. */
15537 if (!subreg_lowpart_p (rtl))
15538 break;
15539 inner = SUBREG_REG (rtl);
15540 /* FALLTHRU */
15541 case TRUNCATE:
15542 if (inner == NULL_RTX)
15543 inner = XEXP (rtl, 0);
15544 if (is_a <scalar_int_mode> (mode, &int_mode)
15545 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15546 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15547 #ifdef POINTERS_EXTEND_UNSIGNED
15548 || (int_mode == Pmode && mem_mode != VOIDmode)
15549 #endif
15551 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15553 mem_loc_result = mem_loc_descriptor (inner,
15554 inner_mode,
15555 mem_mode, initialized);
15556 break;
15558 if (dwarf_strict && dwarf_version < 5)
15559 break;
15560 if (is_a <scalar_int_mode> (mode, &int_mode)
15561 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15562 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15563 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15565 dw_die_ref type_die;
15566 dw_loc_descr_ref cvt;
15568 mem_loc_result = mem_loc_descriptor (inner,
15569 GET_MODE (inner),
15570 mem_mode, initialized);
15571 if (mem_loc_result == NULL)
15572 break;
15573 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15574 if (type_die == NULL)
15576 mem_loc_result = NULL;
15577 break;
15579 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15580 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15581 else
15582 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15583 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15584 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15585 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15586 add_loc_descr (&mem_loc_result, cvt);
15587 if (is_a <scalar_int_mode> (mode, &int_mode)
15588 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15590 /* Convert it to untyped afterwards. */
15591 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15592 add_loc_descr (&mem_loc_result, cvt);
15595 break;
15597 case REG:
15598 if (!is_a <scalar_int_mode> (mode, &int_mode)
15599 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15600 && rtl != arg_pointer_rtx
15601 && rtl != frame_pointer_rtx
15602 #ifdef POINTERS_EXTEND_UNSIGNED
15603 && (int_mode != Pmode || mem_mode == VOIDmode)
15604 #endif
15607 dw_die_ref type_die;
15608 unsigned int dbx_regnum;
15610 if (dwarf_strict && dwarf_version < 5)
15611 break;
15612 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
15613 break;
15614 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15615 if (type_die == NULL)
15616 break;
15618 dbx_regnum = dbx_reg_number (rtl);
15619 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15620 break;
15621 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15622 dbx_regnum, 0);
15623 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15624 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15625 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15626 break;
15628 /* Whenever a register number forms a part of the description of the
15629 method for calculating the (dynamic) address of a memory resident
15630 object, DWARF rules require the register number be referred to as
15631 a "base register". This distinction is not based in any way upon
15632 what category of register the hardware believes the given register
15633 belongs to. This is strictly DWARF terminology we're dealing with
15634 here. Note that in cases where the location of a memory-resident
15635 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15636 OP_CONST (0)) the actual DWARF location descriptor that we generate
15637 may just be OP_BASEREG (basereg). This may look deceptively like
15638 the object in question was allocated to a register (rather than in
15639 memory) so DWARF consumers need to be aware of the subtle
15640 distinction between OP_REG and OP_BASEREG. */
15641 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15642 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15643 else if (stack_realign_drap
15644 && crtl->drap_reg
15645 && crtl->args.internal_arg_pointer == rtl
15646 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15648 /* If RTL is internal_arg_pointer, which has been optimized
15649 out, use DRAP instead. */
15650 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15651 VAR_INIT_STATUS_INITIALIZED);
15653 break;
15655 case SIGN_EXTEND:
15656 case ZERO_EXTEND:
15657 if (!is_a <scalar_int_mode> (mode, &int_mode)
15658 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15659 break;
15660 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15661 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15662 if (op0 == 0)
15663 break;
15664 else if (GET_CODE (rtl) == ZERO_EXTEND
15665 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15666 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15667 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15668 to expand zero extend as two shifts instead of
15669 masking. */
15670 && GET_MODE_SIZE (inner_mode) <= 4)
15672 mem_loc_result = op0;
15673 add_loc_descr (&mem_loc_result,
15674 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15675 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15677 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15679 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15680 shift *= BITS_PER_UNIT;
15681 if (GET_CODE (rtl) == SIGN_EXTEND)
15682 op = DW_OP_shra;
15683 else
15684 op = DW_OP_shr;
15685 mem_loc_result = op0;
15686 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15687 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15688 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15689 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15691 else if (!dwarf_strict || dwarf_version >= 5)
15693 dw_die_ref type_die1, type_die2;
15694 dw_loc_descr_ref cvt;
15696 type_die1 = base_type_for_mode (inner_mode,
15697 GET_CODE (rtl) == ZERO_EXTEND);
15698 if (type_die1 == NULL)
15699 break;
15700 type_die2 = base_type_for_mode (int_mode, 1);
15701 if (type_die2 == NULL)
15702 break;
15703 mem_loc_result = op0;
15704 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15705 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15706 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
15707 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15708 add_loc_descr (&mem_loc_result, cvt);
15709 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15710 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15711 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
15712 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15713 add_loc_descr (&mem_loc_result, cvt);
15715 break;
15717 case MEM:
15719 rtx new_rtl = avoid_constant_pool_reference (rtl);
15720 if (new_rtl != rtl)
15722 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
15723 initialized);
15724 if (mem_loc_result != NULL)
15725 return mem_loc_result;
15728 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
15729 get_address_mode (rtl), mode,
15730 VAR_INIT_STATUS_INITIALIZED);
15731 if (mem_loc_result == NULL)
15732 mem_loc_result = tls_mem_loc_descriptor (rtl);
15733 if (mem_loc_result != NULL)
15735 if (!is_a <scalar_int_mode> (mode, &int_mode)
15736 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15738 dw_die_ref type_die;
15739 dw_loc_descr_ref deref;
15740 HOST_WIDE_INT size;
15742 if (dwarf_strict && dwarf_version < 5)
15743 return NULL;
15744 if (!GET_MODE_SIZE (mode).is_constant (&size))
15745 return NULL;
15746 type_die
15747 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15748 if (type_die == NULL)
15749 return NULL;
15750 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
15751 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15752 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15753 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
15754 add_loc_descr (&mem_loc_result, deref);
15756 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15757 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15758 else
15759 add_loc_descr (&mem_loc_result,
15760 new_loc_descr (DW_OP_deref_size,
15761 GET_MODE_SIZE (int_mode), 0));
15763 break;
15765 case LO_SUM:
15766 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15768 case LABEL_REF:
15769 /* Some ports can transform a symbol ref into a label ref, because
15770 the symbol ref is too far away and has to be dumped into a constant
15771 pool. */
15772 case CONST:
15773 case SYMBOL_REF:
15774 case UNSPEC:
15775 if (!is_a <scalar_int_mode> (mode, &int_mode)
15776 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15777 #ifdef POINTERS_EXTEND_UNSIGNED
15778 && (int_mode != Pmode || mem_mode == VOIDmode)
15779 #endif
15781 break;
15783 if (GET_CODE (rtl) == UNSPEC)
15785 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15786 can't express it in the debug info. This can happen e.g. with some
15787 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
15788 approves. */
15789 bool not_ok = false;
15790 subrtx_var_iterator::array_type array;
15791 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
15792 if (*iter != rtl && !CONSTANT_P (*iter))
15794 not_ok = true;
15795 break;
15798 if (not_ok)
15799 break;
15801 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
15802 if (!const_ok_for_output_1 (*iter))
15804 not_ok = true;
15805 break;
15808 if (not_ok)
15809 break;
15811 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
15812 goto symref;
15815 if (GET_CODE (rtl) == SYMBOL_REF
15816 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15818 dw_loc_descr_ref temp;
15820 /* If this is not defined, we have no way to emit the data. */
15821 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15822 break;
15824 temp = new_addr_loc_descr (rtl, dtprel_true);
15826 /* We check for DWARF 5 here because gdb did not implement
15827 DW_OP_form_tls_address until after 7.12. */
15828 mem_loc_result = new_loc_descr ((dwarf_version >= 5
15829 ? DW_OP_form_tls_address
15830 : DW_OP_GNU_push_tls_address),
15831 0, 0);
15832 add_loc_descr (&mem_loc_result, temp);
15834 break;
15837 if (!const_ok_for_output (rtl))
15839 if (GET_CODE (rtl) == CONST)
15840 switch (GET_CODE (XEXP (rtl, 0)))
15842 case NOT:
15843 op = DW_OP_not;
15844 goto try_const_unop;
15845 case NEG:
15846 op = DW_OP_neg;
15847 goto try_const_unop;
15848 try_const_unop:
15849 rtx arg;
15850 arg = XEXP (XEXP (rtl, 0), 0);
15851 if (!CONSTANT_P (arg))
15852 arg = gen_rtx_CONST (int_mode, arg);
15853 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
15854 initialized);
15855 if (op0)
15857 mem_loc_result = op0;
15858 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15860 break;
15861 default:
15862 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
15863 mem_mode, initialized);
15864 break;
15866 break;
15869 symref:
15870 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15871 vec_safe_push (used_rtx_array, rtl);
15872 break;
15874 case CONCAT:
15875 case CONCATN:
15876 case VAR_LOCATION:
15877 case DEBUG_IMPLICIT_PTR:
15878 expansion_failed (NULL_TREE, rtl,
15879 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15880 return 0;
15882 case ENTRY_VALUE:
15883 if (dwarf_strict && dwarf_version < 5)
15884 return NULL;
15885 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15887 if (!is_a <scalar_int_mode> (mode, &int_mode)
15888 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15889 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15890 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15891 else
15893 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15894 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15895 return NULL;
15896 op0 = one_reg_loc_descriptor (dbx_regnum,
15897 VAR_INIT_STATUS_INITIALIZED);
15900 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15901 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15903 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15904 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15905 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15906 return NULL;
15908 else
15909 gcc_unreachable ();
15910 if (op0 == NULL)
15911 return NULL;
15912 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15913 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15914 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15915 break;
15917 case DEBUG_PARAMETER_REF:
15918 mem_loc_result = parameter_ref_descriptor (rtl);
15919 break;
15921 case PRE_MODIFY:
15922 /* Extract the PLUS expression nested inside and fall into
15923 PLUS code below. */
15924 rtl = XEXP (rtl, 1);
15925 goto plus;
15927 case PRE_INC:
15928 case PRE_DEC:
15929 /* Turn these into a PLUS expression and fall into the PLUS code
15930 below. */
15931 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15932 gen_int_mode (GET_CODE (rtl) == PRE_INC
15933 ? GET_MODE_UNIT_SIZE (mem_mode)
15934 : -GET_MODE_UNIT_SIZE (mem_mode),
15935 mode));
15937 /* fall through */
15939 case PLUS:
15940 plus:
15941 if (is_based_loc (rtl)
15942 && is_a <scalar_int_mode> (mode, &int_mode)
15943 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15944 || XEXP (rtl, 0) == arg_pointer_rtx
15945 || XEXP (rtl, 0) == frame_pointer_rtx))
15946 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15947 INTVAL (XEXP (rtl, 1)),
15948 VAR_INIT_STATUS_INITIALIZED);
15949 else
15951 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15952 VAR_INIT_STATUS_INITIALIZED);
15953 if (mem_loc_result == 0)
15954 break;
15956 if (CONST_INT_P (XEXP (rtl, 1))
15957 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15958 <= DWARF2_ADDR_SIZE))
15959 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15960 else
15962 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15963 VAR_INIT_STATUS_INITIALIZED);
15964 if (op1 == 0)
15965 return NULL;
15966 add_loc_descr (&mem_loc_result, op1);
15967 add_loc_descr (&mem_loc_result,
15968 new_loc_descr (DW_OP_plus, 0, 0));
15971 break;
15973 /* If a pseudo-reg is optimized away, it is possible for it to
15974 be replaced with a MEM containing a multiply or shift. */
15975 case MINUS:
15976 op = DW_OP_minus;
15977 goto do_binop;
15979 case MULT:
15980 op = DW_OP_mul;
15981 goto do_binop;
15983 case DIV:
15984 if ((!dwarf_strict || dwarf_version >= 5)
15985 && is_a <scalar_int_mode> (mode, &int_mode)
15986 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15988 mem_loc_result = typed_binop (DW_OP_div, rtl,
15989 base_type_for_mode (mode, 0),
15990 int_mode, mem_mode);
15991 break;
15993 op = DW_OP_div;
15994 goto do_binop;
15996 case UMOD:
15997 op = DW_OP_mod;
15998 goto do_binop;
16000 case ASHIFT:
16001 op = DW_OP_shl;
16002 goto do_shift;
16004 case ASHIFTRT:
16005 op = DW_OP_shra;
16006 goto do_shift;
16008 case LSHIFTRT:
16009 op = DW_OP_shr;
16010 goto do_shift;
16012 do_shift:
16013 if (!is_a <scalar_int_mode> (mode, &int_mode))
16014 break;
16015 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16016 VAR_INIT_STATUS_INITIALIZED);
16018 rtx rtlop1 = XEXP (rtl, 1);
16019 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16020 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16021 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16022 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16023 VAR_INIT_STATUS_INITIALIZED);
16026 if (op0 == 0 || op1 == 0)
16027 break;
16029 mem_loc_result = op0;
16030 add_loc_descr (&mem_loc_result, op1);
16031 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16032 break;
16034 case AND:
16035 op = DW_OP_and;
16036 goto do_binop;
16038 case IOR:
16039 op = DW_OP_or;
16040 goto do_binop;
16042 case XOR:
16043 op = DW_OP_xor;
16044 goto do_binop;
16046 do_binop:
16047 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16048 VAR_INIT_STATUS_INITIALIZED);
16049 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16050 VAR_INIT_STATUS_INITIALIZED);
16052 if (op0 == 0 || op1 == 0)
16053 break;
16055 mem_loc_result = op0;
16056 add_loc_descr (&mem_loc_result, op1);
16057 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16058 break;
16060 case MOD:
16061 if ((!dwarf_strict || dwarf_version >= 5)
16062 && is_a <scalar_int_mode> (mode, &int_mode)
16063 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16065 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16066 base_type_for_mode (mode, 0),
16067 int_mode, mem_mode);
16068 break;
16071 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16072 VAR_INIT_STATUS_INITIALIZED);
16073 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16074 VAR_INIT_STATUS_INITIALIZED);
16076 if (op0 == 0 || op1 == 0)
16077 break;
16079 mem_loc_result = op0;
16080 add_loc_descr (&mem_loc_result, op1);
16081 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
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_div, 0, 0));
16084 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16085 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16086 break;
16088 case UDIV:
16089 if ((!dwarf_strict || dwarf_version >= 5)
16090 && is_a <scalar_int_mode> (mode, &int_mode))
16092 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16094 op = DW_OP_div;
16095 goto do_binop;
16097 mem_loc_result = typed_binop (DW_OP_div, rtl,
16098 base_type_for_mode (int_mode, 1),
16099 int_mode, mem_mode);
16101 break;
16103 case NOT:
16104 op = DW_OP_not;
16105 goto do_unop;
16107 case ABS:
16108 op = DW_OP_abs;
16109 goto do_unop;
16111 case NEG:
16112 op = DW_OP_neg;
16113 goto do_unop;
16115 do_unop:
16116 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16117 VAR_INIT_STATUS_INITIALIZED);
16119 if (op0 == 0)
16120 break;
16122 mem_loc_result = op0;
16123 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16124 break;
16126 case CONST_INT:
16127 if (!is_a <scalar_int_mode> (mode, &int_mode)
16128 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16129 #ifdef POINTERS_EXTEND_UNSIGNED
16130 || (int_mode == Pmode
16131 && mem_mode != VOIDmode
16132 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16133 #endif
16136 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16137 break;
16139 if ((!dwarf_strict || dwarf_version >= 5)
16140 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16141 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16143 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16144 scalar_int_mode amode;
16145 if (type_die == NULL)
16146 return NULL;
16147 if (INTVAL (rtl) >= 0
16148 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16149 .exists (&amode))
16150 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16151 /* const DW_OP_convert <XXX> vs.
16152 DW_OP_const_type <XXX, 1, const>. */
16153 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16154 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16156 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16157 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16158 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16159 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16160 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16161 add_loc_descr (&mem_loc_result, op0);
16162 return mem_loc_result;
16164 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16165 INTVAL (rtl));
16166 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16167 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16168 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16169 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16170 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16171 else
16173 mem_loc_result->dw_loc_oprnd2.val_class
16174 = dw_val_class_const_double;
16175 mem_loc_result->dw_loc_oprnd2.v.val_double
16176 = double_int::from_shwi (INTVAL (rtl));
16179 break;
16181 case CONST_DOUBLE:
16182 if (!dwarf_strict || dwarf_version >= 5)
16184 dw_die_ref type_die;
16186 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16187 CONST_DOUBLE rtx could represent either a large integer
16188 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16189 the value is always a floating point constant.
16191 When it is an integer, a CONST_DOUBLE is used whenever
16192 the constant requires 2 HWIs to be adequately represented.
16193 We output CONST_DOUBLEs as blocks. */
16194 if (mode == VOIDmode
16195 || (GET_MODE (rtl) == VOIDmode
16196 && maybe_ne (GET_MODE_BITSIZE (mode),
16197 HOST_BITS_PER_DOUBLE_INT)))
16198 break;
16199 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16200 if (type_die == NULL)
16201 return NULL;
16202 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16203 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16204 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16205 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16206 #if TARGET_SUPPORTS_WIDE_INT == 0
16207 if (!SCALAR_FLOAT_MODE_P (mode))
16209 mem_loc_result->dw_loc_oprnd2.val_class
16210 = dw_val_class_const_double;
16211 mem_loc_result->dw_loc_oprnd2.v.val_double
16212 = rtx_to_double_int (rtl);
16214 else
16215 #endif
16217 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16218 unsigned int length = GET_MODE_SIZE (float_mode);
16219 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16221 insert_float (rtl, array);
16222 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16223 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16224 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16225 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16228 break;
16230 case CONST_WIDE_INT:
16231 if (!dwarf_strict || dwarf_version >= 5)
16233 dw_die_ref type_die;
16235 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16236 if (type_die == NULL)
16237 return NULL;
16238 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16239 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16240 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16241 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16242 mem_loc_result->dw_loc_oprnd2.val_class
16243 = dw_val_class_wide_int;
16244 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16245 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16247 break;
16249 case CONST_POLY_INT:
16250 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16251 break;
16253 case EQ:
16254 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16255 break;
16257 case GE:
16258 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16259 break;
16261 case GT:
16262 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16263 break;
16265 case LE:
16266 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16267 break;
16269 case LT:
16270 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16271 break;
16273 case NE:
16274 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16275 break;
16277 case GEU:
16278 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16279 break;
16281 case GTU:
16282 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16283 break;
16285 case LEU:
16286 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16287 break;
16289 case LTU:
16290 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16291 break;
16293 case UMIN:
16294 case UMAX:
16295 if (!SCALAR_INT_MODE_P (mode))
16296 break;
16297 /* FALLTHRU */
16298 case SMIN:
16299 case SMAX:
16300 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16301 break;
16303 case ZERO_EXTRACT:
16304 case SIGN_EXTRACT:
16305 if (CONST_INT_P (XEXP (rtl, 1))
16306 && CONST_INT_P (XEXP (rtl, 2))
16307 && is_a <scalar_int_mode> (mode, &int_mode)
16308 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16309 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16310 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16311 && ((unsigned) INTVAL (XEXP (rtl, 1))
16312 + (unsigned) INTVAL (XEXP (rtl, 2))
16313 <= GET_MODE_BITSIZE (int_mode)))
16315 int shift, size;
16316 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16317 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16318 if (op0 == 0)
16319 break;
16320 if (GET_CODE (rtl) == SIGN_EXTRACT)
16321 op = DW_OP_shra;
16322 else
16323 op = DW_OP_shr;
16324 mem_loc_result = op0;
16325 size = INTVAL (XEXP (rtl, 1));
16326 shift = INTVAL (XEXP (rtl, 2));
16327 if (BITS_BIG_ENDIAN)
16328 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16329 if (shift + size != (int) DWARF2_ADDR_SIZE)
16331 add_loc_descr (&mem_loc_result,
16332 int_loc_descriptor (DWARF2_ADDR_SIZE
16333 - shift - size));
16334 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16336 if (size != (int) DWARF2_ADDR_SIZE)
16338 add_loc_descr (&mem_loc_result,
16339 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16340 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16343 break;
16345 case IF_THEN_ELSE:
16347 dw_loc_descr_ref op2, bra_node, drop_node;
16348 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16349 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16350 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16351 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16352 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16353 VAR_INIT_STATUS_INITIALIZED);
16354 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16355 VAR_INIT_STATUS_INITIALIZED);
16356 if (op0 == NULL || op1 == NULL || op2 == NULL)
16357 break;
16359 mem_loc_result = op1;
16360 add_loc_descr (&mem_loc_result, op2);
16361 add_loc_descr (&mem_loc_result, op0);
16362 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16363 add_loc_descr (&mem_loc_result, bra_node);
16364 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16365 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16366 add_loc_descr (&mem_loc_result, drop_node);
16367 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16368 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16370 break;
16372 case FLOAT_EXTEND:
16373 case FLOAT_TRUNCATE:
16374 case FLOAT:
16375 case UNSIGNED_FLOAT:
16376 case FIX:
16377 case UNSIGNED_FIX:
16378 if (!dwarf_strict || dwarf_version >= 5)
16380 dw_die_ref type_die;
16381 dw_loc_descr_ref cvt;
16383 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16384 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16385 if (op0 == NULL)
16386 break;
16387 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16388 && (GET_CODE (rtl) == FLOAT
16389 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16391 type_die = base_type_for_mode (int_mode,
16392 GET_CODE (rtl) == UNSIGNED_FLOAT);
16393 if (type_die == NULL)
16394 break;
16395 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16396 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16397 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16398 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16399 add_loc_descr (&op0, cvt);
16401 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16402 if (type_die == NULL)
16403 break;
16404 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16405 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16406 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16407 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16408 add_loc_descr (&op0, cvt);
16409 if (is_a <scalar_int_mode> (mode, &int_mode)
16410 && (GET_CODE (rtl) == FIX
16411 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16413 op0 = convert_descriptor_to_mode (int_mode, op0);
16414 if (op0 == NULL)
16415 break;
16417 mem_loc_result = op0;
16419 break;
16421 case CLZ:
16422 case CTZ:
16423 case FFS:
16424 if (is_a <scalar_int_mode> (mode, &int_mode))
16425 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16426 break;
16428 case POPCOUNT:
16429 case PARITY:
16430 if (is_a <scalar_int_mode> (mode, &int_mode))
16431 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16432 break;
16434 case BSWAP:
16435 if (is_a <scalar_int_mode> (mode, &int_mode))
16436 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16437 break;
16439 case ROTATE:
16440 case ROTATERT:
16441 if (is_a <scalar_int_mode> (mode, &int_mode))
16442 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16443 break;
16445 case COMPARE:
16446 /* In theory, we could implement the above. */
16447 /* DWARF cannot represent the unsigned compare operations
16448 natively. */
16449 case SS_MULT:
16450 case US_MULT:
16451 case SS_DIV:
16452 case US_DIV:
16453 case SS_PLUS:
16454 case US_PLUS:
16455 case SS_MINUS:
16456 case US_MINUS:
16457 case SS_NEG:
16458 case US_NEG:
16459 case SS_ABS:
16460 case SS_ASHIFT:
16461 case US_ASHIFT:
16462 case SS_TRUNCATE:
16463 case US_TRUNCATE:
16464 case UNORDERED:
16465 case ORDERED:
16466 case UNEQ:
16467 case UNGE:
16468 case UNGT:
16469 case UNLE:
16470 case UNLT:
16471 case LTGT:
16472 case FRACT_CONVERT:
16473 case UNSIGNED_FRACT_CONVERT:
16474 case SAT_FRACT:
16475 case UNSIGNED_SAT_FRACT:
16476 case SQRT:
16477 case ASM_OPERANDS:
16478 case VEC_MERGE:
16479 case VEC_SELECT:
16480 case VEC_CONCAT:
16481 case VEC_DUPLICATE:
16482 case VEC_SERIES:
16483 case HIGH:
16484 case FMA:
16485 case STRICT_LOW_PART:
16486 case CONST_VECTOR:
16487 case CONST_FIXED:
16488 case CLRSB:
16489 case CLOBBER:
16490 break;
16492 case CONST_STRING:
16493 resolve_one_addr (&rtl);
16494 goto symref;
16496 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16497 the expression. An UNSPEC rtx represents a raw DWARF operation,
16498 new_loc_descr is called for it to build the operation directly.
16499 Otherwise mem_loc_descriptor is called recursively. */
16500 case PARALLEL:
16502 int index = 0;
16503 dw_loc_descr_ref exp_result = NULL;
16505 for (; index < XVECLEN (rtl, 0); index++)
16507 rtx elem = XVECEXP (rtl, 0, index);
16508 if (GET_CODE (elem) == UNSPEC)
16510 /* Each DWARF operation UNSPEC contain two operands, if
16511 one operand is not used for the operation, const0_rtx is
16512 passed. */
16513 gcc_assert (XVECLEN (elem, 0) == 2);
16515 HOST_WIDE_INT dw_op = XINT (elem, 1);
16516 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16517 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16518 exp_result
16519 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16520 oprnd2);
16522 else
16523 exp_result
16524 = mem_loc_descriptor (elem, mode, mem_mode,
16525 VAR_INIT_STATUS_INITIALIZED);
16527 if (!mem_loc_result)
16528 mem_loc_result = exp_result;
16529 else
16530 add_loc_descr (&mem_loc_result, exp_result);
16533 break;
16536 default:
16537 if (flag_checking)
16539 print_rtl (stderr, rtl);
16540 gcc_unreachable ();
16542 break;
16545 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16546 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16548 return mem_loc_result;
16551 /* Return a descriptor that describes the concatenation of two locations.
16552 This is typically a complex variable. */
16554 static dw_loc_descr_ref
16555 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16557 /* At present we only track constant-sized pieces. */
16558 unsigned int size0, size1;
16559 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16560 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16561 return 0;
16563 dw_loc_descr_ref cc_loc_result = NULL;
16564 dw_loc_descr_ref x0_ref
16565 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16566 dw_loc_descr_ref x1_ref
16567 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16569 if (x0_ref == 0 || x1_ref == 0)
16570 return 0;
16572 cc_loc_result = x0_ref;
16573 add_loc_descr_op_piece (&cc_loc_result, size0);
16575 add_loc_descr (&cc_loc_result, x1_ref);
16576 add_loc_descr_op_piece (&cc_loc_result, size1);
16578 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16579 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16581 return cc_loc_result;
16584 /* Return a descriptor that describes the concatenation of N
16585 locations. */
16587 static dw_loc_descr_ref
16588 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16590 unsigned int i;
16591 dw_loc_descr_ref cc_loc_result = NULL;
16592 unsigned int n = XVECLEN (concatn, 0);
16593 unsigned int size;
16595 for (i = 0; i < n; ++i)
16597 dw_loc_descr_ref ref;
16598 rtx x = XVECEXP (concatn, 0, i);
16600 /* At present we only track constant-sized pieces. */
16601 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16602 return NULL;
16604 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16605 if (ref == NULL)
16606 return NULL;
16608 add_loc_descr (&cc_loc_result, ref);
16609 add_loc_descr_op_piece (&cc_loc_result, size);
16612 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16613 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16615 return cc_loc_result;
16618 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16619 for DEBUG_IMPLICIT_PTR RTL. */
16621 static dw_loc_descr_ref
16622 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16624 dw_loc_descr_ref ret;
16625 dw_die_ref ref;
16627 if (dwarf_strict && dwarf_version < 5)
16628 return NULL;
16629 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16630 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16631 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16632 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16633 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16634 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16635 if (ref)
16637 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16638 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16639 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16641 else
16643 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16644 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16646 return ret;
16649 /* Output a proper Dwarf location descriptor for a variable or parameter
16650 which is either allocated in a register or in a memory location. For a
16651 register, we just generate an OP_REG and the register number. For a
16652 memory location we provide a Dwarf postfix expression describing how to
16653 generate the (dynamic) address of the object onto the address stack.
16655 MODE is mode of the decl if this loc_descriptor is going to be used in
16656 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16657 allowed, VOIDmode otherwise.
16659 If we don't know how to describe it, return 0. */
16661 static dw_loc_descr_ref
16662 loc_descriptor (rtx rtl, machine_mode mode,
16663 enum var_init_status initialized)
16665 dw_loc_descr_ref loc_result = NULL;
16666 scalar_int_mode int_mode;
16668 switch (GET_CODE (rtl))
16670 case SUBREG:
16671 /* The case of a subreg may arise when we have a local (register)
16672 variable or a formal (register) parameter which doesn't quite fill
16673 up an entire register. For now, just assume that it is
16674 legitimate to make the Dwarf info refer to the whole register which
16675 contains the given subreg. */
16676 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16677 loc_result = loc_descriptor (SUBREG_REG (rtl),
16678 GET_MODE (SUBREG_REG (rtl)), initialized);
16679 else
16680 goto do_default;
16681 break;
16683 case REG:
16684 loc_result = reg_loc_descriptor (rtl, initialized);
16685 break;
16687 case MEM:
16688 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16689 GET_MODE (rtl), initialized);
16690 if (loc_result == NULL)
16691 loc_result = tls_mem_loc_descriptor (rtl);
16692 if (loc_result == NULL)
16694 rtx new_rtl = avoid_constant_pool_reference (rtl);
16695 if (new_rtl != rtl)
16696 loc_result = loc_descriptor (new_rtl, mode, initialized);
16698 break;
16700 case CONCAT:
16701 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
16702 initialized);
16703 break;
16705 case CONCATN:
16706 loc_result = concatn_loc_descriptor (rtl, initialized);
16707 break;
16709 case VAR_LOCATION:
16710 /* Single part. */
16711 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
16713 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
16714 if (GET_CODE (loc) == EXPR_LIST)
16715 loc = XEXP (loc, 0);
16716 loc_result = loc_descriptor (loc, mode, initialized);
16717 break;
16720 rtl = XEXP (rtl, 1);
16721 /* FALLTHRU */
16723 case PARALLEL:
16725 rtvec par_elems = XVEC (rtl, 0);
16726 int num_elem = GET_NUM_ELEM (par_elems);
16727 machine_mode mode;
16728 int i, size;
16730 /* Create the first one, so we have something to add to. */
16731 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
16732 VOIDmode, initialized);
16733 if (loc_result == NULL)
16734 return NULL;
16735 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
16736 /* At present we only track constant-sized pieces. */
16737 if (!GET_MODE_SIZE (mode).is_constant (&size))
16738 return NULL;
16739 add_loc_descr_op_piece (&loc_result, size);
16740 for (i = 1; i < num_elem; i++)
16742 dw_loc_descr_ref temp;
16744 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
16745 VOIDmode, initialized);
16746 if (temp == NULL)
16747 return NULL;
16748 add_loc_descr (&loc_result, temp);
16749 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
16750 /* At present we only track constant-sized pieces. */
16751 if (!GET_MODE_SIZE (mode).is_constant (&size))
16752 return NULL;
16753 add_loc_descr_op_piece (&loc_result, size);
16756 break;
16758 case CONST_INT:
16759 if (mode != VOIDmode && mode != BLKmode)
16761 int_mode = as_a <scalar_int_mode> (mode);
16762 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
16763 INTVAL (rtl));
16765 break;
16767 case CONST_DOUBLE:
16768 if (mode == VOIDmode)
16769 mode = GET_MODE (rtl);
16771 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16773 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16775 /* Note that a CONST_DOUBLE rtx could represent either an integer
16776 or a floating-point constant. A CONST_DOUBLE is used whenever
16777 the constant requires more than one word in order to be
16778 adequately represented. We output CONST_DOUBLEs as blocks. */
16779 scalar_mode smode = as_a <scalar_mode> (mode);
16780 loc_result = new_loc_descr (DW_OP_implicit_value,
16781 GET_MODE_SIZE (smode), 0);
16782 #if TARGET_SUPPORTS_WIDE_INT == 0
16783 if (!SCALAR_FLOAT_MODE_P (smode))
16785 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
16786 loc_result->dw_loc_oprnd2.v.val_double
16787 = rtx_to_double_int (rtl);
16789 else
16790 #endif
16792 unsigned int length = GET_MODE_SIZE (smode);
16793 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16795 insert_float (rtl, array);
16796 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16797 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16798 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16799 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16802 break;
16804 case CONST_WIDE_INT:
16805 if (mode == VOIDmode)
16806 mode = GET_MODE (rtl);
16808 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16810 int_mode = as_a <scalar_int_mode> (mode);
16811 loc_result = new_loc_descr (DW_OP_implicit_value,
16812 GET_MODE_SIZE (int_mode), 0);
16813 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16814 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16815 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
16817 break;
16819 case CONST_VECTOR:
16820 if (mode == VOIDmode)
16821 mode = GET_MODE (rtl);
16823 if (mode != VOIDmode
16824 /* The combination of a length and byte elt_size doesn't extend
16825 naturally to boolean vectors, where several elements are packed
16826 into the same byte. */
16827 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
16828 && (dwarf_version >= 4 || !dwarf_strict))
16830 unsigned int length;
16831 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
16832 return NULL;
16834 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16835 unsigned char *array
16836 = ggc_vec_alloc<unsigned char> (length * elt_size);
16837 unsigned int i;
16838 unsigned char *p;
16839 machine_mode imode = GET_MODE_INNER (mode);
16841 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16842 switch (GET_MODE_CLASS (mode))
16844 case MODE_VECTOR_INT:
16845 for (i = 0, p = array; i < length; i++, p += elt_size)
16847 rtx elt = CONST_VECTOR_ELT (rtl, i);
16848 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16850 break;
16852 case MODE_VECTOR_FLOAT:
16853 for (i = 0, p = array; i < length; i++, p += elt_size)
16855 rtx elt = CONST_VECTOR_ELT (rtl, i);
16856 insert_float (elt, p);
16858 break;
16860 default:
16861 gcc_unreachable ();
16864 loc_result = new_loc_descr (DW_OP_implicit_value,
16865 length * elt_size, 0);
16866 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16867 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16868 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16869 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16871 break;
16873 case CONST:
16874 if (mode == VOIDmode
16875 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16876 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16877 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16879 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16880 break;
16882 /* FALLTHROUGH */
16883 case SYMBOL_REF:
16884 if (!const_ok_for_output (rtl))
16885 break;
16886 /* FALLTHROUGH */
16887 case LABEL_REF:
16888 if (is_a <scalar_int_mode> (mode, &int_mode)
16889 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
16890 && (dwarf_version >= 4 || !dwarf_strict))
16892 loc_result = new_addr_loc_descr (rtl, dtprel_false);
16893 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16894 vec_safe_push (used_rtx_array, rtl);
16896 break;
16898 case DEBUG_IMPLICIT_PTR:
16899 loc_result = implicit_ptr_descriptor (rtl, 0);
16900 break;
16902 case PLUS:
16903 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16904 && CONST_INT_P (XEXP (rtl, 1)))
16906 loc_result
16907 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16908 break;
16910 /* FALLTHRU */
16911 do_default:
16912 default:
16913 if ((is_a <scalar_int_mode> (mode, &int_mode)
16914 && GET_MODE (rtl) == int_mode
16915 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16916 && dwarf_version >= 4)
16917 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16919 /* Value expression. */
16920 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16921 if (loc_result)
16922 add_loc_descr (&loc_result,
16923 new_loc_descr (DW_OP_stack_value, 0, 0));
16925 break;
16928 return loc_result;
16931 /* We need to figure out what section we should use as the base for the
16932 address ranges where a given location is valid.
16933 1. If this particular DECL has a section associated with it, use that.
16934 2. If this function has a section associated with it, use that.
16935 3. Otherwise, use the text section.
16936 XXX: If you split a variable across multiple sections, we won't notice. */
16938 static const char *
16939 secname_for_decl (const_tree decl)
16941 const char *secname;
16943 if (VAR_OR_FUNCTION_DECL_P (decl)
16944 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16945 && DECL_SECTION_NAME (decl))
16946 secname = DECL_SECTION_NAME (decl);
16947 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16949 if (in_cold_section_p)
16951 section *sec = current_function_section ();
16952 if (sec->common.flags & SECTION_NAMED)
16953 return sec->named.name;
16955 secname = DECL_SECTION_NAME (current_function_decl);
16957 else if (cfun && in_cold_section_p)
16958 secname = crtl->subsections.cold_section_label;
16959 else
16960 secname = text_section_label;
16962 return secname;
16965 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16967 static bool
16968 decl_by_reference_p (tree decl)
16970 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16971 || VAR_P (decl))
16972 && DECL_BY_REFERENCE (decl));
16975 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16976 for VARLOC. */
16978 static dw_loc_descr_ref
16979 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16980 enum var_init_status initialized)
16982 int have_address = 0;
16983 dw_loc_descr_ref descr;
16984 machine_mode mode;
16986 if (want_address != 2)
16988 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16989 /* Single part. */
16990 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16992 varloc = PAT_VAR_LOCATION_LOC (varloc);
16993 if (GET_CODE (varloc) == EXPR_LIST)
16994 varloc = XEXP (varloc, 0);
16995 mode = GET_MODE (varloc);
16996 if (MEM_P (varloc))
16998 rtx addr = XEXP (varloc, 0);
16999 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17000 mode, initialized);
17001 if (descr)
17002 have_address = 1;
17003 else
17005 rtx x = avoid_constant_pool_reference (varloc);
17006 if (x != varloc)
17007 descr = mem_loc_descriptor (x, mode, VOIDmode,
17008 initialized);
17011 else
17012 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17014 else
17015 return 0;
17017 else
17019 if (GET_CODE (varloc) == VAR_LOCATION)
17020 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17021 else
17022 mode = DECL_MODE (loc);
17023 descr = loc_descriptor (varloc, mode, initialized);
17024 have_address = 1;
17027 if (!descr)
17028 return 0;
17030 if (want_address == 2 && !have_address
17031 && (dwarf_version >= 4 || !dwarf_strict))
17033 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17035 expansion_failed (loc, NULL_RTX,
17036 "DWARF address size mismatch");
17037 return 0;
17039 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17040 have_address = 1;
17042 /* Show if we can't fill the request for an address. */
17043 if (want_address && !have_address)
17045 expansion_failed (loc, NULL_RTX,
17046 "Want address and only have value");
17047 return 0;
17050 /* If we've got an address and don't want one, dereference. */
17051 if (!want_address && have_address)
17053 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17054 enum dwarf_location_atom op;
17056 if (size > DWARF2_ADDR_SIZE || size == -1)
17058 expansion_failed (loc, NULL_RTX,
17059 "DWARF address size mismatch");
17060 return 0;
17062 else if (size == DWARF2_ADDR_SIZE)
17063 op = DW_OP_deref;
17064 else
17065 op = DW_OP_deref_size;
17067 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17070 return descr;
17073 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17074 if it is not possible. */
17076 static dw_loc_descr_ref
17077 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17079 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17080 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17081 else if (dwarf_version >= 3 || !dwarf_strict)
17082 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17083 else
17084 return NULL;
17087 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17088 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17090 static dw_loc_descr_ref
17091 dw_sra_loc_expr (tree decl, rtx loc)
17093 rtx p;
17094 unsigned HOST_WIDE_INT padsize = 0;
17095 dw_loc_descr_ref descr, *descr_tail;
17096 unsigned HOST_WIDE_INT decl_size;
17097 rtx varloc;
17098 enum var_init_status initialized;
17100 if (DECL_SIZE (decl) == NULL
17101 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17102 return NULL;
17104 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17105 descr = NULL;
17106 descr_tail = &descr;
17108 for (p = loc; p; p = XEXP (p, 1))
17110 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17111 rtx loc_note = *decl_piece_varloc_ptr (p);
17112 dw_loc_descr_ref cur_descr;
17113 dw_loc_descr_ref *tail, last = NULL;
17114 unsigned HOST_WIDE_INT opsize = 0;
17116 if (loc_note == NULL_RTX
17117 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17119 padsize += bitsize;
17120 continue;
17122 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17123 varloc = NOTE_VAR_LOCATION (loc_note);
17124 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17125 if (cur_descr == NULL)
17127 padsize += bitsize;
17128 continue;
17131 /* Check that cur_descr either doesn't use
17132 DW_OP_*piece operations, or their sum is equal
17133 to bitsize. Otherwise we can't embed it. */
17134 for (tail = &cur_descr; *tail != NULL;
17135 tail = &(*tail)->dw_loc_next)
17136 if ((*tail)->dw_loc_opc == DW_OP_piece)
17138 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17139 * BITS_PER_UNIT;
17140 last = *tail;
17142 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17144 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17145 last = *tail;
17148 if (last != NULL && opsize != bitsize)
17150 padsize += bitsize;
17151 /* Discard the current piece of the descriptor and release any
17152 addr_table entries it uses. */
17153 remove_loc_list_addr_table_entries (cur_descr);
17154 continue;
17157 /* If there is a hole, add DW_OP_*piece after empty DWARF
17158 expression, which means that those bits are optimized out. */
17159 if (padsize)
17161 if (padsize > decl_size)
17163 remove_loc_list_addr_table_entries (cur_descr);
17164 goto discard_descr;
17166 decl_size -= padsize;
17167 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17168 if (*descr_tail == NULL)
17170 remove_loc_list_addr_table_entries (cur_descr);
17171 goto discard_descr;
17173 descr_tail = &(*descr_tail)->dw_loc_next;
17174 padsize = 0;
17176 *descr_tail = cur_descr;
17177 descr_tail = tail;
17178 if (bitsize > decl_size)
17179 goto discard_descr;
17180 decl_size -= bitsize;
17181 if (last == NULL)
17183 HOST_WIDE_INT offset = 0;
17184 if (GET_CODE (varloc) == VAR_LOCATION
17185 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17187 varloc = PAT_VAR_LOCATION_LOC (varloc);
17188 if (GET_CODE (varloc) == EXPR_LIST)
17189 varloc = XEXP (varloc, 0);
17193 if (GET_CODE (varloc) == CONST
17194 || GET_CODE (varloc) == SIGN_EXTEND
17195 || GET_CODE (varloc) == ZERO_EXTEND)
17196 varloc = XEXP (varloc, 0);
17197 else if (GET_CODE (varloc) == SUBREG)
17198 varloc = SUBREG_REG (varloc);
17199 else
17200 break;
17202 while (1);
17203 /* DW_OP_bit_size offset should be zero for register
17204 or implicit location descriptions and empty location
17205 descriptions, but for memory addresses needs big endian
17206 adjustment. */
17207 if (MEM_P (varloc))
17209 unsigned HOST_WIDE_INT memsize;
17210 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17211 goto discard_descr;
17212 memsize *= BITS_PER_UNIT;
17213 if (memsize != bitsize)
17215 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17216 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17217 goto discard_descr;
17218 if (memsize < bitsize)
17219 goto discard_descr;
17220 if (BITS_BIG_ENDIAN)
17221 offset = memsize - bitsize;
17225 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17226 if (*descr_tail == NULL)
17227 goto discard_descr;
17228 descr_tail = &(*descr_tail)->dw_loc_next;
17232 /* If there were any non-empty expressions, add padding till the end of
17233 the decl. */
17234 if (descr != NULL && decl_size != 0)
17236 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17237 if (*descr_tail == NULL)
17238 goto discard_descr;
17240 return descr;
17242 discard_descr:
17243 /* Discard the descriptor and release any addr_table entries it uses. */
17244 remove_loc_list_addr_table_entries (descr);
17245 return NULL;
17248 /* Return the dwarf representation of the location list LOC_LIST of
17249 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17250 function. */
17252 static dw_loc_list_ref
17253 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17255 const char *endname, *secname;
17256 var_loc_view endview;
17257 rtx varloc;
17258 enum var_init_status initialized;
17259 struct var_loc_node *node;
17260 dw_loc_descr_ref descr;
17261 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17262 dw_loc_list_ref list = NULL;
17263 dw_loc_list_ref *listp = &list;
17265 /* Now that we know what section we are using for a base,
17266 actually construct the list of locations.
17267 The first location information is what is passed to the
17268 function that creates the location list, and the remaining
17269 locations just get added on to that list.
17270 Note that we only know the start address for a location
17271 (IE location changes), so to build the range, we use
17272 the range [current location start, next location start].
17273 This means we have to special case the last node, and generate
17274 a range of [last location start, end of function label]. */
17276 if (cfun && crtl->has_bb_partition)
17278 bool save_in_cold_section_p = in_cold_section_p;
17279 in_cold_section_p = first_function_block_is_cold;
17280 if (loc_list->last_before_switch == NULL)
17281 in_cold_section_p = !in_cold_section_p;
17282 secname = secname_for_decl (decl);
17283 in_cold_section_p = save_in_cold_section_p;
17285 else
17286 secname = secname_for_decl (decl);
17288 for (node = loc_list->first; node; node = node->next)
17290 bool range_across_switch = false;
17291 if (GET_CODE (node->loc) == EXPR_LIST
17292 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17294 if (GET_CODE (node->loc) == EXPR_LIST)
17296 descr = NULL;
17297 /* This requires DW_OP_{,bit_}piece, which is not usable
17298 inside DWARF expressions. */
17299 if (want_address == 2)
17300 descr = dw_sra_loc_expr (decl, node->loc);
17302 else
17304 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17305 varloc = NOTE_VAR_LOCATION (node->loc);
17306 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17308 if (descr)
17310 /* If section switch happens in between node->label
17311 and node->next->label (or end of function) and
17312 we can't emit it as a single entry list,
17313 emit two ranges, first one ending at the end
17314 of first partition and second one starting at the
17315 beginning of second partition. */
17316 if (node == loc_list->last_before_switch
17317 && (node != loc_list->first || loc_list->first->next
17318 /* If we are to emit a view number, we will emit
17319 a loclist rather than a single location
17320 expression for the entire function (see
17321 loc_list_has_views), so we have to split the
17322 range that straddles across partitions. */
17323 || !ZERO_VIEW_P (node->view))
17324 && current_function_decl)
17326 endname = cfun->fde->dw_fde_end;
17327 endview = 0;
17328 range_across_switch = true;
17330 /* The variable has a location between NODE->LABEL and
17331 NODE->NEXT->LABEL. */
17332 else if (node->next)
17333 endname = node->next->label, endview = node->next->view;
17334 /* If the variable has a location at the last label
17335 it keeps its location until the end of function. */
17336 else if (!current_function_decl)
17337 endname = text_end_label, endview = 0;
17338 else
17340 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17341 current_function_funcdef_no);
17342 endname = ggc_strdup (label_id);
17343 endview = 0;
17346 *listp = new_loc_list (descr, node->label, node->view,
17347 endname, endview, secname);
17348 if (TREE_CODE (decl) == PARM_DECL
17349 && node == loc_list->first
17350 && NOTE_P (node->loc)
17351 && strcmp (node->label, endname) == 0)
17352 (*listp)->force = true;
17353 listp = &(*listp)->dw_loc_next;
17357 if (cfun
17358 && crtl->has_bb_partition
17359 && node == loc_list->last_before_switch)
17361 bool save_in_cold_section_p = in_cold_section_p;
17362 in_cold_section_p = !first_function_block_is_cold;
17363 secname = secname_for_decl (decl);
17364 in_cold_section_p = save_in_cold_section_p;
17367 if (range_across_switch)
17369 if (GET_CODE (node->loc) == EXPR_LIST)
17370 descr = dw_sra_loc_expr (decl, node->loc);
17371 else
17373 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17374 varloc = NOTE_VAR_LOCATION (node->loc);
17375 descr = dw_loc_list_1 (decl, varloc, want_address,
17376 initialized);
17378 gcc_assert (descr);
17379 /* The variable has a location between NODE->LABEL and
17380 NODE->NEXT->LABEL. */
17381 if (node->next)
17382 endname = node->next->label, endview = node->next->view;
17383 else
17384 endname = cfun->fde->dw_fde_second_end, endview = 0;
17385 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17386 endname, endview, secname);
17387 listp = &(*listp)->dw_loc_next;
17391 /* Try to avoid the overhead of a location list emitting a location
17392 expression instead, but only if we didn't have more than one
17393 location entry in the first place. If some entries were not
17394 representable, we don't want to pretend a single entry that was
17395 applies to the entire scope in which the variable is
17396 available. */
17397 if (list && loc_list->first->next)
17398 gen_llsym (list);
17399 else
17400 maybe_gen_llsym (list);
17402 return list;
17405 /* Return if the loc_list has only single element and thus can be represented
17406 as location description. */
17408 static bool
17409 single_element_loc_list_p (dw_loc_list_ref list)
17411 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17412 return !list->ll_symbol;
17415 /* Duplicate a single element of location list. */
17417 static inline dw_loc_descr_ref
17418 copy_loc_descr (dw_loc_descr_ref ref)
17420 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17421 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17422 return copy;
17425 /* To each location in list LIST append loc descr REF. */
17427 static void
17428 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17430 dw_loc_descr_ref copy;
17431 add_loc_descr (&list->expr, ref);
17432 list = list->dw_loc_next;
17433 while (list)
17435 copy = copy_loc_descr (ref);
17436 add_loc_descr (&list->expr, copy);
17437 while (copy->dw_loc_next)
17438 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17439 list = list->dw_loc_next;
17443 /* To each location in list LIST prepend loc descr REF. */
17445 static void
17446 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17448 dw_loc_descr_ref copy;
17449 dw_loc_descr_ref ref_end = list->expr;
17450 add_loc_descr (&ref, list->expr);
17451 list->expr = ref;
17452 list = list->dw_loc_next;
17453 while (list)
17455 dw_loc_descr_ref end = list->expr;
17456 list->expr = copy = copy_loc_descr (ref);
17457 while (copy->dw_loc_next != ref_end)
17458 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17459 copy->dw_loc_next = end;
17460 list = list->dw_loc_next;
17464 /* Given two lists RET and LIST
17465 produce location list that is result of adding expression in LIST
17466 to expression in RET on each position in program.
17467 Might be destructive on both RET and LIST.
17469 TODO: We handle only simple cases of RET or LIST having at most one
17470 element. General case would involve sorting the lists in program order
17471 and merging them that will need some additional work.
17472 Adding that will improve quality of debug info especially for SRA-ed
17473 structures. */
17475 static void
17476 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17478 if (!list)
17479 return;
17480 if (!*ret)
17482 *ret = list;
17483 return;
17485 if (!list->dw_loc_next)
17487 add_loc_descr_to_each (*ret, list->expr);
17488 return;
17490 if (!(*ret)->dw_loc_next)
17492 prepend_loc_descr_to_each (list, (*ret)->expr);
17493 *ret = list;
17494 return;
17496 expansion_failed (NULL_TREE, NULL_RTX,
17497 "Don't know how to merge two non-trivial"
17498 " location lists.\n");
17499 *ret = NULL;
17500 return;
17503 /* LOC is constant expression. Try a luck, look it up in constant
17504 pool and return its loc_descr of its address. */
17506 static dw_loc_descr_ref
17507 cst_pool_loc_descr (tree loc)
17509 /* Get an RTL for this, if something has been emitted. */
17510 rtx rtl = lookup_constant_def (loc);
17512 if (!rtl || !MEM_P (rtl))
17514 gcc_assert (!rtl);
17515 return 0;
17517 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17519 /* TODO: We might get more coverage if we was actually delaying expansion
17520 of all expressions till end of compilation when constant pools are fully
17521 populated. */
17522 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17524 expansion_failed (loc, NULL_RTX,
17525 "CST value in contant pool but not marked.");
17526 return 0;
17528 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17529 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17532 /* Return dw_loc_list representing address of addr_expr LOC
17533 by looking for inner INDIRECT_REF expression and turning
17534 it into simple arithmetics.
17536 See loc_list_from_tree for the meaning of CONTEXT. */
17538 static dw_loc_list_ref
17539 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17540 loc_descr_context *context)
17542 tree obj, offset;
17543 poly_int64 bitsize, bitpos, bytepos;
17544 machine_mode mode;
17545 int unsignedp, reversep, volatilep = 0;
17546 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17548 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17549 &bitsize, &bitpos, &offset, &mode,
17550 &unsignedp, &reversep, &volatilep);
17551 STRIP_NOPS (obj);
17552 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17554 expansion_failed (loc, NULL_RTX, "bitfield access");
17555 return 0;
17557 if (!INDIRECT_REF_P (obj))
17559 expansion_failed (obj,
17560 NULL_RTX, "no indirect ref in inner refrence");
17561 return 0;
17563 if (!offset && known_eq (bitpos, 0))
17564 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17565 context);
17566 else if (toplev
17567 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17568 && (dwarf_version >= 4 || !dwarf_strict))
17570 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17571 if (!list_ret)
17572 return 0;
17573 if (offset)
17575 /* Variable offset. */
17576 list_ret1 = loc_list_from_tree (offset, 0, context);
17577 if (list_ret1 == 0)
17578 return 0;
17579 add_loc_list (&list_ret, list_ret1);
17580 if (!list_ret)
17581 return 0;
17582 add_loc_descr_to_each (list_ret,
17583 new_loc_descr (DW_OP_plus, 0, 0));
17585 HOST_WIDE_INT value;
17586 if (bytepos.is_constant (&value) && value > 0)
17587 add_loc_descr_to_each (list_ret,
17588 new_loc_descr (DW_OP_plus_uconst, value, 0));
17589 else if (maybe_ne (bytepos, 0))
17590 loc_list_plus_const (list_ret, bytepos);
17591 add_loc_descr_to_each (list_ret,
17592 new_loc_descr (DW_OP_stack_value, 0, 0));
17594 return list_ret;
17597 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17598 all operations from LOC are nops, move to the last one. Insert in NOPS all
17599 operations that are skipped. */
17601 static void
17602 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17603 hash_set<dw_loc_descr_ref> &nops)
17605 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17607 nops.add (loc);
17608 loc = loc->dw_loc_next;
17612 /* Helper for loc_descr_without_nops: free the location description operation
17613 P. */
17615 bool
17616 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17618 ggc_free (loc);
17619 return true;
17622 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17623 finishes LOC. */
17625 static void
17626 loc_descr_without_nops (dw_loc_descr_ref &loc)
17628 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17629 return;
17631 /* Set of all DW_OP_nop operations we remove. */
17632 hash_set<dw_loc_descr_ref> nops;
17634 /* First, strip all prefix NOP operations in order to keep the head of the
17635 operations list. */
17636 loc_descr_to_next_no_nop (loc, nops);
17638 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17640 /* For control flow operations: strip "prefix" nops in destination
17641 labels. */
17642 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17643 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17644 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17645 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17647 /* Do the same for the operations that follow, then move to the next
17648 iteration. */
17649 if (cur->dw_loc_next != NULL)
17650 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17651 cur = cur->dw_loc_next;
17654 nops.traverse<void *, free_loc_descr> (NULL);
17658 struct dwarf_procedure_info;
17660 /* Helper structure for location descriptions generation. */
17661 struct loc_descr_context
17663 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17664 NULL_TREE if DW_OP_push_object_address in invalid for this location
17665 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17666 tree context_type;
17667 /* The ..._DECL node that should be translated as a
17668 DW_OP_push_object_address operation. */
17669 tree base_decl;
17670 /* Information about the DWARF procedure we are currently generating. NULL if
17671 we are not generating a DWARF procedure. */
17672 struct dwarf_procedure_info *dpi;
17673 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17674 by consumer. Used for DW_TAG_generic_subrange attributes. */
17675 bool placeholder_arg;
17676 /* True if PLACEHOLDER_EXPR has been seen. */
17677 bool placeholder_seen;
17680 /* DWARF procedures generation
17682 DWARF expressions (aka. location descriptions) are used to encode variable
17683 things such as sizes or offsets. Such computations can have redundant parts
17684 that can be factorized in order to reduce the size of the output debug
17685 information. This is the whole point of DWARF procedures.
17687 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
17688 already factorized into functions ("size functions") in order to handle very
17689 big and complex types. Such functions are quite simple: they have integral
17690 arguments, they return an integral result and their body contains only a
17691 return statement with arithmetic expressions. This is the only kind of
17692 function we are interested in translating into DWARF procedures, here.
17694 DWARF expressions and DWARF procedure are executed using a stack, so we have
17695 to define some calling convention for them to interact. Let's say that:
17697 - Before calling a DWARF procedure, DWARF expressions must push on the stack
17698 all arguments in reverse order (right-to-left) so that when the DWARF
17699 procedure execution starts, the first argument is the top of the stack.
17701 - Then, when returning, the DWARF procedure must have consumed all arguments
17702 on the stack, must have pushed the result and touched nothing else.
17704 - Each integral argument and the result are integral types can be hold in a
17705 single stack slot.
17707 - We call "frame offset" the number of stack slots that are "under DWARF
17708 procedure control": it includes the arguments slots, the temporaries and
17709 the result slot. Thus, it is equal to the number of arguments when the
17710 procedure execution starts and must be equal to one (the result) when it
17711 returns. */
17713 /* Helper structure used when generating operations for a DWARF procedure. */
17714 struct dwarf_procedure_info
17716 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
17717 currently translated. */
17718 tree fndecl;
17719 /* The number of arguments FNDECL takes. */
17720 unsigned args_count;
17723 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
17724 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
17725 equate it to this DIE. */
17727 static dw_die_ref
17728 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
17729 dw_die_ref parent_die)
17731 dw_die_ref dwarf_proc_die;
17733 if ((dwarf_version < 3 && dwarf_strict)
17734 || location == NULL)
17735 return NULL;
17737 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
17738 if (fndecl)
17739 equate_decl_number_to_die (fndecl, dwarf_proc_die);
17740 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
17741 return dwarf_proc_die;
17744 /* Return whether TYPE is a supported type as a DWARF procedure argument
17745 type or return type (we handle only scalar types and pointer types that
17746 aren't wider than the DWARF expression evaluation stack. */
17748 static bool
17749 is_handled_procedure_type (tree type)
17751 return ((INTEGRAL_TYPE_P (type)
17752 || TREE_CODE (type) == OFFSET_TYPE
17753 || TREE_CODE (type) == POINTER_TYPE)
17754 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
17757 /* Helper for resolve_args_picking: do the same but stop when coming across
17758 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
17759 offset *before* evaluating the corresponding operation. */
17761 static bool
17762 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17763 struct dwarf_procedure_info *dpi,
17764 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
17766 /* The "frame_offset" identifier is already used to name a macro... */
17767 unsigned frame_offset_ = initial_frame_offset;
17768 dw_loc_descr_ref l;
17770 for (l = loc; l != NULL;)
17772 bool existed;
17773 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
17775 /* If we already met this node, there is nothing to compute anymore. */
17776 if (existed)
17778 /* Make sure that the stack size is consistent wherever the execution
17779 flow comes from. */
17780 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
17781 break;
17783 l_frame_offset = frame_offset_;
17785 /* If needed, relocate the picking offset with respect to the frame
17786 offset. */
17787 if (l->frame_offset_rel)
17789 unsigned HOST_WIDE_INT off;
17790 switch (l->dw_loc_opc)
17792 case DW_OP_pick:
17793 off = l->dw_loc_oprnd1.v.val_unsigned;
17794 break;
17795 case DW_OP_dup:
17796 off = 0;
17797 break;
17798 case DW_OP_over:
17799 off = 1;
17800 break;
17801 default:
17802 gcc_unreachable ();
17804 /* frame_offset_ is the size of the current stack frame, including
17805 incoming arguments. Besides, the arguments are pushed
17806 right-to-left. Thus, in order to access the Nth argument from
17807 this operation node, the picking has to skip temporaries *plus*
17808 one stack slot per argument (0 for the first one, 1 for the second
17809 one, etc.).
17811 The targetted argument number (N) is already set as the operand,
17812 and the number of temporaries can be computed with:
17813 frame_offsets_ - dpi->args_count */
17814 off += frame_offset_ - dpi->args_count;
17816 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
17817 if (off > 255)
17818 return false;
17820 if (off == 0)
17822 l->dw_loc_opc = DW_OP_dup;
17823 l->dw_loc_oprnd1.v.val_unsigned = 0;
17825 else if (off == 1)
17827 l->dw_loc_opc = DW_OP_over;
17828 l->dw_loc_oprnd1.v.val_unsigned = 0;
17830 else
17832 l->dw_loc_opc = DW_OP_pick;
17833 l->dw_loc_oprnd1.v.val_unsigned = off;
17837 /* Update frame_offset according to the effect the current operation has
17838 on the stack. */
17839 switch (l->dw_loc_opc)
17841 case DW_OP_deref:
17842 case DW_OP_swap:
17843 case DW_OP_rot:
17844 case DW_OP_abs:
17845 case DW_OP_neg:
17846 case DW_OP_not:
17847 case DW_OP_plus_uconst:
17848 case DW_OP_skip:
17849 case DW_OP_reg0:
17850 case DW_OP_reg1:
17851 case DW_OP_reg2:
17852 case DW_OP_reg3:
17853 case DW_OP_reg4:
17854 case DW_OP_reg5:
17855 case DW_OP_reg6:
17856 case DW_OP_reg7:
17857 case DW_OP_reg8:
17858 case DW_OP_reg9:
17859 case DW_OP_reg10:
17860 case DW_OP_reg11:
17861 case DW_OP_reg12:
17862 case DW_OP_reg13:
17863 case DW_OP_reg14:
17864 case DW_OP_reg15:
17865 case DW_OP_reg16:
17866 case DW_OP_reg17:
17867 case DW_OP_reg18:
17868 case DW_OP_reg19:
17869 case DW_OP_reg20:
17870 case DW_OP_reg21:
17871 case DW_OP_reg22:
17872 case DW_OP_reg23:
17873 case DW_OP_reg24:
17874 case DW_OP_reg25:
17875 case DW_OP_reg26:
17876 case DW_OP_reg27:
17877 case DW_OP_reg28:
17878 case DW_OP_reg29:
17879 case DW_OP_reg30:
17880 case DW_OP_reg31:
17881 case DW_OP_bregx:
17882 case DW_OP_piece:
17883 case DW_OP_deref_size:
17884 case DW_OP_nop:
17885 case DW_OP_bit_piece:
17886 case DW_OP_implicit_value:
17887 case DW_OP_stack_value:
17888 break;
17890 case DW_OP_addr:
17891 case DW_OP_const1u:
17892 case DW_OP_const1s:
17893 case DW_OP_const2u:
17894 case DW_OP_const2s:
17895 case DW_OP_const4u:
17896 case DW_OP_const4s:
17897 case DW_OP_const8u:
17898 case DW_OP_const8s:
17899 case DW_OP_constu:
17900 case DW_OP_consts:
17901 case DW_OP_dup:
17902 case DW_OP_over:
17903 case DW_OP_pick:
17904 case DW_OP_lit0:
17905 case DW_OP_lit1:
17906 case DW_OP_lit2:
17907 case DW_OP_lit3:
17908 case DW_OP_lit4:
17909 case DW_OP_lit5:
17910 case DW_OP_lit6:
17911 case DW_OP_lit7:
17912 case DW_OP_lit8:
17913 case DW_OP_lit9:
17914 case DW_OP_lit10:
17915 case DW_OP_lit11:
17916 case DW_OP_lit12:
17917 case DW_OP_lit13:
17918 case DW_OP_lit14:
17919 case DW_OP_lit15:
17920 case DW_OP_lit16:
17921 case DW_OP_lit17:
17922 case DW_OP_lit18:
17923 case DW_OP_lit19:
17924 case DW_OP_lit20:
17925 case DW_OP_lit21:
17926 case DW_OP_lit22:
17927 case DW_OP_lit23:
17928 case DW_OP_lit24:
17929 case DW_OP_lit25:
17930 case DW_OP_lit26:
17931 case DW_OP_lit27:
17932 case DW_OP_lit28:
17933 case DW_OP_lit29:
17934 case DW_OP_lit30:
17935 case DW_OP_lit31:
17936 case DW_OP_breg0:
17937 case DW_OP_breg1:
17938 case DW_OP_breg2:
17939 case DW_OP_breg3:
17940 case DW_OP_breg4:
17941 case DW_OP_breg5:
17942 case DW_OP_breg6:
17943 case DW_OP_breg7:
17944 case DW_OP_breg8:
17945 case DW_OP_breg9:
17946 case DW_OP_breg10:
17947 case DW_OP_breg11:
17948 case DW_OP_breg12:
17949 case DW_OP_breg13:
17950 case DW_OP_breg14:
17951 case DW_OP_breg15:
17952 case DW_OP_breg16:
17953 case DW_OP_breg17:
17954 case DW_OP_breg18:
17955 case DW_OP_breg19:
17956 case DW_OP_breg20:
17957 case DW_OP_breg21:
17958 case DW_OP_breg22:
17959 case DW_OP_breg23:
17960 case DW_OP_breg24:
17961 case DW_OP_breg25:
17962 case DW_OP_breg26:
17963 case DW_OP_breg27:
17964 case DW_OP_breg28:
17965 case DW_OP_breg29:
17966 case DW_OP_breg30:
17967 case DW_OP_breg31:
17968 case DW_OP_fbreg:
17969 case DW_OP_push_object_address:
17970 case DW_OP_call_frame_cfa:
17971 case DW_OP_GNU_variable_value:
17972 case DW_OP_GNU_addr_index:
17973 case DW_OP_GNU_const_index:
17974 ++frame_offset_;
17975 break;
17977 case DW_OP_drop:
17978 case DW_OP_xderef:
17979 case DW_OP_and:
17980 case DW_OP_div:
17981 case DW_OP_minus:
17982 case DW_OP_mod:
17983 case DW_OP_mul:
17984 case DW_OP_or:
17985 case DW_OP_plus:
17986 case DW_OP_shl:
17987 case DW_OP_shr:
17988 case DW_OP_shra:
17989 case DW_OP_xor:
17990 case DW_OP_bra:
17991 case DW_OP_eq:
17992 case DW_OP_ge:
17993 case DW_OP_gt:
17994 case DW_OP_le:
17995 case DW_OP_lt:
17996 case DW_OP_ne:
17997 case DW_OP_regx:
17998 case DW_OP_xderef_size:
17999 --frame_offset_;
18000 break;
18002 case DW_OP_call2:
18003 case DW_OP_call4:
18004 case DW_OP_call_ref:
18006 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18007 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18009 if (stack_usage == NULL)
18010 return false;
18011 frame_offset_ += *stack_usage;
18012 break;
18015 case DW_OP_implicit_pointer:
18016 case DW_OP_entry_value:
18017 case DW_OP_const_type:
18018 case DW_OP_regval_type:
18019 case DW_OP_deref_type:
18020 case DW_OP_convert:
18021 case DW_OP_reinterpret:
18022 case DW_OP_form_tls_address:
18023 case DW_OP_GNU_push_tls_address:
18024 case DW_OP_GNU_uninit:
18025 case DW_OP_GNU_encoded_addr:
18026 case DW_OP_GNU_implicit_pointer:
18027 case DW_OP_GNU_entry_value:
18028 case DW_OP_GNU_const_type:
18029 case DW_OP_GNU_regval_type:
18030 case DW_OP_GNU_deref_type:
18031 case DW_OP_GNU_convert:
18032 case DW_OP_GNU_reinterpret:
18033 case DW_OP_GNU_parameter_ref:
18034 /* loc_list_from_tree will probably not output these operations for
18035 size functions, so assume they will not appear here. */
18036 /* Fall through... */
18038 default:
18039 gcc_unreachable ();
18042 /* Now, follow the control flow (except subroutine calls). */
18043 switch (l->dw_loc_opc)
18045 case DW_OP_bra:
18046 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18047 frame_offsets))
18048 return false;
18049 /* Fall through. */
18051 case DW_OP_skip:
18052 l = l->dw_loc_oprnd1.v.val_loc;
18053 break;
18055 case DW_OP_stack_value:
18056 return true;
18058 default:
18059 l = l->dw_loc_next;
18060 break;
18064 return true;
18067 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18068 operations) in order to resolve the operand of DW_OP_pick operations that
18069 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18070 offset *before* LOC is executed. Return if all relocations were
18071 successful. */
18073 static bool
18074 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18075 struct dwarf_procedure_info *dpi)
18077 /* Associate to all visited operations the frame offset *before* evaluating
18078 this operation. */
18079 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18081 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
18082 frame_offsets);
18085 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18086 Return NULL if it is not possible. */
18088 static dw_die_ref
18089 function_to_dwarf_procedure (tree fndecl)
18091 struct loc_descr_context ctx;
18092 struct dwarf_procedure_info dpi;
18093 dw_die_ref dwarf_proc_die;
18094 tree tree_body = DECL_SAVED_TREE (fndecl);
18095 dw_loc_descr_ref loc_body, epilogue;
18097 tree cursor;
18098 unsigned i;
18100 /* Do not generate multiple DWARF procedures for the same function
18101 declaration. */
18102 dwarf_proc_die = lookup_decl_die (fndecl);
18103 if (dwarf_proc_die != NULL)
18104 return dwarf_proc_die;
18106 /* DWARF procedures are available starting with the DWARFv3 standard. */
18107 if (dwarf_version < 3 && dwarf_strict)
18108 return NULL;
18110 /* We handle only functions for which we still have a body, that return a
18111 supported type and that takes arguments with supported types. Note that
18112 there is no point translating functions that return nothing. */
18113 if (tree_body == NULL_TREE
18114 || DECL_RESULT (fndecl) == NULL_TREE
18115 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18116 return NULL;
18118 for (cursor = DECL_ARGUMENTS (fndecl);
18119 cursor != NULL_TREE;
18120 cursor = TREE_CHAIN (cursor))
18121 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18122 return NULL;
18124 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18125 if (TREE_CODE (tree_body) != RETURN_EXPR)
18126 return NULL;
18127 tree_body = TREE_OPERAND (tree_body, 0);
18128 if (TREE_CODE (tree_body) != MODIFY_EXPR
18129 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18130 return NULL;
18131 tree_body = TREE_OPERAND (tree_body, 1);
18133 /* Try to translate the body expression itself. Note that this will probably
18134 cause an infinite recursion if its call graph has a cycle. This is very
18135 unlikely for size functions, however, so don't bother with such things at
18136 the moment. */
18137 ctx.context_type = NULL_TREE;
18138 ctx.base_decl = NULL_TREE;
18139 ctx.dpi = &dpi;
18140 ctx.placeholder_arg = false;
18141 ctx.placeholder_seen = false;
18142 dpi.fndecl = fndecl;
18143 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18144 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18145 if (!loc_body)
18146 return NULL;
18148 /* After evaluating all operands in "loc_body", we should still have on the
18149 stack all arguments plus the desired function result (top of the stack).
18150 Generate code in order to keep only the result in our stack frame. */
18151 epilogue = NULL;
18152 for (i = 0; i < dpi.args_count; ++i)
18154 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18155 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18156 op_couple->dw_loc_next->dw_loc_next = epilogue;
18157 epilogue = op_couple;
18159 add_loc_descr (&loc_body, epilogue);
18160 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18161 return NULL;
18163 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18164 because they are considered useful. Now there is an epilogue, they are
18165 not anymore, so give it another try. */
18166 loc_descr_without_nops (loc_body);
18168 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18169 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18170 though, given that size functions do not come from source, so they should
18171 not have a dedicated DW_TAG_subprogram DIE. */
18172 dwarf_proc_die
18173 = new_dwarf_proc_die (loc_body, fndecl,
18174 get_context_die (DECL_CONTEXT (fndecl)));
18176 /* The called DWARF procedure consumes one stack slot per argument and
18177 returns one stack slot. */
18178 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18180 return dwarf_proc_die;
18184 /* Generate Dwarf location list representing LOC.
18185 If WANT_ADDRESS is false, expression computing LOC will be computed
18186 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18187 if WANT_ADDRESS is 2, expression computing address useable in location
18188 will be returned (i.e. DW_OP_reg can be used
18189 to refer to register values).
18191 CONTEXT provides information to customize the location descriptions
18192 generation. Its context_type field specifies what type is implicitly
18193 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18194 will not be generated.
18196 Its DPI field determines whether we are generating a DWARF expression for a
18197 DWARF procedure, so PARM_DECL references are processed specifically.
18199 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18200 and dpi fields were null. */
18202 static dw_loc_list_ref
18203 loc_list_from_tree_1 (tree loc, int want_address,
18204 struct loc_descr_context *context)
18206 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18207 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18208 int have_address = 0;
18209 enum dwarf_location_atom op;
18211 /* ??? Most of the time we do not take proper care for sign/zero
18212 extending the values properly. Hopefully this won't be a real
18213 problem... */
18215 if (context != NULL
18216 && context->base_decl == loc
18217 && want_address == 0)
18219 if (dwarf_version >= 3 || !dwarf_strict)
18220 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18221 NULL, 0, NULL, 0, NULL);
18222 else
18223 return NULL;
18226 switch (TREE_CODE (loc))
18228 case ERROR_MARK:
18229 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18230 return 0;
18232 case PLACEHOLDER_EXPR:
18233 /* This case involves extracting fields from an object to determine the
18234 position of other fields. It is supposed to appear only as the first
18235 operand of COMPONENT_REF nodes and to reference precisely the type
18236 that the context allows. */
18237 if (context != NULL
18238 && TREE_TYPE (loc) == context->context_type
18239 && want_address >= 1)
18241 if (dwarf_version >= 3 || !dwarf_strict)
18243 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18244 have_address = 1;
18245 break;
18247 else
18248 return NULL;
18250 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18251 the single argument passed by consumer. */
18252 else if (context != NULL
18253 && context->placeholder_arg
18254 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18255 && want_address == 0)
18257 ret = new_loc_descr (DW_OP_pick, 0, 0);
18258 ret->frame_offset_rel = 1;
18259 context->placeholder_seen = true;
18260 break;
18262 else
18263 expansion_failed (loc, NULL_RTX,
18264 "PLACEHOLDER_EXPR for an unexpected type");
18265 break;
18267 case CALL_EXPR:
18269 const int nargs = call_expr_nargs (loc);
18270 tree callee = get_callee_fndecl (loc);
18271 int i;
18272 dw_die_ref dwarf_proc;
18274 if (callee == NULL_TREE)
18275 goto call_expansion_failed;
18277 /* We handle only functions that return an integer. */
18278 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
18279 goto call_expansion_failed;
18281 dwarf_proc = function_to_dwarf_procedure (callee);
18282 if (dwarf_proc == NULL)
18283 goto call_expansion_failed;
18285 /* Evaluate arguments right-to-left so that the first argument will
18286 be the top-most one on the stack. */
18287 for (i = nargs - 1; i >= 0; --i)
18289 dw_loc_descr_ref loc_descr
18290 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
18291 context);
18293 if (loc_descr == NULL)
18294 goto call_expansion_failed;
18296 add_loc_descr (&ret, loc_descr);
18299 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18300 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18301 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18302 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18303 add_loc_descr (&ret, ret1);
18304 break;
18306 call_expansion_failed:
18307 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
18308 /* There are no opcodes for these operations. */
18309 return 0;
18312 case PREINCREMENT_EXPR:
18313 case PREDECREMENT_EXPR:
18314 case POSTINCREMENT_EXPR:
18315 case POSTDECREMENT_EXPR:
18316 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18317 /* There are no opcodes for these operations. */
18318 return 0;
18320 case ADDR_EXPR:
18321 /* If we already want an address, see if there is INDIRECT_REF inside
18322 e.g. for &this->field. */
18323 if (want_address)
18325 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18326 (loc, want_address == 2, context);
18327 if (list_ret)
18328 have_address = 1;
18329 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18330 && (ret = cst_pool_loc_descr (loc)))
18331 have_address = 1;
18333 /* Otherwise, process the argument and look for the address. */
18334 if (!list_ret && !ret)
18335 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18336 else
18338 if (want_address)
18339 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18340 return NULL;
18342 break;
18344 case VAR_DECL:
18345 if (DECL_THREAD_LOCAL_P (loc))
18347 rtx rtl;
18348 enum dwarf_location_atom tls_op;
18349 enum dtprel_bool dtprel = dtprel_false;
18351 if (targetm.have_tls)
18353 /* If this is not defined, we have no way to emit the
18354 data. */
18355 if (!targetm.asm_out.output_dwarf_dtprel)
18356 return 0;
18358 /* The way DW_OP_GNU_push_tls_address is specified, we
18359 can only look up addresses of objects in the current
18360 module. We used DW_OP_addr as first op, but that's
18361 wrong, because DW_OP_addr is relocated by the debug
18362 info consumer, while DW_OP_GNU_push_tls_address
18363 operand shouldn't be. */
18364 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18365 return 0;
18366 dtprel = dtprel_true;
18367 /* We check for DWARF 5 here because gdb did not implement
18368 DW_OP_form_tls_address until after 7.12. */
18369 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18370 : DW_OP_GNU_push_tls_address);
18372 else
18374 if (!targetm.emutls.debug_form_tls_address
18375 || !(dwarf_version >= 3 || !dwarf_strict))
18376 return 0;
18377 /* We stuffed the control variable into the DECL_VALUE_EXPR
18378 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18379 no longer appear in gimple code. We used the control
18380 variable in specific so that we could pick it up here. */
18381 loc = DECL_VALUE_EXPR (loc);
18382 tls_op = DW_OP_form_tls_address;
18385 rtl = rtl_for_decl_location (loc);
18386 if (rtl == NULL_RTX)
18387 return 0;
18389 if (!MEM_P (rtl))
18390 return 0;
18391 rtl = XEXP (rtl, 0);
18392 if (! CONSTANT_P (rtl))
18393 return 0;
18395 ret = new_addr_loc_descr (rtl, dtprel);
18396 ret1 = new_loc_descr (tls_op, 0, 0);
18397 add_loc_descr (&ret, ret1);
18399 have_address = 1;
18400 break;
18402 /* FALLTHRU */
18404 case PARM_DECL:
18405 if (context != NULL && context->dpi != NULL
18406 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18408 /* We are generating code for a DWARF procedure and we want to access
18409 one of its arguments: find the appropriate argument offset and let
18410 the resolve_args_picking pass compute the offset that complies
18411 with the stack frame size. */
18412 unsigned i = 0;
18413 tree cursor;
18415 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18416 cursor != NULL_TREE && cursor != loc;
18417 cursor = TREE_CHAIN (cursor), ++i)
18419 /* If we are translating a DWARF procedure, all referenced parameters
18420 must belong to the current function. */
18421 gcc_assert (cursor != NULL_TREE);
18423 ret = new_loc_descr (DW_OP_pick, i, 0);
18424 ret->frame_offset_rel = 1;
18425 break;
18427 /* FALLTHRU */
18429 case RESULT_DECL:
18430 if (DECL_HAS_VALUE_EXPR_P (loc))
18431 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
18432 want_address, context);
18433 /* FALLTHRU */
18435 case FUNCTION_DECL:
18437 rtx rtl;
18438 var_loc_list *loc_list = lookup_decl_loc (loc);
18440 if (loc_list && loc_list->first)
18442 list_ret = dw_loc_list (loc_list, loc, want_address);
18443 have_address = want_address != 0;
18444 break;
18446 rtl = rtl_for_decl_location (loc);
18447 if (rtl == NULL_RTX)
18449 if (TREE_CODE (loc) != FUNCTION_DECL
18450 && early_dwarf
18451 && current_function_decl
18452 && want_address != 1
18453 && ! DECL_IGNORED_P (loc)
18454 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18455 || POINTER_TYPE_P (TREE_TYPE (loc)))
18456 && DECL_CONTEXT (loc) == current_function_decl
18457 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18458 <= DWARF2_ADDR_SIZE))
18460 dw_die_ref ref = lookup_decl_die (loc);
18461 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18462 if (ref)
18464 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18465 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18466 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18468 else
18470 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18471 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18473 break;
18475 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18476 return 0;
18478 else if (CONST_INT_P (rtl))
18480 HOST_WIDE_INT val = INTVAL (rtl);
18481 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18482 val &= GET_MODE_MASK (DECL_MODE (loc));
18483 ret = int_loc_descriptor (val);
18485 else if (GET_CODE (rtl) == CONST_STRING)
18487 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18488 return 0;
18490 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18491 ret = new_addr_loc_descr (rtl, dtprel_false);
18492 else
18494 machine_mode mode, mem_mode;
18496 /* Certain constructs can only be represented at top-level. */
18497 if (want_address == 2)
18499 ret = loc_descriptor (rtl, VOIDmode,
18500 VAR_INIT_STATUS_INITIALIZED);
18501 have_address = 1;
18503 else
18505 mode = GET_MODE (rtl);
18506 mem_mode = VOIDmode;
18507 if (MEM_P (rtl))
18509 mem_mode = mode;
18510 mode = get_address_mode (rtl);
18511 rtl = XEXP (rtl, 0);
18512 have_address = 1;
18514 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18515 VAR_INIT_STATUS_INITIALIZED);
18517 if (!ret)
18518 expansion_failed (loc, rtl,
18519 "failed to produce loc descriptor for rtl");
18522 break;
18524 case MEM_REF:
18525 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18527 have_address = 1;
18528 goto do_plus;
18530 /* Fallthru. */
18531 case INDIRECT_REF:
18532 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18533 have_address = 1;
18534 break;
18536 case TARGET_MEM_REF:
18537 case SSA_NAME:
18538 case DEBUG_EXPR_DECL:
18539 return NULL;
18541 case COMPOUND_EXPR:
18542 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18543 context);
18545 CASE_CONVERT:
18546 case VIEW_CONVERT_EXPR:
18547 case SAVE_EXPR:
18548 case MODIFY_EXPR:
18549 case NON_LVALUE_EXPR:
18550 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18551 context);
18553 case COMPONENT_REF:
18554 case BIT_FIELD_REF:
18555 case ARRAY_REF:
18556 case ARRAY_RANGE_REF:
18557 case REALPART_EXPR:
18558 case IMAGPART_EXPR:
18560 tree obj, offset;
18561 poly_int64 bitsize, bitpos, bytepos;
18562 machine_mode mode;
18563 int unsignedp, reversep, volatilep = 0;
18565 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18566 &unsignedp, &reversep, &volatilep);
18568 gcc_assert (obj != loc);
18570 list_ret = loc_list_from_tree_1 (obj,
18571 want_address == 2
18572 && known_eq (bitpos, 0)
18573 && !offset ? 2 : 1,
18574 context);
18575 /* TODO: We can extract value of the small expression via shifting even
18576 for nonzero bitpos. */
18577 if (list_ret == 0)
18578 return 0;
18579 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18580 || !multiple_p (bitsize, BITS_PER_UNIT))
18582 expansion_failed (loc, NULL_RTX,
18583 "bitfield access");
18584 return 0;
18587 if (offset != NULL_TREE)
18589 /* Variable offset. */
18590 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18591 if (list_ret1 == 0)
18592 return 0;
18593 add_loc_list (&list_ret, list_ret1);
18594 if (!list_ret)
18595 return 0;
18596 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18599 HOST_WIDE_INT value;
18600 if (bytepos.is_constant (&value) && value > 0)
18601 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18602 value, 0));
18603 else if (maybe_ne (bytepos, 0))
18604 loc_list_plus_const (list_ret, bytepos);
18606 have_address = 1;
18607 break;
18610 case INTEGER_CST:
18611 if ((want_address || !tree_fits_shwi_p (loc))
18612 && (ret = cst_pool_loc_descr (loc)))
18613 have_address = 1;
18614 else if (want_address == 2
18615 && tree_fits_shwi_p (loc)
18616 && (ret = address_of_int_loc_descriptor
18617 (int_size_in_bytes (TREE_TYPE (loc)),
18618 tree_to_shwi (loc))))
18619 have_address = 1;
18620 else if (tree_fits_shwi_p (loc))
18621 ret = int_loc_descriptor (tree_to_shwi (loc));
18622 else if (tree_fits_uhwi_p (loc))
18623 ret = uint_loc_descriptor (tree_to_uhwi (loc));
18624 else
18626 expansion_failed (loc, NULL_RTX,
18627 "Integer operand is not host integer");
18628 return 0;
18630 break;
18632 case POLY_INT_CST:
18634 if (want_address)
18636 expansion_failed (loc, NULL_RTX,
18637 "constant address with a runtime component");
18638 return 0;
18640 poly_int64 value;
18641 if (!poly_int_tree_p (loc, &value))
18643 expansion_failed (loc, NULL_RTX, "constant too big");
18644 return 0;
18646 ret = int_loc_descriptor (value);
18648 break;
18650 case CONSTRUCTOR:
18651 case REAL_CST:
18652 case STRING_CST:
18653 case COMPLEX_CST:
18654 if ((ret = cst_pool_loc_descr (loc)))
18655 have_address = 1;
18656 else if (TREE_CODE (loc) == CONSTRUCTOR)
18658 tree type = TREE_TYPE (loc);
18659 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
18660 unsigned HOST_WIDE_INT offset = 0;
18661 unsigned HOST_WIDE_INT cnt;
18662 constructor_elt *ce;
18664 if (TREE_CODE (type) == RECORD_TYPE)
18666 /* This is very limited, but it's enough to output
18667 pointers to member functions, as long as the
18668 referenced function is defined in the current
18669 translation unit. */
18670 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
18672 tree val = ce->value;
18674 tree field = ce->index;
18676 if (val)
18677 STRIP_NOPS (val);
18679 if (!field || DECL_BIT_FIELD (field))
18681 expansion_failed (loc, NULL_RTX,
18682 "bitfield in record type constructor");
18683 size = offset = (unsigned HOST_WIDE_INT)-1;
18684 ret = NULL;
18685 break;
18688 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
18689 unsigned HOST_WIDE_INT pos = int_byte_position (field);
18690 gcc_assert (pos + fieldsize <= size);
18691 if (pos < offset)
18693 expansion_failed (loc, NULL_RTX,
18694 "out-of-order fields in record constructor");
18695 size = offset = (unsigned HOST_WIDE_INT)-1;
18696 ret = NULL;
18697 break;
18699 if (pos > offset)
18701 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
18702 add_loc_descr (&ret, ret1);
18703 offset = pos;
18705 if (val && fieldsize != 0)
18707 ret1 = loc_descriptor_from_tree (val, want_address, context);
18708 if (!ret1)
18710 expansion_failed (loc, NULL_RTX,
18711 "unsupported expression in field");
18712 size = offset = (unsigned HOST_WIDE_INT)-1;
18713 ret = NULL;
18714 break;
18716 add_loc_descr (&ret, ret1);
18718 if (fieldsize)
18720 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
18721 add_loc_descr (&ret, ret1);
18722 offset = pos + fieldsize;
18726 if (offset != size)
18728 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
18729 add_loc_descr (&ret, ret1);
18730 offset = size;
18733 have_address = !!want_address;
18735 else
18736 expansion_failed (loc, NULL_RTX,
18737 "constructor of non-record type");
18739 else
18740 /* We can construct small constants here using int_loc_descriptor. */
18741 expansion_failed (loc, NULL_RTX,
18742 "constructor or constant not in constant pool");
18743 break;
18745 case TRUTH_AND_EXPR:
18746 case TRUTH_ANDIF_EXPR:
18747 case BIT_AND_EXPR:
18748 op = DW_OP_and;
18749 goto do_binop;
18751 case TRUTH_XOR_EXPR:
18752 case BIT_XOR_EXPR:
18753 op = DW_OP_xor;
18754 goto do_binop;
18756 case TRUTH_OR_EXPR:
18757 case TRUTH_ORIF_EXPR:
18758 case BIT_IOR_EXPR:
18759 op = DW_OP_or;
18760 goto do_binop;
18762 case FLOOR_DIV_EXPR:
18763 case CEIL_DIV_EXPR:
18764 case ROUND_DIV_EXPR:
18765 case TRUNC_DIV_EXPR:
18766 case EXACT_DIV_EXPR:
18767 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18768 return 0;
18769 op = DW_OP_div;
18770 goto do_binop;
18772 case MINUS_EXPR:
18773 op = DW_OP_minus;
18774 goto do_binop;
18776 case FLOOR_MOD_EXPR:
18777 case CEIL_MOD_EXPR:
18778 case ROUND_MOD_EXPR:
18779 case TRUNC_MOD_EXPR:
18780 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18782 op = DW_OP_mod;
18783 goto do_binop;
18785 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18786 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18787 if (list_ret == 0 || list_ret1 == 0)
18788 return 0;
18790 add_loc_list (&list_ret, list_ret1);
18791 if (list_ret == 0)
18792 return 0;
18793 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 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_div, 0, 0));
18796 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
18797 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
18798 break;
18800 case MULT_EXPR:
18801 op = DW_OP_mul;
18802 goto do_binop;
18804 case LSHIFT_EXPR:
18805 op = DW_OP_shl;
18806 goto do_binop;
18808 case RSHIFT_EXPR:
18809 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
18810 goto do_binop;
18812 case POINTER_PLUS_EXPR:
18813 case PLUS_EXPR:
18814 do_plus:
18815 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
18817 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
18818 smarter to encode their opposite. The DW_OP_plus_uconst operation
18819 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
18820 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
18821 bytes, Y being the size of the operation that pushes the opposite
18822 of the addend. So let's choose the smallest representation. */
18823 const tree tree_addend = TREE_OPERAND (loc, 1);
18824 offset_int wi_addend;
18825 HOST_WIDE_INT shwi_addend;
18826 dw_loc_descr_ref loc_naddend;
18828 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18829 if (list_ret == 0)
18830 return 0;
18832 /* Try to get the literal to push. It is the opposite of the addend,
18833 so as we rely on wrapping during DWARF evaluation, first decode
18834 the literal as a "DWARF-sized" signed number. */
18835 wi_addend = wi::to_offset (tree_addend);
18836 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
18837 shwi_addend = wi_addend.to_shwi ();
18838 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
18839 ? int_loc_descriptor (-shwi_addend)
18840 : NULL;
18842 if (loc_naddend != NULL
18843 && ((unsigned) size_of_uleb128 (shwi_addend)
18844 > size_of_loc_descr (loc_naddend)))
18846 add_loc_descr_to_each (list_ret, loc_naddend);
18847 add_loc_descr_to_each (list_ret,
18848 new_loc_descr (DW_OP_minus, 0, 0));
18850 else
18852 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
18854 loc_naddend = loc_cur;
18855 loc_cur = loc_cur->dw_loc_next;
18856 ggc_free (loc_naddend);
18858 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
18860 break;
18863 op = DW_OP_plus;
18864 goto do_binop;
18866 case LE_EXPR:
18867 op = DW_OP_le;
18868 goto do_comp_binop;
18870 case GE_EXPR:
18871 op = DW_OP_ge;
18872 goto do_comp_binop;
18874 case LT_EXPR:
18875 op = DW_OP_lt;
18876 goto do_comp_binop;
18878 case GT_EXPR:
18879 op = DW_OP_gt;
18880 goto do_comp_binop;
18882 do_comp_binop:
18883 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18885 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18886 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18887 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18888 TREE_CODE (loc));
18889 break;
18891 else
18892 goto do_binop;
18894 case EQ_EXPR:
18895 op = DW_OP_eq;
18896 goto do_binop;
18898 case NE_EXPR:
18899 op = DW_OP_ne;
18900 goto do_binop;
18902 do_binop:
18903 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18904 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18905 if (list_ret == 0 || list_ret1 == 0)
18906 return 0;
18908 add_loc_list (&list_ret, list_ret1);
18909 if (list_ret == 0)
18910 return 0;
18911 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18912 break;
18914 case TRUTH_NOT_EXPR:
18915 case BIT_NOT_EXPR:
18916 op = DW_OP_not;
18917 goto do_unop;
18919 case ABS_EXPR:
18920 op = DW_OP_abs;
18921 goto do_unop;
18923 case NEGATE_EXPR:
18924 op = DW_OP_neg;
18925 goto do_unop;
18927 do_unop:
18928 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18929 if (list_ret == 0)
18930 return 0;
18932 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18933 break;
18935 case MIN_EXPR:
18936 case MAX_EXPR:
18938 const enum tree_code code =
18939 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18941 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18942 build2 (code, integer_type_node,
18943 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18944 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18947 /* fall through */
18949 case COND_EXPR:
18951 dw_loc_descr_ref lhs
18952 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18953 dw_loc_list_ref rhs
18954 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18955 dw_loc_descr_ref bra_node, jump_node, tmp;
18957 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18958 if (list_ret == 0 || lhs == 0 || rhs == 0)
18959 return 0;
18961 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18962 add_loc_descr_to_each (list_ret, bra_node);
18964 add_loc_list (&list_ret, rhs);
18965 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18966 add_loc_descr_to_each (list_ret, jump_node);
18968 add_loc_descr_to_each (list_ret, lhs);
18969 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18970 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18972 /* ??? Need a node to point the skip at. Use a nop. */
18973 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18974 add_loc_descr_to_each (list_ret, tmp);
18975 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18976 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18978 break;
18980 case FIX_TRUNC_EXPR:
18981 return 0;
18983 default:
18984 /* Leave front-end specific codes as simply unknown. This comes
18985 up, for instance, with the C STMT_EXPR. */
18986 if ((unsigned int) TREE_CODE (loc)
18987 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18989 expansion_failed (loc, NULL_RTX,
18990 "language specific tree node");
18991 return 0;
18994 /* Otherwise this is a generic code; we should just lists all of
18995 these explicitly. We forgot one. */
18996 if (flag_checking)
18997 gcc_unreachable ();
18999 /* In a release build, we want to degrade gracefully: better to
19000 generate incomplete debugging information than to crash. */
19001 return NULL;
19004 if (!ret && !list_ret)
19005 return 0;
19007 if (want_address == 2 && !have_address
19008 && (dwarf_version >= 4 || !dwarf_strict))
19010 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19012 expansion_failed (loc, NULL_RTX,
19013 "DWARF address size mismatch");
19014 return 0;
19016 if (ret)
19017 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19018 else
19019 add_loc_descr_to_each (list_ret,
19020 new_loc_descr (DW_OP_stack_value, 0, 0));
19021 have_address = 1;
19023 /* Show if we can't fill the request for an address. */
19024 if (want_address && !have_address)
19026 expansion_failed (loc, NULL_RTX,
19027 "Want address and only have value");
19028 return 0;
19031 gcc_assert (!ret || !list_ret);
19033 /* If we've got an address and don't want one, dereference. */
19034 if (!want_address && have_address)
19036 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19038 if (size > DWARF2_ADDR_SIZE || size == -1)
19040 expansion_failed (loc, NULL_RTX,
19041 "DWARF address size mismatch");
19042 return 0;
19044 else if (size == DWARF2_ADDR_SIZE)
19045 op = DW_OP_deref;
19046 else
19047 op = DW_OP_deref_size;
19049 if (ret)
19050 add_loc_descr (&ret, new_loc_descr (op, size, 0));
19051 else
19052 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
19054 if (ret)
19055 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19057 return list_ret;
19060 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19061 expressions. */
19063 static dw_loc_list_ref
19064 loc_list_from_tree (tree loc, int want_address,
19065 struct loc_descr_context *context)
19067 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19069 for (dw_loc_list_ref loc_cur = result;
19070 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19071 loc_descr_without_nops (loc_cur->expr);
19072 return result;
19075 /* Same as above but return only single location expression. */
19076 static dw_loc_descr_ref
19077 loc_descriptor_from_tree (tree loc, int want_address,
19078 struct loc_descr_context *context)
19080 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19081 if (!ret)
19082 return NULL;
19083 if (ret->dw_loc_next)
19085 expansion_failed (loc, NULL_RTX,
19086 "Location list where only loc descriptor needed");
19087 return NULL;
19089 return ret->expr;
19092 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19093 pointer to the declared type for the relevant field variable, or return
19094 `integer_type_node' if the given node turns out to be an
19095 ERROR_MARK node. */
19097 static inline tree
19098 field_type (const_tree decl)
19100 tree type;
19102 if (TREE_CODE (decl) == ERROR_MARK)
19103 return integer_type_node;
19105 type = DECL_BIT_FIELD_TYPE (decl);
19106 if (type == NULL_TREE)
19107 type = TREE_TYPE (decl);
19109 return type;
19112 /* Given a pointer to a tree node, return the alignment in bits for
19113 it, or else return BITS_PER_WORD if the node actually turns out to
19114 be an ERROR_MARK node. */
19116 static inline unsigned
19117 simple_type_align_in_bits (const_tree type)
19119 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19122 static inline unsigned
19123 simple_decl_align_in_bits (const_tree decl)
19125 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19128 /* Return the result of rounding T up to ALIGN. */
19130 static inline offset_int
19131 round_up_to_align (const offset_int &t, unsigned int align)
19133 return wi::udiv_trunc (t + align - 1, align) * align;
19136 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
19137 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
19138 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
19139 if we fail to return the size in one of these two forms. */
19141 static dw_loc_descr_ref
19142 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
19144 tree tree_size;
19145 struct loc_descr_context ctx;
19147 /* Return a constant integer in priority, if possible. */
19148 *cst_size = int_size_in_bytes (type);
19149 if (*cst_size != -1)
19150 return NULL;
19152 ctx.context_type = const_cast<tree> (type);
19153 ctx.base_decl = NULL_TREE;
19154 ctx.dpi = NULL;
19155 ctx.placeholder_arg = false;
19156 ctx.placeholder_seen = false;
19158 type = TYPE_MAIN_VARIANT (type);
19159 tree_size = TYPE_SIZE_UNIT (type);
19160 return ((tree_size != NULL_TREE)
19161 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
19162 : NULL);
19165 /* Helper structure for RECORD_TYPE processing. */
19166 struct vlr_context
19168 /* Root RECORD_TYPE. It is needed to generate data member location
19169 descriptions in variable-length records (VLR), but also to cope with
19170 variants, which are composed of nested structures multiplexed with
19171 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19172 function processing a FIELD_DECL, it is required to be non null. */
19173 tree struct_type;
19175 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19176 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19177 this variant part as part of the root record (in storage units). For
19178 regular records, it must be NULL_TREE. */
19179 tree variant_part_offset;
19182 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19183 addressed byte of the "containing object" for the given FIELD_DECL. If
19184 possible, return a native constant through CST_OFFSET (in which case NULL is
19185 returned); otherwise return a DWARF expression that computes the offset.
19187 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19188 that offset is, either because the argument turns out to be a pointer to an
19189 ERROR_MARK node, or because the offset expression is too complex for us.
19191 CTX is required: see the comment for VLR_CONTEXT. */
19193 static dw_loc_descr_ref
19194 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19195 HOST_WIDE_INT *cst_offset)
19197 tree tree_result;
19198 dw_loc_list_ref loc_result;
19200 *cst_offset = 0;
19202 if (TREE_CODE (decl) == ERROR_MARK)
19203 return NULL;
19204 else
19205 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19207 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19208 case. */
19209 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19210 return NULL;
19212 /* We used to handle only constant offsets in all cases. Now, we handle
19213 properly dynamic byte offsets only when PCC bitfield type doesn't
19214 matter. */
19215 if (PCC_BITFIELD_TYPE_MATTERS
19216 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19218 offset_int object_offset_in_bits;
19219 offset_int object_offset_in_bytes;
19220 offset_int bitpos_int;
19221 tree type;
19222 tree field_size_tree;
19223 offset_int deepest_bitpos;
19224 offset_int field_size_in_bits;
19225 unsigned int type_align_in_bits;
19226 unsigned int decl_align_in_bits;
19227 offset_int type_size_in_bits;
19229 bitpos_int = wi::to_offset (bit_position (decl));
19230 type = field_type (decl);
19231 type_size_in_bits = offset_int_type_size_in_bits (type);
19232 type_align_in_bits = simple_type_align_in_bits (type);
19234 field_size_tree = DECL_SIZE (decl);
19236 /* The size could be unspecified if there was an error, or for
19237 a flexible array member. */
19238 if (!field_size_tree)
19239 field_size_tree = bitsize_zero_node;
19241 /* If the size of the field is not constant, use the type size. */
19242 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19243 field_size_in_bits = wi::to_offset (field_size_tree);
19244 else
19245 field_size_in_bits = type_size_in_bits;
19247 decl_align_in_bits = simple_decl_align_in_bits (decl);
19249 /* The GCC front-end doesn't make any attempt to keep track of the
19250 starting bit offset (relative to the start of the containing
19251 structure type) of the hypothetical "containing object" for a
19252 bit-field. Thus, when computing the byte offset value for the
19253 start of the "containing object" of a bit-field, we must deduce
19254 this information on our own. This can be rather tricky to do in
19255 some cases. For example, handling the following structure type
19256 definition when compiling for an i386/i486 target (which only
19257 aligns long long's to 32-bit boundaries) can be very tricky:
19259 struct S { int field1; long long field2:31; };
19261 Fortunately, there is a simple rule-of-thumb which can be used
19262 in such cases. When compiling for an i386/i486, GCC will
19263 allocate 8 bytes for the structure shown above. It decides to
19264 do this based upon one simple rule for bit-field allocation.
19265 GCC allocates each "containing object" for each bit-field at
19266 the first (i.e. lowest addressed) legitimate alignment boundary
19267 (based upon the required minimum alignment for the declared
19268 type of the field) which it can possibly use, subject to the
19269 condition that there is still enough available space remaining
19270 in the containing object (when allocated at the selected point)
19271 to fully accommodate all of the bits of the bit-field itself.
19273 This simple rule makes it obvious why GCC allocates 8 bytes for
19274 each object of the structure type shown above. When looking
19275 for a place to allocate the "containing object" for `field2',
19276 the compiler simply tries to allocate a 64-bit "containing
19277 object" at each successive 32-bit boundary (starting at zero)
19278 until it finds a place to allocate that 64- bit field such that
19279 at least 31 contiguous (and previously unallocated) bits remain
19280 within that selected 64 bit field. (As it turns out, for the
19281 example above, the compiler finds it is OK to allocate the
19282 "containing object" 64-bit field at bit-offset zero within the
19283 structure type.)
19285 Here we attempt to work backwards from the limited set of facts
19286 we're given, and we try to deduce from those facts, where GCC
19287 must have believed that the containing object started (within
19288 the structure type). The value we deduce is then used (by the
19289 callers of this routine) to generate DW_AT_location and
19290 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19291 the case of DW_AT_location, regular fields as well). */
19293 /* Figure out the bit-distance from the start of the structure to
19294 the "deepest" bit of the bit-field. */
19295 deepest_bitpos = bitpos_int + field_size_in_bits;
19297 /* This is the tricky part. Use some fancy footwork to deduce
19298 where the lowest addressed bit of the containing object must
19299 be. */
19300 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19302 /* Round up to type_align by default. This works best for
19303 bitfields. */
19304 object_offset_in_bits
19305 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19307 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19309 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19311 /* Round up to decl_align instead. */
19312 object_offset_in_bits
19313 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19316 object_offset_in_bytes
19317 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19318 if (ctx->variant_part_offset == NULL_TREE)
19320 *cst_offset = object_offset_in_bytes.to_shwi ();
19321 return NULL;
19323 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19325 else
19326 tree_result = byte_position (decl);
19328 if (ctx->variant_part_offset != NULL_TREE)
19329 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19330 ctx->variant_part_offset, tree_result);
19332 /* If the byte offset is a constant, it's simplier to handle a native
19333 constant rather than a DWARF expression. */
19334 if (TREE_CODE (tree_result) == INTEGER_CST)
19336 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19337 return NULL;
19339 struct loc_descr_context loc_ctx = {
19340 ctx->struct_type, /* context_type */
19341 NULL_TREE, /* base_decl */
19342 NULL, /* dpi */
19343 false, /* placeholder_arg */
19344 false /* placeholder_seen */
19346 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19348 /* We want a DWARF expression: abort if we only have a location list with
19349 multiple elements. */
19350 if (!loc_result || !single_element_loc_list_p (loc_result))
19351 return NULL;
19352 else
19353 return loc_result->expr;
19356 /* The following routines define various Dwarf attributes and any data
19357 associated with them. */
19359 /* Add a location description attribute value to a DIE.
19361 This emits location attributes suitable for whole variables and
19362 whole parameters. Note that the location attributes for struct fields are
19363 generated by the routine `data_member_location_attribute' below. */
19365 static inline void
19366 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19367 dw_loc_list_ref descr)
19369 bool check_no_locviews = true;
19370 if (descr == 0)
19371 return;
19372 if (single_element_loc_list_p (descr))
19373 add_AT_loc (die, attr_kind, descr->expr);
19374 else
19376 add_AT_loc_list (die, attr_kind, descr);
19377 gcc_assert (descr->ll_symbol);
19378 if (attr_kind == DW_AT_location && descr->vl_symbol
19379 && dwarf2out_locviews_in_attribute ())
19381 add_AT_view_list (die, DW_AT_GNU_locviews);
19382 check_no_locviews = false;
19386 if (check_no_locviews)
19387 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19390 /* Add DW_AT_accessibility attribute to DIE if needed. */
19392 static void
19393 add_accessibility_attribute (dw_die_ref die, tree decl)
19395 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19396 children, otherwise the default is DW_ACCESS_public. In DWARF2
19397 the default has always been DW_ACCESS_public. */
19398 if (TREE_PROTECTED (decl))
19399 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19400 else if (TREE_PRIVATE (decl))
19402 if (dwarf_version == 2
19403 || die->die_parent == NULL
19404 || die->die_parent->die_tag != DW_TAG_class_type)
19405 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19407 else if (dwarf_version > 2
19408 && die->die_parent
19409 && die->die_parent->die_tag == DW_TAG_class_type)
19410 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19413 /* Attach the specialized form of location attribute used for data members of
19414 struct and union types. In the special case of a FIELD_DECL node which
19415 represents a bit-field, the "offset" part of this special location
19416 descriptor must indicate the distance in bytes from the lowest-addressed
19417 byte of the containing struct or union type to the lowest-addressed byte of
19418 the "containing object" for the bit-field. (See the `field_byte_offset'
19419 function above).
19421 For any given bit-field, the "containing object" is a hypothetical object
19422 (of some integral or enum type) within which the given bit-field lives. The
19423 type of this hypothetical "containing object" is always the same as the
19424 declared type of the individual bit-field itself (for GCC anyway... the
19425 DWARF spec doesn't actually mandate this). Note that it is the size (in
19426 bytes) of the hypothetical "containing object" which will be given in the
19427 DW_AT_byte_size attribute for this bit-field. (See the
19428 `byte_size_attribute' function below.) It is also used when calculating the
19429 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19430 function below.)
19432 CTX is required: see the comment for VLR_CONTEXT. */
19434 static void
19435 add_data_member_location_attribute (dw_die_ref die,
19436 tree decl,
19437 struct vlr_context *ctx)
19439 HOST_WIDE_INT offset;
19440 dw_loc_descr_ref loc_descr = 0;
19442 if (TREE_CODE (decl) == TREE_BINFO)
19444 /* We're working on the TAG_inheritance for a base class. */
19445 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19447 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19448 aren't at a fixed offset from all (sub)objects of the same
19449 type. We need to extract the appropriate offset from our
19450 vtable. The following dwarf expression means
19452 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19454 This is specific to the V3 ABI, of course. */
19456 dw_loc_descr_ref tmp;
19458 /* Make a copy of the object address. */
19459 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19460 add_loc_descr (&loc_descr, tmp);
19462 /* Extract the vtable address. */
19463 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19464 add_loc_descr (&loc_descr, tmp);
19466 /* Calculate the address of the offset. */
19467 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19468 gcc_assert (offset < 0);
19470 tmp = int_loc_descriptor (-offset);
19471 add_loc_descr (&loc_descr, tmp);
19472 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19473 add_loc_descr (&loc_descr, tmp);
19475 /* Extract the offset. */
19476 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19477 add_loc_descr (&loc_descr, tmp);
19479 /* Add it to the object address. */
19480 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19481 add_loc_descr (&loc_descr, tmp);
19483 else
19484 offset = tree_to_shwi (BINFO_OFFSET (decl));
19486 else
19488 loc_descr = field_byte_offset (decl, ctx, &offset);
19490 /* If loc_descr is available then we know the field offset is dynamic.
19491 However, GDB does not handle dynamic field offsets very well at the
19492 moment. */
19493 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
19495 loc_descr = NULL;
19496 offset = 0;
19499 /* Data member location evalutation starts with the base address on the
19500 stack. Compute the field offset and add it to this base address. */
19501 else if (loc_descr != NULL)
19502 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19505 if (! loc_descr)
19507 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19508 e.g. GDB only added support to it in November 2016. For DWARF5
19509 we need newer debug info consumers anyway. We might change this
19510 to dwarf_version >= 4 once most consumers catched up. */
19511 if (dwarf_version >= 5
19512 && TREE_CODE (decl) == FIELD_DECL
19513 && DECL_BIT_FIELD_TYPE (decl)
19514 && (ctx->variant_part_offset == NULL_TREE
19515 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
19517 tree off = bit_position (decl);
19518 if (ctx->variant_part_offset)
19519 off = bit_from_pos (ctx->variant_part_offset, off);
19520 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19522 remove_AT (die, DW_AT_byte_size);
19523 remove_AT (die, DW_AT_bit_offset);
19524 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19525 return;
19528 if (dwarf_version > 2)
19530 /* Don't need to output a location expression, just the constant. */
19531 if (offset < 0)
19532 add_AT_int (die, DW_AT_data_member_location, offset);
19533 else
19534 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19535 return;
19537 else
19539 enum dwarf_location_atom op;
19541 /* The DWARF2 standard says that we should assume that the structure
19542 address is already on the stack, so we can specify a structure
19543 field address by using DW_OP_plus_uconst. */
19544 op = DW_OP_plus_uconst;
19545 loc_descr = new_loc_descr (op, offset, 0);
19549 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19552 /* Writes integer values to dw_vec_const array. */
19554 static void
19555 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19557 while (size != 0)
19559 *dest++ = val & 0xff;
19560 val >>= 8;
19561 --size;
19565 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
19567 static HOST_WIDE_INT
19568 extract_int (const unsigned char *src, unsigned int size)
19570 HOST_WIDE_INT val = 0;
19572 src += size;
19573 while (size != 0)
19575 val <<= 8;
19576 val |= *--src & 0xff;
19577 --size;
19579 return val;
19582 /* Writes wide_int values to dw_vec_const array. */
19584 static void
19585 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
19587 int i;
19589 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
19591 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
19592 return;
19595 /* We'd have to extend this code to support odd sizes. */
19596 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
19598 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
19600 if (WORDS_BIG_ENDIAN)
19601 for (i = n - 1; i >= 0; i--)
19603 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19604 dest += sizeof (HOST_WIDE_INT);
19606 else
19607 for (i = 0; i < n; i++)
19609 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19610 dest += sizeof (HOST_WIDE_INT);
19614 /* Writes floating point values to dw_vec_const array. */
19616 static void
19617 insert_float (const_rtx rtl, unsigned char *array)
19619 long val[4];
19620 int i;
19621 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19623 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
19625 /* real_to_target puts 32-bit pieces in each long. Pack them. */
19626 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
19628 insert_int (val[i], 4, array);
19629 array += 4;
19633 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
19634 does not have a "location" either in memory or in a register. These
19635 things can arise in GNU C when a constant is passed as an actual parameter
19636 to an inlined function. They can also arise in C++ where declared
19637 constants do not necessarily get memory "homes". */
19639 static bool
19640 add_const_value_attribute (dw_die_ref die, rtx rtl)
19642 switch (GET_CODE (rtl))
19644 case CONST_INT:
19646 HOST_WIDE_INT val = INTVAL (rtl);
19648 if (val < 0)
19649 add_AT_int (die, DW_AT_const_value, val);
19650 else
19651 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
19653 return true;
19655 case CONST_WIDE_INT:
19657 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
19658 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
19659 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
19660 wide_int w = wi::zext (w1, prec);
19661 add_AT_wide (die, DW_AT_const_value, w);
19663 return true;
19665 case CONST_DOUBLE:
19666 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
19667 floating-point constant. A CONST_DOUBLE is used whenever the
19668 constant requires more than one word in order to be adequately
19669 represented. */
19670 if (TARGET_SUPPORTS_WIDE_INT == 0
19671 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
19672 add_AT_double (die, DW_AT_const_value,
19673 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
19674 else
19676 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19677 unsigned int length = GET_MODE_SIZE (mode);
19678 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
19680 insert_float (rtl, array);
19681 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
19683 return true;
19685 case CONST_VECTOR:
19687 unsigned int length;
19688 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
19689 return false;
19691 machine_mode mode = GET_MODE (rtl);
19692 /* The combination of a length and byte elt_size doesn't extend
19693 naturally to boolean vectors, where several elements are packed
19694 into the same byte. */
19695 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
19696 return false;
19698 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
19699 unsigned char *array
19700 = ggc_vec_alloc<unsigned char> (length * elt_size);
19701 unsigned int i;
19702 unsigned char *p;
19703 machine_mode imode = GET_MODE_INNER (mode);
19705 switch (GET_MODE_CLASS (mode))
19707 case MODE_VECTOR_INT:
19708 for (i = 0, p = array; i < length; i++, p += elt_size)
19710 rtx elt = CONST_VECTOR_ELT (rtl, i);
19711 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
19713 break;
19715 case MODE_VECTOR_FLOAT:
19716 for (i = 0, p = array; i < length; i++, p += elt_size)
19718 rtx elt = CONST_VECTOR_ELT (rtl, i);
19719 insert_float (elt, p);
19721 break;
19723 default:
19724 gcc_unreachable ();
19727 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
19729 return true;
19731 case CONST_STRING:
19732 if (dwarf_version >= 4 || !dwarf_strict)
19734 dw_loc_descr_ref loc_result;
19735 resolve_one_addr (&rtl);
19736 rtl_addr:
19737 loc_result = new_addr_loc_descr (rtl, dtprel_false);
19738 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
19739 add_AT_loc (die, DW_AT_location, loc_result);
19740 vec_safe_push (used_rtx_array, rtl);
19741 return true;
19743 return false;
19745 case CONST:
19746 if (CONSTANT_P (XEXP (rtl, 0)))
19747 return add_const_value_attribute (die, XEXP (rtl, 0));
19748 /* FALLTHROUGH */
19749 case SYMBOL_REF:
19750 if (!const_ok_for_output (rtl))
19751 return false;
19752 /* FALLTHROUGH */
19753 case LABEL_REF:
19754 if (dwarf_version >= 4 || !dwarf_strict)
19755 goto rtl_addr;
19756 return false;
19758 case PLUS:
19759 /* In cases where an inlined instance of an inline function is passed
19760 the address of an `auto' variable (which is local to the caller) we
19761 can get a situation where the DECL_RTL of the artificial local
19762 variable (for the inlining) which acts as a stand-in for the
19763 corresponding formal parameter (of the inline function) will look
19764 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
19765 exactly a compile-time constant expression, but it isn't the address
19766 of the (artificial) local variable either. Rather, it represents the
19767 *value* which the artificial local variable always has during its
19768 lifetime. We currently have no way to represent such quasi-constant
19769 values in Dwarf, so for now we just punt and generate nothing. */
19770 return false;
19772 case HIGH:
19773 case CONST_FIXED:
19774 case MINUS:
19775 case SIGN_EXTEND:
19776 case ZERO_EXTEND:
19777 case CONST_POLY_INT:
19778 return false;
19780 case MEM:
19781 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
19782 && MEM_READONLY_P (rtl)
19783 && GET_MODE (rtl) == BLKmode)
19785 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
19786 return true;
19788 return false;
19790 default:
19791 /* No other kinds of rtx should be possible here. */
19792 gcc_unreachable ();
19794 return false;
19797 /* Determine whether the evaluation of EXPR references any variables
19798 or functions which aren't otherwise used (and therefore may not be
19799 output). */
19800 static tree
19801 reference_to_unused (tree * tp, int * walk_subtrees,
19802 void * data ATTRIBUTE_UNUSED)
19804 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
19805 *walk_subtrees = 0;
19807 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
19808 && ! TREE_ASM_WRITTEN (*tp))
19809 return *tp;
19810 /* ??? The C++ FE emits debug information for using decls, so
19811 putting gcc_unreachable here falls over. See PR31899. For now
19812 be conservative. */
19813 else if (!symtab->global_info_ready && VAR_P (*tp))
19814 return *tp;
19815 else if (VAR_P (*tp))
19817 varpool_node *node = varpool_node::get (*tp);
19818 if (!node || !node->definition)
19819 return *tp;
19821 else if (TREE_CODE (*tp) == FUNCTION_DECL
19822 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
19824 /* The call graph machinery must have finished analyzing,
19825 optimizing and gimplifying the CU by now.
19826 So if *TP has no call graph node associated
19827 to it, it means *TP will not be emitted. */
19828 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
19829 return *tp;
19831 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
19832 return *tp;
19834 return NULL_TREE;
19837 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
19838 for use in a later add_const_value_attribute call. */
19840 static rtx
19841 rtl_for_decl_init (tree init, tree type)
19843 rtx rtl = NULL_RTX;
19845 STRIP_NOPS (init);
19847 /* If a variable is initialized with a string constant without embedded
19848 zeros, build CONST_STRING. */
19849 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
19851 tree enttype = TREE_TYPE (type);
19852 tree domain = TYPE_DOMAIN (type);
19853 scalar_int_mode mode;
19855 if (is_int_mode (TYPE_MODE (enttype), &mode)
19856 && GET_MODE_SIZE (mode) == 1
19857 && domain
19858 && TYPE_MAX_VALUE (domain)
19859 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
19860 && integer_zerop (TYPE_MIN_VALUE (domain))
19861 && compare_tree_int (TYPE_MAX_VALUE (domain),
19862 TREE_STRING_LENGTH (init) - 1) == 0
19863 && ((size_t) TREE_STRING_LENGTH (init)
19864 == strlen (TREE_STRING_POINTER (init)) + 1))
19866 rtl = gen_rtx_CONST_STRING (VOIDmode,
19867 ggc_strdup (TREE_STRING_POINTER (init)));
19868 rtl = gen_rtx_MEM (BLKmode, rtl);
19869 MEM_READONLY_P (rtl) = 1;
19872 /* Other aggregates, and complex values, could be represented using
19873 CONCAT: FIXME! */
19874 else if (AGGREGATE_TYPE_P (type)
19875 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
19876 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
19877 || TREE_CODE (type) == COMPLEX_TYPE)
19879 /* Vectors only work if their mode is supported by the target.
19880 FIXME: generic vectors ought to work too. */
19881 else if (TREE_CODE (type) == VECTOR_TYPE
19882 && !VECTOR_MODE_P (TYPE_MODE (type)))
19884 /* If the initializer is something that we know will expand into an
19885 immediate RTL constant, expand it now. We must be careful not to
19886 reference variables which won't be output. */
19887 else if (initializer_constant_valid_p (init, type)
19888 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19890 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19891 possible. */
19892 if (TREE_CODE (type) == VECTOR_TYPE)
19893 switch (TREE_CODE (init))
19895 case VECTOR_CST:
19896 break;
19897 case CONSTRUCTOR:
19898 if (TREE_CONSTANT (init))
19900 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19901 bool constant_p = true;
19902 tree value;
19903 unsigned HOST_WIDE_INT ix;
19905 /* Even when ctor is constant, it might contain non-*_CST
19906 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19907 belong into VECTOR_CST nodes. */
19908 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19909 if (!CONSTANT_CLASS_P (value))
19911 constant_p = false;
19912 break;
19915 if (constant_p)
19917 init = build_vector_from_ctor (type, elts);
19918 break;
19921 /* FALLTHRU */
19923 default:
19924 return NULL;
19927 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19929 /* If expand_expr returns a MEM, it wasn't immediate. */
19930 gcc_assert (!rtl || !MEM_P (rtl));
19933 return rtl;
19936 /* Generate RTL for the variable DECL to represent its location. */
19938 static rtx
19939 rtl_for_decl_location (tree decl)
19941 rtx rtl;
19943 /* Here we have to decide where we are going to say the parameter "lives"
19944 (as far as the debugger is concerned). We only have a couple of
19945 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19947 DECL_RTL normally indicates where the parameter lives during most of the
19948 activation of the function. If optimization is enabled however, this
19949 could be either NULL or else a pseudo-reg. Both of those cases indicate
19950 that the parameter doesn't really live anywhere (as far as the code
19951 generation parts of GCC are concerned) during most of the function's
19952 activation. That will happen (for example) if the parameter is never
19953 referenced within the function.
19955 We could just generate a location descriptor here for all non-NULL
19956 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19957 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19958 where DECL_RTL is NULL or is a pseudo-reg.
19960 Note however that we can only get away with using DECL_INCOMING_RTL as
19961 a backup substitute for DECL_RTL in certain limited cases. In cases
19962 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19963 we can be sure that the parameter was passed using the same type as it is
19964 declared to have within the function, and that its DECL_INCOMING_RTL
19965 points us to a place where a value of that type is passed.
19967 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19968 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19969 because in these cases DECL_INCOMING_RTL points us to a value of some
19970 type which is *different* from the type of the parameter itself. Thus,
19971 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19972 such cases, the debugger would end up (for example) trying to fetch a
19973 `float' from a place which actually contains the first part of a
19974 `double'. That would lead to really incorrect and confusing
19975 output at debug-time.
19977 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19978 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19979 are a couple of exceptions however. On little-endian machines we can
19980 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19981 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19982 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19983 when (on a little-endian machine) a non-prototyped function has a
19984 parameter declared to be of type `short' or `char'. In such cases,
19985 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19986 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19987 passed `int' value. If the debugger then uses that address to fetch
19988 a `short' or a `char' (on a little-endian machine) the result will be
19989 the correct data, so we allow for such exceptional cases below.
19991 Note that our goal here is to describe the place where the given formal
19992 parameter lives during most of the function's activation (i.e. between the
19993 end of the prologue and the start of the epilogue). We'll do that as best
19994 as we can. Note however that if the given formal parameter is modified
19995 sometime during the execution of the function, then a stack backtrace (at
19996 debug-time) will show the function as having been called with the *new*
19997 value rather than the value which was originally passed in. This happens
19998 rarely enough that it is not a major problem, but it *is* a problem, and
19999 I'd like to fix it.
20001 A future version of dwarf2out.c may generate two additional attributes for
20002 any given DW_TAG_formal_parameter DIE which will describe the "passed
20003 type" and the "passed location" for the given formal parameter in addition
20004 to the attributes we now generate to indicate the "declared type" and the
20005 "active location" for each parameter. This additional set of attributes
20006 could be used by debuggers for stack backtraces. Separately, note that
20007 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20008 This happens (for example) for inlined-instances of inline function formal
20009 parameters which are never referenced. This really shouldn't be
20010 happening. All PARM_DECL nodes should get valid non-NULL
20011 DECL_INCOMING_RTL values. FIXME. */
20013 /* Use DECL_RTL as the "location" unless we find something better. */
20014 rtl = DECL_RTL_IF_SET (decl);
20016 /* When generating abstract instances, ignore everything except
20017 constants, symbols living in memory, and symbols living in
20018 fixed registers. */
20019 if (! reload_completed)
20021 if (rtl
20022 && (CONSTANT_P (rtl)
20023 || (MEM_P (rtl)
20024 && CONSTANT_P (XEXP (rtl, 0)))
20025 || (REG_P (rtl)
20026 && VAR_P (decl)
20027 && TREE_STATIC (decl))))
20029 rtl = targetm.delegitimize_address (rtl);
20030 return rtl;
20032 rtl = NULL_RTX;
20034 else if (TREE_CODE (decl) == PARM_DECL)
20036 if (rtl == NULL_RTX
20037 || is_pseudo_reg (rtl)
20038 || (MEM_P (rtl)
20039 && is_pseudo_reg (XEXP (rtl, 0))
20040 && DECL_INCOMING_RTL (decl)
20041 && MEM_P (DECL_INCOMING_RTL (decl))
20042 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20044 tree declared_type = TREE_TYPE (decl);
20045 tree passed_type = DECL_ARG_TYPE (decl);
20046 machine_mode dmode = TYPE_MODE (declared_type);
20047 machine_mode pmode = TYPE_MODE (passed_type);
20049 /* This decl represents a formal parameter which was optimized out.
20050 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20051 all cases where (rtl == NULL_RTX) just below. */
20052 if (dmode == pmode)
20053 rtl = DECL_INCOMING_RTL (decl);
20054 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20055 && SCALAR_INT_MODE_P (dmode)
20056 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20057 && DECL_INCOMING_RTL (decl))
20059 rtx inc = DECL_INCOMING_RTL (decl);
20060 if (REG_P (inc))
20061 rtl = inc;
20062 else if (MEM_P (inc))
20064 if (BYTES_BIG_ENDIAN)
20065 rtl = adjust_address_nv (inc, dmode,
20066 GET_MODE_SIZE (pmode)
20067 - GET_MODE_SIZE (dmode));
20068 else
20069 rtl = inc;
20074 /* If the parm was passed in registers, but lives on the stack, then
20075 make a big endian correction if the mode of the type of the
20076 parameter is not the same as the mode of the rtl. */
20077 /* ??? This is the same series of checks that are made in dbxout.c before
20078 we reach the big endian correction code there. It isn't clear if all
20079 of these checks are necessary here, but keeping them all is the safe
20080 thing to do. */
20081 else if (MEM_P (rtl)
20082 && XEXP (rtl, 0) != const0_rtx
20083 && ! CONSTANT_P (XEXP (rtl, 0))
20084 /* Not passed in memory. */
20085 && !MEM_P (DECL_INCOMING_RTL (decl))
20086 /* Not passed by invisible reference. */
20087 && (!REG_P (XEXP (rtl, 0))
20088 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20089 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20090 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20091 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20092 #endif
20094 /* Big endian correction check. */
20095 && BYTES_BIG_ENDIAN
20096 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20097 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20098 UNITS_PER_WORD))
20100 machine_mode addr_mode = get_address_mode (rtl);
20101 poly_int64 offset = (UNITS_PER_WORD
20102 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20104 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20105 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20108 else if (VAR_P (decl)
20109 && rtl
20110 && MEM_P (rtl)
20111 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20113 machine_mode addr_mode = get_address_mode (rtl);
20114 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20115 GET_MODE (rtl));
20117 /* If a variable is declared "register" yet is smaller than
20118 a register, then if we store the variable to memory, it
20119 looks like we're storing a register-sized value, when in
20120 fact we are not. We need to adjust the offset of the
20121 storage location to reflect the actual value's bytes,
20122 else gdb will not be able to display it. */
20123 if (maybe_ne (offset, 0))
20124 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20125 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20128 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20129 and will have been substituted directly into all expressions that use it.
20130 C does not have such a concept, but C++ and other languages do. */
20131 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20132 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20134 if (rtl)
20135 rtl = targetm.delegitimize_address (rtl);
20137 /* If we don't look past the constant pool, we risk emitting a
20138 reference to a constant pool entry that isn't referenced from
20139 code, and thus is not emitted. */
20140 if (rtl)
20141 rtl = avoid_constant_pool_reference (rtl);
20143 /* Try harder to get a rtl. If this symbol ends up not being emitted
20144 in the current CU, resolve_addr will remove the expression referencing
20145 it. */
20146 if (rtl == NULL_RTX
20147 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20148 && VAR_P (decl)
20149 && !DECL_EXTERNAL (decl)
20150 && TREE_STATIC (decl)
20151 && DECL_NAME (decl)
20152 && !DECL_HARD_REGISTER (decl)
20153 && DECL_MODE (decl) != VOIDmode)
20155 rtl = make_decl_rtl_for_debug (decl);
20156 if (!MEM_P (rtl)
20157 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20158 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20159 rtl = NULL_RTX;
20162 return rtl;
20165 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20166 returned. If so, the decl for the COMMON block is returned, and the
20167 value is the offset into the common block for the symbol. */
20169 static tree
20170 fortran_common (tree decl, HOST_WIDE_INT *value)
20172 tree val_expr, cvar;
20173 machine_mode mode;
20174 poly_int64 bitsize, bitpos;
20175 tree offset;
20176 HOST_WIDE_INT cbitpos;
20177 int unsignedp, reversep, volatilep = 0;
20179 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20180 it does not have a value (the offset into the common area), or if it
20181 is thread local (as opposed to global) then it isn't common, and shouldn't
20182 be handled as such. */
20183 if (!VAR_P (decl)
20184 || !TREE_STATIC (decl)
20185 || !DECL_HAS_VALUE_EXPR_P (decl)
20186 || !is_fortran ())
20187 return NULL_TREE;
20189 val_expr = DECL_VALUE_EXPR (decl);
20190 if (TREE_CODE (val_expr) != COMPONENT_REF)
20191 return NULL_TREE;
20193 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20194 &unsignedp, &reversep, &volatilep);
20196 if (cvar == NULL_TREE
20197 || !VAR_P (cvar)
20198 || DECL_ARTIFICIAL (cvar)
20199 || !TREE_PUBLIC (cvar)
20200 /* We don't expect to have to cope with variable offsets,
20201 since at present all static data must have a constant size. */
20202 || !bitpos.is_constant (&cbitpos))
20203 return NULL_TREE;
20205 *value = 0;
20206 if (offset != NULL)
20208 if (!tree_fits_shwi_p (offset))
20209 return NULL_TREE;
20210 *value = tree_to_shwi (offset);
20212 if (cbitpos != 0)
20213 *value += cbitpos / BITS_PER_UNIT;
20215 return cvar;
20218 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20219 data attribute for a variable or a parameter. We generate the
20220 DW_AT_const_value attribute only in those cases where the given variable
20221 or parameter does not have a true "location" either in memory or in a
20222 register. This can happen (for example) when a constant is passed as an
20223 actual argument in a call to an inline function. (It's possible that
20224 these things can crop up in other ways also.) Note that one type of
20225 constant value which can be passed into an inlined function is a constant
20226 pointer. This can happen for example if an actual argument in an inlined
20227 function call evaluates to a compile-time constant address.
20229 CACHE_P is true if it is worth caching the location list for DECL,
20230 so that future calls can reuse it rather than regenerate it from scratch.
20231 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20232 since we will need to refer to them each time the function is inlined. */
20234 static bool
20235 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20237 rtx rtl;
20238 dw_loc_list_ref list;
20239 var_loc_list *loc_list;
20240 cached_dw_loc_list *cache;
20242 if (early_dwarf)
20243 return false;
20245 if (TREE_CODE (decl) == ERROR_MARK)
20246 return false;
20248 if (get_AT (die, DW_AT_location)
20249 || get_AT (die, DW_AT_const_value))
20250 return true;
20252 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20253 || TREE_CODE (decl) == RESULT_DECL);
20255 /* Try to get some constant RTL for this decl, and use that as the value of
20256 the location. */
20258 rtl = rtl_for_decl_location (decl);
20259 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20260 && add_const_value_attribute (die, rtl))
20261 return true;
20263 /* See if we have single element location list that is equivalent to
20264 a constant value. That way we are better to use add_const_value_attribute
20265 rather than expanding constant value equivalent. */
20266 loc_list = lookup_decl_loc (decl);
20267 if (loc_list
20268 && loc_list->first
20269 && loc_list->first->next == NULL
20270 && NOTE_P (loc_list->first->loc)
20271 && NOTE_VAR_LOCATION (loc_list->first->loc)
20272 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20274 struct var_loc_node *node;
20276 node = loc_list->first;
20277 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20278 if (GET_CODE (rtl) == EXPR_LIST)
20279 rtl = XEXP (rtl, 0);
20280 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20281 && add_const_value_attribute (die, rtl))
20282 return true;
20284 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20285 list several times. See if we've already cached the contents. */
20286 list = NULL;
20287 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20288 cache_p = false;
20289 if (cache_p)
20291 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20292 if (cache)
20293 list = cache->loc_list;
20295 if (list == NULL)
20297 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20298 NULL);
20299 /* It is usually worth caching this result if the decl is from
20300 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20301 if (cache_p && list && list->dw_loc_next)
20303 cached_dw_loc_list **slot
20304 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20305 DECL_UID (decl),
20306 INSERT);
20307 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20308 cache->decl_id = DECL_UID (decl);
20309 cache->loc_list = list;
20310 *slot = cache;
20313 if (list)
20315 add_AT_location_description (die, DW_AT_location, list);
20316 return true;
20318 /* None of that worked, so it must not really have a location;
20319 try adding a constant value attribute from the DECL_INITIAL. */
20320 return tree_add_const_value_attribute_for_decl (die, decl);
20323 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20324 attribute is the const value T. */
20326 static bool
20327 tree_add_const_value_attribute (dw_die_ref die, tree t)
20329 tree init;
20330 tree type = TREE_TYPE (t);
20331 rtx rtl;
20333 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20334 return false;
20336 init = t;
20337 gcc_assert (!DECL_P (init));
20339 if (TREE_CODE (init) == INTEGER_CST)
20341 if (tree_fits_uhwi_p (init))
20343 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20344 return true;
20346 if (tree_fits_shwi_p (init))
20348 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20349 return true;
20352 /* Generate the RTL even if early_dwarf to force mangling of all refered to
20353 symbols. */
20354 rtl = rtl_for_decl_init (init, type);
20355 if (rtl && !early_dwarf)
20356 return add_const_value_attribute (die, rtl);
20357 /* If the host and target are sane, try harder. */
20358 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20359 && initializer_constant_valid_p (init, type))
20361 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20362 if (size > 0 && (int) size == size)
20364 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20366 if (native_encode_initializer (init, array, size) == size)
20368 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20369 return true;
20371 ggc_free (array);
20374 return false;
20377 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20378 attribute is the const value of T, where T is an integral constant
20379 variable with static storage duration
20380 (so it can't be a PARM_DECL or a RESULT_DECL). */
20382 static bool
20383 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20386 if (!decl
20387 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20388 || (VAR_P (decl) && !TREE_STATIC (decl)))
20389 return false;
20391 if (TREE_READONLY (decl)
20392 && ! TREE_THIS_VOLATILE (decl)
20393 && DECL_INITIAL (decl))
20394 /* OK */;
20395 else
20396 return false;
20398 /* Don't add DW_AT_const_value if abstract origin already has one. */
20399 if (get_AT (var_die, DW_AT_const_value))
20400 return false;
20402 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20405 /* Convert the CFI instructions for the current function into a
20406 location list. This is used for DW_AT_frame_base when we targeting
20407 a dwarf2 consumer that does not support the dwarf3
20408 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20409 expressions. */
20411 static dw_loc_list_ref
20412 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20414 int ix;
20415 dw_fde_ref fde;
20416 dw_loc_list_ref list, *list_tail;
20417 dw_cfi_ref cfi;
20418 dw_cfa_location last_cfa, next_cfa;
20419 const char *start_label, *last_label, *section;
20420 dw_cfa_location remember;
20422 fde = cfun->fde;
20423 gcc_assert (fde != NULL);
20425 section = secname_for_decl (current_function_decl);
20426 list_tail = &list;
20427 list = NULL;
20429 memset (&next_cfa, 0, sizeof (next_cfa));
20430 next_cfa.reg = INVALID_REGNUM;
20431 remember = next_cfa;
20433 start_label = fde->dw_fde_begin;
20435 /* ??? Bald assumption that the CIE opcode list does not contain
20436 advance opcodes. */
20437 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20438 lookup_cfa_1 (cfi, &next_cfa, &remember);
20440 last_cfa = next_cfa;
20441 last_label = start_label;
20443 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20445 /* If the first partition contained no CFI adjustments, the
20446 CIE opcodes apply to the whole first partition. */
20447 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20448 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20449 list_tail =&(*list_tail)->dw_loc_next;
20450 start_label = last_label = fde->dw_fde_second_begin;
20453 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20455 switch (cfi->dw_cfi_opc)
20457 case DW_CFA_set_loc:
20458 case DW_CFA_advance_loc1:
20459 case DW_CFA_advance_loc2:
20460 case DW_CFA_advance_loc4:
20461 if (!cfa_equal_p (&last_cfa, &next_cfa))
20463 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20464 start_label, 0, last_label, 0, section);
20466 list_tail = &(*list_tail)->dw_loc_next;
20467 last_cfa = next_cfa;
20468 start_label = last_label;
20470 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20471 break;
20473 case DW_CFA_advance_loc:
20474 /* The encoding is complex enough that we should never emit this. */
20475 gcc_unreachable ();
20477 default:
20478 lookup_cfa_1 (cfi, &next_cfa, &remember);
20479 break;
20481 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20483 if (!cfa_equal_p (&last_cfa, &next_cfa))
20485 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20486 start_label, 0, last_label, 0, section);
20488 list_tail = &(*list_tail)->dw_loc_next;
20489 last_cfa = next_cfa;
20490 start_label = last_label;
20492 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20493 start_label, 0, fde->dw_fde_end, 0, section);
20494 list_tail = &(*list_tail)->dw_loc_next;
20495 start_label = last_label = fde->dw_fde_second_begin;
20499 if (!cfa_equal_p (&last_cfa, &next_cfa))
20501 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20502 start_label, 0, last_label, 0, section);
20503 list_tail = &(*list_tail)->dw_loc_next;
20504 start_label = last_label;
20507 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20508 start_label, 0,
20509 fde->dw_fde_second_begin
20510 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20511 section);
20513 maybe_gen_llsym (list);
20515 return list;
20518 /* Compute a displacement from the "steady-state frame pointer" to the
20519 frame base (often the same as the CFA), and store it in
20520 frame_pointer_fb_offset. OFFSET is added to the displacement
20521 before the latter is negated. */
20523 static void
20524 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20526 rtx reg, elim;
20528 #ifdef FRAME_POINTER_CFA_OFFSET
20529 reg = frame_pointer_rtx;
20530 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20531 #else
20532 reg = arg_pointer_rtx;
20533 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20534 #endif
20536 elim = (ira_use_lra_p
20537 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20538 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20539 elim = strip_offset_and_add (elim, &offset);
20541 frame_pointer_fb_offset = -offset;
20543 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20544 in which to eliminate. This is because it's stack pointer isn't
20545 directly accessible as a register within the ISA. To work around
20546 this, assume that while we cannot provide a proper value for
20547 frame_pointer_fb_offset, we won't need one either. We can use
20548 hard frame pointer in debug info even if frame pointer isn't used
20549 since hard frame pointer in debug info is encoded with DW_OP_fbreg
20550 which uses the DW_AT_frame_base attribute, not hard frame pointer
20551 directly. */
20552 frame_pointer_fb_offset_valid
20553 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
20556 /* Generate a DW_AT_name attribute given some string value to be included as
20557 the value of the attribute. */
20559 static void
20560 add_name_attribute (dw_die_ref die, const char *name_string)
20562 if (name_string != NULL && *name_string != 0)
20564 if (demangle_name_func)
20565 name_string = (*demangle_name_func) (name_string);
20567 add_AT_string (die, DW_AT_name, name_string);
20571 /* Generate a DW_AT_name attribute given some string value representing a
20572 file or filepath to be included as value of the attribute. */
20573 static void
20574 add_filename_attribute (dw_die_ref die, const char *name_string)
20576 if (name_string != NULL && *name_string != 0)
20577 add_filepath_AT_string (die, DW_AT_name, name_string);
20580 /* Generate a DW_AT_description attribute given some string value to be included
20581 as the value of the attribute. */
20583 static void
20584 add_desc_attribute (dw_die_ref die, const char *name_string)
20586 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20587 return;
20589 if (name_string == NULL || *name_string == 0)
20590 return;
20592 if (demangle_name_func)
20593 name_string = (*demangle_name_func) (name_string);
20595 add_AT_string (die, DW_AT_description, name_string);
20598 /* Generate a DW_AT_description attribute given some decl to be included
20599 as the value of the attribute. */
20601 static void
20602 add_desc_attribute (dw_die_ref die, tree decl)
20604 tree decl_name;
20606 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20607 return;
20609 if (decl == NULL_TREE || !DECL_P (decl))
20610 return;
20611 decl_name = DECL_NAME (decl);
20613 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20615 const char *name = dwarf2_name (decl, 0);
20616 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
20618 else
20620 char *desc = print_generic_expr_to_str (decl);
20621 add_desc_attribute (die, desc);
20622 free (desc);
20626 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
20627 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
20628 of TYPE accordingly.
20630 ??? This is a temporary measure until after we're able to generate
20631 regular DWARF for the complex Ada type system. */
20633 static void
20634 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
20635 dw_die_ref context_die)
20637 tree dtype;
20638 dw_die_ref dtype_die;
20640 if (!lang_hooks.types.descriptive_type)
20641 return;
20643 dtype = lang_hooks.types.descriptive_type (type);
20644 if (!dtype)
20645 return;
20647 dtype_die = lookup_type_die (dtype);
20648 if (!dtype_die)
20650 gen_type_die (dtype, context_die);
20651 dtype_die = lookup_type_die (dtype);
20652 gcc_assert (dtype_die);
20655 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
20658 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
20660 static const char *
20661 comp_dir_string (void)
20663 const char *wd;
20664 char *wd_plus_sep = NULL;
20665 static const char *cached_wd = NULL;
20667 if (cached_wd != NULL)
20668 return cached_wd;
20670 wd = get_src_pwd ();
20671 if (wd == NULL)
20672 return NULL;
20674 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
20676 size_t wdlen = strlen (wd);
20677 wd_plus_sep = XNEWVEC (char, wdlen + 2);
20678 strcpy (wd_plus_sep, wd);
20679 wd_plus_sep [wdlen] = DIR_SEPARATOR;
20680 wd_plus_sep [wdlen + 1] = 0;
20681 wd = wd_plus_sep;
20684 cached_wd = remap_debug_filename (wd);
20686 /* remap_debug_filename can just pass through wd or return a new gc string.
20687 These two types can't be both stored in a GTY(())-tagged string, but since
20688 the cached value lives forever just copy it if needed. */
20689 if (cached_wd != wd)
20691 cached_wd = xstrdup (cached_wd);
20692 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
20693 free (wd_plus_sep);
20696 return cached_wd;
20699 /* Generate a DW_AT_comp_dir attribute for DIE. */
20701 static void
20702 add_comp_dir_attribute (dw_die_ref die)
20704 const char * wd = comp_dir_string ();
20705 if (wd != NULL)
20706 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
20709 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
20710 pointer computation, ...), output a representation for that bound according
20711 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
20712 loc_list_from_tree for the meaning of CONTEXT. */
20714 static void
20715 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
20716 int forms, struct loc_descr_context *context)
20718 dw_die_ref context_die, decl_die = NULL;
20719 dw_loc_list_ref list;
20720 bool strip_conversions = true;
20721 bool placeholder_seen = false;
20723 while (strip_conversions)
20724 switch (TREE_CODE (value))
20726 case ERROR_MARK:
20727 case SAVE_EXPR:
20728 return;
20730 CASE_CONVERT:
20731 case VIEW_CONVERT_EXPR:
20732 value = TREE_OPERAND (value, 0);
20733 break;
20735 default:
20736 strip_conversions = false;
20737 break;
20740 /* If possible and permitted, output the attribute as a constant. */
20741 if ((forms & dw_scalar_form_constant) != 0
20742 && TREE_CODE (value) == INTEGER_CST)
20744 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
20746 /* If HOST_WIDE_INT is big enough then represent the bound as
20747 a constant value. We need to choose a form based on
20748 whether the type is signed or unsigned. We cannot just
20749 call add_AT_unsigned if the value itself is positive
20750 (add_AT_unsigned might add the unsigned value encoded as
20751 DW_FORM_data[1248]). Some DWARF consumers will lookup the
20752 bounds type and then sign extend any unsigned values found
20753 for signed types. This is needed only for
20754 DW_AT_{lower,upper}_bound, since for most other attributes,
20755 consumers will treat DW_FORM_data[1248] as unsigned values,
20756 regardless of the underlying type. */
20757 if (prec <= HOST_BITS_PER_WIDE_INT
20758 || tree_fits_uhwi_p (value))
20760 if (TYPE_UNSIGNED (TREE_TYPE (value)))
20761 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
20762 else
20763 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
20765 else
20766 /* Otherwise represent the bound as an unsigned value with
20767 the precision of its type. The precision and signedness
20768 of the type will be necessary to re-interpret it
20769 unambiguously. */
20770 add_AT_wide (die, attr, wi::to_wide (value));
20771 return;
20774 /* Otherwise, if it's possible and permitted too, output a reference to
20775 another DIE. */
20776 if ((forms & dw_scalar_form_reference) != 0)
20778 tree decl = NULL_TREE;
20780 /* Some type attributes reference an outer type. For instance, the upper
20781 bound of an array may reference an embedding record (this happens in
20782 Ada). */
20783 if (TREE_CODE (value) == COMPONENT_REF
20784 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
20785 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
20786 decl = TREE_OPERAND (value, 1);
20788 else if (VAR_P (value)
20789 || TREE_CODE (value) == PARM_DECL
20790 || TREE_CODE (value) == RESULT_DECL)
20791 decl = value;
20793 if (decl != NULL_TREE)
20795 decl_die = lookup_decl_die (decl);
20797 /* ??? Can this happen, or should the variable have been bound
20798 first? Probably it can, since I imagine that we try to create
20799 the types of parameters in the order in which they exist in
20800 the list, and won't have created a forward reference to a
20801 later parameter. */
20802 if (decl_die != NULL)
20804 if (get_AT (decl_die, DW_AT_location)
20805 || get_AT (decl_die, DW_AT_data_member_location)
20806 || get_AT (decl_die, DW_AT_const_value))
20808 add_AT_die_ref (die, attr, decl_die);
20809 return;
20815 /* Last chance: try to create a stack operation procedure to evaluate the
20816 value. Do nothing if even that is not possible or permitted. */
20817 if ((forms & dw_scalar_form_exprloc) == 0)
20818 return;
20820 list = loc_list_from_tree (value, 2, context);
20821 if (context && context->placeholder_arg)
20823 placeholder_seen = context->placeholder_seen;
20824 context->placeholder_seen = false;
20826 if (list == NULL || single_element_loc_list_p (list))
20828 /* If this attribute is not a reference nor constant, it is
20829 a DWARF expression rather than location description. For that
20830 loc_list_from_tree (value, 0, &context) is needed. */
20831 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20832 if (list2 && single_element_loc_list_p (list2))
20834 if (placeholder_seen)
20836 struct dwarf_procedure_info dpi;
20837 dpi.fndecl = NULL_TREE;
20838 dpi.args_count = 1;
20839 if (!resolve_args_picking (list2->expr, 1, &dpi))
20840 return;
20842 add_AT_loc (die, attr, list2->expr);
20843 return;
20847 /* If that failed to give a single element location list, fall back to
20848 outputting this as a reference... still if permitted. */
20849 if (list == NULL
20850 || (forms & dw_scalar_form_reference) == 0
20851 || placeholder_seen)
20852 return;
20854 if (!decl_die)
20856 if (current_function_decl == 0)
20857 context_die = comp_unit_die ();
20858 else
20859 context_die = lookup_decl_die (current_function_decl);
20861 decl_die = new_die (DW_TAG_variable, context_die, value);
20862 add_AT_flag (decl_die, DW_AT_artificial, 1);
20863 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20864 context_die);
20867 add_AT_location_description (decl_die, DW_AT_location, list);
20868 add_AT_die_ref (die, attr, decl_die);
20871 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20872 default. */
20874 static int
20875 lower_bound_default (void)
20877 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20879 case DW_LANG_C:
20880 case DW_LANG_C89:
20881 case DW_LANG_C99:
20882 case DW_LANG_C11:
20883 case DW_LANG_C_plus_plus:
20884 case DW_LANG_C_plus_plus_11:
20885 case DW_LANG_C_plus_plus_14:
20886 case DW_LANG_ObjC:
20887 case DW_LANG_ObjC_plus_plus:
20888 return 0;
20889 case DW_LANG_Fortran77:
20890 case DW_LANG_Fortran90:
20891 case DW_LANG_Fortran95:
20892 case DW_LANG_Fortran03:
20893 case DW_LANG_Fortran08:
20894 return 1;
20895 case DW_LANG_UPC:
20896 case DW_LANG_D:
20897 case DW_LANG_Python:
20898 return dwarf_version >= 4 ? 0 : -1;
20899 case DW_LANG_Ada95:
20900 case DW_LANG_Ada83:
20901 case DW_LANG_Cobol74:
20902 case DW_LANG_Cobol85:
20903 case DW_LANG_Modula2:
20904 case DW_LANG_PLI:
20905 return dwarf_version >= 4 ? 1 : -1;
20906 default:
20907 return -1;
20911 /* Given a tree node describing an array bound (either lower or upper) output
20912 a representation for that bound. */
20914 static void
20915 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20916 tree bound, struct loc_descr_context *context)
20918 int dflt;
20920 while (1)
20921 switch (TREE_CODE (bound))
20923 /* Strip all conversions. */
20924 CASE_CONVERT:
20925 case VIEW_CONVERT_EXPR:
20926 bound = TREE_OPERAND (bound, 0);
20927 break;
20929 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20930 are even omitted when they are the default. */
20931 case INTEGER_CST:
20932 /* If the value for this bound is the default one, we can even omit the
20933 attribute. */
20934 if (bound_attr == DW_AT_lower_bound
20935 && tree_fits_shwi_p (bound)
20936 && (dflt = lower_bound_default ()) != -1
20937 && tree_to_shwi (bound) == dflt)
20938 return;
20940 /* FALLTHRU */
20942 default:
20943 /* Because of the complex interaction there can be with other GNAT
20944 encodings, GDB isn't ready yet to handle proper DWARF description
20945 for self-referencial subrange bounds: let GNAT encodings do the
20946 magic in such a case. */
20947 if (is_ada ()
20948 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20949 && contains_placeholder_p (bound))
20950 return;
20952 add_scalar_info (subrange_die, bound_attr, bound,
20953 dw_scalar_form_constant
20954 | dw_scalar_form_exprloc
20955 | dw_scalar_form_reference,
20956 context);
20957 return;
20961 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20962 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20963 Note that the block of subscript information for an array type also
20964 includes information about the element type of the given array type.
20966 This function reuses previously set type and bound information if
20967 available. */
20969 static void
20970 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20972 unsigned dimension_number;
20973 tree lower, upper;
20974 dw_die_ref child = type_die->die_child;
20976 for (dimension_number = 0;
20977 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20978 type = TREE_TYPE (type), dimension_number++)
20980 tree domain = TYPE_DOMAIN (type);
20982 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20983 break;
20985 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20986 and (in GNU C only) variable bounds. Handle all three forms
20987 here. */
20989 /* Find and reuse a previously generated DW_TAG_subrange_type if
20990 available.
20992 For multi-dimensional arrays, as we iterate through the
20993 various dimensions in the enclosing for loop above, we also
20994 iterate through the DIE children and pick at each
20995 DW_TAG_subrange_type previously generated (if available).
20996 Each child DW_TAG_subrange_type DIE describes the range of
20997 the current dimension. At this point we should have as many
20998 DW_TAG_subrange_type's as we have dimensions in the
20999 array. */
21000 dw_die_ref subrange_die = NULL;
21001 if (child)
21002 while (1)
21004 child = child->die_sib;
21005 if (child->die_tag == DW_TAG_subrange_type)
21006 subrange_die = child;
21007 if (child == type_die->die_child)
21009 /* If we wrapped around, stop looking next time. */
21010 child = NULL;
21011 break;
21013 if (child->die_tag == DW_TAG_subrange_type)
21014 break;
21016 if (!subrange_die)
21017 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21019 if (domain)
21021 /* We have an array type with specified bounds. */
21022 lower = TYPE_MIN_VALUE (domain);
21023 upper = TYPE_MAX_VALUE (domain);
21025 /* Define the index type. */
21026 if (TREE_TYPE (domain)
21027 && !get_AT (subrange_die, DW_AT_type))
21029 /* ??? This is probably an Ada unnamed subrange type. Ignore the
21030 TREE_TYPE field. We can't emit debug info for this
21031 because it is an unnamed integral type. */
21032 if (TREE_CODE (domain) == INTEGER_TYPE
21033 && TYPE_NAME (domain) == NULL_TREE
21034 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
21035 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
21037 else
21038 add_type_attribute (subrange_die, TREE_TYPE (domain),
21039 TYPE_UNQUALIFIED, false, type_die);
21042 /* ??? If upper is NULL, the array has unspecified length,
21043 but it does have a lower bound. This happens with Fortran
21044 dimension arr(N:*)
21045 Since the debugger is definitely going to need to know N
21046 to produce useful results, go ahead and output the lower
21047 bound solo, and hope the debugger can cope. */
21049 if (!get_AT (subrange_die, DW_AT_lower_bound))
21050 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21051 if (!get_AT (subrange_die, DW_AT_upper_bound)
21052 && !get_AT (subrange_die, DW_AT_count))
21054 if (upper)
21055 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21056 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21057 /* Zero-length array. */
21058 add_bound_info (subrange_die, DW_AT_count,
21059 build_int_cst (TREE_TYPE (lower), 0), NULL);
21063 /* Otherwise we have an array type with an unspecified length. The
21064 DWARF-2 spec does not say how to handle this; let's just leave out the
21065 bounds. */
21069 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21071 static void
21072 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21074 dw_die_ref decl_die;
21075 HOST_WIDE_INT size;
21076 dw_loc_descr_ref size_expr = NULL;
21078 switch (TREE_CODE (tree_node))
21080 case ERROR_MARK:
21081 size = 0;
21082 break;
21083 case ENUMERAL_TYPE:
21084 case RECORD_TYPE:
21085 case UNION_TYPE:
21086 case QUAL_UNION_TYPE:
21087 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21088 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21090 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21091 return;
21093 size_expr = type_byte_size (tree_node, &size);
21094 break;
21095 case FIELD_DECL:
21096 /* For a data member of a struct or union, the DW_AT_byte_size is
21097 generally given as the number of bytes normally allocated for an
21098 object of the *declared* type of the member itself. This is true
21099 even for bit-fields. */
21100 size = int_size_in_bytes (field_type (tree_node));
21101 break;
21102 default:
21103 gcc_unreachable ();
21106 /* Support for dynamically-sized objects was introduced by DWARFv3.
21107 At the moment, GDB does not handle variable byte sizes very well,
21108 though. */
21109 if ((dwarf_version >= 3 || !dwarf_strict)
21110 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
21111 && size_expr != NULL)
21112 add_AT_loc (die, DW_AT_byte_size, size_expr);
21114 /* Note that `size' might be -1 when we get to this point. If it is, that
21115 indicates that the byte size of the entity in question is variable and
21116 that we could not generate a DWARF expression that computes it. */
21117 if (size >= 0)
21118 add_AT_unsigned (die, DW_AT_byte_size, size);
21121 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21122 alignment. */
21124 static void
21125 add_alignment_attribute (dw_die_ref die, tree tree_node)
21127 if (dwarf_version < 5 && dwarf_strict)
21128 return;
21130 unsigned align;
21132 if (DECL_P (tree_node))
21134 if (!DECL_USER_ALIGN (tree_node))
21135 return;
21137 align = DECL_ALIGN_UNIT (tree_node);
21139 else if (TYPE_P (tree_node))
21141 if (!TYPE_USER_ALIGN (tree_node))
21142 return;
21144 align = TYPE_ALIGN_UNIT (tree_node);
21146 else
21147 gcc_unreachable ();
21149 add_AT_unsigned (die, DW_AT_alignment, align);
21152 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21153 which specifies the distance in bits from the highest order bit of the
21154 "containing object" for the bit-field to the highest order bit of the
21155 bit-field itself.
21157 For any given bit-field, the "containing object" is a hypothetical object
21158 (of some integral or enum type) within which the given bit-field lives. The
21159 type of this hypothetical "containing object" is always the same as the
21160 declared type of the individual bit-field itself. The determination of the
21161 exact location of the "containing object" for a bit-field is rather
21162 complicated. It's handled by the `field_byte_offset' function (above).
21164 Note that it is the size (in bytes) of the hypothetical "containing object"
21165 which will be given in the DW_AT_byte_size attribute for this bit-field.
21166 (See `byte_size_attribute' above). */
21168 static inline void
21169 add_bit_offset_attribute (dw_die_ref die, tree decl)
21171 HOST_WIDE_INT object_offset_in_bytes;
21172 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21173 HOST_WIDE_INT bitpos_int;
21174 HOST_WIDE_INT highest_order_object_bit_offset;
21175 HOST_WIDE_INT highest_order_field_bit_offset;
21176 HOST_WIDE_INT bit_offset;
21178 /* The containing object is within the DECL_CONTEXT. */
21179 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21181 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21183 /* Must be a field and a bit field. */
21184 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21186 /* We can't yet handle bit-fields whose offsets are variable, so if we
21187 encounter such things, just return without generating any attribute
21188 whatsoever. Likewise for variable or too large size. */
21189 if (! tree_fits_shwi_p (bit_position (decl))
21190 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21191 return;
21193 bitpos_int = int_bit_position (decl);
21195 /* Note that the bit offset is always the distance (in bits) from the
21196 highest-order bit of the "containing object" to the highest-order bit of
21197 the bit-field itself. Since the "high-order end" of any object or field
21198 is different on big-endian and little-endian machines, the computation
21199 below must take account of these differences. */
21200 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21201 highest_order_field_bit_offset = bitpos_int;
21203 if (! BYTES_BIG_ENDIAN)
21205 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21206 highest_order_object_bit_offset +=
21207 simple_type_size_in_bits (original_type);
21210 bit_offset
21211 = (! BYTES_BIG_ENDIAN
21212 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21213 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21215 if (bit_offset < 0)
21216 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21217 else
21218 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21221 /* For a FIELD_DECL node which represents a bit field, output an attribute
21222 which specifies the length in bits of the given field. */
21224 static inline void
21225 add_bit_size_attribute (dw_die_ref die, tree decl)
21227 /* Must be a field and a bit field. */
21228 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21229 && DECL_BIT_FIELD_TYPE (decl));
21231 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21232 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21235 /* If the compiled language is ANSI C, then add a 'prototyped'
21236 attribute, if arg types are given for the parameters of a function. */
21238 static inline void
21239 add_prototyped_attribute (dw_die_ref die, tree func_type)
21241 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21243 case DW_LANG_C:
21244 case DW_LANG_C89:
21245 case DW_LANG_C99:
21246 case DW_LANG_C11:
21247 case DW_LANG_ObjC:
21248 if (prototype_p (func_type))
21249 add_AT_flag (die, DW_AT_prototyped, 1);
21250 break;
21251 default:
21252 break;
21256 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21257 by looking in the type declaration, the object declaration equate table or
21258 the block mapping. */
21260 static inline void
21261 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21263 dw_die_ref origin_die = NULL;
21265 /* For late LTO debug output we want to refer directly to the abstract
21266 DIE in the early debug rather to the possibly existing concrete
21267 instance and avoid creating that just for this purpose. */
21268 sym_off_pair *desc;
21269 if (in_lto_p
21270 && external_die_map
21271 && (desc = external_die_map->get (origin)))
21273 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21274 desc->sym, desc->off);
21275 return;
21278 if (DECL_P (origin))
21279 origin_die = lookup_decl_die (origin);
21280 else if (TYPE_P (origin))
21281 origin_die = lookup_type_die (origin);
21282 else if (TREE_CODE (origin) == BLOCK)
21283 origin_die = lookup_block_die (origin);
21285 /* XXX: Functions that are never lowered don't always have correct block
21286 trees (in the case of java, they simply have no block tree, in some other
21287 languages). For these functions, there is nothing we can really do to
21288 output correct debug info for inlined functions in all cases. Rather
21289 than die, we'll just produce deficient debug info now, in that we will
21290 have variables without a proper abstract origin. In the future, when all
21291 functions are lowered, we should re-add a gcc_assert (origin_die)
21292 here. */
21294 if (origin_die)
21295 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21298 /* We do not currently support the pure_virtual attribute. */
21300 static inline void
21301 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21303 if (DECL_VINDEX (func_decl))
21305 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21307 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21308 add_AT_loc (die, DW_AT_vtable_elem_location,
21309 new_loc_descr (DW_OP_constu,
21310 tree_to_shwi (DECL_VINDEX (func_decl)),
21311 0));
21313 /* GNU extension: Record what type this method came from originally. */
21314 if (debug_info_level > DINFO_LEVEL_TERSE
21315 && DECL_CONTEXT (func_decl))
21316 add_AT_die_ref (die, DW_AT_containing_type,
21317 lookup_type_die (DECL_CONTEXT (func_decl)));
21321 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21322 given decl. This used to be a vendor extension until after DWARF 4
21323 standardized it. */
21325 static void
21326 add_linkage_attr (dw_die_ref die, tree decl)
21328 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21330 /* Mimic what assemble_name_raw does with a leading '*'. */
21331 if (name[0] == '*')
21332 name = &name[1];
21334 if (dwarf_version >= 4)
21335 add_AT_string (die, DW_AT_linkage_name, name);
21336 else
21337 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21340 /* Add source coordinate attributes for the given decl. */
21342 static void
21343 add_src_coords_attributes (dw_die_ref die, tree decl)
21345 expanded_location s;
21347 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21348 return;
21349 s = expand_location (DECL_SOURCE_LOCATION (decl));
21350 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21351 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21352 if (debug_column_info && s.column)
21353 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21356 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21358 static void
21359 add_linkage_name_raw (dw_die_ref die, tree decl)
21361 /* Defer until we have an assembler name set. */
21362 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21364 limbo_die_node *asm_name;
21366 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21367 asm_name->die = die;
21368 asm_name->created_for = decl;
21369 asm_name->next = deferred_asm_name;
21370 deferred_asm_name = asm_name;
21372 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21373 add_linkage_attr (die, decl);
21376 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21378 static void
21379 add_linkage_name (dw_die_ref die, tree decl)
21381 if (debug_info_level > DINFO_LEVEL_NONE
21382 && VAR_OR_FUNCTION_DECL_P (decl)
21383 && TREE_PUBLIC (decl)
21384 && !(VAR_P (decl) && DECL_REGISTER (decl))
21385 && die->die_tag != DW_TAG_member)
21386 add_linkage_name_raw (die, decl);
21389 /* Add a DW_AT_name attribute and source coordinate attribute for the
21390 given decl, but only if it actually has a name. */
21392 static void
21393 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21394 bool no_linkage_name)
21396 tree decl_name;
21398 decl_name = DECL_NAME (decl);
21399 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21401 const char *name = dwarf2_name (decl, 0);
21402 if (name)
21403 add_name_attribute (die, name);
21404 else
21405 add_desc_attribute (die, decl);
21407 if (! DECL_ARTIFICIAL (decl))
21408 add_src_coords_attributes (die, decl);
21410 if (!no_linkage_name)
21411 add_linkage_name (die, decl);
21413 else
21414 add_desc_attribute (die, decl);
21416 #ifdef VMS_DEBUGGING_INFO
21417 /* Get the function's name, as described by its RTL. This may be different
21418 from the DECL_NAME name used in the source file. */
21419 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21421 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21422 XEXP (DECL_RTL (decl), 0), false);
21423 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21425 #endif /* VMS_DEBUGGING_INFO */
21428 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21430 static void
21431 add_discr_value (dw_die_ref die, dw_discr_value *value)
21433 dw_attr_node attr;
21435 attr.dw_attr = DW_AT_discr_value;
21436 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21437 attr.dw_attr_val.val_entry = NULL;
21438 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21439 if (value->pos)
21440 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21441 else
21442 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21443 add_dwarf_attr (die, &attr);
21446 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21448 static void
21449 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21451 dw_attr_node attr;
21453 attr.dw_attr = DW_AT_discr_list;
21454 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21455 attr.dw_attr_val.val_entry = NULL;
21456 attr.dw_attr_val.v.val_discr_list = discr_list;
21457 add_dwarf_attr (die, &attr);
21460 static inline dw_discr_list_ref
21461 AT_discr_list (dw_attr_node *attr)
21463 return attr->dw_attr_val.v.val_discr_list;
21466 #ifdef VMS_DEBUGGING_INFO
21467 /* Output the debug main pointer die for VMS */
21469 void
21470 dwarf2out_vms_debug_main_pointer (void)
21472 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21473 dw_die_ref die;
21475 /* Allocate the VMS debug main subprogram die. */
21476 die = new_die_raw (DW_TAG_subprogram);
21477 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21478 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21479 current_function_funcdef_no);
21480 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21482 /* Make it the first child of comp_unit_die (). */
21483 die->die_parent = comp_unit_die ();
21484 if (comp_unit_die ()->die_child)
21486 die->die_sib = comp_unit_die ()->die_child->die_sib;
21487 comp_unit_die ()->die_child->die_sib = die;
21489 else
21491 die->die_sib = die;
21492 comp_unit_die ()->die_child = die;
21495 #endif /* VMS_DEBUGGING_INFO */
21497 /* walk_tree helper function for uses_local_type, below. */
21499 static tree
21500 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21502 if (!TYPE_P (*tp))
21503 *walk_subtrees = 0;
21504 else
21506 tree name = TYPE_NAME (*tp);
21507 if (name && DECL_P (name) && decl_function_context (name))
21508 return *tp;
21510 return NULL_TREE;
21513 /* If TYPE involves a function-local type (including a local typedef to a
21514 non-local type), returns that type; otherwise returns NULL_TREE. */
21516 static tree
21517 uses_local_type (tree type)
21519 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21520 return used;
21523 /* Return the DIE for the scope that immediately contains this type.
21524 Non-named types that do not involve a function-local type get global
21525 scope. Named types nested in namespaces or other types get their
21526 containing scope. All other types (i.e. function-local named types) get
21527 the current active scope. */
21529 static dw_die_ref
21530 scope_die_for (tree t, dw_die_ref context_die)
21532 dw_die_ref scope_die = NULL;
21533 tree containing_scope;
21535 /* Non-types always go in the current scope. */
21536 gcc_assert (TYPE_P (t));
21538 /* Use the scope of the typedef, rather than the scope of the type
21539 it refers to. */
21540 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
21541 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
21542 else
21543 containing_scope = TYPE_CONTEXT (t);
21545 /* Use the containing namespace if there is one. */
21546 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
21548 if (context_die == lookup_decl_die (containing_scope))
21549 /* OK */;
21550 else if (debug_info_level > DINFO_LEVEL_TERSE)
21551 context_die = get_context_die (containing_scope);
21552 else
21553 containing_scope = NULL_TREE;
21556 /* Ignore function type "scopes" from the C frontend. They mean that
21557 a tagged type is local to a parmlist of a function declarator, but
21558 that isn't useful to DWARF. */
21559 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
21560 containing_scope = NULL_TREE;
21562 if (SCOPE_FILE_SCOPE_P (containing_scope))
21564 /* If T uses a local type keep it local as well, to avoid references
21565 to function-local DIEs from outside the function. */
21566 if (current_function_decl && uses_local_type (t))
21567 scope_die = context_die;
21568 else
21569 scope_die = comp_unit_die ();
21571 else if (TYPE_P (containing_scope))
21573 /* For types, we can just look up the appropriate DIE. */
21574 if (debug_info_level > DINFO_LEVEL_TERSE)
21575 scope_die = get_context_die (containing_scope);
21576 else
21578 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
21579 if (scope_die == NULL)
21580 scope_die = comp_unit_die ();
21583 else
21584 scope_die = context_die;
21586 return scope_die;
21589 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
21591 static inline int
21592 local_scope_p (dw_die_ref context_die)
21594 for (; context_die; context_die = context_die->die_parent)
21595 if (context_die->die_tag == DW_TAG_inlined_subroutine
21596 || context_die->die_tag == DW_TAG_subprogram)
21597 return 1;
21599 return 0;
21602 /* Returns nonzero if CONTEXT_DIE is a class. */
21604 static inline int
21605 class_scope_p (dw_die_ref context_die)
21607 return (context_die
21608 && (context_die->die_tag == DW_TAG_structure_type
21609 || context_die->die_tag == DW_TAG_class_type
21610 || context_die->die_tag == DW_TAG_interface_type
21611 || context_die->die_tag == DW_TAG_union_type));
21614 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
21615 whether or not to treat a DIE in this context as a declaration. */
21617 static inline int
21618 class_or_namespace_scope_p (dw_die_ref context_die)
21620 return (class_scope_p (context_die)
21621 || (context_die && context_die->die_tag == DW_TAG_namespace));
21624 /* Many forms of DIEs require a "type description" attribute. This
21625 routine locates the proper "type descriptor" die for the type given
21626 by 'type' plus any additional qualifiers given by 'cv_quals', and
21627 adds a DW_AT_type attribute below the given die. */
21629 static void
21630 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
21631 bool reverse, dw_die_ref context_die)
21633 enum tree_code code = TREE_CODE (type);
21634 dw_die_ref type_die = NULL;
21636 if (debug_info_level <= DINFO_LEVEL_TERSE)
21637 return;
21639 /* ??? If this type is an unnamed subrange type of an integral, floating-point
21640 or fixed-point type, use the inner type. This is because we have no
21641 support for unnamed types in base_type_die. This can happen if this is
21642 an Ada subrange type. Correct solution is emit a subrange type die. */
21643 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
21644 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
21645 type = TREE_TYPE (type), code = TREE_CODE (type);
21647 if (code == ERROR_MARK
21648 /* Handle a special case. For functions whose return type is void, we
21649 generate *no* type attribute. (Note that no object may have type
21650 `void', so this only applies to function return types). */
21651 || code == VOID_TYPE)
21652 return;
21654 type_die = modified_type_die (type,
21655 cv_quals | TYPE_QUALS (type),
21656 reverse,
21657 context_die);
21659 if (type_die != NULL)
21660 add_AT_die_ref (object_die, DW_AT_type, type_die);
21663 /* Given an object die, add the calling convention attribute for the
21664 function call type. */
21665 static void
21666 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
21668 enum dwarf_calling_convention value = DW_CC_normal;
21670 value = ((enum dwarf_calling_convention)
21671 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
21673 if (is_fortran ()
21674 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
21676 /* DWARF 2 doesn't provide a way to identify a program's source-level
21677 entry point. DW_AT_calling_convention attributes are only meant
21678 to describe functions' calling conventions. However, lacking a
21679 better way to signal the Fortran main program, we used this for
21680 a long time, following existing custom. Now, DWARF 4 has
21681 DW_AT_main_subprogram, which we add below, but some tools still
21682 rely on the old way, which we thus keep. */
21683 value = DW_CC_program;
21685 if (dwarf_version >= 4 || !dwarf_strict)
21686 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
21689 /* Only add the attribute if the backend requests it, and
21690 is not DW_CC_normal. */
21691 if (value && (value != DW_CC_normal))
21692 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
21695 /* Given a tree pointer to a struct, class, union, or enum type node, return
21696 a pointer to the (string) tag name for the given type, or zero if the type
21697 was declared without a tag. */
21699 static const char *
21700 type_tag (const_tree type)
21702 const char *name = 0;
21704 if (TYPE_NAME (type) != 0)
21706 tree t = 0;
21708 /* Find the IDENTIFIER_NODE for the type name. */
21709 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
21710 && !TYPE_NAMELESS (type))
21711 t = TYPE_NAME (type);
21713 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
21714 a TYPE_DECL node, regardless of whether or not a `typedef' was
21715 involved. */
21716 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21717 && ! DECL_IGNORED_P (TYPE_NAME (type)))
21719 /* We want to be extra verbose. Don't call dwarf_name if
21720 DECL_NAME isn't set. The default hook for decl_printable_name
21721 doesn't like that, and in this context it's correct to return
21722 0, instead of "<anonymous>" or the like. */
21723 if (DECL_NAME (TYPE_NAME (type))
21724 && !DECL_NAMELESS (TYPE_NAME (type)))
21725 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
21728 /* Now get the name as a string, or invent one. */
21729 if (!name && t != 0)
21730 name = IDENTIFIER_POINTER (t);
21733 return (name == 0 || *name == '\0') ? 0 : name;
21736 /* Return the type associated with a data member, make a special check
21737 for bit field types. */
21739 static inline tree
21740 member_declared_type (const_tree member)
21742 return (DECL_BIT_FIELD_TYPE (member)
21743 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
21746 /* Get the decl's label, as described by its RTL. This may be different
21747 from the DECL_NAME name used in the source file. */
21749 #if 0
21750 static const char *
21751 decl_start_label (tree decl)
21753 rtx x;
21754 const char *fnname;
21756 x = DECL_RTL (decl);
21757 gcc_assert (MEM_P (x));
21759 x = XEXP (x, 0);
21760 gcc_assert (GET_CODE (x) == SYMBOL_REF);
21762 fnname = XSTR (x, 0);
21763 return fnname;
21765 #endif
21767 /* For variable-length arrays that have been previously generated, but
21768 may be incomplete due to missing subscript info, fill the subscript
21769 info. Return TRUE if this is one of those cases. */
21770 static bool
21771 fill_variable_array_bounds (tree type)
21773 if (TREE_ASM_WRITTEN (type)
21774 && TREE_CODE (type) == ARRAY_TYPE
21775 && variably_modified_type_p (type, NULL))
21777 dw_die_ref array_die = lookup_type_die (type);
21778 if (!array_die)
21779 return false;
21780 add_subscript_info (array_die, type, !is_ada ());
21781 return true;
21783 return false;
21786 /* These routines generate the internal representation of the DIE's for
21787 the compilation unit. Debugging information is collected by walking
21788 the declaration trees passed in from dwarf2out_decl(). */
21790 static void
21791 gen_array_type_die (tree type, dw_die_ref context_die)
21793 dw_die_ref array_die;
21795 /* GNU compilers represent multidimensional array types as sequences of one
21796 dimensional array types whose element types are themselves array types.
21797 We sometimes squish that down to a single array_type DIE with multiple
21798 subscripts in the Dwarf debugging info. The draft Dwarf specification
21799 say that we are allowed to do this kind of compression in C, because
21800 there is no difference between an array of arrays and a multidimensional
21801 array. We don't do this for Ada to remain as close as possible to the
21802 actual representation, which is especially important against the language
21803 flexibilty wrt arrays of variable size. */
21805 bool collapse_nested_arrays = !is_ada ();
21807 if (fill_variable_array_bounds (type))
21808 return;
21810 dw_die_ref scope_die = scope_die_for (type, context_die);
21811 tree element_type;
21813 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
21814 DW_TAG_string_type doesn't have DW_AT_type attribute). */
21815 if (TREE_CODE (type) == ARRAY_TYPE
21816 && TYPE_STRING_FLAG (type)
21817 && is_fortran ()
21818 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21820 HOST_WIDE_INT size;
21822 array_die = new_die (DW_TAG_string_type, scope_die, type);
21823 add_name_attribute (array_die, type_tag (type));
21824 equate_type_number_to_die (type, array_die);
21825 size = int_size_in_bytes (type);
21826 if (size >= 0)
21827 add_AT_unsigned (array_die, DW_AT_byte_size, size);
21828 /* ??? We can't annotate types late, but for LTO we may not
21829 generate a location early either (gfortran.dg/save_6.f90). */
21830 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
21831 && TYPE_DOMAIN (type) != NULL_TREE
21832 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21834 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21835 tree rszdecl = szdecl;
21837 size = int_size_in_bytes (TREE_TYPE (szdecl));
21838 if (!DECL_P (szdecl))
21840 if (TREE_CODE (szdecl) == INDIRECT_REF
21841 && DECL_P (TREE_OPERAND (szdecl, 0)))
21843 rszdecl = TREE_OPERAND (szdecl, 0);
21844 if (int_size_in_bytes (TREE_TYPE (rszdecl))
21845 != DWARF2_ADDR_SIZE)
21846 size = 0;
21848 else
21849 size = 0;
21851 if (size > 0)
21853 dw_loc_list_ref loc
21854 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21855 NULL);
21856 if (loc)
21858 add_AT_location_description (array_die, DW_AT_string_length,
21859 loc);
21860 if (size != DWARF2_ADDR_SIZE)
21861 add_AT_unsigned (array_die, dwarf_version >= 5
21862 ? DW_AT_string_length_byte_size
21863 : DW_AT_byte_size, size);
21867 return;
21870 array_die = new_die (DW_TAG_array_type, scope_die, type);
21871 add_name_attribute (array_die, type_tag (type));
21872 equate_type_number_to_die (type, array_die);
21874 if (TREE_CODE (type) == VECTOR_TYPE)
21875 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21877 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
21878 if (is_fortran ()
21879 && TREE_CODE (type) == ARRAY_TYPE
21880 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21881 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21882 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21884 #if 0
21885 /* We default the array ordering. Debuggers will probably do the right
21886 things even if DW_AT_ordering is not present. It's not even an issue
21887 until we start to get into multidimensional arrays anyway. If a debugger
21888 is ever caught doing the Wrong Thing for multi-dimensional arrays,
21889 then we'll have to put the DW_AT_ordering attribute back in. (But if
21890 and when we find out that we need to put these in, we will only do so
21891 for multidimensional arrays. */
21892 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21893 #endif
21895 if (TREE_CODE (type) == VECTOR_TYPE)
21897 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21898 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21899 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21900 add_bound_info (subrange_die, DW_AT_upper_bound,
21901 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21903 else
21904 add_subscript_info (array_die, type, collapse_nested_arrays);
21906 /* Add representation of the type of the elements of this array type and
21907 emit the corresponding DIE if we haven't done it already. */
21908 element_type = TREE_TYPE (type);
21909 if (collapse_nested_arrays)
21910 while (TREE_CODE (element_type) == ARRAY_TYPE)
21912 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21913 break;
21914 element_type = TREE_TYPE (element_type);
21917 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21918 TREE_CODE (type) == ARRAY_TYPE
21919 && TYPE_REVERSE_STORAGE_ORDER (type),
21920 context_die);
21922 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21923 if (TYPE_ARTIFICIAL (type))
21924 add_AT_flag (array_die, DW_AT_artificial, 1);
21926 if (get_AT (array_die, DW_AT_name))
21927 add_pubtype (type, array_die);
21929 add_alignment_attribute (array_die, type);
21932 /* This routine generates DIE for array with hidden descriptor, details
21933 are filled into *info by a langhook. */
21935 static void
21936 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21937 dw_die_ref context_die)
21939 const dw_die_ref scope_die = scope_die_for (type, context_die);
21940 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21941 struct loc_descr_context context = { type, info->base_decl, NULL,
21942 false, false };
21943 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21944 int dim;
21946 add_name_attribute (array_die, type_tag (type));
21947 equate_type_number_to_die (type, array_die);
21949 if (info->ndimensions > 1)
21950 switch (info->ordering)
21952 case array_descr_ordering_row_major:
21953 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21954 break;
21955 case array_descr_ordering_column_major:
21956 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21957 break;
21958 default:
21959 break;
21962 if (dwarf_version >= 3 || !dwarf_strict)
21964 if (info->data_location)
21965 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21966 dw_scalar_form_exprloc, &context);
21967 if (info->associated)
21968 add_scalar_info (array_die, DW_AT_associated, info->associated,
21969 dw_scalar_form_constant
21970 | dw_scalar_form_exprloc
21971 | dw_scalar_form_reference, &context);
21972 if (info->allocated)
21973 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21974 dw_scalar_form_constant
21975 | dw_scalar_form_exprloc
21976 | dw_scalar_form_reference, &context);
21977 if (info->stride)
21979 const enum dwarf_attribute attr
21980 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21981 const int forms
21982 = (info->stride_in_bits)
21983 ? dw_scalar_form_constant
21984 : (dw_scalar_form_constant
21985 | dw_scalar_form_exprloc
21986 | dw_scalar_form_reference);
21988 add_scalar_info (array_die, attr, info->stride, forms, &context);
21991 if (dwarf_version >= 5)
21993 if (info->rank)
21995 add_scalar_info (array_die, DW_AT_rank, info->rank,
21996 dw_scalar_form_constant
21997 | dw_scalar_form_exprloc, &context);
21998 subrange_tag = DW_TAG_generic_subrange;
21999 context.placeholder_arg = true;
22003 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22005 for (dim = 0; dim < info->ndimensions; dim++)
22007 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22009 if (info->dimen[dim].bounds_type)
22010 add_type_attribute (subrange_die,
22011 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22012 false, context_die);
22013 if (info->dimen[dim].lower_bound)
22014 add_bound_info (subrange_die, DW_AT_lower_bound,
22015 info->dimen[dim].lower_bound, &context);
22016 if (info->dimen[dim].upper_bound)
22017 add_bound_info (subrange_die, DW_AT_upper_bound,
22018 info->dimen[dim].upper_bound, &context);
22019 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22020 add_scalar_info (subrange_die, DW_AT_byte_stride,
22021 info->dimen[dim].stride,
22022 dw_scalar_form_constant
22023 | dw_scalar_form_exprloc
22024 | dw_scalar_form_reference,
22025 &context);
22028 gen_type_die (info->element_type, context_die);
22029 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22030 TREE_CODE (type) == ARRAY_TYPE
22031 && TYPE_REVERSE_STORAGE_ORDER (type),
22032 context_die);
22034 if (get_AT (array_die, DW_AT_name))
22035 add_pubtype (type, array_die);
22037 add_alignment_attribute (array_die, type);
22040 #if 0
22041 static void
22042 gen_entry_point_die (tree decl, dw_die_ref context_die)
22044 tree origin = decl_ultimate_origin (decl);
22045 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22047 if (origin != NULL)
22048 add_abstract_origin_attribute (decl_die, origin);
22049 else
22051 add_name_and_src_coords_attributes (decl_die, decl);
22052 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22053 TYPE_UNQUALIFIED, false, context_die);
22056 if (DECL_ABSTRACT_P (decl))
22057 equate_decl_number_to_die (decl, decl_die);
22058 else
22059 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22061 #endif
22063 /* Walk through the list of incomplete types again, trying once more to
22064 emit full debugging info for them. */
22066 static void
22067 retry_incomplete_types (void)
22069 set_early_dwarf s;
22070 int i;
22072 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22073 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22074 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22075 vec_safe_truncate (incomplete_types, 0);
22078 /* Determine what tag to use for a record type. */
22080 static enum dwarf_tag
22081 record_type_tag (tree type)
22083 if (! lang_hooks.types.classify_record)
22084 return DW_TAG_structure_type;
22086 switch (lang_hooks.types.classify_record (type))
22088 case RECORD_IS_STRUCT:
22089 return DW_TAG_structure_type;
22091 case RECORD_IS_CLASS:
22092 return DW_TAG_class_type;
22094 case RECORD_IS_INTERFACE:
22095 if (dwarf_version >= 3 || !dwarf_strict)
22096 return DW_TAG_interface_type;
22097 return DW_TAG_structure_type;
22099 default:
22100 gcc_unreachable ();
22104 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22105 include all of the information about the enumeration values also. Each
22106 enumerated type name/value is listed as a child of the enumerated type
22107 DIE. */
22109 static dw_die_ref
22110 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22112 dw_die_ref type_die = lookup_type_die (type);
22113 dw_die_ref orig_type_die = type_die;
22115 if (type_die == NULL)
22117 type_die = new_die (DW_TAG_enumeration_type,
22118 scope_die_for (type, context_die), type);
22119 equate_type_number_to_die (type, type_die);
22120 add_name_attribute (type_die, type_tag (type));
22121 if ((dwarf_version >= 4 || !dwarf_strict)
22122 && ENUM_IS_SCOPED (type))
22123 add_AT_flag (type_die, DW_AT_enum_class, 1);
22124 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22125 add_AT_flag (type_die, DW_AT_declaration, 1);
22126 if (!dwarf_strict)
22127 add_AT_unsigned (type_die, DW_AT_encoding,
22128 TYPE_UNSIGNED (type)
22129 ? DW_ATE_unsigned
22130 : DW_ATE_signed);
22132 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22133 return type_die;
22134 else
22135 remove_AT (type_die, DW_AT_declaration);
22137 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22138 given enum type is incomplete, do not generate the DW_AT_byte_size
22139 attribute or the DW_AT_element_list attribute. */
22140 if (TYPE_SIZE (type))
22142 tree link;
22144 if (!ENUM_IS_OPAQUE (type))
22145 TREE_ASM_WRITTEN (type) = 1;
22146 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22147 add_byte_size_attribute (type_die, type);
22148 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22149 add_alignment_attribute (type_die, type);
22150 if ((dwarf_version >= 3 || !dwarf_strict)
22151 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22153 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22154 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22155 context_die);
22157 if (TYPE_STUB_DECL (type) != NULL_TREE)
22159 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22160 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22161 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22162 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22165 /* If the first reference to this type was as the return type of an
22166 inline function, then it may not have a parent. Fix this now. */
22167 if (type_die->die_parent == NULL)
22168 add_child_die (scope_die_for (type, context_die), type_die);
22170 for (link = TYPE_VALUES (type);
22171 link != NULL; link = TREE_CHAIN (link))
22173 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22174 tree value = TREE_VALUE (link);
22176 gcc_assert (!ENUM_IS_OPAQUE (type));
22177 add_name_attribute (enum_die,
22178 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22180 if (TREE_CODE (value) == CONST_DECL)
22181 value = DECL_INITIAL (value);
22183 if (simple_type_size_in_bits (TREE_TYPE (value))
22184 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22186 /* For constant forms created by add_AT_unsigned DWARF
22187 consumers (GDB, elfutils, etc.) always zero extend
22188 the value. Only when the actual value is negative
22189 do we need to use add_AT_int to generate a constant
22190 form that can represent negative values. */
22191 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22192 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22193 add_AT_unsigned (enum_die, DW_AT_const_value,
22194 (unsigned HOST_WIDE_INT) val);
22195 else
22196 add_AT_int (enum_die, DW_AT_const_value, val);
22198 else
22199 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22200 that here. TODO: This should be re-worked to use correct
22201 signed/unsigned double tags for all cases. */
22202 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22205 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22206 if (TYPE_ARTIFICIAL (type)
22207 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22208 add_AT_flag (type_die, DW_AT_artificial, 1);
22210 else
22211 add_AT_flag (type_die, DW_AT_declaration, 1);
22213 add_pubtype (type, type_die);
22215 return type_die;
22218 /* Generate a DIE to represent either a real live formal parameter decl or to
22219 represent just the type of some formal parameter position in some function
22220 type.
22222 Note that this routine is a bit unusual because its argument may be a
22223 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22224 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22225 node. If it's the former then this function is being called to output a
22226 DIE to represent a formal parameter object (or some inlining thereof). If
22227 it's the latter, then this function is only being called to output a
22228 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22229 argument type of some subprogram type.
22230 If EMIT_NAME_P is true, name and source coordinate attributes
22231 are emitted. */
22233 static dw_die_ref
22234 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22235 dw_die_ref context_die)
22237 tree node_or_origin = node ? node : origin;
22238 tree ultimate_origin;
22239 dw_die_ref parm_die = NULL;
22241 if (DECL_P (node_or_origin))
22243 parm_die = lookup_decl_die (node);
22245 /* If the contexts differ, we may not be talking about the same
22246 thing.
22247 ??? When in LTO the DIE parent is the "abstract" copy and the
22248 context_die is the specification "copy". */
22249 if (parm_die
22250 && parm_die->die_parent != context_die
22251 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22252 || parm_die->die_parent->die_parent != context_die)
22253 && !in_lto_p)
22255 gcc_assert (!DECL_ABSTRACT_P (node));
22256 /* This can happen when creating a concrete instance, in
22257 which case we need to create a new DIE that will get
22258 annotated with DW_AT_abstract_origin. */
22259 parm_die = NULL;
22262 if (parm_die && parm_die->die_parent == NULL)
22264 /* Check that parm_die already has the right attributes that
22265 we would have added below. If any attributes are
22266 missing, fall through to add them. */
22267 if (! DECL_ABSTRACT_P (node_or_origin)
22268 && !get_AT (parm_die, DW_AT_location)
22269 && !get_AT (parm_die, DW_AT_const_value))
22270 /* We are missing location info, and are about to add it. */
22272 else
22274 add_child_die (context_die, parm_die);
22275 return parm_die;
22280 /* If we have a previously generated DIE, use it, unless this is an
22281 concrete instance (origin != NULL), in which case we need a new
22282 DIE with a corresponding DW_AT_abstract_origin. */
22283 bool reusing_die;
22284 if (parm_die && origin == NULL)
22285 reusing_die = true;
22286 else
22288 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22289 reusing_die = false;
22292 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22294 case tcc_declaration:
22295 ultimate_origin = decl_ultimate_origin (node_or_origin);
22296 if (node || ultimate_origin)
22297 origin = ultimate_origin;
22299 if (reusing_die)
22300 goto add_location;
22302 if (origin != NULL)
22303 add_abstract_origin_attribute (parm_die, origin);
22304 else if (emit_name_p)
22305 add_name_and_src_coords_attributes (parm_die, node);
22306 if (origin == NULL
22307 || (! DECL_ABSTRACT_P (node_or_origin)
22308 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22309 decl_function_context
22310 (node_or_origin))))
22312 tree type = TREE_TYPE (node_or_origin);
22313 if (decl_by_reference_p (node_or_origin))
22314 add_type_attribute (parm_die, TREE_TYPE (type),
22315 TYPE_UNQUALIFIED,
22316 false, context_die);
22317 else
22318 add_type_attribute (parm_die, type,
22319 decl_quals (node_or_origin),
22320 false, context_die);
22322 if (origin == NULL && DECL_ARTIFICIAL (node))
22323 add_AT_flag (parm_die, DW_AT_artificial, 1);
22324 add_location:
22325 if (node && node != origin)
22326 equate_decl_number_to_die (node, parm_die);
22327 if (! DECL_ABSTRACT_P (node_or_origin))
22328 add_location_or_const_value_attribute (parm_die, node_or_origin,
22329 node == NULL);
22331 break;
22333 case tcc_type:
22334 /* We were called with some kind of a ..._TYPE node. */
22335 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22336 context_die);
22337 break;
22339 default:
22340 gcc_unreachable ();
22343 return parm_die;
22346 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22347 children DW_TAG_formal_parameter DIEs representing the arguments of the
22348 parameter pack.
22350 PARM_PACK must be a function parameter pack.
22351 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22352 must point to the subsequent arguments of the function PACK_ARG belongs to.
22353 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22354 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22355 following the last one for which a DIE was generated. */
22357 static dw_die_ref
22358 gen_formal_parameter_pack_die (tree parm_pack,
22359 tree pack_arg,
22360 dw_die_ref subr_die,
22361 tree *next_arg)
22363 tree arg;
22364 dw_die_ref parm_pack_die;
22366 gcc_assert (parm_pack
22367 && lang_hooks.function_parameter_pack_p (parm_pack)
22368 && subr_die);
22370 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22371 add_src_coords_attributes (parm_pack_die, parm_pack);
22373 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22375 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22376 parm_pack))
22377 break;
22378 gen_formal_parameter_die (arg, NULL,
22379 false /* Don't emit name attribute. */,
22380 parm_pack_die);
22382 if (next_arg)
22383 *next_arg = arg;
22384 return parm_pack_die;
22387 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22388 at the end of an (ANSI prototyped) formal parameters list. */
22390 static void
22391 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22393 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22396 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22397 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22398 parameters as specified in some function type specification (except for
22399 those which appear as part of a function *definition*). */
22401 static void
22402 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22404 tree link;
22405 tree formal_type = NULL;
22406 tree first_parm_type;
22407 tree arg;
22409 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22411 arg = DECL_ARGUMENTS (function_or_method_type);
22412 function_or_method_type = TREE_TYPE (function_or_method_type);
22414 else
22415 arg = NULL_TREE;
22417 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22419 /* Make our first pass over the list of formal parameter types and output a
22420 DW_TAG_formal_parameter DIE for each one. */
22421 for (link = first_parm_type; link; )
22423 dw_die_ref parm_die;
22425 formal_type = TREE_VALUE (link);
22426 if (formal_type == void_type_node)
22427 break;
22429 /* Output a (nameless) DIE to represent the formal parameter itself. */
22430 parm_die = gen_formal_parameter_die (formal_type, NULL,
22431 true /* Emit name attribute. */,
22432 context_die);
22433 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22434 && link == first_parm_type)
22436 add_AT_flag (parm_die, DW_AT_artificial, 1);
22437 if (dwarf_version >= 3 || !dwarf_strict)
22438 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22440 else if (arg && DECL_ARTIFICIAL (arg))
22441 add_AT_flag (parm_die, DW_AT_artificial, 1);
22443 link = TREE_CHAIN (link);
22444 if (arg)
22445 arg = DECL_CHAIN (arg);
22448 /* If this function type has an ellipsis, add a
22449 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22450 if (formal_type != void_type_node)
22451 gen_unspecified_parameters_die (function_or_method_type, context_die);
22453 /* Make our second (and final) pass over the list of formal parameter types
22454 and output DIEs to represent those types (as necessary). */
22455 for (link = TYPE_ARG_TYPES (function_or_method_type);
22456 link && TREE_VALUE (link);
22457 link = TREE_CHAIN (link))
22458 gen_type_die (TREE_VALUE (link), context_die);
22461 /* We want to generate the DIE for TYPE so that we can generate the
22462 die for MEMBER, which has been defined; we will need to refer back
22463 to the member declaration nested within TYPE. If we're trying to
22464 generate minimal debug info for TYPE, processing TYPE won't do the
22465 trick; we need to attach the member declaration by hand. */
22467 static void
22468 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22470 gen_type_die (type, context_die);
22472 /* If we're trying to avoid duplicate debug info, we may not have
22473 emitted the member decl for this function. Emit it now. */
22474 if (TYPE_STUB_DECL (type)
22475 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22476 && ! lookup_decl_die (member))
22478 dw_die_ref type_die;
22479 gcc_assert (!decl_ultimate_origin (member));
22481 type_die = lookup_type_die_strip_naming_typedef (type);
22482 if (TREE_CODE (member) == FUNCTION_DECL)
22483 gen_subprogram_die (member, type_die);
22484 else if (TREE_CODE (member) == FIELD_DECL)
22486 /* Ignore the nameless fields that are used to skip bits but handle
22487 C++ anonymous unions and structs. */
22488 if (DECL_NAME (member) != NULL_TREE
22489 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22490 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22492 struct vlr_context vlr_ctx = {
22493 DECL_CONTEXT (member), /* struct_type */
22494 NULL_TREE /* variant_part_offset */
22496 gen_type_die (member_declared_type (member), type_die);
22497 gen_field_die (member, &vlr_ctx, type_die);
22500 else
22501 gen_variable_die (member, NULL_TREE, type_die);
22505 /* Forward declare these functions, because they are mutually recursive
22506 with their set_block_* pairing functions. */
22507 static void set_decl_origin_self (tree);
22509 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22510 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22511 that it points to the node itself, thus indicating that the node is its
22512 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
22513 the given node is NULL, recursively descend the decl/block tree which
22514 it is the root of, and for each other ..._DECL or BLOCK node contained
22515 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
22516 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
22517 values to point to themselves. */
22519 static void
22520 set_block_origin_self (tree stmt)
22522 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
22524 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
22527 tree local_decl;
22529 for (local_decl = BLOCK_VARS (stmt);
22530 local_decl != NULL_TREE;
22531 local_decl = DECL_CHAIN (local_decl))
22532 /* Do not recurse on nested functions since the inlining status
22533 of parent and child can be different as per the DWARF spec. */
22534 if (TREE_CODE (local_decl) != FUNCTION_DECL
22535 && !DECL_EXTERNAL (local_decl))
22536 set_decl_origin_self (local_decl);
22540 tree subblock;
22542 for (subblock = BLOCK_SUBBLOCKS (stmt);
22543 subblock != NULL_TREE;
22544 subblock = BLOCK_CHAIN (subblock))
22545 set_block_origin_self (subblock); /* Recurse. */
22550 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
22551 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
22552 node to so that it points to the node itself, thus indicating that the
22553 node represents its own (abstract) origin. Additionally, if the
22554 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
22555 the decl/block tree of which the given node is the root of, and for
22556 each other ..._DECL or BLOCK node contained therein whose
22557 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
22558 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
22559 point to themselves. */
22561 static void
22562 set_decl_origin_self (tree decl)
22564 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
22566 DECL_ABSTRACT_ORIGIN (decl) = decl;
22567 if (TREE_CODE (decl) == FUNCTION_DECL)
22569 tree arg;
22571 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
22572 DECL_ABSTRACT_ORIGIN (arg) = arg;
22573 if (DECL_INITIAL (decl) != NULL_TREE
22574 && DECL_INITIAL (decl) != error_mark_node)
22575 set_block_origin_self (DECL_INITIAL (decl));
22580 /* Mark the early DIE for DECL as the abstract instance. */
22582 static void
22583 dwarf2out_abstract_function (tree decl)
22585 dw_die_ref old_die;
22587 /* Make sure we have the actual abstract inline, not a clone. */
22588 decl = DECL_ORIGIN (decl);
22590 if (DECL_IGNORED_P (decl))
22591 return;
22593 /* In LTO we're all set. We already created abstract instances
22594 early and we want to avoid creating a concrete instance of that
22595 if we don't output it. */
22596 if (in_lto_p)
22597 return;
22599 old_die = lookup_decl_die (decl);
22600 gcc_assert (old_die != NULL);
22601 if (get_AT (old_die, DW_AT_inline))
22602 /* We've already generated the abstract instance. */
22603 return;
22605 /* Go ahead and put DW_AT_inline on the DIE. */
22606 if (DECL_DECLARED_INLINE_P (decl))
22608 if (cgraph_function_possibly_inlined_p (decl))
22609 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
22610 else
22611 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
22613 else
22615 if (cgraph_function_possibly_inlined_p (decl))
22616 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
22617 else
22618 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
22621 if (DECL_DECLARED_INLINE_P (decl)
22622 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22623 add_AT_flag (old_die, DW_AT_artificial, 1);
22625 set_decl_origin_self (decl);
22628 /* Helper function of premark_used_types() which gets called through
22629 htab_traverse.
22631 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22632 marked as unused by prune_unused_types. */
22634 bool
22635 premark_used_types_helper (tree const &type, void *)
22637 dw_die_ref die;
22639 die = lookup_type_die (type);
22640 if (die != NULL)
22641 die->die_perennial_p = 1;
22642 return true;
22645 /* Helper function of premark_types_used_by_global_vars which gets called
22646 through htab_traverse.
22648 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22649 marked as unused by prune_unused_types. The DIE of the type is marked
22650 only if the global variable using the type will actually be emitted. */
22653 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
22654 void *)
22656 struct types_used_by_vars_entry *entry;
22657 dw_die_ref die;
22659 entry = (struct types_used_by_vars_entry *) *slot;
22660 gcc_assert (entry->type != NULL
22661 && entry->var_decl != NULL);
22662 die = lookup_type_die (entry->type);
22663 if (die)
22665 /* Ask cgraph if the global variable really is to be emitted.
22666 If yes, then we'll keep the DIE of ENTRY->TYPE. */
22667 varpool_node *node = varpool_node::get (entry->var_decl);
22668 if (node && node->definition)
22670 die->die_perennial_p = 1;
22671 /* Keep the parent DIEs as well. */
22672 while ((die = die->die_parent) && die->die_perennial_p == 0)
22673 die->die_perennial_p = 1;
22676 return 1;
22679 /* Mark all members of used_types_hash as perennial. */
22681 static void
22682 premark_used_types (struct function *fun)
22684 if (fun && fun->used_types_hash)
22685 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
22688 /* Mark all members of types_used_by_vars_entry as perennial. */
22690 static void
22691 premark_types_used_by_global_vars (void)
22693 if (types_used_by_vars_hash)
22694 types_used_by_vars_hash
22695 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
22698 /* Mark all variables used by the symtab as perennial. */
22700 static void
22701 premark_used_variables (void)
22703 /* Mark DIEs in the symtab as used. */
22704 varpool_node *var;
22705 FOR_EACH_VARIABLE (var)
22707 dw_die_ref die = lookup_decl_die (var->decl);
22708 if (die)
22709 die->die_perennial_p = 1;
22713 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
22714 for CA_LOC call arg loc node. */
22716 static dw_die_ref
22717 gen_call_site_die (tree decl, dw_die_ref subr_die,
22718 struct call_arg_loc_node *ca_loc)
22720 dw_die_ref stmt_die = NULL, die;
22721 tree block = ca_loc->block;
22723 while (block
22724 && block != DECL_INITIAL (decl)
22725 && TREE_CODE (block) == BLOCK)
22727 stmt_die = lookup_block_die (block);
22728 if (stmt_die)
22729 break;
22730 block = BLOCK_SUPERCONTEXT (block);
22732 if (stmt_die == NULL)
22733 stmt_die = subr_die;
22734 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
22735 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
22736 if (ca_loc->tail_call_p)
22737 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
22738 if (ca_loc->symbol_ref)
22740 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
22741 if (tdie)
22742 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
22743 else
22744 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
22745 false);
22747 return die;
22750 /* Generate a DIE to represent a declared function (either file-scope or
22751 block-local). */
22753 static void
22754 gen_subprogram_die (tree decl, dw_die_ref context_die)
22756 tree origin = decl_ultimate_origin (decl);
22757 dw_die_ref subr_die;
22758 dw_die_ref old_die = lookup_decl_die (decl);
22760 /* This function gets called multiple times for different stages of
22761 the debug process. For example, for func() in this code:
22763 namespace S
22765 void func() { ... }
22768 ...we get called 4 times. Twice in early debug and twice in
22769 late debug:
22771 Early debug
22772 -----------
22774 1. Once while generating func() within the namespace. This is
22775 the declaration. The declaration bit below is set, as the
22776 context is the namespace.
22778 A new DIE will be generated with DW_AT_declaration set.
22780 2. Once for func() itself. This is the specification. The
22781 declaration bit below is clear as the context is the CU.
22783 We will use the cached DIE from (1) to create a new DIE with
22784 DW_AT_specification pointing to the declaration in (1).
22786 Late debug via rest_of_handle_final()
22787 -------------------------------------
22789 3. Once generating func() within the namespace. This is also the
22790 declaration, as in (1), but this time we will early exit below
22791 as we have a cached DIE and a declaration needs no additional
22792 annotations (no locations), as the source declaration line
22793 info is enough.
22795 4. Once for func() itself. As in (2), this is the specification,
22796 but this time we will re-use the cached DIE, and just annotate
22797 it with the location information that should now be available.
22799 For something without namespaces, but with abstract instances, we
22800 are also called a multiple times:
22802 class Base
22804 public:
22805 Base (); // constructor declaration (1)
22808 Base::Base () { } // constructor specification (2)
22810 Early debug
22811 -----------
22813 1. Once for the Base() constructor by virtue of it being a
22814 member of the Base class. This is done via
22815 rest_of_type_compilation.
22817 This is a declaration, so a new DIE will be created with
22818 DW_AT_declaration.
22820 2. Once for the Base() constructor definition, but this time
22821 while generating the abstract instance of the base
22822 constructor (__base_ctor) which is being generated via early
22823 debug of reachable functions.
22825 Even though we have a cached version of the declaration (1),
22826 we will create a DW_AT_specification of the declaration DIE
22827 in (1).
22829 3. Once for the __base_ctor itself, but this time, we generate
22830 an DW_AT_abstract_origin version of the DW_AT_specification in
22831 (2).
22833 Late debug via rest_of_handle_final
22834 -----------------------------------
22836 4. One final time for the __base_ctor (which will have a cached
22837 DIE with DW_AT_abstract_origin created in (3). This time,
22838 we will just annotate the location information now
22839 available.
22841 int declaration = (current_function_decl != decl
22842 || class_or_namespace_scope_p (context_die));
22844 /* A declaration that has been previously dumped needs no
22845 additional information. */
22846 if (old_die && declaration)
22847 return;
22849 /* Now that the C++ front end lazily declares artificial member fns, we
22850 might need to retrofit the declaration into its class. */
22851 if (!declaration && !origin && !old_die
22852 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22853 && !class_or_namespace_scope_p (context_die)
22854 && debug_info_level > DINFO_LEVEL_TERSE)
22855 old_die = force_decl_die (decl);
22857 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
22858 if (origin != NULL)
22860 gcc_assert (!declaration || local_scope_p (context_die));
22862 /* Fixup die_parent for the abstract instance of a nested
22863 inline function. */
22864 if (old_die && old_die->die_parent == NULL)
22865 add_child_die (context_die, old_die);
22867 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22869 /* If we have a DW_AT_abstract_origin we have a working
22870 cached version. */
22871 subr_die = old_die;
22873 else
22875 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22876 add_abstract_origin_attribute (subr_die, origin);
22877 /* This is where the actual code for a cloned function is.
22878 Let's emit linkage name attribute for it. This helps
22879 debuggers to e.g, set breakpoints into
22880 constructors/destructors when the user asks "break
22881 K::K". */
22882 add_linkage_name (subr_die, decl);
22885 /* A cached copy, possibly from early dwarf generation. Reuse as
22886 much as possible. */
22887 else if (old_die)
22889 if (!get_AT_flag (old_die, DW_AT_declaration)
22890 /* We can have a normal definition following an inline one in the
22891 case of redefinition of GNU C extern inlines.
22892 It seems reasonable to use AT_specification in this case. */
22893 && !get_AT (old_die, DW_AT_inline))
22895 /* Detect and ignore this case, where we are trying to output
22896 something we have already output. */
22897 if (get_AT (old_die, DW_AT_low_pc)
22898 || get_AT (old_die, DW_AT_ranges))
22899 return;
22901 /* If we have no location information, this must be a
22902 partially generated DIE from early dwarf generation.
22903 Fall through and generate it. */
22906 /* If the definition comes from the same place as the declaration,
22907 maybe use the old DIE. We always want the DIE for this function
22908 that has the *_pc attributes to be under comp_unit_die so the
22909 debugger can find it. We also need to do this for abstract
22910 instances of inlines, since the spec requires the out-of-line copy
22911 to have the same parent. For local class methods, this doesn't
22912 apply; we just use the old DIE. */
22913 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22914 struct dwarf_file_data * file_index = lookup_filename (s.file);
22915 if (((is_unit_die (old_die->die_parent)
22916 /* This condition fixes the inconsistency/ICE with the
22917 following Fortran test (or some derivative thereof) while
22918 building libgfortran:
22920 module some_m
22921 contains
22922 logical function funky (FLAG)
22923 funky = .true.
22924 end function
22925 end module
22927 || (old_die->die_parent
22928 && old_die->die_parent->die_tag == DW_TAG_module)
22929 || local_scope_p (old_die->die_parent)
22930 || context_die == NULL)
22931 && (DECL_ARTIFICIAL (decl)
22932 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22933 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22934 == (unsigned) s.line)
22935 && (!debug_column_info
22936 || s.column == 0
22937 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22938 == (unsigned) s.column)))))
22939 /* With LTO if there's an abstract instance for
22940 the old DIE, this is a concrete instance and
22941 thus re-use the DIE. */
22942 || get_AT (old_die, DW_AT_abstract_origin))
22944 subr_die = old_die;
22946 /* Clear out the declaration attribute, but leave the
22947 parameters so they can be augmented with location
22948 information later. Unless this was a declaration, in
22949 which case, wipe out the nameless parameters and recreate
22950 them further down. */
22951 if (remove_AT (subr_die, DW_AT_declaration))
22954 remove_AT (subr_die, DW_AT_object_pointer);
22955 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22958 /* Make a specification pointing to the previously built
22959 declaration. */
22960 else
22962 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22963 add_AT_specification (subr_die, old_die);
22964 add_pubname (decl, subr_die);
22965 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22966 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22967 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22968 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22969 if (debug_column_info
22970 && s.column
22971 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22972 != (unsigned) s.column))
22973 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22975 /* If the prototype had an 'auto' or 'decltype(auto)' in
22976 the return type, emit the real type on the definition die. */
22977 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22979 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22980 while (die
22981 && (die->die_tag == DW_TAG_reference_type
22982 || die->die_tag == DW_TAG_rvalue_reference_type
22983 || die->die_tag == DW_TAG_pointer_type
22984 || die->die_tag == DW_TAG_const_type
22985 || die->die_tag == DW_TAG_volatile_type
22986 || die->die_tag == DW_TAG_restrict_type
22987 || die->die_tag == DW_TAG_array_type
22988 || die->die_tag == DW_TAG_ptr_to_member_type
22989 || die->die_tag == DW_TAG_subroutine_type))
22990 die = get_AT_ref (die, DW_AT_type);
22991 if (die == auto_die || die == decltype_auto_die)
22992 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22993 TYPE_UNQUALIFIED, false, context_die);
22996 /* When we process the method declaration, we haven't seen
22997 the out-of-class defaulted definition yet, so we have to
22998 recheck now. */
22999 if ((dwarf_version >= 5 || ! dwarf_strict)
23000 && !get_AT (subr_die, DW_AT_defaulted))
23002 int defaulted
23003 = lang_hooks.decls.decl_dwarf_attribute (decl,
23004 DW_AT_defaulted);
23005 if (defaulted != -1)
23007 /* Other values must have been handled before. */
23008 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23009 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23014 /* Create a fresh DIE for anything else. */
23015 else
23017 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23019 if (TREE_PUBLIC (decl))
23020 add_AT_flag (subr_die, DW_AT_external, 1);
23022 add_name_and_src_coords_attributes (subr_die, decl);
23023 add_pubname (decl, subr_die);
23024 if (debug_info_level > DINFO_LEVEL_TERSE)
23026 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23027 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23028 TYPE_UNQUALIFIED, false, context_die);
23031 add_pure_or_virtual_attribute (subr_die, decl);
23032 if (DECL_ARTIFICIAL (decl))
23033 add_AT_flag (subr_die, DW_AT_artificial, 1);
23035 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23036 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23038 add_alignment_attribute (subr_die, decl);
23040 add_accessibility_attribute (subr_die, decl);
23043 /* Unless we have an existing non-declaration DIE, equate the new
23044 DIE. */
23045 if (!old_die || is_declaration_die (old_die))
23046 equate_decl_number_to_die (decl, subr_die);
23048 if (declaration)
23050 if (!old_die || !get_AT (old_die, DW_AT_inline))
23052 add_AT_flag (subr_die, DW_AT_declaration, 1);
23054 /* If this is an explicit function declaration then generate
23055 a DW_AT_explicit attribute. */
23056 if ((dwarf_version >= 3 || !dwarf_strict)
23057 && lang_hooks.decls.decl_dwarf_attribute (decl,
23058 DW_AT_explicit) == 1)
23059 add_AT_flag (subr_die, DW_AT_explicit, 1);
23061 /* If this is a C++11 deleted special function member then generate
23062 a DW_AT_deleted attribute. */
23063 if ((dwarf_version >= 5 || !dwarf_strict)
23064 && lang_hooks.decls.decl_dwarf_attribute (decl,
23065 DW_AT_deleted) == 1)
23066 add_AT_flag (subr_die, DW_AT_deleted, 1);
23068 /* If this is a C++11 defaulted special function member then
23069 generate a DW_AT_defaulted attribute. */
23070 if (dwarf_version >= 5 || !dwarf_strict)
23072 int defaulted
23073 = lang_hooks.decls.decl_dwarf_attribute (decl,
23074 DW_AT_defaulted);
23075 if (defaulted != -1)
23076 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23079 /* If this is a C++11 non-static member function with & ref-qualifier
23080 then generate a DW_AT_reference attribute. */
23081 if ((dwarf_version >= 5 || !dwarf_strict)
23082 && lang_hooks.decls.decl_dwarf_attribute (decl,
23083 DW_AT_reference) == 1)
23084 add_AT_flag (subr_die, DW_AT_reference, 1);
23086 /* If this is a C++11 non-static member function with &&
23087 ref-qualifier then generate a DW_AT_reference attribute. */
23088 if ((dwarf_version >= 5 || !dwarf_strict)
23089 && lang_hooks.decls.decl_dwarf_attribute (decl,
23090 DW_AT_rvalue_reference)
23091 == 1)
23092 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23095 /* For non DECL_EXTERNALs, if range information is available, fill
23096 the DIE with it. */
23097 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23099 HOST_WIDE_INT cfa_fb_offset;
23101 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23103 if (!crtl->has_bb_partition)
23105 dw_fde_ref fde = fun->fde;
23106 if (fde->dw_fde_begin)
23108 /* We have already generated the labels. */
23109 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23110 fde->dw_fde_end, false);
23112 else
23114 /* Create start/end labels and add the range. */
23115 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23116 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23117 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23118 current_function_funcdef_no);
23119 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23120 current_function_funcdef_no);
23121 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23122 false);
23125 #if VMS_DEBUGGING_INFO
23126 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23127 Section 2.3 Prologue and Epilogue Attributes:
23128 When a breakpoint is set on entry to a function, it is generally
23129 desirable for execution to be suspended, not on the very first
23130 instruction of the function, but rather at a point after the
23131 function's frame has been set up, after any language defined local
23132 declaration processing has been completed, and before execution of
23133 the first statement of the function begins. Debuggers generally
23134 cannot properly determine where this point is. Similarly for a
23135 breakpoint set on exit from a function. The prologue and epilogue
23136 attributes allow a compiler to communicate the location(s) to use. */
23139 if (fde->dw_fde_vms_end_prologue)
23140 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23141 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23143 if (fde->dw_fde_vms_begin_epilogue)
23144 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23145 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23147 #endif
23150 else
23152 /* Generate pubnames entries for the split function code ranges. */
23153 dw_fde_ref fde = fun->fde;
23155 if (fde->dw_fde_second_begin)
23157 if (dwarf_version >= 3 || !dwarf_strict)
23159 /* We should use ranges for non-contiguous code section
23160 addresses. Use the actual code range for the initial
23161 section, since the HOT/COLD labels might precede an
23162 alignment offset. */
23163 bool range_list_added = false;
23164 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23165 fde->dw_fde_end, &range_list_added,
23166 false);
23167 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23168 fde->dw_fde_second_end,
23169 &range_list_added, false);
23170 if (range_list_added)
23171 add_ranges (NULL);
23173 else
23175 /* There is no real support in DW2 for this .. so we make
23176 a work-around. First, emit the pub name for the segment
23177 containing the function label. Then make and emit a
23178 simplified subprogram DIE for the second segment with the
23179 name pre-fixed by __hot/cold_sect_of_. We use the same
23180 linkage name for the second die so that gdb will find both
23181 sections when given "b foo". */
23182 const char *name = NULL;
23183 tree decl_name = DECL_NAME (decl);
23184 dw_die_ref seg_die;
23186 /* Do the 'primary' section. */
23187 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23188 fde->dw_fde_end, false);
23190 /* Build a minimal DIE for the secondary section. */
23191 seg_die = new_die (DW_TAG_subprogram,
23192 subr_die->die_parent, decl);
23194 if (TREE_PUBLIC (decl))
23195 add_AT_flag (seg_die, DW_AT_external, 1);
23197 if (decl_name != NULL
23198 && IDENTIFIER_POINTER (decl_name) != NULL)
23200 name = dwarf2_name (decl, 1);
23201 if (! DECL_ARTIFICIAL (decl))
23202 add_src_coords_attributes (seg_die, decl);
23204 add_linkage_name (seg_die, decl);
23206 gcc_assert (name != NULL);
23207 add_pure_or_virtual_attribute (seg_die, decl);
23208 if (DECL_ARTIFICIAL (decl))
23209 add_AT_flag (seg_die, DW_AT_artificial, 1);
23211 name = concat ("__second_sect_of_", name, NULL);
23212 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23213 fde->dw_fde_second_end, false);
23214 add_name_attribute (seg_die, name);
23215 if (want_pubnames ())
23216 add_pubname_string (name, seg_die);
23219 else
23220 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23221 false);
23224 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23226 /* We define the "frame base" as the function's CFA. This is more
23227 convenient for several reasons: (1) It's stable across the prologue
23228 and epilogue, which makes it better than just a frame pointer,
23229 (2) With dwarf3, there exists a one-byte encoding that allows us
23230 to reference the .debug_frame data by proxy, but failing that,
23231 (3) We can at least reuse the code inspection and interpretation
23232 code that determines the CFA position at various points in the
23233 function. */
23234 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23236 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23237 add_AT_loc (subr_die, DW_AT_frame_base, op);
23239 else
23241 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23242 if (list->dw_loc_next)
23243 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23244 else
23245 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23248 /* Compute a displacement from the "steady-state frame pointer" to
23249 the CFA. The former is what all stack slots and argument slots
23250 will reference in the rtl; the latter is what we've told the
23251 debugger about. We'll need to adjust all frame_base references
23252 by this displacement. */
23253 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23255 if (fun->static_chain_decl)
23257 /* DWARF requires here a location expression that computes the
23258 address of the enclosing subprogram's frame base. The machinery
23259 in tree-nested.c is supposed to store this specific address in the
23260 last field of the FRAME record. */
23261 const tree frame_type
23262 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23263 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23265 tree fb_expr
23266 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23267 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23268 fb_expr, fb_decl, NULL_TREE);
23270 add_AT_location_description (subr_die, DW_AT_static_link,
23271 loc_list_from_tree (fb_expr, 0, NULL));
23274 resolve_variable_values ();
23277 /* Generate child dies for template paramaters. */
23278 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23279 gen_generic_params_dies (decl);
23281 /* Now output descriptions of the arguments for this function. This gets
23282 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23283 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23284 `...' at the end of the formal parameter list. In order to find out if
23285 there was a trailing ellipsis or not, we must instead look at the type
23286 associated with the FUNCTION_DECL. This will be a node of type
23287 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23288 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23289 an ellipsis at the end. */
23291 /* In the case where we are describing a mere function declaration, all we
23292 need to do here (and all we *can* do here) is to describe the *types* of
23293 its formal parameters. */
23294 if (debug_info_level <= DINFO_LEVEL_TERSE)
23296 else if (declaration)
23297 gen_formal_types_die (decl, subr_die);
23298 else
23300 /* Generate DIEs to represent all known formal parameters. */
23301 tree parm = DECL_ARGUMENTS (decl);
23302 tree generic_decl = early_dwarf
23303 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23304 tree generic_decl_parm = generic_decl
23305 ? DECL_ARGUMENTS (generic_decl)
23306 : NULL;
23308 /* Now we want to walk the list of parameters of the function and
23309 emit their relevant DIEs.
23311 We consider the case of DECL being an instance of a generic function
23312 as well as it being a normal function.
23314 If DECL is an instance of a generic function we walk the
23315 parameters of the generic function declaration _and_ the parameters of
23316 DECL itself. This is useful because we want to emit specific DIEs for
23317 function parameter packs and those are declared as part of the
23318 generic function declaration. In that particular case,
23319 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23320 That DIE has children DIEs representing the set of arguments
23321 of the pack. Note that the set of pack arguments can be empty.
23322 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23323 children DIE.
23325 Otherwise, we just consider the parameters of DECL. */
23326 while (generic_decl_parm || parm)
23328 if (generic_decl_parm
23329 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23330 gen_formal_parameter_pack_die (generic_decl_parm,
23331 parm, subr_die,
23332 &parm);
23333 else if (parm)
23335 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23337 if (early_dwarf
23338 && parm == DECL_ARGUMENTS (decl)
23339 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23340 && parm_die
23341 && (dwarf_version >= 3 || !dwarf_strict))
23342 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23344 parm = DECL_CHAIN (parm);
23347 if (generic_decl_parm)
23348 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23351 /* Decide whether we need an unspecified_parameters DIE at the end.
23352 There are 2 more cases to do this for: 1) the ansi ... declaration -
23353 this is detectable when the end of the arg list is not a
23354 void_type_node 2) an unprototyped function declaration (not a
23355 definition). This just means that we have no info about the
23356 parameters at all. */
23357 if (early_dwarf)
23359 if (prototype_p (TREE_TYPE (decl)))
23361 /* This is the prototyped case, check for.... */
23362 if (stdarg_p (TREE_TYPE (decl)))
23363 gen_unspecified_parameters_die (decl, subr_die);
23365 else if (DECL_INITIAL (decl) == NULL_TREE)
23366 gen_unspecified_parameters_die (decl, subr_die);
23370 if (subr_die != old_die)
23371 /* Add the calling convention attribute if requested. */
23372 add_calling_convention_attribute (subr_die, decl);
23374 /* Output Dwarf info for all of the stuff within the body of the function
23375 (if it has one - it may be just a declaration).
23377 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23378 a function. This BLOCK actually represents the outermost binding contour
23379 for the function, i.e. the contour in which the function's formal
23380 parameters and labels get declared. Curiously, it appears that the front
23381 end doesn't actually put the PARM_DECL nodes for the current function onto
23382 the BLOCK_VARS list for this outer scope, but are strung off of the
23383 DECL_ARGUMENTS list for the function instead.
23385 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23386 the LABEL_DECL nodes for the function however, and we output DWARF info
23387 for those in decls_for_scope. Just within the `outer_scope' there will be
23388 a BLOCK node representing the function's outermost pair of curly braces,
23389 and any blocks used for the base and member initializers of a C++
23390 constructor function. */
23391 tree outer_scope = DECL_INITIAL (decl);
23392 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23394 int call_site_note_count = 0;
23395 int tail_call_site_note_count = 0;
23397 /* Emit a DW_TAG_variable DIE for a named return value. */
23398 if (DECL_NAME (DECL_RESULT (decl)))
23399 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23401 /* The first time through decls_for_scope we will generate the
23402 DIEs for the locals. The second time, we fill in the
23403 location info. */
23404 decls_for_scope (outer_scope, subr_die);
23406 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23408 struct call_arg_loc_node *ca_loc;
23409 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23411 dw_die_ref die = NULL;
23412 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23413 rtx arg, next_arg;
23414 tree arg_decl = NULL_TREE;
23416 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23417 ? XEXP (ca_loc->call_arg_loc_note, 0)
23418 : NULL_RTX);
23419 arg; arg = next_arg)
23421 dw_loc_descr_ref reg, val;
23422 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23423 dw_die_ref cdie, tdie = NULL;
23425 next_arg = XEXP (arg, 1);
23426 if (REG_P (XEXP (XEXP (arg, 0), 0))
23427 && next_arg
23428 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23429 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23430 && REGNO (XEXP (XEXP (arg, 0), 0))
23431 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23432 next_arg = XEXP (next_arg, 1);
23433 if (mode == VOIDmode)
23435 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23436 if (mode == VOIDmode)
23437 mode = GET_MODE (XEXP (arg, 0));
23439 if (mode == VOIDmode || mode == BLKmode)
23440 continue;
23441 /* Get dynamic information about call target only if we
23442 have no static information: we cannot generate both
23443 DW_AT_call_origin and DW_AT_call_target
23444 attributes. */
23445 if (ca_loc->symbol_ref == NULL_RTX)
23447 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23449 tloc = XEXP (XEXP (arg, 0), 1);
23450 continue;
23452 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23453 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23455 tlocc = XEXP (XEXP (arg, 0), 1);
23456 continue;
23459 reg = NULL;
23460 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23461 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23462 VAR_INIT_STATUS_INITIALIZED);
23463 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23465 rtx mem = XEXP (XEXP (arg, 0), 0);
23466 reg = mem_loc_descriptor (XEXP (mem, 0),
23467 get_address_mode (mem),
23468 GET_MODE (mem),
23469 VAR_INIT_STATUS_INITIALIZED);
23471 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23472 == DEBUG_PARAMETER_REF)
23474 tree tdecl
23475 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23476 tdie = lookup_decl_die (tdecl);
23477 if (tdie == NULL)
23478 continue;
23479 arg_decl = tdecl;
23481 else
23482 continue;
23483 if (reg == NULL
23484 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23485 != DEBUG_PARAMETER_REF)
23486 continue;
23487 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23488 VOIDmode,
23489 VAR_INIT_STATUS_INITIALIZED);
23490 if (val == NULL)
23491 continue;
23492 if (die == NULL)
23493 die = gen_call_site_die (decl, subr_die, ca_loc);
23494 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23495 NULL_TREE);
23496 add_desc_attribute (cdie, arg_decl);
23497 if (reg != NULL)
23498 add_AT_loc (cdie, DW_AT_location, reg);
23499 else if (tdie != NULL)
23500 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23501 tdie);
23502 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23503 if (next_arg != XEXP (arg, 1))
23505 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
23506 if (mode == VOIDmode)
23507 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
23508 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
23509 0), 1),
23510 mode, VOIDmode,
23511 VAR_INIT_STATUS_INITIALIZED);
23512 if (val != NULL)
23513 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
23514 val);
23517 if (die == NULL
23518 && (ca_loc->symbol_ref || tloc))
23519 die = gen_call_site_die (decl, subr_die, ca_loc);
23520 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
23522 dw_loc_descr_ref tval = NULL;
23524 if (tloc != NULL_RTX)
23525 tval = mem_loc_descriptor (tloc,
23526 GET_MODE (tloc) == VOIDmode
23527 ? Pmode : GET_MODE (tloc),
23528 VOIDmode,
23529 VAR_INIT_STATUS_INITIALIZED);
23530 if (tval)
23531 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
23532 else if (tlocc != NULL_RTX)
23534 tval = mem_loc_descriptor (tlocc,
23535 GET_MODE (tlocc) == VOIDmode
23536 ? Pmode : GET_MODE (tlocc),
23537 VOIDmode,
23538 VAR_INIT_STATUS_INITIALIZED);
23539 if (tval)
23540 add_AT_loc (die,
23541 dwarf_AT (DW_AT_call_target_clobbered),
23542 tval);
23545 if (die != NULL)
23547 call_site_note_count++;
23548 if (ca_loc->tail_call_p)
23549 tail_call_site_note_count++;
23553 call_arg_locations = NULL;
23554 call_arg_loc_last = NULL;
23555 if (tail_call_site_count >= 0
23556 && tail_call_site_count == tail_call_site_note_count
23557 && (!dwarf_strict || dwarf_version >= 5))
23559 if (call_site_count >= 0
23560 && call_site_count == call_site_note_count)
23561 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
23562 else
23563 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
23565 call_site_count = -1;
23566 tail_call_site_count = -1;
23569 /* Mark used types after we have created DIEs for the functions scopes. */
23570 premark_used_types (DECL_STRUCT_FUNCTION (decl));
23573 /* Returns a hash value for X (which really is a die_struct). */
23575 hashval_t
23576 block_die_hasher::hash (die_struct *d)
23578 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
23581 /* Return nonzero if decl_id and die_parent of die_struct X is the same
23582 as decl_id and die_parent of die_struct Y. */
23584 bool
23585 block_die_hasher::equal (die_struct *x, die_struct *y)
23587 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
23590 /* Hold information about markers for inlined entry points. */
23591 struct GTY ((for_user)) inline_entry_data
23593 /* The block that's the inlined_function_outer_scope for an inlined
23594 function. */
23595 tree block;
23597 /* The label at the inlined entry point. */
23598 const char *label_pfx;
23599 unsigned int label_num;
23601 /* The view number to be used as the inlined entry point. */
23602 var_loc_view view;
23605 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
23607 typedef tree compare_type;
23608 static inline hashval_t hash (const inline_entry_data *);
23609 static inline bool equal (const inline_entry_data *, const_tree);
23612 /* Hash table routines for inline_entry_data. */
23614 inline hashval_t
23615 inline_entry_data_hasher::hash (const inline_entry_data *data)
23617 return htab_hash_pointer (data->block);
23620 inline bool
23621 inline_entry_data_hasher::equal (const inline_entry_data *data,
23622 const_tree block)
23624 return data->block == block;
23627 /* Inlined entry points pending DIE creation in this compilation unit. */
23629 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
23632 /* Return TRUE if DECL, which may have been previously generated as
23633 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
23634 true if decl (or its origin) is either an extern declaration or a
23635 class/namespace scoped declaration.
23637 The declare_in_namespace support causes us to get two DIEs for one
23638 variable, both of which are declarations. We want to avoid
23639 considering one to be a specification, so we must test for
23640 DECLARATION and DW_AT_declaration. */
23641 static inline bool
23642 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
23644 return (old_die && TREE_STATIC (decl) && !declaration
23645 && get_AT_flag (old_die, DW_AT_declaration) == 1);
23648 /* Return true if DECL is a local static. */
23650 static inline bool
23651 local_function_static (tree decl)
23653 gcc_assert (VAR_P (decl));
23654 return TREE_STATIC (decl)
23655 && DECL_CONTEXT (decl)
23656 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
23659 /* Return true iff DECL overrides (presumably completes) the type of
23660 OLD_DIE within CONTEXT_DIE. */
23662 static bool
23663 override_type_for_decl_p (tree decl, dw_die_ref old_die,
23664 dw_die_ref context_die)
23666 tree type = TREE_TYPE (decl);
23667 int cv_quals;
23669 if (decl_by_reference_p (decl))
23671 type = TREE_TYPE (type);
23672 cv_quals = TYPE_UNQUALIFIED;
23674 else
23675 cv_quals = decl_quals (decl);
23677 dw_die_ref type_die = modified_type_die (type,
23678 cv_quals | TYPE_QUALS (type),
23679 false,
23680 context_die);
23682 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
23684 return type_die != old_type_die;
23687 /* Generate a DIE to represent a declared data object.
23688 Either DECL or ORIGIN must be non-null. */
23690 static void
23691 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
23693 HOST_WIDE_INT off = 0;
23694 tree com_decl;
23695 tree decl_or_origin = decl ? decl : origin;
23696 tree ultimate_origin;
23697 dw_die_ref var_die;
23698 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
23699 bool declaration = (DECL_EXTERNAL (decl_or_origin)
23700 || class_or_namespace_scope_p (context_die));
23701 bool specialization_p = false;
23702 bool no_linkage_name = false;
23704 /* While C++ inline static data members have definitions inside of the
23705 class, force the first DIE to be a declaration, then let gen_member_die
23706 reparent it to the class context and call gen_variable_die again
23707 to create the outside of the class DIE for the definition. */
23708 if (!declaration
23709 && old_die == NULL
23710 && decl
23711 && DECL_CONTEXT (decl)
23712 && TYPE_P (DECL_CONTEXT (decl))
23713 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
23715 declaration = true;
23716 if (dwarf_version < 5)
23717 no_linkage_name = true;
23720 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23721 if (decl || ultimate_origin)
23722 origin = ultimate_origin;
23723 com_decl = fortran_common (decl_or_origin, &off);
23725 /* Symbol in common gets emitted as a child of the common block, in the form
23726 of a data member. */
23727 if (com_decl)
23729 dw_die_ref com_die;
23730 dw_loc_list_ref loc = NULL;
23731 die_node com_die_arg;
23733 var_die = lookup_decl_die (decl_or_origin);
23734 if (var_die)
23736 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
23738 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
23739 if (loc)
23741 if (off)
23743 /* Optimize the common case. */
23744 if (single_element_loc_list_p (loc)
23745 && loc->expr->dw_loc_opc == DW_OP_addr
23746 && loc->expr->dw_loc_next == NULL
23747 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
23748 == SYMBOL_REF)
23750 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23751 loc->expr->dw_loc_oprnd1.v.val_addr
23752 = plus_constant (GET_MODE (x), x , off);
23754 else
23755 loc_list_plus_const (loc, off);
23757 add_AT_location_description (var_die, DW_AT_location, loc);
23758 remove_AT (var_die, DW_AT_declaration);
23761 return;
23764 if (common_block_die_table == NULL)
23765 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
23767 com_die_arg.decl_id = DECL_UID (com_decl);
23768 com_die_arg.die_parent = context_die;
23769 com_die = common_block_die_table->find (&com_die_arg);
23770 if (! early_dwarf)
23771 loc = loc_list_from_tree (com_decl, 2, NULL);
23772 if (com_die == NULL)
23774 const char *cnam
23775 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
23776 die_node **slot;
23778 com_die = new_die (DW_TAG_common_block, context_die, decl);
23779 add_name_and_src_coords_attributes (com_die, com_decl);
23780 if (loc)
23782 add_AT_location_description (com_die, DW_AT_location, loc);
23783 /* Avoid sharing the same loc descriptor between
23784 DW_TAG_common_block and DW_TAG_variable. */
23785 loc = loc_list_from_tree (com_decl, 2, NULL);
23787 else if (DECL_EXTERNAL (decl_or_origin))
23788 add_AT_flag (com_die, DW_AT_declaration, 1);
23789 if (want_pubnames ())
23790 add_pubname_string (cnam, com_die); /* ??? needed? */
23791 com_die->decl_id = DECL_UID (com_decl);
23792 slot = common_block_die_table->find_slot (com_die, INSERT);
23793 *slot = com_die;
23795 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
23797 add_AT_location_description (com_die, DW_AT_location, loc);
23798 loc = loc_list_from_tree (com_decl, 2, NULL);
23799 remove_AT (com_die, DW_AT_declaration);
23801 var_die = new_die (DW_TAG_variable, com_die, decl);
23802 add_name_and_src_coords_attributes (var_die, decl_or_origin);
23803 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
23804 decl_quals (decl_or_origin), false,
23805 context_die);
23806 add_alignment_attribute (var_die, decl);
23807 add_AT_flag (var_die, DW_AT_external, 1);
23808 if (loc)
23810 if (off)
23812 /* Optimize the common case. */
23813 if (single_element_loc_list_p (loc)
23814 && loc->expr->dw_loc_opc == DW_OP_addr
23815 && loc->expr->dw_loc_next == NULL
23816 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
23818 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23819 loc->expr->dw_loc_oprnd1.v.val_addr
23820 = plus_constant (GET_MODE (x), x, off);
23822 else
23823 loc_list_plus_const (loc, off);
23825 add_AT_location_description (var_die, DW_AT_location, loc);
23827 else if (DECL_EXTERNAL (decl_or_origin))
23828 add_AT_flag (var_die, DW_AT_declaration, 1);
23829 if (decl)
23830 equate_decl_number_to_die (decl, var_die);
23831 return;
23834 if (old_die)
23836 if (declaration)
23838 /* A declaration that has been previously dumped, needs no
23839 further annotations, since it doesn't need location on
23840 the second pass. */
23841 return;
23843 else if (decl_will_get_specification_p (old_die, decl, declaration)
23844 && !get_AT (old_die, DW_AT_specification))
23846 /* Fall-thru so we can make a new variable die along with a
23847 DW_AT_specification. */
23849 else if (origin && old_die->die_parent != context_die)
23851 /* If we will be creating an inlined instance, we need a
23852 new DIE that will get annotated with
23853 DW_AT_abstract_origin. */
23854 gcc_assert (!DECL_ABSTRACT_P (decl));
23856 else
23858 /* If a DIE was dumped early, it still needs location info.
23859 Skip to where we fill the location bits. */
23860 var_die = old_die;
23862 /* ??? In LTRANS we cannot annotate early created variably
23863 modified type DIEs without copying them and adjusting all
23864 references to them. Thus we dumped them again. Also add a
23865 reference to them but beware of -g0 compile and -g link
23866 in which case the reference will be already present. */
23867 tree type = TREE_TYPE (decl_or_origin);
23868 if (in_lto_p
23869 && ! get_AT (var_die, DW_AT_type)
23870 && variably_modified_type_p
23871 (type, decl_function_context (decl_or_origin)))
23873 if (decl_by_reference_p (decl_or_origin))
23874 add_type_attribute (var_die, TREE_TYPE (type),
23875 TYPE_UNQUALIFIED, false, context_die);
23876 else
23877 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
23878 false, context_die);
23881 goto gen_variable_die_location;
23885 /* For static data members, the declaration in the class is supposed
23886 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
23887 also in DWARF2; the specification should still be DW_TAG_variable
23888 referencing the DW_TAG_member DIE. */
23889 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
23890 var_die = new_die (DW_TAG_member, context_die, decl);
23891 else
23892 var_die = new_die (DW_TAG_variable, context_die, decl);
23894 if (origin != NULL)
23895 add_abstract_origin_attribute (var_die, origin);
23897 /* Loop unrolling can create multiple blocks that refer to the same
23898 static variable, so we must test for the DW_AT_declaration flag.
23900 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
23901 copy decls and set the DECL_ABSTRACT_P flag on them instead of
23902 sharing them.
23904 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
23905 else if (decl_will_get_specification_p (old_die, decl, declaration))
23907 /* This is a definition of a C++ class level static. */
23908 add_AT_specification (var_die, old_die);
23909 specialization_p = true;
23910 if (DECL_NAME (decl))
23912 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23913 struct dwarf_file_data * file_index = lookup_filename (s.file);
23915 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23916 add_AT_file (var_die, DW_AT_decl_file, file_index);
23918 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23919 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23921 if (debug_column_info
23922 && s.column
23923 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23924 != (unsigned) s.column))
23925 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23927 if (old_die->die_tag == DW_TAG_member)
23928 add_linkage_name (var_die, decl);
23931 else
23932 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23934 if ((origin == NULL && !specialization_p)
23935 || (origin != NULL
23936 && !DECL_ABSTRACT_P (decl_or_origin)
23937 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23938 decl_function_context
23939 (decl_or_origin)))
23940 || (old_die && specialization_p
23941 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
23943 tree type = TREE_TYPE (decl_or_origin);
23945 if (decl_by_reference_p (decl_or_origin))
23946 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23947 context_die);
23948 else
23949 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23950 context_die);
23953 if (origin == NULL && !specialization_p)
23955 if (TREE_PUBLIC (decl))
23956 add_AT_flag (var_die, DW_AT_external, 1);
23958 if (DECL_ARTIFICIAL (decl))
23959 add_AT_flag (var_die, DW_AT_artificial, 1);
23961 add_alignment_attribute (var_die, decl);
23963 add_accessibility_attribute (var_die, decl);
23966 if (declaration)
23967 add_AT_flag (var_die, DW_AT_declaration, 1);
23969 if (decl && (DECL_ABSTRACT_P (decl)
23970 || !old_die || is_declaration_die (old_die)))
23971 equate_decl_number_to_die (decl, var_die);
23973 gen_variable_die_location:
23974 if (! declaration
23975 && (! DECL_ABSTRACT_P (decl_or_origin)
23976 /* Local static vars are shared between all clones/inlines,
23977 so emit DW_AT_location on the abstract DIE if DECL_RTL is
23978 already set. */
23979 || (VAR_P (decl_or_origin)
23980 && TREE_STATIC (decl_or_origin)
23981 && DECL_RTL_SET_P (decl_or_origin))))
23983 if (early_dwarf)
23984 add_pubname (decl_or_origin, var_die);
23985 else
23986 add_location_or_const_value_attribute (var_die, decl_or_origin,
23987 decl == NULL);
23989 else
23990 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
23992 if ((dwarf_version >= 4 || !dwarf_strict)
23993 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23994 DW_AT_const_expr) == 1
23995 && !get_AT (var_die, DW_AT_const_expr)
23996 && !specialization_p)
23997 add_AT_flag (var_die, DW_AT_const_expr, 1);
23999 if (!dwarf_strict)
24001 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24002 DW_AT_inline);
24003 if (inl != -1
24004 && !get_AT (var_die, DW_AT_inline)
24005 && !specialization_p)
24006 add_AT_unsigned (var_die, DW_AT_inline, inl);
24010 /* Generate a DIE to represent a named constant. */
24012 static void
24013 gen_const_die (tree decl, dw_die_ref context_die)
24015 dw_die_ref const_die;
24016 tree type = TREE_TYPE (decl);
24018 const_die = lookup_decl_die (decl);
24019 if (const_die)
24020 return;
24022 const_die = new_die (DW_TAG_constant, context_die, decl);
24023 equate_decl_number_to_die (decl, const_die);
24024 add_name_and_src_coords_attributes (const_die, decl);
24025 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24026 if (TREE_PUBLIC (decl))
24027 add_AT_flag (const_die, DW_AT_external, 1);
24028 if (DECL_ARTIFICIAL (decl))
24029 add_AT_flag (const_die, DW_AT_artificial, 1);
24030 tree_add_const_value_attribute_for_decl (const_die, decl);
24033 /* Generate a DIE to represent a label identifier. */
24035 static void
24036 gen_label_die (tree decl, dw_die_ref context_die)
24038 tree origin = decl_ultimate_origin (decl);
24039 dw_die_ref lbl_die = lookup_decl_die (decl);
24040 rtx insn;
24041 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24043 if (!lbl_die)
24045 lbl_die = new_die (DW_TAG_label, context_die, decl);
24046 equate_decl_number_to_die (decl, lbl_die);
24048 if (origin != NULL)
24049 add_abstract_origin_attribute (lbl_die, origin);
24050 else
24051 add_name_and_src_coords_attributes (lbl_die, decl);
24054 if (DECL_ABSTRACT_P (decl))
24055 equate_decl_number_to_die (decl, lbl_die);
24056 else if (! early_dwarf)
24058 insn = DECL_RTL_IF_SET (decl);
24060 /* Deleted labels are programmer specified labels which have been
24061 eliminated because of various optimizations. We still emit them
24062 here so that it is possible to put breakpoints on them. */
24063 if (insn
24064 && (LABEL_P (insn)
24065 || ((NOTE_P (insn)
24066 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24068 /* When optimization is enabled (via -O) some parts of the compiler
24069 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
24070 represent source-level labels which were explicitly declared by
24071 the user. This really shouldn't be happening though, so catch
24072 it if it ever does happen. */
24073 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24075 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24076 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24078 else if (insn
24079 && NOTE_P (insn)
24080 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24081 && CODE_LABEL_NUMBER (insn) != -1)
24083 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24084 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24089 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24090 attributes to the DIE for a block STMT, to describe where the inlined
24091 function was called from. This is similar to add_src_coords_attributes. */
24093 static inline void
24094 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24096 /* We can end up with BUILTINS_LOCATION here. */
24097 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24098 return;
24100 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
24102 if (dwarf_version >= 3 || !dwarf_strict)
24104 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24105 add_AT_unsigned (die, DW_AT_call_line, s.line);
24106 if (debug_column_info && s.column)
24107 add_AT_unsigned (die, DW_AT_call_column, s.column);
24112 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24113 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24115 static inline void
24116 add_high_low_attributes (tree stmt, dw_die_ref die)
24118 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24120 if (inline_entry_data **iedp
24121 = !inline_entry_data_table ? NULL
24122 : inline_entry_data_table->find_slot_with_hash (stmt,
24123 htab_hash_pointer (stmt),
24124 NO_INSERT))
24126 inline_entry_data *ied = *iedp;
24127 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24128 gcc_assert (debug_inline_points);
24129 gcc_assert (inlined_function_outer_scope_p (stmt));
24131 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24132 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24134 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24135 && !dwarf_strict)
24137 if (!output_asm_line_debug_info ())
24138 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24139 else
24141 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24142 /* FIXME: this will resolve to a small number. Could we
24143 possibly emit smaller data? Ideally we'd emit a
24144 uleb128, but that would make the size of DIEs
24145 impossible for the compiler to compute, since it's
24146 the assembler that computes the value of the view
24147 label in this case. Ideally, we'd have a single form
24148 encompassing both the address and the view, and
24149 indirecting them through a table might make things
24150 easier, but even that would be more wasteful,
24151 space-wise, than what we have now. */
24152 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24156 inline_entry_data_table->clear_slot (iedp);
24159 if (BLOCK_FRAGMENT_CHAIN (stmt)
24160 && (dwarf_version >= 3 || !dwarf_strict))
24162 tree chain, superblock = NULL_TREE;
24163 dw_die_ref pdie;
24164 dw_attr_node *attr = NULL;
24166 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24168 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24169 BLOCK_NUMBER (stmt));
24170 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24173 /* Optimize duplicate .debug_ranges lists or even tails of
24174 lists. If this BLOCK has same ranges as its supercontext,
24175 lookup DW_AT_ranges attribute in the supercontext (and
24176 recursively so), verify that the ranges_table contains the
24177 right values and use it instead of adding a new .debug_range. */
24178 for (chain = stmt, pdie = die;
24179 BLOCK_SAME_RANGE (chain);
24180 chain = BLOCK_SUPERCONTEXT (chain))
24182 dw_attr_node *new_attr;
24184 pdie = pdie->die_parent;
24185 if (pdie == NULL)
24186 break;
24187 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24188 break;
24189 new_attr = get_AT (pdie, DW_AT_ranges);
24190 if (new_attr == NULL
24191 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24192 break;
24193 attr = new_attr;
24194 superblock = BLOCK_SUPERCONTEXT (chain);
24196 if (attr != NULL
24197 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24198 == (int)BLOCK_NUMBER (superblock))
24199 && BLOCK_FRAGMENT_CHAIN (superblock))
24201 unsigned long off = attr->dw_attr_val.v.val_offset;
24202 unsigned long supercnt = 0, thiscnt = 0;
24203 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24204 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24206 ++supercnt;
24207 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24208 == (int)BLOCK_NUMBER (chain));
24210 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24211 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24212 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24213 ++thiscnt;
24214 gcc_assert (supercnt >= thiscnt);
24215 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24216 false);
24217 note_rnglist_head (off + supercnt - thiscnt);
24218 return;
24221 unsigned int offset = add_ranges (stmt, true);
24222 add_AT_range_list (die, DW_AT_ranges, offset, false);
24223 note_rnglist_head (offset);
24225 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24226 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24229 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24230 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24231 chain = BLOCK_FRAGMENT_CHAIN (chain);
24233 while (chain);
24234 add_ranges (NULL);
24236 else
24238 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24239 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24240 BLOCK_NUMBER (stmt));
24241 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24242 BLOCK_NUMBER (stmt));
24243 add_AT_low_high_pc (die, label, label_high, false);
24247 /* Generate a DIE for a lexical block. */
24249 static void
24250 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24252 dw_die_ref old_die = lookup_block_die (stmt);
24253 dw_die_ref stmt_die = NULL;
24254 if (!old_die)
24256 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24257 equate_block_to_die (stmt, stmt_die);
24260 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24262 /* If this is an inlined or conrecte instance, create a new lexical
24263 die for anything below to attach DW_AT_abstract_origin to. */
24264 if (old_die)
24265 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24267 tree origin = block_ultimate_origin (stmt);
24268 if (origin != NULL_TREE && (origin != stmt || old_die))
24269 add_abstract_origin_attribute (stmt_die, origin);
24271 old_die = NULL;
24274 if (old_die)
24275 stmt_die = old_die;
24277 /* A non abstract block whose blocks have already been reordered
24278 should have the instruction range for this block. If so, set the
24279 high/low attributes. */
24280 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24282 gcc_assert (stmt_die);
24283 add_high_low_attributes (stmt, stmt_die);
24286 decls_for_scope (stmt, stmt_die);
24289 /* Generate a DIE for an inlined subprogram. */
24291 static void
24292 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24294 tree decl = block_ultimate_origin (stmt);
24296 /* Make sure any inlined functions are known to be inlineable. */
24297 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24298 || cgraph_function_possibly_inlined_p (decl));
24300 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24302 if (call_arg_locations || debug_inline_points)
24303 equate_block_to_die (stmt, subr_die);
24304 add_abstract_origin_attribute (subr_die, decl);
24305 if (TREE_ASM_WRITTEN (stmt))
24306 add_high_low_attributes (stmt, subr_die);
24307 add_call_src_coords_attributes (stmt, subr_die);
24309 /* The inliner creates an extra BLOCK for the parameter setup,
24310 we want to merge that with the actual outermost BLOCK of the
24311 inlined function to avoid duplicate locals in consumers.
24312 Do that by doing the recursion to subblocks on the single subblock
24313 of STMT. */
24314 bool unwrap_one = false;
24315 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24317 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24318 if (origin
24319 && TREE_CODE (origin) == BLOCK
24320 && BLOCK_SUPERCONTEXT (origin) == decl)
24321 unwrap_one = true;
24323 decls_for_scope (stmt, subr_die, !unwrap_one);
24324 if (unwrap_one)
24325 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24328 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24329 the comment for VLR_CONTEXT. */
24331 static void
24332 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24334 dw_die_ref decl_die;
24336 if (TREE_TYPE (decl) == error_mark_node)
24337 return;
24339 decl_die = new_die (DW_TAG_member, context_die, decl);
24340 add_name_and_src_coords_attributes (decl_die, decl);
24341 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24342 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24343 context_die);
24345 if (DECL_BIT_FIELD_TYPE (decl))
24347 add_byte_size_attribute (decl_die, decl);
24348 add_bit_size_attribute (decl_die, decl);
24349 add_bit_offset_attribute (decl_die, decl);
24352 add_alignment_attribute (decl_die, decl);
24354 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24355 add_data_member_location_attribute (decl_die, decl, ctx);
24357 if (DECL_ARTIFICIAL (decl))
24358 add_AT_flag (decl_die, DW_AT_artificial, 1);
24360 add_accessibility_attribute (decl_die, decl);
24362 /* Equate decl number to die, so that we can look up this decl later on. */
24363 equate_decl_number_to_die (decl, decl_die);
24366 /* Generate a DIE for a pointer to a member type. TYPE can be an
24367 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24368 pointer to member function. */
24370 static void
24371 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24373 if (lookup_type_die (type))
24374 return;
24376 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24377 scope_die_for (type, context_die), type);
24379 equate_type_number_to_die (type, ptr_die);
24380 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24381 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24382 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24383 context_die);
24384 add_alignment_attribute (ptr_die, type);
24386 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24387 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24389 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24390 add_AT_loc (ptr_die, DW_AT_use_location, op);
24394 static char *producer_string;
24396 /* Return a heap allocated producer string including command line options
24397 if -grecord-gcc-switches. */
24399 static char *
24400 gen_producer_string (void)
24402 size_t j;
24403 auto_vec<const char *> switches;
24404 const char *language_string = lang_hooks.name;
24405 char *producer, *tail;
24406 const char *p;
24407 size_t len = dwarf_record_gcc_switches ? 0 : 3;
24408 size_t plen = strlen (language_string) + 1 + strlen (version_string);
24410 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
24411 switch (save_decoded_options[j].opt_index)
24413 case OPT_o:
24414 case OPT_d:
24415 case OPT_dumpbase:
24416 case OPT_dumpbase_ext:
24417 case OPT_dumpdir:
24418 case OPT_quiet:
24419 case OPT_version:
24420 case OPT_v:
24421 case OPT_w:
24422 case OPT_L:
24423 case OPT_D:
24424 case OPT_I:
24425 case OPT_U:
24426 case OPT_SPECIAL_unknown:
24427 case OPT_SPECIAL_ignore:
24428 case OPT_SPECIAL_warn_removed:
24429 case OPT_SPECIAL_program_name:
24430 case OPT_SPECIAL_input_file:
24431 case OPT_grecord_gcc_switches:
24432 case OPT__output_pch_:
24433 case OPT_fdiagnostics_show_location_:
24434 case OPT_fdiagnostics_show_option:
24435 case OPT_fdiagnostics_show_caret:
24436 case OPT_fdiagnostics_show_labels:
24437 case OPT_fdiagnostics_show_line_numbers:
24438 case OPT_fdiagnostics_color_:
24439 case OPT_fdiagnostics_format_:
24440 case OPT_fverbose_asm:
24441 case OPT____:
24442 case OPT__sysroot_:
24443 case OPT_nostdinc:
24444 case OPT_nostdinc__:
24445 case OPT_fpreprocessed:
24446 case OPT_fltrans_output_list_:
24447 case OPT_fresolution_:
24448 case OPT_fdebug_prefix_map_:
24449 case OPT_fmacro_prefix_map_:
24450 case OPT_ffile_prefix_map_:
24451 case OPT_fcompare_debug:
24452 case OPT_fchecking:
24453 case OPT_fchecking_:
24454 /* Ignore these. */
24455 continue;
24456 case OPT_flto_:
24458 const char *lto_canonical = "-flto";
24459 switches.safe_push (lto_canonical);
24460 len += strlen (lto_canonical) + 1;
24461 break;
24463 default:
24464 if (cl_options[save_decoded_options[j].opt_index].flags
24465 & CL_NO_DWARF_RECORD)
24466 continue;
24467 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
24468 == '-');
24469 switch (save_decoded_options[j].canonical_option[0][1])
24471 case 'M':
24472 case 'i':
24473 case 'W':
24474 continue;
24475 case 'f':
24476 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
24477 "dump", 4) == 0)
24478 continue;
24479 break;
24480 default:
24481 break;
24483 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
24484 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
24485 break;
24488 producer = XNEWVEC (char, plen + 1 + len + 1);
24489 tail = producer;
24490 sprintf (tail, "%s %s", language_string, version_string);
24491 tail += plen;
24493 FOR_EACH_VEC_ELT (switches, j, p)
24495 len = strlen (p);
24496 *tail = ' ';
24497 memcpy (tail + 1, p, len);
24498 tail += len + 1;
24501 *tail = '\0';
24502 return producer;
24505 /* Given a C and/or C++ language/version string return the "highest".
24506 C++ is assumed to be "higher" than C in this case. Used for merging
24507 LTO translation unit languages. */
24508 static const char *
24509 highest_c_language (const char *lang1, const char *lang2)
24511 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24512 return "GNU C++17";
24513 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24514 return "GNU C++14";
24515 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24516 return "GNU C++11";
24517 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24518 return "GNU C++98";
24520 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24521 return "GNU C2X";
24522 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24523 return "GNU C17";
24524 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24525 return "GNU C11";
24526 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24527 return "GNU C99";
24528 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24529 return "GNU C89";
24531 gcc_unreachable ();
24535 /* Generate the DIE for the compilation unit. */
24537 static dw_die_ref
24538 gen_compile_unit_die (const char *filename)
24540 dw_die_ref die;
24541 const char *language_string = lang_hooks.name;
24542 int language;
24544 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24546 if (filename)
24548 add_filename_attribute (die, filename);
24549 /* Don't add cwd for <built-in>. */
24550 if (filename[0] != '<')
24551 add_comp_dir_attribute (die);
24554 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24556 /* If our producer is LTO try to figure out a common language to use
24557 from the global list of translation units. */
24558 if (strcmp (language_string, "GNU GIMPLE") == 0)
24560 unsigned i;
24561 tree t;
24562 const char *common_lang = NULL;
24564 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24566 if (!TRANSLATION_UNIT_LANGUAGE (t))
24567 continue;
24568 if (!common_lang)
24569 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24570 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24572 else if (strncmp (common_lang, "GNU C", 5) == 0
24573 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
24574 /* Mixing C and C++ is ok, use C++ in that case. */
24575 common_lang = highest_c_language (common_lang,
24576 TRANSLATION_UNIT_LANGUAGE (t));
24577 else
24579 /* Fall back to C. */
24580 common_lang = NULL;
24581 break;
24585 if (common_lang)
24586 language_string = common_lang;
24589 language = DW_LANG_C;
24590 if (strncmp (language_string, "GNU C", 5) == 0
24591 && ISDIGIT (language_string[5]))
24593 language = DW_LANG_C89;
24594 if (dwarf_version >= 3 || !dwarf_strict)
24596 if (strcmp (language_string, "GNU C89") != 0)
24597 language = DW_LANG_C99;
24599 if (dwarf_version >= 5 /* || !dwarf_strict */)
24600 if (strcmp (language_string, "GNU C11") == 0
24601 || strcmp (language_string, "GNU C17") == 0
24602 || strcmp (language_string, "GNU C2X"))
24603 language = DW_LANG_C11;
24606 else if (strncmp (language_string, "GNU C++", 7) == 0)
24608 language = DW_LANG_C_plus_plus;
24609 if (dwarf_version >= 5 /* || !dwarf_strict */)
24611 if (strcmp (language_string, "GNU C++11") == 0)
24612 language = DW_LANG_C_plus_plus_11;
24613 else if (strcmp (language_string, "GNU C++14") == 0)
24614 language = DW_LANG_C_plus_plus_14;
24615 else if (strcmp (language_string, "GNU C++17") == 0)
24616 /* For now. */
24617 language = DW_LANG_C_plus_plus_14;
24620 else if (strcmp (language_string, "GNU F77") == 0)
24621 language = DW_LANG_Fortran77;
24622 else if (dwarf_version >= 3 || !dwarf_strict)
24624 if (strcmp (language_string, "GNU Ada") == 0)
24625 language = DW_LANG_Ada95;
24626 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24628 language = DW_LANG_Fortran95;
24629 if (dwarf_version >= 5 /* || !dwarf_strict */)
24631 if (strcmp (language_string, "GNU Fortran2003") == 0)
24632 language = DW_LANG_Fortran03;
24633 else if (strcmp (language_string, "GNU Fortran2008") == 0)
24634 language = DW_LANG_Fortran08;
24637 else if (strcmp (language_string, "GNU Objective-C") == 0)
24638 language = DW_LANG_ObjC;
24639 else if (strcmp (language_string, "GNU Objective-C++") == 0)
24640 language = DW_LANG_ObjC_plus_plus;
24641 else if (strcmp (language_string, "GNU D") == 0)
24642 language = DW_LANG_D;
24643 else if (dwarf_version >= 5 || !dwarf_strict)
24645 if (strcmp (language_string, "GNU Go") == 0)
24646 language = DW_LANG_Go;
24649 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
24650 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24651 language = DW_LANG_Fortran90;
24652 /* Likewise for Ada. */
24653 else if (strcmp (language_string, "GNU Ada") == 0)
24654 language = DW_LANG_Ada83;
24656 add_AT_unsigned (die, DW_AT_language, language);
24658 switch (language)
24660 case DW_LANG_Fortran77:
24661 case DW_LANG_Fortran90:
24662 case DW_LANG_Fortran95:
24663 case DW_LANG_Fortran03:
24664 case DW_LANG_Fortran08:
24665 /* Fortran has case insensitive identifiers and the front-end
24666 lowercases everything. */
24667 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
24668 break;
24669 default:
24670 /* The default DW_ID_case_sensitive doesn't need to be specified. */
24671 break;
24673 return die;
24676 /* Generate the DIE for a base class. */
24678 static void
24679 gen_inheritance_die (tree binfo, tree access, tree type,
24680 dw_die_ref context_die)
24682 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
24683 struct vlr_context ctx = { type, NULL };
24685 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
24686 context_die);
24687 add_data_member_location_attribute (die, binfo, &ctx);
24689 if (BINFO_VIRTUAL_P (binfo))
24690 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
24692 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
24693 children, otherwise the default is DW_ACCESS_public. In DWARF2
24694 the default has always been DW_ACCESS_private. */
24695 if (access == access_public_node)
24697 if (dwarf_version == 2
24698 || context_die->die_tag == DW_TAG_class_type)
24699 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
24701 else if (access == access_protected_node)
24702 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
24703 else if (dwarf_version > 2
24704 && context_die->die_tag != DW_TAG_class_type)
24705 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
24708 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
24709 structure. */
24711 static bool
24712 is_variant_part (tree decl)
24714 return (TREE_CODE (decl) == FIELD_DECL
24715 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
24718 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
24719 return the FIELD_DECL. Return NULL_TREE otherwise. */
24721 static tree
24722 analyze_discr_in_predicate (tree operand, tree struct_type)
24724 while (CONVERT_EXPR_P (operand))
24725 operand = TREE_OPERAND (operand, 0);
24727 /* Match field access to members of struct_type only. */
24728 if (TREE_CODE (operand) == COMPONENT_REF
24729 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
24730 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
24731 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
24732 return TREE_OPERAND (operand, 1);
24733 else
24734 return NULL_TREE;
24737 /* Check that SRC is a constant integer that can be represented as a native
24738 integer constant (either signed or unsigned). If so, store it into DEST and
24739 return true. Return false otherwise. */
24741 static bool
24742 get_discr_value (tree src, dw_discr_value *dest)
24744 tree discr_type = TREE_TYPE (src);
24746 if (lang_hooks.types.get_debug_type)
24748 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
24749 if (debug_type != NULL)
24750 discr_type = debug_type;
24753 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
24754 return false;
24756 /* Signedness can vary between the original type and the debug type. This
24757 can happen for character types in Ada for instance: the character type
24758 used for code generation can be signed, to be compatible with the C one,
24759 but from a debugger point of view, it must be unsigned. */
24760 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
24761 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
24763 if (is_orig_unsigned != is_debug_unsigned)
24764 src = fold_convert (discr_type, src);
24766 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
24767 return false;
24769 dest->pos = is_debug_unsigned;
24770 if (is_debug_unsigned)
24771 dest->v.uval = tree_to_uhwi (src);
24772 else
24773 dest->v.sval = tree_to_shwi (src);
24775 return true;
24778 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
24779 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
24780 store NULL_TREE in DISCR_DECL. Otherwise:
24782 - store the discriminant field in STRUCT_TYPE that controls the variant
24783 part to *DISCR_DECL
24785 - put in *DISCR_LISTS_P an array where for each variant, the item
24786 represents the corresponding matching list of discriminant values.
24788 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
24789 the above array.
24791 Note that when the array is allocated (i.e. when the analysis is
24792 successful), it is up to the caller to free the array. */
24794 static void
24795 analyze_variants_discr (tree variant_part_decl,
24796 tree struct_type,
24797 tree *discr_decl,
24798 dw_discr_list_ref **discr_lists_p,
24799 unsigned *discr_lists_length)
24801 tree variant_part_type = TREE_TYPE (variant_part_decl);
24802 tree variant;
24803 dw_discr_list_ref *discr_lists;
24804 unsigned i;
24806 /* Compute how many variants there are in this variant part. */
24807 *discr_lists_length = 0;
24808 for (variant = TYPE_FIELDS (variant_part_type);
24809 variant != NULL_TREE;
24810 variant = DECL_CHAIN (variant))
24811 ++*discr_lists_length;
24813 *discr_decl = NULL_TREE;
24814 *discr_lists_p
24815 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
24816 sizeof (**discr_lists_p));
24817 discr_lists = *discr_lists_p;
24819 /* And then analyze all variants to extract discriminant information for all
24820 of them. This analysis is conservative: as soon as we detect something we
24821 do not support, abort everything and pretend we found nothing. */
24822 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
24823 variant != NULL_TREE;
24824 variant = DECL_CHAIN (variant), ++i)
24826 tree match_expr = DECL_QUALIFIER (variant);
24828 /* Now, try to analyze the predicate and deduce a discriminant for
24829 it. */
24830 if (match_expr == boolean_true_node)
24831 /* Typically happens for the default variant: it matches all cases that
24832 previous variants rejected. Don't output any matching value for
24833 this one. */
24834 continue;
24836 /* The following loop tries to iterate over each discriminant
24837 possibility: single values or ranges. */
24838 while (match_expr != NULL_TREE)
24840 tree next_round_match_expr;
24841 tree candidate_discr = NULL_TREE;
24842 dw_discr_list_ref new_node = NULL;
24844 /* Possibilities are matched one after the other by nested
24845 TRUTH_ORIF_EXPR expressions. Process the current possibility and
24846 continue with the rest at next iteration. */
24847 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
24849 next_round_match_expr = TREE_OPERAND (match_expr, 0);
24850 match_expr = TREE_OPERAND (match_expr, 1);
24852 else
24853 next_round_match_expr = NULL_TREE;
24855 if (match_expr == boolean_false_node)
24856 /* This sub-expression matches nothing: just wait for the next
24857 one. */
24860 else if (TREE_CODE (match_expr) == EQ_EXPR)
24862 /* We are matching: <discr_field> == <integer_cst>
24863 This sub-expression matches a single value. */
24864 tree integer_cst = TREE_OPERAND (match_expr, 1);
24866 candidate_discr
24867 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
24868 struct_type);
24870 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24871 if (!get_discr_value (integer_cst,
24872 &new_node->dw_discr_lower_bound))
24873 goto abort;
24874 new_node->dw_discr_range = false;
24877 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
24879 /* We are matching:
24880 <discr_field> > <integer_cst>
24881 && <discr_field> < <integer_cst>.
24882 This sub-expression matches the range of values between the
24883 two matched integer constants. Note that comparisons can be
24884 inclusive or exclusive. */
24885 tree candidate_discr_1, candidate_discr_2;
24886 tree lower_cst, upper_cst;
24887 bool lower_cst_included, upper_cst_included;
24888 tree lower_op = TREE_OPERAND (match_expr, 0);
24889 tree upper_op = TREE_OPERAND (match_expr, 1);
24891 /* When the comparison is exclusive, the integer constant is not
24892 the discriminant range bound we are looking for: we will have
24893 to increment or decrement it. */
24894 if (TREE_CODE (lower_op) == GE_EXPR)
24895 lower_cst_included = true;
24896 else if (TREE_CODE (lower_op) == GT_EXPR)
24897 lower_cst_included = false;
24898 else
24899 goto abort;
24901 if (TREE_CODE (upper_op) == LE_EXPR)
24902 upper_cst_included = true;
24903 else if (TREE_CODE (upper_op) == LT_EXPR)
24904 upper_cst_included = false;
24905 else
24906 goto abort;
24908 /* Extract the discriminant from the first operand and check it
24909 is consistant with the same analysis in the second
24910 operand. */
24911 candidate_discr_1
24912 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
24913 struct_type);
24914 candidate_discr_2
24915 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
24916 struct_type);
24917 if (candidate_discr_1 == candidate_discr_2)
24918 candidate_discr = candidate_discr_1;
24919 else
24920 goto abort;
24922 /* Extract bounds from both. */
24923 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24924 lower_cst = TREE_OPERAND (lower_op, 1);
24925 upper_cst = TREE_OPERAND (upper_op, 1);
24927 if (!lower_cst_included)
24928 lower_cst
24929 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
24930 build_int_cst (TREE_TYPE (lower_cst), 1));
24931 if (!upper_cst_included)
24932 upper_cst
24933 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
24934 build_int_cst (TREE_TYPE (upper_cst), 1));
24936 if (!get_discr_value (lower_cst,
24937 &new_node->dw_discr_lower_bound)
24938 || !get_discr_value (upper_cst,
24939 &new_node->dw_discr_upper_bound))
24940 goto abort;
24942 new_node->dw_discr_range = true;
24945 else if ((candidate_discr
24946 = analyze_discr_in_predicate (match_expr, struct_type))
24947 && (TREE_TYPE (candidate_discr) == boolean_type_node
24948 || TREE_TYPE (TREE_TYPE (candidate_discr))
24949 == boolean_type_node))
24951 /* We are matching: <discr_field> for a boolean discriminant.
24952 This sub-expression matches boolean_true_node. */
24953 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24954 if (!get_discr_value (boolean_true_node,
24955 &new_node->dw_discr_lower_bound))
24956 goto abort;
24957 new_node->dw_discr_range = false;
24960 else
24961 /* Unsupported sub-expression: we cannot determine the set of
24962 matching discriminant values. Abort everything. */
24963 goto abort;
24965 /* If the discriminant info is not consistant with what we saw so
24966 far, consider the analysis failed and abort everything. */
24967 if (candidate_discr == NULL_TREE
24968 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
24969 goto abort;
24970 else
24971 *discr_decl = candidate_discr;
24973 if (new_node != NULL)
24975 new_node->dw_discr_next = discr_lists[i];
24976 discr_lists[i] = new_node;
24978 match_expr = next_round_match_expr;
24982 /* If we reach this point, we could match everything we were interested
24983 in. */
24984 return;
24986 abort:
24987 /* Clean all data structure and return no result. */
24988 free (*discr_lists_p);
24989 *discr_lists_p = NULL;
24990 *discr_decl = NULL_TREE;
24993 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
24994 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
24995 under CONTEXT_DIE.
24997 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
24998 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
24999 this type, which are record types, represent the available variants and each
25000 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25001 values are inferred from these attributes.
25003 In trees, the offsets for the fields inside these sub-records are relative
25004 to the variant part itself, whereas the corresponding DIEs should have
25005 offset attributes that are relative to the embedding record base address.
25006 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25007 must be an expression that computes the offset of the variant part to
25008 describe in DWARF. */
25010 static void
25011 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25012 dw_die_ref context_die)
25014 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25015 tree variant_part_offset = vlr_ctx->variant_part_offset;
25016 struct loc_descr_context ctx = {
25017 vlr_ctx->struct_type, /* context_type */
25018 NULL_TREE, /* base_decl */
25019 NULL, /* dpi */
25020 false, /* placeholder_arg */
25021 false /* placeholder_seen */
25024 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25025 NULL_TREE if there is no such field. */
25026 tree discr_decl = NULL_TREE;
25027 dw_discr_list_ref *discr_lists;
25028 unsigned discr_lists_length = 0;
25029 unsigned i;
25031 dw_die_ref dwarf_proc_die = NULL;
25032 dw_die_ref variant_part_die
25033 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25035 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25037 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25038 &discr_decl, &discr_lists, &discr_lists_length);
25040 if (discr_decl != NULL_TREE)
25042 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25044 if (discr_die)
25045 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25046 else
25047 /* We have no DIE for the discriminant, so just discard all
25048 discrimimant information in the output. */
25049 discr_decl = NULL_TREE;
25052 /* If the offset for this variant part is more complex than a constant,
25053 create a DWARF procedure for it so that we will not have to generate DWARF
25054 expressions for it for each member. */
25055 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25056 && (dwarf_version >= 3 || !dwarf_strict))
25058 const tree dwarf_proc_fndecl
25059 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25060 build_function_type (TREE_TYPE (variant_part_offset),
25061 NULL_TREE));
25062 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25063 const dw_loc_descr_ref dwarf_proc_body
25064 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25066 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25067 dwarf_proc_fndecl, context_die);
25068 if (dwarf_proc_die != NULL)
25069 variant_part_offset = dwarf_proc_call;
25072 /* Output DIEs for all variants. */
25073 i = 0;
25074 for (tree variant = TYPE_FIELDS (variant_part_type);
25075 variant != NULL_TREE;
25076 variant = DECL_CHAIN (variant), ++i)
25078 tree variant_type = TREE_TYPE (variant);
25079 dw_die_ref variant_die;
25081 /* All variants (i.e. members of a variant part) are supposed to be
25082 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25083 under these records. */
25084 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25086 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25087 equate_decl_number_to_die (variant, variant_die);
25089 /* Output discriminant values this variant matches, if any. */
25090 if (discr_decl == NULL || discr_lists[i] == NULL)
25091 /* In the case we have discriminant information at all, this is
25092 probably the default variant: as the standard says, don't
25093 output any discriminant value/list attribute. */
25095 else if (discr_lists[i]->dw_discr_next == NULL
25096 && !discr_lists[i]->dw_discr_range)
25097 /* If there is only one accepted value, don't bother outputting a
25098 list. */
25099 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25100 else
25101 add_discr_list (variant_die, discr_lists[i]);
25103 for (tree member = TYPE_FIELDS (variant_type);
25104 member != NULL_TREE;
25105 member = DECL_CHAIN (member))
25107 struct vlr_context vlr_sub_ctx = {
25108 vlr_ctx->struct_type, /* struct_type */
25109 NULL /* variant_part_offset */
25111 if (is_variant_part (member))
25113 /* All offsets for fields inside variant parts are relative to
25114 the top-level embedding RECORD_TYPE's base address. On the
25115 other hand, offsets in GCC's types are relative to the
25116 nested-most variant part. So we have to sum offsets each time
25117 we recurse. */
25119 vlr_sub_ctx.variant_part_offset
25120 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25121 variant_part_offset, byte_position (member));
25122 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25124 else
25126 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25127 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25132 free (discr_lists);
25135 /* Generate a DIE for a class member. */
25137 static void
25138 gen_member_die (tree type, dw_die_ref context_die)
25140 tree member;
25141 tree binfo = TYPE_BINFO (type);
25143 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25145 /* If this is not an incomplete type, output descriptions of each of its
25146 members. Note that as we output the DIEs necessary to represent the
25147 members of this record or union type, we will also be trying to output
25148 DIEs to represent the *types* of those members. However the `type'
25149 function (above) will specifically avoid generating type DIEs for member
25150 types *within* the list of member DIEs for this (containing) type except
25151 for those types (of members) which are explicitly marked as also being
25152 members of this (containing) type themselves. The g++ front- end can
25153 force any given type to be treated as a member of some other (containing)
25154 type by setting the TYPE_CONTEXT of the given (member) type to point to
25155 the TREE node representing the appropriate (containing) type. */
25157 /* First output info about the base classes. */
25158 if (binfo && early_dwarf)
25160 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25161 int i;
25162 tree base;
25164 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25165 gen_inheritance_die (base,
25166 (accesses ? (*accesses)[i] : access_public_node),
25167 type,
25168 context_die);
25171 /* Now output info about the members. */
25172 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25174 /* Ignore clones. */
25175 if (DECL_ABSTRACT_ORIGIN (member))
25176 continue;
25178 struct vlr_context vlr_ctx = { type, NULL_TREE };
25179 bool static_inline_p
25180 = (VAR_P (member)
25181 && TREE_STATIC (member)
25182 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25183 != -1));
25185 /* If we thought we were generating minimal debug info for TYPE
25186 and then changed our minds, some of the member declarations
25187 may have already been defined. Don't define them again, but
25188 do put them in the right order. */
25190 if (dw_die_ref child = lookup_decl_die (member))
25192 /* Handle inline static data members, which only have in-class
25193 declarations. */
25194 bool splice = true;
25196 dw_die_ref ref = NULL;
25197 if (child->die_tag == DW_TAG_variable
25198 && child->die_parent == comp_unit_die ())
25200 ref = get_AT_ref (child, DW_AT_specification);
25202 /* For C++17 inline static data members followed by redundant
25203 out of class redeclaration, we might get here with
25204 child being the DIE created for the out of class
25205 redeclaration and with its DW_AT_specification being
25206 the DIE created for in-class definition. We want to
25207 reparent the latter, and don't want to create another
25208 DIE with DW_AT_specification in that case, because
25209 we already have one. */
25210 if (ref
25211 && static_inline_p
25212 && ref->die_tag == DW_TAG_variable
25213 && ref->die_parent == comp_unit_die ()
25214 && get_AT (ref, DW_AT_specification) == NULL)
25216 child = ref;
25217 ref = NULL;
25218 static_inline_p = false;
25221 if (!ref)
25223 reparent_child (child, context_die);
25224 if (dwarf_version < 5)
25225 child->die_tag = DW_TAG_member;
25226 splice = false;
25230 if (splice)
25231 splice_child_die (context_die, child);
25234 /* Do not generate standard DWARF for variant parts if we are generating
25235 the corresponding GNAT encodings: DIEs generated for both would
25236 conflict in our mappings. */
25237 else if (is_variant_part (member)
25238 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
25240 vlr_ctx.variant_part_offset = byte_position (member);
25241 gen_variant_part (member, &vlr_ctx, context_die);
25243 else
25245 vlr_ctx.variant_part_offset = NULL_TREE;
25246 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25249 /* For C++ inline static data members emit immediately a DW_TAG_variable
25250 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25251 DW_AT_specification. */
25252 if (static_inline_p)
25254 int old_extern = DECL_EXTERNAL (member);
25255 DECL_EXTERNAL (member) = 0;
25256 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25257 DECL_EXTERNAL (member) = old_extern;
25262 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25263 is set, we pretend that the type was never defined, so we only get the
25264 member DIEs needed by later specification DIEs. */
25266 static void
25267 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25268 enum debug_info_usage usage)
25270 if (TREE_ASM_WRITTEN (type))
25272 /* Fill in the bound of variable-length fields in late dwarf if
25273 still incomplete. */
25274 if (!early_dwarf && variably_modified_type_p (type, NULL))
25275 for (tree member = TYPE_FIELDS (type);
25276 member;
25277 member = DECL_CHAIN (member))
25278 fill_variable_array_bounds (TREE_TYPE (member));
25279 return;
25282 dw_die_ref type_die = lookup_type_die (type);
25283 dw_die_ref scope_die = 0;
25284 int nested = 0;
25285 int complete = (TYPE_SIZE (type)
25286 && (! TYPE_STUB_DECL (type)
25287 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25288 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25289 complete = complete && should_emit_struct_debug (type, usage);
25291 if (type_die && ! complete)
25292 return;
25294 if (TYPE_CONTEXT (type) != NULL_TREE
25295 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25296 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25297 nested = 1;
25299 scope_die = scope_die_for (type, context_die);
25301 /* Generate child dies for template paramaters. */
25302 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25303 schedule_generic_params_dies_gen (type);
25305 if (! type_die || (nested && is_cu_die (scope_die)))
25306 /* First occurrence of type or toplevel definition of nested class. */
25308 dw_die_ref old_die = type_die;
25310 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25311 ? record_type_tag (type) : DW_TAG_union_type,
25312 scope_die, type);
25313 equate_type_number_to_die (type, type_die);
25314 if (old_die)
25315 add_AT_specification (type_die, old_die);
25316 else
25317 add_name_attribute (type_die, type_tag (type));
25319 else
25320 remove_AT (type_die, DW_AT_declaration);
25322 /* If this type has been completed, then give it a byte_size attribute and
25323 then give a list of members. */
25324 if (complete && !ns_decl)
25326 /* Prevent infinite recursion in cases where the type of some member of
25327 this type is expressed in terms of this type itself. */
25328 TREE_ASM_WRITTEN (type) = 1;
25329 add_byte_size_attribute (type_die, type);
25330 add_alignment_attribute (type_die, type);
25331 if (TYPE_STUB_DECL (type) != NULL_TREE)
25333 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25334 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25337 /* If the first reference to this type was as the return type of an
25338 inline function, then it may not have a parent. Fix this now. */
25339 if (type_die->die_parent == NULL)
25340 add_child_die (scope_die, type_die);
25342 gen_member_die (type, type_die);
25344 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25345 if (TYPE_ARTIFICIAL (type))
25346 add_AT_flag (type_die, DW_AT_artificial, 1);
25348 /* GNU extension: Record what type our vtable lives in. */
25349 if (TYPE_VFIELD (type))
25351 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25353 gen_type_die (vtype, context_die);
25354 add_AT_die_ref (type_die, DW_AT_containing_type,
25355 lookup_type_die (vtype));
25358 else
25360 add_AT_flag (type_die, DW_AT_declaration, 1);
25362 /* We don't need to do this for function-local types. */
25363 if (TYPE_STUB_DECL (type)
25364 && ! decl_function_context (TYPE_STUB_DECL (type)))
25365 vec_safe_push (incomplete_types, type);
25368 if (get_AT (type_die, DW_AT_name))
25369 add_pubtype (type, type_die);
25372 /* Generate a DIE for a subroutine _type_. */
25374 static void
25375 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25377 tree return_type = TREE_TYPE (type);
25378 dw_die_ref subr_die
25379 = new_die (DW_TAG_subroutine_type,
25380 scope_die_for (type, context_die), type);
25382 equate_type_number_to_die (type, subr_die);
25383 add_prototyped_attribute (subr_die, type);
25384 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25385 context_die);
25386 add_alignment_attribute (subr_die, type);
25387 gen_formal_types_die (type, subr_die);
25389 if (get_AT (subr_die, DW_AT_name))
25390 add_pubtype (type, subr_die);
25391 if ((dwarf_version >= 5 || !dwarf_strict)
25392 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25393 add_AT_flag (subr_die, DW_AT_reference, 1);
25394 if ((dwarf_version >= 5 || !dwarf_strict)
25395 && lang_hooks.types.type_dwarf_attribute (type,
25396 DW_AT_rvalue_reference) != -1)
25397 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25400 /* Generate a DIE for a type definition. */
25402 static void
25403 gen_typedef_die (tree decl, dw_die_ref context_die)
25405 dw_die_ref type_die;
25406 tree type;
25408 if (TREE_ASM_WRITTEN (decl))
25410 if (DECL_ORIGINAL_TYPE (decl))
25411 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25412 return;
25415 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25416 checks in process_scope_var and modified_type_die), this should be called
25417 only for original types. */
25418 gcc_assert (decl_ultimate_origin (decl) == NULL
25419 || decl_ultimate_origin (decl) == decl);
25421 TREE_ASM_WRITTEN (decl) = 1;
25422 type_die = new_die (DW_TAG_typedef, context_die, decl);
25424 add_name_and_src_coords_attributes (type_die, decl);
25425 if (DECL_ORIGINAL_TYPE (decl))
25427 type = DECL_ORIGINAL_TYPE (decl);
25428 if (type == error_mark_node)
25429 return;
25431 gcc_assert (type != TREE_TYPE (decl));
25432 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25434 else
25436 type = TREE_TYPE (decl);
25437 if (type == error_mark_node)
25438 return;
25440 if (is_naming_typedef_decl (TYPE_NAME (type)))
25442 /* Here, we are in the case of decl being a typedef naming
25443 an anonymous type, e.g:
25444 typedef struct {...} foo;
25445 In that case TREE_TYPE (decl) is not a typedef variant
25446 type and TYPE_NAME of the anonymous type is set to the
25447 TYPE_DECL of the typedef. This construct is emitted by
25448 the C++ FE.
25450 TYPE is the anonymous struct named by the typedef
25451 DECL. As we need the DW_AT_type attribute of the
25452 DW_TAG_typedef to point to the DIE of TYPE, let's
25453 generate that DIE right away. add_type_attribute
25454 called below will then pick (via lookup_type_die) that
25455 anonymous struct DIE. */
25456 if (!TREE_ASM_WRITTEN (type))
25457 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25459 /* This is a GNU Extension. We are adding a
25460 DW_AT_linkage_name attribute to the DIE of the
25461 anonymous struct TYPE. The value of that attribute
25462 is the name of the typedef decl naming the anonymous
25463 struct. This greatly eases the work of consumers of
25464 this debug info. */
25465 add_linkage_name_raw (lookup_type_die (type), decl);
25469 add_type_attribute (type_die, type, decl_quals (decl), false,
25470 context_die);
25472 if (is_naming_typedef_decl (decl))
25473 /* We want that all subsequent calls to lookup_type_die with
25474 TYPE in argument yield the DW_TAG_typedef we have just
25475 created. */
25476 equate_type_number_to_die (type, type_die);
25478 add_alignment_attribute (type_die, TREE_TYPE (decl));
25480 add_accessibility_attribute (type_die, decl);
25482 if (DECL_ABSTRACT_P (decl))
25483 equate_decl_number_to_die (decl, type_die);
25485 if (get_AT (type_die, DW_AT_name))
25486 add_pubtype (decl, type_die);
25489 /* Generate a DIE for a struct, class, enum or union type. */
25491 static void
25492 gen_tagged_type_die (tree type,
25493 dw_die_ref context_die,
25494 enum debug_info_usage usage)
25496 if (type == NULL_TREE
25497 || !is_tagged_type (type))
25498 return;
25500 if (TREE_ASM_WRITTEN (type))
25502 /* If this is a nested type whose containing class hasn't been written
25503 out yet, writing it out will cover this one, too. This does not apply
25504 to instantiations of member class templates; they need to be added to
25505 the containing class as they are generated. FIXME: This hurts the
25506 idea of combining type decls from multiple TUs, since we can't predict
25507 what set of template instantiations we'll get. */
25508 else if (TYPE_CONTEXT (type)
25509 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25510 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25512 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25514 if (TREE_ASM_WRITTEN (type))
25515 return;
25517 /* If that failed, attach ourselves to the stub. */
25518 context_die = lookup_type_die (TYPE_CONTEXT (type));
25520 else if (TYPE_CONTEXT (type) != NULL_TREE
25521 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25523 /* If this type is local to a function that hasn't been written
25524 out yet, use a NULL context for now; it will be fixed up in
25525 decls_for_scope. */
25526 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25527 /* A declaration DIE doesn't count; nested types need to go in the
25528 specification. */
25529 if (context_die && is_declaration_die (context_die))
25530 context_die = NULL;
25532 else
25533 context_die = declare_in_namespace (type, context_die);
25535 if (TREE_CODE (type) == ENUMERAL_TYPE)
25537 /* This might have been written out by the call to
25538 declare_in_namespace. */
25539 if (!TREE_ASM_WRITTEN (type))
25540 gen_enumeration_type_die (type, context_die);
25542 else
25543 gen_struct_or_union_type_die (type, context_die, usage);
25545 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25546 it up if it is ever completed. gen_*_type_die will set it for us
25547 when appropriate. */
25550 /* Generate a type description DIE. */
25552 static void
25553 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25554 enum debug_info_usage usage)
25556 struct array_descr_info info;
25558 if (type == NULL_TREE || type == error_mark_node)
25559 return;
25561 if (flag_checking && type)
25562 verify_type (type);
25564 if (TYPE_NAME (type) != NULL_TREE
25565 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25566 && is_redundant_typedef (TYPE_NAME (type))
25567 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25568 /* The DECL of this type is a typedef we don't want to emit debug
25569 info for but we want debug info for its underlying typedef.
25570 This can happen for e.g, the injected-class-name of a C++
25571 type. */
25572 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25574 /* If TYPE is a typedef type variant, let's generate debug info
25575 for the parent typedef which TYPE is a type of. */
25576 if (typedef_variant_p (type))
25578 if (TREE_ASM_WRITTEN (type))
25579 return;
25581 tree name = TYPE_NAME (type);
25582 tree origin = decl_ultimate_origin (name);
25583 if (origin != NULL && origin != name)
25585 gen_decl_die (origin, NULL, NULL, context_die);
25586 return;
25589 /* Prevent broken recursion; we can't hand off to the same type. */
25590 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25592 /* Give typedefs the right scope. */
25593 context_die = scope_die_for (type, context_die);
25595 TREE_ASM_WRITTEN (type) = 1;
25597 gen_decl_die (name, NULL, NULL, context_die);
25598 return;
25601 /* If type is an anonymous tagged type named by a typedef, let's
25602 generate debug info for the typedef. */
25603 if (is_naming_typedef_decl (TYPE_NAME (type)))
25605 /* Give typedefs the right scope. */
25606 context_die = scope_die_for (type, context_die);
25608 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
25609 return;
25612 if (lang_hooks.types.get_debug_type)
25614 tree debug_type = lang_hooks.types.get_debug_type (type);
25616 if (debug_type != NULL_TREE && debug_type != type)
25618 gen_type_die_with_usage (debug_type, context_die, usage);
25619 return;
25623 /* We are going to output a DIE to represent the unqualified version
25624 of this type (i.e. without any const or volatile qualifiers) so
25625 get the main variant (i.e. the unqualified version) of this type
25626 now. (Vectors and arrays are special because the debugging info is in the
25627 cloned type itself. Similarly function/method types can contain extra
25628 ref-qualification). */
25629 if (TREE_CODE (type) == FUNCTION_TYPE
25630 || TREE_CODE (type) == METHOD_TYPE)
25632 /* For function/method types, can't use type_main_variant here,
25633 because that can have different ref-qualifiers for C++,
25634 but try to canonicalize. */
25635 tree main = TYPE_MAIN_VARIANT (type);
25636 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
25637 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
25638 && check_base_type (t, main)
25639 && check_lang_type (t, type))
25641 type = t;
25642 break;
25645 else if (TREE_CODE (type) != VECTOR_TYPE
25646 && TREE_CODE (type) != ARRAY_TYPE)
25647 type = type_main_variant (type);
25649 /* If this is an array type with hidden descriptor, handle it first. */
25650 if (!TREE_ASM_WRITTEN (type)
25651 && lang_hooks.types.get_array_descr_info)
25653 memset (&info, 0, sizeof (info));
25654 if (lang_hooks.types.get_array_descr_info (type, &info))
25656 /* Fortran sometimes emits array types with no dimension. */
25657 gcc_assert (info.ndimensions >= 0
25658 && (info.ndimensions
25659 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
25660 gen_descr_array_type_die (type, &info, context_die);
25661 TREE_ASM_WRITTEN (type) = 1;
25662 return;
25666 if (TREE_ASM_WRITTEN (type))
25668 /* Variable-length types may be incomplete even if
25669 TREE_ASM_WRITTEN. For such types, fall through to
25670 gen_array_type_die() and possibly fill in
25671 DW_AT_{upper,lower}_bound attributes. */
25672 if ((TREE_CODE (type) != ARRAY_TYPE
25673 && TREE_CODE (type) != RECORD_TYPE
25674 && TREE_CODE (type) != UNION_TYPE
25675 && TREE_CODE (type) != QUAL_UNION_TYPE)
25676 || !variably_modified_type_p (type, NULL))
25677 return;
25680 switch (TREE_CODE (type))
25682 case ERROR_MARK:
25683 break;
25685 case POINTER_TYPE:
25686 case REFERENCE_TYPE:
25687 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
25688 ensures that the gen_type_die recursion will terminate even if the
25689 type is recursive. Recursive types are possible in Ada. */
25690 /* ??? We could perhaps do this for all types before the switch
25691 statement. */
25692 TREE_ASM_WRITTEN (type) = 1;
25694 /* For these types, all that is required is that we output a DIE (or a
25695 set of DIEs) to represent the "basis" type. */
25696 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25697 DINFO_USAGE_IND_USE);
25698 break;
25700 case OFFSET_TYPE:
25701 /* This code is used for C++ pointer-to-data-member types.
25702 Output a description of the relevant class type. */
25703 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
25704 DINFO_USAGE_IND_USE);
25706 /* Output a description of the type of the object pointed to. */
25707 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25708 DINFO_USAGE_IND_USE);
25710 /* Now output a DIE to represent this pointer-to-data-member type
25711 itself. */
25712 gen_ptr_to_mbr_type_die (type, context_die);
25713 break;
25715 case FUNCTION_TYPE:
25716 /* Force out return type (in case it wasn't forced out already). */
25717 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25718 DINFO_USAGE_DIR_USE);
25719 gen_subroutine_type_die (type, context_die);
25720 break;
25722 case METHOD_TYPE:
25723 /* Force out return type (in case it wasn't forced out already). */
25724 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25725 DINFO_USAGE_DIR_USE);
25726 gen_subroutine_type_die (type, context_die);
25727 break;
25729 case ARRAY_TYPE:
25730 case VECTOR_TYPE:
25731 gen_array_type_die (type, context_die);
25732 break;
25734 case ENUMERAL_TYPE:
25735 case RECORD_TYPE:
25736 case UNION_TYPE:
25737 case QUAL_UNION_TYPE:
25738 gen_tagged_type_die (type, context_die, usage);
25739 return;
25741 case VOID_TYPE:
25742 case INTEGER_TYPE:
25743 case REAL_TYPE:
25744 case FIXED_POINT_TYPE:
25745 case COMPLEX_TYPE:
25746 case BOOLEAN_TYPE:
25747 /* No DIEs needed for fundamental types. */
25748 break;
25750 case NULLPTR_TYPE:
25751 case LANG_TYPE:
25752 /* Just use DW_TAG_unspecified_type. */
25754 dw_die_ref type_die = lookup_type_die (type);
25755 if (type_die == NULL)
25757 tree name = TYPE_IDENTIFIER (type);
25758 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
25759 type);
25760 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
25761 equate_type_number_to_die (type, type_die);
25764 break;
25766 default:
25767 if (is_cxx_auto (type))
25769 tree name = TYPE_IDENTIFIER (type);
25770 dw_die_ref *die = (name == get_identifier ("auto")
25771 ? &auto_die : &decltype_auto_die);
25772 if (!*die)
25774 *die = new_die (DW_TAG_unspecified_type,
25775 comp_unit_die (), NULL_TREE);
25776 add_name_attribute (*die, IDENTIFIER_POINTER (name));
25778 equate_type_number_to_die (type, *die);
25779 break;
25781 gcc_unreachable ();
25784 TREE_ASM_WRITTEN (type) = 1;
25787 static void
25788 gen_type_die (tree type, dw_die_ref context_die)
25790 if (type != error_mark_node)
25792 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
25793 if (flag_checking)
25795 dw_die_ref die = lookup_type_die (type);
25796 if (die)
25797 check_die (die);
25802 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
25803 things which are local to the given block. */
25805 static void
25806 gen_block_die (tree stmt, dw_die_ref context_die)
25808 int must_output_die = 0;
25809 bool inlined_func;
25811 /* Ignore blocks that are NULL. */
25812 if (stmt == NULL_TREE)
25813 return;
25815 inlined_func = inlined_function_outer_scope_p (stmt);
25817 /* If the block is one fragment of a non-contiguous block, do not
25818 process the variables, since they will have been done by the
25819 origin block. Do process subblocks. */
25820 if (BLOCK_FRAGMENT_ORIGIN (stmt))
25822 tree sub;
25824 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
25825 gen_block_die (sub, context_die);
25827 return;
25830 /* Determine if we need to output any Dwarf DIEs at all to represent this
25831 block. */
25832 if (inlined_func)
25833 /* The outer scopes for inlinings *must* always be represented. We
25834 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
25835 must_output_die = 1;
25836 else if (lookup_block_die (stmt))
25837 /* If we already have a DIE then it was filled early. Meanwhile
25838 we might have pruned all BLOCK_VARS as optimized out but we
25839 still want to generate high/low PC attributes so output it. */
25840 must_output_die = 1;
25841 else if (TREE_USED (stmt)
25842 || TREE_ASM_WRITTEN (stmt))
25844 /* Determine if this block directly contains any "significant"
25845 local declarations which we will need to output DIEs for. */
25846 if (debug_info_level > DINFO_LEVEL_TERSE)
25848 /* We are not in terse mode so any local declaration that
25849 is not ignored for debug purposes counts as being a
25850 "significant" one. */
25851 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
25852 must_output_die = 1;
25853 else
25854 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
25855 if (!DECL_IGNORED_P (var))
25857 must_output_die = 1;
25858 break;
25861 else if (!dwarf2out_ignore_block (stmt))
25862 must_output_die = 1;
25865 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
25866 DIE for any block which contains no significant local declarations at
25867 all. Rather, in such cases we just call `decls_for_scope' so that any
25868 needed Dwarf info for any sub-blocks will get properly generated. Note
25869 that in terse mode, our definition of what constitutes a "significant"
25870 local declaration gets restricted to include only inlined function
25871 instances and local (nested) function definitions. */
25872 if (must_output_die)
25874 if (inlined_func)
25875 gen_inlined_subroutine_die (stmt, context_die);
25876 else
25877 gen_lexical_block_die (stmt, context_die);
25879 else
25880 decls_for_scope (stmt, context_die);
25883 /* Process variable DECL (or variable with origin ORIGIN) within
25884 block STMT and add it to CONTEXT_DIE. */
25885 static void
25886 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
25888 dw_die_ref die;
25889 tree decl_or_origin = decl ? decl : origin;
25891 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
25892 die = lookup_decl_die (decl_or_origin);
25893 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
25895 if (TYPE_DECL_IS_STUB (decl_or_origin))
25896 die = lookup_type_die (TREE_TYPE (decl_or_origin));
25897 else
25898 die = lookup_decl_die (decl_or_origin);
25899 /* Avoid re-creating the DIE late if it was optimized as unused early. */
25900 if (! die && ! early_dwarf)
25901 return;
25903 else
25904 die = NULL;
25906 /* Avoid creating DIEs for local typedefs and concrete static variables that
25907 will only be pruned later. */
25908 if ((origin || decl_ultimate_origin (decl))
25909 && (TREE_CODE (decl_or_origin) == TYPE_DECL
25910 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
25912 origin = decl_ultimate_origin (decl_or_origin);
25913 if (decl && VAR_P (decl) && die != NULL)
25915 die = lookup_decl_die (origin);
25916 if (die != NULL)
25917 equate_decl_number_to_die (decl, die);
25919 return;
25922 if (die != NULL && die->die_parent == NULL)
25923 add_child_die (context_die, die);
25924 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
25926 if (early_dwarf)
25927 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
25928 stmt, context_die);
25930 else
25932 if (decl && DECL_P (decl))
25934 die = lookup_decl_die (decl);
25936 /* Early created DIEs do not have a parent as the decls refer
25937 to the function as DECL_CONTEXT rather than the BLOCK. */
25938 if (die && die->die_parent == NULL)
25940 gcc_assert (in_lto_p);
25941 add_child_die (context_die, die);
25945 gen_decl_die (decl, origin, NULL, context_die);
25949 /* Generate all of the decls declared within a given scope and (recursively)
25950 all of its sub-blocks. */
25952 static void
25953 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
25955 tree decl;
25956 unsigned int i;
25957 tree subblocks;
25959 /* Ignore NULL blocks. */
25960 if (stmt == NULL_TREE)
25961 return;
25963 /* Output the DIEs to represent all of the data objects and typedefs
25964 declared directly within this block but not within any nested
25965 sub-blocks. Also, nested function and tag DIEs have been
25966 generated with a parent of NULL; fix that up now. We don't
25967 have to do this if we're at -g1. */
25968 if (debug_info_level > DINFO_LEVEL_TERSE)
25970 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25971 process_scope_var (stmt, decl, NULL_TREE, context_die);
25972 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25973 origin - avoid doing this twice as we have no good way to see
25974 if we've done it once already. */
25975 if (! early_dwarf)
25976 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25978 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25979 if (decl == current_function_decl)
25980 /* Ignore declarations of the current function, while they
25981 are declarations, gen_subprogram_die would treat them
25982 as definitions again, because they are equal to
25983 current_function_decl and endlessly recurse. */;
25984 else if (TREE_CODE (decl) == FUNCTION_DECL)
25985 process_scope_var (stmt, decl, NULL_TREE, context_die);
25986 else
25987 process_scope_var (stmt, NULL_TREE, decl, context_die);
25991 /* Even if we're at -g1, we need to process the subblocks in order to get
25992 inlined call information. */
25994 /* Output the DIEs to represent all sub-blocks (and the items declared
25995 therein) of this block. */
25996 if (recurse)
25997 for (subblocks = BLOCK_SUBBLOCKS (stmt);
25998 subblocks != NULL;
25999 subblocks = BLOCK_CHAIN (subblocks))
26000 gen_block_die (subblocks, context_die);
26003 /* Is this a typedef we can avoid emitting? */
26005 static bool
26006 is_redundant_typedef (const_tree decl)
26008 if (TYPE_DECL_IS_STUB (decl))
26009 return true;
26011 if (DECL_ARTIFICIAL (decl)
26012 && DECL_CONTEXT (decl)
26013 && is_tagged_type (DECL_CONTEXT (decl))
26014 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26015 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26016 /* Also ignore the artificial member typedef for the class name. */
26017 return true;
26019 return false;
26022 /* Return TRUE if TYPE is a typedef that names a type for linkage
26023 purposes. This kind of typedefs is produced by the C++ FE for
26024 constructs like:
26026 typedef struct {...} foo;
26028 In that case, there is no typedef variant type produced for foo.
26029 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26030 struct type. */
26032 static bool
26033 is_naming_typedef_decl (const_tree decl)
26035 if (decl == NULL_TREE
26036 || TREE_CODE (decl) != TYPE_DECL
26037 || DECL_NAMELESS (decl)
26038 || !is_tagged_type (TREE_TYPE (decl))
26039 || DECL_IS_BUILTIN (decl)
26040 || is_redundant_typedef (decl)
26041 /* It looks like Ada produces TYPE_DECLs that are very similar
26042 to C++ naming typedefs but that have different
26043 semantics. Let's be specific to c++ for now. */
26044 || !is_cxx (decl))
26045 return FALSE;
26047 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26048 && TYPE_NAME (TREE_TYPE (decl)) == decl
26049 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26050 != TYPE_NAME (TREE_TYPE (decl))));
26053 /* Looks up the DIE for a context. */
26055 static inline dw_die_ref
26056 lookup_context_die (tree context)
26058 if (context)
26060 /* Find die that represents this context. */
26061 if (TYPE_P (context))
26063 context = TYPE_MAIN_VARIANT (context);
26064 dw_die_ref ctx = lookup_type_die (context);
26065 if (!ctx)
26066 return NULL;
26067 return strip_naming_typedef (context, ctx);
26069 else
26070 return lookup_decl_die (context);
26072 return comp_unit_die ();
26075 /* Returns the DIE for a context. */
26077 static inline dw_die_ref
26078 get_context_die (tree context)
26080 if (context)
26082 /* Find die that represents this context. */
26083 if (TYPE_P (context))
26085 context = TYPE_MAIN_VARIANT (context);
26086 return strip_naming_typedef (context, force_type_die (context));
26088 else
26089 return force_decl_die (context);
26091 return comp_unit_die ();
26094 /* Returns the DIE for decl. A DIE will always be returned. */
26096 static dw_die_ref
26097 force_decl_die (tree decl)
26099 dw_die_ref decl_die;
26100 unsigned saved_external_flag;
26101 tree save_fn = NULL_TREE;
26102 decl_die = lookup_decl_die (decl);
26103 if (!decl_die)
26105 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26107 decl_die = lookup_decl_die (decl);
26108 if (decl_die)
26109 return decl_die;
26111 switch (TREE_CODE (decl))
26113 case FUNCTION_DECL:
26114 /* Clear current_function_decl, so that gen_subprogram_die thinks
26115 that this is a declaration. At this point, we just want to force
26116 declaration die. */
26117 save_fn = current_function_decl;
26118 current_function_decl = NULL_TREE;
26119 gen_subprogram_die (decl, context_die);
26120 current_function_decl = save_fn;
26121 break;
26123 case VAR_DECL:
26124 /* Set external flag to force declaration die. Restore it after
26125 gen_decl_die() call. */
26126 saved_external_flag = DECL_EXTERNAL (decl);
26127 DECL_EXTERNAL (decl) = 1;
26128 gen_decl_die (decl, NULL, NULL, context_die);
26129 DECL_EXTERNAL (decl) = saved_external_flag;
26130 break;
26132 case NAMESPACE_DECL:
26133 if (dwarf_version >= 3 || !dwarf_strict)
26134 dwarf2out_decl (decl);
26135 else
26136 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26137 decl_die = comp_unit_die ();
26138 break;
26140 case TRANSLATION_UNIT_DECL:
26141 decl_die = comp_unit_die ();
26142 break;
26144 default:
26145 gcc_unreachable ();
26148 /* We should be able to find the DIE now. */
26149 if (!decl_die)
26150 decl_die = lookup_decl_die (decl);
26151 gcc_assert (decl_die);
26154 return decl_die;
26157 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26158 always returned. */
26160 static dw_die_ref
26161 force_type_die (tree type)
26163 dw_die_ref type_die;
26165 type_die = lookup_type_die (type);
26166 if (!type_die)
26168 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26170 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26171 false, context_die);
26172 gcc_assert (type_die);
26174 return type_die;
26177 /* Force out any required namespaces to be able to output DECL,
26178 and return the new context_die for it, if it's changed. */
26180 static dw_die_ref
26181 setup_namespace_context (tree thing, dw_die_ref context_die)
26183 tree context = (DECL_P (thing)
26184 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26185 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26186 /* Force out the namespace. */
26187 context_die = force_decl_die (context);
26189 return context_die;
26192 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26193 type) within its namespace, if appropriate.
26195 For compatibility with older debuggers, namespace DIEs only contain
26196 declarations; all definitions are emitted at CU scope, with
26197 DW_AT_specification pointing to the declaration (like with class
26198 members). */
26200 static dw_die_ref
26201 declare_in_namespace (tree thing, dw_die_ref context_die)
26203 dw_die_ref ns_context;
26205 if (debug_info_level <= DINFO_LEVEL_TERSE)
26206 return context_die;
26208 /* External declarations in the local scope only need to be emitted
26209 once, not once in the namespace and once in the scope.
26211 This avoids declaring the `extern' below in the
26212 namespace DIE as well as in the innermost scope:
26214 namespace S
26216 int i=5;
26217 int foo()
26219 int i=8;
26220 extern int i;
26221 return i;
26225 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26226 return context_die;
26228 /* If this decl is from an inlined function, then don't try to emit it in its
26229 namespace, as we will get confused. It would have already been emitted
26230 when the abstract instance of the inline function was emitted anyways. */
26231 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26232 return context_die;
26234 ns_context = setup_namespace_context (thing, context_die);
26236 if (ns_context != context_die)
26238 if (is_fortran () || is_dlang ())
26239 return ns_context;
26240 if (DECL_P (thing))
26241 gen_decl_die (thing, NULL, NULL, ns_context);
26242 else
26243 gen_type_die (thing, ns_context);
26245 return context_die;
26248 /* Generate a DIE for a namespace or namespace alias. */
26250 static void
26251 gen_namespace_die (tree decl, dw_die_ref context_die)
26253 dw_die_ref namespace_die;
26255 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26256 they are an alias of. */
26257 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26259 /* Output a real namespace or module. */
26260 context_die = setup_namespace_context (decl, comp_unit_die ());
26261 namespace_die = new_die (is_fortran () || is_dlang ()
26262 ? DW_TAG_module : DW_TAG_namespace,
26263 context_die, decl);
26264 /* For Fortran modules defined in different CU don't add src coords. */
26265 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26267 const char *name = dwarf2_name (decl, 0);
26268 if (name)
26269 add_name_attribute (namespace_die, name);
26271 else
26272 add_name_and_src_coords_attributes (namespace_die, decl);
26273 if (DECL_EXTERNAL (decl))
26274 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26275 equate_decl_number_to_die (decl, namespace_die);
26277 else
26279 /* Output a namespace alias. */
26281 /* Force out the namespace we are an alias of, if necessary. */
26282 dw_die_ref origin_die
26283 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26285 if (DECL_FILE_SCOPE_P (decl)
26286 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26287 context_die = setup_namespace_context (decl, comp_unit_die ());
26288 /* Now create the namespace alias DIE. */
26289 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26290 add_name_and_src_coords_attributes (namespace_die, decl);
26291 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26292 equate_decl_number_to_die (decl, namespace_die);
26294 if ((dwarf_version >= 5 || !dwarf_strict)
26295 && lang_hooks.decls.decl_dwarf_attribute (decl,
26296 DW_AT_export_symbols) == 1)
26297 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26299 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26300 if (want_pubnames ())
26301 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26304 /* Generate Dwarf debug information for a decl described by DECL.
26305 The return value is currently only meaningful for PARM_DECLs,
26306 for all other decls it returns NULL.
26308 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26309 It can be NULL otherwise. */
26311 static dw_die_ref
26312 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26313 dw_die_ref context_die)
26315 tree decl_or_origin = decl ? decl : origin;
26316 tree class_origin = NULL, ultimate_origin;
26318 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26319 return NULL;
26321 switch (TREE_CODE (decl_or_origin))
26323 case ERROR_MARK:
26324 break;
26326 case CONST_DECL:
26327 if (!is_fortran () && !is_ada () && !is_dlang ())
26329 /* The individual enumerators of an enum type get output when we output
26330 the Dwarf representation of the relevant enum type itself. */
26331 break;
26334 /* Emit its type. */
26335 gen_type_die (TREE_TYPE (decl), context_die);
26337 /* And its containing namespace. */
26338 context_die = declare_in_namespace (decl, context_die);
26340 gen_const_die (decl, context_die);
26341 break;
26343 case FUNCTION_DECL:
26344 #if 0
26345 /* FIXME */
26346 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26347 on local redeclarations of global functions. That seems broken. */
26348 if (current_function_decl != decl)
26349 /* This is only a declaration. */;
26350 #endif
26352 /* We should have abstract copies already and should not generate
26353 stray type DIEs in late LTO dumping. */
26354 if (! early_dwarf)
26357 /* If we're emitting a clone, emit info for the abstract instance. */
26358 else if (origin || DECL_ORIGIN (decl) != decl)
26359 dwarf2out_abstract_function (origin
26360 ? DECL_ORIGIN (origin)
26361 : DECL_ABSTRACT_ORIGIN (decl));
26363 /* If we're emitting a possibly inlined function emit it as
26364 abstract instance. */
26365 else if (cgraph_function_possibly_inlined_p (decl)
26366 && ! DECL_ABSTRACT_P (decl)
26367 && ! class_or_namespace_scope_p (context_die)
26368 /* dwarf2out_abstract_function won't emit a die if this is just
26369 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26370 that case, because that works only if we have a die. */
26371 && DECL_INITIAL (decl) != NULL_TREE)
26372 dwarf2out_abstract_function (decl);
26374 /* Otherwise we're emitting the primary DIE for this decl. */
26375 else if (debug_info_level > DINFO_LEVEL_TERSE)
26377 /* Before we describe the FUNCTION_DECL itself, make sure that we
26378 have its containing type. */
26379 if (!origin)
26380 origin = decl_class_context (decl);
26381 if (origin != NULL_TREE)
26382 gen_type_die (origin, context_die);
26384 /* And its return type. */
26385 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26387 /* And its virtual context. */
26388 if (DECL_VINDEX (decl) != NULL_TREE)
26389 gen_type_die (DECL_CONTEXT (decl), context_die);
26391 /* Make sure we have a member DIE for decl. */
26392 if (origin != NULL_TREE)
26393 gen_type_die_for_member (origin, decl, context_die);
26395 /* And its containing namespace. */
26396 context_die = declare_in_namespace (decl, context_die);
26399 /* Now output a DIE to represent the function itself. */
26400 if (decl)
26401 gen_subprogram_die (decl, context_die);
26402 break;
26404 case TYPE_DECL:
26405 /* If we are in terse mode, don't generate any DIEs to represent any
26406 actual typedefs. */
26407 if (debug_info_level <= DINFO_LEVEL_TERSE)
26408 break;
26410 /* In the special case of a TYPE_DECL node representing the declaration
26411 of some type tag, if the given TYPE_DECL is marked as having been
26412 instantiated from some other (original) TYPE_DECL node (e.g. one which
26413 was generated within the original definition of an inline function) we
26414 used to generate a special (abbreviated) DW_TAG_structure_type,
26415 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26416 should be actually referencing those DIEs, as variable DIEs with that
26417 type would be emitted already in the abstract origin, so it was always
26418 removed during unused type prunning. Don't add anything in this
26419 case. */
26420 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26421 break;
26423 if (is_redundant_typedef (decl))
26424 gen_type_die (TREE_TYPE (decl), context_die);
26425 else
26426 /* Output a DIE to represent the typedef itself. */
26427 gen_typedef_die (decl, context_die);
26428 break;
26430 case LABEL_DECL:
26431 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26432 gen_label_die (decl, context_die);
26433 break;
26435 case VAR_DECL:
26436 case RESULT_DECL:
26437 /* If we are in terse mode, don't generate any DIEs to represent any
26438 variable declarations or definitions unless it is external. */
26439 if (debug_info_level < DINFO_LEVEL_TERSE
26440 || (debug_info_level == DINFO_LEVEL_TERSE
26441 && !TREE_PUBLIC (decl_or_origin)))
26442 break;
26444 if (debug_info_level > DINFO_LEVEL_TERSE)
26446 /* Avoid generating stray type DIEs during late dwarf dumping.
26447 All types have been dumped early. */
26448 if (early_dwarf
26449 /* ??? But in LTRANS we cannot annotate early created variably
26450 modified type DIEs without copying them and adjusting all
26451 references to them. Dump them again as happens for inlining
26452 which copies both the decl and the types. */
26453 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26454 in VLA bound information for example. */
26455 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26456 current_function_decl)))
26458 /* Output any DIEs that are needed to specify the type of this data
26459 object. */
26460 if (decl_by_reference_p (decl_or_origin))
26461 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26462 else
26463 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26466 if (early_dwarf)
26468 /* And its containing type. */
26469 class_origin = decl_class_context (decl_or_origin);
26470 if (class_origin != NULL_TREE)
26471 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26473 /* And its containing namespace. */
26474 context_die = declare_in_namespace (decl_or_origin, context_die);
26478 /* Now output the DIE to represent the data object itself. This gets
26479 complicated because of the possibility that the VAR_DECL really
26480 represents an inlined instance of a formal parameter for an inline
26481 function. */
26482 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26483 if (ultimate_origin != NULL_TREE
26484 && TREE_CODE (ultimate_origin) == PARM_DECL)
26485 gen_formal_parameter_die (decl, origin,
26486 true /* Emit name attribute. */,
26487 context_die);
26488 else
26489 gen_variable_die (decl, origin, context_die);
26490 break;
26492 case FIELD_DECL:
26493 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26494 /* Ignore the nameless fields that are used to skip bits but handle C++
26495 anonymous unions and structs. */
26496 if (DECL_NAME (decl) != NULL_TREE
26497 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26498 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26500 gen_type_die (member_declared_type (decl), context_die);
26501 gen_field_die (decl, ctx, context_die);
26503 break;
26505 case PARM_DECL:
26506 /* Avoid generating stray type DIEs during late dwarf dumping.
26507 All types have been dumped early. */
26508 if (early_dwarf
26509 /* ??? But in LTRANS we cannot annotate early created variably
26510 modified type DIEs without copying them and adjusting all
26511 references to them. Dump them again as happens for inlining
26512 which copies both the decl and the types. */
26513 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26514 in VLA bound information for example. */
26515 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26516 current_function_decl)))
26518 if (DECL_BY_REFERENCE (decl_or_origin))
26519 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26520 else
26521 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26523 return gen_formal_parameter_die (decl, origin,
26524 true /* Emit name attribute. */,
26525 context_die);
26527 case NAMESPACE_DECL:
26528 if (dwarf_version >= 3 || !dwarf_strict)
26529 gen_namespace_die (decl, context_die);
26530 break;
26532 case IMPORTED_DECL:
26533 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26534 DECL_CONTEXT (decl), context_die);
26535 break;
26537 case NAMELIST_DECL:
26538 gen_namelist_decl (DECL_NAME (decl), context_die,
26539 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26540 break;
26542 default:
26543 /* Probably some frontend-internal decl. Assume we don't care. */
26544 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26545 break;
26548 return NULL;
26551 /* Output initial debug information for global DECL. Called at the
26552 end of the parsing process.
26554 This is the initial debug generation process. As such, the DIEs
26555 generated may be incomplete. A later debug generation pass
26556 (dwarf2out_late_global_decl) will augment the information generated
26557 in this pass (e.g., with complete location info). */
26559 static void
26560 dwarf2out_early_global_decl (tree decl)
26562 set_early_dwarf s;
26564 /* gen_decl_die() will set DECL_ABSTRACT because
26565 cgraph_function_possibly_inlined_p() returns true. This is in
26566 turn will cause DW_AT_inline attributes to be set.
26568 This happens because at early dwarf generation, there is no
26569 cgraph information, causing cgraph_function_possibly_inlined_p()
26570 to return true. Trick cgraph_function_possibly_inlined_p()
26571 while we generate dwarf early. */
26572 bool save = symtab->global_info_ready;
26573 symtab->global_info_ready = true;
26575 /* We don't handle TYPE_DECLs. If required, they'll be reached via
26576 other DECLs and they can point to template types or other things
26577 that dwarf2out can't handle when done via dwarf2out_decl. */
26578 if (TREE_CODE (decl) != TYPE_DECL
26579 && TREE_CODE (decl) != PARM_DECL)
26581 if (TREE_CODE (decl) == FUNCTION_DECL)
26583 tree save_fndecl = current_function_decl;
26585 /* For nested functions, make sure we have DIEs for the parents first
26586 so that all nested DIEs are generated at the proper scope in the
26587 first shot. */
26588 tree context = decl_function_context (decl);
26589 if (context != NULL)
26591 dw_die_ref context_die = lookup_decl_die (context);
26592 current_function_decl = context;
26594 /* Avoid emitting DIEs multiple times, but still process CONTEXT
26595 enough so that it lands in its own context. This avoids type
26596 pruning issues later on. */
26597 if (context_die == NULL || is_declaration_die (context_die))
26598 dwarf2out_early_global_decl (context);
26601 /* Emit an abstract origin of a function first. This happens
26602 with C++ constructor clones for example and makes
26603 dwarf2out_abstract_function happy which requires the early
26604 DIE of the abstract instance to be present. */
26605 tree origin = DECL_ABSTRACT_ORIGIN (decl);
26606 dw_die_ref origin_die;
26607 if (origin != NULL
26608 /* Do not emit the DIE multiple times but make sure to
26609 process it fully here in case we just saw a declaration. */
26610 && ((origin_die = lookup_decl_die (origin)) == NULL
26611 || is_declaration_die (origin_die)))
26613 current_function_decl = origin;
26614 dwarf2out_decl (origin);
26617 /* Emit the DIE for decl but avoid doing that multiple times. */
26618 dw_die_ref old_die;
26619 if ((old_die = lookup_decl_die (decl)) == NULL
26620 || is_declaration_die (old_die))
26622 current_function_decl = decl;
26623 dwarf2out_decl (decl);
26626 current_function_decl = save_fndecl;
26628 else
26629 dwarf2out_decl (decl);
26631 symtab->global_info_ready = save;
26634 /* Return whether EXPR is an expression with the following pattern:
26635 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
26637 static bool
26638 is_trivial_indirect_ref (tree expr)
26640 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
26641 return false;
26643 tree nop = TREE_OPERAND (expr, 0);
26644 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
26645 return false;
26647 tree int_cst = TREE_OPERAND (nop, 0);
26648 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
26651 /* Output debug information for global decl DECL. Called from
26652 toplev.c after compilation proper has finished. */
26654 static void
26655 dwarf2out_late_global_decl (tree decl)
26657 /* Fill-in any location information we were unable to determine
26658 on the first pass. */
26659 if (VAR_P (decl))
26661 dw_die_ref die = lookup_decl_die (decl);
26663 /* We may have to generate full debug late for LTO in case debug
26664 was not enabled at compile-time or the target doesn't support
26665 the LTO early debug scheme. */
26666 if (! die && in_lto_p)
26667 dwarf2out_decl (decl);
26668 else if (die)
26670 /* We get called via the symtab code invoking late_global_decl
26671 for symbols that are optimized out.
26673 Do not add locations for those, except if they have a
26674 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
26675 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
26676 INDIRECT_REF expression, as this could generate relocations to
26677 text symbols in LTO object files, which is invalid. */
26678 varpool_node *node = varpool_node::get (decl);
26679 if ((! node || ! node->definition)
26680 && ! (DECL_HAS_VALUE_EXPR_P (decl)
26681 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
26682 tree_add_const_value_attribute_for_decl (die, decl);
26683 else
26684 add_location_or_const_value_attribute (die, decl, false);
26689 /* Output debug information for type decl DECL. Called from toplev.c
26690 and from language front ends (to record built-in types). */
26691 static void
26692 dwarf2out_type_decl (tree decl, int local)
26694 if (!local)
26696 set_early_dwarf s;
26697 dwarf2out_decl (decl);
26701 /* Output debug information for imported module or decl DECL.
26702 NAME is non-NULL name in the lexical block if the decl has been renamed.
26703 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
26704 that DECL belongs to.
26705 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
26706 static void
26707 dwarf2out_imported_module_or_decl_1 (tree decl,
26708 tree name,
26709 tree lexical_block,
26710 dw_die_ref lexical_block_die)
26712 expanded_location xloc;
26713 dw_die_ref imported_die = NULL;
26714 dw_die_ref at_import_die;
26716 if (TREE_CODE (decl) == IMPORTED_DECL)
26718 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
26719 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
26720 gcc_assert (decl);
26722 else
26723 xloc = expand_location (input_location);
26725 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
26727 at_import_die = force_type_die (TREE_TYPE (decl));
26728 /* For namespace N { typedef void T; } using N::T; base_type_die
26729 returns NULL, but DW_TAG_imported_declaration requires
26730 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
26731 if (!at_import_die)
26733 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
26734 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
26735 at_import_die = lookup_type_die (TREE_TYPE (decl));
26736 gcc_assert (at_import_die);
26739 else
26741 at_import_die = lookup_decl_die (decl);
26742 if (!at_import_die)
26744 /* If we're trying to avoid duplicate debug info, we may not have
26745 emitted the member decl for this field. Emit it now. */
26746 if (TREE_CODE (decl) == FIELD_DECL)
26748 tree type = DECL_CONTEXT (decl);
26750 if (TYPE_CONTEXT (type)
26751 && TYPE_P (TYPE_CONTEXT (type))
26752 && !should_emit_struct_debug (TYPE_CONTEXT (type),
26753 DINFO_USAGE_DIR_USE))
26754 return;
26755 gen_type_die_for_member (type, decl,
26756 get_context_die (TYPE_CONTEXT (type)));
26758 if (TREE_CODE (decl) == NAMELIST_DECL)
26759 at_import_die = gen_namelist_decl (DECL_NAME (decl),
26760 get_context_die (DECL_CONTEXT (decl)),
26761 NULL_TREE);
26762 else
26763 at_import_die = force_decl_die (decl);
26767 if (TREE_CODE (decl) == NAMESPACE_DECL)
26769 if (dwarf_version >= 3 || !dwarf_strict)
26770 imported_die = new_die (DW_TAG_imported_module,
26771 lexical_block_die,
26772 lexical_block);
26773 else
26774 return;
26776 else
26777 imported_die = new_die (DW_TAG_imported_declaration,
26778 lexical_block_die,
26779 lexical_block);
26781 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
26782 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
26783 if (debug_column_info && xloc.column)
26784 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
26785 if (name)
26786 add_AT_string (imported_die, DW_AT_name,
26787 IDENTIFIER_POINTER (name));
26788 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
26791 /* Output debug information for imported module or decl DECL.
26792 NAME is non-NULL name in context if the decl has been renamed.
26793 CHILD is true if decl is one of the renamed decls as part of
26794 importing whole module.
26795 IMPLICIT is set if this hook is called for an implicit import
26796 such as inline namespace. */
26798 static void
26799 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
26800 bool child, bool implicit)
26802 /* dw_die_ref at_import_die; */
26803 dw_die_ref scope_die;
26805 if (debug_info_level <= DINFO_LEVEL_TERSE)
26806 return;
26808 gcc_assert (decl);
26810 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
26811 should be enough, for DWARF4 and older even if we emit as extension
26812 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
26813 for the benefit of consumers unaware of DW_AT_export_symbols. */
26814 if (implicit
26815 && dwarf_version >= 5
26816 && lang_hooks.decls.decl_dwarf_attribute (decl,
26817 DW_AT_export_symbols) == 1)
26818 return;
26820 set_early_dwarf s;
26822 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
26823 We need decl DIE for reference and scope die. First, get DIE for the decl
26824 itself. */
26826 /* Get the scope die for decl context. Use comp_unit_die for global module
26827 or decl. If die is not found for non globals, force new die. */
26828 if (context
26829 && TYPE_P (context)
26830 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
26831 return;
26833 scope_die = get_context_die (context);
26835 if (child)
26837 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
26838 there is nothing we can do, here. */
26839 if (dwarf_version < 3 && dwarf_strict)
26840 return;
26842 gcc_assert (scope_die->die_child);
26843 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
26844 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
26845 scope_die = scope_die->die_child;
26848 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
26849 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
26852 /* Output debug information for namelists. */
26854 static dw_die_ref
26855 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
26857 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
26858 tree value;
26859 unsigned i;
26861 if (debug_info_level <= DINFO_LEVEL_TERSE)
26862 return NULL;
26864 gcc_assert (scope_die != NULL);
26865 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
26866 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
26868 /* If there are no item_decls, we have a nondefining namelist, e.g.
26869 with USE association; hence, set DW_AT_declaration. */
26870 if (item_decls == NULL_TREE)
26872 add_AT_flag (nml_die, DW_AT_declaration, 1);
26873 return nml_die;
26876 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
26878 nml_item_ref_die = lookup_decl_die (value);
26879 if (!nml_item_ref_die)
26880 nml_item_ref_die = force_decl_die (value);
26882 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
26883 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
26885 return nml_die;
26889 /* Write the debugging output for DECL and return the DIE. */
26891 static void
26892 dwarf2out_decl (tree decl)
26894 dw_die_ref context_die = comp_unit_die ();
26896 switch (TREE_CODE (decl))
26898 case ERROR_MARK:
26899 return;
26901 case FUNCTION_DECL:
26902 /* If we're a nested function, initially use a parent of NULL; if we're
26903 a plain function, this will be fixed up in decls_for_scope. If
26904 we're a method, it will be ignored, since we already have a DIE.
26905 Avoid doing this late though since clones of class methods may
26906 otherwise end up in limbo and create type DIEs late. */
26907 if (early_dwarf
26908 && decl_function_context (decl)
26909 /* But if we're in terse mode, we don't care about scope. */
26910 && debug_info_level > DINFO_LEVEL_TERSE)
26911 context_die = NULL;
26912 break;
26914 case VAR_DECL:
26915 /* For local statics lookup proper context die. */
26916 if (local_function_static (decl))
26917 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26919 /* If we are in terse mode, don't generate any DIEs to represent any
26920 variable declarations or definitions unless it is external. */
26921 if (debug_info_level < DINFO_LEVEL_TERSE
26922 || (debug_info_level == DINFO_LEVEL_TERSE
26923 && !TREE_PUBLIC (decl)))
26924 return;
26925 break;
26927 case CONST_DECL:
26928 if (debug_info_level <= DINFO_LEVEL_TERSE)
26929 return;
26930 if (!is_fortran () && !is_ada () && !is_dlang ())
26931 return;
26932 if (TREE_STATIC (decl) && decl_function_context (decl))
26933 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26934 break;
26936 case NAMESPACE_DECL:
26937 case IMPORTED_DECL:
26938 if (debug_info_level <= DINFO_LEVEL_TERSE)
26939 return;
26940 if (lookup_decl_die (decl) != NULL)
26941 return;
26942 break;
26944 case TYPE_DECL:
26945 /* Don't emit stubs for types unless they are needed by other DIEs. */
26946 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
26947 return;
26949 /* Don't bother trying to generate any DIEs to represent any of the
26950 normal built-in types for the language we are compiling. */
26951 if (DECL_IS_BUILTIN (decl))
26952 return;
26954 /* If we are in terse mode, don't generate any DIEs for types. */
26955 if (debug_info_level <= DINFO_LEVEL_TERSE)
26956 return;
26958 /* If we're a function-scope tag, initially use a parent of NULL;
26959 this will be fixed up in decls_for_scope. */
26960 if (decl_function_context (decl))
26961 context_die = NULL;
26963 break;
26965 case NAMELIST_DECL:
26966 break;
26968 default:
26969 return;
26972 gen_decl_die (decl, NULL, NULL, context_die);
26974 if (flag_checking)
26976 dw_die_ref die = lookup_decl_die (decl);
26977 if (die)
26978 check_die (die);
26982 /* Write the debugging output for DECL. */
26984 static void
26985 dwarf2out_function_decl (tree decl)
26987 dwarf2out_decl (decl);
26988 call_arg_locations = NULL;
26989 call_arg_loc_last = NULL;
26990 call_site_count = -1;
26991 tail_call_site_count = -1;
26992 decl_loc_table->empty ();
26993 cached_dw_loc_list_table->empty ();
26996 /* Output a marker (i.e. a label) for the beginning of the generated code for
26997 a lexical block. */
26999 static void
27000 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27001 unsigned int blocknum)
27003 switch_to_section (current_function_section ());
27004 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27007 /* Output a marker (i.e. a label) for the end of the generated code for a
27008 lexical block. */
27010 static void
27011 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27013 switch_to_section (current_function_section ());
27014 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27017 /* Returns nonzero if it is appropriate not to emit any debugging
27018 information for BLOCK, because it doesn't contain any instructions.
27020 Don't allow this for blocks with nested functions or local classes
27021 as we would end up with orphans, and in the presence of scheduling
27022 we may end up calling them anyway. */
27024 static bool
27025 dwarf2out_ignore_block (const_tree block)
27027 tree decl;
27028 unsigned int i;
27030 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27031 if (TREE_CODE (decl) == FUNCTION_DECL
27032 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27033 return 0;
27034 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27036 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27037 if (TREE_CODE (decl) == FUNCTION_DECL
27038 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27039 return 0;
27042 return 1;
27045 /* Hash table routines for file_hash. */
27047 bool
27048 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27050 return filename_cmp (p1->filename, p2) == 0;
27053 hashval_t
27054 dwarf_file_hasher::hash (dwarf_file_data *p)
27056 return htab_hash_string (p->filename);
27059 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27060 dwarf2out.c) and return its "index". The index of each (known) filename is
27061 just a unique number which is associated with only that one filename. We
27062 need such numbers for the sake of generating labels (in the .debug_sfnames
27063 section) and references to those files numbers (in the .debug_srcinfo
27064 and .debug_macinfo sections). If the filename given as an argument is not
27065 found in our current list, add it to the list and assign it the next
27066 available unique index number. */
27068 static struct dwarf_file_data *
27069 lookup_filename (const char *file_name)
27071 struct dwarf_file_data * created;
27073 if (!file_name)
27074 return NULL;
27076 if (!file_name[0])
27077 file_name = "<stdin>";
27079 dwarf_file_data **slot
27080 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27081 INSERT);
27082 if (*slot)
27083 return *slot;
27085 created = ggc_alloc<dwarf_file_data> ();
27086 created->filename = file_name;
27087 created->emitted_number = 0;
27088 *slot = created;
27089 return created;
27092 /* If the assembler will construct the file table, then translate the compiler
27093 internal file table number into the assembler file table number, and emit
27094 a .file directive if we haven't already emitted one yet. The file table
27095 numbers are different because we prune debug info for unused variables and
27096 types, which may include filenames. */
27098 static int
27099 maybe_emit_file (struct dwarf_file_data * fd)
27101 if (! fd->emitted_number)
27103 if (last_emitted_file)
27104 fd->emitted_number = last_emitted_file->emitted_number + 1;
27105 else
27106 fd->emitted_number = 1;
27107 last_emitted_file = fd;
27109 if (output_asm_line_debug_info ())
27111 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27112 output_quoted_string (asm_out_file,
27113 remap_debug_filename (fd->filename));
27114 fputc ('\n', asm_out_file);
27118 return fd->emitted_number;
27121 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27122 That generation should happen after function debug info has been
27123 generated. The value of the attribute is the constant value of ARG. */
27125 static void
27126 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27128 die_arg_entry entry;
27130 if (!die || !arg)
27131 return;
27133 gcc_assert (early_dwarf);
27135 if (!tmpl_value_parm_die_table)
27136 vec_alloc (tmpl_value_parm_die_table, 32);
27138 entry.die = die;
27139 entry.arg = arg;
27140 vec_safe_push (tmpl_value_parm_die_table, entry);
27143 /* Return TRUE if T is an instance of generic type, FALSE
27144 otherwise. */
27146 static bool
27147 generic_type_p (tree t)
27149 if (t == NULL_TREE || !TYPE_P (t))
27150 return false;
27151 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27154 /* Schedule the generation of the generic parameter dies for the
27155 instance of generic type T. The proper generation itself is later
27156 done by gen_scheduled_generic_parms_dies. */
27158 static void
27159 schedule_generic_params_dies_gen (tree t)
27161 if (!generic_type_p (t))
27162 return;
27164 gcc_assert (early_dwarf);
27166 if (!generic_type_instances)
27167 vec_alloc (generic_type_instances, 256);
27169 vec_safe_push (generic_type_instances, t);
27172 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27173 by append_entry_to_tmpl_value_parm_die_table. This function must
27174 be called after function DIEs have been generated. */
27176 static void
27177 gen_remaining_tmpl_value_param_die_attribute (void)
27179 if (tmpl_value_parm_die_table)
27181 unsigned i, j;
27182 die_arg_entry *e;
27184 /* We do this in two phases - first get the cases we can
27185 handle during early-finish, preserving those we cannot
27186 (containing symbolic constants where we don't yet know
27187 whether we are going to output the referenced symbols).
27188 For those we try again at late-finish. */
27189 j = 0;
27190 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27192 if (!e->die->removed
27193 && !tree_add_const_value_attribute (e->die, e->arg))
27195 dw_loc_descr_ref loc = NULL;
27196 if (! early_dwarf
27197 && (dwarf_version >= 5 || !dwarf_strict))
27198 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27199 if (loc)
27200 add_AT_loc (e->die, DW_AT_location, loc);
27201 else
27202 (*tmpl_value_parm_die_table)[j++] = *e;
27205 tmpl_value_parm_die_table->truncate (j);
27209 /* Generate generic parameters DIEs for instances of generic types
27210 that have been previously scheduled by
27211 schedule_generic_params_dies_gen. This function must be called
27212 after all the types of the CU have been laid out. */
27214 static void
27215 gen_scheduled_generic_parms_dies (void)
27217 unsigned i;
27218 tree t;
27220 if (!generic_type_instances)
27221 return;
27223 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27224 if (COMPLETE_TYPE_P (t))
27225 gen_generic_params_dies (t);
27227 generic_type_instances = NULL;
27231 /* Replace DW_AT_name for the decl with name. */
27233 static void
27234 dwarf2out_set_name (tree decl, tree name)
27236 dw_die_ref die;
27237 dw_attr_node *attr;
27238 const char *dname;
27240 die = TYPE_SYMTAB_DIE (decl);
27241 if (!die)
27242 return;
27244 dname = dwarf2_name (name, 0);
27245 if (!dname)
27246 return;
27248 attr = get_AT (die, DW_AT_name);
27249 if (attr)
27251 struct indirect_string_node *node;
27253 node = find_AT_string (dname);
27254 /* replace the string. */
27255 attr->dw_attr_val.v.val_str = node;
27258 else
27259 add_name_attribute (die, dname);
27262 /* True if before or during processing of the first function being emitted. */
27263 static bool in_first_function_p = true;
27264 /* True if loc_note during dwarf2out_var_location call might still be
27265 before first real instruction at address equal to .Ltext0. */
27266 static bool maybe_at_text_label_p = true;
27267 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27268 static unsigned int first_loclabel_num_not_at_text_label;
27270 /* Look ahead for a real insn. */
27272 static rtx_insn *
27273 dwarf2out_next_real_insn (rtx_insn *loc_note)
27275 rtx_insn *next_real = NEXT_INSN (loc_note);
27277 while (next_real)
27278 if (INSN_P (next_real))
27279 break;
27280 else
27281 next_real = NEXT_INSN (next_real);
27283 return next_real;
27286 /* Called by the final INSN scan whenever we see a var location. We
27287 use it to drop labels in the right places, and throw the location in
27288 our lookup table. */
27290 static void
27291 dwarf2out_var_location (rtx_insn *loc_note)
27293 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27294 struct var_loc_node *newloc;
27295 rtx_insn *next_real;
27296 rtx_insn *call_insn = NULL;
27297 static const char *last_label;
27298 static const char *last_postcall_label;
27299 static bool last_in_cold_section_p;
27300 static rtx_insn *expected_next_loc_note;
27301 tree decl;
27302 bool var_loc_p;
27303 var_loc_view view = 0;
27305 if (!NOTE_P (loc_note))
27307 if (CALL_P (loc_note))
27309 maybe_reset_location_view (loc_note, cur_line_info_table);
27310 call_site_count++;
27311 if (SIBLING_CALL_P (loc_note))
27312 tail_call_site_count++;
27313 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27315 call_insn = loc_note;
27316 loc_note = NULL;
27317 var_loc_p = false;
27319 next_real = dwarf2out_next_real_insn (call_insn);
27320 cached_next_real_insn = NULL;
27321 goto create_label;
27323 if (optimize == 0 && !flag_var_tracking)
27325 /* When the var-tracking pass is not running, there is no note
27326 for indirect calls whose target is compile-time known. In this
27327 case, process such calls specifically so that we generate call
27328 sites for them anyway. */
27329 rtx x = PATTERN (loc_note);
27330 if (GET_CODE (x) == PARALLEL)
27331 x = XVECEXP (x, 0, 0);
27332 if (GET_CODE (x) == SET)
27333 x = SET_SRC (x);
27334 if (GET_CODE (x) == CALL)
27335 x = XEXP (x, 0);
27336 if (!MEM_P (x)
27337 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27338 || !SYMBOL_REF_DECL (XEXP (x, 0))
27339 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27340 != FUNCTION_DECL))
27342 call_insn = loc_note;
27343 loc_note = NULL;
27344 var_loc_p = false;
27346 next_real = dwarf2out_next_real_insn (call_insn);
27347 cached_next_real_insn = NULL;
27348 goto create_label;
27352 else if (!debug_variable_location_views)
27353 gcc_unreachable ();
27354 else
27355 maybe_reset_location_view (loc_note, cur_line_info_table);
27357 return;
27360 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27361 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27362 return;
27364 /* Optimize processing a large consecutive sequence of location
27365 notes so we don't spend too much time in next_real_insn. If the
27366 next insn is another location note, remember the next_real_insn
27367 calculation for next time. */
27368 next_real = cached_next_real_insn;
27369 if (next_real)
27371 if (expected_next_loc_note != loc_note)
27372 next_real = NULL;
27375 if (! next_real)
27376 next_real = dwarf2out_next_real_insn (loc_note);
27378 if (next_real)
27380 rtx_insn *next_note = NEXT_INSN (loc_note);
27381 while (next_note != next_real)
27383 if (! next_note->deleted ()
27384 && NOTE_P (next_note)
27385 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
27386 break;
27387 next_note = NEXT_INSN (next_note);
27390 if (next_note == next_real)
27391 cached_next_real_insn = NULL;
27392 else
27394 expected_next_loc_note = next_note;
27395 cached_next_real_insn = next_real;
27398 else
27399 cached_next_real_insn = NULL;
27401 /* If there are no instructions which would be affected by this note,
27402 don't do anything. */
27403 if (var_loc_p
27404 && next_real == NULL_RTX
27405 && !NOTE_DURING_CALL_P (loc_note))
27406 return;
27408 create_label:
27410 if (next_real == NULL_RTX)
27411 next_real = get_last_insn ();
27413 /* If there were any real insns between note we processed last time
27414 and this note (or if it is the first note), clear
27415 last_{,postcall_}label so that they are not reused this time. */
27416 if (last_var_location_insn == NULL_RTX
27417 || last_var_location_insn != next_real
27418 || last_in_cold_section_p != in_cold_section_p)
27420 last_label = NULL;
27421 last_postcall_label = NULL;
27424 if (var_loc_p)
27426 const char *label
27427 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27428 view = cur_line_info_table->view;
27429 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27430 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27431 if (newloc == NULL)
27432 return;
27434 else
27436 decl = NULL_TREE;
27437 newloc = NULL;
27440 /* If there were no real insns between note we processed last time
27441 and this note, use the label we emitted last time. Otherwise
27442 create a new label and emit it. */
27443 if (last_label == NULL)
27445 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27446 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27447 loclabel_num++;
27448 last_label = ggc_strdup (loclabel);
27449 /* See if loclabel might be equal to .Ltext0. If yes,
27450 bump first_loclabel_num_not_at_text_label. */
27451 if (!have_multiple_function_sections
27452 && in_first_function_p
27453 && maybe_at_text_label_p)
27455 static rtx_insn *last_start;
27456 rtx_insn *insn;
27457 for (insn = loc_note; insn; insn = previous_insn (insn))
27458 if (insn == last_start)
27459 break;
27460 else if (!NONDEBUG_INSN_P (insn))
27461 continue;
27462 else
27464 rtx body = PATTERN (insn);
27465 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27466 continue;
27467 /* Inline asm could occupy zero bytes. */
27468 else if (GET_CODE (body) == ASM_INPUT
27469 || asm_noperands (body) >= 0)
27470 continue;
27471 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27472 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27473 continue;
27474 #endif
27475 else
27477 /* Assume insn has non-zero length. */
27478 maybe_at_text_label_p = false;
27479 break;
27482 if (maybe_at_text_label_p)
27484 last_start = loc_note;
27485 first_loclabel_num_not_at_text_label = loclabel_num;
27490 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27491 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27493 if (!var_loc_p)
27495 struct call_arg_loc_node *ca_loc
27496 = ggc_cleared_alloc<call_arg_loc_node> ();
27497 rtx_insn *prev = call_insn;
27499 ca_loc->call_arg_loc_note
27500 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27501 ca_loc->next = NULL;
27502 ca_loc->label = last_label;
27503 gcc_assert (prev
27504 && (CALL_P (prev)
27505 || (NONJUMP_INSN_P (prev)
27506 && GET_CODE (PATTERN (prev)) == SEQUENCE
27507 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27508 if (!CALL_P (prev))
27509 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27510 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27512 /* Look for a SYMBOL_REF in the "prev" instruction. */
27513 rtx x = get_call_rtx_from (prev);
27514 if (x)
27516 /* Try to get the call symbol, if any. */
27517 if (MEM_P (XEXP (x, 0)))
27518 x = XEXP (x, 0);
27519 /* First, look for a memory access to a symbol_ref. */
27520 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27521 && SYMBOL_REF_DECL (XEXP (x, 0))
27522 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27523 ca_loc->symbol_ref = XEXP (x, 0);
27524 /* Otherwise, look at a compile-time known user-level function
27525 declaration. */
27526 else if (MEM_P (x)
27527 && MEM_EXPR (x)
27528 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27529 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27532 ca_loc->block = insn_scope (prev);
27533 if (call_arg_locations)
27534 call_arg_loc_last->next = ca_loc;
27535 else
27536 call_arg_locations = ca_loc;
27537 call_arg_loc_last = ca_loc;
27539 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27541 newloc->label = last_label;
27542 newloc->view = view;
27544 else
27546 if (!last_postcall_label)
27548 sprintf (loclabel, "%s-1", last_label);
27549 last_postcall_label = ggc_strdup (loclabel);
27551 newloc->label = last_postcall_label;
27552 /* ??? This view is at last_label, not last_label-1, but we
27553 could only assume view at last_label-1 is zero if we could
27554 assume calls always have length greater than one. This is
27555 probably true in general, though there might be a rare
27556 exception to this rule, e.g. if a call insn is optimized out
27557 by target magic. Then, even the -1 in the label will be
27558 wrong, which might invalidate the range. Anyway, using view,
27559 though technically possibly incorrect, will work as far as
27560 ranges go: since L-1 is in the middle of the call insn,
27561 (L-1).0 and (L-1).V shouldn't make any difference, and having
27562 the loclist entry refer to the .loc entry might be useful, so
27563 leave it like this. */
27564 newloc->view = view;
27567 if (var_loc_p && flag_debug_asm)
27569 const char *name, *sep, *patstr;
27570 if (decl && DECL_NAME (decl))
27571 name = IDENTIFIER_POINTER (DECL_NAME (decl));
27572 else
27573 name = "";
27574 if (NOTE_VAR_LOCATION_LOC (loc_note))
27576 sep = " => ";
27577 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27579 else
27581 sep = " ";
27582 patstr = "RESET";
27584 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27585 name, sep, patstr);
27588 last_var_location_insn = next_real;
27589 last_in_cold_section_p = in_cold_section_p;
27592 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
27593 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
27594 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
27595 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
27596 BLOCK_FRAGMENT_ORIGIN links. */
27597 static bool
27598 block_within_block_p (tree block, tree outer, bool bothways)
27600 if (block == outer)
27601 return true;
27603 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
27604 for (tree context = BLOCK_SUPERCONTEXT (block);
27605 context != outer;
27606 context = BLOCK_SUPERCONTEXT (context))
27607 if (!context || TREE_CODE (context) != BLOCK)
27608 return false;
27610 if (!bothways)
27611 return true;
27613 /* Now check that each block is actually referenced by its
27614 parent. */
27615 for (tree context = BLOCK_SUPERCONTEXT (block); ;
27616 context = BLOCK_SUPERCONTEXT (context))
27618 if (BLOCK_FRAGMENT_ORIGIN (context))
27620 gcc_assert (!BLOCK_SUBBLOCKS (context));
27621 context = BLOCK_FRAGMENT_ORIGIN (context);
27623 for (tree sub = BLOCK_SUBBLOCKS (context);
27624 sub != block;
27625 sub = BLOCK_CHAIN (sub))
27626 if (!sub)
27627 return false;
27628 if (context == outer)
27629 return true;
27630 else
27631 block = context;
27635 /* Called during final while assembling the marker of the entry point
27636 for an inlined function. */
27638 static void
27639 dwarf2out_inline_entry (tree block)
27641 gcc_assert (debug_inline_points);
27643 /* If we can't represent it, don't bother. */
27644 if (!(dwarf_version >= 3 || !dwarf_strict))
27645 return;
27647 gcc_assert (DECL_P (block_ultimate_origin (block)));
27649 /* Sanity check the block tree. This would catch a case in which
27650 BLOCK got removed from the tree reachable from the outermost
27651 lexical block, but got retained in markers. It would still link
27652 back to its parents, but some ancestor would be missing a link
27653 down the path to the sub BLOCK. If the block got removed, its
27654 BLOCK_NUMBER will not be a usable value. */
27655 if (flag_checking)
27656 gcc_assert (block_within_block_p (block,
27657 DECL_INITIAL (current_function_decl),
27658 true));
27660 gcc_assert (inlined_function_outer_scope_p (block));
27661 gcc_assert (!lookup_block_die (block));
27663 if (BLOCK_FRAGMENT_ORIGIN (block))
27664 block = BLOCK_FRAGMENT_ORIGIN (block);
27665 /* Can the entry point ever not be at the beginning of an
27666 unfragmented lexical block? */
27667 else if (!(BLOCK_FRAGMENT_CHAIN (block)
27668 || (cur_line_info_table
27669 && !ZERO_VIEW_P (cur_line_info_table->view))))
27670 return;
27672 if (!inline_entry_data_table)
27673 inline_entry_data_table
27674 = hash_table<inline_entry_data_hasher>::create_ggc (10);
27677 inline_entry_data **iedp
27678 = inline_entry_data_table->find_slot_with_hash (block,
27679 htab_hash_pointer (block),
27680 INSERT);
27681 if (*iedp)
27682 /* ??? Ideally, we'd record all entry points for the same inlined
27683 function (some may have been duplicated by e.g. unrolling), but
27684 we have no way to represent that ATM. */
27685 return;
27687 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
27688 ied->block = block;
27689 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
27690 ied->label_num = BLOCK_NUMBER (block);
27691 if (cur_line_info_table)
27692 ied->view = cur_line_info_table->view;
27694 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
27695 BLOCK_NUMBER (block));
27698 /* Called from finalize_size_functions for size functions so that their body
27699 can be encoded in the debug info to describe the layout of variable-length
27700 structures. */
27702 static void
27703 dwarf2out_size_function (tree decl)
27705 set_early_dwarf s;
27706 function_to_dwarf_procedure (decl);
27709 /* Note in one location list that text section has changed. */
27712 var_location_switch_text_section_1 (var_loc_list **slot, void *)
27714 var_loc_list *list = *slot;
27715 if (list->first)
27716 list->last_before_switch
27717 = list->last->next ? list->last->next : list->last;
27718 return 1;
27721 /* Note in all location lists that text section has changed. */
27723 static void
27724 var_location_switch_text_section (void)
27726 if (decl_loc_table == NULL)
27727 return;
27729 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
27732 /* Create a new line number table. */
27734 static dw_line_info_table *
27735 new_line_info_table (void)
27737 dw_line_info_table *table;
27739 table = ggc_cleared_alloc<dw_line_info_table> ();
27740 table->file_num = 1;
27741 table->line_num = 1;
27742 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
27743 FORCE_RESET_NEXT_VIEW (table->view);
27744 table->symviews_since_reset = 0;
27746 return table;
27749 /* Lookup the "current" table into which we emit line info, so
27750 that we don't have to do it for every source line. */
27752 static void
27753 set_cur_line_info_table (section *sec)
27755 dw_line_info_table *table;
27757 if (sec == text_section)
27758 table = text_section_line_info;
27759 else if (sec == cold_text_section)
27761 table = cold_text_section_line_info;
27762 if (!table)
27764 cold_text_section_line_info = table = new_line_info_table ();
27765 table->end_label = cold_end_label;
27768 else
27770 const char *end_label;
27772 if (crtl->has_bb_partition)
27774 if (in_cold_section_p)
27775 end_label = crtl->subsections.cold_section_end_label;
27776 else
27777 end_label = crtl->subsections.hot_section_end_label;
27779 else
27781 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27782 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
27783 current_function_funcdef_no);
27784 end_label = ggc_strdup (label);
27787 table = new_line_info_table ();
27788 table->end_label = end_label;
27790 vec_safe_push (separate_line_info, table);
27793 if (output_asm_line_debug_info ())
27794 table->is_stmt = (cur_line_info_table
27795 ? cur_line_info_table->is_stmt
27796 : DWARF_LINE_DEFAULT_IS_STMT_START);
27797 cur_line_info_table = table;
27801 /* We need to reset the locations at the beginning of each
27802 function. We can't do this in the end_function hook, because the
27803 declarations that use the locations won't have been output when
27804 that hook is called. Also compute have_multiple_function_sections here. */
27806 static void
27807 dwarf2out_begin_function (tree fun)
27809 section *sec = function_section (fun);
27811 if (sec != text_section)
27812 have_multiple_function_sections = true;
27814 if (crtl->has_bb_partition && !cold_text_section)
27816 gcc_assert (current_function_decl == fun);
27817 cold_text_section = unlikely_text_section ();
27818 switch_to_section (cold_text_section);
27819 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
27820 switch_to_section (sec);
27823 dwarf2out_note_section_used ();
27824 call_site_count = 0;
27825 tail_call_site_count = 0;
27827 set_cur_line_info_table (sec);
27828 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
27831 /* Helper function of dwarf2out_end_function, called only after emitting
27832 the very first function into assembly. Check if some .debug_loc range
27833 might end with a .LVL* label that could be equal to .Ltext0.
27834 In that case we must force using absolute addresses in .debug_loc ranges,
27835 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
27836 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
27837 list terminator.
27838 Set have_multiple_function_sections to true in that case and
27839 terminate htab traversal. */
27842 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
27844 var_loc_list *entry = *slot;
27845 struct var_loc_node *node;
27847 node = entry->first;
27848 if (node && node->next && node->next->label)
27850 unsigned int i;
27851 const char *label = node->next->label;
27852 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
27854 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
27856 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
27857 if (strcmp (label, loclabel) == 0)
27859 have_multiple_function_sections = true;
27860 return 0;
27864 return 1;
27867 /* Hook called after emitting a function into assembly.
27868 This does something only for the very first function emitted. */
27870 static void
27871 dwarf2out_end_function (unsigned int)
27873 if (in_first_function_p
27874 && !have_multiple_function_sections
27875 && first_loclabel_num_not_at_text_label
27876 && decl_loc_table)
27877 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
27878 in_first_function_p = false;
27879 maybe_at_text_label_p = false;
27882 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
27883 front-ends register a translation unit even before dwarf2out_init is
27884 called. */
27885 static tree main_translation_unit = NULL_TREE;
27887 /* Hook called by front-ends after they built their main translation unit.
27888 Associate comp_unit_die to UNIT. */
27890 static void
27891 dwarf2out_register_main_translation_unit (tree unit)
27893 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
27894 && main_translation_unit == NULL_TREE);
27895 main_translation_unit = unit;
27896 /* If dwarf2out_init has not been called yet, it will perform the association
27897 itself looking at main_translation_unit. */
27898 if (decl_die_table != NULL)
27899 equate_decl_number_to_die (unit, comp_unit_die ());
27902 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
27904 static void
27905 push_dw_line_info_entry (dw_line_info_table *table,
27906 enum dw_line_info_opcode opcode, unsigned int val)
27908 dw_line_info_entry e;
27909 e.opcode = opcode;
27910 e.val = val;
27911 vec_safe_push (table->entries, e);
27914 /* Output a label to mark the beginning of a source code line entry
27915 and record information relating to this source line, in
27916 'line_info_table' for later output of the .debug_line section. */
27917 /* ??? The discriminator parameter ought to be unsigned. */
27919 static void
27920 dwarf2out_source_line (unsigned int line, unsigned int column,
27921 const char *filename,
27922 int discriminator, bool is_stmt)
27924 unsigned int file_num;
27925 dw_line_info_table *table;
27926 static var_loc_view lvugid;
27928 if (debug_info_level < DINFO_LEVEL_TERSE)
27929 return;
27931 table = cur_line_info_table;
27933 if (line == 0)
27935 if (debug_variable_location_views
27936 && output_asm_line_debug_info ()
27937 && table && !RESETTING_VIEW_P (table->view))
27939 /* If we're using the assembler to compute view numbers, we
27940 can't issue a .loc directive for line zero, so we can't
27941 get a view number at this point. We might attempt to
27942 compute it from the previous view, or equate it to a
27943 subsequent view (though it might not be there!), but
27944 since we're omitting the line number entry, we might as
27945 well omit the view number as well. That means pretending
27946 it's a view number zero, which might very well turn out
27947 to be correct. ??? Extend the assembler so that the
27948 compiler could emit e.g. ".locview .LVU#", to output a
27949 view without changing line number information. We'd then
27950 have to count it in symviews_since_reset; when it's omitted,
27951 it doesn't count. */
27952 if (!zero_view_p)
27953 zero_view_p = BITMAP_GGC_ALLOC ();
27954 bitmap_set_bit (zero_view_p, table->view);
27955 if (flag_debug_asm)
27957 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27958 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27959 fprintf (asm_out_file, "\t%s line 0, omitted view ",
27960 ASM_COMMENT_START);
27961 assemble_name (asm_out_file, label);
27962 putc ('\n', asm_out_file);
27964 table->view = ++lvugid;
27966 return;
27969 /* The discriminator column was added in dwarf4. Simplify the below
27970 by simply removing it if we're not supposed to output it. */
27971 if (dwarf_version < 4 && dwarf_strict)
27972 discriminator = 0;
27974 if (!debug_column_info)
27975 column = 0;
27977 file_num = maybe_emit_file (lookup_filename (filename));
27979 /* ??? TODO: Elide duplicate line number entries. Traditionally,
27980 the debugger has used the second (possibly duplicate) line number
27981 at the beginning of the function to mark the end of the prologue.
27982 We could eliminate any other duplicates within the function. For
27983 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
27984 that second line number entry. */
27985 /* Recall that this end-of-prologue indication is *not* the same thing
27986 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
27987 to which the hook corresponds, follows the last insn that was
27988 emitted by gen_prologue. What we need is to precede the first insn
27989 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
27990 insn that corresponds to something the user wrote. These may be
27991 very different locations once scheduling is enabled. */
27993 if (0 && file_num == table->file_num
27994 && line == table->line_num
27995 && column == table->column_num
27996 && discriminator == table->discrim_num
27997 && is_stmt == table->is_stmt)
27998 return;
28000 switch_to_section (current_function_section ());
28002 /* If requested, emit something human-readable. */
28003 if (flag_debug_asm)
28005 if (debug_column_info)
28006 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28007 filename, line, column);
28008 else
28009 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28010 filename, line);
28013 if (output_asm_line_debug_info ())
28015 /* Emit the .loc directive understood by GNU as. */
28016 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28017 file_num, line, is_stmt, discriminator */
28018 fputs ("\t.loc ", asm_out_file);
28019 fprint_ul (asm_out_file, file_num);
28020 putc (' ', asm_out_file);
28021 fprint_ul (asm_out_file, line);
28022 putc (' ', asm_out_file);
28023 fprint_ul (asm_out_file, column);
28025 if (is_stmt != table->is_stmt)
28027 #if HAVE_GAS_LOC_STMT
28028 fputs (" is_stmt ", asm_out_file);
28029 putc (is_stmt ? '1' : '0', asm_out_file);
28030 #endif
28032 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28034 gcc_assert (discriminator > 0);
28035 fputs (" discriminator ", asm_out_file);
28036 fprint_ul (asm_out_file, (unsigned long) discriminator);
28038 if (debug_variable_location_views)
28040 if (!RESETTING_VIEW_P (table->view))
28042 table->symviews_since_reset++;
28043 if (table->symviews_since_reset > symview_upper_bound)
28044 symview_upper_bound = table->symviews_since_reset;
28045 /* When we're using the assembler to compute view
28046 numbers, we output symbolic labels after "view" in
28047 .loc directives, and the assembler will set them for
28048 us, so that we can refer to the view numbers in
28049 location lists. The only exceptions are when we know
28050 a view will be zero: "-0" is a forced reset, used
28051 e.g. in the beginning of functions, whereas "0" tells
28052 the assembler to check that there was a PC change
28053 since the previous view, in a way that implicitly
28054 resets the next view. */
28055 fputs (" view ", asm_out_file);
28056 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28057 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28058 assemble_name (asm_out_file, label);
28059 table->view = ++lvugid;
28061 else
28063 table->symviews_since_reset = 0;
28064 if (FORCE_RESETTING_VIEW_P (table->view))
28065 fputs (" view -0", asm_out_file);
28066 else
28067 fputs (" view 0", asm_out_file);
28068 /* Mark the present view as a zero view. Earlier debug
28069 binds may have already added its id to loclists to be
28070 emitted later, so we can't reuse the id for something
28071 else. However, it's good to know whether a view is
28072 known to be zero, because then we may be able to
28073 optimize out locviews that are all zeros, so take
28074 note of it in zero_view_p. */
28075 if (!zero_view_p)
28076 zero_view_p = BITMAP_GGC_ALLOC ();
28077 bitmap_set_bit (zero_view_p, lvugid);
28078 table->view = ++lvugid;
28081 putc ('\n', asm_out_file);
28083 else
28085 unsigned int label_num = ++line_info_label_num;
28087 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28089 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28090 push_dw_line_info_entry (table, LI_adv_address, label_num);
28091 else
28092 push_dw_line_info_entry (table, LI_set_address, label_num);
28093 if (debug_variable_location_views)
28095 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28096 if (resetting)
28097 table->view = 0;
28099 if (flag_debug_asm)
28100 fprintf (asm_out_file, "\t%s view %s%d\n",
28101 ASM_COMMENT_START,
28102 resetting ? "-" : "",
28103 table->view);
28105 table->view++;
28107 if (file_num != table->file_num)
28108 push_dw_line_info_entry (table, LI_set_file, file_num);
28109 if (discriminator != table->discrim_num)
28110 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28111 if (is_stmt != table->is_stmt)
28112 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28113 push_dw_line_info_entry (table, LI_set_line, line);
28114 if (debug_column_info)
28115 push_dw_line_info_entry (table, LI_set_column, column);
28118 table->file_num = file_num;
28119 table->line_num = line;
28120 table->column_num = column;
28121 table->discrim_num = discriminator;
28122 table->is_stmt = is_stmt;
28123 table->in_use = true;
28126 /* Record the beginning of a new source file. */
28128 static void
28129 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28131 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28133 macinfo_entry e;
28134 e.code = DW_MACINFO_start_file;
28135 e.lineno = lineno;
28136 e.info = ggc_strdup (filename);
28137 vec_safe_push (macinfo_table, e);
28141 /* Record the end of a source file. */
28143 static void
28144 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28146 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28148 macinfo_entry e;
28149 e.code = DW_MACINFO_end_file;
28150 e.lineno = lineno;
28151 e.info = NULL;
28152 vec_safe_push (macinfo_table, e);
28156 /* Called from debug_define in toplev.c. The `buffer' parameter contains
28157 the tail part of the directive line, i.e. the part which is past the
28158 initial whitespace, #, whitespace, directive-name, whitespace part. */
28160 static void
28161 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28162 const char *buffer ATTRIBUTE_UNUSED)
28164 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28166 macinfo_entry e;
28167 /* Insert a dummy first entry to be able to optimize the whole
28168 predefined macro block using DW_MACRO_import. */
28169 if (macinfo_table->is_empty () && lineno <= 1)
28171 e.code = 0;
28172 e.lineno = 0;
28173 e.info = NULL;
28174 vec_safe_push (macinfo_table, e);
28176 e.code = DW_MACINFO_define;
28177 e.lineno = lineno;
28178 e.info = ggc_strdup (buffer);
28179 vec_safe_push (macinfo_table, e);
28183 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
28184 the tail part of the directive line, i.e. the part which is past the
28185 initial whitespace, #, whitespace, directive-name, whitespace part. */
28187 static void
28188 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28189 const char *buffer ATTRIBUTE_UNUSED)
28191 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28193 macinfo_entry e;
28194 /* Insert a dummy first entry to be able to optimize the whole
28195 predefined macro block using DW_MACRO_import. */
28196 if (macinfo_table->is_empty () && lineno <= 1)
28198 e.code = 0;
28199 e.lineno = 0;
28200 e.info = NULL;
28201 vec_safe_push (macinfo_table, e);
28203 e.code = DW_MACINFO_undef;
28204 e.lineno = lineno;
28205 e.info = ggc_strdup (buffer);
28206 vec_safe_push (macinfo_table, e);
28210 /* Helpers to manipulate hash table of CUs. */
28212 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28214 static inline hashval_t hash (const macinfo_entry *);
28215 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28218 inline hashval_t
28219 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28221 return htab_hash_string (entry->info);
28224 inline bool
28225 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28226 const macinfo_entry *entry2)
28228 return !strcmp (entry1->info, entry2->info);
28231 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28233 /* Output a single .debug_macinfo entry. */
28235 static void
28236 output_macinfo_op (macinfo_entry *ref)
28238 int file_num;
28239 size_t len;
28240 struct indirect_string_node *node;
28241 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28242 struct dwarf_file_data *fd;
28244 switch (ref->code)
28246 case DW_MACINFO_start_file:
28247 fd = lookup_filename (ref->info);
28248 file_num = maybe_emit_file (fd);
28249 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28250 dw2_asm_output_data_uleb128 (ref->lineno,
28251 "Included from line number %lu",
28252 (unsigned long) ref->lineno);
28253 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28254 break;
28255 case DW_MACINFO_end_file:
28256 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28257 break;
28258 case DW_MACINFO_define:
28259 case DW_MACINFO_undef:
28260 len = strlen (ref->info) + 1;
28261 if (!dwarf_strict
28262 && len > DWARF_OFFSET_SIZE
28263 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28264 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28266 ref->code = ref->code == DW_MACINFO_define
28267 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28268 output_macinfo_op (ref);
28269 return;
28271 dw2_asm_output_data (1, ref->code,
28272 ref->code == DW_MACINFO_define
28273 ? "Define macro" : "Undefine macro");
28274 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28275 (unsigned long) ref->lineno);
28276 dw2_asm_output_nstring (ref->info, -1, "The macro");
28277 break;
28278 case DW_MACRO_define_strp:
28279 case DW_MACRO_undef_strp:
28280 /* NB: dwarf2out_finish performs:
28281 1. save_macinfo_strings
28282 2. hash table traverse of index_string
28283 3. output_macinfo -> output_macinfo_op
28284 4. output_indirect_strings
28285 -> hash table traverse of output_index_string
28287 When output_macinfo_op is called, all index strings have been
28288 added to hash table by save_macinfo_strings and we can't pass
28289 INSERT to find_slot_with_hash which may expand hash table, even
28290 if no insertion is needed, and change hash table traverse order
28291 between index_string and output_index_string. */
28292 node = find_AT_string (ref->info, NO_INSERT);
28293 gcc_assert (node
28294 && (node->form == DW_FORM_strp
28295 || node->form == dwarf_FORM (DW_FORM_strx)));
28296 dw2_asm_output_data (1, ref->code,
28297 ref->code == DW_MACRO_define_strp
28298 ? "Define macro strp"
28299 : "Undefine macro strp");
28300 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28301 (unsigned long) ref->lineno);
28302 if (node->form == DW_FORM_strp)
28303 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
28304 debug_str_section, "The macro: \"%s\"",
28305 ref->info);
28306 else
28307 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28308 ref->info);
28309 break;
28310 case DW_MACRO_import:
28311 dw2_asm_output_data (1, ref->code, "Import");
28312 ASM_GENERATE_INTERNAL_LABEL (label,
28313 DEBUG_MACRO_SECTION_LABEL,
28314 ref->lineno + macinfo_label_base);
28315 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
28316 break;
28317 default:
28318 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28319 ASM_COMMENT_START, (unsigned long) ref->code);
28320 break;
28324 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28325 other compilation unit .debug_macinfo sections. IDX is the first
28326 index of a define/undef, return the number of ops that should be
28327 emitted in a comdat .debug_macinfo section and emit
28328 a DW_MACRO_import entry referencing it.
28329 If the define/undef entry should be emitted normally, return 0. */
28331 static unsigned
28332 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28333 macinfo_hash_type **macinfo_htab)
28335 macinfo_entry *first, *second, *cur, *inc;
28336 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28337 unsigned char checksum[16];
28338 struct md5_ctx ctx;
28339 char *grp_name, *tail;
28340 const char *base;
28341 unsigned int i, count, encoded_filename_len, linebuf_len;
28342 macinfo_entry **slot;
28344 first = &(*macinfo_table)[idx];
28345 second = &(*macinfo_table)[idx + 1];
28347 /* Optimize only if there are at least two consecutive define/undef ops,
28348 and either all of them are before first DW_MACINFO_start_file
28349 with lineno {0,1} (i.e. predefined macro block), or all of them are
28350 in some included header file. */
28351 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28352 return 0;
28353 if (vec_safe_is_empty (files))
28355 if (first->lineno > 1 || second->lineno > 1)
28356 return 0;
28358 else if (first->lineno == 0)
28359 return 0;
28361 /* Find the last define/undef entry that can be grouped together
28362 with first and at the same time compute md5 checksum of their
28363 codes, linenumbers and strings. */
28364 md5_init_ctx (&ctx);
28365 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28366 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28367 break;
28368 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28369 break;
28370 else
28372 unsigned char code = cur->code;
28373 md5_process_bytes (&code, 1, &ctx);
28374 checksum_uleb128 (cur->lineno, &ctx);
28375 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28377 md5_finish_ctx (&ctx, checksum);
28378 count = i - idx;
28380 /* From the containing include filename (if any) pick up just
28381 usable characters from its basename. */
28382 if (vec_safe_is_empty (files))
28383 base = "";
28384 else
28385 base = lbasename (files->last ().info);
28386 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28387 if (ISIDNUM (base[i]) || base[i] == '.')
28388 encoded_filename_len++;
28389 /* Count . at the end. */
28390 if (encoded_filename_len)
28391 encoded_filename_len++;
28393 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28394 linebuf_len = strlen (linebuf);
28396 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28397 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28398 + 16 * 2 + 1);
28399 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
28400 tail = grp_name + 4;
28401 if (encoded_filename_len)
28403 for (i = 0; base[i]; i++)
28404 if (ISIDNUM (base[i]) || base[i] == '.')
28405 *tail++ = base[i];
28406 *tail++ = '.';
28408 memcpy (tail, linebuf, linebuf_len);
28409 tail += linebuf_len;
28410 *tail++ = '.';
28411 for (i = 0; i < 16; i++)
28412 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28414 /* Construct a macinfo_entry for DW_MACRO_import
28415 in the empty vector entry before the first define/undef. */
28416 inc = &(*macinfo_table)[idx - 1];
28417 inc->code = DW_MACRO_import;
28418 inc->lineno = 0;
28419 inc->info = ggc_strdup (grp_name);
28420 if (!*macinfo_htab)
28421 *macinfo_htab = new macinfo_hash_type (10);
28422 /* Avoid emitting duplicates. */
28423 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28424 if (*slot != NULL)
28426 inc->code = 0;
28427 inc->info = NULL;
28428 /* If such an entry has been used before, just emit
28429 a DW_MACRO_import op. */
28430 inc = *slot;
28431 output_macinfo_op (inc);
28432 /* And clear all macinfo_entry in the range to avoid emitting them
28433 in the second pass. */
28434 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28436 cur->code = 0;
28437 cur->info = NULL;
28440 else
28442 *slot = inc;
28443 inc->lineno = (*macinfo_htab)->elements ();
28444 output_macinfo_op (inc);
28446 return count;
28449 /* Save any strings needed by the macinfo table in the debug str
28450 table. All strings must be collected into the table by the time
28451 index_string is called. */
28453 static void
28454 save_macinfo_strings (void)
28456 unsigned len;
28457 unsigned i;
28458 macinfo_entry *ref;
28460 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28462 switch (ref->code)
28464 /* Match the logic in output_macinfo_op to decide on
28465 indirect strings. */
28466 case DW_MACINFO_define:
28467 case DW_MACINFO_undef:
28468 len = strlen (ref->info) + 1;
28469 if (!dwarf_strict
28470 && len > DWARF_OFFSET_SIZE
28471 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28472 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28473 set_indirect_string (find_AT_string (ref->info));
28474 break;
28475 case DW_MACINFO_start_file:
28476 /* -gsplit-dwarf -g3 will also output filename as indirect
28477 string. */
28478 if (!dwarf_split_debug_info)
28479 break;
28480 /* Fall through. */
28481 case DW_MACRO_define_strp:
28482 case DW_MACRO_undef_strp:
28483 set_indirect_string (find_AT_string (ref->info));
28484 break;
28485 default:
28486 break;
28491 /* Output macinfo section(s). */
28493 static void
28494 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28496 unsigned i;
28497 unsigned long length = vec_safe_length (macinfo_table);
28498 macinfo_entry *ref;
28499 vec<macinfo_entry, va_gc> *files = NULL;
28500 macinfo_hash_type *macinfo_htab = NULL;
28501 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28503 if (! length)
28504 return;
28506 /* output_macinfo* uses these interchangeably. */
28507 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28508 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28509 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28510 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28512 /* AIX Assembler inserts the length, so adjust the reference to match the
28513 offset expected by debuggers. */
28514 strcpy (dl_section_ref, debug_line_label);
28515 if (XCOFF_DEBUGGING_INFO)
28516 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28518 /* For .debug_macro emit the section header. */
28519 if (!dwarf_strict || dwarf_version >= 5)
28521 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28522 "DWARF macro version number");
28523 if (DWARF_OFFSET_SIZE == 8)
28524 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28525 else
28526 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28527 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
28528 debug_line_section, NULL);
28531 /* In the first loop, it emits the primary .debug_macinfo section
28532 and after each emitted op the macinfo_entry is cleared.
28533 If a longer range of define/undef ops can be optimized using
28534 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28535 the vector before the first define/undef in the range and the
28536 whole range of define/undef ops is not emitted and kept. */
28537 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28539 switch (ref->code)
28541 case DW_MACINFO_start_file:
28542 vec_safe_push (files, *ref);
28543 break;
28544 case DW_MACINFO_end_file:
28545 if (!vec_safe_is_empty (files))
28546 files->pop ();
28547 break;
28548 case DW_MACINFO_define:
28549 case DW_MACINFO_undef:
28550 if ((!dwarf_strict || dwarf_version >= 5)
28551 && HAVE_COMDAT_GROUP
28552 && vec_safe_length (files) != 1
28553 && i > 0
28554 && i + 1 < length
28555 && (*macinfo_table)[i - 1].code == 0)
28557 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28558 if (count)
28560 i += count - 1;
28561 continue;
28564 break;
28565 case 0:
28566 /* A dummy entry may be inserted at the beginning to be able
28567 to optimize the whole block of predefined macros. */
28568 if (i == 0)
28569 continue;
28570 default:
28571 break;
28573 output_macinfo_op (ref);
28574 ref->info = NULL;
28575 ref->code = 0;
28578 if (!macinfo_htab)
28579 return;
28581 /* Save the number of transparent includes so we can adjust the
28582 label number for the fat LTO object DWARF. */
28583 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
28585 delete macinfo_htab;
28586 macinfo_htab = NULL;
28588 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
28589 terminate the current chain and switch to a new comdat .debug_macinfo
28590 section and emit the define/undef entries within it. */
28591 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28592 switch (ref->code)
28594 case 0:
28595 continue;
28596 case DW_MACRO_import:
28598 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28599 tree comdat_key = get_identifier (ref->info);
28600 /* Terminate the previous .debug_macinfo section. */
28601 dw2_asm_output_data (1, 0, "End compilation unit");
28602 targetm.asm_out.named_section (debug_macinfo_section_name,
28603 SECTION_DEBUG
28604 | SECTION_LINKONCE
28605 | (early_lto_debug
28606 ? SECTION_EXCLUDE : 0),
28607 comdat_key);
28608 ASM_GENERATE_INTERNAL_LABEL (label,
28609 DEBUG_MACRO_SECTION_LABEL,
28610 ref->lineno + macinfo_label_base);
28611 ASM_OUTPUT_LABEL (asm_out_file, label);
28612 ref->code = 0;
28613 ref->info = NULL;
28614 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28615 "DWARF macro version number");
28616 if (DWARF_OFFSET_SIZE == 8)
28617 dw2_asm_output_data (1, 1, "Flags: 64-bit");
28618 else
28619 dw2_asm_output_data (1, 0, "Flags: 32-bit");
28621 break;
28622 case DW_MACINFO_define:
28623 case DW_MACINFO_undef:
28624 output_macinfo_op (ref);
28625 ref->code = 0;
28626 ref->info = NULL;
28627 break;
28628 default:
28629 gcc_unreachable ();
28632 macinfo_label_base += macinfo_label_base_adj;
28635 /* Initialize the various sections and labels for dwarf output and prefix
28636 them with PREFIX if non-NULL. Returns the generation (zero based
28637 number of times function was called). */
28639 static unsigned
28640 init_sections_and_labels (bool early_lto_debug)
28642 /* As we may get called multiple times have a generation count for
28643 labels. */
28644 static unsigned generation = 0;
28646 if (early_lto_debug)
28648 if (!dwarf_split_debug_info)
28650 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28651 SECTION_DEBUG | SECTION_EXCLUDE,
28652 NULL);
28653 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
28654 SECTION_DEBUG | SECTION_EXCLUDE,
28655 NULL);
28656 debug_macinfo_section_name
28657 = ((dwarf_strict && dwarf_version < 5)
28658 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
28659 debug_macinfo_section = get_section (debug_macinfo_section_name,
28660 SECTION_DEBUG
28661 | SECTION_EXCLUDE, NULL);
28663 else
28665 /* ??? Which of the following do we need early? */
28666 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
28667 SECTION_DEBUG | SECTION_EXCLUDE,
28668 NULL);
28669 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
28670 SECTION_DEBUG | SECTION_EXCLUDE,
28671 NULL);
28672 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28673 SECTION_DEBUG
28674 | SECTION_EXCLUDE, NULL);
28675 debug_skeleton_abbrev_section
28676 = get_section (DEBUG_LTO_ABBREV_SECTION,
28677 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28678 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28679 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28680 generation);
28682 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28683 stay in the main .o, but the skeleton_line goes into the split
28684 off dwo. */
28685 debug_skeleton_line_section
28686 = get_section (DEBUG_LTO_LINE_SECTION,
28687 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28688 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28689 DEBUG_SKELETON_LINE_SECTION_LABEL,
28690 generation);
28691 debug_str_offsets_section
28692 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
28693 SECTION_DEBUG | SECTION_EXCLUDE,
28694 NULL);
28695 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28696 DEBUG_SKELETON_INFO_SECTION_LABEL,
28697 generation);
28698 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
28699 DEBUG_STR_DWO_SECTION_FLAGS,
28700 NULL);
28701 debug_macinfo_section_name
28702 = ((dwarf_strict && dwarf_version < 5)
28703 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
28704 debug_macinfo_section = get_section (debug_macinfo_section_name,
28705 SECTION_DEBUG | SECTION_EXCLUDE,
28706 NULL);
28708 /* For macro info and the file table we have to refer to a
28709 debug_line section. */
28710 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
28711 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28712 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28713 DEBUG_LINE_SECTION_LABEL, generation);
28715 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
28716 DEBUG_STR_SECTION_FLAGS
28717 | SECTION_EXCLUDE, NULL);
28718 if (!dwarf_split_debug_info)
28719 debug_line_str_section
28720 = get_section (DEBUG_LTO_LINE_STR_SECTION,
28721 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
28723 else
28725 if (!dwarf_split_debug_info)
28727 debug_info_section = get_section (DEBUG_INFO_SECTION,
28728 SECTION_DEBUG, NULL);
28729 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28730 SECTION_DEBUG, NULL);
28731 debug_loc_section = get_section (dwarf_version >= 5
28732 ? DEBUG_LOCLISTS_SECTION
28733 : DEBUG_LOC_SECTION,
28734 SECTION_DEBUG, NULL);
28735 debug_macinfo_section_name
28736 = ((dwarf_strict && dwarf_version < 5)
28737 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
28738 debug_macinfo_section = get_section (debug_macinfo_section_name,
28739 SECTION_DEBUG, NULL);
28741 else
28743 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
28744 SECTION_DEBUG | SECTION_EXCLUDE,
28745 NULL);
28746 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
28747 SECTION_DEBUG | SECTION_EXCLUDE,
28748 NULL);
28749 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
28750 SECTION_DEBUG, NULL);
28751 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
28752 SECTION_DEBUG, NULL);
28753 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28754 SECTION_DEBUG, NULL);
28755 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28756 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28757 generation);
28759 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28760 stay in the main .o, but the skeleton_line goes into the
28761 split off dwo. */
28762 debug_skeleton_line_section
28763 = get_section (DEBUG_DWO_LINE_SECTION,
28764 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28765 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28766 DEBUG_SKELETON_LINE_SECTION_LABEL,
28767 generation);
28768 debug_str_offsets_section
28769 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
28770 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28771 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28772 DEBUG_SKELETON_INFO_SECTION_LABEL,
28773 generation);
28774 debug_loc_section = get_section (dwarf_version >= 5
28775 ? DEBUG_DWO_LOCLISTS_SECTION
28776 : DEBUG_DWO_LOC_SECTION,
28777 SECTION_DEBUG | SECTION_EXCLUDE,
28778 NULL);
28779 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
28780 DEBUG_STR_DWO_SECTION_FLAGS,
28781 NULL);
28782 debug_macinfo_section_name
28783 = ((dwarf_strict && dwarf_version < 5)
28784 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
28785 debug_macinfo_section = get_section (debug_macinfo_section_name,
28786 SECTION_DEBUG | SECTION_EXCLUDE,
28787 NULL);
28789 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
28790 SECTION_DEBUG, NULL);
28791 debug_line_section = get_section (DEBUG_LINE_SECTION,
28792 SECTION_DEBUG, NULL);
28793 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
28794 SECTION_DEBUG, NULL);
28795 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
28796 SECTION_DEBUG, NULL);
28797 debug_str_section = get_section (DEBUG_STR_SECTION,
28798 DEBUG_STR_SECTION_FLAGS, NULL);
28799 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
28800 || asm_outputs_debug_line_str ())
28801 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
28802 DEBUG_STR_SECTION_FLAGS, NULL);
28804 debug_ranges_section = get_section (dwarf_version >= 5
28805 ? DEBUG_RNGLISTS_SECTION
28806 : DEBUG_RANGES_SECTION,
28807 SECTION_DEBUG, NULL);
28808 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
28809 SECTION_DEBUG, NULL);
28812 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
28813 DEBUG_ABBREV_SECTION_LABEL, generation);
28814 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
28815 DEBUG_INFO_SECTION_LABEL, generation);
28816 info_section_emitted = false;
28817 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28818 DEBUG_LINE_SECTION_LABEL, generation);
28819 /* There are up to 4 unique ranges labels per generation.
28820 See also output_rnglists. */
28821 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
28822 DEBUG_RANGES_SECTION_LABEL, generation * 4);
28823 if (dwarf_version >= 5 && dwarf_split_debug_info)
28824 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
28825 DEBUG_RANGES_SECTION_LABEL,
28826 1 + generation * 4);
28827 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
28828 DEBUG_ADDR_SECTION_LABEL, generation);
28829 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
28830 (dwarf_strict && dwarf_version < 5)
28831 ? DEBUG_MACINFO_SECTION_LABEL
28832 : DEBUG_MACRO_SECTION_LABEL, generation);
28833 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
28834 generation);
28836 ++generation;
28837 return generation - 1;
28840 /* Set up for Dwarf output at the start of compilation. */
28842 static void
28843 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
28845 /* Allocate the file_table. */
28846 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
28848 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28849 /* Allocate the decl_die_table. */
28850 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
28852 /* Allocate the decl_loc_table. */
28853 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
28855 /* Allocate the cached_dw_loc_list_table. */
28856 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
28858 /* Allocate the initial hunk of the abbrev_die_table. */
28859 vec_alloc (abbrev_die_table, 256);
28860 /* Zero-th entry is allocated, but unused. */
28861 abbrev_die_table->quick_push (NULL);
28863 /* Allocate the dwarf_proc_stack_usage_map. */
28864 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
28866 /* Allocate the pubtypes and pubnames vectors. */
28867 vec_alloc (pubname_table, 32);
28868 vec_alloc (pubtype_table, 32);
28870 vec_alloc (incomplete_types, 64);
28872 vec_alloc (used_rtx_array, 32);
28874 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28875 vec_alloc (macinfo_table, 64);
28876 #endif
28878 /* If front-ends already registered a main translation unit but we were not
28879 ready to perform the association, do this now. */
28880 if (main_translation_unit != NULL_TREE)
28881 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
28884 /* Called before compile () starts outputtting functions, variables
28885 and toplevel asms into assembly. */
28887 static void
28888 dwarf2out_assembly_start (void)
28890 if (text_section_line_info)
28891 return;
28893 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28894 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
28895 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
28896 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
28897 COLD_TEXT_SECTION_LABEL, 0);
28898 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
28900 switch_to_section (text_section);
28901 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
28902 #endif
28904 /* Make sure the line number table for .text always exists. */
28905 text_section_line_info = new_line_info_table ();
28906 text_section_line_info->end_label = text_end_label;
28908 #ifdef DWARF2_LINENO_DEBUGGING_INFO
28909 cur_line_info_table = text_section_line_info;
28910 #endif
28912 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
28913 && dwarf2out_do_cfi_asm ()
28914 && !dwarf2out_do_eh_frame ())
28915 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
28918 /* A helper function for dwarf2out_finish called through
28919 htab_traverse. Assign a string its index. All strings must be
28920 collected into the table by the time index_string is called,
28921 because the indexing code relies on htab_traverse to traverse nodes
28922 in the same order for each run. */
28925 index_string (indirect_string_node **h, unsigned int *index)
28927 indirect_string_node *node = *h;
28929 find_string_form (node);
28930 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28932 gcc_assert (node->index == NO_INDEX_ASSIGNED);
28933 node->index = *index;
28934 *index += 1;
28936 return 1;
28939 /* A helper function for output_indirect_strings called through
28940 htab_traverse. Output the offset to a string and update the
28941 current offset. */
28944 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
28946 indirect_string_node *node = *h;
28948 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28950 /* Assert that this node has been assigned an index. */
28951 gcc_assert (node->index != NO_INDEX_ASSIGNED
28952 && node->index != NOT_INDEXED);
28953 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
28954 "indexed string 0x%x: %s", node->index, node->str);
28955 *offset += strlen (node->str) + 1;
28957 return 1;
28960 /* A helper function for dwarf2out_finish called through
28961 htab_traverse. Output the indexed string. */
28964 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
28966 struct indirect_string_node *node = *h;
28968 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28970 /* Assert that the strings are output in the same order as their
28971 indexes were assigned. */
28972 gcc_assert (*cur_idx == node->index);
28973 assemble_string (node->str, strlen (node->str) + 1);
28974 *cur_idx += 1;
28976 return 1;
28979 /* A helper function for output_indirect_strings. Counts the number
28980 of index strings offsets. Must match the logic of the functions
28981 output_index_string[_offsets] above. */
28983 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
28985 struct indirect_string_node *node = *h;
28987 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28988 *last_idx += 1;
28989 return 1;
28992 /* A helper function for dwarf2out_finish called through
28993 htab_traverse. Emit one queued .debug_str string. */
28996 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
28998 struct indirect_string_node *node = *h;
29000 node->form = find_string_form (node);
29001 if (node->form == form && node->refcount > 0)
29003 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29004 assemble_string (node->str, strlen (node->str) + 1);
29007 return 1;
29010 /* Output the indexed string table. */
29012 static void
29013 output_indirect_strings (void)
29015 switch_to_section (debug_str_section);
29016 if (!dwarf_split_debug_info)
29017 debug_str_hash->traverse<enum dwarf_form,
29018 output_indirect_string> (DW_FORM_strp);
29019 else
29021 unsigned int offset = 0;
29022 unsigned int cur_idx = 0;
29024 if (skeleton_debug_str_hash)
29025 skeleton_debug_str_hash->traverse<enum dwarf_form,
29026 output_indirect_string> (DW_FORM_strp);
29028 switch_to_section (debug_str_offsets_section);
29029 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29030 header. Note that we don't need to generate a label to the
29031 actual index table following the header here, because this is
29032 for the split dwarf case only. In an .dwo file there is only
29033 one string offsets table (and one debug info section). But
29034 if we would start using string offset tables for the main (or
29035 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29036 pointing to the actual index after the header. Split dwarf
29037 units will never have a string offsets base attribute. When
29038 a split unit is moved into a .dwp file the string offsets can
29039 be found through the .debug_cu_index section table. */
29040 if (dwarf_version >= 5)
29042 unsigned int last_idx = 0;
29043 unsigned long str_offsets_length;
29045 debug_str_hash->traverse_noresize
29046 <unsigned int *, count_index_strings> (&last_idx);
29047 str_offsets_length = last_idx * DWARF_OFFSET_SIZE + 4;
29048 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29049 dw2_asm_output_data (4, 0xffffffff,
29050 "Escape value for 64-bit DWARF extension");
29051 dw2_asm_output_data (DWARF_OFFSET_SIZE, str_offsets_length,
29052 "Length of string offsets unit");
29053 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29054 dw2_asm_output_data (2, 0, "Header zero padding");
29056 debug_str_hash->traverse_noresize
29057 <unsigned int *, output_index_string_offset> (&offset);
29058 switch_to_section (debug_str_dwo_section);
29059 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29060 (&cur_idx);
29064 /* Callback for htab_traverse to assign an index to an entry in the
29065 table, and to write that entry to the .debug_addr section. */
29068 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29070 addr_table_entry *entry = *slot;
29072 if (entry->refcount == 0)
29074 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29075 || entry->index == NOT_INDEXED);
29076 return 1;
29079 gcc_assert (entry->index == *cur_index);
29080 (*cur_index)++;
29082 switch (entry->kind)
29084 case ate_kind_rtx:
29085 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29086 "0x%x", entry->index);
29087 break;
29088 case ate_kind_rtx_dtprel:
29089 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29090 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29091 DWARF2_ADDR_SIZE,
29092 entry->addr.rtl);
29093 fputc ('\n', asm_out_file);
29094 break;
29095 case ate_kind_label:
29096 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29097 "0x%x", entry->index);
29098 break;
29099 default:
29100 gcc_unreachable ();
29102 return 1;
29105 /* A helper function for dwarf2out_finish. Counts the number
29106 of indexed addresses. Must match the logic of the functions
29107 output_addr_table_entry above. */
29109 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29111 addr_table_entry *entry = *slot;
29113 if (entry->refcount > 0)
29114 *last_idx += 1;
29115 return 1;
29118 /* Produce the .debug_addr section. */
29120 static void
29121 output_addr_table (void)
29123 unsigned int index = 0;
29124 if (addr_index_table == NULL || addr_index_table->size () == 0)
29125 return;
29127 switch_to_section (debug_addr_section);
29128 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29129 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29130 before DWARF5, didn't have a header for .debug_addr units.
29131 DWARF5 specifies a small header when address tables are used. */
29132 if (dwarf_version >= 5)
29134 unsigned int last_idx = 0;
29135 unsigned long addrs_length;
29137 addr_index_table->traverse_noresize
29138 <unsigned int *, count_index_addrs> (&last_idx);
29139 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29141 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29142 dw2_asm_output_data (4, 0xffffffff,
29143 "Escape value for 64-bit DWARF extension");
29144 dw2_asm_output_data (DWARF_OFFSET_SIZE, addrs_length,
29145 "Length of Address Unit");
29146 dw2_asm_output_data (2, 5, "DWARF addr version");
29147 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29148 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29150 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29152 addr_index_table
29153 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29156 #if ENABLE_ASSERT_CHECKING
29157 /* Verify that all marks are clear. */
29159 static void
29160 verify_marks_clear (dw_die_ref die)
29162 dw_die_ref c;
29164 gcc_assert (! die->die_mark);
29165 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29167 #endif /* ENABLE_ASSERT_CHECKING */
29169 /* Clear the marks for a die and its children.
29170 Be cool if the mark isn't set. */
29172 static void
29173 prune_unmark_dies (dw_die_ref die)
29175 dw_die_ref c;
29177 if (die->die_mark)
29178 die->die_mark = 0;
29179 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29182 /* Given LOC that is referenced by a DIE we're marking as used, find all
29183 referenced DWARF procedures it references and mark them as used. */
29185 static void
29186 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29188 for (; loc != NULL; loc = loc->dw_loc_next)
29189 switch (loc->dw_loc_opc)
29191 case DW_OP_implicit_pointer:
29192 case DW_OP_convert:
29193 case DW_OP_reinterpret:
29194 case DW_OP_GNU_implicit_pointer:
29195 case DW_OP_GNU_convert:
29196 case DW_OP_GNU_reinterpret:
29197 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29198 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29199 break;
29200 case DW_OP_GNU_variable_value:
29201 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29203 dw_die_ref ref
29204 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29205 if (ref == NULL)
29206 break;
29207 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29208 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29209 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29211 /* FALLTHRU */
29212 case DW_OP_call2:
29213 case DW_OP_call4:
29214 case DW_OP_call_ref:
29215 case DW_OP_const_type:
29216 case DW_OP_GNU_const_type:
29217 case DW_OP_GNU_parameter_ref:
29218 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29219 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29220 break;
29221 case DW_OP_regval_type:
29222 case DW_OP_deref_type:
29223 case DW_OP_GNU_regval_type:
29224 case DW_OP_GNU_deref_type:
29225 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29226 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29227 break;
29228 case DW_OP_entry_value:
29229 case DW_OP_GNU_entry_value:
29230 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29231 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29232 break;
29233 default:
29234 break;
29238 /* Given DIE that we're marking as used, find any other dies
29239 it references as attributes and mark them as used. */
29241 static void
29242 prune_unused_types_walk_attribs (dw_die_ref die)
29244 dw_attr_node *a;
29245 unsigned ix;
29247 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29249 switch (AT_class (a))
29251 /* Make sure DWARF procedures referenced by location descriptions will
29252 get emitted. */
29253 case dw_val_class_loc:
29254 prune_unused_types_walk_loc_descr (AT_loc (a));
29255 break;
29256 case dw_val_class_loc_list:
29257 for (dw_loc_list_ref list = AT_loc_list (a);
29258 list != NULL;
29259 list = list->dw_loc_next)
29260 prune_unused_types_walk_loc_descr (list->expr);
29261 break;
29263 case dw_val_class_view_list:
29264 /* This points to a loc_list in another attribute, so it's
29265 already covered. */
29266 break;
29268 case dw_val_class_die_ref:
29269 /* A reference to another DIE.
29270 Make sure that it will get emitted.
29271 If it was broken out into a comdat group, don't follow it. */
29272 if (! AT_ref (a)->comdat_type_p
29273 || a->dw_attr == DW_AT_specification)
29274 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29275 break;
29277 case dw_val_class_str:
29278 /* Set the string's refcount to 0 so that prune_unused_types_mark
29279 accounts properly for it. */
29280 a->dw_attr_val.v.val_str->refcount = 0;
29281 break;
29283 default:
29284 break;
29289 /* Mark the generic parameters and arguments children DIEs of DIE. */
29291 static void
29292 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29294 dw_die_ref c;
29296 if (die == NULL || die->die_child == NULL)
29297 return;
29298 c = die->die_child;
29301 if (is_template_parameter (c))
29302 prune_unused_types_mark (c, 1);
29303 c = c->die_sib;
29304 } while (c && c != die->die_child);
29307 /* Mark DIE as being used. If DOKIDS is true, then walk down
29308 to DIE's children. */
29310 static void
29311 prune_unused_types_mark (dw_die_ref die, int dokids)
29313 dw_die_ref c;
29315 if (die->die_mark == 0)
29317 /* We haven't done this node yet. Mark it as used. */
29318 die->die_mark = 1;
29319 /* If this is the DIE of a generic type instantiation,
29320 mark the children DIEs that describe its generic parms and
29321 args. */
29322 prune_unused_types_mark_generic_parms_dies (die);
29324 /* We also have to mark its parents as used.
29325 (But we don't want to mark our parent's kids due to this,
29326 unless it is a class.) */
29327 if (die->die_parent)
29328 prune_unused_types_mark (die->die_parent,
29329 class_scope_p (die->die_parent));
29331 /* Mark any referenced nodes. */
29332 prune_unused_types_walk_attribs (die);
29334 /* If this node is a specification,
29335 also mark the definition, if it exists. */
29336 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29337 prune_unused_types_mark (die->die_definition, 1);
29340 if (dokids && die->die_mark != 2)
29342 /* We need to walk the children, but haven't done so yet.
29343 Remember that we've walked the kids. */
29344 die->die_mark = 2;
29346 /* If this is an array type, we need to make sure our
29347 kids get marked, even if they're types. If we're
29348 breaking out types into comdat sections, do this
29349 for all type definitions. */
29350 if (die->die_tag == DW_TAG_array_type
29351 || (use_debug_types
29352 && is_type_die (die) && ! is_declaration_die (die)))
29353 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29354 else
29355 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29359 /* For local classes, look if any static member functions were emitted
29360 and if so, mark them. */
29362 static void
29363 prune_unused_types_walk_local_classes (dw_die_ref die)
29365 dw_die_ref c;
29367 if (die->die_mark == 2)
29368 return;
29370 switch (die->die_tag)
29372 case DW_TAG_structure_type:
29373 case DW_TAG_union_type:
29374 case DW_TAG_class_type:
29375 case DW_TAG_interface_type:
29376 break;
29378 case DW_TAG_subprogram:
29379 if (!get_AT_flag (die, DW_AT_declaration)
29380 || die->die_definition != NULL)
29381 prune_unused_types_mark (die, 1);
29382 return;
29384 default:
29385 return;
29388 /* Mark children. */
29389 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29392 /* Walk the tree DIE and mark types that we actually use. */
29394 static void
29395 prune_unused_types_walk (dw_die_ref die)
29397 dw_die_ref c;
29399 /* Don't do anything if this node is already marked and
29400 children have been marked as well. */
29401 if (die->die_mark == 2)
29402 return;
29404 switch (die->die_tag)
29406 case DW_TAG_structure_type:
29407 case DW_TAG_union_type:
29408 case DW_TAG_class_type:
29409 case DW_TAG_interface_type:
29410 if (die->die_perennial_p)
29411 break;
29413 for (c = die->die_parent; c; c = c->die_parent)
29414 if (c->die_tag == DW_TAG_subprogram)
29415 break;
29417 /* Finding used static member functions inside of classes
29418 is needed just for local classes, because for other classes
29419 static member function DIEs with DW_AT_specification
29420 are emitted outside of the DW_TAG_*_type. If we ever change
29421 it, we'd need to call this even for non-local classes. */
29422 if (c)
29423 prune_unused_types_walk_local_classes (die);
29425 /* It's a type node --- don't mark it. */
29426 return;
29428 case DW_TAG_const_type:
29429 case DW_TAG_packed_type:
29430 case DW_TAG_pointer_type:
29431 case DW_TAG_reference_type:
29432 case DW_TAG_rvalue_reference_type:
29433 case DW_TAG_volatile_type:
29434 case DW_TAG_typedef:
29435 case DW_TAG_array_type:
29436 case DW_TAG_friend:
29437 case DW_TAG_enumeration_type:
29438 case DW_TAG_subroutine_type:
29439 case DW_TAG_string_type:
29440 case DW_TAG_set_type:
29441 case DW_TAG_subrange_type:
29442 case DW_TAG_ptr_to_member_type:
29443 case DW_TAG_file_type:
29444 /* Type nodes are useful only when other DIEs reference them --- don't
29445 mark them. */
29446 /* FALLTHROUGH */
29448 case DW_TAG_dwarf_procedure:
29449 /* Likewise for DWARF procedures. */
29451 if (die->die_perennial_p)
29452 break;
29454 return;
29456 case DW_TAG_variable:
29457 if (flag_debug_only_used_symbols)
29459 if (die->die_perennial_p)
29460 break;
29462 /* premark_used_variables marks external variables --- don't mark
29463 them here. But function-local externals are always considered
29464 used. */
29465 if (get_AT (die, DW_AT_external))
29467 for (c = die->die_parent; c; c = c->die_parent)
29468 if (c->die_tag == DW_TAG_subprogram)
29469 break;
29470 if (!c)
29471 return;
29474 /* FALLTHROUGH */
29476 default:
29477 /* Mark everything else. */
29478 break;
29481 if (die->die_mark == 0)
29483 die->die_mark = 1;
29485 /* Now, mark any dies referenced from here. */
29486 prune_unused_types_walk_attribs (die);
29489 die->die_mark = 2;
29491 /* Mark children. */
29492 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29495 /* Increment the string counts on strings referred to from DIE's
29496 attributes. */
29498 static void
29499 prune_unused_types_update_strings (dw_die_ref die)
29501 dw_attr_node *a;
29502 unsigned ix;
29504 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29505 if (AT_class (a) == dw_val_class_str)
29507 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
29508 s->refcount++;
29509 /* Avoid unnecessarily putting strings that are used less than
29510 twice in the hash table. */
29511 if (s->refcount
29512 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
29514 indirect_string_node **slot
29515 = debug_str_hash->find_slot_with_hash (s->str,
29516 htab_hash_string (s->str),
29517 INSERT);
29518 gcc_assert (*slot == NULL);
29519 *slot = s;
29524 /* Mark DIE and its children as removed. */
29526 static void
29527 mark_removed (dw_die_ref die)
29529 dw_die_ref c;
29530 die->removed = true;
29531 FOR_EACH_CHILD (die, c, mark_removed (c));
29534 /* Remove from the tree DIE any dies that aren't marked. */
29536 static void
29537 prune_unused_types_prune (dw_die_ref die)
29539 dw_die_ref c;
29541 gcc_assert (die->die_mark);
29542 prune_unused_types_update_strings (die);
29544 if (! die->die_child)
29545 return;
29547 c = die->die_child;
29548 do {
29549 dw_die_ref prev = c, next;
29550 for (c = c->die_sib; ! c->die_mark; c = next)
29551 if (c == die->die_child)
29553 /* No marked children between 'prev' and the end of the list. */
29554 if (prev == c)
29555 /* No marked children at all. */
29556 die->die_child = NULL;
29557 else
29559 prev->die_sib = c->die_sib;
29560 die->die_child = prev;
29562 c->die_sib = NULL;
29563 mark_removed (c);
29564 return;
29566 else
29568 next = c->die_sib;
29569 c->die_sib = NULL;
29570 mark_removed (c);
29573 if (c != prev->die_sib)
29574 prev->die_sib = c;
29575 prune_unused_types_prune (c);
29576 } while (c != die->die_child);
29579 /* Remove dies representing declarations that we never use. */
29581 static void
29582 prune_unused_types (void)
29584 unsigned int i;
29585 limbo_die_node *node;
29586 comdat_type_node *ctnode;
29587 pubname_entry *pub;
29588 dw_die_ref base_type;
29590 #if ENABLE_ASSERT_CHECKING
29591 /* All the marks should already be clear. */
29592 verify_marks_clear (comp_unit_die ());
29593 for (node = limbo_die_list; node; node = node->next)
29594 verify_marks_clear (node->die);
29595 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29596 verify_marks_clear (ctnode->root_die);
29597 #endif /* ENABLE_ASSERT_CHECKING */
29599 /* Mark types that are used in global variables. */
29600 premark_types_used_by_global_vars ();
29602 /* Mark variables used in the symtab. */
29603 if (flag_debug_only_used_symbols)
29604 premark_used_variables ();
29606 /* Set the mark on nodes that are actually used. */
29607 prune_unused_types_walk (comp_unit_die ());
29608 for (node = limbo_die_list; node; node = node->next)
29609 prune_unused_types_walk (node->die);
29610 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29612 prune_unused_types_walk (ctnode->root_die);
29613 prune_unused_types_mark (ctnode->type_die, 1);
29616 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
29617 are unusual in that they are pubnames that are the children of pubtypes.
29618 They should only be marked via their parent DW_TAG_enumeration_type die,
29619 not as roots in themselves. */
29620 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
29621 if (pub->die->die_tag != DW_TAG_enumerator)
29622 prune_unused_types_mark (pub->die, 1);
29623 for (i = 0; base_types.iterate (i, &base_type); i++)
29624 prune_unused_types_mark (base_type, 1);
29626 /* Also set the mark on nodes that could be referenced by
29627 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
29628 by DW_TAG_inlined_subroutine origins. */
29629 cgraph_node *cnode;
29630 FOR_EACH_FUNCTION (cnode)
29631 if (cnode->referred_to_p (false))
29633 dw_die_ref die = lookup_decl_die (cnode->decl);
29634 if (die == NULL || die->die_mark)
29635 continue;
29636 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
29637 if (e->caller != cnode)
29639 prune_unused_types_mark (die, 1);
29640 break;
29644 if (debug_str_hash)
29645 debug_str_hash->empty ();
29646 if (skeleton_debug_str_hash)
29647 skeleton_debug_str_hash->empty ();
29648 prune_unused_types_prune (comp_unit_die ());
29649 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
29651 node = *pnode;
29652 if (!node->die->die_mark)
29653 *pnode = node->next;
29654 else
29656 prune_unused_types_prune (node->die);
29657 pnode = &node->next;
29660 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29661 prune_unused_types_prune (ctnode->root_die);
29663 /* Leave the marks clear. */
29664 prune_unmark_dies (comp_unit_die ());
29665 for (node = limbo_die_list; node; node = node->next)
29666 prune_unmark_dies (node->die);
29667 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29668 prune_unmark_dies (ctnode->root_die);
29671 /* Helpers to manipulate hash table of comdat type units. */
29673 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
29675 static inline hashval_t hash (const comdat_type_node *);
29676 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
29679 inline hashval_t
29680 comdat_type_hasher::hash (const comdat_type_node *type_node)
29682 hashval_t h;
29683 memcpy (&h, type_node->signature, sizeof (h));
29684 return h;
29687 inline bool
29688 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
29689 const comdat_type_node *type_node_2)
29691 return (! memcmp (type_node_1->signature, type_node_2->signature,
29692 DWARF_TYPE_SIGNATURE_SIZE));
29695 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
29696 to the location it would have been added, should we know its
29697 DECL_ASSEMBLER_NAME when we added other attributes. This will
29698 probably improve compactness of debug info, removing equivalent
29699 abbrevs, and hide any differences caused by deferring the
29700 computation of the assembler name, triggered by e.g. PCH. */
29702 static inline void
29703 move_linkage_attr (dw_die_ref die)
29705 unsigned ix = vec_safe_length (die->die_attr);
29706 dw_attr_node linkage = (*die->die_attr)[ix - 1];
29708 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
29709 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
29711 while (--ix > 0)
29713 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
29715 if (prev->dw_attr == DW_AT_decl_line
29716 || prev->dw_attr == DW_AT_decl_column
29717 || prev->dw_attr == DW_AT_name)
29718 break;
29721 if (ix != vec_safe_length (die->die_attr) - 1)
29723 die->die_attr->pop ();
29724 die->die_attr->quick_insert (ix, linkage);
29728 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
29729 referenced from typed stack ops and count how often they are used. */
29731 static void
29732 mark_base_types (dw_loc_descr_ref loc)
29734 dw_die_ref base_type = NULL;
29736 for (; loc; loc = loc->dw_loc_next)
29738 switch (loc->dw_loc_opc)
29740 case DW_OP_regval_type:
29741 case DW_OP_deref_type:
29742 case DW_OP_GNU_regval_type:
29743 case DW_OP_GNU_deref_type:
29744 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
29745 break;
29746 case DW_OP_convert:
29747 case DW_OP_reinterpret:
29748 case DW_OP_GNU_convert:
29749 case DW_OP_GNU_reinterpret:
29750 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
29751 continue;
29752 /* FALLTHRU */
29753 case DW_OP_const_type:
29754 case DW_OP_GNU_const_type:
29755 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
29756 break;
29757 case DW_OP_entry_value:
29758 case DW_OP_GNU_entry_value:
29759 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
29760 continue;
29761 default:
29762 continue;
29764 gcc_assert (base_type->die_parent == comp_unit_die ());
29765 if (base_type->die_mark)
29766 base_type->die_mark++;
29767 else
29769 base_types.safe_push (base_type);
29770 base_type->die_mark = 1;
29775 /* Comparison function for sorting marked base types. */
29777 static int
29778 base_type_cmp (const void *x, const void *y)
29780 dw_die_ref dx = *(const dw_die_ref *) x;
29781 dw_die_ref dy = *(const dw_die_ref *) y;
29782 unsigned int byte_size1, byte_size2;
29783 unsigned int encoding1, encoding2;
29784 unsigned int align1, align2;
29785 if (dx->die_mark > dy->die_mark)
29786 return -1;
29787 if (dx->die_mark < dy->die_mark)
29788 return 1;
29789 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
29790 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
29791 if (byte_size1 < byte_size2)
29792 return 1;
29793 if (byte_size1 > byte_size2)
29794 return -1;
29795 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
29796 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
29797 if (encoding1 < encoding2)
29798 return 1;
29799 if (encoding1 > encoding2)
29800 return -1;
29801 align1 = get_AT_unsigned (dx, DW_AT_alignment);
29802 align2 = get_AT_unsigned (dy, DW_AT_alignment);
29803 if (align1 < align2)
29804 return 1;
29805 if (align1 > align2)
29806 return -1;
29807 return 0;
29810 /* Move base types marked by mark_base_types as early as possible
29811 in the CU, sorted by decreasing usage count both to make the
29812 uleb128 references as small as possible and to make sure they
29813 will have die_offset already computed by calc_die_sizes when
29814 sizes of typed stack loc ops is computed. */
29816 static void
29817 move_marked_base_types (void)
29819 unsigned int i;
29820 dw_die_ref base_type, die, c;
29822 if (base_types.is_empty ())
29823 return;
29825 /* Sort by decreasing usage count, they will be added again in that
29826 order later on. */
29827 base_types.qsort (base_type_cmp);
29828 die = comp_unit_die ();
29829 c = die->die_child;
29832 dw_die_ref prev = c;
29833 c = c->die_sib;
29834 while (c->die_mark)
29836 remove_child_with_prev (c, prev);
29837 /* As base types got marked, there must be at least
29838 one node other than DW_TAG_base_type. */
29839 gcc_assert (die->die_child != NULL);
29840 c = prev->die_sib;
29843 while (c != die->die_child);
29844 gcc_assert (die->die_child);
29845 c = die->die_child;
29846 for (i = 0; base_types.iterate (i, &base_type); i++)
29848 base_type->die_mark = 0;
29849 base_type->die_sib = c->die_sib;
29850 c->die_sib = base_type;
29851 c = base_type;
29855 /* Helper function for resolve_addr, attempt to resolve
29856 one CONST_STRING, return true if successful. Similarly verify that
29857 SYMBOL_REFs refer to variables emitted in the current CU. */
29859 static bool
29860 resolve_one_addr (rtx *addr)
29862 rtx rtl = *addr;
29864 if (GET_CODE (rtl) == CONST_STRING)
29866 size_t len = strlen (XSTR (rtl, 0)) + 1;
29867 tree t = build_string (len, XSTR (rtl, 0));
29868 tree tlen = size_int (len - 1);
29869 TREE_TYPE (t)
29870 = build_array_type (char_type_node, build_index_type (tlen));
29871 rtl = lookup_constant_def (t);
29872 if (!rtl || !MEM_P (rtl))
29873 return false;
29874 rtl = XEXP (rtl, 0);
29875 if (GET_CODE (rtl) == SYMBOL_REF
29876 && SYMBOL_REF_DECL (rtl)
29877 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29878 return false;
29879 vec_safe_push (used_rtx_array, rtl);
29880 *addr = rtl;
29881 return true;
29884 if (GET_CODE (rtl) == SYMBOL_REF
29885 && SYMBOL_REF_DECL (rtl))
29887 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
29889 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
29890 return false;
29892 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29893 return false;
29896 if (GET_CODE (rtl) == CONST)
29898 subrtx_ptr_iterator::array_type array;
29899 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
29900 if (!resolve_one_addr (*iter))
29901 return false;
29904 return true;
29907 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
29908 if possible, and create DW_TAG_dwarf_procedure that can be referenced
29909 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
29911 static rtx
29912 string_cst_pool_decl (tree t)
29914 rtx rtl = output_constant_def (t, 1);
29915 unsigned char *array;
29916 dw_loc_descr_ref l;
29917 tree decl;
29918 size_t len;
29919 dw_die_ref ref;
29921 if (!rtl || !MEM_P (rtl))
29922 return NULL_RTX;
29923 rtl = XEXP (rtl, 0);
29924 if (GET_CODE (rtl) != SYMBOL_REF
29925 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
29926 return NULL_RTX;
29928 decl = SYMBOL_REF_DECL (rtl);
29929 if (!lookup_decl_die (decl))
29931 len = TREE_STRING_LENGTH (t);
29932 vec_safe_push (used_rtx_array, rtl);
29933 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
29934 array = ggc_vec_alloc<unsigned char> (len);
29935 memcpy (array, TREE_STRING_POINTER (t), len);
29936 l = new_loc_descr (DW_OP_implicit_value, len, 0);
29937 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
29938 l->dw_loc_oprnd2.v.val_vec.length = len;
29939 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
29940 l->dw_loc_oprnd2.v.val_vec.array = array;
29941 add_AT_loc (ref, DW_AT_location, l);
29942 equate_decl_number_to_die (decl, ref);
29944 return rtl;
29947 /* Helper function of resolve_addr_in_expr. LOC is
29948 a DW_OP_addr followed by DW_OP_stack_value, either at the start
29949 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
29950 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
29951 with DW_OP_implicit_pointer if possible
29952 and return true, if unsuccessful, return false. */
29954 static bool
29955 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
29957 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
29958 HOST_WIDE_INT offset = 0;
29959 dw_die_ref ref = NULL;
29960 tree decl;
29962 if (GET_CODE (rtl) == CONST
29963 && GET_CODE (XEXP (rtl, 0)) == PLUS
29964 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
29966 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
29967 rtl = XEXP (XEXP (rtl, 0), 0);
29969 if (GET_CODE (rtl) == CONST_STRING)
29971 size_t len = strlen (XSTR (rtl, 0)) + 1;
29972 tree t = build_string (len, XSTR (rtl, 0));
29973 tree tlen = size_int (len - 1);
29975 TREE_TYPE (t)
29976 = build_array_type (char_type_node, build_index_type (tlen));
29977 rtl = string_cst_pool_decl (t);
29978 if (!rtl)
29979 return false;
29981 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
29983 decl = SYMBOL_REF_DECL (rtl);
29984 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
29986 ref = lookup_decl_die (decl);
29987 if (ref && (get_AT (ref, DW_AT_location)
29988 || get_AT (ref, DW_AT_const_value)))
29990 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
29991 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29992 loc->dw_loc_oprnd1.val_entry = NULL;
29993 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29994 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29995 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29996 loc->dw_loc_oprnd2.v.val_int = offset;
29997 return true;
30001 return false;
30004 /* Helper function for resolve_addr, handle one location
30005 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30006 the location list couldn't be resolved. */
30008 static bool
30009 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30011 dw_loc_descr_ref keep = NULL;
30012 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30013 switch (loc->dw_loc_opc)
30015 case DW_OP_addr:
30016 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30018 if ((prev == NULL
30019 || prev->dw_loc_opc == DW_OP_piece
30020 || prev->dw_loc_opc == DW_OP_bit_piece)
30021 && loc->dw_loc_next
30022 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30023 && (!dwarf_strict || dwarf_version >= 5)
30024 && optimize_one_addr_into_implicit_ptr (loc))
30025 break;
30026 return false;
30028 break;
30029 case DW_OP_GNU_addr_index:
30030 case DW_OP_addrx:
30031 case DW_OP_GNU_const_index:
30032 case DW_OP_constx:
30033 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30034 || loc->dw_loc_opc == DW_OP_addrx)
30035 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30036 || loc->dw_loc_opc == DW_OP_constx)
30037 && loc->dtprel))
30039 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30040 if (!resolve_one_addr (&rtl))
30041 return false;
30042 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30043 loc->dw_loc_oprnd1.val_entry
30044 = add_addr_table_entry (rtl, ate_kind_rtx);
30046 break;
30047 case DW_OP_const4u:
30048 case DW_OP_const8u:
30049 if (loc->dtprel
30050 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30051 return false;
30052 break;
30053 case DW_OP_plus_uconst:
30054 if (size_of_loc_descr (loc)
30055 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30057 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30059 dw_loc_descr_ref repl
30060 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30061 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30062 add_loc_descr (&repl, loc->dw_loc_next);
30063 *loc = *repl;
30065 break;
30066 case DW_OP_implicit_value:
30067 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30068 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30069 return false;
30070 break;
30071 case DW_OP_implicit_pointer:
30072 case DW_OP_GNU_implicit_pointer:
30073 case DW_OP_GNU_parameter_ref:
30074 case DW_OP_GNU_variable_value:
30075 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30077 dw_die_ref ref
30078 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30079 if (ref == NULL)
30080 return false;
30081 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30082 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30083 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30085 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30087 if (prev == NULL
30088 && loc->dw_loc_next == NULL
30089 && AT_class (a) == dw_val_class_loc)
30090 switch (a->dw_attr)
30092 /* Following attributes allow both exprloc and reference,
30093 so if the whole expression is DW_OP_GNU_variable_value
30094 alone we could transform it into reference. */
30095 case DW_AT_byte_size:
30096 case DW_AT_bit_size:
30097 case DW_AT_lower_bound:
30098 case DW_AT_upper_bound:
30099 case DW_AT_bit_stride:
30100 case DW_AT_count:
30101 case DW_AT_allocated:
30102 case DW_AT_associated:
30103 case DW_AT_byte_stride:
30104 a->dw_attr_val.val_class = dw_val_class_die_ref;
30105 a->dw_attr_val.val_entry = NULL;
30106 a->dw_attr_val.v.val_die_ref.die
30107 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30108 a->dw_attr_val.v.val_die_ref.external = 0;
30109 return true;
30110 default:
30111 break;
30113 if (dwarf_strict)
30114 return false;
30116 break;
30117 case DW_OP_const_type:
30118 case DW_OP_regval_type:
30119 case DW_OP_deref_type:
30120 case DW_OP_convert:
30121 case DW_OP_reinterpret:
30122 case DW_OP_GNU_const_type:
30123 case DW_OP_GNU_regval_type:
30124 case DW_OP_GNU_deref_type:
30125 case DW_OP_GNU_convert:
30126 case DW_OP_GNU_reinterpret:
30127 while (loc->dw_loc_next
30128 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30129 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30131 dw_die_ref base1, base2;
30132 unsigned enc1, enc2, size1, size2;
30133 if (loc->dw_loc_opc == DW_OP_regval_type
30134 || loc->dw_loc_opc == DW_OP_deref_type
30135 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30136 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30137 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30138 else if (loc->dw_loc_oprnd1.val_class
30139 == dw_val_class_unsigned_const)
30140 break;
30141 else
30142 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30143 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30144 == dw_val_class_unsigned_const)
30145 break;
30146 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30147 gcc_assert (base1->die_tag == DW_TAG_base_type
30148 && base2->die_tag == DW_TAG_base_type);
30149 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30150 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30151 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30152 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30153 if (size1 == size2
30154 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30155 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30156 && loc != keep)
30157 || enc1 == enc2))
30159 /* Optimize away next DW_OP_convert after
30160 adjusting LOC's base type die reference. */
30161 if (loc->dw_loc_opc == DW_OP_regval_type
30162 || loc->dw_loc_opc == DW_OP_deref_type
30163 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30164 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30165 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30166 else
30167 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30168 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30169 continue;
30171 /* Don't change integer DW_OP_convert after e.g. floating
30172 point typed stack entry. */
30173 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30174 keep = loc->dw_loc_next;
30175 break;
30177 break;
30178 default:
30179 break;
30181 return true;
30184 /* Helper function of resolve_addr. DIE had DW_AT_location of
30185 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30186 and DW_OP_addr couldn't be resolved. resolve_addr has already
30187 removed the DW_AT_location attribute. This function attempts to
30188 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30189 to it or DW_AT_const_value attribute, if possible. */
30191 static void
30192 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30194 if (!VAR_P (decl)
30195 || lookup_decl_die (decl) != die
30196 || DECL_EXTERNAL (decl)
30197 || !TREE_STATIC (decl)
30198 || DECL_INITIAL (decl) == NULL_TREE
30199 || DECL_P (DECL_INITIAL (decl))
30200 || get_AT (die, DW_AT_const_value))
30201 return;
30203 tree init = DECL_INITIAL (decl);
30204 HOST_WIDE_INT offset = 0;
30205 /* For variables that have been optimized away and thus
30206 don't have a memory location, see if we can emit
30207 DW_AT_const_value instead. */
30208 if (tree_add_const_value_attribute (die, init))
30209 return;
30210 if (dwarf_strict && dwarf_version < 5)
30211 return;
30212 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30213 and ADDR_EXPR refers to a decl that has DW_AT_location or
30214 DW_AT_const_value (but isn't addressable, otherwise
30215 resolving the original DW_OP_addr wouldn't fail), see if
30216 we can add DW_OP_implicit_pointer. */
30217 STRIP_NOPS (init);
30218 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30219 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30221 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30222 init = TREE_OPERAND (init, 0);
30223 STRIP_NOPS (init);
30225 if (TREE_CODE (init) != ADDR_EXPR)
30226 return;
30227 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30228 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30229 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30230 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30231 && TREE_OPERAND (init, 0) != decl))
30233 dw_die_ref ref;
30234 dw_loc_descr_ref l;
30236 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30238 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30239 if (!rtl)
30240 return;
30241 decl = SYMBOL_REF_DECL (rtl);
30243 else
30244 decl = TREE_OPERAND (init, 0);
30245 ref = lookup_decl_die (decl);
30246 if (ref == NULL
30247 || (!get_AT (ref, DW_AT_location)
30248 && !get_AT (ref, DW_AT_const_value)))
30249 return;
30250 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30251 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30252 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30253 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30254 add_AT_loc (die, DW_AT_location, l);
30258 /* Return NULL if l is a DWARF expression, or first op that is not
30259 valid DWARF expression. */
30261 static dw_loc_descr_ref
30262 non_dwarf_expression (dw_loc_descr_ref l)
30264 while (l)
30266 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30267 return l;
30268 switch (l->dw_loc_opc)
30270 case DW_OP_regx:
30271 case DW_OP_implicit_value:
30272 case DW_OP_stack_value:
30273 case DW_OP_implicit_pointer:
30274 case DW_OP_GNU_implicit_pointer:
30275 case DW_OP_GNU_parameter_ref:
30276 case DW_OP_piece:
30277 case DW_OP_bit_piece:
30278 return l;
30279 default:
30280 break;
30282 l = l->dw_loc_next;
30284 return NULL;
30287 /* Return adjusted copy of EXPR:
30288 If it is empty DWARF expression, return it.
30289 If it is valid non-empty DWARF expression,
30290 return copy of EXPR with DW_OP_deref appended to it.
30291 If it is DWARF expression followed by DW_OP_reg{N,x}, return
30292 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30293 If it is DWARF expression followed by DW_OP_stack_value, return
30294 copy of the DWARF expression without anything appended.
30295 Otherwise, return NULL. */
30297 static dw_loc_descr_ref
30298 copy_deref_exprloc (dw_loc_descr_ref expr)
30300 dw_loc_descr_ref tail = NULL;
30302 if (expr == NULL)
30303 return NULL;
30305 dw_loc_descr_ref l = non_dwarf_expression (expr);
30306 if (l && l->dw_loc_next)
30307 return NULL;
30309 if (l)
30311 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30312 tail = new_loc_descr ((enum dwarf_location_atom)
30313 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30314 0, 0);
30315 else
30316 switch (l->dw_loc_opc)
30318 case DW_OP_regx:
30319 tail = new_loc_descr (DW_OP_bregx,
30320 l->dw_loc_oprnd1.v.val_unsigned, 0);
30321 break;
30322 case DW_OP_stack_value:
30323 break;
30324 default:
30325 return NULL;
30328 else
30329 tail = new_loc_descr (DW_OP_deref, 0, 0);
30331 dw_loc_descr_ref ret = NULL, *p = &ret;
30332 while (expr != l)
30334 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30335 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30336 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30337 p = &(*p)->dw_loc_next;
30338 expr = expr->dw_loc_next;
30340 *p = tail;
30341 return ret;
30344 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30345 reference to a variable or argument, adjust it if needed and return:
30346 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30347 attribute if present should be removed
30348 0 keep the attribute perhaps with minor modifications, no need to rescan
30349 1 if the attribute has been successfully adjusted. */
30351 static int
30352 optimize_string_length (dw_attr_node *a)
30354 dw_loc_descr_ref l = AT_loc (a), lv;
30355 dw_die_ref die;
30356 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30358 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30359 die = lookup_decl_die (decl);
30360 if (die)
30362 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30363 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30364 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30366 else
30367 return -1;
30369 else
30370 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30372 /* DWARF5 allows reference class, so we can then reference the DIE.
30373 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30374 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30376 a->dw_attr_val.val_class = dw_val_class_die_ref;
30377 a->dw_attr_val.val_entry = NULL;
30378 a->dw_attr_val.v.val_die_ref.die = die;
30379 a->dw_attr_val.v.val_die_ref.external = 0;
30380 return 0;
30383 dw_attr_node *av = get_AT (die, DW_AT_location);
30384 dw_loc_list_ref d;
30385 bool non_dwarf_expr = false;
30387 if (av == NULL)
30388 return dwarf_strict ? -1 : 0;
30389 switch (AT_class (av))
30391 case dw_val_class_loc_list:
30392 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30393 if (d->expr && non_dwarf_expression (d->expr))
30394 non_dwarf_expr = true;
30395 break;
30396 case dw_val_class_view_list:
30397 gcc_unreachable ();
30398 case dw_val_class_loc:
30399 lv = AT_loc (av);
30400 if (lv == NULL)
30401 return dwarf_strict ? -1 : 0;
30402 if (non_dwarf_expression (lv))
30403 non_dwarf_expr = true;
30404 break;
30405 default:
30406 return dwarf_strict ? -1 : 0;
30409 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30410 into DW_OP_call4 or DW_OP_GNU_variable_value into
30411 DW_OP_call4 DW_OP_deref, do so. */
30412 if (!non_dwarf_expr
30413 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30415 l->dw_loc_opc = DW_OP_call4;
30416 if (l->dw_loc_next)
30417 l->dw_loc_next = NULL;
30418 else
30419 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30420 return 0;
30423 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30424 copy over the DW_AT_location attribute from die to a. */
30425 if (l->dw_loc_next != NULL)
30427 a->dw_attr_val = av->dw_attr_val;
30428 return 1;
30431 dw_loc_list_ref list, *p;
30432 switch (AT_class (av))
30434 case dw_val_class_loc_list:
30435 p = &list;
30436 list = NULL;
30437 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30439 lv = copy_deref_exprloc (d->expr);
30440 if (lv)
30442 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
30443 p = &(*p)->dw_loc_next;
30445 else if (!dwarf_strict && d->expr)
30446 return 0;
30448 if (list == NULL)
30449 return dwarf_strict ? -1 : 0;
30450 a->dw_attr_val.val_class = dw_val_class_loc_list;
30451 gen_llsym (list);
30452 *AT_loc_list_ptr (a) = list;
30453 return 1;
30454 case dw_val_class_loc:
30455 lv = copy_deref_exprloc (AT_loc (av));
30456 if (lv == NULL)
30457 return dwarf_strict ? -1 : 0;
30458 a->dw_attr_val.v.val_loc = lv;
30459 return 1;
30460 default:
30461 gcc_unreachable ();
30465 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
30466 an address in .rodata section if the string literal is emitted there,
30467 or remove the containing location list or replace DW_AT_const_value
30468 with DW_AT_location and empty location expression, if it isn't found
30469 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
30470 to something that has been emitted in the current CU. */
30472 static void
30473 resolve_addr (dw_die_ref die)
30475 dw_die_ref c;
30476 dw_attr_node *a;
30477 dw_loc_list_ref *curr, *start, loc;
30478 unsigned ix;
30479 bool remove_AT_byte_size = false;
30481 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30482 switch (AT_class (a))
30484 case dw_val_class_loc_list:
30485 start = curr = AT_loc_list_ptr (a);
30486 loc = *curr;
30487 gcc_assert (loc);
30488 /* The same list can be referenced more than once. See if we have
30489 already recorded the result from a previous pass. */
30490 if (loc->replaced)
30491 *curr = loc->dw_loc_next;
30492 else if (!loc->resolved_addr)
30494 /* As things stand, we do not expect or allow one die to
30495 reference a suffix of another die's location list chain.
30496 References must be identical or completely separate.
30497 There is therefore no need to cache the result of this
30498 pass on any list other than the first; doing so
30499 would lead to unnecessary writes. */
30500 while (*curr)
30502 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
30503 if (!resolve_addr_in_expr (a, (*curr)->expr))
30505 dw_loc_list_ref next = (*curr)->dw_loc_next;
30506 dw_loc_descr_ref l = (*curr)->expr;
30508 if (next && (*curr)->ll_symbol)
30510 gcc_assert (!next->ll_symbol);
30511 next->ll_symbol = (*curr)->ll_symbol;
30512 next->vl_symbol = (*curr)->vl_symbol;
30514 if (dwarf_split_debug_info)
30515 remove_loc_list_addr_table_entries (l);
30516 *curr = next;
30518 else
30520 mark_base_types ((*curr)->expr);
30521 curr = &(*curr)->dw_loc_next;
30524 if (loc == *start)
30525 loc->resolved_addr = 1;
30526 else
30528 loc->replaced = 1;
30529 loc->dw_loc_next = *start;
30532 if (!*start)
30534 remove_AT (die, a->dw_attr);
30535 ix--;
30537 break;
30538 case dw_val_class_view_list:
30540 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30541 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
30542 dw_val_node *llnode
30543 = view_list_to_loc_list_val_node (&a->dw_attr_val);
30544 /* If we no longer have a loclist, or it no longer needs
30545 views, drop this attribute. */
30546 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
30548 remove_AT (die, a->dw_attr);
30549 ix--;
30551 break;
30553 case dw_val_class_loc:
30555 dw_loc_descr_ref l = AT_loc (a);
30556 /* DW_OP_GNU_variable_value DW_OP_stack_value or
30557 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
30558 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
30559 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
30560 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
30561 with DW_FORM_ref referencing the same DIE as
30562 DW_OP_GNU_variable_value used to reference. */
30563 if (a->dw_attr == DW_AT_string_length
30564 && l
30565 && l->dw_loc_opc == DW_OP_GNU_variable_value
30566 && (l->dw_loc_next == NULL
30567 || (l->dw_loc_next->dw_loc_next == NULL
30568 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
30570 switch (optimize_string_length (a))
30572 case -1:
30573 remove_AT (die, a->dw_attr);
30574 ix--;
30575 /* If we drop DW_AT_string_length, we need to drop also
30576 DW_AT_{string_length_,}byte_size. */
30577 remove_AT_byte_size = true;
30578 continue;
30579 default:
30580 break;
30581 case 1:
30582 /* Even if we keep the optimized DW_AT_string_length,
30583 it might have changed AT_class, so process it again. */
30584 ix--;
30585 continue;
30588 /* For -gdwarf-2 don't attempt to optimize
30589 DW_AT_data_member_location containing
30590 DW_OP_plus_uconst - older consumers might
30591 rely on it being that op instead of a more complex,
30592 but shorter, location description. */
30593 if ((dwarf_version > 2
30594 || a->dw_attr != DW_AT_data_member_location
30595 || l == NULL
30596 || l->dw_loc_opc != DW_OP_plus_uconst
30597 || l->dw_loc_next != NULL)
30598 && !resolve_addr_in_expr (a, l))
30600 if (dwarf_split_debug_info)
30601 remove_loc_list_addr_table_entries (l);
30602 if (l != NULL
30603 && l->dw_loc_next == NULL
30604 && l->dw_loc_opc == DW_OP_addr
30605 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
30606 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
30607 && a->dw_attr == DW_AT_location)
30609 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
30610 remove_AT (die, a->dw_attr);
30611 ix--;
30612 optimize_location_into_implicit_ptr (die, decl);
30613 break;
30615 if (a->dw_attr == DW_AT_string_length)
30616 /* If we drop DW_AT_string_length, we need to drop also
30617 DW_AT_{string_length_,}byte_size. */
30618 remove_AT_byte_size = true;
30619 remove_AT (die, a->dw_attr);
30620 ix--;
30622 else
30623 mark_base_types (l);
30625 break;
30626 case dw_val_class_addr:
30627 if (a->dw_attr == DW_AT_const_value
30628 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
30630 if (AT_index (a) != NOT_INDEXED)
30631 remove_addr_table_entry (a->dw_attr_val.val_entry);
30632 remove_AT (die, a->dw_attr);
30633 ix--;
30635 if ((die->die_tag == DW_TAG_call_site
30636 && a->dw_attr == DW_AT_call_origin)
30637 || (die->die_tag == DW_TAG_GNU_call_site
30638 && a->dw_attr == DW_AT_abstract_origin))
30640 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
30641 dw_die_ref tdie = lookup_decl_die (tdecl);
30642 dw_die_ref cdie;
30643 if (tdie == NULL
30644 && DECL_EXTERNAL (tdecl)
30645 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
30646 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
30648 dw_die_ref pdie = cdie;
30649 /* Make sure we don't add these DIEs into type units.
30650 We could emit skeleton DIEs for context (namespaces,
30651 outer structs/classes) and a skeleton DIE for the
30652 innermost context with DW_AT_signature pointing to the
30653 type unit. See PR78835. */
30654 while (pdie && pdie->die_tag != DW_TAG_type_unit)
30655 pdie = pdie->die_parent;
30656 if (pdie == NULL)
30658 /* Creating a full DIE for tdecl is overly expensive and
30659 at this point even wrong when in the LTO phase
30660 as it can end up generating new type DIEs we didn't
30661 output and thus optimize_external_refs will crash. */
30662 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
30663 add_AT_flag (tdie, DW_AT_external, 1);
30664 add_AT_flag (tdie, DW_AT_declaration, 1);
30665 add_linkage_attr (tdie, tdecl);
30666 add_name_and_src_coords_attributes (tdie, tdecl, true);
30667 equate_decl_number_to_die (tdecl, tdie);
30670 if (tdie)
30672 a->dw_attr_val.val_class = dw_val_class_die_ref;
30673 a->dw_attr_val.v.val_die_ref.die = tdie;
30674 a->dw_attr_val.v.val_die_ref.external = 0;
30676 else
30678 if (AT_index (a) != NOT_INDEXED)
30679 remove_addr_table_entry (a->dw_attr_val.val_entry);
30680 remove_AT (die, a->dw_attr);
30681 ix--;
30684 break;
30685 default:
30686 break;
30689 if (remove_AT_byte_size)
30690 remove_AT (die, dwarf_version >= 5
30691 ? DW_AT_string_length_byte_size
30692 : DW_AT_byte_size);
30694 FOR_EACH_CHILD (die, c, resolve_addr (c));
30697 /* Helper routines for optimize_location_lists.
30698 This pass tries to share identical local lists in .debug_loc
30699 section. */
30701 /* Iteratively hash operands of LOC opcode into HSTATE. */
30703 static void
30704 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
30706 dw_val_ref val1 = &loc->dw_loc_oprnd1;
30707 dw_val_ref val2 = &loc->dw_loc_oprnd2;
30709 switch (loc->dw_loc_opc)
30711 case DW_OP_const4u:
30712 case DW_OP_const8u:
30713 if (loc->dtprel)
30714 goto hash_addr;
30715 /* FALLTHRU */
30716 case DW_OP_const1u:
30717 case DW_OP_const1s:
30718 case DW_OP_const2u:
30719 case DW_OP_const2s:
30720 case DW_OP_const4s:
30721 case DW_OP_const8s:
30722 case DW_OP_constu:
30723 case DW_OP_consts:
30724 case DW_OP_pick:
30725 case DW_OP_plus_uconst:
30726 case DW_OP_breg0:
30727 case DW_OP_breg1:
30728 case DW_OP_breg2:
30729 case DW_OP_breg3:
30730 case DW_OP_breg4:
30731 case DW_OP_breg5:
30732 case DW_OP_breg6:
30733 case DW_OP_breg7:
30734 case DW_OP_breg8:
30735 case DW_OP_breg9:
30736 case DW_OP_breg10:
30737 case DW_OP_breg11:
30738 case DW_OP_breg12:
30739 case DW_OP_breg13:
30740 case DW_OP_breg14:
30741 case DW_OP_breg15:
30742 case DW_OP_breg16:
30743 case DW_OP_breg17:
30744 case DW_OP_breg18:
30745 case DW_OP_breg19:
30746 case DW_OP_breg20:
30747 case DW_OP_breg21:
30748 case DW_OP_breg22:
30749 case DW_OP_breg23:
30750 case DW_OP_breg24:
30751 case DW_OP_breg25:
30752 case DW_OP_breg26:
30753 case DW_OP_breg27:
30754 case DW_OP_breg28:
30755 case DW_OP_breg29:
30756 case DW_OP_breg30:
30757 case DW_OP_breg31:
30758 case DW_OP_regx:
30759 case DW_OP_fbreg:
30760 case DW_OP_piece:
30761 case DW_OP_deref_size:
30762 case DW_OP_xderef_size:
30763 hstate.add_object (val1->v.val_int);
30764 break;
30765 case DW_OP_skip:
30766 case DW_OP_bra:
30768 int offset;
30770 gcc_assert (val1->val_class == dw_val_class_loc);
30771 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
30772 hstate.add_object (offset);
30774 break;
30775 case DW_OP_implicit_value:
30776 hstate.add_object (val1->v.val_unsigned);
30777 switch (val2->val_class)
30779 case dw_val_class_const:
30780 hstate.add_object (val2->v.val_int);
30781 break;
30782 case dw_val_class_vec:
30784 unsigned int elt_size = val2->v.val_vec.elt_size;
30785 unsigned int len = val2->v.val_vec.length;
30787 hstate.add_int (elt_size);
30788 hstate.add_int (len);
30789 hstate.add (val2->v.val_vec.array, len * elt_size);
30791 break;
30792 case dw_val_class_const_double:
30793 hstate.add_object (val2->v.val_double.low);
30794 hstate.add_object (val2->v.val_double.high);
30795 break;
30796 case dw_val_class_wide_int:
30797 hstate.add (val2->v.val_wide->get_val (),
30798 get_full_len (*val2->v.val_wide)
30799 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30800 break;
30801 case dw_val_class_addr:
30802 inchash::add_rtx (val2->v.val_addr, hstate);
30803 break;
30804 default:
30805 gcc_unreachable ();
30807 break;
30808 case DW_OP_bregx:
30809 case DW_OP_bit_piece:
30810 hstate.add_object (val1->v.val_int);
30811 hstate.add_object (val2->v.val_int);
30812 break;
30813 case DW_OP_addr:
30814 hash_addr:
30815 if (loc->dtprel)
30817 unsigned char dtprel = 0xd1;
30818 hstate.add_object (dtprel);
30820 inchash::add_rtx (val1->v.val_addr, hstate);
30821 break;
30822 case DW_OP_GNU_addr_index:
30823 case DW_OP_addrx:
30824 case DW_OP_GNU_const_index:
30825 case DW_OP_constx:
30827 if (loc->dtprel)
30829 unsigned char dtprel = 0xd1;
30830 hstate.add_object (dtprel);
30832 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
30834 break;
30835 case DW_OP_implicit_pointer:
30836 case DW_OP_GNU_implicit_pointer:
30837 hstate.add_int (val2->v.val_int);
30838 break;
30839 case DW_OP_entry_value:
30840 case DW_OP_GNU_entry_value:
30841 hstate.add_object (val1->v.val_loc);
30842 break;
30843 case DW_OP_regval_type:
30844 case DW_OP_deref_type:
30845 case DW_OP_GNU_regval_type:
30846 case DW_OP_GNU_deref_type:
30848 unsigned int byte_size
30849 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
30850 unsigned int encoding
30851 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
30852 hstate.add_object (val1->v.val_int);
30853 hstate.add_object (byte_size);
30854 hstate.add_object (encoding);
30856 break;
30857 case DW_OP_convert:
30858 case DW_OP_reinterpret:
30859 case DW_OP_GNU_convert:
30860 case DW_OP_GNU_reinterpret:
30861 if (val1->val_class == dw_val_class_unsigned_const)
30863 hstate.add_object (val1->v.val_unsigned);
30864 break;
30866 /* FALLTHRU */
30867 case DW_OP_const_type:
30868 case DW_OP_GNU_const_type:
30870 unsigned int byte_size
30871 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
30872 unsigned int encoding
30873 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
30874 hstate.add_object (byte_size);
30875 hstate.add_object (encoding);
30876 if (loc->dw_loc_opc != DW_OP_const_type
30877 && loc->dw_loc_opc != DW_OP_GNU_const_type)
30878 break;
30879 hstate.add_object (val2->val_class);
30880 switch (val2->val_class)
30882 case dw_val_class_const:
30883 hstate.add_object (val2->v.val_int);
30884 break;
30885 case dw_val_class_vec:
30887 unsigned int elt_size = val2->v.val_vec.elt_size;
30888 unsigned int len = val2->v.val_vec.length;
30890 hstate.add_object (elt_size);
30891 hstate.add_object (len);
30892 hstate.add (val2->v.val_vec.array, len * elt_size);
30894 break;
30895 case dw_val_class_const_double:
30896 hstate.add_object (val2->v.val_double.low);
30897 hstate.add_object (val2->v.val_double.high);
30898 break;
30899 case dw_val_class_wide_int:
30900 hstate.add (val2->v.val_wide->get_val (),
30901 get_full_len (*val2->v.val_wide)
30902 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30903 break;
30904 default:
30905 gcc_unreachable ();
30908 break;
30910 default:
30911 /* Other codes have no operands. */
30912 break;
30916 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
30918 static inline void
30919 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
30921 dw_loc_descr_ref l;
30922 bool sizes_computed = false;
30923 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
30924 size_of_locs (loc);
30926 for (l = loc; l != NULL; l = l->dw_loc_next)
30928 enum dwarf_location_atom opc = l->dw_loc_opc;
30929 hstate.add_object (opc);
30930 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
30932 size_of_locs (loc);
30933 sizes_computed = true;
30935 hash_loc_operands (l, hstate);
30939 /* Compute hash of the whole location list LIST_HEAD. */
30941 static inline void
30942 hash_loc_list (dw_loc_list_ref list_head)
30944 dw_loc_list_ref curr = list_head;
30945 inchash::hash hstate;
30947 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
30949 hstate.add (curr->begin, strlen (curr->begin) + 1);
30950 hstate.add (curr->end, strlen (curr->end) + 1);
30951 hstate.add_object (curr->vbegin);
30952 hstate.add_object (curr->vend);
30953 if (curr->section)
30954 hstate.add (curr->section, strlen (curr->section) + 1);
30955 hash_locs (curr->expr, hstate);
30957 list_head->hash = hstate.end ();
30960 /* Return true if X and Y opcodes have the same operands. */
30962 static inline bool
30963 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
30965 dw_val_ref valx1 = &x->dw_loc_oprnd1;
30966 dw_val_ref valx2 = &x->dw_loc_oprnd2;
30967 dw_val_ref valy1 = &y->dw_loc_oprnd1;
30968 dw_val_ref valy2 = &y->dw_loc_oprnd2;
30970 switch (x->dw_loc_opc)
30972 case DW_OP_const4u:
30973 case DW_OP_const8u:
30974 if (x->dtprel)
30975 goto hash_addr;
30976 /* FALLTHRU */
30977 case DW_OP_const1u:
30978 case DW_OP_const1s:
30979 case DW_OP_const2u:
30980 case DW_OP_const2s:
30981 case DW_OP_const4s:
30982 case DW_OP_const8s:
30983 case DW_OP_constu:
30984 case DW_OP_consts:
30985 case DW_OP_pick:
30986 case DW_OP_plus_uconst:
30987 case DW_OP_breg0:
30988 case DW_OP_breg1:
30989 case DW_OP_breg2:
30990 case DW_OP_breg3:
30991 case DW_OP_breg4:
30992 case DW_OP_breg5:
30993 case DW_OP_breg6:
30994 case DW_OP_breg7:
30995 case DW_OP_breg8:
30996 case DW_OP_breg9:
30997 case DW_OP_breg10:
30998 case DW_OP_breg11:
30999 case DW_OP_breg12:
31000 case DW_OP_breg13:
31001 case DW_OP_breg14:
31002 case DW_OP_breg15:
31003 case DW_OP_breg16:
31004 case DW_OP_breg17:
31005 case DW_OP_breg18:
31006 case DW_OP_breg19:
31007 case DW_OP_breg20:
31008 case DW_OP_breg21:
31009 case DW_OP_breg22:
31010 case DW_OP_breg23:
31011 case DW_OP_breg24:
31012 case DW_OP_breg25:
31013 case DW_OP_breg26:
31014 case DW_OP_breg27:
31015 case DW_OP_breg28:
31016 case DW_OP_breg29:
31017 case DW_OP_breg30:
31018 case DW_OP_breg31:
31019 case DW_OP_regx:
31020 case DW_OP_fbreg:
31021 case DW_OP_piece:
31022 case DW_OP_deref_size:
31023 case DW_OP_xderef_size:
31024 return valx1->v.val_int == valy1->v.val_int;
31025 case DW_OP_skip:
31026 case DW_OP_bra:
31027 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31028 can cause irrelevant differences in dw_loc_addr. */
31029 gcc_assert (valx1->val_class == dw_val_class_loc
31030 && valy1->val_class == dw_val_class_loc
31031 && (dwarf_split_debug_info
31032 || x->dw_loc_addr == y->dw_loc_addr));
31033 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31034 case DW_OP_implicit_value:
31035 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31036 || valx2->val_class != valy2->val_class)
31037 return false;
31038 switch (valx2->val_class)
31040 case dw_val_class_const:
31041 return valx2->v.val_int == valy2->v.val_int;
31042 case dw_val_class_vec:
31043 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31044 && valx2->v.val_vec.length == valy2->v.val_vec.length
31045 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31046 valx2->v.val_vec.elt_size
31047 * valx2->v.val_vec.length) == 0;
31048 case dw_val_class_const_double:
31049 return valx2->v.val_double.low == valy2->v.val_double.low
31050 && valx2->v.val_double.high == valy2->v.val_double.high;
31051 case dw_val_class_wide_int:
31052 return *valx2->v.val_wide == *valy2->v.val_wide;
31053 case dw_val_class_addr:
31054 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31055 default:
31056 gcc_unreachable ();
31058 case DW_OP_bregx:
31059 case DW_OP_bit_piece:
31060 return valx1->v.val_int == valy1->v.val_int
31061 && valx2->v.val_int == valy2->v.val_int;
31062 case DW_OP_addr:
31063 hash_addr:
31064 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31065 case DW_OP_GNU_addr_index:
31066 case DW_OP_addrx:
31067 case DW_OP_GNU_const_index:
31068 case DW_OP_constx:
31070 rtx ax1 = valx1->val_entry->addr.rtl;
31071 rtx ay1 = valy1->val_entry->addr.rtl;
31072 return rtx_equal_p (ax1, ay1);
31074 case DW_OP_implicit_pointer:
31075 case DW_OP_GNU_implicit_pointer:
31076 return valx1->val_class == dw_val_class_die_ref
31077 && valx1->val_class == valy1->val_class
31078 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31079 && valx2->v.val_int == valy2->v.val_int;
31080 case DW_OP_entry_value:
31081 case DW_OP_GNU_entry_value:
31082 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31083 case DW_OP_const_type:
31084 case DW_OP_GNU_const_type:
31085 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31086 || valx2->val_class != valy2->val_class)
31087 return false;
31088 switch (valx2->val_class)
31090 case dw_val_class_const:
31091 return valx2->v.val_int == valy2->v.val_int;
31092 case dw_val_class_vec:
31093 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31094 && valx2->v.val_vec.length == valy2->v.val_vec.length
31095 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31096 valx2->v.val_vec.elt_size
31097 * valx2->v.val_vec.length) == 0;
31098 case dw_val_class_const_double:
31099 return valx2->v.val_double.low == valy2->v.val_double.low
31100 && valx2->v.val_double.high == valy2->v.val_double.high;
31101 case dw_val_class_wide_int:
31102 return *valx2->v.val_wide == *valy2->v.val_wide;
31103 default:
31104 gcc_unreachable ();
31106 case DW_OP_regval_type:
31107 case DW_OP_deref_type:
31108 case DW_OP_GNU_regval_type:
31109 case DW_OP_GNU_deref_type:
31110 return valx1->v.val_int == valy1->v.val_int
31111 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31112 case DW_OP_convert:
31113 case DW_OP_reinterpret:
31114 case DW_OP_GNU_convert:
31115 case DW_OP_GNU_reinterpret:
31116 if (valx1->val_class != valy1->val_class)
31117 return false;
31118 if (valx1->val_class == dw_val_class_unsigned_const)
31119 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31120 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31121 case DW_OP_GNU_parameter_ref:
31122 return valx1->val_class == dw_val_class_die_ref
31123 && valx1->val_class == valy1->val_class
31124 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31125 default:
31126 /* Other codes have no operands. */
31127 return true;
31131 /* Return true if DWARF location expressions X and Y are the same. */
31133 static inline bool
31134 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31136 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31137 if (x->dw_loc_opc != y->dw_loc_opc
31138 || x->dtprel != y->dtprel
31139 || !compare_loc_operands (x, y))
31140 break;
31141 return x == NULL && y == NULL;
31144 /* Hashtable helpers. */
31146 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31148 static inline hashval_t hash (const dw_loc_list_struct *);
31149 static inline bool equal (const dw_loc_list_struct *,
31150 const dw_loc_list_struct *);
31153 /* Return precomputed hash of location list X. */
31155 inline hashval_t
31156 loc_list_hasher::hash (const dw_loc_list_struct *x)
31158 return x->hash;
31161 /* Return true if location lists A and B are the same. */
31163 inline bool
31164 loc_list_hasher::equal (const dw_loc_list_struct *a,
31165 const dw_loc_list_struct *b)
31167 if (a == b)
31168 return 1;
31169 if (a->hash != b->hash)
31170 return 0;
31171 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31172 if (strcmp (a->begin, b->begin) != 0
31173 || strcmp (a->end, b->end) != 0
31174 || (a->section == NULL) != (b->section == NULL)
31175 || (a->section && strcmp (a->section, b->section) != 0)
31176 || a->vbegin != b->vbegin || a->vend != b->vend
31177 || !compare_locs (a->expr, b->expr))
31178 break;
31179 return a == NULL && b == NULL;
31182 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31185 /* Recursively optimize location lists referenced from DIE
31186 children and share them whenever possible. */
31188 static void
31189 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31191 dw_die_ref c;
31192 dw_attr_node *a;
31193 unsigned ix;
31194 dw_loc_list_struct **slot;
31195 bool drop_locviews = false;
31196 bool has_locviews = false;
31198 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31199 if (AT_class (a) == dw_val_class_loc_list)
31201 dw_loc_list_ref list = AT_loc_list (a);
31202 /* TODO: perform some optimizations here, before hashing
31203 it and storing into the hash table. */
31204 hash_loc_list (list);
31205 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31206 if (*slot == NULL)
31208 *slot = list;
31209 if (loc_list_has_views (list))
31210 gcc_assert (list->vl_symbol);
31211 else if (list->vl_symbol)
31213 drop_locviews = true;
31214 list->vl_symbol = NULL;
31217 else
31219 if (list->vl_symbol && !(*slot)->vl_symbol)
31220 drop_locviews = true;
31221 a->dw_attr_val.v.val_loc_list = *slot;
31224 else if (AT_class (a) == dw_val_class_view_list)
31226 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31227 has_locviews = true;
31231 if (drop_locviews && has_locviews)
31232 remove_AT (die, DW_AT_GNU_locviews);
31234 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31238 /* Recursively assign each location list a unique index into the debug_addr
31239 section. */
31241 static void
31242 index_location_lists (dw_die_ref die)
31244 dw_die_ref c;
31245 dw_attr_node *a;
31246 unsigned ix;
31248 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31249 if (AT_class (a) == dw_val_class_loc_list)
31251 dw_loc_list_ref list = AT_loc_list (a);
31252 dw_loc_list_ref curr;
31253 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31255 /* Don't index an entry that has already been indexed
31256 or won't be output. Make sure skip_loc_list_entry doesn't
31257 call size_of_locs, because that might cause circular dependency,
31258 index_location_lists requiring address table indexes to be
31259 computed, but adding new indexes through add_addr_table_entry
31260 and address table index computation requiring no new additions
31261 to the hash table. In the rare case of DWARF[234] >= 64KB
31262 location expression, we'll just waste unused address table entry
31263 for it. */
31264 if (curr->begin_entry != NULL
31265 || skip_loc_list_entry (curr))
31266 continue;
31268 curr->begin_entry
31269 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31273 FOR_EACH_CHILD (die, c, index_location_lists (c));
31276 /* Optimize location lists referenced from DIE
31277 children and share them whenever possible. */
31279 static void
31280 optimize_location_lists (dw_die_ref die)
31282 loc_list_hash_type htab (500);
31283 optimize_location_lists_1 (die, &htab);
31286 /* Traverse the limbo die list, and add parent/child links. The only
31287 dies without parents that should be here are concrete instances of
31288 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
31289 For concrete instances, we can get the parent die from the abstract
31290 instance. */
31292 static void
31293 flush_limbo_die_list (void)
31295 limbo_die_node *node;
31297 /* get_context_die calls force_decl_die, which can put new DIEs on the
31298 limbo list in LTO mode when nested functions are put in a different
31299 partition than that of their parent function. */
31300 while ((node = limbo_die_list))
31302 dw_die_ref die = node->die;
31303 limbo_die_list = node->next;
31305 if (die->die_parent == NULL)
31307 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31309 if (origin && origin->die_parent)
31310 add_child_die (origin->die_parent, die);
31311 else if (is_cu_die (die))
31313 else if (seen_error ())
31314 /* It's OK to be confused by errors in the input. */
31315 add_child_die (comp_unit_die (), die);
31316 else
31318 /* In certain situations, the lexical block containing a
31319 nested function can be optimized away, which results
31320 in the nested function die being orphaned. Likewise
31321 with the return type of that nested function. Force
31322 this to be a child of the containing function.
31324 It may happen that even the containing function got fully
31325 inlined and optimized out. In that case we are lost and
31326 assign the empty child. This should not be big issue as
31327 the function is likely unreachable too. */
31328 gcc_assert (node->created_for);
31330 if (DECL_P (node->created_for))
31331 origin = get_context_die (DECL_CONTEXT (node->created_for));
31332 else if (TYPE_P (node->created_for))
31333 origin = scope_die_for (node->created_for, comp_unit_die ());
31334 else
31335 origin = comp_unit_die ();
31337 add_child_die (origin, die);
31343 /* Reset DIEs so we can output them again. */
31345 static void
31346 reset_dies (dw_die_ref die)
31348 dw_die_ref c;
31350 /* Remove stuff we re-generate. */
31351 die->die_mark = 0;
31352 die->die_offset = 0;
31353 die->die_abbrev = 0;
31354 remove_AT (die, DW_AT_sibling);
31356 FOR_EACH_CHILD (die, c, reset_dies (c));
31359 /* Output stuff that dwarf requires at the end of every file,
31360 and generate the DWARF-2 debugging info. */
31362 static void
31363 dwarf2out_finish (const char *filename)
31365 comdat_type_node *ctnode;
31366 dw_die_ref main_comp_unit_die;
31367 unsigned char checksum[16];
31368 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31370 /* Flush out any latecomers to the limbo party. */
31371 flush_limbo_die_list ();
31373 if (inline_entry_data_table)
31374 gcc_assert (inline_entry_data_table->is_empty ());
31376 if (flag_checking)
31378 verify_die (comp_unit_die ());
31379 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31380 verify_die (node->die);
31383 /* We shouldn't have any symbols with delayed asm names for
31384 DIEs generated after early finish. */
31385 gcc_assert (deferred_asm_name == NULL);
31387 gen_remaining_tmpl_value_param_die_attribute ();
31389 if (flag_generate_lto || flag_generate_offload)
31391 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31393 /* Prune stuff so that dwarf2out_finish runs successfully
31394 for the fat part of the object. */
31395 reset_dies (comp_unit_die ());
31396 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31397 reset_dies (node->die);
31399 hash_table<comdat_type_hasher> comdat_type_table (100);
31400 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31402 comdat_type_node **slot
31403 = comdat_type_table.find_slot (ctnode, INSERT);
31405 /* Don't reset types twice. */
31406 if (*slot != HTAB_EMPTY_ENTRY)
31407 continue;
31409 /* Remove the pointer to the line table. */
31410 remove_AT (ctnode->root_die, DW_AT_stmt_list);
31412 if (debug_info_level >= DINFO_LEVEL_TERSE)
31413 reset_dies (ctnode->root_die);
31415 *slot = ctnode;
31418 /* Reset die CU symbol so we don't output it twice. */
31419 comp_unit_die ()->die_id.die_symbol = NULL;
31421 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
31422 remove_AT (comp_unit_die (), DW_AT_stmt_list);
31423 if (have_macinfo)
31424 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
31426 /* Remove indirect string decisions. */
31427 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
31428 if (debug_line_str_hash)
31430 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
31431 debug_line_str_hash = NULL;
31435 #if ENABLE_ASSERT_CHECKING
31437 dw_die_ref die = comp_unit_die (), c;
31438 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
31440 #endif
31441 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31442 resolve_addr (ctnode->root_die);
31443 resolve_addr (comp_unit_die ());
31444 move_marked_base_types ();
31446 if (dump_file)
31448 fprintf (dump_file, "DWARF for %s\n", filename);
31449 print_die (comp_unit_die (), dump_file);
31452 /* Initialize sections and labels used for actual assembler output. */
31453 unsigned generation = init_sections_and_labels (false);
31455 /* Traverse the DIE's and add sibling attributes to those DIE's that
31456 have children. */
31457 add_sibling_attributes (comp_unit_die ());
31458 limbo_die_node *node;
31459 for (node = cu_die_list; node; node = node->next)
31460 add_sibling_attributes (node->die);
31461 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31462 add_sibling_attributes (ctnode->root_die);
31464 /* When splitting DWARF info, we put some attributes in the
31465 skeleton compile_unit DIE that remains in the .o, while
31466 most attributes go in the DWO compile_unit_die. */
31467 if (dwarf_split_debug_info)
31469 limbo_die_node *cu;
31470 main_comp_unit_die = gen_compile_unit_die (NULL);
31471 if (dwarf_version >= 5)
31472 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
31473 cu = limbo_die_list;
31474 gcc_assert (cu->die == main_comp_unit_die);
31475 limbo_die_list = limbo_die_list->next;
31476 cu->next = cu_die_list;
31477 cu_die_list = cu;
31479 else
31480 main_comp_unit_die = comp_unit_die ();
31482 /* Output a terminator label for the .text section. */
31483 switch_to_section (text_section);
31484 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
31485 if (cold_text_section)
31487 switch_to_section (cold_text_section);
31488 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
31491 /* We can only use the low/high_pc attributes if all of the code was
31492 in .text. */
31493 if (!have_multiple_function_sections
31494 || (dwarf_version < 3 && dwarf_strict))
31496 /* Don't add if the CU has no associated code. */
31497 if (text_section_used)
31498 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
31499 text_end_label, true);
31501 else
31503 unsigned fde_idx;
31504 dw_fde_ref fde;
31505 bool range_list_added = false;
31507 if (text_section_used)
31508 add_ranges_by_labels (main_comp_unit_die, text_section_label,
31509 text_end_label, &range_list_added, true);
31510 if (cold_text_section_used)
31511 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
31512 cold_end_label, &range_list_added, true);
31514 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
31516 if (DECL_IGNORED_P (fde->decl))
31517 continue;
31518 if (!fde->in_std_section)
31519 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
31520 fde->dw_fde_end, &range_list_added,
31521 true);
31522 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
31523 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
31524 fde->dw_fde_second_end, &range_list_added,
31525 true);
31528 if (range_list_added)
31530 /* We need to give .debug_loc and .debug_ranges an appropriate
31531 "base address". Use zero so that these addresses become
31532 absolute. Historically, we've emitted the unexpected
31533 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
31534 Emit both to give time for other tools to adapt. */
31535 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
31536 if (! dwarf_strict && dwarf_version < 4)
31537 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
31539 add_ranges (NULL);
31543 /* AIX Assembler inserts the length, so adjust the reference to match the
31544 offset expected by debuggers. */
31545 strcpy (dl_section_ref, debug_line_section_label);
31546 if (XCOFF_DEBUGGING_INFO)
31547 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
31549 if (debug_info_level >= DINFO_LEVEL_TERSE)
31550 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
31551 dl_section_ref);
31553 if (have_macinfo)
31554 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31555 macinfo_section_label);
31557 if (dwarf_split_debug_info)
31559 if (have_location_lists)
31561 /* Since we generate the loclists in the split DWARF .dwo
31562 file itself, we don't need to generate a loclists_base
31563 attribute for the split compile unit DIE. That attribute
31564 (and using relocatable sec_offset FORMs) isn't allowed
31565 for a split compile unit. Only if the .debug_loclists
31566 section was in the main file, would we need to generate a
31567 loclists_base attribute here (for the full or skeleton
31568 unit DIE). */
31570 /* optimize_location_lists calculates the size of the lists,
31571 so index them first, and assign indices to the entries.
31572 Although optimize_location_lists will remove entries from
31573 the table, it only does so for duplicates, and therefore
31574 only reduces ref_counts to 1. */
31575 index_location_lists (comp_unit_die ());
31578 if (addr_index_table != NULL)
31580 unsigned int index = 0;
31581 addr_index_table
31582 ->traverse_noresize<unsigned int *, index_addr_table_entry>
31583 (&index);
31587 loc_list_idx = 0;
31588 if (have_location_lists)
31590 optimize_location_lists (comp_unit_die ());
31591 /* And finally assign indexes to the entries for -gsplit-dwarf. */
31592 if (dwarf_version >= 5 && dwarf_split_debug_info)
31593 assign_location_list_indexes (comp_unit_die ());
31596 save_macinfo_strings ();
31598 if (dwarf_split_debug_info)
31600 unsigned int index = 0;
31602 /* Add attributes common to skeleton compile_units and
31603 type_units. Because these attributes include strings, it
31604 must be done before freezing the string table. Top-level
31605 skeleton die attrs are added when the skeleton type unit is
31606 created, so ensure it is created by this point. */
31607 add_top_level_skeleton_die_attrs (main_comp_unit_die);
31608 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31611 /* Output all of the compilation units. We put the main one last so that
31612 the offsets are available to output_pubnames. */
31613 for (node = cu_die_list; node; node = node->next)
31614 output_comp_unit (node->die, 0, NULL);
31616 hash_table<comdat_type_hasher> comdat_type_table (100);
31617 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31619 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31621 /* Don't output duplicate types. */
31622 if (*slot != HTAB_EMPTY_ENTRY)
31623 continue;
31625 /* Add a pointer to the line table for the main compilation unit
31626 so that the debugger can make sense of DW_AT_decl_file
31627 attributes. */
31628 if (debug_info_level >= DINFO_LEVEL_TERSE)
31629 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31630 (!dwarf_split_debug_info
31631 ? dl_section_ref
31632 : debug_skeleton_line_section_label));
31634 output_comdat_type_unit (ctnode, false);
31635 *slot = ctnode;
31638 if (dwarf_split_debug_info)
31640 int mark;
31641 struct md5_ctx ctx;
31643 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
31644 index_rnglists ();
31646 /* Compute a checksum of the comp_unit to use as the dwo_id. */
31647 md5_init_ctx (&ctx);
31648 mark = 0;
31649 die_checksum (comp_unit_die (), &ctx, &mark);
31650 unmark_all_dies (comp_unit_die ());
31651 md5_finish_ctx (&ctx, checksum);
31653 if (dwarf_version < 5)
31655 /* Use the first 8 bytes of the checksum as the dwo_id,
31656 and add it to both comp-unit DIEs. */
31657 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
31658 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
31661 /* Add the base offset of the ranges table to the skeleton
31662 comp-unit DIE. */
31663 if (!vec_safe_is_empty (ranges_table))
31665 if (dwarf_version >= 5)
31666 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
31667 ranges_base_label);
31668 else
31669 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
31670 ranges_section_label);
31673 output_addr_table ();
31676 /* Output the main compilation unit if non-empty or if .debug_macinfo
31677 or .debug_macro will be emitted. */
31678 output_comp_unit (comp_unit_die (), have_macinfo,
31679 dwarf_split_debug_info ? checksum : NULL);
31681 if (dwarf_split_debug_info && info_section_emitted)
31682 output_skeleton_debug_sections (main_comp_unit_die, checksum);
31684 /* Output the abbreviation table. */
31685 if (vec_safe_length (abbrev_die_table) != 1)
31687 switch_to_section (debug_abbrev_section);
31688 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31689 output_abbrev_section ();
31692 /* Output location list section if necessary. */
31693 if (have_location_lists)
31695 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
31696 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
31697 /* Output the location lists info. */
31698 switch_to_section (debug_loc_section);
31699 if (dwarf_version >= 5)
31701 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
31702 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
31703 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31704 dw2_asm_output_data (4, 0xffffffff,
31705 "Initial length escape value indicating "
31706 "64-bit DWARF extension");
31707 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
31708 "Length of Location Lists");
31709 ASM_OUTPUT_LABEL (asm_out_file, l1);
31710 output_dwarf_version ();
31711 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
31712 dw2_asm_output_data (1, 0, "Segment Size");
31713 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
31714 "Offset Entry Count");
31716 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
31717 if (dwarf_version >= 5 && dwarf_split_debug_info)
31719 unsigned int save_loc_list_idx = loc_list_idx;
31720 loc_list_idx = 0;
31721 output_loclists_offsets (comp_unit_die ());
31722 gcc_assert (save_loc_list_idx == loc_list_idx);
31724 output_location_lists (comp_unit_die ());
31725 if (dwarf_version >= 5)
31726 ASM_OUTPUT_LABEL (asm_out_file, l2);
31729 output_pubtables ();
31731 /* Output the address range information if a CU (.debug_info section)
31732 was emitted. We output an empty table even if we had no functions
31733 to put in it. This because the consumer has no way to tell the
31734 difference between an empty table that we omitted and failure to
31735 generate a table that would have contained data. */
31736 if (info_section_emitted)
31738 switch_to_section (debug_aranges_section);
31739 output_aranges ();
31742 /* Output ranges section if necessary. */
31743 if (!vec_safe_is_empty (ranges_table))
31745 if (dwarf_version >= 5)
31746 output_rnglists (generation);
31747 else
31748 output_ranges ();
31751 /* Have to end the macro section. */
31752 if (have_macinfo)
31754 switch_to_section (debug_macinfo_section);
31755 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31756 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
31757 : debug_skeleton_line_section_label, false);
31758 dw2_asm_output_data (1, 0, "End compilation unit");
31761 /* Output the source line correspondence table. We must do this
31762 even if there is no line information. Otherwise, on an empty
31763 translation unit, we will generate a present, but empty,
31764 .debug_info section. IRIX 6.5 `nm' will then complain when
31765 examining the file. This is done late so that any filenames
31766 used by the debug_info section are marked as 'used'. */
31767 switch_to_section (debug_line_section);
31768 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
31769 if (! output_asm_line_debug_info ())
31770 output_line_info (false);
31771 else if (asm_outputs_debug_line_str ())
31773 /* When gas outputs DWARF5 .debug_line[_str] then we have to
31774 tell it the comp_dir and main file name for the zero entry
31775 line table. */
31776 const char *comp_dir, *filename0;
31778 comp_dir = comp_dir_string ();
31779 if (comp_dir == NULL)
31780 comp_dir = "";
31782 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
31783 if (filename0 == NULL)
31784 filename0 = "";
31786 fprintf (asm_out_file, "\t.file 0 ");
31787 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
31788 fputc (' ', asm_out_file);
31789 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
31790 fputc ('\n', asm_out_file);
31793 if (dwarf_split_debug_info && info_section_emitted)
31795 switch_to_section (debug_skeleton_line_section);
31796 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31797 output_line_info (true);
31800 /* If we emitted any indirect strings, output the string table too. */
31801 if (debug_str_hash || skeleton_debug_str_hash)
31802 output_indirect_strings ();
31803 if (debug_line_str_hash)
31805 switch_to_section (debug_line_str_section);
31806 const enum dwarf_form form = DW_FORM_line_strp;
31807 debug_line_str_hash->traverse<enum dwarf_form,
31808 output_indirect_string> (form);
31811 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
31812 symview_upper_bound = 0;
31813 if (zero_view_p)
31814 bitmap_clear (zero_view_p);
31817 /* Returns a hash value for X (which really is a variable_value_struct). */
31819 inline hashval_t
31820 variable_value_hasher::hash (variable_value_struct *x)
31822 return (hashval_t) x->decl_id;
31825 /* Return nonzero if decl_id of variable_value_struct X is the same as
31826 UID of decl Y. */
31828 inline bool
31829 variable_value_hasher::equal (variable_value_struct *x, tree y)
31831 return x->decl_id == DECL_UID (y);
31834 /* Helper function for resolve_variable_value, handle
31835 DW_OP_GNU_variable_value in one location expression.
31836 Return true if exprloc has been changed into loclist. */
31838 static bool
31839 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31841 dw_loc_descr_ref next;
31842 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
31844 next = loc->dw_loc_next;
31845 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
31846 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
31847 continue;
31849 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31850 if (DECL_CONTEXT (decl) != current_function_decl)
31851 continue;
31853 dw_die_ref ref = lookup_decl_die (decl);
31854 if (ref)
31856 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31857 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31858 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31859 continue;
31861 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
31862 if (l == NULL)
31863 continue;
31864 if (l->dw_loc_next)
31866 if (AT_class (a) != dw_val_class_loc)
31867 continue;
31868 switch (a->dw_attr)
31870 /* Following attributes allow both exprloc and loclist
31871 classes, so we can change them into a loclist. */
31872 case DW_AT_location:
31873 case DW_AT_string_length:
31874 case DW_AT_return_addr:
31875 case DW_AT_data_member_location:
31876 case DW_AT_frame_base:
31877 case DW_AT_segment:
31878 case DW_AT_static_link:
31879 case DW_AT_use_location:
31880 case DW_AT_vtable_elem_location:
31881 if (prev)
31883 prev->dw_loc_next = NULL;
31884 prepend_loc_descr_to_each (l, AT_loc (a));
31886 if (next)
31887 add_loc_descr_to_each (l, next);
31888 a->dw_attr_val.val_class = dw_val_class_loc_list;
31889 a->dw_attr_val.val_entry = NULL;
31890 a->dw_attr_val.v.val_loc_list = l;
31891 have_location_lists = true;
31892 return true;
31893 /* Following attributes allow both exprloc and reference,
31894 so if the whole expression is DW_OP_GNU_variable_value alone
31895 we could transform it into reference. */
31896 case DW_AT_byte_size:
31897 case DW_AT_bit_size:
31898 case DW_AT_lower_bound:
31899 case DW_AT_upper_bound:
31900 case DW_AT_bit_stride:
31901 case DW_AT_count:
31902 case DW_AT_allocated:
31903 case DW_AT_associated:
31904 case DW_AT_byte_stride:
31905 if (prev == NULL && next == NULL)
31906 break;
31907 /* FALLTHRU */
31908 default:
31909 if (dwarf_strict)
31910 continue;
31911 break;
31913 /* Create DW_TAG_variable that we can refer to. */
31914 gen_decl_die (decl, NULL_TREE, NULL,
31915 lookup_decl_die (current_function_decl));
31916 ref = lookup_decl_die (decl);
31917 if (ref)
31919 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31920 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31921 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31923 continue;
31925 if (prev)
31927 prev->dw_loc_next = l->expr;
31928 add_loc_descr (&prev->dw_loc_next, next);
31929 free_loc_descr (loc, NULL);
31930 next = prev->dw_loc_next;
31932 else
31934 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
31935 add_loc_descr (&loc, next);
31936 next = loc;
31938 loc = prev;
31940 return false;
31943 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
31945 static void
31946 resolve_variable_value (dw_die_ref die)
31948 dw_attr_node *a;
31949 dw_loc_list_ref loc;
31950 unsigned ix;
31952 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31953 switch (AT_class (a))
31955 case dw_val_class_loc:
31956 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
31957 break;
31958 /* FALLTHRU */
31959 case dw_val_class_loc_list:
31960 loc = AT_loc_list (a);
31961 gcc_assert (loc);
31962 for (; loc; loc = loc->dw_loc_next)
31963 resolve_variable_value_in_expr (a, loc->expr);
31964 break;
31965 default:
31966 break;
31970 /* Attempt to optimize DW_OP_GNU_variable_value refering to
31971 temporaries in the current function. */
31973 static void
31974 resolve_variable_values (void)
31976 if (!variable_value_hash || !current_function_decl)
31977 return;
31979 struct variable_value_struct *node
31980 = variable_value_hash->find_with_hash (current_function_decl,
31981 DECL_UID (current_function_decl));
31983 if (node == NULL)
31984 return;
31986 unsigned int i;
31987 dw_die_ref die;
31988 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
31989 resolve_variable_value (die);
31992 /* Helper function for note_variable_value, handle one location
31993 expression. */
31995 static void
31996 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
31998 for (; loc; loc = loc->dw_loc_next)
31999 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32000 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32002 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32003 dw_die_ref ref = lookup_decl_die (decl);
32004 if (! ref && (flag_generate_lto || flag_generate_offload))
32006 /* ??? This is somewhat a hack because we do not create DIEs
32007 for variables not in BLOCK trees early but when generating
32008 early LTO output we need the dw_val_class_decl_ref to be
32009 fully resolved. For fat LTO objects we'd also like to
32010 undo this after LTO dwarf output. */
32011 gcc_assert (DECL_CONTEXT (decl));
32012 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32013 gcc_assert (ctx != NULL);
32014 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32015 ref = lookup_decl_die (decl);
32016 gcc_assert (ref != NULL);
32018 if (ref)
32020 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32021 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32022 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32023 continue;
32025 if (VAR_P (decl)
32026 && DECL_CONTEXT (decl)
32027 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32028 && lookup_decl_die (DECL_CONTEXT (decl)))
32030 if (!variable_value_hash)
32031 variable_value_hash
32032 = hash_table<variable_value_hasher>::create_ggc (10);
32034 tree fndecl = DECL_CONTEXT (decl);
32035 struct variable_value_struct *node;
32036 struct variable_value_struct **slot
32037 = variable_value_hash->find_slot_with_hash (fndecl,
32038 DECL_UID (fndecl),
32039 INSERT);
32040 if (*slot == NULL)
32042 node = ggc_cleared_alloc<variable_value_struct> ();
32043 node->decl_id = DECL_UID (fndecl);
32044 *slot = node;
32046 else
32047 node = *slot;
32049 vec_safe_push (node->dies, die);
32054 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32055 with dw_val_class_decl_ref operand. */
32057 static void
32058 note_variable_value (dw_die_ref die)
32060 dw_die_ref c;
32061 dw_attr_node *a;
32062 dw_loc_list_ref loc;
32063 unsigned ix;
32065 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32066 switch (AT_class (a))
32068 case dw_val_class_loc_list:
32069 loc = AT_loc_list (a);
32070 gcc_assert (loc);
32071 if (!loc->noted_variable_value)
32073 loc->noted_variable_value = 1;
32074 for (; loc; loc = loc->dw_loc_next)
32075 note_variable_value_in_expr (die, loc->expr);
32077 break;
32078 case dw_val_class_loc:
32079 note_variable_value_in_expr (die, AT_loc (a));
32080 break;
32081 default:
32082 break;
32085 /* Mark children. */
32086 FOR_EACH_CHILD (die, c, note_variable_value (c));
32089 /* Perform any cleanups needed after the early debug generation pass
32090 has run. */
32092 static void
32093 dwarf2out_early_finish (const char *filename)
32095 set_early_dwarf s;
32096 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32098 /* PCH might result in DW_AT_producer string being restored from the
32099 header compilation, so always fill it with empty string initially
32100 and overwrite only here. */
32101 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32102 producer_string = gen_producer_string ();
32103 producer->dw_attr_val.v.val_str->refcount--;
32104 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32106 /* Add the name for the main input file now. We delayed this from
32107 dwarf2out_init to avoid complications with PCH. */
32108 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32109 add_comp_dir_attribute (comp_unit_die ());
32111 /* With LTO early dwarf was really finished at compile-time, so make
32112 sure to adjust the phase after annotating the LTRANS CU DIE. */
32113 if (in_lto_p)
32115 early_dwarf_finished = true;
32116 if (dump_file)
32118 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32119 print_die (comp_unit_die (), dump_file);
32121 return;
32124 /* Walk through the list of incomplete types again, trying once more to
32125 emit full debugging info for them. */
32126 retry_incomplete_types ();
32128 /* The point here is to flush out the limbo list so that it is empty
32129 and we don't need to stream it for LTO. */
32130 flush_limbo_die_list ();
32132 gen_scheduled_generic_parms_dies ();
32133 gen_remaining_tmpl_value_param_die_attribute ();
32135 /* Add DW_AT_linkage_name for all deferred DIEs. */
32136 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32138 tree decl = node->created_for;
32139 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32140 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32141 ended up in deferred_asm_name before we knew it was
32142 constant and never written to disk. */
32143 && DECL_ASSEMBLER_NAME (decl))
32145 add_linkage_attr (node->die, decl);
32146 move_linkage_attr (node->die);
32149 deferred_asm_name = NULL;
32151 if (flag_eliminate_unused_debug_types)
32152 prune_unused_types ();
32154 /* Generate separate COMDAT sections for type DIEs. */
32155 if (use_debug_types)
32157 break_out_comdat_types (comp_unit_die ());
32159 /* Each new type_unit DIE was added to the limbo die list when created.
32160 Since these have all been added to comdat_type_list, clear the
32161 limbo die list. */
32162 limbo_die_list = NULL;
32164 /* For each new comdat type unit, copy declarations for incomplete
32165 types to make the new unit self-contained (i.e., no direct
32166 references to the main compile unit). */
32167 for (comdat_type_node *ctnode = comdat_type_list;
32168 ctnode != NULL; ctnode = ctnode->next)
32169 copy_decls_for_unworthy_types (ctnode->root_die);
32170 copy_decls_for_unworthy_types (comp_unit_die ());
32172 /* In the process of copying declarations from one unit to another,
32173 we may have left some declarations behind that are no longer
32174 referenced. Prune them. */
32175 prune_unused_types ();
32178 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
32179 with dw_val_class_decl_ref operand. */
32180 note_variable_value (comp_unit_die ());
32181 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32182 note_variable_value (node->die);
32183 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
32184 ctnode = ctnode->next)
32185 note_variable_value (ctnode->root_die);
32186 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32187 note_variable_value (node->die);
32189 /* The AT_pubnames attribute needs to go in all skeleton dies, including
32190 both the main_cu and all skeleton TUs. Making this call unconditional
32191 would end up either adding a second copy of the AT_pubnames attribute, or
32192 requiring a special case in add_top_level_skeleton_die_attrs. */
32193 if (!dwarf_split_debug_info)
32194 add_AT_pubnames (comp_unit_die ());
32196 /* The early debug phase is now finished. */
32197 early_dwarf_finished = true;
32198 if (dump_file)
32200 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
32201 print_die (comp_unit_die (), dump_file);
32204 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
32205 if ((!flag_generate_lto && !flag_generate_offload)
32206 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
32207 copy_lto_debug_sections operation of the simple object support in
32208 libiberty is not implemented for them yet. */
32209 || TARGET_PECOFF || TARGET_COFF)
32210 return;
32212 /* Now as we are going to output for LTO initialize sections and labels
32213 to the LTO variants. We don't need a random-seed postfix as other
32214 LTO sections as linking the LTO debug sections into one in a partial
32215 link is fine. */
32216 init_sections_and_labels (true);
32218 /* The output below is modeled after dwarf2out_finish with all
32219 location related output removed and some LTO specific changes.
32220 Some refactoring might make both smaller and easier to match up. */
32222 /* Traverse the DIE's and add sibling attributes to those DIE's
32223 that have children. */
32224 add_sibling_attributes (comp_unit_die ());
32225 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32226 add_sibling_attributes (node->die);
32227 for (comdat_type_node *ctnode = comdat_type_list;
32228 ctnode != NULL; ctnode = ctnode->next)
32229 add_sibling_attributes (ctnode->root_die);
32231 /* AIX Assembler inserts the length, so adjust the reference to match the
32232 offset expected by debuggers. */
32233 strcpy (dl_section_ref, debug_line_section_label);
32234 if (XCOFF_DEBUGGING_INFO)
32235 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32237 if (debug_info_level >= DINFO_LEVEL_TERSE)
32238 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32240 if (have_macinfo)
32241 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32242 macinfo_section_label);
32244 save_macinfo_strings ();
32246 if (dwarf_split_debug_info)
32248 unsigned int index = 0;
32249 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32252 /* Output all of the compilation units. We put the main one last so that
32253 the offsets are available to output_pubnames. */
32254 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32255 output_comp_unit (node->die, 0, NULL);
32257 hash_table<comdat_type_hasher> comdat_type_table (100);
32258 for (comdat_type_node *ctnode = comdat_type_list;
32259 ctnode != NULL; ctnode = ctnode->next)
32261 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32263 /* Don't output duplicate types. */
32264 if (*slot != HTAB_EMPTY_ENTRY)
32265 continue;
32267 /* Add a pointer to the line table for the main compilation unit
32268 so that the debugger can make sense of DW_AT_decl_file
32269 attributes. */
32270 if (debug_info_level >= DINFO_LEVEL_TERSE)
32271 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32272 (!dwarf_split_debug_info
32273 ? debug_line_section_label
32274 : debug_skeleton_line_section_label));
32276 output_comdat_type_unit (ctnode, true);
32277 *slot = ctnode;
32280 /* Stick a unique symbol to the main debuginfo section. */
32281 compute_comp_unit_symbol (comp_unit_die ());
32283 /* Output the main compilation unit. We always need it if only for
32284 the CU symbol. */
32285 output_comp_unit (comp_unit_die (), true, NULL);
32287 /* Output the abbreviation table. */
32288 if (vec_safe_length (abbrev_die_table) != 1)
32290 switch_to_section (debug_abbrev_section);
32291 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32292 output_abbrev_section ();
32295 /* Have to end the macro section. */
32296 if (have_macinfo)
32298 /* We have to save macinfo state if we need to output it again
32299 for the FAT part of the object. */
32300 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32301 if (flag_fat_lto_objects)
32302 macinfo_table = macinfo_table->copy ();
32304 switch_to_section (debug_macinfo_section);
32305 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32306 output_macinfo (debug_line_section_label, true);
32307 dw2_asm_output_data (1, 0, "End compilation unit");
32309 if (flag_fat_lto_objects)
32311 vec_free (macinfo_table);
32312 macinfo_table = saved_macinfo_table;
32316 /* Emit a skeleton debug_line section. */
32317 switch_to_section (debug_line_section);
32318 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32319 output_line_info (true);
32321 /* If we emitted any indirect strings, output the string table too. */
32322 if (debug_str_hash || skeleton_debug_str_hash)
32323 output_indirect_strings ();
32324 if (debug_line_str_hash)
32326 switch_to_section (debug_line_str_section);
32327 const enum dwarf_form form = DW_FORM_line_strp;
32328 debug_line_str_hash->traverse<enum dwarf_form,
32329 output_indirect_string> (form);
32332 /* Switch back to the text section. */
32333 switch_to_section (text_section);
32336 /* Reset all state within dwarf2out.c so that we can rerun the compiler
32337 within the same process. For use by toplev::finalize. */
32339 void
32340 dwarf2out_c_finalize (void)
32342 last_var_location_insn = NULL;
32343 cached_next_real_insn = NULL;
32344 used_rtx_array = NULL;
32345 incomplete_types = NULL;
32346 debug_info_section = NULL;
32347 debug_skeleton_info_section = NULL;
32348 debug_abbrev_section = NULL;
32349 debug_skeleton_abbrev_section = NULL;
32350 debug_aranges_section = NULL;
32351 debug_addr_section = NULL;
32352 debug_macinfo_section = NULL;
32353 debug_line_section = NULL;
32354 debug_skeleton_line_section = NULL;
32355 debug_loc_section = NULL;
32356 debug_pubnames_section = NULL;
32357 debug_pubtypes_section = NULL;
32358 debug_str_section = NULL;
32359 debug_line_str_section = NULL;
32360 debug_str_dwo_section = NULL;
32361 debug_str_offsets_section = NULL;
32362 debug_ranges_section = NULL;
32363 debug_frame_section = NULL;
32364 fde_vec = NULL;
32365 debug_str_hash = NULL;
32366 debug_line_str_hash = NULL;
32367 skeleton_debug_str_hash = NULL;
32368 dw2_string_counter = 0;
32369 have_multiple_function_sections = false;
32370 text_section_used = false;
32371 cold_text_section_used = false;
32372 cold_text_section = NULL;
32373 current_unit_personality = NULL;
32375 early_dwarf = false;
32376 early_dwarf_finished = false;
32378 next_die_offset = 0;
32379 single_comp_unit_die = NULL;
32380 comdat_type_list = NULL;
32381 limbo_die_list = NULL;
32382 file_table = NULL;
32383 decl_die_table = NULL;
32384 common_block_die_table = NULL;
32385 decl_loc_table = NULL;
32386 call_arg_locations = NULL;
32387 call_arg_loc_last = NULL;
32388 call_site_count = -1;
32389 tail_call_site_count = -1;
32390 cached_dw_loc_list_table = NULL;
32391 abbrev_die_table = NULL;
32392 delete dwarf_proc_stack_usage_map;
32393 dwarf_proc_stack_usage_map = NULL;
32394 line_info_label_num = 0;
32395 cur_line_info_table = NULL;
32396 text_section_line_info = NULL;
32397 cold_text_section_line_info = NULL;
32398 separate_line_info = NULL;
32399 info_section_emitted = false;
32400 pubname_table = NULL;
32401 pubtype_table = NULL;
32402 macinfo_table = NULL;
32403 ranges_table = NULL;
32404 ranges_by_label = NULL;
32405 rnglist_idx = 0;
32406 have_location_lists = false;
32407 loclabel_num = 0;
32408 poc_label_num = 0;
32409 last_emitted_file = NULL;
32410 label_num = 0;
32411 tmpl_value_parm_die_table = NULL;
32412 generic_type_instances = NULL;
32413 frame_pointer_fb_offset = 0;
32414 frame_pointer_fb_offset_valid = false;
32415 base_types.release ();
32416 XDELETEVEC (producer_string);
32417 producer_string = NULL;
32420 #include "gt-dwarf2out.h"