[PATCH] RISC-V: Move UNSPEC_SSP_SET and UNSPEC_SSP_TEST to correct enum
[gcc.git] / gcc / dwarf2out.cc
blob43884f206c07facf980495c3b5e01a37bdbf33bf
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2025 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2ctf.h"
83 #include "dwarf2codeview.h"
84 #include "dwarf2asm.h"
85 #include "toplev.h"
86 #include "md5.h"
87 #include "tree-pretty-print.h"
88 #include "print-rtl.h"
89 #include "debug.h"
90 #include "common/common-target.h"
91 #include "langhooks.h"
92 #include "lra.h"
93 #include "dumpfile.h"
94 #include "opts.h"
95 #include "tree-dfa.h"
96 #include "gdb/gdb-index.h"
97 #include "rtl-iter.h"
98 #include "stringpool.h"
99 #include "attribs.h"
100 #include "file-prefix-map.h" /* remap_debug_filename() */
102 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
103 int, bool);
104 static rtx_insn *last_var_location_insn;
105 static rtx_insn *cached_next_real_insn;
106 static void dwarf2out_decl (tree);
107 static bool is_redundant_typedef (const_tree);
109 #ifndef XCOFF_DEBUGGING_INFO
110 #define XCOFF_DEBUGGING_INFO 0
111 #endif
113 #ifndef HAVE_XCOFF_DWARF_EXTRAS
114 #define HAVE_XCOFF_DWARF_EXTRAS 0
115 #endif
117 #ifdef VMS_DEBUGGING_INFO
118 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
120 /* Define this macro to be a nonzero value if the directory specifications
121 which are output in the debug info should end with a separator. */
122 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
123 /* Define this macro to evaluate to a nonzero value if GCC should refrain
124 from generating indirect strings in DWARF2 debug information, for instance
125 if your target is stuck with an old version of GDB that is unable to
126 process them properly or uses VMS Debug. */
127 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
128 #else
129 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
130 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
131 #endif
133 /* ??? Poison these here until it can be done generically. They've been
134 totally replaced in this file; make sure it stays that way. */
135 #undef DWARF2_UNWIND_INFO
136 #undef DWARF2_FRAME_INFO
137 #if (GCC_VERSION >= 3000)
138 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
139 #endif
141 /* The size of the target's pointer type. */
142 #ifndef PTR_SIZE
143 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
144 #endif
146 /* Array of RTXes referenced by the debugging information, which therefore
147 must be kept around forever. */
148 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
150 /* A pointer to the base of a list of incomplete types which might be
151 completed at some later time. incomplete_types_list needs to be a
152 vec<tree, va_gc> *because we want to tell the garbage collector about
153 it. */
154 static GTY(()) vec<tree, va_gc> *incomplete_types;
156 /* Pointers to various DWARF2 sections. */
157 static GTY(()) section *debug_info_section;
158 static GTY(()) section *debug_skeleton_info_section;
159 static GTY(()) section *debug_abbrev_section;
160 static GTY(()) section *debug_skeleton_abbrev_section;
161 static GTY(()) section *debug_aranges_section;
162 static GTY(()) section *debug_addr_section;
163 static GTY(()) section *debug_macinfo_section;
164 static const char *debug_macinfo_section_name;
165 static unsigned macinfo_label_base = 1;
166 static GTY(()) section *debug_line_section;
167 static GTY(()) section *debug_skeleton_line_section;
168 static GTY(()) section *debug_loc_section;
169 static GTY(()) section *debug_pubnames_section;
170 static GTY(()) section *debug_pubtypes_section;
171 static GTY(()) section *debug_str_section;
172 static GTY(()) section *debug_line_str_section;
173 static GTY(()) section *debug_str_dwo_section;
174 static GTY(()) section *debug_str_offsets_section;
175 static GTY(()) section *debug_ranges_section;
176 static GTY(()) section *debug_ranges_dwo_section;
177 static GTY(()) section *debug_frame_section;
179 /* Maximum size (in bytes) of an artificially generated label. */
180 #define MAX_ARTIFICIAL_LABEL_BYTES 40
182 /* According to the (draft) DWARF 3 specification, the initial length
183 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
184 bytes are 0xffffffff, followed by the length stored in the next 8
185 bytes.
187 However, the SGI/MIPS ABI uses an initial length which is equal to
188 dwarf_offset_size. It is defined (elsewhere) accordingly. */
190 #ifndef DWARF_INITIAL_LENGTH_SIZE
191 #define DWARF_INITIAL_LENGTH_SIZE (dwarf_offset_size == 4 ? 4 : 12)
192 #endif
194 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
195 #define DWARF_INITIAL_LENGTH_SIZE_STR (dwarf_offset_size == 4 ? "-4" : "-12")
196 #endif
198 /* Round SIZE up to the nearest BOUNDARY. */
199 #define DWARF_ROUND(SIZE,BOUNDARY) \
200 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
202 /* CIE identifier. */
203 #if HOST_BITS_PER_WIDE_INT >= 64
204 #define DWARF_CIE_ID \
205 (unsigned HOST_WIDE_INT) (dwarf_offset_size == 4 ? DW_CIE_ID : DW64_CIE_ID)
206 #else
207 #define DWARF_CIE_ID DW_CIE_ID
208 #endif
211 /* A vector for a table that contains frame description
212 information for each routine. */
213 #define NOT_INDEXED (-1U)
214 #define NO_INDEX_ASSIGNED (-2U)
216 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
218 struct GTY((for_user)) indirect_string_node {
219 const char *str;
220 unsigned int refcount;
221 enum dwarf_form form;
222 char *label;
223 unsigned int index;
226 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
228 typedef const char *compare_type;
230 static hashval_t hash (indirect_string_node *);
231 static bool equal (indirect_string_node *, const char *);
234 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
236 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
238 /* With split_debug_info, both the comp_dir and dwo_name go in the
239 main object file, rather than the dwo, similar to the force_direct
240 parameter elsewhere but with additional complications:
242 1) The string is needed in both the main object file and the dwo.
243 That is, the comp_dir and dwo_name will appear in both places.
245 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
246 DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
248 3) GCC chooses the form to use late, depending on the size and
249 reference count.
251 Rather than forcing the all debug string handling functions and
252 callers to deal with these complications, simply use a separate,
253 special-cased string table for any attribute that should go in the
254 main object file. This limits the complexity to just the places
255 that need it. */
257 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
259 static GTY(()) int dw2_string_counter;
261 /* True if the compilation unit places functions in more than one section. */
262 static GTY(()) bool have_multiple_function_sections = false;
264 /* The default cold text section. */
265 static GTY(()) section *cold_text_section;
267 /* True if currently in text section. */
268 static GTY(()) bool in_text_section_p = false;
270 /* Last debug-on location in corresponding section. */
271 static GTY(()) const char *last_text_label;
272 static GTY(()) const char *last_cold_label;
274 /* Mark debug-on/off locations per section.
275 NULL means the section is not used at all. */
276 static GTY(()) vec<const char *, va_gc> *switch_text_ranges;
277 static GTY(()) vec<const char *, va_gc> *switch_cold_ranges;
279 /* The DIE for C++14 'auto' in a function return type. */
280 static GTY(()) dw_die_ref auto_die;
282 /* The DIE for C++14 'decltype(auto)' in a function return type. */
283 static GTY(()) dw_die_ref decltype_auto_die;
285 /* Forward declarations for functions defined in this file. */
287 static void output_call_frame_info (int);
289 /* Personality decl of current unit. Used only when assembler does not support
290 personality CFI. */
291 static GTY(()) rtx current_unit_personality;
293 /* Whether an eh_frame section is required. */
294 static GTY(()) bool do_eh_frame = false;
296 /* .debug_rnglists next index. */
297 static unsigned int rnglist_idx;
299 /* Data and reference forms for relocatable data. */
300 #define DW_FORM_data (dwarf_offset_size == 8 ? DW_FORM_data8 : DW_FORM_data4)
301 #define DW_FORM_ref (dwarf_offset_size == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
303 #ifndef DEBUG_FRAME_SECTION
304 #define DEBUG_FRAME_SECTION ".debug_frame"
305 #endif
307 #ifndef FUNC_BEGIN_LABEL
308 #define FUNC_BEGIN_LABEL "LFB"
309 #endif
311 #ifndef FUNC_SECOND_SECT_LABEL
312 #define FUNC_SECOND_SECT_LABEL "LFSB"
313 #endif
315 #ifndef FUNC_END_LABEL
316 #define FUNC_END_LABEL "LFE"
317 #endif
319 #ifndef PROLOGUE_END_LABEL
320 #define PROLOGUE_END_LABEL "LPE"
321 #endif
323 #ifndef EPILOGUE_BEGIN_LABEL
324 #define EPILOGUE_BEGIN_LABEL "LEB"
325 #endif
327 #ifndef FRAME_BEGIN_LABEL
328 #define FRAME_BEGIN_LABEL "Lframe"
329 #endif
330 #define CIE_AFTER_SIZE_LABEL "LSCIE"
331 #define CIE_END_LABEL "LECIE"
332 #define FDE_LABEL "LSFDE"
333 #define FDE_AFTER_SIZE_LABEL "LASFDE"
334 #define FDE_END_LABEL "LEFDE"
335 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
336 #define LINE_NUMBER_END_LABEL "LELT"
337 #define LN_PROLOG_AS_LABEL "LASLTP"
338 #define LN_PROLOG_END_LABEL "LELTP"
339 #define DIE_LABEL_PREFIX "DW"
341 /* Match the base name of a file to the base name of a compilation unit. */
343 static bool
344 matches_main_base (const char *path)
346 /* Cache the last query. */
347 static const char *last_path = NULL;
348 static bool last_match = false;
349 if (path != last_path)
351 const char *base;
352 int length = base_of_path (path, &base);
353 last_path = path;
354 last_match = (length == main_input_baselength
355 && memcmp (base, main_input_basename, length) == 0);
357 return last_match;
360 #ifdef DEBUG_DEBUG_STRUCT
362 static bool
363 dump_struct_debug (tree type, enum debug_info_usage usage,
364 enum debug_struct_file criterion, int generic,
365 bool matches, bool result)
367 /* Find the type name. */
368 tree type_decl = TYPE_STUB_DECL (type);
369 tree t = type_decl;
370 const char *name = 0;
371 if (TREE_CODE (t) == TYPE_DECL)
372 t = DECL_NAME (t);
373 if (t)
374 name = IDENTIFIER_POINTER (t);
376 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
377 criterion,
378 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
379 matches ? "bas" : "hdr",
380 generic ? "gen" : "ord",
381 usage == DINFO_USAGE_DFN ? ";" :
382 usage == DINFO_USAGE_DIR_USE ? "." : "*",
383 result,
384 (void*) type_decl, name);
385 return result;
387 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
388 dump_struct_debug (type, usage, criterion, generic, matches, result)
390 #else
392 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
393 (result)
395 #endif
397 /* Get the number of HOST_WIDE_INTs needed to represent the precision
398 of the number. */
400 static unsigned int
401 get_full_len (const dw_wide_int &op)
403 return CEIL (op.get_precision (), HOST_BITS_PER_WIDE_INT);
406 static bool
407 should_emit_struct_debug (tree type, enum debug_info_usage usage)
409 if (debug_info_level <= DINFO_LEVEL_TERSE)
410 return false;
412 enum debug_struct_file criterion;
413 tree type_decl;
414 bool generic = lang_hooks.types.generic_p (type);
416 if (generic)
417 criterion = debug_struct_generic[usage];
418 else
419 criterion = debug_struct_ordinary[usage];
421 if (criterion == DINFO_STRUCT_FILE_NONE)
422 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
423 if (criterion == DINFO_STRUCT_FILE_ANY)
424 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
426 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
428 if (type_decl != NULL)
430 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
431 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
433 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
434 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
437 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
440 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
441 switch to the data section instead, and write out a synthetic start label
442 for collect2 the first time around. */
444 static void
445 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
447 if (eh_frame_section == 0)
449 int flags;
451 if (EH_TABLES_CAN_BE_READ_ONLY)
453 int fde_encoding;
454 int per_encoding;
455 int lsda_encoding;
457 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
458 /*global=*/0);
459 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
460 /*global=*/1);
461 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
462 /*global=*/0);
463 flags = ((! flag_pic
464 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
465 && (fde_encoding & 0x70) != DW_EH_PE_aligned
466 && (per_encoding & 0x70) != DW_EH_PE_absptr
467 && (per_encoding & 0x70) != DW_EH_PE_aligned
468 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
469 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
470 ? 0 : SECTION_WRITE);
472 else
473 flags = SECTION_WRITE;
475 #ifdef EH_FRAME_SECTION_NAME
476 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
477 #else
478 eh_frame_section = ((flags == SECTION_WRITE)
479 ? data_section : readonly_data_section);
480 #endif /* EH_FRAME_SECTION_NAME */
483 switch_to_section (eh_frame_section);
485 #ifdef EH_FRAME_THROUGH_COLLECT2
486 /* We have no special eh_frame section. Emit special labels to guide
487 collect2. */
488 if (!back)
490 tree label = get_file_function_name ("F");
491 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
492 targetm.asm_out.globalize_label (asm_out_file,
493 IDENTIFIER_POINTER (label));
494 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
496 #endif
499 /* Switch [BACK] to the eh or debug frame table section, depending on
500 FOR_EH. */
502 static void
503 switch_to_frame_table_section (int for_eh, bool back)
505 if (for_eh)
506 switch_to_eh_frame_section (back);
507 else
509 if (!debug_frame_section)
510 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
511 SECTION_DEBUG, NULL);
512 switch_to_section (debug_frame_section);
516 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
518 enum dw_cfi_oprnd_type
519 dw_cfi_oprnd1_desc (dwarf_call_frame_info cfi)
521 switch (cfi)
523 case DW_CFA_nop:
524 case DW_CFA_remember_state:
525 case DW_CFA_restore_state:
526 return dw_cfi_oprnd_unused;
528 case DW_CFA_set_loc:
529 case DW_CFA_advance_loc1:
530 case DW_CFA_advance_loc2:
531 case DW_CFA_advance_loc4:
532 case DW_CFA_MIPS_advance_loc8:
533 return dw_cfi_oprnd_addr;
535 case DW_CFA_offset:
536 case DW_CFA_offset_extended:
537 case DW_CFA_def_cfa:
538 case DW_CFA_offset_extended_sf:
539 case DW_CFA_def_cfa_sf:
540 case DW_CFA_restore:
541 case DW_CFA_restore_extended:
542 case DW_CFA_undefined:
543 case DW_CFA_same_value:
544 case DW_CFA_def_cfa_register:
545 case DW_CFA_register:
546 case DW_CFA_expression:
547 case DW_CFA_val_expression:
548 return dw_cfi_oprnd_reg_num;
550 case DW_CFA_def_cfa_offset:
551 case DW_CFA_GNU_args_size:
552 case DW_CFA_def_cfa_offset_sf:
553 return dw_cfi_oprnd_offset;
555 case DW_CFA_def_cfa_expression:
556 return dw_cfi_oprnd_loc;
558 default:
560 dw_cfi_oprnd_type oprnd_type;
561 if (targetm.dw_cfi_oprnd1_desc (cfi, oprnd_type))
562 return oprnd_type;
563 else
564 gcc_unreachable ();
569 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
571 enum dw_cfi_oprnd_type
572 dw_cfi_oprnd2_desc (dwarf_call_frame_info cfi)
574 switch (cfi)
576 case DW_CFA_def_cfa:
577 case DW_CFA_def_cfa_sf:
578 case DW_CFA_offset:
579 case DW_CFA_offset_extended_sf:
580 case DW_CFA_offset_extended:
581 return dw_cfi_oprnd_offset;
583 case DW_CFA_register:
584 return dw_cfi_oprnd_reg_num;
586 case DW_CFA_expression:
587 case DW_CFA_val_expression:
588 return dw_cfi_oprnd_loc;
590 case DW_CFA_def_cfa_expression:
591 return dw_cfi_oprnd_cfa_loc;
593 default:
594 return dw_cfi_oprnd_unused;
598 /* Output one FDE. */
600 static void
601 output_fde (dw_fde_ref fde, bool for_eh, bool second,
602 char *section_start_label, int fde_encoding, char *augmentation,
603 bool any_lsda_needed, int lsda_encoding)
605 const char *begin, *end;
606 static unsigned int j;
607 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
609 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
610 /* empty */ 0);
611 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
612 for_eh + j);
613 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
614 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
615 if (!XCOFF_DEBUGGING_INFO || for_eh)
617 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
618 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
619 " indicating 64-bit DWARF extension");
620 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
621 "FDE Length");
623 ASM_OUTPUT_LABEL (asm_out_file, l1);
625 if (for_eh)
626 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
627 else
628 dw2_asm_output_offset (dwarf_offset_size, section_start_label,
629 debug_frame_section, "FDE CIE offset");
631 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
632 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
634 if (for_eh)
636 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
637 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
638 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
639 "FDE initial location");
640 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
641 end, begin, "FDE address range");
643 else
645 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
646 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
649 if (augmentation[0])
651 if (any_lsda_needed)
653 int size = size_of_encoded_value (lsda_encoding);
655 if (lsda_encoding == DW_EH_PE_aligned)
657 int offset = ( 4 /* Length */
658 + 4 /* CIE offset */
659 + 2 * size_of_encoded_value (fde_encoding)
660 + 1 /* Augmentation size */ );
661 int pad = -offset & (PTR_SIZE - 1);
663 size += pad;
664 gcc_assert (size_of_uleb128 (size) == 1);
667 dw2_asm_output_data_uleb128 (size, "Augmentation size");
669 if (fde->uses_eh_lsda)
671 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
672 fde->funcdef_number);
673 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
674 gen_rtx_SYMBOL_REF (Pmode, l1),
675 false,
676 "Language Specific Data Area");
678 else
680 if (lsda_encoding == DW_EH_PE_aligned)
681 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
682 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
683 "Language Specific Data Area (none)");
686 else
687 dw2_asm_output_data_uleb128 (0, "Augmentation size");
690 /* Loop through the Call Frame Instructions associated with this FDE. */
691 fde->dw_fde_current_label = begin;
693 size_t from, until, i;
695 from = 0;
696 until = vec_safe_length (fde->dw_fde_cfi);
698 if (fde->dw_fde_second_begin == NULL)
700 else if (!second)
701 until = fde->dw_fde_switch_cfi_index;
702 else
703 from = fde->dw_fde_switch_cfi_index;
705 for (i = from; i < until; i++)
706 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
709 /* If we are to emit a ref/link from function bodies to their frame tables,
710 do it now. This is typically performed to make sure that tables
711 associated with functions are dragged with them and not discarded in
712 garbage collecting links. We need to do this on a per function basis to
713 cope with -ffunction-sections. */
715 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
716 /* Switch to the function section, emit the ref to the tables, and
717 switch *back* into the table section. */
718 switch_to_section (function_section (fde->decl));
719 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
720 switch_to_frame_table_section (for_eh, true);
721 #endif
723 /* Pad the FDE out to an address sized boundary. */
724 ASM_OUTPUT_ALIGN (asm_out_file,
725 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
726 ASM_OUTPUT_LABEL (asm_out_file, l2);
728 j += 2;
731 /* Return true if frame description entry FDE is needed for EH. */
733 static bool
734 fde_needed_for_eh_p (dw_fde_ref fde)
736 if (flag_asynchronous_unwind_tables)
737 return true;
739 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
740 return true;
742 if (fde->uses_eh_lsda)
743 return true;
745 /* If exceptions are enabled, we have collected nothrow info. */
746 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
747 return false;
749 return true;
752 /* Output the call frame information used to record information
753 that relates to calculating the frame pointer, and records the
754 location of saved registers. */
756 static void
757 output_call_frame_info (int for_eh)
759 unsigned int i;
760 dw_fde_ref fde;
761 dw_cfi_ref cfi;
762 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
763 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
764 bool any_lsda_needed = false;
765 char augmentation[6];
766 int augmentation_size;
767 int fde_encoding = DW_EH_PE_absptr;
768 int per_encoding = DW_EH_PE_absptr;
769 int lsda_encoding = DW_EH_PE_absptr;
770 int return_reg;
771 rtx personality = NULL;
772 int dw_cie_version;
774 /* Don't emit a CIE if there won't be any FDEs. */
775 if (!fde_vec)
776 return;
778 /* Nothing to do if the assembler's doing it all. */
779 if (dwarf2out_do_cfi_asm ())
780 return;
782 /* If we don't have any functions we'll want to unwind out of, don't emit
783 any EH unwind information. If we make FDEs linkonce, we may have to
784 emit an empty label for an FDE that wouldn't otherwise be emitted. We
785 want to avoid having an FDE kept around when the function it refers to
786 is discarded. Example where this matters: a primary function template
787 in C++ requires EH information, an explicit specialization doesn't. */
788 if (for_eh)
790 bool any_eh_needed = false;
792 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
794 if (fde->uses_eh_lsda)
795 any_eh_needed = any_lsda_needed = true;
796 else if (fde_needed_for_eh_p (fde))
797 any_eh_needed = true;
798 else if (TARGET_USES_WEAK_UNWIND_INFO)
799 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
802 if (!any_eh_needed)
803 return;
806 /* We're going to be generating comments, so turn on app. */
807 if (flag_debug_asm)
808 app_enable ();
810 /* Switch to the proper frame section, first time. */
811 switch_to_frame_table_section (for_eh, false);
813 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
814 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
816 /* Output the CIE. */
817 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
818 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
819 if (!XCOFF_DEBUGGING_INFO || for_eh)
821 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
822 dw2_asm_output_data (4, 0xffffffff,
823 "Initial length escape value indicating 64-bit DWARF extension");
824 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
825 "Length of Common Information Entry");
827 ASM_OUTPUT_LABEL (asm_out_file, l1);
829 /* Now that the CIE pointer is PC-relative for EH,
830 use 0 to identify the CIE. */
831 dw2_asm_output_data ((for_eh ? 4 : dwarf_offset_size),
832 (for_eh ? 0 : DWARF_CIE_ID),
833 "CIE Identifier Tag");
835 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
836 use CIE version 1, unless that would produce incorrect results
837 due to overflowing the return register column. */
838 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
839 dw_cie_version = 1;
840 if (return_reg >= 256 || dwarf_version > 2)
841 dw_cie_version = 3;
842 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
844 augmentation[0] = 0;
845 augmentation_size = 0;
847 personality = current_unit_personality;
848 if (for_eh)
850 char *p;
852 /* Augmentation:
853 z Indicates that a uleb128 is present to size the
854 augmentation section.
855 L Indicates the encoding (and thus presence) of
856 an LSDA pointer in the FDE augmentation.
857 R Indicates a non-default pointer encoding for
858 FDE code pointers.
859 P Indicates the presence of an encoding + language
860 personality routine in the CIE augmentation. */
862 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
863 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
864 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
866 p = augmentation + 1;
867 if (personality)
869 *p++ = 'P';
870 augmentation_size += 1 + size_of_encoded_value (per_encoding);
871 assemble_external_libcall (personality);
873 if (any_lsda_needed)
875 *p++ = 'L';
876 augmentation_size += 1;
878 if (fde_encoding != DW_EH_PE_absptr)
880 *p++ = 'R';
881 augmentation_size += 1;
883 if (p > augmentation + 1)
885 augmentation[0] = 'z';
886 *p = '\0';
889 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
890 if (personality && per_encoding == DW_EH_PE_aligned)
892 int offset = ( 4 /* Length */
893 + 4 /* CIE Id */
894 + 1 /* CIE version */
895 + strlen (augmentation) + 1 /* Augmentation */
896 + size_of_uleb128 (1) /* Code alignment */
897 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
898 + 1 /* RA column */
899 + 1 /* Augmentation size */
900 + 1 /* Personality encoding */ );
901 int pad = -offset & (PTR_SIZE - 1);
903 augmentation_size += pad;
905 /* Augmentations should be small, so there's scarce need to
906 iterate for a solution. Die if we exceed one uleb128 byte. */
907 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
911 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
912 if (dw_cie_version >= 4)
914 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
915 dw2_asm_output_data (1, 0, "CIE Segment Size");
917 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
918 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
919 "CIE Data Alignment Factor");
921 if (dw_cie_version == 1)
922 dw2_asm_output_data (1, return_reg, "CIE RA Column");
923 else
924 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
926 if (augmentation[0])
928 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
929 if (personality)
931 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
932 eh_data_format_name (per_encoding));
933 dw2_asm_output_encoded_addr_rtx (per_encoding,
934 personality,
935 true, NULL);
938 if (any_lsda_needed)
939 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
940 eh_data_format_name (lsda_encoding));
942 if (fde_encoding != DW_EH_PE_absptr)
943 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
944 eh_data_format_name (fde_encoding));
947 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
948 output_cfi (cfi, NULL, for_eh);
950 /* Pad the CIE out to an address sized boundary. */
951 ASM_OUTPUT_ALIGN (asm_out_file,
952 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
953 ASM_OUTPUT_LABEL (asm_out_file, l2);
955 /* Loop through all of the FDE's. */
956 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
958 unsigned int k;
960 /* Don't emit EH unwind info for leaf functions that don't need it. */
961 if (for_eh && !fde_needed_for_eh_p (fde))
962 continue;
964 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
965 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
966 augmentation, any_lsda_needed, lsda_encoding);
969 if (for_eh && targetm.terminate_dw2_eh_frame_info)
970 dw2_asm_output_data (4, 0, "End of Table");
972 /* Turn off app to make assembly quicker. */
973 if (flag_debug_asm)
974 app_disable ();
977 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
979 static void
980 dwarf2out_do_cfi_startproc (bool second)
982 int enc;
983 rtx ref;
985 fprintf (asm_out_file, "\t.cfi_startproc\n");
987 targetm.asm_out.post_cfi_startproc (asm_out_file, current_function_decl);
989 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
990 eh unwinders. */
991 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
992 return;
994 rtx personality = get_personality_function (current_function_decl);
996 if (personality)
998 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
999 ref = personality;
1001 /* ??? The GAS support isn't entirely consistent. We have to
1002 handle indirect support ourselves, but PC-relative is done
1003 in the assembler. Further, the assembler can't handle any
1004 of the weirder relocation types. */
1005 if (enc & DW_EH_PE_indirect)
1007 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1008 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1009 else
1010 ref = dw2_force_const_mem (ref, true);
1013 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
1014 output_addr_const (asm_out_file, ref);
1015 fputc ('\n', asm_out_file);
1018 if (crtl->uses_eh_lsda)
1020 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1022 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1023 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1024 current_function_funcdef_no);
1025 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1026 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1028 if (enc & DW_EH_PE_indirect)
1030 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1031 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1032 else
1033 ref = dw2_force_const_mem (ref, true);
1036 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1037 output_addr_const (asm_out_file, ref);
1038 fputc ('\n', asm_out_file);
1042 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1043 this allocation may be done before pass_final. */
1045 dw_fde_ref
1046 dwarf2out_alloc_current_fde (void)
1048 dw_fde_ref fde;
1050 fde = ggc_cleared_alloc<dw_fde_node> ();
1051 fde->decl = current_function_decl;
1052 fde->funcdef_number = current_function_funcdef_no;
1053 fde->fde_index = vec_safe_length (fde_vec);
1054 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1055 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1056 fde->nothrow = crtl->nothrow;
1057 fde->drap_reg = INVALID_REGNUM;
1058 fde->vdrap_reg = INVALID_REGNUM;
1060 /* Record the FDE associated with this function. */
1061 cfun->fde = fde;
1062 vec_safe_push (fde_vec, fde);
1064 return fde;
1067 /* Output a marker (i.e. a label) for the beginning of a function, before
1068 the prologue. */
1070 void
1071 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1072 unsigned int column ATTRIBUTE_UNUSED,
1073 const char *file ATTRIBUTE_UNUSED)
1075 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1076 char * dup_label;
1077 dw_fde_ref fde;
1078 section *fnsec;
1079 bool do_frame;
1081 current_function_func_begin_label = NULL;
1083 do_frame = dwarf2out_do_frame ();
1085 /* ??? current_function_func_begin_label is also used by except.cc for
1086 call-site information. We must emit this label if it might be used. */
1087 if (!do_frame
1088 && (!flag_exceptions
1089 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1090 return;
1092 fnsec = function_section (current_function_decl);
1093 switch_to_section (fnsec);
1094 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1095 current_function_funcdef_no);
1096 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1097 current_function_funcdef_no);
1098 dup_label = xstrdup (label);
1099 current_function_func_begin_label = dup_label;
1101 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1102 if (!do_frame)
1103 return;
1105 /* Unlike the debug version, the EH version of frame unwind info is a per-
1106 function setting so we need to record whether we need it for the unit. */
1107 do_eh_frame |= dwarf2out_do_eh_frame ();
1109 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1110 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1111 would include pass_dwarf2_frame. If we've not created the FDE yet,
1112 do so now. */
1113 fde = cfun->fde;
1114 if (fde == NULL)
1115 fde = dwarf2out_alloc_current_fde ();
1117 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1118 fde->dw_fde_begin = dup_label;
1119 fde->dw_fde_current_label = dup_label;
1120 fde->in_std_section = (fnsec == text_section
1121 || (cold_text_section && fnsec == cold_text_section));
1122 fde->ignored_debug = DECL_IGNORED_P (current_function_decl);
1123 in_text_section_p = fnsec == text_section;
1125 /* We only want to output line number information for the genuine dwarf2
1126 prologue case, not the eh frame case. */
1127 #ifdef DWARF2_DEBUGGING_INFO
1128 if (file)
1129 dwarf2out_source_line (line, column, file, 0, true);
1130 #endif
1132 if (dwarf2out_do_cfi_asm ())
1133 dwarf2out_do_cfi_startproc (false);
1134 else
1136 rtx personality = get_personality_function (current_function_decl);
1137 if (!current_unit_personality)
1138 current_unit_personality = personality;
1140 /* We cannot keep a current personality per function as without CFI
1141 asm, at the point where we emit the CFI data, there is no current
1142 function anymore. */
1143 if (personality && current_unit_personality != personality)
1144 sorry ("multiple EH personalities are supported only with assemblers "
1145 "supporting %<.cfi_personality%> directive");
1149 /* Output a marker (i.e. a label) for the end of the generated code
1150 for a function prologue. This gets called *after* the prologue code has
1151 been generated. */
1153 void
1154 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1155 const char *file ATTRIBUTE_UNUSED)
1157 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1159 /* Output a label to mark the endpoint of the code generated for this
1160 function. */
1161 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1162 current_function_funcdef_no);
1163 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1164 current_function_funcdef_no);
1165 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1168 /* Output a marker (i.e. a label) for the beginning of the generated code
1169 for a function epilogue. This gets called *before* the prologue code has
1170 been generated. */
1172 void
1173 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1174 const char *file ATTRIBUTE_UNUSED)
1176 dw_fde_ref fde = cfun->fde;
1177 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1179 if (fde->dw_fde_vms_begin_epilogue)
1180 return;
1182 /* Output a label to mark the endpoint of the code generated for this
1183 function. */
1184 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1185 current_function_funcdef_no);
1186 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1187 current_function_funcdef_no);
1188 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1191 /* Mark the ranges of non-debug subsections in the std text sections. */
1193 static void
1194 mark_ignored_debug_section (dw_fde_ref fde, bool second)
1196 bool std_section;
1197 const char *begin_label, *end_label;
1198 const char **last_end_label;
1199 vec<const char *, va_gc> **switch_ranges;
1201 if (second)
1203 std_section = fde->second_in_std_section;
1204 begin_label = fde->dw_fde_second_begin;
1205 end_label = fde->dw_fde_second_end;
1207 else
1209 std_section = fde->in_std_section;
1210 begin_label = fde->dw_fde_begin;
1211 end_label = fde->dw_fde_end;
1214 if (!std_section)
1215 return;
1217 if (in_text_section_p)
1219 last_end_label = &last_text_label;
1220 switch_ranges = &switch_text_ranges;
1222 else
1224 last_end_label = &last_cold_label;
1225 switch_ranges = &switch_cold_ranges;
1228 if (fde->ignored_debug)
1230 if (*switch_ranges && !(vec_safe_length (*switch_ranges) & 1))
1231 vec_safe_push (*switch_ranges, *last_end_label);
1233 else
1235 *last_end_label = end_label;
1237 if (!*switch_ranges)
1238 vec_alloc (*switch_ranges, 16);
1239 else if (vec_safe_length (*switch_ranges) & 1)
1240 vec_safe_push (*switch_ranges, begin_label);
1244 /* Output a marker (i.e. a label) for the absolute end of the generated code
1245 for a function definition. This gets called *after* the epilogue code has
1246 been generated. */
1248 void
1249 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1250 const char *file ATTRIBUTE_UNUSED)
1252 dw_fde_ref fde;
1253 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1255 last_var_location_insn = NULL;
1256 cached_next_real_insn = NULL;
1258 if (dwarf2out_do_cfi_asm ())
1259 fprintf (asm_out_file, "\t.cfi_endproc\n");
1261 #ifdef CODEVIEW_DEBUGGING_INFO
1262 if (codeview_debuginfo_p ())
1263 codeview_end_epilogue ();
1264 #endif
1266 /* Output a label to mark the endpoint of the code generated for this
1267 function. */
1268 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1269 current_function_funcdef_no);
1270 ASM_OUTPUT_LABEL (asm_out_file, label);
1271 fde = cfun->fde;
1272 gcc_assert (fde != NULL);
1273 if (fde->dw_fde_second_begin == NULL)
1274 fde->dw_fde_end = xstrdup (label);
1276 mark_ignored_debug_section (fde, fde->dw_fde_second_begin != NULL);
1279 void
1280 dwarf2out_frame_finish (void)
1282 /* Output call frame information. */
1283 if (targetm.debug_unwind_info () == UI_DWARF2)
1284 output_call_frame_info (0);
1286 /* Output another copy for the unwinder. */
1287 if (do_eh_frame)
1288 output_call_frame_info (1);
1291 static void var_location_switch_text_section (void);
1292 static void set_cur_line_info_table (section *);
1294 void
1295 dwarf2out_switch_text_section (void)
1297 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1298 section *sect;
1299 dw_fde_ref fde = cfun->fde;
1301 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1303 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1304 current_function_funcdef_no);
1306 fde->dw_fde_second_begin = ggc_strdup (label);
1307 if (!in_cold_section_p)
1309 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1310 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1312 else
1314 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1315 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1317 have_multiple_function_sections = true;
1319 #ifdef CODEVIEW_DEBUGGING_INFO
1320 if (codeview_debuginfo_p ())
1321 codeview_switch_text_section ();
1322 #endif
1324 if (dwarf2out_do_cfi_asm ())
1325 fprintf (asm_out_file, "\t.cfi_endproc\n");
1327 mark_ignored_debug_section (fde, false);
1329 /* Now do the real section switch. */
1330 sect = current_function_section ();
1331 switch_to_section (sect);
1333 fde->second_in_std_section
1334 = (sect == text_section
1335 || (cold_text_section && sect == cold_text_section));
1336 in_text_section_p = sect == text_section;
1338 if (dwarf2out_do_cfi_asm ())
1339 dwarf2out_do_cfi_startproc (true);
1341 var_location_switch_text_section ();
1343 if (cold_text_section != NULL)
1344 set_cur_line_info_table (sect);
1347 /* And now, the subset of the debugging information support code necessary
1348 for emitting location expressions. */
1350 /* Describe an entry into the .debug_addr section. */
1352 enum ate_kind {
1353 ate_kind_rtx,
1354 ate_kind_rtx_dtprel,
1355 ate_kind_label
1358 struct GTY((for_user)) addr_table_entry {
1359 enum ate_kind kind;
1360 unsigned int refcount;
1361 unsigned int index;
1362 union addr_table_entry_struct_union
1364 rtx GTY ((tag ("0"))) rtl;
1365 char * GTY ((tag ("1"))) label;
1367 GTY ((desc ("%1.kind"))) addr;
1370 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1371 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1373 /* Convert a DWARF stack opcode into its string name. */
1375 static const char *
1376 dwarf_stack_op_name (unsigned int op)
1378 const char *name = get_DW_OP_name (op);
1380 if (name != NULL)
1381 return name;
1383 return "OP_<unknown>";
1386 /* Return TRUE iff we're to output location view lists as a separate
1387 attribute next to the location lists, as an extension compatible
1388 with DWARF 2 and above. */
1390 static inline bool
1391 dwarf2out_locviews_in_attribute ()
1393 return debug_variable_location_views == 1;
1396 /* Return TRUE iff we're to output location view lists as part of the
1397 location lists, as proposed for standardization after DWARF 5. */
1399 static inline bool
1400 dwarf2out_locviews_in_loclist ()
1402 #ifndef DW_LLE_view_pair
1403 return false;
1404 #else
1405 return debug_variable_location_views == -1;
1406 #endif
1409 /* Return a pointer to a newly allocated location description. Location
1410 descriptions are simple expression terms that can be strung
1411 together to form more complicated location (address) descriptions. */
1413 static inline dw_loc_descr_ref
1414 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1415 unsigned HOST_WIDE_INT oprnd2)
1417 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1419 descr->dw_loc_opc = op;
1420 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1421 descr->dw_loc_oprnd1.val_entry = NULL;
1422 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1423 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1424 descr->dw_loc_oprnd2.val_entry = NULL;
1425 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1427 return descr;
1430 /* Add a location description term to a location description expression. */
1432 static inline void
1433 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1435 dw_loc_descr_ref *d;
1437 /* Find the end of the chain. */
1438 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1441 *d = descr;
1444 /* Compare two location operands for exact equality. */
1446 static bool
1447 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1449 if (a->val_class != b->val_class)
1450 return false;
1451 switch (a->val_class)
1453 case dw_val_class_none:
1454 return true;
1455 case dw_val_class_addr:
1456 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1458 case dw_val_class_offset:
1459 case dw_val_class_unsigned_const:
1460 case dw_val_class_const:
1461 case dw_val_class_unsigned_const_implicit:
1462 case dw_val_class_const_implicit:
1463 case dw_val_class_range_list:
1464 /* These are all HOST_WIDE_INT, signed or unsigned. */
1465 return a->v.val_unsigned == b->v.val_unsigned;
1467 case dw_val_class_loc:
1468 return a->v.val_loc == b->v.val_loc;
1469 case dw_val_class_loc_list:
1470 return a->v.val_loc_list == b->v.val_loc_list;
1471 case dw_val_class_view_list:
1472 return a->v.val_view_list == b->v.val_view_list;
1473 case dw_val_class_die_ref:
1474 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1475 case dw_val_class_fde_ref:
1476 return a->v.val_fde_index == b->v.val_fde_index;
1477 case dw_val_class_symview:
1478 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1479 case dw_val_class_lbl_id:
1480 case dw_val_class_lineptr:
1481 case dw_val_class_macptr:
1482 case dw_val_class_loclistsptr:
1483 case dw_val_class_high_pc:
1484 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1485 case dw_val_class_str:
1486 return a->v.val_str == b->v.val_str;
1487 case dw_val_class_flag:
1488 return a->v.val_flag == b->v.val_flag;
1489 case dw_val_class_file:
1490 case dw_val_class_file_implicit:
1491 return a->v.val_file == b->v.val_file;
1492 case dw_val_class_decl_ref:
1493 return a->v.val_decl_ref == b->v.val_decl_ref;
1495 case dw_val_class_const_double:
1496 return (a->v.val_double.high == b->v.val_double.high
1497 && a->v.val_double.low == b->v.val_double.low);
1499 case dw_val_class_wide_int:
1500 return *a->v.val_wide == *b->v.val_wide;
1502 case dw_val_class_vec:
1504 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1505 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1507 return (a_len == b_len
1508 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1511 case dw_val_class_data8:
1512 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1514 case dw_val_class_vms_delta:
1515 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1516 && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1518 case dw_val_class_discr_value:
1519 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1520 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1521 case dw_val_class_discr_list:
1522 /* It makes no sense comparing two discriminant value lists. */
1523 return false;
1525 gcc_unreachable ();
1528 /* Compare two location atoms for exact equality. */
1530 static bool
1531 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1533 if (a->dw_loc_opc != b->dw_loc_opc)
1534 return false;
1536 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1537 address size, but since we always allocate cleared storage it
1538 should be zero for other types of locations. */
1539 if (a->dtprel != b->dtprel)
1540 return false;
1542 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1543 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1546 /* Compare two complete location expressions for exact equality. */
1548 bool
1549 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1551 while (1)
1553 if (a == b)
1554 return true;
1555 if (a == NULL || b == NULL)
1556 return false;
1557 if (!loc_descr_equal_p_1 (a, b))
1558 return false;
1560 a = a->dw_loc_next;
1561 b = b->dw_loc_next;
1566 /* Add a constant POLY_OFFSET to a location expression. */
1568 static void
1569 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1571 dw_loc_descr_ref loc;
1572 HOST_WIDE_INT *p;
1574 gcc_assert (*list_head != NULL);
1576 if (known_eq (poly_offset, 0))
1577 return;
1579 /* Find the end of the chain. */
1580 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1583 HOST_WIDE_INT offset;
1584 if (!poly_offset.is_constant (&offset))
1586 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1587 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1588 return;
1591 p = NULL;
1592 if (loc->dw_loc_opc == DW_OP_fbreg
1593 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1594 p = &loc->dw_loc_oprnd1.v.val_int;
1595 else if (loc->dw_loc_opc == DW_OP_bregx)
1596 p = &loc->dw_loc_oprnd2.v.val_int;
1598 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1599 offset. Don't optimize if an signed integer overflow would happen. */
1600 if (p != NULL
1601 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1602 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1603 *p += offset;
1605 else if (offset > 0)
1606 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1608 else
1610 loc->dw_loc_next
1611 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1612 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1616 /* Return a pointer to a newly allocated location description for
1617 REG and OFFSET. */
1619 static inline dw_loc_descr_ref
1620 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1622 HOST_WIDE_INT const_offset;
1623 if (offset.is_constant (&const_offset))
1625 if (reg <= 31)
1626 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1627 const_offset, 0);
1628 else
1629 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1631 else
1633 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1634 loc_descr_plus_const (&ret, offset);
1635 return ret;
1639 /* Add a constant OFFSET to a location list. */
1641 static void
1642 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1644 dw_loc_list_ref d;
1645 for (d = list_head; d != NULL; d = d->dw_loc_next)
1646 loc_descr_plus_const (&d->expr, offset);
1649 #define DWARF_REF_SIZE \
1650 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : dwarf_offset_size)
1652 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1653 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1654 DW_FORM_data16 with 128 bits. */
1655 #define DWARF_LARGEST_DATA_FORM_BITS \
1656 (dwarf_version >= 5 ? 128 : 64)
1658 /* Utility inline function for construction of ops that were GNU extension
1659 before DWARF 5. */
1660 static inline enum dwarf_location_atom
1661 dwarf_OP (enum dwarf_location_atom op)
1663 switch (op)
1665 case DW_OP_implicit_pointer:
1666 if (dwarf_version < 5)
1667 return DW_OP_GNU_implicit_pointer;
1668 break;
1670 case DW_OP_entry_value:
1671 if (dwarf_version < 5)
1672 return DW_OP_GNU_entry_value;
1673 break;
1675 case DW_OP_const_type:
1676 if (dwarf_version < 5)
1677 return DW_OP_GNU_const_type;
1678 break;
1680 case DW_OP_regval_type:
1681 if (dwarf_version < 5)
1682 return DW_OP_GNU_regval_type;
1683 break;
1685 case DW_OP_deref_type:
1686 if (dwarf_version < 5)
1687 return DW_OP_GNU_deref_type;
1688 break;
1690 case DW_OP_convert:
1691 if (dwarf_version < 5)
1692 return DW_OP_GNU_convert;
1693 break;
1695 case DW_OP_reinterpret:
1696 if (dwarf_version < 5)
1697 return DW_OP_GNU_reinterpret;
1698 break;
1700 case DW_OP_addrx:
1701 if (dwarf_version < 5)
1702 return DW_OP_GNU_addr_index;
1703 break;
1705 case DW_OP_constx:
1706 if (dwarf_version < 5)
1707 return DW_OP_GNU_const_index;
1708 break;
1710 default:
1711 break;
1713 return op;
1716 /* Similarly for attributes. */
1717 static inline enum dwarf_attribute
1718 dwarf_AT (enum dwarf_attribute at)
1720 switch (at)
1722 case DW_AT_call_return_pc:
1723 if (dwarf_version < 5)
1724 return DW_AT_low_pc;
1725 break;
1727 case DW_AT_call_tail_call:
1728 if (dwarf_version < 5)
1729 return DW_AT_GNU_tail_call;
1730 break;
1732 case DW_AT_call_origin:
1733 if (dwarf_version < 5)
1734 return DW_AT_abstract_origin;
1735 break;
1737 case DW_AT_call_target:
1738 if (dwarf_version < 5)
1739 return DW_AT_GNU_call_site_target;
1740 break;
1742 case DW_AT_call_target_clobbered:
1743 if (dwarf_version < 5)
1744 return DW_AT_GNU_call_site_target_clobbered;
1745 break;
1747 case DW_AT_call_parameter:
1748 if (dwarf_version < 5)
1749 return DW_AT_abstract_origin;
1750 break;
1752 case DW_AT_call_value:
1753 if (dwarf_version < 5)
1754 return DW_AT_GNU_call_site_value;
1755 break;
1757 case DW_AT_call_data_value:
1758 if (dwarf_version < 5)
1759 return DW_AT_GNU_call_site_data_value;
1760 break;
1762 case DW_AT_call_all_calls:
1763 if (dwarf_version < 5)
1764 return DW_AT_GNU_all_call_sites;
1765 break;
1767 case DW_AT_call_all_tail_calls:
1768 if (dwarf_version < 5)
1769 return DW_AT_GNU_all_tail_call_sites;
1770 break;
1772 case DW_AT_dwo_name:
1773 if (dwarf_version < 5)
1774 return DW_AT_GNU_dwo_name;
1775 break;
1777 case DW_AT_addr_base:
1778 if (dwarf_version < 5)
1779 return DW_AT_GNU_addr_base;
1780 break;
1782 default:
1783 break;
1785 return at;
1788 /* And similarly for tags. */
1789 static inline enum dwarf_tag
1790 dwarf_TAG (enum dwarf_tag tag)
1792 switch (tag)
1794 case DW_TAG_call_site:
1795 if (dwarf_version < 5)
1796 return DW_TAG_GNU_call_site;
1797 break;
1799 case DW_TAG_call_site_parameter:
1800 if (dwarf_version < 5)
1801 return DW_TAG_GNU_call_site_parameter;
1802 break;
1804 default:
1805 break;
1807 return tag;
1810 /* And similarly for forms. */
1811 static inline enum dwarf_form
1812 dwarf_FORM (enum dwarf_form form)
1814 switch (form)
1816 case DW_FORM_addrx:
1817 if (dwarf_version < 5)
1818 return DW_FORM_GNU_addr_index;
1819 break;
1821 case DW_FORM_strx:
1822 if (dwarf_version < 5)
1823 return DW_FORM_GNU_str_index;
1824 break;
1826 default:
1827 break;
1829 return form;
1832 static unsigned long int get_base_type_offset (dw_die_ref);
1834 /* Return the size of a location descriptor. */
1836 static unsigned long
1837 size_of_loc_descr (dw_loc_descr_ref loc)
1839 unsigned long size = 1;
1841 switch (loc->dw_loc_opc)
1843 case DW_OP_addr:
1844 size += DWARF2_ADDR_SIZE;
1845 break;
1846 case DW_OP_GNU_addr_index:
1847 case DW_OP_addrx:
1848 case DW_OP_GNU_const_index:
1849 case DW_OP_constx:
1850 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1851 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1852 break;
1853 case DW_OP_const1u:
1854 case DW_OP_const1s:
1855 size += 1;
1856 break;
1857 case DW_OP_const2u:
1858 case DW_OP_const2s:
1859 size += 2;
1860 break;
1861 case DW_OP_const4u:
1862 case DW_OP_const4s:
1863 size += 4;
1864 break;
1865 case DW_OP_const8u:
1866 case DW_OP_const8s:
1867 size += 8;
1868 break;
1869 case DW_OP_constu:
1870 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1871 break;
1872 case DW_OP_consts:
1873 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1874 break;
1875 case DW_OP_pick:
1876 size += 1;
1877 break;
1878 case DW_OP_plus_uconst:
1879 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1880 break;
1881 case DW_OP_skip:
1882 case DW_OP_bra:
1883 size += 2;
1884 break;
1885 case DW_OP_breg0:
1886 case DW_OP_breg1:
1887 case DW_OP_breg2:
1888 case DW_OP_breg3:
1889 case DW_OP_breg4:
1890 case DW_OP_breg5:
1891 case DW_OP_breg6:
1892 case DW_OP_breg7:
1893 case DW_OP_breg8:
1894 case DW_OP_breg9:
1895 case DW_OP_breg10:
1896 case DW_OP_breg11:
1897 case DW_OP_breg12:
1898 case DW_OP_breg13:
1899 case DW_OP_breg14:
1900 case DW_OP_breg15:
1901 case DW_OP_breg16:
1902 case DW_OP_breg17:
1903 case DW_OP_breg18:
1904 case DW_OP_breg19:
1905 case DW_OP_breg20:
1906 case DW_OP_breg21:
1907 case DW_OP_breg22:
1908 case DW_OP_breg23:
1909 case DW_OP_breg24:
1910 case DW_OP_breg25:
1911 case DW_OP_breg26:
1912 case DW_OP_breg27:
1913 case DW_OP_breg28:
1914 case DW_OP_breg29:
1915 case DW_OP_breg30:
1916 case DW_OP_breg31:
1917 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1918 break;
1919 case DW_OP_regx:
1920 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1921 break;
1922 case DW_OP_fbreg:
1923 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1924 break;
1925 case DW_OP_bregx:
1926 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1927 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1928 break;
1929 case DW_OP_piece:
1930 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1931 break;
1932 case DW_OP_bit_piece:
1933 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1934 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1935 break;
1936 case DW_OP_deref_size:
1937 case DW_OP_xderef_size:
1938 size += 1;
1939 break;
1940 case DW_OP_call2:
1941 size += 2;
1942 break;
1943 case DW_OP_call4:
1944 size += 4;
1945 break;
1946 case DW_OP_call_ref:
1947 case DW_OP_GNU_variable_value:
1948 size += DWARF_REF_SIZE;
1949 break;
1950 case DW_OP_implicit_value:
1951 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1952 + loc->dw_loc_oprnd1.v.val_unsigned;
1953 break;
1954 case DW_OP_implicit_pointer:
1955 case DW_OP_GNU_implicit_pointer:
1956 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1957 break;
1958 case DW_OP_entry_value:
1959 case DW_OP_GNU_entry_value:
1961 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1962 size += size_of_uleb128 (op_size) + op_size;
1963 break;
1965 case DW_OP_const_type:
1966 case DW_OP_GNU_const_type:
1968 unsigned long o
1969 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1970 size += size_of_uleb128 (o) + 1;
1971 switch (loc->dw_loc_oprnd2.val_class)
1973 case dw_val_class_vec:
1974 size += loc->dw_loc_oprnd2.v.val_vec.length
1975 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1976 break;
1977 case dw_val_class_const:
1978 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1979 break;
1980 case dw_val_class_const_double:
1981 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1982 break;
1983 case dw_val_class_wide_int:
1984 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1985 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1986 break;
1987 default:
1988 gcc_unreachable ();
1990 break;
1992 case DW_OP_regval_type:
1993 case DW_OP_GNU_regval_type:
1995 unsigned long o
1996 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1997 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1998 + size_of_uleb128 (o);
2000 break;
2001 case DW_OP_deref_type:
2002 case DW_OP_GNU_deref_type:
2004 unsigned long o
2005 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2006 size += 1 + size_of_uleb128 (o);
2008 break;
2009 case DW_OP_convert:
2010 case DW_OP_reinterpret:
2011 case DW_OP_GNU_convert:
2012 case DW_OP_GNU_reinterpret:
2013 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2014 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2015 else
2017 unsigned long o
2018 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2019 size += size_of_uleb128 (o);
2021 break;
2022 case DW_OP_GNU_parameter_ref:
2023 size += 4;
2024 break;
2025 default:
2026 break;
2029 return size;
2032 /* Return the size of a series of location descriptors. */
2034 unsigned long
2035 size_of_locs (dw_loc_descr_ref loc)
2037 dw_loc_descr_ref l;
2038 unsigned long size;
2040 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2041 field, to avoid writing to a PCH file. */
2042 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2044 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2045 break;
2046 size += size_of_loc_descr (l);
2048 if (! l)
2049 return size;
2051 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2053 l->dw_loc_addr = size;
2054 size += size_of_loc_descr (l);
2057 return size;
2060 /* Return the size of the value in a DW_AT_discr_value attribute. */
2062 static int
2063 size_of_discr_value (dw_discr_value *discr_value)
2065 if (discr_value->pos)
2066 return size_of_uleb128 (discr_value->v.uval);
2067 else
2068 return size_of_sleb128 (discr_value->v.sval);
2071 /* Return the size of the value in a DW_AT_discr_list attribute. */
2073 static int
2074 size_of_discr_list (dw_discr_list_ref discr_list)
2076 int size = 0;
2078 for (dw_discr_list_ref list = discr_list;
2079 list != NULL;
2080 list = list->dw_discr_next)
2082 /* One byte for the discriminant value descriptor, and then one or two
2083 LEB128 numbers, depending on whether it's a single case label or a
2084 range label. */
2085 size += 1;
2086 size += size_of_discr_value (&list->dw_discr_lower_bound);
2087 if (list->dw_discr_range != 0)
2088 size += size_of_discr_value (&list->dw_discr_upper_bound);
2090 return size;
2093 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2094 static void get_ref_die_offset_label (char *, dw_die_ref);
2095 static unsigned long int get_ref_die_offset (dw_die_ref);
2097 /* Output location description stack opcode's operands (if any).
2098 The for_eh_or_skip parameter controls whether register numbers are
2099 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2100 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2101 info). This should be suppressed for the cases that have not been converted
2102 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2104 static void
2105 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2107 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2108 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2110 switch (loc->dw_loc_opc)
2112 #ifdef DWARF2_DEBUGGING_INFO
2113 case DW_OP_const2u:
2114 case DW_OP_const2s:
2115 dw2_asm_output_data (2, val1->v.val_int, NULL);
2116 break;
2117 case DW_OP_const4u:
2118 if (loc->dtprel)
2120 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2121 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2122 val1->v.val_addr);
2123 fputc ('\n', asm_out_file);
2124 break;
2126 /* FALLTHRU */
2127 case DW_OP_const4s:
2128 dw2_asm_output_data (4, val1->v.val_int, NULL);
2129 break;
2130 case DW_OP_const8u:
2131 if (loc->dtprel)
2133 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2134 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2135 val1->v.val_addr);
2136 fputc ('\n', asm_out_file);
2137 break;
2139 /* FALLTHRU */
2140 case DW_OP_const8s:
2141 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2142 dw2_asm_output_data (8, val1->v.val_int, NULL);
2143 break;
2144 case DW_OP_skip:
2145 case DW_OP_bra:
2147 int offset;
2149 gcc_assert (val1->val_class == dw_val_class_loc);
2150 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2152 dw2_asm_output_data (2, offset, NULL);
2154 break;
2155 case DW_OP_implicit_value:
2156 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2157 switch (val2->val_class)
2159 case dw_val_class_const:
2160 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2161 break;
2162 case dw_val_class_vec:
2164 unsigned int elt_size = val2->v.val_vec.elt_size;
2165 unsigned int len = val2->v.val_vec.length;
2166 unsigned int i;
2167 unsigned char *p;
2169 if (elt_size > sizeof (HOST_WIDE_INT))
2171 elt_size /= 2;
2172 len *= 2;
2174 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2175 i < len;
2176 i++, p += elt_size)
2177 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2178 "fp or vector constant word %u", i);
2180 break;
2181 case dw_val_class_const_double:
2183 unsigned HOST_WIDE_INT first, second;
2185 if (WORDS_BIG_ENDIAN)
2187 first = val2->v.val_double.high;
2188 second = val2->v.val_double.low;
2190 else
2192 first = val2->v.val_double.low;
2193 second = val2->v.val_double.high;
2195 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2196 first, NULL);
2197 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2198 second, NULL);
2200 break;
2201 case dw_val_class_wide_int:
2203 int i;
2204 int len = get_full_len (*val2->v.val_wide);
2205 if (WORDS_BIG_ENDIAN)
2206 for (i = len - 1; i >= 0; --i)
2207 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2208 val2->v.val_wide->elt (i), NULL);
2209 else
2210 for (i = 0; i < len; ++i)
2211 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2212 val2->v.val_wide->elt (i), NULL);
2214 break;
2215 case dw_val_class_addr:
2216 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2217 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2218 break;
2219 default:
2220 gcc_unreachable ();
2222 break;
2223 #else
2224 case DW_OP_const2u:
2225 case DW_OP_const2s:
2226 case DW_OP_const4u:
2227 case DW_OP_const4s:
2228 case DW_OP_const8u:
2229 case DW_OP_const8s:
2230 case DW_OP_skip:
2231 case DW_OP_bra:
2232 case DW_OP_implicit_value:
2233 /* We currently don't make any attempt to make sure these are
2234 aligned properly like we do for the main unwind info, so
2235 don't support emitting things larger than a byte if we're
2236 only doing unwinding. */
2237 gcc_unreachable ();
2238 #endif
2239 case DW_OP_const1u:
2240 case DW_OP_const1s:
2241 dw2_asm_output_data (1, val1->v.val_int, NULL);
2242 break;
2243 case DW_OP_constu:
2244 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2245 break;
2246 case DW_OP_consts:
2247 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2248 break;
2249 case DW_OP_pick:
2250 dw2_asm_output_data (1, val1->v.val_int, NULL);
2251 break;
2252 case DW_OP_plus_uconst:
2253 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2254 break;
2255 case DW_OP_breg0:
2256 case DW_OP_breg1:
2257 case DW_OP_breg2:
2258 case DW_OP_breg3:
2259 case DW_OP_breg4:
2260 case DW_OP_breg5:
2261 case DW_OP_breg6:
2262 case DW_OP_breg7:
2263 case DW_OP_breg8:
2264 case DW_OP_breg9:
2265 case DW_OP_breg10:
2266 case DW_OP_breg11:
2267 case DW_OP_breg12:
2268 case DW_OP_breg13:
2269 case DW_OP_breg14:
2270 case DW_OP_breg15:
2271 case DW_OP_breg16:
2272 case DW_OP_breg17:
2273 case DW_OP_breg18:
2274 case DW_OP_breg19:
2275 case DW_OP_breg20:
2276 case DW_OP_breg21:
2277 case DW_OP_breg22:
2278 case DW_OP_breg23:
2279 case DW_OP_breg24:
2280 case DW_OP_breg25:
2281 case DW_OP_breg26:
2282 case DW_OP_breg27:
2283 case DW_OP_breg28:
2284 case DW_OP_breg29:
2285 case DW_OP_breg30:
2286 case DW_OP_breg31:
2287 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2288 break;
2289 case DW_OP_regx:
2291 unsigned r = val1->v.val_unsigned;
2292 if (for_eh_or_skip >= 0)
2293 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2294 gcc_assert (size_of_uleb128 (r)
2295 == size_of_uleb128 (val1->v.val_unsigned));
2296 dw2_asm_output_data_uleb128 (r, NULL);
2298 break;
2299 case DW_OP_fbreg:
2300 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2301 break;
2302 case DW_OP_bregx:
2304 unsigned r = val1->v.val_unsigned;
2305 if (for_eh_or_skip >= 0)
2306 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2307 gcc_assert (size_of_uleb128 (r)
2308 == size_of_uleb128 (val1->v.val_unsigned));
2309 dw2_asm_output_data_uleb128 (r, NULL);
2310 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2312 break;
2313 case DW_OP_piece:
2314 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2315 break;
2316 case DW_OP_bit_piece:
2317 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2318 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2319 break;
2320 case DW_OP_deref_size:
2321 case DW_OP_xderef_size:
2322 dw2_asm_output_data (1, val1->v.val_int, NULL);
2323 break;
2325 case DW_OP_addr:
2326 if (loc->dtprel)
2328 if (targetm.asm_out.output_dwarf_dtprel)
2330 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2331 DWARF2_ADDR_SIZE,
2332 val1->v.val_addr);
2333 fputc ('\n', asm_out_file);
2335 else
2336 gcc_unreachable ();
2338 else
2340 #ifdef DWARF2_DEBUGGING_INFO
2341 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2342 #else
2343 gcc_unreachable ();
2344 #endif
2346 break;
2348 case DW_OP_GNU_addr_index:
2349 case DW_OP_addrx:
2350 case DW_OP_GNU_const_index:
2351 case DW_OP_constx:
2352 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2353 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2354 "(index into .debug_addr)");
2355 break;
2357 case DW_OP_call2:
2358 case DW_OP_call4:
2360 unsigned long die_offset
2361 = get_ref_die_offset (val1->v.val_die_ref.die);
2362 /* Make sure the offset has been computed and that we can encode it as
2363 an operand. */
2364 gcc_assert (die_offset > 0
2365 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2366 ? 0xffff
2367 : 0xffffffff));
2368 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2369 die_offset, NULL);
2371 break;
2373 case DW_OP_call_ref:
2374 case DW_OP_GNU_variable_value:
2376 char label[MAX_ARTIFICIAL_LABEL_BYTES
2377 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2378 gcc_assert (val1->val_class == dw_val_class_die_ref);
2379 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2380 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2382 break;
2384 case DW_OP_implicit_pointer:
2385 case DW_OP_GNU_implicit_pointer:
2387 char label[MAX_ARTIFICIAL_LABEL_BYTES
2388 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2389 gcc_assert (val1->val_class == dw_val_class_die_ref);
2390 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2391 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2392 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2394 break;
2396 case DW_OP_entry_value:
2397 case DW_OP_GNU_entry_value:
2398 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2399 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2400 break;
2402 case DW_OP_const_type:
2403 case DW_OP_GNU_const_type:
2405 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2406 gcc_assert (o);
2407 dw2_asm_output_data_uleb128 (o, NULL);
2408 switch (val2->val_class)
2410 case dw_val_class_const:
2411 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2412 dw2_asm_output_data (1, l, NULL);
2413 dw2_asm_output_data (l, val2->v.val_int, NULL);
2414 break;
2415 case dw_val_class_vec:
2417 unsigned int elt_size = val2->v.val_vec.elt_size;
2418 unsigned int len = val2->v.val_vec.length;
2419 unsigned int i;
2420 unsigned char *p;
2422 l = len * elt_size;
2423 dw2_asm_output_data (1, l, NULL);
2424 if (elt_size > sizeof (HOST_WIDE_INT))
2426 elt_size /= 2;
2427 len *= 2;
2429 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2430 i < len;
2431 i++, p += elt_size)
2432 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2433 "fp or vector constant word %u", i);
2435 break;
2436 case dw_val_class_const_double:
2438 unsigned HOST_WIDE_INT first, second;
2439 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2441 dw2_asm_output_data (1, 2 * l, NULL);
2442 if (WORDS_BIG_ENDIAN)
2444 first = val2->v.val_double.high;
2445 second = val2->v.val_double.low;
2447 else
2449 first = val2->v.val_double.low;
2450 second = val2->v.val_double.high;
2452 dw2_asm_output_data (l, first, NULL);
2453 dw2_asm_output_data (l, second, NULL);
2455 break;
2456 case dw_val_class_wide_int:
2458 int i;
2459 int len = get_full_len (*val2->v.val_wide);
2460 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2462 dw2_asm_output_data (1, len * l, NULL);
2463 if (WORDS_BIG_ENDIAN)
2464 for (i = len - 1; i >= 0; --i)
2465 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2466 else
2467 for (i = 0; i < len; ++i)
2468 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2470 break;
2471 default:
2472 gcc_unreachable ();
2475 break;
2476 case DW_OP_regval_type:
2477 case DW_OP_GNU_regval_type:
2479 unsigned r = val1->v.val_unsigned;
2480 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2481 gcc_assert (o);
2482 if (for_eh_or_skip >= 0)
2484 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2485 gcc_assert (size_of_uleb128 (r)
2486 == size_of_uleb128 (val1->v.val_unsigned));
2488 dw2_asm_output_data_uleb128 (r, NULL);
2489 dw2_asm_output_data_uleb128 (o, NULL);
2491 break;
2492 case DW_OP_deref_type:
2493 case DW_OP_GNU_deref_type:
2495 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2496 gcc_assert (o);
2497 dw2_asm_output_data (1, val1->v.val_int, NULL);
2498 dw2_asm_output_data_uleb128 (o, NULL);
2500 break;
2501 case DW_OP_convert:
2502 case DW_OP_reinterpret:
2503 case DW_OP_GNU_convert:
2504 case DW_OP_GNU_reinterpret:
2505 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2506 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2507 else
2509 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2510 gcc_assert (o);
2511 dw2_asm_output_data_uleb128 (o, NULL);
2513 break;
2515 case DW_OP_GNU_parameter_ref:
2517 unsigned long o;
2518 gcc_assert (val1->val_class == dw_val_class_die_ref);
2519 o = get_ref_die_offset (val1->v.val_die_ref.die);
2520 dw2_asm_output_data (4, o, NULL);
2522 break;
2524 default:
2525 /* Other codes have no operands. */
2526 break;
2530 /* Output a sequence of location operations.
2531 The for_eh_or_skip parameter controls whether register numbers are
2532 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2533 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2534 info). This should be suppressed for the cases that have not been converted
2535 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2537 void
2538 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2540 for (; loc != NULL; loc = loc->dw_loc_next)
2542 enum dwarf_location_atom opc = loc->dw_loc_opc;
2543 /* Output the opcode. */
2544 if (for_eh_or_skip >= 0
2545 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2547 unsigned r = (opc - DW_OP_breg0);
2548 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2549 gcc_assert (r <= 31);
2550 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2552 else if (for_eh_or_skip >= 0
2553 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2555 unsigned r = (opc - DW_OP_reg0);
2556 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2557 gcc_assert (r <= 31);
2558 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2561 dw2_asm_output_data (1, opc,
2562 "%s", dwarf_stack_op_name (opc));
2564 /* Output the operand(s) (if any). */
2565 output_loc_operands (loc, for_eh_or_skip);
2569 /* Output location description stack opcode's operands (if any).
2570 The output is single bytes on a line, suitable for .cfi_escape. */
2572 static void
2573 output_loc_operands_raw (dw_loc_descr_ref loc)
2575 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2576 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2578 switch (loc->dw_loc_opc)
2580 case DW_OP_addr:
2581 case DW_OP_GNU_addr_index:
2582 case DW_OP_addrx:
2583 case DW_OP_GNU_const_index:
2584 case DW_OP_constx:
2585 case DW_OP_implicit_value:
2586 /* We cannot output addresses in .cfi_escape, only bytes. */
2587 gcc_unreachable ();
2589 case DW_OP_const1u:
2590 case DW_OP_const1s:
2591 case DW_OP_pick:
2592 case DW_OP_deref_size:
2593 case DW_OP_xderef_size:
2594 fputc (',', asm_out_file);
2595 dw2_asm_output_data_raw (1, val1->v.val_int);
2596 break;
2598 case DW_OP_const2u:
2599 case DW_OP_const2s:
2600 fputc (',', asm_out_file);
2601 dw2_asm_output_data_raw (2, val1->v.val_int);
2602 break;
2604 case DW_OP_const4u:
2605 case DW_OP_const4s:
2606 fputc (',', asm_out_file);
2607 dw2_asm_output_data_raw (4, val1->v.val_int);
2608 break;
2610 case DW_OP_const8u:
2611 case DW_OP_const8s:
2612 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2613 fputc (',', asm_out_file);
2614 dw2_asm_output_data_raw (8, val1->v.val_int);
2615 break;
2617 case DW_OP_skip:
2618 case DW_OP_bra:
2620 int offset;
2622 gcc_assert (val1->val_class == dw_val_class_loc);
2623 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2625 fputc (',', asm_out_file);
2626 dw2_asm_output_data_raw (2, offset);
2628 break;
2630 case DW_OP_regx:
2632 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2633 gcc_assert (size_of_uleb128 (r)
2634 == size_of_uleb128 (val1->v.val_unsigned));
2635 fputc (',', asm_out_file);
2636 dw2_asm_output_data_uleb128_raw (r);
2638 break;
2640 case DW_OP_constu:
2641 case DW_OP_plus_uconst:
2642 case DW_OP_piece:
2643 fputc (',', asm_out_file);
2644 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2645 break;
2647 case DW_OP_bit_piece:
2648 fputc (',', asm_out_file);
2649 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2650 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2651 break;
2653 case DW_OP_consts:
2654 case DW_OP_breg0:
2655 case DW_OP_breg1:
2656 case DW_OP_breg2:
2657 case DW_OP_breg3:
2658 case DW_OP_breg4:
2659 case DW_OP_breg5:
2660 case DW_OP_breg6:
2661 case DW_OP_breg7:
2662 case DW_OP_breg8:
2663 case DW_OP_breg9:
2664 case DW_OP_breg10:
2665 case DW_OP_breg11:
2666 case DW_OP_breg12:
2667 case DW_OP_breg13:
2668 case DW_OP_breg14:
2669 case DW_OP_breg15:
2670 case DW_OP_breg16:
2671 case DW_OP_breg17:
2672 case DW_OP_breg18:
2673 case DW_OP_breg19:
2674 case DW_OP_breg20:
2675 case DW_OP_breg21:
2676 case DW_OP_breg22:
2677 case DW_OP_breg23:
2678 case DW_OP_breg24:
2679 case DW_OP_breg25:
2680 case DW_OP_breg26:
2681 case DW_OP_breg27:
2682 case DW_OP_breg28:
2683 case DW_OP_breg29:
2684 case DW_OP_breg30:
2685 case DW_OP_breg31:
2686 case DW_OP_fbreg:
2687 fputc (',', asm_out_file);
2688 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2689 break;
2691 case DW_OP_bregx:
2693 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2694 gcc_assert (size_of_uleb128 (r)
2695 == size_of_uleb128 (val1->v.val_unsigned));
2696 fputc (',', asm_out_file);
2697 dw2_asm_output_data_uleb128_raw (r);
2698 fputc (',', asm_out_file);
2699 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2701 break;
2703 case DW_OP_implicit_pointer:
2704 case DW_OP_entry_value:
2705 case DW_OP_const_type:
2706 case DW_OP_regval_type:
2707 case DW_OP_deref_type:
2708 case DW_OP_convert:
2709 case DW_OP_reinterpret:
2710 case DW_OP_GNU_implicit_pointer:
2711 case DW_OP_GNU_entry_value:
2712 case DW_OP_GNU_const_type:
2713 case DW_OP_GNU_regval_type:
2714 case DW_OP_GNU_deref_type:
2715 case DW_OP_GNU_convert:
2716 case DW_OP_GNU_reinterpret:
2717 case DW_OP_GNU_parameter_ref:
2718 gcc_unreachable ();
2719 break;
2721 default:
2722 /* Other codes have no operands. */
2723 break;
2727 void
2728 output_loc_sequence_raw (dw_loc_descr_ref loc)
2730 while (1)
2732 enum dwarf_location_atom opc = loc->dw_loc_opc;
2733 /* Output the opcode. */
2734 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2736 unsigned r = (opc - DW_OP_breg0);
2737 r = DWARF2_FRAME_REG_OUT (r, 1);
2738 gcc_assert (r <= 31);
2739 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2741 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2743 unsigned r = (opc - DW_OP_reg0);
2744 r = DWARF2_FRAME_REG_OUT (r, 1);
2745 gcc_assert (r <= 31);
2746 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2748 /* Output the opcode. */
2749 fprintf (asm_out_file, "%#x", opc);
2750 output_loc_operands_raw (loc);
2752 if (!loc->dw_loc_next)
2753 break;
2754 loc = loc->dw_loc_next;
2756 fputc (',', asm_out_file);
2760 static void
2761 build_breg_loc (struct dw_loc_descr_node **head, unsigned int regno)
2763 if (regno <= 31)
2764 add_loc_descr (head, new_loc_descr ((enum dwarf_location_atom)
2765 (DW_OP_breg0 + regno), 0, 0));
2766 else
2767 add_loc_descr (head, new_loc_descr (DW_OP_bregx, regno, 0));
2770 /* Build a dwarf location for a cfa_reg spanning multiple
2771 consecutive registers. */
2773 struct dw_loc_descr_node *
2774 build_span_loc (struct cfa_reg reg)
2776 struct dw_loc_descr_node *head = NULL;
2778 gcc_assert (reg.span_width > 0);
2779 gcc_assert (reg.span > 1);
2781 /* Start from the highest number register as it goes in the upper bits. */
2782 unsigned int regno = reg.reg + reg.span - 1;
2783 build_breg_loc (&head, regno);
2785 /* Deal with the remaining registers in the span. */
2786 for (int i = reg.span - 2; i >= 0; i--)
2788 add_loc_descr (&head, int_loc_descriptor (reg.span_width * 8));
2789 add_loc_descr (&head, new_loc_descr (DW_OP_shl, 0, 0));
2790 regno--;
2791 build_breg_loc (&head, regno);
2792 add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
2794 return head;
2797 /* This function builds a dwarf location descriptor sequence from a
2798 dw_cfa_location, adding the given OFFSET to the result of the
2799 expression. */
2801 struct dw_loc_descr_node *
2802 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2804 struct dw_loc_descr_node *head, *tmp;
2806 offset += cfa->offset;
2808 if (cfa->reg.span > 1)
2810 head = build_span_loc (cfa->reg);
2812 if (maybe_ne (offset, 0))
2813 loc_descr_plus_const (&head, offset);
2815 else if (cfa->indirect)
2817 head = new_reg_loc_descr (cfa->reg.reg, cfa->base_offset);
2818 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2819 head->dw_loc_oprnd1.val_entry = NULL;
2820 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2821 add_loc_descr (&head, tmp);
2822 loc_descr_plus_const (&head, offset);
2824 else
2825 head = new_reg_loc_descr (cfa->reg.reg, offset);
2827 return head;
2830 /* This function builds a dwarf location descriptor sequence for
2831 the address at OFFSET from the CFA when stack is aligned to
2832 ALIGNMENT byte. */
2834 struct dw_loc_descr_node *
2835 build_cfa_aligned_loc (dw_cfa_location *cfa,
2836 poly_int64 offset, HOST_WIDE_INT alignment)
2838 struct dw_loc_descr_node *head;
2839 unsigned int dwarf_fp
2840 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2842 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2843 if (cfa->reg.reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2845 head = new_reg_loc_descr (dwarf_fp, 0);
2846 add_loc_descr (&head, int_loc_descriptor (alignment));
2847 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2848 loc_descr_plus_const (&head, offset);
2850 else
2851 head = new_reg_loc_descr (dwarf_fp, offset);
2852 return head;
2855 /* And now, the support for symbolic debugging information. */
2857 /* .debug_str support. */
2859 static void dwarf2out_init (const char *);
2860 static void dwarf2out_finish (const char *);
2861 static void dwarf2out_early_finish (const char *);
2862 static void dwarf2out_assembly_start (void);
2863 static void dwarf2out_define (unsigned int, const char *);
2864 static void dwarf2out_undef (unsigned int, const char *);
2865 static void dwarf2out_start_source_file (unsigned, const char *);
2866 static void dwarf2out_end_source_file (unsigned);
2867 static void dwarf2out_function_decl (tree);
2868 static void dwarf2out_begin_block (unsigned, unsigned, tree);
2869 static void dwarf2out_end_block (unsigned, unsigned);
2870 static bool dwarf2out_ignore_block (const_tree);
2871 static void dwarf2out_set_ignored_loc (unsigned, unsigned, const char *);
2872 static void dwarf2out_early_global_decl (tree);
2873 static void dwarf2out_late_global_decl (tree);
2874 static void dwarf2out_type_decl (tree, int);
2875 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2876 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2877 dw_die_ref);
2878 static void dwarf2out_abstract_function (tree);
2879 static void dwarf2out_var_location (rtx_insn *);
2880 static void dwarf2out_inline_entry (tree);
2881 static void dwarf2out_size_function (tree);
2882 static void dwarf2out_begin_function (tree);
2883 static void dwarf2out_end_function (unsigned int);
2884 static void dwarf2out_register_main_translation_unit (tree unit);
2885 static void dwarf2out_set_name (tree, tree);
2886 static void dwarf2out_register_external_die (tree decl, const char *sym,
2887 unsigned HOST_WIDE_INT off);
2888 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2889 unsigned HOST_WIDE_INT *off);
2891 /* The debug hooks structure. */
2893 const struct gcc_debug_hooks dwarf2_debug_hooks =
2895 dwarf2out_init,
2896 dwarf2out_finish,
2897 dwarf2out_early_finish,
2898 dwarf2out_assembly_start,
2899 dwarf2out_define,
2900 dwarf2out_undef,
2901 dwarf2out_start_source_file,
2902 dwarf2out_end_source_file,
2903 dwarf2out_begin_block,
2904 dwarf2out_end_block,
2905 dwarf2out_ignore_block,
2906 dwarf2out_source_line,
2907 dwarf2out_set_ignored_loc,
2908 dwarf2out_begin_prologue,
2909 #if VMS_DEBUGGING_INFO
2910 dwarf2out_vms_end_prologue,
2911 dwarf2out_vms_begin_epilogue,
2912 #else
2913 debug_nothing_int_charstar,
2914 debug_nothing_int_charstar,
2915 #endif
2916 dwarf2out_end_epilogue,
2917 dwarf2out_begin_function,
2918 dwarf2out_end_function, /* end_function */
2919 dwarf2out_register_main_translation_unit,
2920 dwarf2out_function_decl, /* function_decl */
2921 dwarf2out_early_global_decl,
2922 dwarf2out_late_global_decl,
2923 dwarf2out_type_decl, /* type_decl */
2924 dwarf2out_imported_module_or_decl,
2925 dwarf2out_die_ref_for_decl,
2926 dwarf2out_register_external_die,
2927 debug_nothing_tree, /* deferred_inline_function */
2928 /* The DWARF 2 backend tries to reduce debugging bloat by not
2929 emitting the abstract description of inline functions until
2930 something tries to reference them. */
2931 dwarf2out_abstract_function, /* outlining_inline_function */
2932 debug_nothing_rtx_code_label, /* label */
2933 debug_nothing_int, /* handle_pch */
2934 dwarf2out_var_location,
2935 dwarf2out_inline_entry, /* inline_entry */
2936 dwarf2out_size_function, /* size_function */
2937 dwarf2out_switch_text_section,
2938 dwarf2out_set_name,
2939 1, /* start_end_main_source_file */
2940 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2943 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2945 dwarf2out_init,
2946 debug_nothing_charstar,
2947 debug_nothing_charstar,
2948 dwarf2out_assembly_start,
2949 debug_nothing_int_charstar,
2950 debug_nothing_int_charstar,
2951 debug_nothing_int_charstar,
2952 debug_nothing_int,
2953 debug_nothing_int_int_tree, /* begin_block */
2954 debug_nothing_int_int, /* end_block */
2955 debug_true_const_tree, /* ignore_block */
2956 dwarf2out_source_line, /* source_line */
2957 debug_nothing_int_int_charstar, /* set_ignored_loc */
2958 debug_nothing_int_int_charstar, /* begin_prologue */
2959 debug_nothing_int_charstar, /* end_prologue */
2960 debug_nothing_int_charstar, /* begin_epilogue */
2961 debug_nothing_int_charstar, /* end_epilogue */
2962 debug_nothing_tree, /* begin_function */
2963 debug_nothing_int, /* end_function */
2964 debug_nothing_tree, /* register_main_translation_unit */
2965 debug_nothing_tree, /* function_decl */
2966 debug_nothing_tree, /* early_global_decl */
2967 debug_nothing_tree, /* late_global_decl */
2968 debug_nothing_tree_int, /* type_decl */
2969 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2970 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2971 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2972 debug_nothing_tree, /* deferred_inline_function */
2973 debug_nothing_tree, /* outlining_inline_function */
2974 debug_nothing_rtx_code_label, /* label */
2975 debug_nothing_int, /* handle_pch */
2976 debug_nothing_rtx_insn, /* var_location */
2977 debug_nothing_tree, /* inline_entry */
2978 debug_nothing_tree, /* size_function */
2979 debug_nothing_void, /* switch_text_section */
2980 debug_nothing_tree_tree, /* set_name */
2981 0, /* start_end_main_source_file */
2982 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2985 /* NOTE: In the comments in this file, many references are made to
2986 "Debugging Information Entries". This term is abbreviated as `DIE'
2987 throughout the remainder of this file. */
2989 /* An internal representation of the DWARF output is built, and then
2990 walked to generate the DWARF debugging info. The walk of the internal
2991 representation is done after the entire program has been compiled.
2992 The types below are used to describe the internal representation. */
2994 /* Whether to put type DIEs into their own section .debug_types instead
2995 of making them part of the .debug_info section. Only supported for
2996 Dwarf V4 or higher and the user didn't disable them through
2997 -fno-debug-types-section. It is more efficient to put them in a
2998 separate comdat sections since the linker will then be able to
2999 remove duplicates. But not all tools support .debug_types sections
3000 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
3001 it is DW_UT_type unit type in .debug_info section. For late LTO
3002 debug there should be almost no types emitted so avoid enabling
3003 -fdebug-types-section there. */
3005 #define use_debug_types (dwarf_version >= 4 \
3006 && flag_debug_types_section \
3007 && !in_lto_p)
3009 /* Various DIE's use offsets relative to the beginning of the
3010 .debug_info section to refer to each other. */
3012 typedef long int dw_offset;
3014 struct comdat_type_node;
3016 /* The entries in the line_info table more-or-less mirror the opcodes
3017 that are used in the real dwarf line table. Arrays of these entries
3018 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
3019 supported. */
3021 enum dw_line_info_opcode {
3022 /* Emit DW_LNE_set_address; the operand is the label index. */
3023 LI_set_address,
3025 /* Emit a row to the matrix with the given line. This may be done
3026 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3027 special opcodes. */
3028 LI_set_line,
3030 /* Emit a DW_LNS_set_file. */
3031 LI_set_file,
3033 /* Emit a DW_LNS_set_column. */
3034 LI_set_column,
3036 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
3037 LI_negate_stmt,
3039 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
3040 LI_set_prologue_end,
3041 LI_set_epilogue_begin,
3043 /* Emit a DW_LNE_set_discriminator. */
3044 LI_set_discriminator,
3046 /* Output a Fixed Advance PC; the target PC is the label index; the
3047 base PC is the previous LI_adv_address or LI_set_address entry.
3048 We only use this when emitting debug views without assembler
3049 support, at explicit user request. Ideally, we should only use
3050 it when the offset might be zero but we can't tell: it's the only
3051 way to maybe change the PC without resetting the view number. */
3052 LI_adv_address
3055 typedef struct GTY(()) dw_line_info_struct {
3056 enum dw_line_info_opcode opcode;
3057 unsigned int val;
3058 } dw_line_info_entry;
3061 struct GTY(()) dw_line_info_table {
3062 /* The label that marks the end of this section. */
3063 const char *end_label;
3065 /* The values for the last row of the matrix, as collected in the table.
3066 These are used to minimize the changes to the next row. */
3067 unsigned int file_num;
3068 unsigned int line_num;
3069 unsigned int column_num;
3070 int discrim_num;
3071 bool is_stmt;
3072 bool in_use;
3074 /* This denotes the NEXT view number.
3076 If it is 0, it is known that the NEXT view will be the first view
3077 at the given PC.
3079 If it is -1, we're forcing the view number to be reset, e.g. at a
3080 function entry.
3082 The meaning of other nonzero values depends on whether we're
3083 computing views internally or leaving it for the assembler to do
3084 so. If we're emitting them internally, view denotes the view
3085 number since the last known advance of PC. If we're leaving it
3086 for the assembler, it denotes the LVU label number that we're
3087 going to ask the assembler to assign. */
3088 var_loc_view view;
3090 /* This counts the number of symbolic views emitted in this table
3091 since the latest view reset. Its max value, over all tables,
3092 sets symview_upper_bound. */
3093 var_loc_view symviews_since_reset;
3095 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3096 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3097 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3098 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3100 vec<dw_line_info_entry, va_gc> *entries;
3103 /* This is an upper bound for view numbers that the assembler may
3104 assign to symbolic views output in this translation. It is used to
3105 decide how big a field to use to represent view numbers in
3106 symview-classed attributes. */
3108 static var_loc_view symview_upper_bound;
3110 /* If we're keep track of location views and their reset points, and
3111 INSN is a reset point (i.e., it necessarily advances the PC), mark
3112 the next view in TABLE as reset. */
3114 static void
3115 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3117 if (!debug_internal_reset_location_views)
3118 return;
3120 /* Maybe turn (part of?) this test into a default target hook. */
3121 int reset = 0;
3123 if (targetm.reset_location_view)
3124 reset = targetm.reset_location_view (insn);
3126 if (reset)
3128 else if (JUMP_TABLE_DATA_P (insn))
3129 reset = 1;
3130 else if (GET_CODE (insn) == USE
3131 || GET_CODE (insn) == CLOBBER
3132 || GET_CODE (insn) == ASM_INPUT
3133 || asm_noperands (insn) >= 0)
3135 else if (get_attr_min_length (insn) > 0)
3136 reset = 1;
3138 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3139 RESET_NEXT_VIEW (table->view);
3142 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3143 The children of each node form a circular list linked by
3144 die_sib. die_child points to the node *before* the "first" child node. */
3146 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3147 union die_symbol_or_type_node
3149 const char * GTY ((tag ("0"))) die_symbol;
3150 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3152 GTY ((desc ("%0.comdat_type_p"))) die_id;
3153 vec<dw_attr_node, va_gc> *die_attr;
3154 dw_die_ref die_parent;
3155 dw_die_ref die_child;
3156 dw_die_ref die_sib;
3157 dw_die_ref die_definition; /* ref from a specification to its definition */
3158 dw_offset die_offset;
3159 unsigned long die_abbrev;
3160 int die_mark;
3161 unsigned int decl_id;
3162 enum dwarf_tag die_tag;
3163 /* Die is used and must not be pruned as unused. */
3164 BOOL_BITFIELD die_perennial_p : 1;
3165 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3166 /* For an external ref to die_symbol if die_offset contains an extra
3167 offset to that symbol. */
3168 BOOL_BITFIELD with_offset : 1;
3169 /* Whether this DIE was removed from the DIE tree, for example via
3170 prune_unused_types. We don't consider those present from the
3171 DIE lookup routines. */
3172 BOOL_BITFIELD removed : 1;
3173 /* Lots of spare bits. */
3175 die_node;
3177 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3178 static bool early_dwarf;
3179 static bool early_dwarf_finished;
3180 class set_early_dwarf {
3181 public:
3182 bool saved;
3183 set_early_dwarf () : saved(early_dwarf)
3185 gcc_assert (! early_dwarf_finished);
3186 early_dwarf = true;
3188 ~set_early_dwarf () { early_dwarf = saved; }
3191 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3192 #define FOR_EACH_CHILD(die, c, expr) do { \
3193 c = die->die_child; \
3194 if (c) do { \
3195 c = c->die_sib; \
3196 expr; \
3197 } while (c != die->die_child); \
3198 } while (0)
3200 /* The pubname structure */
3202 typedef struct GTY(()) pubname_struct {
3203 dw_die_ref die;
3204 const char *name;
3206 pubname_entry;
3209 struct GTY(()) dw_ranges {
3210 const char *label;
3211 /* If this is positive, it's a block number, otherwise it's a
3212 bitwise-negated index into dw_ranges_by_label. */
3213 int num;
3214 /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3215 into .debug_rnglists section rather than .debug_rnglists.dwo
3216 for -gsplit-dwarf and DWARF >= 5. */
3217 #define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3218 /* Index for the range list for DW_FORM_rnglistx. */
3219 unsigned int idx : 31;
3220 /* True if this range might be possibly in a different section
3221 from previous entry. */
3222 unsigned int maybe_new_sec : 1;
3223 addr_table_entry *begin_entry;
3224 addr_table_entry *end_entry;
3227 /* A structure to hold a macinfo entry. */
3229 typedef struct GTY(()) macinfo_struct {
3230 unsigned char code;
3231 unsigned HOST_WIDE_INT lineno;
3232 const char *info;
3234 macinfo_entry;
3237 struct GTY(()) dw_ranges_by_label {
3238 const char *begin;
3239 const char *end;
3242 /* The comdat type node structure. */
3243 struct GTY(()) comdat_type_node
3245 dw_die_ref root_die;
3246 dw_die_ref type_die;
3247 dw_die_ref skeleton_die;
3248 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3249 comdat_type_node *next;
3252 /* A list of DIEs for which we can't determine ancestry (parent_die
3253 field) just yet. Later in dwarf2out_finish we will fill in the
3254 missing bits. */
3255 typedef struct GTY(()) limbo_die_struct {
3256 dw_die_ref die;
3257 /* The tree for which this DIE was created. We use this to
3258 determine ancestry later. */
3259 tree created_for;
3260 struct limbo_die_struct *next;
3262 limbo_die_node;
3264 typedef struct skeleton_chain_struct
3266 dw_die_ref old_die;
3267 dw_die_ref new_die;
3268 struct skeleton_chain_struct *parent;
3270 skeleton_chain_node;
3272 /* Define a macro which returns nonzero for a TYPE_DECL which was
3273 implicitly generated for a type.
3275 Note that, unlike the C front-end (which generates a NULL named
3276 TYPE_DECL node for each complete tagged type, each array type,
3277 and each function type node created) the C++ front-end generates
3278 a _named_ TYPE_DECL node for each tagged type node created.
3279 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3280 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3281 front-end, but for each type, tagged or not. */
3283 #define TYPE_DECL_IS_STUB(decl) \
3284 (DECL_NAME (decl) == NULL_TREE \
3285 || (DECL_ARTIFICIAL (decl) \
3286 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3287 /* This is necessary for stub decls that \
3288 appear in nested inline functions. */ \
3289 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3290 && (decl_ultimate_origin (decl) \
3291 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3293 /* Information concerning the compilation unit's programming
3294 language, and compiler version. */
3296 /* Fixed size portion of the DWARF compilation unit header. */
3297 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3298 (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size \
3299 + (dwarf_version >= 5 ? 4 : 3))
3301 /* Fixed size portion of the DWARF comdat type unit header. */
3302 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3303 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3304 + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3306 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3307 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3308 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3310 /* Fixed size portion of public names info. */
3311 #define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3313 /* Fixed size portion of the address range info. */
3314 #define DWARF_ARANGES_HEADER_SIZE \
3315 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3316 DWARF2_ADDR_SIZE * 2) \
3317 - DWARF_INITIAL_LENGTH_SIZE)
3319 /* Size of padding portion in the address range info. It must be
3320 aligned to twice the pointer size. */
3321 #define DWARF_ARANGES_PAD_SIZE \
3322 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3323 DWARF2_ADDR_SIZE * 2) \
3324 - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3326 /* Use assembler line directives if available. */
3327 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3328 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3329 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3330 #else
3331 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3332 #endif
3333 #endif
3335 /* Use assembler views in line directives if available. */
3336 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3337 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3338 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3339 #else
3340 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3341 #endif
3342 #endif
3344 /* Return true if GCC configure detected assembler support for .loc. */
3346 bool
3347 dwarf2out_default_as_loc_support (void)
3349 return DWARF2_ASM_LINE_DEBUG_INFO;
3350 #if (GCC_VERSION >= 3000)
3351 # undef DWARF2_ASM_LINE_DEBUG_INFO
3352 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3353 #endif
3356 /* Return true if GCC configure detected assembler support for views
3357 in .loc directives. */
3359 bool
3360 dwarf2out_default_as_locview_support (void)
3362 return DWARF2_ASM_VIEW_DEBUG_INFO;
3363 #if (GCC_VERSION >= 3000)
3364 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3365 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3366 #endif
3369 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3370 view computation, and it refers to a view identifier for which we
3371 will not emit a label because it is known to map to a view number
3372 zero. We won't allocate the bitmap if we're not using assembler
3373 support for location views, but we have to make the variable
3374 visible for GGC and for code that will be optimized out for lack of
3375 support but that's still parsed and compiled. We could abstract it
3376 out with macros, but it's not worth it. */
3377 static GTY(()) bitmap zero_view_p;
3379 /* Evaluate to TRUE iff N is known to identify the first location view
3380 at its PC. When not using assembler location view computation,
3381 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3382 and views label numbers recorded in it are the ones known to be
3383 zero. */
3384 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3385 || (N) == (var_loc_view)-1 \
3386 || (zero_view_p \
3387 && bitmap_bit_p (zero_view_p, (N))))
3389 /* Return true iff we're to emit .loc directives for the assembler to
3390 generate line number sections.
3392 When we're not emitting views, all we need from the assembler is
3393 support for .loc directives.
3395 If we are emitting views, we can only use the assembler's .loc
3396 support if it also supports views.
3398 When the compiler is emitting the line number programs and
3399 computing view numbers itself, it resets view numbers at known PC
3400 changes and counts from that, and then it emits view numbers as
3401 literal constants in locviewlists. There are cases in which the
3402 compiler is not sure about PC changes, e.g. when extra alignment is
3403 requested for a label. In these cases, the compiler may not reset
3404 the view counter, and the potential PC advance in the line number
3405 program will use an opcode that does not reset the view counter
3406 even if the PC actually changes, so that compiler and debug info
3407 consumer can keep view numbers in sync.
3409 When the compiler defers view computation to the assembler, it
3410 emits symbolic view numbers in locviewlists, with the exception of
3411 views known to be zero (forced resets, or reset after
3412 compiler-visible PC changes): instead of emitting symbols for
3413 these, we emit literal zero and assert the assembler agrees with
3414 the compiler's assessment. We could use symbolic views everywhere,
3415 instead of special-casing zero views, but then we'd be unable to
3416 optimize out locviewlists that contain only zeros. */
3418 static bool
3419 output_asm_line_debug_info (void)
3421 return (dwarf2out_as_loc_support
3422 && (dwarf2out_as_locview_support
3423 || !debug_variable_location_views));
3426 static bool asm_outputs_debug_line_str (void);
3428 /* Minimum line offset in a special line info. opcode.
3429 This value was chosen to give a reasonable range of values. */
3430 #define DWARF_LINE_BASE -10
3432 /* First special line opcode - leave room for the standard opcodes. */
3433 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3435 /* Range of line offsets in a special line info. opcode. */
3436 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3438 /* Flag that indicates the initial value of the is_stmt_start flag.
3439 In the present implementation, we do not mark any lines as
3440 the beginning of a source statement, because that information
3441 is not made available by the GCC front-end. */
3442 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3444 /* Maximum number of operations per instruction bundle. */
3445 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3446 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3447 #endif
3449 /* This location is used by calc_die_sizes() to keep track
3450 the offset of each DIE within the .debug_info section. */
3451 static unsigned long next_die_offset;
3453 /* Record the root of the DIE's built for the current compilation unit. */
3454 static GTY(()) dw_die_ref single_comp_unit_die;
3456 /* A list of type DIEs that have been separated into comdat sections. */
3457 static GTY(()) comdat_type_node *comdat_type_list;
3459 /* A list of CU DIEs that have been separated. */
3460 static GTY(()) limbo_die_node *cu_die_list;
3462 /* A list of DIEs with a NULL parent waiting to be relocated. */
3463 static GTY(()) limbo_die_node *limbo_die_list;
3465 /* A list of DIEs for which we may have to generate
3466 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3467 static GTY(()) limbo_die_node *deferred_asm_name;
3469 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3471 typedef const char *compare_type;
3473 static hashval_t hash (dwarf_file_data *);
3474 static bool equal (dwarf_file_data *, const char *);
3477 /* Filenames referenced by this compilation unit. */
3478 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3480 struct decl_die_hasher : ggc_ptr_hash<die_node>
3482 typedef tree compare_type;
3484 static hashval_t hash (die_node *);
3485 static bool equal (die_node *, tree);
3487 /* A hash table of references to DIE's that describe declarations.
3488 The key is a DECL_UID() which is a unique number identifying each decl. */
3489 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3491 struct GTY ((for_user)) variable_value_struct {
3492 unsigned int decl_id;
3493 vec<dw_die_ref, va_gc> *dies;
3496 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3498 typedef tree compare_type;
3500 static hashval_t hash (variable_value_struct *);
3501 static bool equal (variable_value_struct *, tree);
3503 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3504 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3505 DECL_CONTEXT of the referenced VAR_DECLs. */
3506 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3508 struct block_die_hasher : ggc_ptr_hash<die_struct>
3510 static hashval_t hash (die_struct *);
3511 static bool equal (die_struct *, die_struct *);
3514 /* A hash table of references to DIE's that describe COMMON blocks.
3515 The key is DECL_UID() ^ die_parent. */
3516 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3518 typedef struct GTY(()) die_arg_entry_struct {
3519 dw_die_ref die;
3520 tree arg;
3521 } die_arg_entry;
3524 /* Node of the variable location list. */
3525 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3526 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3527 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3528 in mode of the EXPR_LIST node and first EXPR_LIST operand
3529 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3530 location or NULL for padding. For larger bitsizes,
3531 mode is 0 and first operand is a CONCAT with bitsize
3532 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3533 NULL as second operand. */
3534 rtx GTY (()) loc;
3535 const char * GTY (()) label;
3536 struct var_loc_node * GTY (()) next;
3537 var_loc_view view;
3540 /* Variable location list. */
3541 struct GTY ((for_user)) var_loc_list_def {
3542 struct var_loc_node * GTY (()) first;
3544 /* Pointer to the last but one or last element of the
3545 chained list. If the list is empty, both first and
3546 last are NULL, if the list contains just one node
3547 or the last node certainly is not redundant, it points
3548 to the last node, otherwise points to the last but one.
3549 Do not mark it for GC because it is marked through the chain. */
3550 struct var_loc_node * GTY ((skip ("%h"))) last;
3552 /* Pointer to the last element before section switch,
3553 if NULL, either sections weren't switched or first
3554 is after section switch. */
3555 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3557 /* DECL_UID of the variable decl. */
3558 unsigned int decl_id;
3560 typedef struct var_loc_list_def var_loc_list;
3562 /* Call argument location list. */
3563 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3564 rtx_insn * GTY (()) call_insn;
3565 const char * GTY (()) label;
3566 tree GTY (()) block;
3567 bool tail_call_p;
3568 rtx GTY (()) symbol_ref;
3569 struct call_arg_loc_node * GTY (()) next;
3573 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3575 typedef const_tree compare_type;
3577 static hashval_t hash (var_loc_list *);
3578 static bool equal (var_loc_list *, const_tree);
3581 /* Table of decl location linked lists. */
3582 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3584 /* Head and tail of call_arg_loc chain. */
3585 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3586 static struct call_arg_loc_node *call_arg_loc_last;
3588 /* Number of call sites in the current function. */
3589 static int call_site_count = -1;
3590 /* Number of tail call sites in the current function. */
3591 static int tail_call_site_count = -1;
3593 /* A cached location list. */
3594 struct GTY ((for_user)) cached_dw_loc_list_def {
3595 /* The DECL_UID of the decl that this entry describes. */
3596 unsigned int decl_id;
3598 /* The cached location list. */
3599 dw_loc_list_ref loc_list;
3601 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3603 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3606 typedef const_tree compare_type;
3608 static hashval_t hash (cached_dw_loc_list *);
3609 static bool equal (cached_dw_loc_list *, const_tree);
3612 /* Table of cached location lists. */
3613 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3615 /* A vector of references to DIE's that are uniquely identified by their tag,
3616 presence/absence of children DIE's, and list of attribute/value pairs. */
3617 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3619 /* A hash map to remember the stack usage for DWARF procedures. The value
3620 stored is the stack size difference between before the DWARF procedure
3621 invokation and after it returned. In other words, for a DWARF procedure
3622 that consumes N stack slots and that pushes M ones, this stores M - N. */
3623 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3625 /* A global counter for generating labels for line number data. */
3626 static unsigned int line_info_label_num;
3628 /* The current table to which we should emit line number information
3629 for the current function. This will be set up at the beginning of
3630 assembly for the function. */
3631 static GTY(()) dw_line_info_table *cur_line_info_table;
3633 /* The two default tables of line number info. */
3634 static GTY(()) dw_line_info_table *text_section_line_info;
3635 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3637 /* The set of all non-default tables of line number info. */
3638 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3640 /* A flag to tell pubnames/types export if there is an info section to
3641 refer to. */
3642 static bool info_section_emitted;
3644 /* A pointer to the base of a table that contains a list of publicly
3645 accessible names. */
3646 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3648 /* A pointer to the base of a table that contains a list of publicly
3649 accessible types. */
3650 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3652 /* A pointer to the base of a table that contains a list of macro
3653 defines/undefines (and file start/end markers). */
3654 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3656 /* True if .debug_macinfo or .debug_macros section is going to be
3657 emitted. */
3658 #define have_macinfo \
3659 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3660 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3661 && !macinfo_table->is_empty ())
3663 /* Vector of dies for which we should generate .debug_ranges info. */
3664 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3666 /* Vector of pairs of labels referenced in ranges_table. */
3667 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3669 /* Whether we have location lists that need outputting */
3670 static GTY(()) bool have_location_lists;
3672 /* Unique label counter. */
3673 static GTY(()) unsigned int loclabel_num;
3675 /* Unique label counter for point-of-call tables. */
3676 static GTY(()) unsigned int poc_label_num;
3678 /* The last file entry emitted by maybe_emit_file(). */
3679 static GTY(()) struct dwarf_file_data * last_emitted_file;
3681 /* Number of internal labels generated by gen_internal_sym(). */
3682 static GTY(()) int label_num;
3684 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3686 /* Instances of generic types for which we need to generate debug
3687 info that describe their generic parameters and arguments. That
3688 generation needs to happen once all types are properly laid out so
3689 we do it at the end of compilation. */
3690 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3692 /* Offset from the "steady-state frame pointer" to the frame base,
3693 within the current function. */
3694 static poly_int64 frame_pointer_fb_offset;
3695 static bool frame_pointer_fb_offset_valid;
3697 static vec<dw_die_ref> base_types;
3699 /* Flags to represent a set of attribute classes for attributes that represent
3700 a scalar value (bounds, pointers, ...). */
3701 enum dw_scalar_form
3703 dw_scalar_form_constant = 0x01,
3704 dw_scalar_form_exprloc = 0x02,
3705 dw_scalar_form_reference = 0x04
3708 /* Forward declarations for functions defined in this file. */
3710 static bool is_pseudo_reg (const_rtx);
3711 static tree type_main_variant (tree);
3712 static bool is_tagged_type (const_tree);
3713 static const char *dwarf_tag_name (unsigned);
3714 static const char *dwarf_attr_name (unsigned);
3715 static const char *dwarf_form_name (unsigned);
3716 static tree decl_ultimate_origin (const_tree);
3717 static tree decl_class_context (tree);
3718 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3719 static inline unsigned int AT_index (dw_attr_node *);
3720 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3721 static inline unsigned AT_flag (dw_attr_node *);
3722 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3723 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3724 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3725 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3726 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3727 unsigned int, unsigned char *);
3728 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3729 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3730 static inline const char *AT_string (dw_attr_node *);
3731 static enum dwarf_form AT_string_form (dw_attr_node *);
3732 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3733 static void add_AT_specification (dw_die_ref, dw_die_ref);
3734 static inline dw_die_ref AT_ref (dw_attr_node *);
3735 static inline int AT_ref_external (dw_attr_node *);
3736 static inline void set_AT_ref_external (dw_attr_node *, int);
3737 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3738 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3739 dw_loc_list_ref);
3740 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3741 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3742 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3743 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3744 static void remove_addr_table_entry (addr_table_entry *);
3745 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3746 static inline rtx AT_addr (dw_attr_node *);
3747 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3748 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *,
3749 int = 0);
3750 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3751 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3752 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3753 unsigned long, bool);
3754 static inline const char *AT_lbl (dw_attr_node *);
3755 static const char *get_AT_low_pc (dw_die_ref);
3756 static bool is_c (void);
3757 static bool is_cxx (void);
3758 static bool is_cxx (const_tree);
3759 static bool is_fortran (void);
3760 static bool is_ada (void);
3761 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3762 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3763 static void add_child_die (dw_die_ref, dw_die_ref);
3764 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3765 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3766 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3767 static void equate_type_number_to_die (tree, dw_die_ref);
3768 static var_loc_list *lookup_decl_loc (const_tree);
3769 static void equate_decl_number_to_die (tree, dw_die_ref);
3770 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3771 static void print_spaces (FILE *);
3772 static void print_die (dw_die_ref, FILE *);
3773 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3774 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3775 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3776 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3777 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3778 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3779 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3780 struct md5_ctx *, int *);
3781 struct checksum_attributes;
3782 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3783 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3784 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3785 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3786 static bool same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3787 static bool same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3788 static bool same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3789 static bool same_die_p (dw_die_ref, dw_die_ref, int *);
3790 static bool is_type_die (dw_die_ref);
3791 static inline bool is_template_instantiation (dw_die_ref);
3792 static bool is_declaration_die (dw_die_ref);
3793 static bool should_move_die_to_comdat (dw_die_ref);
3794 static dw_die_ref clone_as_declaration (dw_die_ref);
3795 static dw_die_ref clone_die (dw_die_ref);
3796 static dw_die_ref clone_tree (dw_die_ref);
3797 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3798 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3799 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3800 static dw_die_ref generate_skeleton (dw_die_ref);
3801 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3802 dw_die_ref,
3803 dw_die_ref);
3804 static void break_out_comdat_types (dw_die_ref);
3805 static void copy_decls_for_unworthy_types (dw_die_ref);
3807 static void add_sibling_attributes (dw_die_ref);
3808 static void output_location_lists (dw_die_ref);
3809 static int constant_size (unsigned HOST_WIDE_INT);
3810 static unsigned long size_of_die (dw_die_ref);
3811 static void calc_die_sizes (dw_die_ref);
3812 static void calc_base_type_die_sizes (void);
3813 static void mark_dies (dw_die_ref);
3814 static void unmark_dies (dw_die_ref);
3815 static void unmark_all_dies (dw_die_ref);
3816 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3817 static unsigned long size_of_aranges (void);
3818 static enum dwarf_form value_format (dw_attr_node *);
3819 static void output_value_format (dw_attr_node *);
3820 static void output_abbrev_section (void);
3821 static void output_die_abbrevs (unsigned long, dw_die_ref);
3822 static void output_die (dw_die_ref);
3823 static void output_compilation_unit_header (enum dwarf_unit_type);
3824 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3825 static void output_comdat_type_unit (comdat_type_node *, bool);
3826 static const char *dwarf2_name (tree, int);
3827 static void add_pubname (tree, dw_die_ref);
3828 static void add_enumerator_pubname (const char *, dw_die_ref);
3829 static void add_pubname_string (const char *, dw_die_ref);
3830 static void add_pubtype (tree, dw_die_ref);
3831 static void output_pubnames (vec<pubname_entry, va_gc> *);
3832 static void output_aranges (void);
3833 static unsigned int add_ranges (const_tree, bool = false);
3834 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3835 bool *, bool);
3836 static void output_ranges (void);
3837 static dw_line_info_table *new_line_info_table (void);
3838 static void output_line_info (bool);
3839 static void output_file_names (void);
3840 static bool is_base_type (tree);
3841 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3842 static int decl_quals (const_tree);
3843 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3844 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3845 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3846 static unsigned int debugger_reg_number (const_rtx);
3847 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3848 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3849 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3850 enum var_init_status);
3851 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3852 enum var_init_status);
3853 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3854 enum var_init_status);
3855 static bool is_based_loc (const_rtx);
3856 static bool resolve_one_addr (rtx *);
3857 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3858 enum var_init_status);
3859 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3860 enum var_init_status);
3861 struct loc_descr_context;
3862 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3863 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3864 static dw_loc_list_ref loc_list_from_tree (tree, int,
3865 struct loc_descr_context *);
3866 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3867 struct loc_descr_context *);
3868 static tree field_type (const_tree);
3869 static unsigned int simple_type_align_in_bits (const_tree);
3870 static unsigned int simple_decl_align_in_bits (const_tree);
3871 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3872 struct vlr_context;
3873 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3874 HOST_WIDE_INT *);
3875 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3876 dw_loc_list_ref);
3877 static void add_data_member_location_attribute (dw_die_ref, tree,
3878 struct vlr_context *);
3879 static bool add_const_value_attribute (dw_die_ref, machine_mode, rtx);
3880 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3881 static void insert_wide_int (const wide_int_ref &, unsigned char *, int);
3882 static unsigned insert_float (const_rtx, unsigned char *);
3883 static rtx rtl_for_decl_location (tree);
3884 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3885 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3886 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3887 static void add_desc_attribute (dw_die_ref, tree);
3888 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3889 static void add_comp_dir_attribute (dw_die_ref);
3890 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3891 struct loc_descr_context *);
3892 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3893 struct loc_descr_context *);
3894 static void add_subscript_info (dw_die_ref, tree, bool);
3895 static void add_byte_size_attribute (dw_die_ref, tree);
3896 static void add_alignment_attribute (dw_die_ref, tree);
3897 static void add_bit_offset_attribute (dw_die_ref, tree);
3898 static void add_bit_size_attribute (dw_die_ref, tree);
3899 static void add_prototyped_attribute (dw_die_ref, tree);
3900 static void add_abstract_origin_attribute (dw_die_ref, tree);
3901 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3902 static void add_src_coords_attributes (dw_die_ref, tree);
3903 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3904 static void add_discr_value (dw_die_ref, dw_discr_value *);
3905 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3906 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3907 static dw_die_ref scope_die_for (tree, dw_die_ref);
3908 static inline bool local_scope_p (dw_die_ref);
3909 static inline bool class_scope_p (dw_die_ref);
3910 static inline bool class_or_namespace_scope_p (dw_die_ref);
3911 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3912 static void add_calling_convention_attribute (dw_die_ref, tree);
3913 static const char *type_tag (const_tree);
3914 static tree member_declared_type (const_tree);
3915 #if 0
3916 static const char *decl_start_label (tree);
3917 #endif
3918 static void gen_array_type_die (tree, dw_die_ref);
3919 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3920 #if 0
3921 static void gen_entry_point_die (tree, dw_die_ref);
3922 #endif
3923 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref, bool);
3924 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3925 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3926 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3927 static void gen_formal_types_die (tree, dw_die_ref);
3928 static void gen_subprogram_die (tree, dw_die_ref);
3929 static void gen_variable_die (tree, tree, dw_die_ref);
3930 static void gen_const_die (tree, dw_die_ref);
3931 static void gen_label_die (tree, dw_die_ref);
3932 static void gen_lexical_block_die (tree, dw_die_ref);
3933 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3934 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3935 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3936 static dw_die_ref gen_compile_unit_die (const char *);
3937 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3938 static void gen_member_die (tree, dw_die_ref);
3939 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3940 enum debug_info_usage);
3941 static void gen_subroutine_type_die (tree, dw_die_ref);
3942 static void gen_typedef_die (tree, dw_die_ref);
3943 static void gen_type_die (tree, dw_die_ref, bool = false);
3944 static void gen_block_die (tree, dw_die_ref);
3945 static void decls_for_scope (tree, dw_die_ref, bool = true);
3946 static bool is_naming_typedef_decl (const_tree);
3947 static inline dw_die_ref get_context_die (tree);
3948 static void gen_namespace_die (tree, dw_die_ref);
3949 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3950 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3951 static dw_die_ref force_decl_die (tree);
3952 static dw_die_ref force_type_die (tree);
3953 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3954 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3955 static struct dwarf_file_data * lookup_filename (const char *);
3956 static void retry_incomplete_types (void);
3957 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3958 static void gen_generic_params_dies (tree);
3959 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage,
3960 bool = false);
3961 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage,
3962 bool = false);
3963 static void splice_child_die (dw_die_ref, dw_die_ref);
3964 static int file_info_cmp (const void *, const void *);
3965 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3966 const char *, var_loc_view, const char *);
3967 static void output_loc_list (dw_loc_list_ref);
3968 static char *gen_internal_sym (const char *);
3969 static bool want_pubnames (void);
3971 static void prune_unmark_dies (dw_die_ref);
3972 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3973 static void prune_unused_types_mark (dw_die_ref, int);
3974 static void prune_unused_types_walk (dw_die_ref);
3975 static void prune_unused_types_walk_attribs (dw_die_ref);
3976 static void prune_unused_types_prune (dw_die_ref);
3977 static void prune_unused_types (void);
3978 static int maybe_emit_file (struct dwarf_file_data *fd);
3979 static inline const char *AT_vms_delta1 (dw_attr_node *);
3980 static inline const char *AT_vms_delta2 (dw_attr_node *);
3981 #if VMS_DEBUGGING_INFO
3982 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3983 const char *, const char *);
3984 #endif
3985 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3986 static void gen_remaining_tmpl_value_param_die_attribute (void);
3987 static bool generic_type_p (tree);
3988 static void schedule_generic_params_dies_gen (tree t);
3989 static void gen_scheduled_generic_parms_dies (void);
3990 static void resolve_variable_values (void);
3992 static const char *comp_dir_string (void);
3994 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3996 /* enum for tracking thread-local variables whose address is really an offset
3997 relative to the TLS pointer, which will need link-time relocation, but will
3998 not need relocation by the DWARF consumer. */
4000 enum dtprel_bool
4002 dtprel_false = 0,
4003 dtprel_true = 1
4006 /* Return the operator to use for an address of a variable. For dtprel_true, we
4007 use DW_OP_const*. For regular variables, which need both link-time
4008 relocation and consumer-level relocation (e.g., to account for shared objects
4009 loaded at a random address), we use DW_OP_addr*. */
4011 static inline enum dwarf_location_atom
4012 dw_addr_op (enum dtprel_bool dtprel)
4014 if (dtprel == dtprel_true)
4015 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
4016 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
4017 else
4018 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
4021 /* Return a pointer to a newly allocated address location description. If
4022 dwarf_split_debug_info is true, then record the address with the appropriate
4023 relocation. */
4024 static inline dw_loc_descr_ref
4025 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
4027 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
4029 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
4030 ref->dw_loc_oprnd1.v.val_addr = addr;
4031 ref->dtprel = dtprel;
4032 if (dwarf_split_debug_info)
4033 ref->dw_loc_oprnd1.val_entry
4034 = add_addr_table_entry (addr,
4035 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
4036 else
4037 ref->dw_loc_oprnd1.val_entry = NULL;
4039 return ref;
4042 /* Section names used to hold DWARF debugging information. */
4044 #ifndef DEBUG_INFO_SECTION
4045 #define DEBUG_INFO_SECTION ".debug_info"
4046 #endif
4047 #ifndef DEBUG_DWO_INFO_SECTION
4048 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
4049 #endif
4050 #ifndef DEBUG_LTO_INFO_SECTION
4051 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
4052 #endif
4053 #ifndef DEBUG_LTO_DWO_INFO_SECTION
4054 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
4055 #endif
4056 #ifndef DEBUG_ABBREV_SECTION
4057 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4058 #endif
4059 #ifndef DEBUG_LTO_ABBREV_SECTION
4060 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
4061 #endif
4062 #ifndef DEBUG_DWO_ABBREV_SECTION
4063 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
4064 #endif
4065 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
4066 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
4067 #endif
4068 #ifndef DEBUG_ARANGES_SECTION
4069 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4070 #endif
4071 #ifndef DEBUG_ADDR_SECTION
4072 #define DEBUG_ADDR_SECTION ".debug_addr"
4073 #endif
4074 #ifndef DEBUG_MACINFO_SECTION
4075 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4076 #endif
4077 #ifndef DEBUG_LTO_MACINFO_SECTION
4078 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4079 #endif
4080 #ifndef DEBUG_DWO_MACINFO_SECTION
4081 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4082 #endif
4083 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4084 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4085 #endif
4086 #ifndef DEBUG_MACRO_SECTION
4087 #define DEBUG_MACRO_SECTION ".debug_macro"
4088 #endif
4089 #ifndef DEBUG_LTO_MACRO_SECTION
4090 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4091 #endif
4092 #ifndef DEBUG_DWO_MACRO_SECTION
4093 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4094 #endif
4095 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4096 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4097 #endif
4098 #ifndef DEBUG_LINE_SECTION
4099 #define DEBUG_LINE_SECTION ".debug_line"
4100 #endif
4101 #ifndef DEBUG_LTO_LINE_SECTION
4102 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4103 #endif
4104 #ifndef DEBUG_DWO_LINE_SECTION
4105 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4106 #endif
4107 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4108 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4109 #endif
4110 #ifndef DEBUG_LOC_SECTION
4111 #define DEBUG_LOC_SECTION ".debug_loc"
4112 #endif
4113 #ifndef DEBUG_DWO_LOC_SECTION
4114 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4115 #endif
4116 #ifndef DEBUG_LOCLISTS_SECTION
4117 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4118 #endif
4119 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4120 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4121 #endif
4122 #ifndef DEBUG_PUBNAMES_SECTION
4123 #define DEBUG_PUBNAMES_SECTION \
4124 ((debug_generate_pub_sections == 2) \
4125 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4126 #endif
4127 #ifndef DEBUG_PUBTYPES_SECTION
4128 #define DEBUG_PUBTYPES_SECTION \
4129 ((debug_generate_pub_sections == 2) \
4130 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4131 #endif
4132 #ifndef DEBUG_STR_OFFSETS_SECTION
4133 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4134 #endif
4135 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4136 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4137 #endif
4138 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4139 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4140 #endif
4141 #ifndef DEBUG_STR_SECTION
4142 #define DEBUG_STR_SECTION ".debug_str"
4143 #endif
4144 #ifndef DEBUG_LTO_STR_SECTION
4145 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4146 #endif
4147 #ifndef DEBUG_STR_DWO_SECTION
4148 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4149 #endif
4150 #ifndef DEBUG_LTO_STR_DWO_SECTION
4151 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4152 #endif
4153 #ifndef DEBUG_RANGES_SECTION
4154 #define DEBUG_RANGES_SECTION ".debug_ranges"
4155 #endif
4156 #ifndef DEBUG_RNGLISTS_SECTION
4157 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4158 #endif
4159 #ifndef DEBUG_DWO_RNGLISTS_SECTION
4160 #define DEBUG_DWO_RNGLISTS_SECTION ".debug_rnglists.dwo"
4161 #endif
4162 #ifndef DEBUG_LINE_STR_SECTION
4163 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4164 #endif
4165 #ifndef DEBUG_LTO_LINE_STR_SECTION
4166 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4167 #endif
4169 /* Section flags for .debug_str section. */
4170 #define DEBUG_STR_SECTION_FLAGS \
4171 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4172 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4173 : SECTION_DEBUG)
4175 /* Section flags for .debug_str.dwo section. */
4176 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4178 /* Attribute used to refer to the macro section. */
4179 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4180 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4182 /* Labels we insert at beginning sections we can reference instead of
4183 the section names themselves. */
4185 #ifndef TEXT_SECTION_LABEL
4186 #define TEXT_SECTION_LABEL "Ltext"
4187 #endif
4188 #ifndef COLD_TEXT_SECTION_LABEL
4189 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4190 #endif
4191 #ifndef DEBUG_LINE_SECTION_LABEL
4192 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4193 #endif
4194 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4195 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4196 #endif
4197 #ifndef DEBUG_INFO_SECTION_LABEL
4198 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4199 #endif
4200 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4201 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4202 #endif
4203 #ifndef DEBUG_ABBREV_SECTION_LABEL
4204 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4205 #endif
4206 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4207 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4208 #endif
4209 #ifndef DEBUG_ADDR_SECTION_LABEL
4210 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4211 #endif
4212 #ifndef DEBUG_LOC_SECTION_LABEL
4213 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4214 #endif
4215 #ifndef DEBUG_RANGES_SECTION_LABEL
4216 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4217 #endif
4218 #ifndef DEBUG_MACINFO_SECTION_LABEL
4219 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4220 #endif
4221 #ifndef DEBUG_MACRO_SECTION_LABEL
4222 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4223 #endif
4224 #define SKELETON_COMP_DIE_ABBREV 1
4225 #define SKELETON_TYPE_DIE_ABBREV 2
4227 /* Definitions of defaults for formats and names of various special
4228 (artificial) labels which may be generated within this file (when the -g
4229 options is used and DWARF2_DEBUGGING_INFO is in effect.
4230 If necessary, these may be overridden from within the tm.h file, but
4231 typically, overriding these defaults is unnecessary. */
4233 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4234 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4235 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4236 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4237 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4238 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4239 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4240 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4241 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4242 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4243 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4244 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4245 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4246 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4247 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4249 #ifndef TEXT_END_LABEL
4250 #define TEXT_END_LABEL "Letext"
4251 #endif
4252 #ifndef COLD_END_LABEL
4253 #define COLD_END_LABEL "Letext_cold"
4254 #endif
4255 #ifndef BLOCK_BEGIN_LABEL
4256 #define BLOCK_BEGIN_LABEL "LBB"
4257 #endif
4258 #ifndef BLOCK_INLINE_ENTRY_LABEL
4259 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4260 #endif
4261 #ifndef BLOCK_END_LABEL
4262 #define BLOCK_END_LABEL "LBE"
4263 #endif
4264 #ifndef LINE_CODE_LABEL
4265 #define LINE_CODE_LABEL "LM"
4266 #endif
4269 /* Return the root of the DIE's built for the current compilation unit. */
4270 static dw_die_ref
4271 comp_unit_die (void)
4273 if (!single_comp_unit_die)
4274 single_comp_unit_die = gen_compile_unit_die (NULL);
4275 return single_comp_unit_die;
4278 /* We allow a language front-end to designate a function that is to be
4279 called to "demangle" any name before it is put into a DIE. */
4281 static const char *(*demangle_name_func) (const char *);
4283 void
4284 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4286 demangle_name_func = func;
4289 /* Test if rtl node points to a pseudo register. */
4291 static inline bool
4292 is_pseudo_reg (const_rtx rtl)
4294 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4295 || (GET_CODE (rtl) == SUBREG
4296 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4299 /* Return a reference to a type, with its const and volatile qualifiers
4300 removed. */
4302 static inline tree
4303 type_main_variant (tree type)
4305 type = TYPE_MAIN_VARIANT (type);
4307 /* ??? There really should be only one main variant among any group of
4308 variants of a given type (and all of the MAIN_VARIANT values for all
4309 members of the group should point to that one type) but sometimes the C
4310 front-end messes this up for array types, so we work around that bug
4311 here. */
4312 if (TREE_CODE (type) == ARRAY_TYPE)
4313 while (type != TYPE_MAIN_VARIANT (type))
4314 type = TYPE_MAIN_VARIANT (type);
4316 return type;
4319 /* Return true if the given type node represents a tagged type. */
4321 static inline bool
4322 is_tagged_type (const_tree type)
4324 enum tree_code code = TREE_CODE (type);
4326 return (code == RECORD_TYPE || code == UNION_TYPE
4327 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4330 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4332 static void
4333 get_ref_die_offset_label (char *label, dw_die_ref ref)
4335 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4338 /* Return die_offset of a DIE reference to a base type. */
4340 static unsigned long int
4341 get_base_type_offset (dw_die_ref ref)
4343 if (ref->die_offset)
4344 return ref->die_offset;
4345 if (comp_unit_die ()->die_abbrev)
4347 calc_base_type_die_sizes ();
4348 gcc_assert (ref->die_offset);
4350 return ref->die_offset;
4353 /* Return die_offset of a DIE reference other than base type. */
4355 static unsigned long int
4356 get_ref_die_offset (dw_die_ref ref)
4358 gcc_assert (ref->die_offset);
4359 return ref->die_offset;
4362 /* Convert a DIE tag into its string name. */
4364 static const char *
4365 dwarf_tag_name (unsigned int tag)
4367 const char *name = get_DW_TAG_name (tag);
4369 if (name != NULL)
4370 return name;
4372 return "DW_TAG_<unknown>";
4375 /* Convert a DWARF attribute code into its string name. */
4377 static const char *
4378 dwarf_attr_name (unsigned int attr)
4380 const char *name;
4382 switch (attr)
4384 #if VMS_DEBUGGING_INFO
4385 case DW_AT_HP_prologue:
4386 return "DW_AT_HP_prologue";
4387 #else
4388 case DW_AT_MIPS_loop_unroll_factor:
4389 return "DW_AT_MIPS_loop_unroll_factor";
4390 #endif
4392 #if VMS_DEBUGGING_INFO
4393 case DW_AT_HP_epilogue:
4394 return "DW_AT_HP_epilogue";
4395 #else
4396 case DW_AT_MIPS_stride:
4397 return "DW_AT_MIPS_stride";
4398 #endif
4401 name = get_DW_AT_name (attr);
4403 if (name != NULL)
4404 return name;
4406 return "DW_AT_<unknown>";
4409 /* Convert a DWARF value form code into its string name. */
4411 static const char *
4412 dwarf_form_name (unsigned int form)
4414 const char *name = get_DW_FORM_name (form);
4416 if (name != NULL)
4417 return name;
4419 return "DW_FORM_<unknown>";
4422 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4423 instance of an inlined instance of a decl which is local to an inline
4424 function, so we have to trace all of the way back through the origin chain
4425 to find out what sort of node actually served as the original seed for the
4426 given block. */
4428 static tree
4429 decl_ultimate_origin (const_tree decl)
4431 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4432 return NULL_TREE;
4434 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4435 we're trying to output the abstract instance of this function. */
4436 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4437 return NULL_TREE;
4439 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4440 most distant ancestor, this should never happen. */
4441 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4443 return DECL_ABSTRACT_ORIGIN (decl);
4446 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4447 of a virtual function may refer to a base class, so we check the 'this'
4448 parameter. */
4450 static tree
4451 decl_class_context (tree decl)
4453 tree context = NULL_TREE;
4455 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4456 context = DECL_CONTEXT (decl);
4457 else
4458 context = TYPE_MAIN_VARIANT
4459 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4461 if (context && !TYPE_P (context))
4462 context = NULL_TREE;
4464 return context;
4467 /* Add an attribute/value pair to a DIE. */
4469 static inline void
4470 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4472 /* Maybe this should be an assert? */
4473 if (die == NULL)
4474 return;
4476 if (flag_checking)
4478 /* Check we do not add duplicate attrs. Can't use get_AT here
4479 because that recurses to the specification/abstract origin DIE. */
4480 dw_attr_node *a;
4481 unsigned ix;
4482 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4483 gcc_assert (a->dw_attr != attr->dw_attr);
4486 vec_safe_reserve (die->die_attr, 1);
4487 vec_safe_push (die->die_attr, *attr);
4490 enum dw_val_class
4491 AT_class (dw_attr_node *a)
4493 return a->dw_attr_val.val_class;
4496 /* Return the index for any attribute that will be referenced with a
4497 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4498 indices are stored in dw_attr_val.v.val_str for reference counting
4499 pruning. */
4501 static inline unsigned int
4502 AT_index (dw_attr_node *a)
4504 if (AT_class (a) == dw_val_class_str)
4505 return a->dw_attr_val.v.val_str->index;
4506 else if (a->dw_attr_val.val_entry != NULL)
4507 return a->dw_attr_val.val_entry->index;
4508 return NOT_INDEXED;
4511 /* Add a flag value attribute to a DIE. */
4513 static inline void
4514 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4516 dw_attr_node attr;
4518 attr.dw_attr = attr_kind;
4519 attr.dw_attr_val.val_class = dw_val_class_flag;
4520 attr.dw_attr_val.val_entry = NULL;
4521 attr.dw_attr_val.v.val_flag = flag;
4522 add_dwarf_attr (die, &attr);
4525 static inline unsigned
4526 AT_flag (dw_attr_node *a)
4528 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4529 return a->dw_attr_val.v.val_flag;
4532 /* Add a signed integer attribute value to a DIE. */
4534 static inline void
4535 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4537 dw_attr_node attr;
4539 attr.dw_attr = attr_kind;
4540 attr.dw_attr_val.val_class = dw_val_class_const;
4541 attr.dw_attr_val.val_entry = NULL;
4542 attr.dw_attr_val.v.val_int = int_val;
4543 add_dwarf_attr (die, &attr);
4546 HOST_WIDE_INT
4547 AT_int (dw_attr_node *a)
4549 gcc_assert (a && (AT_class (a) == dw_val_class_const
4550 || AT_class (a) == dw_val_class_const_implicit));
4551 return a->dw_attr_val.v.val_int;
4554 /* Add an unsigned integer attribute value to a DIE. */
4556 static inline void
4557 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4558 unsigned HOST_WIDE_INT unsigned_val)
4560 dw_attr_node attr;
4562 attr.dw_attr = attr_kind;
4563 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4564 attr.dw_attr_val.val_entry = NULL;
4565 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4566 add_dwarf_attr (die, &attr);
4569 unsigned HOST_WIDE_INT
4570 AT_unsigned (dw_attr_node *a)
4572 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4573 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4574 return a->dw_attr_val.v.val_unsigned;
4577 dw_wide_int *
4578 alloc_dw_wide_int (const wide_int_ref &w)
4580 dw_wide_int *p
4581 = (dw_wide_int *) ggc_internal_alloc (sizeof (dw_wide_int)
4582 + ((w.get_len () - 1)
4583 * sizeof (HOST_WIDE_INT)));
4584 p->precision = w.get_precision ();
4585 p->len = w.get_len ();
4586 memcpy (p->val, w.get_val (), p->len * sizeof (HOST_WIDE_INT));
4587 return p;
4590 /* Add an unsigned wide integer attribute value to a DIE. */
4592 static inline void
4593 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4594 const wide_int_ref &w)
4596 dw_attr_node attr;
4598 attr.dw_attr = attr_kind;
4599 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4600 attr.dw_attr_val.val_entry = NULL;
4601 attr.dw_attr_val.v.val_wide = alloc_dw_wide_int (w);
4602 add_dwarf_attr (die, &attr);
4605 /* Add an unsigned double integer attribute value to a DIE. */
4607 static inline void
4608 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4609 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4611 dw_attr_node attr;
4613 attr.dw_attr = attr_kind;
4614 attr.dw_attr_val.val_class = dw_val_class_const_double;
4615 attr.dw_attr_val.val_entry = NULL;
4616 attr.dw_attr_val.v.val_double.high = high;
4617 attr.dw_attr_val.v.val_double.low = low;
4618 add_dwarf_attr (die, &attr);
4621 /* Add a floating point attribute value to a DIE and return it. */
4623 static inline void
4624 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4625 unsigned int length, unsigned int elt_size, unsigned char *array)
4627 dw_attr_node attr;
4629 attr.dw_attr = attr_kind;
4630 attr.dw_attr_val.val_class = dw_val_class_vec;
4631 attr.dw_attr_val.val_entry = NULL;
4632 attr.dw_attr_val.v.val_vec.length = length;
4633 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4634 attr.dw_attr_val.v.val_vec.array = array;
4635 add_dwarf_attr (die, &attr);
4638 /* Add an 8-byte data attribute value to a DIE. */
4640 static inline void
4641 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4642 unsigned char data8[8])
4644 dw_attr_node attr;
4646 attr.dw_attr = attr_kind;
4647 attr.dw_attr_val.val_class = dw_val_class_data8;
4648 attr.dw_attr_val.val_entry = NULL;
4649 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4650 add_dwarf_attr (die, &attr);
4653 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4654 dwarf_split_debug_info, address attributes in dies destined for the
4655 final executable have force_direct set to avoid using indexed
4656 references. */
4658 static inline void
4659 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4660 bool force_direct)
4662 dw_attr_node attr;
4663 char * lbl_id;
4665 lbl_id = xstrdup (lbl_low);
4666 attr.dw_attr = DW_AT_low_pc;
4667 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4668 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4669 if (dwarf_split_debug_info && !force_direct)
4670 attr.dw_attr_val.val_entry
4671 = add_addr_table_entry (lbl_id, ate_kind_label);
4672 else
4673 attr.dw_attr_val.val_entry = NULL;
4674 add_dwarf_attr (die, &attr);
4676 attr.dw_attr = DW_AT_high_pc;
4677 if (dwarf_version < 4)
4678 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4679 else
4680 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4681 lbl_id = xstrdup (lbl_high);
4682 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4683 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4684 && dwarf_split_debug_info && !force_direct)
4685 attr.dw_attr_val.val_entry
4686 = add_addr_table_entry (lbl_id, ate_kind_label);
4687 else
4688 attr.dw_attr_val.val_entry = NULL;
4689 add_dwarf_attr (die, &attr);
4692 /* Hash and equality functions for debug_str_hash. */
4694 hashval_t
4695 indirect_string_hasher::hash (indirect_string_node *x)
4697 return htab_hash_string (x->str);
4700 bool
4701 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4703 return strcmp (x1->str, x2) == 0;
4706 /* Add STR to the given string hash table. */
4708 static struct indirect_string_node *
4709 find_AT_string_in_table (const char *str,
4710 hash_table<indirect_string_hasher> *table,
4711 enum insert_option insert = INSERT)
4713 struct indirect_string_node *node;
4715 indirect_string_node **slot
4716 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4717 if (*slot == NULL)
4719 node = ggc_cleared_alloc<indirect_string_node> ();
4720 node->str = ggc_strdup (str);
4721 *slot = node;
4723 else
4724 node = *slot;
4726 node->refcount++;
4727 return node;
4730 /* Add STR to the indirect string hash table. */
4732 static struct indirect_string_node *
4733 find_AT_string (const char *str, enum insert_option insert = INSERT)
4735 if (! debug_str_hash)
4736 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4738 return find_AT_string_in_table (str, debug_str_hash, insert);
4741 /* Add a string attribute value to a DIE. */
4743 static inline void
4744 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4746 dw_attr_node attr;
4747 struct indirect_string_node *node;
4749 node = find_AT_string (str);
4751 attr.dw_attr = attr_kind;
4752 attr.dw_attr_val.val_class = dw_val_class_str;
4753 attr.dw_attr_val.val_entry = NULL;
4754 attr.dw_attr_val.v.val_str = node;
4755 add_dwarf_attr (die, &attr);
4758 static inline const char *
4759 AT_string (dw_attr_node *a)
4761 gcc_assert (a && AT_class (a) == dw_val_class_str);
4762 return a->dw_attr_val.v.val_str->str;
4765 /* Call this function directly to bypass AT_string_form's logic to put
4766 the string inline in the die. */
4768 static void
4769 set_indirect_string (struct indirect_string_node *node)
4771 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4772 /* Already indirect is a no op. */
4773 if (node->form == DW_FORM_strp
4774 || node->form == DW_FORM_line_strp
4775 || node->form == dwarf_FORM (DW_FORM_strx))
4777 gcc_assert (node->label);
4778 return;
4780 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4781 ++dw2_string_counter;
4782 node->label = xstrdup (label);
4784 if (!dwarf_split_debug_info)
4786 node->form = DW_FORM_strp;
4787 node->index = NOT_INDEXED;
4789 else
4791 node->form = dwarf_FORM (DW_FORM_strx);
4792 node->index = NO_INDEX_ASSIGNED;
4796 /* A helper function for dwarf2out_finish, called to reset indirect
4797 string decisions done for early LTO dwarf output before fat object
4798 dwarf output. */
4801 reset_indirect_string (indirect_string_node **h, void *)
4803 struct indirect_string_node *node = *h;
4804 if (node->form == DW_FORM_strp
4805 || node->form == DW_FORM_line_strp
4806 || node->form == dwarf_FORM (DW_FORM_strx))
4808 free (node->label);
4809 node->label = NULL;
4810 node->form = (dwarf_form) 0;
4811 node->index = 0;
4813 return 1;
4816 /* Add a string representing a file or filepath attribute value to a DIE. */
4818 static inline void
4819 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4820 const char *str)
4822 if (! asm_outputs_debug_line_str ())
4823 add_AT_string (die, attr_kind, str);
4824 else
4826 dw_attr_node attr;
4827 struct indirect_string_node *node;
4829 if (!debug_line_str_hash)
4830 debug_line_str_hash
4831 = hash_table<indirect_string_hasher>::create_ggc (10);
4833 node = find_AT_string_in_table (str, debug_line_str_hash);
4834 set_indirect_string (node);
4835 node->form = DW_FORM_line_strp;
4837 attr.dw_attr = attr_kind;
4838 attr.dw_attr_val.val_class = dw_val_class_str;
4839 attr.dw_attr_val.val_entry = NULL;
4840 attr.dw_attr_val.v.val_str = node;
4841 add_dwarf_attr (die, &attr);
4845 /* Find out whether a string should be output inline in DIE
4846 or out-of-line in .debug_str section. */
4848 static enum dwarf_form
4849 find_string_form (struct indirect_string_node *node)
4851 unsigned int len;
4853 if (node->form)
4854 return node->form;
4856 len = strlen (node->str) + 1;
4858 /* If the string is shorter or equal to the size of the reference, it is
4859 always better to put it inline. */
4860 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4861 return node->form = DW_FORM_string;
4863 /* If we cannot expect the linker to merge strings in .debug_str
4864 section, only put it into .debug_str if it is worth even in this
4865 single module. */
4866 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4867 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4868 && (len - dwarf_offset_size) * node->refcount <= len))
4869 return node->form = DW_FORM_string;
4871 set_indirect_string (node);
4873 return node->form;
4876 /* Find out whether the string referenced from the attribute should be
4877 output inline in DIE or out-of-line in .debug_str section. */
4879 static enum dwarf_form
4880 AT_string_form (dw_attr_node *a)
4882 gcc_assert (a && AT_class (a) == dw_val_class_str);
4883 return find_string_form (a->dw_attr_val.v.val_str);
4886 /* Add a DIE reference attribute value to a DIE. */
4888 static inline void
4889 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4891 dw_attr_node attr;
4892 gcc_checking_assert (targ_die != NULL);
4893 gcc_assert (targ_die != die
4894 || (attr_kind != DW_AT_abstract_origin
4895 && attr_kind != DW_AT_specification));
4897 /* With LTO we can end up trying to reference something we didn't create
4898 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4899 if (targ_die == NULL)
4900 return;
4902 attr.dw_attr = attr_kind;
4903 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4904 attr.dw_attr_val.val_entry = NULL;
4905 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4906 attr.dw_attr_val.v.val_die_ref.external = 0;
4907 add_dwarf_attr (die, &attr);
4910 /* Change DIE reference REF to point to NEW_DIE instead. */
4912 static inline void
4913 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4915 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4916 ref->dw_attr_val.v.val_die_ref.die = new_die;
4917 ref->dw_attr_val.v.val_die_ref.external = 0;
4920 /* Add an AT_specification attribute to a DIE, and also make the back
4921 pointer from the specification to the definition. */
4923 static inline void
4924 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4926 add_AT_die_ref (die, DW_AT_specification, targ_die);
4927 gcc_assert (!targ_die->die_definition);
4928 targ_die->die_definition = die;
4931 static inline dw_die_ref
4932 AT_ref (dw_attr_node *a)
4934 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4935 return a->dw_attr_val.v.val_die_ref.die;
4938 static inline int
4939 AT_ref_external (dw_attr_node *a)
4941 if (a && AT_class (a) == dw_val_class_die_ref)
4942 return a->dw_attr_val.v.val_die_ref.external;
4944 return 0;
4947 static inline void
4948 set_AT_ref_external (dw_attr_node *a, int i)
4950 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4951 a->dw_attr_val.v.val_die_ref.external = i;
4954 /* Add a location description attribute value to a DIE. */
4956 static inline void
4957 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4959 dw_attr_node attr;
4961 attr.dw_attr = attr_kind;
4962 attr.dw_attr_val.val_class = dw_val_class_loc;
4963 attr.dw_attr_val.val_entry = NULL;
4964 attr.dw_attr_val.v.val_loc = loc;
4965 add_dwarf_attr (die, &attr);
4968 dw_loc_descr_ref
4969 AT_loc (dw_attr_node *a)
4971 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4972 return a->dw_attr_val.v.val_loc;
4975 static inline void
4976 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4978 dw_attr_node attr;
4980 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4981 return;
4983 attr.dw_attr = attr_kind;
4984 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4985 attr.dw_attr_val.val_entry = NULL;
4986 attr.dw_attr_val.v.val_loc_list = loc_list;
4987 add_dwarf_attr (die, &attr);
4988 have_location_lists = true;
4991 static inline dw_loc_list_ref
4992 AT_loc_list (dw_attr_node *a)
4994 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4995 return a->dw_attr_val.v.val_loc_list;
4998 /* Add a view list attribute to DIE. It must have a DW_AT_location
4999 attribute, because the view list complements the location list. */
5001 static inline void
5002 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
5004 dw_attr_node attr;
5006 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5007 return;
5009 attr.dw_attr = attr_kind;
5010 attr.dw_attr_val.val_class = dw_val_class_view_list;
5011 attr.dw_attr_val.val_entry = NULL;
5012 attr.dw_attr_val.v.val_view_list = die;
5013 add_dwarf_attr (die, &attr);
5014 gcc_checking_assert (get_AT (die, DW_AT_location));
5015 gcc_assert (have_location_lists);
5018 /* Return a pointer to the location list referenced by the attribute.
5019 If the named attribute is a view list, look up the corresponding
5020 DW_AT_location attribute and return its location list. */
5022 static inline dw_loc_list_ref *
5023 AT_loc_list_ptr (dw_attr_node *a)
5025 gcc_assert (a);
5026 switch (AT_class (a))
5028 case dw_val_class_loc_list:
5029 return &a->dw_attr_val.v.val_loc_list;
5030 case dw_val_class_view_list:
5032 dw_attr_node *l;
5033 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5034 if (!l)
5035 return NULL;
5036 gcc_checking_assert (l + 1 == a);
5037 return AT_loc_list_ptr (l);
5039 default:
5040 gcc_unreachable ();
5044 /* Return the location attribute value associated with a view list
5045 attribute value. */
5047 static inline dw_val_node *
5048 view_list_to_loc_list_val_node (dw_val_node *val)
5050 gcc_assert (val->val_class == dw_val_class_view_list);
5051 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5052 if (!loc)
5053 return NULL;
5054 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5055 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5056 return &loc->dw_attr_val;
5059 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5061 static hashval_t hash (addr_table_entry *);
5062 static bool equal (addr_table_entry *, addr_table_entry *);
5065 /* Table of entries into the .debug_addr section. */
5067 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5069 /* Hash an address_table_entry. */
5071 hashval_t
5072 addr_hasher::hash (addr_table_entry *a)
5074 inchash::hash hstate;
5075 switch (a->kind)
5077 case ate_kind_rtx:
5078 hstate.add_int (0);
5079 break;
5080 case ate_kind_rtx_dtprel:
5081 hstate.add_int (1);
5082 break;
5083 case ate_kind_label:
5084 return htab_hash_string (a->addr.label);
5085 default:
5086 gcc_unreachable ();
5088 inchash::add_rtx (a->addr.rtl, hstate);
5089 return hstate.end ();
5092 /* Determine equality for two address_table_entries. */
5094 bool
5095 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5097 if (a1->kind != a2->kind)
5098 return false;
5099 switch (a1->kind)
5101 case ate_kind_rtx:
5102 case ate_kind_rtx_dtprel:
5103 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5104 case ate_kind_label:
5105 return strcmp (a1->addr.label, a2->addr.label) == 0;
5106 default:
5107 gcc_unreachable ();
5111 /* Initialize an addr_table_entry. */
5113 void
5114 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5116 e->kind = kind;
5117 switch (kind)
5119 case ate_kind_rtx:
5120 case ate_kind_rtx_dtprel:
5121 e->addr.rtl = (rtx) addr;
5122 break;
5123 case ate_kind_label:
5124 e->addr.label = (char *) addr;
5125 break;
5127 e->refcount = 0;
5128 e->index = NO_INDEX_ASSIGNED;
5131 /* Add attr to the address table entry to the table. Defer setting an
5132 index until output time. */
5134 static addr_table_entry *
5135 add_addr_table_entry (void *addr, enum ate_kind kind)
5137 addr_table_entry *node;
5138 addr_table_entry finder;
5140 gcc_assert (dwarf_split_debug_info);
5141 if (! addr_index_table)
5142 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5143 init_addr_table_entry (&finder, kind, addr);
5144 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5146 if (*slot == HTAB_EMPTY_ENTRY)
5148 node = ggc_cleared_alloc<addr_table_entry> ();
5149 init_addr_table_entry (node, kind, addr);
5150 *slot = node;
5152 else
5153 node = *slot;
5155 node->refcount++;
5156 return node;
5159 /* Remove an entry from the addr table by decrementing its refcount.
5160 Strictly, decrementing the refcount would be enough, but the
5161 assertion that the entry is actually in the table has found
5162 bugs. */
5164 static void
5165 remove_addr_table_entry (addr_table_entry *entry)
5167 gcc_assert (dwarf_split_debug_info && addr_index_table);
5168 /* After an index is assigned, the table is frozen. */
5169 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5170 entry->refcount--;
5173 /* Given a location list, remove all addresses it refers to from the
5174 address_table. */
5176 static void
5177 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5179 for (; descr; descr = descr->dw_loc_next)
5180 if (descr->dw_loc_oprnd1.val_entry != NULL)
5182 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5183 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5187 /* A helper function for dwarf2out_finish called through
5188 htab_traverse. Assign an addr_table_entry its index. All entries
5189 must be collected into the table when this function is called,
5190 because the indexing code relies on htab_traverse to traverse nodes
5191 in the same order for each run. */
5194 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5196 addr_table_entry *node = *h;
5198 /* Don't index unreferenced nodes. */
5199 if (node->refcount == 0)
5200 return 1;
5202 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5203 node->index = *index;
5204 *index += 1;
5206 return 1;
5209 /* Return the tag of a given DIE. */
5211 enum dwarf_tag
5212 dw_get_die_tag (dw_die_ref die)
5214 return die->die_tag;
5217 /* Return a reference to the children list of a given DIE. */
5219 dw_die_ref
5220 dw_get_die_child (dw_die_ref die)
5222 return die->die_child;
5225 /* Return a reference to the sibling of a given DIE. */
5227 dw_die_ref
5228 dw_get_die_sib (dw_die_ref die)
5230 return die->die_sib;
5233 /* Add an address constant attribute value to a DIE. When using
5234 dwarf_split_debug_info, address attributes in dies destined for the
5235 final executable should be direct references--setting the parameter
5236 force_direct ensures this behavior. */
5238 static inline void
5239 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5240 bool force_direct)
5242 dw_attr_node attr;
5244 attr.dw_attr = attr_kind;
5245 attr.dw_attr_val.val_class = dw_val_class_addr;
5246 attr.dw_attr_val.v.val_addr = addr;
5247 if (dwarf_split_debug_info && !force_direct)
5248 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5249 else
5250 attr.dw_attr_val.val_entry = NULL;
5251 add_dwarf_attr (die, &attr);
5254 /* Get the RTX from to an address DIE attribute. */
5256 static inline rtx
5257 AT_addr (dw_attr_node *a)
5259 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5260 return a->dw_attr_val.v.val_addr;
5263 /* Add a file attribute value to a DIE. */
5265 static inline void
5266 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5267 struct dwarf_file_data *fd)
5269 dw_attr_node attr;
5271 attr.dw_attr = attr_kind;
5272 attr.dw_attr_val.val_class = dw_val_class_file;
5273 attr.dw_attr_val.val_entry = NULL;
5274 attr.dw_attr_val.v.val_file = fd;
5275 add_dwarf_attr (die, &attr);
5278 /* Get the dwarf_file_data from a file DIE attribute. */
5280 static inline struct dwarf_file_data *
5281 AT_file (dw_attr_node *a)
5283 gcc_assert (a && (AT_class (a) == dw_val_class_file
5284 || AT_class (a) == dw_val_class_file_implicit));
5285 return a->dw_attr_val.v.val_file;
5288 #if VMS_DEBUGGING_INFO
5289 /* Add a vms delta attribute value to a DIE. */
5291 static inline void
5292 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5293 const char *lbl1, const char *lbl2)
5295 dw_attr_node attr;
5297 attr.dw_attr = attr_kind;
5298 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5299 attr.dw_attr_val.val_entry = NULL;
5300 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5301 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5302 add_dwarf_attr (die, &attr);
5304 #endif
5306 /* Add a symbolic view identifier attribute value to a DIE. */
5308 static inline void
5309 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5310 const char *view_label)
5312 dw_attr_node attr;
5314 attr.dw_attr = attr_kind;
5315 attr.dw_attr_val.val_class = dw_val_class_symview;
5316 attr.dw_attr_val.val_entry = NULL;
5317 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5318 add_dwarf_attr (die, &attr);
5321 /* Add a label identifier attribute value to a DIE. */
5323 static inline void
5324 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5325 const char *lbl_id, int offset)
5327 dw_attr_node attr;
5329 attr.dw_attr = attr_kind;
5330 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5331 attr.dw_attr_val.val_entry = NULL;
5332 if (!offset)
5333 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5334 else
5335 attr.dw_attr_val.v.val_lbl_id = xasprintf ("%s%+i", lbl_id, offset);
5336 if (dwarf_split_debug_info)
5337 attr.dw_attr_val.val_entry
5338 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5339 ate_kind_label);
5340 add_dwarf_attr (die, &attr);
5343 /* Add a section offset attribute value to a DIE, an offset into the
5344 debug_line section. */
5346 static inline void
5347 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5348 const char *label)
5350 dw_attr_node attr;
5352 attr.dw_attr = attr_kind;
5353 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5354 attr.dw_attr_val.val_entry = NULL;
5355 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5356 add_dwarf_attr (die, &attr);
5359 /* Add a section offset attribute value to a DIE, an offset into the
5360 debug_macinfo section. */
5362 static inline void
5363 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5364 const char *label)
5366 dw_attr_node attr;
5368 attr.dw_attr = attr_kind;
5369 attr.dw_attr_val.val_class = dw_val_class_macptr;
5370 attr.dw_attr_val.val_entry = NULL;
5371 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5372 add_dwarf_attr (die, &attr);
5375 /* Add a range_list attribute value to a DIE. When using
5376 dwarf_split_debug_info, address attributes in dies destined for the
5377 final executable should be direct references--setting the parameter
5378 force_direct ensures this behavior. */
5380 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5381 #define RELOCATED_OFFSET (NULL)
5383 static void
5384 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5385 long unsigned int offset, bool force_direct)
5387 dw_attr_node attr;
5389 attr.dw_attr = attr_kind;
5390 attr.dw_attr_val.val_class = dw_val_class_range_list;
5391 /* For the range_list attribute, use val_entry to store whether the
5392 offset should follow split-debug-info or normal semantics. This
5393 value is read in output_range_list_offset. */
5394 if (dwarf_split_debug_info && !force_direct)
5395 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5396 else
5397 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5398 attr.dw_attr_val.v.val_offset = offset;
5399 add_dwarf_attr (die, &attr);
5402 /* Return the start label of a delta attribute. */
5404 static inline const char *
5405 AT_vms_delta1 (dw_attr_node *a)
5407 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5408 return a->dw_attr_val.v.val_vms_delta.lbl1;
5411 /* Return the end label of a delta attribute. */
5413 static inline const char *
5414 AT_vms_delta2 (dw_attr_node *a)
5416 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5417 return a->dw_attr_val.v.val_vms_delta.lbl2;
5420 static inline const char *
5421 AT_lbl (dw_attr_node *a)
5423 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5424 || AT_class (a) == dw_val_class_lineptr
5425 || AT_class (a) == dw_val_class_macptr
5426 || AT_class (a) == dw_val_class_loclistsptr
5427 || AT_class (a) == dw_val_class_high_pc));
5428 return a->dw_attr_val.v.val_lbl_id;
5431 /* Get the attribute of type attr_kind. */
5433 dw_attr_node *
5434 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5436 dw_attr_node *a;
5437 unsigned ix;
5438 dw_die_ref spec = NULL;
5440 if (! die)
5441 return NULL;
5443 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5444 if (a->dw_attr == attr_kind)
5445 return a;
5446 else if (a->dw_attr == DW_AT_specification
5447 || a->dw_attr == DW_AT_abstract_origin)
5448 spec = AT_ref (a);
5450 if (spec)
5451 return get_AT (spec, attr_kind);
5453 return NULL;
5456 /* Returns the parent of the declaration of DIE. */
5458 dw_die_ref
5459 dw_get_die_parent (dw_die_ref die)
5461 dw_die_ref t;
5463 if (!die)
5464 return NULL;
5466 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5467 || (t = get_AT_ref (die, DW_AT_specification)))
5468 die = t;
5470 return die->die_parent;
5473 /* Return the "low pc" attribute value, typically associated with a subprogram
5474 DIE. Return null if the "low pc" attribute is either not present, or if it
5475 cannot be represented as an assembler label identifier. */
5477 static inline const char *
5478 get_AT_low_pc (dw_die_ref die)
5480 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5482 return a ? AT_lbl (a) : NULL;
5485 /* Return the value of the string attribute designated by ATTR_KIND, or
5486 NULL if it is not present. */
5488 const char *
5489 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5491 dw_attr_node *a = get_AT (die, attr_kind);
5493 return a ? AT_string (a) : NULL;
5496 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5497 if it is not present. */
5500 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5502 dw_attr_node *a = get_AT (die, attr_kind);
5504 return a ? AT_flag (a) : 0;
5507 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5508 if it is not present. */
5510 unsigned
5511 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5513 dw_attr_node *a = get_AT (die, attr_kind);
5515 return a ? AT_unsigned (a) : 0;
5518 dw_die_ref
5519 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5521 dw_attr_node *a = get_AT (die, attr_kind);
5523 return a ? AT_ref (a) : NULL;
5526 struct dwarf_file_data *
5527 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5529 dw_attr_node *a = get_AT (die, attr_kind);
5531 return a ? AT_file (a) : NULL;
5534 /* Return TRUE if the language is C. */
5536 static inline bool
5537 is_c (void)
5539 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5541 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5542 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5547 /* Return TRUE if the language is C++. */
5549 static inline bool
5550 is_cxx (void)
5552 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5554 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5555 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5558 /* Return TRUE if DECL was created by the C++ frontend. */
5560 static bool
5561 is_cxx (const_tree decl)
5563 if (in_lto_p)
5565 const_tree context = get_ultimate_context (decl);
5566 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5567 return startswith (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++");
5569 return is_cxx ();
5572 /* Return TRUE if the language is Fortran. */
5574 static inline bool
5575 is_fortran (void)
5577 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5579 return (lang == DW_LANG_Fortran77
5580 || lang == DW_LANG_Fortran90
5581 || lang == DW_LANG_Fortran95
5582 || lang == DW_LANG_Fortran03
5583 || lang == DW_LANG_Fortran08);
5586 static inline bool
5587 is_fortran (const_tree decl)
5589 if (in_lto_p)
5591 const_tree context = get_ultimate_context (decl);
5592 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5593 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5594 "GNU Fortran", 11) == 0
5595 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5596 "GNU F77") == 0);
5598 return is_fortran ();
5601 /* Return TRUE if the language is Rust.
5602 Note, returns FALSE for dwarf_version < 5 && dwarf_strict. */
5604 static inline bool
5605 is_rust (void)
5607 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5609 return lang == DW_LANG_Rust;
5612 /* Return TRUE if the language is Ada. */
5614 static inline bool
5615 is_ada (void)
5617 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5619 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5622 /* Return TRUE if the language is D. */
5624 static inline bool
5625 is_dlang (void)
5627 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5629 return lang == DW_LANG_D;
5632 /* Remove the specified attribute if present. Return TRUE if removal
5633 was successful. */
5635 static bool
5636 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5638 dw_attr_node *a;
5639 unsigned ix;
5641 if (! die)
5642 return false;
5644 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5645 if (a->dw_attr == attr_kind)
5647 if (AT_class (a) == dw_val_class_str)
5648 if (a->dw_attr_val.v.val_str->refcount)
5649 a->dw_attr_val.v.val_str->refcount--;
5651 /* vec::ordered_remove should help reduce the number of abbrevs
5652 that are needed. */
5653 die->die_attr->ordered_remove (ix);
5654 return true;
5656 return false;
5659 /* Remove CHILD from its parent. PREV must have the property that
5660 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5662 static void
5663 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5665 gcc_assert (child->die_parent == prev->die_parent);
5666 gcc_assert (prev->die_sib == child);
5667 if (prev == child)
5669 gcc_assert (child->die_parent->die_child == child);
5670 prev = NULL;
5672 else
5673 prev->die_sib = child->die_sib;
5674 if (child->die_parent->die_child == child)
5675 child->die_parent->die_child = prev;
5676 child->die_sib = NULL;
5679 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5680 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5682 static void
5683 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5685 dw_die_ref parent = old_child->die_parent;
5687 gcc_assert (parent == prev->die_parent);
5688 gcc_assert (prev->die_sib == old_child);
5690 new_child->die_parent = parent;
5691 if (prev == old_child)
5693 gcc_assert (parent->die_child == old_child);
5694 new_child->die_sib = new_child;
5696 else
5698 prev->die_sib = new_child;
5699 new_child->die_sib = old_child->die_sib;
5701 if (old_child->die_parent->die_child == old_child)
5702 old_child->die_parent->die_child = new_child;
5703 old_child->die_sib = NULL;
5706 /* Move all children from OLD_PARENT to NEW_PARENT. */
5708 static void
5709 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5711 dw_die_ref c;
5712 new_parent->die_child = old_parent->die_child;
5713 old_parent->die_child = NULL;
5714 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5717 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5718 matches TAG. */
5720 static void
5721 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5723 dw_die_ref c;
5725 c = die->die_child;
5726 if (c) do {
5727 dw_die_ref prev = c;
5728 c = c->die_sib;
5729 while (c->die_tag == tag)
5731 remove_child_with_prev (c, prev);
5732 c->die_parent = NULL;
5733 /* Might have removed every child. */
5734 if (die->die_child == NULL)
5735 return;
5736 c = prev->die_sib;
5738 } while (c != die->die_child);
5741 /* Add a CHILD_DIE as the last child of DIE. */
5743 static void
5744 add_child_die (dw_die_ref die, dw_die_ref child_die)
5746 /* FIXME this should probably be an assert. */
5747 if (! die || ! child_die)
5748 return;
5749 gcc_assert (die != child_die);
5751 child_die->die_parent = die;
5752 if (die->die_child)
5754 child_die->die_sib = die->die_child->die_sib;
5755 die->die_child->die_sib = child_die;
5757 else
5758 child_die->die_sib = child_die;
5759 die->die_child = child_die;
5762 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5764 static void
5765 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5766 dw_die_ref after_die)
5768 gcc_assert (die
5769 && child_die
5770 && after_die
5771 && die->die_child
5772 && die != child_die);
5774 child_die->die_parent = die;
5775 child_die->die_sib = after_die->die_sib;
5776 after_die->die_sib = child_die;
5777 if (die->die_child == after_die)
5778 die->die_child = child_die;
5781 /* Unassociate CHILD from its parent, and make its parent be
5782 NEW_PARENT. */
5784 static void
5785 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5787 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5788 if (p->die_sib == child)
5790 remove_child_with_prev (child, p);
5791 break;
5793 add_child_die (new_parent, child);
5796 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5797 is the specification, to the end of PARENT's list of children.
5798 This is done by removing and re-adding it. */
5800 static void
5801 splice_child_die (dw_die_ref parent, dw_die_ref child)
5803 /* We want the declaration DIE from inside the class, not the
5804 specification DIE at toplevel. */
5805 if (child->die_parent != parent)
5807 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5809 if (tmp)
5810 child = tmp;
5813 gcc_assert (child->die_parent == parent
5814 || (child->die_parent
5815 == get_AT_ref (parent, DW_AT_specification)));
5817 reparent_child (child, parent);
5820 /* Create and return a new die with TAG_VALUE as tag. */
5822 dw_die_ref
5823 new_die_raw (enum dwarf_tag tag_value)
5825 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5826 die->die_tag = tag_value;
5827 return die;
5830 /* Create and return a new die with a parent of PARENT_DIE. If
5831 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5832 associated tree T must be supplied to determine parenthood
5833 later. */
5835 static inline dw_die_ref
5836 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5838 dw_die_ref die = new_die_raw (tag_value);
5840 if (parent_die != NULL)
5841 add_child_die (parent_die, die);
5842 else
5844 limbo_die_node *limbo_node;
5846 /* No DIEs created after early dwarf should end up in limbo,
5847 because the limbo list should not persist past LTO
5848 streaming. */
5849 if (tag_value != DW_TAG_compile_unit
5850 /* These are allowed because they're generated while
5851 breaking out COMDAT units late. */
5852 && tag_value != DW_TAG_type_unit
5853 && tag_value != DW_TAG_skeleton_unit
5854 && !early_dwarf
5855 /* Allow nested functions to live in limbo because they will
5856 only temporarily live there, as decls_for_scope will fix
5857 them up. */
5858 && (TREE_CODE (t) != FUNCTION_DECL
5859 || !decl_function_context (t))
5860 /* Same as nested functions above but for types. Types that
5861 are local to a function will be fixed in
5862 decls_for_scope. */
5863 && (!RECORD_OR_UNION_TYPE_P (t)
5864 || !TYPE_CONTEXT (t)
5865 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5866 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5867 especially in the ltrans stage, but once we implement LTO
5868 dwarf streaming, we should remove this exception. */
5869 && !in_lto_p)
5871 fprintf (stderr, "symbol ended up in limbo too late:");
5872 debug_generic_stmt (t);
5873 gcc_unreachable ();
5876 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5877 limbo_node->die = die;
5878 limbo_node->created_for = t;
5879 limbo_node->next = limbo_die_list;
5880 limbo_die_list = limbo_node;
5883 return die;
5886 /* Return the DIE associated with the given type specifier. */
5888 dw_die_ref
5889 lookup_type_die (tree type)
5891 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5892 if (die && die->removed)
5894 TYPE_SYMTAB_DIE (type) = NULL;
5895 TREE_ASM_WRITTEN (type) = 0;
5896 return NULL;
5898 return die;
5901 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5902 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5903 anonymous type instead the one of the naming typedef. */
5905 static inline dw_die_ref
5906 strip_naming_typedef (tree type, dw_die_ref type_die)
5908 if (type
5909 && TREE_CODE (type) == RECORD_TYPE
5910 && type_die
5911 && type_die->die_tag == DW_TAG_typedef
5912 && is_naming_typedef_decl (TYPE_NAME (type)))
5913 type_die = get_AT_ref (type_die, DW_AT_type);
5914 return type_die;
5917 /* Like lookup_type_die, but if type is an anonymous type named by a
5918 typedef[1], return the DIE of the anonymous type instead the one of
5919 the naming typedef. This is because in gen_typedef_die, we did
5920 equate the anonymous struct named by the typedef with the DIE of
5921 the naming typedef. So by default, lookup_type_die on an anonymous
5922 struct yields the DIE of the naming typedef.
5924 [1]: Read the comment of is_naming_typedef_decl to learn about what
5925 a naming typedef is. */
5927 static inline dw_die_ref
5928 lookup_type_die_strip_naming_typedef (tree type)
5930 dw_die_ref die = lookup_type_die (type);
5931 return strip_naming_typedef (type, die);
5934 /* Equate a DIE to a given type specifier. */
5936 static inline void
5937 equate_type_number_to_die (tree type, dw_die_ref type_die)
5939 TYPE_SYMTAB_DIE (type) = type_die;
5942 static dw_die_ref maybe_create_die_with_external_ref (tree);
5943 struct GTY(()) sym_off_pair
5945 const char * GTY((skip)) sym;
5946 unsigned HOST_WIDE_INT off;
5948 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5950 /* Returns a hash value for X (which really is a die_struct). */
5952 inline hashval_t
5953 decl_die_hasher::hash (die_node *x)
5955 return (hashval_t) x->decl_id;
5958 /* Return true if decl_id of die_struct X is the same as UID of decl *Y. */
5960 inline bool
5961 decl_die_hasher::equal (die_node *x, tree y)
5963 return (x->decl_id == DECL_UID (y));
5966 /* Return the DIE associated with a given declaration. */
5968 dw_die_ref
5969 lookup_decl_die (tree decl)
5971 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5972 NO_INSERT);
5973 if (!die)
5975 if (in_lto_p)
5976 return maybe_create_die_with_external_ref (decl);
5977 return NULL;
5979 if ((*die)->removed)
5981 decl_die_table->clear_slot (die);
5982 return NULL;
5984 return *die;
5988 /* Return the DIE associated with BLOCK. */
5990 static inline dw_die_ref
5991 lookup_block_die (tree block)
5993 dw_die_ref die = BLOCK_DIE (block);
5994 if (!die && in_lto_p)
5995 return maybe_create_die_with_external_ref (block);
5996 return die;
5999 /* Associate DIE with BLOCK. */
6001 static inline void
6002 equate_block_to_die (tree block, dw_die_ref die)
6004 BLOCK_DIE (block) = die;
6006 #undef BLOCK_DIE
6009 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
6010 style reference. Return true if we found one refering to a DIE for
6011 DECL, otherwise return false. */
6013 static bool
6014 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
6015 unsigned HOST_WIDE_INT *off)
6017 dw_die_ref die;
6019 if (in_lto_p)
6021 /* During WPA stage and incremental linking we use a hash-map
6022 to store the decl <-> label + offset map. */
6023 if (!external_die_map)
6024 return false;
6025 sym_off_pair *desc = external_die_map->get (decl);
6026 if (!desc)
6027 return false;
6028 *sym = desc->sym;
6029 *off = desc->off;
6030 return true;
6033 if (TREE_CODE (decl) == BLOCK)
6034 die = lookup_block_die (decl);
6035 else
6036 die = lookup_decl_die (decl);
6037 if (!die)
6038 return false;
6040 /* Similar to get_ref_die_offset_label, but using the "correct"
6041 label. */
6042 *off = die->die_offset;
6043 while (die->die_parent)
6044 die = die->die_parent;
6045 /* For the containing CU DIE we compute a die_symbol in
6046 compute_comp_unit_symbol. */
6047 if (die->die_tag == DW_TAG_compile_unit)
6049 gcc_assert (die->die_id.die_symbol != NULL);
6050 *sym = die->die_id.die_symbol;
6051 return true;
6053 /* While we can gracefully handle running into say a type unit
6054 we don't really want and consider this a bug. */
6055 if (flag_checking)
6056 gcc_unreachable ();
6057 return false;
6060 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6062 static void
6063 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6064 const char *symbol, HOST_WIDE_INT offset)
6066 /* Create a fake DIE that contains the reference. Don't use
6067 new_die because we don't want to end up in the limbo list. */
6068 /* ??? We probably want to share these, thus put a ref to the DIE
6069 we create here to the external_die_map entry. */
6070 dw_die_ref ref = new_die_raw (die->die_tag);
6071 ref->die_id.die_symbol = symbol;
6072 ref->die_offset = offset;
6073 ref->with_offset = 1;
6074 add_AT_die_ref (die, attr_kind, ref);
6077 /* Create a DIE for DECL if required and add a reference to a DIE
6078 at SYMBOL + OFFSET which contains attributes dumped early. */
6080 static void
6081 dwarf2out_register_external_die (tree decl, const char *sym,
6082 unsigned HOST_WIDE_INT off)
6084 if (debug_info_level == DINFO_LEVEL_NONE)
6085 return;
6087 if (!external_die_map)
6088 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
6089 gcc_checking_assert (!external_die_map->get (decl));
6090 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
6091 external_die_map->put (decl, p);
6094 /* If we have a registered external DIE for DECL return a new DIE for
6095 the concrete instance with an appropriate abstract origin. */
6097 static dw_die_ref
6098 maybe_create_die_with_external_ref (tree decl)
6100 if (!external_die_map)
6101 return NULL;
6102 sym_off_pair *desc = external_die_map->get (decl);
6103 if (!desc)
6104 return NULL;
6106 const char *sym = desc->sym;
6107 unsigned HOST_WIDE_INT off = desc->off;
6108 external_die_map->remove (decl);
6110 in_lto_p = false;
6111 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6112 ? lookup_block_die (decl) : lookup_decl_die (decl));
6113 gcc_assert (!die);
6114 in_lto_p = true;
6116 tree ctx;
6117 dw_die_ref parent = NULL;
6118 /* Need to lookup a DIE for the decls context - the containing
6119 function or translation unit. */
6120 if (TREE_CODE (decl) == BLOCK)
6122 ctx = BLOCK_SUPERCONTEXT (decl);
6123 /* ??? We do not output DIEs for all scopes thus skip as
6124 many DIEs as needed. */
6125 while (TREE_CODE (ctx) == BLOCK
6126 && !lookup_block_die (ctx))
6127 ctx = BLOCK_SUPERCONTEXT (ctx);
6129 else
6130 ctx = DECL_CONTEXT (decl);
6131 /* Peel types in the context stack. */
6132 while (ctx && TYPE_P (ctx))
6133 ctx = TYPE_CONTEXT (ctx);
6134 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6135 if (debug_info_level <= DINFO_LEVEL_TERSE)
6136 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6137 ctx = DECL_CONTEXT (ctx);
6138 if (ctx)
6140 if (TREE_CODE (ctx) == BLOCK)
6141 parent = lookup_block_die (ctx);
6142 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6143 /* Keep the 1:1 association during WPA. */
6144 && !flag_wpa
6145 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6146 /* Otherwise all late annotations go to the main CU which
6147 imports the original CUs. */
6148 parent = comp_unit_die ();
6149 else if (TREE_CODE (ctx) == FUNCTION_DECL
6150 && TREE_CODE (decl) != FUNCTION_DECL
6151 && TREE_CODE (decl) != PARM_DECL
6152 && TREE_CODE (decl) != RESULT_DECL
6153 && TREE_CODE (decl) != BLOCK)
6154 /* Leave function local entities parent determination to when
6155 we process scope vars. */
6157 else
6158 parent = lookup_decl_die (ctx);
6160 else
6161 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6162 Handle this case gracefully by globalizing stuff. */
6163 parent = comp_unit_die ();
6164 /* Create a DIE "stub". */
6165 switch (TREE_CODE (decl))
6167 case TRANSLATION_UNIT_DECL:
6169 die = comp_unit_die ();
6170 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6171 to create a DIE for the original CUs. */
6172 return die;
6174 case NAMESPACE_DECL:
6175 if (is_fortran (decl))
6176 die = new_die (DW_TAG_module, parent, decl);
6177 else
6178 die = new_die (DW_TAG_namespace, parent, decl);
6179 break;
6180 case FUNCTION_DECL:
6181 die = new_die (DW_TAG_subprogram, parent, decl);
6182 break;
6183 case VAR_DECL:
6184 die = new_die (DW_TAG_variable, parent, decl);
6185 break;
6186 case RESULT_DECL:
6187 die = new_die (DW_TAG_variable, parent, decl);
6188 break;
6189 case PARM_DECL:
6190 die = new_die (DW_TAG_formal_parameter, parent, decl);
6191 break;
6192 case CONST_DECL:
6193 die = new_die (DW_TAG_constant, parent, decl);
6194 break;
6195 case LABEL_DECL:
6196 die = new_die (DW_TAG_label, parent, decl);
6197 break;
6198 case BLOCK:
6199 die = new_die (DW_TAG_lexical_block, parent, decl);
6200 break;
6201 default:
6202 gcc_unreachable ();
6204 if (TREE_CODE (decl) == BLOCK)
6205 equate_block_to_die (decl, die);
6206 else
6207 equate_decl_number_to_die (decl, die);
6209 add_desc_attribute (die, decl);
6211 /* Add a reference to the DIE providing early debug at $sym + off. */
6212 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6214 return die;
6217 /* Returns a hash value for X (which really is a var_loc_list). */
6219 inline hashval_t
6220 decl_loc_hasher::hash (var_loc_list *x)
6222 return (hashval_t) x->decl_id;
6225 /* Return true if decl_id of var_loc_list X is the same as
6226 UID of decl *Y. */
6228 inline bool
6229 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6231 return (x->decl_id == DECL_UID (y));
6234 /* Return the var_loc list associated with a given declaration. */
6236 static inline var_loc_list *
6237 lookup_decl_loc (const_tree decl)
6239 if (!decl_loc_table)
6240 return NULL;
6241 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6244 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6246 inline hashval_t
6247 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6249 return (hashval_t) x->decl_id;
6252 /* Return true if decl_id of cached_dw_loc_list X is the same as
6253 UID of decl *Y. */
6255 inline bool
6256 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6258 return (x->decl_id == DECL_UID (y));
6261 /* Equate a DIE to a particular declaration. */
6263 static void
6264 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6266 unsigned int decl_id = DECL_UID (decl);
6268 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6269 decl_die->decl_id = decl_id;
6272 /* Return how many bits covers PIECE EXPR_LIST. */
6274 static HOST_WIDE_INT
6275 decl_piece_bitsize (rtx piece)
6277 int ret = (int) GET_MODE (piece);
6278 if (ret)
6279 return ret;
6280 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6281 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6282 return INTVAL (XEXP (XEXP (piece, 0), 0));
6285 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6287 static rtx *
6288 decl_piece_varloc_ptr (rtx piece)
6290 if ((int) GET_MODE (piece))
6291 return &XEXP (piece, 0);
6292 else
6293 return &XEXP (XEXP (piece, 0), 1);
6296 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6297 Next is the chain of following piece nodes. */
6299 static rtx_expr_list *
6300 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6302 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6303 return alloc_EXPR_LIST (bitsize, loc_note, next);
6304 else
6305 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6306 GEN_INT (bitsize),
6307 loc_note), next);
6310 /* Return rtx that should be stored into loc field for
6311 LOC_NOTE and BITPOS/BITSIZE. */
6313 static rtx
6314 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6315 HOST_WIDE_INT bitsize)
6317 if (bitsize != -1)
6319 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6320 if (bitpos != 0)
6321 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6323 return loc_note;
6326 /* This function either modifies location piece list *DEST in
6327 place (if SRC and INNER is NULL), or copies location piece list
6328 *SRC to *DEST while modifying it. Location BITPOS is modified
6329 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6330 not copied and if needed some padding around it is added.
6331 When modifying in place, DEST should point to EXPR_LIST where
6332 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6333 to the start of the whole list and INNER points to the EXPR_LIST
6334 where earlier pieces cover PIECE_BITPOS bits. */
6336 static void
6337 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6338 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6339 HOST_WIDE_INT bitsize, rtx loc_note)
6341 HOST_WIDE_INT diff;
6342 bool copy = inner != NULL;
6344 if (copy)
6346 /* First copy all nodes preceding the current bitpos. */
6347 while (src != inner)
6349 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6350 decl_piece_bitsize (*src), NULL_RTX);
6351 dest = &XEXP (*dest, 1);
6352 src = &XEXP (*src, 1);
6355 /* Add padding if needed. */
6356 if (bitpos != piece_bitpos)
6358 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6359 copy ? NULL_RTX : *dest);
6360 dest = &XEXP (*dest, 1);
6362 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6364 gcc_assert (!copy);
6365 /* A piece with correct bitpos and bitsize already exist,
6366 just update the location for it and return. */
6367 *decl_piece_varloc_ptr (*dest) = loc_note;
6368 return;
6370 /* Add the piece that changed. */
6371 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6372 dest = &XEXP (*dest, 1);
6373 /* Skip over pieces that overlap it. */
6374 diff = bitpos - piece_bitpos + bitsize;
6375 if (!copy)
6376 src = dest;
6377 while (diff > 0 && *src)
6379 rtx piece = *src;
6380 diff -= decl_piece_bitsize (piece);
6381 if (copy)
6382 src = &XEXP (piece, 1);
6383 else
6385 *src = XEXP (piece, 1);
6386 free_EXPR_LIST_node (piece);
6389 /* Add padding if needed. */
6390 if (diff < 0 && *src)
6392 if (!copy)
6393 dest = src;
6394 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6395 dest = &XEXP (*dest, 1);
6397 if (!copy)
6398 return;
6399 /* Finally copy all nodes following it. */
6400 while (*src)
6402 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6403 decl_piece_bitsize (*src), NULL_RTX);
6404 dest = &XEXP (*dest, 1);
6405 src = &XEXP (*src, 1);
6409 /* Add a variable location node to the linked list for DECL. */
6411 static struct var_loc_node *
6412 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6414 unsigned int decl_id;
6415 var_loc_list *temp;
6416 struct var_loc_node *loc = NULL;
6417 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6419 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6421 tree realdecl = DECL_DEBUG_EXPR (decl);
6422 if (handled_component_p (realdecl)
6423 || (TREE_CODE (realdecl) == MEM_REF
6424 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6426 bool reverse;
6427 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6428 &bitsize, &reverse);
6429 if (!innerdecl
6430 || !DECL_P (innerdecl)
6431 || DECL_IGNORED_P (innerdecl)
6432 || TREE_STATIC (innerdecl)
6433 || bitsize == 0
6434 || bitpos + bitsize > 256)
6435 return NULL;
6436 decl = innerdecl;
6440 decl_id = DECL_UID (decl);
6441 var_loc_list **slot
6442 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6443 if (*slot == NULL)
6445 temp = ggc_cleared_alloc<var_loc_list> ();
6446 temp->decl_id = decl_id;
6447 *slot = temp;
6449 else
6450 temp = *slot;
6452 /* For PARM_DECLs try to keep around the original incoming value,
6453 even if that means we'll emit a zero-range .debug_loc entry. */
6454 if (temp->last
6455 && temp->first == temp->last
6456 && TREE_CODE (decl) == PARM_DECL
6457 && NOTE_P (temp->first->loc)
6458 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6459 && DECL_INCOMING_RTL (decl)
6460 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6461 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6462 == GET_CODE (DECL_INCOMING_RTL (decl))
6463 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6464 && (bitsize != -1
6465 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6466 NOTE_VAR_LOCATION_LOC (loc_note))
6467 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6468 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6470 loc = ggc_cleared_alloc<var_loc_node> ();
6471 temp->first->next = loc;
6472 temp->last = loc;
6473 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6475 else if (temp->last)
6477 struct var_loc_node *last = temp->last, *unused = NULL;
6478 rtx *piece_loc = NULL, last_loc_note;
6479 HOST_WIDE_INT piece_bitpos = 0;
6480 if (last->next)
6482 last = last->next;
6483 gcc_assert (last->next == NULL);
6485 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6487 piece_loc = &last->loc;
6490 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6491 if (piece_bitpos + cur_bitsize > bitpos)
6492 break;
6493 piece_bitpos += cur_bitsize;
6494 piece_loc = &XEXP (*piece_loc, 1);
6496 while (*piece_loc);
6498 /* TEMP->LAST here is either pointer to the last but one or
6499 last element in the chained list, LAST is pointer to the
6500 last element. */
6501 if (label && strcmp (last->label, label) == 0 && last->view == view)
6503 /* For SRA optimized variables if there weren't any real
6504 insns since last note, just modify the last node. */
6505 if (piece_loc != NULL)
6507 adjust_piece_list (piece_loc, NULL, NULL,
6508 bitpos, piece_bitpos, bitsize, loc_note);
6509 return NULL;
6511 /* If the last note doesn't cover any instructions, remove it. */
6512 if (temp->last != last)
6514 temp->last->next = NULL;
6515 unused = last;
6516 last = temp->last;
6517 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6519 else
6521 gcc_assert (temp->first == temp->last
6522 || (temp->first->next == temp->last
6523 && TREE_CODE (decl) == PARM_DECL));
6524 memset (temp->last, '\0', sizeof (*temp->last));
6525 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6526 return temp->last;
6529 if (bitsize == -1 && NOTE_P (last->loc))
6530 last_loc_note = last->loc;
6531 else if (piece_loc != NULL
6532 && *piece_loc != NULL_RTX
6533 && piece_bitpos == bitpos
6534 && decl_piece_bitsize (*piece_loc) == bitsize)
6535 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6536 else
6537 last_loc_note = NULL_RTX;
6538 /* If the current location is the same as the end of the list,
6539 and either both or neither of the locations is uninitialized,
6540 we have nothing to do. */
6541 if (last_loc_note == NULL_RTX
6542 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6543 NOTE_VAR_LOCATION_LOC (loc_note)))
6544 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6545 != NOTE_VAR_LOCATION_STATUS (loc_note))
6546 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6547 == VAR_INIT_STATUS_UNINITIALIZED)
6548 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6549 == VAR_INIT_STATUS_UNINITIALIZED))))
6551 /* Add LOC to the end of list and update LAST. If the last
6552 element of the list has been removed above, reuse its
6553 memory for the new node, otherwise allocate a new one. */
6554 if (unused)
6556 loc = unused;
6557 memset (loc, '\0', sizeof (*loc));
6559 else
6560 loc = ggc_cleared_alloc<var_loc_node> ();
6561 if (bitsize == -1 || piece_loc == NULL)
6562 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6563 else
6564 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6565 bitpos, piece_bitpos, bitsize, loc_note);
6566 last->next = loc;
6567 /* Ensure TEMP->LAST will point either to the new last but one
6568 element of the chain, or to the last element in it. */
6569 if (last != temp->last)
6570 temp->last = last;
6572 else if (unused)
6573 ggc_free (unused);
6575 else
6577 loc = ggc_cleared_alloc<var_loc_node> ();
6578 temp->first = loc;
6579 temp->last = loc;
6580 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6582 return loc;
6585 /* Keep track of the number of spaces used to indent the
6586 output of the debugging routines that print the structure of
6587 the DIE internal representation. */
6588 static int print_indent;
6590 /* Indent the line the number of spaces given by print_indent. */
6592 static inline void
6593 print_spaces (FILE *outfile)
6595 fprintf (outfile, "%*s", print_indent, "");
6598 /* Print a type signature in hex. */
6600 static inline void
6601 print_signature (FILE *outfile, char *sig)
6603 int i;
6605 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6606 fprintf (outfile, "%02x", sig[i] & 0xff);
6609 static inline void
6610 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6612 if (discr_value->pos)
6613 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6614 else
6615 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6618 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6620 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6621 RECURSE, output location descriptor operations. */
6623 static void
6624 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6626 switch (val->val_class)
6628 case dw_val_class_addr:
6629 fprintf (outfile, "address");
6630 break;
6631 case dw_val_class_offset:
6632 fprintf (outfile, "offset");
6633 break;
6634 case dw_val_class_loc:
6635 fprintf (outfile, "location descriptor");
6636 if (val->v.val_loc == NULL)
6637 fprintf (outfile, " -> <null>");
6638 else if (recurse)
6640 fprintf (outfile, ":\n");
6641 print_indent += 4;
6642 print_loc_descr (val->v.val_loc, outfile);
6643 print_indent -= 4;
6645 else
6647 if (flag_dump_noaddr || flag_dump_unnumbered)
6648 fprintf (outfile, " #");
6649 else
6650 fprintf (outfile, " (%p)", (void *) val->v.val_loc);
6652 break;
6653 case dw_val_class_loc_list:
6654 fprintf (outfile, "location list -> label:%s",
6655 val->v.val_loc_list->ll_symbol);
6656 break;
6657 case dw_val_class_view_list:
6658 val = view_list_to_loc_list_val_node (val);
6659 fprintf (outfile, "location list with views -> labels:%s and %s",
6660 val->v.val_loc_list->ll_symbol,
6661 val->v.val_loc_list->vl_symbol);
6662 break;
6663 case dw_val_class_range_list:
6664 fprintf (outfile, "range list");
6665 break;
6666 case dw_val_class_const:
6667 case dw_val_class_const_implicit:
6668 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6669 break;
6670 case dw_val_class_unsigned_const:
6671 case dw_val_class_unsigned_const_implicit:
6672 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6673 break;
6674 case dw_val_class_const_double:
6675 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6676 HOST_WIDE_INT_PRINT_UNSIGNED")",
6677 val->v.val_double.high,
6678 val->v.val_double.low);
6679 break;
6680 case dw_val_class_wide_int:
6682 int i = val->v.val_wide->get_len ();
6683 fprintf (outfile, "constant (");
6684 gcc_assert (i > 0);
6685 if (val->v.val_wide->elt (i - 1) == 0)
6686 fprintf (outfile, "0x");
6687 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6688 val->v.val_wide->elt (--i));
6689 while (--i >= 0)
6690 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6691 val->v.val_wide->elt (i));
6692 fprintf (outfile, ")");
6693 break;
6695 case dw_val_class_vec:
6696 fprintf (outfile, "floating-point or vector constant");
6697 break;
6698 case dw_val_class_flag:
6699 fprintf (outfile, "%u", val->v.val_flag);
6700 break;
6701 case dw_val_class_die_ref:
6702 if (val->v.val_die_ref.die != NULL)
6704 dw_die_ref die = val->v.val_die_ref.die;
6706 if (die->comdat_type_p)
6708 fprintf (outfile, "die -> signature: ");
6709 print_signature (outfile,
6710 die->die_id.die_type_node->signature);
6712 else if (die->die_id.die_symbol)
6714 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6715 if (die->with_offset)
6716 fprintf (outfile, " + %ld", die->die_offset);
6718 else
6719 fprintf (outfile, "die -> %ld", die->die_offset);
6720 if (flag_dump_noaddr || flag_dump_unnumbered)
6721 fprintf (outfile, " #");
6722 else
6723 fprintf (outfile, " (%p)", (void *) die);
6725 else
6726 fprintf (outfile, "die -> <null>");
6727 break;
6728 case dw_val_class_vms_delta:
6729 fprintf (outfile, "delta: @slotcount(%s-%s)",
6730 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6731 break;
6732 case dw_val_class_symview:
6733 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6734 break;
6735 case dw_val_class_lbl_id:
6736 case dw_val_class_lineptr:
6737 case dw_val_class_macptr:
6738 case dw_val_class_loclistsptr:
6739 case dw_val_class_high_pc:
6740 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6741 break;
6742 case dw_val_class_str:
6743 if (val->v.val_str->str != NULL)
6744 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6745 else
6746 fprintf (outfile, "<null>");
6747 break;
6748 case dw_val_class_file:
6749 case dw_val_class_file_implicit:
6750 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6751 val->v.val_file->emitted_number);
6752 break;
6753 case dw_val_class_data8:
6755 int i;
6757 for (i = 0; i < 8; i++)
6758 fprintf (outfile, "%02x", val->v.val_data8[i]);
6759 break;
6761 case dw_val_class_discr_value:
6762 print_discr_value (outfile, &val->v.val_discr_value);
6763 break;
6764 case dw_val_class_discr_list:
6765 for (dw_discr_list_ref node = val->v.val_discr_list;
6766 node != NULL;
6767 node = node->dw_discr_next)
6769 if (node->dw_discr_range)
6771 fprintf (outfile, " .. ");
6772 print_discr_value (outfile, &node->dw_discr_lower_bound);
6773 print_discr_value (outfile, &node->dw_discr_upper_bound);
6775 else
6776 print_discr_value (outfile, &node->dw_discr_lower_bound);
6778 if (node->dw_discr_next != NULL)
6779 fprintf (outfile, " | ");
6781 default:
6782 break;
6786 /* Likewise, for a DIE attribute. */
6788 static void
6789 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6791 print_dw_val (&a->dw_attr_val, recurse, outfile);
6795 /* Print the list of operands in the LOC location description to OUTFILE. This
6796 routine is a debugging aid only. */
6798 static void
6799 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6801 dw_loc_descr_ref l = loc;
6803 if (loc == NULL)
6805 print_spaces (outfile);
6806 fprintf (outfile, "<null>\n");
6807 return;
6810 for (l = loc; l != NULL; l = l->dw_loc_next)
6812 print_spaces (outfile);
6813 if (flag_dump_noaddr || flag_dump_unnumbered)
6814 fprintf (outfile, "#");
6815 else
6816 fprintf (outfile, "(%p)", (void *) l);
6817 fprintf (outfile, " %s",
6818 dwarf_stack_op_name (l->dw_loc_opc));
6819 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6821 fprintf (outfile, " ");
6822 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6824 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6826 fprintf (outfile, ", ");
6827 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6829 fprintf (outfile, "\n");
6833 /* Print the information associated with a given DIE, and its children.
6834 This routine is a debugging aid only. */
6836 static void
6837 print_die (dw_die_ref die, FILE *outfile)
6839 dw_attr_node *a;
6840 dw_die_ref c;
6841 unsigned ix;
6843 print_spaces (outfile);
6844 fprintf (outfile, "DIE %4ld: %s ",
6845 die->die_offset, dwarf_tag_name (die->die_tag));
6846 if (flag_dump_noaddr || flag_dump_unnumbered)
6847 fprintf (outfile, "#\n");
6848 else
6849 fprintf (outfile, "(%p)\n", (void*) die);
6850 print_spaces (outfile);
6851 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6852 fprintf (outfile, " offset: %ld", die->die_offset);
6853 fprintf (outfile, " mark: %d\n", die->die_mark);
6855 if (die->comdat_type_p)
6857 print_spaces (outfile);
6858 fprintf (outfile, " signature: ");
6859 print_signature (outfile, die->die_id.die_type_node->signature);
6860 fprintf (outfile, "\n");
6863 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6865 print_spaces (outfile);
6866 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6868 print_attribute (a, true, outfile);
6869 fprintf (outfile, "\n");
6872 if (die->die_child != NULL)
6874 print_indent += 4;
6875 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6876 print_indent -= 4;
6878 if (print_indent == 0)
6879 fprintf (outfile, "\n");
6882 /* Print the list of operations in the LOC location description. */
6884 DEBUG_FUNCTION void
6885 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6887 print_loc_descr (loc, stderr);
6890 /* Print the information collected for a given DIE. */
6892 DEBUG_FUNCTION void
6893 debug_dwarf_die (dw_die_ref die)
6895 print_die (die, stderr);
6898 DEBUG_FUNCTION void
6899 debug (die_struct &ref)
6901 print_die (&ref, stderr);
6904 DEBUG_FUNCTION void
6905 debug (die_struct *ptr)
6907 if (ptr)
6908 debug (*ptr);
6909 else
6910 fprintf (stderr, "<nil>\n");
6914 /* Print all DWARF information collected for the compilation unit.
6915 This routine is a debugging aid only. */
6917 DEBUG_FUNCTION void
6918 debug_dwarf (void)
6920 print_indent = 0;
6921 print_die (comp_unit_die (), stderr);
6924 /* Verify the DIE tree structure. */
6926 DEBUG_FUNCTION void
6927 verify_die (dw_die_ref die)
6929 gcc_assert (!die->die_mark);
6930 if (die->die_parent == NULL
6931 && die->die_sib == NULL)
6932 return;
6933 /* Verify the die_sib list is cyclic. */
6934 dw_die_ref x = die;
6937 x->die_mark = 1;
6938 x = x->die_sib;
6940 while (x && !x->die_mark);
6941 gcc_assert (x == die);
6942 x = die;
6945 /* Verify all dies have the same parent. */
6946 gcc_assert (x->die_parent == die->die_parent);
6947 if (x->die_child)
6949 /* Verify the child has the proper parent and recurse. */
6950 gcc_assert (x->die_child->die_parent == x);
6951 verify_die (x->die_child);
6953 x->die_mark = 0;
6954 x = x->die_sib;
6956 while (x && x->die_mark);
6959 /* Sanity checks on DIEs. */
6961 static void
6962 check_die (dw_die_ref die)
6964 unsigned ix;
6965 dw_attr_node *a;
6966 bool inline_found = false;
6967 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6968 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6969 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6971 switch (a->dw_attr)
6973 case DW_AT_inline:
6974 if (a->dw_attr_val.v.val_unsigned)
6975 inline_found = true;
6976 break;
6977 case DW_AT_location:
6978 ++n_location;
6979 break;
6980 case DW_AT_low_pc:
6981 ++n_low_pc;
6982 break;
6983 case DW_AT_high_pc:
6984 ++n_high_pc;
6985 break;
6986 case DW_AT_artificial:
6987 ++n_artificial;
6988 break;
6989 case DW_AT_decl_column:
6990 ++n_decl_column;
6991 break;
6992 case DW_AT_decl_line:
6993 ++n_decl_line;
6994 break;
6995 case DW_AT_decl_file:
6996 ++n_decl_file;
6997 break;
6998 default:
6999 break;
7002 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
7003 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
7005 fprintf (stderr, "Duplicate attributes in DIE:\n");
7006 debug_dwarf_die (die);
7007 gcc_unreachable ();
7009 if (inline_found)
7011 /* A debugging information entry that is a member of an abstract
7012 instance tree [that has DW_AT_inline] should not contain any
7013 attributes which describe aspects of the subroutine which vary
7014 between distinct inlined expansions or distinct out-of-line
7015 expansions. */
7016 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7017 gcc_assert (a->dw_attr != DW_AT_low_pc
7018 && a->dw_attr != DW_AT_high_pc
7019 && a->dw_attr != DW_AT_location
7020 && a->dw_attr != DW_AT_frame_base
7021 && a->dw_attr != DW_AT_call_all_calls
7022 && a->dw_attr != DW_AT_GNU_all_call_sites);
7026 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7027 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7028 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7030 /* Calculate the checksum of a location expression. */
7032 static inline void
7033 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7035 int tem;
7036 inchash::hash hstate;
7037 hashval_t hash;
7039 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7040 CHECKSUM (tem);
7041 hash_loc_operands (loc, hstate);
7042 hash = hstate.end();
7043 CHECKSUM (hash);
7046 /* Calculate the checksum of an attribute. */
7048 static void
7049 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
7051 dw_loc_descr_ref loc;
7052 rtx r;
7054 CHECKSUM (at->dw_attr);
7056 /* We don't care that this was compiled with a different compiler
7057 snapshot; if the output is the same, that's what matters. */
7058 if (at->dw_attr == DW_AT_producer)
7059 return;
7061 switch (AT_class (at))
7063 case dw_val_class_const:
7064 case dw_val_class_const_implicit:
7065 CHECKSUM (at->dw_attr_val.v.val_int);
7066 break;
7067 case dw_val_class_unsigned_const:
7068 case dw_val_class_unsigned_const_implicit:
7069 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7070 break;
7071 case dw_val_class_const_double:
7072 CHECKSUM (at->dw_attr_val.v.val_double);
7073 break;
7074 case dw_val_class_wide_int:
7075 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7076 get_full_len (*at->dw_attr_val.v.val_wide)
7077 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7078 break;
7079 case dw_val_class_vec:
7080 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7081 (at->dw_attr_val.v.val_vec.length
7082 * at->dw_attr_val.v.val_vec.elt_size));
7083 break;
7084 case dw_val_class_flag:
7085 CHECKSUM (at->dw_attr_val.v.val_flag);
7086 break;
7087 case dw_val_class_str:
7088 CHECKSUM_STRING (AT_string (at));
7089 break;
7091 case dw_val_class_addr:
7092 r = AT_addr (at);
7093 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7094 CHECKSUM_STRING (XSTR (r, 0));
7095 break;
7097 case dw_val_class_offset:
7098 CHECKSUM (at->dw_attr_val.v.val_offset);
7099 break;
7101 case dw_val_class_loc:
7102 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7103 loc_checksum (loc, ctx);
7104 break;
7106 case dw_val_class_die_ref:
7107 die_checksum (AT_ref (at), ctx, mark);
7108 break;
7110 case dw_val_class_fde_ref:
7111 case dw_val_class_vms_delta:
7112 case dw_val_class_symview:
7113 case dw_val_class_lbl_id:
7114 case dw_val_class_lineptr:
7115 case dw_val_class_macptr:
7116 case dw_val_class_loclistsptr:
7117 case dw_val_class_high_pc:
7118 break;
7120 case dw_val_class_file:
7121 case dw_val_class_file_implicit:
7122 CHECKSUM_STRING (AT_file (at)->filename);
7123 break;
7125 case dw_val_class_data8:
7126 CHECKSUM (at->dw_attr_val.v.val_data8);
7127 break;
7129 default:
7130 break;
7134 /* Calculate the checksum of a DIE. */
7136 static void
7137 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7139 dw_die_ref c;
7140 dw_attr_node *a;
7141 unsigned ix;
7143 /* To avoid infinite recursion. */
7144 if (die->die_mark)
7146 CHECKSUM (die->die_mark);
7147 return;
7149 die->die_mark = ++(*mark);
7151 CHECKSUM (die->die_tag);
7153 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7154 attr_checksum (a, ctx, mark);
7156 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7159 #undef CHECKSUM
7160 #undef CHECKSUM_BLOCK
7161 #undef CHECKSUM_STRING
7163 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7164 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7165 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7166 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7167 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7168 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7169 #define CHECKSUM_ATTR(FOO) \
7170 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7172 /* Calculate the checksum of a number in signed LEB128 format. */
7174 static void
7175 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7177 unsigned char byte;
7178 bool more;
7180 while (1)
7182 byte = (value & 0x7f);
7183 value >>= 7;
7184 more = !((value == 0 && (byte & 0x40) == 0)
7185 || (value == -1 && (byte & 0x40) != 0));
7186 if (more)
7187 byte |= 0x80;
7188 CHECKSUM (byte);
7189 if (!more)
7190 break;
7194 /* Calculate the checksum of a number in unsigned LEB128 format. */
7196 static void
7197 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7199 while (1)
7201 unsigned char byte = (value & 0x7f);
7202 value >>= 7;
7203 if (value != 0)
7204 /* More bytes to follow. */
7205 byte |= 0x80;
7206 CHECKSUM (byte);
7207 if (value == 0)
7208 break;
7212 /* Checksum the context of the DIE. This adds the names of any
7213 surrounding namespaces or structures to the checksum. */
7215 static void
7216 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7218 const char *name;
7219 dw_die_ref spec;
7220 int tag = die->die_tag;
7222 if (tag != DW_TAG_namespace
7223 && tag != DW_TAG_structure_type
7224 && tag != DW_TAG_class_type)
7225 return;
7227 name = get_AT_string (die, DW_AT_name);
7229 spec = get_AT_ref (die, DW_AT_specification);
7230 if (spec != NULL)
7231 die = spec;
7233 if (die->die_parent != NULL)
7234 checksum_die_context (die->die_parent, ctx);
7236 CHECKSUM_ULEB128 ('C');
7237 CHECKSUM_ULEB128 (tag);
7238 if (name != NULL)
7239 CHECKSUM_STRING (name);
7242 /* Calculate the checksum of a location expression. */
7244 static inline void
7245 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7247 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7248 were emitted as a DW_FORM_sdata instead of a location expression. */
7249 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7251 CHECKSUM_ULEB128 (DW_FORM_sdata);
7252 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7253 return;
7256 /* Otherwise, just checksum the raw location expression. */
7257 while (loc != NULL)
7259 inchash::hash hstate;
7260 hashval_t hash;
7262 CHECKSUM_ULEB128 (loc->dtprel);
7263 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7264 hash_loc_operands (loc, hstate);
7265 hash = hstate.end ();
7266 CHECKSUM (hash);
7267 loc = loc->dw_loc_next;
7271 /* Calculate the checksum of an attribute. */
7273 static void
7274 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7275 struct md5_ctx *ctx, int *mark)
7277 dw_loc_descr_ref loc;
7278 rtx r;
7280 if (AT_class (at) == dw_val_class_die_ref)
7282 dw_die_ref target_die = AT_ref (at);
7284 /* For pointer and reference types, we checksum only the (qualified)
7285 name of the target type (if there is a name). For friend entries,
7286 we checksum only the (qualified) name of the target type or function.
7287 This allows the checksum to remain the same whether the target type
7288 is complete or not. */
7289 if ((at->dw_attr == DW_AT_type
7290 && (tag == DW_TAG_pointer_type
7291 || tag == DW_TAG_reference_type
7292 || tag == DW_TAG_rvalue_reference_type
7293 || tag == DW_TAG_ptr_to_member_type))
7294 || (at->dw_attr == DW_AT_friend
7295 && tag == DW_TAG_friend))
7297 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7299 if (name_attr != NULL)
7301 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7303 if (decl == NULL)
7304 decl = target_die;
7305 CHECKSUM_ULEB128 ('N');
7306 CHECKSUM_ULEB128 (at->dw_attr);
7307 if (decl->die_parent != NULL)
7308 checksum_die_context (decl->die_parent, ctx);
7309 CHECKSUM_ULEB128 ('E');
7310 CHECKSUM_STRING (AT_string (name_attr));
7311 return;
7315 /* For all other references to another DIE, we check to see if the
7316 target DIE has already been visited. If it has, we emit a
7317 backward reference; if not, we descend recursively. */
7318 if (target_die->die_mark > 0)
7320 CHECKSUM_ULEB128 ('R');
7321 CHECKSUM_ULEB128 (at->dw_attr);
7322 CHECKSUM_ULEB128 (target_die->die_mark);
7324 else
7326 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7328 if (decl == NULL)
7329 decl = target_die;
7330 target_die->die_mark = ++(*mark);
7331 CHECKSUM_ULEB128 ('T');
7332 CHECKSUM_ULEB128 (at->dw_attr);
7333 if (decl->die_parent != NULL)
7334 checksum_die_context (decl->die_parent, ctx);
7335 die_checksum_ordered (target_die, ctx, mark);
7337 return;
7340 CHECKSUM_ULEB128 ('A');
7341 CHECKSUM_ULEB128 (at->dw_attr);
7343 switch (AT_class (at))
7345 case dw_val_class_const:
7346 case dw_val_class_const_implicit:
7347 CHECKSUM_ULEB128 (DW_FORM_sdata);
7348 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7349 break;
7351 case dw_val_class_unsigned_const:
7352 case dw_val_class_unsigned_const_implicit:
7353 CHECKSUM_ULEB128 (DW_FORM_sdata);
7354 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7355 break;
7357 case dw_val_class_const_double:
7358 CHECKSUM_ULEB128 (DW_FORM_block);
7359 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7360 CHECKSUM (at->dw_attr_val.v.val_double);
7361 break;
7363 case dw_val_class_wide_int:
7364 CHECKSUM_ULEB128 (DW_FORM_block);
7365 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7366 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7367 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7368 get_full_len (*at->dw_attr_val.v.val_wide)
7369 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7370 break;
7372 case dw_val_class_vec:
7373 CHECKSUM_ULEB128 (DW_FORM_block);
7374 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7375 * at->dw_attr_val.v.val_vec.elt_size);
7376 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7377 (at->dw_attr_val.v.val_vec.length
7378 * at->dw_attr_val.v.val_vec.elt_size));
7379 break;
7381 case dw_val_class_flag:
7382 CHECKSUM_ULEB128 (DW_FORM_flag);
7383 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7384 break;
7386 case dw_val_class_str:
7387 CHECKSUM_ULEB128 (DW_FORM_string);
7388 CHECKSUM_STRING (AT_string (at));
7389 break;
7391 case dw_val_class_addr:
7392 r = AT_addr (at);
7393 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7394 CHECKSUM_ULEB128 (DW_FORM_string);
7395 CHECKSUM_STRING (XSTR (r, 0));
7396 break;
7398 case dw_val_class_offset:
7399 CHECKSUM_ULEB128 (DW_FORM_sdata);
7400 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7401 break;
7403 case dw_val_class_loc:
7404 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7405 loc_checksum_ordered (loc, ctx);
7406 break;
7408 case dw_val_class_fde_ref:
7409 case dw_val_class_symview:
7410 case dw_val_class_lbl_id:
7411 case dw_val_class_lineptr:
7412 case dw_val_class_macptr:
7413 case dw_val_class_loclistsptr:
7414 case dw_val_class_high_pc:
7415 break;
7417 case dw_val_class_file:
7418 case dw_val_class_file_implicit:
7419 CHECKSUM_ULEB128 (DW_FORM_string);
7420 CHECKSUM_STRING (AT_file (at)->filename);
7421 break;
7423 case dw_val_class_data8:
7424 CHECKSUM (at->dw_attr_val.v.val_data8);
7425 break;
7427 default:
7428 break;
7432 struct checksum_attributes
7434 dw_attr_node *at_name;
7435 dw_attr_node *at_type;
7436 dw_attr_node *at_friend;
7437 dw_attr_node *at_accessibility;
7438 dw_attr_node *at_address_class;
7439 dw_attr_node *at_alignment;
7440 dw_attr_node *at_allocated;
7441 dw_attr_node *at_artificial;
7442 dw_attr_node *at_associated;
7443 dw_attr_node *at_binary_scale;
7444 dw_attr_node *at_bit_offset;
7445 dw_attr_node *at_bit_size;
7446 dw_attr_node *at_bit_stride;
7447 dw_attr_node *at_byte_size;
7448 dw_attr_node *at_byte_stride;
7449 dw_attr_node *at_const_value;
7450 dw_attr_node *at_containing_type;
7451 dw_attr_node *at_count;
7452 dw_attr_node *at_data_location;
7453 dw_attr_node *at_data_member_location;
7454 dw_attr_node *at_decimal_scale;
7455 dw_attr_node *at_decimal_sign;
7456 dw_attr_node *at_default_value;
7457 dw_attr_node *at_digit_count;
7458 dw_attr_node *at_discr;
7459 dw_attr_node *at_discr_list;
7460 dw_attr_node *at_discr_value;
7461 dw_attr_node *at_encoding;
7462 dw_attr_node *at_endianity;
7463 dw_attr_node *at_explicit;
7464 dw_attr_node *at_is_optional;
7465 dw_attr_node *at_location;
7466 dw_attr_node *at_lower_bound;
7467 dw_attr_node *at_mutable;
7468 dw_attr_node *at_ordering;
7469 dw_attr_node *at_picture_string;
7470 dw_attr_node *at_prototyped;
7471 dw_attr_node *at_small;
7472 dw_attr_node *at_segment;
7473 dw_attr_node *at_string_length;
7474 dw_attr_node *at_string_length_bit_size;
7475 dw_attr_node *at_string_length_byte_size;
7476 dw_attr_node *at_threads_scaled;
7477 dw_attr_node *at_upper_bound;
7478 dw_attr_node *at_use_location;
7479 dw_attr_node *at_use_UTF8;
7480 dw_attr_node *at_variable_parameter;
7481 dw_attr_node *at_virtuality;
7482 dw_attr_node *at_visibility;
7483 dw_attr_node *at_vtable_elem_location;
7486 /* Collect the attributes that we will want to use for the checksum. */
7488 static void
7489 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7491 dw_attr_node *a;
7492 unsigned ix;
7494 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7496 switch (a->dw_attr)
7498 case DW_AT_name:
7499 attrs->at_name = a;
7500 break;
7501 case DW_AT_type:
7502 attrs->at_type = a;
7503 break;
7504 case DW_AT_friend:
7505 attrs->at_friend = a;
7506 break;
7507 case DW_AT_accessibility:
7508 attrs->at_accessibility = a;
7509 break;
7510 case DW_AT_address_class:
7511 attrs->at_address_class = a;
7512 break;
7513 case DW_AT_alignment:
7514 attrs->at_alignment = a;
7515 break;
7516 case DW_AT_allocated:
7517 attrs->at_allocated = a;
7518 break;
7519 case DW_AT_artificial:
7520 attrs->at_artificial = a;
7521 break;
7522 case DW_AT_associated:
7523 attrs->at_associated = a;
7524 break;
7525 case DW_AT_binary_scale:
7526 attrs->at_binary_scale = a;
7527 break;
7528 case DW_AT_bit_offset:
7529 attrs->at_bit_offset = a;
7530 break;
7531 case DW_AT_bit_size:
7532 attrs->at_bit_size = a;
7533 break;
7534 case DW_AT_bit_stride:
7535 attrs->at_bit_stride = a;
7536 break;
7537 case DW_AT_byte_size:
7538 attrs->at_byte_size = a;
7539 break;
7540 case DW_AT_byte_stride:
7541 attrs->at_byte_stride = a;
7542 break;
7543 case DW_AT_const_value:
7544 attrs->at_const_value = a;
7545 break;
7546 case DW_AT_containing_type:
7547 attrs->at_containing_type = a;
7548 break;
7549 case DW_AT_count:
7550 attrs->at_count = a;
7551 break;
7552 case DW_AT_data_location:
7553 attrs->at_data_location = a;
7554 break;
7555 case DW_AT_data_member_location:
7556 attrs->at_data_member_location = a;
7557 break;
7558 case DW_AT_decimal_scale:
7559 attrs->at_decimal_scale = a;
7560 break;
7561 case DW_AT_decimal_sign:
7562 attrs->at_decimal_sign = a;
7563 break;
7564 case DW_AT_default_value:
7565 attrs->at_default_value = a;
7566 break;
7567 case DW_AT_digit_count:
7568 attrs->at_digit_count = a;
7569 break;
7570 case DW_AT_discr:
7571 attrs->at_discr = a;
7572 break;
7573 case DW_AT_discr_list:
7574 attrs->at_discr_list = a;
7575 break;
7576 case DW_AT_discr_value:
7577 attrs->at_discr_value = a;
7578 break;
7579 case DW_AT_encoding:
7580 attrs->at_encoding = a;
7581 break;
7582 case DW_AT_endianity:
7583 attrs->at_endianity = a;
7584 break;
7585 case DW_AT_explicit:
7586 attrs->at_explicit = a;
7587 break;
7588 case DW_AT_is_optional:
7589 attrs->at_is_optional = a;
7590 break;
7591 case DW_AT_location:
7592 attrs->at_location = a;
7593 break;
7594 case DW_AT_lower_bound:
7595 attrs->at_lower_bound = a;
7596 break;
7597 case DW_AT_mutable:
7598 attrs->at_mutable = a;
7599 break;
7600 case DW_AT_ordering:
7601 attrs->at_ordering = a;
7602 break;
7603 case DW_AT_picture_string:
7604 attrs->at_picture_string = a;
7605 break;
7606 case DW_AT_prototyped:
7607 attrs->at_prototyped = a;
7608 break;
7609 case DW_AT_small:
7610 attrs->at_small = a;
7611 break;
7612 case DW_AT_segment:
7613 attrs->at_segment = a;
7614 break;
7615 case DW_AT_string_length:
7616 attrs->at_string_length = a;
7617 break;
7618 case DW_AT_string_length_bit_size:
7619 attrs->at_string_length_bit_size = a;
7620 break;
7621 case DW_AT_string_length_byte_size:
7622 attrs->at_string_length_byte_size = a;
7623 break;
7624 case DW_AT_threads_scaled:
7625 attrs->at_threads_scaled = a;
7626 break;
7627 case DW_AT_upper_bound:
7628 attrs->at_upper_bound = a;
7629 break;
7630 case DW_AT_use_location:
7631 attrs->at_use_location = a;
7632 break;
7633 case DW_AT_use_UTF8:
7634 attrs->at_use_UTF8 = a;
7635 break;
7636 case DW_AT_variable_parameter:
7637 attrs->at_variable_parameter = a;
7638 break;
7639 case DW_AT_virtuality:
7640 attrs->at_virtuality = a;
7641 break;
7642 case DW_AT_visibility:
7643 attrs->at_visibility = a;
7644 break;
7645 case DW_AT_vtable_elem_location:
7646 attrs->at_vtable_elem_location = a;
7647 break;
7648 default:
7649 break;
7654 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7656 static void
7657 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7659 dw_die_ref c;
7660 dw_die_ref decl;
7661 struct checksum_attributes attrs;
7663 CHECKSUM_ULEB128 ('D');
7664 CHECKSUM_ULEB128 (die->die_tag);
7666 memset (&attrs, 0, sizeof (attrs));
7668 decl = get_AT_ref (die, DW_AT_specification);
7669 if (decl != NULL)
7670 collect_checksum_attributes (&attrs, decl);
7671 collect_checksum_attributes (&attrs, die);
7673 CHECKSUM_ATTR (attrs.at_name);
7674 CHECKSUM_ATTR (attrs.at_accessibility);
7675 CHECKSUM_ATTR (attrs.at_address_class);
7676 CHECKSUM_ATTR (attrs.at_allocated);
7677 CHECKSUM_ATTR (attrs.at_artificial);
7678 CHECKSUM_ATTR (attrs.at_associated);
7679 CHECKSUM_ATTR (attrs.at_binary_scale);
7680 CHECKSUM_ATTR (attrs.at_bit_offset);
7681 CHECKSUM_ATTR (attrs.at_bit_size);
7682 CHECKSUM_ATTR (attrs.at_bit_stride);
7683 CHECKSUM_ATTR (attrs.at_byte_size);
7684 CHECKSUM_ATTR (attrs.at_byte_stride);
7685 CHECKSUM_ATTR (attrs.at_const_value);
7686 CHECKSUM_ATTR (attrs.at_containing_type);
7687 CHECKSUM_ATTR (attrs.at_count);
7688 CHECKSUM_ATTR (attrs.at_data_location);
7689 CHECKSUM_ATTR (attrs.at_data_member_location);
7690 CHECKSUM_ATTR (attrs.at_decimal_scale);
7691 CHECKSUM_ATTR (attrs.at_decimal_sign);
7692 CHECKSUM_ATTR (attrs.at_default_value);
7693 CHECKSUM_ATTR (attrs.at_digit_count);
7694 CHECKSUM_ATTR (attrs.at_discr);
7695 CHECKSUM_ATTR (attrs.at_discr_list);
7696 CHECKSUM_ATTR (attrs.at_discr_value);
7697 CHECKSUM_ATTR (attrs.at_encoding);
7698 CHECKSUM_ATTR (attrs.at_endianity);
7699 CHECKSUM_ATTR (attrs.at_explicit);
7700 CHECKSUM_ATTR (attrs.at_is_optional);
7701 CHECKSUM_ATTR (attrs.at_location);
7702 CHECKSUM_ATTR (attrs.at_lower_bound);
7703 CHECKSUM_ATTR (attrs.at_mutable);
7704 CHECKSUM_ATTR (attrs.at_ordering);
7705 CHECKSUM_ATTR (attrs.at_picture_string);
7706 CHECKSUM_ATTR (attrs.at_prototyped);
7707 CHECKSUM_ATTR (attrs.at_small);
7708 CHECKSUM_ATTR (attrs.at_segment);
7709 CHECKSUM_ATTR (attrs.at_string_length);
7710 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7711 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7712 CHECKSUM_ATTR (attrs.at_threads_scaled);
7713 CHECKSUM_ATTR (attrs.at_upper_bound);
7714 CHECKSUM_ATTR (attrs.at_use_location);
7715 CHECKSUM_ATTR (attrs.at_use_UTF8);
7716 CHECKSUM_ATTR (attrs.at_variable_parameter);
7717 CHECKSUM_ATTR (attrs.at_virtuality);
7718 CHECKSUM_ATTR (attrs.at_visibility);
7719 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7720 CHECKSUM_ATTR (attrs.at_type);
7721 CHECKSUM_ATTR (attrs.at_friend);
7722 CHECKSUM_ATTR (attrs.at_alignment);
7724 /* Checksum the child DIEs. */
7725 c = die->die_child;
7726 if (c) do {
7727 dw_attr_node *name_attr;
7729 c = c->die_sib;
7730 name_attr = get_AT (c, DW_AT_name);
7731 if (is_template_instantiation (c))
7733 /* Ignore instantiations of member type and function templates. */
7735 else if (name_attr != NULL
7736 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7738 /* Use a shallow checksum for named nested types and member
7739 functions. */
7740 CHECKSUM_ULEB128 ('S');
7741 CHECKSUM_ULEB128 (c->die_tag);
7742 CHECKSUM_STRING (AT_string (name_attr));
7744 else
7746 /* Use a deep checksum for other children. */
7747 /* Mark this DIE so it gets processed when unmarking. */
7748 if (c->die_mark == 0)
7749 c->die_mark = -1;
7750 die_checksum_ordered (c, ctx, mark);
7752 } while (c != die->die_child);
7754 CHECKSUM_ULEB128 (0);
7757 /* Add a type name and tag to a hash. */
7758 static void
7759 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7761 CHECKSUM_ULEB128 (tag);
7762 CHECKSUM_STRING (name);
7765 #undef CHECKSUM
7766 #undef CHECKSUM_STRING
7767 #undef CHECKSUM_ATTR
7768 #undef CHECKSUM_LEB128
7769 #undef CHECKSUM_ULEB128
7771 /* Generate the type signature for DIE. This is computed by generating an
7772 MD5 checksum over the DIE's tag, its relevant attributes, and its
7773 children. Attributes that are references to other DIEs are processed
7774 by recursion, using the MARK field to prevent infinite recursion.
7775 If the DIE is nested inside a namespace or another type, we also
7776 need to include that context in the signature. The lower 64 bits
7777 of the resulting MD5 checksum comprise the signature. */
7779 static void
7780 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7782 int mark;
7783 const char *name;
7784 unsigned char checksum[16];
7785 struct md5_ctx ctx;
7786 dw_die_ref decl;
7787 dw_die_ref parent;
7789 name = get_AT_string (die, DW_AT_name);
7790 decl = get_AT_ref (die, DW_AT_specification);
7791 parent = dw_get_die_parent (die);
7793 /* First, compute a signature for just the type name (and its surrounding
7794 context, if any. This is stored in the type unit DIE for link-time
7795 ODR (one-definition rule) checking. */
7797 if (is_cxx () && name != NULL)
7799 md5_init_ctx (&ctx);
7801 /* Checksum the names of surrounding namespaces and structures. */
7802 if (parent != NULL)
7803 checksum_die_context (parent, &ctx);
7805 /* Checksum the current DIE. */
7806 die_odr_checksum (die->die_tag, name, &ctx);
7807 md5_finish_ctx (&ctx, checksum);
7809 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7812 /* Next, compute the complete type signature. */
7814 md5_init_ctx (&ctx);
7815 mark = 1;
7816 die->die_mark = mark;
7818 /* Checksum the names of surrounding namespaces and structures. */
7819 if (parent != NULL)
7820 checksum_die_context (parent, &ctx);
7822 /* Checksum the DIE and its children. */
7823 die_checksum_ordered (die, &ctx, &mark);
7824 unmark_all_dies (die);
7825 md5_finish_ctx (&ctx, checksum);
7827 /* Store the signature in the type node and link the type DIE and the
7828 type node together. */
7829 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7830 DWARF_TYPE_SIGNATURE_SIZE);
7831 die->comdat_type_p = true;
7832 die->die_id.die_type_node = type_node;
7833 type_node->type_die = die;
7835 /* If the DIE is a specification, link its declaration to the type node
7836 as well. */
7837 if (decl != NULL)
7839 decl->comdat_type_p = true;
7840 decl->die_id.die_type_node = type_node;
7844 /* Do the location expressions look same? */
7845 static inline bool
7846 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7848 return loc1->dw_loc_opc == loc2->dw_loc_opc
7849 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7850 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7853 /* Do the values look the same? */
7854 static bool
7855 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7857 dw_loc_descr_ref loc1, loc2;
7858 rtx r1, r2;
7860 if (v1->val_class != v2->val_class)
7861 return false;
7863 switch (v1->val_class)
7865 case dw_val_class_const:
7866 case dw_val_class_const_implicit:
7867 return v1->v.val_int == v2->v.val_int;
7868 case dw_val_class_unsigned_const:
7869 case dw_val_class_unsigned_const_implicit:
7870 return v1->v.val_unsigned == v2->v.val_unsigned;
7871 case dw_val_class_const_double:
7872 return v1->v.val_double.high == v2->v.val_double.high
7873 && v1->v.val_double.low == v2->v.val_double.low;
7874 case dw_val_class_wide_int:
7875 return *v1->v.val_wide == *v2->v.val_wide;
7876 case dw_val_class_vec:
7877 if (v1->v.val_vec.length != v2->v.val_vec.length
7878 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7879 return false;
7880 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7881 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7882 return false;
7883 return true;
7884 case dw_val_class_flag:
7885 return v1->v.val_flag == v2->v.val_flag;
7886 case dw_val_class_str:
7887 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7889 case dw_val_class_addr:
7890 r1 = v1->v.val_addr;
7891 r2 = v2->v.val_addr;
7892 if (GET_CODE (r1) != GET_CODE (r2))
7893 return false;
7894 return !rtx_equal_p (r1, r2);
7896 case dw_val_class_offset:
7897 return v1->v.val_offset == v2->v.val_offset;
7899 case dw_val_class_loc:
7900 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7901 loc1 && loc2;
7902 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7903 if (!same_loc_p (loc1, loc2, mark))
7904 return false;
7905 return !loc1 && !loc2;
7907 case dw_val_class_die_ref:
7908 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7910 case dw_val_class_symview:
7911 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7913 case dw_val_class_fde_ref:
7914 case dw_val_class_vms_delta:
7915 case dw_val_class_lbl_id:
7916 case dw_val_class_lineptr:
7917 case dw_val_class_macptr:
7918 case dw_val_class_loclistsptr:
7919 case dw_val_class_high_pc:
7920 return true;
7922 case dw_val_class_file:
7923 case dw_val_class_file_implicit:
7924 return v1->v.val_file == v2->v.val_file;
7926 case dw_val_class_data8:
7927 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7929 default:
7930 return true;
7934 /* Do the attributes look the same? */
7936 static bool
7937 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7939 if (at1->dw_attr != at2->dw_attr)
7940 return false;
7942 /* We don't care that this was compiled with a different compiler
7943 snapshot; if the output is the same, that's what matters. */
7944 if (at1->dw_attr == DW_AT_producer)
7945 return true;
7947 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7950 /* Do the dies look the same? */
7952 static bool
7953 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7955 dw_die_ref c1, c2;
7956 dw_attr_node *a1;
7957 unsigned ix;
7959 /* To avoid infinite recursion. */
7960 if (die1->die_mark)
7961 return die1->die_mark == die2->die_mark;
7962 die1->die_mark = die2->die_mark = ++(*mark);
7964 if (die1->die_tag != die2->die_tag)
7965 return false;
7967 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7968 return false;
7970 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7971 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7972 return false;
7974 c1 = die1->die_child;
7975 c2 = die2->die_child;
7976 if (! c1)
7978 if (c2)
7979 return false;
7981 else
7982 for (;;)
7984 if (!same_die_p (c1, c2, mark))
7985 return false;
7986 c1 = c1->die_sib;
7987 c2 = c2->die_sib;
7988 if (c1 == die1->die_child)
7990 if (c2 == die2->die_child)
7991 break;
7992 else
7993 return false;
7997 return true;
8000 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8001 children, and set die_symbol. */
8003 static void
8004 compute_comp_unit_symbol (dw_die_ref unit_die)
8006 const char *die_name = get_AT_string (unit_die, DW_AT_name);
8007 const char *base = die_name ? lbasename (die_name) : "anonymous";
8008 char *name = XALLOCAVEC (char, strlen (base) + 64);
8009 char *p;
8010 int i, mark;
8011 unsigned char checksum[16];
8012 struct md5_ctx ctx;
8014 /* Compute the checksum of the DIE, then append part of it as hex digits to
8015 the name filename of the unit. */
8017 md5_init_ctx (&ctx);
8018 mark = 0;
8019 die_checksum (unit_die, &ctx, &mark);
8020 unmark_all_dies (unit_die);
8021 md5_finish_ctx (&ctx, checksum);
8023 /* When we this for comp_unit_die () we have a DW_AT_name that might
8024 not start with a letter but with anything valid for filenames and
8025 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
8026 character is not a letter. */
8027 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
8028 clean_symbol_name (name);
8030 p = name + strlen (name);
8031 for (i = 0; i < 4; i++)
8033 sprintf (p, "%.2x", checksum[i]);
8034 p += 2;
8037 unit_die->die_id.die_symbol = xstrdup (name);
8040 /* Returns true if DIE represents a type, in the sense of TYPE_P. */
8042 static bool
8043 is_type_die (dw_die_ref die)
8045 switch (die->die_tag)
8047 case DW_TAG_array_type:
8048 case DW_TAG_class_type:
8049 case DW_TAG_interface_type:
8050 case DW_TAG_enumeration_type:
8051 case DW_TAG_pointer_type:
8052 case DW_TAG_reference_type:
8053 case DW_TAG_rvalue_reference_type:
8054 case DW_TAG_string_type:
8055 case DW_TAG_structure_type:
8056 case DW_TAG_subroutine_type:
8057 case DW_TAG_union_type:
8058 case DW_TAG_ptr_to_member_type:
8059 case DW_TAG_set_type:
8060 case DW_TAG_subrange_type:
8061 case DW_TAG_base_type:
8062 case DW_TAG_const_type:
8063 case DW_TAG_file_type:
8064 case DW_TAG_packed_type:
8065 case DW_TAG_volatile_type:
8066 case DW_TAG_typedef:
8067 return true;
8068 default:
8069 return false;
8073 /* Returns true iff C is a compile-unit DIE. */
8075 static inline bool
8076 is_cu_die (dw_die_ref c)
8078 return c && (c->die_tag == DW_TAG_compile_unit
8079 || c->die_tag == DW_TAG_skeleton_unit);
8082 /* Returns true iff C is a unit DIE of some sort. */
8084 static inline bool
8085 is_unit_die (dw_die_ref c)
8087 return c && (c->die_tag == DW_TAG_compile_unit
8088 || c->die_tag == DW_TAG_partial_unit
8089 || c->die_tag == DW_TAG_type_unit
8090 || c->die_tag == DW_TAG_skeleton_unit);
8093 /* Returns true iff C is a namespace DIE. */
8095 static inline bool
8096 is_namespace_die (dw_die_ref c)
8098 return c && c->die_tag == DW_TAG_namespace;
8101 /* Return true if this DIE is a template parameter. */
8103 static inline bool
8104 is_template_parameter (dw_die_ref die)
8106 switch (die->die_tag)
8108 case DW_TAG_template_type_param:
8109 case DW_TAG_template_value_param:
8110 case DW_TAG_GNU_template_template_param:
8111 case DW_TAG_GNU_template_parameter_pack:
8112 return true;
8113 default:
8114 return false;
8118 /* Return true if this DIE represents a template instantiation. */
8120 static inline bool
8121 is_template_instantiation (dw_die_ref die)
8123 dw_die_ref c;
8125 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8126 return false;
8127 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8128 return false;
8131 static char *
8132 gen_internal_sym (const char *prefix)
8134 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8136 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8137 return xstrdup (buf);
8140 /* Return true if this DIE is a declaration. */
8142 static bool
8143 is_declaration_die (dw_die_ref die)
8145 dw_attr_node *a;
8146 unsigned ix;
8148 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8149 if (a->dw_attr == DW_AT_declaration)
8150 return true;
8152 return false;
8155 /* Return true if this DIE is nested inside a subprogram. */
8157 static bool
8158 is_nested_in_subprogram (dw_die_ref die)
8160 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8162 if (decl == NULL)
8163 decl = die;
8164 return local_scope_p (decl);
8167 /* Return true if this DIE contains a defining declaration of a
8168 subprogram. */
8170 static bool
8171 contains_subprogram_definition (dw_die_ref die)
8173 dw_die_ref c;
8175 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8176 return true;
8177 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8178 return false;
8181 /* Return true if this is a type DIE that should be moved to a
8182 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8183 unit type. */
8185 static bool
8186 should_move_die_to_comdat (dw_die_ref die)
8188 switch (die->die_tag)
8190 case DW_TAG_class_type:
8191 case DW_TAG_structure_type:
8192 case DW_TAG_enumeration_type:
8193 case DW_TAG_union_type:
8194 /* Don't move declarations, inlined instances, types nested in a
8195 subprogram, or types that contain subprogram definitions. */
8196 if (is_declaration_die (die)
8197 || get_AT (die, DW_AT_abstract_origin)
8198 || is_nested_in_subprogram (die)
8199 || contains_subprogram_definition (die))
8200 return false;
8201 if (die->die_tag != DW_TAG_enumeration_type)
8203 /* Don't move non-constant size aggregates. */
8204 dw_attr_node *sz = get_AT (die, DW_AT_byte_size);
8205 if (sz == NULL
8206 || (AT_class (sz) != dw_val_class_unsigned_const
8207 && AT_class (sz) != dw_val_class_unsigned_const_implicit))
8208 return false;
8210 return true;
8211 case DW_TAG_array_type:
8212 case DW_TAG_interface_type:
8213 case DW_TAG_pointer_type:
8214 case DW_TAG_reference_type:
8215 case DW_TAG_rvalue_reference_type:
8216 case DW_TAG_string_type:
8217 case DW_TAG_subroutine_type:
8218 case DW_TAG_ptr_to_member_type:
8219 case DW_TAG_set_type:
8220 case DW_TAG_subrange_type:
8221 case DW_TAG_base_type:
8222 case DW_TAG_const_type:
8223 case DW_TAG_file_type:
8224 case DW_TAG_packed_type:
8225 case DW_TAG_volatile_type:
8226 case DW_TAG_typedef:
8227 default:
8228 return false;
8232 /* Make a clone of DIE. */
8234 static dw_die_ref
8235 clone_die (dw_die_ref die)
8237 dw_die_ref clone = new_die_raw (die->die_tag);
8238 dw_attr_node *a;
8239 unsigned ix;
8241 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8242 add_dwarf_attr (clone, a);
8244 return clone;
8247 /* Make a clone of the tree rooted at DIE. */
8249 static dw_die_ref
8250 clone_tree (dw_die_ref die)
8252 dw_die_ref c;
8253 dw_die_ref clone = clone_die (die);
8255 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8257 return clone;
8260 /* Make a clone of DIE as a declaration. */
8262 static dw_die_ref
8263 clone_as_declaration (dw_die_ref die)
8265 dw_die_ref clone;
8266 dw_die_ref decl;
8267 dw_attr_node *a;
8268 unsigned ix;
8270 /* If the DIE is already a declaration, just clone it. */
8271 if (is_declaration_die (die))
8272 return clone_die (die);
8274 /* If the DIE is a specification, just clone its declaration DIE. */
8275 decl = get_AT_ref (die, DW_AT_specification);
8276 if (decl != NULL)
8278 clone = clone_die (decl);
8279 if (die->comdat_type_p)
8280 add_AT_die_ref (clone, DW_AT_signature, die);
8281 return clone;
8284 clone = new_die_raw (die->die_tag);
8286 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8288 /* We don't want to copy over all attributes.
8289 For example we don't want DW_AT_byte_size because otherwise we will no
8290 longer have a declaration and GDB will treat it as a definition. */
8292 switch (a->dw_attr)
8294 case DW_AT_abstract_origin:
8295 case DW_AT_artificial:
8296 case DW_AT_containing_type:
8297 case DW_AT_external:
8298 case DW_AT_name:
8299 case DW_AT_type:
8300 case DW_AT_virtuality:
8301 case DW_AT_linkage_name:
8302 case DW_AT_MIPS_linkage_name:
8303 add_dwarf_attr (clone, a);
8304 break;
8305 case DW_AT_byte_size:
8306 case DW_AT_alignment:
8307 default:
8308 break;
8312 if (die->comdat_type_p)
8313 add_AT_die_ref (clone, DW_AT_signature, die);
8315 add_AT_flag (clone, DW_AT_declaration, 1);
8316 return clone;
8320 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8322 struct decl_table_entry
8324 dw_die_ref orig;
8325 dw_die_ref copy;
8328 /* Helpers to manipulate hash table of copied declarations. */
8330 /* Hashtable helpers. */
8332 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8334 typedef die_struct *compare_type;
8335 static inline hashval_t hash (const decl_table_entry *);
8336 static inline bool equal (const decl_table_entry *, const die_struct *);
8339 inline hashval_t
8340 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8342 return htab_hash_pointer (entry->orig);
8345 inline bool
8346 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8347 const die_struct *entry2)
8349 return entry1->orig == entry2;
8352 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8354 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8355 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8356 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8357 to check if the ancestor has already been copied into UNIT. */
8359 static dw_die_ref
8360 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8361 decl_hash_type *decl_table)
8363 dw_die_ref parent = die->die_parent;
8364 dw_die_ref new_parent = unit;
8365 dw_die_ref copy;
8366 decl_table_entry **slot = NULL;
8367 struct decl_table_entry *entry = NULL;
8369 /* If DIE refers to a stub unfold that so we get the appropriate
8370 DIE registered as orig in decl_table. */
8371 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8372 die = c;
8374 if (decl_table)
8376 /* Check if the entry has already been copied to UNIT. */
8377 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8378 INSERT);
8379 if (*slot != HTAB_EMPTY_ENTRY)
8381 entry = *slot;
8382 return entry->copy;
8385 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8386 entry = XCNEW (struct decl_table_entry);
8387 entry->orig = die;
8388 entry->copy = NULL;
8389 *slot = entry;
8392 if (parent != NULL)
8394 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8395 if (spec != NULL)
8396 parent = spec;
8397 if (!is_unit_die (parent))
8398 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8401 copy = clone_as_declaration (die);
8402 add_child_die (new_parent, copy);
8404 if (decl_table)
8406 /* Record the pointer to the copy. */
8407 entry->copy = copy;
8410 return copy;
8412 /* Copy the declaration context to the new type unit DIE. This includes
8413 any surrounding namespace or type declarations. If the DIE has an
8414 AT_specification attribute, it also includes attributes and children
8415 attached to the specification, and returns a pointer to the original
8416 parent of the declaration DIE. Returns NULL otherwise. */
8418 static dw_die_ref
8419 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8421 dw_die_ref decl;
8422 dw_die_ref new_decl;
8423 dw_die_ref orig_parent = NULL;
8425 decl = get_AT_ref (die, DW_AT_specification);
8426 if (decl == NULL)
8427 decl = die;
8428 else
8430 unsigned ix;
8431 dw_die_ref c;
8432 dw_attr_node *a;
8434 /* The original DIE will be changed to a declaration, and must
8435 be moved to be a child of the original declaration DIE. */
8436 orig_parent = decl->die_parent;
8438 /* Copy the type node pointer from the new DIE to the original
8439 declaration DIE so we can forward references later. */
8440 decl->comdat_type_p = true;
8441 decl->die_id.die_type_node = die->die_id.die_type_node;
8443 remove_AT (die, DW_AT_specification);
8445 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8447 if (a->dw_attr != DW_AT_name
8448 && a->dw_attr != DW_AT_declaration
8449 && a->dw_attr != DW_AT_external)
8450 add_dwarf_attr (die, a);
8453 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8456 if (decl->die_parent != NULL
8457 && !is_unit_die (decl->die_parent))
8459 new_decl = copy_ancestor_tree (unit, decl, NULL);
8460 if (new_decl != NULL)
8462 remove_AT (new_decl, DW_AT_signature);
8463 add_AT_specification (die, new_decl);
8467 return orig_parent;
8470 /* Generate the skeleton ancestor tree for the given NODE, then clone
8471 the DIE and add the clone into the tree. */
8473 static void
8474 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8476 if (node->new_die != NULL)
8477 return;
8479 node->new_die = clone_as_declaration (node->old_die);
8481 if (node->parent != NULL)
8483 generate_skeleton_ancestor_tree (node->parent);
8484 add_child_die (node->parent->new_die, node->new_die);
8488 /* Generate a skeleton tree of DIEs containing any declarations that are
8489 found in the original tree. We traverse the tree looking for declaration
8490 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8492 static void
8493 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8495 skeleton_chain_node node;
8496 dw_die_ref c;
8497 dw_die_ref first;
8498 dw_die_ref prev = NULL;
8499 dw_die_ref next = NULL;
8501 node.parent = parent;
8503 first = c = parent->old_die->die_child;
8504 if (c)
8505 next = c->die_sib;
8506 if (c) do {
8507 if (prev == NULL || prev->die_sib == c)
8508 prev = c;
8509 c = next;
8510 next = (c == first ? NULL : c->die_sib);
8511 node.old_die = c;
8512 node.new_die = NULL;
8513 if (is_declaration_die (c))
8515 if (is_template_instantiation (c))
8517 /* Instantiated templates do not need to be cloned into the
8518 type unit. Just move the DIE and its children back to
8519 the skeleton tree (in the main CU). */
8520 remove_child_with_prev (c, prev);
8521 add_child_die (parent->new_die, c);
8522 c = prev;
8524 else if (c->comdat_type_p)
8526 /* This is the skeleton of earlier break_out_comdat_types
8527 type. Clone the existing DIE, but keep the children
8528 under the original (which is in the main CU). */
8529 dw_die_ref clone = clone_die (c);
8531 replace_child (c, clone, prev);
8532 generate_skeleton_ancestor_tree (parent);
8533 add_child_die (parent->new_die, c);
8534 c = clone;
8535 continue;
8537 else
8539 /* Clone the existing DIE, move the original to the skeleton
8540 tree (which is in the main CU), and put the clone, with
8541 all the original's children, where the original came from
8542 (which is about to be moved to the type unit). */
8543 dw_die_ref clone = clone_die (c);
8544 move_all_children (c, clone);
8546 /* If the original has a DW_AT_object_pointer attribute,
8547 it would now point to a child DIE just moved to the
8548 cloned tree, so we need to remove that attribute from
8549 the original. */
8550 remove_AT (c, DW_AT_object_pointer);
8552 replace_child (c, clone, prev);
8553 generate_skeleton_ancestor_tree (parent);
8554 add_child_die (parent->new_die, c);
8555 node.old_die = clone;
8556 node.new_die = c;
8557 c = clone;
8560 generate_skeleton_bottom_up (&node);
8561 } while (next != NULL);
8564 /* Wrapper function for generate_skeleton_bottom_up. */
8566 static dw_die_ref
8567 generate_skeleton (dw_die_ref die)
8569 skeleton_chain_node node;
8571 node.old_die = die;
8572 node.new_die = NULL;
8573 node.parent = NULL;
8575 /* If this type definition is nested inside another type,
8576 and is not an instantiation of a template, always leave
8577 at least a declaration in its place. */
8578 if (die->die_parent != NULL
8579 && is_type_die (die->die_parent)
8580 && !is_template_instantiation (die))
8581 node.new_die = clone_as_declaration (die);
8583 generate_skeleton_bottom_up (&node);
8584 return node.new_die;
8587 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8588 declaration. The original DIE is moved to a new compile unit so that
8589 existing references to it follow it to the new location. If any of the
8590 original DIE's descendants is a declaration, we need to replace the
8591 original DIE with a skeleton tree and move the declarations back into the
8592 skeleton tree. */
8594 static dw_die_ref
8595 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8596 dw_die_ref prev)
8598 dw_die_ref skeleton, orig_parent;
8600 /* Copy the declaration context to the type unit DIE. If the returned
8601 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8602 that DIE. */
8603 orig_parent = copy_declaration_context (unit, child);
8605 skeleton = generate_skeleton (child);
8606 if (skeleton == NULL)
8607 remove_child_with_prev (child, prev);
8608 else
8610 skeleton->comdat_type_p = true;
8611 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8613 /* If the original DIE was a specification, we need to put
8614 the skeleton under the parent DIE of the declaration.
8615 This leaves the original declaration in the tree, but
8616 it will be pruned later since there are no longer any
8617 references to it. */
8618 if (orig_parent != NULL)
8620 remove_child_with_prev (child, prev);
8621 add_child_die (orig_parent, skeleton);
8623 else
8624 replace_child (child, skeleton, prev);
8627 return skeleton;
8630 static void
8631 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8632 comdat_type_node *type_node,
8633 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8635 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8636 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8637 DWARF procedure references in the DW_AT_location attribute. */
8639 static dw_die_ref
8640 copy_dwarf_procedure (dw_die_ref die,
8641 comdat_type_node *type_node,
8642 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8644 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8646 /* DWARF procedures are not supposed to have children... */
8647 gcc_assert (die->die_child == NULL);
8649 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8650 gcc_assert (vec_safe_length (die->die_attr) == 1
8651 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8653 /* Do not copy more than once DWARF procedures. */
8654 bool existed;
8655 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8656 if (existed)
8657 return die_copy;
8659 die_copy = clone_die (die);
8660 add_child_die (type_node->root_die, die_copy);
8661 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8662 return die_copy;
8665 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8666 procedures in DIE's attributes. */
8668 static void
8669 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8670 comdat_type_node *type_node,
8671 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8673 dw_attr_node *a;
8674 unsigned i;
8676 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8678 dw_loc_descr_ref loc;
8680 if (a->dw_attr_val.val_class != dw_val_class_loc)
8681 continue;
8683 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8685 switch (loc->dw_loc_opc)
8687 case DW_OP_call2:
8688 case DW_OP_call4:
8689 case DW_OP_call_ref:
8690 gcc_assert (loc->dw_loc_oprnd1.val_class
8691 == dw_val_class_die_ref);
8692 loc->dw_loc_oprnd1.v.val_die_ref.die
8693 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8694 type_node,
8695 copied_dwarf_procs);
8697 default:
8698 break;
8704 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8705 rewrite references to point to the copies.
8707 References are looked for in DIE's attributes and recursively in all its
8708 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8709 mapping from old DWARF procedures to their copy. It is used not to copy
8710 twice the same DWARF procedure under TYPE_NODE. */
8712 static void
8713 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8714 comdat_type_node *type_node,
8715 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8717 dw_die_ref c;
8719 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8720 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8721 type_node,
8722 copied_dwarf_procs));
8725 /* Traverse the DIE and set up additional .debug_types or .debug_info
8726 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8727 section. */
8729 static void
8730 break_out_comdat_types (dw_die_ref die)
8732 dw_die_ref c;
8733 dw_die_ref first;
8734 dw_die_ref prev = NULL;
8735 dw_die_ref next = NULL;
8736 dw_die_ref unit = NULL;
8738 first = c = die->die_child;
8739 if (c)
8740 next = c->die_sib;
8741 if (c) do {
8742 if (prev == NULL || prev->die_sib == c)
8743 prev = c;
8744 c = next;
8745 next = (c == first ? NULL : c->die_sib);
8746 if (should_move_die_to_comdat (c))
8748 dw_die_ref replacement;
8749 comdat_type_node *type_node;
8751 /* Break out nested types into their own type units. */
8752 break_out_comdat_types (c);
8754 /* Create a new type unit DIE as the root for the new tree. */
8755 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8756 add_AT_unsigned (unit, DW_AT_language,
8757 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8758 if (unsigned lname = get_AT_unsigned (comp_unit_die (),
8759 DW_AT_language_name))
8761 add_AT_unsigned (unit, DW_AT_language_name, lname);
8762 add_AT_unsigned (unit, DW_AT_language_version,
8763 get_AT_unsigned (comp_unit_die (),
8764 DW_AT_language_version));
8767 /* Add the new unit's type DIE into the comdat type list. */
8768 type_node = ggc_cleared_alloc<comdat_type_node> ();
8769 type_node->root_die = unit;
8770 type_node->next = comdat_type_list;
8771 comdat_type_list = type_node;
8773 /* Generate the type signature. */
8774 generate_type_signature (c, type_node);
8776 /* Copy the declaration context, attributes, and children of the
8777 declaration into the new type unit DIE, then remove this DIE
8778 from the main CU (or replace it with a skeleton if necessary). */
8779 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8780 type_node->skeleton_die = replacement;
8782 /* Add the DIE to the new compunit. */
8783 add_child_die (unit, c);
8785 /* Types can reference DWARF procedures for type size or data location
8786 expressions. Calls in DWARF expressions cannot target procedures
8787 that are not in the same section. So we must copy DWARF procedures
8788 along with this type and then rewrite references to them. */
8789 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8790 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8792 if (replacement != NULL)
8793 c = replacement;
8795 else if (c->die_tag == DW_TAG_namespace
8796 || c->die_tag == DW_TAG_class_type
8797 || c->die_tag == DW_TAG_structure_type
8798 || c->die_tag == DW_TAG_union_type)
8800 /* Look for nested types that can be broken out. */
8801 break_out_comdat_types (c);
8803 } while (next != NULL);
8806 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8807 Enter all the cloned children into the hash table decl_table. */
8809 static dw_die_ref
8810 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8812 dw_die_ref c;
8813 dw_die_ref clone;
8814 struct decl_table_entry *entry;
8815 decl_table_entry **slot;
8817 if (die->die_tag == DW_TAG_subprogram)
8818 clone = clone_as_declaration (die);
8819 else
8820 clone = clone_die (die);
8822 slot = decl_table->find_slot_with_hash (die,
8823 htab_hash_pointer (die), INSERT);
8825 /* Assert that DIE isn't in the hash table yet. If it would be there
8826 before, the ancestors would be necessarily there as well, therefore
8827 clone_tree_partial wouldn't be called. */
8828 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8830 entry = XCNEW (struct decl_table_entry);
8831 entry->orig = die;
8832 entry->copy = clone;
8833 *slot = entry;
8835 if (die->die_tag != DW_TAG_subprogram)
8836 FOR_EACH_CHILD (die, c,
8837 add_child_die (clone, clone_tree_partial (c, decl_table)));
8839 return clone;
8842 /* Walk the DIE and its children, looking for references to incomplete
8843 or trivial types that are unmarked (i.e., that are not in the current
8844 type_unit). */
8846 static void
8847 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8849 dw_die_ref c;
8850 dw_attr_node *a;
8851 unsigned ix;
8853 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8855 if (AT_class (a) == dw_val_class_die_ref)
8857 dw_die_ref targ = AT_ref (a);
8858 decl_table_entry **slot;
8859 struct decl_table_entry *entry;
8861 if (targ->die_mark != 0 || targ->comdat_type_p)
8862 continue;
8864 slot = decl_table->find_slot_with_hash (targ,
8865 htab_hash_pointer (targ),
8866 INSERT);
8868 if (*slot != HTAB_EMPTY_ENTRY)
8870 /* TARG has already been copied, so we just need to
8871 modify the reference to point to the copy. */
8872 entry = *slot;
8873 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8875 else
8877 dw_die_ref parent = unit;
8878 dw_die_ref copy = clone_die (targ);
8880 /* Record in DECL_TABLE that TARG has been copied.
8881 Need to do this now, before the recursive call,
8882 because DECL_TABLE may be expanded and SLOT
8883 would no longer be a valid pointer. */
8884 entry = XCNEW (struct decl_table_entry);
8885 entry->orig = targ;
8886 entry->copy = copy;
8887 *slot = entry;
8889 /* If TARG is not a declaration DIE, we need to copy its
8890 children. */
8891 if (!is_declaration_die (targ))
8893 FOR_EACH_CHILD (
8894 targ, c,
8895 add_child_die (copy,
8896 clone_tree_partial (c, decl_table)));
8899 /* Make sure the cloned tree is marked as part of the
8900 type unit. */
8901 mark_dies (copy);
8903 /* If TARG has surrounding context, copy its ancestor tree
8904 into the new type unit. */
8905 if (targ->die_parent != NULL
8906 && !is_unit_die (targ->die_parent))
8907 parent = copy_ancestor_tree (unit, targ->die_parent,
8908 decl_table);
8910 add_child_die (parent, copy);
8911 a->dw_attr_val.v.val_die_ref.die = copy;
8913 /* Make sure the newly-copied DIE is walked. If it was
8914 installed in a previously-added context, it won't
8915 get visited otherwise. */
8916 if (parent != unit)
8918 /* Find the highest point of the newly-added tree,
8919 mark each node along the way, and walk from there. */
8920 parent->die_mark = 1;
8921 while (parent->die_parent
8922 && parent->die_parent->die_mark == 0)
8924 parent = parent->die_parent;
8925 parent->die_mark = 1;
8927 copy_decls_walk (unit, parent, decl_table);
8933 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8936 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8937 and record them in DECL_TABLE. */
8939 static void
8940 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8942 dw_die_ref c;
8944 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8946 dw_die_ref targ = AT_ref (a);
8947 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8948 decl_table_entry **slot
8949 = decl_table->find_slot_with_hash (targ,
8950 htab_hash_pointer (targ),
8951 INSERT);
8952 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8953 /* Record in DECL_TABLE that TARG has been already copied
8954 by remove_child_or_replace_with_skeleton. */
8955 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8956 entry->orig = targ;
8957 entry->copy = die;
8958 *slot = entry;
8960 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8963 /* Copy declarations for "unworthy" types into the new comdat section.
8964 Incomplete types, modified types, and certain other types aren't broken
8965 out into comdat sections of their own, so they don't have a signature,
8966 and we need to copy the declaration into the same section so that we
8967 don't have an external reference. */
8969 static void
8970 copy_decls_for_unworthy_types (dw_die_ref unit)
8972 mark_dies (unit);
8973 decl_hash_type decl_table (10);
8974 collect_skeleton_dies (unit, &decl_table);
8975 copy_decls_walk (unit, unit, &decl_table);
8976 unmark_dies (unit);
8979 /* Traverse the DIE and add a sibling attribute if it may have the
8980 effect of speeding up access to siblings. To save some space,
8981 avoid generating sibling attributes for DIE's without children. */
8983 static void
8984 add_sibling_attributes (dw_die_ref die)
8986 dw_die_ref c;
8988 if (! die->die_child)
8989 return;
8991 if (die->die_parent && die != die->die_parent->die_child)
8992 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8994 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8997 /* Output all location lists for the DIE and its children. */
8999 static void
9000 output_location_lists (dw_die_ref die)
9002 dw_die_ref c;
9003 dw_attr_node *a;
9004 unsigned ix;
9006 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9007 if (AT_class (a) == dw_val_class_loc_list)
9008 output_loc_list (AT_loc_list (a));
9010 FOR_EACH_CHILD (die, c, output_location_lists (c));
9013 /* During assign_location_list_indexes and output_loclists_offset the
9014 current index, after it the number of assigned indexes (i.e. how
9015 large the .debug_loclists* offset table should be). */
9016 static unsigned int loc_list_idx;
9018 /* Output all location list offsets for the DIE and its children. */
9020 static void
9021 output_loclists_offsets (dw_die_ref die)
9023 dw_die_ref c;
9024 dw_attr_node *a;
9025 unsigned ix;
9027 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9028 if (AT_class (a) == dw_val_class_loc_list)
9030 dw_loc_list_ref l = AT_loc_list (a);
9031 if (l->offset_emitted)
9032 continue;
9033 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
9034 loc_section_label, NULL);
9035 gcc_assert (l->hash == loc_list_idx);
9036 loc_list_idx++;
9037 l->offset_emitted = true;
9040 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
9043 /* Recursively set indexes of location lists. */
9045 static void
9046 assign_location_list_indexes (dw_die_ref die)
9048 dw_die_ref c;
9049 dw_attr_node *a;
9050 unsigned ix;
9052 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9053 if (AT_class (a) == dw_val_class_loc_list)
9055 dw_loc_list_ref list = AT_loc_list (a);
9056 if (!list->num_assigned)
9058 list->num_assigned = true;
9059 list->hash = loc_list_idx++;
9063 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
9066 /* We want to limit the number of external references, because they are
9067 larger than local references: a relocation takes multiple words, and
9068 even a sig8 reference is always eight bytes, whereas a local reference
9069 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
9070 So if we encounter multiple external references to the same type DIE, we
9071 make a local typedef stub for it and redirect all references there.
9073 This is the element of the hash table for keeping track of these
9074 references. */
9076 struct external_ref
9078 dw_die_ref type;
9079 dw_die_ref stub;
9080 unsigned n_refs;
9083 /* Hashtable helpers. */
9085 struct external_ref_hasher : free_ptr_hash <external_ref>
9087 static inline hashval_t hash (const external_ref *);
9088 static inline bool equal (const external_ref *, const external_ref *);
9091 inline hashval_t
9092 external_ref_hasher::hash (const external_ref *r)
9094 dw_die_ref die = r->type;
9095 hashval_t h = 0;
9097 /* We can't use the address of the DIE for hashing, because
9098 that will make the order of the stub DIEs non-deterministic. */
9099 if (! die->comdat_type_p)
9100 /* We have a symbol; use it to compute a hash. */
9101 h = htab_hash_string (die->die_id.die_symbol);
9102 else
9104 /* We have a type signature; use a subset of the bits as the hash.
9105 The 8-byte signature is at least as large as hashval_t. */
9106 comdat_type_node *type_node = die->die_id.die_type_node;
9107 memcpy (&h, type_node->signature, sizeof (h));
9109 return h;
9112 inline bool
9113 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9115 return r1->type == r2->type;
9118 typedef hash_table<external_ref_hasher> external_ref_hash_type;
9120 /* Return a pointer to the external_ref for references to DIE. */
9122 static struct external_ref *
9123 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9125 struct external_ref ref, *ref_p;
9126 external_ref **slot;
9128 ref.type = die;
9129 slot = map->find_slot (&ref, INSERT);
9130 if (*slot != HTAB_EMPTY_ENTRY)
9131 return *slot;
9133 ref_p = XCNEW (struct external_ref);
9134 ref_p->type = die;
9135 *slot = ref_p;
9136 return ref_p;
9139 /* Subroutine of optimize_external_refs, below.
9141 If we see a type skeleton, record it as our stub. If we see external
9142 references, remember how many we've seen. */
9144 static void
9145 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9147 dw_die_ref c;
9148 dw_attr_node *a;
9149 unsigned ix;
9150 struct external_ref *ref_p;
9152 if (is_type_die (die)
9153 && (c = get_AT_ref (die, DW_AT_signature)))
9155 /* This is a local skeleton; use it for local references. */
9156 ref_p = lookup_external_ref (map, c);
9157 ref_p->stub = die;
9160 /* Scan the DIE references, and remember any that refer to DIEs from
9161 other CUs (i.e. those which are not marked). */
9162 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9163 if (AT_class (a) == dw_val_class_die_ref
9164 && (c = AT_ref (a))->die_mark == 0
9165 && is_type_die (c))
9167 ref_p = lookup_external_ref (map, c);
9168 ref_p->n_refs++;
9171 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9174 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9175 points to an external_ref, DATA is the CU we're processing. If we don't
9176 already have a local stub, and we have multiple refs, build a stub. */
9179 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9181 struct external_ref *ref_p = *slot;
9183 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9185 /* We have multiple references to this type, so build a small stub.
9186 Both of these forms are a bit dodgy from the perspective of the
9187 DWARF standard, since technically they should have names. */
9188 dw_die_ref cu = data;
9189 dw_die_ref type = ref_p->type;
9190 dw_die_ref stub = NULL;
9192 if (type->comdat_type_p)
9194 /* If we refer to this type via sig8, use AT_signature. */
9195 stub = new_die (type->die_tag, cu, NULL_TREE);
9196 add_AT_die_ref (stub, DW_AT_signature, type);
9198 else
9200 /* Otherwise, use a typedef with no name. */
9201 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9202 add_AT_die_ref (stub, DW_AT_type, type);
9205 stub->die_mark++;
9206 ref_p->stub = stub;
9208 return 1;
9211 /* DIE is a unit; look through all the DIE references to see if there are
9212 any external references to types, and if so, create local stubs for
9213 them which will be applied in build_abbrev_table. This is useful because
9214 references to local DIEs are smaller. */
9216 static external_ref_hash_type *
9217 optimize_external_refs (dw_die_ref die)
9219 external_ref_hash_type *map = new external_ref_hash_type (10);
9220 optimize_external_refs_1 (die, map);
9221 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9222 return map;
9225 /* The following 3 variables are temporaries that are computed only during the
9226 build_abbrev_table call and used and released during the following
9227 optimize_abbrev_table call. */
9229 /* First abbrev_id that can be optimized based on usage. */
9230 static unsigned int abbrev_opt_start;
9232 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9233 abbrev_id smaller than this, because they must be already sized
9234 during build_abbrev_table). */
9235 static unsigned int abbrev_opt_base_type_end;
9237 /* Vector of usage counts during build_abbrev_table. Indexed by
9238 abbrev_id - abbrev_opt_start. */
9239 static vec<unsigned int> abbrev_usage_count;
9241 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9242 static vec<dw_die_ref> sorted_abbrev_dies;
9244 /* The format of each DIE (and its attribute value pairs) is encoded in an
9245 abbreviation table. This routine builds the abbreviation table and assigns
9246 a unique abbreviation id for each abbreviation entry. The children of each
9247 die are visited recursively. */
9249 static void
9250 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9252 unsigned int abbrev_id = 0;
9253 dw_die_ref c;
9254 dw_attr_node *a;
9255 unsigned ix;
9256 dw_die_ref abbrev;
9258 /* Scan the DIE references, and replace any that refer to
9259 DIEs from other CUs (i.e. those which are not marked) with
9260 the local stubs we built in optimize_external_refs. */
9261 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9262 if (AT_class (a) == dw_val_class_die_ref
9263 && (c = AT_ref (a))->die_mark == 0)
9265 struct external_ref *ref_p;
9266 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9268 if (is_type_die (c)
9269 && (ref_p = lookup_external_ref (extern_map, c))
9270 && ref_p->stub && ref_p->stub != die)
9272 gcc_assert (a->dw_attr != DW_AT_signature);
9273 change_AT_die_ref (a, ref_p->stub);
9275 else
9276 /* We aren't changing this reference, so mark it external. */
9277 set_AT_ref_external (a, 1);
9280 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9282 dw_attr_node *die_a, *abbrev_a;
9283 unsigned ix;
9284 bool ok = true;
9286 if (abbrev_id == 0)
9287 continue;
9288 if (abbrev->die_tag != die->die_tag)
9289 continue;
9290 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9291 continue;
9293 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9294 continue;
9296 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9298 abbrev_a = &(*abbrev->die_attr)[ix];
9299 if ((abbrev_a->dw_attr != die_a->dw_attr)
9300 || (value_format (abbrev_a) != value_format (die_a)))
9302 ok = false;
9303 break;
9306 if (ok)
9307 break;
9310 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9312 vec_safe_push (abbrev_die_table, die);
9313 if (abbrev_opt_start)
9314 abbrev_usage_count.safe_push (0);
9316 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9318 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9319 sorted_abbrev_dies.safe_push (die);
9322 die->die_abbrev = abbrev_id;
9323 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9326 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9327 by die_abbrev's usage count, from the most commonly used
9328 abbreviation to the least. */
9330 static int
9331 die_abbrev_cmp (const void *p1, const void *p2)
9333 dw_die_ref die1 = *(const dw_die_ref *) p1;
9334 dw_die_ref die2 = *(const dw_die_ref *) p2;
9336 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9337 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9339 if (die1->die_abbrev >= abbrev_opt_base_type_end
9340 && die2->die_abbrev >= abbrev_opt_base_type_end)
9342 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9343 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9344 return -1;
9345 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9346 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9347 return 1;
9350 /* Stabilize the sort. */
9351 if (die1->die_abbrev < die2->die_abbrev)
9352 return -1;
9353 if (die1->die_abbrev > die2->die_abbrev)
9354 return 1;
9356 return 0;
9359 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9360 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9361 into dw_val_class_const_implicit or
9362 dw_val_class_unsigned_const_implicit. */
9364 static void
9365 optimize_implicit_const (unsigned int first_id, unsigned int end,
9366 vec<bool> &implicit_consts)
9368 /* It never makes sense if there is just one DIE using the abbreviation. */
9369 if (end < first_id + 2)
9370 return;
9372 dw_attr_node *a;
9373 unsigned ix, i;
9374 dw_die_ref die = sorted_abbrev_dies[first_id];
9375 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9376 if (implicit_consts[ix])
9378 enum dw_val_class new_class = dw_val_class_none;
9379 switch (AT_class (a))
9381 case dw_val_class_unsigned_const:
9382 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9383 continue;
9385 /* The .debug_abbrev section will grow by
9386 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9387 in all the DIEs using that abbreviation. */
9388 if (constant_size (AT_unsigned (a)) * (end - first_id)
9389 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9390 continue;
9392 new_class = dw_val_class_unsigned_const_implicit;
9393 break;
9395 case dw_val_class_const:
9396 new_class = dw_val_class_const_implicit;
9397 break;
9399 case dw_val_class_file:
9400 new_class = dw_val_class_file_implicit;
9401 break;
9403 default:
9404 continue;
9406 for (i = first_id; i < end; i++)
9407 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9408 = new_class;
9412 /* Attempt to optimize abbreviation table from abbrev_opt_start
9413 abbreviation above. */
9415 static void
9416 optimize_abbrev_table (void)
9418 if (abbrev_opt_start
9419 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9420 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9422 auto_vec<bool, 32> implicit_consts;
9423 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9425 unsigned int abbrev_id = abbrev_opt_start - 1;
9426 unsigned int first_id = ~0U;
9427 unsigned int last_abbrev_id = 0;
9428 unsigned int i;
9429 dw_die_ref die;
9430 if (abbrev_opt_base_type_end > abbrev_opt_start)
9431 abbrev_id = abbrev_opt_base_type_end - 1;
9432 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9433 most commonly used abbreviations come first. */
9434 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9436 dw_attr_node *a;
9437 unsigned ix;
9439 /* If calc_base_type_die_sizes has been called, the CU and
9440 base types after it can't be optimized, because we've already
9441 calculated their DIE offsets. We've sorted them first. */
9442 if (die->die_abbrev < abbrev_opt_base_type_end)
9443 continue;
9444 if (die->die_abbrev != last_abbrev_id)
9446 last_abbrev_id = die->die_abbrev;
9447 if (dwarf_version >= 5 && first_id != ~0U)
9448 optimize_implicit_const (first_id, i, implicit_consts);
9449 abbrev_id++;
9450 (*abbrev_die_table)[abbrev_id] = die;
9451 if (dwarf_version >= 5)
9453 first_id = i;
9454 implicit_consts.truncate (0);
9456 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9457 switch (AT_class (a))
9459 case dw_val_class_const:
9460 case dw_val_class_unsigned_const:
9461 case dw_val_class_file:
9462 implicit_consts.safe_push (true);
9463 break;
9464 default:
9465 implicit_consts.safe_push (false);
9466 break;
9470 else if (dwarf_version >= 5)
9472 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9473 if (!implicit_consts[ix])
9474 continue;
9475 else
9477 dw_attr_node *other_a
9478 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9479 if (!dw_val_equal_p (&a->dw_attr_val,
9480 &other_a->dw_attr_val))
9481 implicit_consts[ix] = false;
9484 die->die_abbrev = abbrev_id;
9486 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9487 if (dwarf_version >= 5 && first_id != ~0U)
9488 optimize_implicit_const (first_id, i, implicit_consts);
9491 abbrev_opt_start = 0;
9492 abbrev_opt_base_type_end = 0;
9493 abbrev_usage_count.release ();
9494 sorted_abbrev_dies.release ();
9497 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9499 static int
9500 constant_size (unsigned HOST_WIDE_INT value)
9502 int log;
9504 if (value == 0)
9505 log = 0;
9506 else
9507 log = floor_log2 (value);
9509 log = log / 8;
9510 log = 1 << (floor_log2 (log) + 1);
9512 return log;
9515 /* Return the size of a DIE as it is represented in the
9516 .debug_info section. */
9518 static unsigned long
9519 size_of_die (dw_die_ref die)
9521 unsigned long size = 0;
9522 dw_attr_node *a;
9523 unsigned ix;
9524 enum dwarf_form form;
9526 size += size_of_uleb128 (die->die_abbrev);
9527 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9529 switch (AT_class (a))
9531 case dw_val_class_addr:
9532 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9534 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9535 size += size_of_uleb128 (AT_index (a));
9537 else
9538 size += DWARF2_ADDR_SIZE;
9539 break;
9540 case dw_val_class_offset:
9541 size += dwarf_offset_size;
9542 break;
9543 case dw_val_class_loc:
9545 unsigned long lsize = size_of_locs (AT_loc (a));
9547 /* Block length. */
9548 if (dwarf_version >= 4)
9549 size += size_of_uleb128 (lsize);
9550 else
9551 size += constant_size (lsize);
9552 size += lsize;
9554 break;
9555 case dw_val_class_loc_list:
9556 if (dwarf_split_debug_info && dwarf_version >= 5)
9558 gcc_assert (AT_loc_list (a)->num_assigned);
9559 size += size_of_uleb128 (AT_loc_list (a)->hash);
9561 else
9562 size += dwarf_offset_size;
9563 break;
9564 case dw_val_class_view_list:
9565 size += dwarf_offset_size;
9566 break;
9567 case dw_val_class_range_list:
9568 if (value_format (a) == DW_FORM_rnglistx)
9570 gcc_assert (rnglist_idx);
9571 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9572 size += size_of_uleb128 (r->idx);
9574 else
9575 size += dwarf_offset_size;
9576 break;
9577 case dw_val_class_const:
9578 size += size_of_sleb128 (AT_int (a));
9579 break;
9580 case dw_val_class_unsigned_const:
9582 int csize = constant_size (AT_unsigned (a));
9583 if (dwarf_version == 3
9584 && a->dw_attr == DW_AT_data_member_location
9585 && csize >= 4)
9586 size += size_of_uleb128 (AT_unsigned (a));
9587 else
9588 size += csize;
9590 break;
9591 case dw_val_class_symview:
9592 if (symview_upper_bound <= 0xff)
9593 size += 1;
9594 else if (symview_upper_bound <= 0xffff)
9595 size += 2;
9596 else if (symview_upper_bound <= 0xffffffff)
9597 size += 4;
9598 else
9599 size += 8;
9600 break;
9601 case dw_val_class_const_implicit:
9602 case dw_val_class_unsigned_const_implicit:
9603 case dw_val_class_file_implicit:
9604 /* These occupy no size in the DIE, just an extra sleb128 in
9605 .debug_abbrev. */
9606 break;
9607 case dw_val_class_const_double:
9608 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9609 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9610 size++; /* block */
9611 break;
9612 case dw_val_class_wide_int:
9613 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9614 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9615 if (get_full_len (*a->dw_attr_val.v.val_wide)
9616 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9617 size++; /* block */
9618 break;
9619 case dw_val_class_vec:
9620 size += constant_size (a->dw_attr_val.v.val_vec.length
9621 * a->dw_attr_val.v.val_vec.elt_size)
9622 + a->dw_attr_val.v.val_vec.length
9623 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9624 break;
9625 case dw_val_class_flag:
9626 if (dwarf_version >= 4)
9627 /* Currently all add_AT_flag calls pass in 1 as last argument,
9628 so DW_FORM_flag_present can be used. If that ever changes,
9629 we'll need to use DW_FORM_flag and have some optimization
9630 in build_abbrev_table that will change those to
9631 DW_FORM_flag_present if it is set to 1 in all DIEs using
9632 the same abbrev entry. */
9633 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9634 else
9635 size += 1;
9636 break;
9637 case dw_val_class_die_ref:
9638 if (AT_ref_external (a))
9640 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9641 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9642 is sized by target address length, whereas in DWARF3
9643 it's always sized as an offset. */
9644 if (AT_ref (a)->comdat_type_p)
9645 size += DWARF_TYPE_SIGNATURE_SIZE;
9646 else if (dwarf_version == 2)
9647 size += DWARF2_ADDR_SIZE;
9648 else
9649 size += dwarf_offset_size;
9651 else
9652 size += dwarf_offset_size;
9653 break;
9654 case dw_val_class_fde_ref:
9655 size += dwarf_offset_size;
9656 break;
9657 case dw_val_class_lbl_id:
9658 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9660 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9661 size += size_of_uleb128 (AT_index (a));
9663 else
9664 size += DWARF2_ADDR_SIZE;
9665 break;
9666 case dw_val_class_lineptr:
9667 case dw_val_class_macptr:
9668 case dw_val_class_loclistsptr:
9669 size += dwarf_offset_size;
9670 break;
9671 case dw_val_class_str:
9672 form = AT_string_form (a);
9673 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9674 size += dwarf_offset_size;
9675 else if (form == dwarf_FORM (DW_FORM_strx))
9676 size += size_of_uleb128 (AT_index (a));
9677 else
9678 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9679 break;
9680 case dw_val_class_file:
9681 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9682 break;
9683 case dw_val_class_data8:
9684 size += 8;
9685 break;
9686 case dw_val_class_vms_delta:
9687 size += dwarf_offset_size;
9688 break;
9689 case dw_val_class_high_pc:
9690 size += DWARF2_ADDR_SIZE;
9691 break;
9692 case dw_val_class_discr_value:
9693 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9694 break;
9695 case dw_val_class_discr_list:
9697 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9699 /* This is a block, so we have the block length and then its
9700 data. */
9701 size += constant_size (block_size) + block_size;
9703 break;
9704 default:
9705 gcc_unreachable ();
9709 return size;
9712 /* Size the debugging information associated with a given DIE. Visits the
9713 DIE's children recursively. Updates the global variable next_die_offset, on
9714 each time through. Uses the current value of next_die_offset to update the
9715 die_offset field in each DIE. */
9717 static void
9718 calc_die_sizes (dw_die_ref die)
9720 dw_die_ref c;
9722 gcc_assert (die->die_offset == 0
9723 || (unsigned long int) die->die_offset == next_die_offset);
9724 die->die_offset = next_die_offset;
9725 next_die_offset += size_of_die (die);
9727 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9729 if (die->die_child != NULL)
9730 /* Count the null byte used to terminate sibling lists. */
9731 next_die_offset += 1;
9734 /* Size just the base type children at the start of the CU.
9735 This is needed because build_abbrev needs to size locs
9736 and sizing of type based stack ops needs to know die_offset
9737 values for the base types. */
9739 static void
9740 calc_base_type_die_sizes (void)
9742 unsigned long die_offset = (dwarf_split_debug_info
9743 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9744 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9745 unsigned int i;
9746 dw_die_ref base_type;
9747 #if ENABLE_ASSERT_CHECKING
9748 dw_die_ref prev = comp_unit_die ()->die_child;
9749 #endif
9751 die_offset += size_of_die (comp_unit_die ());
9752 for (i = 0; base_types.iterate (i, &base_type); i++)
9754 #if ENABLE_ASSERT_CHECKING
9755 gcc_assert (base_type->die_offset == 0
9756 && prev->die_sib == base_type
9757 && base_type->die_child == NULL
9758 && base_type->die_abbrev);
9759 prev = base_type;
9760 #endif
9761 if (abbrev_opt_start
9762 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9763 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9764 base_type->die_offset = die_offset;
9765 die_offset += size_of_die (base_type);
9769 /* Set the marks for a die and its children. We do this so
9770 that we know whether or not a reference needs to use FORM_ref_addr; only
9771 DIEs in the same CU will be marked. We used to clear out the offset
9772 and use that as the flag, but ran into ordering problems. */
9774 static void
9775 mark_dies (dw_die_ref die)
9777 dw_die_ref c;
9779 gcc_assert (!die->die_mark);
9781 die->die_mark = 1;
9782 FOR_EACH_CHILD (die, c, mark_dies (c));
9785 /* Clear the marks for a die and its children. */
9787 static void
9788 unmark_dies (dw_die_ref die)
9790 dw_die_ref c;
9792 if (! use_debug_types)
9793 gcc_assert (die->die_mark);
9795 die->die_mark = 0;
9796 FOR_EACH_CHILD (die, c, unmark_dies (c));
9799 /* Clear the marks for a die, its children and referred dies. */
9801 static void
9802 unmark_all_dies (dw_die_ref die)
9804 dw_die_ref c;
9805 dw_attr_node *a;
9806 unsigned ix;
9808 if (!die->die_mark)
9809 return;
9810 die->die_mark = 0;
9812 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9814 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9815 if (AT_class (a) == dw_val_class_die_ref)
9816 unmark_all_dies (AT_ref (a));
9819 /* Calculate if the entry should appear in the final output file. It may be
9820 from a pruned a type. */
9822 static bool
9823 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9825 /* By limiting gnu pubnames to definitions only, gold can generate a
9826 gdb index without entries for declarations, which don't include
9827 enough information to be useful. */
9828 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9829 return false;
9831 if (table == pubname_table)
9833 /* Enumerator names are part of the pubname table, but the
9834 parent DW_TAG_enumeration_type die may have been pruned.
9835 Don't output them if that is the case. */
9836 if (p->die->die_tag == DW_TAG_enumerator &&
9837 (p->die->die_parent == NULL
9838 || !p->die->die_parent->die_perennial_p))
9839 return false;
9841 /* Everything else in the pubname table is included. */
9842 return true;
9845 /* The pubtypes table shouldn't include types that have been
9846 pruned. */
9847 return (p->die->die_offset != 0
9848 || !flag_eliminate_unused_debug_types);
9851 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9852 generated for the compilation unit. */
9854 static unsigned long
9855 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9857 unsigned long size;
9858 unsigned i;
9859 pubname_entry *p;
9860 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9862 size = DWARF_PUBNAMES_HEADER_SIZE;
9863 FOR_EACH_VEC_ELT (*names, i, p)
9864 if (include_pubname_in_output (names, p))
9865 size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9867 size += dwarf_offset_size;
9868 return size;
9871 /* Return the size of the information in the .debug_aranges section. */
9873 static unsigned long
9874 size_of_aranges (void)
9876 unsigned long size;
9878 size = DWARF_ARANGES_HEADER_SIZE;
9880 /* Count the address/length pair for this compilation unit. */
9881 if (switch_text_ranges)
9882 size += 2 * DWARF2_ADDR_SIZE
9883 * (vec_safe_length (switch_text_ranges) / 2 + 1);
9884 if (switch_cold_ranges)
9885 size += 2 * DWARF2_ADDR_SIZE
9886 * (vec_safe_length (switch_cold_ranges) / 2 + 1);
9887 if (have_multiple_function_sections)
9889 unsigned fde_idx;
9890 dw_fde_ref fde;
9892 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9894 if (fde->ignored_debug)
9895 continue;
9896 if (!fde->in_std_section)
9897 size += 2 * DWARF2_ADDR_SIZE;
9898 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9899 size += 2 * DWARF2_ADDR_SIZE;
9903 /* Count the two zero words used to terminated the address range table. */
9904 size += 2 * DWARF2_ADDR_SIZE;
9905 return size;
9908 /* Select the encoding of an attribute value. */
9910 static enum dwarf_form
9911 value_format (dw_attr_node *a)
9913 switch (AT_class (a))
9915 case dw_val_class_addr:
9916 /* Only very few attributes allow DW_FORM_addr. */
9917 switch (a->dw_attr)
9919 case DW_AT_low_pc:
9920 case DW_AT_high_pc:
9921 case DW_AT_entry_pc:
9922 case DW_AT_trampoline:
9923 return (AT_index (a) == NOT_INDEXED
9924 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9925 default:
9926 break;
9928 switch (DWARF2_ADDR_SIZE)
9930 case 1:
9931 return DW_FORM_data1;
9932 case 2:
9933 return DW_FORM_data2;
9934 case 4:
9935 return DW_FORM_data4;
9936 case 8:
9937 return DW_FORM_data8;
9938 default:
9939 gcc_unreachable ();
9941 case dw_val_class_loc_list:
9942 if (dwarf_split_debug_info
9943 && dwarf_version >= 5
9944 && AT_loc_list (a)->num_assigned)
9945 return DW_FORM_loclistx;
9946 /* FALLTHRU */
9947 case dw_val_class_view_list:
9948 case dw_val_class_range_list:
9949 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9950 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9951 care about sizes of .debug* sections in shared libraries and
9952 executables and don't take into account relocations that affect just
9953 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9954 table in the .debug_rnglists section. */
9955 if (dwarf_split_debug_info
9956 && dwarf_version >= 5
9957 && AT_class (a) == dw_val_class_range_list
9958 && rnglist_idx
9959 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9960 return DW_FORM_rnglistx;
9961 if (dwarf_version >= 4)
9962 return DW_FORM_sec_offset;
9963 /* FALLTHRU */
9964 case dw_val_class_vms_delta:
9965 case dw_val_class_offset:
9966 switch (dwarf_offset_size)
9968 case 4:
9969 return DW_FORM_data4;
9970 case 8:
9971 return DW_FORM_data8;
9972 default:
9973 gcc_unreachable ();
9975 case dw_val_class_loc:
9976 if (dwarf_version >= 4)
9977 return DW_FORM_exprloc;
9978 switch (constant_size (size_of_locs (AT_loc (a))))
9980 case 1:
9981 return DW_FORM_block1;
9982 case 2:
9983 return DW_FORM_block2;
9984 case 4:
9985 return DW_FORM_block4;
9986 default:
9987 gcc_unreachable ();
9989 case dw_val_class_const:
9990 return DW_FORM_sdata;
9991 case dw_val_class_unsigned_const:
9992 switch (constant_size (AT_unsigned (a)))
9994 case 1:
9995 return DW_FORM_data1;
9996 case 2:
9997 return DW_FORM_data2;
9998 case 4:
9999 /* In DWARF3 DW_AT_data_member_location with
10000 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
10001 constant, so we need to use DW_FORM_udata if we need
10002 a large constant. */
10003 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
10004 return DW_FORM_udata;
10005 return DW_FORM_data4;
10006 case 8:
10007 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
10008 return DW_FORM_udata;
10009 return DW_FORM_data8;
10010 default:
10011 gcc_unreachable ();
10013 case dw_val_class_const_implicit:
10014 case dw_val_class_unsigned_const_implicit:
10015 case dw_val_class_file_implicit:
10016 return DW_FORM_implicit_const;
10017 case dw_val_class_const_double:
10018 switch (HOST_BITS_PER_WIDE_INT)
10020 case 8:
10021 return DW_FORM_data2;
10022 case 16:
10023 return DW_FORM_data4;
10024 case 32:
10025 return DW_FORM_data8;
10026 case 64:
10027 if (dwarf_version >= 5)
10028 return DW_FORM_data16;
10029 /* FALLTHRU */
10030 default:
10031 return DW_FORM_block1;
10033 case dw_val_class_wide_int:
10034 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
10036 case 8:
10037 return DW_FORM_data1;
10038 case 16:
10039 return DW_FORM_data2;
10040 case 32:
10041 return DW_FORM_data4;
10042 case 64:
10043 return DW_FORM_data8;
10044 case 128:
10045 if (dwarf_version >= 5)
10046 return DW_FORM_data16;
10047 /* FALLTHRU */
10048 default:
10049 return DW_FORM_block1;
10051 case dw_val_class_symview:
10052 /* ??? We might use uleb128, but then we'd have to compute
10053 .debug_info offsets in the assembler. */
10054 if (symview_upper_bound <= 0xff)
10055 return DW_FORM_data1;
10056 else if (symview_upper_bound <= 0xffff)
10057 return DW_FORM_data2;
10058 else if (symview_upper_bound <= 0xffffffff)
10059 return DW_FORM_data4;
10060 else
10061 return DW_FORM_data8;
10062 case dw_val_class_vec:
10063 switch (constant_size (a->dw_attr_val.v.val_vec.length
10064 * a->dw_attr_val.v.val_vec.elt_size))
10066 case 1:
10067 return DW_FORM_block1;
10068 case 2:
10069 return DW_FORM_block2;
10070 case 4:
10071 return DW_FORM_block4;
10072 default:
10073 gcc_unreachable ();
10075 case dw_val_class_flag:
10076 if (dwarf_version >= 4)
10078 /* Currently all add_AT_flag calls pass in 1 as last argument,
10079 so DW_FORM_flag_present can be used. If that ever changes,
10080 we'll need to use DW_FORM_flag and have some optimization
10081 in build_abbrev_table that will change those to
10082 DW_FORM_flag_present if it is set to 1 in all DIEs using
10083 the same abbrev entry. */
10084 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10085 return DW_FORM_flag_present;
10087 return DW_FORM_flag;
10088 case dw_val_class_die_ref:
10089 if (AT_ref_external (a))
10091 if (AT_ref (a)->comdat_type_p)
10092 return DW_FORM_ref_sig8;
10093 else
10094 return DW_FORM_ref_addr;
10096 else
10097 return DW_FORM_ref;
10098 case dw_val_class_fde_ref:
10099 return DW_FORM_data;
10100 case dw_val_class_lbl_id:
10101 return (AT_index (a) == NOT_INDEXED
10102 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
10103 case dw_val_class_lineptr:
10104 case dw_val_class_macptr:
10105 case dw_val_class_loclistsptr:
10106 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10107 case dw_val_class_str:
10108 return AT_string_form (a);
10109 case dw_val_class_file:
10110 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10112 case 1:
10113 return DW_FORM_data1;
10114 case 2:
10115 return DW_FORM_data2;
10116 case 4:
10117 return DW_FORM_data4;
10118 default:
10119 gcc_unreachable ();
10122 case dw_val_class_data8:
10123 return DW_FORM_data8;
10125 case dw_val_class_high_pc:
10126 switch (DWARF2_ADDR_SIZE)
10128 case 1:
10129 return DW_FORM_data1;
10130 case 2:
10131 return DW_FORM_data2;
10132 case 4:
10133 return DW_FORM_data4;
10134 case 8:
10135 return DW_FORM_data8;
10136 default:
10137 gcc_unreachable ();
10140 case dw_val_class_discr_value:
10141 return (a->dw_attr_val.v.val_discr_value.pos
10142 ? DW_FORM_udata
10143 : DW_FORM_sdata);
10144 case dw_val_class_discr_list:
10145 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10147 case 1:
10148 return DW_FORM_block1;
10149 case 2:
10150 return DW_FORM_block2;
10151 case 4:
10152 return DW_FORM_block4;
10153 default:
10154 gcc_unreachable ();
10157 default:
10158 gcc_unreachable ();
10162 /* Output the encoding of an attribute value. */
10164 static void
10165 output_value_format (dw_attr_node *a)
10167 enum dwarf_form form = value_format (a);
10169 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10172 /* Given a die and id, produce the appropriate abbreviations. */
10174 static void
10175 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10177 unsigned ix;
10178 dw_attr_node *a_attr;
10180 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10181 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10182 dwarf_tag_name (abbrev->die_tag));
10184 if (abbrev->die_child != NULL)
10185 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10186 else
10187 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10189 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10191 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10192 dwarf_attr_name (a_attr->dw_attr));
10193 output_value_format (a_attr);
10194 if (value_format (a_attr) == DW_FORM_implicit_const)
10196 if (AT_class (a_attr) == dw_val_class_file_implicit)
10198 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10199 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10200 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10202 else
10203 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10207 dw2_asm_output_data (1, 0, NULL);
10208 dw2_asm_output_data (1, 0, NULL);
10212 /* Output the .debug_abbrev section which defines the DIE abbreviation
10213 table. */
10215 static void
10216 output_abbrev_section (void)
10218 unsigned int abbrev_id;
10219 dw_die_ref abbrev;
10221 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10222 if (abbrev_id != 0)
10223 output_die_abbrevs (abbrev_id, abbrev);
10225 /* Terminate the table. */
10226 dw2_asm_output_data (1, 0, NULL);
10229 /* Return a new location list, given the begin and end range, and the
10230 expression. */
10232 static inline dw_loc_list_ref
10233 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10234 const char *end, var_loc_view vend,
10235 const char *section)
10237 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10239 retlist->begin = begin;
10240 retlist->begin_entry = NULL;
10241 retlist->end = end;
10242 retlist->end_entry = NULL;
10243 retlist->expr = expr;
10244 retlist->section = section;
10245 retlist->vbegin = vbegin;
10246 retlist->vend = vend;
10248 return retlist;
10251 /* Return true iff there's any nonzero view number in the loc list.
10253 ??? When views are not enabled, we'll often extend a single range
10254 to the entire function, so that we emit a single location
10255 expression rather than a location list. With views, even with a
10256 single range, we'll output a list if start or end have a nonzero
10257 view. If we change this, we may want to stop splitting a single
10258 range in dw_loc_list just because of a nonzero view, even if it
10259 straddles across hot/cold partitions. */
10261 static bool
10262 loc_list_has_views (dw_loc_list_ref list)
10264 if (!debug_variable_location_views)
10265 return false;
10267 for (dw_loc_list_ref loc = list;
10268 loc != NULL; loc = loc->dw_loc_next)
10269 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10270 return true;
10272 return false;
10275 /* Generate a new internal symbol for this location list node, if it
10276 hasn't got one yet. */
10278 static inline void
10279 gen_llsym (dw_loc_list_ref list)
10281 gcc_assert (!list->ll_symbol);
10282 list->ll_symbol = gen_internal_sym ("LLST");
10284 if (!loc_list_has_views (list))
10285 return;
10287 if (dwarf2out_locviews_in_attribute ())
10289 /* Use the same label_num for the view list. */
10290 label_num--;
10291 list->vl_symbol = gen_internal_sym ("LVUS");
10293 else
10294 list->vl_symbol = list->ll_symbol;
10297 /* Generate a symbol for the list, but only if we really want to emit
10298 it as a list. */
10300 static inline void
10301 maybe_gen_llsym (dw_loc_list_ref list)
10303 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10304 return;
10306 gen_llsym (list);
10309 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10310 NULL, don't consider size of the location expression. If we're not
10311 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10312 representation in *SIZEP. */
10314 static bool
10315 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10317 /* Don't output an entry that starts and ends at the same address. */
10318 if (strcmp (curr->begin, curr->end) == 0
10319 && curr->vbegin == curr->vend && !curr->force)
10320 return true;
10322 if (!sizep)
10323 return false;
10325 unsigned long size = size_of_locs (curr->expr);
10327 /* If the expression is too large, drop it on the floor. We could
10328 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10329 in the expression, but >= 64KB expressions for a single value
10330 in a single range are unlikely very useful. */
10331 if (dwarf_version < 5 && size > 0xffff)
10332 return true;
10334 *sizep = size;
10336 return false;
10339 /* Output a view pair loclist entry for CURR, if it requires one. */
10341 static void
10342 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10344 if (!dwarf2out_locviews_in_loclist ())
10345 return;
10347 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10348 return;
10350 #ifdef DW_LLE_view_pair
10351 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10353 if (dwarf2out_as_locview_support)
10355 if (ZERO_VIEW_P (curr->vbegin))
10356 dw2_asm_output_data_uleb128 (0, "Location view begin");
10357 else
10359 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10360 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10361 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10364 if (ZERO_VIEW_P (curr->vend))
10365 dw2_asm_output_data_uleb128 (0, "Location view end");
10366 else
10368 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10369 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10370 dw2_asm_output_symname_uleb128 (label, "Location view end");
10373 else
10375 dw2_asm_output_data_uleb128 (ZERO_VIEW_P (curr->vbegin)
10376 ? 0 : curr->vbegin, "Location view begin");
10377 dw2_asm_output_data_uleb128 (ZERO_VIEW_P (curr->vend)
10378 ? 0 : curr->vend, "Location view end");
10380 #endif /* DW_LLE_view_pair */
10382 return;
10385 /* Output the location list given to us. */
10387 static void
10388 output_loc_list (dw_loc_list_ref list_head)
10390 int vcount = 0, lcount = 0;
10392 if (list_head->emitted)
10393 return;
10394 list_head->emitted = true;
10396 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10398 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10400 for (dw_loc_list_ref curr = list_head; curr != NULL;
10401 curr = curr->dw_loc_next)
10403 unsigned long size;
10405 if (skip_loc_list_entry (curr, &size))
10406 continue;
10408 vcount++;
10410 /* ?? dwarf_split_debug_info? */
10411 if (dwarf2out_as_locview_support)
10413 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10415 if (!ZERO_VIEW_P (curr->vbegin))
10417 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10418 dw2_asm_output_symname_uleb128 (label,
10419 "View list begin (%s)",
10420 list_head->vl_symbol);
10422 else
10423 dw2_asm_output_data_uleb128 (0,
10424 "View list begin (%s)",
10425 list_head->vl_symbol);
10427 if (!ZERO_VIEW_P (curr->vend))
10429 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10430 dw2_asm_output_symname_uleb128 (label,
10431 "View list end (%s)",
10432 list_head->vl_symbol);
10434 else
10435 dw2_asm_output_data_uleb128 (0,
10436 "View list end (%s)",
10437 list_head->vl_symbol);
10439 else
10441 dw2_asm_output_data_uleb128 (ZERO_VIEW_P (curr->vbegin)
10442 ? 0 : curr->vbegin,
10443 "View list begin (%s)",
10444 list_head->vl_symbol);
10445 dw2_asm_output_data_uleb128 (ZERO_VIEW_P (curr->vend)
10446 ? 0 : curr->vend,
10447 "View list end (%s)",
10448 list_head->vl_symbol);
10453 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10455 const char *last_section = NULL;
10456 const char *base_label = NULL;
10458 /* Walk the location list, and output each range + expression. */
10459 for (dw_loc_list_ref curr = list_head; curr != NULL;
10460 curr = curr->dw_loc_next)
10462 unsigned long size;
10464 /* Skip this entry? If we skip it here, we must skip it in the
10465 view list above as well. */
10466 if (skip_loc_list_entry (curr, &size))
10467 continue;
10469 lcount++;
10471 if (dwarf_version >= 5)
10473 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10475 dwarf2out_maybe_output_loclist_view_pair (curr);
10476 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10477 uleb128 index into .debug_addr and uleb128 length. */
10478 dw2_asm_output_data (1, DW_LLE_startx_length,
10479 "DW_LLE_startx_length (%s)",
10480 list_head->ll_symbol);
10481 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10482 "Location list range start index "
10483 "(%s)", curr->begin);
10484 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10485 "Location list length (%s)",
10486 list_head->ll_symbol);
10488 else if (dwarf_split_debug_info)
10490 dwarf2out_maybe_output_loclist_view_pair (curr);
10491 /* For -gsplit-dwarf without usable .uleb128 support, emit
10492 DW_LLE_startx_endx, which has two uleb128 indexes into
10493 .debug_addr. */
10494 dw2_asm_output_data (1, DW_LLE_startx_endx,
10495 "DW_LLE_startx_endx (%s)",
10496 list_head->ll_symbol);
10497 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10498 "Location list range start index "
10499 "(%s)", curr->begin);
10500 dw2_asm_output_data_uleb128 (curr->end_entry->index,
10501 "Location list range end index "
10502 "(%s)", curr->end);
10504 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10506 dwarf2out_maybe_output_loclist_view_pair (curr);
10507 /* If all code is in .text section, the base address is
10508 already provided by the CU attributes. Use
10509 DW_LLE_offset_pair where both addresses are uleb128 encoded
10510 offsets against that base. */
10511 dw2_asm_output_data (1, DW_LLE_offset_pair,
10512 "DW_LLE_offset_pair (%s)",
10513 list_head->ll_symbol);
10514 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10515 "Location list begin address (%s)",
10516 list_head->ll_symbol);
10517 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10518 "Location list end address (%s)",
10519 list_head->ll_symbol);
10521 else if (HAVE_AS_LEB128)
10523 /* Otherwise, find out how many consecutive entries could share
10524 the same base entry. If just one, emit DW_LLE_start_length,
10525 otherwise emit DW_LLE_base_address for the base address
10526 followed by a series of DW_LLE_offset_pair. */
10527 if (last_section == NULL || curr->section != last_section)
10529 dw_loc_list_ref curr2;
10530 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10531 curr2 = curr2->dw_loc_next)
10533 if (strcmp (curr2->begin, curr2->end) == 0
10534 && !curr2->force)
10535 continue;
10536 break;
10538 if (curr2 == NULL || curr->section != curr2->section)
10539 last_section = NULL;
10540 else
10542 last_section = curr->section;
10543 base_label = curr->begin;
10544 dw2_asm_output_data (1, DW_LLE_base_address,
10545 "DW_LLE_base_address (%s)",
10546 list_head->ll_symbol);
10547 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10548 "Base address (%s)",
10549 list_head->ll_symbol);
10552 /* Only one entry with the same base address. Use
10553 DW_LLE_start_length with absolute address and uleb128
10554 length. */
10555 if (last_section == NULL)
10557 dwarf2out_maybe_output_loclist_view_pair (curr);
10558 dw2_asm_output_data (1, DW_LLE_start_length,
10559 "DW_LLE_start_length (%s)",
10560 list_head->ll_symbol);
10561 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10562 "Location list begin address (%s)",
10563 list_head->ll_symbol);
10564 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10565 "Location list length "
10566 "(%s)", list_head->ll_symbol);
10568 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10569 DW_LLE_base_address. */
10570 else
10572 dwarf2out_maybe_output_loclist_view_pair (curr);
10573 dw2_asm_output_data (1, DW_LLE_offset_pair,
10574 "DW_LLE_offset_pair (%s)",
10575 list_head->ll_symbol);
10576 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10577 "Location list begin address "
10578 "(%s)", list_head->ll_symbol);
10579 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10580 "Location list end address "
10581 "(%s)", list_head->ll_symbol);
10584 /* The assembler does not support .uleb128 directive. Emit
10585 DW_LLE_start_end with a pair of absolute addresses. */
10586 else
10588 dwarf2out_maybe_output_loclist_view_pair (curr);
10589 dw2_asm_output_data (1, DW_LLE_start_end,
10590 "DW_LLE_start_end (%s)",
10591 list_head->ll_symbol);
10592 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10593 "Location list begin address (%s)",
10594 list_head->ll_symbol);
10595 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10596 "Location list end address (%s)",
10597 list_head->ll_symbol);
10600 else if (dwarf_split_debug_info)
10602 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10603 and 4 byte length. */
10604 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10605 "Location list start/length entry (%s)",
10606 list_head->ll_symbol);
10607 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10608 "Location list range start index (%s)",
10609 curr->begin);
10610 /* The length field is 4 bytes. If we ever need to support
10611 an 8-byte length, we can add a new DW_LLE code or fall back
10612 to DW_LLE_GNU_start_end_entry. */
10613 dw2_asm_output_delta (4, curr->end, curr->begin,
10614 "Location list range length (%s)",
10615 list_head->ll_symbol);
10617 else if (!have_multiple_function_sections)
10619 /* Pair of relative addresses against start of text section. */
10620 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10621 "Location list begin address (%s)",
10622 list_head->ll_symbol);
10623 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10624 "Location list end address (%s)",
10625 list_head->ll_symbol);
10627 else
10629 /* Pair of absolute addresses. */
10630 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10631 "Location list begin address (%s)",
10632 list_head->ll_symbol);
10633 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10634 "Location list end address (%s)",
10635 list_head->ll_symbol);
10638 /* Output the block length for this list of location operations. */
10639 if (dwarf_version >= 5)
10640 dw2_asm_output_data_uleb128 (size, "Location expression size");
10641 else
10643 gcc_assert (size <= 0xffff);
10644 dw2_asm_output_data (2, size, "Location expression size");
10647 output_loc_sequence (curr->expr, -1);
10650 /* And finally list termination. */
10651 if (dwarf_version >= 5)
10652 dw2_asm_output_data (1, DW_LLE_end_of_list,
10653 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10654 else if (dwarf_split_debug_info)
10655 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10656 "Location list terminator (%s)",
10657 list_head->ll_symbol);
10658 else
10660 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10661 "Location list terminator begin (%s)",
10662 list_head->ll_symbol);
10663 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10664 "Location list terminator end (%s)",
10665 list_head->ll_symbol);
10668 gcc_assert (!list_head->vl_symbol
10669 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10672 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10673 section. Emit a relocated reference if val_entry is NULL, otherwise,
10674 emit an indirect reference. */
10676 static void
10677 output_range_list_offset (dw_attr_node *a)
10679 const char *name = dwarf_attr_name (a->dw_attr);
10681 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10683 if (dwarf_version >= 5)
10685 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10686 dw2_asm_output_offset (dwarf_offset_size, r->label,
10687 debug_ranges_section, "%s", name);
10689 else
10691 char *p = strchr (ranges_section_label, '\0');
10692 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10693 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10694 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10695 debug_ranges_section, "%s", name);
10696 *p = '\0';
10699 else if (dwarf_version >= 5)
10701 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10702 gcc_assert (rnglist_idx);
10703 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10705 else
10706 dw2_asm_output_data (dwarf_offset_size,
10707 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10708 "%s (offset from %s)", name, ranges_section_label);
10711 /* Output the offset into the debug_loc section. */
10713 static void
10714 output_loc_list_offset (dw_attr_node *a)
10716 char *sym = AT_loc_list (a)->ll_symbol;
10718 gcc_assert (sym);
10719 if (!dwarf_split_debug_info)
10720 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10721 "%s", dwarf_attr_name (a->dw_attr));
10722 else if (dwarf_version >= 5)
10724 gcc_assert (AT_loc_list (a)->num_assigned);
10725 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10726 dwarf_attr_name (a->dw_attr),
10727 sym);
10729 else
10730 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10731 "%s", dwarf_attr_name (a->dw_attr));
10734 /* Output the offset into the debug_loc section. */
10736 static void
10737 output_view_list_offset (dw_attr_node *a)
10739 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10741 gcc_assert (sym);
10742 if (dwarf_split_debug_info)
10743 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10744 "%s", dwarf_attr_name (a->dw_attr));
10745 else
10746 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10747 "%s", dwarf_attr_name (a->dw_attr));
10750 /* Output an attribute's index or value appropriately. */
10752 static void
10753 output_attr_index_or_value (dw_attr_node *a)
10755 const char *name = dwarf_attr_name (a->dw_attr);
10757 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10759 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10760 return;
10762 switch (AT_class (a))
10764 case dw_val_class_addr:
10765 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10766 break;
10767 case dw_val_class_high_pc:
10768 case dw_val_class_lbl_id:
10769 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10770 break;
10771 default:
10772 gcc_unreachable ();
10776 /* Output a type signature. */
10778 static inline void
10779 output_signature (const char *sig, const char *name)
10781 int i;
10783 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10784 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10787 /* Output a discriminant value. */
10789 static inline void
10790 output_discr_value (dw_discr_value *discr_value, const char *name)
10792 if (discr_value->pos)
10793 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10794 else
10795 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10798 /* Output the DIE and its attributes. Called recursively to generate
10799 the definitions of each child DIE. */
10801 static void
10802 output_die (dw_die_ref die)
10804 dw_attr_node *a;
10805 dw_die_ref c;
10806 unsigned long size;
10807 unsigned ix;
10809 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10810 (unsigned long)die->die_offset,
10811 dwarf_tag_name (die->die_tag));
10813 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10815 const char *name = dwarf_attr_name (a->dw_attr);
10817 switch (AT_class (a))
10819 case dw_val_class_addr:
10820 output_attr_index_or_value (a);
10821 break;
10823 case dw_val_class_offset:
10824 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10825 "%s", name);
10826 break;
10828 case dw_val_class_range_list:
10829 output_range_list_offset (a);
10830 break;
10832 case dw_val_class_loc:
10833 size = size_of_locs (AT_loc (a));
10835 /* Output the block length for this list of location operations. */
10836 if (dwarf_version >= 4)
10837 dw2_asm_output_data_uleb128 (size, "%s", name);
10838 else
10839 dw2_asm_output_data (constant_size (size), size, "%s", name);
10841 output_loc_sequence (AT_loc (a), -1);
10842 break;
10844 case dw_val_class_const:
10845 /* ??? It would be slightly more efficient to use a scheme like is
10846 used for unsigned constants below, but gdb 4.x does not sign
10847 extend. Gdb 5.x does sign extend. */
10848 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10849 break;
10851 case dw_val_class_unsigned_const:
10853 int csize = constant_size (AT_unsigned (a));
10854 if (dwarf_version == 3
10855 && a->dw_attr == DW_AT_data_member_location
10856 && csize >= 4)
10857 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10858 else
10859 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10861 break;
10863 case dw_val_class_symview:
10865 int vsize;
10866 if (symview_upper_bound <= 0xff)
10867 vsize = 1;
10868 else if (symview_upper_bound <= 0xffff)
10869 vsize = 2;
10870 else if (symview_upper_bound <= 0xffffffff)
10871 vsize = 4;
10872 else
10873 vsize = 8;
10874 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10875 "%s", name);
10877 break;
10879 case dw_val_class_const_implicit:
10880 if (flag_debug_asm)
10881 fprintf (asm_out_file, "\t\t\t%s %s ("
10882 HOST_WIDE_INT_PRINT_DEC ")\n",
10883 ASM_COMMENT_START, name, AT_int (a));
10884 break;
10886 case dw_val_class_unsigned_const_implicit:
10887 if (flag_debug_asm)
10888 fprintf (asm_out_file, "\t\t\t%s %s ("
10889 HOST_WIDE_INT_PRINT_HEX ")\n",
10890 ASM_COMMENT_START, name, AT_unsigned (a));
10891 break;
10893 case dw_val_class_const_double:
10895 unsigned HOST_WIDE_INT first, second;
10897 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10898 dw2_asm_output_data (1,
10899 HOST_BITS_PER_DOUBLE_INT
10900 / HOST_BITS_PER_CHAR,
10901 NULL);
10903 if (WORDS_BIG_ENDIAN)
10905 first = a->dw_attr_val.v.val_double.high;
10906 second = a->dw_attr_val.v.val_double.low;
10908 else
10910 first = a->dw_attr_val.v.val_double.low;
10911 second = a->dw_attr_val.v.val_double.high;
10914 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10915 first, "%s", name);
10916 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10917 second, NULL);
10919 break;
10921 case dw_val_class_wide_int:
10923 int i;
10924 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10925 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10926 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10927 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10928 * l, NULL);
10930 if (WORDS_BIG_ENDIAN)
10931 for (i = len - 1; i >= 0; --i)
10933 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10934 "%s", name);
10935 name = "";
10937 else
10938 for (i = 0; i < len; ++i)
10940 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10941 "%s", name);
10942 name = "";
10945 break;
10947 case dw_val_class_vec:
10949 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10950 unsigned int len = a->dw_attr_val.v.val_vec.length;
10951 unsigned int i;
10952 unsigned char *p;
10954 dw2_asm_output_data (constant_size (len * elt_size),
10955 len * elt_size, "%s", name);
10956 if (elt_size > sizeof (HOST_WIDE_INT))
10958 elt_size /= 2;
10959 len *= 2;
10961 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10962 i < len;
10963 i++, p += elt_size)
10964 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10965 "fp or vector constant word %u", i);
10966 break;
10969 case dw_val_class_flag:
10970 if (dwarf_version >= 4)
10972 /* Currently all add_AT_flag calls pass in 1 as last argument,
10973 so DW_FORM_flag_present can be used. If that ever changes,
10974 we'll need to use DW_FORM_flag and have some optimization
10975 in build_abbrev_table that will change those to
10976 DW_FORM_flag_present if it is set to 1 in all DIEs using
10977 the same abbrev entry. */
10978 gcc_assert (AT_flag (a) == 1);
10979 if (flag_debug_asm)
10980 fprintf (asm_out_file, "\t\t\t%s %s\n",
10981 ASM_COMMENT_START, name);
10982 break;
10984 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10985 break;
10987 case dw_val_class_loc_list:
10988 output_loc_list_offset (a);
10989 break;
10991 case dw_val_class_view_list:
10992 output_view_list_offset (a);
10993 break;
10995 case dw_val_class_die_ref:
10996 if (AT_ref_external (a))
10998 if (AT_ref (a)->comdat_type_p)
11000 comdat_type_node *type_node
11001 = AT_ref (a)->die_id.die_type_node;
11003 gcc_assert (type_node);
11004 output_signature (type_node->signature, name);
11006 else
11008 const char *sym = AT_ref (a)->die_id.die_symbol;
11009 int size;
11011 gcc_assert (sym);
11012 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11013 length, whereas in DWARF3 it's always sized as an
11014 offset. */
11015 if (dwarf_version == 2)
11016 size = DWARF2_ADDR_SIZE;
11017 else
11018 size = dwarf_offset_size;
11019 /* ??? We cannot unconditionally output die_offset if
11020 non-zero - others might create references to those
11021 DIEs via symbols.
11022 And we do not clear its DIE offset after outputting it
11023 (and the label refers to the actual DIEs, not the
11024 DWARF CU unit header which is when using label + offset
11025 would be the correct thing to do).
11026 ??? This is the reason for the with_offset flag. */
11027 if (AT_ref (a)->with_offset)
11028 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
11029 debug_info_section, "%s", name);
11030 else
11031 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11032 name);
11035 else
11037 gcc_assert (AT_ref (a)->die_offset);
11038 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
11039 "%s", name);
11041 break;
11043 case dw_val_class_fde_ref:
11045 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11047 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11048 a->dw_attr_val.v.val_fde_index * 2);
11049 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
11050 "%s", name);
11052 break;
11054 case dw_val_class_vms_delta:
11055 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
11056 dw2_asm_output_vms_delta (dwarf_offset_size,
11057 AT_vms_delta2 (a), AT_vms_delta1 (a),
11058 "%s", name);
11059 #else
11060 dw2_asm_output_delta (dwarf_offset_size,
11061 AT_vms_delta2 (a), AT_vms_delta1 (a),
11062 "%s", name);
11063 #endif
11064 break;
11066 case dw_val_class_lbl_id:
11067 output_attr_index_or_value (a);
11068 break;
11070 case dw_val_class_lineptr:
11071 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11072 debug_line_section, "%s", name);
11073 break;
11075 case dw_val_class_macptr:
11076 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11077 debug_macinfo_section, "%s", name);
11078 break;
11080 case dw_val_class_loclistsptr:
11081 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11082 debug_loc_section, "%s", name);
11083 break;
11085 case dw_val_class_str:
11086 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11087 dw2_asm_output_offset (dwarf_offset_size,
11088 a->dw_attr_val.v.val_str->label,
11089 debug_str_section,
11090 "%s: \"%s\"", name, AT_string (a));
11091 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11092 dw2_asm_output_offset (dwarf_offset_size,
11093 a->dw_attr_val.v.val_str->label,
11094 debug_line_str_section,
11095 "%s: \"%s\"", name, AT_string (a));
11096 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
11097 dw2_asm_output_data_uleb128 (AT_index (a),
11098 "%s: \"%s\"", name, AT_string (a));
11099 else
11100 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11101 break;
11103 case dw_val_class_file:
11105 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11107 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11108 a->dw_attr_val.v.val_file->filename);
11109 break;
11112 case dw_val_class_file_implicit:
11113 if (flag_debug_asm)
11114 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
11115 ASM_COMMENT_START, name,
11116 maybe_emit_file (a->dw_attr_val.v.val_file),
11117 a->dw_attr_val.v.val_file->filename);
11118 break;
11120 case dw_val_class_data8:
11122 int i;
11124 for (i = 0; i < 8; i++)
11125 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11126 i == 0 ? "%s" : NULL, name);
11127 break;
11130 case dw_val_class_high_pc:
11131 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11132 get_AT_low_pc (die), "DW_AT_high_pc");
11133 break;
11135 case dw_val_class_discr_value:
11136 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11137 break;
11139 case dw_val_class_discr_list:
11141 dw_discr_list_ref list = AT_discr_list (a);
11142 const int size = size_of_discr_list (list);
11144 /* This is a block, so output its length first. */
11145 dw2_asm_output_data (constant_size (size), size,
11146 "%s: block size", name);
11148 for (; list != NULL; list = list->dw_discr_next)
11150 /* One byte for the discriminant value descriptor, and then as
11151 many LEB128 numbers as required. */
11152 if (list->dw_discr_range)
11153 dw2_asm_output_data (1, DW_DSC_range,
11154 "%s: DW_DSC_range", name);
11155 else
11156 dw2_asm_output_data (1, DW_DSC_label,
11157 "%s: DW_DSC_label", name);
11159 output_discr_value (&list->dw_discr_lower_bound, name);
11160 if (list->dw_discr_range)
11161 output_discr_value (&list->dw_discr_upper_bound, name);
11163 break;
11166 default:
11167 gcc_unreachable ();
11171 FOR_EACH_CHILD (die, c, output_die (c));
11173 /* Add null byte to terminate sibling list. */
11174 if (die->die_child != NULL)
11175 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11176 (unsigned long) die->die_offset);
11179 /* Output the dwarf version number. */
11181 static void
11182 output_dwarf_version ()
11184 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11185 views in loclist. That will change eventually. */
11186 if (dwarf_version == 6)
11188 static bool once;
11189 if (!once)
11191 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11192 "incompatibilities");
11193 once = true;
11195 dw2_asm_output_data (2, 5, "DWARF version number");
11197 else
11198 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11201 /* Output the compilation unit that appears at the beginning of the
11202 .debug_info section, and precedes the DIE descriptions. */
11204 static void
11205 output_compilation_unit_header (enum dwarf_unit_type ut)
11207 if (!XCOFF_DEBUGGING_INFO)
11209 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11210 dw2_asm_output_data (4, 0xffffffff,
11211 "Initial length escape value indicating 64-bit DWARF extension");
11212 dw2_asm_output_data (dwarf_offset_size,
11213 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11214 "Length of Compilation Unit Info");
11217 output_dwarf_version ();
11218 if (dwarf_version >= 5)
11220 const char *name;
11221 switch (ut)
11223 case DW_UT_compile: name = "DW_UT_compile"; break;
11224 case DW_UT_type: name = "DW_UT_type"; break;
11225 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11226 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11227 default: gcc_unreachable ();
11229 dw2_asm_output_data (1, ut, "%s", name);
11230 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11232 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11233 debug_abbrev_section,
11234 "Offset Into Abbrev. Section");
11235 if (dwarf_version < 5)
11236 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11239 /* Output the compilation unit DIE and its children. */
11241 static void
11242 output_comp_unit (dw_die_ref die, int output_if_empty,
11243 const unsigned char *dwo_id)
11245 const char *secname, *oldsym;
11246 char *tmp;
11248 /* Unless we are outputting main CU, we may throw away empty ones. */
11249 if (!output_if_empty && die->die_child == NULL)
11250 return;
11252 /* Even if there are no children of this DIE, we must output the information
11253 about the compilation unit. Otherwise, on an empty translation unit, we
11254 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11255 will then complain when examining the file. First mark all the DIEs in
11256 this CU so we know which get local refs. */
11257 mark_dies (die);
11259 external_ref_hash_type *extern_map = optimize_external_refs (die);
11261 /* For now, optimize only the main CU, in order to optimize the rest
11262 we'd need to see all of them earlier. Leave the rest for post-linking
11263 tools like DWZ. */
11264 if (die == comp_unit_die ())
11265 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11267 build_abbrev_table (die, extern_map);
11269 optimize_abbrev_table ();
11271 delete extern_map;
11273 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11274 next_die_offset = (dwo_id
11275 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11276 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11277 calc_die_sizes (die);
11279 oldsym = die->die_id.die_symbol;
11280 if (oldsym && die->comdat_type_p)
11282 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11284 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11285 secname = tmp;
11286 die->die_id.die_symbol = NULL;
11287 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11289 else
11291 switch_to_section (debug_info_section);
11292 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11293 info_section_emitted = true;
11296 /* For LTO cross unit DIE refs we want a symbol on the start of the
11297 debuginfo section, not on the CU DIE. */
11298 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11300 /* ??? No way to get visibility assembled without a decl. */
11301 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11302 get_identifier (oldsym), char_type_node);
11303 TREE_PUBLIC (decl) = true;
11304 TREE_STATIC (decl) = true;
11305 DECL_ARTIFICIAL (decl) = true;
11306 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11307 DECL_VISIBILITY_SPECIFIED (decl) = true;
11308 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11309 #ifdef ASM_WEAKEN_LABEL
11310 /* We prefer a .weak because that handles duplicates from duplicate
11311 archive members in a graceful way. */
11312 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11313 #else
11314 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11315 #endif
11316 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11319 /* Output debugging information. */
11320 output_compilation_unit_header (dwo_id
11321 ? DW_UT_split_compile : DW_UT_compile);
11322 if (dwarf_version >= 5)
11324 if (dwo_id != NULL)
11325 for (int i = 0; i < 8; i++)
11326 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11328 output_die (die);
11330 /* Leave the marks on the main CU, so we can check them in
11331 output_pubnames. */
11332 if (oldsym)
11334 unmark_dies (die);
11335 die->die_id.die_symbol = oldsym;
11339 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11340 and .debug_pubtypes. This is configured per-target, but can be
11341 overridden by the -gpubnames or -gno-pubnames options. */
11343 static inline bool
11344 want_pubnames (void)
11346 if (debug_info_level <= DINFO_LEVEL_TERSE
11347 /* Names and types go to the early debug part only. */
11348 || in_lto_p)
11349 return false;
11350 if (debug_generate_pub_sections != -1)
11351 return debug_generate_pub_sections;
11352 return targetm.want_debug_pub_sections;
11355 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11357 static void
11358 add_AT_pubnames (dw_die_ref die)
11360 if (want_pubnames ())
11361 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11364 /* Add a string attribute value to a skeleton DIE. */
11366 static inline void
11367 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11368 const char *str)
11370 dw_attr_node attr;
11371 struct indirect_string_node *node;
11373 if (! skeleton_debug_str_hash)
11374 skeleton_debug_str_hash
11375 = hash_table<indirect_string_hasher>::create_ggc (10);
11377 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11378 find_string_form (node);
11379 if (node->form == dwarf_FORM (DW_FORM_strx))
11380 node->form = DW_FORM_strp;
11382 attr.dw_attr = attr_kind;
11383 attr.dw_attr_val.val_class = dw_val_class_str;
11384 attr.dw_attr_val.val_entry = NULL;
11385 attr.dw_attr_val.v.val_str = node;
11386 add_dwarf_attr (die, &attr);
11389 /* Helper function to generate top-level dies for skeleton debug_info and
11390 debug_types. */
11392 static void
11393 add_top_level_skeleton_die_attrs (dw_die_ref die)
11395 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11396 const char *comp_dir = comp_dir_string ();
11398 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11399 if (comp_dir != NULL)
11400 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11401 add_AT_pubnames (die);
11402 if (addr_index_table != NULL && addr_index_table->size () > 0)
11403 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11406 /* Output skeleton debug sections that point to the dwo file. */
11408 static void
11409 output_skeleton_debug_sections (dw_die_ref comp_unit,
11410 const unsigned char *dwo_id)
11412 /* These attributes will be found in the full debug_info section. */
11413 remove_AT (comp_unit, DW_AT_producer);
11414 remove_AT (comp_unit, DW_AT_language);
11415 remove_AT (comp_unit, DW_AT_language_name);
11416 remove_AT (comp_unit, DW_AT_language_version);
11418 switch_to_section (debug_skeleton_info_section);
11419 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11421 /* Produce the skeleton compilation-unit header. This one differs enough from
11422 a normal CU header that it's better not to call output_compilation_unit
11423 header. */
11424 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11425 dw2_asm_output_data (4, 0xffffffff,
11426 "Initial length escape value indicating 64-bit "
11427 "DWARF extension");
11429 dw2_asm_output_data (dwarf_offset_size,
11430 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11431 - DWARF_INITIAL_LENGTH_SIZE
11432 + size_of_die (comp_unit),
11433 "Length of Compilation Unit Info");
11434 output_dwarf_version ();
11435 if (dwarf_version >= 5)
11437 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11438 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11440 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11441 debug_skeleton_abbrev_section,
11442 "Offset Into Abbrev. Section");
11443 if (dwarf_version < 5)
11444 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11445 else
11446 for (int i = 0; i < 8; i++)
11447 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11449 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11450 output_die (comp_unit);
11452 /* Build the skeleton debug_abbrev section. */
11453 switch_to_section (debug_skeleton_abbrev_section);
11454 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11456 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11458 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11461 /* Output a comdat type unit DIE and its children. */
11463 static void
11464 output_comdat_type_unit (comdat_type_node *node,
11465 bool early_lto_debug ATTRIBUTE_UNUSED)
11467 const char *secname;
11468 char *tmp;
11469 int i;
11470 #if defined (OBJECT_FORMAT_ELF)
11471 tree comdat_key;
11472 #endif
11474 /* First mark all the DIEs in this CU so we know which get local refs. */
11475 mark_dies (node->root_die);
11477 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11479 build_abbrev_table (node->root_die, extern_map);
11481 delete extern_map;
11482 extern_map = NULL;
11484 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11485 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11486 calc_die_sizes (node->root_die);
11488 #if defined (OBJECT_FORMAT_ELF)
11489 if (dwarf_version >= 5)
11491 if (!dwarf_split_debug_info)
11492 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11493 else
11494 secname = (early_lto_debug
11495 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11497 else if (!dwarf_split_debug_info)
11498 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11499 else
11500 secname = (early_lto_debug
11501 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11503 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11504 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11505 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11506 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11507 comdat_key = get_identifier (tmp);
11508 targetm.asm_out.named_section (secname,
11509 SECTION_DEBUG | SECTION_LINKONCE,
11510 comdat_key);
11511 #else
11512 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11513 sprintf (tmp, (dwarf_version >= 5
11514 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11515 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11516 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11517 secname = tmp;
11518 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11519 #endif
11521 /* Output debugging information. */
11522 output_compilation_unit_header (dwarf_split_debug_info
11523 ? DW_UT_split_type : DW_UT_type);
11524 output_signature (node->signature, "Type Signature");
11525 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11526 "Offset to Type DIE");
11527 output_die (node->root_die);
11529 unmark_dies (node->root_die);
11532 /* Return the DWARF2/3 pubname associated with a decl. */
11534 static const char *
11535 dwarf2_name (tree decl, int scope)
11537 if (DECL_NAMELESS (decl))
11538 return NULL;
11539 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11542 /* Add a new entry to .debug_pubnames if appropriate. */
11544 static void
11545 add_pubname_string (const char *str, dw_die_ref die)
11547 pubname_entry e;
11549 e.die = die;
11550 e.name = xstrdup (str);
11551 vec_safe_push (pubname_table, e);
11554 static void
11555 add_pubname (tree decl, dw_die_ref die)
11557 if (!want_pubnames ())
11558 return;
11560 /* Don't add items to the table when we expect that the consumer will have
11561 just read the enclosing die. For example, if the consumer is looking at a
11562 class_member, it will either be inside the class already, or will have just
11563 looked up the class to find the member. Either way, searching the class is
11564 faster than searching the index. */
11565 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11566 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11568 const char *name = dwarf2_name (decl, 1);
11570 if (name)
11571 add_pubname_string (name, die);
11575 /* Add an enumerator to the pubnames section. */
11577 static void
11578 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11580 pubname_entry e;
11582 gcc_assert (scope_name);
11583 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11584 e.die = die;
11585 vec_safe_push (pubname_table, e);
11588 /* Add a new entry to .debug_pubtypes if appropriate. */
11590 static void
11591 add_pubtype (tree decl, dw_die_ref die)
11593 pubname_entry e;
11595 if (!want_pubnames ())
11596 return;
11598 if ((TREE_PUBLIC (decl)
11599 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11600 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11602 tree scope = NULL;
11603 const char *scope_name = "";
11604 const char *sep = is_cxx () ? "::" : ".";
11605 const char *name;
11607 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11608 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11610 scope_name = lang_hooks.dwarf_name (scope, 1);
11611 if (scope_name != NULL && scope_name[0] != '\0')
11612 scope_name = concat (scope_name, sep, NULL);
11613 else
11614 scope_name = "";
11617 if (TYPE_P (decl))
11618 name = type_tag (decl);
11619 else
11620 name = lang_hooks.dwarf_name (decl, 1);
11622 /* If we don't have a name for the type, there's no point in adding
11623 it to the table. */
11624 if (name != NULL && name[0] != '\0')
11626 e.die = die;
11627 e.name = concat (scope_name, name, NULL);
11628 vec_safe_push (pubtype_table, e);
11631 /* Although it might be more consistent to add the pubinfo for the
11632 enumerators as their dies are created, they should only be added if the
11633 enum type meets the criteria above. So rather than re-check the parent
11634 enum type whenever an enumerator die is created, just output them all
11635 here. This isn't protected by the name conditional because anonymous
11636 enums don't have names. */
11637 if (die->die_tag == DW_TAG_enumeration_type)
11639 dw_die_ref c;
11641 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11646 /* Output a single entry in the pubnames table. */
11648 static void
11649 output_pubname (dw_offset die_offset, pubname_entry *entry)
11651 dw_die_ref die = entry->die;
11652 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11654 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11656 if (debug_generate_pub_sections == 2)
11658 /* This logic follows gdb's method for determining the value of the flag
11659 byte. */
11660 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11661 switch (die->die_tag)
11663 case DW_TAG_typedef:
11664 case DW_TAG_base_type:
11665 case DW_TAG_subrange_type:
11666 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11667 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11668 break;
11669 case DW_TAG_enumerator:
11670 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11671 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11672 if (!is_cxx ())
11673 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11674 break;
11675 case DW_TAG_subprogram:
11676 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11677 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11678 if (!is_ada ())
11679 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11680 break;
11681 case DW_TAG_constant:
11682 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11683 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11684 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11685 break;
11686 case DW_TAG_variable:
11687 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11688 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11689 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11690 break;
11691 case DW_TAG_namespace:
11692 case DW_TAG_imported_declaration:
11693 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11694 break;
11695 case DW_TAG_class_type:
11696 case DW_TAG_interface_type:
11697 case DW_TAG_structure_type:
11698 case DW_TAG_union_type:
11699 case DW_TAG_enumeration_type:
11700 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11701 if (!is_cxx ())
11702 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11703 break;
11704 default:
11705 /* An unusual tag. Leave the flag-byte empty. */
11706 break;
11708 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11709 "GDB-index flags");
11712 dw2_asm_output_nstring (entry->name, -1, "external name");
11716 /* Output the public names table used to speed up access to externally
11717 visible names; or the public types table used to find type definitions. */
11719 static void
11720 output_pubnames (vec<pubname_entry, va_gc> *names)
11722 unsigned i;
11723 unsigned long pubnames_length = size_of_pubnames (names);
11724 pubname_entry *pub;
11726 if (!XCOFF_DEBUGGING_INFO)
11728 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11729 dw2_asm_output_data (4, 0xffffffff,
11730 "Initial length escape value indicating 64-bit DWARF extension");
11731 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11732 "Pub Info Length");
11735 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11736 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11738 if (dwarf_split_debug_info)
11739 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11740 debug_skeleton_info_section,
11741 "Offset of Compilation Unit Info");
11742 else
11743 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11744 debug_info_section,
11745 "Offset of Compilation Unit Info");
11746 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11747 "Compilation Unit Length");
11749 FOR_EACH_VEC_ELT (*names, i, pub)
11751 if (include_pubname_in_output (names, pub))
11753 dw_offset die_offset = pub->die->die_offset;
11755 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11756 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11757 gcc_assert (pub->die->die_mark);
11759 /* If we're putting types in their own .debug_types sections,
11760 the .debug_pubtypes table will still point to the compile
11761 unit (not the type unit), so we want to use the offset of
11762 the skeleton DIE (if there is one). */
11763 if (pub->die->comdat_type_p && names == pubtype_table)
11765 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11767 if (type_node != NULL)
11768 die_offset = (type_node->skeleton_die != NULL
11769 ? type_node->skeleton_die->die_offset
11770 : comp_unit_die ()->die_offset);
11773 output_pubname (die_offset, pub);
11777 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11780 /* Output public names and types tables if necessary. */
11782 static void
11783 output_pubtables (void)
11785 if (!want_pubnames () || !info_section_emitted)
11786 return;
11788 switch_to_section (debug_pubnames_section);
11789 output_pubnames (pubname_table);
11790 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11791 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11792 simply won't look for the section. */
11793 switch_to_section (debug_pubtypes_section);
11794 output_pubnames (pubtype_table);
11798 /* Output the information that goes into the .debug_aranges table.
11799 Namely, define the beginning and ending address range of the
11800 text section generated for this compilation unit. */
11802 static void
11803 output_aranges (void)
11805 unsigned i;
11806 unsigned long aranges_length = size_of_aranges ();
11808 if (!XCOFF_DEBUGGING_INFO)
11810 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11811 dw2_asm_output_data (4, 0xffffffff,
11812 "Initial length escape value indicating 64-bit DWARF extension");
11813 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11814 "Length of Address Ranges Info");
11817 /* Version number for aranges is still 2, even up to DWARF5. */
11818 dw2_asm_output_data (2, 2, "DWARF aranges version");
11819 if (dwarf_split_debug_info)
11820 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11821 debug_skeleton_info_section,
11822 "Offset of Compilation Unit Info");
11823 else
11824 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11825 debug_info_section,
11826 "Offset of Compilation Unit Info");
11827 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11828 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11830 /* We need to align to twice the pointer size here. */
11831 if (DWARF_ARANGES_PAD_SIZE)
11833 /* Pad using a 2 byte words so that padding is correct for any
11834 pointer size. */
11835 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11836 2 * DWARF2_ADDR_SIZE);
11837 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11838 dw2_asm_output_data (2, 0, NULL);
11841 /* It is necessary not to output these entries if the sections were
11842 not used; if the sections were not used, the length will be 0 and
11843 the address may end up as 0 if the section is discarded by ld
11844 --gc-sections, leaving an invalid (0, 0) entry that can be
11845 confused with the terminator. */
11846 if (switch_text_ranges)
11848 const char *prev_loc = text_section_label;
11849 const char *loc;
11850 unsigned idx;
11852 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11853 if (prev_loc)
11855 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11856 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11857 prev_loc = NULL;
11859 else
11860 prev_loc = loc;
11862 if (prev_loc)
11864 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11865 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11866 prev_loc, "Length");
11870 if (switch_cold_ranges)
11872 const char *prev_loc = cold_text_section_label;
11873 const char *loc;
11874 unsigned idx;
11876 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11877 if (prev_loc)
11879 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11880 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11881 prev_loc = NULL;
11883 else
11884 prev_loc = loc;
11886 if (prev_loc)
11888 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11889 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11890 prev_loc, "Length");
11894 if (have_multiple_function_sections)
11896 unsigned fde_idx;
11897 dw_fde_ref fde;
11899 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11901 if (fde->ignored_debug)
11902 continue;
11903 if (!fde->in_std_section)
11905 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11906 "Address");
11907 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11908 fde->dw_fde_begin, "Length");
11910 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11912 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11913 "Address");
11914 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11915 fde->dw_fde_second_begin, "Length");
11920 /* Output the terminator words. */
11921 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11922 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11925 /* Add a new entry to .debug_ranges. Return its index into
11926 ranges_table vector. */
11928 static unsigned int
11929 add_ranges_num (int num, bool maybe_new_sec)
11931 dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11932 vec_safe_push (ranges_table, r);
11933 return vec_safe_length (ranges_table) - 1;
11936 /* Add a new entry to .debug_ranges corresponding to a block, or a
11937 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11938 this entry might be in a different section from previous range. */
11940 static unsigned int
11941 add_ranges (const_tree block, bool maybe_new_sec)
11943 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11946 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11947 chain, or middle entry of a chain that will be directly referred to. */
11949 static void
11950 note_rnglist_head (unsigned int offset)
11952 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11953 return;
11954 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11957 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11958 When using dwarf_split_debug_info, address attributes in dies destined
11959 for the final executable should be direct references--setting the
11960 parameter force_direct ensures this behavior. */
11962 static void
11963 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11964 bool *added, bool force_direct)
11966 unsigned int in_use = vec_safe_length (ranges_by_label);
11967 unsigned int offset;
11968 dw_ranges_by_label rbl = { begin, end };
11969 vec_safe_push (ranges_by_label, rbl);
11970 offset = add_ranges_num (-(int)in_use - 1, true);
11971 if (!*added)
11973 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11974 *added = true;
11975 note_rnglist_head (offset);
11976 if (dwarf_split_debug_info && force_direct)
11977 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11981 /* Emit .debug_ranges section. */
11983 static void
11984 output_ranges (void)
11986 unsigned i;
11987 static const char *const start_fmt = "Offset %#x";
11988 const char *fmt = start_fmt;
11989 dw_ranges *r;
11991 switch_to_section (debug_ranges_section);
11992 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11993 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11995 int block_num = r->num;
11997 if (block_num > 0)
11999 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12000 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12002 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12003 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12005 /* If all code is in the text section, then the compilation
12006 unit base address defaults to DW_AT_low_pc, which is the
12007 base of the text section. */
12008 if (!have_multiple_function_sections)
12010 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
12011 text_section_label,
12012 fmt, i * 2 * DWARF2_ADDR_SIZE);
12013 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
12014 text_section_label, NULL);
12017 /* Otherwise, the compilation unit base address is zero,
12018 which allows us to use absolute addresses, and not worry
12019 about whether the target supports cross-section
12020 arithmetic. */
12021 else
12023 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12024 fmt, i * 2 * DWARF2_ADDR_SIZE);
12025 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12028 fmt = NULL;
12031 /* Negative block_num stands for an index into ranges_by_label. */
12032 else if (block_num < 0)
12034 int lab_idx = - block_num - 1;
12036 if (!have_multiple_function_sections)
12038 gcc_unreachable ();
12039 #if 0
12040 /* If we ever use add_ranges_by_labels () for a single
12041 function section, all we have to do is to take out
12042 the #if 0 above. */
12043 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12044 (*ranges_by_label)[lab_idx].begin,
12045 text_section_label,
12046 fmt, i * 2 * DWARF2_ADDR_SIZE);
12047 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12048 (*ranges_by_label)[lab_idx].end,
12049 text_section_label, NULL);
12050 #endif
12052 else
12054 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12055 (*ranges_by_label)[lab_idx].begin,
12056 fmt, i * 2 * DWARF2_ADDR_SIZE);
12057 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12058 (*ranges_by_label)[lab_idx].end,
12059 NULL);
12062 else
12064 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12065 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12066 fmt = start_fmt;
12071 /* Non-zero if .debug_line_str should be used for .debug_line section
12072 strings or strings that are likely shareable with those. */
12073 #define DWARF5_USE_DEBUG_LINE_STR \
12074 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
12075 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
12076 /* FIXME: there is no .debug_line_str.dwo section, \
12077 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
12078 && !dwarf_split_debug_info)
12081 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
12082 DWARF5 .debug_line tables using .debug_line_str or we generate
12083 it ourselves, except for split-dwarf which doesn't have a
12084 .debug_line_str. */
12085 static bool
12086 asm_outputs_debug_line_str (void)
12088 if (dwarf_version >= 5
12089 && ! output_asm_line_debug_info ()
12090 && DWARF5_USE_DEBUG_LINE_STR)
12091 return true;
12092 else
12094 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12095 return !dwarf_split_debug_info && dwarf_version >= 5;
12096 #else
12097 return false;
12098 #endif
12102 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12103 I is index of the following range. */
12105 static bool
12106 use_distinct_base_address_for_range (unsigned int i)
12108 if (i >= vec_safe_length (ranges_table))
12109 return false;
12111 dw_ranges *r2 = &(*ranges_table)[i];
12112 /* Use DW_RLE_base_address{,x} if there is a next range in the
12113 range list and is guaranteed to be in the same section. */
12114 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12117 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12118 section when needed. */
12120 static void
12121 index_rnglists (void)
12123 unsigned i;
12124 dw_ranges *r;
12125 bool base = false;
12127 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12129 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12130 r->idx = rnglist_idx++;
12132 int block_num = r->num;
12133 if ((HAVE_AS_LEB128 || block_num < 0)
12134 && !have_multiple_function_sections)
12135 continue;
12136 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12137 base = false;
12138 if (block_num > 0)
12140 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12141 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12143 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12144 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12146 if (HAVE_AS_LEB128)
12148 if (!base && use_distinct_base_address_for_range (i + 1))
12150 r->begin_entry = add_addr_table_entry (xstrdup (blabel),
12151 ate_kind_label);
12152 base = true;
12154 if (base)
12155 /* If we have a base, no need for further
12156 begin_entry/end_entry, as DW_RLE_offset_pair will be
12157 used. */
12158 continue;
12159 r->begin_entry
12160 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12161 /* No need for end_entry, DW_RLE_start{,x}_length will use
12162 length as opposed to a pair of addresses. */
12164 else
12166 r->begin_entry
12167 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12168 r->end_entry
12169 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12173 /* Negative block_num stands for an index into ranges_by_label. */
12174 else if (block_num < 0)
12176 int lab_idx = - block_num - 1;
12177 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12178 const char *elabel = (*ranges_by_label)[lab_idx].end;
12180 r->begin_entry
12181 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12182 if (!HAVE_AS_LEB128)
12183 r->end_entry
12184 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12189 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12191 static bool
12192 output_rnglists (unsigned generation, bool dwo)
12194 unsigned i;
12195 dw_ranges *r;
12196 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12197 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12198 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12200 if (dwo)
12201 switch_to_section (debug_ranges_dwo_section);
12202 else
12204 switch_to_section (debug_ranges_section);
12205 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12207 /* There are up to 4 unique ranges labels per generation.
12208 See also init_sections_and_labels. */
12209 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12210 2 + 2 * dwo + generation * 6);
12211 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12212 3 + 2 * dwo + generation * 6);
12213 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12214 dw2_asm_output_data (4, 0xffffffff,
12215 "Initial length escape value indicating "
12216 "64-bit DWARF extension");
12217 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12218 "Length of Range Lists");
12219 ASM_OUTPUT_LABEL (asm_out_file, l1);
12220 output_dwarf_version ();
12221 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12222 dw2_asm_output_data (1, 0, "Segment Size");
12223 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12224 about relocation sizes and primarily care about the size of .debug*
12225 sections in linked shared libraries and executables, then
12226 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12227 into it are usually larger than just DW_FORM_sec_offset offsets
12228 into the .debug_rnglists section. */
12229 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12230 "Offset Entry Count");
12231 if (dwo)
12233 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12234 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12235 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12236 dw2_asm_output_delta (dwarf_offset_size, r->label,
12237 ranges_base_label, NULL);
12240 const char *lab = "";
12241 const char *base = NULL;
12242 bool skipping = false;
12243 bool ret = false;
12244 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12246 int block_num = r->num;
12248 if (r->label)
12250 if (dwarf_split_debug_info
12251 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12253 ret = true;
12254 skipping = true;
12255 continue;
12257 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12258 lab = r->label;
12260 if (skipping)
12262 if (block_num == 0)
12263 skipping = false;
12264 continue;
12266 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12267 base = NULL;
12268 if (block_num > 0)
12270 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12271 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12273 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12274 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12276 if (HAVE_AS_LEB128)
12278 /* If all code is in the text section, then the compilation
12279 unit base address defaults to DW_AT_low_pc, which is the
12280 base of the text section. */
12281 if (!have_multiple_function_sections)
12283 dw2_asm_output_data (1, DW_RLE_offset_pair,
12284 "DW_RLE_offset_pair (%s)", lab);
12285 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12286 "Range begin address (%s)", lab);
12287 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12288 "Range end address (%s)", lab);
12289 continue;
12291 if (base == NULL && use_distinct_base_address_for_range (i + 1))
12293 if (dwarf_split_debug_info)
12295 dw2_asm_output_data (1, DW_RLE_base_addressx,
12296 "DW_RLE_base_addressx (%s)", lab);
12297 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12298 "Base address index (%s)",
12299 blabel);
12301 else
12303 dw2_asm_output_data (1, DW_RLE_base_address,
12304 "DW_RLE_base_address (%s)", lab);
12305 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12306 "Base address (%s)", lab);
12308 strcpy (basebuf, blabel);
12309 base = basebuf;
12311 if (base)
12313 dw2_asm_output_data (1, DW_RLE_offset_pair,
12314 "DW_RLE_offset_pair (%s)", lab);
12315 dw2_asm_output_delta_uleb128 (blabel, base,
12316 "Range begin address (%s)", lab);
12317 dw2_asm_output_delta_uleb128 (elabel, base,
12318 "Range end address (%s)", lab);
12319 continue;
12321 if (dwarf_split_debug_info)
12323 dw2_asm_output_data (1, DW_RLE_startx_length,
12324 "DW_RLE_startx_length (%s)", lab);
12325 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12326 "Range begin address index "
12327 "(%s)", blabel);
12329 else
12331 dw2_asm_output_data (1, DW_RLE_start_length,
12332 "DW_RLE_start_length (%s)", lab);
12333 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12334 "Range begin address (%s)", lab);
12336 dw2_asm_output_delta_uleb128 (elabel, blabel,
12337 "Range length (%s)", lab);
12339 else if (dwarf_split_debug_info)
12341 dw2_asm_output_data (1, DW_RLE_startx_endx,
12342 "DW_RLE_startx_endx (%s)", lab);
12343 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12344 "Range begin address index "
12345 "(%s)", blabel);
12346 dw2_asm_output_data_uleb128 (r->end_entry->index,
12347 "Range end address index "
12348 "(%s)", elabel);
12350 else
12352 dw2_asm_output_data (1, DW_RLE_start_end,
12353 "DW_RLE_start_end (%s)", lab);
12354 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12355 "Range begin address (%s)", lab);
12356 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12357 "Range end address (%s)", lab);
12361 /* Negative block_num stands for an index into ranges_by_label. */
12362 else if (block_num < 0)
12364 int lab_idx = - block_num - 1;
12365 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12366 const char *elabel = (*ranges_by_label)[lab_idx].end;
12368 if (!have_multiple_function_sections)
12369 gcc_unreachable ();
12370 if (HAVE_AS_LEB128)
12372 if (dwarf_split_debug_info)
12374 dw2_asm_output_data (1, DW_RLE_startx_length,
12375 "DW_RLE_startx_length (%s)", lab);
12376 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12377 "Range begin address index "
12378 "(%s)", blabel);
12380 else
12382 dw2_asm_output_data (1, DW_RLE_start_length,
12383 "DW_RLE_start_length (%s)", lab);
12384 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12385 "Range begin address (%s)", lab);
12387 dw2_asm_output_delta_uleb128 (elabel, blabel,
12388 "Range length (%s)", lab);
12390 else if (dwarf_split_debug_info)
12392 dw2_asm_output_data (1, DW_RLE_startx_endx,
12393 "DW_RLE_startx_endx (%s)", lab);
12394 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12395 "Range begin address index "
12396 "(%s)", blabel);
12397 dw2_asm_output_data_uleb128 (r->end_entry->index,
12398 "Range end address index "
12399 "(%s)", elabel);
12401 else
12403 dw2_asm_output_data (1, DW_RLE_start_end,
12404 "DW_RLE_start_end (%s)", lab);
12405 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12406 "Range begin address (%s)", lab);
12407 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12408 "Range end address (%s)", lab);
12411 else
12412 dw2_asm_output_data (1, DW_RLE_end_of_list,
12413 "DW_RLE_end_of_list (%s)", lab);
12415 ASM_OUTPUT_LABEL (asm_out_file, l2);
12416 return ret;
12419 /* Data structure containing information about input files. */
12420 struct file_info
12422 const char *path; /* Complete file name. */
12423 const char *fname; /* File name part. */
12424 int length; /* Length of entire string. */
12425 struct dwarf_file_data * file_idx; /* Index in input file table. */
12426 int dir_idx; /* Index in directory table. */
12429 /* Data structure containing information about directories with source
12430 files. */
12431 struct dir_info
12433 const char *path; /* Path including directory name. */
12434 int length; /* Path length. */
12435 int prefix; /* Index of directory entry which is a prefix. */
12436 int count; /* Number of files in this directory. */
12437 int dir_idx; /* Index of directory used as base. */
12440 /* Callback function for file_info comparison. We sort by looking at
12441 the directories in the path. */
12443 static int
12444 file_info_cmp (const void *p1, const void *p2)
12446 const struct file_info *const s1 = (const struct file_info *) p1;
12447 const struct file_info *const s2 = (const struct file_info *) p2;
12448 const unsigned char *cp1;
12449 const unsigned char *cp2;
12451 /* Take care of file names without directories. We need to make sure that
12452 we return consistent values to qsort since some will get confused if
12453 we return the same value when identical operands are passed in opposite
12454 orders. So if neither has a directory, return 0 and otherwise return
12455 1 or -1 depending on which one has the directory. We want the one with
12456 the directory to sort after the one without, so all no directory files
12457 are at the start (normally only the compilation unit file). */
12458 if ((s1->path == s1->fname || s2->path == s2->fname))
12459 return (s2->path == s2->fname) - (s1->path == s1->fname);
12461 cp1 = (const unsigned char *) s1->path;
12462 cp2 = (const unsigned char *) s2->path;
12464 while (1)
12466 ++cp1;
12467 ++cp2;
12468 /* Reached the end of the first path? If so, handle like above,
12469 but now we want longer directory prefixes before shorter ones. */
12470 if ((cp1 == (const unsigned char *) s1->fname)
12471 || (cp2 == (const unsigned char *) s2->fname))
12472 return ((cp1 == (const unsigned char *) s1->fname)
12473 - (cp2 == (const unsigned char *) s2->fname));
12475 /* Character of current path component the same? */
12476 else if (*cp1 != *cp2)
12477 return *cp1 - *cp2;
12481 struct file_name_acquire_data
12483 struct file_info *files;
12484 int used_files;
12485 int max_files;
12488 /* Traversal function for the hash table. */
12491 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12493 struct dwarf_file_data *d = *slot;
12494 struct file_info *fi;
12495 const char *f;
12497 gcc_assert (fnad->max_files >= d->emitted_number);
12499 if (! d->emitted_number)
12500 return 1;
12502 gcc_assert (fnad->max_files != fnad->used_files);
12504 fi = fnad->files + fnad->used_files++;
12506 f = d->filename;
12508 /* Skip all leading "./". */
12509 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12510 f += 2;
12512 /* Create a new array entry. */
12513 fi->path = f;
12514 fi->length = strlen (f);
12515 fi->file_idx = d;
12517 /* Search for the file name part. */
12518 f = strrchr (f, DIR_SEPARATOR);
12519 #if defined (DIR_SEPARATOR_2)
12521 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12523 if (g != NULL)
12525 if (f == NULL || f < g)
12526 f = g;
12529 #endif
12531 fi->fname = f == NULL ? fi->path : f + 1;
12532 return 1;
12535 /* Helper function for output_file_names. Emit a FORM encoded
12536 string STR, with assembly comment start ENTRY_KIND and
12537 index IDX */
12539 static void
12540 output_line_string (enum dwarf_form form, const char *str,
12541 const char *entry_kind, unsigned int idx)
12543 switch (form)
12545 case DW_FORM_string:
12546 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12547 break;
12548 case DW_FORM_line_strp:
12549 if (!debug_line_str_hash)
12550 debug_line_str_hash
12551 = hash_table<indirect_string_hasher>::create_ggc (10);
12553 struct indirect_string_node *node;
12554 node = find_AT_string_in_table (str, debug_line_str_hash);
12555 set_indirect_string (node);
12556 node->form = form;
12557 dw2_asm_output_offset (dwarf_offset_size, node->label,
12558 debug_line_str_section, "%s: %#x: \"%s\"",
12559 entry_kind, 0, node->str);
12560 break;
12561 default:
12562 gcc_unreachable ();
12566 /* Output the directory table and the file name table. We try to minimize
12567 the total amount of memory needed. A heuristic is used to avoid large
12568 slowdowns with many input files. */
12570 static void
12571 output_file_names (void)
12573 struct file_name_acquire_data fnad;
12574 int numfiles;
12575 struct file_info *files;
12576 struct dir_info *dirs;
12577 int *saved;
12578 int *savehere;
12579 int *backmap;
12580 int ndirs;
12581 int idx_offset;
12582 int i;
12584 if (!last_emitted_file)
12586 if (dwarf_version >= 5)
12588 const char *comp_dir = comp_dir_string ();
12589 if (comp_dir == NULL)
12590 comp_dir = "";
12591 dw2_asm_output_data (1, 1, "Directory entry format count");
12592 enum dwarf_form str_form = DW_FORM_string;
12593 if (DWARF5_USE_DEBUG_LINE_STR)
12594 str_form = DW_FORM_line_strp;
12595 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12596 dw2_asm_output_data_uleb128 (str_form, "%s",
12597 get_DW_FORM_name (str_form));
12598 dw2_asm_output_data_uleb128 (1, "Directories count");
12599 if (str_form == DW_FORM_string)
12600 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12601 else
12602 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12603 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12604 if (filename0 == NULL)
12605 filename0 = "";
12606 #ifdef VMS_DEBUGGING_INFO
12607 dw2_asm_output_data (1, 4, "File name entry format count");
12608 #else
12609 dw2_asm_output_data (1, 2, "File name entry format count");
12610 #endif
12611 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12612 dw2_asm_output_data_uleb128 (str_form, "%s",
12613 get_DW_FORM_name (str_form));
12614 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12615 "DW_LNCT_directory_index");
12616 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12617 get_DW_FORM_name (DW_FORM_data1));
12618 #ifdef VMS_DEBUGGING_INFO
12619 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12620 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12621 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12622 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12623 #endif
12624 dw2_asm_output_data_uleb128 (1, "File names count");
12626 output_line_string (str_form, filename0, "File Entry", 0);
12627 dw2_asm_output_data (1, 0, NULL);
12628 #ifdef VMS_DEBUGGING_INFO
12629 dw2_asm_output_data_uleb128 (0, NULL);
12630 dw2_asm_output_data_uleb128 (0, NULL);
12631 #endif
12633 else
12635 dw2_asm_output_data (1, 0, "End directory table");
12636 dw2_asm_output_data (1, 0, "End file name table");
12638 return;
12641 numfiles = last_emitted_file->emitted_number;
12643 /* Allocate the various arrays we need. */
12644 files = XALLOCAVEC (struct file_info, numfiles);
12645 dirs = XALLOCAVEC (struct dir_info, numfiles);
12647 fnad.files = files;
12648 fnad.used_files = 0;
12649 fnad.max_files = numfiles;
12650 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12651 gcc_assert (fnad.used_files == fnad.max_files);
12653 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12655 /* Find all the different directories used. */
12656 dirs[0].path = files[0].path;
12657 dirs[0].length = files[0].fname - files[0].path;
12658 dirs[0].prefix = -1;
12659 dirs[0].count = 1;
12660 dirs[0].dir_idx = 0;
12661 files[0].dir_idx = 0;
12662 ndirs = 1;
12664 for (i = 1; i < numfiles; i++)
12665 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12666 && memcmp (dirs[ndirs - 1].path, files[i].path,
12667 dirs[ndirs - 1].length) == 0)
12669 /* Same directory as last entry. */
12670 files[i].dir_idx = ndirs - 1;
12671 ++dirs[ndirs - 1].count;
12673 else
12675 int j;
12677 /* This is a new directory. */
12678 dirs[ndirs].path = files[i].path;
12679 dirs[ndirs].length = files[i].fname - files[i].path;
12680 dirs[ndirs].count = 1;
12681 dirs[ndirs].dir_idx = ndirs;
12682 files[i].dir_idx = ndirs;
12684 /* Search for a prefix. */
12685 dirs[ndirs].prefix = -1;
12686 for (j = 0; j < ndirs; j++)
12687 if (dirs[j].length < dirs[ndirs].length
12688 && dirs[j].length > 1
12689 && (dirs[ndirs].prefix == -1
12690 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12691 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12692 dirs[ndirs].prefix = j;
12694 ++ndirs;
12697 /* Now to the actual work. We have to find a subset of the directories which
12698 allow expressing the file name using references to the directory table
12699 with the least amount of characters. We do not do an exhaustive search
12700 where we would have to check out every combination of every single
12701 possible prefix. Instead we use a heuristic which provides nearly optimal
12702 results in most cases and never is much off. */
12703 saved = XALLOCAVEC (int, ndirs);
12704 savehere = XALLOCAVEC (int, ndirs);
12706 memset (saved, '\0', ndirs * sizeof (saved[0]));
12707 for (i = 0; i < ndirs; i++)
12709 int j;
12710 int total;
12712 /* We can always save some space for the current directory. But this
12713 does not mean it will be enough to justify adding the directory. */
12714 savehere[i] = dirs[i].length;
12715 total = (savehere[i] - saved[i]) * dirs[i].count;
12717 for (j = i + 1; j < ndirs; j++)
12719 savehere[j] = 0;
12720 if (saved[j] < dirs[i].length)
12722 /* Determine whether the dirs[i] path is a prefix of the
12723 dirs[j] path. */
12724 int k;
12726 k = dirs[j].prefix;
12727 while (k != -1 && k != (int) i)
12728 k = dirs[k].prefix;
12730 if (k == (int) i)
12732 /* Yes it is. We can possibly save some memory by
12733 writing the filenames in dirs[j] relative to
12734 dirs[i]. */
12735 savehere[j] = dirs[i].length;
12736 total += (savehere[j] - saved[j]) * dirs[j].count;
12741 /* Check whether we can save enough to justify adding the dirs[i]
12742 directory. */
12743 if (total > dirs[i].length + 1)
12745 /* It's worthwhile adding. */
12746 for (j = i; j < ndirs; j++)
12747 if (savehere[j] > 0)
12749 /* Remember how much we saved for this directory so far. */
12750 saved[j] = savehere[j];
12752 /* Remember the prefix directory. */
12753 dirs[j].dir_idx = i;
12758 /* Emit the directory name table. */
12759 idx_offset = dirs[0].length > 0 ? 1 : 0;
12760 enum dwarf_form str_form = DW_FORM_string;
12761 enum dwarf_form idx_form = DW_FORM_udata;
12762 if (dwarf_version >= 5)
12764 const char *comp_dir = comp_dir_string ();
12765 if (comp_dir == NULL)
12766 comp_dir = "";
12767 dw2_asm_output_data (1, 1, "Directory entry format count");
12768 if (DWARF5_USE_DEBUG_LINE_STR)
12769 str_form = DW_FORM_line_strp;
12770 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12771 dw2_asm_output_data_uleb128 (str_form, "%s",
12772 get_DW_FORM_name (str_form));
12773 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12774 if (str_form == DW_FORM_string)
12776 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12777 for (i = 1 - idx_offset; i < ndirs; i++)
12778 dw2_asm_output_nstring (dirs[i].path,
12779 dirs[i].length
12780 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12781 "Directory Entry: %#x", i + idx_offset);
12783 else
12785 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12786 for (i = 1 - idx_offset; i < ndirs; i++)
12788 const char *str
12789 = ggc_alloc_string (dirs[i].path,
12790 dirs[i].length
12791 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12792 output_line_string (str_form, str, "Directory Entry",
12793 (unsigned) i + idx_offset);
12797 else
12799 for (i = 1 - idx_offset; i < ndirs; i++)
12800 dw2_asm_output_nstring (dirs[i].path,
12801 dirs[i].length
12802 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12803 "Directory Entry: %#x", i + idx_offset);
12805 dw2_asm_output_data (1, 0, "End directory table");
12808 /* We have to emit them in the order of emitted_number since that's
12809 used in the debug info generation. To do this efficiently we
12810 generate a back-mapping of the indices first. */
12811 backmap = XALLOCAVEC (int, numfiles);
12812 for (i = 0; i < numfiles; i++)
12813 backmap[files[i].file_idx->emitted_number - 1] = i;
12815 if (dwarf_version >= 5)
12817 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12818 if (filename0 == NULL)
12819 filename0 = "";
12820 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12821 DW_FORM_data2. Choose one based on the number of directories
12822 and how much space would they occupy in each encoding.
12823 If we have at most 256 directories, all indexes fit into
12824 a single byte, so DW_FORM_data1 is most compact (if there
12825 are at most 128 directories, DW_FORM_udata would be as
12826 compact as that, but not shorter and slower to decode). */
12827 if (ndirs + idx_offset <= 256)
12828 idx_form = DW_FORM_data1;
12829 /* If there are more than 65536 directories, we have to use
12830 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12831 Otherwise, compute what space would occupy if all the indexes
12832 used DW_FORM_udata - sum - and compare that to how large would
12833 be DW_FORM_data2 encoding, and pick the more efficient one. */
12834 else if (ndirs + idx_offset <= 65536)
12836 unsigned HOST_WIDE_INT sum = 1;
12837 for (i = 0; i < numfiles; i++)
12839 int file_idx = backmap[i];
12840 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12841 sum += size_of_uleb128 (dir_idx);
12843 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12844 idx_form = DW_FORM_data2;
12846 #ifdef VMS_DEBUGGING_INFO
12847 dw2_asm_output_data (1, 4, "File name entry format count");
12848 #else
12849 dw2_asm_output_data (1, 2, "File name entry format count");
12850 #endif
12851 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12852 dw2_asm_output_data_uleb128 (str_form, "%s",
12853 get_DW_FORM_name (str_form));
12854 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12855 "DW_LNCT_directory_index");
12856 dw2_asm_output_data_uleb128 (idx_form, "%s",
12857 get_DW_FORM_name (idx_form));
12858 #ifdef VMS_DEBUGGING_INFO
12859 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12860 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12861 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12862 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12863 #endif
12864 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12866 output_line_string (str_form, filename0, "File Entry", 0);
12868 /* Include directory index. */
12869 if (idx_form != DW_FORM_udata)
12870 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12871 0, NULL);
12872 else
12873 dw2_asm_output_data_uleb128 (0, NULL);
12875 #ifdef VMS_DEBUGGING_INFO
12876 dw2_asm_output_data_uleb128 (0, NULL);
12877 dw2_asm_output_data_uleb128 (0, NULL);
12878 #endif
12881 /* Now write all the file names. */
12882 for (i = 0; i < numfiles; i++)
12884 int file_idx = backmap[i];
12885 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12887 #ifdef VMS_DEBUGGING_INFO
12888 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12890 /* Setting these fields can lead to debugger miscomparisons,
12891 but VMS Debug requires them to be set correctly. */
12893 int ver;
12894 long long cdt;
12895 long siz;
12896 int maxfilelen = (strlen (files[file_idx].path)
12897 + dirs[dir_idx].length
12898 + MAX_VMS_VERSION_LEN + 1);
12899 char *filebuf = XALLOCAVEC (char, maxfilelen);
12901 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12902 snprintf (filebuf, maxfilelen, "%s;%d",
12903 files[file_idx].path + dirs[dir_idx].length, ver);
12905 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12907 /* Include directory index. */
12908 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12909 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12910 dir_idx + idx_offset, NULL);
12911 else
12912 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12914 /* Modification time. */
12915 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12916 &cdt, 0, 0, 0) == 0)
12917 ? cdt : 0, NULL);
12919 /* File length in bytes. */
12920 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12921 0, &siz, 0, 0) == 0)
12922 ? siz : 0, NULL);
12923 #else
12924 output_line_string (str_form,
12925 files[file_idx].path + dirs[dir_idx].length,
12926 "File Entry", (unsigned) i + 1);
12928 /* Include directory index. */
12929 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12930 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12931 dir_idx + idx_offset, NULL);
12932 else
12933 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12935 if (dwarf_version >= 5)
12936 continue;
12938 /* Modification time. */
12939 dw2_asm_output_data_uleb128 (0, NULL);
12941 /* File length in bytes. */
12942 dw2_asm_output_data_uleb128 (0, NULL);
12943 #endif /* VMS_DEBUGGING_INFO */
12946 if (dwarf_version < 5)
12947 dw2_asm_output_data (1, 0, "End file name table");
12951 /* Output one line number table into the .debug_line section. */
12953 static void
12954 output_one_line_info_table (dw_line_info_table *table)
12956 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12957 unsigned int current_line = 1;
12958 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12959 dw_line_info_entry *ent, *prev_addr = NULL;
12960 size_t i;
12961 unsigned int view;
12963 view = 0;
12965 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12967 switch (ent->opcode)
12969 case LI_set_address:
12970 /* ??? Unfortunately, we have little choice here currently, and
12971 must always use the most general form. GCC does not know the
12972 address delta itself, so we can't use DW_LNS_advance_pc. Many
12973 ports do have length attributes which will give an upper bound
12974 on the address range. We could perhaps use length attributes
12975 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12976 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12978 view = 0;
12980 /* This can handle any delta. This takes
12981 4+DWARF2_ADDR_SIZE bytes. */
12982 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12983 debug_variable_location_views
12984 ? ", reset view to 0" : "");
12985 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12986 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12987 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12989 prev_addr = ent;
12990 break;
12992 case LI_adv_address:
12994 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12995 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12996 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12998 view++;
13000 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
13001 dw2_asm_output_delta (2, line_label, prev_label,
13002 "from %s to %s", prev_label, line_label);
13004 prev_addr = ent;
13005 break;
13008 case LI_set_line:
13009 if (ent->val == current_line)
13011 /* We still need to start a new row, so output a copy insn. */
13012 dw2_asm_output_data (1, DW_LNS_copy,
13013 "copy line %u", current_line);
13015 else
13017 int line_offset = ent->val - current_line;
13018 int line_delta = line_offset - DWARF_LINE_BASE;
13020 current_line = ent->val;
13021 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
13023 /* This can handle deltas from -10 to 234, using the current
13024 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
13025 This takes 1 byte. */
13026 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
13027 "line %u", current_line);
13029 else
13031 /* This can handle any delta. This takes at least 4 bytes,
13032 depending on the value being encoded. */
13033 dw2_asm_output_data (1, DW_LNS_advance_line,
13034 "advance to line %u", current_line);
13035 dw2_asm_output_data_sleb128 (line_offset, NULL);
13036 dw2_asm_output_data (1, DW_LNS_copy, NULL);
13039 break;
13041 case LI_set_file:
13042 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
13043 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13044 break;
13046 case LI_set_column:
13047 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
13048 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13049 break;
13051 case LI_negate_stmt:
13052 current_is_stmt = !current_is_stmt;
13053 dw2_asm_output_data (1, DW_LNS_negate_stmt,
13054 "is_stmt %d", current_is_stmt);
13055 break;
13057 case LI_set_prologue_end:
13058 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
13059 "set prologue end");
13060 break;
13062 case LI_set_epilogue_begin:
13063 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
13064 "set epilogue begin");
13065 break;
13067 case LI_set_discriminator:
13068 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
13069 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
13070 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
13071 dw2_asm_output_data_uleb128 (ent->val, NULL);
13072 break;
13076 /* Emit debug info for the address of the end of the table. */
13077 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
13078 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
13079 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13080 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13082 dw2_asm_output_data (1, 0, "end sequence");
13083 dw2_asm_output_data_uleb128 (1, NULL);
13084 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13087 static unsigned int output_line_info_generation;
13089 /* Output the source line number correspondence information. This
13090 information goes into the .debug_line section. */
13092 static void
13093 output_line_info (bool prologue_only)
13095 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13096 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13097 bool saw_one = false;
13098 int opc;
13100 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13101 output_line_info_generation);
13102 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13103 output_line_info_generation);
13104 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13105 output_line_info_generation);
13106 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13107 output_line_info_generation++);
13109 if (!XCOFF_DEBUGGING_INFO)
13111 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13112 dw2_asm_output_data (4, 0xffffffff,
13113 "Initial length escape value indicating 64-bit DWARF extension");
13114 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13115 "Length of Source Line Info");
13118 ASM_OUTPUT_LABEL (asm_out_file, l1);
13120 output_dwarf_version ();
13121 if (dwarf_version >= 5)
13123 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13124 dw2_asm_output_data (1, 0, "Segment Size");
13126 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13127 ASM_OUTPUT_LABEL (asm_out_file, p1);
13129 /* Define the architecture-dependent minimum instruction length (in bytes).
13130 In this implementation of DWARF, this field is used for information
13131 purposes only. Since GCC generates assembly language, we have no
13132 a priori knowledge of how many instruction bytes are generated for each
13133 source line, and therefore can use only the DW_LNE_set_address and
13134 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13135 this as '1', which is "correct enough" for all architectures,
13136 and don't let the target override. */
13137 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13139 if (dwarf_version >= 4)
13140 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13141 "Maximum Operations Per Instruction");
13142 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13143 "Default is_stmt_start flag");
13144 dw2_asm_output_data (1, DWARF_LINE_BASE,
13145 "Line Base Value (Special Opcodes)");
13146 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13147 "Line Range Value (Special Opcodes)");
13148 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13149 "Special Opcode Base");
13151 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13153 int n_op_args;
13154 switch (opc)
13156 case DW_LNS_advance_pc:
13157 case DW_LNS_advance_line:
13158 case DW_LNS_set_file:
13159 case DW_LNS_set_column:
13160 case DW_LNS_fixed_advance_pc:
13161 case DW_LNS_set_isa:
13162 n_op_args = 1;
13163 break;
13164 default:
13165 n_op_args = 0;
13166 break;
13169 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13170 opc, n_op_args);
13173 /* Write out the information about the files we use. */
13174 output_file_names ();
13175 ASM_OUTPUT_LABEL (asm_out_file, p2);
13176 if (prologue_only)
13178 /* Output the marker for the end of the line number info. */
13179 ASM_OUTPUT_LABEL (asm_out_file, l2);
13180 return;
13183 if (separate_line_info)
13185 dw_line_info_table *table;
13186 size_t i;
13188 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13189 if (table->in_use)
13191 output_one_line_info_table (table);
13192 saw_one = true;
13195 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13197 output_one_line_info_table (cold_text_section_line_info);
13198 saw_one = true;
13201 /* ??? Some Darwin linkers crash on a .debug_line section with no
13202 sequences. Further, merely a DW_LNE_end_sequence entry is not
13203 sufficient -- the address column must also be initialized.
13204 Make sure to output at least one set_address/end_sequence pair,
13205 choosing .text since that section is always present. */
13206 if (text_section_line_info->in_use || !saw_one)
13207 output_one_line_info_table (text_section_line_info);
13209 /* Output the marker for the end of the line number info. */
13210 ASM_OUTPUT_LABEL (asm_out_file, l2);
13213 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13215 static inline bool
13216 need_endianity_attribute_p (bool reverse)
13218 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13221 /* Given a pointer to a tree node for some base type, return a pointer to
13222 a DIE that describes the given type. REVERSE is true if the type is
13223 to be interpreted in the reverse storage order wrt the target order.
13225 This routine must only be called for GCC type nodes that correspond to
13226 Dwarf base (fundamental) types. */
13228 dw_die_ref
13229 base_type_die (tree type, bool reverse)
13231 dw_die_ref base_type_result;
13232 enum dwarf_type encoding;
13233 bool fpt_used = false;
13234 struct fixed_point_type_info fpt_info;
13235 tree type_bias = NULL_TREE;
13237 /* If this is a subtype that should not be emitted as a subrange type,
13238 use the base type. See subrange_type_for_debug_p. */
13239 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13240 type = TREE_TYPE (type);
13242 switch (TREE_CODE (type))
13244 case INTEGER_TYPE:
13245 if ((dwarf_version >= 4 || !dwarf_strict)
13246 && TYPE_NAME (type)
13247 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13248 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13249 && DECL_NAME (TYPE_NAME (type)))
13251 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13252 if (strcmp (name, "char16_t") == 0
13253 || strcmp (name, "char8_t") == 0
13254 || strcmp (name, "char32_t") == 0)
13256 encoding = DW_ATE_UTF;
13257 break;
13260 if ((dwarf_version >= 3 || !dwarf_strict)
13261 && lang_hooks.types.get_fixed_point_type_info)
13263 memset (&fpt_info, 0, sizeof (fpt_info));
13264 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13266 fpt_used = true;
13267 encoding = ((TYPE_UNSIGNED (type))
13268 ? DW_ATE_unsigned_fixed
13269 : DW_ATE_signed_fixed);
13270 break;
13273 if (TYPE_STRING_FLAG (type))
13275 if ((dwarf_version >= 4 || !dwarf_strict)
13276 && is_rust ()
13277 && int_size_in_bytes (type) == 4)
13278 encoding = DW_ATE_UTF;
13279 else if (TYPE_UNSIGNED (type))
13280 encoding = DW_ATE_unsigned_char;
13281 else
13282 encoding = DW_ATE_signed_char;
13284 else if (TYPE_UNSIGNED (type))
13285 encoding = DW_ATE_unsigned;
13286 else
13287 encoding = DW_ATE_signed;
13289 if (!dwarf_strict
13290 && lang_hooks.types.get_type_bias)
13291 type_bias = lang_hooks.types.get_type_bias (type);
13292 break;
13294 case REAL_TYPE:
13295 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13297 if (dwarf_version >= 3 || !dwarf_strict)
13298 encoding = DW_ATE_decimal_float;
13299 else
13300 encoding = DW_ATE_lo_user;
13302 else
13303 encoding = DW_ATE_float;
13304 break;
13306 case FIXED_POINT_TYPE:
13307 if (!(dwarf_version >= 3 || !dwarf_strict))
13308 encoding = DW_ATE_lo_user;
13309 else if (TYPE_UNSIGNED (type))
13310 encoding = DW_ATE_unsigned_fixed;
13311 else
13312 encoding = DW_ATE_signed_fixed;
13313 break;
13315 /* Dwarf2 doesn't know anything about complex ints, so use
13316 a user defined type for it. */
13317 case COMPLEX_TYPE:
13318 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (type)))
13319 encoding = DW_ATE_complex_float;
13320 else
13321 encoding = DW_ATE_lo_user;
13322 break;
13324 case BOOLEAN_TYPE:
13325 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13326 encoding = DW_ATE_boolean;
13327 break;
13329 case BITINT_TYPE:
13330 /* C23 _BitInt(N). */
13331 if (TYPE_UNSIGNED (type))
13332 encoding = DW_ATE_unsigned;
13333 else
13334 encoding = DW_ATE_signed;
13335 break;
13337 default:
13338 /* No other TREE_CODEs are Dwarf fundamental types. */
13339 gcc_unreachable ();
13342 base_type_result = new_die_raw (DW_TAG_base_type);
13344 add_AT_unsigned (base_type_result, DW_AT_byte_size,
13345 int_size_in_bytes (type));
13346 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13347 if (TREE_CODE (type) == BITINT_TYPE)
13348 add_AT_unsigned (base_type_result, DW_AT_bit_size, TYPE_PRECISION (type));
13350 if (need_endianity_attribute_p (reverse))
13351 add_AT_unsigned (base_type_result, DW_AT_endianity,
13352 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13354 add_alignment_attribute (base_type_result, type);
13356 if (fpt_used)
13358 switch (fpt_info.scale_factor_kind)
13360 case fixed_point_scale_factor_binary:
13361 add_AT_int (base_type_result, DW_AT_binary_scale,
13362 fpt_info.scale_factor.binary);
13363 break;
13365 case fixed_point_scale_factor_decimal:
13366 add_AT_int (base_type_result, DW_AT_decimal_scale,
13367 fpt_info.scale_factor.decimal);
13368 break;
13370 case fixed_point_scale_factor_arbitrary:
13371 /* Arbitrary scale factors cannot be described in standard DWARF. */
13372 if (!dwarf_strict)
13374 /* Describe the scale factor as a rational constant. */
13375 const dw_die_ref scale_factor
13376 = new_die (DW_TAG_constant, comp_unit_die (), type);
13378 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13379 fpt_info.scale_factor.arbitrary.numerator,
13380 dw_scalar_form_constant, NULL);
13381 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13382 fpt_info.scale_factor.arbitrary.denominator,
13383 dw_scalar_form_constant, NULL);
13385 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13387 break;
13389 default:
13390 gcc_unreachable ();
13394 if (type_bias)
13395 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13396 dw_scalar_form_constant
13397 | dw_scalar_form_exprloc
13398 | dw_scalar_form_reference,
13399 NULL);
13401 return base_type_result;
13404 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13405 named 'auto' in its type: return true for it, false otherwise. */
13407 static inline bool
13408 is_cxx_auto (tree type)
13410 if (is_cxx ())
13412 tree name = TYPE_IDENTIFIER (type);
13413 if (name == get_identifier ("auto")
13414 || name == get_identifier ("decltype(auto)"))
13415 return true;
13417 return false;
13420 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if the
13421 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13423 static inline bool
13424 is_base_type (tree type)
13426 switch (TREE_CODE (type))
13428 case INTEGER_TYPE:
13429 case REAL_TYPE:
13430 case FIXED_POINT_TYPE:
13431 case COMPLEX_TYPE:
13432 case BOOLEAN_TYPE:
13433 case BITINT_TYPE:
13434 return true;
13436 case VOID_TYPE:
13437 case OPAQUE_TYPE:
13438 case ARRAY_TYPE:
13439 case RECORD_TYPE:
13440 case UNION_TYPE:
13441 case QUAL_UNION_TYPE:
13442 case ENUMERAL_TYPE:
13443 case FUNCTION_TYPE:
13444 case METHOD_TYPE:
13445 case POINTER_TYPE:
13446 case REFERENCE_TYPE:
13447 case NULLPTR_TYPE:
13448 case OFFSET_TYPE:
13449 case LANG_TYPE:
13450 case VECTOR_TYPE:
13451 return false;
13453 default:
13454 if (is_cxx_auto (type))
13455 return false;
13456 gcc_unreachable ();
13460 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13461 node, return the size in bits for the type if it is a constant, or else
13462 return the alignment for the type if the type's size is not constant, or
13463 else return BITS_PER_WORD if the type actually turns out to be an
13464 ERROR_MARK node. */
13466 static inline unsigned HOST_WIDE_INT
13467 simple_type_size_in_bits (const_tree type)
13469 if (TREE_CODE (type) == ERROR_MARK)
13470 return BITS_PER_WORD;
13471 else if (TYPE_SIZE (type) == NULL_TREE)
13472 return 0;
13473 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13474 return tree_to_uhwi (TYPE_SIZE (type));
13475 else
13476 return TYPE_ALIGN (type);
13479 /* Similarly, but return an offset_int instead of UHWI. */
13481 static inline offset_int
13482 offset_int_type_size_in_bits (const_tree type)
13484 if (TREE_CODE (type) == ERROR_MARK)
13485 return BITS_PER_WORD;
13486 else if (TYPE_SIZE (type) == NULL_TREE)
13487 return 0;
13488 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13489 return wi::to_offset (TYPE_SIZE (type));
13490 else
13491 return TYPE_ALIGN (type);
13494 /* Given a pointer to a tree node for a subrange type, return a pointer
13495 to a DIE that describes the given type. */
13497 static dw_die_ref
13498 subrange_type_die (tree type, tree low, tree high, tree bias,
13499 dw_die_ref context_die)
13501 dw_die_ref subrange_die;
13502 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13504 if (context_die == NULL)
13505 context_die = comp_unit_die ();
13507 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13509 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13511 /* The size of the subrange type and its base type do not match,
13512 so we need to generate a size attribute for the subrange type. */
13513 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13516 add_alignment_attribute (subrange_die, type);
13518 if (low)
13519 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13520 if (high)
13521 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13522 if (bias && !dwarf_strict)
13523 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13524 dw_scalar_form_constant
13525 | dw_scalar_form_exprloc
13526 | dw_scalar_form_reference,
13527 NULL);
13529 return subrange_die;
13532 /* Returns the (const and/or volatile) cv_qualifiers associated with
13533 the decl node. This will normally be augmented with the
13534 cv_qualifiers of the underlying type in add_type_attribute. */
13536 static int
13537 decl_quals (const_tree decl)
13539 return ((TREE_READONLY (decl)
13540 /* The C++ front-end correctly marks reference-typed
13541 variables as readonly, but from a language (and debug
13542 info) standpoint they are not const-qualified. */
13543 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13544 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13545 | (TREE_THIS_VOLATILE (decl)
13546 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13549 /* Determine the TYPE whose qualifiers match the largest strict subset
13550 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13551 qualifiers outside QUAL_MASK. */
13553 static int
13554 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13556 tree t;
13557 int best_rank = 0, best_qual = 0, max_rank;
13559 type_quals &= qual_mask;
13560 max_rank = popcount_hwi (type_quals) - 1;
13562 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13563 t = TYPE_NEXT_VARIANT (t))
13565 int q = TYPE_QUALS (t) & qual_mask;
13567 if ((q & type_quals) == q && q != type_quals
13568 && check_base_type (t, type))
13570 int rank = popcount_hwi (q);
13572 if (rank > best_rank)
13574 best_rank = rank;
13575 best_qual = q;
13580 return best_qual;
13583 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13584 static const dwarf_qual_info_t dwarf_qual_info[] =
13586 { TYPE_QUAL_CONST, DW_TAG_const_type },
13587 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13588 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13589 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13591 static const unsigned int dwarf_qual_info_size = ARRAY_SIZE (dwarf_qual_info);
13593 /* If DIE is a qualified DIE of some base DIE with the same parent,
13594 return the base DIE, otherwise return NULL. Set MASK to the
13595 qualifiers added compared to the returned DIE. */
13597 static dw_die_ref
13598 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13600 unsigned int i;
13601 for (i = 0; i < dwarf_qual_info_size; i++)
13602 if (die->die_tag == dwarf_qual_info[i].t)
13603 break;
13604 if (i == dwarf_qual_info_size)
13605 return NULL;
13606 if (vec_safe_length (die->die_attr) != 1)
13607 return NULL;
13608 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13609 if (type == NULL || type->die_parent != die->die_parent)
13610 return NULL;
13611 *mask |= dwarf_qual_info[i].q;
13612 if (depth)
13614 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13615 if (ret)
13616 return ret;
13618 return type;
13621 /* If TYPE is long double or complex long double that
13622 should be emitted as artificial typedef to _Float128 or
13623 complex _Float128, return the type it should be emitted as.
13624 This is done in case the target already supports 16-byte
13625 composite floating point type (ibm_extended_format). */
13627 static tree
13628 long_double_as_float128 (tree type)
13630 if (type != long_double_type_node
13631 && type != complex_long_double_type_node)
13632 return NULL_TREE;
13634 machine_mode mode, fmode;
13635 if (TREE_CODE (type) == COMPLEX_TYPE)
13636 mode = TYPE_MODE (TREE_TYPE (type));
13637 else
13638 mode = TYPE_MODE (type);
13639 if (known_eq (GET_MODE_SIZE (mode), 16) && !MODE_COMPOSITE_P (mode))
13640 FOR_EACH_MODE_IN_CLASS (fmode, MODE_FLOAT)
13641 if (known_eq (GET_MODE_SIZE (fmode), 16)
13642 && MODE_COMPOSITE_P (fmode))
13644 if (type == long_double_type_node)
13646 if (float128_type_node
13647 && (TYPE_MODE (float128_type_node)
13648 == TYPE_MODE (type)))
13649 return float128_type_node;
13650 return NULL_TREE;
13652 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
13653 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
13654 && (TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i))
13655 == TYPE_MODE (type)))
13656 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
13659 return NULL_TREE;
13662 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13663 entry that chains the modifiers specified by CV_QUALS in front of the
13664 given type. REVERSE is true if the type is to be interpreted in the
13665 reverse storage order wrt the target order. */
13667 static dw_die_ref
13668 modified_type_die (tree type, int cv_quals, bool reverse,
13669 dw_die_ref context_die)
13671 enum tree_code code = TREE_CODE (type);
13672 dw_die_ref mod_type_die;
13673 dw_die_ref sub_die = NULL;
13674 tree item_type = NULL;
13675 tree qualified_type;
13676 tree name, low, high;
13677 dw_die_ref mod_scope;
13678 struct array_descr_info info;
13679 /* Only these cv-qualifiers are currently handled. */
13680 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13681 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
13682 /* DW_AT_endianity is specified only for base types in the standard. */
13683 const bool reverse_type
13684 = need_endianity_attribute_p (reverse)
13685 && (is_base_type (type)
13686 || (TREE_CODE (type) == ENUMERAL_TYPE && !dwarf_strict));
13688 if (code == ERROR_MARK)
13689 return NULL;
13691 if (lang_hooks.types.get_debug_type)
13693 tree debug_type = lang_hooks.types.get_debug_type (type);
13695 if (debug_type != NULL_TREE && debug_type != type)
13696 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13699 cv_quals &= cv_qual_mask;
13701 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13702 tag modifier (and not an attribute) old consumers won't be able
13703 to handle it. */
13704 if (dwarf_version < 3)
13705 cv_quals &= ~TYPE_QUAL_RESTRICT;
13707 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13708 if (dwarf_version < 5)
13709 cv_quals &= ~TYPE_QUAL_ATOMIC;
13711 /* See if we already have the appropriately qualified variant of
13712 this type. */
13713 qualified_type = get_qualified_type (type, cv_quals);
13715 if (qualified_type == sizetype)
13717 /* Try not to expose the internal sizetype type's name. */
13718 if (TYPE_NAME (qualified_type)
13719 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13721 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13723 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13724 && (TYPE_PRECISION (t)
13725 == TYPE_PRECISION (qualified_type))
13726 && (TYPE_UNSIGNED (t)
13727 == TYPE_UNSIGNED (qualified_type)));
13728 qualified_type = t;
13730 else if (qualified_type == sizetype
13731 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13732 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13733 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13734 qualified_type = size_type_node;
13735 if (type == sizetype)
13736 type = qualified_type;
13739 /* If we do, then we can just use its DIE, if it exists. */
13740 if (qualified_type)
13742 mod_type_die = lookup_type_die (qualified_type);
13744 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13745 dealt with specially: the DIE with the attribute, if it exists, is
13746 placed immediately after the regular DIE for the same type. */
13747 if (mod_type_die
13748 && (!reverse_type
13749 || ((mod_type_die = mod_type_die->die_sib) != NULL
13750 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13751 return mod_type_die;
13754 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13756 /* Handle C typedef types. */
13757 if (name
13758 && TREE_CODE (name) == TYPE_DECL
13759 && DECL_ORIGINAL_TYPE (name)
13760 && !DECL_ARTIFICIAL (name))
13762 tree dtype = TREE_TYPE (name);
13764 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13765 if (qualified_type == dtype && !reverse_type)
13767 tree origin = decl_ultimate_origin (name);
13769 /* Typedef variants that have an abstract origin don't get their own
13770 type DIE (see gen_typedef_die), so fall back on the ultimate
13771 abstract origin instead. */
13772 if (origin != NULL && origin != name)
13773 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13774 context_die);
13776 /* For a named type, use the typedef. */
13777 gen_type_die (qualified_type, context_die);
13778 return lookup_type_die (qualified_type);
13780 else
13782 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13783 dquals &= cv_qual_mask;
13784 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13785 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13786 /* cv-unqualified version of named type. Just use
13787 the unnamed type to which it refers. */
13788 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13789 reverse, context_die);
13790 /* Else cv-qualified version of named type; fall through. */
13794 mod_scope = scope_die_for (type, context_die);
13796 if (cv_quals)
13798 int sub_quals = 0, first_quals = 0;
13799 unsigned i;
13800 dw_die_ref first = NULL, last = NULL;
13802 /* Determine a lesser qualified type that most closely matches
13803 this one. Then generate DW_TAG_* entries for the remaining
13804 qualifiers. */
13805 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13806 cv_qual_mask);
13807 if (sub_quals && use_debug_types)
13809 bool needed = false;
13810 /* If emitting type units, make sure the order of qualifiers
13811 is canonical. Thus, start from unqualified type if
13812 an earlier qualifier is missing in sub_quals, but some later
13813 one is present there. */
13814 for (i = 0; i < dwarf_qual_info_size; i++)
13815 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13816 needed = true;
13817 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13819 sub_quals = 0;
13820 break;
13823 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13824 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13826 /* As not all intermediate qualified DIEs have corresponding
13827 tree types, ensure that qualified DIEs in the same scope
13828 as their DW_AT_type are emitted after their DW_AT_type,
13829 only with other qualified DIEs for the same type possibly
13830 in between them. Determine the range of such qualified
13831 DIEs now (first being the base type, last being corresponding
13832 last qualified DIE for it). */
13833 unsigned int count = 0;
13834 first = qualified_die_p (mod_type_die, &first_quals,
13835 dwarf_qual_info_size);
13836 if (first == NULL)
13837 first = mod_type_die;
13838 gcc_assert ((first_quals & ~sub_quals) == 0);
13839 for (count = 0, last = first;
13840 count < (1U << dwarf_qual_info_size);
13841 count++, last = last->die_sib)
13843 int quals = 0;
13844 if (last == mod_scope->die_child)
13845 break;
13846 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13847 != first)
13848 break;
13852 for (i = 0; i < dwarf_qual_info_size; i++)
13853 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13855 dw_die_ref d;
13856 if (first && first != last)
13858 for (d = first->die_sib; ; d = d->die_sib)
13860 int quals = 0;
13861 qualified_die_p (d, &quals, dwarf_qual_info_size);
13862 if (quals == (first_quals | dwarf_qual_info[i].q))
13863 break;
13864 if (d == last)
13866 d = NULL;
13867 break;
13870 if (d)
13872 mod_type_die = d;
13873 continue;
13876 if (first)
13878 d = new_die_raw (dwarf_qual_info[i].t);
13879 add_child_die_after (mod_scope, d, last);
13880 last = d;
13882 else
13883 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13884 if (mod_type_die)
13885 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13886 mod_type_die = d;
13887 first_quals |= dwarf_qual_info[i].q;
13890 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13892 dwarf_tag tag = DW_TAG_pointer_type;
13893 if (code == REFERENCE_TYPE)
13895 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13896 tag = DW_TAG_rvalue_reference_type;
13897 else
13898 tag = DW_TAG_reference_type;
13900 mod_type_die = new_die (tag, mod_scope, type);
13902 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13903 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13904 add_alignment_attribute (mod_type_die, type);
13905 item_type = TREE_TYPE (type);
13907 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13908 if (!ADDR_SPACE_GENERIC_P (as))
13910 int action = targetm.addr_space.debug (as);
13911 if (action >= 0)
13913 /* Positive values indicate an address_class. */
13914 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13916 else
13918 /* Negative values indicate an (inverted) segment base reg. */
13919 dw_loc_descr_ref d
13920 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13921 add_AT_loc (mod_type_die, DW_AT_segment, d);
13925 else if (code == ARRAY_TYPE
13926 || (lang_hooks.types.get_array_descr_info
13927 && lang_hooks.types.get_array_descr_info (type, &info)))
13929 gen_type_die (type, context_die);
13930 return lookup_type_die (type);
13932 else if (code == INTEGER_TYPE
13933 && TREE_TYPE (type) != NULL_TREE
13934 && subrange_type_for_debug_p (type, &low, &high))
13936 tree bias = NULL_TREE;
13937 if (lang_hooks.types.get_type_bias)
13938 bias = lang_hooks.types.get_type_bias (type);
13939 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13940 item_type = TREE_TYPE (type);
13942 else if (is_base_type (type))
13944 /* If a target supports long double as different floating point
13945 modes with the same 16-byte size, use normal DW_TAG_base_type
13946 only for the composite (ibm_extended_real_format) type and
13947 for the other for the time being emit instead a "_Float128"
13948 or "complex _Float128" DW_TAG_base_type and a "long double"
13949 or "complex long double" typedef to it. */
13950 if (tree other_type = long_double_as_float128 (type))
13952 dw_die_ref other_die;
13953 if (TYPE_NAME (other_type))
13954 other_die
13955 = modified_type_die (other_type, TYPE_UNQUALIFIED, reverse,
13956 context_die);
13957 else
13959 other_die = base_type_die (type, reverse);
13960 add_child_die (comp_unit_die (), other_die);
13961 add_name_attribute (other_die,
13962 TREE_CODE (type) == COMPLEX_TYPE
13963 ? "complex _Float128" : "_Float128");
13965 mod_type_die = new_die_raw (DW_TAG_typedef);
13966 add_AT_die_ref (mod_type_die, DW_AT_type, other_die);
13968 else
13969 mod_type_die = base_type_die (type, reverse);
13971 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13972 if (reverse_type)
13974 dw_die_ref after_die
13975 = modified_type_die (type, cv_quals, false, context_die);
13976 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13978 else
13979 add_child_die (comp_unit_die (), mod_type_die);
13981 add_pubtype (type, mod_type_die);
13983 else
13985 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13986 if (reverse_type)
13988 dw_die_ref after_die
13989 = modified_type_die (type, cv_quals, false, context_die);
13990 gen_type_die (type, context_die, true);
13991 gcc_assert (after_die->die_sib
13992 && get_AT_unsigned (after_die->die_sib, DW_AT_endianity));
13993 return after_die->die_sib;
13996 gen_type_die (type, context_die);
13998 /* We have to get the type_main_variant here (and pass that to the
13999 `lookup_type_die' routine) because the ..._TYPE node we have
14000 might simply be a *copy* of some original type node (where the
14001 copy was created to help us keep track of typedef names) and
14002 that copy might have a different TYPE_UID from the original
14003 ..._TYPE node. */
14004 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
14006 /* For function/method types, can't just use type_main_variant here,
14007 because that can have different ref-qualifiers for C++,
14008 but try to canonicalize. */
14009 tree main = TYPE_MAIN_VARIANT (type);
14010 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
14011 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
14012 && check_base_type (t, main)
14013 && check_lang_type (t, type))
14014 return lookup_type_die (t);
14015 return lookup_type_die (type);
14017 /* Vectors have the debugging information in the type,
14018 not the main variant. */
14019 else if (code == VECTOR_TYPE)
14020 return lookup_type_die (type);
14021 else
14022 return lookup_type_die (type_main_variant (type));
14025 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
14026 don't output a DW_TAG_typedef, since there isn't one in the
14027 user's program; just attach a DW_AT_name to the type.
14028 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
14029 if the base type already has the same name. */
14030 if (name
14031 && ((TREE_CODE (name) != TYPE_DECL
14032 && (qualified_type == TYPE_MAIN_VARIANT (type)
14033 || (cv_quals == TYPE_UNQUALIFIED)))
14034 || (TREE_CODE (name) == TYPE_DECL
14035 && DECL_NAME (name)
14036 && !DECL_NAMELESS (name)
14037 && (TREE_TYPE (name) == qualified_type
14038 || (lang_hooks.types.get_debug_type
14039 && (lang_hooks.types.get_debug_type (TREE_TYPE (name))
14040 == qualified_type))))))
14042 if (TREE_CODE (name) == TYPE_DECL)
14043 /* Could just call add_name_and_src_coords_attributes here,
14044 but since this is a builtin type it doesn't have any
14045 useful source coordinates anyway. */
14046 name = DECL_NAME (name);
14047 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
14049 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
14051 if (TREE_CODE (type) == BITINT_TYPE)
14053 char name_buf[sizeof ("unsigned _BitInt(2147483647)")];
14054 snprintf (name_buf, sizeof (name_buf),
14055 "%s_BitInt(%d)", TYPE_UNSIGNED (type) ? "unsigned " : "",
14056 TYPE_PRECISION (type));
14057 add_name_attribute (mod_type_die, name_buf);
14059 else
14061 /* This probably indicates a bug. */
14062 name = TYPE_IDENTIFIER (type);
14063 add_name_attribute (mod_type_die,
14064 name
14065 ? IDENTIFIER_POINTER (name) : "__unknown__");
14069 if (qualified_type && !reverse_type)
14070 equate_type_number_to_die (qualified_type, mod_type_die);
14072 if (item_type)
14073 /* We must do this after the equate_type_number_to_die call, in case
14074 this is a recursive type. This ensures that the modified_type_die
14075 recursion will terminate even if the type is recursive. Recursive
14076 types are possible in Ada. */
14077 sub_die = modified_type_die (item_type,
14078 TYPE_QUALS_NO_ADDR_SPACE (item_type),
14079 reverse,
14080 context_die);
14082 if (sub_die != NULL)
14083 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
14085 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
14086 if (TYPE_ARTIFICIAL (type))
14087 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
14089 return mod_type_die;
14092 /* Generate DIEs for the generic parameters of T.
14093 T must be either a generic type or a generic function.
14094 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
14096 static void
14097 gen_generic_params_dies (tree t)
14099 tree parms, args;
14100 int parms_num, i;
14101 dw_die_ref die = NULL;
14102 int non_default;
14104 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
14105 return;
14107 if (TYPE_P (t))
14108 die = lookup_type_die (t);
14109 else if (DECL_P (t))
14110 die = lookup_decl_die (t);
14112 gcc_assert (die);
14114 parms = lang_hooks.get_innermost_generic_parms (t);
14115 if (!parms)
14116 /* T has no generic parameter. It means T is neither a generic type
14117 or function. End of story. */
14118 return;
14120 parms_num = TREE_VEC_LENGTH (parms);
14121 args = lang_hooks.get_innermost_generic_args (t);
14122 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
14123 non_default = int_cst_value (TREE_CHAIN (args));
14124 else
14125 non_default = TREE_VEC_LENGTH (args);
14126 for (i = 0; i < parms_num; i++)
14128 tree parm, arg, arg_pack_elems;
14129 dw_die_ref parm_die;
14131 parm = TREE_VEC_ELT (parms, i);
14132 arg = TREE_VEC_ELT (args, i);
14133 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
14134 gcc_assert (parm && TREE_VALUE (parm) && arg);
14136 if (parm && TREE_VALUE (parm) && arg)
14138 /* If PARM represents a template parameter pack,
14139 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
14140 by DW_TAG_template_*_parameter DIEs for the argument
14141 pack elements of ARG. Note that ARG would then be
14142 an argument pack. */
14143 if (arg_pack_elems)
14144 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
14145 arg_pack_elems,
14146 die);
14147 else
14148 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
14149 true /* emit name */, die);
14150 if (i >= non_default)
14151 add_AT_flag (parm_die, DW_AT_default_value, 1);
14156 /* Create and return a DIE for PARM which should be
14157 the representation of a generic type parameter.
14158 For instance, in the C++ front end, PARM would be a template parameter.
14159 ARG is the argument to PARM.
14160 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
14161 name of the PARM.
14162 PARENT_DIE is the parent DIE which the new created DIE should be added to,
14163 as a child node. */
14165 static dw_die_ref
14166 generic_parameter_die (tree parm, tree arg,
14167 bool emit_name_p,
14168 dw_die_ref parent_die)
14170 dw_die_ref tmpl_die = NULL;
14171 const char *name = NULL;
14173 /* C++20 accepts class literals as template parameters, and var
14174 decls with initializers represent them. The VAR_DECLs would be
14175 rejected, but we can take the DECL_INITIAL constructor and
14176 attempt to expand it. */
14177 if (arg && VAR_P (arg))
14178 arg = DECL_INITIAL (arg);
14180 if (!parm || !DECL_NAME (parm) || !arg)
14181 return NULL;
14183 /* We support non-type generic parameters and arguments,
14184 type generic parameters and arguments, as well as
14185 generic generic parameters (a.k.a. template template parameters in C++)
14186 and arguments. */
14187 if (TREE_CODE (parm) == PARM_DECL)
14188 /* PARM is a nontype generic parameter */
14189 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
14190 else if (TREE_CODE (parm) == TYPE_DECL)
14191 /* PARM is a type generic parameter. */
14192 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14193 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14194 /* PARM is a generic generic parameter.
14195 Its DIE is a GNU extension. It shall have a
14196 DW_AT_name attribute to represent the name of the template template
14197 parameter, and a DW_AT_GNU_template_name attribute to represent the
14198 name of the template template argument. */
14199 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14200 parent_die, parm);
14201 else
14202 gcc_unreachable ();
14204 if (tmpl_die)
14206 tree tmpl_type;
14208 /* If PARM is a generic parameter pack, it means we are
14209 emitting debug info for a template argument pack element.
14210 In other terms, ARG is a template argument pack element.
14211 In that case, we don't emit any DW_AT_name attribute for
14212 the die. */
14213 if (emit_name_p)
14215 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14216 gcc_assert (name);
14217 add_AT_string (tmpl_die, DW_AT_name, name);
14220 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14222 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14223 TMPL_DIE should have a child DW_AT_type attribute that is set
14224 to the type of the argument to PARM, which is ARG.
14225 If PARM is a type generic parameter, TMPL_DIE should have a
14226 child DW_AT_type that is set to ARG. */
14227 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14228 add_type_attribute (tmpl_die, tmpl_type,
14229 (TREE_THIS_VOLATILE (tmpl_type)
14230 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14231 false, parent_die);
14233 else
14235 /* So TMPL_DIE is a DIE representing a
14236 a generic generic template parameter, a.k.a template template
14237 parameter in C++ and arg is a template. */
14239 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14240 to the name of the argument. */
14241 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14242 if (name)
14243 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14246 if (TREE_CODE (parm) == PARM_DECL)
14247 /* So PARM is a non-type generic parameter.
14248 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14249 attribute of TMPL_DIE which value represents the value
14250 of ARG.
14251 We must be careful here:
14252 The value of ARG might reference some function decls.
14253 We might currently be emitting debug info for a generic
14254 type and types are emitted before function decls, we don't
14255 know if the function decls referenced by ARG will actually be
14256 emitted after cgraph computations.
14257 So must defer the generation of the DW_AT_const_value to
14258 after cgraph is ready. */
14259 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14262 return tmpl_die;
14265 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14266 PARM_PACK must be a template parameter pack. The returned DIE
14267 will be child DIE of PARENT_DIE. */
14269 static dw_die_ref
14270 template_parameter_pack_die (tree parm_pack,
14271 tree parm_pack_args,
14272 dw_die_ref parent_die)
14274 dw_die_ref die;
14275 int j;
14277 gcc_assert (parent_die && parm_pack);
14279 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14280 add_name_and_src_coords_attributes (die, parm_pack);
14281 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14282 generic_parameter_die (parm_pack,
14283 TREE_VEC_ELT (parm_pack_args, j),
14284 false /* Don't emit DW_AT_name */,
14285 die);
14286 return die;
14289 /* Return the debugger register number described by a given RTL node. */
14291 static unsigned int
14292 debugger_reg_number (const_rtx rtl)
14294 unsigned regno = REGNO (rtl);
14296 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14298 #ifdef LEAF_REG_REMAP
14299 if (crtl->uses_only_leaf_regs)
14301 int leaf_reg = LEAF_REG_REMAP (regno);
14302 if (leaf_reg != -1)
14303 regno = (unsigned) leaf_reg;
14305 #endif
14307 regno = DEBUGGER_REGNO (regno);
14308 gcc_assert (regno != INVALID_REGNUM);
14309 return regno;
14312 /* Optionally add a DW_OP_piece term to a location description expression.
14313 DW_OP_piece is only added if the location description expression already
14314 doesn't end with DW_OP_piece. */
14316 static void
14317 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14319 dw_loc_descr_ref loc;
14321 if (*list_head != NULL)
14323 /* Find the end of the chain. */
14324 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14327 if (loc->dw_loc_opc != DW_OP_piece)
14328 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14332 /* Return a location descriptor that designates a machine register or
14333 zero if there is none. */
14335 static dw_loc_descr_ref
14336 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14338 rtx regs;
14340 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14341 return 0;
14343 /* We only use "frame base" when we're sure we're talking about the
14344 post-prologue local stack frame. We do this by *not* running
14345 register elimination until this point, and recognizing the special
14346 argument pointer and soft frame pointer rtx's.
14347 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14348 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14349 && (ira_use_lra_p
14350 ? lra_eliminate_regs (rtl, VOIDmode, NULL_RTX)
14351 : eliminate_regs (rtl, VOIDmode, NULL_RTX)) != rtl)
14353 dw_loc_descr_ref result = NULL;
14355 if (dwarf_version >= 4 || !dwarf_strict)
14357 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14358 initialized);
14359 if (result)
14360 add_loc_descr (&result,
14361 new_loc_descr (DW_OP_stack_value, 0, 0));
14363 return result;
14366 regs = targetm.dwarf_register_span (rtl);
14368 if (REG_NREGS (rtl) > 1 || regs)
14369 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14370 else
14372 unsigned int debugger_regnum = debugger_reg_number (rtl);
14373 if (debugger_regnum == IGNORED_DWARF_REGNUM)
14374 return 0;
14375 return one_reg_loc_descriptor (debugger_regnum, initialized);
14379 /* Return a location descriptor that designates a machine register for
14380 a given hard register number. */
14382 static dw_loc_descr_ref
14383 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14385 dw_loc_descr_ref reg_loc_descr;
14387 if (regno <= 31)
14388 reg_loc_descr
14389 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14390 else
14391 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14393 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14394 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14396 return reg_loc_descr;
14399 /* Given an RTL of a register, return a location descriptor that
14400 designates a value that spans more than one register. */
14402 static dw_loc_descr_ref
14403 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14404 enum var_init_status initialized)
14406 int size, i;
14407 dw_loc_descr_ref loc_result = NULL;
14409 /* Simple, contiguous registers. */
14410 if (regs == NULL_RTX)
14412 unsigned reg = REGNO (rtl);
14413 int nregs;
14415 #ifdef LEAF_REG_REMAP
14416 if (crtl->uses_only_leaf_regs)
14418 int leaf_reg = LEAF_REG_REMAP (reg);
14419 if (leaf_reg != -1)
14420 reg = (unsigned) leaf_reg;
14422 #endif
14424 gcc_assert ((unsigned) DEBUGGER_REGNO (reg) == debugger_reg_number (rtl));
14425 nregs = REG_NREGS (rtl);
14427 /* At present we only track constant-sized pieces. */
14428 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14429 return NULL;
14430 size /= nregs;
14432 loc_result = NULL;
14433 while (nregs--)
14435 dw_loc_descr_ref t;
14437 t = one_reg_loc_descriptor (DEBUGGER_REGNO (reg),
14438 VAR_INIT_STATUS_INITIALIZED);
14439 add_loc_descr (&loc_result, t);
14440 add_loc_descr_op_piece (&loc_result, size);
14441 ++reg;
14443 return loc_result;
14446 /* Now onto stupid register sets in non contiguous locations. */
14448 gcc_assert (GET_CODE (regs) == PARALLEL);
14450 /* At present we only track constant-sized pieces. */
14451 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14452 return NULL;
14453 loc_result = NULL;
14455 for (i = 0; i < XVECLEN (regs, 0); ++i)
14457 dw_loc_descr_ref t;
14459 t = one_reg_loc_descriptor (debugger_reg_number (XVECEXP (regs, 0, i)),
14460 VAR_INIT_STATUS_INITIALIZED);
14461 add_loc_descr (&loc_result, t);
14462 add_loc_descr_op_piece (&loc_result, size);
14465 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14466 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14467 return loc_result;
14470 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14472 /* Return a location descriptor that designates a constant i,
14473 as a compound operation from constant (i >> shift), constant shift
14474 and DW_OP_shl. */
14476 static dw_loc_descr_ref
14477 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14479 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14480 add_loc_descr (&ret, int_loc_descriptor (shift));
14481 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14482 return ret;
14485 /* Return a location descriptor that designates constant POLY_I. */
14487 static dw_loc_descr_ref
14488 int_loc_descriptor (poly_int64 poly_i)
14490 enum dwarf_location_atom op;
14492 HOST_WIDE_INT i;
14493 if (!poly_i.is_constant (&i))
14495 /* Create location descriptions for the non-constant part and
14496 add any constant offset at the end. */
14497 dw_loc_descr_ref ret = NULL;
14498 HOST_WIDE_INT constant = poly_i.coeffs[0];
14499 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14501 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14502 if (coeff != 0)
14504 dw_loc_descr_ref start = ret;
14505 unsigned int factor;
14506 int bias;
14507 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14508 (j, &factor, &bias);
14510 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14511 add COEFF * (REGNO / FACTOR) now and subtract
14512 COEFF * BIAS from the final constant part. */
14513 constant -= coeff * bias;
14514 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14515 if (coeff % factor == 0)
14516 coeff /= factor;
14517 else
14519 int amount = exact_log2 (factor);
14520 gcc_assert (amount >= 0);
14521 add_loc_descr (&ret, int_loc_descriptor (amount));
14522 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14524 if (coeff != 1)
14526 add_loc_descr (&ret, int_loc_descriptor (coeff));
14527 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14529 if (start)
14530 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14533 loc_descr_plus_const (&ret, constant);
14534 return ret;
14537 /* Pick the smallest representation of a constant, rather than just
14538 defaulting to the LEB encoding. */
14539 if (i >= 0)
14541 int clz = clz_hwi (i);
14542 int ctz = ctz_hwi (i);
14543 if (i <= 31)
14544 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14545 else if (i <= 0xff)
14546 op = DW_OP_const1u;
14547 else if (i <= 0xffff)
14548 op = DW_OP_const2u;
14549 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14550 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14551 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14552 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14553 while DW_OP_const4u is 5 bytes. */
14554 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14555 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14556 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14557 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14558 while DW_OP_const4u is 5 bytes. */
14559 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14561 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14562 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14563 <= 4)
14565 /* As i >= 2**31, the double cast above will yield a negative number.
14566 Since wrapping is defined in DWARF expressions we can output big
14567 positive integers as small negative ones, regardless of the size
14568 of host wide ints.
14570 Here, since the evaluator will handle 32-bit values and since i >=
14571 2**31, we know it's going to be interpreted as a negative literal:
14572 store it this way if we can do better than 5 bytes this way. */
14573 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14575 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14576 op = DW_OP_const4u;
14578 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14579 least 6 bytes: see if we can do better before falling back to it. */
14580 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14581 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14582 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14583 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14584 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14585 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14586 >= HOST_BITS_PER_WIDE_INT)
14587 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14588 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14589 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14590 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14591 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14592 && size_of_uleb128 (i) > 6)
14593 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14594 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14595 else
14596 op = DW_OP_constu;
14598 else
14600 if (i >= -0x80)
14601 op = DW_OP_const1s;
14602 else if (i >= -0x8000)
14603 op = DW_OP_const2s;
14604 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14606 if (size_of_int_loc_descriptor (i) < 5)
14608 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14609 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14610 return ret;
14612 op = DW_OP_const4s;
14614 else
14616 if (size_of_int_loc_descriptor (i)
14617 < (unsigned long) 1 + size_of_sleb128 (i))
14619 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14620 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14621 return ret;
14623 op = DW_OP_consts;
14627 return new_loc_descr (op, i, 0);
14630 /* Likewise, for unsigned constants. */
14632 static dw_loc_descr_ref
14633 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14635 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14636 const unsigned HOST_WIDE_INT max_uint
14637 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14639 /* If possible, use the clever signed constants handling. */
14640 if (i <= max_int)
14641 return int_loc_descriptor ((HOST_WIDE_INT) i);
14643 /* Here, we are left with positive numbers that cannot be represented as
14644 HOST_WIDE_INT, i.e.:
14645 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14647 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14648 whereas may be better to output a negative integer: thanks to integer
14649 wrapping, we know that:
14650 x = x - 2 ** DWARF2_ADDR_SIZE
14651 = x - 2 * (max (HOST_WIDE_INT) + 1)
14652 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14653 small negative integers. Let's try that in cases it will clearly improve
14654 the encoding: there is no gain turning DW_OP_const4u into
14655 DW_OP_const4s. */
14656 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14657 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14658 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14660 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14662 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14663 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14664 const HOST_WIDE_INT second_shift
14665 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14667 /* So we finally have:
14668 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14669 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14670 return int_loc_descriptor (second_shift);
14673 /* Last chance: fallback to a simple constant operation. */
14674 return new_loc_descr
14675 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14676 ? DW_OP_const4u
14677 : DW_OP_const8u,
14678 i, 0);
14681 /* Generate and return a location description that computes the unsigned
14682 comparison of the two stack top entries (a OP b where b is the top-most
14683 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14684 LE_EXPR, GT_EXPR or GE_EXPR. */
14686 static dw_loc_descr_ref
14687 uint_comparison_loc_list (enum tree_code kind)
14689 enum dwarf_location_atom op, flip_op;
14690 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14692 switch (kind)
14694 case LT_EXPR:
14695 op = DW_OP_lt;
14696 break;
14697 case LE_EXPR:
14698 op = DW_OP_le;
14699 break;
14700 case GT_EXPR:
14701 op = DW_OP_gt;
14702 break;
14703 case GE_EXPR:
14704 op = DW_OP_ge;
14705 break;
14706 default:
14707 gcc_unreachable ();
14710 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14711 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14713 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14714 possible to perform unsigned comparisons: we just have to distinguish
14715 three cases:
14717 1. when a and b have the same sign (as signed integers); then we should
14718 return: a OP(signed) b;
14720 2. when a is a negative signed integer while b is a positive one, then a
14721 is a greater unsigned integer than b; likewise when a and b's roles
14722 are flipped.
14724 So first, compare the sign of the two operands. */
14725 ret = new_loc_descr (DW_OP_over, 0, 0);
14726 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14727 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14728 /* If they have different signs (i.e. they have different sign bits), then
14729 the stack top value has now the sign bit set and thus it's smaller than
14730 zero. */
14731 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14732 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14733 add_loc_descr (&ret, bra_node);
14735 /* We are in case 1. At this point, we know both operands have the same
14736 sign, to it's safe to use the built-in signed comparison. */
14737 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14738 add_loc_descr (&ret, jmp_node);
14740 /* We are in case 2. Here, we know both operands do not have the same sign,
14741 so we have to flip the signed comparison. */
14742 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14743 tmp = new_loc_descr (flip_op, 0, 0);
14744 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14745 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14746 add_loc_descr (&ret, tmp);
14748 /* This dummy operation is necessary to make the two branches join. */
14749 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14750 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14751 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14752 add_loc_descr (&ret, tmp);
14754 return ret;
14757 /* Likewise, but takes the location description lists (might be destructive on
14758 them). Return NULL if either is NULL or if concatenation fails. */
14760 static dw_loc_list_ref
14761 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14762 enum tree_code kind)
14764 if (left == NULL || right == NULL)
14765 return NULL;
14767 add_loc_list (&left, right);
14768 if (left == NULL)
14769 return NULL;
14771 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14772 return left;
14775 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14776 without actually allocating it. */
14778 static unsigned long
14779 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14781 return size_of_int_loc_descriptor (i >> shift)
14782 + size_of_int_loc_descriptor (shift)
14783 + 1;
14786 /* Return size_of_locs (int_loc_descriptor (i)) without
14787 actually allocating it. */
14789 static unsigned long
14790 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14792 unsigned long s;
14794 if (i >= 0)
14796 int clz, ctz;
14797 if (i <= 31)
14798 return 1;
14799 else if (i <= 0xff)
14800 return 2;
14801 else if (i <= 0xffff)
14802 return 3;
14803 clz = clz_hwi (i);
14804 ctz = ctz_hwi (i);
14805 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14806 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14807 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14808 - clz - 5);
14809 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14810 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14811 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14812 - clz - 8);
14813 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14814 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14815 <= 4)
14816 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14817 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14818 return 5;
14819 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14820 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14821 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14822 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14823 - clz - 8);
14824 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14825 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14826 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14827 - clz - 16);
14828 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14829 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14830 && s > 6)
14831 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14832 - clz - 32);
14833 else
14834 return 1 + s;
14836 else
14838 if (i >= -0x80)
14839 return 2;
14840 else if (i >= -0x8000)
14841 return 3;
14842 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14844 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14846 s = size_of_int_loc_descriptor (-i) + 1;
14847 if (s < 5)
14848 return s;
14850 return 5;
14852 else
14854 unsigned long r = 1 + size_of_sleb128 (i);
14855 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14857 s = size_of_int_loc_descriptor (-i) + 1;
14858 if (s < r)
14859 return s;
14861 return r;
14866 /* Return loc description representing "address" of integer value.
14867 This can appear only as toplevel expression. */
14869 static dw_loc_descr_ref
14870 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14872 int litsize;
14873 dw_loc_descr_ref loc_result = NULL;
14875 if (!(dwarf_version >= 4 || !dwarf_strict))
14876 return NULL;
14878 litsize = size_of_int_loc_descriptor (i);
14879 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14880 is more compact. For DW_OP_stack_value we need:
14881 litsize + 1 (DW_OP_stack_value)
14882 and for DW_OP_implicit_value:
14883 1 (DW_OP_implicit_value) + 1 (length) + size. */
14884 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14886 loc_result = int_loc_descriptor (i);
14887 add_loc_descr (&loc_result,
14888 new_loc_descr (DW_OP_stack_value, 0, 0));
14889 return loc_result;
14892 loc_result = new_loc_descr (DW_OP_implicit_value,
14893 size, 0);
14894 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14895 loc_result->dw_loc_oprnd2.v.val_int = i;
14896 return loc_result;
14899 /* Return a location descriptor that designates a base+offset location. */
14901 static dw_loc_descr_ref
14902 based_loc_descr (rtx reg, poly_int64 offset,
14903 enum var_init_status initialized)
14905 unsigned int regno;
14906 dw_loc_descr_ref result;
14907 dw_fde_ref fde = cfun->fde;
14909 /* We only use "frame base" when we're sure we're talking about the
14910 post-prologue local stack frame. We do this by *not* running
14911 register elimination until this point, and recognizing the special
14912 argument pointer and soft frame pointer rtx's. */
14913 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14915 rtx elim = (ira_use_lra_p
14916 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14917 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14919 if (elim != reg)
14921 /* Allow hard frame pointer here even if frame pointer
14922 isn't used since hard frame pointer is encoded with
14923 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14924 not hard frame pointer directly. */
14925 elim = strip_offset_and_add (elim, &offset);
14926 gcc_assert (elim == hard_frame_pointer_rtx
14927 || elim == stack_pointer_rtx);
14929 /* If drap register is used to align stack, use frame
14930 pointer + offset to access stack variables. If stack
14931 is aligned without drap, use stack pointer + offset to
14932 access stack variables. */
14933 if (crtl->stack_realign_tried
14934 && reg == frame_pointer_rtx)
14936 int base_reg
14937 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14938 ? HARD_FRAME_POINTER_REGNUM
14939 : REGNO (elim));
14940 return new_reg_loc_descr (base_reg, offset);
14943 gcc_assert (frame_pointer_fb_offset_valid);
14944 offset += frame_pointer_fb_offset;
14945 HOST_WIDE_INT const_offset;
14946 if (offset.is_constant (&const_offset))
14947 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14948 else
14950 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14951 loc_descr_plus_const (&ret, offset);
14952 return ret;
14957 regno = REGNO (reg);
14958 #ifdef LEAF_REG_REMAP
14959 if (crtl->uses_only_leaf_regs)
14961 int leaf_reg = LEAF_REG_REMAP (regno);
14962 if (leaf_reg != -1)
14963 regno = (unsigned) leaf_reg;
14965 #endif
14966 regno = DWARF_FRAME_REGNUM (regno);
14968 HOST_WIDE_INT const_offset;
14969 if (!optimize && fde
14970 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14971 && offset.is_constant (&const_offset))
14973 /* Use cfa+offset to represent the location of arguments passed
14974 on the stack when drap is used to align stack.
14975 Only do this when not optimizing, for optimized code var-tracking
14976 is supposed to track where the arguments live and the register
14977 used as vdrap or drap in some spot might be used for something
14978 else in other part of the routine. */
14979 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14982 result = new_reg_loc_descr (regno, offset);
14984 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14985 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14987 return result;
14990 /* Return true if this RTL expression describes a base+offset calculation. */
14992 static inline bool
14993 is_based_loc (const_rtx rtl)
14995 return (GET_CODE (rtl) == PLUS
14996 && ((REG_P (XEXP (rtl, 0))
14997 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14998 && CONST_INT_P (XEXP (rtl, 1)))));
15001 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
15002 failed. */
15004 static dw_loc_descr_ref
15005 tls_mem_loc_descriptor (rtx mem)
15007 tree base;
15008 dw_loc_descr_ref loc_result;
15010 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
15011 return NULL;
15013 base = get_base_address (MEM_EXPR (mem));
15014 if (base == NULL
15015 || !VAR_P (base)
15016 || !DECL_THREAD_LOCAL_P (base))
15017 return NULL;
15019 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
15020 if (loc_result == NULL)
15021 return NULL;
15023 if (maybe_ne (MEM_OFFSET (mem), 0))
15024 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
15026 return loc_result;
15029 /* Output debug info about reason why we failed to expand expression as dwarf
15030 expression. */
15032 static void
15033 expansion_failed (tree expr, rtx rtl, char const *reason)
15035 if (dump_file && (dump_flags & TDF_DETAILS))
15037 fprintf (dump_file, "Failed to expand as dwarf: ");
15038 if (expr)
15039 print_generic_expr (dump_file, expr, dump_flags);
15040 if (rtl)
15042 fprintf (dump_file, "\n");
15043 print_rtl (dump_file, rtl);
15045 fprintf (dump_file, "\nReason: %s\n", reason);
15049 /* Helper function for const_ok_for_output. */
15051 static bool
15052 const_ok_for_output_1 (rtx rtl)
15054 if (targetm.const_not_ok_for_debug_p (rtl))
15056 if (GET_CODE (rtl) != UNSPEC)
15058 expansion_failed (NULL_TREE, rtl,
15059 "Expression rejected for debug by the backend.\n");
15060 return false;
15063 /* If delegitimize_address couldn't do anything with the UNSPEC, and
15064 the target hook doesn't explicitly allow it in debug info, assume
15065 we can't express it in the debug info. */
15066 /* Don't complain about TLS UNSPECs, those are just too hard to
15067 delegitimize. Note this could be a non-decl SYMBOL_REF such as
15068 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
15069 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
15070 if (flag_checking
15071 && (XVECLEN (rtl, 0) == 0
15072 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
15073 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
15074 inform (current_function_decl
15075 ? DECL_SOURCE_LOCATION (current_function_decl)
15076 : UNKNOWN_LOCATION,
15077 #if NUM_UNSPEC_VALUES > 0
15078 "non-delegitimized UNSPEC %s (%d) found in variable location",
15079 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
15080 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
15081 #else
15082 "non-delegitimized UNSPEC %d found in variable location",
15083 #endif
15084 XINT (rtl, 1));
15085 expansion_failed (NULL_TREE, rtl,
15086 "UNSPEC hasn't been delegitimized.\n");
15087 return false;
15090 if (CONST_POLY_INT_P (rtl))
15091 return false;
15093 /* FIXME: Refer to PR60655. It is possible for simplification
15094 of rtl expressions in var tracking to produce such expressions.
15095 We should really identify / validate expressions
15096 enclosed in CONST that can be handled by assemblers on various
15097 targets and only handle legitimate cases here. */
15098 switch (GET_CODE (rtl))
15100 case SYMBOL_REF:
15101 break;
15102 case NOT:
15103 case NEG:
15104 return false;
15105 case PLUS:
15107 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
15108 operands. */
15109 subrtx_var_iterator::array_type array;
15110 bool first = false;
15111 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15112 if (SYMBOL_REF_P (*iter)
15113 || LABEL_P (*iter)
15114 || GET_CODE (*iter) == UNSPEC)
15116 first = true;
15117 break;
15119 if (!first)
15120 return true;
15121 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15122 if (SYMBOL_REF_P (*iter)
15123 || LABEL_P (*iter)
15124 || GET_CODE (*iter) == UNSPEC)
15125 return false;
15126 return true;
15128 case MINUS:
15130 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
15131 appear in the second operand of MINUS. */
15132 subrtx_var_iterator::array_type array;
15133 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15134 if (SYMBOL_REF_P (*iter)
15135 || LABEL_P (*iter)
15136 || GET_CODE (*iter) == UNSPEC)
15137 return false;
15138 return true;
15140 default:
15141 return true;
15144 if (CONSTANT_POOL_ADDRESS_P (rtl))
15146 bool marked;
15147 get_pool_constant_mark (rtl, &marked);
15148 /* If all references to this pool constant were optimized away,
15149 it was not output and thus we can't represent it. */
15150 if (!marked)
15152 expansion_failed (NULL_TREE, rtl,
15153 "Constant was removed from constant pool.\n");
15154 return false;
15158 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15159 return false;
15161 /* Avoid references to external symbols in debug info, on several targets
15162 the linker might even refuse to link when linking a shared library,
15163 and in many other cases the relocations for .debug_info/.debug_loc are
15164 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
15165 to be defined within the same shared library or executable are fine. */
15166 if (SYMBOL_REF_EXTERNAL_P (rtl))
15168 tree decl = SYMBOL_REF_DECL (rtl);
15170 if (decl == NULL || !targetm.binds_local_p (decl))
15172 expansion_failed (NULL_TREE, rtl,
15173 "Symbol not defined in current TU.\n");
15174 return false;
15178 return true;
15181 /* Return true if constant RTL can be emitted in DW_OP_addr or
15182 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
15183 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
15185 static bool
15186 const_ok_for_output (rtx rtl)
15188 if (GET_CODE (rtl) == SYMBOL_REF)
15189 return const_ok_for_output_1 (rtl);
15191 if (GET_CODE (rtl) == CONST)
15193 subrtx_var_iterator::array_type array;
15194 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15195 if (!const_ok_for_output_1 (*iter))
15196 return false;
15197 return true;
15200 return true;
15203 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15204 if possible, NULL otherwise. */
15206 static dw_die_ref
15207 base_type_for_mode (machine_mode mode, bool unsignedp)
15209 dw_die_ref type_die;
15210 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15212 if (type == NULL)
15213 return NULL;
15214 switch (TREE_CODE (type))
15216 case INTEGER_TYPE:
15217 case REAL_TYPE:
15218 break;
15219 default:
15220 return NULL;
15222 type_die = lookup_type_die (type);
15223 if (!type_die)
15224 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15225 comp_unit_die ());
15226 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15227 return NULL;
15228 return type_die;
15231 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15232 type matching MODE, or, if MODE is narrower than or as wide as
15233 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15234 possible. */
15236 static dw_loc_descr_ref
15237 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15239 machine_mode outer_mode = mode;
15240 dw_die_ref type_die;
15241 dw_loc_descr_ref cvt;
15243 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15245 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15246 return op;
15248 type_die = base_type_for_mode (outer_mode, 1);
15249 if (type_die == NULL)
15250 return NULL;
15251 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15252 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15253 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15254 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15255 add_loc_descr (&op, cvt);
15256 return op;
15259 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15261 static dw_loc_descr_ref
15262 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15263 dw_loc_descr_ref op1)
15265 dw_loc_descr_ref ret = op0;
15266 add_loc_descr (&ret, op1);
15267 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15268 if (STORE_FLAG_VALUE != 1)
15270 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15271 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15273 return ret;
15276 /* Subroutine of scompare_loc_descriptor for the case in which we're
15277 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15278 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15280 static dw_loc_descr_ref
15281 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15282 scalar_int_mode op_mode,
15283 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15285 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15286 dw_loc_descr_ref cvt;
15288 if (type_die == NULL)
15289 return NULL;
15290 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15291 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15292 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15293 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15294 add_loc_descr (&op0, cvt);
15295 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15296 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15297 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15298 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15299 add_loc_descr (&op1, cvt);
15300 return compare_loc_descriptor (op, op0, op1);
15303 /* Subroutine of scompare_loc_descriptor for the case in which we're
15304 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15305 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15307 static dw_loc_descr_ref
15308 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15309 scalar_int_mode op_mode,
15310 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15312 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15313 /* For eq/ne, if the operands are known to be zero-extended,
15314 there is no need to do the fancy shifting up. */
15315 if (op == DW_OP_eq || op == DW_OP_ne)
15317 dw_loc_descr_ref last0, last1;
15318 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15320 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15322 /* deref_size zero extends, and for constants we can check
15323 whether they are zero extended or not. */
15324 if (((last0->dw_loc_opc == DW_OP_deref_size
15325 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15326 || (CONST_INT_P (XEXP (rtl, 0))
15327 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15328 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15329 && ((last1->dw_loc_opc == DW_OP_deref_size
15330 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15331 || (CONST_INT_P (XEXP (rtl, 1))
15332 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15333 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15334 return compare_loc_descriptor (op, op0, op1);
15336 /* EQ/NE comparison against constant in narrower type than
15337 DWARF2_ADDR_SIZE can be performed either as
15338 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15339 DW_OP_{eq,ne}
15341 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15342 DW_OP_{eq,ne}. Pick whatever is shorter. */
15343 if (CONST_INT_P (XEXP (rtl, 1))
15344 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15345 && (size_of_int_loc_descriptor (shift) + 1
15346 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15347 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15348 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15349 & GET_MODE_MASK (op_mode))))
15351 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15352 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15353 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15354 & GET_MODE_MASK (op_mode));
15355 return compare_loc_descriptor (op, op0, op1);
15358 add_loc_descr (&op0, int_loc_descriptor (shift));
15359 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15360 if (CONST_INT_P (XEXP (rtl, 1)))
15361 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15362 else
15364 add_loc_descr (&op1, int_loc_descriptor (shift));
15365 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15367 return compare_loc_descriptor (op, op0, op1);
15370 /* Return location descriptor for signed comparison OP RTL. */
15372 static dw_loc_descr_ref
15373 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15374 machine_mode mem_mode)
15376 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15377 dw_loc_descr_ref op0, op1;
15379 if (op_mode == VOIDmode)
15380 op_mode = GET_MODE (XEXP (rtl, 1));
15381 if (op_mode == VOIDmode)
15382 return NULL;
15384 scalar_int_mode int_op_mode;
15385 if (dwarf_strict
15386 && dwarf_version < 5
15387 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15388 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15389 return NULL;
15391 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15392 VAR_INIT_STATUS_INITIALIZED);
15393 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15394 VAR_INIT_STATUS_INITIALIZED);
15396 if (op0 == NULL || op1 == NULL)
15397 return NULL;
15399 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15401 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15402 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15404 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15405 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15407 return compare_loc_descriptor (op, op0, op1);
15410 /* Return location descriptor for unsigned comparison OP RTL. */
15412 static dw_loc_descr_ref
15413 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15414 machine_mode mem_mode)
15416 dw_loc_descr_ref op0, op1;
15418 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15419 if (test_op_mode == VOIDmode)
15420 test_op_mode = GET_MODE (XEXP (rtl, 1));
15422 scalar_int_mode op_mode;
15423 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15424 return NULL;
15426 if (dwarf_strict
15427 && dwarf_version < 5
15428 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15429 return NULL;
15431 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15432 VAR_INIT_STATUS_INITIALIZED);
15433 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15434 VAR_INIT_STATUS_INITIALIZED);
15436 if (op0 == NULL || op1 == NULL)
15437 return NULL;
15439 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15441 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15442 dw_loc_descr_ref last0, last1;
15443 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15445 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15447 if (CONST_INT_P (XEXP (rtl, 0)))
15448 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15449 /* deref_size zero extends, so no need to mask it again. */
15450 else if (last0->dw_loc_opc != DW_OP_deref_size
15451 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15453 add_loc_descr (&op0, int_loc_descriptor (mask));
15454 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15456 if (CONST_INT_P (XEXP (rtl, 1)))
15457 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15458 /* deref_size zero extends, so no need to mask it again. */
15459 else if (last1->dw_loc_opc != DW_OP_deref_size
15460 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15462 add_loc_descr (&op1, int_loc_descriptor (mask));
15463 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15466 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15468 HOST_WIDE_INT bias = 1;
15469 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15470 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15471 if (CONST_INT_P (XEXP (rtl, 1)))
15472 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15473 + INTVAL (XEXP (rtl, 1)));
15474 else
15475 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15476 bias, 0));
15478 return compare_loc_descriptor (op, op0, op1);
15481 /* Return location descriptor for {U,S}{MIN,MAX}. */
15483 static dw_loc_descr_ref
15484 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15485 machine_mode mem_mode)
15487 enum dwarf_location_atom op;
15488 dw_loc_descr_ref op0, op1, ret;
15489 dw_loc_descr_ref bra_node, drop_node;
15491 scalar_int_mode int_mode;
15492 if (dwarf_strict
15493 && dwarf_version < 5
15494 && (!is_a <scalar_int_mode> (mode, &int_mode)
15495 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15496 return NULL;
15498 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15499 VAR_INIT_STATUS_INITIALIZED);
15500 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15501 VAR_INIT_STATUS_INITIALIZED);
15503 if (op0 == NULL || op1 == NULL)
15504 return NULL;
15506 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15507 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15508 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15509 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15511 /* Checked by the caller. */
15512 int_mode = as_a <scalar_int_mode> (mode);
15513 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15515 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15516 add_loc_descr (&op0, int_loc_descriptor (mask));
15517 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15518 add_loc_descr (&op1, int_loc_descriptor (mask));
15519 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15521 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15523 HOST_WIDE_INT bias = 1;
15524 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15525 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15526 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15529 else if (is_a <scalar_int_mode> (mode, &int_mode)
15530 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15532 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15533 add_loc_descr (&op0, int_loc_descriptor (shift));
15534 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15535 add_loc_descr (&op1, int_loc_descriptor (shift));
15536 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15538 else if (is_a <scalar_int_mode> (mode, &int_mode)
15539 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15541 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15542 dw_loc_descr_ref cvt;
15543 if (type_die == NULL)
15544 return NULL;
15545 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15546 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15547 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15548 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15549 add_loc_descr (&op0, cvt);
15550 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15551 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15552 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15553 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15554 add_loc_descr (&op1, cvt);
15557 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15558 op = DW_OP_lt;
15559 else
15560 op = DW_OP_gt;
15561 ret = op0;
15562 add_loc_descr (&ret, op1);
15563 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15564 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15565 add_loc_descr (&ret, bra_node);
15566 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15567 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15568 add_loc_descr (&ret, drop_node);
15569 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15570 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15571 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15572 && is_a <scalar_int_mode> (mode, &int_mode)
15573 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15574 ret = convert_descriptor_to_mode (int_mode, ret);
15575 return ret;
15578 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15579 but after converting arguments to type_die, afterwards
15580 convert back to unsigned. */
15582 static dw_loc_descr_ref
15583 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15584 scalar_int_mode mode, machine_mode mem_mode)
15586 dw_loc_descr_ref cvt, op0, op1;
15588 if (type_die == NULL)
15589 return NULL;
15590 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15591 VAR_INIT_STATUS_INITIALIZED);
15592 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15593 VAR_INIT_STATUS_INITIALIZED);
15594 if (op0 == NULL || op1 == NULL)
15595 return NULL;
15596 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15597 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15598 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15599 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15600 add_loc_descr (&op0, cvt);
15601 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15602 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15603 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15604 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15605 add_loc_descr (&op1, cvt);
15606 add_loc_descr (&op0, op1);
15607 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15608 return convert_descriptor_to_mode (mode, op0);
15611 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15612 const0 is DW_OP_lit0 or corresponding typed constant,
15613 const1 is DW_OP_lit1 or corresponding typed constant
15614 and constMSB is constant with just the MSB bit set
15615 for the mode):
15616 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15617 L1: const0 DW_OP_swap
15618 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15619 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15620 L3: DW_OP_drop
15621 L4: DW_OP_nop
15623 CTZ is similar:
15624 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15625 L1: const0 DW_OP_swap
15626 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15627 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15628 L3: DW_OP_drop
15629 L4: DW_OP_nop
15631 FFS is similar:
15632 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15633 L1: const1 DW_OP_swap
15634 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15635 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15636 L3: DW_OP_drop
15637 L4: DW_OP_nop */
15639 static dw_loc_descr_ref
15640 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15641 machine_mode mem_mode)
15643 dw_loc_descr_ref op0, ret, tmp;
15644 HOST_WIDE_INT valv;
15645 dw_loc_descr_ref l1jump, l1label;
15646 dw_loc_descr_ref l2jump, l2label;
15647 dw_loc_descr_ref l3jump, l3label;
15648 dw_loc_descr_ref l4jump, l4label;
15649 rtx msb;
15651 if (GET_MODE (XEXP (rtl, 0)) != mode)
15652 return NULL;
15654 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15655 VAR_INIT_STATUS_INITIALIZED);
15656 if (op0 == NULL)
15657 return NULL;
15658 ret = op0;
15659 if (GET_CODE (rtl) == CLZ)
15661 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15662 valv = GET_MODE_BITSIZE (mode);
15664 else if (GET_CODE (rtl) == FFS)
15665 valv = 0;
15666 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15667 valv = GET_MODE_BITSIZE (mode);
15668 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15669 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15670 add_loc_descr (&ret, l1jump);
15671 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15672 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15673 VAR_INIT_STATUS_INITIALIZED);
15674 if (tmp == NULL)
15675 return NULL;
15676 add_loc_descr (&ret, tmp);
15677 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15678 add_loc_descr (&ret, l4jump);
15679 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15680 ? const1_rtx : const0_rtx,
15681 mode, mem_mode,
15682 VAR_INIT_STATUS_INITIALIZED);
15683 if (l1label == NULL)
15684 return NULL;
15685 add_loc_descr (&ret, l1label);
15686 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15687 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15688 add_loc_descr (&ret, l2label);
15689 if (GET_CODE (rtl) != CLZ)
15690 msb = const1_rtx;
15691 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15692 msb = GEN_INT (HOST_WIDE_INT_1U
15693 << (GET_MODE_BITSIZE (mode) - 1));
15694 else
15695 msb = immed_wide_int_const
15696 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15697 GET_MODE_PRECISION (mode)), mode);
15698 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15699 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15700 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15701 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15702 else
15703 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15704 VAR_INIT_STATUS_INITIALIZED);
15705 if (tmp == NULL)
15706 return NULL;
15707 add_loc_descr (&ret, tmp);
15708 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15709 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15710 add_loc_descr (&ret, l3jump);
15711 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15712 VAR_INIT_STATUS_INITIALIZED);
15713 if (tmp == NULL)
15714 return NULL;
15715 add_loc_descr (&ret, tmp);
15716 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15717 ? DW_OP_shl : DW_OP_shr, 0, 0));
15718 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15719 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15720 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15721 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15722 add_loc_descr (&ret, l2jump);
15723 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15724 add_loc_descr (&ret, l3label);
15725 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15726 add_loc_descr (&ret, l4label);
15727 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15728 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15729 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15730 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15731 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15732 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15733 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15734 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15735 return ret;
15738 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15739 const1 is DW_OP_lit1 or corresponding typed constant):
15740 const0 DW_OP_swap
15741 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15742 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15743 L2: DW_OP_drop
15745 PARITY is similar:
15746 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15747 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15748 L2: DW_OP_drop */
15750 static dw_loc_descr_ref
15751 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15752 machine_mode mem_mode)
15754 dw_loc_descr_ref op0, ret, tmp;
15755 dw_loc_descr_ref l1jump, l1label;
15756 dw_loc_descr_ref l2jump, l2label;
15758 if (GET_MODE (XEXP (rtl, 0)) != mode)
15759 return NULL;
15761 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15762 VAR_INIT_STATUS_INITIALIZED);
15763 if (op0 == NULL)
15764 return NULL;
15765 ret = op0;
15766 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15767 VAR_INIT_STATUS_INITIALIZED);
15768 if (tmp == NULL)
15769 return NULL;
15770 add_loc_descr (&ret, tmp);
15771 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15772 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15773 add_loc_descr (&ret, l1label);
15774 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15775 add_loc_descr (&ret, l2jump);
15776 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15777 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15778 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15779 VAR_INIT_STATUS_INITIALIZED);
15780 if (tmp == NULL)
15781 return NULL;
15782 add_loc_descr (&ret, tmp);
15783 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15784 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15785 ? DW_OP_plus : DW_OP_xor, 0, 0));
15786 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15787 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15788 VAR_INIT_STATUS_INITIALIZED);
15789 add_loc_descr (&ret, tmp);
15790 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15791 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15792 add_loc_descr (&ret, l1jump);
15793 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15794 add_loc_descr (&ret, l2label);
15795 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15796 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15797 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15798 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15799 return ret;
15802 /* BSWAP (constS is initial shift count, either 56 or 24):
15803 constS const0
15804 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15805 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15806 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15807 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15808 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15810 static dw_loc_descr_ref
15811 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15812 machine_mode mem_mode)
15814 dw_loc_descr_ref op0, ret, tmp;
15815 dw_loc_descr_ref l1jump, l1label;
15816 dw_loc_descr_ref l2jump, l2label;
15818 if (BITS_PER_UNIT != 8
15819 || (GET_MODE_BITSIZE (mode) != 32
15820 && GET_MODE_BITSIZE (mode) != 64))
15821 return NULL;
15823 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15824 VAR_INIT_STATUS_INITIALIZED);
15825 if (op0 == NULL)
15826 return NULL;
15828 ret = op0;
15829 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15830 mode, mem_mode,
15831 VAR_INIT_STATUS_INITIALIZED);
15832 if (tmp == NULL)
15833 return NULL;
15834 add_loc_descr (&ret, tmp);
15835 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15836 VAR_INIT_STATUS_INITIALIZED);
15837 if (tmp == NULL)
15838 return NULL;
15839 add_loc_descr (&ret, tmp);
15840 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15841 add_loc_descr (&ret, l1label);
15842 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15843 mode, mem_mode,
15844 VAR_INIT_STATUS_INITIALIZED);
15845 add_loc_descr (&ret, tmp);
15846 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15847 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15848 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15849 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15850 VAR_INIT_STATUS_INITIALIZED);
15851 if (tmp == NULL)
15852 return NULL;
15853 add_loc_descr (&ret, tmp);
15854 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15855 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15856 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15857 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15858 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15859 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15860 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15861 VAR_INIT_STATUS_INITIALIZED);
15862 add_loc_descr (&ret, tmp);
15863 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15864 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15865 add_loc_descr (&ret, l2jump);
15866 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15867 VAR_INIT_STATUS_INITIALIZED);
15868 add_loc_descr (&ret, tmp);
15869 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15870 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15871 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15872 add_loc_descr (&ret, l1jump);
15873 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15874 add_loc_descr (&ret, l2label);
15875 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15876 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15877 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15878 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15879 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15880 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15881 return ret;
15884 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15885 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15886 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15887 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15889 ROTATERT is similar:
15890 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15891 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15892 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15894 static dw_loc_descr_ref
15895 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15896 machine_mode mem_mode)
15898 rtx rtlop1 = XEXP (rtl, 1);
15899 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15900 int i;
15902 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15903 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15904 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15905 VAR_INIT_STATUS_INITIALIZED);
15906 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15907 VAR_INIT_STATUS_INITIALIZED);
15908 if (op0 == NULL || op1 == NULL)
15909 return NULL;
15910 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15911 for (i = 0; i < 2; i++)
15913 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15914 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15915 mode, mem_mode,
15916 VAR_INIT_STATUS_INITIALIZED);
15917 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15918 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15919 ? DW_OP_const4u
15920 : HOST_BITS_PER_WIDE_INT == 64
15921 ? DW_OP_const8u : DW_OP_constu,
15922 GET_MODE_MASK (mode), 0);
15923 else
15924 mask[i] = NULL;
15925 if (mask[i] == NULL)
15926 return NULL;
15927 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15929 ret = op0;
15930 add_loc_descr (&ret, op1);
15931 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15932 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15933 if (GET_CODE (rtl) == ROTATERT)
15935 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15936 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15937 GET_MODE_BITSIZE (mode), 0));
15939 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15940 if (mask[0] != NULL)
15941 add_loc_descr (&ret, mask[0]);
15942 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15943 if (mask[1] != NULL)
15945 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15946 add_loc_descr (&ret, mask[1]);
15947 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15949 if (GET_CODE (rtl) == ROTATE)
15951 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15952 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15953 GET_MODE_BITSIZE (mode), 0));
15955 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15956 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15957 return ret;
15960 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15961 for DEBUG_PARAMETER_REF RTL. */
15963 static dw_loc_descr_ref
15964 parameter_ref_descriptor (rtx rtl)
15966 dw_loc_descr_ref ret;
15967 dw_die_ref ref;
15969 if (dwarf_strict)
15970 return NULL;
15971 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15972 /* With LTO during LTRANS we get the late DIE that refers to the early
15973 DIE, thus we add another indirection here. This seems to confuse
15974 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15975 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15976 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15977 if (ref)
15979 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15980 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15981 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15983 else
15985 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15986 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15988 return ret;
15991 /* The following routine converts the RTL for a variable or parameter
15992 (resident in memory) into an equivalent Dwarf representation of a
15993 mechanism for getting the address of that same variable onto the top of a
15994 hypothetical "address evaluation" stack.
15996 When creating memory location descriptors, we are effectively transforming
15997 the RTL for a memory-resident object into its Dwarf postfix expression
15998 equivalent. This routine recursively descends an RTL tree, turning
15999 it into Dwarf postfix code as it goes.
16001 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
16003 MEM_MODE is the mode of the memory reference, needed to handle some
16004 autoincrement addressing modes.
16006 Return 0 if we can't represent the location. */
16008 dw_loc_descr_ref
16009 mem_loc_descriptor (rtx rtl, machine_mode mode,
16010 machine_mode mem_mode,
16011 enum var_init_status initialized)
16013 dw_loc_descr_ref mem_loc_result = NULL;
16014 enum dwarf_location_atom op;
16015 dw_loc_descr_ref op0, op1;
16016 rtx inner = NULL_RTX;
16018 if (mode == VOIDmode)
16019 mode = GET_MODE (rtl);
16021 /* Note that for a dynamically sized array, the location we will generate a
16022 description of here will be the lowest numbered location which is
16023 actually within the array. That's *not* necessarily the same as the
16024 zeroth element of the array. */
16026 rtl = targetm.delegitimize_address (rtl);
16028 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
16029 return NULL;
16031 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
16032 switch (GET_CODE (rtl))
16034 case POST_INC:
16035 case POST_DEC:
16036 case POST_MODIFY:
16037 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
16039 case SUBREG:
16040 /* The case of a subreg may arise when we have a local (register)
16041 variable or a formal (register) parameter which doesn't quite fill
16042 up an entire register. For now, just assume that it is
16043 legitimate to make the Dwarf info refer to the whole register which
16044 contains the given subreg. */
16045 if (!subreg_lowpart_p (rtl))
16046 break;
16047 inner = SUBREG_REG (rtl);
16048 /* FALLTHRU */
16049 case TRUNCATE:
16050 if (inner == NULL_RTX)
16051 inner = XEXP (rtl, 0);
16052 if (is_a <scalar_int_mode> (mode, &int_mode)
16053 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
16054 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16055 #ifdef POINTERS_EXTEND_UNSIGNED
16056 || (int_mode == Pmode && mem_mode != VOIDmode)
16057 #endif
16059 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
16061 mem_loc_result = mem_loc_descriptor (inner,
16062 inner_mode,
16063 mem_mode, initialized);
16064 break;
16066 if (dwarf_strict && dwarf_version < 5)
16067 break;
16068 if (is_a <scalar_int_mode> (mode, &int_mode)
16069 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
16070 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
16071 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16073 dw_die_ref type_die;
16074 dw_loc_descr_ref cvt;
16076 mem_loc_result = mem_loc_descriptor (inner,
16077 GET_MODE (inner),
16078 mem_mode, initialized);
16079 if (mem_loc_result == NULL)
16080 break;
16081 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16082 if (type_die == NULL)
16084 mem_loc_result = NULL;
16085 break;
16087 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16088 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16089 else
16090 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
16091 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16092 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16093 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16094 add_loc_descr (&mem_loc_result, cvt);
16095 if (is_a <scalar_int_mode> (mode, &int_mode)
16096 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16098 /* Convert it to untyped afterwards. */
16099 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16100 add_loc_descr (&mem_loc_result, cvt);
16103 break;
16105 case REG:
16106 if (!is_a <scalar_int_mode> (mode, &int_mode)
16107 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16108 && rtl != arg_pointer_rtx
16109 && rtl != frame_pointer_rtx
16110 #ifdef POINTERS_EXTEND_UNSIGNED
16111 && (int_mode != Pmode || mem_mode == VOIDmode)
16112 #endif
16115 dw_die_ref type_die;
16116 unsigned int debugger_regnum;
16118 if (dwarf_strict && dwarf_version < 5)
16119 break;
16120 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
16121 break;
16122 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16123 if (type_die == NULL)
16124 break;
16126 debugger_regnum = debugger_reg_number (rtl);
16127 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16128 break;
16129 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
16130 debugger_regnum, 0);
16131 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16132 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16133 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
16134 break;
16136 /* Whenever a register number forms a part of the description of the
16137 method for calculating the (dynamic) address of a memory resident
16138 object, DWARF rules require the register number be referred to as
16139 a "base register". This distinction is not based in any way upon
16140 what category of register the hardware believes the given register
16141 belongs to. This is strictly DWARF terminology we're dealing with
16142 here. Note that in cases where the location of a memory-resident
16143 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
16144 OP_CONST (0)) the actual DWARF location descriptor that we generate
16145 may just be OP_BASEREG (basereg). This may look deceptively like
16146 the object in question was allocated to a register (rather than in
16147 memory) so DWARF consumers need to be aware of the subtle
16148 distinction between OP_REG and OP_BASEREG. */
16149 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
16150 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
16151 else if (stack_realign_drap
16152 && crtl->drap_reg
16153 && crtl->args.internal_arg_pointer == rtl
16154 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
16156 /* If RTL is internal_arg_pointer, which has been optimized
16157 out, use DRAP instead. */
16158 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
16159 VAR_INIT_STATUS_INITIALIZED);
16161 break;
16163 case SIGN_EXTEND:
16164 case ZERO_EXTEND:
16165 if (!is_a <scalar_int_mode> (mode, &int_mode)
16166 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
16167 break;
16168 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16169 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16170 if (op0 == 0)
16171 break;
16172 else if (GET_CODE (rtl) == ZERO_EXTEND
16173 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16174 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
16175 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
16176 to expand zero extend as two shifts instead of
16177 masking. */
16178 && GET_MODE_SIZE (inner_mode) <= 4)
16180 mem_loc_result = op0;
16181 add_loc_descr (&mem_loc_result,
16182 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
16183 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
16185 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16187 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
16188 shift *= BITS_PER_UNIT;
16189 if (GET_CODE (rtl) == SIGN_EXTEND)
16190 op = DW_OP_shra;
16191 else
16192 op = DW_OP_shr;
16193 mem_loc_result = op0;
16194 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16195 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16196 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16197 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16199 else if (!dwarf_strict || dwarf_version >= 5)
16201 dw_die_ref type_die1, type_die2;
16202 dw_loc_descr_ref cvt;
16204 type_die1 = base_type_for_mode (inner_mode,
16205 GET_CODE (rtl) == ZERO_EXTEND);
16206 if (type_die1 == NULL)
16207 break;
16208 type_die2 = base_type_for_mode (int_mode, 1);
16209 if (type_die2 == NULL)
16210 break;
16211 mem_loc_result = op0;
16212 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16213 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16214 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16215 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16216 add_loc_descr (&mem_loc_result, cvt);
16217 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16218 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16219 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16220 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16221 add_loc_descr (&mem_loc_result, cvt);
16223 break;
16225 case MEM:
16227 rtx new_rtl = avoid_constant_pool_reference (rtl);
16228 if (new_rtl != rtl)
16230 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16231 initialized);
16232 if (mem_loc_result != NULL)
16233 return mem_loc_result;
16236 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16237 get_address_mode (rtl), mode,
16238 VAR_INIT_STATUS_INITIALIZED);
16239 if (mem_loc_result == NULL)
16240 mem_loc_result = tls_mem_loc_descriptor (rtl);
16241 if (mem_loc_result != NULL)
16243 if (!is_a <scalar_int_mode> (mode, &int_mode)
16244 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16246 dw_die_ref type_die;
16247 dw_loc_descr_ref deref;
16248 HOST_WIDE_INT size;
16250 if (dwarf_strict && dwarf_version < 5)
16251 return NULL;
16252 if (!GET_MODE_SIZE (mode).is_constant (&size))
16253 return NULL;
16254 type_die
16255 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16256 if (type_die == NULL)
16257 return NULL;
16258 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16259 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16260 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16261 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16262 add_loc_descr (&mem_loc_result, deref);
16264 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16265 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16266 else
16267 add_loc_descr (&mem_loc_result,
16268 new_loc_descr (DW_OP_deref_size,
16269 GET_MODE_SIZE (int_mode), 0));
16271 break;
16273 case LO_SUM:
16274 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16276 case LABEL_REF:
16277 /* Some ports can transform a symbol ref into a label ref, because
16278 the symbol ref is too far away and has to be dumped into a constant
16279 pool. */
16280 case CONST:
16281 case SYMBOL_REF:
16282 case UNSPEC:
16283 if (!is_a <scalar_int_mode> (mode, &int_mode)
16284 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16285 #ifdef POINTERS_EXTEND_UNSIGNED
16286 && (int_mode != Pmode || mem_mode == VOIDmode)
16287 #endif
16289 break;
16291 if (GET_CODE (rtl) == UNSPEC)
16293 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16294 can't express it in the debug info. This can happen e.g. with some
16295 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16296 approves. */
16297 bool not_ok = false;
16298 subrtx_var_iterator::array_type array;
16299 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16300 if (*iter != rtl && !CONSTANT_P (*iter))
16302 not_ok = true;
16303 break;
16306 if (not_ok)
16307 break;
16309 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16310 if (!const_ok_for_output_1 (*iter))
16312 not_ok = true;
16313 break;
16316 if (not_ok)
16317 break;
16319 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16320 goto symref;
16323 if (GET_CODE (rtl) == SYMBOL_REF
16324 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16326 dw_loc_descr_ref temp;
16328 /* If this is not defined, we have no way to emit the data. */
16329 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16330 break;
16332 temp = new_addr_loc_descr (rtl, dtprel_true);
16334 /* We check for DWARF 5 here because gdb did not implement
16335 DW_OP_form_tls_address until after 7.12. */
16336 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16337 ? DW_OP_form_tls_address
16338 : DW_OP_GNU_push_tls_address),
16339 0, 0);
16340 add_loc_descr (&mem_loc_result, temp);
16342 break;
16345 if (!const_ok_for_output (rtl))
16347 if (GET_CODE (rtl) == CONST)
16348 switch (GET_CODE (XEXP (rtl, 0)))
16350 case NOT:
16351 op = DW_OP_not;
16352 goto try_const_unop;
16353 case NEG:
16354 op = DW_OP_neg;
16355 goto try_const_unop;
16356 try_const_unop:
16357 rtx arg;
16358 arg = XEXP (XEXP (rtl, 0), 0);
16359 if (!CONSTANT_P (arg))
16360 arg = gen_rtx_CONST (int_mode, arg);
16361 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16362 initialized);
16363 if (op0)
16365 mem_loc_result = op0;
16366 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16368 break;
16369 default:
16370 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16371 mem_mode, initialized);
16372 break;
16374 break;
16377 symref:
16378 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16379 vec_safe_push (used_rtx_array, rtl);
16380 break;
16382 case CONCAT:
16383 case CONCATN:
16384 case VAR_LOCATION:
16385 case DEBUG_IMPLICIT_PTR:
16386 expansion_failed (NULL_TREE, rtl,
16387 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16388 return 0;
16390 case ENTRY_VALUE:
16391 if (dwarf_strict && dwarf_version < 5)
16392 return NULL;
16393 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16395 if (!is_a <scalar_int_mode> (mode, &int_mode)
16396 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16397 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16398 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16399 else
16401 unsigned int debugger_regnum = debugger_reg_number (ENTRY_VALUE_EXP (rtl));
16402 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16403 return NULL;
16404 op0 = one_reg_loc_descriptor (debugger_regnum,
16405 VAR_INIT_STATUS_INITIALIZED);
16408 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16409 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16411 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16412 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16413 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16414 return NULL;
16416 else
16417 gcc_unreachable ();
16418 if (op0 == NULL)
16419 return NULL;
16420 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16421 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16422 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16423 break;
16425 case DEBUG_PARAMETER_REF:
16426 mem_loc_result = parameter_ref_descriptor (rtl);
16427 break;
16429 case PRE_MODIFY:
16430 /* Extract the PLUS expression nested inside and fall into
16431 PLUS code below. */
16432 rtl = XEXP (rtl, 1);
16433 goto plus;
16435 case PRE_INC:
16436 case PRE_DEC:
16437 /* Turn these into a PLUS expression and fall into the PLUS code
16438 below. */
16439 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16440 gen_int_mode (GET_CODE (rtl) == PRE_INC
16441 ? GET_MODE_UNIT_SIZE (mem_mode)
16442 : -GET_MODE_UNIT_SIZE (mem_mode),
16443 mode));
16445 /* fall through */
16447 case PLUS:
16448 plus:
16449 if (is_based_loc (rtl)
16450 && is_a <scalar_int_mode> (mode, &int_mode)
16451 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16452 || XEXP (rtl, 0) == arg_pointer_rtx
16453 || XEXP (rtl, 0) == frame_pointer_rtx))
16454 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16455 INTVAL (XEXP (rtl, 1)),
16456 VAR_INIT_STATUS_INITIALIZED);
16457 else
16459 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16460 VAR_INIT_STATUS_INITIALIZED);
16461 if (mem_loc_result == 0)
16462 break;
16464 if (CONST_INT_P (XEXP (rtl, 1))
16465 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16466 <= DWARF2_ADDR_SIZE))
16467 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16468 else
16470 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16471 VAR_INIT_STATUS_INITIALIZED);
16472 if (op1 == 0)
16473 return NULL;
16474 add_loc_descr (&mem_loc_result, op1);
16475 add_loc_descr (&mem_loc_result,
16476 new_loc_descr (DW_OP_plus, 0, 0));
16479 break;
16481 /* If a pseudo-reg is optimized away, it is possible for it to
16482 be replaced with a MEM containing a multiply or shift. */
16483 case MINUS:
16484 op = DW_OP_minus;
16485 goto do_binop;
16487 case MULT:
16488 op = DW_OP_mul;
16489 goto do_binop;
16491 case DIV:
16492 if ((!dwarf_strict || dwarf_version >= 5)
16493 && is_a <scalar_int_mode> (mode, &int_mode)
16494 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16496 mem_loc_result = typed_binop (DW_OP_div, rtl,
16497 base_type_for_mode (mode, 0),
16498 int_mode, mem_mode);
16499 break;
16501 op = DW_OP_div;
16502 goto do_binop;
16504 case UMOD:
16505 op = DW_OP_mod;
16506 goto do_binop;
16508 case ASHIFT:
16509 op = DW_OP_shl;
16510 goto do_shift;
16512 case ASHIFTRT:
16513 op = DW_OP_shra;
16514 goto do_shift;
16516 case LSHIFTRT:
16517 op = DW_OP_shr;
16518 goto do_shift;
16520 do_shift:
16521 if (!is_a <scalar_int_mode> (mode, &int_mode))
16522 break;
16523 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16524 VAR_INIT_STATUS_INITIALIZED);
16526 rtx rtlop1 = XEXP (rtl, 1);
16527 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16528 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16529 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16530 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16531 VAR_INIT_STATUS_INITIALIZED);
16534 if (op0 == 0 || op1 == 0)
16535 break;
16537 mem_loc_result = op0;
16538 add_loc_descr (&mem_loc_result, op1);
16539 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16540 break;
16542 case AND:
16543 op = DW_OP_and;
16544 goto do_binop;
16546 case IOR:
16547 op = DW_OP_or;
16548 goto do_binop;
16550 case XOR:
16551 op = DW_OP_xor;
16552 goto do_binop;
16554 do_binop:
16555 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16556 VAR_INIT_STATUS_INITIALIZED);
16557 if (XEXP (rtl, 0) == XEXP (rtl, 1))
16559 if (op0 == 0)
16560 break;
16561 mem_loc_result = op0;
16562 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_dup, 0, 0));
16563 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16564 break;
16566 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16567 VAR_INIT_STATUS_INITIALIZED);
16569 if (op0 == 0 || op1 == 0)
16570 break;
16572 mem_loc_result = op0;
16573 add_loc_descr (&mem_loc_result, op1);
16574 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16575 break;
16577 case MOD:
16578 if ((!dwarf_strict || dwarf_version >= 5)
16579 && is_a <scalar_int_mode> (mode, &int_mode)
16580 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16582 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16583 base_type_for_mode (mode, 0),
16584 int_mode, mem_mode);
16585 break;
16588 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16589 VAR_INIT_STATUS_INITIALIZED);
16590 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16591 VAR_INIT_STATUS_INITIALIZED);
16593 if (op0 == 0 || op1 == 0)
16594 break;
16596 mem_loc_result = op0;
16597 add_loc_descr (&mem_loc_result, op1);
16598 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16599 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16600 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16601 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16602 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16603 break;
16605 case UDIV:
16606 if ((!dwarf_strict || dwarf_version >= 5)
16607 && is_a <scalar_int_mode> (mode, &int_mode))
16609 /* We can use a signed divide if the sign bit is not set. */
16610 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16612 op = DW_OP_div;
16613 goto do_binop;
16616 mem_loc_result = typed_binop (DW_OP_div, rtl,
16617 base_type_for_mode (int_mode, 1),
16618 int_mode, mem_mode);
16620 break;
16622 case NOT:
16623 op = DW_OP_not;
16624 goto do_unop;
16626 case ABS:
16627 op = DW_OP_abs;
16628 goto do_unop;
16630 case NEG:
16631 op = DW_OP_neg;
16632 goto do_unop;
16634 do_unop:
16635 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16636 VAR_INIT_STATUS_INITIALIZED);
16638 if (op0 == 0)
16639 break;
16641 mem_loc_result = op0;
16642 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16643 break;
16645 case CONST_INT:
16646 if (!is_a <scalar_int_mode> (mode, &int_mode)
16647 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16648 #ifdef POINTERS_EXTEND_UNSIGNED
16649 || (int_mode == Pmode
16650 && mem_mode != VOIDmode
16651 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16652 #endif
16655 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16656 break;
16658 if ((!dwarf_strict || dwarf_version >= 5)
16659 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16660 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16662 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16663 scalar_int_mode amode;
16664 if (type_die == NULL)
16665 return NULL;
16666 if (INTVAL (rtl) >= 0
16667 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16668 .exists (&amode))
16669 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16670 /* const DW_OP_convert <XXX> vs.
16671 DW_OP_const_type <XXX, 1, const>. */
16672 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16673 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16675 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16676 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16677 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16678 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16679 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16680 add_loc_descr (&mem_loc_result, op0);
16681 return mem_loc_result;
16683 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16684 INTVAL (rtl));
16685 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16686 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16687 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16688 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16689 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16690 else
16692 mem_loc_result->dw_loc_oprnd2.val_class
16693 = dw_val_class_const_double;
16694 mem_loc_result->dw_loc_oprnd2.v.val_double
16695 = double_int::from_shwi (INTVAL (rtl));
16698 break;
16700 case CONST_DOUBLE:
16701 if (!dwarf_strict || dwarf_version >= 5)
16703 dw_die_ref type_die;
16705 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16706 CONST_DOUBLE rtx could represent either a large integer
16707 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16708 the value is always a floating point constant.
16710 When it is an integer, a CONST_DOUBLE is used whenever
16711 the constant requires 2 HWIs to be adequately represented.
16712 We output CONST_DOUBLEs as blocks. */
16713 if (mode == VOIDmode
16714 || (GET_MODE (rtl) == VOIDmode
16715 && maybe_ne (GET_MODE_BITSIZE (mode),
16716 HOST_BITS_PER_DOUBLE_INT)))
16717 break;
16718 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16719 if (type_die == NULL)
16720 return NULL;
16721 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16722 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16723 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16724 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16725 #if TARGET_SUPPORTS_WIDE_INT == 0
16726 if (!SCALAR_FLOAT_MODE_P (mode))
16728 mem_loc_result->dw_loc_oprnd2.val_class
16729 = dw_val_class_const_double;
16730 mem_loc_result->dw_loc_oprnd2.v.val_double
16731 = rtx_to_double_int (rtl);
16733 else
16734 #endif
16736 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16737 unsigned int length = GET_MODE_SIZE (float_mode);
16738 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16739 unsigned int elt_size = insert_float (rtl, array);
16741 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16742 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16743 = length / elt_size;
16744 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16745 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16748 break;
16750 case CONST_WIDE_INT:
16751 if (!dwarf_strict || dwarf_version >= 5)
16753 dw_die_ref type_die;
16755 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16756 if (type_die == NULL)
16757 return NULL;
16758 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16759 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16760 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16761 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16762 mem_loc_result->dw_loc_oprnd2.val_class
16763 = dw_val_class_wide_int;
16764 mem_loc_result->dw_loc_oprnd2.v.val_wide
16765 = alloc_dw_wide_int (rtx_mode_t (rtl, mode));
16767 break;
16769 case CONST_POLY_INT:
16770 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16771 break;
16773 case EQ:
16774 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16775 break;
16777 case GE:
16778 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16779 break;
16781 case GT:
16782 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16783 break;
16785 case LE:
16786 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16787 break;
16789 case LT:
16790 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16791 break;
16793 case NE:
16794 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16795 break;
16797 case GEU:
16798 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16799 break;
16801 case GTU:
16802 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16803 break;
16805 case LEU:
16806 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16807 break;
16809 case LTU:
16810 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16811 break;
16813 case UMIN:
16814 case UMAX:
16815 if (!SCALAR_INT_MODE_P (mode))
16816 break;
16817 /* FALLTHRU */
16818 case SMIN:
16819 case SMAX:
16820 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16821 break;
16823 case ZERO_EXTRACT:
16824 case SIGN_EXTRACT:
16825 if (CONST_INT_P (XEXP (rtl, 1))
16826 && CONST_INT_P (XEXP (rtl, 2))
16827 && is_a <scalar_int_mode> (mode, &int_mode)
16828 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16829 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16830 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16831 && ((unsigned) INTVAL (XEXP (rtl, 1))
16832 + (unsigned) INTVAL (XEXP (rtl, 2))
16833 <= GET_MODE_BITSIZE (int_mode)))
16835 int shift, size;
16836 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16837 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16838 if (op0 == 0)
16839 break;
16840 if (GET_CODE (rtl) == SIGN_EXTRACT)
16841 op = DW_OP_shra;
16842 else
16843 op = DW_OP_shr;
16844 mem_loc_result = op0;
16845 size = INTVAL (XEXP (rtl, 1));
16846 shift = INTVAL (XEXP (rtl, 2));
16847 if (BITS_BIG_ENDIAN)
16848 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16849 if (shift + size != (int) DWARF2_ADDR_SIZE)
16851 add_loc_descr (&mem_loc_result,
16852 int_loc_descriptor (DWARF2_ADDR_SIZE
16853 - shift - size));
16854 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16856 if (size != (int) DWARF2_ADDR_SIZE)
16858 add_loc_descr (&mem_loc_result,
16859 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16860 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16863 break;
16865 case IF_THEN_ELSE:
16867 dw_loc_descr_ref op2, bra_node, drop_node;
16868 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16869 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16870 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16871 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16872 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16873 VAR_INIT_STATUS_INITIALIZED);
16874 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16875 VAR_INIT_STATUS_INITIALIZED);
16876 if (op0 == NULL || op1 == NULL || op2 == NULL)
16877 break;
16879 mem_loc_result = op1;
16880 add_loc_descr (&mem_loc_result, op2);
16881 add_loc_descr (&mem_loc_result, op0);
16882 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16883 add_loc_descr (&mem_loc_result, bra_node);
16884 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16885 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16886 add_loc_descr (&mem_loc_result, drop_node);
16887 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16888 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16890 break;
16892 case FLOAT_EXTEND:
16893 case FLOAT_TRUNCATE:
16894 case FLOAT:
16895 case UNSIGNED_FLOAT:
16896 case FIX:
16897 case UNSIGNED_FIX:
16898 if (!dwarf_strict || dwarf_version >= 5)
16900 dw_die_ref type_die;
16901 dw_loc_descr_ref cvt;
16903 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16904 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16905 if (op0 == NULL)
16906 break;
16907 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16908 && (GET_CODE (rtl) == FLOAT
16909 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16911 type_die = base_type_for_mode (int_mode,
16912 GET_CODE (rtl) == UNSIGNED_FLOAT);
16913 if (type_die == NULL)
16914 break;
16915 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16916 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16917 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16918 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16919 add_loc_descr (&op0, cvt);
16921 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16922 if (type_die == NULL)
16923 break;
16924 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16925 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16926 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16927 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16928 add_loc_descr (&op0, cvt);
16929 if (is_a <scalar_int_mode> (mode, &int_mode)
16930 && (GET_CODE (rtl) == FIX
16931 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16933 op0 = convert_descriptor_to_mode (int_mode, op0);
16934 if (op0 == NULL)
16935 break;
16937 mem_loc_result = op0;
16939 break;
16941 case CLZ:
16942 case CTZ:
16943 case FFS:
16944 if (is_a <scalar_int_mode> (mode, &int_mode))
16945 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16946 break;
16948 case POPCOUNT:
16949 case PARITY:
16950 if (is_a <scalar_int_mode> (mode, &int_mode))
16951 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16952 break;
16954 case BSWAP:
16955 if (is_a <scalar_int_mode> (mode, &int_mode))
16956 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16957 break;
16959 case ROTATE:
16960 case ROTATERT:
16961 if (is_a <scalar_int_mode> (mode, &int_mode))
16962 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16963 break;
16965 case COMPARE:
16966 /* In theory, we could implement the above. */
16967 /* DWARF cannot represent the unsigned compare operations
16968 natively. */
16969 case SS_MULT:
16970 case US_MULT:
16971 case SS_DIV:
16972 case US_DIV:
16973 case SS_PLUS:
16974 case US_PLUS:
16975 case SS_MINUS:
16976 case US_MINUS:
16977 case SS_NEG:
16978 case US_NEG:
16979 case SS_ABS:
16980 case SS_ASHIFT:
16981 case US_ASHIFT:
16982 case SS_TRUNCATE:
16983 case US_TRUNCATE:
16984 case UNORDERED:
16985 case ORDERED:
16986 case UNEQ:
16987 case UNGE:
16988 case UNGT:
16989 case UNLE:
16990 case UNLT:
16991 case LTGT:
16992 case FRACT_CONVERT:
16993 case UNSIGNED_FRACT_CONVERT:
16994 case SAT_FRACT:
16995 case UNSIGNED_SAT_FRACT:
16996 case SQRT:
16997 case ASM_OPERANDS:
16998 case VEC_MERGE:
16999 case VEC_SELECT:
17000 case VEC_CONCAT:
17001 case VEC_DUPLICATE:
17002 case VEC_SERIES:
17003 case HIGH:
17004 case FMA:
17005 case STRICT_LOW_PART:
17006 case CONST_VECTOR:
17007 case CONST_FIXED:
17008 case CLRSB:
17009 case CLOBBER:
17010 case SMUL_HIGHPART:
17011 case UMUL_HIGHPART:
17012 case BITREVERSE:
17013 case COPYSIGN:
17014 break;
17016 case CONST_STRING:
17017 resolve_one_addr (&rtl);
17018 goto symref;
17020 /* RTL sequences inside PARALLEL record a series of DWARF operations for
17021 the expression. An UNSPEC rtx represents a raw DWARF operation,
17022 new_loc_descr is called for it to build the operation directly.
17023 Otherwise mem_loc_descriptor is called recursively. */
17024 case PARALLEL:
17026 int index = 0;
17027 dw_loc_descr_ref exp_result = NULL;
17029 for (; index < XVECLEN (rtl, 0); index++)
17031 rtx elem = XVECEXP (rtl, 0, index);
17032 if (GET_CODE (elem) == UNSPEC)
17034 /* Each DWARF operation UNSPEC contain two operands, if
17035 one operand is not used for the operation, const0_rtx is
17036 passed. */
17037 gcc_assert (XVECLEN (elem, 0) == 2);
17039 HOST_WIDE_INT dw_op = XINT (elem, 1);
17040 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
17041 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
17042 exp_result
17043 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
17044 oprnd2);
17046 else
17047 exp_result
17048 = mem_loc_descriptor (elem, mode, mem_mode,
17049 VAR_INIT_STATUS_INITIALIZED);
17051 if (!mem_loc_result)
17052 mem_loc_result = exp_result;
17053 else
17054 add_loc_descr (&mem_loc_result, exp_result);
17057 break;
17060 default:
17061 if (flag_checking)
17063 print_rtl (stderr, rtl);
17064 gcc_unreachable ();
17066 break;
17069 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17070 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17072 return mem_loc_result;
17075 /* Return a descriptor that describes the concatenation of two locations.
17076 This is typically a complex variable. */
17078 static dw_loc_descr_ref
17079 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
17081 /* At present we only track constant-sized pieces. */
17082 unsigned int size0, size1;
17083 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
17084 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
17085 return 0;
17087 dw_loc_descr_ref cc_loc_result = NULL;
17088 dw_loc_descr_ref x0_ref
17089 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17090 dw_loc_descr_ref x1_ref
17091 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17093 if (x0_ref == 0 || x1_ref == 0)
17094 return 0;
17096 cc_loc_result = x0_ref;
17097 add_loc_descr_op_piece (&cc_loc_result, size0);
17099 add_loc_descr (&cc_loc_result, x1_ref);
17100 add_loc_descr_op_piece (&cc_loc_result, size1);
17102 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
17103 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17105 return cc_loc_result;
17108 /* Return a descriptor that describes the concatenation of N
17109 locations. */
17111 static dw_loc_descr_ref
17112 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
17114 unsigned int i;
17115 dw_loc_descr_ref cc_loc_result = NULL;
17116 unsigned int n = XVECLEN (concatn, 0);
17117 unsigned int size;
17119 for (i = 0; i < n; ++i)
17121 dw_loc_descr_ref ref;
17122 rtx x = XVECEXP (concatn, 0, i);
17124 /* At present we only track constant-sized pieces. */
17125 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
17126 return NULL;
17128 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17129 if (ref == NULL)
17130 return NULL;
17132 add_loc_descr (&cc_loc_result, ref);
17133 add_loc_descr_op_piece (&cc_loc_result, size);
17136 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17137 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17139 return cc_loc_result;
17142 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
17143 for DEBUG_IMPLICIT_PTR RTL. */
17145 static dw_loc_descr_ref
17146 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
17148 dw_loc_descr_ref ret;
17149 dw_die_ref ref;
17151 if (dwarf_strict && dwarf_version < 5)
17152 return NULL;
17153 gcc_assert (VAR_P (DEBUG_IMPLICIT_PTR_DECL (rtl))
17154 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
17155 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
17156 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
17157 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
17158 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
17159 if (ref)
17161 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17162 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17163 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17165 else
17167 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17168 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
17170 return ret;
17173 /* Output a proper Dwarf location descriptor for a variable or parameter
17174 which is either allocated in a register or in a memory location. For a
17175 register, we just generate an OP_REG and the register number. For a
17176 memory location we provide a Dwarf postfix expression describing how to
17177 generate the (dynamic) address of the object onto the address stack.
17179 MODE is mode of the decl if this loc_descriptor is going to be used in
17180 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
17181 allowed, VOIDmode otherwise.
17183 If we don't know how to describe it, return 0. */
17185 static dw_loc_descr_ref
17186 loc_descriptor (rtx rtl, machine_mode mode,
17187 enum var_init_status initialized)
17189 dw_loc_descr_ref loc_result = NULL;
17190 scalar_int_mode int_mode;
17192 switch (GET_CODE (rtl))
17194 case SUBREG:
17195 /* The case of a subreg may arise when we have a local (register)
17196 variable or a formal (register) parameter which doesn't quite fill
17197 up an entire register. For now, just assume that it is
17198 legitimate to make the Dwarf info refer to the whole register which
17199 contains the given subreg. */
17200 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
17201 loc_result = loc_descriptor (SUBREG_REG (rtl),
17202 GET_MODE (SUBREG_REG (rtl)), initialized);
17203 else
17204 goto do_default;
17205 break;
17207 case REG:
17208 loc_result = reg_loc_descriptor (rtl, initialized);
17209 break;
17211 case MEM:
17212 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17213 GET_MODE (rtl), initialized);
17214 if (loc_result == NULL)
17215 loc_result = tls_mem_loc_descriptor (rtl);
17216 if (loc_result == NULL)
17218 rtx new_rtl = avoid_constant_pool_reference (rtl);
17219 if (new_rtl != rtl)
17220 loc_result = loc_descriptor (new_rtl, mode, initialized);
17222 break;
17224 case CONCAT:
17225 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17226 initialized);
17227 break;
17229 case CONCATN:
17230 loc_result = concatn_loc_descriptor (rtl, initialized);
17231 break;
17233 case VAR_LOCATION:
17234 /* Single part. */
17235 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17237 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17238 if (GET_CODE (loc) == EXPR_LIST)
17239 loc = XEXP (loc, 0);
17240 loc_result = loc_descriptor (loc, mode, initialized);
17241 break;
17244 rtl = XEXP (rtl, 1);
17245 /* FALLTHRU */
17247 case PARALLEL:
17249 rtvec par_elems = XVEC (rtl, 0);
17250 int num_elem = GET_NUM_ELEM (par_elems);
17251 machine_mode mode;
17252 int i, size;
17254 /* Create the first one, so we have something to add to. */
17255 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17256 VOIDmode, initialized);
17257 if (loc_result == NULL)
17258 return NULL;
17259 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17260 /* At present we only track constant-sized pieces. */
17261 if (!GET_MODE_SIZE (mode).is_constant (&size))
17262 return NULL;
17263 add_loc_descr_op_piece (&loc_result, size);
17264 for (i = 1; i < num_elem; i++)
17266 dw_loc_descr_ref temp;
17268 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17269 VOIDmode, initialized);
17270 if (temp == NULL)
17271 return NULL;
17272 add_loc_descr (&loc_result, temp);
17273 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17274 /* At present we only track constant-sized pieces. */
17275 if (!GET_MODE_SIZE (mode).is_constant (&size))
17276 return NULL;
17277 add_loc_descr_op_piece (&loc_result, size);
17280 break;
17282 case CONST_INT:
17283 if (mode != VOIDmode && mode != BLKmode)
17285 int_mode = as_a <scalar_int_mode> (mode);
17286 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17287 INTVAL (rtl));
17289 break;
17291 case CONST_DOUBLE:
17292 if (mode == VOIDmode)
17293 mode = GET_MODE (rtl);
17295 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17297 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17299 /* Note that a CONST_DOUBLE rtx could represent either an integer
17300 or a floating-point constant. A CONST_DOUBLE is used whenever
17301 the constant requires more than one word in order to be
17302 adequately represented. We output CONST_DOUBLEs as blocks. */
17303 scalar_mode smode = as_a <scalar_mode> (mode);
17304 loc_result = new_loc_descr (DW_OP_implicit_value,
17305 GET_MODE_SIZE (smode), 0);
17306 #if TARGET_SUPPORTS_WIDE_INT == 0
17307 if (!SCALAR_FLOAT_MODE_P (smode))
17309 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17310 loc_result->dw_loc_oprnd2.v.val_double
17311 = rtx_to_double_int (rtl);
17313 else
17314 #endif
17316 unsigned int length = GET_MODE_SIZE (smode);
17317 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17318 unsigned int elt_size = insert_float (rtl, array);
17320 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17321 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17322 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17323 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17326 break;
17328 case CONST_WIDE_INT:
17329 if (mode == VOIDmode)
17330 mode = GET_MODE (rtl);
17332 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17334 int_mode = as_a <scalar_int_mode> (mode);
17335 loc_result = new_loc_descr (DW_OP_implicit_value,
17336 GET_MODE_SIZE (int_mode), 0);
17337 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17338 loc_result->dw_loc_oprnd2.v.val_wide
17339 = alloc_dw_wide_int (rtx_mode_t (rtl, int_mode));
17341 break;
17343 case CONST_VECTOR:
17344 if (mode == VOIDmode)
17345 mode = GET_MODE (rtl);
17347 if (mode != VOIDmode
17348 /* The combination of a length and byte elt_size doesn't extend
17349 naturally to boolean vectors, where several elements are packed
17350 into the same byte. */
17351 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17352 && (dwarf_version >= 4 || !dwarf_strict))
17354 unsigned int length;
17355 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17356 return NULL;
17358 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17359 unsigned char *array
17360 = ggc_vec_alloc<unsigned char> (length * elt_size);
17361 unsigned int i;
17362 unsigned char *p;
17363 machine_mode imode = GET_MODE_INNER (mode);
17365 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17366 switch (GET_MODE_CLASS (mode))
17368 case MODE_VECTOR_INT:
17369 for (i = 0, p = array; i < length; i++, p += elt_size)
17371 rtx elt = CONST_VECTOR_ELT (rtl, i);
17372 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17374 break;
17376 case MODE_VECTOR_FLOAT:
17377 for (i = 0, p = array; i < length; i++, p += elt_size)
17379 rtx elt = CONST_VECTOR_ELT (rtl, i);
17380 insert_float (elt, p);
17382 break;
17384 default:
17385 gcc_unreachable ();
17388 loc_result = new_loc_descr (DW_OP_implicit_value,
17389 length * elt_size, 0);
17390 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17391 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17392 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17393 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17395 break;
17397 case CONST:
17398 if (mode == VOIDmode
17399 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17400 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17401 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17403 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17404 break;
17406 /* FALLTHROUGH */
17407 case SYMBOL_REF:
17408 if (!const_ok_for_output (rtl))
17409 break;
17410 /* FALLTHROUGH */
17411 case LABEL_REF:
17412 if (is_a <scalar_int_mode> (mode, &int_mode)
17413 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17414 && (dwarf_version >= 4 || !dwarf_strict))
17416 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17417 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17418 vec_safe_push (used_rtx_array, rtl);
17420 break;
17422 case DEBUG_IMPLICIT_PTR:
17423 loc_result = implicit_ptr_descriptor (rtl, 0);
17424 break;
17426 case PLUS:
17427 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17428 && CONST_INT_P (XEXP (rtl, 1)))
17430 loc_result
17431 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17432 break;
17434 /* FALLTHRU */
17435 do_default:
17436 default:
17437 if ((is_a <scalar_int_mode> (mode, &int_mode)
17438 && GET_MODE (rtl) == int_mode
17439 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17440 && dwarf_version >= 4)
17441 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17443 /* Value expression. */
17444 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17445 if (loc_result)
17446 add_loc_descr (&loc_result,
17447 new_loc_descr (DW_OP_stack_value, 0, 0));
17449 break;
17452 return loc_result;
17455 /* We need to figure out what section we should use as the base for the
17456 address ranges where a given location is valid.
17457 1. If this particular DECL has a section associated with it, use that.
17458 2. If this function has a section associated with it, use that.
17459 3. Otherwise, use the text section.
17460 XXX: If you split a variable across multiple sections, we won't notice. */
17462 static const char *
17463 secname_for_decl (const_tree decl)
17465 const char *secname;
17467 if (VAR_OR_FUNCTION_DECL_P (decl)
17468 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17469 && DECL_SECTION_NAME (decl))
17470 secname = DECL_SECTION_NAME (decl);
17471 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17473 if (in_cold_section_p)
17475 section *sec = current_function_section ();
17476 if (sec->common.flags & SECTION_NAMED)
17477 return sec->named.name;
17479 secname = DECL_SECTION_NAME (current_function_decl);
17481 else if (cfun && in_cold_section_p)
17482 secname = crtl->subsections.cold_section_label;
17483 else
17484 secname = text_section_label;
17486 return secname;
17489 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17491 static bool
17492 decl_by_reference_p (tree decl)
17494 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17495 || VAR_P (decl))
17496 && DECL_BY_REFERENCE (decl));
17499 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17500 for VARLOC. */
17502 static dw_loc_descr_ref
17503 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17504 enum var_init_status initialized)
17506 int have_address = 0;
17507 dw_loc_descr_ref descr;
17508 machine_mode mode;
17510 if (want_address != 2)
17512 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17513 /* Single part. */
17514 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17516 varloc = PAT_VAR_LOCATION_LOC (varloc);
17517 if (GET_CODE (varloc) == EXPR_LIST)
17518 varloc = XEXP (varloc, 0);
17519 mode = GET_MODE (varloc);
17520 if (MEM_P (varloc))
17522 rtx addr = XEXP (varloc, 0);
17523 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17524 mode, initialized);
17525 if (descr)
17526 have_address = 1;
17527 else
17529 rtx x = avoid_constant_pool_reference (varloc);
17530 if (x != varloc)
17531 descr = mem_loc_descriptor (x, mode, VOIDmode,
17532 initialized);
17535 else
17536 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17538 else
17539 return 0;
17541 else
17543 if (GET_CODE (varloc) == VAR_LOCATION)
17544 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17545 else
17546 mode = DECL_MODE (loc);
17547 descr = loc_descriptor (varloc, mode, initialized);
17548 have_address = 1;
17551 if (!descr)
17552 return 0;
17554 if (want_address == 2 && !have_address
17555 && (dwarf_version >= 4 || !dwarf_strict))
17557 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17559 expansion_failed (loc, NULL_RTX,
17560 "DWARF address size mismatch");
17561 return 0;
17563 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17564 have_address = 1;
17566 /* Show if we can't fill the request for an address. */
17567 if (want_address && !have_address)
17569 expansion_failed (loc, NULL_RTX,
17570 "Want address and only have value");
17571 return 0;
17574 /* If we've got an address and don't want one, dereference. */
17575 if (!want_address && have_address)
17577 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17578 enum dwarf_location_atom op;
17580 if (size > DWARF2_ADDR_SIZE || size == -1)
17582 expansion_failed (loc, NULL_RTX,
17583 "DWARF address size mismatch");
17584 return 0;
17586 else if (size == DWARF2_ADDR_SIZE)
17587 op = DW_OP_deref;
17588 else
17589 op = DW_OP_deref_size;
17591 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17594 return descr;
17597 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17598 if it is not possible. */
17600 static dw_loc_descr_ref
17601 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17603 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17604 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17605 else if (dwarf_version >= 3 || !dwarf_strict)
17606 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17607 else
17608 return NULL;
17611 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17612 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17614 static dw_loc_descr_ref
17615 dw_sra_loc_expr (tree decl, rtx loc)
17617 rtx p;
17618 unsigned HOST_WIDE_INT padsize = 0;
17619 dw_loc_descr_ref descr, *descr_tail;
17620 unsigned HOST_WIDE_INT decl_size;
17621 rtx varloc;
17622 enum var_init_status initialized;
17624 if (DECL_SIZE (decl) == NULL
17625 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17626 return NULL;
17628 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17629 descr = NULL;
17630 descr_tail = &descr;
17632 for (p = loc; p; p = XEXP (p, 1))
17634 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17635 rtx loc_note = *decl_piece_varloc_ptr (p);
17636 dw_loc_descr_ref cur_descr;
17637 dw_loc_descr_ref *tail, last = NULL;
17638 unsigned HOST_WIDE_INT opsize = 0;
17640 if (loc_note == NULL_RTX
17641 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17643 padsize += bitsize;
17644 continue;
17646 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17647 varloc = NOTE_VAR_LOCATION (loc_note);
17648 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17649 if (cur_descr == NULL)
17651 padsize += bitsize;
17652 continue;
17655 /* Check that cur_descr either doesn't use
17656 DW_OP_*piece operations, or their sum is equal
17657 to bitsize. Otherwise we can't embed it. */
17658 for (tail = &cur_descr; *tail != NULL;
17659 tail = &(*tail)->dw_loc_next)
17660 if ((*tail)->dw_loc_opc == DW_OP_piece)
17662 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17663 * BITS_PER_UNIT;
17664 last = *tail;
17666 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17668 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17669 last = *tail;
17672 if (last != NULL && opsize != bitsize)
17674 padsize += bitsize;
17675 /* Discard the current piece of the descriptor and release any
17676 addr_table entries it uses. */
17677 remove_loc_list_addr_table_entries (cur_descr);
17678 continue;
17681 /* If there is a hole, add DW_OP_*piece after empty DWARF
17682 expression, which means that those bits are optimized out. */
17683 if (padsize)
17685 if (padsize > decl_size)
17687 remove_loc_list_addr_table_entries (cur_descr);
17688 goto discard_descr;
17690 decl_size -= padsize;
17691 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17692 if (*descr_tail == NULL)
17694 remove_loc_list_addr_table_entries (cur_descr);
17695 goto discard_descr;
17697 descr_tail = &(*descr_tail)->dw_loc_next;
17698 padsize = 0;
17700 *descr_tail = cur_descr;
17701 descr_tail = tail;
17702 if (bitsize > decl_size)
17703 goto discard_descr;
17704 decl_size -= bitsize;
17705 if (last == NULL)
17707 HOST_WIDE_INT offset = 0;
17708 if (GET_CODE (varloc) == VAR_LOCATION
17709 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17711 varloc = PAT_VAR_LOCATION_LOC (varloc);
17712 if (GET_CODE (varloc) == EXPR_LIST)
17713 varloc = XEXP (varloc, 0);
17717 if (GET_CODE (varloc) == CONST
17718 || GET_CODE (varloc) == SIGN_EXTEND
17719 || GET_CODE (varloc) == ZERO_EXTEND)
17720 varloc = XEXP (varloc, 0);
17721 else if (GET_CODE (varloc) == SUBREG)
17722 varloc = SUBREG_REG (varloc);
17723 else
17724 break;
17726 while (1);
17727 /* DW_OP_bit_size offset should be zero for register
17728 or implicit location descriptions and empty location
17729 descriptions, but for memory addresses needs big endian
17730 adjustment. */
17731 if (MEM_P (varloc))
17733 unsigned HOST_WIDE_INT memsize;
17734 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17735 goto discard_descr;
17736 memsize *= BITS_PER_UNIT;
17737 if (memsize != bitsize)
17739 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17740 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17741 goto discard_descr;
17742 if (memsize < bitsize)
17743 goto discard_descr;
17744 if (BITS_BIG_ENDIAN)
17745 offset = memsize - bitsize;
17749 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17750 if (*descr_tail == NULL)
17751 goto discard_descr;
17752 descr_tail = &(*descr_tail)->dw_loc_next;
17756 /* If there were any non-empty expressions, add padding till the end of
17757 the decl. */
17758 if (descr != NULL && decl_size != 0)
17760 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17761 if (*descr_tail == NULL)
17762 goto discard_descr;
17764 return descr;
17766 discard_descr:
17767 /* Discard the descriptor and release any addr_table entries it uses. */
17768 remove_loc_list_addr_table_entries (descr);
17769 return NULL;
17772 /* Return the dwarf representation of the location list LOC_LIST of
17773 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17774 function. */
17776 static dw_loc_list_ref
17777 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17779 const char *endname, *secname;
17780 var_loc_view endview;
17781 rtx varloc;
17782 enum var_init_status initialized;
17783 struct var_loc_node *node;
17784 dw_loc_descr_ref descr;
17785 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17786 dw_loc_list_ref list = NULL;
17787 dw_loc_list_ref *listp = &list;
17789 /* Now that we know what section we are using for a base,
17790 actually construct the list of locations.
17791 The first location information is what is passed to the
17792 function that creates the location list, and the remaining
17793 locations just get added on to that list.
17794 Note that we only know the start address for a location
17795 (IE location changes), so to build the range, we use
17796 the range [current location start, next location start].
17797 This means we have to special case the last node, and generate
17798 a range of [last location start, end of function label]. */
17800 if (cfun && crtl->has_bb_partition)
17802 bool save_in_cold_section_p = in_cold_section_p;
17803 in_cold_section_p = first_function_block_is_cold;
17804 if (loc_list->last_before_switch == NULL)
17805 in_cold_section_p = !in_cold_section_p;
17806 secname = secname_for_decl (decl);
17807 in_cold_section_p = save_in_cold_section_p;
17809 else
17810 secname = secname_for_decl (decl);
17812 for (node = loc_list->first; node; node = node->next)
17814 bool range_across_switch = false;
17815 if (GET_CODE (node->loc) == EXPR_LIST
17816 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17818 if (GET_CODE (node->loc) == EXPR_LIST)
17820 descr = NULL;
17821 /* This requires DW_OP_{,bit_}piece, which is not usable
17822 inside DWARF expressions. */
17823 if (want_address == 2)
17824 descr = dw_sra_loc_expr (decl, node->loc);
17826 else
17828 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17829 varloc = NOTE_VAR_LOCATION (node->loc);
17830 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17832 if (descr)
17834 /* If section switch happens in between node->label
17835 and node->next->label (or end of function) and
17836 we can't emit it as a single entry list,
17837 emit two ranges, first one ending at the end
17838 of first partition and second one starting at the
17839 beginning of second partition. */
17840 if (node == loc_list->last_before_switch
17841 && (node != loc_list->first || loc_list->first->next
17842 /* If we are to emit a view number, we will emit
17843 a loclist rather than a single location
17844 expression for the entire function (see
17845 loc_list_has_views), so we have to split the
17846 range that straddles across partitions. */
17847 || !ZERO_VIEW_P (node->view))
17848 && current_function_decl)
17850 endname = cfun->fde->dw_fde_end;
17851 endview = 0;
17852 range_across_switch = true;
17854 /* The variable has a location between NODE->LABEL and
17855 NODE->NEXT->LABEL. */
17856 else if (node->next)
17857 endname = node->next->label, endview = node->next->view;
17858 /* If the variable has a location at the last label
17859 it keeps its location until the end of function. */
17860 else if (!current_function_decl)
17861 endname = text_end_label, endview = 0;
17862 else
17864 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17865 current_function_funcdef_no);
17866 endname = ggc_strdup (label_id);
17867 endview = 0;
17870 *listp = new_loc_list (descr, node->label, node->view,
17871 endname, endview, secname);
17872 if (TREE_CODE (decl) == PARM_DECL
17873 && node == loc_list->first
17874 && NOTE_P (node->loc)
17875 && strcmp (node->label, endname) == 0)
17876 (*listp)->force = true;
17877 listp = &(*listp)->dw_loc_next;
17881 if (cfun
17882 && crtl->has_bb_partition
17883 && node == loc_list->last_before_switch)
17885 bool save_in_cold_section_p = in_cold_section_p;
17886 in_cold_section_p = !first_function_block_is_cold;
17887 secname = secname_for_decl (decl);
17888 in_cold_section_p = save_in_cold_section_p;
17891 if (range_across_switch)
17893 if (GET_CODE (node->loc) == EXPR_LIST)
17894 descr = dw_sra_loc_expr (decl, node->loc);
17895 else
17897 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17898 varloc = NOTE_VAR_LOCATION (node->loc);
17899 descr = dw_loc_list_1 (decl, varloc, want_address,
17900 initialized);
17902 gcc_assert (descr);
17903 /* The variable has a location between NODE->LABEL and
17904 NODE->NEXT->LABEL. */
17905 if (node->next)
17906 endname = node->next->label, endview = node->next->view;
17907 else
17908 endname = cfun->fde->dw_fde_second_end, endview = 0;
17909 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17910 endname, endview, secname);
17911 listp = &(*listp)->dw_loc_next;
17915 /* Try to avoid the overhead of a location list emitting a location
17916 expression instead, but only if we didn't have more than one
17917 location entry in the first place. If some entries were not
17918 representable, we don't want to pretend a single entry that was
17919 applies to the entire scope in which the variable is
17920 available. */
17921 if (list && loc_list->first->next)
17922 gen_llsym (list);
17923 else
17924 maybe_gen_llsym (list);
17926 return list;
17929 /* Return true if the loc_list has only single element and thus
17930 can be represented as location description. */
17932 static bool
17933 single_element_loc_list_p (dw_loc_list_ref list)
17935 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17936 return !list->ll_symbol;
17939 /* Duplicate a single element of location list. */
17941 static inline dw_loc_descr_ref
17942 copy_loc_descr (dw_loc_descr_ref ref)
17944 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17945 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17946 return copy;
17949 /* To each location in list LIST append loc descr REF. */
17951 static void
17952 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17954 dw_loc_descr_ref copy;
17955 add_loc_descr (&list->expr, ref);
17956 list = list->dw_loc_next;
17957 while (list)
17959 copy = copy_loc_descr (ref);
17960 add_loc_descr (&list->expr, copy);
17961 while (copy->dw_loc_next)
17962 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17963 list = list->dw_loc_next;
17967 /* To each location in list LIST prepend loc descr REF. */
17969 static void
17970 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17972 dw_loc_descr_ref copy;
17973 dw_loc_descr_ref ref_end = list->expr;
17974 add_loc_descr (&ref, list->expr);
17975 list->expr = ref;
17976 list = list->dw_loc_next;
17977 while (list)
17979 dw_loc_descr_ref end = list->expr;
17980 list->expr = copy = copy_loc_descr (ref);
17981 while (copy->dw_loc_next != ref_end)
17982 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17983 copy->dw_loc_next = end;
17984 list = list->dw_loc_next;
17988 /* Given two lists RET and LIST
17989 produce location list that is result of adding expression in LIST
17990 to expression in RET on each position in program.
17991 Might be destructive on both RET and LIST.
17993 TODO: We handle only simple cases of RET or LIST having at most one
17994 element. General case would involve sorting the lists in program order
17995 and merging them that will need some additional work.
17996 Adding that will improve quality of debug info especially for SRA-ed
17997 structures. */
17999 static void
18000 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
18002 if (!list)
18003 return;
18004 if (!*ret)
18006 *ret = list;
18007 return;
18009 if (!list->dw_loc_next)
18011 add_loc_descr_to_each (*ret, list->expr);
18012 return;
18014 if (!(*ret)->dw_loc_next)
18016 prepend_loc_descr_to_each (list, (*ret)->expr);
18017 *ret = list;
18018 return;
18020 expansion_failed (NULL_TREE, NULL_RTX,
18021 "Don't know how to merge two non-trivial"
18022 " location lists.\n");
18023 *ret = NULL;
18024 return;
18027 /* LOC is constant expression. Try a luck, look it up in constant
18028 pool and return its loc_descr of its address. */
18030 static dw_loc_descr_ref
18031 cst_pool_loc_descr (tree loc)
18033 /* Get an RTL for this, if something has been emitted. */
18034 rtx rtl = lookup_constant_def (loc);
18036 if (!rtl || !MEM_P (rtl))
18038 gcc_assert (!rtl);
18039 return 0;
18041 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
18043 /* TODO: We might get more coverage if we was actually delaying expansion
18044 of all expressions till end of compilation when constant pools are fully
18045 populated. */
18046 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
18048 expansion_failed (loc, NULL_RTX,
18049 "CST value in contant pool but not marked.");
18050 return 0;
18052 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
18053 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
18056 /* Return dw_loc_list representing address of addr_expr LOC
18057 by looking for inner INDIRECT_REF expression and turning
18058 it into simple arithmetics.
18060 See loc_list_from_tree for the meaning of CONTEXT. */
18062 static dw_loc_list_ref
18063 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
18064 loc_descr_context *context)
18066 tree obj, offset;
18067 poly_int64 bitsize, bitpos, bytepos;
18068 machine_mode mode;
18069 int unsignedp, reversep, volatilep = 0;
18070 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18072 obj = get_inner_reference (TREE_OPERAND (loc, 0),
18073 &bitsize, &bitpos, &offset, &mode,
18074 &unsignedp, &reversep, &volatilep);
18075 STRIP_NOPS (obj);
18076 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
18078 expansion_failed (loc, NULL_RTX, "bitfield access");
18079 return 0;
18081 if (!INDIRECT_REF_P (obj))
18083 expansion_failed (obj,
18084 NULL_RTX, "no indirect ref in inner refrence");
18085 return 0;
18087 if (!offset && known_eq (bitpos, 0))
18088 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
18089 context);
18090 else if (toplev
18091 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
18092 && (dwarf_version >= 4 || !dwarf_strict))
18094 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
18095 if (!list_ret)
18096 return 0;
18097 if (offset)
18099 /* Variable offset. */
18100 list_ret1 = loc_list_from_tree (offset, 0, context);
18101 if (list_ret1 == 0)
18102 return 0;
18103 add_loc_list (&list_ret, list_ret1);
18104 if (!list_ret)
18105 return 0;
18106 add_loc_descr_to_each (list_ret,
18107 new_loc_descr (DW_OP_plus, 0, 0));
18109 HOST_WIDE_INT value;
18110 if (bytepos.is_constant (&value) && value > 0)
18111 add_loc_descr_to_each (list_ret,
18112 new_loc_descr (DW_OP_plus_uconst, value, 0));
18113 else if (maybe_ne (bytepos, 0))
18114 loc_list_plus_const (list_ret, bytepos);
18115 add_loc_descr_to_each (list_ret,
18116 new_loc_descr (DW_OP_stack_value, 0, 0));
18118 return list_ret;
18121 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
18122 all operations from LOC are nops, move to the last one. Insert in NOPS all
18123 operations that are skipped. */
18125 static void
18126 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
18127 hash_set<dw_loc_descr_ref> &nops)
18129 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
18131 nops.add (loc);
18132 loc = loc->dw_loc_next;
18136 /* Helper for loc_descr_without_nops: free the location description operation
18137 P. */
18139 bool
18140 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
18142 ggc_free (loc);
18143 return true;
18146 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
18147 finishes LOC. */
18149 static void
18150 loc_descr_without_nops (dw_loc_descr_ref &loc)
18152 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
18153 return;
18155 /* Set of all DW_OP_nop operations we remove. */
18156 hash_set<dw_loc_descr_ref> nops;
18158 /* First, strip all prefix NOP operations in order to keep the head of the
18159 operations list. */
18160 loc_descr_to_next_no_nop (loc, nops);
18162 for (dw_loc_descr_ref cur = loc; cur != NULL;)
18164 /* For control flow operations: strip "prefix" nops in destination
18165 labels. */
18166 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
18167 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
18168 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
18169 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
18171 /* Do the same for the operations that follow, then move to the next
18172 iteration. */
18173 if (cur->dw_loc_next != NULL)
18174 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
18175 cur = cur->dw_loc_next;
18178 nops.traverse<void *, free_loc_descr> (NULL);
18182 struct dwarf_procedure_info;
18184 /* Helper structure for location descriptions generation. */
18185 struct loc_descr_context
18187 /* The type that is implicitly referenced by DW_OP_push_object_address, or
18188 NULL_TREE if DW_OP_push_object_address in invalid for this location
18189 description. This is used when processing PLACEHOLDER_EXPR nodes. */
18190 tree context_type;
18191 /* The ..._DECL node that should be translated as a
18192 DW_OP_push_object_address operation. */
18193 tree base_decl;
18194 /* Information about the DWARF procedure we are currently generating. NULL if
18195 we are not generating a DWARF procedure. */
18196 struct dwarf_procedure_info *dpi;
18197 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
18198 by consumer. Used for DW_TAG_generic_subrange attributes. */
18199 bool placeholder_arg;
18200 /* True if PLACEHOLDER_EXPR has been seen. */
18201 bool placeholder_seen;
18202 /* True if strict preservation of signedness has been requested. */
18203 bool strict_signedness;
18206 /* DWARF procedures generation
18208 DWARF expressions (aka. location descriptions) are used to encode variable
18209 things such as sizes or offsets. Such computations can have redundant parts
18210 that can be factorized in order to reduce the size of the output debug
18211 information. This is the whole point of DWARF procedures.
18213 Thanks to stor-layout.cc, size and offset expressions in GENERIC trees are
18214 already factorized into functions ("size functions") in order to handle very
18215 big and complex types. Such functions are quite simple: they have integral
18216 arguments, they return an integral result and their body contains only a
18217 return statement with arithmetic expressions. This is the only kind of
18218 function we are interested in translating into DWARF procedures, here.
18220 DWARF expressions and DWARF procedure are executed using a stack, so we have
18221 to define some calling convention for them to interact. Let's say that:
18223 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18224 all arguments in reverse order (right-to-left) so that when the DWARF
18225 procedure execution starts, the first argument is the top of the stack.
18227 - Then, when returning, the DWARF procedure must have consumed all arguments
18228 on the stack, must have pushed the result and touched nothing else.
18230 - Each integral argument and the result are integral types can be hold in a
18231 single stack slot.
18233 - We call "frame offset" the number of stack slots that are "under DWARF
18234 procedure control": it includes the arguments slots, the temporaries and
18235 the result slot. Thus, it is equal to the number of arguments when the
18236 procedure execution starts and must be equal to one (the result) when it
18237 returns. */
18239 /* Helper structure used when generating operations for a DWARF procedure. */
18240 struct dwarf_procedure_info
18242 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18243 currently translated. */
18244 tree fndecl;
18245 /* The number of arguments FNDECL takes. */
18246 unsigned args_count;
18249 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18250 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18251 equate it to this DIE. */
18253 static dw_die_ref
18254 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18255 dw_die_ref parent_die)
18257 dw_die_ref dwarf_proc_die;
18259 if ((dwarf_version < 3 && dwarf_strict)
18260 || location == NULL)
18261 return NULL;
18263 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18264 if (fndecl)
18265 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18266 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18267 return dwarf_proc_die;
18270 /* Return whether TYPE is a supported type as a DWARF procedure argument
18271 type or return type (we handle only scalar types and pointer types that
18272 aren't wider than the DWARF expression evaluation stack). */
18274 static bool
18275 is_handled_procedure_type (tree type)
18277 return ((INTEGRAL_TYPE_P (type)
18278 || TREE_CODE (type) == OFFSET_TYPE
18279 || TREE_CODE (type) == POINTER_TYPE)
18280 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18283 /* Helper for resolve_args_picking: do the same but stop when coming across
18284 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18285 offset *before* evaluating the corresponding operation. */
18287 static bool
18288 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18289 struct dwarf_procedure_info *dpi,
18290 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18292 /* The "frame_offset" identifier is already used to name a macro... */
18293 unsigned frame_offset_ = initial_frame_offset;
18294 dw_loc_descr_ref l;
18296 for (l = loc; l != NULL;)
18298 bool existed;
18299 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18301 /* If we already met this node, there is nothing to compute anymore. */
18302 if (existed)
18304 /* Make sure that the stack size is consistent wherever the execution
18305 flow comes from. */
18306 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18307 break;
18309 l_frame_offset = frame_offset_;
18311 /* If needed, relocate the picking offset with respect to the frame
18312 offset. */
18313 if (l->frame_offset_rel)
18315 unsigned HOST_WIDE_INT off;
18316 switch (l->dw_loc_opc)
18318 case DW_OP_pick:
18319 off = l->dw_loc_oprnd1.v.val_unsigned;
18320 break;
18321 case DW_OP_dup:
18322 off = 0;
18323 break;
18324 case DW_OP_over:
18325 off = 1;
18326 break;
18327 default:
18328 gcc_unreachable ();
18330 /* frame_offset_ is the size of the current stack frame, including
18331 incoming arguments. Besides, the arguments are pushed
18332 right-to-left. Thus, in order to access the Nth argument from
18333 this operation node, the picking has to skip temporaries *plus*
18334 one stack slot per argument (0 for the first one, 1 for the second
18335 one, etc.).
18337 The targetted argument number (N) is already set as the operand,
18338 and the number of temporaries can be computed with:
18339 frame_offsets_ - dpi->args_count */
18340 off += frame_offset_ - dpi->args_count;
18342 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18343 if (off > 255)
18344 return false;
18346 if (off == 0)
18348 l->dw_loc_opc = DW_OP_dup;
18349 l->dw_loc_oprnd1.v.val_unsigned = 0;
18351 else if (off == 1)
18353 l->dw_loc_opc = DW_OP_over;
18354 l->dw_loc_oprnd1.v.val_unsigned = 0;
18356 else
18358 l->dw_loc_opc = DW_OP_pick;
18359 l->dw_loc_oprnd1.v.val_unsigned = off;
18363 /* Update frame_offset according to the effect the current operation has
18364 on the stack. */
18365 switch (l->dw_loc_opc)
18367 case DW_OP_deref:
18368 case DW_OP_swap:
18369 case DW_OP_rot:
18370 case DW_OP_abs:
18371 case DW_OP_neg:
18372 case DW_OP_not:
18373 case DW_OP_plus_uconst:
18374 case DW_OP_skip:
18375 case DW_OP_reg0:
18376 case DW_OP_reg1:
18377 case DW_OP_reg2:
18378 case DW_OP_reg3:
18379 case DW_OP_reg4:
18380 case DW_OP_reg5:
18381 case DW_OP_reg6:
18382 case DW_OP_reg7:
18383 case DW_OP_reg8:
18384 case DW_OP_reg9:
18385 case DW_OP_reg10:
18386 case DW_OP_reg11:
18387 case DW_OP_reg12:
18388 case DW_OP_reg13:
18389 case DW_OP_reg14:
18390 case DW_OP_reg15:
18391 case DW_OP_reg16:
18392 case DW_OP_reg17:
18393 case DW_OP_reg18:
18394 case DW_OP_reg19:
18395 case DW_OP_reg20:
18396 case DW_OP_reg21:
18397 case DW_OP_reg22:
18398 case DW_OP_reg23:
18399 case DW_OP_reg24:
18400 case DW_OP_reg25:
18401 case DW_OP_reg26:
18402 case DW_OP_reg27:
18403 case DW_OP_reg28:
18404 case DW_OP_reg29:
18405 case DW_OP_reg30:
18406 case DW_OP_reg31:
18407 case DW_OP_bregx:
18408 case DW_OP_piece:
18409 case DW_OP_deref_size:
18410 case DW_OP_nop:
18411 case DW_OP_bit_piece:
18412 case DW_OP_implicit_value:
18413 case DW_OP_stack_value:
18414 case DW_OP_deref_type:
18415 case DW_OP_convert:
18416 case DW_OP_reinterpret:
18417 case DW_OP_GNU_deref_type:
18418 case DW_OP_GNU_convert:
18419 case DW_OP_GNU_reinterpret:
18420 break;
18422 case DW_OP_addr:
18423 case DW_OP_const1u:
18424 case DW_OP_const1s:
18425 case DW_OP_const2u:
18426 case DW_OP_const2s:
18427 case DW_OP_const4u:
18428 case DW_OP_const4s:
18429 case DW_OP_const8u:
18430 case DW_OP_const8s:
18431 case DW_OP_constu:
18432 case DW_OP_consts:
18433 case DW_OP_dup:
18434 case DW_OP_over:
18435 case DW_OP_pick:
18436 case DW_OP_lit0:
18437 case DW_OP_lit1:
18438 case DW_OP_lit2:
18439 case DW_OP_lit3:
18440 case DW_OP_lit4:
18441 case DW_OP_lit5:
18442 case DW_OP_lit6:
18443 case DW_OP_lit7:
18444 case DW_OP_lit8:
18445 case DW_OP_lit9:
18446 case DW_OP_lit10:
18447 case DW_OP_lit11:
18448 case DW_OP_lit12:
18449 case DW_OP_lit13:
18450 case DW_OP_lit14:
18451 case DW_OP_lit15:
18452 case DW_OP_lit16:
18453 case DW_OP_lit17:
18454 case DW_OP_lit18:
18455 case DW_OP_lit19:
18456 case DW_OP_lit20:
18457 case DW_OP_lit21:
18458 case DW_OP_lit22:
18459 case DW_OP_lit23:
18460 case DW_OP_lit24:
18461 case DW_OP_lit25:
18462 case DW_OP_lit26:
18463 case DW_OP_lit27:
18464 case DW_OP_lit28:
18465 case DW_OP_lit29:
18466 case DW_OP_lit30:
18467 case DW_OP_lit31:
18468 case DW_OP_breg0:
18469 case DW_OP_breg1:
18470 case DW_OP_breg2:
18471 case DW_OP_breg3:
18472 case DW_OP_breg4:
18473 case DW_OP_breg5:
18474 case DW_OP_breg6:
18475 case DW_OP_breg7:
18476 case DW_OP_breg8:
18477 case DW_OP_breg9:
18478 case DW_OP_breg10:
18479 case DW_OP_breg11:
18480 case DW_OP_breg12:
18481 case DW_OP_breg13:
18482 case DW_OP_breg14:
18483 case DW_OP_breg15:
18484 case DW_OP_breg16:
18485 case DW_OP_breg17:
18486 case DW_OP_breg18:
18487 case DW_OP_breg19:
18488 case DW_OP_breg20:
18489 case DW_OP_breg21:
18490 case DW_OP_breg22:
18491 case DW_OP_breg23:
18492 case DW_OP_breg24:
18493 case DW_OP_breg25:
18494 case DW_OP_breg26:
18495 case DW_OP_breg27:
18496 case DW_OP_breg28:
18497 case DW_OP_breg29:
18498 case DW_OP_breg30:
18499 case DW_OP_breg31:
18500 case DW_OP_fbreg:
18501 case DW_OP_push_object_address:
18502 case DW_OP_call_frame_cfa:
18503 case DW_OP_GNU_variable_value:
18504 case DW_OP_GNU_addr_index:
18505 case DW_OP_GNU_const_index:
18506 ++frame_offset_;
18507 break;
18509 case DW_OP_drop:
18510 case DW_OP_xderef:
18511 case DW_OP_and:
18512 case DW_OP_div:
18513 case DW_OP_minus:
18514 case DW_OP_mod:
18515 case DW_OP_mul:
18516 case DW_OP_or:
18517 case DW_OP_plus:
18518 case DW_OP_shl:
18519 case DW_OP_shr:
18520 case DW_OP_shra:
18521 case DW_OP_xor:
18522 case DW_OP_bra:
18523 case DW_OP_eq:
18524 case DW_OP_ge:
18525 case DW_OP_gt:
18526 case DW_OP_le:
18527 case DW_OP_lt:
18528 case DW_OP_ne:
18529 case DW_OP_regx:
18530 case DW_OP_xderef_size:
18531 --frame_offset_;
18532 break;
18534 case DW_OP_call2:
18535 case DW_OP_call4:
18536 case DW_OP_call_ref:
18538 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18539 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18541 if (stack_usage == NULL)
18542 return false;
18543 frame_offset_ += *stack_usage;
18544 break;
18547 case DW_OP_implicit_pointer:
18548 case DW_OP_entry_value:
18549 case DW_OP_const_type:
18550 case DW_OP_regval_type:
18551 case DW_OP_form_tls_address:
18552 case DW_OP_GNU_push_tls_address:
18553 case DW_OP_GNU_uninit:
18554 case DW_OP_GNU_encoded_addr:
18555 case DW_OP_GNU_implicit_pointer:
18556 case DW_OP_GNU_entry_value:
18557 case DW_OP_GNU_const_type:
18558 case DW_OP_GNU_regval_type:
18559 case DW_OP_GNU_parameter_ref:
18560 /* loc_list_from_tree will probably not output these operations for
18561 size functions, so assume they will not appear here. */
18562 /* Fall through... */
18564 default:
18565 gcc_unreachable ();
18568 /* Now, follow the control flow (except subroutine calls). */
18569 switch (l->dw_loc_opc)
18571 case DW_OP_bra:
18572 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18573 frame_offsets))
18574 return false;
18575 /* Fall through. */
18577 case DW_OP_skip:
18578 l = l->dw_loc_oprnd1.v.val_loc;
18579 break;
18581 case DW_OP_stack_value:
18582 return true;
18584 default:
18585 l = l->dw_loc_next;
18586 break;
18590 return true;
18593 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18594 operations) in order to resolve the operand of DW_OP_pick operations that
18595 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18596 offset *before* LOC is executed. Return if all relocations were
18597 successful. */
18599 static bool
18600 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18601 struct dwarf_procedure_info *dpi)
18603 /* Associate to all visited operations the frame offset *before* evaluating
18604 this operation. */
18605 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18607 return
18608 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18611 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18612 Return NULL if it is not possible. */
18614 static dw_die_ref
18615 function_to_dwarf_procedure (tree fndecl)
18617 struct dwarf_procedure_info dpi;
18618 struct loc_descr_context ctx = {
18619 NULL_TREE, /* context_type */
18620 NULL_TREE, /* base_decl */
18621 &dpi, /* dpi */
18622 false, /* placeholder_arg */
18623 false, /* placeholder_seen */
18624 true /* strict_signedness */
18626 dw_die_ref dwarf_proc_die;
18627 tree tree_body = DECL_SAVED_TREE (fndecl);
18628 dw_loc_descr_ref loc_body, epilogue;
18630 tree cursor;
18631 unsigned i;
18633 /* Do not generate multiple DWARF procedures for the same function
18634 declaration. */
18635 dwarf_proc_die = lookup_decl_die (fndecl);
18636 if (dwarf_proc_die != NULL)
18637 return dwarf_proc_die;
18639 /* DWARF procedures are available starting with the DWARFv3 standard. */
18640 if (dwarf_version < 3 && dwarf_strict)
18641 return NULL;
18643 /* We handle only functions for which we still have a body, that return a
18644 supported type and that takes arguments with supported types. Note that
18645 there is no point translating functions that return nothing. */
18646 if (tree_body == NULL_TREE
18647 || DECL_RESULT (fndecl) == NULL_TREE
18648 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18649 return NULL;
18651 for (cursor = DECL_ARGUMENTS (fndecl);
18652 cursor != NULL_TREE;
18653 cursor = TREE_CHAIN (cursor))
18654 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18655 return NULL;
18657 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18658 if (TREE_CODE (tree_body) != RETURN_EXPR)
18659 return NULL;
18660 tree_body = TREE_OPERAND (tree_body, 0);
18661 if (TREE_CODE (tree_body) != MODIFY_EXPR
18662 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18663 return NULL;
18664 tree_body = TREE_OPERAND (tree_body, 1);
18666 /* Try to translate the body expression itself. Note that this will probably
18667 cause an infinite recursion if its call graph has a cycle. This is very
18668 unlikely for size functions, however, so don't bother with such things at
18669 the moment. */
18670 dpi.fndecl = fndecl;
18671 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18672 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18673 if (!loc_body)
18674 return NULL;
18676 /* After evaluating all operands in "loc_body", we should still have on the
18677 stack all arguments plus the desired function result (top of the stack).
18678 Generate code in order to keep only the result in our stack frame. */
18679 epilogue = NULL;
18680 for (i = 0; i < dpi.args_count; ++i)
18682 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18683 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18684 op_couple->dw_loc_next->dw_loc_next = epilogue;
18685 epilogue = op_couple;
18687 add_loc_descr (&loc_body, epilogue);
18688 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18689 return NULL;
18691 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18692 because they are considered useful. Now there is an epilogue, they are
18693 not anymore, so give it another try. */
18694 loc_descr_without_nops (loc_body);
18696 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18697 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18698 though, given that size functions do not come from source, so they should
18699 not have a dedicated DW_TAG_subprogram DIE. */
18700 dwarf_proc_die
18701 = new_dwarf_proc_die (loc_body, fndecl,
18702 get_context_die (DECL_CONTEXT (fndecl)));
18704 /* The called DWARF procedure consumes one stack slot per argument and
18705 returns one stack slot. */
18706 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18708 return dwarf_proc_die;
18711 /* Helper function for loc_list_from_tree. Perform OP binary op,
18712 but after converting arguments to type_die, afterwards convert
18713 back to unsigned. */
18715 static dw_loc_list_ref
18716 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18717 dw_die_ref type_die, scalar_int_mode mode,
18718 struct loc_descr_context *context)
18720 dw_loc_list_ref op0, op1;
18721 dw_loc_descr_ref cvt, binop;
18723 if (type_die == NULL)
18724 return NULL;
18726 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18727 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18728 if (op0 == NULL || op1 == NULL)
18729 return NULL;
18731 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18732 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18733 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18734 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18735 add_loc_descr_to_each (op0, cvt);
18737 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18738 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18739 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18740 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18741 add_loc_descr_to_each (op1, cvt);
18743 add_loc_list (&op0, op1);
18744 if (op0 == NULL)
18745 return NULL;
18747 binop = new_loc_descr (op, 0, 0);
18748 convert_descriptor_to_mode (mode, binop);
18749 add_loc_descr_to_each (op0, binop);
18751 return op0;
18754 /* Generate Dwarf location list representing LOC.
18755 If WANT_ADDRESS is false, expression computing LOC will be computed
18756 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18757 if WANT_ADDRESS is 2, expression computing address useable in location
18758 will be returned (i.e. DW_OP_reg can be used
18759 to refer to register values).
18761 CONTEXT provides information to customize the location descriptions
18762 generation. Its context_type field specifies what type is implicitly
18763 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18764 will not be generated.
18766 Its DPI field determines whether we are generating a DWARF expression for a
18767 DWARF procedure, so PARM_DECL references are processed specifically.
18769 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18770 and dpi fields were null. */
18772 static dw_loc_list_ref
18773 loc_list_from_tree_1 (tree loc, int want_address,
18774 struct loc_descr_context *context)
18776 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18777 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18778 int have_address = 0;
18779 enum dwarf_location_atom op;
18781 /* ??? Most of the time we do not take proper care for sign/zero
18782 extending the values properly. Hopefully this won't be a real
18783 problem... */
18785 if (context != NULL
18786 && context->base_decl == loc
18787 && want_address == 0)
18789 if (dwarf_version >= 3 || !dwarf_strict)
18790 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18791 NULL, 0, NULL, 0, NULL);
18792 else
18793 return NULL;
18796 switch (TREE_CODE (loc))
18798 case ERROR_MARK:
18799 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18800 return 0;
18802 case PLACEHOLDER_EXPR:
18803 /* This case involves extracting fields from an object to determine the
18804 position of other fields. It is supposed to appear only as the first
18805 operand of COMPONENT_REF nodes and to reference precisely the type
18806 that the context allows or its enclosing type. */
18807 if (context != NULL
18808 && (TREE_TYPE (loc) == context->context_type
18809 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18810 && want_address >= 1)
18812 if (dwarf_version >= 3 || !dwarf_strict)
18814 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18815 have_address = 1;
18816 break;
18818 else
18819 return NULL;
18821 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18822 the single argument passed by consumer. */
18823 else if (context != NULL
18824 && context->placeholder_arg
18825 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18826 && want_address == 0)
18828 ret = new_loc_descr (DW_OP_pick, 0, 0);
18829 ret->frame_offset_rel = 1;
18830 context->placeholder_seen = true;
18831 break;
18833 else
18834 expansion_failed (loc, NULL_RTX,
18835 "PLACEHOLDER_EXPR for an unexpected type");
18836 break;
18838 case CALL_EXPR:
18840 tree callee = get_callee_fndecl (loc);
18841 dw_die_ref dwarf_proc;
18843 if (callee
18844 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18845 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18847 /* DWARF procedures are used for size functions, which are built
18848 when size expressions contain conditional constructs, so we
18849 request strict preservation of signedness for comparisons. */
18850 bool old_strict_signedness;
18851 if (context)
18853 old_strict_signedness = context->strict_signedness;
18854 context->strict_signedness = true;
18857 /* Evaluate arguments right-to-left so that the first argument
18858 will be the top-most one on the stack. */
18859 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18861 tree arg = CALL_EXPR_ARG (loc, i);
18862 ret1 = loc_descriptor_from_tree (arg, 0, context);
18863 if (!ret1)
18865 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18866 return NULL;
18868 add_loc_descr (&ret, ret1);
18871 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18872 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18873 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18874 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18875 add_loc_descr (&ret, ret1);
18876 if (context)
18877 context->strict_signedness = old_strict_signedness;
18879 else
18880 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18881 break;
18884 case PREINCREMENT_EXPR:
18885 case PREDECREMENT_EXPR:
18886 case POSTINCREMENT_EXPR:
18887 case POSTDECREMENT_EXPR:
18888 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18889 /* There are no opcodes for these operations. */
18890 return 0;
18892 case ADDR_EXPR:
18893 /* If we already want an address, see if there is INDIRECT_REF inside
18894 e.g. for &this->field. */
18895 if (want_address)
18897 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18898 (loc, want_address == 2, context);
18899 if (list_ret)
18900 have_address = 1;
18901 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18902 && (ret = cst_pool_loc_descr (loc)))
18903 have_address = 1;
18905 /* Otherwise, process the argument and look for the address. */
18906 if (!list_ret && !ret)
18907 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18908 else
18910 if (want_address)
18911 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18912 return NULL;
18914 break;
18916 case VAR_DECL:
18917 if (DECL_THREAD_LOCAL_P (loc))
18919 rtx rtl;
18920 enum dwarf_location_atom tls_op;
18921 enum dtprel_bool dtprel = dtprel_false;
18923 if (targetm.have_tls)
18925 /* If this is not defined, we have no way to emit the
18926 data. */
18927 if (!targetm.asm_out.output_dwarf_dtprel)
18928 return 0;
18930 /* The way DW_OP_GNU_push_tls_address is specified, we
18931 can only look up addresses of objects in the current
18932 module. We used DW_OP_addr as first op, but that's
18933 wrong, because DW_OP_addr is relocated by the debug
18934 info consumer, while DW_OP_GNU_push_tls_address
18935 operand shouldn't be. */
18936 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18937 return 0;
18938 dtprel = dtprel_true;
18939 /* We check for DWARF 5 here because gdb did not implement
18940 DW_OP_form_tls_address until after 7.12. */
18941 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18942 : DW_OP_GNU_push_tls_address);
18944 else
18946 if (!targetm.emutls.debug_form_tls_address
18947 || !(dwarf_version >= 3 || !dwarf_strict))
18948 return 0;
18949 /* We stuffed the control variable into the DECL_VALUE_EXPR
18950 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18951 no longer appear in gimple code. We used the control
18952 variable in specific so that we could pick it up here. */
18953 loc = DECL_VALUE_EXPR (loc);
18954 tls_op = DW_OP_form_tls_address;
18957 rtl = rtl_for_decl_location (loc);
18958 if (rtl == NULL_RTX)
18959 return 0;
18961 if (!MEM_P (rtl))
18962 return 0;
18963 rtl = XEXP (rtl, 0);
18964 if (! CONSTANT_P (rtl))
18965 return 0;
18967 ret = new_addr_loc_descr (rtl, dtprel);
18968 ret1 = new_loc_descr (tls_op, 0, 0);
18969 add_loc_descr (&ret, ret1);
18971 have_address = 1;
18972 break;
18974 /* FALLTHRU */
18976 case PARM_DECL:
18977 if (context != NULL && context->dpi != NULL
18978 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18980 /* We are generating code for a DWARF procedure and we want to access
18981 one of its arguments: find the appropriate argument offset and let
18982 the resolve_args_picking pass compute the offset that complies
18983 with the stack frame size. */
18984 unsigned i = 0;
18985 tree cursor;
18987 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18988 cursor != NULL_TREE && cursor != loc;
18989 cursor = TREE_CHAIN (cursor), ++i)
18991 /* If we are translating a DWARF procedure, all referenced parameters
18992 must belong to the current function. */
18993 gcc_assert (cursor != NULL_TREE);
18995 ret = new_loc_descr (DW_OP_pick, i, 0);
18996 ret->frame_offset_rel = 1;
18997 break;
18999 /* FALLTHRU */
19001 case RESULT_DECL:
19002 if (DECL_HAS_VALUE_EXPR_P (loc))
19004 tree value_expr = DECL_VALUE_EXPR (loc);
19006 /* Non-local frame structures are DECL_IGNORED_P variables so we need
19007 to wait until they get an RTX in order to reference them. */
19008 if (early_dwarf
19009 && TREE_CODE (value_expr) == COMPONENT_REF
19010 && VAR_P (TREE_OPERAND (value_expr, 0))
19011 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
19013 else
19014 return loc_list_from_tree_1 (value_expr, want_address, context);
19017 /* FALLTHRU */
19019 case FUNCTION_DECL:
19021 rtx rtl;
19022 var_loc_list *loc_list = lookup_decl_loc (loc);
19024 if (loc_list && loc_list->first)
19026 list_ret = dw_loc_list (loc_list, loc, want_address);
19027 have_address = want_address != 0;
19028 break;
19030 rtl = rtl_for_decl_location (loc);
19031 if (rtl == NULL_RTX)
19033 if (TREE_CODE (loc) != FUNCTION_DECL
19034 && early_dwarf
19035 && want_address != 1
19036 && ! DECL_IGNORED_P (loc)
19037 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
19038 || POINTER_TYPE_P (TREE_TYPE (loc)))
19039 && TYPE_MODE (TREE_TYPE (loc)) != BLKmode
19040 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
19041 <= DWARF2_ADDR_SIZE))
19043 dw_die_ref ref = lookup_decl_die (loc);
19044 if (ref)
19046 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
19047 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19048 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
19049 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
19051 else if (current_function_decl
19052 && DECL_CONTEXT (loc) == current_function_decl)
19054 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
19055 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19056 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
19058 break;
19060 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
19061 return 0;
19063 else if (CONST_INT_P (rtl))
19065 HOST_WIDE_INT val = INTVAL (rtl);
19066 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19067 val &= GET_MODE_MASK (DECL_MODE (loc));
19068 ret = int_loc_descriptor (val);
19070 else if (GET_CODE (rtl) == CONST_STRING)
19072 expansion_failed (loc, NULL_RTX, "CONST_STRING");
19073 return 0;
19075 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
19076 ret = new_addr_loc_descr (rtl, dtprel_false);
19077 else
19079 machine_mode mode, mem_mode;
19081 /* Certain constructs can only be represented at top-level. */
19082 if (want_address == 2)
19084 ret = loc_descriptor (rtl, VOIDmode,
19085 VAR_INIT_STATUS_INITIALIZED);
19086 have_address = 1;
19088 else
19090 mode = GET_MODE (rtl);
19091 mem_mode = VOIDmode;
19092 if (MEM_P (rtl))
19094 mem_mode = mode;
19095 mode = get_address_mode (rtl);
19096 rtl = XEXP (rtl, 0);
19097 have_address = 1;
19099 ret = mem_loc_descriptor (rtl, mode, mem_mode,
19100 VAR_INIT_STATUS_INITIALIZED);
19102 if (!ret)
19103 expansion_failed (loc, rtl,
19104 "failed to produce loc descriptor for rtl");
19107 break;
19109 case MEM_REF:
19110 if (!integer_zerop (TREE_OPERAND (loc, 1)))
19112 have_address = 1;
19113 goto do_plus;
19115 /* Fallthru. */
19116 case INDIRECT_REF:
19117 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19118 have_address = 1;
19119 break;
19121 case TARGET_MEM_REF:
19122 case SSA_NAME:
19123 case DEBUG_EXPR_DECL:
19124 return NULL;
19126 case COMPOUND_EXPR:
19127 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
19128 context);
19130 CASE_CONVERT:
19131 case VIEW_CONVERT_EXPR:
19132 case SAVE_EXPR:
19133 case MODIFY_EXPR:
19134 case NON_LVALUE_EXPR:
19135 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
19136 context);
19138 case COMPONENT_REF:
19139 case BIT_FIELD_REF:
19140 case ARRAY_REF:
19141 case ARRAY_RANGE_REF:
19142 case REALPART_EXPR:
19143 case IMAGPART_EXPR:
19145 tree obj, offset;
19146 poly_int64 bitsize, bitpos, bytepos;
19147 machine_mode mode;
19148 int unsignedp, reversep, volatilep = 0;
19150 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
19151 &unsignedp, &reversep, &volatilep);
19153 gcc_assert (obj != loc);
19155 list_ret = loc_list_from_tree_1 (obj,
19156 want_address == 2
19157 && known_eq (bitpos, 0)
19158 && !offset ? 2 : 1,
19159 context);
19160 /* TODO: We can extract value of the small expression via shifting even
19161 for nonzero bitpos. */
19162 if (list_ret == 0)
19163 return 0;
19164 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
19165 || !multiple_p (bitsize, BITS_PER_UNIT))
19167 expansion_failed (loc, NULL_RTX,
19168 "bitfield access");
19169 return 0;
19172 if (offset != NULL_TREE)
19174 /* Variable offset. */
19175 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
19176 if (list_ret1 == 0)
19177 return 0;
19178 add_loc_list (&list_ret, list_ret1);
19179 if (!list_ret)
19180 return 0;
19181 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
19184 HOST_WIDE_INT value;
19185 if (bytepos.is_constant (&value) && value > 0)
19186 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
19187 value, 0));
19188 else if (maybe_ne (bytepos, 0))
19189 loc_list_plus_const (list_ret, bytepos);
19191 have_address = 1;
19192 break;
19195 case INTEGER_CST:
19196 if ((want_address || !tree_fits_shwi_p (loc))
19197 && (ret = cst_pool_loc_descr (loc)))
19198 have_address = 1;
19199 else if (want_address == 2
19200 && tree_fits_shwi_p (loc)
19201 && (ret = address_of_int_loc_descriptor
19202 (int_size_in_bytes (TREE_TYPE (loc)),
19203 tree_to_shwi (loc))))
19204 have_address = 1;
19205 else if (tree_fits_shwi_p (loc))
19206 ret = int_loc_descriptor (tree_to_shwi (loc));
19207 else if (tree_fits_uhwi_p (loc))
19208 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19209 else
19211 expansion_failed (loc, NULL_RTX,
19212 "Integer operand is not host integer");
19213 return 0;
19215 break;
19217 case POLY_INT_CST:
19219 if (want_address)
19221 expansion_failed (loc, NULL_RTX,
19222 "constant address with a runtime component");
19223 return 0;
19225 poly_int64 value;
19226 if (!poly_int_tree_p (loc, &value))
19228 expansion_failed (loc, NULL_RTX, "constant too big");
19229 return 0;
19231 ret = int_loc_descriptor (value);
19233 break;
19235 case CONSTRUCTOR:
19236 case REAL_CST:
19237 case STRING_CST:
19238 case COMPLEX_CST:
19239 if ((ret = cst_pool_loc_descr (loc)))
19240 have_address = 1;
19241 else if (TREE_CODE (loc) == CONSTRUCTOR)
19243 tree type = TREE_TYPE (loc);
19244 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19245 unsigned HOST_WIDE_INT offset = 0;
19246 unsigned HOST_WIDE_INT cnt;
19247 constructor_elt *ce;
19249 if (TREE_CODE (type) == RECORD_TYPE)
19251 /* This is very limited, but it's enough to output
19252 pointers to member functions, as long as the
19253 referenced function is defined in the current
19254 translation unit. */
19255 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19257 tree val = ce->value;
19259 tree field = ce->index;
19261 if (val)
19262 STRIP_NOPS (val);
19264 if (!field || DECL_BIT_FIELD (field))
19266 expansion_failed (loc, NULL_RTX,
19267 "bitfield in record type constructor");
19268 size = offset = (unsigned HOST_WIDE_INT)-1;
19269 ret = NULL;
19270 break;
19273 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19274 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19275 gcc_assert (pos + fieldsize <= size);
19276 if (pos < offset)
19278 expansion_failed (loc, NULL_RTX,
19279 "out-of-order fields in record constructor");
19280 size = offset = (unsigned HOST_WIDE_INT)-1;
19281 ret = NULL;
19282 break;
19284 if (pos > offset)
19286 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19287 add_loc_descr (&ret, ret1);
19288 offset = pos;
19290 if (val && fieldsize != 0)
19292 ret1 = loc_descriptor_from_tree (val, want_address, context);
19293 if (!ret1)
19295 expansion_failed (loc, NULL_RTX,
19296 "unsupported expression in field");
19297 size = offset = (unsigned HOST_WIDE_INT)-1;
19298 ret = NULL;
19299 break;
19301 add_loc_descr (&ret, ret1);
19303 if (fieldsize)
19305 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19306 add_loc_descr (&ret, ret1);
19307 offset = pos + fieldsize;
19311 if (offset != size)
19313 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19314 add_loc_descr (&ret, ret1);
19315 offset = size;
19318 have_address = !!want_address;
19320 else
19321 expansion_failed (loc, NULL_RTX,
19322 "constructor of non-record type");
19324 else
19325 /* We can construct small constants here using int_loc_descriptor. */
19326 expansion_failed (loc, NULL_RTX,
19327 "constructor or constant not in constant pool");
19328 break;
19330 case TRUTH_AND_EXPR:
19331 case TRUTH_ANDIF_EXPR:
19332 case BIT_AND_EXPR:
19333 op = DW_OP_and;
19334 goto do_binop;
19336 case TRUTH_XOR_EXPR:
19337 case BIT_XOR_EXPR:
19338 op = DW_OP_xor;
19339 goto do_binop;
19341 case TRUTH_OR_EXPR:
19342 case TRUTH_ORIF_EXPR:
19343 case BIT_IOR_EXPR:
19344 op = DW_OP_or;
19345 goto do_binop;
19347 case EXACT_DIV_EXPR:
19348 case FLOOR_DIV_EXPR:
19349 case TRUNC_DIV_EXPR:
19350 /* Turn a divide by a power of 2 into a shift when possible. */
19351 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19352 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19354 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19355 if (log2 > 0)
19357 list_ret
19358 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19359 if (list_ret == 0)
19360 return 0;
19362 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19363 add_loc_descr_to_each (list_ret,
19364 new_loc_descr (DW_OP_shr, 0, 0));
19365 break;
19369 /* fall through */
19371 case CEIL_DIV_EXPR:
19372 case ROUND_DIV_EXPR:
19373 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19375 const enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19376 scalar_int_mode int_mode;
19378 if ((dwarf_strict && dwarf_version < 5)
19379 || !is_a <scalar_int_mode> (mode, &int_mode))
19380 return 0;
19382 /* We can use a signed divide if the sign bit is not set. */
19383 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19385 op = DW_OP_div;
19386 goto do_binop;
19389 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19390 base_type_for_mode (int_mode, 1),
19391 int_mode, context);
19392 break;
19394 op = DW_OP_div;
19395 goto do_binop;
19397 case MINUS_EXPR:
19398 op = DW_OP_minus;
19399 goto do_binop;
19401 case FLOOR_MOD_EXPR:
19402 case CEIL_MOD_EXPR:
19403 case ROUND_MOD_EXPR:
19404 case TRUNC_MOD_EXPR:
19405 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19407 op = DW_OP_mod;
19408 goto do_binop;
19410 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19411 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19412 if (list_ret == 0 || list_ret1 == 0)
19413 return 0;
19415 add_loc_list (&list_ret, list_ret1);
19416 if (list_ret == 0)
19417 return 0;
19418 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19419 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19420 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19421 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19422 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19423 break;
19425 case MULT_EXPR:
19426 op = DW_OP_mul;
19427 goto do_binop;
19429 case LSHIFT_EXPR:
19430 op = DW_OP_shl;
19431 goto do_binop;
19433 case RSHIFT_EXPR:
19434 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19435 goto do_binop;
19437 case POINTER_PLUS_EXPR:
19438 case PLUS_EXPR:
19439 do_plus:
19440 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19442 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19443 smarter to encode their opposite. The DW_OP_plus_uconst operation
19444 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19445 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19446 bytes, Y being the size of the operation that pushes the opposite
19447 of the addend. So let's choose the smallest representation. */
19448 const tree tree_addend = TREE_OPERAND (loc, 1);
19449 offset_int wi_addend;
19450 HOST_WIDE_INT shwi_addend;
19451 dw_loc_descr_ref loc_naddend;
19453 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19454 if (list_ret == 0)
19455 return 0;
19457 /* Try to get the literal to push. It is the opposite of the addend,
19458 so as we rely on wrapping during DWARF evaluation, first decode
19459 the literal as a "DWARF-sized" signed number. */
19460 wi_addend = wi::to_offset (tree_addend);
19461 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19462 shwi_addend = wi_addend.to_shwi ();
19463 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19464 ? int_loc_descriptor (-shwi_addend)
19465 : NULL;
19467 if (loc_naddend != NULL
19468 && ((unsigned) size_of_uleb128 (shwi_addend)
19469 > size_of_loc_descr (loc_naddend)))
19471 add_loc_descr_to_each (list_ret, loc_naddend);
19472 add_loc_descr_to_each (list_ret,
19473 new_loc_descr (DW_OP_minus, 0, 0));
19475 else
19477 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19479 loc_naddend = loc_cur;
19480 loc_cur = loc_cur->dw_loc_next;
19481 ggc_free (loc_naddend);
19483 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19485 break;
19488 op = DW_OP_plus;
19489 goto do_binop;
19491 case LE_EXPR:
19492 op = DW_OP_le;
19493 goto do_comp_binop;
19495 case GE_EXPR:
19496 op = DW_OP_ge;
19497 goto do_comp_binop;
19499 case LT_EXPR:
19500 op = DW_OP_lt;
19501 goto do_comp_binop;
19503 case GT_EXPR:
19504 op = DW_OP_gt;
19505 goto do_comp_binop;
19507 do_comp_binop:
19508 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19510 const enum machine_mode mode
19511 = TYPE_MODE (TREE_TYPE (TREE_OPERAND (loc, 0)));
19512 scalar_int_mode int_mode;
19514 /* We can use a signed comparison if the sign bit is not set. */
19515 if (is_a <scalar_int_mode> (mode, &int_mode)
19516 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19517 goto do_binop;
19519 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19520 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19521 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19522 TREE_CODE (loc));
19523 break;
19525 else
19526 goto do_binop;
19528 case EQ_EXPR:
19529 op = DW_OP_eq;
19530 goto do_binop;
19532 case NE_EXPR:
19533 op = DW_OP_ne;
19534 goto do_binop;
19536 do_binop:
19537 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19538 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19539 if (list_ret == 0 || list_ret1 == 0)
19540 return 0;
19542 add_loc_list (&list_ret, list_ret1);
19543 if (list_ret == 0)
19544 return 0;
19546 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19547 break;
19549 case TRUTH_NOT_EXPR:
19550 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19551 if (list_ret == 0)
19552 return 0;
19554 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_lit0, 0, 0));
19555 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_eq, 0, 0));
19556 break;
19558 case BIT_NOT_EXPR:
19559 op = DW_OP_not;
19560 goto do_unop;
19562 case ABS_EXPR:
19563 op = DW_OP_abs;
19564 goto do_unop;
19566 case NEGATE_EXPR:
19567 op = DW_OP_neg;
19568 goto do_unop;
19570 do_unop:
19571 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19572 if (list_ret == 0)
19573 return 0;
19575 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19576 break;
19578 case MIN_EXPR:
19579 case MAX_EXPR:
19581 const enum tree_code code =
19582 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19584 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19585 build2 (code, integer_type_node,
19586 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19587 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19590 /* fall through */
19592 case COND_EXPR:
19594 dw_loc_descr_ref lhs
19595 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19596 dw_loc_list_ref rhs
19597 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19598 dw_loc_descr_ref bra_node, jump_node, tmp;
19600 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19601 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19602 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19603 list_ret
19604 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19605 0, context);
19606 /* Likewise, swap the operands for a logically negated condition. */
19607 else if (TREE_CODE (TREE_OPERAND (loc, 0)) == TRUTH_NOT_EXPR)
19609 lhs = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0, context);
19610 rhs = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19611 list_ret
19612 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19613 0, context);
19615 else
19616 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19617 if (list_ret == 0 || lhs == 0 || rhs == 0)
19618 return 0;
19620 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19621 add_loc_descr_to_each (list_ret, bra_node);
19623 add_loc_list (&list_ret, rhs);
19624 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19625 add_loc_descr_to_each (list_ret, jump_node);
19627 add_loc_descr_to_each (list_ret, lhs);
19628 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19629 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19631 /* ??? Need a node to point the skip at. Use a nop. */
19632 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19633 add_loc_descr_to_each (list_ret, tmp);
19634 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19635 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19637 break;
19639 case FIX_TRUNC_EXPR:
19640 return 0;
19642 case COMPOUND_LITERAL_EXPR:
19643 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19644 0, context);
19646 default:
19647 /* Leave front-end specific codes as simply unknown. This comes
19648 up, for instance, with the C STMT_EXPR. */
19649 if ((unsigned int) TREE_CODE (loc)
19650 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19652 expansion_failed (loc, NULL_RTX,
19653 "language specific tree node");
19654 return 0;
19657 /* Otherwise this is a generic code; we should just lists all of
19658 these explicitly. We forgot one. */
19659 if (flag_checking)
19660 gcc_unreachable ();
19662 /* In a release build, we want to degrade gracefully: better to
19663 generate incomplete debugging information than to crash. */
19664 return NULL;
19667 if (!ret && !list_ret)
19668 return 0;
19670 /* Implement wrap-around arithmetics for small integer types. */
19671 if ((TREE_CODE (loc) == PLUS_EXPR
19672 || TREE_CODE (loc) == MINUS_EXPR
19673 || TREE_CODE (loc) == MULT_EXPR
19674 || TREE_CODE (loc) == NEGATE_EXPR
19675 || TREE_CODE (loc) == LSHIFT_EXPR)
19676 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
19677 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (loc)))
19679 const enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19680 scalar_int_mode int_mode;
19682 if (is_a <scalar_int_mode> (mode, &int_mode)
19683 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19685 const unsigned HOST_WIDE_INT mask
19686 = (HOST_WIDE_INT_1U << GET_MODE_BITSIZE (int_mode)) - 1;
19687 add_loc_descr_to_each (list_ret, uint_loc_descriptor (mask));
19688 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_and, 0, 0));
19692 if (want_address == 2 && !have_address
19693 && (dwarf_version >= 4 || !dwarf_strict))
19695 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19697 expansion_failed (loc, NULL_RTX,
19698 "DWARF address size mismatch");
19699 return 0;
19701 if (ret)
19702 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19703 else
19704 add_loc_descr_to_each (list_ret,
19705 new_loc_descr (DW_OP_stack_value, 0, 0));
19706 have_address = 1;
19708 /* Show if we can't fill the request for an address. */
19709 if (want_address && !have_address)
19711 expansion_failed (loc, NULL_RTX,
19712 "Want address and only have value");
19713 return 0;
19716 gcc_assert (!ret || !list_ret);
19718 /* If we've got an address and don't want one, dereference. */
19719 if (!want_address && have_address)
19721 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19722 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19723 scalar_int_mode int_mode;
19724 dw_die_ref type_die;
19725 dw_loc_descr_ref deref;
19727 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19728 if (size > DWARF2_ADDR_SIZE || size == -1)
19730 expansion_failed (loc, NULL_RTX,
19731 "DWARF address size mismatch");
19732 return 0;
19735 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19736 else if (size == DWARF2_ADDR_SIZE)
19737 deref = new_loc_descr (DW_OP_deref, size, 0);
19739 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19740 extend the value, which is really OK for unsigned types only. */
19741 else if (!(context && context->strict_signedness)
19742 || TYPE_UNSIGNED (TREE_TYPE (loc))
19743 || (dwarf_strict && dwarf_version < 5)
19744 || !is_a <scalar_int_mode> (mode, &int_mode)
19745 || !(type_die = base_type_for_mode (mode, false)))
19746 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19748 /* Use DW_OP_deref_type for signed integral types if possible, but
19749 convert back to the generic type to avoid type mismatches later. */
19750 else
19752 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19753 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19754 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19755 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19756 add_loc_descr (&deref,
19757 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19760 if (ret)
19761 add_loc_descr (&ret, deref);
19762 else
19763 add_loc_descr_to_each (list_ret, deref);
19766 if (ret)
19767 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19769 return list_ret;
19772 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19773 expressions. */
19775 static dw_loc_list_ref
19776 loc_list_from_tree (tree loc, int want_address,
19777 struct loc_descr_context *context)
19779 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19781 for (dw_loc_list_ref loc_cur = result;
19782 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19783 loc_descr_without_nops (loc_cur->expr);
19784 return result;
19787 /* Same as above but return only single location expression. */
19788 static dw_loc_descr_ref
19789 loc_descriptor_from_tree (tree loc, int want_address,
19790 struct loc_descr_context *context)
19792 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19793 if (!ret)
19794 return NULL;
19795 if (ret->dw_loc_next)
19797 expansion_failed (loc, NULL_RTX,
19798 "Location list where only loc descriptor needed");
19799 return NULL;
19801 return ret->expr;
19804 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19805 pointer to the declared type for the relevant field variable, or return
19806 `integer_type_node' if the given node turns out to be an
19807 ERROR_MARK node. */
19809 static inline tree
19810 field_type (const_tree decl)
19812 tree type;
19814 if (TREE_CODE (decl) == ERROR_MARK)
19815 return integer_type_node;
19817 type = DECL_BIT_FIELD_TYPE (decl);
19818 if (type == NULL_TREE)
19819 type = TREE_TYPE (decl);
19821 return type;
19824 /* Given a pointer to a tree node, return the alignment in bits for
19825 it, or else return BITS_PER_WORD if the node actually turns out to
19826 be an ERROR_MARK node. */
19828 static inline unsigned
19829 simple_type_align_in_bits (const_tree type)
19831 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19834 static inline unsigned
19835 simple_decl_align_in_bits (const_tree decl)
19837 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19840 /* Return the result of rounding T up to ALIGN. */
19842 static inline offset_int
19843 round_up_to_align (const offset_int &t, unsigned int align)
19845 return wi::udiv_trunc (t + align - 1, align) * align;
19848 /* Helper structure for RECORD_TYPE processing. */
19849 struct vlr_context
19851 /* Root RECORD_TYPE. It is needed to generate data member location
19852 descriptions in variable-length records (VLR), but also to cope with
19853 variants, which are composed of nested structures multiplexed with
19854 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19855 function processing a FIELD_DECL, it is required to be non null. */
19856 tree struct_type;
19858 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19859 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19860 this variant part as part of the root record (in storage units). For
19861 regular records, it must be NULL_TREE. */
19862 tree variant_part_offset;
19865 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19866 addressed byte of the "containing object" for the given FIELD_DECL. If
19867 possible, return a native constant through CST_OFFSET (in which case NULL is
19868 returned); otherwise return a DWARF expression that computes the offset.
19870 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19871 that offset is, either because the argument turns out to be a pointer to an
19872 ERROR_MARK node, or because the offset expression is too complex for us.
19874 CTX is required: see the comment for VLR_CONTEXT. */
19876 static dw_loc_descr_ref
19877 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19878 HOST_WIDE_INT *cst_offset)
19880 tree tree_result;
19881 dw_loc_list_ref loc_result;
19883 *cst_offset = 0;
19885 if (TREE_CODE (decl) == ERROR_MARK)
19886 return NULL;
19887 else
19888 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19890 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19891 case. */
19892 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19893 return NULL;
19895 /* We used to handle only constant offsets in all cases. Now, we handle
19896 properly dynamic byte offsets only when PCC bitfield type doesn't
19897 matter. */
19898 if (PCC_BITFIELD_TYPE_MATTERS
19899 && DECL_BIT_FIELD_TYPE (decl)
19900 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19902 offset_int object_offset_in_bits;
19903 offset_int object_offset_in_bytes;
19904 offset_int bitpos_int;
19905 tree type;
19906 tree field_size_tree;
19907 offset_int deepest_bitpos;
19908 offset_int field_size_in_bits;
19909 unsigned int type_align_in_bits;
19910 unsigned int decl_align_in_bits;
19911 offset_int type_size_in_bits;
19913 bitpos_int = wi::to_offset (bit_position (decl));
19914 type = field_type (decl);
19915 type_size_in_bits = offset_int_type_size_in_bits (type);
19916 type_align_in_bits = simple_type_align_in_bits (type);
19918 field_size_tree = DECL_SIZE (decl);
19920 /* The size could be unspecified if there was an error, or for
19921 a flexible array member. */
19922 if (!field_size_tree)
19923 field_size_tree = bitsize_zero_node;
19925 /* If the size of the field is not constant, use the type size. */
19926 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19927 field_size_in_bits = wi::to_offset (field_size_tree);
19928 else
19929 field_size_in_bits = type_size_in_bits;
19931 decl_align_in_bits = simple_decl_align_in_bits (decl);
19933 /* The GCC front-end doesn't make any attempt to keep track of the
19934 starting bit offset (relative to the start of the containing
19935 structure type) of the hypothetical "containing object" for a
19936 bit-field. Thus, when computing the byte offset value for the
19937 start of the "containing object" of a bit-field, we must deduce
19938 this information on our own. This can be rather tricky to do in
19939 some cases. For example, handling the following structure type
19940 definition when compiling for an i386/i486 target (which only
19941 aligns long long's to 32-bit boundaries) can be very tricky:
19943 struct S { int field1; long long field2:31; };
19945 Fortunately, there is a simple rule-of-thumb which can be used
19946 in such cases. When compiling for an i386/i486, GCC will
19947 allocate 8 bytes for the structure shown above. It decides to
19948 do this based upon one simple rule for bit-field allocation.
19949 GCC allocates each "containing object" for each bit-field at
19950 the first (i.e. lowest addressed) legitimate alignment boundary
19951 (based upon the required minimum alignment for the declared
19952 type of the field) which it can possibly use, subject to the
19953 condition that there is still enough available space remaining
19954 in the containing object (when allocated at the selected point)
19955 to fully accommodate all of the bits of the bit-field itself.
19957 This simple rule makes it obvious why GCC allocates 8 bytes for
19958 each object of the structure type shown above. When looking
19959 for a place to allocate the "containing object" for `field2',
19960 the compiler simply tries to allocate a 64-bit "containing
19961 object" at each successive 32-bit boundary (starting at zero)
19962 until it finds a place to allocate that 64- bit field such that
19963 at least 31 contiguous (and previously unallocated) bits remain
19964 within that selected 64 bit field. (As it turns out, for the
19965 example above, the compiler finds it is OK to allocate the
19966 "containing object" 64-bit field at bit-offset zero within the
19967 structure type.)
19969 Here we attempt to work backwards from the limited set of facts
19970 we're given, and we try to deduce from those facts, where GCC
19971 must have believed that the containing object started (within
19972 the structure type). The value we deduce is then used (by the
19973 callers of this routine) to generate DW_AT_location and
19974 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19975 the case of DW_AT_location, regular fields as well). */
19977 /* Figure out the bit-distance from the start of the structure to
19978 the "deepest" bit of the bit-field. */
19979 deepest_bitpos = bitpos_int + field_size_in_bits;
19981 /* This is the tricky part. Use some fancy footwork to deduce
19982 where the lowest addressed bit of the containing object must
19983 be. */
19984 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19986 /* Round up to type_align by default. This works best for
19987 bitfields. */
19988 object_offset_in_bits
19989 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19991 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19993 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19995 /* Round up to decl_align instead. */
19996 object_offset_in_bits
19997 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
20000 object_offset_in_bytes
20001 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
20002 if (ctx->variant_part_offset == NULL_TREE)
20004 *cst_offset = object_offset_in_bytes.to_shwi ();
20005 return NULL;
20007 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
20009 else
20010 tree_result = byte_position (decl);
20012 if (ctx->variant_part_offset != NULL_TREE)
20013 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
20014 ctx->variant_part_offset, tree_result);
20016 /* If the byte offset is a constant, it's simplier to handle a native
20017 constant rather than a DWARF expression. */
20018 if (TREE_CODE (tree_result) == INTEGER_CST)
20020 *cst_offset = wi::to_offset (tree_result).to_shwi ();
20021 return NULL;
20024 struct loc_descr_context loc_ctx = {
20025 ctx->struct_type, /* context_type */
20026 NULL_TREE, /* base_decl */
20027 NULL, /* dpi */
20028 false, /* placeholder_arg */
20029 false, /* placeholder_seen */
20030 false /* strict_signedness */
20032 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
20034 /* We want a DWARF expression: abort if we only have a location list with
20035 multiple elements. */
20036 if (!loc_result || !single_element_loc_list_p (loc_result))
20037 return NULL;
20038 else
20039 return loc_result->expr;
20042 /* The following routines define various Dwarf attributes and any data
20043 associated with them. */
20045 /* Add a location description attribute value to a DIE.
20047 This emits location attributes suitable for whole variables and
20048 whole parameters. Note that the location attributes for struct fields are
20049 generated by the routine `data_member_location_attribute' below. */
20051 static inline void
20052 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
20053 dw_loc_list_ref descr)
20055 bool check_no_locviews = true;
20056 if (descr == 0)
20057 return;
20058 if (single_element_loc_list_p (descr))
20059 add_AT_loc (die, attr_kind, descr->expr);
20060 else
20062 add_AT_loc_list (die, attr_kind, descr);
20063 gcc_assert (descr->ll_symbol);
20064 if (attr_kind == DW_AT_location && descr->vl_symbol
20065 && dwarf2out_locviews_in_attribute ())
20067 add_AT_view_list (die, DW_AT_GNU_locviews);
20068 check_no_locviews = false;
20072 if (check_no_locviews)
20073 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
20076 /* Add DW_AT_accessibility attribute to DIE if needed. */
20078 static void
20079 add_accessibility_attribute (dw_die_ref die, tree decl)
20081 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20082 children, otherwise the default is DW_ACCESS_public. In DWARF2
20083 the default has always been DW_ACCESS_public. */
20084 if (TREE_PROTECTED (decl))
20085 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20086 else if (TREE_PRIVATE (decl))
20088 if (dwarf_version == 2
20089 || die->die_parent == NULL
20090 || die->die_parent->die_tag != DW_TAG_class_type)
20091 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20093 else if (dwarf_version > 2
20094 && die->die_parent
20095 && die->die_parent->die_tag == DW_TAG_class_type)
20096 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20099 /* Attach the specialized form of location attribute used for data members of
20100 struct and union types. In the special case of a FIELD_DECL node which
20101 represents a bit-field, the "offset" part of this special location
20102 descriptor must indicate the distance in bytes from the lowest-addressed
20103 byte of the containing struct or union type to the lowest-addressed byte of
20104 the "containing object" for the bit-field. (See the `field_byte_offset'
20105 function above).
20107 For any given bit-field, the "containing object" is a hypothetical object
20108 (of some integral or enum type) within which the given bit-field lives. The
20109 type of this hypothetical "containing object" is always the same as the
20110 declared type of the individual bit-field itself (for GCC anyway... the
20111 DWARF spec doesn't actually mandate this). Note that it is the size (in
20112 bytes) of the hypothetical "containing object" which will be given in the
20113 DW_AT_byte_size attribute for this bit-field. (See the
20114 `byte_size_attribute' function below.) It is also used when calculating the
20115 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
20116 function below.)
20118 CTX is required: see the comment for VLR_CONTEXT. */
20120 static void
20121 add_data_member_location_attribute (dw_die_ref die,
20122 tree decl,
20123 struct vlr_context *ctx)
20125 HOST_WIDE_INT offset;
20126 dw_loc_descr_ref loc_descr = 0;
20128 if (TREE_CODE (decl) == TREE_BINFO)
20130 /* We're working on the TAG_inheritance for a base class. */
20131 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
20133 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
20134 aren't at a fixed offset from all (sub)objects of the same
20135 type. We need to extract the appropriate offset from our
20136 vtable. The following dwarf expression means
20138 BaseAddr = ObAddr + *((*ObAddr) - Offset)
20140 This is specific to the V3 ABI, of course. */
20142 dw_loc_descr_ref tmp;
20144 /* Make a copy of the object address. */
20145 tmp = new_loc_descr (DW_OP_dup, 0, 0);
20146 add_loc_descr (&loc_descr, tmp);
20148 /* Extract the vtable address. */
20149 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20150 add_loc_descr (&loc_descr, tmp);
20152 /* Calculate the address of the offset. */
20153 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
20154 gcc_assert (offset < 0);
20156 tmp = int_loc_descriptor (-offset);
20157 add_loc_descr (&loc_descr, tmp);
20158 tmp = new_loc_descr (DW_OP_minus, 0, 0);
20159 add_loc_descr (&loc_descr, tmp);
20161 /* Extract the offset. */
20162 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20163 add_loc_descr (&loc_descr, tmp);
20165 /* Add it to the object address. */
20166 tmp = new_loc_descr (DW_OP_plus, 0, 0);
20167 add_loc_descr (&loc_descr, tmp);
20169 else
20170 offset = tree_to_shwi (BINFO_OFFSET (decl));
20172 else
20174 loc_descr = field_byte_offset (decl, ctx, &offset);
20176 if (!loc_descr)
20179 /* If loc_descr is available, then we know the offset is dynamic. */
20180 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
20182 loc_descr = NULL;
20183 offset = 0;
20186 /* Data member location evaluation starts with the base address on the
20187 stack. Compute the field offset and add it to this base address. */
20188 else
20189 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
20192 if (!loc_descr)
20194 /* While DW_AT_data_bit_offset has been added already in DWARF4,
20195 e.g. GDB only added support to it in November 2016. For DWARF5
20196 we need newer debug info consumers anyway. We might change this
20197 to dwarf_version >= 4 once most consumers catched up. */
20198 if (dwarf_version >= 5
20199 && TREE_CODE (decl) == FIELD_DECL
20200 && DECL_BIT_FIELD_TYPE (decl)
20201 && (ctx->variant_part_offset == NULL_TREE
20202 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
20204 tree off = bit_position (decl);
20205 if (ctx->variant_part_offset)
20206 off = bit_from_pos (ctx->variant_part_offset, off);
20207 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
20209 remove_AT (die, DW_AT_byte_size);
20210 remove_AT (die, DW_AT_bit_offset);
20211 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
20212 return;
20215 if (dwarf_version > 2)
20217 /* Don't need to output a location expression, just the constant. */
20218 if (offset < 0)
20219 add_AT_int (die, DW_AT_data_member_location, offset);
20220 else
20221 add_AT_unsigned (die, DW_AT_data_member_location, offset);
20222 return;
20224 else
20226 enum dwarf_location_atom op;
20228 /* The DWARF2 standard says that we should assume that the structure
20229 address is already on the stack, so we can specify a structure
20230 field address by using DW_OP_plus_uconst. */
20231 op = DW_OP_plus_uconst;
20232 loc_descr = new_loc_descr (op, offset, 0);
20236 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
20239 /* Writes integer values to dw_vec_const array. */
20241 static void
20242 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
20244 while (size != 0)
20246 *dest++ = val & 0xff;
20247 val >>= 8;
20248 --size;
20252 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20254 static HOST_WIDE_INT
20255 extract_int (const unsigned char *src, unsigned int size)
20257 HOST_WIDE_INT val = 0;
20259 src += size;
20260 while (size != 0)
20262 val <<= 8;
20263 val |= *--src & 0xff;
20264 --size;
20266 return val;
20269 /* Writes wide_int values to dw_vec_const array. */
20271 static void
20272 insert_wide_int (const wide_int_ref &val, unsigned char *dest, int elt_size)
20274 int i;
20276 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20278 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20279 return;
20282 /* We'd have to extend this code to support odd sizes. */
20283 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20285 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20287 if (WORDS_BIG_ENDIAN)
20288 for (i = n - 1; i >= 0; i--)
20290 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20291 dest += sizeof (HOST_WIDE_INT);
20293 else
20294 for (i = 0; i < n; i++)
20296 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20297 dest += sizeof (HOST_WIDE_INT);
20301 /* Writes floating point values to dw_vec_const array. */
20303 static unsigned
20304 insert_float (const_rtx rtl, unsigned char *array)
20306 long val[4];
20307 int i;
20308 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20310 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20312 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20313 if (GET_MODE_SIZE (mode) < 4)
20315 gcc_assert (GET_MODE_SIZE (mode) == 2);
20316 insert_int (val[0], 2, array);
20317 return 2;
20320 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20322 insert_int (val[i], 4, array);
20323 array += 4;
20325 return 4;
20328 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20329 does not have a "location" either in memory or in a register. These
20330 things can arise in GNU C when a constant is passed as an actual parameter
20331 to an inlined function. They can also arise in C++ where declared
20332 constants do not necessarily get memory "homes". */
20334 static bool
20335 add_const_value_attribute (dw_die_ref die, machine_mode mode, rtx rtl)
20337 scalar_mode int_mode;
20339 switch (GET_CODE (rtl))
20341 case CONST_INT:
20343 HOST_WIDE_INT val = INTVAL (rtl);
20345 if (val < 0)
20346 add_AT_int (die, DW_AT_const_value, val);
20347 else
20348 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20350 return true;
20352 case CONST_WIDE_INT:
20353 if (is_int_mode (mode, &int_mode)
20354 && (GET_MODE_PRECISION (int_mode)
20355 & (HOST_BITS_PER_WIDE_INT - 1)) == 0)
20357 add_AT_wide (die, DW_AT_const_value, rtx_mode_t (rtl, int_mode));
20358 return true;
20360 return false;
20362 case CONST_DOUBLE:
20363 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20364 floating-point constant. A CONST_DOUBLE is used whenever the
20365 constant requires more than one word in order to be adequately
20366 represented. */
20367 if (TARGET_SUPPORTS_WIDE_INT == 0
20368 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20369 add_AT_double (die, DW_AT_const_value,
20370 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20371 else
20373 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20374 unsigned int length = GET_MODE_SIZE (mode);
20375 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20376 unsigned int elt_size = insert_float (rtl, array);
20378 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20379 array);
20381 return true;
20383 case CONST_VECTOR:
20385 unsigned int length;
20386 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20387 return false;
20389 machine_mode mode = GET_MODE (rtl);
20390 /* The combination of a length and byte elt_size doesn't extend
20391 naturally to boolean vectors, where several elements are packed
20392 into the same byte. */
20393 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20394 return false;
20396 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20397 unsigned char *array
20398 = ggc_vec_alloc<unsigned char> (length * elt_size);
20399 unsigned int i;
20400 unsigned char *p;
20401 machine_mode imode = GET_MODE_INNER (mode);
20403 switch (GET_MODE_CLASS (mode))
20405 case MODE_VECTOR_INT:
20406 for (i = 0, p = array; i < length; i++, p += elt_size)
20408 rtx elt = CONST_VECTOR_ELT (rtl, i);
20409 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20411 break;
20413 case MODE_VECTOR_FLOAT:
20414 for (i = 0, p = array; i < length; i++, p += elt_size)
20416 rtx elt = CONST_VECTOR_ELT (rtl, i);
20417 insert_float (elt, p);
20419 break;
20421 default:
20422 gcc_unreachable ();
20425 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20427 return true;
20429 case CONST_STRING:
20430 if (dwarf_version >= 4 || !dwarf_strict)
20432 dw_loc_descr_ref loc_result;
20433 resolve_one_addr (&rtl);
20434 rtl_addr:
20435 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20436 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20437 add_AT_loc (die, DW_AT_location, loc_result);
20438 vec_safe_push (used_rtx_array, rtl);
20439 return true;
20441 return false;
20443 case CONST:
20444 if (CONSTANT_P (XEXP (rtl, 0)))
20445 return add_const_value_attribute (die, mode, XEXP (rtl, 0));
20446 /* FALLTHROUGH */
20447 case SYMBOL_REF:
20448 if (!const_ok_for_output (rtl))
20449 return false;
20450 /* FALLTHROUGH */
20451 case LABEL_REF:
20452 if (dwarf_version >= 4 || !dwarf_strict)
20453 goto rtl_addr;
20454 return false;
20456 case PLUS:
20457 /* In cases where an inlined instance of an inline function is passed
20458 the address of an `auto' variable (which is local to the caller) we
20459 can get a situation where the DECL_RTL of the artificial local
20460 variable (for the inlining) which acts as a stand-in for the
20461 corresponding formal parameter (of the inline function) will look
20462 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20463 exactly a compile-time constant expression, but it isn't the address
20464 of the (artificial) local variable either. Rather, it represents the
20465 *value* which the artificial local variable always has during its
20466 lifetime. We currently have no way to represent such quasi-constant
20467 values in Dwarf, so for now we just punt and generate nothing. */
20468 return false;
20470 case HIGH:
20471 case CONST_FIXED:
20472 case MINUS:
20473 case SIGN_EXTEND:
20474 case ZERO_EXTEND:
20475 case CONST_POLY_INT:
20476 return false;
20478 case MEM:
20479 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20480 && MEM_READONLY_P (rtl)
20481 && GET_MODE (rtl) == BLKmode)
20483 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20484 return true;
20486 return false;
20488 default:
20489 /* No other kinds of rtx should be possible here. */
20490 gcc_unreachable ();
20494 /* Determine whether the evaluation of EXPR references any variables
20495 or functions which aren't otherwise used (and therefore may not be
20496 output). */
20497 static tree
20498 reference_to_unused (tree * tp, int * walk_subtrees,
20499 void * data ATTRIBUTE_UNUSED)
20501 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20502 *walk_subtrees = 0;
20504 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20505 && ! TREE_ASM_WRITTEN (*tp))
20506 return *tp;
20507 /* ??? The C++ FE emits debug information for using decls, so
20508 putting gcc_unreachable here falls over. See PR31899. For now
20509 be conservative. */
20510 else if (!symtab->global_info_ready && VAR_P (*tp))
20511 return *tp;
20512 else if (VAR_P (*tp))
20514 varpool_node *node = varpool_node::get (*tp);
20515 if (!node || !node->definition)
20516 return *tp;
20518 else if (TREE_CODE (*tp) == FUNCTION_DECL
20519 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20521 /* The call graph machinery must have finished analyzing,
20522 optimizing and gimplifying the CU by now.
20523 So if *TP has no call graph node associated
20524 to it, it means *TP will not be emitted. */
20525 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20526 return *tp;
20528 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20529 return *tp;
20531 return NULL_TREE;
20534 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20535 for use in a later add_const_value_attribute call. */
20537 static rtx
20538 rtl_for_decl_init (tree init, tree type)
20540 rtx rtl = NULL_RTX;
20542 STRIP_NOPS (init);
20544 /* If a variable is initialized with a string constant without embedded
20545 zeros, build CONST_STRING. */
20546 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20548 tree enttype = TREE_TYPE (type);
20549 tree domain = TYPE_DOMAIN (type);
20550 scalar_int_mode mode;
20552 if (is_int_mode (TYPE_MODE (enttype), &mode)
20553 && GET_MODE_SIZE (mode) == 1
20554 && domain
20555 && TYPE_MAX_VALUE (domain)
20556 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20557 && integer_zerop (TYPE_MIN_VALUE (domain))
20558 && compare_tree_int (TYPE_MAX_VALUE (domain),
20559 TREE_STRING_LENGTH (init) - 1) == 0
20560 && ((size_t) TREE_STRING_LENGTH (init)
20561 == strlen (TREE_STRING_POINTER (init)) + 1))
20563 rtl = gen_rtx_CONST_STRING (VOIDmode,
20564 ggc_strdup (TREE_STRING_POINTER (init)));
20565 rtl = gen_rtx_MEM (BLKmode, rtl);
20566 MEM_READONLY_P (rtl) = 1;
20569 /* Other aggregates, and complex values, could be represented using
20570 CONCAT: FIXME!
20571 If this changes, please adjust tree_add_const_value_attribute
20572 so that for early_dwarf it will for such initializers mangle referenced
20573 decls. */
20574 else if (AGGREGATE_TYPE_P (type)
20575 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20576 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20577 || TREE_CODE (type) == COMPLEX_TYPE)
20579 /* Vectors only work if their mode is supported by the target.
20580 FIXME: generic vectors ought to work too. */
20581 else if (TREE_CODE (type) == VECTOR_TYPE
20582 && !VECTOR_MODE_P (TYPE_MODE (type)))
20584 /* If the initializer is something that we know will expand into an
20585 immediate RTL constant, expand it now. We must be careful not to
20586 reference variables which won't be output. */
20587 else if (initializer_constant_valid_p (init, type)
20588 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20590 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20591 possible. */
20592 if (TREE_CODE (type) == VECTOR_TYPE)
20593 switch (TREE_CODE (init))
20595 case VECTOR_CST:
20596 break;
20597 case CONSTRUCTOR:
20598 if (TREE_CONSTANT (init))
20600 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20601 bool constant_p = true;
20602 tree value;
20603 unsigned HOST_WIDE_INT ix;
20605 /* Even when ctor is constant, it might contain non-*_CST
20606 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20607 belong into VECTOR_CST nodes. */
20608 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20609 if (!CONSTANT_CLASS_P (value))
20611 constant_p = false;
20612 break;
20615 if (constant_p)
20617 init = build_vector_from_ctor (type, elts);
20618 break;
20621 /* FALLTHRU */
20623 default:
20624 return NULL;
20627 /* Large _BitInt BLKmode INTEGER_CSTs would yield a MEM. */
20628 if (TREE_CODE (init) == INTEGER_CST
20629 && TREE_CODE (TREE_TYPE (init)) == BITINT_TYPE
20630 && TYPE_MODE (TREE_TYPE (init)) == BLKmode)
20632 if (tree_fits_shwi_p (init))
20633 return GEN_INT (tree_to_shwi (init));
20634 else
20635 return NULL;
20638 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20640 /* If expand_expr returns a MEM, it wasn't immediate. */
20641 gcc_assert (!rtl || !MEM_P (rtl));
20644 return rtl;
20647 /* Generate RTL for the variable DECL to represent its location. */
20649 static rtx
20650 rtl_for_decl_location (tree decl)
20652 rtx rtl;
20654 /* Here we have to decide where we are going to say the parameter "lives"
20655 (as far as the debugger is concerned). We only have a couple of
20656 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20658 DECL_RTL normally indicates where the parameter lives during most of the
20659 activation of the function. If optimization is enabled however, this
20660 could be either NULL or else a pseudo-reg. Both of those cases indicate
20661 that the parameter doesn't really live anywhere (as far as the code
20662 generation parts of GCC are concerned) during most of the function's
20663 activation. That will happen (for example) if the parameter is never
20664 referenced within the function.
20666 We could just generate a location descriptor here for all non-NULL
20667 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20668 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20669 where DECL_RTL is NULL or is a pseudo-reg.
20671 Note however that we can only get away with using DECL_INCOMING_RTL as
20672 a backup substitute for DECL_RTL in certain limited cases. In cases
20673 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20674 we can be sure that the parameter was passed using the same type as it is
20675 declared to have within the function, and that its DECL_INCOMING_RTL
20676 points us to a place where a value of that type is passed.
20678 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20679 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20680 because in these cases DECL_INCOMING_RTL points us to a value of some
20681 type which is *different* from the type of the parameter itself. Thus,
20682 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20683 such cases, the debugger would end up (for example) trying to fetch a
20684 `float' from a place which actually contains the first part of a
20685 `double'. That would lead to really incorrect and confusing
20686 output at debug-time.
20688 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20689 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20690 are a couple of exceptions however. On little-endian machines we can
20691 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20692 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20693 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20694 when (on a little-endian machine) a non-prototyped function has a
20695 parameter declared to be of type `short' or `char'. In such cases,
20696 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20697 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20698 passed `int' value. If the debugger then uses that address to fetch
20699 a `short' or a `char' (on a little-endian machine) the result will be
20700 the correct data, so we allow for such exceptional cases below.
20702 Note that our goal here is to describe the place where the given formal
20703 parameter lives during most of the function's activation (i.e. between the
20704 end of the prologue and the start of the epilogue). We'll do that as best
20705 as we can. Note however that if the given formal parameter is modified
20706 sometime during the execution of the function, then a stack backtrace (at
20707 debug-time) will show the function as having been called with the *new*
20708 value rather than the value which was originally passed in. This happens
20709 rarely enough that it is not a major problem, but it *is* a problem, and
20710 I'd like to fix it.
20712 A future version of dwarf2out.cc may generate two additional attributes for
20713 any given DW_TAG_formal_parameter DIE which will describe the "passed
20714 type" and the "passed location" for the given formal parameter in addition
20715 to the attributes we now generate to indicate the "declared type" and the
20716 "active location" for each parameter. This additional set of attributes
20717 could be used by debuggers for stack backtraces. Separately, note that
20718 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20719 This happens (for example) for inlined-instances of inline function formal
20720 parameters which are never referenced. This really shouldn't be
20721 happening. All PARM_DECL nodes should get valid non-NULL
20722 DECL_INCOMING_RTL values. FIXME. */
20724 /* Use DECL_RTL as the "location" unless we find something better. */
20725 rtl = DECL_RTL_IF_SET (decl);
20727 /* When generating abstract instances, ignore everything except
20728 constants, symbols living in memory, and symbols living in
20729 fixed registers. */
20730 if (! reload_completed)
20732 if (rtl
20733 && (CONSTANT_P (rtl)
20734 || (MEM_P (rtl)
20735 && CONSTANT_P (XEXP (rtl, 0)))
20736 || (REG_P (rtl)
20737 && VAR_P (decl)
20738 && TREE_STATIC (decl))))
20740 rtl = targetm.delegitimize_address (rtl);
20741 return rtl;
20743 rtl = NULL_RTX;
20745 else if (TREE_CODE (decl) == PARM_DECL)
20747 if (rtl == NULL_RTX
20748 || is_pseudo_reg (rtl)
20749 || (MEM_P (rtl)
20750 && is_pseudo_reg (XEXP (rtl, 0))
20751 && DECL_INCOMING_RTL (decl)
20752 && MEM_P (DECL_INCOMING_RTL (decl))
20753 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20755 tree declared_type = TREE_TYPE (decl);
20756 tree passed_type = DECL_ARG_TYPE (decl);
20757 machine_mode dmode = TYPE_MODE (declared_type);
20758 machine_mode pmode = TYPE_MODE (passed_type);
20760 /* This decl represents a formal parameter which was optimized out.
20761 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20762 all cases where (rtl == NULL_RTX) just below. */
20763 if (dmode == pmode)
20764 rtl = DECL_INCOMING_RTL (decl);
20765 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20766 && SCALAR_INT_MODE_P (dmode)
20767 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20768 && DECL_INCOMING_RTL (decl))
20770 rtx inc = DECL_INCOMING_RTL (decl);
20771 if (REG_P (inc))
20772 rtl = inc;
20773 else if (MEM_P (inc))
20775 if (BYTES_BIG_ENDIAN)
20776 rtl = adjust_address_nv (inc, dmode,
20777 GET_MODE_SIZE (pmode)
20778 - GET_MODE_SIZE (dmode));
20779 else
20780 rtl = inc;
20785 /* If the parm was passed in registers, but lives on the stack, then
20786 make a big endian correction if the mode of the type of the
20787 parameter is not the same as the mode of the rtl. */
20788 /* ??? This is the same series of checks that are made in dbxout.cc before
20789 we reach the big endian correction code there. It isn't clear if all
20790 of these checks are necessary here, but keeping them all is the safe
20791 thing to do. */
20792 else if (MEM_P (rtl)
20793 && XEXP (rtl, 0) != const0_rtx
20794 && ! CONSTANT_P (XEXP (rtl, 0))
20795 /* Not passed in memory. */
20796 && !MEM_P (DECL_INCOMING_RTL (decl))
20797 /* Not passed by invisible reference. */
20798 && (!REG_P (XEXP (rtl, 0))
20799 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20800 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20801 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20802 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20803 #endif
20805 /* Big endian correction check. */
20806 && BYTES_BIG_ENDIAN
20807 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20808 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20809 UNITS_PER_WORD))
20811 machine_mode addr_mode = get_address_mode (rtl);
20812 poly_int64 offset = (UNITS_PER_WORD
20813 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20815 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20816 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20819 else if (VAR_P (decl)
20820 && rtl
20821 && MEM_P (rtl)
20822 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20824 machine_mode addr_mode = get_address_mode (rtl);
20825 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20826 GET_MODE (rtl));
20828 /* If a variable is declared "register" yet is smaller than
20829 a register, then if we store the variable to memory, it
20830 looks like we're storing a register-sized value, when in
20831 fact we are not. We need to adjust the offset of the
20832 storage location to reflect the actual value's bytes,
20833 else gdb will not be able to display it. */
20834 if (maybe_ne (offset, 0))
20835 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20836 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20839 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20840 and will have been substituted directly into all expressions that use it.
20841 C does not have such a concept, but C++ and other languages do. */
20842 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20843 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20845 if (rtl)
20846 rtl = targetm.delegitimize_address (rtl);
20848 /* If we don't look past the constant pool, we risk emitting a
20849 reference to a constant pool entry that isn't referenced from
20850 code, and thus is not emitted. */
20851 if (rtl)
20852 rtl = avoid_constant_pool_reference (rtl);
20854 /* Try harder to get a rtl. If this symbol ends up not being emitted
20855 in the current CU, resolve_addr will remove the expression referencing
20856 it. */
20857 if (rtl == NULL_RTX
20858 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20859 && VAR_P (decl)
20860 && !DECL_EXTERNAL (decl)
20861 && TREE_STATIC (decl)
20862 && DECL_NAME (decl)
20863 && !DECL_HARD_REGISTER (decl)
20864 && DECL_MODE (decl) != VOIDmode)
20866 rtl = make_decl_rtl_for_debug (decl);
20867 if (!MEM_P (rtl)
20868 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20869 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20870 rtl = NULL_RTX;
20873 return rtl;
20876 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20877 returned. If so, the decl for the COMMON block is returned, and the
20878 value is the offset into the common block for the symbol. */
20880 static tree
20881 fortran_common (tree decl, HOST_WIDE_INT *value)
20883 tree val_expr, cvar;
20884 machine_mode mode;
20885 poly_int64 bitsize, bitpos;
20886 tree offset;
20887 HOST_WIDE_INT cbitpos;
20888 int unsignedp, reversep, volatilep = 0;
20890 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20891 it does not have a value (the offset into the common area), or if it
20892 is thread local (as opposed to global) then it isn't common, and shouldn't
20893 be handled as such. */
20894 if (!VAR_P (decl)
20895 || !TREE_STATIC (decl)
20896 || !DECL_HAS_VALUE_EXPR_P (decl)
20897 || !is_fortran ())
20898 return NULL_TREE;
20900 val_expr = DECL_VALUE_EXPR (decl);
20901 if (TREE_CODE (val_expr) != COMPONENT_REF)
20902 return NULL_TREE;
20904 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20905 &unsignedp, &reversep, &volatilep);
20907 if (cvar == NULL_TREE
20908 || !VAR_P (cvar)
20909 || DECL_ARTIFICIAL (cvar)
20910 || !TREE_PUBLIC (cvar)
20911 /* We don't expect to have to cope with variable offsets,
20912 since at present all static data must have a constant size. */
20913 || !bitpos.is_constant (&cbitpos))
20914 return NULL_TREE;
20916 *value = 0;
20917 if (offset != NULL)
20919 if (!tree_fits_shwi_p (offset))
20920 return NULL_TREE;
20921 *value = tree_to_shwi (offset);
20923 if (cbitpos != 0)
20924 *value += cbitpos / BITS_PER_UNIT;
20926 return cvar;
20929 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20930 data attribute for a variable or a parameter. We generate the
20931 DW_AT_const_value attribute only in those cases where the given variable
20932 or parameter does not have a true "location" either in memory or in a
20933 register. This can happen (for example) when a constant is passed as an
20934 actual argument in a call to an inline function. (It's possible that
20935 these things can crop up in other ways also.) Note that one type of
20936 constant value which can be passed into an inlined function is a constant
20937 pointer. This can happen for example if an actual argument in an inlined
20938 function call evaluates to a compile-time constant address.
20940 CACHE_P is true if it is worth caching the location list for DECL,
20941 so that future calls can reuse it rather than regenerate it from scratch.
20942 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20943 since we will need to refer to them each time the function is inlined. */
20945 static bool
20946 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20948 rtx rtl;
20949 dw_loc_list_ref list;
20950 var_loc_list *loc_list;
20951 cached_dw_loc_list *cache;
20953 if (early_dwarf)
20954 return false;
20956 if (TREE_CODE (decl) == ERROR_MARK)
20957 return false;
20959 if (get_AT (die, DW_AT_location)
20960 || get_AT (die, DW_AT_const_value))
20961 return true;
20963 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20964 || TREE_CODE (decl) == RESULT_DECL);
20966 /* Try to get some constant RTL for this decl, and use that as the value of
20967 the location. */
20969 rtl = rtl_for_decl_location (decl);
20970 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20971 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20972 return true;
20974 /* See if we have single element location list that is equivalent to
20975 a constant value. That way we are better to use add_const_value_attribute
20976 rather than expanding constant value equivalent. */
20977 loc_list = lookup_decl_loc (decl);
20978 if (loc_list
20979 && loc_list->first
20980 && loc_list->first->next == NULL
20981 && NOTE_P (loc_list->first->loc)
20982 && NOTE_VAR_LOCATION (loc_list->first->loc)
20983 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20985 struct var_loc_node *node;
20987 node = loc_list->first;
20988 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20989 if (GET_CODE (rtl) == EXPR_LIST)
20990 rtl = XEXP (rtl, 0);
20991 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20992 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20993 return true;
20995 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20996 list several times. See if we've already cached the contents. */
20997 list = NULL;
20998 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20999 cache_p = false;
21000 if (cache_p)
21002 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
21003 if (cache)
21004 list = cache->loc_list;
21006 if (list == NULL)
21008 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
21009 NULL);
21010 /* It is usually worth caching this result if the decl is from
21011 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
21012 if (cache_p && list && list->dw_loc_next)
21014 cached_dw_loc_list **slot
21015 = cached_dw_loc_list_table->find_slot_with_hash (decl,
21016 DECL_UID (decl),
21017 INSERT);
21018 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
21019 cache->decl_id = DECL_UID (decl);
21020 cache->loc_list = list;
21021 *slot = cache;
21024 if (list)
21026 add_AT_location_description (die, DW_AT_location, list);
21027 return true;
21029 /* None of that worked, so it must not really have a location;
21030 try adding a constant value attribute from the DECL_INITIAL. */
21031 return tree_add_const_value_attribute_for_decl (die, decl);
21034 /* Mangle referenced decls. */
21035 static tree
21036 mangle_referenced_decls (tree *tp, int *walk_subtrees, void *)
21038 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
21039 *walk_subtrees = 0;
21041 if (VAR_OR_FUNCTION_DECL_P (*tp))
21042 assign_assembler_name_if_needed (*tp);
21044 return NULL_TREE;
21047 /* Attach a DW_AT_const_value attribute to DIE. The value of the
21048 attribute is the const value T. */
21050 static bool
21051 tree_add_const_value_attribute (dw_die_ref die, tree t)
21053 tree init;
21054 tree type = TREE_TYPE (t);
21056 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
21057 return false;
21059 init = t;
21060 gcc_assert (!DECL_P (init));
21062 if (TREE_CODE (init) == INTEGER_CST)
21064 if (tree_fits_uhwi_p (init))
21066 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
21067 return true;
21069 if (tree_fits_shwi_p (init))
21071 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
21072 return true;
21075 if (!early_dwarf)
21077 rtx rtl = rtl_for_decl_init (init, type);
21078 if (rtl)
21079 return add_const_value_attribute (die, TYPE_MODE (type), rtl);
21081 else
21083 /* For early_dwarf force mangling of all referenced symbols. */
21084 tree initializer = init;
21085 STRIP_NOPS (initializer);
21086 /* rtl_for_decl_init punts on other aggregates, and complex values. */
21087 if (AGGREGATE_TYPE_P (type)
21088 || (TREE_CODE (initializer) == VIEW_CONVERT_EXPR
21089 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (initializer, 0))))
21090 || TREE_CODE (type) == COMPLEX_TYPE)
21092 else if (initializer_constant_valid_p (initializer, type))
21093 walk_tree (&initializer, mangle_referenced_decls, NULL, NULL);
21095 /* If the host and target are sane, try harder. */
21096 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
21097 && initializer_constant_valid_p (init, type))
21099 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
21100 if (size > 0 && (int) size == size)
21102 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
21104 if (native_encode_initializer (init, array, size) == size)
21106 add_AT_vec (die, DW_AT_const_value, size, 1, array);
21107 return true;
21109 ggc_free (array);
21112 return false;
21115 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
21116 attribute is the const value of T, where T is an integral constant
21117 variable with static storage duration
21118 (so it can't be a PARM_DECL or a RESULT_DECL). */
21120 static bool
21121 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
21124 if (!decl
21125 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
21126 || (VAR_P (decl) && !TREE_STATIC (decl)))
21127 return false;
21129 if (TREE_READONLY (decl)
21130 && ! TREE_THIS_VOLATILE (decl)
21131 && DECL_INITIAL (decl))
21132 /* OK */;
21133 else
21134 return false;
21136 /* Don't add DW_AT_const_value if abstract origin already has one. */
21137 if (get_AT (var_die, DW_AT_const_value))
21138 return false;
21140 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
21143 /* Convert the CFI instructions for the current function into a
21144 location list. This is used for DW_AT_frame_base when we targeting
21145 a dwarf2 consumer that does not support the dwarf3
21146 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
21147 expressions. */
21149 static dw_loc_list_ref
21150 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
21152 int ix;
21153 dw_fde_ref fde;
21154 dw_loc_list_ref list, *list_tail;
21155 dw_cfi_ref cfi;
21156 dw_cfa_location last_cfa, next_cfa;
21157 const char *start_label, *last_label, *section;
21158 dw_cfa_location remember;
21160 fde = cfun->fde;
21161 gcc_assert (fde != NULL);
21163 section = secname_for_decl (current_function_decl);
21164 list_tail = &list;
21165 list = NULL;
21167 memset (&next_cfa, 0, sizeof (next_cfa));
21169 #ifdef CODEVIEW_DEBUGGING_INFO
21170 /* We can write simplified frame base information for CodeView, as we're
21171 not using it for rewinding. */
21172 if (codeview_debuginfo_p ())
21174 int dwreg = DEBUGGER_REGNO (cfun->machine->fs.cfa_reg->u.reg.regno);
21176 next_cfa.reg.set_by_dwreg (dwreg);
21177 next_cfa.offset = cfun->machine->fs.fp_valid
21178 ? cfun->machine->fs.fp_offset : cfun->machine->fs.sp_offset;
21180 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
21181 fde->dw_fde_begin, 0,
21182 fde->dw_fde_second_begin
21183 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
21184 section);
21185 maybe_gen_llsym (list);
21187 return list;
21189 #endif
21191 next_cfa.reg.set_by_dwreg (INVALID_REGNUM);
21192 remember = next_cfa;
21194 start_label = fde->dw_fde_begin;
21196 /* ??? Bald assumption that the CIE opcode list does not contain
21197 advance opcodes. */
21198 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
21199 lookup_cfa_1 (cfi, &next_cfa, &remember);
21201 last_cfa = next_cfa;
21202 last_label = start_label;
21204 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
21206 /* If the first partition contained no CFI adjustments, the
21207 CIE opcodes apply to the whole first partition. */
21208 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21209 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
21210 list_tail =&(*list_tail)->dw_loc_next;
21211 start_label = last_label = fde->dw_fde_second_begin;
21214 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
21216 switch (cfi->dw_cfi_opc)
21218 case DW_CFA_set_loc:
21219 case DW_CFA_advance_loc1:
21220 case DW_CFA_advance_loc2:
21221 case DW_CFA_advance_loc4:
21222 if (!cfa_equal_p (&last_cfa, &next_cfa))
21224 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21225 start_label, 0, last_label, 0, section);
21227 list_tail = &(*list_tail)->dw_loc_next;
21228 last_cfa = next_cfa;
21229 start_label = last_label;
21231 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
21232 break;
21234 case DW_CFA_advance_loc:
21235 /* The encoding is complex enough that we should never emit this. */
21236 gcc_unreachable ();
21238 default:
21239 lookup_cfa_1 (cfi, &next_cfa, &remember);
21240 break;
21242 if (ix + 1 == fde->dw_fde_switch_cfi_index)
21244 if (!cfa_equal_p (&last_cfa, &next_cfa))
21246 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21247 start_label, 0, last_label, 0, section);
21249 list_tail = &(*list_tail)->dw_loc_next;
21250 last_cfa = next_cfa;
21251 start_label = last_label;
21253 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21254 start_label, 0, fde->dw_fde_end, 0, section);
21255 list_tail = &(*list_tail)->dw_loc_next;
21256 start_label = last_label = fde->dw_fde_second_begin;
21260 if (!cfa_equal_p (&last_cfa, &next_cfa))
21262 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21263 start_label, 0, last_label, 0, section);
21264 list_tail = &(*list_tail)->dw_loc_next;
21265 start_label = last_label;
21268 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
21269 start_label, 0,
21270 fde->dw_fde_second_begin
21271 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
21272 section);
21274 maybe_gen_llsym (list);
21276 return list;
21279 /* Compute a displacement from the "steady-state frame pointer" to the
21280 frame base (often the same as the CFA), and store it in
21281 frame_pointer_fb_offset. OFFSET is added to the displacement
21282 before the latter is negated. */
21284 static void
21285 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
21287 rtx reg, elim;
21289 #ifdef FRAME_POINTER_CFA_OFFSET
21290 reg = frame_pointer_rtx;
21291 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
21292 #else
21293 reg = arg_pointer_rtx;
21294 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
21295 #endif
21297 elim = (ira_use_lra_p
21298 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
21299 : eliminate_regs (reg, VOIDmode, NULL_RTX));
21300 elim = strip_offset_and_add (elim, &offset);
21302 frame_pointer_fb_offset = -offset;
21304 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
21305 in which to eliminate. This is because it's stack pointer isn't
21306 directly accessible as a register within the ISA. To work around
21307 this, assume that while we cannot provide a proper value for
21308 frame_pointer_fb_offset, we won't need one either. We can use
21309 hard frame pointer in debug info even if frame pointer isn't used
21310 since hard frame pointer in debug info is encoded with DW_OP_fbreg
21311 which uses the DW_AT_frame_base attribute, not hard frame pointer
21312 directly. */
21313 frame_pointer_fb_offset_valid
21314 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21317 /* Generate a DW_AT_name attribute given some string value to be included as
21318 the value of the attribute. */
21320 void
21321 add_name_attribute (dw_die_ref die, const char *name_string)
21323 if (name_string != NULL && *name_string != 0)
21325 if (demangle_name_func)
21326 name_string = (*demangle_name_func) (name_string);
21328 add_AT_string (die, DW_AT_name, name_string);
21332 /* Generate a DW_AT_name attribute given some string value representing a
21333 file or filepath to be included as value of the attribute. */
21334 static void
21335 add_filename_attribute (dw_die_ref die, const char *name_string)
21337 if (name_string != NULL && *name_string != 0)
21338 add_filepath_AT_string (die, DW_AT_name, name_string);
21341 /* Generate a DW_AT_description attribute given some string value to be included
21342 as the value of the attribute. */
21344 static void
21345 add_desc_attribute (dw_die_ref die, const char *name_string)
21347 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21348 return;
21350 if (name_string == NULL || *name_string == 0)
21351 return;
21353 if (demangle_name_func)
21354 name_string = (*demangle_name_func) (name_string);
21356 add_AT_string (die, DW_AT_description, name_string);
21359 /* Generate a DW_AT_description attribute given some decl to be included
21360 as the value of the attribute. */
21362 static void
21363 add_desc_attribute (dw_die_ref die, tree decl)
21365 tree decl_name;
21367 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21368 return;
21370 if (decl == NULL_TREE || !DECL_P (decl))
21371 return;
21372 decl_name = DECL_NAME (decl);
21374 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21376 const char *name = dwarf2_name (decl, 0);
21377 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21379 else
21381 char *desc = print_generic_expr_to_str (decl);
21382 add_desc_attribute (die, desc);
21383 free (desc);
21387 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21388 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21389 of TYPE accordingly.
21391 ??? This is a temporary measure until after we're able to generate
21392 regular DWARF for the complex Ada type system. */
21394 static void
21395 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21396 dw_die_ref context_die)
21398 tree dtype;
21399 dw_die_ref dtype_die;
21401 if (!lang_hooks.types.descriptive_type)
21402 return;
21404 dtype = lang_hooks.types.descriptive_type (type);
21405 if (!dtype)
21406 return;
21408 dtype_die = lookup_type_die (dtype);
21409 if (!dtype_die)
21411 gen_type_die (dtype, context_die);
21412 dtype_die = lookup_type_die (dtype);
21413 gcc_assert (dtype_die);
21416 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21419 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21421 static const char *
21422 comp_dir_string (void)
21424 const char *wd;
21425 char *wd_plus_sep = NULL;
21426 static const char *cached_wd = NULL;
21428 if (cached_wd != NULL)
21429 return cached_wd;
21431 wd = get_src_pwd ();
21432 if (wd == NULL)
21433 return NULL;
21435 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21437 size_t wdlen = strlen (wd);
21438 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21439 strcpy (wd_plus_sep, wd);
21440 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21441 wd_plus_sep [wdlen + 1] = 0;
21442 wd = wd_plus_sep;
21445 cached_wd = remap_debug_filename (wd);
21447 /* remap_debug_filename can just pass through wd or return a new gc string.
21448 These two types can't be both stored in a GTY(())-tagged string, but since
21449 the cached value lives forever just copy it if needed. */
21450 if (cached_wd != wd)
21452 cached_wd = xstrdup (cached_wd);
21453 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21454 free (wd_plus_sep);
21457 return cached_wd;
21460 /* Generate a DW_AT_comp_dir attribute for DIE. */
21462 static void
21463 add_comp_dir_attribute (dw_die_ref die)
21465 const char * wd = comp_dir_string ();
21466 if (wd != NULL)
21467 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21470 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21471 pointer computation, ...), output a representation for that bound according
21472 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21473 loc_list_from_tree for the meaning of CONTEXT. */
21475 static void
21476 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21477 int forms, struct loc_descr_context *context)
21479 dw_die_ref context_die, decl_die = NULL;
21480 dw_loc_list_ref list;
21481 bool strip_conversions = true;
21482 bool placeholder_seen = false;
21484 while (strip_conversions)
21485 switch (TREE_CODE (value))
21487 case ERROR_MARK:
21488 case SAVE_EXPR:
21489 return;
21491 CASE_CONVERT:
21492 case VIEW_CONVERT_EXPR:
21493 value = TREE_OPERAND (value, 0);
21494 break;
21496 default:
21497 strip_conversions = false;
21498 break;
21501 /* If possible and permitted, output the attribute as a constant. */
21502 if ((forms & dw_scalar_form_constant) != 0
21503 && TREE_CODE (value) == INTEGER_CST)
21505 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21507 /* If HOST_WIDE_INT is big enough then represent the bound as
21508 a constant value. We need to choose a form based on
21509 whether the type is signed or unsigned. We cannot just
21510 call add_AT_unsigned if the value itself is positive
21511 (add_AT_unsigned might add the unsigned value encoded as
21512 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21513 bounds type and then sign extend any unsigned values found
21514 for signed types. This is needed only for
21515 DW_AT_{lower,upper}_bound, since for most other attributes,
21516 consumers will treat DW_FORM_data[1248] as unsigned values,
21517 regardless of the underlying type. */
21518 if (prec <= HOST_BITS_PER_WIDE_INT
21519 || tree_fits_uhwi_p (value))
21521 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21522 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21523 else
21524 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21526 else if (dwarf_version >= 5
21527 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21528 /* Otherwise represent the bound as an unsigned value with
21529 the precision of its type. The precision and signedness
21530 of the type will be necessary to re-interpret it
21531 unambiguously. */
21532 add_AT_wide (die, attr, wi::to_wide (value));
21533 else
21535 rtx v = immed_wide_int_const (wi::to_wide (value),
21536 TYPE_MODE (TREE_TYPE (value)));
21537 dw_loc_descr_ref loc
21538 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21539 VAR_INIT_STATUS_INITIALIZED);
21540 if (loc)
21541 add_AT_loc (die, attr, loc);
21543 return;
21546 /* Otherwise, if it's possible and permitted too, output a reference to
21547 another DIE. */
21548 if ((forms & dw_scalar_form_reference) != 0)
21550 tree decl = NULL_TREE;
21552 /* Some type attributes reference an outer type. For instance, the upper
21553 bound of an array may reference an embedding record (this happens in
21554 Ada). */
21555 if (TREE_CODE (value) == COMPONENT_REF
21556 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21557 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21558 decl = TREE_OPERAND (value, 1);
21560 else if (VAR_P (value)
21561 || TREE_CODE (value) == PARM_DECL
21562 || TREE_CODE (value) == RESULT_DECL)
21563 decl = value;
21565 if (decl != NULL_TREE)
21567 decl_die = lookup_decl_die (decl);
21569 /* ??? Can this happen, or should the variable have been bound
21570 first? Probably it can, since I imagine that we try to create
21571 the types of parameters in the order in which they exist in
21572 the list, and won't have created a forward reference to a
21573 later parameter. */
21574 if (decl_die != NULL)
21576 if (get_AT (decl_die, DW_AT_location)
21577 || get_AT (decl_die, DW_AT_data_member_location)
21578 || get_AT (decl_die, DW_AT_data_bit_offset)
21579 || get_AT (decl_die, DW_AT_const_value))
21581 add_AT_die_ref (die, attr, decl_die);
21582 return;
21588 /* Last chance: try to create a stack operation procedure to evaluate the
21589 value. Do nothing if even that is not possible or permitted. */
21590 if ((forms & dw_scalar_form_exprloc) == 0)
21591 return;
21593 list = loc_list_from_tree (value, 2, context);
21594 if (context && context->placeholder_arg)
21596 placeholder_seen = context->placeholder_seen;
21597 context->placeholder_seen = false;
21599 if (list == NULL || single_element_loc_list_p (list))
21601 /* If this attribute is not a reference nor constant, it is
21602 a DWARF expression rather than location description. For that
21603 loc_list_from_tree (value, 0, &context) is needed. */
21604 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21605 if (list2 && single_element_loc_list_p (list2))
21607 if (placeholder_seen)
21609 struct dwarf_procedure_info dpi;
21610 dpi.fndecl = NULL_TREE;
21611 dpi.args_count = 1;
21612 if (!resolve_args_picking (list2->expr, 1, &dpi))
21613 return;
21615 add_AT_loc (die, attr, list2->expr);
21616 return;
21620 /* If that failed to give a single element location list, fall back to
21621 outputting this as a reference... still if permitted. */
21622 if (list == NULL
21623 || (forms & dw_scalar_form_reference) == 0
21624 || placeholder_seen)
21625 return;
21627 if (!decl_die)
21629 if (current_function_decl == 0)
21630 context_die = comp_unit_die ();
21631 else
21632 context_die = lookup_decl_die (current_function_decl);
21634 decl_die = new_die (DW_TAG_variable, context_die, value);
21635 add_AT_flag (decl_die, DW_AT_artificial, 1);
21636 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21637 context_die);
21640 add_AT_location_description (decl_die, DW_AT_location, list);
21641 add_AT_die_ref (die, attr, decl_die);
21644 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21645 default. */
21647 static int
21648 lower_bound_default (void)
21650 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21652 case DW_LANG_C:
21653 case DW_LANG_C89:
21654 case DW_LANG_C99:
21655 case DW_LANG_C11:
21656 case DW_LANG_C_plus_plus:
21657 case DW_LANG_C_plus_plus_11:
21658 case DW_LANG_C_plus_plus_14:
21659 case DW_LANG_ObjC:
21660 case DW_LANG_ObjC_plus_plus:
21661 return 0;
21662 case DW_LANG_Fortran77:
21663 case DW_LANG_Fortran90:
21664 case DW_LANG_Fortran95:
21665 case DW_LANG_Fortran03:
21666 case DW_LANG_Fortran08:
21667 return 1;
21668 case DW_LANG_UPC:
21669 case DW_LANG_D:
21670 case DW_LANG_Python:
21671 return dwarf_version >= 4 ? 0 : -1;
21672 case DW_LANG_Ada95:
21673 case DW_LANG_Ada83:
21674 case DW_LANG_Cobol74:
21675 case DW_LANG_Cobol85:
21676 case DW_LANG_Modula2:
21677 case DW_LANG_PLI:
21678 return dwarf_version >= 4 ? 1 : -1;
21679 default:
21680 return -1;
21684 /* Given a tree node describing an array bound (either lower or upper) output
21685 a representation for that bound. */
21687 static void
21688 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21689 tree bound, struct loc_descr_context *context)
21691 int dflt;
21693 while (1)
21694 switch (TREE_CODE (bound))
21696 /* Strip all conversions. */
21697 CASE_CONVERT:
21698 case VIEW_CONVERT_EXPR:
21699 bound = TREE_OPERAND (bound, 0);
21700 break;
21702 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21703 are even omitted when they are the default. */
21704 case INTEGER_CST:
21705 /* If the value for this bound is the default one, we can even omit the
21706 attribute. */
21707 if (bound_attr == DW_AT_lower_bound
21708 && tree_fits_shwi_p (bound)
21709 && (dflt = lower_bound_default ()) != -1
21710 && tree_to_shwi (bound) == dflt)
21711 return;
21713 /* FALLTHRU */
21715 default:
21716 /* Let GNAT encodings do the magic for self-referential bounds. */
21717 if (is_ada ()
21718 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21719 && contains_placeholder_p (bound))
21720 return;
21722 add_scalar_info (subrange_die, bound_attr, bound,
21723 dw_scalar_form_constant
21724 | dw_scalar_form_exprloc
21725 | dw_scalar_form_reference,
21726 context);
21727 return;
21731 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21732 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21734 This function reuses previously set type and bound information if
21735 available. */
21737 static void
21738 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21740 dw_die_ref child = type_die->die_child;
21741 struct array_descr_info info;
21742 int dimension_number;
21744 if (lang_hooks.types.get_array_descr_info)
21746 memset (&info, 0, sizeof (info));
21747 if (lang_hooks.types.get_array_descr_info (type, &info))
21748 /* Fortran sometimes emits array types with no dimension. */
21749 gcc_assert (info.ndimensions >= 0
21750 && info.ndimensions
21751 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21753 else
21754 info.ndimensions = 0;
21756 for (dimension_number = 0;
21757 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21758 type = TREE_TYPE (type), dimension_number++)
21760 tree domain = TYPE_DOMAIN (type);
21762 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21763 break;
21765 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21766 and (in GNU C only) variable bounds. Handle all three forms
21767 here. */
21769 /* Find and reuse a previously generated DW_TAG_subrange_type if
21770 available.
21772 For multi-dimensional arrays, as we iterate through the
21773 various dimensions in the enclosing for loop above, we also
21774 iterate through the DIE children and pick at each
21775 DW_TAG_subrange_type previously generated (if available).
21776 Each child DW_TAG_subrange_type DIE describes the range of
21777 the current dimension. At this point we should have as many
21778 DW_TAG_subrange_type's as we have dimensions in the
21779 array. */
21780 dw_die_ref subrange_die = NULL;
21781 if (child)
21782 while (1)
21784 child = child->die_sib;
21785 if (child->die_tag == DW_TAG_subrange_type)
21786 subrange_die = child;
21787 if (child == type_die->die_child)
21789 /* If we wrapped around, stop looking next time. */
21790 child = NULL;
21791 break;
21793 if (child->die_tag == DW_TAG_subrange_type)
21794 break;
21796 if (!subrange_die)
21797 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21799 if (domain)
21801 /* We have an array type with specified bounds. */
21802 tree lower = TYPE_MIN_VALUE (domain);
21803 tree upper = TYPE_MAX_VALUE (domain);
21804 tree index_type = TREE_TYPE (domain);
21806 if (dimension_number <= info.ndimensions - 1)
21808 lower = info.dimen[dimension_number].lower_bound;
21809 upper = info.dimen[dimension_number].upper_bound;
21810 index_type = info.dimen[dimension_number].bounds_type;
21813 /* Define the index type. */
21814 if (index_type && !get_AT (subrange_die, DW_AT_type))
21815 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21816 false, type_die);
21818 /* ??? If upper is NULL, the array has unspecified length,
21819 but it does have a lower bound. This happens with Fortran
21820 dimension arr(N:*)
21821 Since the debugger is definitely going to need to know N
21822 to produce useful results, go ahead and output the lower
21823 bound solo, and hope the debugger can cope. */
21825 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21826 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21828 if (!get_AT (subrange_die, DW_AT_upper_bound)
21829 && !get_AT (subrange_die, DW_AT_count))
21831 if (upper)
21832 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21833 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21834 /* Zero-length array. */
21835 add_bound_info (subrange_die, DW_AT_count,
21836 build_int_cst (TREE_TYPE (lower), 0), NULL);
21840 /* Otherwise we have an array type with an unspecified length. The
21841 DWARF-2 spec does not say how to handle this; let's just leave out the
21842 bounds. */
21846 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21848 static void
21849 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21851 dw_die_ref decl_die;
21852 HOST_WIDE_INT size;
21854 switch (TREE_CODE (tree_node))
21856 case ERROR_MARK:
21857 size = 0;
21858 break;
21859 case ENUMERAL_TYPE:
21860 case RECORD_TYPE:
21861 case UNION_TYPE:
21862 case QUAL_UNION_TYPE:
21863 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21864 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21866 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21867 return;
21869 size = int_size_in_bytes (tree_node);
21870 break;
21871 case FIELD_DECL:
21872 /* For a data member of a struct or union, the DW_AT_byte_size is
21873 generally given as the number of bytes normally allocated for an
21874 object of the *declared* type of the member itself. This is true
21875 even for bit-fields. */
21876 size = int_size_in_bytes (field_type (tree_node));
21877 break;
21878 default:
21879 gcc_unreachable ();
21882 /* Note that `size' might be -1 when we get to this point. If it is, that
21883 indicates that the byte size of the entity in question is variable. */
21884 if (size >= 0)
21885 add_AT_unsigned (die, DW_AT_byte_size, size);
21887 /* Support for dynamically-sized objects was introduced in DWARF3. */
21888 else if (TYPE_P (tree_node)
21889 && (dwarf_version >= 3 || !dwarf_strict)
21890 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21892 struct loc_descr_context ctx = {
21893 const_cast<tree> (tree_node), /* context_type */
21894 NULL_TREE, /* base_decl */
21895 NULL, /* dpi */
21896 false, /* placeholder_arg */
21897 false, /* placeholder_seen */
21898 false /* strict_signedness */
21901 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21902 add_scalar_info (die, DW_AT_byte_size, tree_size,
21903 dw_scalar_form_constant
21904 | dw_scalar_form_exprloc
21905 | dw_scalar_form_reference,
21906 &ctx);
21910 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21911 alignment. */
21913 static void
21914 add_alignment_attribute (dw_die_ref die, tree tree_node)
21916 if (dwarf_version < 5 && dwarf_strict)
21917 return;
21919 unsigned align;
21921 if (DECL_P (tree_node))
21923 if (!DECL_USER_ALIGN (tree_node))
21924 return;
21926 align = DECL_ALIGN_UNIT (tree_node);
21928 else if (TYPE_P (tree_node))
21930 if (!TYPE_USER_ALIGN (tree_node))
21931 return;
21933 align = TYPE_ALIGN_UNIT (tree_node);
21935 else
21936 gcc_unreachable ();
21938 add_AT_unsigned (die, DW_AT_alignment, align);
21941 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21942 which specifies the distance in bits from the highest order bit of the
21943 "containing object" for the bit-field to the highest order bit of the
21944 bit-field itself.
21946 For any given bit-field, the "containing object" is a hypothetical object
21947 (of some integral or enum type) within which the given bit-field lives. The
21948 type of this hypothetical "containing object" is always the same as the
21949 declared type of the individual bit-field itself. The determination of the
21950 exact location of the "containing object" for a bit-field is rather
21951 complicated. It's handled by the `field_byte_offset' function (above).
21953 Note that it is the size (in bytes) of the hypothetical "containing object"
21954 which will be given in the DW_AT_byte_size attribute for this bit-field.
21955 (See `byte_size_attribute' above). */
21957 static inline void
21958 add_bit_offset_attribute (dw_die_ref die, tree decl)
21960 HOST_WIDE_INT object_offset_in_bytes;
21961 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21962 HOST_WIDE_INT bitpos_int;
21963 HOST_WIDE_INT highest_order_object_bit_offset;
21964 HOST_WIDE_INT highest_order_field_bit_offset;
21965 HOST_WIDE_INT bit_offset;
21967 /* The containing object is within the DECL_CONTEXT. */
21968 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21970 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21972 /* Must be a field and a bit field. */
21973 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21975 /* We can't yet handle bit-fields whose offsets are variable, so if we
21976 encounter such things, just return without generating any attribute
21977 whatsoever. Likewise for variable or too large size. */
21978 if (! tree_fits_shwi_p (bit_position (decl))
21979 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21980 return;
21982 bitpos_int = int_bit_position (decl);
21984 /* Note that the bit offset is always the distance (in bits) from the
21985 highest-order bit of the "containing object" to the highest-order bit of
21986 the bit-field itself. Since the "high-order end" of any object or field
21987 is different on big-endian and little-endian machines, the computation
21988 below must take account of these differences. */
21989 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21990 highest_order_field_bit_offset = bitpos_int;
21992 if (! BYTES_BIG_ENDIAN)
21994 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21995 highest_order_object_bit_offset +=
21996 simple_type_size_in_bits (original_type);
21999 bit_offset
22000 = (! BYTES_BIG_ENDIAN
22001 ? highest_order_object_bit_offset - highest_order_field_bit_offset
22002 : highest_order_field_bit_offset - highest_order_object_bit_offset);
22004 if (bit_offset < 0)
22005 add_AT_int (die, DW_AT_bit_offset, bit_offset);
22006 else
22007 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
22010 /* For a FIELD_DECL node which represents a bit field, output an attribute
22011 which specifies the length in bits of the given field. */
22013 static inline void
22014 add_bit_size_attribute (dw_die_ref die, tree decl)
22016 /* Must be a field and a bit field. */
22017 gcc_assert (TREE_CODE (decl) == FIELD_DECL
22018 && DECL_BIT_FIELD_TYPE (decl));
22020 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
22021 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
22024 /* If the compiled language is ANSI C, then add a 'prototyped'
22025 attribute, if arg types are given for the parameters of a function. */
22027 static inline void
22028 add_prototyped_attribute (dw_die_ref die, tree func_type)
22030 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
22032 case DW_LANG_C:
22033 case DW_LANG_C89:
22034 case DW_LANG_C99:
22035 case DW_LANG_C11:
22036 case DW_LANG_ObjC:
22037 if (prototype_p (func_type))
22038 add_AT_flag (die, DW_AT_prototyped, 1);
22039 break;
22040 default:
22041 break;
22045 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
22046 by looking in the type declaration, the object declaration equate table or
22047 the block mapping. */
22049 static inline void
22050 add_abstract_origin_attribute (dw_die_ref die, tree origin)
22052 dw_die_ref origin_die = NULL;
22054 /* For late LTO debug output we want to refer directly to the abstract
22055 DIE in the early debug rather to the possibly existing concrete
22056 instance and avoid creating that just for this purpose. */
22057 sym_off_pair *desc;
22058 if (in_lto_p
22059 && external_die_map
22060 && (desc = external_die_map->get (origin)))
22062 add_AT_external_die_ref (die, DW_AT_abstract_origin,
22063 desc->sym, desc->off);
22064 return;
22067 if (DECL_P (origin))
22068 origin_die = lookup_decl_die (origin);
22069 else if (TYPE_P (origin))
22070 origin_die = lookup_type_die (origin);
22071 else if (TREE_CODE (origin) == BLOCK)
22072 origin_die = lookup_block_die (origin);
22074 /* XXX: Functions that are never lowered don't always have correct block
22075 trees (in the case of java, they simply have no block tree, in some other
22076 languages). For these functions, there is nothing we can really do to
22077 output correct debug info for inlined functions in all cases. Rather
22078 than die, we'll just produce deficient debug info now, in that we will
22079 have variables without a proper abstract origin. In the future, when all
22080 functions are lowered, we should re-add a gcc_assert (origin_die)
22081 here. */
22083 if (origin_die)
22085 dw_attr_node *a;
22086 /* Like above, if we already created a concrete instance DIE
22087 do not use that for the abstract origin but the early DIE
22088 if present. */
22089 if (in_lto_p
22090 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
22091 origin_die = AT_ref (a);
22092 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
22096 /* We do not currently support the pure_virtual attribute. */
22098 static inline void
22099 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
22101 if (DECL_VINDEX (func_decl))
22103 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22105 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
22106 add_AT_loc (die, DW_AT_vtable_elem_location,
22107 new_loc_descr (DW_OP_constu,
22108 tree_to_shwi (DECL_VINDEX (func_decl)),
22109 0));
22111 /* GNU extension: Record what type this method came from originally. */
22112 if (debug_info_level > DINFO_LEVEL_TERSE
22113 && DECL_CONTEXT (func_decl))
22114 add_AT_die_ref (die, DW_AT_containing_type,
22115 lookup_type_die (DECL_CONTEXT (func_decl)));
22119 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
22120 given decl. This used to be a vendor extension until after DWARF 4
22121 standardized it. */
22123 static void
22124 add_linkage_attr (dw_die_ref die, tree decl)
22126 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
22128 /* Mimic what assemble_name_raw does with a leading '*'. */
22129 if (name[0] == '*')
22130 name = &name[1];
22132 if (dwarf_version >= 4)
22133 add_AT_string (die, DW_AT_linkage_name, name);
22134 else
22135 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
22138 /* Add source coordinate attributes for the given decl. */
22140 static void
22141 add_src_coords_attributes (dw_die_ref die, tree decl)
22143 expanded_location s;
22145 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
22146 return;
22147 s = expand_location (DECL_SOURCE_LOCATION (decl));
22148 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
22149 add_AT_unsigned (die, DW_AT_decl_line, s.line);
22150 if (debug_column_info && s.column)
22151 add_AT_unsigned (die, DW_AT_decl_column, s.column);
22154 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
22156 static void
22157 add_linkage_name_raw (dw_die_ref die, tree decl)
22159 /* Defer until we have an assembler name set. */
22160 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
22162 limbo_die_node *asm_name;
22164 asm_name = ggc_cleared_alloc<limbo_die_node> ();
22165 asm_name->die = die;
22166 asm_name->created_for = decl;
22167 asm_name->next = deferred_asm_name;
22168 deferred_asm_name = asm_name;
22170 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22171 add_linkage_attr (die, decl);
22174 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
22176 static void
22177 add_linkage_name (dw_die_ref die, tree decl)
22179 if (debug_info_level > DINFO_LEVEL_NONE
22180 && VAR_OR_FUNCTION_DECL_P (decl)
22181 && TREE_PUBLIC (decl)
22182 && !(VAR_P (decl) && DECL_REGISTER (decl))
22183 && die->die_tag != DW_TAG_member)
22184 add_linkage_name_raw (die, decl);
22187 /* Add a DW_AT_name attribute and source coordinate attribute for the
22188 given decl, but only if it actually has a name. */
22190 static void
22191 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
22192 bool no_linkage_name)
22194 tree decl_name;
22196 decl_name = DECL_NAME (decl);
22197 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
22199 const char *name = dwarf2_name (decl, 0);
22200 if (name)
22201 add_name_attribute (die, name);
22202 else
22203 add_desc_attribute (die, decl);
22205 if (! DECL_ARTIFICIAL (decl))
22206 add_src_coords_attributes (die, decl);
22208 if (!no_linkage_name)
22209 add_linkage_name (die, decl);
22211 else
22212 add_desc_attribute (die, decl);
22214 #ifdef VMS_DEBUGGING_INFO
22215 /* Get the function's name, as described by its RTL. This may be different
22216 from the DECL_NAME name used in the source file. */
22217 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
22219 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
22220 XEXP (DECL_RTL (decl), 0), false);
22221 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
22223 #endif /* VMS_DEBUGGING_INFO */
22226 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
22228 static void
22229 add_discr_value (dw_die_ref die, dw_discr_value *value)
22231 dw_attr_node attr;
22233 attr.dw_attr = DW_AT_discr_value;
22234 attr.dw_attr_val.val_class = dw_val_class_discr_value;
22235 attr.dw_attr_val.val_entry = NULL;
22236 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
22237 if (value->pos)
22238 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
22239 else
22240 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
22241 add_dwarf_attr (die, &attr);
22244 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
22246 static void
22247 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
22249 dw_attr_node attr;
22251 attr.dw_attr = DW_AT_discr_list;
22252 attr.dw_attr_val.val_class = dw_val_class_discr_list;
22253 attr.dw_attr_val.val_entry = NULL;
22254 attr.dw_attr_val.v.val_discr_list = discr_list;
22255 add_dwarf_attr (die, &attr);
22258 static inline dw_discr_list_ref
22259 AT_discr_list (dw_attr_node *attr)
22261 return attr->dw_attr_val.v.val_discr_list;
22264 #ifdef VMS_DEBUGGING_INFO
22265 /* Output the debug main pointer die for VMS */
22267 void
22268 dwarf2out_vms_debug_main_pointer (void)
22270 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22271 dw_die_ref die;
22273 /* Allocate the VMS debug main subprogram die. */
22274 die = new_die_raw (DW_TAG_subprogram);
22275 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
22276 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
22277 current_function_funcdef_no);
22278 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22280 /* Make it the first child of comp_unit_die (). */
22281 die->die_parent = comp_unit_die ();
22282 if (comp_unit_die ()->die_child)
22284 die->die_sib = comp_unit_die ()->die_child->die_sib;
22285 comp_unit_die ()->die_child->die_sib = die;
22287 else
22289 die->die_sib = die;
22290 comp_unit_die ()->die_child = die;
22293 #endif /* VMS_DEBUGGING_INFO */
22295 /* walk_tree helper function for uses_local_type, below. */
22297 static tree
22298 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
22300 if (!TYPE_P (*tp))
22301 *walk_subtrees = 0;
22302 else
22304 tree name = TYPE_NAME (*tp);
22305 if (name && DECL_P (name) && decl_function_context (name))
22306 return *tp;
22308 return NULL_TREE;
22311 /* If TYPE involves a function-local type (including a local typedef to a
22312 non-local type), returns that type; otherwise returns NULL_TREE. */
22314 static tree
22315 uses_local_type (tree type)
22317 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22318 return used;
22321 /* Return the DIE for the scope that immediately contains this type.
22322 Non-named types that do not involve a function-local type get global
22323 scope. Named types nested in namespaces or other types get their
22324 containing scope. All other types (i.e. function-local named types) get
22325 the current active scope. */
22327 static dw_die_ref
22328 scope_die_for (tree t, dw_die_ref context_die)
22330 dw_die_ref scope_die = NULL;
22331 tree containing_scope;
22333 /* Non-types always go in the current scope. */
22334 gcc_assert (TYPE_P (t));
22336 /* Use the scope of the typedef, rather than the scope of the type
22337 it refers to. */
22338 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22339 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22340 else
22341 containing_scope = TYPE_CONTEXT (t);
22343 /* Use the containing namespace if there is one. */
22344 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22346 if (context_die == lookup_decl_die (containing_scope))
22347 /* OK */;
22348 else if (debug_info_level > DINFO_LEVEL_TERSE)
22349 context_die = get_context_die (containing_scope);
22350 else
22351 containing_scope = NULL_TREE;
22354 /* Ignore function type "scopes" from the C frontend. They mean that
22355 a tagged type is local to a parmlist of a function declarator, but
22356 that isn't useful to DWARF. */
22357 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22358 containing_scope = NULL_TREE;
22360 if (SCOPE_FILE_SCOPE_P (containing_scope))
22362 /* If T uses a local type keep it local as well, to avoid references
22363 to function-local DIEs from outside the function. */
22364 if (current_function_decl && uses_local_type (t))
22365 scope_die = context_die;
22366 else
22367 scope_die = comp_unit_die ();
22369 else if (TYPE_P (containing_scope))
22371 /* For types, we can just look up the appropriate DIE. */
22372 if (debug_info_level > DINFO_LEVEL_TERSE)
22373 scope_die = get_context_die (containing_scope);
22374 else
22376 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22377 if (scope_die == NULL)
22378 scope_die = comp_unit_die ();
22381 else
22382 scope_die = context_die;
22384 return scope_die;
22387 /* Returns true if CONTEXT_DIE is internal to a function. */
22389 static inline bool
22390 local_scope_p (dw_die_ref context_die)
22392 for (; context_die; context_die = context_die->die_parent)
22393 if (context_die->die_tag == DW_TAG_inlined_subroutine
22394 || context_die->die_tag == DW_TAG_subprogram)
22395 return true;
22397 return false;
22400 /* Returns true if CONTEXT_DIE is a class. */
22402 static inline bool
22403 class_scope_p (dw_die_ref context_die)
22405 return (context_die
22406 && (context_die->die_tag == DW_TAG_structure_type
22407 || context_die->die_tag == DW_TAG_class_type
22408 || context_die->die_tag == DW_TAG_interface_type
22409 || context_die->die_tag == DW_TAG_union_type));
22412 /* Returns true if CONTEXT_DIE is a class or namespace, for deciding
22413 whether or not to treat a DIE in this context as a declaration. */
22415 static inline bool
22416 class_or_namespace_scope_p (dw_die_ref context_die)
22418 return (class_scope_p (context_die)
22419 || (context_die && context_die->die_tag == DW_TAG_namespace));
22422 /* Many forms of DIEs require a "type description" attribute. This
22423 routine locates the proper "type descriptor" die for the type given
22424 by 'type' plus any additional qualifiers given by 'cv_quals', and
22425 adds a DW_AT_type attribute below the given die. */
22427 static void
22428 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22429 bool reverse, dw_die_ref context_die)
22431 enum tree_code code = TREE_CODE (type);
22432 dw_die_ref type_die = NULL;
22434 if (debug_info_level <= DINFO_LEVEL_TERSE)
22435 return;
22437 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22438 or fixed-point type, use the inner type. This is because we have no
22439 support for unnamed types in base_type_die. This can happen if this is
22440 an Ada subrange type. Correct solution is emit a subrange type die. */
22441 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22442 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22443 type = TREE_TYPE (type), code = TREE_CODE (type);
22445 if (code == ERROR_MARK
22446 /* Handle a special case. For functions whose return type is void, we
22447 generate *no* type attribute. (Note that no object may have type
22448 `void', so this only applies to function return types). */
22449 || code == VOID_TYPE)
22450 return;
22452 type_die = modified_type_die (type,
22453 cv_quals | TYPE_QUALS (type),
22454 reverse,
22455 context_die);
22457 if (type_die != NULL)
22458 add_AT_die_ref (object_die, DW_AT_type, type_die);
22461 /* Given an object die, add the calling convention attribute for the
22462 function call type. */
22463 static void
22464 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22466 enum dwarf_calling_convention value = DW_CC_normal;
22468 value = ((enum dwarf_calling_convention)
22469 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22471 if (is_fortran ()
22472 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22474 /* DWARF 2 doesn't provide a way to identify a program's source-level
22475 entry point. DW_AT_calling_convention attributes are only meant
22476 to describe functions' calling conventions. However, lacking a
22477 better way to signal the Fortran main program, we used this for
22478 a long time, following existing custom. Now, DWARF 4 has
22479 DW_AT_main_subprogram, which we add below, but some tools still
22480 rely on the old way, which we thus keep. */
22481 value = DW_CC_program;
22483 if (dwarf_version >= 4 || !dwarf_strict)
22484 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22487 /* Only add the attribute if the backend requests it, and
22488 is not DW_CC_normal. */
22489 if (value && (value != DW_CC_normal))
22490 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22493 /* Given a tree pointer to a struct, class, union, or enum type node, return
22494 a pointer to the (string) tag name for the given type, or zero if the type
22495 was declared without a tag. */
22497 static const char *
22498 type_tag (const_tree type)
22500 const char *name = 0;
22502 if (TYPE_NAME (type) != 0)
22504 tree t = 0;
22506 /* Find the IDENTIFIER_NODE for the type name. */
22507 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22508 && !TYPE_NAMELESS (type))
22509 t = TYPE_NAME (type);
22511 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22512 a TYPE_DECL node, regardless of whether or not a `typedef' was
22513 involved. */
22514 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22515 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22517 /* We want to be extra verbose. Don't call dwarf_name if
22518 DECL_NAME isn't set. The default hook for decl_printable_name
22519 doesn't like that, and in this context it's correct to return
22520 0, instead of "<anonymous>" or the like. */
22521 if (DECL_NAME (TYPE_NAME (type))
22522 && !DECL_NAMELESS (TYPE_NAME (type)))
22523 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22526 /* Now get the name as a string, or invent one. */
22527 if (!name && t != 0)
22528 name = IDENTIFIER_POINTER (t);
22531 return (name == 0 || *name == '\0') ? 0 : name;
22534 /* Return the type associated with a data member, make a special check
22535 for bit field types. */
22537 static inline tree
22538 member_declared_type (const_tree member)
22540 return (DECL_BIT_FIELD_TYPE (member)
22541 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22544 /* Get the decl's label, as described by its RTL. This may be different
22545 from the DECL_NAME name used in the source file. */
22547 #if 0
22548 static const char *
22549 decl_start_label (tree decl)
22551 rtx x;
22552 const char *fnname;
22554 x = DECL_RTL (decl);
22555 gcc_assert (MEM_P (x));
22557 x = XEXP (x, 0);
22558 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22560 fnname = XSTR (x, 0);
22561 return fnname;
22563 #endif
22565 /* For variable-length arrays that have been previously generated, but
22566 may be incomplete due to missing subscript info, fill the subscript
22567 info. Return TRUE if this is one of those cases. */
22569 static bool
22570 fill_variable_array_bounds (tree type)
22572 if (TREE_ASM_WRITTEN (type)
22573 && TREE_CODE (type) == ARRAY_TYPE
22574 && variably_modified_type_p (type, NULL))
22576 dw_die_ref array_die = lookup_type_die (type);
22577 if (!array_die)
22578 return false;
22579 add_subscript_info (array_die, type, !is_ada ());
22580 return true;
22582 return false;
22585 /* These routines generate the internal representation of the DIE's for
22586 the compilation unit. Debugging information is collected by walking
22587 the declaration trees passed in from dwarf2out_decl(). */
22589 static void
22590 gen_array_type_die (tree type, dw_die_ref context_die)
22592 dw_die_ref array_die;
22594 /* GNU compilers represent multidimensional array types as sequences of one
22595 dimensional array types whose element types are themselves array types.
22596 We sometimes squish that down to a single array_type DIE with multiple
22597 subscripts in the Dwarf debugging info. The draft Dwarf specification
22598 say that we are allowed to do this kind of compression in C, because
22599 there is no difference between an array of arrays and a multidimensional
22600 array. We don't do this for Ada to remain as close as possible to the
22601 actual representation, which is especially important against the language
22602 flexibilty wrt arrays of variable size. */
22604 bool collapse_nested_arrays = !is_ada ();
22606 if (fill_variable_array_bounds (type))
22607 return;
22609 dw_die_ref scope_die = scope_die_for (type, context_die);
22610 tree element_type;
22612 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22613 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22614 if (TREE_CODE (type) == ARRAY_TYPE
22615 && TYPE_STRING_FLAG (type)
22616 && is_fortran ()
22617 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22619 HOST_WIDE_INT size;
22621 array_die = new_die (DW_TAG_string_type, scope_die, type);
22622 add_name_attribute (array_die, type_tag (type));
22623 equate_type_number_to_die (type, array_die);
22624 size = int_size_in_bytes (type);
22625 if (size >= 0)
22626 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22627 /* ??? We can't annotate types late, but for LTO we may not
22628 generate a location early either (gfortran.dg/save_6.f90). */
22629 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22630 && TYPE_DOMAIN (type) != NULL_TREE
22631 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22633 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22634 tree rszdecl = szdecl;
22636 size = int_size_in_bytes (TREE_TYPE (szdecl));
22637 if (!DECL_P (szdecl))
22639 if (INDIRECT_REF_P (szdecl)
22640 && DECL_P (TREE_OPERAND (szdecl, 0)))
22642 rszdecl = TREE_OPERAND (szdecl, 0);
22643 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22644 != DWARF2_ADDR_SIZE)
22645 size = 0;
22647 else
22648 size = 0;
22650 if (size > 0)
22652 dw_loc_list_ref loc
22653 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22654 NULL);
22655 if (loc)
22657 add_AT_location_description (array_die, DW_AT_string_length,
22658 loc);
22659 if (size != DWARF2_ADDR_SIZE)
22660 add_AT_unsigned (array_die, dwarf_version >= 5
22661 ? DW_AT_string_length_byte_size
22662 : DW_AT_byte_size, size);
22666 return;
22669 array_die = new_die (DW_TAG_array_type, scope_die, type);
22670 add_name_attribute (array_die, type_tag (type));
22671 equate_type_number_to_die (type, array_die);
22673 if (VECTOR_TYPE_P (type))
22674 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22676 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22677 if (is_fortran ()
22678 && TREE_CODE (type) == ARRAY_TYPE
22679 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22680 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22681 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22683 #if 0
22684 /* We default the array ordering. Debuggers will probably do the right
22685 things even if DW_AT_ordering is not present. It's not even an issue
22686 until we start to get into multidimensional arrays anyway. If a debugger
22687 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22688 then we'll have to put the DW_AT_ordering attribute back in. (But if
22689 and when we find out that we need to put these in, we will only do so
22690 for multidimensional arrays. */
22691 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22692 #endif
22694 if (VECTOR_TYPE_P (type))
22696 /* For VECTOR_TYPEs we use an array DIE with appropriate bounds. */
22697 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22698 int lb = lower_bound_default ();
22699 if (lb == -1)
22700 lb = 0;
22701 add_bound_info (subrange_die, DW_AT_lower_bound, size_int (lb), NULL);
22702 add_bound_info (subrange_die, DW_AT_upper_bound,
22703 size_int (lb + TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22705 else
22706 add_subscript_info (array_die, type, collapse_nested_arrays);
22708 /* Add representation of the type of the elements of this array type and
22709 emit the corresponding DIE if we haven't done it already. */
22710 element_type = TREE_TYPE (type);
22711 if (collapse_nested_arrays)
22712 while (TREE_CODE (element_type) == ARRAY_TYPE)
22714 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22715 break;
22716 element_type = TREE_TYPE (element_type);
22719 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22720 TREE_CODE (type) == ARRAY_TYPE
22721 && TYPE_REVERSE_STORAGE_ORDER (type),
22722 context_die);
22724 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22725 if (TYPE_ARTIFICIAL (type))
22726 add_AT_flag (array_die, DW_AT_artificial, 1);
22728 if (get_AT (array_die, DW_AT_name))
22729 add_pubtype (type, array_die);
22731 add_alignment_attribute (array_die, type);
22734 /* This routine generates DIE for array with hidden descriptor, details
22735 are filled into *info by a langhook. */
22737 static void
22738 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22739 dw_die_ref context_die)
22741 const dw_die_ref scope_die = scope_die_for (type, context_die);
22742 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22743 struct loc_descr_context context = {
22744 type, /* context_type */
22745 info->base_decl, /* base_decl */
22746 NULL, /* dpi */
22747 false, /* placeholder_arg */
22748 false, /* placeholder_seen */
22749 false /* strict_signedness */
22751 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22752 int dim;
22754 add_name_attribute (array_die, type_tag (type));
22755 equate_type_number_to_die (type, array_die);
22757 if (info->ndimensions > 1)
22758 switch (info->ordering)
22760 case array_descr_ordering_row_major:
22761 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22762 break;
22763 case array_descr_ordering_column_major:
22764 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22765 break;
22766 default:
22767 break;
22770 if (dwarf_version >= 3 || !dwarf_strict)
22772 if (info->data_location)
22773 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22774 dw_scalar_form_exprloc, &context);
22775 if (info->associated)
22776 add_scalar_info (array_die, DW_AT_associated, info->associated,
22777 dw_scalar_form_constant
22778 | dw_scalar_form_exprloc
22779 | dw_scalar_form_reference, &context);
22780 if (info->allocated)
22781 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22782 dw_scalar_form_constant
22783 | dw_scalar_form_exprloc
22784 | dw_scalar_form_reference, &context);
22785 if (info->stride)
22787 const enum dwarf_attribute attr
22788 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22789 const int forms
22790 = (info->stride_in_bits)
22791 ? dw_scalar_form_constant
22792 : (dw_scalar_form_constant
22793 | dw_scalar_form_exprloc
22794 | dw_scalar_form_reference);
22796 add_scalar_info (array_die, attr, info->stride, forms, &context);
22799 if (dwarf_version >= 5)
22801 if (info->rank)
22803 add_scalar_info (array_die, DW_AT_rank, info->rank,
22804 dw_scalar_form_constant
22805 | dw_scalar_form_exprloc, &context);
22806 subrange_tag = DW_TAG_generic_subrange;
22807 context.placeholder_arg = true;
22811 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22813 for (dim = 0; dim < info->ndimensions; dim++)
22815 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22817 if (info->dimen[dim].bounds_type)
22818 add_type_attribute (subrange_die,
22819 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22820 false, context_die);
22821 if (info->dimen[dim].lower_bound)
22822 add_bound_info (subrange_die, DW_AT_lower_bound,
22823 info->dimen[dim].lower_bound, &context);
22824 if (info->dimen[dim].upper_bound)
22825 add_bound_info (subrange_die, DW_AT_upper_bound,
22826 info->dimen[dim].upper_bound, &context);
22827 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22828 add_scalar_info (subrange_die, DW_AT_byte_stride,
22829 info->dimen[dim].stride,
22830 dw_scalar_form_constant
22831 | dw_scalar_form_exprloc
22832 | dw_scalar_form_reference,
22833 &context);
22836 gen_type_die (info->element_type, context_die);
22837 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22838 TREE_CODE (type) == ARRAY_TYPE
22839 && TYPE_REVERSE_STORAGE_ORDER (type),
22840 context_die);
22842 if (get_AT (array_die, DW_AT_name))
22843 add_pubtype (type, array_die);
22845 add_alignment_attribute (array_die, type);
22848 #if 0
22849 static void
22850 gen_entry_point_die (tree decl, dw_die_ref context_die)
22852 tree origin = decl_ultimate_origin (decl);
22853 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22855 if (origin != NULL)
22856 add_abstract_origin_attribute (decl_die, origin);
22857 else
22859 add_name_and_src_coords_attributes (decl_die, decl);
22860 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22861 TYPE_UNQUALIFIED, false, context_die);
22864 if (DECL_ABSTRACT_P (decl))
22865 equate_decl_number_to_die (decl, decl_die);
22866 else
22867 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22869 #endif
22871 /* Walk through the list of incomplete types again, trying once more to
22872 emit full debugging info for them. */
22874 static void
22875 retry_incomplete_types (void)
22877 set_early_dwarf s;
22878 int i;
22880 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22881 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22882 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22883 vec_safe_truncate (incomplete_types, 0);
22886 /* Determine what tag to use for a record type. */
22888 static enum dwarf_tag
22889 record_type_tag (tree type)
22891 if (! lang_hooks.types.classify_record)
22892 return DW_TAG_structure_type;
22894 switch (lang_hooks.types.classify_record (type))
22896 case RECORD_IS_STRUCT:
22897 return DW_TAG_structure_type;
22899 case RECORD_IS_CLASS:
22900 return DW_TAG_class_type;
22902 case RECORD_IS_INTERFACE:
22903 if (dwarf_version >= 3 || !dwarf_strict)
22904 return DW_TAG_interface_type;
22905 return DW_TAG_structure_type;
22907 default:
22908 gcc_unreachable ();
22912 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22913 include all of the information about the enumeration values also. Each
22914 enumerated type name/value is listed as a child of the enumerated type
22915 DIE. REVERSE is true if the type is to be interpreted in the reverse
22916 storage order wrt the target order. */
22918 static dw_die_ref
22919 gen_enumeration_type_die (tree type, dw_die_ref context_die, bool reverse)
22921 dw_die_ref type_die = lookup_type_die (type);
22922 dw_die_ref orig_type_die = type_die;
22924 if (type_die == NULL || reverse)
22926 dw_die_ref scope_die = scope_die_for (type, context_die);
22928 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
22929 if (reverse)
22931 gcc_assert (type_die);
22932 dw_die_ref after_die = type_die;
22933 type_die = new_die_raw (DW_TAG_enumeration_type);
22934 add_child_die_after (scope_die, type_die, after_die);
22936 else
22938 type_die = new_die (DW_TAG_enumeration_type, scope_die, type);
22939 equate_type_number_to_die (type, type_die);
22941 add_name_attribute (type_die, type_tag (type));
22942 if ((dwarf_version >= 4 || !dwarf_strict)
22943 && ENUM_IS_SCOPED (type))
22944 add_AT_flag (type_die, DW_AT_enum_class, 1);
22945 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22946 add_AT_flag (type_die, DW_AT_declaration, 1);
22947 if (!dwarf_strict)
22948 add_AT_unsigned (type_die, DW_AT_encoding,
22949 TYPE_UNSIGNED (type)
22950 ? DW_ATE_unsigned
22951 : DW_ATE_signed);
22952 if (reverse)
22953 add_AT_unsigned (type_die, DW_AT_endianity,
22954 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
22956 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22957 return type_die;
22958 else
22959 remove_AT (type_die, DW_AT_declaration);
22961 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22962 given enum type is incomplete, do not generate the DW_AT_byte_size
22963 attribute or the DW_AT_element_list attribute. */
22964 if (TYPE_SIZE (type))
22966 tree link;
22968 if (!ENUM_IS_OPAQUE (type))
22969 TREE_ASM_WRITTEN (type) = 1;
22970 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22971 add_byte_size_attribute (type_die, type);
22972 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22973 add_alignment_attribute (type_die, type);
22974 if ((dwarf_version >= 3 || !dwarf_strict)
22975 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22977 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22978 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22979 context_die);
22981 if (TYPE_STUB_DECL (type) != NULL_TREE)
22983 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22984 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22985 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22986 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22989 /* If the first reference to this type was as the return type of an
22990 inline function, then it may not have a parent. Fix this now. */
22991 if (type_die->die_parent == NULL)
22992 add_child_die (scope_die_for (type, context_die), type_die);
22994 for (link = TYPE_VALUES (type);
22995 link != NULL; link = TREE_CHAIN (link))
22997 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22998 tree value = TREE_VALUE (link);
23000 if (DECL_P (value))
23001 equate_decl_number_to_die (value, enum_die);
23003 gcc_assert (!ENUM_IS_OPAQUE (type));
23004 add_name_attribute (enum_die,
23005 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
23007 if (TREE_CODE (value) == CONST_DECL)
23008 value = DECL_INITIAL (value);
23010 if (simple_type_size_in_bits (TREE_TYPE (value))
23011 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
23013 /* For constant forms created by add_AT_unsigned DWARF
23014 consumers (GDB, elfutils, etc.) always zero extend
23015 the value. Only when the actual value is negative
23016 do we need to use add_AT_int to generate a constant
23017 form that can represent negative values. */
23018 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
23019 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
23020 add_AT_unsigned (enum_die, DW_AT_const_value,
23021 (unsigned HOST_WIDE_INT) val);
23022 else
23023 add_AT_int (enum_die, DW_AT_const_value, val);
23025 else
23026 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
23027 that here. TODO: This should be re-worked to use correct
23028 signed/unsigned double tags for all cases. */
23029 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
23032 add_gnat_descriptive_type_attribute (type_die, type, context_die);
23033 if (TYPE_ARTIFICIAL (type)
23034 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
23035 add_AT_flag (type_die, DW_AT_artificial, 1);
23037 else
23038 add_AT_flag (type_die, DW_AT_declaration, 1);
23040 add_pubtype (type, type_die);
23042 return type_die;
23045 /* Generate a DIE to represent either a real live formal parameter decl or to
23046 represent just the type of some formal parameter position in some function
23047 type.
23049 Note that this routine is a bit unusual because its argument may be a
23050 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
23051 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
23052 node. If it's the former then this function is being called to output a
23053 DIE to represent a formal parameter object (or some inlining thereof). If
23054 it's the latter, then this function is only being called to output a
23055 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
23056 argument type of some subprogram type.
23057 If EMIT_NAME_P is true, name and source coordinate attributes
23058 are emitted. */
23060 static dw_die_ref
23061 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
23062 dw_die_ref context_die)
23064 tree node_or_origin = node ? node : origin;
23065 tree ultimate_origin;
23066 dw_die_ref parm_die = NULL;
23068 if (DECL_P (node_or_origin))
23070 parm_die = lookup_decl_die (node);
23072 /* If the contexts differ, we may not be talking about the same
23073 thing.
23074 ??? When in LTO the DIE parent is the "abstract" copy and the
23075 context_die is the specification "copy". */
23076 if (parm_die
23077 && parm_die->die_parent != context_die
23078 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
23079 || parm_die->die_parent->die_parent != context_die)
23080 && !in_lto_p)
23082 gcc_assert (!DECL_ABSTRACT_P (node));
23083 /* This can happen when creating a concrete instance, in
23084 which case we need to create a new DIE that will get
23085 annotated with DW_AT_abstract_origin. */
23086 parm_die = NULL;
23089 if (parm_die && parm_die->die_parent == NULL)
23091 /* Check that parm_die already has the right attributes that
23092 we would have added below. If any attributes are
23093 missing, fall through to add them. */
23094 if (! DECL_ABSTRACT_P (node_or_origin)
23095 && !get_AT (parm_die, DW_AT_location)
23096 && !get_AT (parm_die, DW_AT_const_value))
23097 /* We are missing location info, and are about to add it. */
23099 else
23101 add_child_die (context_die, parm_die);
23102 return parm_die;
23107 /* If we have a previously generated DIE, use it, unless this is an
23108 concrete instance (origin != NULL), in which case we need a new
23109 DIE with a corresponding DW_AT_abstract_origin. */
23110 bool reusing_die;
23111 if (parm_die && origin == NULL)
23112 reusing_die = true;
23113 else
23115 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
23116 reusing_die = false;
23119 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
23121 case tcc_declaration:
23122 ultimate_origin = decl_ultimate_origin (node_or_origin);
23123 if (node || ultimate_origin)
23124 origin = ultimate_origin;
23126 if (reusing_die)
23127 goto add_location;
23129 if (origin != NULL)
23130 add_abstract_origin_attribute (parm_die, origin);
23131 else if (emit_name_p)
23132 add_name_and_src_coords_attributes (parm_die, node);
23133 if (origin == NULL
23134 || (! DECL_ABSTRACT_P (node_or_origin)
23135 && variably_modified_type_p (TREE_TYPE (node_or_origin),
23136 decl_function_context
23137 (node_or_origin))))
23139 tree type = TREE_TYPE (node_or_origin);
23140 if (decl_by_reference_p (node_or_origin))
23141 add_type_attribute (parm_die, TREE_TYPE (type),
23142 TYPE_UNQUALIFIED,
23143 false, context_die);
23144 else
23145 add_type_attribute (parm_die, type,
23146 decl_quals (node_or_origin),
23147 false, context_die);
23149 if (origin == NULL && DECL_ARTIFICIAL (node))
23150 add_AT_flag (parm_die, DW_AT_artificial, 1);
23151 add_location:
23152 if (node && node != origin)
23153 equate_decl_number_to_die (node, parm_die);
23154 if (! DECL_ABSTRACT_P (node_or_origin))
23155 add_location_or_const_value_attribute (parm_die, node_or_origin,
23156 node == NULL);
23158 break;
23160 case tcc_type:
23161 /* We were called with some kind of a ..._TYPE node. */
23162 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
23163 context_die);
23164 break;
23166 default:
23167 gcc_unreachable ();
23170 return parm_die;
23173 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
23174 children DW_TAG_formal_parameter DIEs representing the arguments of the
23175 parameter pack.
23177 PARM_PACK must be a function parameter pack.
23178 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
23179 must point to the subsequent arguments of the function PACK_ARG belongs to.
23180 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
23181 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
23182 following the last one for which a DIE was generated. */
23184 static dw_die_ref
23185 gen_formal_parameter_pack_die (tree parm_pack,
23186 tree pack_arg,
23187 dw_die_ref subr_die,
23188 tree *next_arg)
23190 tree arg;
23191 dw_die_ref parm_pack_die;
23193 gcc_assert (parm_pack
23194 && lang_hooks.function_parameter_pack_p (parm_pack)
23195 && subr_die);
23197 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
23198 add_src_coords_attributes (parm_pack_die, parm_pack);
23200 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
23202 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
23203 parm_pack))
23204 break;
23205 gen_formal_parameter_die (arg, NULL,
23206 false /* Don't emit name attribute. */,
23207 parm_pack_die);
23209 if (next_arg)
23210 *next_arg = arg;
23211 return parm_pack_die;
23214 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
23215 at the end of an (ANSI prototyped) formal parameters list. */
23217 static void
23218 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
23220 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
23223 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
23224 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
23225 parameters as specified in some function type specification (except for
23226 those which appear as part of a function *definition*). */
23228 static void
23229 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
23231 tree link;
23232 tree formal_type = NULL;
23233 tree first_parm_type;
23234 tree arg;
23236 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
23238 arg = DECL_ARGUMENTS (function_or_method_type);
23239 function_or_method_type = TREE_TYPE (function_or_method_type);
23241 else
23242 arg = NULL_TREE;
23244 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
23246 /* Make our first pass over the list of formal parameter types and output a
23247 DW_TAG_formal_parameter DIE for each one. */
23248 for (link = first_parm_type; link; )
23250 dw_die_ref parm_die;
23252 formal_type = TREE_VALUE (link);
23253 if (formal_type == void_type_node)
23254 break;
23256 /* Output a (nameless) DIE to represent the formal parameter itself. */
23257 parm_die = gen_formal_parameter_die (formal_type, NULL,
23258 true /* Emit name attribute. */,
23259 context_die);
23260 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
23261 && link == first_parm_type)
23263 add_AT_flag (parm_die, DW_AT_artificial, 1);
23264 if (dwarf_version >= 3 || !dwarf_strict)
23265 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
23267 else if (arg && DECL_ARTIFICIAL (arg))
23268 add_AT_flag (parm_die, DW_AT_artificial, 1);
23270 link = TREE_CHAIN (link);
23271 if (arg)
23272 arg = DECL_CHAIN (arg);
23275 /* If this function type has an ellipsis, add a
23276 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
23277 if (formal_type != void_type_node)
23278 gen_unspecified_parameters_die (function_or_method_type, context_die);
23280 /* Make our second (and final) pass over the list of formal parameter types
23281 and output DIEs to represent those types (as necessary). */
23282 for (link = TYPE_ARG_TYPES (function_or_method_type);
23283 link && TREE_VALUE (link);
23284 link = TREE_CHAIN (link))
23285 gen_type_die (TREE_VALUE (link), context_die);
23288 /* We want to generate the DIE for TYPE so that we can generate the
23289 die for MEMBER, which has been defined; we will need to refer back
23290 to the member declaration nested within TYPE. If we're trying to
23291 generate minimal debug info for TYPE, processing TYPE won't do the
23292 trick; we need to attach the member declaration by hand. */
23294 static void
23295 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
23297 gen_type_die (type, context_die);
23299 /* If we're trying to avoid duplicate debug info, we may not have
23300 emitted the member decl for this function. Emit it now. */
23301 if (TYPE_STUB_DECL (type)
23302 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
23303 && ! lookup_decl_die (member))
23305 dw_die_ref type_die;
23306 gcc_assert (!decl_ultimate_origin (member));
23308 type_die = lookup_type_die_strip_naming_typedef (type);
23309 if (TREE_CODE (member) == FUNCTION_DECL)
23310 gen_subprogram_die (member, type_die);
23311 else if (TREE_CODE (member) == FIELD_DECL)
23313 /* Ignore the nameless fields that are used to skip bits but handle
23314 C++ anonymous unions and structs. */
23315 if (DECL_NAME (member) != NULL_TREE
23316 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
23317 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
23319 struct vlr_context vlr_ctx = {
23320 DECL_CONTEXT (member), /* struct_type */
23321 NULL_TREE /* variant_part_offset */
23323 gen_type_die (member_declared_type (member), type_die);
23324 gen_field_die (member, &vlr_ctx, type_die);
23327 else
23328 gen_variable_die (member, NULL_TREE, type_die);
23332 /* Forward declare these functions, because they are mutually recursive
23333 with their set_block_* pairing functions. */
23334 static void set_decl_origin_self (tree);
23336 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23337 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23338 that it points to the node itself, thus indicating that the node is its
23339 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23340 the given node is NULL, recursively descend the decl/block tree which
23341 it is the root of, and for each other ..._DECL or BLOCK node contained
23342 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23343 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23344 values to point to themselves. */
23346 static void
23347 set_block_origin_self (tree stmt)
23349 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23351 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23354 tree local_decl;
23356 for (local_decl = BLOCK_VARS (stmt);
23357 local_decl != NULL_TREE;
23358 local_decl = DECL_CHAIN (local_decl))
23359 /* Do not recurse on nested functions since the inlining status
23360 of parent and child can be different as per the DWARF spec. */
23361 if (TREE_CODE (local_decl) != FUNCTION_DECL
23362 && !DECL_EXTERNAL (local_decl))
23363 set_decl_origin_self (local_decl);
23367 tree subblock;
23369 for (subblock = BLOCK_SUBBLOCKS (stmt);
23370 subblock != NULL_TREE;
23371 subblock = BLOCK_CHAIN (subblock))
23372 set_block_origin_self (subblock); /* Recurse. */
23377 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23378 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23379 node to so that it points to the node itself, thus indicating that the
23380 node represents its own (abstract) origin. Additionally, if the
23381 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23382 the decl/block tree of which the given node is the root of, and for
23383 each other ..._DECL or BLOCK node contained therein whose
23384 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23385 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23386 point to themselves. */
23388 static void
23389 set_decl_origin_self (tree decl)
23391 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23393 DECL_ABSTRACT_ORIGIN (decl) = decl;
23394 if (TREE_CODE (decl) == FUNCTION_DECL)
23396 tree arg;
23398 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23399 DECL_ABSTRACT_ORIGIN (arg) = arg;
23400 if (DECL_INITIAL (decl) != NULL_TREE
23401 && DECL_INITIAL (decl) != error_mark_node)
23402 set_block_origin_self (DECL_INITIAL (decl));
23407 /* Mark the early DIE for DECL as the abstract instance. */
23409 static void
23410 dwarf2out_abstract_function (tree decl)
23412 dw_die_ref old_die;
23414 /* Make sure we have the actual abstract inline, not a clone. */
23415 decl = DECL_ORIGIN (decl);
23417 if (DECL_IGNORED_P (decl))
23418 return;
23420 #ifdef CODEVIEW_DEBUGGING_INFO
23421 if (codeview_debuginfo_p ())
23422 codeview_abstract_function (decl);
23423 #endif
23425 /* In LTO we're all set. We already created abstract instances
23426 early and we want to avoid creating a concrete instance of that
23427 if we don't output it. */
23428 if (in_lto_p)
23429 return;
23431 old_die = lookup_decl_die (decl);
23432 gcc_assert (old_die != NULL);
23433 if (get_AT (old_die, DW_AT_inline))
23434 /* We've already generated the abstract instance. */
23435 return;
23437 /* Go ahead and put DW_AT_inline on the DIE. */
23438 if (DECL_DECLARED_INLINE_P (decl))
23440 if (cgraph_function_possibly_inlined_p (decl))
23441 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23442 else
23443 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23445 else
23447 if (cgraph_function_possibly_inlined_p (decl))
23448 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23449 else
23450 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23453 if (DECL_DECLARED_INLINE_P (decl)
23454 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23455 add_AT_flag (old_die, DW_AT_artificial, 1);
23457 set_decl_origin_self (decl);
23460 /* Helper function of premark_used_types() which gets called through
23461 htab_traverse.
23463 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23464 marked as unused by prune_unused_types. */
23466 bool
23467 premark_used_types_helper (tree const &type, void *)
23469 dw_die_ref die;
23471 die = lookup_type_die (type);
23472 if (die != NULL)
23473 die->die_perennial_p = 1;
23474 return true;
23477 /* Helper function of premark_types_used_by_global_vars which gets called
23478 through htab_traverse.
23480 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23481 marked as unused by prune_unused_types. The DIE of the type is marked
23482 only if the global variable using the type will actually be emitted. */
23485 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23486 void *)
23488 struct types_used_by_vars_entry *entry;
23489 dw_die_ref die;
23491 entry = (struct types_used_by_vars_entry *) *slot;
23492 gcc_assert (entry->type != NULL
23493 && entry->var_decl != NULL);
23494 die = lookup_type_die (entry->type);
23495 if (die)
23497 /* Ask cgraph if the global variable really is to be emitted.
23498 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23499 varpool_node *node = varpool_node::get (entry->var_decl);
23500 if (node && node->definition)
23502 die->die_perennial_p = 1;
23503 /* Keep the parent DIEs as well. */
23504 while ((die = die->die_parent) && die->die_perennial_p == 0)
23505 die->die_perennial_p = 1;
23508 return 1;
23511 /* Mark all members of used_types_hash as perennial. */
23513 static void
23514 premark_used_types (struct function *fun)
23516 if (fun && fun->used_types_hash)
23517 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23520 /* Mark all members of types_used_by_vars_entry as perennial. */
23522 static void
23523 premark_types_used_by_global_vars (void)
23525 if (types_used_by_vars_hash)
23526 types_used_by_vars_hash
23527 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23530 /* Mark all variables used by the symtab as perennial. */
23532 static void
23533 premark_used_variables (void)
23535 /* Mark DIEs in the symtab as used. */
23536 varpool_node *var;
23537 FOR_EACH_VARIABLE (var)
23539 dw_die_ref die = lookup_decl_die (var->decl);
23540 if (die)
23541 die->die_perennial_p = 1;
23545 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23546 for CA_LOC call arg loc node. */
23548 static dw_die_ref
23549 gen_call_site_die (tree decl, dw_die_ref subr_die,
23550 struct call_arg_loc_node *ca_loc)
23552 dw_die_ref stmt_die = NULL, die;
23553 tree block = ca_loc->block;
23555 while (block
23556 && block != DECL_INITIAL (decl)
23557 && TREE_CODE (block) == BLOCK)
23559 stmt_die = lookup_block_die (block);
23560 if (stmt_die)
23561 break;
23562 block = BLOCK_SUPERCONTEXT (block);
23564 if (stmt_die == NULL)
23565 stmt_die = subr_die;
23566 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23567 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc),
23568 ca_loc->label,
23569 targetm.calls.call_offset_return_label (ca_loc->call_insn));
23570 if (ca_loc->tail_call_p)
23571 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23572 if (ca_loc->symbol_ref)
23574 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23575 if (tdie)
23576 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23577 else
23578 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23579 false);
23581 return die;
23584 /* Generate a DIE to represent a declared function (either file-scope or
23585 block-local). */
23587 static void
23588 gen_subprogram_die (tree decl, dw_die_ref context_die)
23590 tree origin = decl_ultimate_origin (decl);
23591 dw_die_ref subr_die;
23592 dw_die_ref old_die = lookup_decl_die (decl);
23593 bool old_die_had_no_children = false;
23595 /* This function gets called multiple times for different stages of
23596 the debug process. For example, for func() in this code:
23598 namespace S
23600 void func() { ... }
23603 ...we get called 4 times. Twice in early debug and twice in
23604 late debug:
23606 Early debug
23607 -----------
23609 1. Once while generating func() within the namespace. This is
23610 the declaration. The declaration bit below is set, as the
23611 context is the namespace.
23613 A new DIE will be generated with DW_AT_declaration set.
23615 2. Once for func() itself. This is the specification. The
23616 declaration bit below is clear as the context is the CU.
23618 We will use the cached DIE from (1) to create a new DIE with
23619 DW_AT_specification pointing to the declaration in (1).
23621 Late debug via rest_of_handle_final()
23622 -------------------------------------
23624 3. Once generating func() within the namespace. This is also the
23625 declaration, as in (1), but this time we will early exit below
23626 as we have a cached DIE and a declaration needs no additional
23627 annotations (no locations), as the source declaration line
23628 info is enough.
23630 4. Once for func() itself. As in (2), this is the specification,
23631 but this time we will re-use the cached DIE, and just annotate
23632 it with the location information that should now be available.
23634 For something without namespaces, but with abstract instances, we
23635 are also called a multiple times:
23637 class Base
23639 public:
23640 Base (); // constructor declaration (1)
23643 Base::Base () { } // constructor specification (2)
23645 Early debug
23646 -----------
23648 1. Once for the Base() constructor by virtue of it being a
23649 member of the Base class. This is done via
23650 rest_of_type_compilation.
23652 This is a declaration, so a new DIE will be created with
23653 DW_AT_declaration.
23655 2. Once for the Base() constructor definition, but this time
23656 while generating the abstract instance of the base
23657 constructor (__base_ctor) which is being generated via early
23658 debug of reachable functions.
23660 Even though we have a cached version of the declaration (1),
23661 we will create a DW_AT_specification of the declaration DIE
23662 in (1).
23664 3. Once for the __base_ctor itself, but this time, we generate
23665 an DW_AT_abstract_origin version of the DW_AT_specification in
23666 (2).
23668 Late debug via rest_of_handle_final
23669 -----------------------------------
23671 4. One final time for the __base_ctor (which will have a cached
23672 DIE with DW_AT_abstract_origin created in (3). This time,
23673 we will just annotate the location information now
23674 available.
23676 int declaration = (current_function_decl != decl
23677 || (!DECL_INITIAL (decl) && !origin)
23678 || class_or_namespace_scope_p (context_die));
23680 /* A declaration that has been previously dumped needs no
23681 additional information. */
23682 if (old_die && declaration)
23683 return;
23685 if (in_lto_p && old_die && old_die->die_child == NULL)
23686 old_die_had_no_children = true;
23688 /* Now that the C++ front end lazily declares artificial member fns, we
23689 might need to retrofit the declaration into its class. */
23690 if (!declaration && !origin && !old_die
23691 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23692 && !class_or_namespace_scope_p (context_die)
23693 && debug_info_level > DINFO_LEVEL_TERSE)
23694 old_die = force_decl_die (decl);
23696 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23697 if (origin != NULL)
23699 gcc_assert (!declaration || local_scope_p (context_die));
23701 /* Fixup die_parent for the abstract instance of a nested
23702 inline function. */
23703 if (old_die && old_die->die_parent == NULL)
23704 add_child_die (context_die, old_die);
23706 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23708 /* If we have a DW_AT_abstract_origin we have a working
23709 cached version. */
23710 subr_die = old_die;
23712 else
23714 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23715 add_abstract_origin_attribute (subr_die, origin);
23716 /* This is where the actual code for a cloned function is.
23717 Let's emit linkage name attribute for it. This helps
23718 debuggers to e.g, set breakpoints into
23719 constructors/destructors when the user asks "break
23720 K::K". */
23721 add_linkage_name (subr_die, decl);
23724 /* A cached copy, possibly from early dwarf generation. Reuse as
23725 much as possible. */
23726 else if (old_die)
23728 if (!get_AT_flag (old_die, DW_AT_declaration)
23729 /* We can have a normal definition following an inline one in the
23730 case of redefinition of GNU C extern inlines.
23731 It seems reasonable to use AT_specification in this case. */
23732 && !get_AT (old_die, DW_AT_inline))
23734 /* Detect and ignore this case, where we are trying to output
23735 something we have already output. */
23736 if (get_AT (old_die, DW_AT_low_pc)
23737 || get_AT (old_die, DW_AT_ranges))
23738 return;
23740 /* If we have no location information, this must be a
23741 partially generated DIE from early dwarf generation.
23742 Fall through and generate it. */
23745 /* If the definition comes from the same place as the declaration,
23746 maybe use the old DIE. We always want the DIE for this function
23747 that has the *_pc attributes to be under comp_unit_die so the
23748 debugger can find it. We also need to do this for abstract
23749 instances of inlines, since the spec requires the out-of-line copy
23750 to have the same parent. For local class methods, this doesn't
23751 apply; we just use the old DIE. */
23752 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23753 struct dwarf_file_data * file_index = lookup_filename (s.file);
23754 if (((is_unit_die (old_die->die_parent)
23755 /* This condition fixes the inconsistency/ICE with the
23756 following Fortran test (or some derivative thereof) while
23757 building libgfortran:
23759 module some_m
23760 contains
23761 logical function funky (FLAG)
23762 funky = .true.
23763 end function
23764 end module
23766 || (old_die->die_parent
23767 && old_die->die_parent->die_tag == DW_TAG_module)
23768 || local_scope_p (old_die->die_parent)
23769 || context_die == NULL)
23770 && (DECL_ARTIFICIAL (decl)
23771 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23772 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23773 == (unsigned) s.line)
23774 && (!debug_column_info
23775 || s.column == 0
23776 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23777 == (unsigned) s.column)))))
23778 /* With LTO if there's an abstract instance for
23779 the old DIE, this is a concrete instance and
23780 thus re-use the DIE. */
23781 || get_AT (old_die, DW_AT_abstract_origin))
23783 subr_die = old_die;
23785 /* Clear out the declaration attribute, but leave the
23786 parameters so they can be augmented with location
23787 information later. Unless this was a declaration, in
23788 which case, wipe out the nameless parameters and recreate
23789 them further down. */
23790 if (remove_AT (subr_die, DW_AT_declaration))
23793 remove_AT (subr_die, DW_AT_object_pointer);
23794 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23797 /* Make a specification pointing to the previously built
23798 declaration. */
23799 else
23801 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23802 add_AT_specification (subr_die, old_die);
23803 add_pubname (decl, subr_die);
23804 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23805 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23806 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23807 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23808 if (debug_column_info
23809 && s.column
23810 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23811 != (unsigned) s.column))
23812 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23814 /* If the prototype had an 'auto' or 'decltype(auto)' in
23815 the return type, emit the real type on the definition die. */
23816 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23818 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23819 while (die
23820 && (die->die_tag == DW_TAG_reference_type
23821 || die->die_tag == DW_TAG_rvalue_reference_type
23822 || die->die_tag == DW_TAG_pointer_type
23823 || die->die_tag == DW_TAG_const_type
23824 || die->die_tag == DW_TAG_volatile_type
23825 || die->die_tag == DW_TAG_restrict_type
23826 || die->die_tag == DW_TAG_array_type
23827 || die->die_tag == DW_TAG_ptr_to_member_type
23828 || die->die_tag == DW_TAG_subroutine_type))
23829 die = get_AT_ref (die, DW_AT_type);
23830 if (die == auto_die || die == decltype_auto_die)
23831 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23832 TYPE_UNQUALIFIED, false, context_die);
23835 /* When we process the method declaration, we haven't seen
23836 the out-of-class defaulted definition yet, so we have to
23837 recheck now. */
23838 if ((dwarf_version >= 5 || ! dwarf_strict)
23839 && !get_AT (subr_die, DW_AT_defaulted))
23841 int defaulted
23842 = lang_hooks.decls.decl_dwarf_attribute (decl,
23843 DW_AT_defaulted);
23844 if (defaulted != -1)
23846 /* Other values must have been handled before. */
23847 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23848 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23853 /* Create a fresh DIE for anything else. */
23854 else
23856 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23858 if (TREE_PUBLIC (decl))
23859 add_AT_flag (subr_die, DW_AT_external, 1);
23861 add_name_and_src_coords_attributes (subr_die, decl);
23862 add_pubname (decl, subr_die);
23863 if (debug_info_level > DINFO_LEVEL_TERSE)
23865 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23866 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23867 TYPE_UNQUALIFIED, false, context_die);
23870 add_pure_or_virtual_attribute (subr_die, decl);
23871 if (DECL_ARTIFICIAL (decl))
23872 add_AT_flag (subr_die, DW_AT_artificial, 1);
23874 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23875 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23877 add_alignment_attribute (subr_die, decl);
23879 add_accessibility_attribute (subr_die, decl);
23882 /* Unless we have an existing non-declaration DIE, equate the new
23883 DIE. */
23884 if (!old_die || is_declaration_die (old_die))
23885 equate_decl_number_to_die (decl, subr_die);
23887 if (declaration)
23889 if (!old_die || !get_AT (old_die, DW_AT_inline))
23891 add_AT_flag (subr_die, DW_AT_declaration, 1);
23893 /* If this is an explicit function declaration then generate
23894 a DW_AT_explicit attribute. */
23895 if ((dwarf_version >= 3 || !dwarf_strict)
23896 && lang_hooks.decls.decl_dwarf_attribute (decl,
23897 DW_AT_explicit) == 1)
23898 add_AT_flag (subr_die, DW_AT_explicit, 1);
23900 /* If this is a C++11 deleted special function member then generate
23901 a DW_AT_deleted attribute. */
23902 if ((dwarf_version >= 5 || !dwarf_strict)
23903 && lang_hooks.decls.decl_dwarf_attribute (decl,
23904 DW_AT_deleted) == 1)
23905 add_AT_flag (subr_die, DW_AT_deleted, 1);
23907 /* If this is a C++11 defaulted special function member then
23908 generate a DW_AT_defaulted attribute. */
23909 if (dwarf_version >= 5 || !dwarf_strict)
23911 int defaulted
23912 = lang_hooks.decls.decl_dwarf_attribute (decl,
23913 DW_AT_defaulted);
23914 if (defaulted != -1)
23915 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23918 /* If this is a C++11 non-static member function with & ref-qualifier
23919 then generate a DW_AT_reference attribute. */
23920 if ((dwarf_version >= 5 || !dwarf_strict)
23921 && lang_hooks.decls.decl_dwarf_attribute (decl,
23922 DW_AT_reference) == 1)
23923 add_AT_flag (subr_die, DW_AT_reference, 1);
23925 /* If this is a C++11 non-static member function with &&
23926 ref-qualifier then generate a DW_AT_reference attribute. */
23927 if ((dwarf_version >= 5 || !dwarf_strict)
23928 && lang_hooks.decls.decl_dwarf_attribute (decl,
23929 DW_AT_rvalue_reference)
23930 == 1)
23931 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23934 /* For non DECL_EXTERNALs, if range information is available, fill
23935 the DIE with it. */
23936 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23938 HOST_WIDE_INT cfa_fb_offset;
23940 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23942 if (!crtl->has_bb_partition)
23944 dw_fde_ref fde = fun->fde;
23945 if (fde->dw_fde_begin)
23947 /* We have already generated the labels. */
23948 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23949 fde->dw_fde_end, false);
23951 else
23953 /* Create start/end labels and add the range. */
23954 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23955 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23956 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23957 current_function_funcdef_no);
23958 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23959 current_function_funcdef_no);
23960 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23961 false);
23964 #if VMS_DEBUGGING_INFO
23965 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23966 Section 2.3 Prologue and Epilogue Attributes:
23967 When a breakpoint is set on entry to a function, it is generally
23968 desirable for execution to be suspended, not on the very first
23969 instruction of the function, but rather at a point after the
23970 function's frame has been set up, after any language defined local
23971 declaration processing has been completed, and before execution of
23972 the first statement of the function begins. Debuggers generally
23973 cannot properly determine where this point is. Similarly for a
23974 breakpoint set on exit from a function. The prologue and epilogue
23975 attributes allow a compiler to communicate the location(s) to use. */
23978 if (fde->dw_fde_vms_end_prologue)
23979 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23980 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23982 if (fde->dw_fde_vms_begin_epilogue)
23983 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23984 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23986 #endif
23989 else
23991 /* Generate pubnames entries for the split function code ranges. */
23992 dw_fde_ref fde = fun->fde;
23994 if (fde->dw_fde_second_begin)
23996 if (dwarf_version >= 3 || !dwarf_strict)
23998 /* We should use ranges for non-contiguous code section
23999 addresses. Use the actual code range for the initial
24000 section, since the HOT/COLD labels might precede an
24001 alignment offset. */
24002 bool range_list_added = false;
24003 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
24004 fde->dw_fde_end, &range_list_added,
24005 false);
24006 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
24007 fde->dw_fde_second_end,
24008 &range_list_added, false);
24009 if (range_list_added)
24010 add_ranges (NULL);
24012 else
24014 /* There is no real support in DW2 for this .. so we make
24015 a work-around. First, emit the pub name for the segment
24016 containing the function label. Then make and emit a
24017 simplified subprogram DIE for the second segment with the
24018 name pre-fixed by __hot/cold_sect_of_. We use the same
24019 linkage name for the second die so that gdb will find both
24020 sections when given "b foo". */
24021 const char *name = NULL;
24022 tree decl_name = DECL_NAME (decl);
24023 dw_die_ref seg_die;
24025 /* Do the 'primary' section. */
24026 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
24027 fde->dw_fde_end, false);
24029 /* Build a minimal DIE for the secondary section. */
24030 seg_die = new_die (DW_TAG_subprogram,
24031 subr_die->die_parent, decl);
24033 if (TREE_PUBLIC (decl))
24034 add_AT_flag (seg_die, DW_AT_external, 1);
24036 if (decl_name != NULL
24037 && IDENTIFIER_POINTER (decl_name) != NULL)
24039 name = dwarf2_name (decl, 1);
24040 if (! DECL_ARTIFICIAL (decl))
24041 add_src_coords_attributes (seg_die, decl);
24043 add_linkage_name (seg_die, decl);
24045 gcc_assert (name != NULL);
24046 add_pure_or_virtual_attribute (seg_die, decl);
24047 if (DECL_ARTIFICIAL (decl))
24048 add_AT_flag (seg_die, DW_AT_artificial, 1);
24050 name = concat ("__second_sect_of_", name, NULL);
24051 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
24052 fde->dw_fde_second_end, false);
24053 add_name_attribute (seg_die, name);
24054 if (want_pubnames ())
24055 add_pubname_string (name, seg_die);
24058 else
24059 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
24060 false);
24063 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
24065 /* We define the "frame base" as the function's CFA. This is more
24066 convenient for several reasons: (1) It's stable across the prologue
24067 and epilogue, which makes it better than just a frame pointer,
24068 (2) With dwarf3, there exists a one-byte encoding that allows us
24069 to reference the .debug_frame data by proxy, but failing that,
24070 (3) We can at least reuse the code inspection and interpretation
24071 code that determines the CFA position at various points in the
24072 function. */
24073 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
24075 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
24076 add_AT_loc (subr_die, DW_AT_frame_base, op);
24078 else
24080 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
24081 if (list->dw_loc_next)
24082 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
24083 else
24084 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
24087 /* Compute a displacement from the "steady-state frame pointer" to
24088 the CFA. The former is what all stack slots and argument slots
24089 will reference in the rtl; the latter is what we've told the
24090 debugger about. We'll need to adjust all frame_base references
24091 by this displacement. */
24092 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
24094 if (fun->static_chain_decl)
24096 /* DWARF requires here a location expression that computes the
24097 address of the enclosing subprogram's frame base. The machinery
24098 in tree-nested.cc is supposed to store this specific address in the
24099 last field of the FRAME record. */
24100 const tree frame_type
24101 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
24102 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
24104 tree fb_expr
24105 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
24106 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
24107 fb_expr, fb_decl, NULL_TREE);
24109 add_AT_location_description (subr_die, DW_AT_static_link,
24110 loc_list_from_tree (fb_expr, 0, NULL));
24113 resolve_variable_values ();
24116 /* Generate child dies for template parameters. */
24117 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
24118 gen_generic_params_dies (decl);
24120 /* Now output descriptions of the arguments for this function. This gets
24121 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
24122 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
24123 `...' at the end of the formal parameter list. In order to find out if
24124 there was a trailing ellipsis or not, we must instead look at the type
24125 associated with the FUNCTION_DECL. This will be a node of type
24126 FUNCTION_TYPE. If the chain of type nodes hanging off of this
24127 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
24128 an ellipsis at the end. */
24130 /* In the case where we are describing a mere function declaration, all we
24131 need to do here (and all we *can* do here) is to describe the *types* of
24132 its formal parameters. */
24133 if (debug_info_level <= DINFO_LEVEL_TERSE)
24135 else if (declaration)
24136 gen_formal_types_die (decl, subr_die);
24137 else
24139 /* Generate DIEs to represent all known formal parameters. */
24140 tree parm = DECL_ARGUMENTS (decl);
24141 tree generic_decl = early_dwarf
24142 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
24143 tree generic_decl_parm = generic_decl
24144 ? DECL_ARGUMENTS (generic_decl)
24145 : NULL;
24147 /* Now we want to walk the list of parameters of the function and
24148 emit their relevant DIEs.
24150 We consider the case of DECL being an instance of a generic function
24151 as well as it being a normal function.
24153 If DECL is an instance of a generic function we walk the
24154 parameters of the generic function declaration _and_ the parameters of
24155 DECL itself. This is useful because we want to emit specific DIEs for
24156 function parameter packs and those are declared as part of the
24157 generic function declaration. In that particular case,
24158 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
24159 That DIE has children DIEs representing the set of arguments
24160 of the pack. Note that the set of pack arguments can be empty.
24161 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
24162 children DIE.
24164 Otherwise, we just consider the parameters of DECL. */
24165 while (generic_decl_parm || parm)
24167 if (generic_decl_parm
24168 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
24169 gen_formal_parameter_pack_die (generic_decl_parm,
24170 parm, subr_die,
24171 &parm);
24172 else if (parm)
24174 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
24176 if (early_dwarf
24177 && parm == DECL_ARGUMENTS (decl)
24178 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
24179 && parm_die
24180 && (dwarf_version >= 3 || !dwarf_strict))
24181 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
24183 parm = DECL_CHAIN (parm);
24186 if (generic_decl_parm)
24187 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
24190 /* Decide whether we need an unspecified_parameters DIE at the end.
24191 There are 2 more cases to do this for: 1) the ansi ... declaration -
24192 this is detectable when the end of the arg list is not a
24193 void_type_node 2) an unprototyped function declaration (not a
24194 definition). This just means that we have no info about the
24195 parameters at all. */
24196 if (early_dwarf)
24198 if (prototype_p (TREE_TYPE (decl)))
24200 /* This is the prototyped case, check for.... */
24201 if (stdarg_p (TREE_TYPE (decl)))
24202 gen_unspecified_parameters_die (decl, subr_die);
24204 else if (DECL_INITIAL (decl) == NULL_TREE)
24205 gen_unspecified_parameters_die (decl, subr_die);
24207 else if ((subr_die != old_die || old_die_had_no_children)
24208 && prototype_p (TREE_TYPE (decl))
24209 && stdarg_p (TREE_TYPE (decl)))
24210 gen_unspecified_parameters_die (decl, subr_die);
24213 if (subr_die != old_die)
24214 /* Add the calling convention attribute if requested. */
24215 add_calling_convention_attribute (subr_die, decl);
24217 /* Output Dwarf info for all of the stuff within the body of the function
24218 (if it has one - it may be just a declaration).
24220 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
24221 a function. This BLOCK actually represents the outermost binding contour
24222 for the function, i.e. the contour in which the function's formal
24223 parameters and labels get declared. Curiously, it appears that the front
24224 end doesn't actually put the PARM_DECL nodes for the current function onto
24225 the BLOCK_VARS list for this outer scope, but are strung off of the
24226 DECL_ARGUMENTS list for the function instead.
24228 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
24229 the LABEL_DECL nodes for the function however, and we output DWARF info
24230 for those in decls_for_scope. Just within the `outer_scope' there will be
24231 a BLOCK node representing the function's outermost pair of curly braces,
24232 and any blocks used for the base and member initializers of a C++
24233 constructor function. */
24234 tree outer_scope = DECL_INITIAL (decl);
24235 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
24237 int call_site_note_count = 0;
24238 int tail_call_site_note_count = 0;
24240 /* Emit a DW_TAG_variable DIE for a named return value. */
24241 if (DECL_NAME (DECL_RESULT (decl)))
24242 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
24244 /* The first time through decls_for_scope we will generate the
24245 DIEs for the locals. The second time, we fill in the
24246 location info. */
24247 decls_for_scope (outer_scope, subr_die);
24249 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
24251 struct call_arg_loc_node *ca_loc;
24252 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
24254 dw_die_ref die = NULL;
24255 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
24256 rtx call_arg_loc_note
24257 = find_reg_note (ca_loc->call_insn,
24258 REG_CALL_ARG_LOCATION, NULL_RTX);
24259 rtx arg, next_arg;
24260 tree arg_decl = NULL_TREE;
24262 for (arg = (call_arg_loc_note != NULL_RTX
24263 ? XEXP (call_arg_loc_note, 0)
24264 : NULL_RTX);
24265 arg; arg = next_arg)
24267 dw_loc_descr_ref reg, val;
24268 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
24269 dw_die_ref cdie, tdie = NULL;
24271 next_arg = XEXP (arg, 1);
24272 if (REG_P (XEXP (XEXP (arg, 0), 0))
24273 && next_arg
24274 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
24275 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
24276 && REGNO (XEXP (XEXP (arg, 0), 0))
24277 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
24278 next_arg = XEXP (next_arg, 1);
24279 if (mode == VOIDmode)
24281 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
24282 if (mode == VOIDmode)
24283 mode = GET_MODE (XEXP (arg, 0));
24285 if (mode == VOIDmode || mode == BLKmode)
24286 continue;
24287 /* Get dynamic information about call target only if we
24288 have no static information: we cannot generate both
24289 DW_AT_call_origin and DW_AT_call_target
24290 attributes. */
24291 if (ca_loc->symbol_ref == NULL_RTX)
24293 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
24295 tloc = XEXP (XEXP (arg, 0), 1);
24296 continue;
24298 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
24299 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
24301 tlocc = XEXP (XEXP (arg, 0), 1);
24302 continue;
24305 reg = NULL;
24306 if (REG_P (XEXP (XEXP (arg, 0), 0)))
24307 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
24308 VAR_INIT_STATUS_INITIALIZED);
24309 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
24311 rtx mem = XEXP (XEXP (arg, 0), 0);
24312 reg = mem_loc_descriptor (XEXP (mem, 0),
24313 get_address_mode (mem),
24314 GET_MODE (mem),
24315 VAR_INIT_STATUS_INITIALIZED);
24317 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
24318 == DEBUG_PARAMETER_REF)
24320 tree tdecl
24321 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
24322 tdie = lookup_decl_die (tdecl);
24323 if (tdie == NULL)
24324 continue;
24325 arg_decl = tdecl;
24327 else
24328 continue;
24329 if (reg == NULL
24330 && GET_CODE (XEXP (XEXP (arg, 0), 0))
24331 != DEBUG_PARAMETER_REF)
24332 continue;
24333 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
24334 VOIDmode,
24335 VAR_INIT_STATUS_INITIALIZED);
24336 if (val == NULL)
24337 continue;
24338 if (die == NULL)
24339 die = gen_call_site_die (decl, subr_die, ca_loc);
24340 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
24341 NULL_TREE);
24342 add_desc_attribute (cdie, arg_decl);
24343 if (reg != NULL)
24344 add_AT_loc (cdie, DW_AT_location, reg);
24345 else if (tdie != NULL)
24346 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24347 tdie);
24348 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24349 if (next_arg != XEXP (arg, 1))
24351 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24352 if (mode == VOIDmode)
24353 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24354 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24355 0), 1),
24356 mode, VOIDmode,
24357 VAR_INIT_STATUS_INITIALIZED);
24358 if (val != NULL)
24359 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24360 val);
24363 if (die == NULL
24364 && (ca_loc->symbol_ref || tloc))
24365 die = gen_call_site_die (decl, subr_die, ca_loc);
24366 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24368 dw_loc_descr_ref tval = NULL;
24370 if (tloc != NULL_RTX)
24371 tval = mem_loc_descriptor (tloc,
24372 GET_MODE (tloc) == VOIDmode
24373 ? Pmode : GET_MODE (tloc),
24374 VOIDmode,
24375 VAR_INIT_STATUS_INITIALIZED);
24376 if (tval)
24377 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24378 else if (tlocc != NULL_RTX)
24380 tval = mem_loc_descriptor (tlocc,
24381 GET_MODE (tlocc) == VOIDmode
24382 ? Pmode : GET_MODE (tlocc),
24383 VOIDmode,
24384 VAR_INIT_STATUS_INITIALIZED);
24385 if (tval)
24386 add_AT_loc (die,
24387 dwarf_AT (DW_AT_call_target_clobbered),
24388 tval);
24391 if (die != NULL)
24393 call_site_note_count++;
24394 if (ca_loc->tail_call_p)
24395 tail_call_site_note_count++;
24399 call_arg_locations = NULL;
24400 call_arg_loc_last = NULL;
24401 if (tail_call_site_count >= 0
24402 && tail_call_site_count == tail_call_site_note_count
24403 && (!dwarf_strict || dwarf_version >= 5))
24405 if (call_site_count >= 0
24406 && call_site_count == call_site_note_count)
24407 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24408 else
24409 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24411 call_site_count = -1;
24412 tail_call_site_count = -1;
24415 /* Mark used types after we have created DIEs for the functions scopes. */
24416 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24419 /* Returns a hash value for X (which really is a die_struct). */
24421 hashval_t
24422 block_die_hasher::hash (die_struct *d)
24424 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24427 /* Return true if decl_id and die_parent of die_struct X is the same
24428 as decl_id and die_parent of die_struct Y. */
24430 bool
24431 block_die_hasher::equal (die_struct *x, die_struct *y)
24433 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24436 /* Hold information about markers for inlined entry points. */
24437 struct GTY ((for_user)) inline_entry_data
24439 /* The block that's the inlined_function_outer_scope for an inlined
24440 function. */
24441 tree block;
24443 /* The label at the inlined entry point. */
24444 const char *label_pfx;
24445 unsigned int label_num;
24447 /* The view number to be used as the inlined entry point. */
24448 var_loc_view view;
24451 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24453 typedef tree compare_type;
24454 static inline hashval_t hash (const inline_entry_data *);
24455 static inline bool equal (const inline_entry_data *, const_tree);
24458 /* Hash table routines for inline_entry_data. */
24460 inline hashval_t
24461 inline_entry_data_hasher::hash (const inline_entry_data *data)
24463 return htab_hash_pointer (data->block);
24466 inline bool
24467 inline_entry_data_hasher::equal (const inline_entry_data *data,
24468 const_tree block)
24470 return data->block == block;
24473 /* Inlined entry points pending DIE creation in this compilation unit. */
24475 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24478 /* Return TRUE if DECL, which may have been previously generated as
24479 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24480 true if decl (or its origin) is either an extern declaration or a
24481 class/namespace scoped declaration.
24483 The declare_in_namespace support causes us to get two DIEs for one
24484 variable, both of which are declarations. We want to avoid
24485 considering one to be a specification, so we must test for
24486 DECLARATION and DW_AT_declaration. */
24487 static inline bool
24488 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24490 return (old_die && TREE_STATIC (decl) && !declaration
24491 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24494 /* Return true if DECL is a local static. */
24496 static inline bool
24497 local_function_static (tree decl)
24499 gcc_assert (VAR_P (decl));
24500 return TREE_STATIC (decl)
24501 && DECL_CONTEXT (decl)
24502 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24505 /* Return true iff DECL overrides (presumably completes) the type of
24506 OLD_DIE within CONTEXT_DIE. */
24508 static bool
24509 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24510 dw_die_ref context_die)
24512 tree type = TREE_TYPE (decl);
24513 int cv_quals;
24515 if (decl_by_reference_p (decl))
24517 type = TREE_TYPE (type);
24518 cv_quals = TYPE_UNQUALIFIED;
24520 else
24521 cv_quals = decl_quals (decl);
24523 dw_die_ref type_die = modified_type_die (type,
24524 cv_quals | TYPE_QUALS (type),
24525 false,
24526 context_die);
24528 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24530 return type_die != old_type_die;
24533 /* Generate a DIE to represent a declared data object.
24534 Either DECL or ORIGIN must be non-null. */
24536 static void
24537 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24539 HOST_WIDE_INT off = 0;
24540 tree com_decl;
24541 tree decl_or_origin = decl ? decl : origin;
24542 tree ultimate_origin;
24543 dw_die_ref var_die;
24544 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24545 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24546 || class_or_namespace_scope_p (context_die));
24547 bool specialization_p = false;
24548 bool no_linkage_name = false;
24550 /* While C++ inline static data members have definitions inside of the
24551 class, force the first DIE to be a declaration, then let gen_member_die
24552 reparent it to the class context and call gen_variable_die again
24553 to create the outside of the class DIE for the definition. */
24554 if (!declaration
24555 && old_die == NULL
24556 && decl
24557 && DECL_CONTEXT (decl)
24558 && TYPE_P (DECL_CONTEXT (decl))
24559 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24561 declaration = true;
24562 if (dwarf_version < 5)
24563 no_linkage_name = true;
24566 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24567 if (decl || ultimate_origin)
24568 origin = ultimate_origin;
24569 com_decl = fortran_common (decl_or_origin, &off);
24571 /* Symbol in common gets emitted as a child of the common block, in the form
24572 of a data member. */
24573 if (com_decl)
24575 dw_die_ref com_die;
24576 dw_loc_list_ref loc = NULL;
24577 die_node com_die_arg;
24579 var_die = lookup_decl_die (decl_or_origin);
24580 if (var_die)
24582 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24584 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24585 if (loc)
24587 if (off)
24589 /* Optimize the common case. */
24590 if (single_element_loc_list_p (loc)
24591 && loc->expr->dw_loc_opc == DW_OP_addr
24592 && loc->expr->dw_loc_next == NULL
24593 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24594 == SYMBOL_REF)
24596 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24597 loc->expr->dw_loc_oprnd1.v.val_addr
24598 = plus_constant (GET_MODE (x), x , off);
24600 else
24601 loc_list_plus_const (loc, off);
24603 add_AT_location_description (var_die, DW_AT_location, loc);
24604 remove_AT (var_die, DW_AT_declaration);
24607 return;
24610 if (common_block_die_table == NULL)
24611 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24613 com_die_arg.decl_id = DECL_UID (com_decl);
24614 com_die_arg.die_parent = context_die;
24615 com_die = common_block_die_table->find (&com_die_arg);
24616 if (! early_dwarf)
24617 loc = loc_list_from_tree (com_decl, 2, NULL);
24618 if (com_die == NULL)
24620 const char *cnam
24621 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24622 die_node **slot;
24624 com_die = new_die (DW_TAG_common_block, context_die, decl);
24625 add_name_and_src_coords_attributes (com_die, com_decl);
24626 if (loc)
24628 add_AT_location_description (com_die, DW_AT_location, loc);
24629 /* Avoid sharing the same loc descriptor between
24630 DW_TAG_common_block and DW_TAG_variable. */
24631 loc = loc_list_from_tree (com_decl, 2, NULL);
24633 else if (DECL_EXTERNAL (decl_or_origin))
24634 add_AT_flag (com_die, DW_AT_declaration, 1);
24635 if (want_pubnames ())
24636 add_pubname_string (cnam, com_die); /* ??? needed? */
24637 com_die->decl_id = DECL_UID (com_decl);
24638 slot = common_block_die_table->find_slot (com_die, INSERT);
24639 *slot = com_die;
24641 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24643 add_AT_location_description (com_die, DW_AT_location, loc);
24644 loc = loc_list_from_tree (com_decl, 2, NULL);
24645 remove_AT (com_die, DW_AT_declaration);
24647 var_die = new_die (DW_TAG_variable, com_die, decl);
24648 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24649 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24650 decl_quals (decl_or_origin), false,
24651 context_die);
24652 add_alignment_attribute (var_die, decl);
24653 add_AT_flag (var_die, DW_AT_external, 1);
24654 if (loc)
24656 if (off)
24658 /* Optimize the common case. */
24659 if (single_element_loc_list_p (loc)
24660 && loc->expr->dw_loc_opc == DW_OP_addr
24661 && loc->expr->dw_loc_next == NULL
24662 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24664 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24665 loc->expr->dw_loc_oprnd1.v.val_addr
24666 = plus_constant (GET_MODE (x), x, off);
24668 else
24669 loc_list_plus_const (loc, off);
24671 add_AT_location_description (var_die, DW_AT_location, loc);
24673 else if (DECL_EXTERNAL (decl_or_origin))
24674 add_AT_flag (var_die, DW_AT_declaration, 1);
24675 if (decl)
24676 equate_decl_number_to_die (decl, var_die);
24677 return;
24680 if (old_die)
24682 if (declaration)
24684 /* A declaration that has been previously dumped, needs no
24685 further annotations, since it doesn't need location on
24686 the second pass. */
24687 return;
24689 else if (decl_will_get_specification_p (old_die, decl, declaration)
24690 && !get_AT (old_die, DW_AT_specification))
24692 /* Fall-thru so we can make a new variable die along with a
24693 DW_AT_specification. */
24695 else if (origin && old_die->die_parent != context_die)
24697 /* If we will be creating an inlined instance, we need a
24698 new DIE that will get annotated with
24699 DW_AT_abstract_origin. */
24700 gcc_assert (!DECL_ABSTRACT_P (decl));
24702 else
24704 /* If a DIE was dumped early, it still needs location info.
24705 Skip to where we fill the location bits. */
24706 var_die = old_die;
24708 /* ??? In LTRANS we cannot annotate early created variably
24709 modified type DIEs without copying them and adjusting all
24710 references to them. Thus we dumped them again. Also add a
24711 reference to them but beware of -g0 compile and -g link
24712 in which case the reference will be already present. */
24713 tree type = TREE_TYPE (decl_or_origin);
24714 if (in_lto_p
24715 && ! get_AT (var_die, DW_AT_type)
24716 && variably_modified_type_p
24717 (type, decl_function_context (decl_or_origin)))
24719 if (decl_by_reference_p (decl_or_origin))
24720 add_type_attribute (var_die, TREE_TYPE (type),
24721 TYPE_UNQUALIFIED, false, context_die);
24722 else
24723 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24724 false, context_die);
24727 goto gen_variable_die_location;
24731 /* For static data members, the declaration in the class is supposed
24732 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24733 also in DWARF2; the specification should still be DW_TAG_variable
24734 referencing the DW_TAG_member DIE. */
24735 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24736 var_die = new_die (DW_TAG_member, context_die, decl);
24737 else
24738 var_die = new_die (DW_TAG_variable, context_die, decl);
24740 if (origin != NULL)
24741 add_abstract_origin_attribute (var_die, origin);
24743 /* Loop unrolling can create multiple blocks that refer to the same
24744 static variable, so we must test for the DW_AT_declaration flag.
24746 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24747 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24748 sharing them.
24750 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24751 else if (decl_will_get_specification_p (old_die, decl, declaration))
24753 /* This is a definition of a C++ class level static. */
24754 add_AT_specification (var_die, old_die);
24755 specialization_p = true;
24756 if (DECL_NAME (decl))
24758 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24759 struct dwarf_file_data * file_index = lookup_filename (s.file);
24761 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24762 add_AT_file (var_die, DW_AT_decl_file, file_index);
24764 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24765 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24767 if (debug_column_info
24768 && s.column
24769 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24770 != (unsigned) s.column))
24771 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24773 if (old_die->die_tag == DW_TAG_member)
24774 add_linkage_name (var_die, decl);
24777 else
24778 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24780 if ((origin == NULL && !specialization_p)
24781 || (origin != NULL
24782 && !DECL_ABSTRACT_P (decl_or_origin)
24783 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24784 decl_function_context
24785 (decl_or_origin)))
24786 || (old_die && specialization_p
24787 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24789 tree type = TREE_TYPE (decl_or_origin);
24791 if (decl_by_reference_p (decl_or_origin))
24792 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24793 context_die);
24794 else
24795 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24796 context_die);
24799 if (origin == NULL && !specialization_p)
24801 if (TREE_PUBLIC (decl))
24802 add_AT_flag (var_die, DW_AT_external, 1);
24804 if (DECL_ARTIFICIAL (decl))
24805 add_AT_flag (var_die, DW_AT_artificial, 1);
24807 add_alignment_attribute (var_die, decl);
24809 add_accessibility_attribute (var_die, decl);
24812 if (declaration)
24813 add_AT_flag (var_die, DW_AT_declaration, 1);
24815 if (decl && (DECL_ABSTRACT_P (decl)
24816 || !old_die || is_declaration_die (old_die)))
24817 equate_decl_number_to_die (decl, var_die);
24819 gen_variable_die_location:
24820 if (! declaration
24821 && (! DECL_ABSTRACT_P (decl_or_origin)
24822 /* Local static vars are shared between all clones/inlines,
24823 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24824 already set. */
24825 || (VAR_P (decl_or_origin)
24826 && TREE_STATIC (decl_or_origin)
24827 && DECL_RTL_SET_P (decl_or_origin))))
24829 if (early_dwarf)
24831 add_pubname (decl_or_origin, var_die);
24832 /* For global register variables, emit DW_AT_location if possible
24833 already during early_dwarf, as late_global_decl won't be usually
24834 called. */
24835 if (DECL_HARD_REGISTER (decl_or_origin)
24836 && TREE_STATIC (decl_or_origin)
24837 && !decl_by_reference_p (decl_or_origin)
24838 && !get_AT (var_die, DW_AT_location)
24839 && !get_AT (var_die, DW_AT_const_value)
24840 && DECL_RTL_SET_P (decl_or_origin)
24841 && REG_P (DECL_RTL (decl_or_origin)))
24843 dw_loc_descr_ref descr
24844 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24845 VAR_INIT_STATUS_INITIALIZED);
24846 if (descr)
24847 add_AT_loc (var_die, DW_AT_location, descr);
24850 else
24851 add_location_or_const_value_attribute (var_die, decl_or_origin,
24852 decl == NULL);
24854 else
24855 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24857 if ((dwarf_version >= 4 || !dwarf_strict)
24858 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24859 DW_AT_const_expr) == 1
24860 && !get_AT (var_die, DW_AT_const_expr)
24861 && !specialization_p)
24862 add_AT_flag (var_die, DW_AT_const_expr, 1);
24864 if (!dwarf_strict)
24866 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24867 DW_AT_inline);
24868 if (inl != -1
24869 && !get_AT (var_die, DW_AT_inline)
24870 && !specialization_p)
24871 add_AT_unsigned (var_die, DW_AT_inline, inl);
24875 /* Generate a DIE to represent a named constant. */
24877 static void
24878 gen_const_die (tree decl, dw_die_ref context_die)
24880 dw_die_ref const_die;
24881 tree type = TREE_TYPE (decl);
24883 const_die = lookup_decl_die (decl);
24884 if (const_die)
24885 return;
24887 const_die = new_die (DW_TAG_constant, context_die, decl);
24888 equate_decl_number_to_die (decl, const_die);
24889 add_name_and_src_coords_attributes (const_die, decl);
24890 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24891 if (TREE_PUBLIC (decl))
24892 add_AT_flag (const_die, DW_AT_external, 1);
24893 if (DECL_ARTIFICIAL (decl))
24894 add_AT_flag (const_die, DW_AT_artificial, 1);
24895 tree_add_const_value_attribute_for_decl (const_die, decl);
24898 /* Generate a DIE to represent a label identifier. */
24900 static void
24901 gen_label_die (tree decl, dw_die_ref context_die)
24903 tree origin = decl_ultimate_origin (decl);
24904 dw_die_ref lbl_die = lookup_decl_die (decl);
24905 rtx insn;
24906 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24908 if (!lbl_die)
24910 lbl_die = new_die (DW_TAG_label, context_die, decl);
24911 equate_decl_number_to_die (decl, lbl_die);
24913 if (origin != NULL)
24914 add_abstract_origin_attribute (lbl_die, origin);
24915 else
24916 add_name_and_src_coords_attributes (lbl_die, decl);
24919 if (DECL_ABSTRACT_P (decl))
24920 equate_decl_number_to_die (decl, lbl_die);
24921 else if (! early_dwarf)
24923 insn = DECL_RTL_IF_SET (decl);
24925 /* Deleted labels are programmer specified labels which have been
24926 eliminated because of various optimizations. We still emit them
24927 here so that it is possible to put breakpoints on them. */
24928 if (insn
24929 && (LABEL_P (insn)
24930 || ((NOTE_P (insn)
24931 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24933 /* When optimization is enabled (via -O) some parts of the compiler
24934 (e.g. jump.cc and cse.cc) may try to delete CODE_LABEL insns which
24935 represent source-level labels which were explicitly declared by
24936 the user. This really shouldn't be happening though, so catch
24937 it if it ever does happen. */
24938 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24940 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24941 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24943 else if (insn
24944 && NOTE_P (insn)
24945 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24946 && CODE_LABEL_NUMBER (insn) != -1)
24948 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24949 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24954 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24955 attributes to the DIE for a block STMT, to describe where the inlined
24956 function was called from. This is similar to add_src_coords_attributes. */
24958 static inline void
24959 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24961 /* We can end up with BUILTINS_LOCATION here. */
24962 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24963 return;
24965 location_t locus = BLOCK_SOURCE_LOCATION (stmt);
24966 expanded_location s = expand_location (locus);
24968 if (dwarf_version >= 3 || !dwarf_strict)
24970 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24971 add_AT_unsigned (die, DW_AT_call_line, s.line);
24972 if (debug_column_info && s.column)
24973 add_AT_unsigned (die, DW_AT_call_column, s.column);
24974 unsigned discr = get_discriminator_from_loc (locus);
24975 if (discr != 0)
24976 add_AT_unsigned (die, DW_AT_GNU_discriminator, discr);
24981 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24982 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24984 static inline void
24985 add_high_low_attributes (tree stmt, dw_die_ref die)
24987 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24989 if (inline_entry_data **iedp
24990 = !inline_entry_data_table ? NULL
24991 : inline_entry_data_table->find_slot_with_hash (stmt,
24992 htab_hash_pointer (stmt),
24993 NO_INSERT))
24995 inline_entry_data *ied = *iedp;
24996 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24997 gcc_assert (debug_inline_points);
24998 gcc_assert (inlined_function_outer_scope_p (stmt));
25000 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
25001 add_AT_lbl_id (die, DW_AT_entry_pc, label);
25003 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
25004 && !dwarf_strict)
25006 if (!output_asm_line_debug_info ())
25007 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
25008 else
25010 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
25011 /* FIXME: this will resolve to a small number. Could we
25012 possibly emit smaller data? Ideally we'd emit a
25013 uleb128, but that would make the size of DIEs
25014 impossible for the compiler to compute, since it's
25015 the assembler that computes the value of the view
25016 label in this case. Ideally, we'd have a single form
25017 encompassing both the address and the view, and
25018 indirecting them through a table might make things
25019 easier, but even that would be more wasteful,
25020 space-wise, than what we have now. */
25021 add_AT_symview (die, DW_AT_GNU_entry_view, label);
25025 inline_entry_data_table->clear_slot (iedp);
25028 if (BLOCK_FRAGMENT_CHAIN (stmt)
25029 && (dwarf_version >= 3 || !dwarf_strict))
25031 tree chain, superblock = NULL_TREE;
25032 dw_die_ref pdie;
25033 dw_attr_node *attr = NULL;
25035 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
25037 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
25038 BLOCK_NUMBER (stmt));
25039 add_AT_lbl_id (die, DW_AT_entry_pc, label);
25042 /* Optimize duplicate .debug_ranges lists or even tails of
25043 lists. If this BLOCK has same ranges as its supercontext,
25044 lookup DW_AT_ranges attribute in the supercontext (and
25045 recursively so), verify that the ranges_table contains the
25046 right values and use it instead of adding a new .debug_range. */
25047 for (chain = stmt, pdie = die;
25048 BLOCK_SAME_RANGE (chain);
25049 chain = BLOCK_SUPERCONTEXT (chain))
25051 dw_attr_node *new_attr;
25053 pdie = pdie->die_parent;
25054 if (pdie == NULL)
25055 break;
25056 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
25057 break;
25058 new_attr = get_AT (pdie, DW_AT_ranges);
25059 if (new_attr == NULL
25060 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
25061 break;
25062 attr = new_attr;
25063 superblock = BLOCK_SUPERCONTEXT (chain);
25065 if (attr != NULL
25066 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
25067 == (int)BLOCK_NUMBER (superblock))
25068 && BLOCK_FRAGMENT_CHAIN (superblock))
25070 unsigned long off = attr->dw_attr_val.v.val_offset;
25071 unsigned long supercnt = 0, thiscnt = 0;
25072 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
25073 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
25075 ++supercnt;
25076 gcc_checking_assert ((*ranges_table)[off + supercnt].num
25077 == (int)BLOCK_NUMBER (chain));
25079 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
25080 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
25081 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
25082 ++thiscnt;
25083 gcc_assert (supercnt >= thiscnt);
25084 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
25085 false);
25086 note_rnglist_head (off + supercnt - thiscnt);
25087 return;
25090 unsigned int offset = add_ranges (stmt, true);
25091 add_AT_range_list (die, DW_AT_ranges, offset, false);
25092 note_rnglist_head (offset);
25094 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
25095 chain = BLOCK_FRAGMENT_CHAIN (stmt);
25098 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
25099 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
25100 chain = BLOCK_FRAGMENT_CHAIN (chain);
25102 while (chain);
25103 add_ranges (NULL);
25105 else
25107 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
25108 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
25109 BLOCK_NUMBER (stmt));
25110 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
25111 BLOCK_NUMBER (stmt));
25112 add_AT_low_high_pc (die, label, label_high, false);
25116 /* Generate a DIE for a lexical block. */
25118 static void
25119 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
25121 dw_die_ref old_die = lookup_block_die (stmt);
25122 dw_die_ref stmt_die = NULL;
25123 if (!old_die)
25125 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
25126 equate_block_to_die (stmt, stmt_die);
25129 if (BLOCK_ABSTRACT_ORIGIN (stmt))
25131 /* If this is an inlined or conrecte instance, create a new lexical
25132 die for anything below to attach DW_AT_abstract_origin to. */
25133 if (old_die)
25134 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
25136 tree origin = block_ultimate_origin (stmt);
25137 if (origin != NULL_TREE && (origin != stmt || old_die))
25138 add_abstract_origin_attribute (stmt_die, origin);
25140 old_die = NULL;
25143 if (old_die)
25144 stmt_die = old_die;
25146 /* A non abstract block whose blocks have already been reordered
25147 should have the instruction range for this block. If so, set the
25148 high/low attributes. */
25149 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
25151 gcc_assert (stmt_die);
25152 add_high_low_attributes (stmt, stmt_die);
25155 decls_for_scope (stmt, stmt_die);
25158 /* Generate a DIE for an inlined subprogram. */
25160 static void
25161 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
25163 tree decl = block_ultimate_origin (stmt);
25165 /* Make sure any inlined functions are known to be inlineable. */
25166 gcc_checking_assert (DECL_ABSTRACT_P (decl)
25167 || cgraph_function_possibly_inlined_p (decl));
25169 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
25171 if (call_arg_locations || debug_inline_points)
25172 equate_block_to_die (stmt, subr_die);
25173 add_abstract_origin_attribute (subr_die, decl);
25174 if (TREE_ASM_WRITTEN (stmt))
25175 add_high_low_attributes (stmt, subr_die);
25176 add_call_src_coords_attributes (stmt, subr_die);
25178 /* The inliner creates an extra BLOCK for the parameter setup,
25179 we want to merge that with the actual outermost BLOCK of the
25180 inlined function to avoid duplicate locals in consumers.
25181 Do that by doing the recursion to subblocks on the single subblock
25182 of STMT. */
25183 bool unwrap_one = false;
25184 tree sub = BLOCK_SUBBLOCKS (stmt);
25185 if (sub)
25187 tree origin = block_ultimate_origin (sub);
25188 if (origin
25189 && TREE_CODE (origin) == BLOCK
25190 && BLOCK_SUPERCONTEXT (origin) == decl)
25191 unwrap_one = true;
25192 for (tree next = BLOCK_CHAIN (sub); unwrap_one && next;
25193 next = BLOCK_CHAIN (next))
25194 if (BLOCK_FRAGMENT_ORIGIN (next) != sub)
25195 unwrap_one = false;
25197 decls_for_scope (stmt, subr_die, !unwrap_one);
25198 if (unwrap_one)
25200 decls_for_scope (sub, subr_die);
25201 for (sub = BLOCK_CHAIN (sub); sub; sub = BLOCK_CHAIN (sub))
25202 gen_block_die (sub, subr_die);
25206 /* Generate a DIE for a field in a record, or structure. CTX is required: see
25207 the comment for VLR_CONTEXT. */
25209 static void
25210 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
25212 dw_die_ref decl_die;
25214 if (TREE_TYPE (decl) == error_mark_node)
25215 return;
25217 decl_die = new_die (DW_TAG_member, context_die, decl);
25218 add_name_and_src_coords_attributes (decl_die, decl);
25219 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
25220 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
25221 context_die);
25223 if (DECL_BIT_FIELD_TYPE (decl))
25225 add_byte_size_attribute (decl_die, decl);
25226 add_bit_size_attribute (decl_die, decl);
25227 add_bit_offset_attribute (decl_die, decl);
25230 add_alignment_attribute (decl_die, decl);
25232 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
25233 add_data_member_location_attribute (decl_die, decl, ctx);
25235 if (DECL_ARTIFICIAL (decl))
25236 add_AT_flag (decl_die, DW_AT_artificial, 1);
25238 add_accessibility_attribute (decl_die, decl);
25240 /* Add DW_AT_export_symbols to anonymous unions or structs. */
25241 if ((dwarf_version >= 5 || !dwarf_strict) && DECL_NAME (decl) == NULL_TREE)
25242 if (tree type = member_declared_type (decl))
25243 if (lang_hooks.types.type_dwarf_attribute (TYPE_MAIN_VARIANT (type),
25244 DW_AT_export_symbols) != -1)
25246 dw_die_ref type_die = lookup_type_die (TYPE_MAIN_VARIANT (type));
25247 if (type_die && get_AT (type_die, DW_AT_export_symbols) == NULL)
25248 add_AT_flag (type_die, DW_AT_export_symbols, 1);
25251 /* Equate decl number to die, so that we can look up this decl later on. */
25252 equate_decl_number_to_die (decl, decl_die);
25255 /* Generate a DIE for a pointer to a member type. TYPE can be an
25256 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
25257 pointer to member function. */
25259 static void
25260 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
25262 if (lookup_type_die (type))
25263 return;
25265 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
25266 scope_die_for (type, context_die), type);
25268 equate_type_number_to_die (type, ptr_die);
25269 add_AT_die_ref (ptr_die, DW_AT_containing_type,
25270 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
25271 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
25272 context_die);
25273 add_alignment_attribute (ptr_die, type);
25275 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
25276 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
25278 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
25279 add_AT_loc (ptr_die, DW_AT_use_location, op);
25283 static char *producer_string;
25285 /* Given a C and/or C++ language/version string return the "highest".
25286 C++ is assumed to be "higher" than C in this case. Used for merging
25287 LTO translation unit languages. */
25288 static const char *
25289 highest_c_language (const char *lang1, const char *lang2)
25291 if (strcmp ("GNU C++26", lang1) == 0 || strcmp ("GNU C++26", lang2) == 0)
25292 return "GNU C++26";
25293 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
25294 return "GNU C++23";
25295 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
25296 return "GNU C++20";
25297 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
25298 return "GNU C++17";
25299 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
25300 return "GNU C++14";
25301 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
25302 return "GNU C++11";
25303 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
25304 return "GNU C++98";
25306 if (strcmp ("GNU C2Y", lang1) == 0 || strcmp ("GNU C2Y", lang2) == 0)
25307 return "GNU C2Y";
25308 if (strcmp ("GNU C23", lang1) == 0 || strcmp ("GNU C23", lang2) == 0)
25309 return "GNU C23";
25310 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
25311 return "GNU C17";
25312 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
25313 return "GNU C11";
25314 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
25315 return "GNU C99";
25316 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
25317 return "GNU C89";
25319 gcc_unreachable ();
25323 /* Generate the DIE for the compilation unit. */
25325 static dw_die_ref
25326 gen_compile_unit_die (const char *filename)
25328 dw_die_ref die;
25329 const char *language_string = lang_hooks.name;
25330 int language, lname, lversion;
25332 die = new_die (DW_TAG_compile_unit, NULL, NULL);
25334 if (filename)
25336 add_filename_attribute (die, filename);
25337 /* Don't add cwd for <built-in>. */
25338 if (filename[0] != '<')
25339 add_comp_dir_attribute (die);
25342 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
25344 /* If our producer is LTO try to figure out a common language to use
25345 from the global list of translation units. */
25346 if (strcmp (language_string, "GNU GIMPLE") == 0)
25348 unsigned i;
25349 tree t;
25350 const char *common_lang = NULL;
25352 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
25354 if (!TRANSLATION_UNIT_LANGUAGE (t))
25355 continue;
25356 if (!common_lang)
25357 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
25358 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
25360 else if (startswith (common_lang, "GNU C")
25361 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
25362 /* Mixing C and C++ is ok, use C++ in that case. */
25363 common_lang = highest_c_language (common_lang,
25364 TRANSLATION_UNIT_LANGUAGE (t));
25365 else
25367 /* Fall back to C. */
25368 common_lang = NULL;
25369 break;
25373 if (common_lang)
25374 language_string = common_lang;
25377 language = DW_LANG_C;
25378 lname = 0;
25379 lversion = 0;
25380 if (startswith (language_string, "GNU C")
25381 && ISDIGIT (language_string[5]))
25383 language = DW_LANG_C89;
25384 if (dwarf_version >= 3 || !dwarf_strict)
25386 if (strcmp (language_string, "GNU C89") != 0)
25387 language = DW_LANG_C99;
25389 if (dwarf_version >= 5 /* || !dwarf_strict */)
25391 if (strcmp (language_string, "GNU C11") == 0)
25392 language = DW_LANG_C11;
25393 else if (strcmp (language_string, "GNU C17") == 0)
25395 language = DW_LANG_C11;
25396 lname = DW_LNAME_C;
25397 lversion = 201710;
25399 else if (strcmp (language_string, "GNU C23") == 0)
25401 language = DW_LANG_C11;
25402 lname = DW_LNAME_C;
25403 lversion = 202311;
25405 else if (strcmp (language_string, "GNU C2Y") == 0)
25407 language = DW_LANG_C11;
25408 lname = DW_LNAME_C;
25409 lversion = 202500;
25414 else if (startswith (language_string, "GNU C++"))
25416 language = DW_LANG_C_plus_plus;
25417 if (dwarf_version >= 5 /* || !dwarf_strict */)
25419 if (strcmp (language_string, "GNU C++11") == 0)
25420 language = DW_LANG_C_plus_plus_11;
25421 else if (strcmp (language_string, "GNU C++14") == 0)
25422 language = DW_LANG_C_plus_plus_14;
25423 else if (strcmp (language_string, "GNU C++17") == 0)
25425 language = DW_LANG_C_plus_plus_14;
25426 lname = DW_LNAME_C_plus_plus;
25427 lversion = 201703;
25429 else if (strcmp (language_string, "GNU C++20") == 0)
25431 language = DW_LANG_C_plus_plus_14;
25432 lname = DW_LNAME_C_plus_plus;
25433 lversion = 202002;
25435 else if (strcmp (language_string, "GNU C++23") == 0)
25437 language = DW_LANG_C_plus_plus_14;
25438 lname = DW_LNAME_C_plus_plus;
25439 lversion = 202302;
25441 else if (strcmp (language_string, "GNU C++26") == 0)
25443 language = DW_LANG_C_plus_plus_14;
25444 lname = DW_LNAME_C_plus_plus;
25445 lversion = 202400;
25449 else if (strcmp (language_string, "GNU F77") == 0)
25450 language = DW_LANG_Fortran77;
25451 else if (strcmp (language_string, "GNU Modula-2") == 0)
25452 language = DW_LANG_Modula2;
25453 else if (dwarf_version >= 3 || !dwarf_strict)
25455 if (strcmp (language_string, "GNU Ada") == 0)
25456 language = DW_LANG_Ada95;
25457 else if (startswith (language_string, "GNU Fortran"))
25459 language = DW_LANG_Fortran95;
25460 if (dwarf_version >= 5 /* || !dwarf_strict */)
25462 if (strcmp (language_string, "GNU Fortran2003") == 0)
25463 language = DW_LANG_Fortran03;
25464 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25465 language = DW_LANG_Fortran08;
25468 else if (strcmp (language_string, "GNU Objective-C") == 0)
25469 language = DW_LANG_ObjC;
25470 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25471 language = DW_LANG_ObjC_plus_plus;
25472 else if (strcmp (language_string, "GNU D") == 0)
25473 language = DW_LANG_D;
25474 else if (dwarf_version >= 5 || !dwarf_strict)
25476 if (strcmp (language_string, "GNU Go") == 0)
25477 language = DW_LANG_Go;
25478 else if (strcmp (language_string, "GNU Rust") == 0)
25479 language = DW_LANG_Rust;
25482 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25483 else if (startswith (language_string, "GNU Fortran"))
25484 language = DW_LANG_Fortran90;
25485 /* Likewise for Ada. */
25486 else if (strcmp (language_string, "GNU Ada") == 0)
25487 language = DW_LANG_Ada83;
25489 add_AT_unsigned (die, DW_AT_language, language);
25490 if (lname && dwarf_version >= 5 && !dwarf_strict)
25492 add_AT_unsigned (die, DW_AT_language_name, lname);
25493 add_AT_unsigned (die, DW_AT_language_version, lversion);
25496 switch (language)
25498 case DW_LANG_Fortran77:
25499 case DW_LANG_Fortran90:
25500 case DW_LANG_Fortran95:
25501 case DW_LANG_Fortran03:
25502 case DW_LANG_Fortran08:
25503 /* Fortran has case insensitive identifiers and the front-end
25504 lowercases everything. */
25505 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25506 break;
25507 default:
25508 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25509 break;
25511 return die;
25514 /* Generate the DIE for a base class. */
25516 static void
25517 gen_inheritance_die (tree binfo, tree access, tree type,
25518 dw_die_ref context_die)
25520 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25521 struct vlr_context ctx = { type, NULL };
25523 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25524 context_die);
25525 add_data_member_location_attribute (die, binfo, &ctx);
25527 if (BINFO_VIRTUAL_P (binfo))
25528 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25530 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25531 children, otherwise the default is DW_ACCESS_public. In DWARF2
25532 the default has always been DW_ACCESS_private. */
25533 if (access == access_public_node)
25535 if (dwarf_version == 2
25536 || context_die->die_tag == DW_TAG_class_type)
25537 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25539 else if (access == access_protected_node)
25540 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25541 else if (dwarf_version > 2
25542 && context_die->die_tag != DW_TAG_class_type)
25543 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25546 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25547 structure. */
25549 static bool
25550 is_variant_part (tree decl)
25552 return (TREE_CODE (decl) == FIELD_DECL
25553 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25556 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25557 return the FIELD_DECL. Return NULL_TREE otherwise. */
25559 static tree
25560 analyze_discr_in_predicate (tree operand, tree struct_type)
25562 while (CONVERT_EXPR_P (operand))
25563 operand = TREE_OPERAND (operand, 0);
25565 /* Match field access to members of struct_type only. */
25566 if (TREE_CODE (operand) == COMPONENT_REF
25567 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25568 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25569 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25570 return TREE_OPERAND (operand, 1);
25571 else
25572 return NULL_TREE;
25575 /* Check that SRC is a constant integer that can be represented as a native
25576 integer constant (either signed or unsigned). If so, store it into DEST and
25577 return true. Return false otherwise. */
25579 static bool
25580 get_discr_value (tree src, dw_discr_value *dest)
25582 tree discr_type = TREE_TYPE (src);
25584 if (lang_hooks.types.get_debug_type)
25586 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25587 if (debug_type != NULL)
25588 discr_type = debug_type;
25591 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25592 return false;
25594 /* Signedness can vary between the original type and the debug type. This
25595 can happen for character types in Ada for instance: the character type
25596 used for code generation can be signed, to be compatible with the C one,
25597 but from a debugger point of view, it must be unsigned. */
25598 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25599 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25601 if (is_orig_unsigned != is_debug_unsigned)
25602 src = fold_convert (discr_type, src);
25604 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25605 return false;
25607 dest->pos = is_debug_unsigned;
25608 if (is_debug_unsigned)
25609 dest->v.uval = tree_to_uhwi (src);
25610 else
25611 dest->v.sval = tree_to_shwi (src);
25613 return true;
25616 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25617 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25618 store NULL_TREE in DISCR_DECL. Otherwise:
25620 - store the discriminant field in STRUCT_TYPE that controls the variant
25621 part to *DISCR_DECL
25623 - put in *DISCR_LISTS_P an array where for each variant, the item
25624 represents the corresponding matching list of discriminant values.
25626 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25627 the above array.
25629 Note that when the array is allocated (i.e. when the analysis is
25630 successful), it is up to the caller to free the array. */
25632 static void
25633 analyze_variants_discr (tree variant_part_decl,
25634 tree struct_type,
25635 tree *discr_decl,
25636 dw_discr_list_ref **discr_lists_p,
25637 unsigned *discr_lists_length)
25639 tree variant_part_type = TREE_TYPE (variant_part_decl);
25640 tree variant;
25641 dw_discr_list_ref *discr_lists;
25642 unsigned i;
25644 /* Compute how many variants there are in this variant part. */
25645 *discr_lists_length = 0;
25646 for (variant = TYPE_FIELDS (variant_part_type);
25647 variant != NULL_TREE;
25648 variant = DECL_CHAIN (variant))
25649 ++*discr_lists_length;
25651 *discr_decl = NULL_TREE;
25652 *discr_lists_p
25653 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25654 sizeof (**discr_lists_p));
25655 discr_lists = *discr_lists_p;
25657 /* And then analyze all variants to extract discriminant information for all
25658 of them. This analysis is conservative: as soon as we detect something we
25659 do not support, abort everything and pretend we found nothing. */
25660 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25661 variant != NULL_TREE;
25662 variant = DECL_CHAIN (variant), ++i)
25664 tree match_expr = DECL_QUALIFIER (variant);
25666 /* Now, try to analyze the predicate and deduce a discriminant for
25667 it. */
25668 if (match_expr == boolean_true_node)
25669 /* Typically happens for the default variant: it matches all cases that
25670 previous variants rejected. Don't output any matching value for
25671 this one. */
25672 continue;
25674 /* The following loop tries to iterate over each discriminant
25675 possibility: single values or ranges. */
25676 while (match_expr != NULL_TREE)
25678 tree next_round_match_expr;
25679 tree candidate_discr = NULL_TREE;
25680 dw_discr_list_ref new_node = NULL;
25682 /* Possibilities are matched one after the other by nested
25683 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25684 continue with the rest at next iteration. */
25685 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25687 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25688 match_expr = TREE_OPERAND (match_expr, 1);
25690 else
25691 next_round_match_expr = NULL_TREE;
25693 if (match_expr == boolean_false_node)
25694 /* This sub-expression matches nothing: just wait for the next
25695 one. */
25698 else if (TREE_CODE (match_expr) == EQ_EXPR)
25700 /* We are matching: <discr_field> == <integer_cst>
25701 This sub-expression matches a single value. */
25702 tree integer_cst = TREE_OPERAND (match_expr, 1);
25704 candidate_discr
25705 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25706 struct_type);
25708 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25709 if (!get_discr_value (integer_cst,
25710 &new_node->dw_discr_lower_bound))
25711 goto abort;
25712 new_node->dw_discr_range = false;
25715 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25717 /* We are matching:
25718 <discr_field> > <integer_cst>
25719 && <discr_field> < <integer_cst>.
25720 This sub-expression matches the range of values between the
25721 two matched integer constants. Note that comparisons can be
25722 inclusive or exclusive. */
25723 tree candidate_discr_1, candidate_discr_2;
25724 tree lower_cst, upper_cst;
25725 bool lower_cst_included, upper_cst_included;
25726 tree lower_op = TREE_OPERAND (match_expr, 0);
25727 tree upper_op = TREE_OPERAND (match_expr, 1);
25729 /* When the comparison is exclusive, the integer constant is not
25730 the discriminant range bound we are looking for: we will have
25731 to increment or decrement it. */
25732 if (TREE_CODE (lower_op) == GE_EXPR)
25733 lower_cst_included = true;
25734 else if (TREE_CODE (lower_op) == GT_EXPR)
25735 lower_cst_included = false;
25736 else
25737 goto abort;
25739 if (TREE_CODE (upper_op) == LE_EXPR)
25740 upper_cst_included = true;
25741 else if (TREE_CODE (upper_op) == LT_EXPR)
25742 upper_cst_included = false;
25743 else
25744 goto abort;
25746 /* Extract the discriminant from the first operand and check it
25747 is consistant with the same analysis in the second
25748 operand. */
25749 candidate_discr_1
25750 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25751 struct_type);
25752 candidate_discr_2
25753 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25754 struct_type);
25755 if (candidate_discr_1 == candidate_discr_2)
25756 candidate_discr = candidate_discr_1;
25757 else
25758 goto abort;
25760 /* Extract bounds from both. */
25761 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25762 lower_cst = TREE_OPERAND (lower_op, 1);
25763 upper_cst = TREE_OPERAND (upper_op, 1);
25765 if (!lower_cst_included)
25766 lower_cst
25767 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25768 build_int_cst (TREE_TYPE (lower_cst), 1));
25769 if (!upper_cst_included)
25770 upper_cst
25771 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25772 build_int_cst (TREE_TYPE (upper_cst), 1));
25774 if (!get_discr_value (lower_cst,
25775 &new_node->dw_discr_lower_bound)
25776 || !get_discr_value (upper_cst,
25777 &new_node->dw_discr_upper_bound))
25778 goto abort;
25780 new_node->dw_discr_range = true;
25783 else if ((candidate_discr
25784 = analyze_discr_in_predicate (match_expr, struct_type))
25785 && (TREE_TYPE (candidate_discr) == boolean_type_node
25786 || TREE_TYPE (TREE_TYPE (candidate_discr))
25787 == boolean_type_node))
25789 /* We are matching: <discr_field> for a boolean discriminant.
25790 This sub-expression matches boolean_true_node. */
25791 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25792 if (!get_discr_value (boolean_true_node,
25793 &new_node->dw_discr_lower_bound))
25794 goto abort;
25795 new_node->dw_discr_range = false;
25798 else
25799 /* Unsupported sub-expression: we cannot determine the set of
25800 matching discriminant values. Abort everything. */
25801 goto abort;
25803 /* If the discriminant info is not consistant with what we saw so
25804 far, consider the analysis failed and abort everything. */
25805 if (candidate_discr == NULL_TREE
25806 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25807 goto abort;
25808 else
25809 *discr_decl = candidate_discr;
25811 if (new_node != NULL)
25813 new_node->dw_discr_next = discr_lists[i];
25814 discr_lists[i] = new_node;
25816 match_expr = next_round_match_expr;
25820 /* If we reach this point, we could match everything we were interested
25821 in. */
25822 return;
25824 abort:
25825 /* Clean all data structure and return no result. */
25826 free (*discr_lists_p);
25827 *discr_lists_p = NULL;
25828 *discr_decl = NULL_TREE;
25831 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25832 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25833 under CONTEXT_DIE.
25835 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25836 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25837 this type, which are record types, represent the available variants and each
25838 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25839 values are inferred from these attributes.
25841 In trees, the offsets for the fields inside these sub-records are relative
25842 to the variant part itself, whereas the corresponding DIEs should have
25843 offset attributes that are relative to the embedding record base address.
25844 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25845 must be an expression that computes the offset of the variant part to
25846 describe in DWARF. */
25848 static void
25849 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25850 dw_die_ref context_die)
25852 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25853 tree variant_part_offset = vlr_ctx->variant_part_offset;
25855 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25856 NULL_TREE if there is no such field. */
25857 tree discr_decl = NULL_TREE;
25858 dw_discr_list_ref *discr_lists;
25859 unsigned discr_lists_length = 0;
25860 unsigned i;
25862 dw_die_ref dwarf_proc_die = NULL;
25863 dw_die_ref variant_part_die
25864 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25866 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25868 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25869 &discr_decl, &discr_lists, &discr_lists_length);
25871 if (discr_decl != NULL_TREE)
25873 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25875 if (discr_die)
25876 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25877 else
25878 /* We have no DIE for the discriminant, so just discard all
25879 discrimimant information in the output. */
25880 discr_decl = NULL_TREE;
25883 /* If the offset for this variant part is more complex than a constant,
25884 create a DWARF procedure for it so that we will not have to generate
25885 DWARF expressions for it for each member. */
25886 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25887 && (dwarf_version >= 3 || !dwarf_strict))
25889 struct loc_descr_context ctx = {
25890 vlr_ctx->struct_type, /* context_type */
25891 NULL_TREE, /* base_decl */
25892 NULL, /* dpi */
25893 false, /* placeholder_arg */
25894 false, /* placeholder_seen */
25895 false /* strict_signedness */
25897 const tree dwarf_proc_fndecl
25898 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25899 build_function_type (TREE_TYPE (variant_part_offset),
25900 NULL_TREE));
25901 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25902 const dw_loc_descr_ref dwarf_proc_body
25903 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25905 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25906 dwarf_proc_fndecl, context_die);
25907 if (dwarf_proc_die != NULL)
25908 variant_part_offset = dwarf_proc_call;
25911 /* Output DIEs for all variants. */
25912 i = 0;
25913 for (tree variant = TYPE_FIELDS (variant_part_type);
25914 variant != NULL_TREE;
25915 variant = DECL_CHAIN (variant), ++i)
25917 tree variant_type = TREE_TYPE (variant);
25918 dw_die_ref variant_die;
25920 /* All variants (i.e. members of a variant part) are supposed to be
25921 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25922 under these records. */
25923 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25925 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25926 equate_decl_number_to_die (variant, variant_die);
25928 /* Output discriminant values this variant matches, if any. */
25929 if (discr_decl == NULL || discr_lists[i] == NULL)
25930 /* In the case we have discriminant information at all, this is
25931 probably the default variant: as the standard says, don't
25932 output any discriminant value/list attribute. */
25934 else if (discr_lists[i]->dw_discr_next == NULL
25935 && !discr_lists[i]->dw_discr_range)
25936 /* If there is only one accepted value, don't bother outputting a
25937 list. */
25938 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25939 else
25940 add_discr_list (variant_die, discr_lists[i]);
25942 for (tree member = TYPE_FIELDS (variant_type);
25943 member != NULL_TREE;
25944 member = DECL_CHAIN (member))
25946 struct vlr_context vlr_sub_ctx = {
25947 vlr_ctx->struct_type, /* struct_type */
25948 NULL /* variant_part_offset */
25950 if (is_variant_part (member))
25952 /* All offsets for fields inside variant parts are relative to
25953 the top-level embedding RECORD_TYPE's base address. On the
25954 other hand, offsets in GCC's types are relative to the
25955 nested-most variant part. So we have to sum offsets each time
25956 we recurse. */
25958 vlr_sub_ctx.variant_part_offset
25959 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25960 variant_part_offset, byte_position (member));
25961 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25963 else
25965 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25966 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25971 free (discr_lists);
25974 /* Generate a DIE for a class member. */
25976 static void
25977 gen_member_die (tree type, dw_die_ref context_die)
25979 tree member;
25980 tree binfo = TYPE_BINFO (type);
25982 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25984 /* If this is not an incomplete type, output descriptions of each of its
25985 members. Note that as we output the DIEs necessary to represent the
25986 members of this record or union type, we will also be trying to output
25987 DIEs to represent the *types* of those members. However the `type'
25988 function (above) will specifically avoid generating type DIEs for member
25989 types *within* the list of member DIEs for this (containing) type except
25990 for those types (of members) which are explicitly marked as also being
25991 members of this (containing) type themselves. The g++ front- end can
25992 force any given type to be treated as a member of some other (containing)
25993 type by setting the TYPE_CONTEXT of the given (member) type to point to
25994 the TREE node representing the appropriate (containing) type. */
25996 /* First output info about the base classes. */
25997 if (binfo && early_dwarf)
25999 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
26000 int i;
26001 tree base;
26003 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
26004 gen_inheritance_die (base,
26005 (accesses ? (*accesses)[i] : access_public_node),
26006 type,
26007 context_die);
26010 /* Now output info about the members. */
26011 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
26013 /* Ignore clones. */
26014 if (DECL_ABSTRACT_ORIGIN (member))
26015 continue;
26017 struct vlr_context vlr_ctx = { type, NULL_TREE };
26018 bool static_inline_p
26019 = (VAR_P (member)
26020 && TREE_STATIC (member)
26021 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
26022 != -1));
26024 /* If we thought we were generating minimal debug info for TYPE
26025 and then changed our minds, some of the member declarations
26026 may have already been defined. Don't define them again, but
26027 do put them in the right order. */
26029 if (dw_die_ref child = lookup_decl_die (member))
26031 /* Handle inline static data members, which only have in-class
26032 declarations. */
26033 bool splice = true;
26035 dw_die_ref ref = NULL;
26036 if (child->die_tag == DW_TAG_variable
26037 && child->die_parent == comp_unit_die ())
26039 ref = get_AT_ref (child, DW_AT_specification);
26041 /* For C++17 inline static data members followed by redundant
26042 out of class redeclaration, we might get here with
26043 child being the DIE created for the out of class
26044 redeclaration and with its DW_AT_specification being
26045 the DIE created for in-class definition. We want to
26046 reparent the latter, and don't want to create another
26047 DIE with DW_AT_specification in that case, because
26048 we already have one. */
26049 if (ref
26050 && static_inline_p
26051 && ref->die_tag == DW_TAG_variable
26052 && ref->die_parent == comp_unit_die ()
26053 && get_AT (ref, DW_AT_specification) == NULL)
26055 child = ref;
26056 ref = NULL;
26057 static_inline_p = false;
26060 if (!ref)
26062 reparent_child (child, context_die);
26063 if (dwarf_version < 5)
26064 child->die_tag = DW_TAG_member;
26065 splice = false;
26068 else if (child->die_tag == DW_TAG_enumerator)
26069 /* Enumerators remain under their enumeration even if
26070 their names are introduced in the enclosing scope. */
26071 splice = false;
26073 if (splice)
26074 splice_child_die (context_die, child);
26077 /* Do not generate DWARF for variant parts if we are generating the
26078 corresponding GNAT encodings: DIEs generated for the two schemes
26079 would conflict in our mappings. */
26080 else if (is_variant_part (member)
26081 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
26083 vlr_ctx.variant_part_offset = byte_position (member);
26084 gen_variant_part (member, &vlr_ctx, context_die);
26086 else
26088 vlr_ctx.variant_part_offset = NULL_TREE;
26089 gen_decl_die (member, NULL, &vlr_ctx, context_die);
26092 /* For C++ inline static data members emit immediately a DW_TAG_variable
26093 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
26094 DW_AT_specification. */
26095 if (static_inline_p)
26097 int old_extern = DECL_EXTERNAL (member);
26098 DECL_EXTERNAL (member) = 0;
26099 gen_decl_die (member, NULL, NULL, comp_unit_die ());
26100 DECL_EXTERNAL (member) = old_extern;
26105 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
26106 is set, we pretend that the type was never defined, so we only get the
26107 member DIEs needed by later specification DIEs. */
26109 static void
26110 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
26111 enum debug_info_usage usage)
26113 if (TREE_ASM_WRITTEN (type))
26115 /* Fill in the bound of variable-length fields in late dwarf if
26116 still incomplete. */
26117 if (!early_dwarf && variably_modified_type_p (type, NULL))
26118 for (tree member = TYPE_FIELDS (type);
26119 member;
26120 member = DECL_CHAIN (member))
26121 fill_variable_array_bounds (TREE_TYPE (member));
26122 return;
26125 dw_die_ref type_die = lookup_type_die (type);
26126 dw_die_ref scope_die = 0;
26127 bool nested = false;
26128 bool complete = (TYPE_SIZE (type)
26129 && (! TYPE_STUB_DECL (type)
26130 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
26131 bool ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
26132 complete = complete && should_emit_struct_debug (type, usage);
26134 if (type_die && ! complete)
26135 return;
26137 if (TYPE_CONTEXT (type) != NULL_TREE
26138 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26139 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
26140 nested = true;
26142 scope_die = scope_die_for (type, context_die);
26144 /* Generate child dies for template parameters. */
26145 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
26146 schedule_generic_params_dies_gen (type);
26148 if (! type_die || (nested && is_cu_die (scope_die)))
26149 /* First occurrence of type or toplevel definition of nested class. */
26151 dw_die_ref old_die = type_die;
26153 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
26154 ? record_type_tag (type) : DW_TAG_union_type,
26155 scope_die, type);
26156 equate_type_number_to_die (type, type_die);
26157 if (old_die)
26158 add_AT_specification (type_die, old_die);
26159 else
26160 add_name_attribute (type_die, type_tag (type));
26162 else
26163 remove_AT (type_die, DW_AT_declaration);
26165 /* If this type has been completed, then give it a byte_size attribute and
26166 then give a list of members. */
26167 if (complete && !ns_decl)
26169 /* Prevent infinite recursion in cases where the type of some member of
26170 this type is expressed in terms of this type itself. */
26171 TREE_ASM_WRITTEN (type) = 1;
26172 add_byte_size_attribute (type_die, type);
26173 add_alignment_attribute (type_die, type);
26174 if (TYPE_STUB_DECL (type) != NULL_TREE)
26176 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
26177 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
26180 /* If the first reference to this type was as the return type of an
26181 inline function, then it may not have a parent. Fix this now. */
26182 if (type_die->die_parent == NULL)
26183 add_child_die (scope_die, type_die);
26185 gen_member_die (type, type_die);
26187 add_gnat_descriptive_type_attribute (type_die, type, context_die);
26188 if (TYPE_ARTIFICIAL (type))
26189 add_AT_flag (type_die, DW_AT_artificial, 1);
26191 /* GNU extension: Record what type our vtable lives in. */
26192 if (TYPE_VFIELD (type))
26194 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
26196 gen_type_die (vtype, context_die);
26197 add_AT_die_ref (type_die, DW_AT_containing_type,
26198 lookup_type_die (vtype));
26201 else
26203 add_AT_flag (type_die, DW_AT_declaration, 1);
26205 /* We don't need to do this for function-local types. */
26206 if (TYPE_STUB_DECL (type)
26207 && ! decl_function_context (TYPE_STUB_DECL (type)))
26208 vec_safe_push (incomplete_types, type);
26211 if (get_AT (type_die, DW_AT_name))
26212 add_pubtype (type, type_die);
26215 /* Generate a DIE for a subroutine _type_. */
26217 static void
26218 gen_subroutine_type_die (tree type, dw_die_ref context_die)
26220 tree return_type = TREE_TYPE (type);
26221 dw_die_ref subr_die
26222 = new_die (DW_TAG_subroutine_type,
26223 scope_die_for (type, context_die), type);
26225 equate_type_number_to_die (type, subr_die);
26226 add_prototyped_attribute (subr_die, type);
26227 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
26228 context_die);
26229 add_alignment_attribute (subr_die, type);
26230 gen_formal_types_die (type, subr_die);
26232 if (get_AT (subr_die, DW_AT_name))
26233 add_pubtype (type, subr_die);
26234 if ((dwarf_version >= 5 || !dwarf_strict)
26235 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
26236 add_AT_flag (subr_die, DW_AT_reference, 1);
26237 if ((dwarf_version >= 5 || !dwarf_strict)
26238 && lang_hooks.types.type_dwarf_attribute (type,
26239 DW_AT_rvalue_reference) != -1)
26240 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
26243 /* Generate a DIE for a type definition. */
26245 static void
26246 gen_typedef_die (tree decl, dw_die_ref context_die)
26248 dw_die_ref type_die;
26249 tree type;
26251 if (TREE_ASM_WRITTEN (decl))
26253 if (DECL_ORIGINAL_TYPE (decl))
26254 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
26255 return;
26258 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
26259 checks in process_scope_var and modified_type_die), this should be called
26260 only for original types. */
26261 gcc_assert (decl_ultimate_origin (decl) == NULL
26262 || decl_ultimate_origin (decl) == decl);
26264 TREE_ASM_WRITTEN (decl) = 1;
26265 type_die = new_die (DW_TAG_typedef, context_die, decl);
26267 add_name_and_src_coords_attributes (type_die, decl);
26268 if (DECL_ORIGINAL_TYPE (decl))
26270 type = DECL_ORIGINAL_TYPE (decl);
26271 if (type == error_mark_node)
26272 return;
26274 gcc_assert (type != TREE_TYPE (decl));
26275 equate_type_number_to_die (TREE_TYPE (decl), type_die);
26277 else
26279 type = TREE_TYPE (decl);
26280 if (type == error_mark_node)
26281 return;
26283 if (is_naming_typedef_decl (TYPE_NAME (type)))
26285 /* Here, we are in the case of decl being a typedef naming
26286 an anonymous type, e.g:
26287 typedef struct {...} foo;
26288 In that case TREE_TYPE (decl) is not a typedef variant
26289 type and TYPE_NAME of the anonymous type is set to the
26290 TYPE_DECL of the typedef. This construct is emitted by
26291 the C++ FE.
26293 TYPE is the anonymous struct named by the typedef
26294 DECL. As we need the DW_AT_type attribute of the
26295 DW_TAG_typedef to point to the DIE of TYPE, let's
26296 generate that DIE right away. add_type_attribute
26297 called below will then pick (via lookup_type_die) that
26298 anonymous struct DIE. */
26299 if (!TREE_ASM_WRITTEN (type))
26300 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
26302 /* This is a GNU Extension. We are adding a
26303 DW_AT_linkage_name attribute to the DIE of the
26304 anonymous struct TYPE. The value of that attribute
26305 is the name of the typedef decl naming the anonymous
26306 struct. This greatly eases the work of consumers of
26307 this debug info. */
26308 add_linkage_name_raw (lookup_type_die (type), decl);
26312 add_type_attribute (type_die, type, decl_quals (decl), false,
26313 context_die);
26315 if (is_naming_typedef_decl (decl))
26316 /* We want that all subsequent calls to lookup_type_die with
26317 TYPE in argument yield the DW_TAG_typedef we have just
26318 created. */
26319 equate_type_number_to_die (type, type_die);
26321 add_alignment_attribute (type_die, TREE_TYPE (decl));
26323 add_accessibility_attribute (type_die, decl);
26325 if (DECL_ABSTRACT_P (decl))
26326 equate_decl_number_to_die (decl, type_die);
26328 if (get_AT (type_die, DW_AT_name))
26329 add_pubtype (decl, type_die);
26332 /* Generate a DIE for a struct, class, enum or union type. */
26334 static void
26335 gen_tagged_type_die (tree type,
26336 dw_die_ref context_die,
26337 enum debug_info_usage usage,
26338 bool reverse)
26340 if (type == NULL_TREE
26341 || !is_tagged_type (type))
26342 return;
26344 if (TREE_ASM_WRITTEN (type))
26346 /* If this is a nested type whose containing class hasn't been written
26347 out yet, writing it out will cover this one, too. This does not apply
26348 to instantiations of member class templates; they need to be added to
26349 the containing class as they are generated. FIXME: This hurts the
26350 idea of combining type decls from multiple TUs, since we can't predict
26351 what set of template instantiations we'll get. */
26352 else if (TYPE_CONTEXT (type)
26353 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26354 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
26356 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
26358 if (TREE_ASM_WRITTEN (type))
26359 return;
26361 /* If that failed, attach ourselves to the stub. */
26362 context_die = lookup_type_die (TYPE_CONTEXT (type));
26364 else if (TYPE_CONTEXT (type) != NULL_TREE
26365 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
26367 /* If this type is local to a function that hasn't been written
26368 out yet, use a NULL context for now; it will be fixed up in
26369 decls_for_scope. */
26370 context_die = lookup_decl_die (TYPE_CONTEXT (type));
26371 /* A declaration DIE doesn't count; nested types need to go in the
26372 specification. */
26373 if (context_die && is_declaration_die (context_die))
26374 context_die = NULL;
26376 else
26377 context_die = declare_in_namespace (type, context_die);
26379 if (TREE_CODE (type) == ENUMERAL_TYPE)
26381 /* This might have been written out by the call to
26382 declare_in_namespace. */
26383 if (!TREE_ASM_WRITTEN (type) || reverse)
26384 gen_enumeration_type_die (type, context_die, reverse);
26386 else
26387 gen_struct_or_union_type_die (type, context_die, usage);
26389 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
26390 it up if it is ever completed. gen_*_type_die will set it for us
26391 when appropriate. */
26394 /* Generate a type description DIE. */
26396 static void
26397 gen_type_die_with_usage (tree type, dw_die_ref context_die,
26398 enum debug_info_usage usage, bool reverse)
26400 struct array_descr_info info;
26402 if (type == NULL_TREE || type == error_mark_node)
26403 return;
26405 if (flag_checking && type)
26406 verify_type (type);
26408 if (TYPE_NAME (type) != NULL_TREE
26409 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
26410 && is_redundant_typedef (TYPE_NAME (type))
26411 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
26412 /* The DECL of this type is a typedef we don't want to emit debug
26413 info for but we want debug info for its underlying typedef.
26414 This can happen for e.g, the injected-class-name of a C++
26415 type. */
26416 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
26418 /* If TYPE is a typedef type variant, let's generate debug info
26419 for the parent typedef which TYPE is a type of. */
26420 if (typedef_variant_p (type))
26422 if (TREE_ASM_WRITTEN (type))
26423 return;
26425 tree name = TYPE_NAME (type);
26426 tree origin = decl_ultimate_origin (name);
26427 if (origin != NULL && origin != name)
26429 gen_decl_die (origin, NULL, NULL, context_die);
26430 return;
26433 /* Prevent broken recursion; we can't hand off to the same type. */
26434 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26436 /* Give typedefs the right scope. */
26437 context_die = scope_die_for (type, context_die);
26439 TREE_ASM_WRITTEN (type) = 1;
26441 gen_decl_die (name, NULL, NULL, context_die);
26442 return;
26445 /* If type is an anonymous tagged type named by a typedef, let's
26446 generate debug info for the typedef. */
26447 if (is_naming_typedef_decl (TYPE_NAME (type)))
26449 /* Give typedefs the right scope. */
26450 context_die = scope_die_for (type, context_die);
26452 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26453 return;
26456 if (lang_hooks.types.get_debug_type)
26458 tree debug_type = lang_hooks.types.get_debug_type (type);
26460 if (debug_type != NULL_TREE && debug_type != type)
26462 gen_type_die_with_usage (debug_type, context_die, usage, reverse);
26463 return;
26467 /* We are going to output a DIE to represent the unqualified version
26468 of this type (i.e. without any const or volatile qualifiers) so
26469 get the main variant (i.e. the unqualified version) of this type
26470 now. (Vectors and arrays are special because the debugging info is in the
26471 cloned type itself. Similarly function/method types can contain extra
26472 ref-qualification). */
26473 if (FUNC_OR_METHOD_TYPE_P (type))
26475 /* For function/method types, can't use type_main_variant here,
26476 because that can have different ref-qualifiers for C++,
26477 but try to canonicalize. */
26478 tree main = TYPE_MAIN_VARIANT (type);
26479 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26480 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26481 && check_base_type (t, main)
26482 && check_lang_type (t, type))
26484 type = t;
26485 break;
26488 else if (TREE_CODE (type) != VECTOR_TYPE
26489 && TREE_CODE (type) != ARRAY_TYPE)
26490 type = type_main_variant (type);
26492 /* If this is an array type with hidden descriptor, handle it first. */
26493 if (!TREE_ASM_WRITTEN (type)
26494 && lang_hooks.types.get_array_descr_info)
26496 memset (&info, 0, sizeof (info));
26497 if (lang_hooks.types.get_array_descr_info (type, &info))
26499 /* Fortran sometimes emits array types with no dimension. */
26500 gcc_assert (info.ndimensions >= 0
26501 && (info.ndimensions
26502 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26503 gen_descr_array_type_die (type, &info, context_die);
26504 TREE_ASM_WRITTEN (type) = 1;
26505 return;
26509 if (TREE_ASM_WRITTEN (type) && !reverse)
26511 /* Variable-length types may be incomplete even if
26512 TREE_ASM_WRITTEN. For such types, fall through to
26513 gen_array_type_die() and possibly fill in
26514 DW_AT_{upper,lower}_bound attributes. */
26515 if ((TREE_CODE (type) != ARRAY_TYPE
26516 && TREE_CODE (type) != RECORD_TYPE
26517 && TREE_CODE (type) != UNION_TYPE
26518 && TREE_CODE (type) != QUAL_UNION_TYPE)
26519 || !variably_modified_type_p (type, NULL))
26520 return;
26523 switch (TREE_CODE (type))
26525 case ERROR_MARK:
26526 break;
26528 case POINTER_TYPE:
26529 case REFERENCE_TYPE:
26530 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26531 ensures that the gen_type_die recursion will terminate even if the
26532 type is recursive. Recursive types are possible in Ada. */
26533 /* ??? We could perhaps do this for all types before the switch
26534 statement. */
26535 TREE_ASM_WRITTEN (type) = 1;
26537 /* For these types, all that is required is that we output a DIE (or a
26538 set of DIEs) to represent the "basis" type. */
26539 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26540 DINFO_USAGE_IND_USE);
26541 break;
26543 case OFFSET_TYPE:
26544 /* This code is used for C++ pointer-to-data-member types.
26545 Output a description of the relevant class type. */
26546 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26547 DINFO_USAGE_IND_USE);
26549 /* Output a description of the type of the object pointed to. */
26550 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26551 DINFO_USAGE_IND_USE);
26553 /* Now output a DIE to represent this pointer-to-data-member type
26554 itself. */
26555 gen_ptr_to_mbr_type_die (type, context_die);
26556 break;
26558 case FUNCTION_TYPE:
26559 /* Force out return type (in case it wasn't forced out already). */
26560 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26561 DINFO_USAGE_DIR_USE);
26562 gen_subroutine_type_die (type, context_die);
26563 break;
26565 case METHOD_TYPE:
26566 /* Force out return type (in case it wasn't forced out already). */
26567 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26568 DINFO_USAGE_DIR_USE);
26569 gen_subroutine_type_die (type, context_die);
26570 break;
26572 case ARRAY_TYPE:
26573 case VECTOR_TYPE:
26574 gen_array_type_die (type, context_die);
26575 break;
26577 case ENUMERAL_TYPE:
26578 case RECORD_TYPE:
26579 case UNION_TYPE:
26580 case QUAL_UNION_TYPE:
26581 gen_tagged_type_die (type, context_die, usage, reverse);
26582 return;
26584 case VOID_TYPE:
26585 case OPAQUE_TYPE:
26586 case INTEGER_TYPE:
26587 case REAL_TYPE:
26588 case FIXED_POINT_TYPE:
26589 case COMPLEX_TYPE:
26590 case BOOLEAN_TYPE:
26591 case BITINT_TYPE:
26592 /* No DIEs needed for fundamental types. */
26593 break;
26595 case NULLPTR_TYPE:
26596 case LANG_TYPE:
26597 /* Just use DW_TAG_unspecified_type. */
26599 dw_die_ref type_die = lookup_type_die (type);
26600 if (type_die == NULL)
26602 tree name = TYPE_IDENTIFIER (type);
26603 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26604 type);
26605 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26606 equate_type_number_to_die (type, type_die);
26609 break;
26611 default:
26612 if (is_cxx_auto (type))
26614 tree name = TYPE_IDENTIFIER (type);
26615 dw_die_ref *die = (name == get_identifier ("auto")
26616 ? &auto_die : &decltype_auto_die);
26617 if (!*die)
26619 *die = new_die (DW_TAG_unspecified_type,
26620 comp_unit_die (), NULL_TREE);
26621 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26623 equate_type_number_to_die (type, *die);
26624 break;
26626 gcc_unreachable ();
26629 TREE_ASM_WRITTEN (type) = 1;
26632 static void
26633 gen_type_die (tree type, dw_die_ref context_die, bool reverse)
26635 if (type != error_mark_node)
26637 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE, reverse);
26638 if (flag_checking)
26640 dw_die_ref die = lookup_type_die (type);
26641 if (die)
26642 check_die (die);
26647 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26648 things which are local to the given block. */
26650 static void
26651 gen_block_die (tree stmt, dw_die_ref context_die)
26653 int must_output_die = 0;
26654 bool inlined_func;
26656 /* Ignore blocks that are NULL. */
26657 if (stmt == NULL_TREE)
26658 return;
26660 inlined_func = inlined_function_outer_scope_p (stmt);
26662 /* If the block is one fragment of a non-contiguous block, do not
26663 process the variables, since they will have been done by the
26664 origin block. Do process subblocks. */
26665 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26667 tree sub;
26669 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26670 gen_block_die (sub, context_die);
26672 return;
26675 /* Determine if we need to output any Dwarf DIEs at all to represent this
26676 block. */
26677 if (inlined_func)
26678 /* The outer scopes for inlinings *must* always be represented. We
26679 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26680 must_output_die = 1;
26681 else if (lookup_block_die (stmt))
26682 /* If we already have a DIE then it was filled early. Meanwhile
26683 we might have pruned all BLOCK_VARS as optimized out but we
26684 still want to generate high/low PC attributes so output it. */
26685 must_output_die = 1;
26686 else if (TREE_USED (stmt)
26687 || TREE_ASM_WRITTEN (stmt))
26689 /* Determine if this block directly contains any "significant"
26690 local declarations which we will need to output DIEs for. */
26691 if (debug_info_level > DINFO_LEVEL_TERSE)
26693 /* We are not in terse mode so any local declaration that
26694 is not ignored for debug purposes counts as being a
26695 "significant" one. */
26696 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26697 must_output_die = 1;
26698 else
26699 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26700 if (!DECL_IGNORED_P (var))
26702 must_output_die = 1;
26703 break;
26706 else if (!dwarf2out_ignore_block (stmt))
26707 must_output_die = 1;
26710 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26711 DIE for any block which contains no significant local declarations at
26712 all. Rather, in such cases we just call `decls_for_scope' so that any
26713 needed Dwarf info for any sub-blocks will get properly generated. Note
26714 that in terse mode, our definition of what constitutes a "significant"
26715 local declaration gets restricted to include only inlined function
26716 instances and local (nested) function definitions. */
26717 if (must_output_die)
26719 if (inlined_func)
26720 gen_inlined_subroutine_die (stmt, context_die);
26721 else
26722 gen_lexical_block_die (stmt, context_die);
26724 else
26725 decls_for_scope (stmt, context_die);
26728 /* Process variable DECL (or variable with origin ORIGIN) within
26729 block STMT and add it to CONTEXT_DIE. */
26730 static void
26731 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26733 dw_die_ref die;
26734 tree decl_or_origin = decl ? decl : origin;
26736 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26737 die = lookup_decl_die (decl_or_origin);
26738 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26740 if (TYPE_DECL_IS_STUB (decl_or_origin))
26741 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26742 else
26743 die = lookup_decl_die (decl_or_origin);
26744 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26745 if (! die && ! early_dwarf)
26746 return;
26748 else
26749 die = NULL;
26751 /* Avoid creating DIEs for local typedefs and concrete static variables that
26752 will only be pruned later. */
26753 if ((origin || decl_ultimate_origin (decl))
26754 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26755 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26757 origin = decl_ultimate_origin (decl_or_origin);
26758 if (decl && VAR_P (decl) && die != NULL)
26760 die = lookup_decl_die (origin);
26761 if (die != NULL)
26762 equate_decl_number_to_die (decl, die);
26764 return;
26767 if (die != NULL && die->die_parent == NULL)
26768 add_child_die (context_die, die);
26770 if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26772 if (early_dwarf)
26773 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26774 stmt, context_die);
26776 else
26778 if (decl && DECL_P (decl))
26780 die = lookup_decl_die (decl);
26782 /* Early created DIEs do not have a parent as the decls refer
26783 to the function as DECL_CONTEXT rather than the BLOCK. */
26784 if (die && die->die_parent == NULL)
26786 gcc_assert (in_lto_p);
26787 add_child_die (context_die, die);
26791 gen_decl_die (decl, origin, NULL, context_die);
26795 /* Generate all of the decls declared within a given scope and (recursively)
26796 all of its sub-blocks. */
26798 static void
26799 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26801 tree decl;
26802 unsigned int i;
26803 tree subblocks;
26805 /* Ignore NULL blocks. */
26806 if (stmt == NULL_TREE)
26807 return;
26809 /* Output the DIEs to represent all of the data objects and typedefs
26810 declared directly within this block but not within any nested
26811 sub-blocks. Also, nested function and tag DIEs have been
26812 generated with a parent of NULL; fix that up now. We don't
26813 have to do this if we're at -g1. */
26814 if (debug_info_level > DINFO_LEVEL_TERSE)
26816 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26817 process_scope_var (stmt, decl, NULL_TREE, context_die);
26818 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26819 origin - avoid doing this twice as we have no good way to see
26820 if we've done it once already. */
26821 if (! early_dwarf)
26822 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26824 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26825 if (decl == current_function_decl)
26826 /* Ignore declarations of the current function, while they
26827 are declarations, gen_subprogram_die would treat them
26828 as definitions again, because they are equal to
26829 current_function_decl and endlessly recurse. */;
26830 else if (TREE_CODE (decl) == FUNCTION_DECL)
26831 process_scope_var (stmt, decl, NULL_TREE, context_die);
26832 else
26833 process_scope_var (stmt, NULL_TREE, decl, context_die);
26837 /* Even if we're at -g1, we need to process the subblocks in order to get
26838 inlined call information. */
26840 /* Output the DIEs to represent all sub-blocks (and the items declared
26841 therein) of this block. */
26842 if (recurse)
26843 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26844 subblocks != NULL;
26845 subblocks = BLOCK_CHAIN (subblocks))
26846 gen_block_die (subblocks, context_die);
26849 /* Is this a typedef we can avoid emitting? */
26851 static bool
26852 is_redundant_typedef (const_tree decl)
26854 if (TYPE_DECL_IS_STUB (decl))
26855 return true;
26857 if (DECL_ARTIFICIAL (decl)
26858 && DECL_CONTEXT (decl)
26859 && is_tagged_type (DECL_CONTEXT (decl))
26860 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26861 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26862 /* Also ignore the artificial member typedef for the class name. */
26863 return true;
26865 return false;
26868 /* Return TRUE if TYPE is a typedef that names a type for linkage
26869 purposes. This kind of typedefs is produced by the C++ FE for
26870 constructs like:
26872 typedef struct {...} foo;
26874 In that case, there is no typedef variant type produced for foo.
26875 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26876 struct type. */
26878 static bool
26879 is_naming_typedef_decl (const_tree decl)
26881 if (decl == NULL_TREE
26882 || TREE_CODE (decl) != TYPE_DECL
26883 || DECL_NAMELESS (decl)
26884 || !is_tagged_type (TREE_TYPE (decl))
26885 || DECL_IS_UNDECLARED_BUILTIN (decl)
26886 || is_redundant_typedef (decl)
26887 /* It looks like Ada produces TYPE_DECLs that are very similar
26888 to C++ naming typedefs but that have different
26889 semantics. Let's be specific to c++ for now. */
26890 || !is_cxx (decl))
26891 return false;
26893 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26894 && TYPE_NAME (TREE_TYPE (decl)) == decl
26895 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26896 != TYPE_NAME (TREE_TYPE (decl))));
26899 /* Looks up the DIE for a context. */
26901 static inline dw_die_ref
26902 lookup_context_die (tree context)
26904 if (context)
26906 /* Find die that represents this context. */
26907 if (TYPE_P (context))
26909 context = TYPE_MAIN_VARIANT (context);
26910 dw_die_ref ctx = lookup_type_die (context);
26911 if (!ctx)
26912 return NULL;
26913 return strip_naming_typedef (context, ctx);
26915 else
26916 return lookup_decl_die (context);
26918 return comp_unit_die ();
26921 /* Returns the DIE for a context. */
26923 static inline dw_die_ref
26924 get_context_die (tree context)
26926 if (context)
26928 /* Find die that represents this context. */
26929 if (TYPE_P (context))
26931 context = TYPE_MAIN_VARIANT (context);
26932 return strip_naming_typedef (context, force_type_die (context));
26934 else
26935 return force_decl_die (context);
26937 return comp_unit_die ();
26940 /* Returns the DIE for decl. A DIE will always be returned. */
26942 static dw_die_ref
26943 force_decl_die (tree decl)
26945 dw_die_ref decl_die;
26946 unsigned saved_external_flag;
26947 tree save_fn = NULL_TREE;
26948 decl_die = lookup_decl_die (decl);
26949 if (!decl_die)
26951 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26953 decl_die = lookup_decl_die (decl);
26954 if (decl_die)
26955 return decl_die;
26957 switch (TREE_CODE (decl))
26959 case FUNCTION_DECL:
26960 /* Clear current_function_decl, so that gen_subprogram_die thinks
26961 that this is a declaration. At this point, we just want to force
26962 declaration die. */
26963 save_fn = current_function_decl;
26964 current_function_decl = NULL_TREE;
26965 gen_subprogram_die (decl, context_die);
26966 current_function_decl = save_fn;
26967 break;
26969 case VAR_DECL:
26970 /* Set external flag to force declaration die. Restore it after
26971 gen_decl_die() call. */
26972 saved_external_flag = DECL_EXTERNAL (decl);
26973 DECL_EXTERNAL (decl) = 1;
26974 gen_decl_die (decl, NULL, NULL, context_die);
26975 DECL_EXTERNAL (decl) = saved_external_flag;
26976 break;
26978 case NAMESPACE_DECL:
26979 if (dwarf_version >= 3 || !dwarf_strict)
26980 dwarf2out_decl (decl);
26981 else
26982 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26983 decl_die = comp_unit_die ();
26984 break;
26986 case CONST_DECL:
26987 /* Enumerators shouldn't need force_decl_die. */
26988 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26989 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26990 gen_decl_die (decl, NULL, NULL, context_die);
26991 break;
26993 case TRANSLATION_UNIT_DECL:
26994 decl_die = comp_unit_die ();
26995 break;
26997 default:
26998 gcc_unreachable ();
27001 /* We should be able to find the DIE now. */
27002 if (!decl_die)
27003 decl_die = lookup_decl_die (decl);
27004 gcc_assert (decl_die);
27007 return decl_die;
27010 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
27011 always returned. */
27013 static dw_die_ref
27014 force_type_die (tree type)
27016 dw_die_ref type_die;
27018 type_die = lookup_type_die (type);
27019 if (!type_die)
27021 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
27023 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
27024 false, context_die);
27025 gcc_assert (type_die);
27027 return type_die;
27030 /* Force out any required namespaces to be able to output DECL,
27031 and return the new context_die for it, if it's changed. */
27033 static dw_die_ref
27034 setup_namespace_context (tree thing, dw_die_ref context_die)
27036 tree context = (DECL_P (thing)
27037 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
27038 if (context && TREE_CODE (context) == NAMESPACE_DECL)
27039 /* Force out the namespace. */
27040 context_die = force_decl_die (context);
27042 return context_die;
27045 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
27046 type) within its namespace, if appropriate.
27048 For compatibility with older debuggers, namespace DIEs only contain
27049 declarations; all definitions are emitted at CU scope, with
27050 DW_AT_specification pointing to the declaration (like with class
27051 members). */
27053 static dw_die_ref
27054 declare_in_namespace (tree thing, dw_die_ref context_die)
27056 dw_die_ref ns_context;
27058 if (debug_info_level <= DINFO_LEVEL_TERSE)
27059 return context_die;
27061 /* External declarations in the local scope only need to be emitted
27062 once, not once in the namespace and once in the scope.
27064 This avoids declaring the `extern' below in the
27065 namespace DIE as well as in the innermost scope:
27067 namespace S
27069 int i=5;
27070 int foo()
27072 int i=8;
27073 extern int i;
27074 return i;
27078 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
27079 return context_die;
27081 /* If this decl is from an inlined function, then don't try to emit it in its
27082 namespace, as we will get confused. It would have already been emitted
27083 when the abstract instance of the inline function was emitted anyways. */
27084 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
27085 return context_die;
27087 ns_context = setup_namespace_context (thing, context_die);
27089 if (ns_context != context_die)
27091 if (is_fortran () || is_dlang ())
27092 return ns_context;
27093 if (DECL_P (thing))
27094 gen_decl_die (thing, NULL, NULL, ns_context);
27095 else
27096 gen_type_die (thing, ns_context);
27098 return context_die;
27101 /* Generate a DIE for a namespace or namespace alias. */
27103 static void
27104 gen_namespace_die (tree decl, dw_die_ref context_die)
27106 dw_die_ref namespace_die;
27108 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
27109 they are an alias of. */
27110 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
27112 /* Output a real namespace or module. */
27113 context_die = setup_namespace_context (decl, comp_unit_die ());
27114 namespace_die = new_die (is_fortran () || is_dlang () || is_ada ()
27115 ? DW_TAG_module : DW_TAG_namespace,
27116 context_die, decl);
27117 /* For Fortran modules defined in different CU don't add src coords. */
27118 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
27120 const char *name = dwarf2_name (decl, 0);
27121 if (name)
27122 add_name_attribute (namespace_die, name);
27124 else
27125 add_name_and_src_coords_attributes (namespace_die, decl);
27126 if (DECL_EXTERNAL (decl))
27127 add_AT_flag (namespace_die, DW_AT_declaration, 1);
27128 equate_decl_number_to_die (decl, namespace_die);
27130 else
27132 /* Output a namespace alias. */
27134 /* Force out the namespace we are an alias of, if necessary. */
27135 dw_die_ref origin_die
27136 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
27138 if (DECL_FILE_SCOPE_P (decl)
27139 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
27140 context_die = setup_namespace_context (decl, comp_unit_die ());
27141 /* Now create the namespace alias DIE. */
27142 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
27143 add_name_and_src_coords_attributes (namespace_die, decl);
27144 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
27145 equate_decl_number_to_die (decl, namespace_die);
27147 if ((dwarf_version >= 5 || !dwarf_strict)
27148 && lang_hooks.decls.decl_dwarf_attribute (decl,
27149 DW_AT_export_symbols) == 1)
27150 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
27152 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
27153 if (want_pubnames ())
27154 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
27157 /* Generate Dwarf debug information for a decl described by DECL.
27158 The return value is currently only meaningful for PARM_DECLs,
27159 for all other decls it returns NULL.
27161 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
27162 It can be NULL otherwise. */
27164 static dw_die_ref
27165 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
27166 dw_die_ref context_die)
27168 tree decl_or_origin = decl ? decl : origin;
27169 tree class_origin = NULL, ultimate_origin;
27171 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
27172 return NULL;
27174 switch (TREE_CODE (decl_or_origin))
27176 case ERROR_MARK:
27177 break;
27179 case CONST_DECL:
27180 if (!is_fortran () && !is_ada () && !is_dlang ())
27182 /* The individual enumerators of an enum type get output when we output
27183 the Dwarf representation of the relevant enum type itself. */
27184 break;
27187 /* Emit its type. */
27188 gen_type_die (TREE_TYPE (decl), context_die);
27190 /* And its containing namespace. */
27191 context_die = declare_in_namespace (decl, context_die);
27193 gen_const_die (decl, context_die);
27194 break;
27196 case FUNCTION_DECL:
27197 #if 0
27198 /* FIXME */
27199 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
27200 on local redeclarations of global functions. That seems broken. */
27201 if (current_function_decl != decl)
27202 /* This is only a declaration. */;
27203 #endif
27205 /* We should have abstract copies already and should not generate
27206 stray type DIEs in late LTO dumping. */
27207 if (! early_dwarf)
27210 /* If we're emitting a clone, emit info for the abstract instance. */
27211 else if (origin || DECL_ORIGIN (decl) != decl)
27212 dwarf2out_abstract_function (origin
27213 ? DECL_ORIGIN (origin)
27214 : DECL_ABSTRACT_ORIGIN (decl));
27216 /* If we're emitting a possibly inlined function emit it as
27217 abstract instance. */
27218 else if (cgraph_function_possibly_inlined_p (decl)
27219 && ! DECL_ABSTRACT_P (decl)
27220 && ! class_or_namespace_scope_p (context_die)
27221 /* dwarf2out_abstract_function won't emit a die if this is just
27222 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
27223 that case, because that works only if we have a die. */
27224 && DECL_INITIAL (decl) != NULL_TREE)
27225 dwarf2out_abstract_function (decl);
27227 /* Otherwise we're emitting the primary DIE for this decl. */
27228 else if (debug_info_level > DINFO_LEVEL_TERSE)
27230 /* Before we describe the FUNCTION_DECL itself, make sure that we
27231 have its containing type. */
27232 if (!origin)
27233 origin = decl_class_context (decl);
27234 if (origin != NULL_TREE)
27235 gen_type_die (origin, context_die);
27237 /* And its return type. */
27238 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
27240 /* And its virtual context. */
27241 if (DECL_VINDEX (decl) != NULL_TREE)
27242 gen_type_die (DECL_CONTEXT (decl), context_die);
27244 /* Make sure we have a member DIE for decl. */
27245 if (origin != NULL_TREE)
27246 gen_type_die_for_member (origin, decl, context_die);
27248 /* And its containing namespace. */
27249 context_die = declare_in_namespace (decl, context_die);
27252 /* Now output a DIE to represent the function itself. */
27253 if (decl)
27254 gen_subprogram_die (decl, context_die);
27255 break;
27257 case TYPE_DECL:
27258 /* If we are in terse mode, don't generate any DIEs to represent any
27259 actual typedefs. */
27260 if (debug_info_level <= DINFO_LEVEL_TERSE)
27261 break;
27263 /* In the special case of a TYPE_DECL node representing the declaration
27264 of some type tag, if the given TYPE_DECL is marked as having been
27265 instantiated from some other (original) TYPE_DECL node (e.g. one which
27266 was generated within the original definition of an inline function) we
27267 used to generate a special (abbreviated) DW_TAG_structure_type,
27268 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
27269 should be actually referencing those DIEs, as variable DIEs with that
27270 type would be emitted already in the abstract origin, so it was always
27271 removed during unused type prunning. Don't add anything in this
27272 case. */
27273 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
27274 break;
27276 if (is_redundant_typedef (decl))
27277 gen_type_die (TREE_TYPE (decl), context_die);
27278 else
27279 /* Output a DIE to represent the typedef itself. */
27280 gen_typedef_die (decl, context_die);
27281 break;
27283 case LABEL_DECL:
27284 if (debug_info_level >= DINFO_LEVEL_NORMAL)
27285 gen_label_die (decl, context_die);
27286 break;
27288 case VAR_DECL:
27289 case RESULT_DECL:
27290 /* If we are in terse mode, don't generate any DIEs to represent any
27291 variable declarations or definitions unless it is external. */
27292 if (debug_info_level < DINFO_LEVEL_TERSE
27293 || (debug_info_level == DINFO_LEVEL_TERSE
27294 && !TREE_PUBLIC (decl_or_origin)))
27295 break;
27297 if (debug_info_level > DINFO_LEVEL_TERSE)
27299 /* Avoid generating stray type DIEs during late dwarf dumping.
27300 All types have been dumped early. */
27301 if (early_dwarf
27302 /* ??? But in LTRANS we cannot annotate early created variably
27303 modified type DIEs without copying them and adjusting all
27304 references to them. Dump them again as happens for inlining
27305 which copies both the decl and the types. */
27306 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27307 in VLA bound information for example. */
27308 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27309 current_function_decl)))
27311 /* Output any DIEs that are needed to specify the type of this data
27312 object. */
27313 if (decl_by_reference_p (decl_or_origin))
27314 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27315 else
27316 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27319 if (early_dwarf)
27321 /* And its containing type. */
27322 class_origin = decl_class_context (decl_or_origin);
27323 if (class_origin != NULL_TREE)
27324 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
27326 /* And its containing namespace. */
27327 context_die = declare_in_namespace (decl_or_origin, context_die);
27331 /* Now output the DIE to represent the data object itself. This gets
27332 complicated because of the possibility that the VAR_DECL really
27333 represents an inlined instance of a formal parameter for an inline
27334 function. */
27335 ultimate_origin = decl_ultimate_origin (decl_or_origin);
27336 if (ultimate_origin != NULL_TREE
27337 && TREE_CODE (ultimate_origin) == PARM_DECL)
27338 gen_formal_parameter_die (decl, origin,
27339 true /* Emit name attribute. */,
27340 context_die);
27341 else
27342 gen_variable_die (decl, origin, context_die);
27343 break;
27345 case FIELD_DECL:
27346 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
27347 /* Ignore the nameless fields that are used to skip bits but handle C++
27348 anonymous unions and structs. */
27349 if (DECL_NAME (decl) != NULL_TREE
27350 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
27351 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
27353 gen_type_die (member_declared_type (decl), context_die);
27354 gen_field_die (decl, ctx, context_die);
27356 break;
27358 case PARM_DECL:
27359 /* Avoid generating stray type DIEs during late dwarf dumping.
27360 All types have been dumped early. */
27361 if (early_dwarf
27362 /* ??? But in LTRANS we cannot annotate early created variably
27363 modified type DIEs without copying them and adjusting all
27364 references to them. Dump them again as happens for inlining
27365 which copies both the decl and the types. */
27366 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27367 in VLA bound information for example. */
27368 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27369 current_function_decl)))
27371 if (DECL_BY_REFERENCE (decl_or_origin))
27372 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27373 else
27374 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27376 return gen_formal_parameter_die (decl, origin,
27377 true /* Emit name attribute. */,
27378 context_die);
27380 case NAMESPACE_DECL:
27381 if (dwarf_version >= 3 || !dwarf_strict)
27382 gen_namespace_die (decl, context_die);
27383 break;
27385 case IMPORTED_DECL:
27386 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
27387 DECL_CONTEXT (decl), context_die);
27388 break;
27390 case NAMELIST_DECL:
27391 gen_namelist_decl (DECL_NAME (decl), context_die,
27392 NAMELIST_DECL_ASSOCIATED_DECL (decl));
27393 break;
27395 default:
27396 /* Probably some frontend-internal decl. Assume we don't care. */
27397 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
27398 break;
27401 return NULL;
27404 /* Output initial debug information for global DECL. Called at the
27405 end of the parsing process.
27407 This is the initial debug generation process. As such, the DIEs
27408 generated may be incomplete. A later debug generation pass
27409 (dwarf2out_late_global_decl) will augment the information generated
27410 in this pass (e.g., with complete location info). */
27412 static void
27413 dwarf2out_early_global_decl (tree decl)
27415 set_early_dwarf s;
27417 /* gen_decl_die() will set DECL_ABSTRACT because
27418 cgraph_function_possibly_inlined_p() returns true. This is in
27419 turn will cause DW_AT_inline attributes to be set.
27421 This happens because at early dwarf generation, there is no
27422 cgraph information, causing cgraph_function_possibly_inlined_p()
27423 to return true. Trick cgraph_function_possibly_inlined_p()
27424 while we generate dwarf early. */
27425 bool save = symtab->global_info_ready;
27426 symtab->global_info_ready = true;
27428 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27429 other DECLs and they can point to template types or other things
27430 that dwarf2out can't handle when done via dwarf2out_decl. */
27431 if (TREE_CODE (decl) != TYPE_DECL
27432 && TREE_CODE (decl) != PARM_DECL)
27434 if (TREE_CODE (decl) == FUNCTION_DECL)
27436 tree save_fndecl = current_function_decl;
27438 /* For nested functions, make sure we have DIEs for the parents first
27439 so that all nested DIEs are generated at the proper scope in the
27440 first shot. */
27441 tree context = decl_function_context (decl);
27442 if (context != NULL)
27444 dw_die_ref context_die = lookup_decl_die (context);
27445 current_function_decl = context;
27447 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27448 enough so that it lands in its own context. This avoids type
27449 pruning issues later on. */
27450 if (context_die == NULL || is_declaration_die (context_die))
27451 dwarf2out_early_global_decl (context);
27454 /* Emit an abstract origin of a function first. This happens
27455 with C++ constructor clones for example and makes
27456 dwarf2out_abstract_function happy which requires the early
27457 DIE of the abstract instance to be present. */
27458 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27459 dw_die_ref origin_die;
27460 if (origin != NULL
27461 /* Do not emit the DIE multiple times but make sure to
27462 process it fully here in case we just saw a declaration. */
27463 && ((origin_die = lookup_decl_die (origin)) == NULL
27464 || is_declaration_die (origin_die)))
27466 current_function_decl = origin;
27467 dwarf2out_decl (origin);
27470 /* Emit the DIE for decl but avoid doing that multiple times. */
27471 dw_die_ref old_die;
27472 if ((old_die = lookup_decl_die (decl)) == NULL
27473 || is_declaration_die (old_die))
27475 current_function_decl = decl;
27476 dwarf2out_decl (decl);
27479 current_function_decl = save_fndecl;
27481 else
27482 dwarf2out_decl (decl);
27484 symtab->global_info_ready = save;
27487 /* Return whether EXPR is an expression with the following pattern:
27488 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27490 static bool
27491 is_trivial_indirect_ref (tree expr)
27493 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27494 return false;
27496 tree nop = TREE_OPERAND (expr, 0);
27497 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27498 return false;
27500 tree int_cst = TREE_OPERAND (nop, 0);
27501 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27504 /* Output debug information for global decl DECL. Called from
27505 toplev.cc after compilation proper has finished. */
27507 static void
27508 dwarf2out_late_global_decl (tree decl)
27510 /* Fill-in any location information we were unable to determine
27511 on the first pass. */
27512 if (VAR_P (decl))
27514 dw_die_ref die = lookup_decl_die (decl);
27516 /* We may have to generate full debug late for LTO in case debug
27517 was not enabled at compile-time or the target doesn't support
27518 the LTO early debug scheme. */
27519 if (! die && in_lto_p
27520 /* Function scope variables are emitted when emitting the
27521 DIE for the function. */
27522 && ! local_function_static (decl))
27523 dwarf2out_decl (decl);
27524 else if (die)
27526 /* We get called via the symtab code invoking late_global_decl
27527 for symbols that are optimized out.
27529 Do not add locations for those, except if they have a
27530 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27531 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27532 INDIRECT_REF expression, as this could generate relocations to
27533 text symbols in LTO object files, which is invalid. */
27534 varpool_node *node = varpool_node::get (decl);
27535 if ((! node || ! node->definition)
27536 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27537 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27538 tree_add_const_value_attribute_for_decl (die, decl);
27539 else
27540 add_location_or_const_value_attribute (die, decl, false);
27545 /* Output debug information for type decl DECL. Called from toplev.cc
27546 and from language front ends (to record built-in types). */
27547 static void
27548 dwarf2out_type_decl (tree decl, int local)
27550 if (!local)
27552 set_early_dwarf s;
27553 dwarf2out_decl (decl);
27557 /* Output debug information for imported module or decl DECL.
27558 NAME is non-NULL name in the lexical block if the decl has been renamed.
27559 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27560 that DECL belongs to.
27561 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27562 static void
27563 dwarf2out_imported_module_or_decl_1 (tree decl,
27564 tree name,
27565 tree lexical_block,
27566 dw_die_ref lexical_block_die)
27568 expanded_location xloc;
27569 dw_die_ref imported_die = NULL;
27570 dw_die_ref at_import_die;
27572 if (TREE_CODE (decl) == IMPORTED_DECL)
27574 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27575 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27576 gcc_assert (decl);
27578 else
27579 xloc = expand_location (input_location);
27581 if (TREE_CODE (decl) == TYPE_DECL)
27583 at_import_die = force_type_die (TREE_TYPE (decl));
27584 /* For namespace N { typedef void T; } using N::T; base_type_die
27585 returns NULL, but DW_TAG_imported_declaration requires
27586 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27587 if (!at_import_die)
27589 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27590 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27591 at_import_die = lookup_type_die (TREE_TYPE (decl));
27592 gcc_assert (at_import_die);
27595 else
27597 at_import_die = lookup_decl_die (decl);
27598 if (!at_import_die)
27600 /* If we're trying to avoid duplicate debug info, we may not have
27601 emitted the member decl for this field. Emit it now. */
27602 if (TREE_CODE (decl) == FIELD_DECL)
27604 tree type = DECL_CONTEXT (decl);
27606 if (TYPE_CONTEXT (type)
27607 && TYPE_P (TYPE_CONTEXT (type))
27608 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27609 DINFO_USAGE_DIR_USE))
27610 return;
27611 gen_type_die_for_member (type, decl,
27612 get_context_die (TYPE_CONTEXT (type)));
27614 if (TREE_CODE (decl) == CONST_DECL)
27616 /* Individual enumerators of an enum type do not get output here
27617 (see gen_decl_die), so we cannot call force_decl_die. */
27618 if (!is_fortran () && !is_ada () && !is_dlang ())
27619 return;
27621 if (TREE_CODE (decl) == NAMELIST_DECL)
27622 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27623 get_context_die (DECL_CONTEXT (decl)),
27624 NULL_TREE);
27625 else
27626 at_import_die = force_decl_die (decl);
27630 if (TREE_CODE (decl) == NAMESPACE_DECL)
27632 if (dwarf_version >= 3 || !dwarf_strict)
27633 imported_die = new_die (DW_TAG_imported_module,
27634 lexical_block_die,
27635 lexical_block);
27636 else
27637 return;
27639 else
27640 imported_die = new_die (DW_TAG_imported_declaration,
27641 lexical_block_die,
27642 lexical_block);
27644 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27645 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27646 if (debug_column_info && xloc.column)
27647 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27648 if (name)
27649 add_AT_string (imported_die, DW_AT_name,
27650 IDENTIFIER_POINTER (name));
27651 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27654 /* Output debug information for imported module or decl DECL.
27655 NAME is non-NULL name in context if the decl has been renamed.
27656 CHILD is true if decl is one of the renamed decls as part of
27657 importing whole module.
27658 IMPLICIT is set if this hook is called for an implicit import
27659 such as inline namespace. */
27661 static void
27662 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27663 bool child, bool implicit)
27665 /* dw_die_ref at_import_die; */
27666 dw_die_ref scope_die;
27668 if (debug_info_level <= DINFO_LEVEL_TERSE)
27669 return;
27671 gcc_assert (decl);
27673 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27674 should be enough, for DWARF4 and older even if we emit as extension
27675 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27676 for the benefit of consumers unaware of DW_AT_export_symbols. */
27677 if (implicit
27678 && dwarf_version >= 5
27679 && lang_hooks.decls.decl_dwarf_attribute (decl,
27680 DW_AT_export_symbols) == 1)
27681 return;
27683 set_early_dwarf s;
27685 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27686 We need decl DIE for reference and scope die. First, get DIE for the decl
27687 itself. */
27689 /* Get the scope die for decl context. Use comp_unit_die for global module
27690 or decl. If die is not found for non globals, force new die. */
27691 if (context
27692 && TYPE_P (context)
27693 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27694 return;
27696 scope_die = get_context_die (context);
27698 if (child)
27700 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27701 there is nothing we can do, here. */
27702 if (dwarf_version < 3 && dwarf_strict)
27703 return;
27705 gcc_assert (scope_die->die_child);
27706 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27707 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27708 scope_die = scope_die->die_child;
27711 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27712 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27715 /* Output debug information for namelists. */
27717 static dw_die_ref
27718 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27720 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27721 tree value;
27722 unsigned i;
27724 if (debug_info_level <= DINFO_LEVEL_TERSE)
27725 return NULL;
27727 gcc_assert (scope_die != NULL);
27728 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27729 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27731 /* If there are no item_decls, we have a nondefining namelist, e.g.
27732 with USE association; hence, set DW_AT_declaration. */
27733 if (item_decls == NULL_TREE)
27735 add_AT_flag (nml_die, DW_AT_declaration, 1);
27736 return nml_die;
27739 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27741 nml_item_ref_die = lookup_decl_die (value);
27742 if (!nml_item_ref_die)
27743 nml_item_ref_die = force_decl_die (value);
27745 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27746 add_AT_die_ref (nml_item_die, DW_AT_namelist_item, nml_item_ref_die);
27748 return nml_die;
27752 /* Write the debugging output for DECL and return the DIE. */
27754 static void
27755 dwarf2out_decl (tree decl)
27757 dw_die_ref context_die = comp_unit_die ();
27759 switch (TREE_CODE (decl))
27761 case ERROR_MARK:
27762 return;
27764 case FUNCTION_DECL:
27765 /* If we're a nested function, initially use a parent of NULL; if we're
27766 a plain function, this will be fixed up in decls_for_scope. If
27767 we're a method, it will be ignored, since we already have a DIE.
27768 Avoid doing this late though since clones of class methods may
27769 otherwise end up in limbo and create type DIEs late. */
27770 if (early_dwarf
27771 && decl_function_context (decl)
27772 /* But if we're in terse mode, we don't care about scope. */
27773 && debug_info_level > DINFO_LEVEL_TERSE)
27774 context_die = NULL;
27775 break;
27777 case VAR_DECL:
27778 /* For local statics lookup proper context die. */
27779 if (local_function_static (decl))
27780 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27782 /* If we are in terse mode, don't generate any DIEs to represent any
27783 variable declarations or definitions unless it is external. */
27784 if (debug_info_level < DINFO_LEVEL_TERSE
27785 || (debug_info_level == DINFO_LEVEL_TERSE
27786 && !TREE_PUBLIC (decl)))
27787 return;
27788 break;
27790 case CONST_DECL:
27791 if (debug_info_level <= DINFO_LEVEL_TERSE)
27792 return;
27793 if (!is_fortran () && !is_ada () && !is_dlang ())
27794 return;
27795 if (TREE_STATIC (decl) && decl_function_context (decl))
27796 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27797 break;
27799 case NAMESPACE_DECL:
27800 case IMPORTED_DECL:
27801 if (debug_info_level <= DINFO_LEVEL_TERSE)
27802 return;
27803 if (lookup_decl_die (decl) != NULL)
27804 return;
27805 break;
27807 case TYPE_DECL:
27808 /* Don't emit stubs for types unless they are needed by other DIEs. */
27809 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27810 return;
27812 /* Don't bother trying to generate any DIEs to represent any of the
27813 normal built-in types for the language we are compiling. */
27814 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27815 return;
27817 /* If we are in terse mode, don't generate any DIEs for types. */
27818 if (debug_info_level <= DINFO_LEVEL_TERSE)
27819 return;
27821 /* If we're a function-scope tag, initially use a parent of NULL;
27822 this will be fixed up in decls_for_scope. */
27823 if (decl_function_context (decl))
27824 context_die = NULL;
27826 break;
27828 case NAMELIST_DECL:
27829 break;
27831 default:
27832 return;
27835 gen_decl_die (decl, NULL, NULL, context_die);
27837 if (flag_checking)
27839 dw_die_ref die = lookup_decl_die (decl);
27840 if (die)
27841 check_die (die);
27845 /* Write the debugging output for DECL. */
27847 static void
27848 dwarf2out_function_decl (tree decl)
27850 dwarf2out_decl (decl);
27851 call_arg_locations = NULL;
27852 call_arg_loc_last = NULL;
27853 call_site_count = -1;
27854 tail_call_site_count = -1;
27855 decl_loc_table->empty ();
27856 cached_dw_loc_list_table->empty ();
27859 /* Output a marker (i.e. a label) for the beginning of the generated code for
27860 a lexical block. */
27862 static void
27863 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27864 unsigned int blocknum,
27865 tree block ATTRIBUTE_UNUSED)
27867 #ifdef CODEVIEW_DEBUGGING_INFO
27868 if (codeview_debuginfo_p ())
27869 codeview_begin_block (line, blocknum, block);
27870 #endif
27872 switch_to_section (current_function_section ());
27873 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27876 /* Output a marker (i.e. a label) for the end of the generated code for a
27877 lexical block. */
27879 static void
27880 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27882 #ifdef CODEVIEW_DEBUGGING_INFO
27883 if (codeview_debuginfo_p ())
27884 codeview_end_block (line, blocknum);
27885 #endif
27887 switch_to_section (current_function_section ());
27888 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27891 /* Returns true if it is appropriate not to emit any debugging
27892 information for BLOCK, because it doesn't contain any instructions.
27894 Don't allow this for blocks with nested functions or local classes
27895 as we would end up with orphans, and in the presence of scheduling
27896 we may end up calling them anyway. */
27898 static bool
27899 dwarf2out_ignore_block (const_tree block)
27901 tree decl;
27902 unsigned int i;
27904 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27905 if (TREE_CODE (decl) == FUNCTION_DECL
27906 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27907 return false;
27908 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27910 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27911 if (TREE_CODE (decl) == FUNCTION_DECL
27912 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27913 return false;
27916 return true;
27919 /* Hash table routines for file_hash. */
27921 bool
27922 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27924 return filename_cmp (p1->key, p2) == 0;
27927 hashval_t
27928 dwarf_file_hasher::hash (dwarf_file_data *p)
27930 return htab_hash_string (p->key);
27933 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27934 dwarf2out.cc) and return its "index". The index of each (known) filename is
27935 just a unique number which is associated with only that one filename. We
27936 need such numbers for the sake of generating labels (in the .debug_sfnames
27937 section) and references to those files numbers (in the .debug_srcinfo
27938 and .debug_macinfo sections). If the filename given as an argument is not
27939 found in our current list, add it to the list and assign it the next
27940 available unique index number. */
27942 static struct dwarf_file_data *
27943 lookup_filename (const char *file_name)
27945 struct dwarf_file_data * created;
27947 if (!file_name)
27948 return NULL;
27950 if (!file_name[0])
27951 file_name = "<stdin>";
27953 dwarf_file_data **slot
27954 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27955 INSERT);
27956 if (*slot)
27957 return *slot;
27959 created = ggc_alloc<dwarf_file_data> ();
27960 created->key = file_name;
27961 created->filename = remap_debug_filename (file_name);
27962 created->emitted_number = 0;
27963 *slot = created;
27964 return created;
27967 /* If the assembler will construct the file table, then translate the compiler
27968 internal file table number into the assembler file table number, and emit
27969 a .file directive if we haven't already emitted one yet. The file table
27970 numbers are different because we prune debug info for unused variables and
27971 types, which may include filenames. */
27973 static int
27974 maybe_emit_file (struct dwarf_file_data * fd)
27976 if (! fd->emitted_number)
27978 if (last_emitted_file)
27979 fd->emitted_number = last_emitted_file->emitted_number + 1;
27980 else
27981 fd->emitted_number = 1;
27982 last_emitted_file = fd;
27984 if (output_asm_line_debug_info ())
27986 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27987 output_quoted_string (asm_out_file, fd->filename);
27988 fputc ('\n', asm_out_file);
27992 return fd->emitted_number;
27995 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27996 That generation should happen after function debug info has been
27997 generated. The value of the attribute is the constant value of ARG. */
27999 static void
28000 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
28002 die_arg_entry entry;
28004 if (!die || !arg)
28005 return;
28007 gcc_assert (early_dwarf);
28009 if (!tmpl_value_parm_die_table)
28010 vec_alloc (tmpl_value_parm_die_table, 32);
28012 entry.die = die;
28013 entry.arg = arg;
28014 vec_safe_push (tmpl_value_parm_die_table, entry);
28017 /* Return TRUE if T is an instance of generic type, FALSE
28018 otherwise. */
28020 static bool
28021 generic_type_p (tree t)
28023 if (t == NULL_TREE || !TYPE_P (t))
28024 return false;
28025 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
28028 /* Schedule the generation of the generic parameter dies for the
28029 instance of generic type T. The proper generation itself is later
28030 done by gen_scheduled_generic_parms_dies. */
28032 static void
28033 schedule_generic_params_dies_gen (tree t)
28035 if (!generic_type_p (t))
28036 return;
28038 gcc_assert (early_dwarf);
28040 if (!generic_type_instances)
28041 vec_alloc (generic_type_instances, 256);
28043 vec_safe_push (generic_type_instances, t);
28046 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
28047 by append_entry_to_tmpl_value_parm_die_table. This function must
28048 be called after function DIEs have been generated. */
28050 static void
28051 gen_remaining_tmpl_value_param_die_attribute (void)
28053 if (tmpl_value_parm_die_table)
28055 unsigned i, j;
28056 die_arg_entry *e;
28058 /* We do this in two phases - first get the cases we can
28059 handle during early-finish, preserving those we cannot
28060 (containing symbolic constants where we don't yet know
28061 whether we are going to output the referenced symbols).
28062 For those we try again at late-finish. */
28063 j = 0;
28064 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
28066 if (!e->die->removed
28067 && !tree_add_const_value_attribute (e->die, e->arg))
28069 dw_loc_descr_ref loc = NULL;
28070 if (! early_dwarf
28071 && (dwarf_version >= 5 || !dwarf_strict))
28072 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
28073 if (loc)
28074 add_AT_loc (e->die, DW_AT_location, loc);
28075 else
28076 (*tmpl_value_parm_die_table)[j++] = *e;
28079 tmpl_value_parm_die_table->truncate (j);
28083 /* Generate generic parameters DIEs for instances of generic types
28084 that have been previously scheduled by
28085 schedule_generic_params_dies_gen. This function must be called
28086 after all the types of the CU have been laid out. */
28088 static void
28089 gen_scheduled_generic_parms_dies (void)
28091 unsigned i;
28092 tree t;
28094 if (!generic_type_instances)
28095 return;
28097 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
28098 if (COMPLETE_TYPE_P (t))
28099 gen_generic_params_dies (t);
28101 generic_type_instances = NULL;
28105 /* Replace DW_AT_name for the decl with name. */
28107 static void
28108 dwarf2out_set_name (tree decl, tree name)
28110 dw_die_ref die;
28111 dw_attr_node *attr;
28112 const char *dname;
28114 die = TYPE_SYMTAB_DIE (decl);
28115 if (!die)
28116 return;
28118 dname = dwarf2_name (name, 0);
28119 if (!dname)
28120 return;
28122 attr = get_AT (die, DW_AT_name);
28123 if (attr)
28125 struct indirect_string_node *node;
28127 node = find_AT_string (dname);
28128 /* replace the string. */
28129 attr->dw_attr_val.v.val_str = node;
28132 else
28133 add_name_attribute (die, dname);
28136 /* True if before or during processing of the first function being emitted. */
28137 static bool in_first_function_p = true;
28138 /* True if loc_note during dwarf2out_var_location call might still be
28139 before first real instruction at address equal to .Ltext0. */
28140 static bool maybe_at_text_label_p = true;
28141 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
28142 static unsigned int first_loclabel_num_not_at_text_label;
28144 /* Look ahead for a real insn. */
28146 static rtx_insn *
28147 dwarf2out_next_real_insn (rtx_insn *loc_note)
28149 rtx_insn *next_real = NEXT_INSN (loc_note);
28151 while (next_real)
28152 if (INSN_P (next_real))
28153 break;
28154 else
28155 next_real = NEXT_INSN (next_real);
28157 return next_real;
28160 /* Called by the final INSN scan whenever we see a var location. We
28161 use it to drop labels in the right places, and throw the location in
28162 our lookup table. */
28164 static void
28165 dwarf2out_var_location (rtx_insn *loc_note)
28167 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
28168 struct var_loc_node *newloc;
28169 rtx_insn *next_real;
28170 rtx_insn *call_insn = NULL;
28171 static const char *last_label;
28172 static const char *last_postcall_label;
28173 static bool last_in_cold_section_p;
28174 static rtx_insn *expected_next_loc_note;
28175 tree decl;
28176 bool var_loc_p;
28177 var_loc_view view = 0;
28179 if (!NOTE_P (loc_note))
28181 if (CALL_P (loc_note))
28183 maybe_reset_location_view (loc_note, cur_line_info_table);
28184 call_site_count++;
28185 if (SIBLING_CALL_P (loc_note))
28186 tail_call_site_count++;
28187 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
28189 call_insn = loc_note;
28190 loc_note = NULL;
28191 var_loc_p = false;
28193 next_real = dwarf2out_next_real_insn (call_insn);
28194 cached_next_real_insn = NULL;
28195 goto create_label;
28197 if (optimize == 0 && !flag_var_tracking)
28199 /* When the var-tracking pass is not running, there is no note
28200 for indirect calls whose target is compile-time known. In this
28201 case, process such calls specifically so that we generate call
28202 sites for them anyway. */
28203 rtx x = PATTERN (loc_note);
28204 if (GET_CODE (x) == PARALLEL)
28205 x = XVECEXP (x, 0, 0);
28206 if (GET_CODE (x) == SET)
28207 x = SET_SRC (x);
28208 if (GET_CODE (x) == CALL)
28209 x = XEXP (x, 0);
28210 if (!MEM_P (x)
28211 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
28212 || !SYMBOL_REF_DECL (XEXP (x, 0))
28213 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
28214 != FUNCTION_DECL))
28216 call_insn = loc_note;
28217 loc_note = NULL;
28218 var_loc_p = false;
28220 next_real = dwarf2out_next_real_insn (call_insn);
28221 cached_next_real_insn = NULL;
28222 goto create_label;
28226 else if (!debug_variable_location_views)
28227 gcc_unreachable ();
28228 else
28229 maybe_reset_location_view (loc_note, cur_line_info_table);
28231 return;
28234 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
28235 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
28236 return;
28238 /* Optimize processing a large consecutive sequence of location
28239 notes so we don't spend too much time in next_real_insn. If the
28240 next insn is another location note, remember the next_real_insn
28241 calculation for next time. */
28242 next_real = cached_next_real_insn;
28243 if (next_real)
28245 if (expected_next_loc_note != loc_note)
28246 next_real = NULL;
28249 if (! next_real)
28250 next_real = dwarf2out_next_real_insn (loc_note);
28252 if (next_real)
28254 rtx_insn *next_note = NEXT_INSN (loc_note);
28255 while (next_note != next_real)
28257 if (! next_note->deleted ()
28258 && NOTE_P (next_note)
28259 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
28260 break;
28261 next_note = NEXT_INSN (next_note);
28264 if (next_note == next_real)
28265 cached_next_real_insn = NULL;
28266 else
28268 expected_next_loc_note = next_note;
28269 cached_next_real_insn = next_real;
28272 else
28273 cached_next_real_insn = NULL;
28275 /* If there are no instructions which would be affected by this note,
28276 don't do anything. */
28277 if (var_loc_p
28278 && next_real == NULL_RTX
28279 && !NOTE_DURING_CALL_P (loc_note))
28280 return;
28282 create_label:
28284 if (next_real == NULL_RTX)
28285 next_real = get_last_insn ();
28287 /* If there were any real insns between note we processed last time
28288 and this note (or if it is the first note), clear
28289 last_{,postcall_}label so that they are not reused this time. */
28290 if (last_var_location_insn == NULL_RTX
28291 || last_var_location_insn != next_real
28292 || last_in_cold_section_p != in_cold_section_p)
28294 last_label = NULL;
28295 last_postcall_label = NULL;
28298 if (var_loc_p)
28300 const char *label
28301 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
28302 view = cur_line_info_table->view;
28303 decl = NOTE_VAR_LOCATION_DECL (loc_note);
28304 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
28305 if (newloc == NULL)
28306 return;
28308 else
28310 decl = NULL_TREE;
28311 newloc = NULL;
28314 /* If there were no real insns between note we processed last time
28315 and this note, use the label we emitted last time. Otherwise
28316 create a new label and emit it. */
28317 if (last_label == NULL)
28319 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
28320 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
28321 loclabel_num++;
28322 last_label = ggc_strdup (loclabel);
28323 /* See if loclabel might be equal to .Ltext0. If yes,
28324 bump first_loclabel_num_not_at_text_label. */
28325 if (!have_multiple_function_sections
28326 && in_first_function_p
28327 && maybe_at_text_label_p)
28329 static rtx_insn *last_start;
28330 rtx_insn *insn;
28331 for (insn = loc_note; insn; insn = previous_insn (insn))
28332 if (insn == last_start)
28333 break;
28334 else if (!NONDEBUG_INSN_P (insn))
28335 continue;
28336 else
28338 rtx body = PATTERN (insn);
28339 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
28340 continue;
28341 /* Inline asm could occupy zero bytes. */
28342 else if (GET_CODE (body) == ASM_INPUT
28343 || asm_noperands (body) >= 0)
28344 continue;
28345 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
28346 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
28347 continue;
28348 #endif
28349 else
28351 /* Assume insn has non-zero length. */
28352 maybe_at_text_label_p = false;
28353 break;
28356 if (maybe_at_text_label_p)
28358 last_start = loc_note;
28359 first_loclabel_num_not_at_text_label = loclabel_num;
28364 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
28365 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
28367 if (!var_loc_p)
28369 struct call_arg_loc_node *ca_loc
28370 = ggc_cleared_alloc<call_arg_loc_node> ();
28371 rtx_insn *prev = call_insn;
28373 ca_loc->call_insn = call_insn;
28374 ca_loc->next = NULL;
28375 ca_loc->label = last_label;
28376 gcc_assert (prev
28377 && (CALL_P (prev)
28378 || (NONJUMP_INSN_P (prev)
28379 && GET_CODE (PATTERN (prev)) == SEQUENCE
28380 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
28381 if (!CALL_P (prev))
28382 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
28383 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
28385 /* Look for a SYMBOL_REF in the "prev" instruction. */
28386 rtx x = get_call_rtx_from (prev);
28387 if (x)
28389 /* Try to get the call symbol, if any. */
28390 if (MEM_P (XEXP (x, 0)))
28391 x = XEXP (x, 0);
28392 /* First, look for a memory access to a symbol_ref. */
28393 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
28394 && SYMBOL_REF_DECL (XEXP (x, 0))
28395 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
28396 ca_loc->symbol_ref = XEXP (x, 0);
28397 /* Otherwise, look at a compile-time known user-level function
28398 declaration. */
28399 else if (MEM_P (x)
28400 && MEM_EXPR (x)
28401 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
28402 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
28405 ca_loc->block = insn_scope (prev);
28406 if (call_arg_locations)
28407 call_arg_loc_last->next = ca_loc;
28408 else
28409 call_arg_locations = ca_loc;
28410 call_arg_loc_last = ca_loc;
28412 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
28414 newloc->label = last_label;
28415 newloc->view = view;
28417 else
28419 if (!last_postcall_label)
28421 sprintf (loclabel, "%s-1", last_label);
28422 last_postcall_label = ggc_strdup (loclabel);
28424 newloc->label = last_postcall_label;
28425 /* ??? This view is at last_label, not last_label-1, but we
28426 could only assume view at last_label-1 is zero if we could
28427 assume calls always have length greater than one. This is
28428 probably true in general, though there might be a rare
28429 exception to this rule, e.g. if a call insn is optimized out
28430 by target magic. Then, even the -1 in the label will be
28431 wrong, which might invalidate the range. Anyway, using view,
28432 though technically possibly incorrect, will work as far as
28433 ranges go: since L-1 is in the middle of the call insn,
28434 (L-1).0 and (L-1).V shouldn't make any difference, and having
28435 the loclist entry refer to the .loc entry might be useful, so
28436 leave it like this. */
28437 newloc->view = view;
28440 if (var_loc_p && flag_debug_asm)
28442 const char *name, *sep, *patstr;
28443 if (decl && DECL_NAME (decl))
28444 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28445 else
28446 name = "";
28447 if (NOTE_VAR_LOCATION_LOC (loc_note))
28449 sep = " => ";
28450 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28452 else
28454 sep = " ";
28455 patstr = "RESET";
28457 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28458 name, sep, patstr);
28461 last_var_location_insn = next_real;
28462 last_in_cold_section_p = in_cold_section_p;
28465 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28466 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28467 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28468 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28469 BLOCK_FRAGMENT_ORIGIN links. */
28470 static bool
28471 block_within_block_p (tree block, tree outer, bool bothways)
28473 if (block == outer)
28474 return true;
28476 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28477 for (tree context = BLOCK_SUPERCONTEXT (block);
28478 context != outer;
28479 context = BLOCK_SUPERCONTEXT (context))
28480 if (!context || TREE_CODE (context) != BLOCK)
28481 return false;
28483 if (!bothways)
28484 return true;
28486 /* Now check that each block is actually referenced by its
28487 parent. */
28488 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28489 context = BLOCK_SUPERCONTEXT (context))
28491 if (BLOCK_FRAGMENT_ORIGIN (context))
28493 gcc_assert (!BLOCK_SUBBLOCKS (context));
28494 context = BLOCK_FRAGMENT_ORIGIN (context);
28496 for (tree sub = BLOCK_SUBBLOCKS (context);
28497 sub != block;
28498 sub = BLOCK_CHAIN (sub))
28499 if (!sub)
28500 return false;
28501 if (context == outer)
28502 return true;
28503 else
28504 block = context;
28508 /* Called during final while assembling the marker of the entry point
28509 for an inlined function. */
28511 static void
28512 dwarf2out_inline_entry (tree block)
28514 gcc_assert (debug_inline_points);
28516 /* If we can't represent it, don't bother. */
28517 if (!(dwarf_version >= 3 || !dwarf_strict))
28518 return;
28520 gcc_assert (DECL_P (block_ultimate_origin (block)));
28522 /* Sanity check the block tree. This would catch a case in which
28523 BLOCK got removed from the tree reachable from the outermost
28524 lexical block, but got retained in markers. It would still link
28525 back to its parents, but some ancestor would be missing a link
28526 down the path to the sub BLOCK. If the block got removed, its
28527 BLOCK_NUMBER will not be a usable value. */
28528 if (flag_checking)
28529 gcc_assert (block_within_block_p (block,
28530 DECL_INITIAL (current_function_decl),
28531 true));
28533 gcc_assert (inlined_function_outer_scope_p (block));
28534 gcc_assert (!lookup_block_die (block));
28536 if (BLOCK_FRAGMENT_ORIGIN (block))
28537 block = BLOCK_FRAGMENT_ORIGIN (block);
28538 /* Can the entry point ever not be at the beginning of an
28539 unfragmented lexical block? */
28540 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28541 || (cur_line_info_table
28542 && !ZERO_VIEW_P (cur_line_info_table->view))))
28543 return;
28545 if (!inline_entry_data_table)
28546 inline_entry_data_table
28547 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28550 inline_entry_data **iedp
28551 = inline_entry_data_table->find_slot_with_hash (block,
28552 htab_hash_pointer (block),
28553 INSERT);
28554 if (*iedp)
28555 /* ??? Ideally, we'd record all entry points for the same inlined
28556 function (some may have been duplicated by e.g. unrolling), but
28557 we have no way to represent that ATM. */
28558 return;
28560 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28561 ied->block = block;
28562 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28563 ied->label_num = BLOCK_NUMBER (block);
28564 if (cur_line_info_table)
28565 ied->view = cur_line_info_table->view;
28567 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28568 BLOCK_NUMBER (block));
28571 /* Called from finalize_size_functions for size functions so that their body
28572 can be encoded in the debug info to describe the layout of variable-length
28573 structures. */
28575 static void
28576 dwarf2out_size_function (tree decl)
28578 set_early_dwarf s;
28579 function_to_dwarf_procedure (decl);
28582 /* Note in one location list that text section has changed. */
28585 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28587 var_loc_list *list = *slot;
28588 if (list->first)
28589 list->last_before_switch
28590 = list->last->next ? list->last->next : list->last;
28591 return 1;
28594 /* Note in all location lists that text section has changed. */
28596 static void
28597 var_location_switch_text_section (void)
28599 if (decl_loc_table == NULL)
28600 return;
28602 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28605 /* Create a new line number table. */
28607 static dw_line_info_table *
28608 new_line_info_table (void)
28610 dw_line_info_table *table;
28612 table = ggc_cleared_alloc<dw_line_info_table> ();
28613 table->file_num = 1;
28614 table->line_num = 1;
28615 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28616 FORCE_RESET_NEXT_VIEW (table->view);
28617 table->symviews_since_reset = 0;
28619 return table;
28622 /* Lookup the "current" table into which we emit line info, so
28623 that we don't have to do it for every source line. */
28625 static void
28626 set_cur_line_info_table (section *sec)
28628 dw_line_info_table *table;
28630 if (sec == text_section)
28631 table = text_section_line_info;
28632 else if (sec == cold_text_section)
28634 table = cold_text_section_line_info;
28635 if (!table)
28637 cold_text_section_line_info = table = new_line_info_table ();
28638 table->end_label = cold_end_label;
28641 else
28643 const char *end_label;
28645 if (crtl->has_bb_partition)
28647 if (in_cold_section_p)
28648 end_label = crtl->subsections.cold_section_end_label;
28649 else
28650 end_label = crtl->subsections.hot_section_end_label;
28652 else
28654 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28655 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28656 current_function_funcdef_no);
28657 end_label = ggc_strdup (label);
28660 table = new_line_info_table ();
28661 table->end_label = end_label;
28663 vec_safe_push (separate_line_info, table);
28666 if (output_asm_line_debug_info ())
28667 table->is_stmt = (cur_line_info_table
28668 ? cur_line_info_table->is_stmt
28669 : DWARF_LINE_DEFAULT_IS_STMT_START);
28670 cur_line_info_table = table;
28674 /* We need to reset the locations at the beginning of each
28675 function. We can't do this in the end_function hook, because the
28676 declarations that use the locations won't have been output when
28677 that hook is called. Also compute have_multiple_function_sections here. */
28679 static void
28680 dwarf2out_begin_function (tree fun)
28682 section *sec = function_section (fun);
28684 if (sec != text_section)
28685 have_multiple_function_sections = true;
28687 if (crtl->has_bb_partition && !cold_text_section)
28689 gcc_assert (current_function_decl == fun);
28690 cold_text_section = unlikely_text_section ();
28691 switch_to_section (cold_text_section);
28692 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28693 switch_to_section (sec);
28696 call_site_count = 0;
28697 tail_call_site_count = 0;
28699 set_cur_line_info_table (sec);
28700 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28703 /* Helper function of dwarf2out_end_function, called only after emitting
28704 the very first function into assembly. Check if some .debug_loc range
28705 might end with a .LVL* label that could be equal to .Ltext0.
28706 In that case we must force using absolute addresses in .debug_loc ranges,
28707 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28708 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28709 list terminator.
28710 Set have_multiple_function_sections to true in that case and
28711 terminate htab traversal. */
28714 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28716 var_loc_list *entry = *slot;
28717 struct var_loc_node *node;
28719 node = entry->first;
28720 if (node && node->next && node->next->label)
28722 unsigned int i;
28723 const char *label = node->next->label;
28724 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28726 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28728 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28729 if (strcmp (label, loclabel) == 0)
28731 have_multiple_function_sections = true;
28732 return 0;
28736 return 1;
28739 /* Hook called after emitting a function into assembly.
28740 This does something only for the very first function emitted. */
28742 static void
28743 dwarf2out_end_function (unsigned int)
28745 if (in_first_function_p
28746 && !have_multiple_function_sections
28747 && first_loclabel_num_not_at_text_label
28748 && decl_loc_table)
28749 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28750 in_first_function_p = false;
28751 maybe_at_text_label_p = false;
28754 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28755 front-ends register a translation unit even before dwarf2out_init is
28756 called. */
28757 static tree main_translation_unit = NULL_TREE;
28759 /* Hook called by front-ends after they built their main translation unit.
28760 Associate comp_unit_die to UNIT. */
28762 static void
28763 dwarf2out_register_main_translation_unit (tree unit)
28765 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28766 && main_translation_unit == NULL_TREE);
28767 main_translation_unit = unit;
28768 /* If dwarf2out_init has not been called yet, it will perform the association
28769 itself looking at main_translation_unit. */
28770 if (decl_die_table != NULL)
28771 equate_decl_number_to_die (unit, comp_unit_die ());
28774 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28776 static void
28777 push_dw_line_info_entry (dw_line_info_table *table,
28778 enum dw_line_info_opcode opcode, unsigned int val)
28780 dw_line_info_entry e;
28781 e.opcode = opcode;
28782 e.val = val;
28783 vec_safe_push (table->entries, e);
28786 /* Output a label to mark the beginning of a source code line entry
28787 and record information relating to this source line, in
28788 'line_info_table' for later output of the .debug_line section. */
28789 /* ??? The discriminator parameter ought to be unsigned. */
28791 static void
28792 dwarf2out_source_line (unsigned int line, unsigned int column,
28793 const char *filename,
28794 int discriminator, bool is_stmt)
28796 unsigned int file_num;
28797 dw_line_info_table *table;
28798 static var_loc_view lvugid;
28800 #ifdef CODEVIEW_DEBUGGING_INFO
28801 if (codeview_debuginfo_p ())
28802 codeview_source_line (line, filename);
28803 #endif
28805 /* 'line_info_table' information gathering is not needed when the debug
28806 info level is set to the lowest value. Also, the current DWARF-based
28807 debug formats do not use this info. */
28808 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28809 return;
28811 table = cur_line_info_table;
28813 if (line == 0)
28815 if (debug_variable_location_views
28816 && output_asm_line_debug_info ()
28817 && table && !RESETTING_VIEW_P (table->view))
28819 /* If we're using the assembler to compute view numbers, we
28820 can't issue a .loc directive for line zero, so we can't
28821 get a view number at this point. We might attempt to
28822 compute it from the previous view, or equate it to a
28823 subsequent view (though it might not be there!), but
28824 since we're omitting the line number entry, we might as
28825 well omit the view number as well. That means pretending
28826 it's a view number zero, which might very well turn out
28827 to be correct. ??? Extend the assembler so that the
28828 compiler could emit e.g. ".locview .LVU#", to output a
28829 view without changing line number information. We'd then
28830 have to count it in symviews_since_reset; when it's omitted,
28831 it doesn't count. */
28832 if (!zero_view_p)
28833 zero_view_p = BITMAP_GGC_ALLOC ();
28834 bitmap_set_bit (zero_view_p, table->view);
28835 if (flag_debug_asm)
28837 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28838 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28839 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28840 ASM_COMMENT_START);
28841 assemble_name (asm_out_file, label);
28842 putc ('\n', asm_out_file);
28844 table->view = ++lvugid;
28846 return;
28849 /* The discriminator column was added in dwarf4. Simplify the below
28850 by simply removing it if we're not supposed to output it. */
28851 if (dwarf_version < 4 && dwarf_strict)
28852 discriminator = 0;
28854 if (!debug_column_info)
28855 column = 0;
28857 file_num = maybe_emit_file (lookup_filename (filename));
28859 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28860 the debugger has used the second (possibly duplicate) line number
28861 at the beginning of the function to mark the end of the prologue.
28862 We could eliminate any other duplicates within the function. For
28863 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28864 that second line number entry. */
28865 /* Recall that this end-of-prologue indication is *not* the same thing
28866 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28867 to which the hook corresponds, follows the last insn that was
28868 emitted by gen_prologue. What we need is to precede the first insn
28869 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28870 insn that corresponds to something the user wrote. These may be
28871 very different locations once scheduling is enabled. */
28873 if (0 && file_num == table->file_num
28874 && line == table->line_num
28875 && column == table->column_num
28876 && discriminator == table->discrim_num
28877 && is_stmt == table->is_stmt)
28878 return;
28880 switch_to_section (current_function_section ());
28882 /* If requested, emit something human-readable. */
28883 if (flag_debug_asm)
28885 if (debug_column_info)
28886 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28887 filename, line, column);
28888 else
28889 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28890 filename, line);
28893 if (output_asm_line_debug_info ())
28895 /* Emit the .loc directive understood by GNU as. */
28896 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28897 file_num, line, is_stmt, discriminator */
28898 fputs ("\t.loc ", asm_out_file);
28899 fprint_ul (asm_out_file, file_num);
28900 putc (' ', asm_out_file);
28901 fprint_ul (asm_out_file, line);
28902 putc (' ', asm_out_file);
28903 fprint_ul (asm_out_file, column);
28905 if (is_stmt != table->is_stmt)
28907 #if HAVE_GAS_LOC_STMT
28908 fputs (" is_stmt ", asm_out_file);
28909 putc (is_stmt ? '1' : '0', asm_out_file);
28910 #endif
28912 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28914 gcc_assert (discriminator > 0);
28915 fputs (" discriminator ", asm_out_file);
28916 fprint_ul (asm_out_file, (unsigned long) discriminator);
28918 if (debug_variable_location_views)
28920 if (!RESETTING_VIEW_P (table->view))
28922 table->symviews_since_reset++;
28923 if (table->symviews_since_reset > symview_upper_bound)
28924 symview_upper_bound = table->symviews_since_reset;
28925 /* When we're using the assembler to compute view
28926 numbers, we output symbolic labels after "view" in
28927 .loc directives, and the assembler will set them for
28928 us, so that we can refer to the view numbers in
28929 location lists. The only exceptions are when we know
28930 a view will be zero: "-0" is a forced reset, used
28931 e.g. in the beginning of functions, whereas "0" tells
28932 the assembler to check that there was a PC change
28933 since the previous view, in a way that implicitly
28934 resets the next view. */
28935 fputs (" view ", asm_out_file);
28936 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28937 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28938 assemble_name (asm_out_file, label);
28939 table->view = ++lvugid;
28941 else
28943 table->symviews_since_reset = 0;
28944 if (FORCE_RESETTING_VIEW_P (table->view))
28945 fputs (" view -0", asm_out_file);
28946 else
28947 fputs (" view 0", asm_out_file);
28948 /* Mark the present view as a zero view. Earlier debug
28949 binds may have already added its id to loclists to be
28950 emitted later, so we can't reuse the id for something
28951 else. However, it's good to know whether a view is
28952 known to be zero, because then we may be able to
28953 optimize out locviews that are all zeros, so take
28954 note of it in zero_view_p. */
28955 if (!zero_view_p)
28956 zero_view_p = BITMAP_GGC_ALLOC ();
28957 bitmap_set_bit (zero_view_p, lvugid);
28958 table->view = ++lvugid;
28961 putc ('\n', asm_out_file);
28963 else
28965 unsigned int label_num = ++line_info_label_num;
28967 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28969 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28970 push_dw_line_info_entry (table, LI_adv_address, label_num);
28971 else
28972 push_dw_line_info_entry (table, LI_set_address, label_num);
28973 if (debug_variable_location_views)
28975 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28976 if (resetting)
28977 table->view = 0;
28979 if (flag_debug_asm)
28980 fprintf (asm_out_file, "\t%s view %s%d\n",
28981 ASM_COMMENT_START,
28982 resetting ? "-" : "",
28983 table->view);
28985 table->view++;
28987 if (file_num != table->file_num)
28988 push_dw_line_info_entry (table, LI_set_file, file_num);
28989 if (discriminator != table->discrim_num)
28990 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28991 if (is_stmt != table->is_stmt)
28992 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28993 push_dw_line_info_entry (table, LI_set_line, line);
28994 if (debug_column_info)
28995 push_dw_line_info_entry (table, LI_set_column, column);
28998 table->file_num = file_num;
28999 table->line_num = line;
29000 table->column_num = column;
29001 table->discrim_num = discriminator;
29002 table->is_stmt = is_stmt;
29003 table->in_use = true;
29006 /* Record a source file location for a DECL_IGNORED_P function. */
29008 static void
29009 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
29010 const char *filename)
29012 dw_fde_ref fde = cfun->fde;
29014 fde->ignored_debug = false;
29015 set_cur_line_info_table (current_function_section ());
29017 dwarf2out_source_line (line, column, filename, 0, true);
29020 /* Record the beginning of a new source file. */
29022 static void
29023 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
29025 #ifdef CODEVIEW_DEBUGGING_INFO
29026 if (codeview_debuginfo_p ())
29027 codeview_start_source_file (filename);
29028 #endif
29030 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29032 macinfo_entry e;
29033 e.code = DW_MACINFO_start_file;
29034 e.lineno = lineno;
29035 e.info = ggc_strdup (filename);
29036 vec_safe_push (macinfo_table, e);
29040 /* Record the end of a source file. */
29042 static void
29043 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
29045 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29047 macinfo_entry e;
29048 e.code = DW_MACINFO_end_file;
29049 e.lineno = lineno;
29050 e.info = NULL;
29051 vec_safe_push (macinfo_table, e);
29055 /* Called from debug_define in toplev.cc. The `buffer' parameter contains
29056 the tail part of the directive line, i.e. the part which is past the
29057 initial whitespace, #, whitespace, directive-name, whitespace part. */
29059 static void
29060 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
29061 const char *buffer ATTRIBUTE_UNUSED)
29063 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29065 macinfo_entry e;
29066 /* Insert a dummy first entry to be able to optimize the whole
29067 predefined macro block using DW_MACRO_import. */
29068 if (macinfo_table->is_empty () && lineno <= 1)
29070 e.code = 0;
29071 e.lineno = 0;
29072 e.info = NULL;
29073 vec_safe_push (macinfo_table, e);
29075 e.code = DW_MACINFO_define;
29076 e.lineno = lineno;
29077 e.info = ggc_strdup (buffer);
29078 vec_safe_push (macinfo_table, e);
29082 /* Called from debug_undef in toplev.cc. The `buffer' parameter contains
29083 the tail part of the directive line, i.e. the part which is past the
29084 initial whitespace, #, whitespace, directive-name, whitespace part. */
29086 static void
29087 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
29088 const char *buffer ATTRIBUTE_UNUSED)
29090 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29092 macinfo_entry e;
29093 /* Insert a dummy first entry to be able to optimize the whole
29094 predefined macro block using DW_MACRO_import. */
29095 if (macinfo_table->is_empty () && lineno <= 1)
29097 e.code = 0;
29098 e.lineno = 0;
29099 e.info = NULL;
29100 vec_safe_push (macinfo_table, e);
29102 e.code = DW_MACINFO_undef;
29103 e.lineno = lineno;
29104 e.info = ggc_strdup (buffer);
29105 vec_safe_push (macinfo_table, e);
29109 /* Helpers to manipulate hash table of CUs. */
29111 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
29113 static inline hashval_t hash (const macinfo_entry *);
29114 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
29117 inline hashval_t
29118 macinfo_entry_hasher::hash (const macinfo_entry *entry)
29120 return htab_hash_string (entry->info);
29123 inline bool
29124 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
29125 const macinfo_entry *entry2)
29127 return !strcmp (entry1->info, entry2->info);
29130 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
29132 /* Output a single .debug_macinfo entry. */
29134 static void
29135 output_macinfo_op (macinfo_entry *ref)
29137 int file_num;
29138 size_t len;
29139 struct indirect_string_node *node;
29140 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29141 struct dwarf_file_data *fd;
29143 switch (ref->code)
29145 case DW_MACINFO_start_file:
29146 fd = lookup_filename (ref->info);
29147 file_num = maybe_emit_file (fd);
29148 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
29149 dw2_asm_output_data_uleb128 (ref->lineno,
29150 "Included from line number "
29151 HOST_WIDE_INT_PRINT_UNSIGNED,
29152 ref->lineno);
29153 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
29154 break;
29155 case DW_MACINFO_end_file:
29156 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
29157 break;
29158 case DW_MACINFO_define:
29159 case DW_MACINFO_undef:
29160 len = strlen (ref->info) + 1;
29161 if ((!dwarf_strict || dwarf_version >= 5)
29162 && len > (size_t) dwarf_offset_size
29163 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29164 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29166 if (dwarf_split_debug_info)
29167 ref->code = ref->code == DW_MACINFO_define
29168 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
29169 else
29170 ref->code = ref->code == DW_MACINFO_define
29171 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
29172 output_macinfo_op (ref);
29173 return;
29175 dw2_asm_output_data (1, ref->code,
29176 ref->code == DW_MACINFO_define
29177 ? "Define macro" : "Undefine macro");
29178 dw2_asm_output_data_uleb128 (ref->lineno,
29179 "At line number "
29180 HOST_WIDE_INT_PRINT_UNSIGNED,
29181 ref->lineno);
29182 dw2_asm_output_nstring (ref->info, -1, "The macro");
29183 break;
29184 case DW_MACRO_define_strp:
29185 dw2_asm_output_data (1, ref->code, "Define macro strp");
29186 goto do_DW_MACRO_define_strpx;
29187 case DW_MACRO_undef_strp:
29188 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
29189 goto do_DW_MACRO_define_strpx;
29190 case DW_MACRO_define_strx:
29191 dw2_asm_output_data (1, ref->code, "Define macro strx");
29192 goto do_DW_MACRO_define_strpx;
29193 case DW_MACRO_undef_strx:
29194 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
29195 /* FALLTHRU */
29196 do_DW_MACRO_define_strpx:
29197 /* NB: dwarf2out_finish performs:
29198 1. save_macinfo_strings
29199 2. hash table traverse of index_string
29200 3. output_macinfo -> output_macinfo_op
29201 4. output_indirect_strings
29202 -> hash table traverse of output_index_string
29204 When output_macinfo_op is called, all index strings have been
29205 added to hash table by save_macinfo_strings and we can't pass
29206 INSERT to find_slot_with_hash which may expand hash table, even
29207 if no insertion is needed, and change hash table traverse order
29208 between index_string and output_index_string. */
29209 node = find_AT_string (ref->info, NO_INSERT);
29210 gcc_assert (node
29211 && (node->form == DW_FORM_strp
29212 || node->form == dwarf_FORM (DW_FORM_strx)));
29213 dw2_asm_output_data_uleb128 (ref->lineno,
29214 "At line number "
29215 HOST_WIDE_INT_PRINT_UNSIGNED,
29216 ref->lineno);
29217 if (node->form == DW_FORM_strp)
29219 gcc_assert (ref->code == DW_MACRO_define_strp
29220 || ref->code == DW_MACRO_undef_strp);
29221 dw2_asm_output_offset (dwarf_offset_size, node->label,
29222 debug_str_section, "The macro: \"%s\"",
29223 ref->info);
29225 else
29227 gcc_assert (ref->code == DW_MACRO_define_strx
29228 || ref->code == DW_MACRO_undef_strx);
29229 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
29230 ref->info);
29232 break;
29233 case DW_MACRO_import:
29234 dw2_asm_output_data (1, ref->code, "Import");
29235 ASM_GENERATE_INTERNAL_LABEL (label,
29236 DEBUG_MACRO_SECTION_LABEL,
29237 ref->lineno + macinfo_label_base);
29238 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
29239 break;
29240 default:
29241 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
29242 ASM_COMMENT_START, (unsigned long) ref->code);
29243 break;
29247 /* Attempt to make a sequence of define/undef macinfo ops shareable with
29248 other compilation unit .debug_macinfo sections. IDX is the first
29249 index of a define/undef, return the number of ops that should be
29250 emitted in a comdat .debug_macinfo section and emit
29251 a DW_MACRO_import entry referencing it.
29252 If the define/undef entry should be emitted normally, return 0. */
29254 static unsigned
29255 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
29256 macinfo_hash_type **macinfo_htab)
29258 macinfo_entry *first, *second, *cur, *inc;
29259 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
29260 unsigned char checksum[16];
29261 struct md5_ctx ctx;
29262 char *grp_name, *tail;
29263 const char *base;
29264 unsigned int i, count, encoded_filename_len, linebuf_len;
29265 macinfo_entry **slot;
29267 first = &(*macinfo_table)[idx];
29268 second = &(*macinfo_table)[idx + 1];
29270 /* Optimize only if there are at least two consecutive define/undef ops,
29271 and either all of them are before first DW_MACINFO_start_file
29272 with lineno {0,1} (i.e. predefined macro block), or all of them are
29273 in some included header file. */
29274 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
29275 return 0;
29276 if (vec_safe_is_empty (files))
29278 if (first->lineno > 1 || second->lineno > 1)
29279 return 0;
29281 else if (first->lineno == 0)
29282 return 0;
29284 /* Find the last define/undef entry that can be grouped together
29285 with first and at the same time compute md5 checksum of their
29286 codes, linenumbers and strings. */
29287 md5_init_ctx (&ctx);
29288 for (i = idx; macinfo_table->iterate (i, &cur); i++)
29289 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
29290 break;
29291 else if (vec_safe_is_empty (files) && cur->lineno > 1)
29292 break;
29293 else
29295 unsigned char code = cur->code;
29296 md5_process_bytes (&code, 1, &ctx);
29297 checksum_uleb128 (cur->lineno, &ctx);
29298 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
29300 md5_finish_ctx (&ctx, checksum);
29301 count = i - idx;
29303 /* From the containing include filename (if any) pick up just
29304 usable characters from its basename. */
29305 if (vec_safe_is_empty (files))
29306 base = "";
29307 else
29308 base = lbasename (files->last ().info);
29309 for (encoded_filename_len = 0, i = 0; base[i]; i++)
29310 if (ISIDNUM (base[i]) || base[i] == '.')
29311 encoded_filename_len++;
29312 /* Count . at the end. */
29313 if (encoded_filename_len)
29314 encoded_filename_len++;
29316 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
29317 linebuf_len = strlen (linebuf);
29319 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
29320 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
29321 + 16 * 2 + 1);
29322 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
29323 tail = grp_name + 4;
29324 if (encoded_filename_len)
29326 for (i = 0; base[i]; i++)
29327 if (ISIDNUM (base[i]) || base[i] == '.')
29328 *tail++ = base[i];
29329 *tail++ = '.';
29331 memcpy (tail, linebuf, linebuf_len);
29332 tail += linebuf_len;
29333 *tail++ = '.';
29334 for (i = 0; i < 16; i++)
29335 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
29337 /* Construct a macinfo_entry for DW_MACRO_import
29338 in the empty vector entry before the first define/undef. */
29339 inc = &(*macinfo_table)[idx - 1];
29340 inc->code = DW_MACRO_import;
29341 inc->lineno = 0;
29342 inc->info = ggc_strdup (grp_name);
29343 if (!*macinfo_htab)
29344 *macinfo_htab = new macinfo_hash_type (10);
29345 /* Avoid emitting duplicates. */
29346 slot = (*macinfo_htab)->find_slot (inc, INSERT);
29347 if (*slot != NULL)
29349 inc->code = 0;
29350 inc->info = NULL;
29351 /* If such an entry has been used before, just emit
29352 a DW_MACRO_import op. */
29353 inc = *slot;
29354 output_macinfo_op (inc);
29355 /* And clear all macinfo_entry in the range to avoid emitting them
29356 in the second pass. */
29357 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
29359 cur->code = 0;
29360 cur->info = NULL;
29363 else
29365 *slot = inc;
29366 inc->lineno = (*macinfo_htab)->elements ();
29367 output_macinfo_op (inc);
29369 return count;
29372 /* Save any strings needed by the macinfo table in the debug str
29373 table. All strings must be collected into the table by the time
29374 index_string is called. */
29376 static void
29377 save_macinfo_strings (void)
29379 unsigned len;
29380 unsigned i;
29381 macinfo_entry *ref;
29383 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
29385 switch (ref->code)
29387 /* Match the logic in output_macinfo_op to decide on
29388 indirect strings. */
29389 case DW_MACINFO_define:
29390 case DW_MACINFO_undef:
29391 len = strlen (ref->info) + 1;
29392 if ((!dwarf_strict || dwarf_version >= 5)
29393 && len > (unsigned) dwarf_offset_size
29394 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29395 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29396 set_indirect_string (find_AT_string (ref->info));
29397 break;
29398 case DW_MACINFO_start_file:
29399 /* -gsplit-dwarf -g3 will also output filename as indirect
29400 string. */
29401 if (!dwarf_split_debug_info)
29402 break;
29403 /* Fall through. */
29404 case DW_MACRO_define_strp:
29405 case DW_MACRO_undef_strp:
29406 case DW_MACRO_define_strx:
29407 case DW_MACRO_undef_strx:
29408 set_indirect_string (find_AT_string (ref->info));
29409 break;
29410 default:
29411 break;
29416 /* Output macinfo section(s). */
29418 static void
29419 output_macinfo (const char *debug_line_label, bool early_lto_debug)
29421 unsigned i;
29422 unsigned long length = vec_safe_length (macinfo_table);
29423 macinfo_entry *ref;
29424 vec<macinfo_entry, va_gc> *files = NULL;
29425 macinfo_hash_type *macinfo_htab = NULL;
29426 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29428 if (! length)
29429 return;
29431 /* output_macinfo* uses these interchangeably. */
29432 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
29433 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
29434 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
29435 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
29437 /* AIX Assembler inserts the length, so adjust the reference to match the
29438 offset expected by debuggers. */
29439 strcpy (dl_section_ref, debug_line_label);
29440 if (XCOFF_DEBUGGING_INFO)
29441 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29443 /* For .debug_macro emit the section header. */
29444 if (!dwarf_strict || dwarf_version >= 5)
29446 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29447 "DWARF macro version number");
29448 if (dwarf_offset_size == 8)
29449 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
29450 else
29451 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
29452 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
29453 debug_line_section, NULL);
29456 /* In the first loop, it emits the primary .debug_macinfo section
29457 and after each emitted op the macinfo_entry is cleared.
29458 If a longer range of define/undef ops can be optimized using
29459 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29460 the vector before the first define/undef in the range and the
29461 whole range of define/undef ops is not emitted and kept. */
29462 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29464 switch (ref->code)
29466 case DW_MACINFO_start_file:
29467 vec_safe_push (files, *ref);
29468 break;
29469 case DW_MACINFO_end_file:
29470 if (!vec_safe_is_empty (files))
29471 files->pop ();
29472 break;
29473 case DW_MACINFO_define:
29474 case DW_MACINFO_undef:
29475 if ((!dwarf_strict || dwarf_version >= 5)
29476 && !dwarf_split_debug_info
29477 && HAVE_COMDAT_GROUP
29478 && vec_safe_length (files) != 1
29479 && i > 0
29480 && i + 1 < length
29481 && (*macinfo_table)[i - 1].code == 0)
29483 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29484 if (count)
29486 i += count - 1;
29487 continue;
29490 break;
29491 case 0:
29492 /* A dummy entry may be inserted at the beginning to be able
29493 to optimize the whole block of predefined macros. */
29494 if (i == 0)
29495 continue;
29496 default:
29497 break;
29499 output_macinfo_op (ref);
29500 ref->info = NULL;
29501 ref->code = 0;
29504 if (!macinfo_htab)
29505 return;
29507 /* Save the number of transparent includes so we can adjust the
29508 label number for the fat LTO object DWARF. */
29509 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29511 delete macinfo_htab;
29512 macinfo_htab = NULL;
29514 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29515 terminate the current chain and switch to a new comdat .debug_macinfo
29516 section and emit the define/undef entries within it. */
29517 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29518 switch (ref->code)
29520 case 0:
29521 continue;
29522 case DW_MACRO_import:
29524 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29525 tree comdat_key = get_identifier (ref->info);
29526 /* Terminate the previous .debug_macinfo section. */
29527 dw2_asm_output_data (1, 0, "End compilation unit");
29528 targetm.asm_out.named_section (debug_macinfo_section_name,
29529 SECTION_DEBUG
29530 | SECTION_LINKONCE
29531 | (early_lto_debug
29532 ? SECTION_EXCLUDE : 0),
29533 comdat_key);
29534 ASM_GENERATE_INTERNAL_LABEL (label,
29535 DEBUG_MACRO_SECTION_LABEL,
29536 ref->lineno + macinfo_label_base);
29537 ASM_OUTPUT_LABEL (asm_out_file, label);
29538 ref->code = 0;
29539 ref->info = NULL;
29540 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29541 "DWARF macro version number");
29542 if (dwarf_offset_size == 8)
29543 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29544 else
29545 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29547 break;
29548 case DW_MACINFO_define:
29549 case DW_MACINFO_undef:
29550 output_macinfo_op (ref);
29551 ref->code = 0;
29552 ref->info = NULL;
29553 break;
29554 default:
29555 gcc_unreachable ();
29558 macinfo_label_base += macinfo_label_base_adj;
29561 /* As init_sections_and_labels may get called multiple times, have a
29562 generation count for labels. */
29563 static unsigned init_sections_and_labels_generation;
29565 /* Initialize the various sections and labels for dwarf output and prefix
29566 them with PREFIX if non-NULL. Returns the generation (zero based
29567 number of times function was called). */
29569 static unsigned
29570 init_sections_and_labels (bool early_lto_debug)
29572 if (early_lto_debug)
29574 if (!dwarf_split_debug_info)
29576 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29577 SECTION_DEBUG | SECTION_EXCLUDE,
29578 NULL);
29579 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29580 SECTION_DEBUG | SECTION_EXCLUDE,
29581 NULL);
29582 debug_macinfo_section_name
29583 = ((dwarf_strict && dwarf_version < 5)
29584 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29585 debug_macinfo_section = get_section (debug_macinfo_section_name,
29586 SECTION_DEBUG
29587 | SECTION_EXCLUDE, NULL);
29589 else
29591 /* ??? Which of the following do we need early? */
29592 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29593 SECTION_DEBUG | SECTION_EXCLUDE,
29594 NULL);
29595 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29596 SECTION_DEBUG | SECTION_EXCLUDE,
29597 NULL);
29598 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29599 SECTION_DEBUG
29600 | SECTION_EXCLUDE, NULL);
29601 debug_skeleton_abbrev_section
29602 = get_section (DEBUG_LTO_ABBREV_SECTION,
29603 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29604 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29605 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29606 init_sections_and_labels_generation);
29608 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29609 stay in the main .o, but the skeleton_line goes into the split
29610 off dwo. */
29611 debug_skeleton_line_section
29612 = get_section (DEBUG_LTO_LINE_SECTION,
29613 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29614 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29615 DEBUG_SKELETON_LINE_SECTION_LABEL,
29616 init_sections_and_labels_generation);
29617 debug_str_offsets_section
29618 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29619 SECTION_DEBUG | SECTION_EXCLUDE,
29620 NULL);
29621 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29622 DEBUG_SKELETON_INFO_SECTION_LABEL,
29623 init_sections_and_labels_generation);
29624 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29625 DEBUG_STR_DWO_SECTION_FLAGS,
29626 NULL);
29627 debug_macinfo_section_name
29628 = ((dwarf_strict && dwarf_version < 5)
29629 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29630 debug_macinfo_section = get_section (debug_macinfo_section_name,
29631 SECTION_DEBUG | SECTION_EXCLUDE,
29632 NULL);
29634 /* For macro info and the file table we have to refer to a
29635 debug_line section. */
29636 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29637 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29638 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29639 DEBUG_LINE_SECTION_LABEL,
29640 init_sections_and_labels_generation);
29642 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29643 DEBUG_STR_SECTION_FLAGS
29644 | SECTION_EXCLUDE, NULL);
29645 if (!dwarf_split_debug_info)
29646 debug_line_str_section
29647 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29648 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29650 else
29652 if (!dwarf_split_debug_info)
29654 debug_info_section = get_section (DEBUG_INFO_SECTION,
29655 SECTION_DEBUG, NULL);
29656 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29657 SECTION_DEBUG, NULL);
29658 debug_loc_section = get_section (dwarf_version >= 5
29659 ? DEBUG_LOCLISTS_SECTION
29660 : DEBUG_LOC_SECTION,
29661 SECTION_DEBUG, NULL);
29662 debug_macinfo_section_name
29663 = ((dwarf_strict && dwarf_version < 5)
29664 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29665 debug_macinfo_section = get_section (debug_macinfo_section_name,
29666 SECTION_DEBUG, NULL);
29668 else
29670 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29671 SECTION_DEBUG | SECTION_EXCLUDE,
29672 NULL);
29673 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29674 SECTION_DEBUG | SECTION_EXCLUDE,
29675 NULL);
29676 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29677 SECTION_DEBUG, NULL);
29678 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29679 SECTION_DEBUG, NULL);
29680 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29681 SECTION_DEBUG, NULL);
29682 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29683 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29684 init_sections_and_labels_generation);
29686 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29687 stay in the main .o, but the skeleton_line goes into the
29688 split off dwo. */
29689 debug_skeleton_line_section
29690 = get_section (DEBUG_DWO_LINE_SECTION,
29691 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29692 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29693 DEBUG_SKELETON_LINE_SECTION_LABEL,
29694 init_sections_and_labels_generation);
29695 debug_str_offsets_section
29696 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29697 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29698 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29699 DEBUG_SKELETON_INFO_SECTION_LABEL,
29700 init_sections_and_labels_generation);
29701 debug_loc_section = get_section (dwarf_version >= 5
29702 ? DEBUG_DWO_LOCLISTS_SECTION
29703 : DEBUG_DWO_LOC_SECTION,
29704 SECTION_DEBUG | SECTION_EXCLUDE,
29705 NULL);
29706 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29707 DEBUG_STR_DWO_SECTION_FLAGS,
29708 NULL);
29709 debug_macinfo_section_name
29710 = ((dwarf_strict && dwarf_version < 5)
29711 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29712 debug_macinfo_section = get_section (debug_macinfo_section_name,
29713 SECTION_DEBUG | SECTION_EXCLUDE,
29714 NULL);
29715 if (dwarf_version >= 5)
29716 debug_ranges_dwo_section
29717 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29718 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29720 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29721 SECTION_DEBUG, NULL);
29722 debug_line_section = get_section (DEBUG_LINE_SECTION,
29723 SECTION_DEBUG, NULL);
29724 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29725 SECTION_DEBUG, NULL);
29726 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29727 SECTION_DEBUG, NULL);
29728 debug_str_section = get_section (DEBUG_STR_SECTION,
29729 DEBUG_STR_SECTION_FLAGS, NULL);
29730 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29731 || asm_outputs_debug_line_str ())
29732 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29733 DEBUG_STR_SECTION_FLAGS, NULL);
29735 debug_ranges_section = get_section (dwarf_version >= 5
29736 ? DEBUG_RNGLISTS_SECTION
29737 : DEBUG_RANGES_SECTION,
29738 SECTION_DEBUG, NULL);
29739 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29740 SECTION_DEBUG, NULL);
29743 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29744 DEBUG_ABBREV_SECTION_LABEL,
29745 init_sections_and_labels_generation);
29746 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29747 DEBUG_INFO_SECTION_LABEL,
29748 init_sections_and_labels_generation);
29749 info_section_emitted = false;
29750 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29751 DEBUG_LINE_SECTION_LABEL,
29752 init_sections_and_labels_generation);
29753 /* There are up to 6 unique ranges labels per generation.
29754 See also output_rnglists. */
29755 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29756 DEBUG_RANGES_SECTION_LABEL,
29757 init_sections_and_labels_generation * 6);
29758 if (dwarf_version >= 5 && dwarf_split_debug_info)
29759 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29760 DEBUG_RANGES_SECTION_LABEL,
29761 1 + init_sections_and_labels_generation * 6);
29762 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29763 DEBUG_ADDR_SECTION_LABEL,
29764 init_sections_and_labels_generation);
29765 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29766 (dwarf_strict && dwarf_version < 5)
29767 ? DEBUG_MACINFO_SECTION_LABEL
29768 : DEBUG_MACRO_SECTION_LABEL,
29769 init_sections_and_labels_generation);
29770 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29771 init_sections_and_labels_generation);
29773 ++init_sections_and_labels_generation;
29774 return init_sections_and_labels_generation - 1;
29777 /* Set up for Dwarf output at the start of compilation. */
29779 static void
29780 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29782 /* Allocate the file_table. */
29783 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29785 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29786 /* Allocate the decl_die_table. */
29787 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29789 /* Allocate the decl_loc_table. */
29790 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29792 /* Allocate the cached_dw_loc_list_table. */
29793 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29795 /* Allocate the initial hunk of the abbrev_die_table. */
29796 vec_alloc (abbrev_die_table, 256);
29797 /* Zero-th entry is allocated, but unused. */
29798 abbrev_die_table->quick_push (NULL);
29800 /* Allocate the dwarf_proc_stack_usage_map. */
29801 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29803 /* Allocate the pubtypes and pubnames vectors. */
29804 vec_alloc (pubname_table, 32);
29805 vec_alloc (pubtype_table, 32);
29807 vec_alloc (incomplete_types, 64);
29809 vec_alloc (used_rtx_array, 32);
29811 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29812 vec_alloc (macinfo_table, 64);
29813 #endif
29815 /* If front-ends already registered a main translation unit but we were not
29816 ready to perform the association, do this now. */
29817 if (main_translation_unit != NULL_TREE)
29818 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29821 /* Called before compile () starts outputtting functions, variables
29822 and toplevel asms into assembly. */
29824 static void
29825 dwarf2out_assembly_start (void)
29827 if (text_section_line_info)
29828 return;
29830 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29831 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29832 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29833 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29834 COLD_TEXT_SECTION_LABEL, 0);
29835 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29837 switch_to_section (text_section);
29838 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29839 #endif
29841 /* Make sure the line number table for .text always exists. */
29842 text_section_line_info = new_line_info_table ();
29843 text_section_line_info->end_label = text_end_label;
29845 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29846 cur_line_info_table = text_section_line_info;
29847 #endif
29849 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29850 && dwarf2out_do_cfi_asm ()
29851 && !dwarf2out_do_eh_frame ())
29852 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29854 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29855 if (output_asm_line_debug_info () && dwarf_version >= 5)
29857 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29858 tell it the comp_dir and main file name for the zero entry
29859 line table. */
29860 const char *comp_dir, *filename0;
29862 comp_dir = comp_dir_string ();
29863 if (comp_dir == NULL)
29864 comp_dir = "";
29866 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29867 if (filename0 == NULL)
29868 filename0 = "";
29870 fprintf (asm_out_file, "\t.file 0 ");
29871 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29872 fputc (' ', asm_out_file);
29873 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29874 fputc ('\n', asm_out_file);
29876 else
29877 #endif
29878 /* Work around for PR101575: output a dummy .file directive. */
29879 if (!last_emitted_file && dwarf_debuginfo_p ()
29880 && debug_info_level >= DINFO_LEVEL_TERSE)
29882 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29884 if (filename0 == NULL)
29885 filename0 = "<dummy>";
29886 maybe_emit_file (lookup_filename (filename0));
29890 /* A helper function for dwarf2out_finish called through
29891 htab_traverse. Assign a string its index. All strings must be
29892 collected into the table by the time index_string is called,
29893 because the indexing code relies on htab_traverse to traverse nodes
29894 in the same order for each run. */
29897 index_string (indirect_string_node **h, unsigned int *index)
29899 indirect_string_node *node = *h;
29901 find_string_form (node);
29902 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29904 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29905 node->index = *index;
29906 *index += 1;
29908 return 1;
29911 /* A helper function for output_indirect_strings called through
29912 htab_traverse. Output the offset to a string and update the
29913 current offset. */
29916 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29918 indirect_string_node *node = *h;
29920 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29922 /* Assert that this node has been assigned an index. */
29923 gcc_assert (node->index != NO_INDEX_ASSIGNED
29924 && node->index != NOT_INDEXED);
29925 dw2_asm_output_data (dwarf_offset_size, *offset,
29926 "indexed string 0x%x: %s", node->index, node->str);
29927 *offset += strlen (node->str) + 1;
29929 return 1;
29932 /* A helper function for dwarf2out_finish called through
29933 htab_traverse. Output the indexed string. */
29936 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29938 struct indirect_string_node *node = *h;
29940 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29942 /* Assert that the strings are output in the same order as their
29943 indexes were assigned. */
29944 gcc_assert (*cur_idx == node->index);
29945 assemble_string (node->str, strlen (node->str) + 1);
29946 *cur_idx += 1;
29948 return 1;
29951 /* A helper function for output_indirect_strings. Counts the number
29952 of index strings offsets. Must match the logic of the functions
29953 output_index_string[_offsets] above. */
29955 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29957 struct indirect_string_node *node = *h;
29959 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29960 *last_idx += 1;
29961 return 1;
29964 /* A helper function for dwarf2out_finish called through
29965 htab_traverse. Emit one queued .debug_str string. */
29968 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29970 struct indirect_string_node *node = *h;
29972 node->form = find_string_form (node);
29973 if (node->form == form && node->refcount > 0)
29975 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29976 assemble_string (node->str, strlen (node->str) + 1);
29979 return 1;
29982 /* Output the indexed string table. */
29984 static void
29985 output_indirect_strings (void)
29987 switch_to_section (debug_str_section);
29988 if (!dwarf_split_debug_info)
29989 debug_str_hash->traverse<enum dwarf_form,
29990 output_indirect_string> (DW_FORM_strp);
29991 else
29993 unsigned int offset = 0;
29994 unsigned int cur_idx = 0;
29996 if (skeleton_debug_str_hash)
29997 skeleton_debug_str_hash->traverse<enum dwarf_form,
29998 output_indirect_string> (DW_FORM_strp);
30000 switch_to_section (debug_str_offsets_section);
30001 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
30002 header. Note that we don't need to generate a label to the
30003 actual index table following the header here, because this is
30004 for the split dwarf case only. In an .dwo file there is only
30005 one string offsets table (and one debug info section). But
30006 if we would start using string offset tables for the main (or
30007 skeleton) unit, then we have to add a DW_AT_str_offsets_base
30008 pointing to the actual index after the header. Split dwarf
30009 units will never have a string offsets base attribute. When
30010 a split unit is moved into a .dwp file the string offsets can
30011 be found through the .debug_cu_index section table. */
30012 if (dwarf_version >= 5)
30014 unsigned int last_idx = 0;
30015 unsigned long str_offsets_length;
30017 debug_str_hash->traverse_noresize
30018 <unsigned int *, count_index_strings> (&last_idx);
30019 str_offsets_length = last_idx * dwarf_offset_size + 4;
30020 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
30021 dw2_asm_output_data (4, 0xffffffff,
30022 "Escape value for 64-bit DWARF extension");
30023 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
30024 "Length of string offsets unit");
30025 dw2_asm_output_data (2, 5, "DWARF string offsets version");
30026 dw2_asm_output_data (2, 0, "Header zero padding");
30028 debug_str_hash->traverse_noresize
30029 <unsigned int *, output_index_string_offset> (&offset);
30030 switch_to_section (debug_str_dwo_section);
30031 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
30032 (&cur_idx);
30036 /* Callback for htab_traverse to assign an index to an entry in the
30037 table, and to write that entry to the .debug_addr section. */
30040 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
30042 addr_table_entry *entry = *slot;
30044 if (entry->refcount == 0)
30046 gcc_assert (entry->index == NO_INDEX_ASSIGNED
30047 || entry->index == NOT_INDEXED);
30048 return 1;
30051 gcc_assert (entry->index == *cur_index);
30052 (*cur_index)++;
30054 switch (entry->kind)
30056 case ate_kind_rtx:
30057 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
30058 "0x%x", entry->index);
30059 break;
30060 case ate_kind_rtx_dtprel:
30061 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
30062 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
30063 DWARF2_ADDR_SIZE,
30064 entry->addr.rtl);
30065 fputc ('\n', asm_out_file);
30066 break;
30067 case ate_kind_label:
30068 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
30069 "0x%x", entry->index);
30070 break;
30071 default:
30072 gcc_unreachable ();
30074 return 1;
30077 /* A helper function for dwarf2out_finish. Counts the number
30078 of indexed addresses. Must match the logic of the functions
30079 output_addr_table_entry above. */
30081 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
30083 addr_table_entry *entry = *slot;
30085 if (entry->refcount > 0)
30086 *last_idx += 1;
30087 return 1;
30090 /* Produce the .debug_addr section. */
30092 static void
30093 output_addr_table (void)
30095 unsigned int index = 0;
30096 if (addr_index_table == NULL || addr_index_table->size () == 0)
30097 return;
30099 switch_to_section (debug_addr_section);
30100 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
30101 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
30102 before DWARF5, didn't have a header for .debug_addr units.
30103 DWARF5 specifies a small header when address tables are used. */
30104 if (dwarf_version >= 5)
30106 unsigned int last_idx = 0;
30107 unsigned long addrs_length;
30109 addr_index_table->traverse_noresize
30110 <unsigned int *, count_index_addrs> (&last_idx);
30111 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
30113 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
30114 dw2_asm_output_data (4, 0xffffffff,
30115 "Escape value for 64-bit DWARF extension");
30116 dw2_asm_output_data (dwarf_offset_size, addrs_length,
30117 "Length of Address Unit");
30118 dw2_asm_output_data (2, 5, "DWARF addr version");
30119 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
30120 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
30122 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30124 addr_index_table
30125 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
30128 #if ENABLE_ASSERT_CHECKING
30129 /* Verify that all marks are clear. */
30131 static void
30132 verify_marks_clear (dw_die_ref die)
30134 dw_die_ref c;
30136 gcc_assert (! die->die_mark);
30137 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
30139 #endif /* ENABLE_ASSERT_CHECKING */
30141 /* Clear the marks for a die and its children.
30142 Be cool if the mark isn't set. */
30144 static void
30145 prune_unmark_dies (dw_die_ref die)
30147 dw_die_ref c;
30149 if (die->die_mark)
30150 die->die_mark = 0;
30151 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
30154 /* Given LOC that is referenced by a DIE we're marking as used, find all
30155 referenced DWARF procedures it references and mark them as used. */
30157 static void
30158 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
30160 for (; loc != NULL; loc = loc->dw_loc_next)
30161 switch (loc->dw_loc_opc)
30163 case DW_OP_implicit_pointer:
30164 case DW_OP_convert:
30165 case DW_OP_reinterpret:
30166 case DW_OP_GNU_implicit_pointer:
30167 case DW_OP_GNU_convert:
30168 case DW_OP_GNU_reinterpret:
30169 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
30170 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
30171 break;
30172 case DW_OP_GNU_variable_value:
30173 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30175 dw_die_ref ref
30176 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30177 if (ref == NULL)
30178 break;
30179 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30180 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30181 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30183 /* FALLTHRU */
30184 case DW_OP_call2:
30185 case DW_OP_call4:
30186 case DW_OP_call_ref:
30187 case DW_OP_const_type:
30188 case DW_OP_GNU_const_type:
30189 case DW_OP_GNU_parameter_ref:
30190 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
30191 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
30192 break;
30193 case DW_OP_regval_type:
30194 case DW_OP_deref_type:
30195 case DW_OP_GNU_regval_type:
30196 case DW_OP_GNU_deref_type:
30197 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
30198 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
30199 break;
30200 case DW_OP_entry_value:
30201 case DW_OP_GNU_entry_value:
30202 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
30203 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
30204 break;
30205 default:
30206 break;
30210 /* Given DIE that we're marking as used, find any other dies
30211 it references as attributes and mark them as used. */
30213 static void
30214 prune_unused_types_walk_attribs (dw_die_ref die)
30216 dw_attr_node *a;
30217 unsigned ix;
30219 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30221 switch (AT_class (a))
30223 /* Make sure DWARF procedures referenced by location descriptions will
30224 get emitted. */
30225 case dw_val_class_loc:
30226 prune_unused_types_walk_loc_descr (AT_loc (a));
30227 break;
30228 case dw_val_class_loc_list:
30229 for (dw_loc_list_ref list = AT_loc_list (a);
30230 list != NULL;
30231 list = list->dw_loc_next)
30232 prune_unused_types_walk_loc_descr (list->expr);
30233 break;
30235 case dw_val_class_view_list:
30236 /* This points to a loc_list in another attribute, so it's
30237 already covered. */
30238 break;
30240 case dw_val_class_die_ref:
30241 /* A reference to another DIE.
30242 Make sure that it will get emitted.
30243 If it was broken out into a comdat group, don't follow it. */
30244 if (! AT_ref (a)->comdat_type_p
30245 || a->dw_attr == DW_AT_specification)
30246 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
30247 break;
30249 case dw_val_class_str:
30250 /* Set the string's refcount to 0 so that prune_unused_types_mark
30251 accounts properly for it. */
30252 a->dw_attr_val.v.val_str->refcount = 0;
30253 break;
30255 default:
30256 break;
30261 /* Mark the generic parameters and arguments children DIEs of DIE. */
30263 static void
30264 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
30266 dw_die_ref c;
30268 if (die == NULL || die->die_child == NULL)
30269 return;
30270 c = die->die_child;
30273 if (is_template_parameter (c))
30274 prune_unused_types_mark (c, 1);
30275 c = c->die_sib;
30276 } while (c && c != die->die_child);
30279 /* Mark DIE as being used. If DOKIDS is true, then walk down
30280 to DIE's children. */
30282 static void
30283 prune_unused_types_mark (dw_die_ref die, int dokids)
30285 dw_die_ref c;
30287 if (die->die_mark == 0)
30289 /* We haven't done this node yet. Mark it as used. */
30290 die->die_mark = 1;
30291 /* If this is the DIE of a generic type instantiation,
30292 mark the children DIEs that describe its generic parms and
30293 args. */
30294 prune_unused_types_mark_generic_parms_dies (die);
30296 /* We also have to mark its parents as used.
30297 (But we don't want to mark our parent's kids due to this,
30298 unless it is a class.) */
30299 if (die->die_parent)
30300 prune_unused_types_mark (die->die_parent,
30301 class_scope_p (die->die_parent));
30303 /* Mark any referenced nodes. */
30304 prune_unused_types_walk_attribs (die);
30306 /* If this node is a specification,
30307 also mark the definition, if it exists. */
30308 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
30309 prune_unused_types_mark (die->die_definition, 1);
30312 if (dokids && die->die_mark != 2)
30314 /* We need to walk the children, but haven't done so yet.
30315 Remember that we've walked the kids. */
30316 die->die_mark = 2;
30318 /* If this is an array type, we need to make sure our
30319 kids get marked, even if they're types. If we're
30320 breaking out types into comdat sections, do this
30321 for all type definitions. */
30322 if (die->die_tag == DW_TAG_array_type
30323 || (use_debug_types
30324 && is_type_die (die) && ! is_declaration_die (die)))
30325 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
30326 else
30327 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30331 /* For local classes, look if any static member functions were emitted
30332 and if so, mark them. */
30334 static void
30335 prune_unused_types_walk_local_classes (dw_die_ref die)
30337 dw_die_ref c;
30339 if (die->die_mark == 2)
30340 return;
30342 switch (die->die_tag)
30344 case DW_TAG_structure_type:
30345 case DW_TAG_union_type:
30346 case DW_TAG_class_type:
30347 case DW_TAG_interface_type:
30348 break;
30350 case DW_TAG_subprogram:
30351 if (!get_AT_flag (die, DW_AT_declaration)
30352 || die->die_definition != NULL)
30353 prune_unused_types_mark (die, 1);
30354 return;
30356 default:
30357 return;
30360 /* Mark children. */
30361 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
30364 /* Walk the tree DIE and mark types that we actually use. */
30366 static void
30367 prune_unused_types_walk (dw_die_ref die)
30369 dw_die_ref c;
30371 /* Don't do anything if this node is already marked and
30372 children have been marked as well. */
30373 if (die->die_mark == 2)
30374 return;
30376 switch (die->die_tag)
30378 case DW_TAG_structure_type:
30379 case DW_TAG_union_type:
30380 case DW_TAG_class_type:
30381 case DW_TAG_interface_type:
30382 if (die->die_perennial_p)
30383 break;
30385 for (c = die->die_parent; c; c = c->die_parent)
30386 if (c->die_tag == DW_TAG_subprogram)
30387 break;
30389 /* Finding used static member functions inside of classes
30390 is needed just for local classes, because for other classes
30391 static member function DIEs with DW_AT_specification
30392 are emitted outside of the DW_TAG_*_type. If we ever change
30393 it, we'd need to call this even for non-local classes. */
30394 if (c)
30395 prune_unused_types_walk_local_classes (die);
30397 /* It's a type node --- don't mark it. */
30398 return;
30400 case DW_TAG_const_type:
30401 case DW_TAG_packed_type:
30402 case DW_TAG_pointer_type:
30403 case DW_TAG_reference_type:
30404 case DW_TAG_rvalue_reference_type:
30405 case DW_TAG_volatile_type:
30406 case DW_TAG_restrict_type:
30407 case DW_TAG_shared_type:
30408 case DW_TAG_atomic_type:
30409 case DW_TAG_immutable_type:
30410 case DW_TAG_typedef:
30411 case DW_TAG_array_type:
30412 case DW_TAG_coarray_type:
30413 case DW_TAG_friend:
30414 case DW_TAG_enumeration_type:
30415 case DW_TAG_subroutine_type:
30416 case DW_TAG_string_type:
30417 case DW_TAG_set_type:
30418 case DW_TAG_subrange_type:
30419 case DW_TAG_ptr_to_member_type:
30420 case DW_TAG_file_type:
30421 case DW_TAG_unspecified_type:
30422 case DW_TAG_dynamic_type:
30423 /* Type nodes are useful only when other DIEs reference them --- don't
30424 mark them. */
30425 /* FALLTHROUGH */
30427 case DW_TAG_dwarf_procedure:
30428 /* Likewise for DWARF procedures. */
30430 if (die->die_perennial_p)
30431 break;
30433 return;
30435 case DW_TAG_variable:
30436 if (flag_debug_only_used_symbols)
30438 if (die->die_perennial_p)
30439 break;
30441 /* For static data members, the declaration in the class is supposed
30442 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
30443 DWARF5. DW_TAG_member will be marked, so mark even such
30444 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
30445 attribute. */
30446 if (dwarf_version >= 5
30447 && class_scope_p (die->die_parent)
30448 && get_AT (die, DW_AT_const_value))
30449 break;
30451 /* premark_used_variables marks external variables --- don't mark
30452 them here. But function-local externals are always considered
30453 used. */
30454 if (get_AT (die, DW_AT_external))
30456 for (c = die->die_parent; c; c = c->die_parent)
30457 if (c->die_tag == DW_TAG_subprogram)
30458 break;
30459 if (!c)
30460 return;
30463 /* FALLTHROUGH */
30465 default:
30466 /* Mark everything else. */
30467 break;
30470 if (die->die_mark == 0)
30472 die->die_mark = 1;
30474 /* Now, mark any dies referenced from here. */
30475 prune_unused_types_walk_attribs (die);
30478 die->die_mark = 2;
30480 /* Mark children. */
30481 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30484 /* Increment the string counts on strings referred to from DIE's
30485 attributes. */
30487 static void
30488 prune_unused_types_update_strings (dw_die_ref die)
30490 dw_attr_node *a;
30491 unsigned ix;
30493 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30494 if (AT_class (a) == dw_val_class_str)
30496 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30497 s->refcount++;
30498 /* Avoid unnecessarily putting strings that are used less than
30499 twice in the hash table. */
30500 if (s->form != DW_FORM_line_strp
30501 && (s->refcount
30502 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30504 indirect_string_node **slot
30505 = debug_str_hash->find_slot_with_hash (s->str,
30506 htab_hash_string (s->str),
30507 INSERT);
30508 gcc_assert (*slot == NULL);
30509 *slot = s;
30514 /* Mark DIE and its children as removed. */
30516 static void
30517 mark_removed (dw_die_ref die)
30519 dw_die_ref c;
30520 die->removed = true;
30521 FOR_EACH_CHILD (die, c, mark_removed (c));
30524 /* Remove from the tree DIE any dies that aren't marked. */
30526 static void
30527 prune_unused_types_prune (dw_die_ref die)
30529 dw_die_ref c;
30531 gcc_assert (die->die_mark);
30532 prune_unused_types_update_strings (die);
30534 if (! die->die_child)
30535 return;
30537 c = die->die_child;
30538 do {
30539 dw_die_ref prev = c, next;
30540 for (c = c->die_sib; ! c->die_mark; c = next)
30541 if (c == die->die_child)
30543 /* No marked children between 'prev' and the end of the list. */
30544 if (prev == c)
30545 /* No marked children at all. */
30546 die->die_child = NULL;
30547 else
30549 prev->die_sib = c->die_sib;
30550 die->die_child = prev;
30552 c->die_sib = NULL;
30553 mark_removed (c);
30554 return;
30556 else
30558 next = c->die_sib;
30559 c->die_sib = NULL;
30560 mark_removed (c);
30563 if (c != prev->die_sib)
30564 prev->die_sib = c;
30565 prune_unused_types_prune (c);
30566 } while (c != die->die_child);
30569 /* Remove dies representing declarations that we never use. */
30571 static void
30572 prune_unused_types (void)
30574 unsigned int i;
30575 limbo_die_node *node;
30576 comdat_type_node *ctnode;
30577 pubname_entry *pub;
30578 dw_die_ref base_type;
30580 #if ENABLE_ASSERT_CHECKING
30581 /* All the marks should already be clear. */
30582 verify_marks_clear (comp_unit_die ());
30583 for (node = limbo_die_list; node; node = node->next)
30584 verify_marks_clear (node->die);
30585 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30586 verify_marks_clear (ctnode->root_die);
30587 #endif /* ENABLE_ASSERT_CHECKING */
30589 /* Mark types that are used in global variables. */
30590 premark_types_used_by_global_vars ();
30592 /* Mark variables used in the symtab. */
30593 if (flag_debug_only_used_symbols)
30594 premark_used_variables ();
30596 /* Set the mark on nodes that are actually used. */
30597 prune_unused_types_walk (comp_unit_die ());
30598 for (node = limbo_die_list; node; node = node->next)
30599 prune_unused_types_walk (node->die);
30600 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30602 prune_unused_types_walk (ctnode->root_die);
30603 prune_unused_types_mark (ctnode->type_die, 1);
30606 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30607 are unusual in that they are pubnames that are the children of pubtypes.
30608 They should only be marked via their parent DW_TAG_enumeration_type die,
30609 not as roots in themselves. */
30610 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30611 if (pub->die->die_tag != DW_TAG_enumerator)
30612 prune_unused_types_mark (pub->die, 1);
30613 for (i = 0; base_types.iterate (i, &base_type); i++)
30614 prune_unused_types_mark (base_type, 1);
30616 /* Also set the mark on nodes that could be referenced by
30617 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30618 by DW_TAG_inlined_subroutine origins. */
30619 cgraph_node *cnode;
30620 FOR_EACH_FUNCTION (cnode)
30621 if (cnode->referred_to_p (false))
30623 dw_die_ref die = lookup_decl_die (cnode->decl);
30624 if (die == NULL || die->die_mark)
30625 continue;
30626 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30627 if (e->caller != cnode)
30629 prune_unused_types_mark (die, 1);
30630 break;
30634 if (debug_str_hash)
30635 debug_str_hash->empty ();
30636 if (skeleton_debug_str_hash)
30637 skeleton_debug_str_hash->empty ();
30638 prune_unused_types_prune (comp_unit_die ());
30639 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30641 node = *pnode;
30642 if (!node->die->die_mark)
30643 *pnode = node->next;
30644 else
30646 prune_unused_types_prune (node->die);
30647 pnode = &node->next;
30650 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30651 prune_unused_types_prune (ctnode->root_die);
30653 /* Leave the marks clear. */
30654 prune_unmark_dies (comp_unit_die ());
30655 for (node = limbo_die_list; node; node = node->next)
30656 prune_unmark_dies (node->die);
30657 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30658 prune_unmark_dies (ctnode->root_die);
30661 /* Helpers to manipulate hash table of comdat type units. */
30663 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30665 static inline hashval_t hash (const comdat_type_node *);
30666 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30669 inline hashval_t
30670 comdat_type_hasher::hash (const comdat_type_node *type_node)
30672 hashval_t h;
30673 memcpy (&h, type_node->signature, sizeof (h));
30674 return h;
30677 inline bool
30678 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30679 const comdat_type_node *type_node_2)
30681 return (! memcmp (type_node_1->signature, type_node_2->signature,
30682 DWARF_TYPE_SIGNATURE_SIZE));
30685 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30686 to the location it would have been added, should we know its
30687 DECL_ASSEMBLER_NAME when we added other attributes. This will
30688 probably improve compactness of debug info, removing equivalent
30689 abbrevs, and hide any differences caused by deferring the
30690 computation of the assembler name, triggered by e.g. PCH. */
30692 static inline void
30693 move_linkage_attr (dw_die_ref die)
30695 unsigned ix = vec_safe_length (die->die_attr);
30696 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30698 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30699 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30701 while (--ix > 0)
30703 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30705 if (prev->dw_attr == DW_AT_decl_line
30706 || prev->dw_attr == DW_AT_decl_column
30707 || prev->dw_attr == DW_AT_name)
30708 break;
30711 if (ix != vec_safe_length (die->die_attr) - 1)
30713 die->die_attr->pop ();
30714 die->die_attr->quick_insert (ix, linkage);
30718 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30719 referenced from typed stack ops and count how often they are used. */
30721 static void
30722 mark_base_types (dw_loc_descr_ref loc)
30724 dw_die_ref base_type = NULL;
30726 for (; loc; loc = loc->dw_loc_next)
30728 switch (loc->dw_loc_opc)
30730 case DW_OP_regval_type:
30731 case DW_OP_deref_type:
30732 case DW_OP_GNU_regval_type:
30733 case DW_OP_GNU_deref_type:
30734 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30735 break;
30736 case DW_OP_convert:
30737 case DW_OP_reinterpret:
30738 case DW_OP_GNU_convert:
30739 case DW_OP_GNU_reinterpret:
30740 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30741 continue;
30742 /* FALLTHRU */
30743 case DW_OP_const_type:
30744 case DW_OP_GNU_const_type:
30745 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30746 break;
30747 case DW_OP_entry_value:
30748 case DW_OP_GNU_entry_value:
30749 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30750 continue;
30751 default:
30752 continue;
30754 gcc_assert (base_type->die_parent == comp_unit_die ());
30755 if (base_type->die_mark)
30756 base_type->die_mark++;
30757 else
30759 base_types.safe_push (base_type);
30760 base_type->die_mark = 1;
30765 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30766 referenced from typed stack ops and count how often they are used. */
30768 static void
30769 mark_base_types (dw_die_ref die)
30771 dw_die_ref c;
30772 dw_attr_node *a;
30773 dw_loc_list_ref *curr;
30774 unsigned ix;
30776 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30777 switch (AT_class (a))
30779 case dw_val_class_loc_list:
30780 curr = AT_loc_list_ptr (a);
30781 while (*curr)
30783 mark_base_types ((*curr)->expr);
30784 curr = &(*curr)->dw_loc_next;
30786 break;
30788 case dw_val_class_loc:
30789 mark_base_types (AT_loc (a));
30790 break;
30792 default:
30793 break;
30796 FOR_EACH_CHILD (die, c, mark_base_types (c));
30799 /* Comparison function for sorting marked base types. */
30801 static int
30802 base_type_cmp (const void *x, const void *y)
30804 dw_die_ref dx = *(const dw_die_ref *) x;
30805 dw_die_ref dy = *(const dw_die_ref *) y;
30806 unsigned int byte_size1, byte_size2;
30807 unsigned int encoding1, encoding2;
30808 unsigned int align1, align2;
30809 if (dx->die_mark > dy->die_mark)
30810 return -1;
30811 if (dx->die_mark < dy->die_mark)
30812 return 1;
30813 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30814 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30815 if (byte_size1 < byte_size2)
30816 return 1;
30817 if (byte_size1 > byte_size2)
30818 return -1;
30819 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30820 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30821 if (encoding1 < encoding2)
30822 return 1;
30823 if (encoding1 > encoding2)
30824 return -1;
30825 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30826 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30827 if (align1 < align2)
30828 return 1;
30829 if (align1 > align2)
30830 return -1;
30831 return 0;
30834 /* Move base types marked by mark_base_types as early as possible
30835 in the CU, sorted by decreasing usage count both to make the
30836 uleb128 references as small as possible and to make sure they
30837 will have die_offset already computed by calc_die_sizes when
30838 sizes of typed stack loc ops is computed. */
30840 static void
30841 move_marked_base_types (void)
30843 unsigned int i;
30844 dw_die_ref base_type, die, c;
30846 if (base_types.is_empty ())
30847 return;
30849 /* Sort by decreasing usage count, they will be added again in that
30850 order later on. */
30851 base_types.qsort (base_type_cmp);
30852 die = comp_unit_die ();
30853 c = die->die_child;
30856 dw_die_ref prev = c;
30857 c = c->die_sib;
30858 while (c->die_mark)
30860 remove_child_with_prev (c, prev);
30861 /* As base types got marked, there must be at least
30862 one node other than DW_TAG_base_type. */
30863 gcc_assert (die->die_child != NULL);
30864 c = prev->die_sib;
30867 while (c != die->die_child);
30868 gcc_assert (die->die_child);
30869 c = die->die_child;
30870 for (i = 0; base_types.iterate (i, &base_type); i++)
30872 base_type->die_mark = 0;
30873 base_type->die_sib = c->die_sib;
30874 c->die_sib = base_type;
30875 c = base_type;
30879 /* Helper function for resolve_addr, attempt to resolve
30880 one CONST_STRING, return true if successful. Similarly verify that
30881 SYMBOL_REFs refer to variables emitted in the current CU. */
30883 static bool
30884 resolve_one_addr (rtx *addr)
30886 rtx rtl = *addr;
30888 if (GET_CODE (rtl) == CONST_STRING)
30890 size_t len = strlen (XSTR (rtl, 0)) + 1;
30891 tree t = build_string (len, XSTR (rtl, 0));
30892 tree tlen = size_int (len - 1);
30893 TREE_TYPE (t)
30894 = build_array_type (char_type_node, build_index_type (tlen));
30895 rtl = lookup_constant_def (t);
30896 if (!rtl || !MEM_P (rtl))
30897 return false;
30898 rtl = XEXP (rtl, 0);
30899 if (GET_CODE (rtl) == SYMBOL_REF
30900 && SYMBOL_REF_DECL (rtl)
30901 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30902 return false;
30903 vec_safe_push (used_rtx_array, rtl);
30904 *addr = rtl;
30905 return true;
30908 if (GET_CODE (rtl) == SYMBOL_REF
30909 && SYMBOL_REF_DECL (rtl))
30911 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30913 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30914 return false;
30916 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30917 return false;
30920 if (GET_CODE (rtl) == CONST)
30922 subrtx_ptr_iterator::array_type array;
30923 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30924 if (!resolve_one_addr (*iter))
30925 return false;
30928 return true;
30931 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30932 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30933 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30935 static rtx
30936 string_cst_pool_decl (tree t)
30938 rtx rtl = output_constant_def (t, 1);
30939 unsigned char *array;
30940 dw_loc_descr_ref l;
30941 tree decl;
30942 size_t len;
30943 dw_die_ref ref;
30945 if (!rtl || !MEM_P (rtl))
30946 return NULL_RTX;
30947 rtl = XEXP (rtl, 0);
30948 if (GET_CODE (rtl) != SYMBOL_REF
30949 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30950 return NULL_RTX;
30952 decl = SYMBOL_REF_DECL (rtl);
30953 if (!lookup_decl_die (decl))
30955 len = TREE_STRING_LENGTH (t);
30956 vec_safe_push (used_rtx_array, rtl);
30957 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30958 array = ggc_vec_alloc<unsigned char> (len);
30959 memcpy (array, TREE_STRING_POINTER (t), len);
30960 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30961 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30962 l->dw_loc_oprnd2.v.val_vec.length = len;
30963 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30964 l->dw_loc_oprnd2.v.val_vec.array = array;
30965 add_AT_loc (ref, DW_AT_location, l);
30966 equate_decl_number_to_die (decl, ref);
30968 return rtl;
30971 /* Helper function of resolve_addr_in_expr. LOC is
30972 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30973 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30974 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30975 with DW_OP_implicit_pointer if possible
30976 and return true, if unsuccessful, return false. */
30978 static bool
30979 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30981 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30982 HOST_WIDE_INT offset = 0;
30983 dw_die_ref ref = NULL;
30984 tree decl;
30986 if (GET_CODE (rtl) == CONST
30987 && GET_CODE (XEXP (rtl, 0)) == PLUS
30988 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30990 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30991 rtl = XEXP (XEXP (rtl, 0), 0);
30993 if (GET_CODE (rtl) == CONST_STRING)
30995 size_t len = strlen (XSTR (rtl, 0)) + 1;
30996 tree t = build_string (len, XSTR (rtl, 0));
30997 tree tlen = size_int (len - 1);
30999 TREE_TYPE (t)
31000 = build_array_type (char_type_node, build_index_type (tlen));
31001 rtl = string_cst_pool_decl (t);
31002 if (!rtl)
31003 return false;
31005 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
31007 decl = SYMBOL_REF_DECL (rtl);
31008 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
31010 ref = lookup_decl_die (decl);
31011 if (ref && (get_AT (ref, DW_AT_location)
31012 || get_AT (ref, DW_AT_const_value)))
31014 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
31015 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31016 loc->dw_loc_oprnd1.val_entry = NULL;
31017 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31018 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31019 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
31020 loc->dw_loc_oprnd2.v.val_int = offset;
31021 return true;
31025 return false;
31028 /* Helper function for resolve_addr, handle one location
31029 expression, return false if at least one CONST_STRING or SYMBOL_REF in
31030 the location list couldn't be resolved. */
31032 static bool
31033 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31035 dw_loc_descr_ref keep = NULL;
31036 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
31037 switch (loc->dw_loc_opc)
31039 case DW_OP_addr:
31040 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
31042 if ((prev == NULL
31043 || prev->dw_loc_opc == DW_OP_piece
31044 || prev->dw_loc_opc == DW_OP_bit_piece)
31045 && loc->dw_loc_next
31046 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
31047 && (!dwarf_strict || dwarf_version >= 5)
31048 && optimize_one_addr_into_implicit_ptr (loc))
31049 break;
31050 return false;
31052 break;
31053 case DW_OP_GNU_addr_index:
31054 case DW_OP_addrx:
31055 case DW_OP_GNU_const_index:
31056 case DW_OP_constx:
31057 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
31058 || loc->dw_loc_opc == DW_OP_addrx)
31059 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
31060 || loc->dw_loc_opc == DW_OP_constx)
31061 && loc->dtprel))
31063 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
31064 if (!resolve_one_addr (&rtl))
31065 return false;
31066 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
31067 loc->dw_loc_oprnd1.val_entry
31068 = add_addr_table_entry (rtl, ate_kind_rtx);
31070 break;
31071 case DW_OP_const4u:
31072 case DW_OP_const8u:
31073 if (loc->dtprel
31074 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
31075 return false;
31076 break;
31077 case DW_OP_plus_uconst:
31078 if (size_of_loc_descr (loc)
31079 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
31081 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
31083 dw_loc_descr_ref repl
31084 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
31085 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
31086 add_loc_descr (&repl, loc->dw_loc_next);
31087 *loc = *repl;
31089 break;
31090 case DW_OP_implicit_value:
31091 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
31092 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
31093 return false;
31094 break;
31095 case DW_OP_implicit_pointer:
31096 case DW_OP_GNU_implicit_pointer:
31097 case DW_OP_GNU_parameter_ref:
31098 case DW_OP_GNU_variable_value:
31099 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31101 dw_die_ref ref
31102 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
31103 if (ref == NULL)
31104 return false;
31105 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31106 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31107 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31109 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
31111 if (prev == NULL
31112 && loc->dw_loc_next == NULL
31113 && AT_class (a) == dw_val_class_loc)
31114 switch (a->dw_attr)
31116 /* Following attributes allow both exprloc and reference,
31117 so if the whole expression is DW_OP_GNU_variable_value
31118 alone we could transform it into reference. */
31119 case DW_AT_byte_size:
31120 case DW_AT_bit_size:
31121 case DW_AT_lower_bound:
31122 case DW_AT_upper_bound:
31123 case DW_AT_bit_stride:
31124 case DW_AT_count:
31125 case DW_AT_allocated:
31126 case DW_AT_associated:
31127 case DW_AT_byte_stride:
31128 a->dw_attr_val.val_class = dw_val_class_die_ref;
31129 a->dw_attr_val.val_entry = NULL;
31130 a->dw_attr_val.v.val_die_ref.die
31131 = loc->dw_loc_oprnd1.v.val_die_ref.die;
31132 a->dw_attr_val.v.val_die_ref.external = 0;
31133 return true;
31134 default:
31135 break;
31137 if (dwarf_strict)
31138 return false;
31140 break;
31141 case DW_OP_const_type:
31142 case DW_OP_regval_type:
31143 case DW_OP_deref_type:
31144 case DW_OP_convert:
31145 case DW_OP_reinterpret:
31146 case DW_OP_GNU_const_type:
31147 case DW_OP_GNU_regval_type:
31148 case DW_OP_GNU_deref_type:
31149 case DW_OP_GNU_convert:
31150 case DW_OP_GNU_reinterpret:
31151 while (loc->dw_loc_next
31152 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
31153 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
31155 dw_die_ref base1, base2;
31156 unsigned enc1, enc2, size1, size2;
31157 if (loc->dw_loc_opc == DW_OP_regval_type
31158 || loc->dw_loc_opc == DW_OP_deref_type
31159 || loc->dw_loc_opc == DW_OP_GNU_regval_type
31160 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
31161 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
31162 else if (loc->dw_loc_oprnd1.val_class
31163 == dw_val_class_unsigned_const)
31164 break;
31165 else
31166 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
31167 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
31168 == dw_val_class_unsigned_const)
31169 break;
31170 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
31171 gcc_assert (base1->die_tag == DW_TAG_base_type
31172 && base2->die_tag == DW_TAG_base_type);
31173 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
31174 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
31175 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
31176 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
31177 if (size1 == size2
31178 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
31179 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
31180 && loc != keep)
31181 || enc1 == enc2))
31183 /* Optimize away next DW_OP_convert after
31184 adjusting LOC's base type die reference. */
31185 if (loc->dw_loc_opc == DW_OP_regval_type
31186 || loc->dw_loc_opc == DW_OP_deref_type
31187 || loc->dw_loc_opc == DW_OP_GNU_regval_type
31188 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
31189 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
31190 else
31191 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
31192 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
31193 continue;
31195 /* Don't change integer DW_OP_convert after e.g. floating
31196 point typed stack entry. */
31197 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
31198 keep = loc->dw_loc_next;
31199 break;
31201 break;
31202 default:
31203 break;
31205 return true;
31208 /* Helper function of resolve_addr. DIE had DW_AT_location of
31209 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
31210 and DW_OP_addr couldn't be resolved. resolve_addr has already
31211 removed the DW_AT_location attribute. This function attempts to
31212 add a new DW_AT_location attribute with DW_OP_implicit_pointer
31213 to it or DW_AT_const_value attribute, if possible. */
31215 static void
31216 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
31218 if (!VAR_P (decl)
31219 || lookup_decl_die (decl) != die
31220 || DECL_EXTERNAL (decl)
31221 || !TREE_STATIC (decl)
31222 || DECL_INITIAL (decl) == NULL_TREE
31223 || DECL_P (DECL_INITIAL (decl))
31224 || get_AT (die, DW_AT_const_value))
31225 return;
31227 tree init = DECL_INITIAL (decl);
31228 HOST_WIDE_INT offset = 0;
31229 /* For variables that have been optimized away and thus
31230 don't have a memory location, see if we can emit
31231 DW_AT_const_value instead. */
31232 if (tree_add_const_value_attribute (die, init))
31233 return;
31234 if (dwarf_strict && dwarf_version < 5)
31235 return;
31236 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
31237 and ADDR_EXPR refers to a decl that has DW_AT_location or
31238 DW_AT_const_value (but isn't addressable, otherwise
31239 resolving the original DW_OP_addr wouldn't fail), see if
31240 we can add DW_OP_implicit_pointer. */
31241 STRIP_NOPS (init);
31242 if (TREE_CODE (init) == POINTER_PLUS_EXPR
31243 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
31245 offset = tree_to_shwi (TREE_OPERAND (init, 1));
31246 init = TREE_OPERAND (init, 0);
31247 STRIP_NOPS (init);
31249 if (TREE_CODE (init) != ADDR_EXPR)
31250 return;
31251 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
31252 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
31253 || (VAR_P (TREE_OPERAND (init, 0))
31254 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
31255 && TREE_OPERAND (init, 0) != decl))
31257 dw_die_ref ref;
31258 dw_loc_descr_ref l;
31260 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
31262 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
31263 if (!rtl)
31264 return;
31265 decl = SYMBOL_REF_DECL (rtl);
31267 else
31268 decl = TREE_OPERAND (init, 0);
31269 ref = lookup_decl_die (decl);
31270 if (ref == NULL
31271 || (!get_AT (ref, DW_AT_location)
31272 && !get_AT (ref, DW_AT_const_value)))
31273 return;
31274 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
31275 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31276 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
31277 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31278 add_AT_loc (die, DW_AT_location, l);
31282 /* Return NULL if l is a DWARF expression, or first op that is not
31283 valid DWARF expression. */
31285 static dw_loc_descr_ref
31286 non_dwarf_expression (dw_loc_descr_ref l)
31288 while (l)
31290 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31291 return l;
31292 switch (l->dw_loc_opc)
31294 case DW_OP_regx:
31295 case DW_OP_implicit_value:
31296 case DW_OP_stack_value:
31297 case DW_OP_implicit_pointer:
31298 case DW_OP_GNU_implicit_pointer:
31299 case DW_OP_GNU_parameter_ref:
31300 case DW_OP_piece:
31301 case DW_OP_bit_piece:
31302 return l;
31303 default:
31304 break;
31306 l = l->dw_loc_next;
31308 return NULL;
31311 /* Return adjusted copy of EXPR:
31312 If it is empty DWARF expression, return it.
31313 If it is valid non-empty DWARF expression,
31314 return copy of EXPR with DW_OP_deref appended to it.
31315 If it is DWARF expression followed by DW_OP_reg{N,x}, return
31316 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
31317 If it is DWARF expression followed by DW_OP_stack_value, return
31318 copy of the DWARF expression without anything appended.
31319 Otherwise, return NULL. */
31321 static dw_loc_descr_ref
31322 copy_deref_exprloc (dw_loc_descr_ref expr)
31324 dw_loc_descr_ref tail = NULL;
31326 if (expr == NULL)
31327 return NULL;
31329 dw_loc_descr_ref l = non_dwarf_expression (expr);
31330 if (l && l->dw_loc_next)
31331 return NULL;
31333 if (l)
31335 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31336 tail = new_loc_descr ((enum dwarf_location_atom)
31337 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
31338 0, 0);
31339 else
31340 switch (l->dw_loc_opc)
31342 case DW_OP_regx:
31343 tail = new_loc_descr (DW_OP_bregx,
31344 l->dw_loc_oprnd1.v.val_unsigned, 0);
31345 break;
31346 case DW_OP_stack_value:
31347 break;
31348 default:
31349 return NULL;
31352 else
31353 tail = new_loc_descr (DW_OP_deref, 0, 0);
31355 dw_loc_descr_ref ret = NULL, *p = &ret;
31356 while (expr != l)
31358 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
31359 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
31360 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
31361 p = &(*p)->dw_loc_next;
31362 expr = expr->dw_loc_next;
31364 *p = tail;
31365 return ret;
31368 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
31369 reference to a variable or argument, adjust it if needed and return:
31370 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
31371 attribute if present should be removed
31372 0 keep the attribute perhaps with minor modifications, no need to rescan
31373 1 if the attribute has been successfully adjusted. */
31375 static int
31376 optimize_string_length (dw_attr_node *a)
31378 dw_loc_descr_ref l = AT_loc (a), lv;
31379 dw_die_ref die;
31380 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31382 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
31383 die = lookup_decl_die (decl);
31384 if (die)
31386 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31387 l->dw_loc_oprnd1.v.val_die_ref.die = die;
31388 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31390 else
31391 return -1;
31393 else
31394 die = l->dw_loc_oprnd1.v.val_die_ref.die;
31396 /* DWARF5 allows reference class, so we can then reference the DIE.
31397 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
31398 if (l->dw_loc_next != NULL && dwarf_version >= 5)
31400 a->dw_attr_val.val_class = dw_val_class_die_ref;
31401 a->dw_attr_val.val_entry = NULL;
31402 a->dw_attr_val.v.val_die_ref.die = die;
31403 a->dw_attr_val.v.val_die_ref.external = 0;
31404 return 0;
31407 dw_attr_node *av = get_AT (die, DW_AT_location);
31408 dw_loc_list_ref d;
31409 bool non_dwarf_expr = false;
31411 if (av == NULL)
31412 return dwarf_strict ? -1 : 0;
31413 switch (AT_class (av))
31415 case dw_val_class_loc_list:
31416 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31417 if (d->expr && non_dwarf_expression (d->expr))
31418 non_dwarf_expr = true;
31419 break;
31420 case dw_val_class_view_list:
31421 gcc_unreachable ();
31422 case dw_val_class_loc:
31423 lv = AT_loc (av);
31424 if (lv == NULL)
31425 return dwarf_strict ? -1 : 0;
31426 if (non_dwarf_expression (lv))
31427 non_dwarf_expr = true;
31428 break;
31429 default:
31430 return dwarf_strict ? -1 : 0;
31433 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
31434 into DW_OP_call4 or DW_OP_GNU_variable_value into
31435 DW_OP_call4 DW_OP_deref, do so. */
31436 if (!non_dwarf_expr
31437 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
31439 l->dw_loc_opc = DW_OP_call4;
31440 if (l->dw_loc_next)
31441 l->dw_loc_next = NULL;
31442 else
31443 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
31444 return 0;
31447 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
31448 copy over the DW_AT_location attribute from die to a. */
31449 if (l->dw_loc_next != NULL)
31451 a->dw_attr_val = av->dw_attr_val;
31452 return 1;
31455 dw_loc_list_ref list, *p;
31456 switch (AT_class (av))
31458 case dw_val_class_loc_list:
31459 p = &list;
31460 list = NULL;
31461 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31463 lv = copy_deref_exprloc (d->expr);
31464 if (lv)
31466 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
31467 p = &(*p)->dw_loc_next;
31469 else if (!dwarf_strict && d->expr)
31470 return 0;
31472 if (list == NULL)
31473 return dwarf_strict ? -1 : 0;
31474 a->dw_attr_val.val_class = dw_val_class_loc_list;
31475 gen_llsym (list);
31476 *AT_loc_list_ptr (a) = list;
31477 return 1;
31478 case dw_val_class_loc:
31479 lv = copy_deref_exprloc (AT_loc (av));
31480 if (lv == NULL)
31481 return dwarf_strict ? -1 : 0;
31482 a->dw_attr_val.v.val_loc = lv;
31483 return 1;
31484 default:
31485 gcc_unreachable ();
31489 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31490 an address in .rodata section if the string literal is emitted there,
31491 or remove the containing location list or replace DW_AT_const_value
31492 with DW_AT_location and empty location expression, if it isn't found
31493 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31494 to something that has been emitted in the current CU. */
31496 static void
31497 resolve_addr (dw_die_ref die)
31499 dw_die_ref c;
31500 dw_attr_node *a;
31501 dw_loc_list_ref *curr, *start, loc;
31502 unsigned ix;
31503 bool remove_AT_byte_size = false;
31505 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31506 switch (AT_class (a))
31508 case dw_val_class_loc_list:
31509 start = curr = AT_loc_list_ptr (a);
31510 loc = *curr;
31511 gcc_assert (loc);
31512 /* The same list can be referenced more than once. See if we have
31513 already recorded the result from a previous pass. */
31514 if (loc->replaced)
31515 *curr = loc->dw_loc_next;
31516 else if (!loc->resolved_addr)
31518 /* As things stand, we do not expect or allow one die to
31519 reference a suffix of another die's location list chain.
31520 References must be identical or completely separate.
31521 There is therefore no need to cache the result of this
31522 pass on any list other than the first; doing so
31523 would lead to unnecessary writes. */
31524 while (*curr)
31526 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31527 if (!resolve_addr_in_expr (a, (*curr)->expr))
31529 dw_loc_list_ref next = (*curr)->dw_loc_next;
31530 dw_loc_descr_ref l = (*curr)->expr;
31532 if (next && (*curr)->ll_symbol)
31534 gcc_assert (!next->ll_symbol);
31535 next->ll_symbol = (*curr)->ll_symbol;
31536 next->vl_symbol = (*curr)->vl_symbol;
31538 if (dwarf_split_debug_info)
31539 remove_loc_list_addr_table_entries (l);
31540 *curr = next;
31542 else
31544 mark_base_types ((*curr)->expr);
31545 curr = &(*curr)->dw_loc_next;
31548 if (loc == *start)
31549 loc->resolved_addr = 1;
31550 else
31552 loc->replaced = 1;
31553 loc->dw_loc_next = *start;
31556 if (!*start)
31558 remove_AT (die, a->dw_attr);
31559 ix--;
31561 break;
31562 case dw_val_class_view_list:
31564 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31565 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31566 dw_val_node *llnode
31567 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31568 /* If we no longer have a loclist, or it no longer needs
31569 views, drop this attribute. */
31570 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31572 remove_AT (die, a->dw_attr);
31573 ix--;
31575 break;
31577 case dw_val_class_loc:
31579 dw_loc_descr_ref l = AT_loc (a);
31580 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31581 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31582 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31583 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31584 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31585 with DW_FORM_ref referencing the same DIE as
31586 DW_OP_GNU_variable_value used to reference. */
31587 if (a->dw_attr == DW_AT_string_length
31588 && l
31589 && l->dw_loc_opc == DW_OP_GNU_variable_value
31590 && (l->dw_loc_next == NULL
31591 || (l->dw_loc_next->dw_loc_next == NULL
31592 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31594 switch (optimize_string_length (a))
31596 case -1:
31597 remove_AT (die, a->dw_attr);
31598 ix--;
31599 /* If we drop DW_AT_string_length, we need to drop also
31600 DW_AT_{string_length_,}byte_size. */
31601 remove_AT_byte_size = true;
31602 continue;
31603 default:
31604 break;
31605 case 1:
31606 /* Even if we keep the optimized DW_AT_string_length,
31607 it might have changed AT_class, so process it again. */
31608 ix--;
31609 continue;
31612 /* For -gdwarf-2 don't attempt to optimize
31613 DW_AT_data_member_location containing
31614 DW_OP_plus_uconst - older consumers might
31615 rely on it being that op instead of a more complex,
31616 but shorter, location description. */
31617 if ((dwarf_version > 2
31618 || a->dw_attr != DW_AT_data_member_location
31619 || l == NULL
31620 || l->dw_loc_opc != DW_OP_plus_uconst
31621 || l->dw_loc_next != NULL)
31622 && !resolve_addr_in_expr (a, l))
31624 if (dwarf_split_debug_info)
31625 remove_loc_list_addr_table_entries (l);
31626 if (l != NULL
31627 && l->dw_loc_next == NULL
31628 && l->dw_loc_opc == DW_OP_addr
31629 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31630 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31631 && a->dw_attr == DW_AT_location)
31633 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31634 remove_AT (die, a->dw_attr);
31635 ix--;
31636 optimize_location_into_implicit_ptr (die, decl);
31637 break;
31639 if (a->dw_attr == DW_AT_string_length)
31640 /* If we drop DW_AT_string_length, we need to drop also
31641 DW_AT_{string_length_,}byte_size. */
31642 remove_AT_byte_size = true;
31643 remove_AT (die, a->dw_attr);
31644 ix--;
31646 else
31647 mark_base_types (l);
31649 break;
31650 case dw_val_class_addr:
31651 if (a->dw_attr == DW_AT_const_value
31652 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31654 if (AT_index (a) != NOT_INDEXED)
31655 remove_addr_table_entry (a->dw_attr_val.val_entry);
31656 remove_AT (die, a->dw_attr);
31657 ix--;
31659 if ((die->die_tag == DW_TAG_call_site
31660 && a->dw_attr == DW_AT_call_origin)
31661 || (die->die_tag == DW_TAG_GNU_call_site
31662 && a->dw_attr == DW_AT_abstract_origin))
31664 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31665 dw_die_ref tdie = lookup_decl_die (tdecl);
31666 dw_die_ref cdie;
31667 if (tdie == NULL
31668 && DECL_EXTERNAL (tdecl)
31669 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31670 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31672 dw_die_ref pdie = cdie;
31673 /* Make sure we don't add these DIEs into type units.
31674 We could emit skeleton DIEs for context (namespaces,
31675 outer structs/classes) and a skeleton DIE for the
31676 innermost context with DW_AT_signature pointing to the
31677 type unit. See PR78835. */
31678 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31679 pdie = pdie->die_parent;
31680 if (pdie == NULL)
31682 /* Creating a full DIE for tdecl is overly expensive and
31683 at this point even wrong when in the LTO phase
31684 as it can end up generating new type DIEs we didn't
31685 output and thus optimize_external_refs will crash. */
31686 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31687 add_AT_flag (tdie, DW_AT_external, 1);
31688 add_AT_flag (tdie, DW_AT_declaration, 1);
31689 add_linkage_attr (tdie, tdecl);
31690 add_name_and_src_coords_attributes (tdie, tdecl, true);
31691 equate_decl_number_to_die (tdecl, tdie);
31694 if (tdie)
31696 a->dw_attr_val.val_class = dw_val_class_die_ref;
31697 a->dw_attr_val.v.val_die_ref.die = tdie;
31698 a->dw_attr_val.v.val_die_ref.external = 0;
31700 else
31702 if (AT_index (a) != NOT_INDEXED)
31703 remove_addr_table_entry (a->dw_attr_val.val_entry);
31704 remove_AT (die, a->dw_attr);
31705 ix--;
31708 break;
31709 default:
31710 break;
31713 if (remove_AT_byte_size)
31714 remove_AT (die, dwarf_version >= 5
31715 ? DW_AT_string_length_byte_size
31716 : DW_AT_byte_size);
31718 FOR_EACH_CHILD (die, c, resolve_addr (c));
31721 /* Helper routines for optimize_location_lists.
31722 This pass tries to share identical local lists in .debug_loc
31723 section. */
31725 /* Iteratively hash operands of LOC opcode into HSTATE. */
31727 static void
31728 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31730 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31731 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31733 switch (loc->dw_loc_opc)
31735 case DW_OP_const4u:
31736 case DW_OP_const8u:
31737 if (loc->dtprel)
31738 goto hash_addr;
31739 /* FALLTHRU */
31740 case DW_OP_const1u:
31741 case DW_OP_const1s:
31742 case DW_OP_const2u:
31743 case DW_OP_const2s:
31744 case DW_OP_const4s:
31745 case DW_OP_const8s:
31746 case DW_OP_constu:
31747 case DW_OP_consts:
31748 case DW_OP_pick:
31749 case DW_OP_plus_uconst:
31750 case DW_OP_breg0:
31751 case DW_OP_breg1:
31752 case DW_OP_breg2:
31753 case DW_OP_breg3:
31754 case DW_OP_breg4:
31755 case DW_OP_breg5:
31756 case DW_OP_breg6:
31757 case DW_OP_breg7:
31758 case DW_OP_breg8:
31759 case DW_OP_breg9:
31760 case DW_OP_breg10:
31761 case DW_OP_breg11:
31762 case DW_OP_breg12:
31763 case DW_OP_breg13:
31764 case DW_OP_breg14:
31765 case DW_OP_breg15:
31766 case DW_OP_breg16:
31767 case DW_OP_breg17:
31768 case DW_OP_breg18:
31769 case DW_OP_breg19:
31770 case DW_OP_breg20:
31771 case DW_OP_breg21:
31772 case DW_OP_breg22:
31773 case DW_OP_breg23:
31774 case DW_OP_breg24:
31775 case DW_OP_breg25:
31776 case DW_OP_breg26:
31777 case DW_OP_breg27:
31778 case DW_OP_breg28:
31779 case DW_OP_breg29:
31780 case DW_OP_breg30:
31781 case DW_OP_breg31:
31782 case DW_OP_regx:
31783 case DW_OP_fbreg:
31784 case DW_OP_piece:
31785 case DW_OP_deref_size:
31786 case DW_OP_xderef_size:
31787 hstate.add_object (val1->v.val_int);
31788 break;
31789 case DW_OP_skip:
31790 case DW_OP_bra:
31792 int offset;
31794 gcc_assert (val1->val_class == dw_val_class_loc);
31795 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31796 hstate.add_object (offset);
31798 break;
31799 case DW_OP_implicit_value:
31800 hstate.add_object (val1->v.val_unsigned);
31801 switch (val2->val_class)
31803 case dw_val_class_const:
31804 hstate.add_object (val2->v.val_int);
31805 break;
31806 case dw_val_class_vec:
31808 unsigned int elt_size = val2->v.val_vec.elt_size;
31809 unsigned int len = val2->v.val_vec.length;
31811 hstate.add_int (elt_size);
31812 hstate.add_int (len);
31813 hstate.add (val2->v.val_vec.array, len * elt_size);
31815 break;
31816 case dw_val_class_const_double:
31817 hstate.add_object (val2->v.val_double.low);
31818 hstate.add_object (val2->v.val_double.high);
31819 break;
31820 case dw_val_class_wide_int:
31821 hstate.add (val2->v.val_wide->get_val (),
31822 get_full_len (*val2->v.val_wide)
31823 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31824 break;
31825 case dw_val_class_addr:
31826 inchash::add_rtx (val2->v.val_addr, hstate);
31827 break;
31828 default:
31829 gcc_unreachable ();
31831 break;
31832 case DW_OP_bregx:
31833 case DW_OP_bit_piece:
31834 hstate.add_object (val1->v.val_int);
31835 hstate.add_object (val2->v.val_int);
31836 break;
31837 case DW_OP_addr:
31838 hash_addr:
31839 if (loc->dtprel)
31841 unsigned char dtprel = 0xd1;
31842 hstate.add_object (dtprel);
31844 inchash::add_rtx (val1->v.val_addr, hstate);
31845 break;
31846 case DW_OP_GNU_addr_index:
31847 case DW_OP_addrx:
31848 case DW_OP_GNU_const_index:
31849 case DW_OP_constx:
31851 if (loc->dtprel)
31853 unsigned char dtprel = 0xd1;
31854 hstate.add_object (dtprel);
31856 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31858 break;
31859 case DW_OP_implicit_pointer:
31860 case DW_OP_GNU_implicit_pointer:
31861 hstate.add_int (val2->v.val_int);
31862 break;
31863 case DW_OP_entry_value:
31864 case DW_OP_GNU_entry_value:
31865 hstate.add_object (val1->v.val_loc);
31866 break;
31867 case DW_OP_regval_type:
31868 case DW_OP_deref_type:
31869 case DW_OP_GNU_regval_type:
31870 case DW_OP_GNU_deref_type:
31872 unsigned int byte_size
31873 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31874 unsigned int encoding
31875 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31876 hstate.add_object (val1->v.val_int);
31877 hstate.add_object (byte_size);
31878 hstate.add_object (encoding);
31880 break;
31881 case DW_OP_convert:
31882 case DW_OP_reinterpret:
31883 case DW_OP_GNU_convert:
31884 case DW_OP_GNU_reinterpret:
31885 if (val1->val_class == dw_val_class_unsigned_const)
31887 hstate.add_object (val1->v.val_unsigned);
31888 break;
31890 /* FALLTHRU */
31891 case DW_OP_const_type:
31892 case DW_OP_GNU_const_type:
31894 unsigned int byte_size
31895 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31896 unsigned int encoding
31897 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31898 hstate.add_object (byte_size);
31899 hstate.add_object (encoding);
31900 if (loc->dw_loc_opc != DW_OP_const_type
31901 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31902 break;
31903 hstate.add_object (val2->val_class);
31904 switch (val2->val_class)
31906 case dw_val_class_const:
31907 hstate.add_object (val2->v.val_int);
31908 break;
31909 case dw_val_class_vec:
31911 unsigned int elt_size = val2->v.val_vec.elt_size;
31912 unsigned int len = val2->v.val_vec.length;
31914 hstate.add_object (elt_size);
31915 hstate.add_object (len);
31916 hstate.add (val2->v.val_vec.array, len * elt_size);
31918 break;
31919 case dw_val_class_const_double:
31920 hstate.add_object (val2->v.val_double.low);
31921 hstate.add_object (val2->v.val_double.high);
31922 break;
31923 case dw_val_class_wide_int:
31924 hstate.add (val2->v.val_wide->get_val (),
31925 get_full_len (*val2->v.val_wide)
31926 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31927 break;
31928 default:
31929 gcc_unreachable ();
31932 break;
31934 default:
31935 /* Other codes have no operands. */
31936 break;
31940 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31942 static inline void
31943 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31945 dw_loc_descr_ref l;
31946 bool sizes_computed = false;
31947 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31948 size_of_locs (loc);
31950 for (l = loc; l != NULL; l = l->dw_loc_next)
31952 enum dwarf_location_atom opc = l->dw_loc_opc;
31953 hstate.add_object (opc);
31954 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31956 size_of_locs (loc);
31957 sizes_computed = true;
31959 hash_loc_operands (l, hstate);
31963 /* Compute hash of the whole location list LIST_HEAD. */
31965 static inline void
31966 hash_loc_list (dw_loc_list_ref list_head)
31968 dw_loc_list_ref curr = list_head;
31969 inchash::hash hstate;
31971 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31973 hstate.add (curr->begin, strlen (curr->begin) + 1);
31974 hstate.add (curr->end, strlen (curr->end) + 1);
31975 hstate.add_object (curr->vbegin);
31976 hstate.add_object (curr->vend);
31977 if (curr->section)
31978 hstate.add (curr->section, strlen (curr->section) + 1);
31979 hash_locs (curr->expr, hstate);
31981 list_head->hash = hstate.end ();
31984 /* Return true if X and Y opcodes have the same operands. */
31986 static inline bool
31987 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31989 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31990 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31991 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31992 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31994 switch (x->dw_loc_opc)
31996 case DW_OP_const4u:
31997 case DW_OP_const8u:
31998 if (x->dtprel)
31999 goto hash_addr;
32000 /* FALLTHRU */
32001 case DW_OP_const1u:
32002 case DW_OP_const1s:
32003 case DW_OP_const2u:
32004 case DW_OP_const2s:
32005 case DW_OP_const4s:
32006 case DW_OP_const8s:
32007 case DW_OP_constu:
32008 case DW_OP_consts:
32009 case DW_OP_pick:
32010 case DW_OP_plus_uconst:
32011 case DW_OP_breg0:
32012 case DW_OP_breg1:
32013 case DW_OP_breg2:
32014 case DW_OP_breg3:
32015 case DW_OP_breg4:
32016 case DW_OP_breg5:
32017 case DW_OP_breg6:
32018 case DW_OP_breg7:
32019 case DW_OP_breg8:
32020 case DW_OP_breg9:
32021 case DW_OP_breg10:
32022 case DW_OP_breg11:
32023 case DW_OP_breg12:
32024 case DW_OP_breg13:
32025 case DW_OP_breg14:
32026 case DW_OP_breg15:
32027 case DW_OP_breg16:
32028 case DW_OP_breg17:
32029 case DW_OP_breg18:
32030 case DW_OP_breg19:
32031 case DW_OP_breg20:
32032 case DW_OP_breg21:
32033 case DW_OP_breg22:
32034 case DW_OP_breg23:
32035 case DW_OP_breg24:
32036 case DW_OP_breg25:
32037 case DW_OP_breg26:
32038 case DW_OP_breg27:
32039 case DW_OP_breg28:
32040 case DW_OP_breg29:
32041 case DW_OP_breg30:
32042 case DW_OP_breg31:
32043 case DW_OP_regx:
32044 case DW_OP_fbreg:
32045 case DW_OP_piece:
32046 case DW_OP_deref_size:
32047 case DW_OP_xderef_size:
32048 return valx1->v.val_int == valy1->v.val_int;
32049 case DW_OP_skip:
32050 case DW_OP_bra:
32051 /* If splitting debug info, the use of DW_OP_GNU_addr_index
32052 can cause irrelevant differences in dw_loc_addr. */
32053 gcc_assert (valx1->val_class == dw_val_class_loc
32054 && valy1->val_class == dw_val_class_loc
32055 && (dwarf_split_debug_info
32056 || x->dw_loc_addr == y->dw_loc_addr));
32057 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
32058 case DW_OP_implicit_value:
32059 if (valx1->v.val_unsigned != valy1->v.val_unsigned
32060 || valx2->val_class != valy2->val_class)
32061 return false;
32062 switch (valx2->val_class)
32064 case dw_val_class_const:
32065 return valx2->v.val_int == valy2->v.val_int;
32066 case dw_val_class_vec:
32067 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
32068 && valx2->v.val_vec.length == valy2->v.val_vec.length
32069 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
32070 valx2->v.val_vec.elt_size
32071 * valx2->v.val_vec.length) == 0;
32072 case dw_val_class_const_double:
32073 return valx2->v.val_double.low == valy2->v.val_double.low
32074 && valx2->v.val_double.high == valy2->v.val_double.high;
32075 case dw_val_class_wide_int:
32076 return *valx2->v.val_wide == *valy2->v.val_wide;
32077 case dw_val_class_addr:
32078 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
32079 default:
32080 gcc_unreachable ();
32082 case DW_OP_bregx:
32083 case DW_OP_bit_piece:
32084 return valx1->v.val_int == valy1->v.val_int
32085 && valx2->v.val_int == valy2->v.val_int;
32086 case DW_OP_addr:
32087 hash_addr:
32088 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
32089 case DW_OP_GNU_addr_index:
32090 case DW_OP_addrx:
32091 case DW_OP_GNU_const_index:
32092 case DW_OP_constx:
32094 rtx ax1 = valx1->val_entry->addr.rtl;
32095 rtx ay1 = valy1->val_entry->addr.rtl;
32096 return rtx_equal_p (ax1, ay1);
32098 case DW_OP_implicit_pointer:
32099 case DW_OP_GNU_implicit_pointer:
32100 return valx1->val_class == dw_val_class_die_ref
32101 && valx1->val_class == valy1->val_class
32102 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
32103 && valx2->v.val_int == valy2->v.val_int;
32104 case DW_OP_entry_value:
32105 case DW_OP_GNU_entry_value:
32106 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
32107 case DW_OP_const_type:
32108 case DW_OP_GNU_const_type:
32109 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
32110 || valx2->val_class != valy2->val_class)
32111 return false;
32112 switch (valx2->val_class)
32114 case dw_val_class_const:
32115 return valx2->v.val_int == valy2->v.val_int;
32116 case dw_val_class_vec:
32117 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
32118 && valx2->v.val_vec.length == valy2->v.val_vec.length
32119 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
32120 valx2->v.val_vec.elt_size
32121 * valx2->v.val_vec.length) == 0;
32122 case dw_val_class_const_double:
32123 return valx2->v.val_double.low == valy2->v.val_double.low
32124 && valx2->v.val_double.high == valy2->v.val_double.high;
32125 case dw_val_class_wide_int:
32126 return *valx2->v.val_wide == *valy2->v.val_wide;
32127 default:
32128 gcc_unreachable ();
32130 case DW_OP_regval_type:
32131 case DW_OP_deref_type:
32132 case DW_OP_GNU_regval_type:
32133 case DW_OP_GNU_deref_type:
32134 return valx1->v.val_int == valy1->v.val_int
32135 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
32136 case DW_OP_convert:
32137 case DW_OP_reinterpret:
32138 case DW_OP_GNU_convert:
32139 case DW_OP_GNU_reinterpret:
32140 if (valx1->val_class != valy1->val_class)
32141 return false;
32142 if (valx1->val_class == dw_val_class_unsigned_const)
32143 return valx1->v.val_unsigned == valy1->v.val_unsigned;
32144 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
32145 case DW_OP_GNU_parameter_ref:
32146 return valx1->val_class == dw_val_class_die_ref
32147 && valx1->val_class == valy1->val_class
32148 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
32149 default:
32150 /* Other codes have no operands. */
32151 return true;
32155 /* Return true if DWARF location expressions X and Y are the same. */
32157 static inline bool
32158 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
32160 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
32161 if (x->dw_loc_opc != y->dw_loc_opc
32162 || x->dtprel != y->dtprel
32163 || !compare_loc_operands (x, y))
32164 break;
32165 return x == NULL && y == NULL;
32168 /* Hashtable helpers. */
32170 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
32172 static inline hashval_t hash (const dw_loc_list_struct *);
32173 static inline bool equal (const dw_loc_list_struct *,
32174 const dw_loc_list_struct *);
32177 /* Return precomputed hash of location list X. */
32179 inline hashval_t
32180 loc_list_hasher::hash (const dw_loc_list_struct *x)
32182 return x->hash;
32185 /* Return true if location lists A and B are the same. */
32187 inline bool
32188 loc_list_hasher::equal (const dw_loc_list_struct *a,
32189 const dw_loc_list_struct *b)
32191 if (a == b)
32192 return true;
32193 if (a->hash != b->hash)
32194 return false;
32195 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
32196 if (strcmp (a->begin, b->begin) != 0
32197 || strcmp (a->end, b->end) != 0
32198 || (a->section == NULL) != (b->section == NULL)
32199 || (a->section && strcmp (a->section, b->section) != 0)
32200 || a->vbegin != b->vbegin || a->vend != b->vend
32201 || !compare_locs (a->expr, b->expr))
32202 break;
32203 return a == NULL && b == NULL;
32206 typedef hash_table<loc_list_hasher> loc_list_hash_type;
32209 /* Recursively optimize location lists referenced from DIE
32210 children and share them whenever possible. */
32212 static void
32213 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
32215 dw_die_ref c;
32216 dw_attr_node *a;
32217 unsigned ix;
32218 dw_loc_list_struct **slot;
32219 bool drop_locviews = false;
32220 bool has_locviews = false;
32222 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32223 if (AT_class (a) == dw_val_class_loc_list)
32225 dw_loc_list_ref list = AT_loc_list (a);
32226 /* TODO: perform some optimizations here, before hashing
32227 it and storing into the hash table. */
32228 hash_loc_list (list);
32229 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
32230 if (*slot == NULL)
32232 *slot = list;
32233 if (loc_list_has_views (list))
32234 gcc_assert (list->vl_symbol);
32235 else if (list->vl_symbol)
32237 drop_locviews = true;
32238 list->vl_symbol = NULL;
32241 else
32243 if (list->vl_symbol && !(*slot)->vl_symbol)
32244 drop_locviews = true;
32245 a->dw_attr_val.v.val_loc_list = *slot;
32248 else if (AT_class (a) == dw_val_class_view_list)
32250 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
32251 has_locviews = true;
32255 if (drop_locviews && has_locviews)
32256 remove_AT (die, DW_AT_GNU_locviews);
32258 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
32262 /* Recursively assign each location list a unique index into the debug_addr
32263 section. */
32265 static void
32266 index_location_lists (dw_die_ref die)
32268 dw_die_ref c;
32269 dw_attr_node *a;
32270 unsigned ix;
32272 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32273 if (AT_class (a) == dw_val_class_loc_list)
32275 dw_loc_list_ref list = AT_loc_list (a);
32276 dw_loc_list_ref curr;
32277 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
32279 /* Don't index an entry that has already been indexed
32280 or won't be output. Make sure skip_loc_list_entry doesn't
32281 call size_of_locs, because that might cause circular dependency,
32282 index_location_lists requiring address table indexes to be
32283 computed, but adding new indexes through add_addr_table_entry
32284 and address table index computation requiring no new additions
32285 to the hash table. In the rare case of DWARF[234] >= 64KB
32286 location expression, we'll just waste unused address table entry
32287 for it. */
32288 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
32289 continue;
32291 curr->begin_entry
32292 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
32293 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
32294 curr->end_entry
32295 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
32299 FOR_EACH_CHILD (die, c, index_location_lists (c));
32302 /* Optimize location lists referenced from DIE
32303 children and share them whenever possible. */
32305 static void
32306 optimize_location_lists (dw_die_ref die)
32308 loc_list_hash_type htab (500);
32309 optimize_location_lists_1 (die, &htab);
32312 /* Traverse the limbo die list, and add parent/child links. The only
32313 dies without parents that should be here are concrete instances of
32314 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
32315 For concrete instances, we can get the parent die from the abstract
32316 instance. */
32318 static void
32319 flush_limbo_die_list (void)
32321 limbo_die_node *node;
32323 /* get_context_die calls force_decl_die, which can put new DIEs on the
32324 limbo list in LTO mode when nested functions are put in a different
32325 partition than that of their parent function. */
32326 while ((node = limbo_die_list))
32328 dw_die_ref die = node->die;
32329 limbo_die_list = node->next;
32331 if (die->die_parent == NULL)
32333 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
32335 if (origin && origin->die_parent)
32336 add_child_die (origin->die_parent, die);
32337 else if (is_cu_die (die))
32339 else if (seen_error ())
32340 /* It's OK to be confused by errors in the input. */
32341 add_child_die (comp_unit_die (), die);
32342 else
32344 /* In certain situations, the lexical block containing a
32345 nested function can be optimized away, which results
32346 in the nested function die being orphaned. Likewise
32347 with the return type of that nested function. Force
32348 this to be a child of the containing function.
32350 It may happen that even the containing function got fully
32351 inlined and optimized out. In that case we are lost and
32352 assign the empty child. This should not be big issue as
32353 the function is likely unreachable too. */
32354 gcc_assert (node->created_for);
32356 if (DECL_P (node->created_for))
32357 origin = get_context_die (DECL_CONTEXT (node->created_for));
32358 else if (TYPE_P (node->created_for))
32359 origin = scope_die_for (node->created_for, comp_unit_die ());
32360 else
32361 origin = comp_unit_die ();
32363 add_child_die (origin, die);
32369 /* Reset DIEs so we can output them again. */
32371 static void
32372 reset_dies (dw_die_ref die)
32374 dw_die_ref c;
32376 /* Remove stuff we re-generate. */
32377 die->die_mark = 0;
32378 die->die_offset = 0;
32379 die->die_abbrev = 0;
32380 remove_AT (die, DW_AT_sibling);
32382 FOR_EACH_CHILD (die, c, reset_dies (c));
32385 /* reset_indirect_string removed the references coming from DW_AT_name
32386 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
32387 .debug_line_str strings again. */
32389 static void
32390 adjust_name_comp_dir (dw_die_ref die)
32392 for (int i = 0; i < 2; i++)
32394 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
32395 dw_attr_node *a = get_AT (die, attr_kind);
32396 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
32397 continue;
32399 if (!debug_line_str_hash)
32400 debug_line_str_hash
32401 = hash_table<indirect_string_hasher>::create_ggc (10);
32403 struct indirect_string_node *node
32404 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
32405 debug_line_str_hash);
32406 set_indirect_string (node);
32407 node->form = DW_FORM_line_strp;
32408 a->dw_attr_val.v.val_str = node;
32412 /* Output stuff that dwarf requires at the end of every file,
32413 and generate the DWARF-2 debugging info. */
32415 static void
32416 dwarf2out_finish (const char *filename)
32418 comdat_type_node *ctnode;
32419 dw_die_ref main_comp_unit_die;
32420 unsigned char checksum[16];
32421 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32423 /* Generate CTF/BTF debug info. */
32424 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32425 || btf_debuginfo_p ()) && lang_GNU_C ())
32426 ctf_debug_finish ();
32428 #ifdef CODEVIEW_DEBUGGING_INFO
32429 if (codeview_debuginfo_p ())
32430 codeview_debug_finish ();
32431 #endif
32433 /* Skip emitting DWARF if not required. */
32434 if (!dwarf_debuginfo_p ())
32435 return;
32437 /* Flush out any latecomers to the limbo party. */
32438 flush_limbo_die_list ();
32440 if (inline_entry_data_table)
32441 gcc_assert (inline_entry_data_table->is_empty ());
32443 if (flag_checking)
32445 verify_die (comp_unit_die ());
32446 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32447 verify_die (node->die);
32450 /* We shouldn't have any symbols with delayed asm names for
32451 DIEs generated after early finish. */
32452 gcc_assert (deferred_asm_name == NULL);
32454 gen_remaining_tmpl_value_param_die_attribute ();
32456 if (flag_generate_lto || flag_generate_offload)
32458 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
32460 /* Prune stuff so that dwarf2out_finish runs successfully
32461 for the fat part of the object. */
32462 reset_dies (comp_unit_die ());
32463 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32464 reset_dies (node->die);
32465 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32467 /* Remove the pointer to the line table. */
32468 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32469 if (debug_info_level >= DINFO_LEVEL_TERSE)
32470 reset_dies (ctnode->root_die);
32473 /* Reset die CU symbol so we don't output it twice. */
32474 comp_unit_die ()->die_id.die_symbol = NULL;
32476 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32477 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32478 if (have_macinfo)
32479 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32481 /* Remove indirect string decisions. */
32482 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32483 if (debug_line_str_hash)
32485 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32486 debug_line_str_hash = NULL;
32487 if (asm_outputs_debug_line_str ())
32489 adjust_name_comp_dir (comp_unit_die ());
32490 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32491 adjust_name_comp_dir (node->die);
32496 #if ENABLE_ASSERT_CHECKING
32498 dw_die_ref die = comp_unit_die (), c;
32499 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32501 #endif
32502 base_types.truncate (0);
32503 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32504 resolve_addr (ctnode->root_die);
32505 resolve_addr (comp_unit_die ());
32506 move_marked_base_types ();
32508 if (dump_file)
32510 fprintf (dump_file, "DWARF for %s\n", filename);
32511 print_die (comp_unit_die (), dump_file);
32514 /* Initialize sections and labels used for actual assembler output. */
32515 unsigned generation = init_sections_and_labels (false);
32517 /* Traverse the DIE's and add sibling attributes to those DIE's that
32518 have children. */
32519 add_sibling_attributes (comp_unit_die ());
32520 limbo_die_node *node;
32521 for (node = cu_die_list; node; node = node->next)
32522 add_sibling_attributes (node->die);
32523 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32524 add_sibling_attributes (ctnode->root_die);
32526 /* When splitting DWARF info, we put some attributes in the
32527 skeleton compile_unit DIE that remains in the .o, while
32528 most attributes go in the DWO compile_unit_die. */
32529 if (dwarf_split_debug_info)
32531 limbo_die_node *cu;
32532 main_comp_unit_die = gen_compile_unit_die (NULL);
32533 if (dwarf_version >= 5)
32534 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32535 cu = limbo_die_list;
32536 gcc_assert (cu->die == main_comp_unit_die);
32537 limbo_die_list = limbo_die_list->next;
32538 cu->next = cu_die_list;
32539 cu_die_list = cu;
32541 else
32542 main_comp_unit_die = comp_unit_die ();
32544 /* Output a terminator label for the .text section. */
32545 switch_to_section (text_section);
32546 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32547 if (cold_text_section)
32549 switch_to_section (cold_text_section);
32550 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32553 /* We can only use the low/high_pc attributes if all of the code was
32554 in .text. */
32555 if ((!have_multiple_function_sections
32556 && vec_safe_length (switch_text_ranges) < 2)
32557 || (dwarf_version < 3 && dwarf_strict))
32559 const char *end_label = text_end_label;
32560 if (vec_safe_length (switch_text_ranges) == 1)
32561 end_label = (*switch_text_ranges)[0];
32562 /* Don't add if the CU has no associated code. */
32563 if (switch_text_ranges)
32564 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32565 end_label, true);
32567 else
32569 unsigned fde_idx;
32570 dw_fde_ref fde;
32571 bool range_list_added = false;
32572 if (switch_text_ranges)
32574 const char *prev_loc = text_section_label;
32575 const char *loc;
32576 unsigned idx;
32578 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32579 if (prev_loc)
32581 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32582 loc, &range_list_added, true);
32583 prev_loc = NULL;
32585 else
32586 prev_loc = loc;
32588 if (prev_loc)
32589 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32590 text_end_label, &range_list_added, true);
32593 if (switch_cold_ranges)
32595 const char *prev_loc = cold_text_section_label;
32596 const char *loc;
32597 unsigned idx;
32599 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32600 if (prev_loc)
32602 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32603 loc, &range_list_added, true);
32604 prev_loc = NULL;
32606 else
32607 prev_loc = loc;
32609 if (prev_loc)
32610 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32611 cold_end_label, &range_list_added, true);
32614 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32616 if (fde->ignored_debug)
32617 continue;
32618 if (!fde->in_std_section)
32619 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32620 fde->dw_fde_end, &range_list_added,
32621 true);
32622 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32623 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32624 fde->dw_fde_second_end, &range_list_added,
32625 true);
32628 if (range_list_added)
32630 /* We need to give .debug_loc and .debug_ranges an appropriate
32631 "base address". Use zero so that these addresses become
32632 absolute. Historically, we've emitted the unexpected
32633 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32634 Emit both to give time for other tools to adapt. */
32635 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32636 if (! dwarf_strict && dwarf_version < 4)
32637 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32639 add_ranges (NULL);
32640 have_multiple_function_sections = true;
32644 /* AIX Assembler inserts the length, so adjust the reference to match the
32645 offset expected by debuggers. */
32646 strcpy (dl_section_ref, debug_line_section_label);
32647 if (XCOFF_DEBUGGING_INFO)
32648 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32650 if (debug_info_level >= DINFO_LEVEL_TERSE)
32651 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32652 dl_section_ref);
32654 if (have_macinfo)
32655 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32656 macinfo_section_label);
32658 if (dwarf_split_debug_info)
32660 if (have_location_lists)
32662 /* Since we generate the loclists in the split DWARF .dwo
32663 file itself, we don't need to generate a loclists_base
32664 attribute for the split compile unit DIE. That attribute
32665 (and using relocatable sec_offset FORMs) isn't allowed
32666 for a split compile unit. Only if the .debug_loclists
32667 section was in the main file, would we need to generate a
32668 loclists_base attribute here (for the full or skeleton
32669 unit DIE). */
32671 /* optimize_location_lists calculates the size of the lists,
32672 so index them first, and assign indices to the entries.
32673 Although optimize_location_lists will remove entries from
32674 the table, it only does so for duplicates, and therefore
32675 only reduces ref_counts to 1. */
32676 index_location_lists (comp_unit_die ());
32679 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32680 index_rnglists ();
32682 if (addr_index_table != NULL)
32684 unsigned int index = 0;
32685 addr_index_table
32686 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32687 (&index);
32691 loc_list_idx = 0;
32692 if (have_location_lists)
32694 optimize_location_lists (comp_unit_die ());
32695 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32696 if (dwarf_version >= 5 && dwarf_split_debug_info)
32697 assign_location_list_indexes (comp_unit_die ());
32700 save_macinfo_strings ();
32702 if (dwarf_split_debug_info)
32704 unsigned int index = 0;
32706 /* Add attributes common to skeleton compile_units and
32707 type_units. Because these attributes include strings, it
32708 must be done before freezing the string table. Top-level
32709 skeleton die attrs are added when the skeleton type unit is
32710 created, so ensure it is created by this point. */
32711 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32712 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32715 /* Output all of the compilation units. We put the main one last so that
32716 the offsets are available to output_pubnames. */
32717 for (node = cu_die_list; node; node = node->next)
32718 output_comp_unit (node->die, 0, NULL);
32720 hash_table<comdat_type_hasher> comdat_type_table (100);
32721 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32723 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32725 /* Don't output duplicate types. */
32726 if (*slot != HTAB_EMPTY_ENTRY)
32727 continue;
32729 /* Add a pointer to the line table for the main compilation unit
32730 so that the debugger can make sense of DW_AT_decl_file
32731 attributes. */
32732 if (debug_info_level >= DINFO_LEVEL_TERSE)
32733 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32734 (!dwarf_split_debug_info
32735 ? dl_section_ref
32736 : debug_skeleton_line_section_label));
32738 output_comdat_type_unit (ctnode, false);
32739 *slot = ctnode;
32742 if (dwarf_split_debug_info)
32744 int mark;
32745 struct md5_ctx ctx;
32747 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32748 md5_init_ctx (&ctx);
32749 mark = 0;
32750 die_checksum (comp_unit_die (), &ctx, &mark);
32751 unmark_all_dies (comp_unit_die ());
32752 md5_finish_ctx (&ctx, checksum);
32754 if (dwarf_version < 5)
32756 /* Use the first 8 bytes of the checksum as the dwo_id,
32757 and add it to both comp-unit DIEs. */
32758 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32759 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32762 /* Add the base offset of the ranges table to the skeleton
32763 comp-unit DIE. */
32764 if (!vec_safe_is_empty (ranges_table))
32766 if (dwarf_version < 5)
32767 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32768 ranges_section_label);
32771 output_addr_table ();
32774 /* Output the main compilation unit if non-empty or if .debug_macinfo
32775 or .debug_macro will be emitted. */
32776 output_comp_unit (comp_unit_die (), have_macinfo,
32777 dwarf_split_debug_info ? checksum : NULL);
32779 if (dwarf_split_debug_info && info_section_emitted)
32780 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32782 /* Output the abbreviation table. */
32783 if (vec_safe_length (abbrev_die_table) != 1)
32785 switch_to_section (debug_abbrev_section);
32786 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32787 output_abbrev_section ();
32790 /* Output location list section if necessary. */
32791 if (have_location_lists)
32793 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32794 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32795 /* Output the location lists info. */
32796 switch_to_section (debug_loc_section);
32797 if (dwarf_version >= 5)
32799 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32800 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32801 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32802 dw2_asm_output_data (4, 0xffffffff,
32803 "Initial length escape value indicating "
32804 "64-bit DWARF extension");
32805 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32806 "Length of Location Lists");
32807 ASM_OUTPUT_LABEL (asm_out_file, l1);
32808 output_dwarf_version ();
32809 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32810 dw2_asm_output_data (1, 0, "Segment Size");
32811 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32812 "Offset Entry Count");
32814 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32815 if (dwarf_version >= 5 && dwarf_split_debug_info)
32817 unsigned int save_loc_list_idx = loc_list_idx;
32818 loc_list_idx = 0;
32819 output_loclists_offsets (comp_unit_die ());
32820 gcc_assert (save_loc_list_idx == loc_list_idx);
32822 output_location_lists (comp_unit_die ());
32823 if (dwarf_version >= 5)
32824 ASM_OUTPUT_LABEL (asm_out_file, l2);
32827 output_pubtables ();
32829 /* Output the address range information if a CU (.debug_info section)
32830 was emitted. We output an empty table even if we had no functions
32831 to put in it. This because the consumer has no way to tell the
32832 difference between an empty table that we omitted and failure to
32833 generate a table that would have contained data. */
32834 if (info_section_emitted)
32836 switch_to_section (debug_aranges_section);
32837 output_aranges ();
32840 /* Output ranges section if necessary. */
32841 if (!vec_safe_is_empty (ranges_table))
32843 if (dwarf_version >= 5)
32845 if (dwarf_split_debug_info)
32847 /* We don't know right now whether there are any
32848 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32849 Depending on into which of those two belongs the first
32850 ranges_table entry, emit that section first and that
32851 output_rnglists call will return true if the other kind of
32852 ranges needs to be emitted as well. */
32853 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32854 if (output_rnglists (generation, dwo))
32855 output_rnglists (generation, !dwo);
32857 else
32858 output_rnglists (generation, false);
32860 else
32861 output_ranges ();
32864 /* Have to end the macro section. */
32865 if (have_macinfo)
32867 switch_to_section (debug_macinfo_section);
32868 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32869 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32870 : debug_skeleton_line_section_label, false);
32871 dw2_asm_output_data (1, 0, "End compilation unit");
32874 /* Output the source line correspondence table. We must do this
32875 even if there is no line information. Otherwise, on an empty
32876 translation unit, we will generate a present, but empty,
32877 .debug_info section. IRIX 6.5 `nm' will then complain when
32878 examining the file. This is done late so that any filenames
32879 used by the debug_info section are marked as 'used'. */
32880 switch_to_section (debug_line_section);
32881 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32882 if (! output_asm_line_debug_info ())
32883 output_line_info (false);
32885 if (dwarf_split_debug_info && info_section_emitted)
32887 switch_to_section (debug_skeleton_line_section);
32888 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32889 output_line_info (true);
32892 /* If we emitted any indirect strings, output the string table too. */
32893 if (debug_str_hash || skeleton_debug_str_hash)
32894 output_indirect_strings ();
32895 if (debug_line_str_hash)
32897 switch_to_section (debug_line_str_section);
32898 const enum dwarf_form form = DW_FORM_line_strp;
32899 debug_line_str_hash->traverse<enum dwarf_form,
32900 output_indirect_string> (form);
32903 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32904 symview_upper_bound = 0;
32905 if (zero_view_p)
32906 bitmap_clear (zero_view_p);
32909 /* Returns a hash value for X (which really is a variable_value_struct). */
32911 inline hashval_t
32912 variable_value_hasher::hash (variable_value_struct *x)
32914 return (hashval_t) x->decl_id;
32917 /* Return true if decl_id of variable_value_struct X is the same as
32918 UID of decl Y. */
32920 inline bool
32921 variable_value_hasher::equal (variable_value_struct *x, tree y)
32923 return x->decl_id == DECL_UID (y);
32926 /* Helper function for resolve_variable_value, handle
32927 DW_OP_GNU_variable_value in one location expression.
32928 Return true if exprloc has been changed into loclist. */
32930 static bool
32931 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32933 dw_loc_descr_ref next;
32934 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32936 next = loc->dw_loc_next;
32937 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32938 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32939 continue;
32941 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32942 if (DECL_CONTEXT (decl) != current_function_decl)
32943 continue;
32945 dw_die_ref ref = lookup_decl_die (decl);
32946 if (ref)
32948 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32949 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32950 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32951 continue;
32953 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32954 if (l == NULL)
32955 continue;
32956 if (l->dw_loc_next)
32958 if (AT_class (a) != dw_val_class_loc)
32959 continue;
32960 switch (a->dw_attr)
32962 /* Following attributes allow both exprloc and loclist
32963 classes, so we can change them into a loclist. */
32964 case DW_AT_location:
32965 case DW_AT_string_length:
32966 case DW_AT_return_addr:
32967 case DW_AT_data_member_location:
32968 case DW_AT_frame_base:
32969 case DW_AT_segment:
32970 case DW_AT_static_link:
32971 case DW_AT_use_location:
32972 case DW_AT_vtable_elem_location:
32973 if (prev)
32975 prev->dw_loc_next = NULL;
32976 prepend_loc_descr_to_each (l, AT_loc (a));
32978 if (next)
32979 add_loc_descr_to_each (l, next);
32980 a->dw_attr_val.val_class = dw_val_class_loc_list;
32981 a->dw_attr_val.val_entry = NULL;
32982 a->dw_attr_val.v.val_loc_list = l;
32983 have_location_lists = true;
32984 return true;
32985 /* Following attributes allow both exprloc and reference,
32986 so if the whole expression is DW_OP_GNU_variable_value alone
32987 we could transform it into reference. */
32988 case DW_AT_byte_size:
32989 case DW_AT_bit_size:
32990 case DW_AT_lower_bound:
32991 case DW_AT_upper_bound:
32992 case DW_AT_bit_stride:
32993 case DW_AT_count:
32994 case DW_AT_allocated:
32995 case DW_AT_associated:
32996 case DW_AT_byte_stride:
32997 if (prev == NULL && next == NULL)
32998 break;
32999 /* FALLTHRU */
33000 default:
33001 if (dwarf_strict)
33002 continue;
33003 break;
33005 /* Create DW_TAG_variable that we can refer to. */
33006 gen_decl_die (decl, NULL_TREE, NULL,
33007 lookup_decl_die (current_function_decl));
33008 ref = lookup_decl_die (decl);
33009 if (ref)
33011 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
33012 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
33013 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
33015 continue;
33017 if (prev)
33019 prev->dw_loc_next = l->expr;
33020 add_loc_descr (&prev->dw_loc_next, next);
33021 free_loc_descr (loc, NULL);
33022 next = prev->dw_loc_next;
33024 else
33026 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
33027 add_loc_descr (&loc, next);
33028 next = loc;
33030 loc = prev;
33032 return false;
33035 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
33037 static void
33038 resolve_variable_value (dw_die_ref die)
33040 dw_attr_node *a;
33041 dw_loc_list_ref loc;
33042 unsigned ix;
33044 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
33045 switch (AT_class (a))
33047 case dw_val_class_loc:
33048 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
33049 break;
33050 /* FALLTHRU */
33051 case dw_val_class_loc_list:
33052 loc = AT_loc_list (a);
33053 gcc_assert (loc);
33054 for (; loc; loc = loc->dw_loc_next)
33055 resolve_variable_value_in_expr (a, loc->expr);
33056 break;
33057 default:
33058 break;
33062 /* Attempt to optimize DW_OP_GNU_variable_value refering to
33063 temporaries in the current function. */
33065 static void
33066 resolve_variable_values (void)
33068 if (!variable_value_hash || !current_function_decl)
33069 return;
33071 struct variable_value_struct *node
33072 = variable_value_hash->find_with_hash (current_function_decl,
33073 DECL_UID (current_function_decl));
33075 if (node == NULL)
33076 return;
33078 unsigned int i;
33079 dw_die_ref die;
33080 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
33081 resolve_variable_value (die);
33084 /* Helper function for note_variable_value, handle one location
33085 expression. */
33087 static void
33088 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
33090 for (; loc; loc = loc->dw_loc_next)
33091 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
33092 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
33094 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
33095 dw_die_ref ref = lookup_decl_die (decl);
33096 if (! ref && (flag_generate_lto || flag_generate_offload))
33098 /* ??? This is somewhat a hack because we do not create DIEs
33099 for variables not in BLOCK trees early but when generating
33100 early LTO output we need the dw_val_class_decl_ref to be
33101 fully resolved. For fat LTO objects we'd also like to
33102 undo this after LTO dwarf output. */
33103 gcc_assert (DECL_CONTEXT (decl));
33104 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
33105 gcc_assert (ctx != NULL);
33106 gen_decl_die (decl, NULL_TREE, NULL, ctx);
33107 ref = lookup_decl_die (decl);
33108 gcc_assert (ref != NULL);
33110 if (ref)
33112 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
33113 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
33114 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
33115 continue;
33117 if (VAR_P (decl)
33118 && DECL_CONTEXT (decl)
33119 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
33120 && lookup_decl_die (DECL_CONTEXT (decl)))
33122 if (!variable_value_hash)
33123 variable_value_hash
33124 = hash_table<variable_value_hasher>::create_ggc (10);
33126 tree fndecl = DECL_CONTEXT (decl);
33127 struct variable_value_struct *node;
33128 struct variable_value_struct **slot
33129 = variable_value_hash->find_slot_with_hash (fndecl,
33130 DECL_UID (fndecl),
33131 INSERT);
33132 if (*slot == NULL)
33134 node = ggc_cleared_alloc<variable_value_struct> ();
33135 node->decl_id = DECL_UID (fndecl);
33136 *slot = node;
33138 else
33139 node = *slot;
33141 vec_safe_push (node->dies, die);
33146 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
33147 with dw_val_class_decl_ref operand. */
33149 static void
33150 note_variable_value (dw_die_ref die)
33152 dw_die_ref c;
33153 dw_attr_node *a;
33154 dw_loc_list_ref loc;
33155 unsigned ix;
33157 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
33158 switch (AT_class (a))
33160 case dw_val_class_loc_list:
33161 loc = AT_loc_list (a);
33162 gcc_assert (loc);
33163 if (!loc->noted_variable_value)
33165 loc->noted_variable_value = 1;
33166 for (; loc; loc = loc->dw_loc_next)
33167 note_variable_value_in_expr (die, loc->expr);
33169 break;
33170 case dw_val_class_loc:
33171 note_variable_value_in_expr (die, AT_loc (a));
33172 break;
33173 default:
33174 break;
33177 /* Mark children. */
33178 FOR_EACH_CHILD (die, c, note_variable_value (c));
33181 /* Process DWARF dies for CTF generation. */
33183 static void
33184 ctf_debug_do_cu (dw_die_ref die)
33186 dw_die_ref c;
33188 if (!ctf_do_die (die))
33189 return;
33191 FOR_EACH_CHILD (die, c, ctf_do_die (c));
33194 /* Perform any cleanups needed after the early debug generation pass
33195 has run. */
33197 static void
33198 dwarf2out_early_finish (const char *filename)
33200 comdat_type_node *ctnode;
33201 set_early_dwarf s;
33202 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
33204 /* PCH might result in DW_AT_producer string being restored from the
33205 header compilation, so always fill it with empty string initially
33206 and overwrite only here. */
33207 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
33209 if (dwarf_record_gcc_switches)
33210 producer_string = gen_producer_string (lang_hooks.name,
33211 save_decoded_options,
33212 save_decoded_options_count);
33213 else
33214 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
33216 producer->dw_attr_val.v.val_str->refcount--;
33217 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
33219 /* Add the name for the main input file now. We delayed this from
33220 dwarf2out_init to avoid complications with PCH. */
33221 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
33222 add_comp_dir_attribute (comp_unit_die ());
33224 /* With LTO early dwarf was really finished at compile-time, so make
33225 sure to adjust the phase after annotating the LTRANS CU DIE. */
33226 if (in_lto_p)
33228 early_dwarf_finished = true;
33229 if (dump_file)
33231 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
33232 print_die (comp_unit_die (), dump_file);
33234 return;
33237 /* Walk through the list of incomplete types again, trying once more to
33238 emit full debugging info for them. */
33239 retry_incomplete_types ();
33241 gen_scheduled_generic_parms_dies ();
33242 gen_remaining_tmpl_value_param_die_attribute ();
33244 /* The point here is to flush out the limbo list so that it is empty
33245 and we don't need to stream it for LTO. */
33246 flush_limbo_die_list ();
33248 /* Add DW_AT_linkage_name for all deferred DIEs. */
33249 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
33251 tree decl = node->created_for;
33252 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
33253 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
33254 ended up in deferred_asm_name before we knew it was
33255 constant and never written to disk. */
33256 && DECL_ASSEMBLER_NAME (decl))
33258 add_linkage_attr (node->die, decl);
33259 move_linkage_attr (node->die);
33262 deferred_asm_name = NULL;
33264 if (flag_eliminate_unused_debug_types)
33265 prune_unused_types ();
33267 /* Generate separate COMDAT sections for type DIEs. */
33268 if (use_debug_types)
33270 break_out_comdat_types (comp_unit_die ());
33272 /* Each new type_unit DIE was added to the limbo die list when created.
33273 Since these have all been added to comdat_type_list, clear the
33274 limbo die list. */
33275 limbo_die_list = NULL;
33277 /* For each new comdat type unit, copy declarations for incomplete
33278 types to make the new unit self-contained (i.e., no direct
33279 references to the main compile unit). */
33280 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33281 copy_decls_for_unworthy_types (ctnode->root_die);
33282 copy_decls_for_unworthy_types (comp_unit_die ());
33284 /* In the process of copying declarations from one unit to another,
33285 we may have left some declarations behind that are no longer
33286 referenced. Prune them. */
33287 prune_unused_types ();
33290 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
33291 with dw_val_class_decl_ref operand. */
33292 note_variable_value (comp_unit_die ());
33293 for (limbo_die_node *node = cu_die_list; node; node = node->next)
33294 note_variable_value (node->die);
33295 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33296 note_variable_value (ctnode->root_die);
33297 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33298 note_variable_value (node->die);
33300 /* The AT_pubnames attribute needs to go in all skeleton dies, including
33301 both the main_cu and all skeleton TUs. Making this call unconditional
33302 would end up either adding a second copy of the AT_pubnames attribute, or
33303 requiring a special case in add_top_level_skeleton_die_attrs. */
33304 if (!dwarf_split_debug_info)
33305 add_AT_pubnames (comp_unit_die ());
33307 /* The early debug phase is now finished. */
33308 early_dwarf_finished = true;
33309 if (dump_file)
33311 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
33312 print_die (comp_unit_die (), dump_file);
33315 /* Generate CTF/BTF debug info. */
33316 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
33317 || btf_debuginfo_p ()) && lang_GNU_C ())
33319 ctf_debug_init ();
33320 ctf_debug_do_cu (comp_unit_die ());
33321 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33322 ctf_debug_do_cu (node->die);
33324 ctf_debug_early_finish (filename);
33327 #ifdef CODEVIEW_DEBUGGING_INFO
33328 if (codeview_debuginfo_p ())
33329 codeview_debug_early_finish (comp_unit_die ());
33330 #endif
33332 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
33333 if ((!flag_generate_lto && !flag_generate_offload)
33334 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
33335 copy_lto_debug_sections operation of the simple object support in
33336 libiberty is not implemented for them yet. */
33337 || TARGET_PECOFF || TARGET_COFF)
33338 return;
33340 /* Now as we are going to output for LTO initialize sections and labels
33341 to the LTO variants. We don't need a random-seed postfix as other
33342 LTO sections as linking the LTO debug sections into one in a partial
33343 link is fine. */
33344 init_sections_and_labels (true);
33346 /* The output below is modeled after dwarf2out_finish with all
33347 location related output removed and some LTO specific changes.
33348 Some refactoring might make both smaller and easier to match up. */
33350 base_types.truncate (0);
33351 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33352 mark_base_types (ctnode->root_die);
33353 mark_base_types (comp_unit_die ());
33354 move_marked_base_types ();
33356 /* Traverse the DIE's and add sibling attributes to those DIE's
33357 that have children. */
33358 add_sibling_attributes (comp_unit_die ());
33359 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33360 add_sibling_attributes (node->die);
33361 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33362 add_sibling_attributes (ctnode->root_die);
33364 /* AIX Assembler inserts the length, so adjust the reference to match the
33365 offset expected by debuggers. */
33366 strcpy (dl_section_ref, debug_line_section_label);
33367 if (XCOFF_DEBUGGING_INFO)
33368 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
33370 if (debug_info_level >= DINFO_LEVEL_TERSE)
33371 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
33373 if (have_macinfo)
33374 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
33375 macinfo_section_label);
33377 save_macinfo_strings ();
33379 if (dwarf_split_debug_info)
33381 unsigned int index = 0;
33382 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
33385 /* Output all of the compilation units. We put the main one last so that
33386 the offsets are available to output_pubnames. */
33387 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33388 output_comp_unit (node->die, 0, NULL);
33390 hash_table<comdat_type_hasher> comdat_type_table (100);
33391 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33393 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
33395 /* Don't output duplicate types. */
33396 if (*slot != HTAB_EMPTY_ENTRY)
33397 continue;
33399 /* Add a pointer to the line table for the main compilation unit
33400 so that the debugger can make sense of DW_AT_decl_file
33401 attributes. */
33402 if (debug_info_level >= DINFO_LEVEL_TERSE)
33403 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
33404 (!dwarf_split_debug_info
33405 ? debug_line_section_label
33406 : debug_skeleton_line_section_label));
33408 output_comdat_type_unit (ctnode, true);
33409 *slot = ctnode;
33412 /* Stick a unique symbol to the main debuginfo section. */
33413 compute_comp_unit_symbol (comp_unit_die ());
33415 /* Output the main compilation unit. We always need it if only for
33416 the CU symbol. */
33417 output_comp_unit (comp_unit_die (), true, NULL);
33419 /* Output the abbreviation table. */
33420 if (vec_safe_length (abbrev_die_table) != 1)
33422 switch_to_section (debug_abbrev_section);
33423 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
33424 output_abbrev_section ();
33427 /* Have to end the macro section. */
33428 if (have_macinfo)
33430 /* We have to save macinfo state if we need to output it again
33431 for the FAT part of the object. */
33432 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
33433 if (flag_fat_lto_objects)
33434 macinfo_table = macinfo_table->copy ();
33436 switch_to_section (debug_macinfo_section);
33437 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
33438 output_macinfo (debug_line_section_label, true);
33439 dw2_asm_output_data (1, 0, "End compilation unit");
33441 if (flag_fat_lto_objects)
33443 vec_free (macinfo_table);
33444 macinfo_table = saved_macinfo_table;
33448 /* Emit a skeleton debug_line section. */
33449 switch_to_section (debug_line_section);
33450 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
33451 output_line_info (true);
33453 /* If we emitted any indirect strings, output the string table too. */
33454 if (debug_str_hash || skeleton_debug_str_hash)
33455 output_indirect_strings ();
33456 if (debug_line_str_hash)
33458 switch_to_section (debug_line_str_section);
33459 const enum dwarf_form form = DW_FORM_line_strp;
33460 debug_line_str_hash->traverse<enum dwarf_form,
33461 output_indirect_string> (form);
33464 /* Switch back to the text section. */
33465 switch_to_section (text_section);
33468 /* Reset all state within dwarf2out.cc so that we can rerun the compiler
33469 within the same process. For use by toplev::finalize. */
33471 void
33472 dwarf2out_cc_finalize (void)
33474 last_var_location_insn = NULL;
33475 cached_next_real_insn = NULL;
33476 used_rtx_array = NULL;
33477 incomplete_types = NULL;
33478 debug_info_section = NULL;
33479 debug_skeleton_info_section = NULL;
33480 debug_abbrev_section = NULL;
33481 debug_skeleton_abbrev_section = NULL;
33482 debug_aranges_section = NULL;
33483 debug_addr_section = NULL;
33484 debug_macinfo_section = NULL;
33485 debug_line_section = NULL;
33486 debug_skeleton_line_section = NULL;
33487 debug_loc_section = NULL;
33488 debug_pubnames_section = NULL;
33489 debug_pubtypes_section = NULL;
33490 debug_str_section = NULL;
33491 debug_line_str_section = NULL;
33492 debug_str_dwo_section = NULL;
33493 debug_str_offsets_section = NULL;
33494 debug_ranges_section = NULL;
33495 debug_ranges_dwo_section = NULL;
33496 debug_frame_section = NULL;
33497 fde_vec = NULL;
33498 debug_str_hash = NULL;
33499 debug_line_str_hash = NULL;
33500 skeleton_debug_str_hash = NULL;
33501 dw2_string_counter = 0;
33502 have_multiple_function_sections = false;
33503 in_text_section_p = false;
33504 cold_text_section = NULL;
33505 last_text_label = NULL;
33506 last_cold_label = NULL;
33507 switch_text_ranges = NULL;
33508 switch_cold_ranges = NULL;
33509 current_unit_personality = NULL;
33511 early_dwarf = false;
33512 early_dwarf_finished = false;
33514 next_die_offset = 0;
33515 single_comp_unit_die = NULL;
33516 comdat_type_list = NULL;
33517 limbo_die_list = NULL;
33518 file_table = NULL;
33519 decl_die_table = NULL;
33520 common_block_die_table = NULL;
33521 decl_loc_table = NULL;
33522 call_arg_locations = NULL;
33523 call_arg_loc_last = NULL;
33524 call_site_count = -1;
33525 tail_call_site_count = -1;
33526 cached_dw_loc_list_table = NULL;
33527 abbrev_die_table = NULL;
33528 delete dwarf_proc_stack_usage_map;
33529 dwarf_proc_stack_usage_map = NULL;
33530 line_info_label_num = 0;
33531 cur_line_info_table = NULL;
33532 text_section_line_info = NULL;
33533 cold_text_section_line_info = NULL;
33534 separate_line_info = NULL;
33535 info_section_emitted = false;
33536 pubname_table = NULL;
33537 pubtype_table = NULL;
33538 macinfo_table = NULL;
33539 ranges_table = NULL;
33540 ranges_by_label = NULL;
33541 rnglist_idx = 0;
33542 have_location_lists = false;
33543 loclabel_num = 0;
33544 poc_label_num = 0;
33545 last_emitted_file = NULL;
33546 label_num = 0;
33547 tmpl_value_parm_die_table = NULL;
33548 generic_type_instances = NULL;
33549 frame_pointer_fb_offset = 0;
33550 frame_pointer_fb_offset_valid = false;
33551 base_types.release ();
33552 XDELETEVEC (producer_string);
33553 producer_string = NULL;
33554 output_line_info_generation = 0;
33555 init_sections_and_labels_generation = 0;
33558 #include "gt-dwarf2out.h"