[PATCH v2 2/4] Output file checksums in CodeView section
[official-gcc.git] / gcc / dwarf2out.cc
blob43ef714b75e5a520d5d8049fd980eeaac729b919
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2024 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 (enum dwarf_call_frame_info cfi)
521 switch (cfi)
523 case DW_CFA_nop:
524 case DW_CFA_GNU_window_save:
525 case DW_CFA_remember_state:
526 case DW_CFA_restore_state:
527 return dw_cfi_oprnd_unused;
529 case DW_CFA_set_loc:
530 case DW_CFA_advance_loc1:
531 case DW_CFA_advance_loc2:
532 case DW_CFA_advance_loc4:
533 case DW_CFA_MIPS_advance_loc8:
534 return dw_cfi_oprnd_addr;
536 case DW_CFA_offset:
537 case DW_CFA_offset_extended:
538 case DW_CFA_def_cfa:
539 case DW_CFA_offset_extended_sf:
540 case DW_CFA_def_cfa_sf:
541 case DW_CFA_restore:
542 case DW_CFA_restore_extended:
543 case DW_CFA_undefined:
544 case DW_CFA_same_value:
545 case DW_CFA_def_cfa_register:
546 case DW_CFA_register:
547 case DW_CFA_expression:
548 case DW_CFA_val_expression:
549 return dw_cfi_oprnd_reg_num;
551 case DW_CFA_def_cfa_offset:
552 case DW_CFA_GNU_args_size:
553 case DW_CFA_def_cfa_offset_sf:
554 return dw_cfi_oprnd_offset;
556 case DW_CFA_def_cfa_expression:
557 return dw_cfi_oprnd_loc;
559 default:
560 gcc_unreachable ();
564 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
566 enum dw_cfi_oprnd_type
567 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
569 switch (cfi)
571 case DW_CFA_def_cfa:
572 case DW_CFA_def_cfa_sf:
573 case DW_CFA_offset:
574 case DW_CFA_offset_extended_sf:
575 case DW_CFA_offset_extended:
576 return dw_cfi_oprnd_offset;
578 case DW_CFA_register:
579 return dw_cfi_oprnd_reg_num;
581 case DW_CFA_expression:
582 case DW_CFA_val_expression:
583 return dw_cfi_oprnd_loc;
585 case DW_CFA_def_cfa_expression:
586 return dw_cfi_oprnd_cfa_loc;
588 default:
589 return dw_cfi_oprnd_unused;
593 /* Output one FDE. */
595 static void
596 output_fde (dw_fde_ref fde, bool for_eh, bool second,
597 char *section_start_label, int fde_encoding, char *augmentation,
598 bool any_lsda_needed, int lsda_encoding)
600 const char *begin, *end;
601 static unsigned int j;
602 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
604 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
605 /* empty */ 0);
606 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
607 for_eh + j);
608 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
609 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
610 if (!XCOFF_DEBUGGING_INFO || for_eh)
612 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
613 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
614 " indicating 64-bit DWARF extension");
615 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
616 "FDE Length");
618 ASM_OUTPUT_LABEL (asm_out_file, l1);
620 if (for_eh)
621 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
622 else
623 dw2_asm_output_offset (dwarf_offset_size, section_start_label,
624 debug_frame_section, "FDE CIE offset");
626 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
627 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
629 if (for_eh)
631 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
632 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
633 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
634 "FDE initial location");
635 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
636 end, begin, "FDE address range");
638 else
640 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
641 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
644 if (augmentation[0])
646 if (any_lsda_needed)
648 int size = size_of_encoded_value (lsda_encoding);
650 if (lsda_encoding == DW_EH_PE_aligned)
652 int offset = ( 4 /* Length */
653 + 4 /* CIE offset */
654 + 2 * size_of_encoded_value (fde_encoding)
655 + 1 /* Augmentation size */ );
656 int pad = -offset & (PTR_SIZE - 1);
658 size += pad;
659 gcc_assert (size_of_uleb128 (size) == 1);
662 dw2_asm_output_data_uleb128 (size, "Augmentation size");
664 if (fde->uses_eh_lsda)
666 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
667 fde->funcdef_number);
668 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
669 gen_rtx_SYMBOL_REF (Pmode, l1),
670 false,
671 "Language Specific Data Area");
673 else
675 if (lsda_encoding == DW_EH_PE_aligned)
676 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
677 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
678 "Language Specific Data Area (none)");
681 else
682 dw2_asm_output_data_uleb128 (0, "Augmentation size");
685 /* Loop through the Call Frame Instructions associated with this FDE. */
686 fde->dw_fde_current_label = begin;
688 size_t from, until, i;
690 from = 0;
691 until = vec_safe_length (fde->dw_fde_cfi);
693 if (fde->dw_fde_second_begin == NULL)
695 else if (!second)
696 until = fde->dw_fde_switch_cfi_index;
697 else
698 from = fde->dw_fde_switch_cfi_index;
700 for (i = from; i < until; i++)
701 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
704 /* If we are to emit a ref/link from function bodies to their frame tables,
705 do it now. This is typically performed to make sure that tables
706 associated with functions are dragged with them and not discarded in
707 garbage collecting links. We need to do this on a per function basis to
708 cope with -ffunction-sections. */
710 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
711 /* Switch to the function section, emit the ref to the tables, and
712 switch *back* into the table section. */
713 switch_to_section (function_section (fde->decl));
714 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
715 switch_to_frame_table_section (for_eh, true);
716 #endif
718 /* Pad the FDE out to an address sized boundary. */
719 ASM_OUTPUT_ALIGN (asm_out_file,
720 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
721 ASM_OUTPUT_LABEL (asm_out_file, l2);
723 j += 2;
726 /* Return true if frame description entry FDE is needed for EH. */
728 static bool
729 fde_needed_for_eh_p (dw_fde_ref fde)
731 if (flag_asynchronous_unwind_tables)
732 return true;
734 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
735 return true;
737 if (fde->uses_eh_lsda)
738 return true;
740 /* If exceptions are enabled, we have collected nothrow info. */
741 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
742 return false;
744 return true;
747 /* Output the call frame information used to record information
748 that relates to calculating the frame pointer, and records the
749 location of saved registers. */
751 static void
752 output_call_frame_info (int for_eh)
754 unsigned int i;
755 dw_fde_ref fde;
756 dw_cfi_ref cfi;
757 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
758 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
759 bool any_lsda_needed = false;
760 char augmentation[6];
761 int augmentation_size;
762 int fde_encoding = DW_EH_PE_absptr;
763 int per_encoding = DW_EH_PE_absptr;
764 int lsda_encoding = DW_EH_PE_absptr;
765 int return_reg;
766 rtx personality = NULL;
767 int dw_cie_version;
769 /* Don't emit a CIE if there won't be any FDEs. */
770 if (!fde_vec)
771 return;
773 /* Nothing to do if the assembler's doing it all. */
774 if (dwarf2out_do_cfi_asm ())
775 return;
777 /* If we don't have any functions we'll want to unwind out of, don't emit
778 any EH unwind information. If we make FDEs linkonce, we may have to
779 emit an empty label for an FDE that wouldn't otherwise be emitted. We
780 want to avoid having an FDE kept around when the function it refers to
781 is discarded. Example where this matters: a primary function template
782 in C++ requires EH information, an explicit specialization doesn't. */
783 if (for_eh)
785 bool any_eh_needed = false;
787 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
789 if (fde->uses_eh_lsda)
790 any_eh_needed = any_lsda_needed = true;
791 else if (fde_needed_for_eh_p (fde))
792 any_eh_needed = true;
793 else if (TARGET_USES_WEAK_UNWIND_INFO)
794 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
797 if (!any_eh_needed)
798 return;
801 /* We're going to be generating comments, so turn on app. */
802 if (flag_debug_asm)
803 app_enable ();
805 /* Switch to the proper frame section, first time. */
806 switch_to_frame_table_section (for_eh, false);
808 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
809 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
811 /* Output the CIE. */
812 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
813 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
814 if (!XCOFF_DEBUGGING_INFO || for_eh)
816 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
817 dw2_asm_output_data (4, 0xffffffff,
818 "Initial length escape value indicating 64-bit DWARF extension");
819 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
820 "Length of Common Information Entry");
822 ASM_OUTPUT_LABEL (asm_out_file, l1);
824 /* Now that the CIE pointer is PC-relative for EH,
825 use 0 to identify the CIE. */
826 dw2_asm_output_data ((for_eh ? 4 : dwarf_offset_size),
827 (for_eh ? 0 : DWARF_CIE_ID),
828 "CIE Identifier Tag");
830 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
831 use CIE version 1, unless that would produce incorrect results
832 due to overflowing the return register column. */
833 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
834 dw_cie_version = 1;
835 if (return_reg >= 256 || dwarf_version > 2)
836 dw_cie_version = 3;
837 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
839 augmentation[0] = 0;
840 augmentation_size = 0;
842 personality = current_unit_personality;
843 if (for_eh)
845 char *p;
847 /* Augmentation:
848 z Indicates that a uleb128 is present to size the
849 augmentation section.
850 L Indicates the encoding (and thus presence) of
851 an LSDA pointer in the FDE augmentation.
852 R Indicates a non-default pointer encoding for
853 FDE code pointers.
854 P Indicates the presence of an encoding + language
855 personality routine in the CIE augmentation. */
857 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
858 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
859 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
861 p = augmentation + 1;
862 if (personality)
864 *p++ = 'P';
865 augmentation_size += 1 + size_of_encoded_value (per_encoding);
866 assemble_external_libcall (personality);
868 if (any_lsda_needed)
870 *p++ = 'L';
871 augmentation_size += 1;
873 if (fde_encoding != DW_EH_PE_absptr)
875 *p++ = 'R';
876 augmentation_size += 1;
878 if (p > augmentation + 1)
880 augmentation[0] = 'z';
881 *p = '\0';
884 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
885 if (personality && per_encoding == DW_EH_PE_aligned)
887 int offset = ( 4 /* Length */
888 + 4 /* CIE Id */
889 + 1 /* CIE version */
890 + strlen (augmentation) + 1 /* Augmentation */
891 + size_of_uleb128 (1) /* Code alignment */
892 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
893 + 1 /* RA column */
894 + 1 /* Augmentation size */
895 + 1 /* Personality encoding */ );
896 int pad = -offset & (PTR_SIZE - 1);
898 augmentation_size += pad;
900 /* Augmentations should be small, so there's scarce need to
901 iterate for a solution. Die if we exceed one uleb128 byte. */
902 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
906 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
907 if (dw_cie_version >= 4)
909 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
910 dw2_asm_output_data (1, 0, "CIE Segment Size");
912 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
913 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
914 "CIE Data Alignment Factor");
916 if (dw_cie_version == 1)
917 dw2_asm_output_data (1, return_reg, "CIE RA Column");
918 else
919 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
921 if (augmentation[0])
923 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
924 if (personality)
926 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
927 eh_data_format_name (per_encoding));
928 dw2_asm_output_encoded_addr_rtx (per_encoding,
929 personality,
930 true, NULL);
933 if (any_lsda_needed)
934 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
935 eh_data_format_name (lsda_encoding));
937 if (fde_encoding != DW_EH_PE_absptr)
938 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
939 eh_data_format_name (fde_encoding));
942 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
943 output_cfi (cfi, NULL, for_eh);
945 /* Pad the CIE out to an address sized boundary. */
946 ASM_OUTPUT_ALIGN (asm_out_file,
947 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
948 ASM_OUTPUT_LABEL (asm_out_file, l2);
950 /* Loop through all of the FDE's. */
951 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
953 unsigned int k;
955 /* Don't emit EH unwind info for leaf functions that don't need it. */
956 if (for_eh && !fde_needed_for_eh_p (fde))
957 continue;
959 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
960 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
961 augmentation, any_lsda_needed, lsda_encoding);
964 if (for_eh && targetm.terminate_dw2_eh_frame_info)
965 dw2_asm_output_data (4, 0, "End of Table");
967 /* Turn off app to make assembly quicker. */
968 if (flag_debug_asm)
969 app_disable ();
972 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
974 static void
975 dwarf2out_do_cfi_startproc (bool second)
977 int enc;
978 rtx ref;
980 fprintf (asm_out_file, "\t.cfi_startproc\n");
982 targetm.asm_out.post_cfi_startproc (asm_out_file, current_function_decl);
984 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
985 eh unwinders. */
986 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
987 return;
989 rtx personality = get_personality_function (current_function_decl);
991 if (personality)
993 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
994 ref = personality;
996 /* ??? The GAS support isn't entirely consistent. We have to
997 handle indirect support ourselves, but PC-relative is done
998 in the assembler. Further, the assembler can't handle any
999 of the weirder relocation types. */
1000 if (enc & DW_EH_PE_indirect)
1002 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1003 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1004 else
1005 ref = dw2_force_const_mem (ref, true);
1008 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
1009 output_addr_const (asm_out_file, ref);
1010 fputc ('\n', asm_out_file);
1013 if (crtl->uses_eh_lsda)
1015 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1017 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1018 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1019 current_function_funcdef_no);
1020 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1021 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1023 if (enc & DW_EH_PE_indirect)
1025 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1026 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1027 else
1028 ref = dw2_force_const_mem (ref, true);
1031 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1032 output_addr_const (asm_out_file, ref);
1033 fputc ('\n', asm_out_file);
1037 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1038 this allocation may be done before pass_final. */
1040 dw_fde_ref
1041 dwarf2out_alloc_current_fde (void)
1043 dw_fde_ref fde;
1045 fde = ggc_cleared_alloc<dw_fde_node> ();
1046 fde->decl = current_function_decl;
1047 fde->funcdef_number = current_function_funcdef_no;
1048 fde->fde_index = vec_safe_length (fde_vec);
1049 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1050 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1051 fde->nothrow = crtl->nothrow;
1052 fde->drap_reg = INVALID_REGNUM;
1053 fde->vdrap_reg = INVALID_REGNUM;
1055 /* Record the FDE associated with this function. */
1056 cfun->fde = fde;
1057 vec_safe_push (fde_vec, fde);
1059 return fde;
1062 /* Output a marker (i.e. a label) for the beginning of a function, before
1063 the prologue. */
1065 void
1066 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1067 unsigned int column ATTRIBUTE_UNUSED,
1068 const char *file ATTRIBUTE_UNUSED)
1070 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1071 char * dup_label;
1072 dw_fde_ref fde;
1073 section *fnsec;
1074 bool do_frame;
1076 current_function_func_begin_label = NULL;
1078 do_frame = dwarf2out_do_frame ();
1080 /* ??? current_function_func_begin_label is also used by except.cc for
1081 call-site information. We must emit this label if it might be used. */
1082 if (!do_frame
1083 && (!flag_exceptions
1084 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1085 return;
1087 fnsec = function_section (current_function_decl);
1088 switch_to_section (fnsec);
1089 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1090 current_function_funcdef_no);
1091 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1092 current_function_funcdef_no);
1093 dup_label = xstrdup (label);
1094 current_function_func_begin_label = dup_label;
1096 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1097 if (!do_frame)
1098 return;
1100 /* Unlike the debug version, the EH version of frame unwind info is a per-
1101 function setting so we need to record whether we need it for the unit. */
1102 do_eh_frame |= dwarf2out_do_eh_frame ();
1104 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1105 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1106 would include pass_dwarf2_frame. If we've not created the FDE yet,
1107 do so now. */
1108 fde = cfun->fde;
1109 if (fde == NULL)
1110 fde = dwarf2out_alloc_current_fde ();
1112 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1113 fde->dw_fde_begin = dup_label;
1114 fde->dw_fde_current_label = dup_label;
1115 fde->in_std_section = (fnsec == text_section
1116 || (cold_text_section && fnsec == cold_text_section));
1117 fde->ignored_debug = DECL_IGNORED_P (current_function_decl);
1118 in_text_section_p = fnsec == text_section;
1120 /* We only want to output line number information for the genuine dwarf2
1121 prologue case, not the eh frame case. */
1122 #ifdef DWARF2_DEBUGGING_INFO
1123 if (file)
1124 dwarf2out_source_line (line, column, file, 0, true);
1125 #endif
1127 if (dwarf2out_do_cfi_asm ())
1128 dwarf2out_do_cfi_startproc (false);
1129 else
1131 rtx personality = get_personality_function (current_function_decl);
1132 if (!current_unit_personality)
1133 current_unit_personality = personality;
1135 /* We cannot keep a current personality per function as without CFI
1136 asm, at the point where we emit the CFI data, there is no current
1137 function anymore. */
1138 if (personality && current_unit_personality != personality)
1139 sorry ("multiple EH personalities are supported only with assemblers "
1140 "supporting %<.cfi_personality%> directive");
1144 /* Output a marker (i.e. a label) for the end of the generated code
1145 for a function prologue. This gets called *after* the prologue code has
1146 been generated. */
1148 void
1149 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1150 const char *file ATTRIBUTE_UNUSED)
1152 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1154 /* Output a label to mark the endpoint of the code generated for this
1155 function. */
1156 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1157 current_function_funcdef_no);
1158 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1159 current_function_funcdef_no);
1160 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1163 /* Output a marker (i.e. a label) for the beginning of the generated code
1164 for a function epilogue. This gets called *before* the prologue code has
1165 been generated. */
1167 void
1168 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1169 const char *file ATTRIBUTE_UNUSED)
1171 dw_fde_ref fde = cfun->fde;
1172 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1174 if (fde->dw_fde_vms_begin_epilogue)
1175 return;
1177 /* Output a label to mark the endpoint of the code generated for this
1178 function. */
1179 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1180 current_function_funcdef_no);
1181 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1182 current_function_funcdef_no);
1183 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1186 /* Mark the ranges of non-debug subsections in the std text sections. */
1188 static void
1189 mark_ignored_debug_section (dw_fde_ref fde, bool second)
1191 bool std_section;
1192 const char *begin_label, *end_label;
1193 const char **last_end_label;
1194 vec<const char *, va_gc> **switch_ranges;
1196 if (second)
1198 std_section = fde->second_in_std_section;
1199 begin_label = fde->dw_fde_second_begin;
1200 end_label = fde->dw_fde_second_end;
1202 else
1204 std_section = fde->in_std_section;
1205 begin_label = fde->dw_fde_begin;
1206 end_label = fde->dw_fde_end;
1209 if (!std_section)
1210 return;
1212 if (in_text_section_p)
1214 last_end_label = &last_text_label;
1215 switch_ranges = &switch_text_ranges;
1217 else
1219 last_end_label = &last_cold_label;
1220 switch_ranges = &switch_cold_ranges;
1223 if (fde->ignored_debug)
1225 if (*switch_ranges && !(vec_safe_length (*switch_ranges) & 1))
1226 vec_safe_push (*switch_ranges, *last_end_label);
1228 else
1230 *last_end_label = end_label;
1232 if (!*switch_ranges)
1233 vec_alloc (*switch_ranges, 16);
1234 else if (vec_safe_length (*switch_ranges) & 1)
1235 vec_safe_push (*switch_ranges, begin_label);
1239 /* Output a marker (i.e. a label) for the absolute end of the generated code
1240 for a function definition. This gets called *after* the epilogue code has
1241 been generated. */
1243 void
1244 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1245 const char *file ATTRIBUTE_UNUSED)
1247 dw_fde_ref fde;
1248 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1250 last_var_location_insn = NULL;
1251 cached_next_real_insn = NULL;
1253 if (dwarf2out_do_cfi_asm ())
1254 fprintf (asm_out_file, "\t.cfi_endproc\n");
1256 /* Output a label to mark the endpoint of the code generated for this
1257 function. */
1258 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1259 current_function_funcdef_no);
1260 ASM_OUTPUT_LABEL (asm_out_file, label);
1261 fde = cfun->fde;
1262 gcc_assert (fde != NULL);
1263 if (fde->dw_fde_second_begin == NULL)
1264 fde->dw_fde_end = xstrdup (label);
1266 mark_ignored_debug_section (fde, fde->dw_fde_second_begin != NULL);
1269 void
1270 dwarf2out_frame_finish (void)
1272 /* Output call frame information. */
1273 if (targetm.debug_unwind_info () == UI_DWARF2)
1274 output_call_frame_info (0);
1276 /* Output another copy for the unwinder. */
1277 if (do_eh_frame)
1278 output_call_frame_info (1);
1281 static void var_location_switch_text_section (void);
1282 static void set_cur_line_info_table (section *);
1284 void
1285 dwarf2out_switch_text_section (void)
1287 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1288 section *sect;
1289 dw_fde_ref fde = cfun->fde;
1291 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1293 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1294 current_function_funcdef_no);
1296 fde->dw_fde_second_begin = ggc_strdup (label);
1297 if (!in_cold_section_p)
1299 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1300 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1302 else
1304 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1305 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1307 have_multiple_function_sections = true;
1309 if (dwarf2out_do_cfi_asm ())
1310 fprintf (asm_out_file, "\t.cfi_endproc\n");
1312 mark_ignored_debug_section (fde, false);
1314 /* Now do the real section switch. */
1315 sect = current_function_section ();
1316 switch_to_section (sect);
1318 fde->second_in_std_section
1319 = (sect == text_section
1320 || (cold_text_section && sect == cold_text_section));
1321 in_text_section_p = sect == text_section;
1323 if (dwarf2out_do_cfi_asm ())
1324 dwarf2out_do_cfi_startproc (true);
1326 var_location_switch_text_section ();
1328 if (cold_text_section != NULL)
1329 set_cur_line_info_table (sect);
1332 /* And now, the subset of the debugging information support code necessary
1333 for emitting location expressions. */
1335 /* Describe an entry into the .debug_addr section. */
1337 enum ate_kind {
1338 ate_kind_rtx,
1339 ate_kind_rtx_dtprel,
1340 ate_kind_label
1343 struct GTY((for_user)) addr_table_entry {
1344 enum ate_kind kind;
1345 unsigned int refcount;
1346 unsigned int index;
1347 union addr_table_entry_struct_union
1349 rtx GTY ((tag ("0"))) rtl;
1350 char * GTY ((tag ("1"))) label;
1352 GTY ((desc ("%1.kind"))) addr;
1355 typedef unsigned int var_loc_view;
1357 /* Location lists are ranges + location descriptions for that range,
1358 so you can track variables that are in different places over
1359 their entire life. */
1360 typedef struct GTY(()) dw_loc_list_struct {
1361 dw_loc_list_ref dw_loc_next;
1362 const char *begin; /* Label and addr_entry for start of range */
1363 addr_table_entry *begin_entry;
1364 const char *end; /* Label for end of range */
1365 addr_table_entry *end_entry;
1366 char *ll_symbol; /* Label for beginning of location list.
1367 Only on head of list. */
1368 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1369 const char *section; /* Section this loclist is relative to */
1370 dw_loc_descr_ref expr;
1371 var_loc_view vbegin, vend;
1372 hashval_t hash;
1373 /* True if all addresses in this and subsequent lists are known to be
1374 resolved. */
1375 bool resolved_addr;
1376 /* True if this list has been replaced by dw_loc_next. */
1377 bool replaced;
1378 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1379 section. */
1380 unsigned char emitted : 1;
1381 /* True if hash field is index rather than hash value. */
1382 unsigned char num_assigned : 1;
1383 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1384 unsigned char offset_emitted : 1;
1385 /* True if note_variable_value_in_expr has been called on it. */
1386 unsigned char noted_variable_value : 1;
1387 /* True if the range should be emitted even if begin and end
1388 are the same. */
1389 bool force;
1390 } dw_loc_list_node;
1392 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1393 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1395 /* Convert a DWARF stack opcode into its string name. */
1397 static const char *
1398 dwarf_stack_op_name (unsigned int op)
1400 const char *name = get_DW_OP_name (op);
1402 if (name != NULL)
1403 return name;
1405 return "OP_<unknown>";
1408 /* Return TRUE iff we're to output location view lists as a separate
1409 attribute next to the location lists, as an extension compatible
1410 with DWARF 2 and above. */
1412 static inline bool
1413 dwarf2out_locviews_in_attribute ()
1415 return debug_variable_location_views == 1;
1418 /* Return TRUE iff we're to output location view lists as part of the
1419 location lists, as proposed for standardization after DWARF 5. */
1421 static inline bool
1422 dwarf2out_locviews_in_loclist ()
1424 #ifndef DW_LLE_view_pair
1425 return false;
1426 #else
1427 return debug_variable_location_views == -1;
1428 #endif
1431 /* Return a pointer to a newly allocated location description. Location
1432 descriptions are simple expression terms that can be strung
1433 together to form more complicated location (address) descriptions. */
1435 static inline dw_loc_descr_ref
1436 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1437 unsigned HOST_WIDE_INT oprnd2)
1439 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1441 descr->dw_loc_opc = op;
1442 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1443 descr->dw_loc_oprnd1.val_entry = NULL;
1444 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1445 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1446 descr->dw_loc_oprnd2.val_entry = NULL;
1447 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1449 return descr;
1452 /* Add a location description term to a location description expression. */
1454 static inline void
1455 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1457 dw_loc_descr_ref *d;
1459 /* Find the end of the chain. */
1460 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1463 *d = descr;
1466 /* Compare two location operands for exact equality. */
1468 static bool
1469 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1471 if (a->val_class != b->val_class)
1472 return false;
1473 switch (a->val_class)
1475 case dw_val_class_none:
1476 return true;
1477 case dw_val_class_addr:
1478 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1480 case dw_val_class_offset:
1481 case dw_val_class_unsigned_const:
1482 case dw_val_class_const:
1483 case dw_val_class_unsigned_const_implicit:
1484 case dw_val_class_const_implicit:
1485 case dw_val_class_range_list:
1486 /* These are all HOST_WIDE_INT, signed or unsigned. */
1487 return a->v.val_unsigned == b->v.val_unsigned;
1489 case dw_val_class_loc:
1490 return a->v.val_loc == b->v.val_loc;
1491 case dw_val_class_loc_list:
1492 return a->v.val_loc_list == b->v.val_loc_list;
1493 case dw_val_class_view_list:
1494 return a->v.val_view_list == b->v.val_view_list;
1495 case dw_val_class_die_ref:
1496 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1497 case dw_val_class_fde_ref:
1498 return a->v.val_fde_index == b->v.val_fde_index;
1499 case dw_val_class_symview:
1500 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1501 case dw_val_class_lbl_id:
1502 case dw_val_class_lineptr:
1503 case dw_val_class_macptr:
1504 case dw_val_class_loclistsptr:
1505 case dw_val_class_high_pc:
1506 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1507 case dw_val_class_str:
1508 return a->v.val_str == b->v.val_str;
1509 case dw_val_class_flag:
1510 return a->v.val_flag == b->v.val_flag;
1511 case dw_val_class_file:
1512 case dw_val_class_file_implicit:
1513 return a->v.val_file == b->v.val_file;
1514 case dw_val_class_decl_ref:
1515 return a->v.val_decl_ref == b->v.val_decl_ref;
1517 case dw_val_class_const_double:
1518 return (a->v.val_double.high == b->v.val_double.high
1519 && a->v.val_double.low == b->v.val_double.low);
1521 case dw_val_class_wide_int:
1522 return *a->v.val_wide == *b->v.val_wide;
1524 case dw_val_class_vec:
1526 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1527 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1529 return (a_len == b_len
1530 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1533 case dw_val_class_data8:
1534 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1536 case dw_val_class_vms_delta:
1537 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1538 && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1540 case dw_val_class_discr_value:
1541 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1542 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1543 case dw_val_class_discr_list:
1544 /* It makes no sense comparing two discriminant value lists. */
1545 return false;
1547 gcc_unreachable ();
1550 /* Compare two location atoms for exact equality. */
1552 static bool
1553 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1555 if (a->dw_loc_opc != b->dw_loc_opc)
1556 return false;
1558 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1559 address size, but since we always allocate cleared storage it
1560 should be zero for other types of locations. */
1561 if (a->dtprel != b->dtprel)
1562 return false;
1564 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1565 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1568 /* Compare two complete location expressions for exact equality. */
1570 bool
1571 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1573 while (1)
1575 if (a == b)
1576 return true;
1577 if (a == NULL || b == NULL)
1578 return false;
1579 if (!loc_descr_equal_p_1 (a, b))
1580 return false;
1582 a = a->dw_loc_next;
1583 b = b->dw_loc_next;
1588 /* Add a constant POLY_OFFSET to a location expression. */
1590 static void
1591 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1593 dw_loc_descr_ref loc;
1594 HOST_WIDE_INT *p;
1596 gcc_assert (*list_head != NULL);
1598 if (known_eq (poly_offset, 0))
1599 return;
1601 /* Find the end of the chain. */
1602 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1605 HOST_WIDE_INT offset;
1606 if (!poly_offset.is_constant (&offset))
1608 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1609 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1610 return;
1613 p = NULL;
1614 if (loc->dw_loc_opc == DW_OP_fbreg
1615 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1616 p = &loc->dw_loc_oprnd1.v.val_int;
1617 else if (loc->dw_loc_opc == DW_OP_bregx)
1618 p = &loc->dw_loc_oprnd2.v.val_int;
1620 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1621 offset. Don't optimize if an signed integer overflow would happen. */
1622 if (p != NULL
1623 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1624 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1625 *p += offset;
1627 else if (offset > 0)
1628 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1630 else
1632 loc->dw_loc_next
1633 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1634 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1638 /* Return a pointer to a newly allocated location description for
1639 REG and OFFSET. */
1641 static inline dw_loc_descr_ref
1642 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1644 HOST_WIDE_INT const_offset;
1645 if (offset.is_constant (&const_offset))
1647 if (reg <= 31)
1648 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1649 const_offset, 0);
1650 else
1651 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1653 else
1655 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1656 loc_descr_plus_const (&ret, offset);
1657 return ret;
1661 /* Add a constant OFFSET to a location list. */
1663 static void
1664 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1666 dw_loc_list_ref d;
1667 for (d = list_head; d != NULL; d = d->dw_loc_next)
1668 loc_descr_plus_const (&d->expr, offset);
1671 #define DWARF_REF_SIZE \
1672 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : dwarf_offset_size)
1674 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1675 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1676 DW_FORM_data16 with 128 bits. */
1677 #define DWARF_LARGEST_DATA_FORM_BITS \
1678 (dwarf_version >= 5 ? 128 : 64)
1680 /* Utility inline function for construction of ops that were GNU extension
1681 before DWARF 5. */
1682 static inline enum dwarf_location_atom
1683 dwarf_OP (enum dwarf_location_atom op)
1685 switch (op)
1687 case DW_OP_implicit_pointer:
1688 if (dwarf_version < 5)
1689 return DW_OP_GNU_implicit_pointer;
1690 break;
1692 case DW_OP_entry_value:
1693 if (dwarf_version < 5)
1694 return DW_OP_GNU_entry_value;
1695 break;
1697 case DW_OP_const_type:
1698 if (dwarf_version < 5)
1699 return DW_OP_GNU_const_type;
1700 break;
1702 case DW_OP_regval_type:
1703 if (dwarf_version < 5)
1704 return DW_OP_GNU_regval_type;
1705 break;
1707 case DW_OP_deref_type:
1708 if (dwarf_version < 5)
1709 return DW_OP_GNU_deref_type;
1710 break;
1712 case DW_OP_convert:
1713 if (dwarf_version < 5)
1714 return DW_OP_GNU_convert;
1715 break;
1717 case DW_OP_reinterpret:
1718 if (dwarf_version < 5)
1719 return DW_OP_GNU_reinterpret;
1720 break;
1722 case DW_OP_addrx:
1723 if (dwarf_version < 5)
1724 return DW_OP_GNU_addr_index;
1725 break;
1727 case DW_OP_constx:
1728 if (dwarf_version < 5)
1729 return DW_OP_GNU_const_index;
1730 break;
1732 default:
1733 break;
1735 return op;
1738 /* Similarly for attributes. */
1739 static inline enum dwarf_attribute
1740 dwarf_AT (enum dwarf_attribute at)
1742 switch (at)
1744 case DW_AT_call_return_pc:
1745 if (dwarf_version < 5)
1746 return DW_AT_low_pc;
1747 break;
1749 case DW_AT_call_tail_call:
1750 if (dwarf_version < 5)
1751 return DW_AT_GNU_tail_call;
1752 break;
1754 case DW_AT_call_origin:
1755 if (dwarf_version < 5)
1756 return DW_AT_abstract_origin;
1757 break;
1759 case DW_AT_call_target:
1760 if (dwarf_version < 5)
1761 return DW_AT_GNU_call_site_target;
1762 break;
1764 case DW_AT_call_target_clobbered:
1765 if (dwarf_version < 5)
1766 return DW_AT_GNU_call_site_target_clobbered;
1767 break;
1769 case DW_AT_call_parameter:
1770 if (dwarf_version < 5)
1771 return DW_AT_abstract_origin;
1772 break;
1774 case DW_AT_call_value:
1775 if (dwarf_version < 5)
1776 return DW_AT_GNU_call_site_value;
1777 break;
1779 case DW_AT_call_data_value:
1780 if (dwarf_version < 5)
1781 return DW_AT_GNU_call_site_data_value;
1782 break;
1784 case DW_AT_call_all_calls:
1785 if (dwarf_version < 5)
1786 return DW_AT_GNU_all_call_sites;
1787 break;
1789 case DW_AT_call_all_tail_calls:
1790 if (dwarf_version < 5)
1791 return DW_AT_GNU_all_tail_call_sites;
1792 break;
1794 case DW_AT_dwo_name:
1795 if (dwarf_version < 5)
1796 return DW_AT_GNU_dwo_name;
1797 break;
1799 case DW_AT_addr_base:
1800 if (dwarf_version < 5)
1801 return DW_AT_GNU_addr_base;
1802 break;
1804 default:
1805 break;
1807 return at;
1810 /* And similarly for tags. */
1811 static inline enum dwarf_tag
1812 dwarf_TAG (enum dwarf_tag tag)
1814 switch (tag)
1816 case DW_TAG_call_site:
1817 if (dwarf_version < 5)
1818 return DW_TAG_GNU_call_site;
1819 break;
1821 case DW_TAG_call_site_parameter:
1822 if (dwarf_version < 5)
1823 return DW_TAG_GNU_call_site_parameter;
1824 break;
1826 default:
1827 break;
1829 return tag;
1832 /* And similarly for forms. */
1833 static inline enum dwarf_form
1834 dwarf_FORM (enum dwarf_form form)
1836 switch (form)
1838 case DW_FORM_addrx:
1839 if (dwarf_version < 5)
1840 return DW_FORM_GNU_addr_index;
1841 break;
1843 case DW_FORM_strx:
1844 if (dwarf_version < 5)
1845 return DW_FORM_GNU_str_index;
1846 break;
1848 default:
1849 break;
1851 return form;
1854 static unsigned long int get_base_type_offset (dw_die_ref);
1856 /* Return the size of a location descriptor. */
1858 static unsigned long
1859 size_of_loc_descr (dw_loc_descr_ref loc)
1861 unsigned long size = 1;
1863 switch (loc->dw_loc_opc)
1865 case DW_OP_addr:
1866 size += DWARF2_ADDR_SIZE;
1867 break;
1868 case DW_OP_GNU_addr_index:
1869 case DW_OP_addrx:
1870 case DW_OP_GNU_const_index:
1871 case DW_OP_constx:
1872 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1873 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1874 break;
1875 case DW_OP_const1u:
1876 case DW_OP_const1s:
1877 size += 1;
1878 break;
1879 case DW_OP_const2u:
1880 case DW_OP_const2s:
1881 size += 2;
1882 break;
1883 case DW_OP_const4u:
1884 case DW_OP_const4s:
1885 size += 4;
1886 break;
1887 case DW_OP_const8u:
1888 case DW_OP_const8s:
1889 size += 8;
1890 break;
1891 case DW_OP_constu:
1892 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1893 break;
1894 case DW_OP_consts:
1895 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1896 break;
1897 case DW_OP_pick:
1898 size += 1;
1899 break;
1900 case DW_OP_plus_uconst:
1901 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1902 break;
1903 case DW_OP_skip:
1904 case DW_OP_bra:
1905 size += 2;
1906 break;
1907 case DW_OP_breg0:
1908 case DW_OP_breg1:
1909 case DW_OP_breg2:
1910 case DW_OP_breg3:
1911 case DW_OP_breg4:
1912 case DW_OP_breg5:
1913 case DW_OP_breg6:
1914 case DW_OP_breg7:
1915 case DW_OP_breg8:
1916 case DW_OP_breg9:
1917 case DW_OP_breg10:
1918 case DW_OP_breg11:
1919 case DW_OP_breg12:
1920 case DW_OP_breg13:
1921 case DW_OP_breg14:
1922 case DW_OP_breg15:
1923 case DW_OP_breg16:
1924 case DW_OP_breg17:
1925 case DW_OP_breg18:
1926 case DW_OP_breg19:
1927 case DW_OP_breg20:
1928 case DW_OP_breg21:
1929 case DW_OP_breg22:
1930 case DW_OP_breg23:
1931 case DW_OP_breg24:
1932 case DW_OP_breg25:
1933 case DW_OP_breg26:
1934 case DW_OP_breg27:
1935 case DW_OP_breg28:
1936 case DW_OP_breg29:
1937 case DW_OP_breg30:
1938 case DW_OP_breg31:
1939 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1940 break;
1941 case DW_OP_regx:
1942 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1943 break;
1944 case DW_OP_fbreg:
1945 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1946 break;
1947 case DW_OP_bregx:
1948 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1949 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1950 break;
1951 case DW_OP_piece:
1952 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1953 break;
1954 case DW_OP_bit_piece:
1955 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1956 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1957 break;
1958 case DW_OP_deref_size:
1959 case DW_OP_xderef_size:
1960 size += 1;
1961 break;
1962 case DW_OP_call2:
1963 size += 2;
1964 break;
1965 case DW_OP_call4:
1966 size += 4;
1967 break;
1968 case DW_OP_call_ref:
1969 case DW_OP_GNU_variable_value:
1970 size += DWARF_REF_SIZE;
1971 break;
1972 case DW_OP_implicit_value:
1973 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1974 + loc->dw_loc_oprnd1.v.val_unsigned;
1975 break;
1976 case DW_OP_implicit_pointer:
1977 case DW_OP_GNU_implicit_pointer:
1978 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1979 break;
1980 case DW_OP_entry_value:
1981 case DW_OP_GNU_entry_value:
1983 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1984 size += size_of_uleb128 (op_size) + op_size;
1985 break;
1987 case DW_OP_const_type:
1988 case DW_OP_GNU_const_type:
1990 unsigned long o
1991 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1992 size += size_of_uleb128 (o) + 1;
1993 switch (loc->dw_loc_oprnd2.val_class)
1995 case dw_val_class_vec:
1996 size += loc->dw_loc_oprnd2.v.val_vec.length
1997 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1998 break;
1999 case dw_val_class_const:
2000 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
2001 break;
2002 case dw_val_class_const_double:
2003 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
2004 break;
2005 case dw_val_class_wide_int:
2006 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
2007 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
2008 break;
2009 default:
2010 gcc_unreachable ();
2012 break;
2014 case DW_OP_regval_type:
2015 case DW_OP_GNU_regval_type:
2017 unsigned long o
2018 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2019 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
2020 + size_of_uleb128 (o);
2022 break;
2023 case DW_OP_deref_type:
2024 case DW_OP_GNU_deref_type:
2026 unsigned long o
2027 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2028 size += 1 + size_of_uleb128 (o);
2030 break;
2031 case DW_OP_convert:
2032 case DW_OP_reinterpret:
2033 case DW_OP_GNU_convert:
2034 case DW_OP_GNU_reinterpret:
2035 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2036 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2037 else
2039 unsigned long o
2040 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2041 size += size_of_uleb128 (o);
2043 break;
2044 case DW_OP_GNU_parameter_ref:
2045 size += 4;
2046 break;
2047 default:
2048 break;
2051 return size;
2054 /* Return the size of a series of location descriptors. */
2056 unsigned long
2057 size_of_locs (dw_loc_descr_ref loc)
2059 dw_loc_descr_ref l;
2060 unsigned long size;
2062 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2063 field, to avoid writing to a PCH file. */
2064 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2066 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2067 break;
2068 size += size_of_loc_descr (l);
2070 if (! l)
2071 return size;
2073 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2075 l->dw_loc_addr = size;
2076 size += size_of_loc_descr (l);
2079 return size;
2082 /* Return the size of the value in a DW_AT_discr_value attribute. */
2084 static int
2085 size_of_discr_value (dw_discr_value *discr_value)
2087 if (discr_value->pos)
2088 return size_of_uleb128 (discr_value->v.uval);
2089 else
2090 return size_of_sleb128 (discr_value->v.sval);
2093 /* Return the size of the value in a DW_AT_discr_list attribute. */
2095 static int
2096 size_of_discr_list (dw_discr_list_ref discr_list)
2098 int size = 0;
2100 for (dw_discr_list_ref list = discr_list;
2101 list != NULL;
2102 list = list->dw_discr_next)
2104 /* One byte for the discriminant value descriptor, and then one or two
2105 LEB128 numbers, depending on whether it's a single case label or a
2106 range label. */
2107 size += 1;
2108 size += size_of_discr_value (&list->dw_discr_lower_bound);
2109 if (list->dw_discr_range != 0)
2110 size += size_of_discr_value (&list->dw_discr_upper_bound);
2112 return size;
2115 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2116 static void get_ref_die_offset_label (char *, dw_die_ref);
2117 static unsigned long int get_ref_die_offset (dw_die_ref);
2119 /* Output location description stack opcode's operands (if any).
2120 The for_eh_or_skip parameter controls whether register numbers are
2121 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2122 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2123 info). This should be suppressed for the cases that have not been converted
2124 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2126 static void
2127 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2129 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2130 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2132 switch (loc->dw_loc_opc)
2134 #ifdef DWARF2_DEBUGGING_INFO
2135 case DW_OP_const2u:
2136 case DW_OP_const2s:
2137 dw2_asm_output_data (2, val1->v.val_int, NULL);
2138 break;
2139 case DW_OP_const4u:
2140 if (loc->dtprel)
2142 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2143 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2144 val1->v.val_addr);
2145 fputc ('\n', asm_out_file);
2146 break;
2148 /* FALLTHRU */
2149 case DW_OP_const4s:
2150 dw2_asm_output_data (4, val1->v.val_int, NULL);
2151 break;
2152 case DW_OP_const8u:
2153 if (loc->dtprel)
2155 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2156 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2157 val1->v.val_addr);
2158 fputc ('\n', asm_out_file);
2159 break;
2161 /* FALLTHRU */
2162 case DW_OP_const8s:
2163 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2164 dw2_asm_output_data (8, val1->v.val_int, NULL);
2165 break;
2166 case DW_OP_skip:
2167 case DW_OP_bra:
2169 int offset;
2171 gcc_assert (val1->val_class == dw_val_class_loc);
2172 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2174 dw2_asm_output_data (2, offset, NULL);
2176 break;
2177 case DW_OP_implicit_value:
2178 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2179 switch (val2->val_class)
2181 case dw_val_class_const:
2182 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2183 break;
2184 case dw_val_class_vec:
2186 unsigned int elt_size = val2->v.val_vec.elt_size;
2187 unsigned int len = val2->v.val_vec.length;
2188 unsigned int i;
2189 unsigned char *p;
2191 if (elt_size > sizeof (HOST_WIDE_INT))
2193 elt_size /= 2;
2194 len *= 2;
2196 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2197 i < len;
2198 i++, p += elt_size)
2199 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2200 "fp or vector constant word %u", i);
2202 break;
2203 case dw_val_class_const_double:
2205 unsigned HOST_WIDE_INT first, second;
2207 if (WORDS_BIG_ENDIAN)
2209 first = val2->v.val_double.high;
2210 second = val2->v.val_double.low;
2212 else
2214 first = val2->v.val_double.low;
2215 second = val2->v.val_double.high;
2217 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2218 first, NULL);
2219 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2220 second, NULL);
2222 break;
2223 case dw_val_class_wide_int:
2225 int i;
2226 int len = get_full_len (*val2->v.val_wide);
2227 if (WORDS_BIG_ENDIAN)
2228 for (i = len - 1; i >= 0; --i)
2229 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2230 val2->v.val_wide->elt (i), NULL);
2231 else
2232 for (i = 0; i < len; ++i)
2233 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2234 val2->v.val_wide->elt (i), NULL);
2236 break;
2237 case dw_val_class_addr:
2238 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2239 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2240 break;
2241 default:
2242 gcc_unreachable ();
2244 break;
2245 #else
2246 case DW_OP_const2u:
2247 case DW_OP_const2s:
2248 case DW_OP_const4u:
2249 case DW_OP_const4s:
2250 case DW_OP_const8u:
2251 case DW_OP_const8s:
2252 case DW_OP_skip:
2253 case DW_OP_bra:
2254 case DW_OP_implicit_value:
2255 /* We currently don't make any attempt to make sure these are
2256 aligned properly like we do for the main unwind info, so
2257 don't support emitting things larger than a byte if we're
2258 only doing unwinding. */
2259 gcc_unreachable ();
2260 #endif
2261 case DW_OP_const1u:
2262 case DW_OP_const1s:
2263 dw2_asm_output_data (1, val1->v.val_int, NULL);
2264 break;
2265 case DW_OP_constu:
2266 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2267 break;
2268 case DW_OP_consts:
2269 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2270 break;
2271 case DW_OP_pick:
2272 dw2_asm_output_data (1, val1->v.val_int, NULL);
2273 break;
2274 case DW_OP_plus_uconst:
2275 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2276 break;
2277 case DW_OP_breg0:
2278 case DW_OP_breg1:
2279 case DW_OP_breg2:
2280 case DW_OP_breg3:
2281 case DW_OP_breg4:
2282 case DW_OP_breg5:
2283 case DW_OP_breg6:
2284 case DW_OP_breg7:
2285 case DW_OP_breg8:
2286 case DW_OP_breg9:
2287 case DW_OP_breg10:
2288 case DW_OP_breg11:
2289 case DW_OP_breg12:
2290 case DW_OP_breg13:
2291 case DW_OP_breg14:
2292 case DW_OP_breg15:
2293 case DW_OP_breg16:
2294 case DW_OP_breg17:
2295 case DW_OP_breg18:
2296 case DW_OP_breg19:
2297 case DW_OP_breg20:
2298 case DW_OP_breg21:
2299 case DW_OP_breg22:
2300 case DW_OP_breg23:
2301 case DW_OP_breg24:
2302 case DW_OP_breg25:
2303 case DW_OP_breg26:
2304 case DW_OP_breg27:
2305 case DW_OP_breg28:
2306 case DW_OP_breg29:
2307 case DW_OP_breg30:
2308 case DW_OP_breg31:
2309 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2310 break;
2311 case DW_OP_regx:
2313 unsigned r = val1->v.val_unsigned;
2314 if (for_eh_or_skip >= 0)
2315 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2316 gcc_assert (size_of_uleb128 (r)
2317 == size_of_uleb128 (val1->v.val_unsigned));
2318 dw2_asm_output_data_uleb128 (r, NULL);
2320 break;
2321 case DW_OP_fbreg:
2322 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2323 break;
2324 case DW_OP_bregx:
2326 unsigned r = val1->v.val_unsigned;
2327 if (for_eh_or_skip >= 0)
2328 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2329 gcc_assert (size_of_uleb128 (r)
2330 == size_of_uleb128 (val1->v.val_unsigned));
2331 dw2_asm_output_data_uleb128 (r, NULL);
2332 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2334 break;
2335 case DW_OP_piece:
2336 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2337 break;
2338 case DW_OP_bit_piece:
2339 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2340 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2341 break;
2342 case DW_OP_deref_size:
2343 case DW_OP_xderef_size:
2344 dw2_asm_output_data (1, val1->v.val_int, NULL);
2345 break;
2347 case DW_OP_addr:
2348 if (loc->dtprel)
2350 if (targetm.asm_out.output_dwarf_dtprel)
2352 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2353 DWARF2_ADDR_SIZE,
2354 val1->v.val_addr);
2355 fputc ('\n', asm_out_file);
2357 else
2358 gcc_unreachable ();
2360 else
2362 #ifdef DWARF2_DEBUGGING_INFO
2363 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2364 #else
2365 gcc_unreachable ();
2366 #endif
2368 break;
2370 case DW_OP_GNU_addr_index:
2371 case DW_OP_addrx:
2372 case DW_OP_GNU_const_index:
2373 case DW_OP_constx:
2374 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2375 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2376 "(index into .debug_addr)");
2377 break;
2379 case DW_OP_call2:
2380 case DW_OP_call4:
2382 unsigned long die_offset
2383 = get_ref_die_offset (val1->v.val_die_ref.die);
2384 /* Make sure the offset has been computed and that we can encode it as
2385 an operand. */
2386 gcc_assert (die_offset > 0
2387 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2388 ? 0xffff
2389 : 0xffffffff));
2390 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2391 die_offset, NULL);
2393 break;
2395 case DW_OP_call_ref:
2396 case DW_OP_GNU_variable_value:
2398 char label[MAX_ARTIFICIAL_LABEL_BYTES
2399 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2400 gcc_assert (val1->val_class == dw_val_class_die_ref);
2401 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2402 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2404 break;
2406 case DW_OP_implicit_pointer:
2407 case DW_OP_GNU_implicit_pointer:
2409 char label[MAX_ARTIFICIAL_LABEL_BYTES
2410 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2411 gcc_assert (val1->val_class == dw_val_class_die_ref);
2412 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2413 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2414 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2416 break;
2418 case DW_OP_entry_value:
2419 case DW_OP_GNU_entry_value:
2420 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2421 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2422 break;
2424 case DW_OP_const_type:
2425 case DW_OP_GNU_const_type:
2427 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2428 gcc_assert (o);
2429 dw2_asm_output_data_uleb128 (o, NULL);
2430 switch (val2->val_class)
2432 case dw_val_class_const:
2433 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2434 dw2_asm_output_data (1, l, NULL);
2435 dw2_asm_output_data (l, val2->v.val_int, NULL);
2436 break;
2437 case dw_val_class_vec:
2439 unsigned int elt_size = val2->v.val_vec.elt_size;
2440 unsigned int len = val2->v.val_vec.length;
2441 unsigned int i;
2442 unsigned char *p;
2444 l = len * elt_size;
2445 dw2_asm_output_data (1, l, NULL);
2446 if (elt_size > sizeof (HOST_WIDE_INT))
2448 elt_size /= 2;
2449 len *= 2;
2451 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2452 i < len;
2453 i++, p += elt_size)
2454 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2455 "fp or vector constant word %u", i);
2457 break;
2458 case dw_val_class_const_double:
2460 unsigned HOST_WIDE_INT first, second;
2461 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2463 dw2_asm_output_data (1, 2 * l, NULL);
2464 if (WORDS_BIG_ENDIAN)
2466 first = val2->v.val_double.high;
2467 second = val2->v.val_double.low;
2469 else
2471 first = val2->v.val_double.low;
2472 second = val2->v.val_double.high;
2474 dw2_asm_output_data (l, first, NULL);
2475 dw2_asm_output_data (l, second, NULL);
2477 break;
2478 case dw_val_class_wide_int:
2480 int i;
2481 int len = get_full_len (*val2->v.val_wide);
2482 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2484 dw2_asm_output_data (1, len * l, NULL);
2485 if (WORDS_BIG_ENDIAN)
2486 for (i = len - 1; i >= 0; --i)
2487 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2488 else
2489 for (i = 0; i < len; ++i)
2490 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2492 break;
2493 default:
2494 gcc_unreachable ();
2497 break;
2498 case DW_OP_regval_type:
2499 case DW_OP_GNU_regval_type:
2501 unsigned r = val1->v.val_unsigned;
2502 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2503 gcc_assert (o);
2504 if (for_eh_or_skip >= 0)
2506 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2507 gcc_assert (size_of_uleb128 (r)
2508 == size_of_uleb128 (val1->v.val_unsigned));
2510 dw2_asm_output_data_uleb128 (r, NULL);
2511 dw2_asm_output_data_uleb128 (o, NULL);
2513 break;
2514 case DW_OP_deref_type:
2515 case DW_OP_GNU_deref_type:
2517 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2518 gcc_assert (o);
2519 dw2_asm_output_data (1, val1->v.val_int, NULL);
2520 dw2_asm_output_data_uleb128 (o, NULL);
2522 break;
2523 case DW_OP_convert:
2524 case DW_OP_reinterpret:
2525 case DW_OP_GNU_convert:
2526 case DW_OP_GNU_reinterpret:
2527 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2528 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2529 else
2531 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2532 gcc_assert (o);
2533 dw2_asm_output_data_uleb128 (o, NULL);
2535 break;
2537 case DW_OP_GNU_parameter_ref:
2539 unsigned long o;
2540 gcc_assert (val1->val_class == dw_val_class_die_ref);
2541 o = get_ref_die_offset (val1->v.val_die_ref.die);
2542 dw2_asm_output_data (4, o, NULL);
2544 break;
2546 default:
2547 /* Other codes have no operands. */
2548 break;
2552 /* Output a sequence of location operations.
2553 The for_eh_or_skip parameter controls whether register numbers are
2554 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2555 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2556 info). This should be suppressed for the cases that have not been converted
2557 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2559 void
2560 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2562 for (; loc != NULL; loc = loc->dw_loc_next)
2564 enum dwarf_location_atom opc = loc->dw_loc_opc;
2565 /* Output the opcode. */
2566 if (for_eh_or_skip >= 0
2567 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2569 unsigned r = (opc - DW_OP_breg0);
2570 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2571 gcc_assert (r <= 31);
2572 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2574 else if (for_eh_or_skip >= 0
2575 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2577 unsigned r = (opc - DW_OP_reg0);
2578 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2579 gcc_assert (r <= 31);
2580 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2583 dw2_asm_output_data (1, opc,
2584 "%s", dwarf_stack_op_name (opc));
2586 /* Output the operand(s) (if any). */
2587 output_loc_operands (loc, for_eh_or_skip);
2591 /* Output location description stack opcode's operands (if any).
2592 The output is single bytes on a line, suitable for .cfi_escape. */
2594 static void
2595 output_loc_operands_raw (dw_loc_descr_ref loc)
2597 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2598 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2600 switch (loc->dw_loc_opc)
2602 case DW_OP_addr:
2603 case DW_OP_GNU_addr_index:
2604 case DW_OP_addrx:
2605 case DW_OP_GNU_const_index:
2606 case DW_OP_constx:
2607 case DW_OP_implicit_value:
2608 /* We cannot output addresses in .cfi_escape, only bytes. */
2609 gcc_unreachable ();
2611 case DW_OP_const1u:
2612 case DW_OP_const1s:
2613 case DW_OP_pick:
2614 case DW_OP_deref_size:
2615 case DW_OP_xderef_size:
2616 fputc (',', asm_out_file);
2617 dw2_asm_output_data_raw (1, val1->v.val_int);
2618 break;
2620 case DW_OP_const2u:
2621 case DW_OP_const2s:
2622 fputc (',', asm_out_file);
2623 dw2_asm_output_data_raw (2, val1->v.val_int);
2624 break;
2626 case DW_OP_const4u:
2627 case DW_OP_const4s:
2628 fputc (',', asm_out_file);
2629 dw2_asm_output_data_raw (4, val1->v.val_int);
2630 break;
2632 case DW_OP_const8u:
2633 case DW_OP_const8s:
2634 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2635 fputc (',', asm_out_file);
2636 dw2_asm_output_data_raw (8, val1->v.val_int);
2637 break;
2639 case DW_OP_skip:
2640 case DW_OP_bra:
2642 int offset;
2644 gcc_assert (val1->val_class == dw_val_class_loc);
2645 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2647 fputc (',', asm_out_file);
2648 dw2_asm_output_data_raw (2, offset);
2650 break;
2652 case DW_OP_regx:
2654 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2655 gcc_assert (size_of_uleb128 (r)
2656 == size_of_uleb128 (val1->v.val_unsigned));
2657 fputc (',', asm_out_file);
2658 dw2_asm_output_data_uleb128_raw (r);
2660 break;
2662 case DW_OP_constu:
2663 case DW_OP_plus_uconst:
2664 case DW_OP_piece:
2665 fputc (',', asm_out_file);
2666 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2667 break;
2669 case DW_OP_bit_piece:
2670 fputc (',', asm_out_file);
2671 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2672 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2673 break;
2675 case DW_OP_consts:
2676 case DW_OP_breg0:
2677 case DW_OP_breg1:
2678 case DW_OP_breg2:
2679 case DW_OP_breg3:
2680 case DW_OP_breg4:
2681 case DW_OP_breg5:
2682 case DW_OP_breg6:
2683 case DW_OP_breg7:
2684 case DW_OP_breg8:
2685 case DW_OP_breg9:
2686 case DW_OP_breg10:
2687 case DW_OP_breg11:
2688 case DW_OP_breg12:
2689 case DW_OP_breg13:
2690 case DW_OP_breg14:
2691 case DW_OP_breg15:
2692 case DW_OP_breg16:
2693 case DW_OP_breg17:
2694 case DW_OP_breg18:
2695 case DW_OP_breg19:
2696 case DW_OP_breg20:
2697 case DW_OP_breg21:
2698 case DW_OP_breg22:
2699 case DW_OP_breg23:
2700 case DW_OP_breg24:
2701 case DW_OP_breg25:
2702 case DW_OP_breg26:
2703 case DW_OP_breg27:
2704 case DW_OP_breg28:
2705 case DW_OP_breg29:
2706 case DW_OP_breg30:
2707 case DW_OP_breg31:
2708 case DW_OP_fbreg:
2709 fputc (',', asm_out_file);
2710 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2711 break;
2713 case DW_OP_bregx:
2715 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2716 gcc_assert (size_of_uleb128 (r)
2717 == size_of_uleb128 (val1->v.val_unsigned));
2718 fputc (',', asm_out_file);
2719 dw2_asm_output_data_uleb128_raw (r);
2720 fputc (',', asm_out_file);
2721 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2723 break;
2725 case DW_OP_implicit_pointer:
2726 case DW_OP_entry_value:
2727 case DW_OP_const_type:
2728 case DW_OP_regval_type:
2729 case DW_OP_deref_type:
2730 case DW_OP_convert:
2731 case DW_OP_reinterpret:
2732 case DW_OP_GNU_implicit_pointer:
2733 case DW_OP_GNU_entry_value:
2734 case DW_OP_GNU_const_type:
2735 case DW_OP_GNU_regval_type:
2736 case DW_OP_GNU_deref_type:
2737 case DW_OP_GNU_convert:
2738 case DW_OP_GNU_reinterpret:
2739 case DW_OP_GNU_parameter_ref:
2740 gcc_unreachable ();
2741 break;
2743 default:
2744 /* Other codes have no operands. */
2745 break;
2749 void
2750 output_loc_sequence_raw (dw_loc_descr_ref loc)
2752 while (1)
2754 enum dwarf_location_atom opc = loc->dw_loc_opc;
2755 /* Output the opcode. */
2756 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2758 unsigned r = (opc - DW_OP_breg0);
2759 r = DWARF2_FRAME_REG_OUT (r, 1);
2760 gcc_assert (r <= 31);
2761 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2763 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2765 unsigned r = (opc - DW_OP_reg0);
2766 r = DWARF2_FRAME_REG_OUT (r, 1);
2767 gcc_assert (r <= 31);
2768 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2770 /* Output the opcode. */
2771 fprintf (asm_out_file, "%#x", opc);
2772 output_loc_operands_raw (loc);
2774 if (!loc->dw_loc_next)
2775 break;
2776 loc = loc->dw_loc_next;
2778 fputc (',', asm_out_file);
2782 static void
2783 build_breg_loc (struct dw_loc_descr_node **head, unsigned int regno)
2785 if (regno <= 31)
2786 add_loc_descr (head, new_loc_descr ((enum dwarf_location_atom)
2787 (DW_OP_breg0 + regno), 0, 0));
2788 else
2789 add_loc_descr (head, new_loc_descr (DW_OP_bregx, regno, 0));
2792 /* Build a dwarf location for a cfa_reg spanning multiple
2793 consecutive registers. */
2795 struct dw_loc_descr_node *
2796 build_span_loc (struct cfa_reg reg)
2798 struct dw_loc_descr_node *head = NULL;
2800 gcc_assert (reg.span_width > 0);
2801 gcc_assert (reg.span > 1);
2803 /* Start from the highest number register as it goes in the upper bits. */
2804 unsigned int regno = reg.reg + reg.span - 1;
2805 build_breg_loc (&head, regno);
2807 /* Deal with the remaining registers in the span. */
2808 for (int i = reg.span - 2; i >= 0; i--)
2810 add_loc_descr (&head, int_loc_descriptor (reg.span_width * 8));
2811 add_loc_descr (&head, new_loc_descr (DW_OP_shl, 0, 0));
2812 regno--;
2813 build_breg_loc (&head, regno);
2814 add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
2816 return head;
2819 /* This function builds a dwarf location descriptor sequence from a
2820 dw_cfa_location, adding the given OFFSET to the result of the
2821 expression. */
2823 struct dw_loc_descr_node *
2824 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2826 struct dw_loc_descr_node *head, *tmp;
2828 offset += cfa->offset;
2830 if (cfa->reg.span > 1)
2832 head = build_span_loc (cfa->reg);
2834 if (maybe_ne (offset, 0))
2835 loc_descr_plus_const (&head, offset);
2837 else if (cfa->indirect)
2839 head = new_reg_loc_descr (cfa->reg.reg, cfa->base_offset);
2840 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2841 head->dw_loc_oprnd1.val_entry = NULL;
2842 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2843 add_loc_descr (&head, tmp);
2844 loc_descr_plus_const (&head, offset);
2846 else
2847 head = new_reg_loc_descr (cfa->reg.reg, offset);
2849 return head;
2852 /* This function builds a dwarf location descriptor sequence for
2853 the address at OFFSET from the CFA when stack is aligned to
2854 ALIGNMENT byte. */
2856 struct dw_loc_descr_node *
2857 build_cfa_aligned_loc (dw_cfa_location *cfa,
2858 poly_int64 offset, HOST_WIDE_INT alignment)
2860 struct dw_loc_descr_node *head;
2861 unsigned int dwarf_fp
2862 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2864 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2865 if (cfa->reg.reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2867 head = new_reg_loc_descr (dwarf_fp, 0);
2868 add_loc_descr (&head, int_loc_descriptor (alignment));
2869 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2870 loc_descr_plus_const (&head, offset);
2872 else
2873 head = new_reg_loc_descr (dwarf_fp, offset);
2874 return head;
2877 /* And now, the support for symbolic debugging information. */
2879 /* .debug_str support. */
2881 static void dwarf2out_init (const char *);
2882 static void dwarf2out_finish (const char *);
2883 static void dwarf2out_early_finish (const char *);
2884 static void dwarf2out_assembly_start (void);
2885 static void dwarf2out_define (unsigned int, const char *);
2886 static void dwarf2out_undef (unsigned int, const char *);
2887 static void dwarf2out_start_source_file (unsigned, const char *);
2888 static void dwarf2out_end_source_file (unsigned);
2889 static void dwarf2out_function_decl (tree);
2890 static void dwarf2out_begin_block (unsigned, unsigned);
2891 static void dwarf2out_end_block (unsigned, unsigned);
2892 static bool dwarf2out_ignore_block (const_tree);
2893 static void dwarf2out_set_ignored_loc (unsigned, unsigned, const char *);
2894 static void dwarf2out_early_global_decl (tree);
2895 static void dwarf2out_late_global_decl (tree);
2896 static void dwarf2out_type_decl (tree, int);
2897 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2898 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2899 dw_die_ref);
2900 static void dwarf2out_abstract_function (tree);
2901 static void dwarf2out_var_location (rtx_insn *);
2902 static void dwarf2out_inline_entry (tree);
2903 static void dwarf2out_size_function (tree);
2904 static void dwarf2out_begin_function (tree);
2905 static void dwarf2out_end_function (unsigned int);
2906 static void dwarf2out_register_main_translation_unit (tree unit);
2907 static void dwarf2out_set_name (tree, tree);
2908 static void dwarf2out_register_external_die (tree decl, const char *sym,
2909 unsigned HOST_WIDE_INT off);
2910 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2911 unsigned HOST_WIDE_INT *off);
2913 /* The debug hooks structure. */
2915 const struct gcc_debug_hooks dwarf2_debug_hooks =
2917 dwarf2out_init,
2918 dwarf2out_finish,
2919 dwarf2out_early_finish,
2920 dwarf2out_assembly_start,
2921 dwarf2out_define,
2922 dwarf2out_undef,
2923 dwarf2out_start_source_file,
2924 dwarf2out_end_source_file,
2925 dwarf2out_begin_block,
2926 dwarf2out_end_block,
2927 dwarf2out_ignore_block,
2928 dwarf2out_source_line,
2929 dwarf2out_set_ignored_loc,
2930 dwarf2out_begin_prologue,
2931 #if VMS_DEBUGGING_INFO
2932 dwarf2out_vms_end_prologue,
2933 dwarf2out_vms_begin_epilogue,
2934 #else
2935 debug_nothing_int_charstar,
2936 debug_nothing_int_charstar,
2937 #endif
2938 dwarf2out_end_epilogue,
2939 dwarf2out_begin_function,
2940 dwarf2out_end_function, /* end_function */
2941 dwarf2out_register_main_translation_unit,
2942 dwarf2out_function_decl, /* function_decl */
2943 dwarf2out_early_global_decl,
2944 dwarf2out_late_global_decl,
2945 dwarf2out_type_decl, /* type_decl */
2946 dwarf2out_imported_module_or_decl,
2947 dwarf2out_die_ref_for_decl,
2948 dwarf2out_register_external_die,
2949 debug_nothing_tree, /* deferred_inline_function */
2950 /* The DWARF 2 backend tries to reduce debugging bloat by not
2951 emitting the abstract description of inline functions until
2952 something tries to reference them. */
2953 dwarf2out_abstract_function, /* outlining_inline_function */
2954 debug_nothing_rtx_code_label, /* label */
2955 debug_nothing_int, /* handle_pch */
2956 dwarf2out_var_location,
2957 dwarf2out_inline_entry, /* inline_entry */
2958 dwarf2out_size_function, /* size_function */
2959 dwarf2out_switch_text_section,
2960 dwarf2out_set_name,
2961 1, /* start_end_main_source_file */
2962 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2965 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2967 dwarf2out_init,
2968 debug_nothing_charstar,
2969 debug_nothing_charstar,
2970 dwarf2out_assembly_start,
2971 debug_nothing_int_charstar,
2972 debug_nothing_int_charstar,
2973 debug_nothing_int_charstar,
2974 debug_nothing_int,
2975 debug_nothing_int_int, /* begin_block */
2976 debug_nothing_int_int, /* end_block */
2977 debug_true_const_tree, /* ignore_block */
2978 dwarf2out_source_line, /* source_line */
2979 debug_nothing_int_int_charstar, /* set_ignored_loc */
2980 debug_nothing_int_int_charstar, /* begin_prologue */
2981 debug_nothing_int_charstar, /* end_prologue */
2982 debug_nothing_int_charstar, /* begin_epilogue */
2983 debug_nothing_int_charstar, /* end_epilogue */
2984 debug_nothing_tree, /* begin_function */
2985 debug_nothing_int, /* end_function */
2986 debug_nothing_tree, /* register_main_translation_unit */
2987 debug_nothing_tree, /* function_decl */
2988 debug_nothing_tree, /* early_global_decl */
2989 debug_nothing_tree, /* late_global_decl */
2990 debug_nothing_tree_int, /* type_decl */
2991 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2992 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2993 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2994 debug_nothing_tree, /* deferred_inline_function */
2995 debug_nothing_tree, /* outlining_inline_function */
2996 debug_nothing_rtx_code_label, /* label */
2997 debug_nothing_int, /* handle_pch */
2998 debug_nothing_rtx_insn, /* var_location */
2999 debug_nothing_tree, /* inline_entry */
3000 debug_nothing_tree, /* size_function */
3001 debug_nothing_void, /* switch_text_section */
3002 debug_nothing_tree_tree, /* set_name */
3003 0, /* start_end_main_source_file */
3004 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
3007 /* NOTE: In the comments in this file, many references are made to
3008 "Debugging Information Entries". This term is abbreviated as `DIE'
3009 throughout the remainder of this file. */
3011 /* An internal representation of the DWARF output is built, and then
3012 walked to generate the DWARF debugging info. The walk of the internal
3013 representation is done after the entire program has been compiled.
3014 The types below are used to describe the internal representation. */
3016 /* Whether to put type DIEs into their own section .debug_types instead
3017 of making them part of the .debug_info section. Only supported for
3018 Dwarf V4 or higher and the user didn't disable them through
3019 -fno-debug-types-section. It is more efficient to put them in a
3020 separate comdat sections since the linker will then be able to
3021 remove duplicates. But not all tools support .debug_types sections
3022 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
3023 it is DW_UT_type unit type in .debug_info section. For late LTO
3024 debug there should be almost no types emitted so avoid enabling
3025 -fdebug-types-section there. */
3027 #define use_debug_types (dwarf_version >= 4 \
3028 && flag_debug_types_section \
3029 && !in_lto_p)
3031 /* Various DIE's use offsets relative to the beginning of the
3032 .debug_info section to refer to each other. */
3034 typedef long int dw_offset;
3036 struct comdat_type_node;
3038 /* The entries in the line_info table more-or-less mirror the opcodes
3039 that are used in the real dwarf line table. Arrays of these entries
3040 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
3041 supported. */
3043 enum dw_line_info_opcode {
3044 /* Emit DW_LNE_set_address; the operand is the label index. */
3045 LI_set_address,
3047 /* Emit a row to the matrix with the given line. This may be done
3048 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3049 special opcodes. */
3050 LI_set_line,
3052 /* Emit a DW_LNS_set_file. */
3053 LI_set_file,
3055 /* Emit a DW_LNS_set_column. */
3056 LI_set_column,
3058 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
3059 LI_negate_stmt,
3061 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
3062 LI_set_prologue_end,
3063 LI_set_epilogue_begin,
3065 /* Emit a DW_LNE_set_discriminator. */
3066 LI_set_discriminator,
3068 /* Output a Fixed Advance PC; the target PC is the label index; the
3069 base PC is the previous LI_adv_address or LI_set_address entry.
3070 We only use this when emitting debug views without assembler
3071 support, at explicit user request. Ideally, we should only use
3072 it when the offset might be zero but we can't tell: it's the only
3073 way to maybe change the PC without resetting the view number. */
3074 LI_adv_address
3077 typedef struct GTY(()) dw_line_info_struct {
3078 enum dw_line_info_opcode opcode;
3079 unsigned int val;
3080 } dw_line_info_entry;
3083 struct GTY(()) dw_line_info_table {
3084 /* The label that marks the end of this section. */
3085 const char *end_label;
3087 /* The values for the last row of the matrix, as collected in the table.
3088 These are used to minimize the changes to the next row. */
3089 unsigned int file_num;
3090 unsigned int line_num;
3091 unsigned int column_num;
3092 int discrim_num;
3093 bool is_stmt;
3094 bool in_use;
3096 /* This denotes the NEXT view number.
3098 If it is 0, it is known that the NEXT view will be the first view
3099 at the given PC.
3101 If it is -1, we're forcing the view number to be reset, e.g. at a
3102 function entry.
3104 The meaning of other nonzero values depends on whether we're
3105 computing views internally or leaving it for the assembler to do
3106 so. If we're emitting them internally, view denotes the view
3107 number since the last known advance of PC. If we're leaving it
3108 for the assembler, it denotes the LVU label number that we're
3109 going to ask the assembler to assign. */
3110 var_loc_view view;
3112 /* This counts the number of symbolic views emitted in this table
3113 since the latest view reset. Its max value, over all tables,
3114 sets symview_upper_bound. */
3115 var_loc_view symviews_since_reset;
3117 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3118 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3119 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3120 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3122 vec<dw_line_info_entry, va_gc> *entries;
3125 /* This is an upper bound for view numbers that the assembler may
3126 assign to symbolic views output in this translation. It is used to
3127 decide how big a field to use to represent view numbers in
3128 symview-classed attributes. */
3130 static var_loc_view symview_upper_bound;
3132 /* If we're keep track of location views and their reset points, and
3133 INSN is a reset point (i.e., it necessarily advances the PC), mark
3134 the next view in TABLE as reset. */
3136 static void
3137 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3139 if (!debug_internal_reset_location_views)
3140 return;
3142 /* Maybe turn (part of?) this test into a default target hook. */
3143 int reset = 0;
3145 if (targetm.reset_location_view)
3146 reset = targetm.reset_location_view (insn);
3148 if (reset)
3150 else if (JUMP_TABLE_DATA_P (insn))
3151 reset = 1;
3152 else if (GET_CODE (insn) == USE
3153 || GET_CODE (insn) == CLOBBER
3154 || GET_CODE (insn) == ASM_INPUT
3155 || asm_noperands (insn) >= 0)
3157 else if (get_attr_min_length (insn) > 0)
3158 reset = 1;
3160 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3161 RESET_NEXT_VIEW (table->view);
3164 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3165 The children of each node form a circular list linked by
3166 die_sib. die_child points to the node *before* the "first" child node. */
3168 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3169 union die_symbol_or_type_node
3171 const char * GTY ((tag ("0"))) die_symbol;
3172 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3174 GTY ((desc ("%0.comdat_type_p"))) die_id;
3175 vec<dw_attr_node, va_gc> *die_attr;
3176 dw_die_ref die_parent;
3177 dw_die_ref die_child;
3178 dw_die_ref die_sib;
3179 dw_die_ref die_definition; /* ref from a specification to its definition */
3180 dw_offset die_offset;
3181 unsigned long die_abbrev;
3182 int die_mark;
3183 unsigned int decl_id;
3184 enum dwarf_tag die_tag;
3185 /* Die is used and must not be pruned as unused. */
3186 BOOL_BITFIELD die_perennial_p : 1;
3187 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3188 /* For an external ref to die_symbol if die_offset contains an extra
3189 offset to that symbol. */
3190 BOOL_BITFIELD with_offset : 1;
3191 /* Whether this DIE was removed from the DIE tree, for example via
3192 prune_unused_types. We don't consider those present from the
3193 DIE lookup routines. */
3194 BOOL_BITFIELD removed : 1;
3195 /* Lots of spare bits. */
3197 die_node;
3199 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3200 static bool early_dwarf;
3201 static bool early_dwarf_finished;
3202 class set_early_dwarf {
3203 public:
3204 bool saved;
3205 set_early_dwarf () : saved(early_dwarf)
3207 gcc_assert (! early_dwarf_finished);
3208 early_dwarf = true;
3210 ~set_early_dwarf () { early_dwarf = saved; }
3213 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3214 #define FOR_EACH_CHILD(die, c, expr) do { \
3215 c = die->die_child; \
3216 if (c) do { \
3217 c = c->die_sib; \
3218 expr; \
3219 } while (c != die->die_child); \
3220 } while (0)
3222 /* The pubname structure */
3224 typedef struct GTY(()) pubname_struct {
3225 dw_die_ref die;
3226 const char *name;
3228 pubname_entry;
3231 struct GTY(()) dw_ranges {
3232 const char *label;
3233 /* If this is positive, it's a block number, otherwise it's a
3234 bitwise-negated index into dw_ranges_by_label. */
3235 int num;
3236 /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3237 into .debug_rnglists section rather than .debug_rnglists.dwo
3238 for -gsplit-dwarf and DWARF >= 5. */
3239 #define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3240 /* Index for the range list for DW_FORM_rnglistx. */
3241 unsigned int idx : 31;
3242 /* True if this range might be possibly in a different section
3243 from previous entry. */
3244 unsigned int maybe_new_sec : 1;
3245 addr_table_entry *begin_entry;
3246 addr_table_entry *end_entry;
3249 /* A structure to hold a macinfo entry. */
3251 typedef struct GTY(()) macinfo_struct {
3252 unsigned char code;
3253 unsigned HOST_WIDE_INT lineno;
3254 const char *info;
3256 macinfo_entry;
3259 struct GTY(()) dw_ranges_by_label {
3260 const char *begin;
3261 const char *end;
3264 /* The comdat type node structure. */
3265 struct GTY(()) comdat_type_node
3267 dw_die_ref root_die;
3268 dw_die_ref type_die;
3269 dw_die_ref skeleton_die;
3270 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3271 comdat_type_node *next;
3274 /* A list of DIEs for which we can't determine ancestry (parent_die
3275 field) just yet. Later in dwarf2out_finish we will fill in the
3276 missing bits. */
3277 typedef struct GTY(()) limbo_die_struct {
3278 dw_die_ref die;
3279 /* The tree for which this DIE was created. We use this to
3280 determine ancestry later. */
3281 tree created_for;
3282 struct limbo_die_struct *next;
3284 limbo_die_node;
3286 typedef struct skeleton_chain_struct
3288 dw_die_ref old_die;
3289 dw_die_ref new_die;
3290 struct skeleton_chain_struct *parent;
3292 skeleton_chain_node;
3294 /* Define a macro which returns nonzero for a TYPE_DECL which was
3295 implicitly generated for a type.
3297 Note that, unlike the C front-end (which generates a NULL named
3298 TYPE_DECL node for each complete tagged type, each array type,
3299 and each function type node created) the C++ front-end generates
3300 a _named_ TYPE_DECL node for each tagged type node created.
3301 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3302 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3303 front-end, but for each type, tagged or not. */
3305 #define TYPE_DECL_IS_STUB(decl) \
3306 (DECL_NAME (decl) == NULL_TREE \
3307 || (DECL_ARTIFICIAL (decl) \
3308 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3309 /* This is necessary for stub decls that \
3310 appear in nested inline functions. */ \
3311 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3312 && (decl_ultimate_origin (decl) \
3313 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3315 /* Information concerning the compilation unit's programming
3316 language, and compiler version. */
3318 /* Fixed size portion of the DWARF compilation unit header. */
3319 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3320 (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size \
3321 + (dwarf_version >= 5 ? 4 : 3))
3323 /* Fixed size portion of the DWARF comdat type unit header. */
3324 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3325 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3326 + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3328 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3329 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3330 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3332 /* Fixed size portion of public names info. */
3333 #define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3335 /* Fixed size portion of the address range info. */
3336 #define DWARF_ARANGES_HEADER_SIZE \
3337 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3338 DWARF2_ADDR_SIZE * 2) \
3339 - DWARF_INITIAL_LENGTH_SIZE)
3341 /* Size of padding portion in the address range info. It must be
3342 aligned to twice the pointer size. */
3343 #define DWARF_ARANGES_PAD_SIZE \
3344 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3345 DWARF2_ADDR_SIZE * 2) \
3346 - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3348 /* Use assembler line directives if available. */
3349 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3350 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3351 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3352 #else
3353 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3354 #endif
3355 #endif
3357 /* Use assembler views in line directives if available. */
3358 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3359 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3360 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3361 #else
3362 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3363 #endif
3364 #endif
3366 /* Return true if GCC configure detected assembler support for .loc. */
3368 bool
3369 dwarf2out_default_as_loc_support (void)
3371 return DWARF2_ASM_LINE_DEBUG_INFO;
3372 #if (GCC_VERSION >= 3000)
3373 # undef DWARF2_ASM_LINE_DEBUG_INFO
3374 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3375 #endif
3378 /* Return true if GCC configure detected assembler support for views
3379 in .loc directives. */
3381 bool
3382 dwarf2out_default_as_locview_support (void)
3384 return DWARF2_ASM_VIEW_DEBUG_INFO;
3385 #if (GCC_VERSION >= 3000)
3386 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3387 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3388 #endif
3391 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3392 view computation, and it refers to a view identifier for which we
3393 will not emit a label because it is known to map to a view number
3394 zero. We won't allocate the bitmap if we're not using assembler
3395 support for location views, but we have to make the variable
3396 visible for GGC and for code that will be optimized out for lack of
3397 support but that's still parsed and compiled. We could abstract it
3398 out with macros, but it's not worth it. */
3399 static GTY(()) bitmap zero_view_p;
3401 /* Evaluate to TRUE iff N is known to identify the first location view
3402 at its PC. When not using assembler location view computation,
3403 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3404 and views label numbers recorded in it are the ones known to be
3405 zero. */
3406 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3407 || (N) == (var_loc_view)-1 \
3408 || (zero_view_p \
3409 && bitmap_bit_p (zero_view_p, (N))))
3411 /* Return true iff we're to emit .loc directives for the assembler to
3412 generate line number sections.
3414 When we're not emitting views, all we need from the assembler is
3415 support for .loc directives.
3417 If we are emitting views, we can only use the assembler's .loc
3418 support if it also supports views.
3420 When the compiler is emitting the line number programs and
3421 computing view numbers itself, it resets view numbers at known PC
3422 changes and counts from that, and then it emits view numbers as
3423 literal constants in locviewlists. There are cases in which the
3424 compiler is not sure about PC changes, e.g. when extra alignment is
3425 requested for a label. In these cases, the compiler may not reset
3426 the view counter, and the potential PC advance in the line number
3427 program will use an opcode that does not reset the view counter
3428 even if the PC actually changes, so that compiler and debug info
3429 consumer can keep view numbers in sync.
3431 When the compiler defers view computation to the assembler, it
3432 emits symbolic view numbers in locviewlists, with the exception of
3433 views known to be zero (forced resets, or reset after
3434 compiler-visible PC changes): instead of emitting symbols for
3435 these, we emit literal zero and assert the assembler agrees with
3436 the compiler's assessment. We could use symbolic views everywhere,
3437 instead of special-casing zero views, but then we'd be unable to
3438 optimize out locviewlists that contain only zeros. */
3440 static bool
3441 output_asm_line_debug_info (void)
3443 return (dwarf2out_as_loc_support
3444 && (dwarf2out_as_locview_support
3445 || !debug_variable_location_views));
3448 static bool asm_outputs_debug_line_str (void);
3450 /* Minimum line offset in a special line info. opcode.
3451 This value was chosen to give a reasonable range of values. */
3452 #define DWARF_LINE_BASE -10
3454 /* First special line opcode - leave room for the standard opcodes. */
3455 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3457 /* Range of line offsets in a special line info. opcode. */
3458 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3460 /* Flag that indicates the initial value of the is_stmt_start flag.
3461 In the present implementation, we do not mark any lines as
3462 the beginning of a source statement, because that information
3463 is not made available by the GCC front-end. */
3464 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3466 /* Maximum number of operations per instruction bundle. */
3467 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3468 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3469 #endif
3471 /* This location is used by calc_die_sizes() to keep track
3472 the offset of each DIE within the .debug_info section. */
3473 static unsigned long next_die_offset;
3475 /* Record the root of the DIE's built for the current compilation unit. */
3476 static GTY(()) dw_die_ref single_comp_unit_die;
3478 /* A list of type DIEs that have been separated into comdat sections. */
3479 static GTY(()) comdat_type_node *comdat_type_list;
3481 /* A list of CU DIEs that have been separated. */
3482 static GTY(()) limbo_die_node *cu_die_list;
3484 /* A list of DIEs with a NULL parent waiting to be relocated. */
3485 static GTY(()) limbo_die_node *limbo_die_list;
3487 /* A list of DIEs for which we may have to generate
3488 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3489 static GTY(()) limbo_die_node *deferred_asm_name;
3491 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3493 typedef const char *compare_type;
3495 static hashval_t hash (dwarf_file_data *);
3496 static bool equal (dwarf_file_data *, const char *);
3499 /* Filenames referenced by this compilation unit. */
3500 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3502 struct decl_die_hasher : ggc_ptr_hash<die_node>
3504 typedef tree compare_type;
3506 static hashval_t hash (die_node *);
3507 static bool equal (die_node *, tree);
3509 /* A hash table of references to DIE's that describe declarations.
3510 The key is a DECL_UID() which is a unique number identifying each decl. */
3511 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3513 struct GTY ((for_user)) variable_value_struct {
3514 unsigned int decl_id;
3515 vec<dw_die_ref, va_gc> *dies;
3518 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3520 typedef tree compare_type;
3522 static hashval_t hash (variable_value_struct *);
3523 static bool equal (variable_value_struct *, tree);
3525 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3526 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3527 DECL_CONTEXT of the referenced VAR_DECLs. */
3528 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3530 struct block_die_hasher : ggc_ptr_hash<die_struct>
3532 static hashval_t hash (die_struct *);
3533 static bool equal (die_struct *, die_struct *);
3536 /* A hash table of references to DIE's that describe COMMON blocks.
3537 The key is DECL_UID() ^ die_parent. */
3538 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3540 typedef struct GTY(()) die_arg_entry_struct {
3541 dw_die_ref die;
3542 tree arg;
3543 } die_arg_entry;
3546 /* Node of the variable location list. */
3547 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3548 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3549 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3550 in mode of the EXPR_LIST node and first EXPR_LIST operand
3551 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3552 location or NULL for padding. For larger bitsizes,
3553 mode is 0 and first operand is a CONCAT with bitsize
3554 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3555 NULL as second operand. */
3556 rtx GTY (()) loc;
3557 const char * GTY (()) label;
3558 struct var_loc_node * GTY (()) next;
3559 var_loc_view view;
3562 /* Variable location list. */
3563 struct GTY ((for_user)) var_loc_list_def {
3564 struct var_loc_node * GTY (()) first;
3566 /* Pointer to the last but one or last element of the
3567 chained list. If the list is empty, both first and
3568 last are NULL, if the list contains just one node
3569 or the last node certainly is not redundant, it points
3570 to the last node, otherwise points to the last but one.
3571 Do not mark it for GC because it is marked through the chain. */
3572 struct var_loc_node * GTY ((skip ("%h"))) last;
3574 /* Pointer to the last element before section switch,
3575 if NULL, either sections weren't switched or first
3576 is after section switch. */
3577 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3579 /* DECL_UID of the variable decl. */
3580 unsigned int decl_id;
3582 typedef struct var_loc_list_def var_loc_list;
3584 /* Call argument location list. */
3585 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3586 rtx GTY (()) call_arg_loc_note;
3587 const char * GTY (()) label;
3588 tree GTY (()) block;
3589 bool tail_call_p;
3590 rtx GTY (()) symbol_ref;
3591 struct call_arg_loc_node * GTY (()) next;
3595 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3597 typedef const_tree compare_type;
3599 static hashval_t hash (var_loc_list *);
3600 static bool equal (var_loc_list *, const_tree);
3603 /* Table of decl location linked lists. */
3604 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3606 /* Head and tail of call_arg_loc chain. */
3607 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3608 static struct call_arg_loc_node *call_arg_loc_last;
3610 /* Number of call sites in the current function. */
3611 static int call_site_count = -1;
3612 /* Number of tail call sites in the current function. */
3613 static int tail_call_site_count = -1;
3615 /* A cached location list. */
3616 struct GTY ((for_user)) cached_dw_loc_list_def {
3617 /* The DECL_UID of the decl that this entry describes. */
3618 unsigned int decl_id;
3620 /* The cached location list. */
3621 dw_loc_list_ref loc_list;
3623 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3625 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3628 typedef const_tree compare_type;
3630 static hashval_t hash (cached_dw_loc_list *);
3631 static bool equal (cached_dw_loc_list *, const_tree);
3634 /* Table of cached location lists. */
3635 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3637 /* A vector of references to DIE's that are uniquely identified by their tag,
3638 presence/absence of children DIE's, and list of attribute/value pairs. */
3639 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3641 /* A hash map to remember the stack usage for DWARF procedures. The value
3642 stored is the stack size difference between before the DWARF procedure
3643 invokation and after it returned. In other words, for a DWARF procedure
3644 that consumes N stack slots and that pushes M ones, this stores M - N. */
3645 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3647 /* A global counter for generating labels for line number data. */
3648 static unsigned int line_info_label_num;
3650 /* The current table to which we should emit line number information
3651 for the current function. This will be set up at the beginning of
3652 assembly for the function. */
3653 static GTY(()) dw_line_info_table *cur_line_info_table;
3655 /* The two default tables of line number info. */
3656 static GTY(()) dw_line_info_table *text_section_line_info;
3657 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3659 /* The set of all non-default tables of line number info. */
3660 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3662 /* A flag to tell pubnames/types export if there is an info section to
3663 refer to. */
3664 static bool info_section_emitted;
3666 /* A pointer to the base of a table that contains a list of publicly
3667 accessible names. */
3668 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3670 /* A pointer to the base of a table that contains a list of publicly
3671 accessible types. */
3672 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3674 /* A pointer to the base of a table that contains a list of macro
3675 defines/undefines (and file start/end markers). */
3676 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3678 /* True if .debug_macinfo or .debug_macros section is going to be
3679 emitted. */
3680 #define have_macinfo \
3681 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3682 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3683 && !macinfo_table->is_empty ())
3685 /* Vector of dies for which we should generate .debug_ranges info. */
3686 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3688 /* Vector of pairs of labels referenced in ranges_table. */
3689 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3691 /* Whether we have location lists that need outputting */
3692 static GTY(()) bool have_location_lists;
3694 /* Unique label counter. */
3695 static GTY(()) unsigned int loclabel_num;
3697 /* Unique label counter for point-of-call tables. */
3698 static GTY(()) unsigned int poc_label_num;
3700 /* The last file entry emitted by maybe_emit_file(). */
3701 static GTY(()) struct dwarf_file_data * last_emitted_file;
3703 /* Number of internal labels generated by gen_internal_sym(). */
3704 static GTY(()) int label_num;
3706 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3708 /* Instances of generic types for which we need to generate debug
3709 info that describe their generic parameters and arguments. That
3710 generation needs to happen once all types are properly laid out so
3711 we do it at the end of compilation. */
3712 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3714 /* Offset from the "steady-state frame pointer" to the frame base,
3715 within the current function. */
3716 static poly_int64 frame_pointer_fb_offset;
3717 static bool frame_pointer_fb_offset_valid;
3719 static vec<dw_die_ref> base_types;
3721 /* Flags to represent a set of attribute classes for attributes that represent
3722 a scalar value (bounds, pointers, ...). */
3723 enum dw_scalar_form
3725 dw_scalar_form_constant = 0x01,
3726 dw_scalar_form_exprloc = 0x02,
3727 dw_scalar_form_reference = 0x04
3730 /* Forward declarations for functions defined in this file. */
3732 static bool is_pseudo_reg (const_rtx);
3733 static tree type_main_variant (tree);
3734 static bool is_tagged_type (const_tree);
3735 static const char *dwarf_tag_name (unsigned);
3736 static const char *dwarf_attr_name (unsigned);
3737 static const char *dwarf_form_name (unsigned);
3738 static tree decl_ultimate_origin (const_tree);
3739 static tree decl_class_context (tree);
3740 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3741 static inline unsigned int AT_index (dw_attr_node *);
3742 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3743 static inline unsigned AT_flag (dw_attr_node *);
3744 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3745 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3746 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3747 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3748 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3749 unsigned int, unsigned char *);
3750 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3751 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3752 static inline const char *AT_string (dw_attr_node *);
3753 static enum dwarf_form AT_string_form (dw_attr_node *);
3754 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3755 static void add_AT_specification (dw_die_ref, dw_die_ref);
3756 static inline dw_die_ref AT_ref (dw_attr_node *);
3757 static inline int AT_ref_external (dw_attr_node *);
3758 static inline void set_AT_ref_external (dw_attr_node *, int);
3759 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3760 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3761 dw_loc_list_ref);
3762 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3763 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3764 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3765 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3766 static void remove_addr_table_entry (addr_table_entry *);
3767 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3768 static inline rtx AT_addr (dw_attr_node *);
3769 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3770 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3771 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3772 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3773 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3774 unsigned long, bool);
3775 static inline const char *AT_lbl (dw_attr_node *);
3776 static const char *get_AT_low_pc (dw_die_ref);
3777 static bool is_c (void);
3778 static bool is_cxx (void);
3779 static bool is_cxx (const_tree);
3780 static bool is_fortran (void);
3781 static bool is_ada (void);
3782 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3783 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3784 static void add_child_die (dw_die_ref, dw_die_ref);
3785 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3786 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3787 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3788 static void equate_type_number_to_die (tree, dw_die_ref);
3789 static var_loc_list *lookup_decl_loc (const_tree);
3790 static void equate_decl_number_to_die (tree, dw_die_ref);
3791 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3792 static void print_spaces (FILE *);
3793 static void print_die (dw_die_ref, FILE *);
3794 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3795 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3796 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3797 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3798 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3799 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3800 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3801 struct md5_ctx *, int *);
3802 struct checksum_attributes;
3803 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3804 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3805 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3806 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3807 static bool same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3808 static bool same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3809 static bool same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3810 static bool same_die_p (dw_die_ref, dw_die_ref, int *);
3811 static bool is_type_die (dw_die_ref);
3812 static inline bool is_template_instantiation (dw_die_ref);
3813 static bool is_declaration_die (dw_die_ref);
3814 static bool should_move_die_to_comdat (dw_die_ref);
3815 static dw_die_ref clone_as_declaration (dw_die_ref);
3816 static dw_die_ref clone_die (dw_die_ref);
3817 static dw_die_ref clone_tree (dw_die_ref);
3818 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3819 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3820 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3821 static dw_die_ref generate_skeleton (dw_die_ref);
3822 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3823 dw_die_ref,
3824 dw_die_ref);
3825 static void break_out_comdat_types (dw_die_ref);
3826 static void copy_decls_for_unworthy_types (dw_die_ref);
3828 static void add_sibling_attributes (dw_die_ref);
3829 static void output_location_lists (dw_die_ref);
3830 static int constant_size (unsigned HOST_WIDE_INT);
3831 static unsigned long size_of_die (dw_die_ref);
3832 static void calc_die_sizes (dw_die_ref);
3833 static void calc_base_type_die_sizes (void);
3834 static void mark_dies (dw_die_ref);
3835 static void unmark_dies (dw_die_ref);
3836 static void unmark_all_dies (dw_die_ref);
3837 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3838 static unsigned long size_of_aranges (void);
3839 static enum dwarf_form value_format (dw_attr_node *);
3840 static void output_value_format (dw_attr_node *);
3841 static void output_abbrev_section (void);
3842 static void output_die_abbrevs (unsigned long, dw_die_ref);
3843 static void output_die (dw_die_ref);
3844 static void output_compilation_unit_header (enum dwarf_unit_type);
3845 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3846 static void output_comdat_type_unit (comdat_type_node *, bool);
3847 static const char *dwarf2_name (tree, int);
3848 static void add_pubname (tree, dw_die_ref);
3849 static void add_enumerator_pubname (const char *, dw_die_ref);
3850 static void add_pubname_string (const char *, dw_die_ref);
3851 static void add_pubtype (tree, dw_die_ref);
3852 static void output_pubnames (vec<pubname_entry, va_gc> *);
3853 static void output_aranges (void);
3854 static unsigned int add_ranges (const_tree, bool = false);
3855 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3856 bool *, bool);
3857 static void output_ranges (void);
3858 static dw_line_info_table *new_line_info_table (void);
3859 static void output_line_info (bool);
3860 static void output_file_names (void);
3861 static bool is_base_type (tree);
3862 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3863 static int decl_quals (const_tree);
3864 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3865 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3866 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3867 static unsigned int debugger_reg_number (const_rtx);
3868 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3869 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3870 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3871 enum var_init_status);
3872 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3873 enum var_init_status);
3874 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3875 enum var_init_status);
3876 static bool is_based_loc (const_rtx);
3877 static bool resolve_one_addr (rtx *);
3878 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3879 enum var_init_status);
3880 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3881 enum var_init_status);
3882 struct loc_descr_context;
3883 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3884 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3885 static dw_loc_list_ref loc_list_from_tree (tree, int,
3886 struct loc_descr_context *);
3887 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3888 struct loc_descr_context *);
3889 static tree field_type (const_tree);
3890 static unsigned int simple_type_align_in_bits (const_tree);
3891 static unsigned int simple_decl_align_in_bits (const_tree);
3892 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3893 struct vlr_context;
3894 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3895 HOST_WIDE_INT *);
3896 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3897 dw_loc_list_ref);
3898 static void add_data_member_location_attribute (dw_die_ref, tree,
3899 struct vlr_context *);
3900 static bool add_const_value_attribute (dw_die_ref, machine_mode, rtx);
3901 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3902 static void insert_wide_int (const wide_int_ref &, unsigned char *, int);
3903 static unsigned insert_float (const_rtx, unsigned char *);
3904 static rtx rtl_for_decl_location (tree);
3905 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3906 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3907 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3908 static void add_desc_attribute (dw_die_ref, tree);
3909 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3910 static void add_comp_dir_attribute (dw_die_ref);
3911 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3912 struct loc_descr_context *);
3913 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3914 struct loc_descr_context *);
3915 static void add_subscript_info (dw_die_ref, tree, bool);
3916 static void add_byte_size_attribute (dw_die_ref, tree);
3917 static void add_alignment_attribute (dw_die_ref, tree);
3918 static void add_bit_offset_attribute (dw_die_ref, tree);
3919 static void add_bit_size_attribute (dw_die_ref, tree);
3920 static void add_prototyped_attribute (dw_die_ref, tree);
3921 static void add_abstract_origin_attribute (dw_die_ref, tree);
3922 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3923 static void add_src_coords_attributes (dw_die_ref, tree);
3924 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3925 static void add_discr_value (dw_die_ref, dw_discr_value *);
3926 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3927 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3928 static dw_die_ref scope_die_for (tree, dw_die_ref);
3929 static inline bool local_scope_p (dw_die_ref);
3930 static inline bool class_scope_p (dw_die_ref);
3931 static inline bool class_or_namespace_scope_p (dw_die_ref);
3932 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3933 static void add_calling_convention_attribute (dw_die_ref, tree);
3934 static const char *type_tag (const_tree);
3935 static tree member_declared_type (const_tree);
3936 #if 0
3937 static const char *decl_start_label (tree);
3938 #endif
3939 static void gen_array_type_die (tree, dw_die_ref);
3940 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3941 #if 0
3942 static void gen_entry_point_die (tree, dw_die_ref);
3943 #endif
3944 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref, bool);
3945 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3946 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3947 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3948 static void gen_formal_types_die (tree, dw_die_ref);
3949 static void gen_subprogram_die (tree, dw_die_ref);
3950 static void gen_variable_die (tree, tree, dw_die_ref);
3951 static void gen_const_die (tree, dw_die_ref);
3952 static void gen_label_die (tree, dw_die_ref);
3953 static void gen_lexical_block_die (tree, dw_die_ref);
3954 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3955 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3956 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3957 static dw_die_ref gen_compile_unit_die (const char *);
3958 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3959 static void gen_member_die (tree, dw_die_ref);
3960 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3961 enum debug_info_usage);
3962 static void gen_subroutine_type_die (tree, dw_die_ref);
3963 static void gen_typedef_die (tree, dw_die_ref);
3964 static void gen_type_die (tree, dw_die_ref, bool = false);
3965 static void gen_block_die (tree, dw_die_ref);
3966 static void decls_for_scope (tree, dw_die_ref, bool = true);
3967 static bool is_naming_typedef_decl (const_tree);
3968 static inline dw_die_ref get_context_die (tree);
3969 static void gen_namespace_die (tree, dw_die_ref);
3970 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3971 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3972 static dw_die_ref force_decl_die (tree);
3973 static dw_die_ref force_type_die (tree);
3974 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3975 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3976 static struct dwarf_file_data * lookup_filename (const char *);
3977 static void retry_incomplete_types (void);
3978 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3979 static void gen_generic_params_dies (tree);
3980 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage,
3981 bool = false);
3982 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage,
3983 bool = false);
3984 static void splice_child_die (dw_die_ref, dw_die_ref);
3985 static int file_info_cmp (const void *, const void *);
3986 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3987 const char *, var_loc_view, const char *);
3988 static void output_loc_list (dw_loc_list_ref);
3989 static char *gen_internal_sym (const char *);
3990 static bool want_pubnames (void);
3992 static void prune_unmark_dies (dw_die_ref);
3993 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3994 static void prune_unused_types_mark (dw_die_ref, int);
3995 static void prune_unused_types_walk (dw_die_ref);
3996 static void prune_unused_types_walk_attribs (dw_die_ref);
3997 static void prune_unused_types_prune (dw_die_ref);
3998 static void prune_unused_types (void);
3999 static int maybe_emit_file (struct dwarf_file_data *fd);
4000 static inline const char *AT_vms_delta1 (dw_attr_node *);
4001 static inline const char *AT_vms_delta2 (dw_attr_node *);
4002 #if VMS_DEBUGGING_INFO
4003 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
4004 const char *, const char *);
4005 #endif
4006 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
4007 static void gen_remaining_tmpl_value_param_die_attribute (void);
4008 static bool generic_type_p (tree);
4009 static void schedule_generic_params_dies_gen (tree t);
4010 static void gen_scheduled_generic_parms_dies (void);
4011 static void resolve_variable_values (void);
4013 static const char *comp_dir_string (void);
4015 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
4017 /* enum for tracking thread-local variables whose address is really an offset
4018 relative to the TLS pointer, which will need link-time relocation, but will
4019 not need relocation by the DWARF consumer. */
4021 enum dtprel_bool
4023 dtprel_false = 0,
4024 dtprel_true = 1
4027 /* Return the operator to use for an address of a variable. For dtprel_true, we
4028 use DW_OP_const*. For regular variables, which need both link-time
4029 relocation and consumer-level relocation (e.g., to account for shared objects
4030 loaded at a random address), we use DW_OP_addr*. */
4032 static inline enum dwarf_location_atom
4033 dw_addr_op (enum dtprel_bool dtprel)
4035 if (dtprel == dtprel_true)
4036 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
4037 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
4038 else
4039 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
4042 /* Return a pointer to a newly allocated address location description. If
4043 dwarf_split_debug_info is true, then record the address with the appropriate
4044 relocation. */
4045 static inline dw_loc_descr_ref
4046 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
4048 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
4050 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
4051 ref->dw_loc_oprnd1.v.val_addr = addr;
4052 ref->dtprel = dtprel;
4053 if (dwarf_split_debug_info)
4054 ref->dw_loc_oprnd1.val_entry
4055 = add_addr_table_entry (addr,
4056 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
4057 else
4058 ref->dw_loc_oprnd1.val_entry = NULL;
4060 return ref;
4063 /* Section names used to hold DWARF debugging information. */
4065 #ifndef DEBUG_INFO_SECTION
4066 #define DEBUG_INFO_SECTION ".debug_info"
4067 #endif
4068 #ifndef DEBUG_DWO_INFO_SECTION
4069 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
4070 #endif
4071 #ifndef DEBUG_LTO_INFO_SECTION
4072 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
4073 #endif
4074 #ifndef DEBUG_LTO_DWO_INFO_SECTION
4075 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
4076 #endif
4077 #ifndef DEBUG_ABBREV_SECTION
4078 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4079 #endif
4080 #ifndef DEBUG_LTO_ABBREV_SECTION
4081 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
4082 #endif
4083 #ifndef DEBUG_DWO_ABBREV_SECTION
4084 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
4085 #endif
4086 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
4087 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
4088 #endif
4089 #ifndef DEBUG_ARANGES_SECTION
4090 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4091 #endif
4092 #ifndef DEBUG_ADDR_SECTION
4093 #define DEBUG_ADDR_SECTION ".debug_addr"
4094 #endif
4095 #ifndef DEBUG_MACINFO_SECTION
4096 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4097 #endif
4098 #ifndef DEBUG_LTO_MACINFO_SECTION
4099 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4100 #endif
4101 #ifndef DEBUG_DWO_MACINFO_SECTION
4102 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4103 #endif
4104 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4105 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4106 #endif
4107 #ifndef DEBUG_MACRO_SECTION
4108 #define DEBUG_MACRO_SECTION ".debug_macro"
4109 #endif
4110 #ifndef DEBUG_LTO_MACRO_SECTION
4111 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4112 #endif
4113 #ifndef DEBUG_DWO_MACRO_SECTION
4114 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4115 #endif
4116 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4117 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4118 #endif
4119 #ifndef DEBUG_LINE_SECTION
4120 #define DEBUG_LINE_SECTION ".debug_line"
4121 #endif
4122 #ifndef DEBUG_LTO_LINE_SECTION
4123 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4124 #endif
4125 #ifndef DEBUG_DWO_LINE_SECTION
4126 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4127 #endif
4128 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4129 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4130 #endif
4131 #ifndef DEBUG_LOC_SECTION
4132 #define DEBUG_LOC_SECTION ".debug_loc"
4133 #endif
4134 #ifndef DEBUG_DWO_LOC_SECTION
4135 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4136 #endif
4137 #ifndef DEBUG_LOCLISTS_SECTION
4138 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4139 #endif
4140 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4141 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4142 #endif
4143 #ifndef DEBUG_PUBNAMES_SECTION
4144 #define DEBUG_PUBNAMES_SECTION \
4145 ((debug_generate_pub_sections == 2) \
4146 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4147 #endif
4148 #ifndef DEBUG_PUBTYPES_SECTION
4149 #define DEBUG_PUBTYPES_SECTION \
4150 ((debug_generate_pub_sections == 2) \
4151 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4152 #endif
4153 #ifndef DEBUG_STR_OFFSETS_SECTION
4154 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4155 #endif
4156 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4157 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4158 #endif
4159 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4160 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4161 #endif
4162 #ifndef DEBUG_STR_SECTION
4163 #define DEBUG_STR_SECTION ".debug_str"
4164 #endif
4165 #ifndef DEBUG_LTO_STR_SECTION
4166 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4167 #endif
4168 #ifndef DEBUG_STR_DWO_SECTION
4169 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4170 #endif
4171 #ifndef DEBUG_LTO_STR_DWO_SECTION
4172 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4173 #endif
4174 #ifndef DEBUG_RANGES_SECTION
4175 #define DEBUG_RANGES_SECTION ".debug_ranges"
4176 #endif
4177 #ifndef DEBUG_RNGLISTS_SECTION
4178 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4179 #endif
4180 #ifndef DEBUG_DWO_RNGLISTS_SECTION
4181 #define DEBUG_DWO_RNGLISTS_SECTION ".debug_rnglists.dwo"
4182 #endif
4183 #ifndef DEBUG_LINE_STR_SECTION
4184 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4185 #endif
4186 #ifndef DEBUG_LTO_LINE_STR_SECTION
4187 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4188 #endif
4190 /* Section flags for .debug_str section. */
4191 #define DEBUG_STR_SECTION_FLAGS \
4192 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4193 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4194 : SECTION_DEBUG)
4196 /* Section flags for .debug_str.dwo section. */
4197 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4199 /* Attribute used to refer to the macro section. */
4200 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4201 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4203 /* Labels we insert at beginning sections we can reference instead of
4204 the section names themselves. */
4206 #ifndef TEXT_SECTION_LABEL
4207 #define TEXT_SECTION_LABEL "Ltext"
4208 #endif
4209 #ifndef COLD_TEXT_SECTION_LABEL
4210 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4211 #endif
4212 #ifndef DEBUG_LINE_SECTION_LABEL
4213 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4214 #endif
4215 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4216 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4217 #endif
4218 #ifndef DEBUG_INFO_SECTION_LABEL
4219 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4220 #endif
4221 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4222 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4223 #endif
4224 #ifndef DEBUG_ABBREV_SECTION_LABEL
4225 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4226 #endif
4227 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4228 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4229 #endif
4230 #ifndef DEBUG_ADDR_SECTION_LABEL
4231 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4232 #endif
4233 #ifndef DEBUG_LOC_SECTION_LABEL
4234 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4235 #endif
4236 #ifndef DEBUG_RANGES_SECTION_LABEL
4237 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4238 #endif
4239 #ifndef DEBUG_MACINFO_SECTION_LABEL
4240 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4241 #endif
4242 #ifndef DEBUG_MACRO_SECTION_LABEL
4243 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4244 #endif
4245 #define SKELETON_COMP_DIE_ABBREV 1
4246 #define SKELETON_TYPE_DIE_ABBREV 2
4248 /* Definitions of defaults for formats and names of various special
4249 (artificial) labels which may be generated within this file (when the -g
4250 options is used and DWARF2_DEBUGGING_INFO is in effect.
4251 If necessary, these may be overridden from within the tm.h file, but
4252 typically, overriding these defaults is unnecessary. */
4254 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4255 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4256 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4257 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4258 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4259 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4260 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4261 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4262 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4263 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4264 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4265 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4266 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4267 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4268 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4270 #ifndef TEXT_END_LABEL
4271 #define TEXT_END_LABEL "Letext"
4272 #endif
4273 #ifndef COLD_END_LABEL
4274 #define COLD_END_LABEL "Letext_cold"
4275 #endif
4276 #ifndef BLOCK_BEGIN_LABEL
4277 #define BLOCK_BEGIN_LABEL "LBB"
4278 #endif
4279 #ifndef BLOCK_INLINE_ENTRY_LABEL
4280 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4281 #endif
4282 #ifndef BLOCK_END_LABEL
4283 #define BLOCK_END_LABEL "LBE"
4284 #endif
4285 #ifndef LINE_CODE_LABEL
4286 #define LINE_CODE_LABEL "LM"
4287 #endif
4290 /* Return the root of the DIE's built for the current compilation unit. */
4291 static dw_die_ref
4292 comp_unit_die (void)
4294 if (!single_comp_unit_die)
4295 single_comp_unit_die = gen_compile_unit_die (NULL);
4296 return single_comp_unit_die;
4299 /* We allow a language front-end to designate a function that is to be
4300 called to "demangle" any name before it is put into a DIE. */
4302 static const char *(*demangle_name_func) (const char *);
4304 void
4305 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4307 demangle_name_func = func;
4310 /* Test if rtl node points to a pseudo register. */
4312 static inline bool
4313 is_pseudo_reg (const_rtx rtl)
4315 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4316 || (GET_CODE (rtl) == SUBREG
4317 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4320 /* Return a reference to a type, with its const and volatile qualifiers
4321 removed. */
4323 static inline tree
4324 type_main_variant (tree type)
4326 type = TYPE_MAIN_VARIANT (type);
4328 /* ??? There really should be only one main variant among any group of
4329 variants of a given type (and all of the MAIN_VARIANT values for all
4330 members of the group should point to that one type) but sometimes the C
4331 front-end messes this up for array types, so we work around that bug
4332 here. */
4333 if (TREE_CODE (type) == ARRAY_TYPE)
4334 while (type != TYPE_MAIN_VARIANT (type))
4335 type = TYPE_MAIN_VARIANT (type);
4337 return type;
4340 /* Return true if the given type node represents a tagged type. */
4342 static inline bool
4343 is_tagged_type (const_tree type)
4345 enum tree_code code = TREE_CODE (type);
4347 return (code == RECORD_TYPE || code == UNION_TYPE
4348 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4351 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4353 static void
4354 get_ref_die_offset_label (char *label, dw_die_ref ref)
4356 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4359 /* Return die_offset of a DIE reference to a base type. */
4361 static unsigned long int
4362 get_base_type_offset (dw_die_ref ref)
4364 if (ref->die_offset)
4365 return ref->die_offset;
4366 if (comp_unit_die ()->die_abbrev)
4368 calc_base_type_die_sizes ();
4369 gcc_assert (ref->die_offset);
4371 return ref->die_offset;
4374 /* Return die_offset of a DIE reference other than base type. */
4376 static unsigned long int
4377 get_ref_die_offset (dw_die_ref ref)
4379 gcc_assert (ref->die_offset);
4380 return ref->die_offset;
4383 /* Convert a DIE tag into its string name. */
4385 static const char *
4386 dwarf_tag_name (unsigned int tag)
4388 const char *name = get_DW_TAG_name (tag);
4390 if (name != NULL)
4391 return name;
4393 return "DW_TAG_<unknown>";
4396 /* Convert a DWARF attribute code into its string name. */
4398 static const char *
4399 dwarf_attr_name (unsigned int attr)
4401 const char *name;
4403 switch (attr)
4405 #if VMS_DEBUGGING_INFO
4406 case DW_AT_HP_prologue:
4407 return "DW_AT_HP_prologue";
4408 #else
4409 case DW_AT_MIPS_loop_unroll_factor:
4410 return "DW_AT_MIPS_loop_unroll_factor";
4411 #endif
4413 #if VMS_DEBUGGING_INFO
4414 case DW_AT_HP_epilogue:
4415 return "DW_AT_HP_epilogue";
4416 #else
4417 case DW_AT_MIPS_stride:
4418 return "DW_AT_MIPS_stride";
4419 #endif
4422 name = get_DW_AT_name (attr);
4424 if (name != NULL)
4425 return name;
4427 return "DW_AT_<unknown>";
4430 /* Convert a DWARF value form code into its string name. */
4432 static const char *
4433 dwarf_form_name (unsigned int form)
4435 const char *name = get_DW_FORM_name (form);
4437 if (name != NULL)
4438 return name;
4440 return "DW_FORM_<unknown>";
4443 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4444 instance of an inlined instance of a decl which is local to an inline
4445 function, so we have to trace all of the way back through the origin chain
4446 to find out what sort of node actually served as the original seed for the
4447 given block. */
4449 static tree
4450 decl_ultimate_origin (const_tree decl)
4452 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4453 return NULL_TREE;
4455 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4456 we're trying to output the abstract instance of this function. */
4457 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4458 return NULL_TREE;
4460 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4461 most distant ancestor, this should never happen. */
4462 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4464 return DECL_ABSTRACT_ORIGIN (decl);
4467 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4468 of a virtual function may refer to a base class, so we check the 'this'
4469 parameter. */
4471 static tree
4472 decl_class_context (tree decl)
4474 tree context = NULL_TREE;
4476 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4477 context = DECL_CONTEXT (decl);
4478 else
4479 context = TYPE_MAIN_VARIANT
4480 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4482 if (context && !TYPE_P (context))
4483 context = NULL_TREE;
4485 return context;
4488 /* Add an attribute/value pair to a DIE. */
4490 static inline void
4491 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4493 /* Maybe this should be an assert? */
4494 if (die == NULL)
4495 return;
4497 if (flag_checking)
4499 /* Check we do not add duplicate attrs. Can't use get_AT here
4500 because that recurses to the specification/abstract origin DIE. */
4501 dw_attr_node *a;
4502 unsigned ix;
4503 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4504 gcc_assert (a->dw_attr != attr->dw_attr);
4507 vec_safe_reserve (die->die_attr, 1);
4508 vec_safe_push (die->die_attr, *attr);
4511 enum dw_val_class
4512 AT_class (dw_attr_node *a)
4514 return a->dw_attr_val.val_class;
4517 /* Return the index for any attribute that will be referenced with a
4518 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4519 indices are stored in dw_attr_val.v.val_str for reference counting
4520 pruning. */
4522 static inline unsigned int
4523 AT_index (dw_attr_node *a)
4525 if (AT_class (a) == dw_val_class_str)
4526 return a->dw_attr_val.v.val_str->index;
4527 else if (a->dw_attr_val.val_entry != NULL)
4528 return a->dw_attr_val.val_entry->index;
4529 return NOT_INDEXED;
4532 /* Add a flag value attribute to a DIE. */
4534 static inline void
4535 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4537 dw_attr_node attr;
4539 attr.dw_attr = attr_kind;
4540 attr.dw_attr_val.val_class = dw_val_class_flag;
4541 attr.dw_attr_val.val_entry = NULL;
4542 attr.dw_attr_val.v.val_flag = flag;
4543 add_dwarf_attr (die, &attr);
4546 static inline unsigned
4547 AT_flag (dw_attr_node *a)
4549 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4550 return a->dw_attr_val.v.val_flag;
4553 /* Add a signed integer attribute value to a DIE. */
4555 static inline void
4556 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4558 dw_attr_node attr;
4560 attr.dw_attr = attr_kind;
4561 attr.dw_attr_val.val_class = dw_val_class_const;
4562 attr.dw_attr_val.val_entry = NULL;
4563 attr.dw_attr_val.v.val_int = int_val;
4564 add_dwarf_attr (die, &attr);
4567 HOST_WIDE_INT
4568 AT_int (dw_attr_node *a)
4570 gcc_assert (a && (AT_class (a) == dw_val_class_const
4571 || AT_class (a) == dw_val_class_const_implicit));
4572 return a->dw_attr_val.v.val_int;
4575 /* Add an unsigned integer attribute value to a DIE. */
4577 static inline void
4578 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4579 unsigned HOST_WIDE_INT unsigned_val)
4581 dw_attr_node attr;
4583 attr.dw_attr = attr_kind;
4584 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4585 attr.dw_attr_val.val_entry = NULL;
4586 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4587 add_dwarf_attr (die, &attr);
4590 unsigned HOST_WIDE_INT
4591 AT_unsigned (dw_attr_node *a)
4593 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4594 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4595 return a->dw_attr_val.v.val_unsigned;
4598 dw_wide_int *
4599 alloc_dw_wide_int (const wide_int_ref &w)
4601 dw_wide_int *p
4602 = (dw_wide_int *) ggc_internal_alloc (sizeof (dw_wide_int)
4603 + ((w.get_len () - 1)
4604 * sizeof (HOST_WIDE_INT)));
4605 p->precision = w.get_precision ();
4606 p->len = w.get_len ();
4607 memcpy (p->val, w.get_val (), p->len * sizeof (HOST_WIDE_INT));
4608 return p;
4611 /* Add an unsigned wide integer attribute value to a DIE. */
4613 static inline void
4614 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4615 const wide_int_ref &w)
4617 dw_attr_node attr;
4619 attr.dw_attr = attr_kind;
4620 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4621 attr.dw_attr_val.val_entry = NULL;
4622 attr.dw_attr_val.v.val_wide = alloc_dw_wide_int (w);
4623 add_dwarf_attr (die, &attr);
4626 /* Add an unsigned double integer attribute value to a DIE. */
4628 static inline void
4629 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4630 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4632 dw_attr_node attr;
4634 attr.dw_attr = attr_kind;
4635 attr.dw_attr_val.val_class = dw_val_class_const_double;
4636 attr.dw_attr_val.val_entry = NULL;
4637 attr.dw_attr_val.v.val_double.high = high;
4638 attr.dw_attr_val.v.val_double.low = low;
4639 add_dwarf_attr (die, &attr);
4642 /* Add a floating point attribute value to a DIE and return it. */
4644 static inline void
4645 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4646 unsigned int length, unsigned int elt_size, unsigned char *array)
4648 dw_attr_node attr;
4650 attr.dw_attr = attr_kind;
4651 attr.dw_attr_val.val_class = dw_val_class_vec;
4652 attr.dw_attr_val.val_entry = NULL;
4653 attr.dw_attr_val.v.val_vec.length = length;
4654 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4655 attr.dw_attr_val.v.val_vec.array = array;
4656 add_dwarf_attr (die, &attr);
4659 /* Add an 8-byte data attribute value to a DIE. */
4661 static inline void
4662 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4663 unsigned char data8[8])
4665 dw_attr_node attr;
4667 attr.dw_attr = attr_kind;
4668 attr.dw_attr_val.val_class = dw_val_class_data8;
4669 attr.dw_attr_val.val_entry = NULL;
4670 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4671 add_dwarf_attr (die, &attr);
4674 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4675 dwarf_split_debug_info, address attributes in dies destined for the
4676 final executable have force_direct set to avoid using indexed
4677 references. */
4679 static inline void
4680 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4681 bool force_direct)
4683 dw_attr_node attr;
4684 char * lbl_id;
4686 lbl_id = xstrdup (lbl_low);
4687 attr.dw_attr = DW_AT_low_pc;
4688 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4689 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4690 if (dwarf_split_debug_info && !force_direct)
4691 attr.dw_attr_val.val_entry
4692 = add_addr_table_entry (lbl_id, ate_kind_label);
4693 else
4694 attr.dw_attr_val.val_entry = NULL;
4695 add_dwarf_attr (die, &attr);
4697 attr.dw_attr = DW_AT_high_pc;
4698 if (dwarf_version < 4)
4699 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4700 else
4701 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4702 lbl_id = xstrdup (lbl_high);
4703 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4704 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4705 && dwarf_split_debug_info && !force_direct)
4706 attr.dw_attr_val.val_entry
4707 = add_addr_table_entry (lbl_id, ate_kind_label);
4708 else
4709 attr.dw_attr_val.val_entry = NULL;
4710 add_dwarf_attr (die, &attr);
4713 /* Hash and equality functions for debug_str_hash. */
4715 hashval_t
4716 indirect_string_hasher::hash (indirect_string_node *x)
4718 return htab_hash_string (x->str);
4721 bool
4722 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4724 return strcmp (x1->str, x2) == 0;
4727 /* Add STR to the given string hash table. */
4729 static struct indirect_string_node *
4730 find_AT_string_in_table (const char *str,
4731 hash_table<indirect_string_hasher> *table,
4732 enum insert_option insert = INSERT)
4734 struct indirect_string_node *node;
4736 indirect_string_node **slot
4737 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4738 if (*slot == NULL)
4740 node = ggc_cleared_alloc<indirect_string_node> ();
4741 node->str = ggc_strdup (str);
4742 *slot = node;
4744 else
4745 node = *slot;
4747 node->refcount++;
4748 return node;
4751 /* Add STR to the indirect string hash table. */
4753 static struct indirect_string_node *
4754 find_AT_string (const char *str, enum insert_option insert = INSERT)
4756 if (! debug_str_hash)
4757 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4759 return find_AT_string_in_table (str, debug_str_hash, insert);
4762 /* Add a string attribute value to a DIE. */
4764 static inline void
4765 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4767 dw_attr_node attr;
4768 struct indirect_string_node *node;
4770 node = find_AT_string (str);
4772 attr.dw_attr = attr_kind;
4773 attr.dw_attr_val.val_class = dw_val_class_str;
4774 attr.dw_attr_val.val_entry = NULL;
4775 attr.dw_attr_val.v.val_str = node;
4776 add_dwarf_attr (die, &attr);
4779 static inline const char *
4780 AT_string (dw_attr_node *a)
4782 gcc_assert (a && AT_class (a) == dw_val_class_str);
4783 return a->dw_attr_val.v.val_str->str;
4786 /* Call this function directly to bypass AT_string_form's logic to put
4787 the string inline in the die. */
4789 static void
4790 set_indirect_string (struct indirect_string_node *node)
4792 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4793 /* Already indirect is a no op. */
4794 if (node->form == DW_FORM_strp
4795 || node->form == DW_FORM_line_strp
4796 || node->form == dwarf_FORM (DW_FORM_strx))
4798 gcc_assert (node->label);
4799 return;
4801 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4802 ++dw2_string_counter;
4803 node->label = xstrdup (label);
4805 if (!dwarf_split_debug_info)
4807 node->form = DW_FORM_strp;
4808 node->index = NOT_INDEXED;
4810 else
4812 node->form = dwarf_FORM (DW_FORM_strx);
4813 node->index = NO_INDEX_ASSIGNED;
4817 /* A helper function for dwarf2out_finish, called to reset indirect
4818 string decisions done for early LTO dwarf output before fat object
4819 dwarf output. */
4822 reset_indirect_string (indirect_string_node **h, void *)
4824 struct indirect_string_node *node = *h;
4825 if (node->form == DW_FORM_strp
4826 || node->form == DW_FORM_line_strp
4827 || node->form == dwarf_FORM (DW_FORM_strx))
4829 free (node->label);
4830 node->label = NULL;
4831 node->form = (dwarf_form) 0;
4832 node->index = 0;
4834 return 1;
4837 /* Add a string representing a file or filepath attribute value to a DIE. */
4839 static inline void
4840 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4841 const char *str)
4843 if (! asm_outputs_debug_line_str ())
4844 add_AT_string (die, attr_kind, str);
4845 else
4847 dw_attr_node attr;
4848 struct indirect_string_node *node;
4850 if (!debug_line_str_hash)
4851 debug_line_str_hash
4852 = hash_table<indirect_string_hasher>::create_ggc (10);
4854 node = find_AT_string_in_table (str, debug_line_str_hash);
4855 set_indirect_string (node);
4856 node->form = DW_FORM_line_strp;
4858 attr.dw_attr = attr_kind;
4859 attr.dw_attr_val.val_class = dw_val_class_str;
4860 attr.dw_attr_val.val_entry = NULL;
4861 attr.dw_attr_val.v.val_str = node;
4862 add_dwarf_attr (die, &attr);
4866 /* Find out whether a string should be output inline in DIE
4867 or out-of-line in .debug_str section. */
4869 static enum dwarf_form
4870 find_string_form (struct indirect_string_node *node)
4872 unsigned int len;
4874 if (node->form)
4875 return node->form;
4877 len = strlen (node->str) + 1;
4879 /* If the string is shorter or equal to the size of the reference, it is
4880 always better to put it inline. */
4881 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4882 return node->form = DW_FORM_string;
4884 /* If we cannot expect the linker to merge strings in .debug_str
4885 section, only put it into .debug_str if it is worth even in this
4886 single module. */
4887 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4888 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4889 && (len - dwarf_offset_size) * node->refcount <= len))
4890 return node->form = DW_FORM_string;
4892 set_indirect_string (node);
4894 return node->form;
4897 /* Find out whether the string referenced from the attribute should be
4898 output inline in DIE or out-of-line in .debug_str section. */
4900 static enum dwarf_form
4901 AT_string_form (dw_attr_node *a)
4903 gcc_assert (a && AT_class (a) == dw_val_class_str);
4904 return find_string_form (a->dw_attr_val.v.val_str);
4907 /* Add a DIE reference attribute value to a DIE. */
4909 static inline void
4910 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4912 dw_attr_node attr;
4913 gcc_checking_assert (targ_die != NULL);
4914 gcc_assert (targ_die != die
4915 || (attr_kind != DW_AT_abstract_origin
4916 && attr_kind != DW_AT_specification));
4918 /* With LTO we can end up trying to reference something we didn't create
4919 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4920 if (targ_die == NULL)
4921 return;
4923 attr.dw_attr = attr_kind;
4924 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4925 attr.dw_attr_val.val_entry = NULL;
4926 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4927 attr.dw_attr_val.v.val_die_ref.external = 0;
4928 add_dwarf_attr (die, &attr);
4931 /* Change DIE reference REF to point to NEW_DIE instead. */
4933 static inline void
4934 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4936 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4937 ref->dw_attr_val.v.val_die_ref.die = new_die;
4938 ref->dw_attr_val.v.val_die_ref.external = 0;
4941 /* Add an AT_specification attribute to a DIE, and also make the back
4942 pointer from the specification to the definition. */
4944 static inline void
4945 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4947 add_AT_die_ref (die, DW_AT_specification, targ_die);
4948 gcc_assert (!targ_die->die_definition);
4949 targ_die->die_definition = die;
4952 static inline dw_die_ref
4953 AT_ref (dw_attr_node *a)
4955 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4956 return a->dw_attr_val.v.val_die_ref.die;
4959 static inline int
4960 AT_ref_external (dw_attr_node *a)
4962 if (a && AT_class (a) == dw_val_class_die_ref)
4963 return a->dw_attr_val.v.val_die_ref.external;
4965 return 0;
4968 static inline void
4969 set_AT_ref_external (dw_attr_node *a, int i)
4971 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4972 a->dw_attr_val.v.val_die_ref.external = i;
4975 /* Add a location description attribute value to a DIE. */
4977 static inline void
4978 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4980 dw_attr_node attr;
4982 attr.dw_attr = attr_kind;
4983 attr.dw_attr_val.val_class = dw_val_class_loc;
4984 attr.dw_attr_val.val_entry = NULL;
4985 attr.dw_attr_val.v.val_loc = loc;
4986 add_dwarf_attr (die, &attr);
4989 dw_loc_descr_ref
4990 AT_loc (dw_attr_node *a)
4992 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4993 return a->dw_attr_val.v.val_loc;
4996 static inline void
4997 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4999 dw_attr_node attr;
5001 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5002 return;
5004 attr.dw_attr = attr_kind;
5005 attr.dw_attr_val.val_class = dw_val_class_loc_list;
5006 attr.dw_attr_val.val_entry = NULL;
5007 attr.dw_attr_val.v.val_loc_list = loc_list;
5008 add_dwarf_attr (die, &attr);
5009 have_location_lists = true;
5012 static inline dw_loc_list_ref
5013 AT_loc_list (dw_attr_node *a)
5015 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5016 return a->dw_attr_val.v.val_loc_list;
5019 /* Add a view list attribute to DIE. It must have a DW_AT_location
5020 attribute, because the view list complements the location list. */
5022 static inline void
5023 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
5025 dw_attr_node attr;
5027 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5028 return;
5030 attr.dw_attr = attr_kind;
5031 attr.dw_attr_val.val_class = dw_val_class_view_list;
5032 attr.dw_attr_val.val_entry = NULL;
5033 attr.dw_attr_val.v.val_view_list = die;
5034 add_dwarf_attr (die, &attr);
5035 gcc_checking_assert (get_AT (die, DW_AT_location));
5036 gcc_assert (have_location_lists);
5039 /* Return a pointer to the location list referenced by the attribute.
5040 If the named attribute is a view list, look up the corresponding
5041 DW_AT_location attribute and return its location list. */
5043 static inline dw_loc_list_ref *
5044 AT_loc_list_ptr (dw_attr_node *a)
5046 gcc_assert (a);
5047 switch (AT_class (a))
5049 case dw_val_class_loc_list:
5050 return &a->dw_attr_val.v.val_loc_list;
5051 case dw_val_class_view_list:
5053 dw_attr_node *l;
5054 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5055 if (!l)
5056 return NULL;
5057 gcc_checking_assert (l + 1 == a);
5058 return AT_loc_list_ptr (l);
5060 default:
5061 gcc_unreachable ();
5065 /* Return the location attribute value associated with a view list
5066 attribute value. */
5068 static inline dw_val_node *
5069 view_list_to_loc_list_val_node (dw_val_node *val)
5071 gcc_assert (val->val_class == dw_val_class_view_list);
5072 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5073 if (!loc)
5074 return NULL;
5075 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5076 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5077 return &loc->dw_attr_val;
5080 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5082 static hashval_t hash (addr_table_entry *);
5083 static bool equal (addr_table_entry *, addr_table_entry *);
5086 /* Table of entries into the .debug_addr section. */
5088 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5090 /* Hash an address_table_entry. */
5092 hashval_t
5093 addr_hasher::hash (addr_table_entry *a)
5095 inchash::hash hstate;
5096 switch (a->kind)
5098 case ate_kind_rtx:
5099 hstate.add_int (0);
5100 break;
5101 case ate_kind_rtx_dtprel:
5102 hstate.add_int (1);
5103 break;
5104 case ate_kind_label:
5105 return htab_hash_string (a->addr.label);
5106 default:
5107 gcc_unreachable ();
5109 inchash::add_rtx (a->addr.rtl, hstate);
5110 return hstate.end ();
5113 /* Determine equality for two address_table_entries. */
5115 bool
5116 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5118 if (a1->kind != a2->kind)
5119 return false;
5120 switch (a1->kind)
5122 case ate_kind_rtx:
5123 case ate_kind_rtx_dtprel:
5124 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5125 case ate_kind_label:
5126 return strcmp (a1->addr.label, a2->addr.label) == 0;
5127 default:
5128 gcc_unreachable ();
5132 /* Initialize an addr_table_entry. */
5134 void
5135 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5137 e->kind = kind;
5138 switch (kind)
5140 case ate_kind_rtx:
5141 case ate_kind_rtx_dtprel:
5142 e->addr.rtl = (rtx) addr;
5143 break;
5144 case ate_kind_label:
5145 e->addr.label = (char *) addr;
5146 break;
5148 e->refcount = 0;
5149 e->index = NO_INDEX_ASSIGNED;
5152 /* Add attr to the address table entry to the table. Defer setting an
5153 index until output time. */
5155 static addr_table_entry *
5156 add_addr_table_entry (void *addr, enum ate_kind kind)
5158 addr_table_entry *node;
5159 addr_table_entry finder;
5161 gcc_assert (dwarf_split_debug_info);
5162 if (! addr_index_table)
5163 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5164 init_addr_table_entry (&finder, kind, addr);
5165 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5167 if (*slot == HTAB_EMPTY_ENTRY)
5169 node = ggc_cleared_alloc<addr_table_entry> ();
5170 init_addr_table_entry (node, kind, addr);
5171 *slot = node;
5173 else
5174 node = *slot;
5176 node->refcount++;
5177 return node;
5180 /* Remove an entry from the addr table by decrementing its refcount.
5181 Strictly, decrementing the refcount would be enough, but the
5182 assertion that the entry is actually in the table has found
5183 bugs. */
5185 static void
5186 remove_addr_table_entry (addr_table_entry *entry)
5188 gcc_assert (dwarf_split_debug_info && addr_index_table);
5189 /* After an index is assigned, the table is frozen. */
5190 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5191 entry->refcount--;
5194 /* Given a location list, remove all addresses it refers to from the
5195 address_table. */
5197 static void
5198 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5200 for (; descr; descr = descr->dw_loc_next)
5201 if (descr->dw_loc_oprnd1.val_entry != NULL)
5203 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5204 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5208 /* A helper function for dwarf2out_finish called through
5209 htab_traverse. Assign an addr_table_entry its index. All entries
5210 must be collected into the table when this function is called,
5211 because the indexing code relies on htab_traverse to traverse nodes
5212 in the same order for each run. */
5215 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5217 addr_table_entry *node = *h;
5219 /* Don't index unreferenced nodes. */
5220 if (node->refcount == 0)
5221 return 1;
5223 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5224 node->index = *index;
5225 *index += 1;
5227 return 1;
5230 /* Return the tag of a given DIE. */
5232 enum dwarf_tag
5233 dw_get_die_tag (dw_die_ref die)
5235 return die->die_tag;
5238 /* Return a reference to the children list of a given DIE. */
5240 dw_die_ref
5241 dw_get_die_child (dw_die_ref die)
5243 return die->die_child;
5246 /* Return a reference to the sibling of a given DIE. */
5248 dw_die_ref
5249 dw_get_die_sib (dw_die_ref die)
5251 return die->die_sib;
5254 /* Add an address constant attribute value to a DIE. When using
5255 dwarf_split_debug_info, address attributes in dies destined for the
5256 final executable should be direct references--setting the parameter
5257 force_direct ensures this behavior. */
5259 static inline void
5260 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5261 bool force_direct)
5263 dw_attr_node attr;
5265 attr.dw_attr = attr_kind;
5266 attr.dw_attr_val.val_class = dw_val_class_addr;
5267 attr.dw_attr_val.v.val_addr = addr;
5268 if (dwarf_split_debug_info && !force_direct)
5269 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5270 else
5271 attr.dw_attr_val.val_entry = NULL;
5272 add_dwarf_attr (die, &attr);
5275 /* Get the RTX from to an address DIE attribute. */
5277 static inline rtx
5278 AT_addr (dw_attr_node *a)
5280 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5281 return a->dw_attr_val.v.val_addr;
5284 /* Add a file attribute value to a DIE. */
5286 static inline void
5287 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5288 struct dwarf_file_data *fd)
5290 dw_attr_node attr;
5292 attr.dw_attr = attr_kind;
5293 attr.dw_attr_val.val_class = dw_val_class_file;
5294 attr.dw_attr_val.val_entry = NULL;
5295 attr.dw_attr_val.v.val_file = fd;
5296 add_dwarf_attr (die, &attr);
5299 /* Get the dwarf_file_data from a file DIE attribute. */
5301 static inline struct dwarf_file_data *
5302 AT_file (dw_attr_node *a)
5304 gcc_assert (a && (AT_class (a) == dw_val_class_file
5305 || AT_class (a) == dw_val_class_file_implicit));
5306 return a->dw_attr_val.v.val_file;
5309 #if VMS_DEBUGGING_INFO
5310 /* Add a vms delta attribute value to a DIE. */
5312 static inline void
5313 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5314 const char *lbl1, const char *lbl2)
5316 dw_attr_node attr;
5318 attr.dw_attr = attr_kind;
5319 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5320 attr.dw_attr_val.val_entry = NULL;
5321 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5322 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5323 add_dwarf_attr (die, &attr);
5325 #endif
5327 /* Add a symbolic view identifier attribute value to a DIE. */
5329 static inline void
5330 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5331 const char *view_label)
5333 dw_attr_node attr;
5335 attr.dw_attr = attr_kind;
5336 attr.dw_attr_val.val_class = dw_val_class_symview;
5337 attr.dw_attr_val.val_entry = NULL;
5338 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5339 add_dwarf_attr (die, &attr);
5342 /* Add a label identifier attribute value to a DIE. */
5344 static inline void
5345 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5346 const char *lbl_id)
5348 dw_attr_node attr;
5350 attr.dw_attr = attr_kind;
5351 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5352 attr.dw_attr_val.val_entry = NULL;
5353 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5354 if (dwarf_split_debug_info)
5355 attr.dw_attr_val.val_entry
5356 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5357 ate_kind_label);
5358 add_dwarf_attr (die, &attr);
5361 /* Add a section offset attribute value to a DIE, an offset into the
5362 debug_line section. */
5364 static inline void
5365 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5366 const char *label)
5368 dw_attr_node attr;
5370 attr.dw_attr = attr_kind;
5371 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5372 attr.dw_attr_val.val_entry = NULL;
5373 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5374 add_dwarf_attr (die, &attr);
5377 /* Add a section offset attribute value to a DIE, an offset into the
5378 debug_macinfo section. */
5380 static inline void
5381 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5382 const char *label)
5384 dw_attr_node attr;
5386 attr.dw_attr = attr_kind;
5387 attr.dw_attr_val.val_class = dw_val_class_macptr;
5388 attr.dw_attr_val.val_entry = NULL;
5389 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5390 add_dwarf_attr (die, &attr);
5393 /* Add a range_list attribute value to a DIE. When using
5394 dwarf_split_debug_info, address attributes in dies destined for the
5395 final executable should be direct references--setting the parameter
5396 force_direct ensures this behavior. */
5398 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5399 #define RELOCATED_OFFSET (NULL)
5401 static void
5402 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5403 long unsigned int offset, bool force_direct)
5405 dw_attr_node attr;
5407 attr.dw_attr = attr_kind;
5408 attr.dw_attr_val.val_class = dw_val_class_range_list;
5409 /* For the range_list attribute, use val_entry to store whether the
5410 offset should follow split-debug-info or normal semantics. This
5411 value is read in output_range_list_offset. */
5412 if (dwarf_split_debug_info && !force_direct)
5413 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5414 else
5415 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5416 attr.dw_attr_val.v.val_offset = offset;
5417 add_dwarf_attr (die, &attr);
5420 /* Return the start label of a delta attribute. */
5422 static inline const char *
5423 AT_vms_delta1 (dw_attr_node *a)
5425 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5426 return a->dw_attr_val.v.val_vms_delta.lbl1;
5429 /* Return the end label of a delta attribute. */
5431 static inline const char *
5432 AT_vms_delta2 (dw_attr_node *a)
5434 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5435 return a->dw_attr_val.v.val_vms_delta.lbl2;
5438 static inline const char *
5439 AT_lbl (dw_attr_node *a)
5441 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5442 || AT_class (a) == dw_val_class_lineptr
5443 || AT_class (a) == dw_val_class_macptr
5444 || AT_class (a) == dw_val_class_loclistsptr
5445 || AT_class (a) == dw_val_class_high_pc));
5446 return a->dw_attr_val.v.val_lbl_id;
5449 /* Get the attribute of type attr_kind. */
5451 dw_attr_node *
5452 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5454 dw_attr_node *a;
5455 unsigned ix;
5456 dw_die_ref spec = NULL;
5458 if (! die)
5459 return NULL;
5461 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5462 if (a->dw_attr == attr_kind)
5463 return a;
5464 else if (a->dw_attr == DW_AT_specification
5465 || a->dw_attr == DW_AT_abstract_origin)
5466 spec = AT_ref (a);
5468 if (spec)
5469 return get_AT (spec, attr_kind);
5471 return NULL;
5474 /* Returns the parent of the declaration of DIE. */
5476 static dw_die_ref
5477 get_die_parent (dw_die_ref die)
5479 dw_die_ref t;
5481 if (!die)
5482 return NULL;
5484 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5485 || (t = get_AT_ref (die, DW_AT_specification)))
5486 die = t;
5488 return die->die_parent;
5491 /* Return the "low pc" attribute value, typically associated with a subprogram
5492 DIE. Return null if the "low pc" attribute is either not present, or if it
5493 cannot be represented as an assembler label identifier. */
5495 static inline const char *
5496 get_AT_low_pc (dw_die_ref die)
5498 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5500 return a ? AT_lbl (a) : NULL;
5503 /* Return the value of the string attribute designated by ATTR_KIND, or
5504 NULL if it is not present. */
5506 const char *
5507 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5509 dw_attr_node *a = get_AT (die, attr_kind);
5511 return a ? AT_string (a) : NULL;
5514 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5515 if it is not present. */
5518 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5520 dw_attr_node *a = get_AT (die, attr_kind);
5522 return a ? AT_flag (a) : 0;
5525 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5526 if it is not present. */
5528 unsigned
5529 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5531 dw_attr_node *a = get_AT (die, attr_kind);
5533 return a ? AT_unsigned (a) : 0;
5536 dw_die_ref
5537 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5539 dw_attr_node *a = get_AT (die, attr_kind);
5541 return a ? AT_ref (a) : NULL;
5544 struct dwarf_file_data *
5545 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5547 dw_attr_node *a = get_AT (die, attr_kind);
5549 return a ? AT_file (a) : NULL;
5552 /* Return TRUE if the language is C. */
5554 static inline bool
5555 is_c (void)
5557 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5559 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5560 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5565 /* Return TRUE if the language is C++. */
5567 static inline bool
5568 is_cxx (void)
5570 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5572 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5573 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5576 /* Return TRUE if DECL was created by the C++ frontend. */
5578 static bool
5579 is_cxx (const_tree decl)
5581 if (in_lto_p)
5583 const_tree context = get_ultimate_context (decl);
5584 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5585 return startswith (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++");
5587 return is_cxx ();
5590 /* Return TRUE if the language is Fortran. */
5592 static inline bool
5593 is_fortran (void)
5595 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5597 return (lang == DW_LANG_Fortran77
5598 || lang == DW_LANG_Fortran90
5599 || lang == DW_LANG_Fortran95
5600 || lang == DW_LANG_Fortran03
5601 || lang == DW_LANG_Fortran08);
5604 static inline bool
5605 is_fortran (const_tree decl)
5607 if (in_lto_p)
5609 const_tree context = get_ultimate_context (decl);
5610 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5611 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5612 "GNU Fortran", 11) == 0
5613 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5614 "GNU F77") == 0);
5616 return is_fortran ();
5619 /* Return TRUE if the language is Rust.
5620 Note, returns FALSE for dwarf_version < 5 && dwarf_strict. */
5622 static inline bool
5623 is_rust (void)
5625 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5627 return lang == DW_LANG_Rust;
5630 /* Return TRUE if the language is Ada. */
5632 static inline bool
5633 is_ada (void)
5635 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5637 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5640 /* Return TRUE if the language is D. */
5642 static inline bool
5643 is_dlang (void)
5645 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5647 return lang == DW_LANG_D;
5650 /* Remove the specified attribute if present. Return TRUE if removal
5651 was successful. */
5653 static bool
5654 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5656 dw_attr_node *a;
5657 unsigned ix;
5659 if (! die)
5660 return false;
5662 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5663 if (a->dw_attr == attr_kind)
5665 if (AT_class (a) == dw_val_class_str)
5666 if (a->dw_attr_val.v.val_str->refcount)
5667 a->dw_attr_val.v.val_str->refcount--;
5669 /* vec::ordered_remove should help reduce the number of abbrevs
5670 that are needed. */
5671 die->die_attr->ordered_remove (ix);
5672 return true;
5674 return false;
5677 /* Remove CHILD from its parent. PREV must have the property that
5678 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5680 static void
5681 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5683 gcc_assert (child->die_parent == prev->die_parent);
5684 gcc_assert (prev->die_sib == child);
5685 if (prev == child)
5687 gcc_assert (child->die_parent->die_child == child);
5688 prev = NULL;
5690 else
5691 prev->die_sib = child->die_sib;
5692 if (child->die_parent->die_child == child)
5693 child->die_parent->die_child = prev;
5694 child->die_sib = NULL;
5697 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5698 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5700 static void
5701 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5703 dw_die_ref parent = old_child->die_parent;
5705 gcc_assert (parent == prev->die_parent);
5706 gcc_assert (prev->die_sib == old_child);
5708 new_child->die_parent = parent;
5709 if (prev == old_child)
5711 gcc_assert (parent->die_child == old_child);
5712 new_child->die_sib = new_child;
5714 else
5716 prev->die_sib = new_child;
5717 new_child->die_sib = old_child->die_sib;
5719 if (old_child->die_parent->die_child == old_child)
5720 old_child->die_parent->die_child = new_child;
5721 old_child->die_sib = NULL;
5724 /* Move all children from OLD_PARENT to NEW_PARENT. */
5726 static void
5727 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5729 dw_die_ref c;
5730 new_parent->die_child = old_parent->die_child;
5731 old_parent->die_child = NULL;
5732 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5735 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5736 matches TAG. */
5738 static void
5739 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5741 dw_die_ref c;
5743 c = die->die_child;
5744 if (c) do {
5745 dw_die_ref prev = c;
5746 c = c->die_sib;
5747 while (c->die_tag == tag)
5749 remove_child_with_prev (c, prev);
5750 c->die_parent = NULL;
5751 /* Might have removed every child. */
5752 if (die->die_child == NULL)
5753 return;
5754 c = prev->die_sib;
5756 } while (c != die->die_child);
5759 /* Add a CHILD_DIE as the last child of DIE. */
5761 static void
5762 add_child_die (dw_die_ref die, dw_die_ref child_die)
5764 /* FIXME this should probably be an assert. */
5765 if (! die || ! child_die)
5766 return;
5767 gcc_assert (die != child_die);
5769 child_die->die_parent = die;
5770 if (die->die_child)
5772 child_die->die_sib = die->die_child->die_sib;
5773 die->die_child->die_sib = child_die;
5775 else
5776 child_die->die_sib = child_die;
5777 die->die_child = child_die;
5780 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5782 static void
5783 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5784 dw_die_ref after_die)
5786 gcc_assert (die
5787 && child_die
5788 && after_die
5789 && die->die_child
5790 && die != child_die);
5792 child_die->die_parent = die;
5793 child_die->die_sib = after_die->die_sib;
5794 after_die->die_sib = child_die;
5795 if (die->die_child == after_die)
5796 die->die_child = child_die;
5799 /* Unassociate CHILD from its parent, and make its parent be
5800 NEW_PARENT. */
5802 static void
5803 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5805 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5806 if (p->die_sib == child)
5808 remove_child_with_prev (child, p);
5809 break;
5811 add_child_die (new_parent, child);
5814 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5815 is the specification, to the end of PARENT's list of children.
5816 This is done by removing and re-adding it. */
5818 static void
5819 splice_child_die (dw_die_ref parent, dw_die_ref child)
5821 /* We want the declaration DIE from inside the class, not the
5822 specification DIE at toplevel. */
5823 if (child->die_parent != parent)
5825 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5827 if (tmp)
5828 child = tmp;
5831 gcc_assert (child->die_parent == parent
5832 || (child->die_parent
5833 == get_AT_ref (parent, DW_AT_specification)));
5835 reparent_child (child, parent);
5838 /* Create and return a new die with TAG_VALUE as tag. */
5840 dw_die_ref
5841 new_die_raw (enum dwarf_tag tag_value)
5843 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5844 die->die_tag = tag_value;
5845 return die;
5848 /* Create and return a new die with a parent of PARENT_DIE. If
5849 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5850 associated tree T must be supplied to determine parenthood
5851 later. */
5853 static inline dw_die_ref
5854 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5856 dw_die_ref die = new_die_raw (tag_value);
5858 if (parent_die != NULL)
5859 add_child_die (parent_die, die);
5860 else
5862 limbo_die_node *limbo_node;
5864 /* No DIEs created after early dwarf should end up in limbo,
5865 because the limbo list should not persist past LTO
5866 streaming. */
5867 if (tag_value != DW_TAG_compile_unit
5868 /* These are allowed because they're generated while
5869 breaking out COMDAT units late. */
5870 && tag_value != DW_TAG_type_unit
5871 && tag_value != DW_TAG_skeleton_unit
5872 && !early_dwarf
5873 /* Allow nested functions to live in limbo because they will
5874 only temporarily live there, as decls_for_scope will fix
5875 them up. */
5876 && (TREE_CODE (t) != FUNCTION_DECL
5877 || !decl_function_context (t))
5878 /* Same as nested functions above but for types. Types that
5879 are local to a function will be fixed in
5880 decls_for_scope. */
5881 && (!RECORD_OR_UNION_TYPE_P (t)
5882 || !TYPE_CONTEXT (t)
5883 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5884 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5885 especially in the ltrans stage, but once we implement LTO
5886 dwarf streaming, we should remove this exception. */
5887 && !in_lto_p)
5889 fprintf (stderr, "symbol ended up in limbo too late:");
5890 debug_generic_stmt (t);
5891 gcc_unreachable ();
5894 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5895 limbo_node->die = die;
5896 limbo_node->created_for = t;
5897 limbo_node->next = limbo_die_list;
5898 limbo_die_list = limbo_node;
5901 return die;
5904 /* Return the DIE associated with the given type specifier. */
5906 dw_die_ref
5907 lookup_type_die (tree type)
5909 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5910 if (die && die->removed)
5912 TYPE_SYMTAB_DIE (type) = NULL;
5913 TREE_ASM_WRITTEN (type) = 0;
5914 return NULL;
5916 return die;
5919 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5920 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5921 anonymous type instead the one of the naming typedef. */
5923 static inline dw_die_ref
5924 strip_naming_typedef (tree type, dw_die_ref type_die)
5926 if (type
5927 && TREE_CODE (type) == RECORD_TYPE
5928 && type_die
5929 && type_die->die_tag == DW_TAG_typedef
5930 && is_naming_typedef_decl (TYPE_NAME (type)))
5931 type_die = get_AT_ref (type_die, DW_AT_type);
5932 return type_die;
5935 /* Like lookup_type_die, but if type is an anonymous type named by a
5936 typedef[1], return the DIE of the anonymous type instead the one of
5937 the naming typedef. This is because in gen_typedef_die, we did
5938 equate the anonymous struct named by the typedef with the DIE of
5939 the naming typedef. So by default, lookup_type_die on an anonymous
5940 struct yields the DIE of the naming typedef.
5942 [1]: Read the comment of is_naming_typedef_decl to learn about what
5943 a naming typedef is. */
5945 static inline dw_die_ref
5946 lookup_type_die_strip_naming_typedef (tree type)
5948 dw_die_ref die = lookup_type_die (type);
5949 return strip_naming_typedef (type, die);
5952 /* Equate a DIE to a given type specifier. */
5954 static inline void
5955 equate_type_number_to_die (tree type, dw_die_ref type_die)
5957 TYPE_SYMTAB_DIE (type) = type_die;
5960 static dw_die_ref maybe_create_die_with_external_ref (tree);
5961 struct GTY(()) sym_off_pair
5963 const char * GTY((skip)) sym;
5964 unsigned HOST_WIDE_INT off;
5966 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5968 /* Returns a hash value for X (which really is a die_struct). */
5970 inline hashval_t
5971 decl_die_hasher::hash (die_node *x)
5973 return (hashval_t) x->decl_id;
5976 /* Return true if decl_id of die_struct X is the same as UID of decl *Y. */
5978 inline bool
5979 decl_die_hasher::equal (die_node *x, tree y)
5981 return (x->decl_id == DECL_UID (y));
5984 /* Return the DIE associated with a given declaration. */
5986 dw_die_ref
5987 lookup_decl_die (tree decl)
5989 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5990 NO_INSERT);
5991 if (!die)
5993 if (in_lto_p)
5994 return maybe_create_die_with_external_ref (decl);
5995 return NULL;
5997 if ((*die)->removed)
5999 decl_die_table->clear_slot (die);
6000 return NULL;
6002 return *die;
6006 /* Return the DIE associated with BLOCK. */
6008 static inline dw_die_ref
6009 lookup_block_die (tree block)
6011 dw_die_ref die = BLOCK_DIE (block);
6012 if (!die && in_lto_p)
6013 return maybe_create_die_with_external_ref (block);
6014 return die;
6017 /* Associate DIE with BLOCK. */
6019 static inline void
6020 equate_block_to_die (tree block, dw_die_ref die)
6022 BLOCK_DIE (block) = die;
6024 #undef BLOCK_DIE
6027 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
6028 style reference. Return true if we found one refering to a DIE for
6029 DECL, otherwise return false. */
6031 static bool
6032 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
6033 unsigned HOST_WIDE_INT *off)
6035 dw_die_ref die;
6037 if (in_lto_p)
6039 /* During WPA stage and incremental linking we use a hash-map
6040 to store the decl <-> label + offset map. */
6041 if (!external_die_map)
6042 return false;
6043 sym_off_pair *desc = external_die_map->get (decl);
6044 if (!desc)
6045 return false;
6046 *sym = desc->sym;
6047 *off = desc->off;
6048 return true;
6051 if (TREE_CODE (decl) == BLOCK)
6052 die = lookup_block_die (decl);
6053 else
6054 die = lookup_decl_die (decl);
6055 if (!die)
6056 return false;
6058 /* Similar to get_ref_die_offset_label, but using the "correct"
6059 label. */
6060 *off = die->die_offset;
6061 while (die->die_parent)
6062 die = die->die_parent;
6063 /* For the containing CU DIE we compute a die_symbol in
6064 compute_comp_unit_symbol. */
6065 if (die->die_tag == DW_TAG_compile_unit)
6067 gcc_assert (die->die_id.die_symbol != NULL);
6068 *sym = die->die_id.die_symbol;
6069 return true;
6071 /* While we can gracefully handle running into say a type unit
6072 we don't really want and consider this a bug. */
6073 if (flag_checking)
6074 gcc_unreachable ();
6075 return false;
6078 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6080 static void
6081 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6082 const char *symbol, HOST_WIDE_INT offset)
6084 /* Create a fake DIE that contains the reference. Don't use
6085 new_die because we don't want to end up in the limbo list. */
6086 /* ??? We probably want to share these, thus put a ref to the DIE
6087 we create here to the external_die_map entry. */
6088 dw_die_ref ref = new_die_raw (die->die_tag);
6089 ref->die_id.die_symbol = symbol;
6090 ref->die_offset = offset;
6091 ref->with_offset = 1;
6092 add_AT_die_ref (die, attr_kind, ref);
6095 /* Create a DIE for DECL if required and add a reference to a DIE
6096 at SYMBOL + OFFSET which contains attributes dumped early. */
6098 static void
6099 dwarf2out_register_external_die (tree decl, const char *sym,
6100 unsigned HOST_WIDE_INT off)
6102 if (debug_info_level == DINFO_LEVEL_NONE)
6103 return;
6105 if (!external_die_map)
6106 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
6107 gcc_checking_assert (!external_die_map->get (decl));
6108 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
6109 external_die_map->put (decl, p);
6112 /* If we have a registered external DIE for DECL return a new DIE for
6113 the concrete instance with an appropriate abstract origin. */
6115 static dw_die_ref
6116 maybe_create_die_with_external_ref (tree decl)
6118 if (!external_die_map)
6119 return NULL;
6120 sym_off_pair *desc = external_die_map->get (decl);
6121 if (!desc)
6122 return NULL;
6124 const char *sym = desc->sym;
6125 unsigned HOST_WIDE_INT off = desc->off;
6126 external_die_map->remove (decl);
6128 in_lto_p = false;
6129 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6130 ? lookup_block_die (decl) : lookup_decl_die (decl));
6131 gcc_assert (!die);
6132 in_lto_p = true;
6134 tree ctx;
6135 dw_die_ref parent = NULL;
6136 /* Need to lookup a DIE for the decls context - the containing
6137 function or translation unit. */
6138 if (TREE_CODE (decl) == BLOCK)
6140 ctx = BLOCK_SUPERCONTEXT (decl);
6141 /* ??? We do not output DIEs for all scopes thus skip as
6142 many DIEs as needed. */
6143 while (TREE_CODE (ctx) == BLOCK
6144 && !lookup_block_die (ctx))
6145 ctx = BLOCK_SUPERCONTEXT (ctx);
6147 else
6148 ctx = DECL_CONTEXT (decl);
6149 /* Peel types in the context stack. */
6150 while (ctx && TYPE_P (ctx))
6151 ctx = TYPE_CONTEXT (ctx);
6152 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6153 if (debug_info_level <= DINFO_LEVEL_TERSE)
6154 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6155 ctx = DECL_CONTEXT (ctx);
6156 if (ctx)
6158 if (TREE_CODE (ctx) == BLOCK)
6159 parent = lookup_block_die (ctx);
6160 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6161 /* Keep the 1:1 association during WPA. */
6162 && !flag_wpa
6163 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6164 /* Otherwise all late annotations go to the main CU which
6165 imports the original CUs. */
6166 parent = comp_unit_die ();
6167 else if (TREE_CODE (ctx) == FUNCTION_DECL
6168 && TREE_CODE (decl) != FUNCTION_DECL
6169 && TREE_CODE (decl) != PARM_DECL
6170 && TREE_CODE (decl) != RESULT_DECL
6171 && TREE_CODE (decl) != BLOCK)
6172 /* Leave function local entities parent determination to when
6173 we process scope vars. */
6175 else
6176 parent = lookup_decl_die (ctx);
6178 else
6179 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6180 Handle this case gracefully by globalizing stuff. */
6181 parent = comp_unit_die ();
6182 /* Create a DIE "stub". */
6183 switch (TREE_CODE (decl))
6185 case TRANSLATION_UNIT_DECL:
6187 die = comp_unit_die ();
6188 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6189 to create a DIE for the original CUs. */
6190 return die;
6192 case NAMESPACE_DECL:
6193 if (is_fortran (decl))
6194 die = new_die (DW_TAG_module, parent, decl);
6195 else
6196 die = new_die (DW_TAG_namespace, parent, decl);
6197 break;
6198 case FUNCTION_DECL:
6199 die = new_die (DW_TAG_subprogram, parent, decl);
6200 break;
6201 case VAR_DECL:
6202 die = new_die (DW_TAG_variable, parent, decl);
6203 break;
6204 case RESULT_DECL:
6205 die = new_die (DW_TAG_variable, parent, decl);
6206 break;
6207 case PARM_DECL:
6208 die = new_die (DW_TAG_formal_parameter, parent, decl);
6209 break;
6210 case CONST_DECL:
6211 die = new_die (DW_TAG_constant, parent, decl);
6212 break;
6213 case LABEL_DECL:
6214 die = new_die (DW_TAG_label, parent, decl);
6215 break;
6216 case BLOCK:
6217 die = new_die (DW_TAG_lexical_block, parent, decl);
6218 break;
6219 default:
6220 gcc_unreachable ();
6222 if (TREE_CODE (decl) == BLOCK)
6223 equate_block_to_die (decl, die);
6224 else
6225 equate_decl_number_to_die (decl, die);
6227 add_desc_attribute (die, decl);
6229 /* Add a reference to the DIE providing early debug at $sym + off. */
6230 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6232 return die;
6235 /* Returns a hash value for X (which really is a var_loc_list). */
6237 inline hashval_t
6238 decl_loc_hasher::hash (var_loc_list *x)
6240 return (hashval_t) x->decl_id;
6243 /* Return true if decl_id of var_loc_list X is the same as
6244 UID of decl *Y. */
6246 inline bool
6247 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6249 return (x->decl_id == DECL_UID (y));
6252 /* Return the var_loc list associated with a given declaration. */
6254 static inline var_loc_list *
6255 lookup_decl_loc (const_tree decl)
6257 if (!decl_loc_table)
6258 return NULL;
6259 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6262 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6264 inline hashval_t
6265 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6267 return (hashval_t) x->decl_id;
6270 /* Return true if decl_id of cached_dw_loc_list X is the same as
6271 UID of decl *Y. */
6273 inline bool
6274 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6276 return (x->decl_id == DECL_UID (y));
6279 /* Equate a DIE to a particular declaration. */
6281 static void
6282 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6284 unsigned int decl_id = DECL_UID (decl);
6286 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6287 decl_die->decl_id = decl_id;
6290 /* Return how many bits covers PIECE EXPR_LIST. */
6292 static HOST_WIDE_INT
6293 decl_piece_bitsize (rtx piece)
6295 int ret = (int) GET_MODE (piece);
6296 if (ret)
6297 return ret;
6298 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6299 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6300 return INTVAL (XEXP (XEXP (piece, 0), 0));
6303 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6305 static rtx *
6306 decl_piece_varloc_ptr (rtx piece)
6308 if ((int) GET_MODE (piece))
6309 return &XEXP (piece, 0);
6310 else
6311 return &XEXP (XEXP (piece, 0), 1);
6314 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6315 Next is the chain of following piece nodes. */
6317 static rtx_expr_list *
6318 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6320 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6321 return alloc_EXPR_LIST (bitsize, loc_note, next);
6322 else
6323 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6324 GEN_INT (bitsize),
6325 loc_note), next);
6328 /* Return rtx that should be stored into loc field for
6329 LOC_NOTE and BITPOS/BITSIZE. */
6331 static rtx
6332 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6333 HOST_WIDE_INT bitsize)
6335 if (bitsize != -1)
6337 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6338 if (bitpos != 0)
6339 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6341 return loc_note;
6344 /* This function either modifies location piece list *DEST in
6345 place (if SRC and INNER is NULL), or copies location piece list
6346 *SRC to *DEST while modifying it. Location BITPOS is modified
6347 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6348 not copied and if needed some padding around it is added.
6349 When modifying in place, DEST should point to EXPR_LIST where
6350 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6351 to the start of the whole list and INNER points to the EXPR_LIST
6352 where earlier pieces cover PIECE_BITPOS bits. */
6354 static void
6355 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6356 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6357 HOST_WIDE_INT bitsize, rtx loc_note)
6359 HOST_WIDE_INT diff;
6360 bool copy = inner != NULL;
6362 if (copy)
6364 /* First copy all nodes preceding the current bitpos. */
6365 while (src != inner)
6367 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6368 decl_piece_bitsize (*src), NULL_RTX);
6369 dest = &XEXP (*dest, 1);
6370 src = &XEXP (*src, 1);
6373 /* Add padding if needed. */
6374 if (bitpos != piece_bitpos)
6376 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6377 copy ? NULL_RTX : *dest);
6378 dest = &XEXP (*dest, 1);
6380 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6382 gcc_assert (!copy);
6383 /* A piece with correct bitpos and bitsize already exist,
6384 just update the location for it and return. */
6385 *decl_piece_varloc_ptr (*dest) = loc_note;
6386 return;
6388 /* Add the piece that changed. */
6389 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6390 dest = &XEXP (*dest, 1);
6391 /* Skip over pieces that overlap it. */
6392 diff = bitpos - piece_bitpos + bitsize;
6393 if (!copy)
6394 src = dest;
6395 while (diff > 0 && *src)
6397 rtx piece = *src;
6398 diff -= decl_piece_bitsize (piece);
6399 if (copy)
6400 src = &XEXP (piece, 1);
6401 else
6403 *src = XEXP (piece, 1);
6404 free_EXPR_LIST_node (piece);
6407 /* Add padding if needed. */
6408 if (diff < 0 && *src)
6410 if (!copy)
6411 dest = src;
6412 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6413 dest = &XEXP (*dest, 1);
6415 if (!copy)
6416 return;
6417 /* Finally copy all nodes following it. */
6418 while (*src)
6420 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6421 decl_piece_bitsize (*src), NULL_RTX);
6422 dest = &XEXP (*dest, 1);
6423 src = &XEXP (*src, 1);
6427 /* Add a variable location node to the linked list for DECL. */
6429 static struct var_loc_node *
6430 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6432 unsigned int decl_id;
6433 var_loc_list *temp;
6434 struct var_loc_node *loc = NULL;
6435 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6437 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6439 tree realdecl = DECL_DEBUG_EXPR (decl);
6440 if (handled_component_p (realdecl)
6441 || (TREE_CODE (realdecl) == MEM_REF
6442 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6444 bool reverse;
6445 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6446 &bitsize, &reverse);
6447 if (!innerdecl
6448 || !DECL_P (innerdecl)
6449 || DECL_IGNORED_P (innerdecl)
6450 || TREE_STATIC (innerdecl)
6451 || bitsize == 0
6452 || bitpos + bitsize > 256)
6453 return NULL;
6454 decl = innerdecl;
6458 decl_id = DECL_UID (decl);
6459 var_loc_list **slot
6460 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6461 if (*slot == NULL)
6463 temp = ggc_cleared_alloc<var_loc_list> ();
6464 temp->decl_id = decl_id;
6465 *slot = temp;
6467 else
6468 temp = *slot;
6470 /* For PARM_DECLs try to keep around the original incoming value,
6471 even if that means we'll emit a zero-range .debug_loc entry. */
6472 if (temp->last
6473 && temp->first == temp->last
6474 && TREE_CODE (decl) == PARM_DECL
6475 && NOTE_P (temp->first->loc)
6476 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6477 && DECL_INCOMING_RTL (decl)
6478 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6479 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6480 == GET_CODE (DECL_INCOMING_RTL (decl))
6481 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6482 && (bitsize != -1
6483 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6484 NOTE_VAR_LOCATION_LOC (loc_note))
6485 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6486 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6488 loc = ggc_cleared_alloc<var_loc_node> ();
6489 temp->first->next = loc;
6490 temp->last = loc;
6491 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6493 else if (temp->last)
6495 struct var_loc_node *last = temp->last, *unused = NULL;
6496 rtx *piece_loc = NULL, last_loc_note;
6497 HOST_WIDE_INT piece_bitpos = 0;
6498 if (last->next)
6500 last = last->next;
6501 gcc_assert (last->next == NULL);
6503 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6505 piece_loc = &last->loc;
6508 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6509 if (piece_bitpos + cur_bitsize > bitpos)
6510 break;
6511 piece_bitpos += cur_bitsize;
6512 piece_loc = &XEXP (*piece_loc, 1);
6514 while (*piece_loc);
6516 /* TEMP->LAST here is either pointer to the last but one or
6517 last element in the chained list, LAST is pointer to the
6518 last element. */
6519 if (label && strcmp (last->label, label) == 0 && last->view == view)
6521 /* For SRA optimized variables if there weren't any real
6522 insns since last note, just modify the last node. */
6523 if (piece_loc != NULL)
6525 adjust_piece_list (piece_loc, NULL, NULL,
6526 bitpos, piece_bitpos, bitsize, loc_note);
6527 return NULL;
6529 /* If the last note doesn't cover any instructions, remove it. */
6530 if (temp->last != last)
6532 temp->last->next = NULL;
6533 unused = last;
6534 last = temp->last;
6535 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6537 else
6539 gcc_assert (temp->first == temp->last
6540 || (temp->first->next == temp->last
6541 && TREE_CODE (decl) == PARM_DECL));
6542 memset (temp->last, '\0', sizeof (*temp->last));
6543 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6544 return temp->last;
6547 if (bitsize == -1 && NOTE_P (last->loc))
6548 last_loc_note = last->loc;
6549 else if (piece_loc != NULL
6550 && *piece_loc != NULL_RTX
6551 && piece_bitpos == bitpos
6552 && decl_piece_bitsize (*piece_loc) == bitsize)
6553 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6554 else
6555 last_loc_note = NULL_RTX;
6556 /* If the current location is the same as the end of the list,
6557 and either both or neither of the locations is uninitialized,
6558 we have nothing to do. */
6559 if (last_loc_note == NULL_RTX
6560 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6561 NOTE_VAR_LOCATION_LOC (loc_note)))
6562 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6563 != NOTE_VAR_LOCATION_STATUS (loc_note))
6564 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6565 == VAR_INIT_STATUS_UNINITIALIZED)
6566 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6567 == VAR_INIT_STATUS_UNINITIALIZED))))
6569 /* Add LOC to the end of list and update LAST. If the last
6570 element of the list has been removed above, reuse its
6571 memory for the new node, otherwise allocate a new one. */
6572 if (unused)
6574 loc = unused;
6575 memset (loc, '\0', sizeof (*loc));
6577 else
6578 loc = ggc_cleared_alloc<var_loc_node> ();
6579 if (bitsize == -1 || piece_loc == NULL)
6580 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6581 else
6582 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6583 bitpos, piece_bitpos, bitsize, loc_note);
6584 last->next = loc;
6585 /* Ensure TEMP->LAST will point either to the new last but one
6586 element of the chain, or to the last element in it. */
6587 if (last != temp->last)
6588 temp->last = last;
6590 else if (unused)
6591 ggc_free (unused);
6593 else
6595 loc = ggc_cleared_alloc<var_loc_node> ();
6596 temp->first = loc;
6597 temp->last = loc;
6598 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6600 return loc;
6603 /* Keep track of the number of spaces used to indent the
6604 output of the debugging routines that print the structure of
6605 the DIE internal representation. */
6606 static int print_indent;
6608 /* Indent the line the number of spaces given by print_indent. */
6610 static inline void
6611 print_spaces (FILE *outfile)
6613 fprintf (outfile, "%*s", print_indent, "");
6616 /* Print a type signature in hex. */
6618 static inline void
6619 print_signature (FILE *outfile, char *sig)
6621 int i;
6623 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6624 fprintf (outfile, "%02x", sig[i] & 0xff);
6627 static inline void
6628 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6630 if (discr_value->pos)
6631 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6632 else
6633 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6636 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6638 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6639 RECURSE, output location descriptor operations. */
6641 static void
6642 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6644 switch (val->val_class)
6646 case dw_val_class_addr:
6647 fprintf (outfile, "address");
6648 break;
6649 case dw_val_class_offset:
6650 fprintf (outfile, "offset");
6651 break;
6652 case dw_val_class_loc:
6653 fprintf (outfile, "location descriptor");
6654 if (val->v.val_loc == NULL)
6655 fprintf (outfile, " -> <null>");
6656 else if (recurse)
6658 fprintf (outfile, ":\n");
6659 print_indent += 4;
6660 print_loc_descr (val->v.val_loc, outfile);
6661 print_indent -= 4;
6663 else
6665 if (flag_dump_noaddr || flag_dump_unnumbered)
6666 fprintf (outfile, " #");
6667 else
6668 fprintf (outfile, " (%p)", (void *) val->v.val_loc);
6670 break;
6671 case dw_val_class_loc_list:
6672 fprintf (outfile, "location list -> label:%s",
6673 val->v.val_loc_list->ll_symbol);
6674 break;
6675 case dw_val_class_view_list:
6676 val = view_list_to_loc_list_val_node (val);
6677 fprintf (outfile, "location list with views -> labels:%s and %s",
6678 val->v.val_loc_list->ll_symbol,
6679 val->v.val_loc_list->vl_symbol);
6680 break;
6681 case dw_val_class_range_list:
6682 fprintf (outfile, "range list");
6683 break;
6684 case dw_val_class_const:
6685 case dw_val_class_const_implicit:
6686 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6687 break;
6688 case dw_val_class_unsigned_const:
6689 case dw_val_class_unsigned_const_implicit:
6690 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6691 break;
6692 case dw_val_class_const_double:
6693 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6694 HOST_WIDE_INT_PRINT_UNSIGNED")",
6695 val->v.val_double.high,
6696 val->v.val_double.low);
6697 break;
6698 case dw_val_class_wide_int:
6700 int i = val->v.val_wide->get_len ();
6701 fprintf (outfile, "constant (");
6702 gcc_assert (i > 0);
6703 if (val->v.val_wide->elt (i - 1) == 0)
6704 fprintf (outfile, "0x");
6705 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6706 val->v.val_wide->elt (--i));
6707 while (--i >= 0)
6708 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6709 val->v.val_wide->elt (i));
6710 fprintf (outfile, ")");
6711 break;
6713 case dw_val_class_vec:
6714 fprintf (outfile, "floating-point or vector constant");
6715 break;
6716 case dw_val_class_flag:
6717 fprintf (outfile, "%u", val->v.val_flag);
6718 break;
6719 case dw_val_class_die_ref:
6720 if (val->v.val_die_ref.die != NULL)
6722 dw_die_ref die = val->v.val_die_ref.die;
6724 if (die->comdat_type_p)
6726 fprintf (outfile, "die -> signature: ");
6727 print_signature (outfile,
6728 die->die_id.die_type_node->signature);
6730 else if (die->die_id.die_symbol)
6732 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6733 if (die->with_offset)
6734 fprintf (outfile, " + %ld", die->die_offset);
6736 else
6737 fprintf (outfile, "die -> %ld", die->die_offset);
6738 if (flag_dump_noaddr || flag_dump_unnumbered)
6739 fprintf (outfile, " #");
6740 else
6741 fprintf (outfile, " (%p)", (void *) die);
6743 else
6744 fprintf (outfile, "die -> <null>");
6745 break;
6746 case dw_val_class_vms_delta:
6747 fprintf (outfile, "delta: @slotcount(%s-%s)",
6748 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6749 break;
6750 case dw_val_class_symview:
6751 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6752 break;
6753 case dw_val_class_lbl_id:
6754 case dw_val_class_lineptr:
6755 case dw_val_class_macptr:
6756 case dw_val_class_loclistsptr:
6757 case dw_val_class_high_pc:
6758 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6759 break;
6760 case dw_val_class_str:
6761 if (val->v.val_str->str != NULL)
6762 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6763 else
6764 fprintf (outfile, "<null>");
6765 break;
6766 case dw_val_class_file:
6767 case dw_val_class_file_implicit:
6768 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6769 val->v.val_file->emitted_number);
6770 break;
6771 case dw_val_class_data8:
6773 int i;
6775 for (i = 0; i < 8; i++)
6776 fprintf (outfile, "%02x", val->v.val_data8[i]);
6777 break;
6779 case dw_val_class_discr_value:
6780 print_discr_value (outfile, &val->v.val_discr_value);
6781 break;
6782 case dw_val_class_discr_list:
6783 for (dw_discr_list_ref node = val->v.val_discr_list;
6784 node != NULL;
6785 node = node->dw_discr_next)
6787 if (node->dw_discr_range)
6789 fprintf (outfile, " .. ");
6790 print_discr_value (outfile, &node->dw_discr_lower_bound);
6791 print_discr_value (outfile, &node->dw_discr_upper_bound);
6793 else
6794 print_discr_value (outfile, &node->dw_discr_lower_bound);
6796 if (node->dw_discr_next != NULL)
6797 fprintf (outfile, " | ");
6799 default:
6800 break;
6804 /* Likewise, for a DIE attribute. */
6806 static void
6807 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6809 print_dw_val (&a->dw_attr_val, recurse, outfile);
6813 /* Print the list of operands in the LOC location description to OUTFILE. This
6814 routine is a debugging aid only. */
6816 static void
6817 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6819 dw_loc_descr_ref l = loc;
6821 if (loc == NULL)
6823 print_spaces (outfile);
6824 fprintf (outfile, "<null>\n");
6825 return;
6828 for (l = loc; l != NULL; l = l->dw_loc_next)
6830 print_spaces (outfile);
6831 if (flag_dump_noaddr || flag_dump_unnumbered)
6832 fprintf (outfile, "#");
6833 else
6834 fprintf (outfile, "(%p)", (void *) l);
6835 fprintf (outfile, " %s",
6836 dwarf_stack_op_name (l->dw_loc_opc));
6837 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6839 fprintf (outfile, " ");
6840 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6842 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6844 fprintf (outfile, ", ");
6845 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6847 fprintf (outfile, "\n");
6851 /* Print the information associated with a given DIE, and its children.
6852 This routine is a debugging aid only. */
6854 static void
6855 print_die (dw_die_ref die, FILE *outfile)
6857 dw_attr_node *a;
6858 dw_die_ref c;
6859 unsigned ix;
6861 print_spaces (outfile);
6862 fprintf (outfile, "DIE %4ld: %s ",
6863 die->die_offset, dwarf_tag_name (die->die_tag));
6864 if (flag_dump_noaddr || flag_dump_unnumbered)
6865 fprintf (outfile, "#\n");
6866 else
6867 fprintf (outfile, "(%p)\n", (void*) die);
6868 print_spaces (outfile);
6869 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6870 fprintf (outfile, " offset: %ld", die->die_offset);
6871 fprintf (outfile, " mark: %d\n", die->die_mark);
6873 if (die->comdat_type_p)
6875 print_spaces (outfile);
6876 fprintf (outfile, " signature: ");
6877 print_signature (outfile, die->die_id.die_type_node->signature);
6878 fprintf (outfile, "\n");
6881 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6883 print_spaces (outfile);
6884 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6886 print_attribute (a, true, outfile);
6887 fprintf (outfile, "\n");
6890 if (die->die_child != NULL)
6892 print_indent += 4;
6893 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6894 print_indent -= 4;
6896 if (print_indent == 0)
6897 fprintf (outfile, "\n");
6900 /* Print the list of operations in the LOC location description. */
6902 DEBUG_FUNCTION void
6903 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6905 print_loc_descr (loc, stderr);
6908 /* Print the information collected for a given DIE. */
6910 DEBUG_FUNCTION void
6911 debug_dwarf_die (dw_die_ref die)
6913 print_die (die, stderr);
6916 DEBUG_FUNCTION void
6917 debug (die_struct &ref)
6919 print_die (&ref, stderr);
6922 DEBUG_FUNCTION void
6923 debug (die_struct *ptr)
6925 if (ptr)
6926 debug (*ptr);
6927 else
6928 fprintf (stderr, "<nil>\n");
6932 /* Print all DWARF information collected for the compilation unit.
6933 This routine is a debugging aid only. */
6935 DEBUG_FUNCTION void
6936 debug_dwarf (void)
6938 print_indent = 0;
6939 print_die (comp_unit_die (), stderr);
6942 /* Verify the DIE tree structure. */
6944 DEBUG_FUNCTION void
6945 verify_die (dw_die_ref die)
6947 gcc_assert (!die->die_mark);
6948 if (die->die_parent == NULL
6949 && die->die_sib == NULL)
6950 return;
6951 /* Verify the die_sib list is cyclic. */
6952 dw_die_ref x = die;
6955 x->die_mark = 1;
6956 x = x->die_sib;
6958 while (x && !x->die_mark);
6959 gcc_assert (x == die);
6960 x = die;
6963 /* Verify all dies have the same parent. */
6964 gcc_assert (x->die_parent == die->die_parent);
6965 if (x->die_child)
6967 /* Verify the child has the proper parent and recurse. */
6968 gcc_assert (x->die_child->die_parent == x);
6969 verify_die (x->die_child);
6971 x->die_mark = 0;
6972 x = x->die_sib;
6974 while (x && x->die_mark);
6977 /* Sanity checks on DIEs. */
6979 static void
6980 check_die (dw_die_ref die)
6982 unsigned ix;
6983 dw_attr_node *a;
6984 bool inline_found = false;
6985 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6986 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6987 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6989 switch (a->dw_attr)
6991 case DW_AT_inline:
6992 if (a->dw_attr_val.v.val_unsigned)
6993 inline_found = true;
6994 break;
6995 case DW_AT_location:
6996 ++n_location;
6997 break;
6998 case DW_AT_low_pc:
6999 ++n_low_pc;
7000 break;
7001 case DW_AT_high_pc:
7002 ++n_high_pc;
7003 break;
7004 case DW_AT_artificial:
7005 ++n_artificial;
7006 break;
7007 case DW_AT_decl_column:
7008 ++n_decl_column;
7009 break;
7010 case DW_AT_decl_line:
7011 ++n_decl_line;
7012 break;
7013 case DW_AT_decl_file:
7014 ++n_decl_file;
7015 break;
7016 default:
7017 break;
7020 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
7021 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
7023 fprintf (stderr, "Duplicate attributes in DIE:\n");
7024 debug_dwarf_die (die);
7025 gcc_unreachable ();
7027 if (inline_found)
7029 /* A debugging information entry that is a member of an abstract
7030 instance tree [that has DW_AT_inline] should not contain any
7031 attributes which describe aspects of the subroutine which vary
7032 between distinct inlined expansions or distinct out-of-line
7033 expansions. */
7034 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7035 gcc_assert (a->dw_attr != DW_AT_low_pc
7036 && a->dw_attr != DW_AT_high_pc
7037 && a->dw_attr != DW_AT_location
7038 && a->dw_attr != DW_AT_frame_base
7039 && a->dw_attr != DW_AT_call_all_calls
7040 && a->dw_attr != DW_AT_GNU_all_call_sites);
7044 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7045 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7046 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7048 /* Calculate the checksum of a location expression. */
7050 static inline void
7051 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7053 int tem;
7054 inchash::hash hstate;
7055 hashval_t hash;
7057 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7058 CHECKSUM (tem);
7059 hash_loc_operands (loc, hstate);
7060 hash = hstate.end();
7061 CHECKSUM (hash);
7064 /* Calculate the checksum of an attribute. */
7066 static void
7067 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
7069 dw_loc_descr_ref loc;
7070 rtx r;
7072 CHECKSUM (at->dw_attr);
7074 /* We don't care that this was compiled with a different compiler
7075 snapshot; if the output is the same, that's what matters. */
7076 if (at->dw_attr == DW_AT_producer)
7077 return;
7079 switch (AT_class (at))
7081 case dw_val_class_const:
7082 case dw_val_class_const_implicit:
7083 CHECKSUM (at->dw_attr_val.v.val_int);
7084 break;
7085 case dw_val_class_unsigned_const:
7086 case dw_val_class_unsigned_const_implicit:
7087 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7088 break;
7089 case dw_val_class_const_double:
7090 CHECKSUM (at->dw_attr_val.v.val_double);
7091 break;
7092 case dw_val_class_wide_int:
7093 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7094 get_full_len (*at->dw_attr_val.v.val_wide)
7095 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7096 break;
7097 case dw_val_class_vec:
7098 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7099 (at->dw_attr_val.v.val_vec.length
7100 * at->dw_attr_val.v.val_vec.elt_size));
7101 break;
7102 case dw_val_class_flag:
7103 CHECKSUM (at->dw_attr_val.v.val_flag);
7104 break;
7105 case dw_val_class_str:
7106 CHECKSUM_STRING (AT_string (at));
7107 break;
7109 case dw_val_class_addr:
7110 r = AT_addr (at);
7111 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7112 CHECKSUM_STRING (XSTR (r, 0));
7113 break;
7115 case dw_val_class_offset:
7116 CHECKSUM (at->dw_attr_val.v.val_offset);
7117 break;
7119 case dw_val_class_loc:
7120 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7121 loc_checksum (loc, ctx);
7122 break;
7124 case dw_val_class_die_ref:
7125 die_checksum (AT_ref (at), ctx, mark);
7126 break;
7128 case dw_val_class_fde_ref:
7129 case dw_val_class_vms_delta:
7130 case dw_val_class_symview:
7131 case dw_val_class_lbl_id:
7132 case dw_val_class_lineptr:
7133 case dw_val_class_macptr:
7134 case dw_val_class_loclistsptr:
7135 case dw_val_class_high_pc:
7136 break;
7138 case dw_val_class_file:
7139 case dw_val_class_file_implicit:
7140 CHECKSUM_STRING (AT_file (at)->filename);
7141 break;
7143 case dw_val_class_data8:
7144 CHECKSUM (at->dw_attr_val.v.val_data8);
7145 break;
7147 default:
7148 break;
7152 /* Calculate the checksum of a DIE. */
7154 static void
7155 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7157 dw_die_ref c;
7158 dw_attr_node *a;
7159 unsigned ix;
7161 /* To avoid infinite recursion. */
7162 if (die->die_mark)
7164 CHECKSUM (die->die_mark);
7165 return;
7167 die->die_mark = ++(*mark);
7169 CHECKSUM (die->die_tag);
7171 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7172 attr_checksum (a, ctx, mark);
7174 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7177 #undef CHECKSUM
7178 #undef CHECKSUM_BLOCK
7179 #undef CHECKSUM_STRING
7181 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7182 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7183 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7184 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7185 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7186 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7187 #define CHECKSUM_ATTR(FOO) \
7188 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7190 /* Calculate the checksum of a number in signed LEB128 format. */
7192 static void
7193 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7195 unsigned char byte;
7196 bool more;
7198 while (1)
7200 byte = (value & 0x7f);
7201 value >>= 7;
7202 more = !((value == 0 && (byte & 0x40) == 0)
7203 || (value == -1 && (byte & 0x40) != 0));
7204 if (more)
7205 byte |= 0x80;
7206 CHECKSUM (byte);
7207 if (!more)
7208 break;
7212 /* Calculate the checksum of a number in unsigned LEB128 format. */
7214 static void
7215 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7217 while (1)
7219 unsigned char byte = (value & 0x7f);
7220 value >>= 7;
7221 if (value != 0)
7222 /* More bytes to follow. */
7223 byte |= 0x80;
7224 CHECKSUM (byte);
7225 if (value == 0)
7226 break;
7230 /* Checksum the context of the DIE. This adds the names of any
7231 surrounding namespaces or structures to the checksum. */
7233 static void
7234 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7236 const char *name;
7237 dw_die_ref spec;
7238 int tag = die->die_tag;
7240 if (tag != DW_TAG_namespace
7241 && tag != DW_TAG_structure_type
7242 && tag != DW_TAG_class_type)
7243 return;
7245 name = get_AT_string (die, DW_AT_name);
7247 spec = get_AT_ref (die, DW_AT_specification);
7248 if (spec != NULL)
7249 die = spec;
7251 if (die->die_parent != NULL)
7252 checksum_die_context (die->die_parent, ctx);
7254 CHECKSUM_ULEB128 ('C');
7255 CHECKSUM_ULEB128 (tag);
7256 if (name != NULL)
7257 CHECKSUM_STRING (name);
7260 /* Calculate the checksum of a location expression. */
7262 static inline void
7263 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7265 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7266 were emitted as a DW_FORM_sdata instead of a location expression. */
7267 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7269 CHECKSUM_ULEB128 (DW_FORM_sdata);
7270 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7271 return;
7274 /* Otherwise, just checksum the raw location expression. */
7275 while (loc != NULL)
7277 inchash::hash hstate;
7278 hashval_t hash;
7280 CHECKSUM_ULEB128 (loc->dtprel);
7281 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7282 hash_loc_operands (loc, hstate);
7283 hash = hstate.end ();
7284 CHECKSUM (hash);
7285 loc = loc->dw_loc_next;
7289 /* Calculate the checksum of an attribute. */
7291 static void
7292 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7293 struct md5_ctx *ctx, int *mark)
7295 dw_loc_descr_ref loc;
7296 rtx r;
7298 if (AT_class (at) == dw_val_class_die_ref)
7300 dw_die_ref target_die = AT_ref (at);
7302 /* For pointer and reference types, we checksum only the (qualified)
7303 name of the target type (if there is a name). For friend entries,
7304 we checksum only the (qualified) name of the target type or function.
7305 This allows the checksum to remain the same whether the target type
7306 is complete or not. */
7307 if ((at->dw_attr == DW_AT_type
7308 && (tag == DW_TAG_pointer_type
7309 || tag == DW_TAG_reference_type
7310 || tag == DW_TAG_rvalue_reference_type
7311 || tag == DW_TAG_ptr_to_member_type))
7312 || (at->dw_attr == DW_AT_friend
7313 && tag == DW_TAG_friend))
7315 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7317 if (name_attr != NULL)
7319 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7321 if (decl == NULL)
7322 decl = target_die;
7323 CHECKSUM_ULEB128 ('N');
7324 CHECKSUM_ULEB128 (at->dw_attr);
7325 if (decl->die_parent != NULL)
7326 checksum_die_context (decl->die_parent, ctx);
7327 CHECKSUM_ULEB128 ('E');
7328 CHECKSUM_STRING (AT_string (name_attr));
7329 return;
7333 /* For all other references to another DIE, we check to see if the
7334 target DIE has already been visited. If it has, we emit a
7335 backward reference; if not, we descend recursively. */
7336 if (target_die->die_mark > 0)
7338 CHECKSUM_ULEB128 ('R');
7339 CHECKSUM_ULEB128 (at->dw_attr);
7340 CHECKSUM_ULEB128 (target_die->die_mark);
7342 else
7344 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7346 if (decl == NULL)
7347 decl = target_die;
7348 target_die->die_mark = ++(*mark);
7349 CHECKSUM_ULEB128 ('T');
7350 CHECKSUM_ULEB128 (at->dw_attr);
7351 if (decl->die_parent != NULL)
7352 checksum_die_context (decl->die_parent, ctx);
7353 die_checksum_ordered (target_die, ctx, mark);
7355 return;
7358 CHECKSUM_ULEB128 ('A');
7359 CHECKSUM_ULEB128 (at->dw_attr);
7361 switch (AT_class (at))
7363 case dw_val_class_const:
7364 case dw_val_class_const_implicit:
7365 CHECKSUM_ULEB128 (DW_FORM_sdata);
7366 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7367 break;
7369 case dw_val_class_unsigned_const:
7370 case dw_val_class_unsigned_const_implicit:
7371 CHECKSUM_ULEB128 (DW_FORM_sdata);
7372 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7373 break;
7375 case dw_val_class_const_double:
7376 CHECKSUM_ULEB128 (DW_FORM_block);
7377 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7378 CHECKSUM (at->dw_attr_val.v.val_double);
7379 break;
7381 case dw_val_class_wide_int:
7382 CHECKSUM_ULEB128 (DW_FORM_block);
7383 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7384 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7385 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7386 get_full_len (*at->dw_attr_val.v.val_wide)
7387 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7388 break;
7390 case dw_val_class_vec:
7391 CHECKSUM_ULEB128 (DW_FORM_block);
7392 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7393 * at->dw_attr_val.v.val_vec.elt_size);
7394 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7395 (at->dw_attr_val.v.val_vec.length
7396 * at->dw_attr_val.v.val_vec.elt_size));
7397 break;
7399 case dw_val_class_flag:
7400 CHECKSUM_ULEB128 (DW_FORM_flag);
7401 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7402 break;
7404 case dw_val_class_str:
7405 CHECKSUM_ULEB128 (DW_FORM_string);
7406 CHECKSUM_STRING (AT_string (at));
7407 break;
7409 case dw_val_class_addr:
7410 r = AT_addr (at);
7411 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7412 CHECKSUM_ULEB128 (DW_FORM_string);
7413 CHECKSUM_STRING (XSTR (r, 0));
7414 break;
7416 case dw_val_class_offset:
7417 CHECKSUM_ULEB128 (DW_FORM_sdata);
7418 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7419 break;
7421 case dw_val_class_loc:
7422 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7423 loc_checksum_ordered (loc, ctx);
7424 break;
7426 case dw_val_class_fde_ref:
7427 case dw_val_class_symview:
7428 case dw_val_class_lbl_id:
7429 case dw_val_class_lineptr:
7430 case dw_val_class_macptr:
7431 case dw_val_class_loclistsptr:
7432 case dw_val_class_high_pc:
7433 break;
7435 case dw_val_class_file:
7436 case dw_val_class_file_implicit:
7437 CHECKSUM_ULEB128 (DW_FORM_string);
7438 CHECKSUM_STRING (AT_file (at)->filename);
7439 break;
7441 case dw_val_class_data8:
7442 CHECKSUM (at->dw_attr_val.v.val_data8);
7443 break;
7445 default:
7446 break;
7450 struct checksum_attributes
7452 dw_attr_node *at_name;
7453 dw_attr_node *at_type;
7454 dw_attr_node *at_friend;
7455 dw_attr_node *at_accessibility;
7456 dw_attr_node *at_address_class;
7457 dw_attr_node *at_alignment;
7458 dw_attr_node *at_allocated;
7459 dw_attr_node *at_artificial;
7460 dw_attr_node *at_associated;
7461 dw_attr_node *at_binary_scale;
7462 dw_attr_node *at_bit_offset;
7463 dw_attr_node *at_bit_size;
7464 dw_attr_node *at_bit_stride;
7465 dw_attr_node *at_byte_size;
7466 dw_attr_node *at_byte_stride;
7467 dw_attr_node *at_const_value;
7468 dw_attr_node *at_containing_type;
7469 dw_attr_node *at_count;
7470 dw_attr_node *at_data_location;
7471 dw_attr_node *at_data_member_location;
7472 dw_attr_node *at_decimal_scale;
7473 dw_attr_node *at_decimal_sign;
7474 dw_attr_node *at_default_value;
7475 dw_attr_node *at_digit_count;
7476 dw_attr_node *at_discr;
7477 dw_attr_node *at_discr_list;
7478 dw_attr_node *at_discr_value;
7479 dw_attr_node *at_encoding;
7480 dw_attr_node *at_endianity;
7481 dw_attr_node *at_explicit;
7482 dw_attr_node *at_is_optional;
7483 dw_attr_node *at_location;
7484 dw_attr_node *at_lower_bound;
7485 dw_attr_node *at_mutable;
7486 dw_attr_node *at_ordering;
7487 dw_attr_node *at_picture_string;
7488 dw_attr_node *at_prototyped;
7489 dw_attr_node *at_small;
7490 dw_attr_node *at_segment;
7491 dw_attr_node *at_string_length;
7492 dw_attr_node *at_string_length_bit_size;
7493 dw_attr_node *at_string_length_byte_size;
7494 dw_attr_node *at_threads_scaled;
7495 dw_attr_node *at_upper_bound;
7496 dw_attr_node *at_use_location;
7497 dw_attr_node *at_use_UTF8;
7498 dw_attr_node *at_variable_parameter;
7499 dw_attr_node *at_virtuality;
7500 dw_attr_node *at_visibility;
7501 dw_attr_node *at_vtable_elem_location;
7504 /* Collect the attributes that we will want to use for the checksum. */
7506 static void
7507 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7509 dw_attr_node *a;
7510 unsigned ix;
7512 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7514 switch (a->dw_attr)
7516 case DW_AT_name:
7517 attrs->at_name = a;
7518 break;
7519 case DW_AT_type:
7520 attrs->at_type = a;
7521 break;
7522 case DW_AT_friend:
7523 attrs->at_friend = a;
7524 break;
7525 case DW_AT_accessibility:
7526 attrs->at_accessibility = a;
7527 break;
7528 case DW_AT_address_class:
7529 attrs->at_address_class = a;
7530 break;
7531 case DW_AT_alignment:
7532 attrs->at_alignment = a;
7533 break;
7534 case DW_AT_allocated:
7535 attrs->at_allocated = a;
7536 break;
7537 case DW_AT_artificial:
7538 attrs->at_artificial = a;
7539 break;
7540 case DW_AT_associated:
7541 attrs->at_associated = a;
7542 break;
7543 case DW_AT_binary_scale:
7544 attrs->at_binary_scale = a;
7545 break;
7546 case DW_AT_bit_offset:
7547 attrs->at_bit_offset = a;
7548 break;
7549 case DW_AT_bit_size:
7550 attrs->at_bit_size = a;
7551 break;
7552 case DW_AT_bit_stride:
7553 attrs->at_bit_stride = a;
7554 break;
7555 case DW_AT_byte_size:
7556 attrs->at_byte_size = a;
7557 break;
7558 case DW_AT_byte_stride:
7559 attrs->at_byte_stride = a;
7560 break;
7561 case DW_AT_const_value:
7562 attrs->at_const_value = a;
7563 break;
7564 case DW_AT_containing_type:
7565 attrs->at_containing_type = a;
7566 break;
7567 case DW_AT_count:
7568 attrs->at_count = a;
7569 break;
7570 case DW_AT_data_location:
7571 attrs->at_data_location = a;
7572 break;
7573 case DW_AT_data_member_location:
7574 attrs->at_data_member_location = a;
7575 break;
7576 case DW_AT_decimal_scale:
7577 attrs->at_decimal_scale = a;
7578 break;
7579 case DW_AT_decimal_sign:
7580 attrs->at_decimal_sign = a;
7581 break;
7582 case DW_AT_default_value:
7583 attrs->at_default_value = a;
7584 break;
7585 case DW_AT_digit_count:
7586 attrs->at_digit_count = a;
7587 break;
7588 case DW_AT_discr:
7589 attrs->at_discr = a;
7590 break;
7591 case DW_AT_discr_list:
7592 attrs->at_discr_list = a;
7593 break;
7594 case DW_AT_discr_value:
7595 attrs->at_discr_value = a;
7596 break;
7597 case DW_AT_encoding:
7598 attrs->at_encoding = a;
7599 break;
7600 case DW_AT_endianity:
7601 attrs->at_endianity = a;
7602 break;
7603 case DW_AT_explicit:
7604 attrs->at_explicit = a;
7605 break;
7606 case DW_AT_is_optional:
7607 attrs->at_is_optional = a;
7608 break;
7609 case DW_AT_location:
7610 attrs->at_location = a;
7611 break;
7612 case DW_AT_lower_bound:
7613 attrs->at_lower_bound = a;
7614 break;
7615 case DW_AT_mutable:
7616 attrs->at_mutable = a;
7617 break;
7618 case DW_AT_ordering:
7619 attrs->at_ordering = a;
7620 break;
7621 case DW_AT_picture_string:
7622 attrs->at_picture_string = a;
7623 break;
7624 case DW_AT_prototyped:
7625 attrs->at_prototyped = a;
7626 break;
7627 case DW_AT_small:
7628 attrs->at_small = a;
7629 break;
7630 case DW_AT_segment:
7631 attrs->at_segment = a;
7632 break;
7633 case DW_AT_string_length:
7634 attrs->at_string_length = a;
7635 break;
7636 case DW_AT_string_length_bit_size:
7637 attrs->at_string_length_bit_size = a;
7638 break;
7639 case DW_AT_string_length_byte_size:
7640 attrs->at_string_length_byte_size = a;
7641 break;
7642 case DW_AT_threads_scaled:
7643 attrs->at_threads_scaled = a;
7644 break;
7645 case DW_AT_upper_bound:
7646 attrs->at_upper_bound = a;
7647 break;
7648 case DW_AT_use_location:
7649 attrs->at_use_location = a;
7650 break;
7651 case DW_AT_use_UTF8:
7652 attrs->at_use_UTF8 = a;
7653 break;
7654 case DW_AT_variable_parameter:
7655 attrs->at_variable_parameter = a;
7656 break;
7657 case DW_AT_virtuality:
7658 attrs->at_virtuality = a;
7659 break;
7660 case DW_AT_visibility:
7661 attrs->at_visibility = a;
7662 break;
7663 case DW_AT_vtable_elem_location:
7664 attrs->at_vtable_elem_location = a;
7665 break;
7666 default:
7667 break;
7672 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7674 static void
7675 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7677 dw_die_ref c;
7678 dw_die_ref decl;
7679 struct checksum_attributes attrs;
7681 CHECKSUM_ULEB128 ('D');
7682 CHECKSUM_ULEB128 (die->die_tag);
7684 memset (&attrs, 0, sizeof (attrs));
7686 decl = get_AT_ref (die, DW_AT_specification);
7687 if (decl != NULL)
7688 collect_checksum_attributes (&attrs, decl);
7689 collect_checksum_attributes (&attrs, die);
7691 CHECKSUM_ATTR (attrs.at_name);
7692 CHECKSUM_ATTR (attrs.at_accessibility);
7693 CHECKSUM_ATTR (attrs.at_address_class);
7694 CHECKSUM_ATTR (attrs.at_allocated);
7695 CHECKSUM_ATTR (attrs.at_artificial);
7696 CHECKSUM_ATTR (attrs.at_associated);
7697 CHECKSUM_ATTR (attrs.at_binary_scale);
7698 CHECKSUM_ATTR (attrs.at_bit_offset);
7699 CHECKSUM_ATTR (attrs.at_bit_size);
7700 CHECKSUM_ATTR (attrs.at_bit_stride);
7701 CHECKSUM_ATTR (attrs.at_byte_size);
7702 CHECKSUM_ATTR (attrs.at_byte_stride);
7703 CHECKSUM_ATTR (attrs.at_const_value);
7704 CHECKSUM_ATTR (attrs.at_containing_type);
7705 CHECKSUM_ATTR (attrs.at_count);
7706 CHECKSUM_ATTR (attrs.at_data_location);
7707 CHECKSUM_ATTR (attrs.at_data_member_location);
7708 CHECKSUM_ATTR (attrs.at_decimal_scale);
7709 CHECKSUM_ATTR (attrs.at_decimal_sign);
7710 CHECKSUM_ATTR (attrs.at_default_value);
7711 CHECKSUM_ATTR (attrs.at_digit_count);
7712 CHECKSUM_ATTR (attrs.at_discr);
7713 CHECKSUM_ATTR (attrs.at_discr_list);
7714 CHECKSUM_ATTR (attrs.at_discr_value);
7715 CHECKSUM_ATTR (attrs.at_encoding);
7716 CHECKSUM_ATTR (attrs.at_endianity);
7717 CHECKSUM_ATTR (attrs.at_explicit);
7718 CHECKSUM_ATTR (attrs.at_is_optional);
7719 CHECKSUM_ATTR (attrs.at_location);
7720 CHECKSUM_ATTR (attrs.at_lower_bound);
7721 CHECKSUM_ATTR (attrs.at_mutable);
7722 CHECKSUM_ATTR (attrs.at_ordering);
7723 CHECKSUM_ATTR (attrs.at_picture_string);
7724 CHECKSUM_ATTR (attrs.at_prototyped);
7725 CHECKSUM_ATTR (attrs.at_small);
7726 CHECKSUM_ATTR (attrs.at_segment);
7727 CHECKSUM_ATTR (attrs.at_string_length);
7728 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7729 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7730 CHECKSUM_ATTR (attrs.at_threads_scaled);
7731 CHECKSUM_ATTR (attrs.at_upper_bound);
7732 CHECKSUM_ATTR (attrs.at_use_location);
7733 CHECKSUM_ATTR (attrs.at_use_UTF8);
7734 CHECKSUM_ATTR (attrs.at_variable_parameter);
7735 CHECKSUM_ATTR (attrs.at_virtuality);
7736 CHECKSUM_ATTR (attrs.at_visibility);
7737 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7738 CHECKSUM_ATTR (attrs.at_type);
7739 CHECKSUM_ATTR (attrs.at_friend);
7740 CHECKSUM_ATTR (attrs.at_alignment);
7742 /* Checksum the child DIEs. */
7743 c = die->die_child;
7744 if (c) do {
7745 dw_attr_node *name_attr;
7747 c = c->die_sib;
7748 name_attr = get_AT (c, DW_AT_name);
7749 if (is_template_instantiation (c))
7751 /* Ignore instantiations of member type and function templates. */
7753 else if (name_attr != NULL
7754 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7756 /* Use a shallow checksum for named nested types and member
7757 functions. */
7758 CHECKSUM_ULEB128 ('S');
7759 CHECKSUM_ULEB128 (c->die_tag);
7760 CHECKSUM_STRING (AT_string (name_attr));
7762 else
7764 /* Use a deep checksum for other children. */
7765 /* Mark this DIE so it gets processed when unmarking. */
7766 if (c->die_mark == 0)
7767 c->die_mark = -1;
7768 die_checksum_ordered (c, ctx, mark);
7770 } while (c != die->die_child);
7772 CHECKSUM_ULEB128 (0);
7775 /* Add a type name and tag to a hash. */
7776 static void
7777 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7779 CHECKSUM_ULEB128 (tag);
7780 CHECKSUM_STRING (name);
7783 #undef CHECKSUM
7784 #undef CHECKSUM_STRING
7785 #undef CHECKSUM_ATTR
7786 #undef CHECKSUM_LEB128
7787 #undef CHECKSUM_ULEB128
7789 /* Generate the type signature for DIE. This is computed by generating an
7790 MD5 checksum over the DIE's tag, its relevant attributes, and its
7791 children. Attributes that are references to other DIEs are processed
7792 by recursion, using the MARK field to prevent infinite recursion.
7793 If the DIE is nested inside a namespace or another type, we also
7794 need to include that context in the signature. The lower 64 bits
7795 of the resulting MD5 checksum comprise the signature. */
7797 static void
7798 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7800 int mark;
7801 const char *name;
7802 unsigned char checksum[16];
7803 struct md5_ctx ctx;
7804 dw_die_ref decl;
7805 dw_die_ref parent;
7807 name = get_AT_string (die, DW_AT_name);
7808 decl = get_AT_ref (die, DW_AT_specification);
7809 parent = get_die_parent (die);
7811 /* First, compute a signature for just the type name (and its surrounding
7812 context, if any. This is stored in the type unit DIE for link-time
7813 ODR (one-definition rule) checking. */
7815 if (is_cxx () && name != NULL)
7817 md5_init_ctx (&ctx);
7819 /* Checksum the names of surrounding namespaces and structures. */
7820 if (parent != NULL)
7821 checksum_die_context (parent, &ctx);
7823 /* Checksum the current DIE. */
7824 die_odr_checksum (die->die_tag, name, &ctx);
7825 md5_finish_ctx (&ctx, checksum);
7827 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7830 /* Next, compute the complete type signature. */
7832 md5_init_ctx (&ctx);
7833 mark = 1;
7834 die->die_mark = mark;
7836 /* Checksum the names of surrounding namespaces and structures. */
7837 if (parent != NULL)
7838 checksum_die_context (parent, &ctx);
7840 /* Checksum the DIE and its children. */
7841 die_checksum_ordered (die, &ctx, &mark);
7842 unmark_all_dies (die);
7843 md5_finish_ctx (&ctx, checksum);
7845 /* Store the signature in the type node and link the type DIE and the
7846 type node together. */
7847 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7848 DWARF_TYPE_SIGNATURE_SIZE);
7849 die->comdat_type_p = true;
7850 die->die_id.die_type_node = type_node;
7851 type_node->type_die = die;
7853 /* If the DIE is a specification, link its declaration to the type node
7854 as well. */
7855 if (decl != NULL)
7857 decl->comdat_type_p = true;
7858 decl->die_id.die_type_node = type_node;
7862 /* Do the location expressions look same? */
7863 static inline bool
7864 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7866 return loc1->dw_loc_opc == loc2->dw_loc_opc
7867 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7868 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7871 /* Do the values look the same? */
7872 static bool
7873 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7875 dw_loc_descr_ref loc1, loc2;
7876 rtx r1, r2;
7878 if (v1->val_class != v2->val_class)
7879 return false;
7881 switch (v1->val_class)
7883 case dw_val_class_const:
7884 case dw_val_class_const_implicit:
7885 return v1->v.val_int == v2->v.val_int;
7886 case dw_val_class_unsigned_const:
7887 case dw_val_class_unsigned_const_implicit:
7888 return v1->v.val_unsigned == v2->v.val_unsigned;
7889 case dw_val_class_const_double:
7890 return v1->v.val_double.high == v2->v.val_double.high
7891 && v1->v.val_double.low == v2->v.val_double.low;
7892 case dw_val_class_wide_int:
7893 return *v1->v.val_wide == *v2->v.val_wide;
7894 case dw_val_class_vec:
7895 if (v1->v.val_vec.length != v2->v.val_vec.length
7896 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7897 return false;
7898 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7899 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7900 return false;
7901 return true;
7902 case dw_val_class_flag:
7903 return v1->v.val_flag == v2->v.val_flag;
7904 case dw_val_class_str:
7905 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7907 case dw_val_class_addr:
7908 r1 = v1->v.val_addr;
7909 r2 = v2->v.val_addr;
7910 if (GET_CODE (r1) != GET_CODE (r2))
7911 return false;
7912 return !rtx_equal_p (r1, r2);
7914 case dw_val_class_offset:
7915 return v1->v.val_offset == v2->v.val_offset;
7917 case dw_val_class_loc:
7918 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7919 loc1 && loc2;
7920 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7921 if (!same_loc_p (loc1, loc2, mark))
7922 return false;
7923 return !loc1 && !loc2;
7925 case dw_val_class_die_ref:
7926 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7928 case dw_val_class_symview:
7929 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7931 case dw_val_class_fde_ref:
7932 case dw_val_class_vms_delta:
7933 case dw_val_class_lbl_id:
7934 case dw_val_class_lineptr:
7935 case dw_val_class_macptr:
7936 case dw_val_class_loclistsptr:
7937 case dw_val_class_high_pc:
7938 return true;
7940 case dw_val_class_file:
7941 case dw_val_class_file_implicit:
7942 return v1->v.val_file == v2->v.val_file;
7944 case dw_val_class_data8:
7945 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7947 default:
7948 return true;
7952 /* Do the attributes look the same? */
7954 static bool
7955 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7957 if (at1->dw_attr != at2->dw_attr)
7958 return false;
7960 /* We don't care that this was compiled with a different compiler
7961 snapshot; if the output is the same, that's what matters. */
7962 if (at1->dw_attr == DW_AT_producer)
7963 return true;
7965 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7968 /* Do the dies look the same? */
7970 static bool
7971 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7973 dw_die_ref c1, c2;
7974 dw_attr_node *a1;
7975 unsigned ix;
7977 /* To avoid infinite recursion. */
7978 if (die1->die_mark)
7979 return die1->die_mark == die2->die_mark;
7980 die1->die_mark = die2->die_mark = ++(*mark);
7982 if (die1->die_tag != die2->die_tag)
7983 return false;
7985 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7986 return false;
7988 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7989 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7990 return false;
7992 c1 = die1->die_child;
7993 c2 = die2->die_child;
7994 if (! c1)
7996 if (c2)
7997 return false;
7999 else
8000 for (;;)
8002 if (!same_die_p (c1, c2, mark))
8003 return false;
8004 c1 = c1->die_sib;
8005 c2 = c2->die_sib;
8006 if (c1 == die1->die_child)
8008 if (c2 == die2->die_child)
8009 break;
8010 else
8011 return false;
8015 return true;
8018 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8019 children, and set die_symbol. */
8021 static void
8022 compute_comp_unit_symbol (dw_die_ref unit_die)
8024 const char *die_name = get_AT_string (unit_die, DW_AT_name);
8025 const char *base = die_name ? lbasename (die_name) : "anonymous";
8026 char *name = XALLOCAVEC (char, strlen (base) + 64);
8027 char *p;
8028 int i, mark;
8029 unsigned char checksum[16];
8030 struct md5_ctx ctx;
8032 /* Compute the checksum of the DIE, then append part of it as hex digits to
8033 the name filename of the unit. */
8035 md5_init_ctx (&ctx);
8036 mark = 0;
8037 die_checksum (unit_die, &ctx, &mark);
8038 unmark_all_dies (unit_die);
8039 md5_finish_ctx (&ctx, checksum);
8041 /* When we this for comp_unit_die () we have a DW_AT_name that might
8042 not start with a letter but with anything valid for filenames and
8043 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
8044 character is not a letter. */
8045 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
8046 clean_symbol_name (name);
8048 p = name + strlen (name);
8049 for (i = 0; i < 4; i++)
8051 sprintf (p, "%.2x", checksum[i]);
8052 p += 2;
8055 unit_die->die_id.die_symbol = xstrdup (name);
8058 /* Returns true if DIE represents a type, in the sense of TYPE_P. */
8060 static bool
8061 is_type_die (dw_die_ref die)
8063 switch (die->die_tag)
8065 case DW_TAG_array_type:
8066 case DW_TAG_class_type:
8067 case DW_TAG_interface_type:
8068 case DW_TAG_enumeration_type:
8069 case DW_TAG_pointer_type:
8070 case DW_TAG_reference_type:
8071 case DW_TAG_rvalue_reference_type:
8072 case DW_TAG_string_type:
8073 case DW_TAG_structure_type:
8074 case DW_TAG_subroutine_type:
8075 case DW_TAG_union_type:
8076 case DW_TAG_ptr_to_member_type:
8077 case DW_TAG_set_type:
8078 case DW_TAG_subrange_type:
8079 case DW_TAG_base_type:
8080 case DW_TAG_const_type:
8081 case DW_TAG_file_type:
8082 case DW_TAG_packed_type:
8083 case DW_TAG_volatile_type:
8084 case DW_TAG_typedef:
8085 return true;
8086 default:
8087 return false;
8091 /* Returns true iff C is a compile-unit DIE. */
8093 static inline bool
8094 is_cu_die (dw_die_ref c)
8096 return c && (c->die_tag == DW_TAG_compile_unit
8097 || c->die_tag == DW_TAG_skeleton_unit);
8100 /* Returns true iff C is a unit DIE of some sort. */
8102 static inline bool
8103 is_unit_die (dw_die_ref c)
8105 return c && (c->die_tag == DW_TAG_compile_unit
8106 || c->die_tag == DW_TAG_partial_unit
8107 || c->die_tag == DW_TAG_type_unit
8108 || c->die_tag == DW_TAG_skeleton_unit);
8111 /* Returns true iff C is a namespace DIE. */
8113 static inline bool
8114 is_namespace_die (dw_die_ref c)
8116 return c && c->die_tag == DW_TAG_namespace;
8119 /* Return true if this DIE is a template parameter. */
8121 static inline bool
8122 is_template_parameter (dw_die_ref die)
8124 switch (die->die_tag)
8126 case DW_TAG_template_type_param:
8127 case DW_TAG_template_value_param:
8128 case DW_TAG_GNU_template_template_param:
8129 case DW_TAG_GNU_template_parameter_pack:
8130 return true;
8131 default:
8132 return false;
8136 /* Return true if this DIE represents a template instantiation. */
8138 static inline bool
8139 is_template_instantiation (dw_die_ref die)
8141 dw_die_ref c;
8143 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8144 return false;
8145 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8146 return false;
8149 static char *
8150 gen_internal_sym (const char *prefix)
8152 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8154 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8155 return xstrdup (buf);
8158 /* Return true if this DIE is a declaration. */
8160 static bool
8161 is_declaration_die (dw_die_ref die)
8163 dw_attr_node *a;
8164 unsigned ix;
8166 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8167 if (a->dw_attr == DW_AT_declaration)
8168 return true;
8170 return false;
8173 /* Return true if this DIE is nested inside a subprogram. */
8175 static bool
8176 is_nested_in_subprogram (dw_die_ref die)
8178 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8180 if (decl == NULL)
8181 decl = die;
8182 return local_scope_p (decl);
8185 /* Return true if this DIE contains a defining declaration of a
8186 subprogram. */
8188 static bool
8189 contains_subprogram_definition (dw_die_ref die)
8191 dw_die_ref c;
8193 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8194 return true;
8195 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8196 return false;
8199 /* Return true if this is a type DIE that should be moved to a
8200 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8201 unit type. */
8203 static bool
8204 should_move_die_to_comdat (dw_die_ref die)
8206 switch (die->die_tag)
8208 case DW_TAG_class_type:
8209 case DW_TAG_structure_type:
8210 case DW_TAG_enumeration_type:
8211 case DW_TAG_union_type:
8212 /* Don't move declarations, inlined instances, types nested in a
8213 subprogram, or types that contain subprogram definitions. */
8214 if (is_declaration_die (die)
8215 || get_AT (die, DW_AT_abstract_origin)
8216 || is_nested_in_subprogram (die)
8217 || contains_subprogram_definition (die))
8218 return false;
8219 if (die->die_tag != DW_TAG_enumeration_type)
8221 /* Don't move non-constant size aggregates. */
8222 dw_attr_node *sz = get_AT (die, DW_AT_byte_size);
8223 if (sz == NULL
8224 || (AT_class (sz) != dw_val_class_unsigned_const
8225 && AT_class (sz) != dw_val_class_unsigned_const_implicit))
8226 return false;
8228 return true;
8229 case DW_TAG_array_type:
8230 case DW_TAG_interface_type:
8231 case DW_TAG_pointer_type:
8232 case DW_TAG_reference_type:
8233 case DW_TAG_rvalue_reference_type:
8234 case DW_TAG_string_type:
8235 case DW_TAG_subroutine_type:
8236 case DW_TAG_ptr_to_member_type:
8237 case DW_TAG_set_type:
8238 case DW_TAG_subrange_type:
8239 case DW_TAG_base_type:
8240 case DW_TAG_const_type:
8241 case DW_TAG_file_type:
8242 case DW_TAG_packed_type:
8243 case DW_TAG_volatile_type:
8244 case DW_TAG_typedef:
8245 default:
8246 return false;
8250 /* Make a clone of DIE. */
8252 static dw_die_ref
8253 clone_die (dw_die_ref die)
8255 dw_die_ref clone = new_die_raw (die->die_tag);
8256 dw_attr_node *a;
8257 unsigned ix;
8259 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8260 add_dwarf_attr (clone, a);
8262 return clone;
8265 /* Make a clone of the tree rooted at DIE. */
8267 static dw_die_ref
8268 clone_tree (dw_die_ref die)
8270 dw_die_ref c;
8271 dw_die_ref clone = clone_die (die);
8273 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8275 return clone;
8278 /* Make a clone of DIE as a declaration. */
8280 static dw_die_ref
8281 clone_as_declaration (dw_die_ref die)
8283 dw_die_ref clone;
8284 dw_die_ref decl;
8285 dw_attr_node *a;
8286 unsigned ix;
8288 /* If the DIE is already a declaration, just clone it. */
8289 if (is_declaration_die (die))
8290 return clone_die (die);
8292 /* If the DIE is a specification, just clone its declaration DIE. */
8293 decl = get_AT_ref (die, DW_AT_specification);
8294 if (decl != NULL)
8296 clone = clone_die (decl);
8297 if (die->comdat_type_p)
8298 add_AT_die_ref (clone, DW_AT_signature, die);
8299 return clone;
8302 clone = new_die_raw (die->die_tag);
8304 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8306 /* We don't want to copy over all attributes.
8307 For example we don't want DW_AT_byte_size because otherwise we will no
8308 longer have a declaration and GDB will treat it as a definition. */
8310 switch (a->dw_attr)
8312 case DW_AT_abstract_origin:
8313 case DW_AT_artificial:
8314 case DW_AT_containing_type:
8315 case DW_AT_external:
8316 case DW_AT_name:
8317 case DW_AT_type:
8318 case DW_AT_virtuality:
8319 case DW_AT_linkage_name:
8320 case DW_AT_MIPS_linkage_name:
8321 add_dwarf_attr (clone, a);
8322 break;
8323 case DW_AT_byte_size:
8324 case DW_AT_alignment:
8325 default:
8326 break;
8330 if (die->comdat_type_p)
8331 add_AT_die_ref (clone, DW_AT_signature, die);
8333 add_AT_flag (clone, DW_AT_declaration, 1);
8334 return clone;
8338 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8340 struct decl_table_entry
8342 dw_die_ref orig;
8343 dw_die_ref copy;
8346 /* Helpers to manipulate hash table of copied declarations. */
8348 /* Hashtable helpers. */
8350 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8352 typedef die_struct *compare_type;
8353 static inline hashval_t hash (const decl_table_entry *);
8354 static inline bool equal (const decl_table_entry *, const die_struct *);
8357 inline hashval_t
8358 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8360 return htab_hash_pointer (entry->orig);
8363 inline bool
8364 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8365 const die_struct *entry2)
8367 return entry1->orig == entry2;
8370 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8372 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8373 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8374 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8375 to check if the ancestor has already been copied into UNIT. */
8377 static dw_die_ref
8378 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8379 decl_hash_type *decl_table)
8381 dw_die_ref parent = die->die_parent;
8382 dw_die_ref new_parent = unit;
8383 dw_die_ref copy;
8384 decl_table_entry **slot = NULL;
8385 struct decl_table_entry *entry = NULL;
8387 /* If DIE refers to a stub unfold that so we get the appropriate
8388 DIE registered as orig in decl_table. */
8389 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8390 die = c;
8392 if (decl_table)
8394 /* Check if the entry has already been copied to UNIT. */
8395 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8396 INSERT);
8397 if (*slot != HTAB_EMPTY_ENTRY)
8399 entry = *slot;
8400 return entry->copy;
8403 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8404 entry = XCNEW (struct decl_table_entry);
8405 entry->orig = die;
8406 entry->copy = NULL;
8407 *slot = entry;
8410 if (parent != NULL)
8412 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8413 if (spec != NULL)
8414 parent = spec;
8415 if (!is_unit_die (parent))
8416 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8419 copy = clone_as_declaration (die);
8420 add_child_die (new_parent, copy);
8422 if (decl_table)
8424 /* Record the pointer to the copy. */
8425 entry->copy = copy;
8428 return copy;
8430 /* Copy the declaration context to the new type unit DIE. This includes
8431 any surrounding namespace or type declarations. If the DIE has an
8432 AT_specification attribute, it also includes attributes and children
8433 attached to the specification, and returns a pointer to the original
8434 parent of the declaration DIE. Returns NULL otherwise. */
8436 static dw_die_ref
8437 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8439 dw_die_ref decl;
8440 dw_die_ref new_decl;
8441 dw_die_ref orig_parent = NULL;
8443 decl = get_AT_ref (die, DW_AT_specification);
8444 if (decl == NULL)
8445 decl = die;
8446 else
8448 unsigned ix;
8449 dw_die_ref c;
8450 dw_attr_node *a;
8452 /* The original DIE will be changed to a declaration, and must
8453 be moved to be a child of the original declaration DIE. */
8454 orig_parent = decl->die_parent;
8456 /* Copy the type node pointer from the new DIE to the original
8457 declaration DIE so we can forward references later. */
8458 decl->comdat_type_p = true;
8459 decl->die_id.die_type_node = die->die_id.die_type_node;
8461 remove_AT (die, DW_AT_specification);
8463 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8465 if (a->dw_attr != DW_AT_name
8466 && a->dw_attr != DW_AT_declaration
8467 && a->dw_attr != DW_AT_external)
8468 add_dwarf_attr (die, a);
8471 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8474 if (decl->die_parent != NULL
8475 && !is_unit_die (decl->die_parent))
8477 new_decl = copy_ancestor_tree (unit, decl, NULL);
8478 if (new_decl != NULL)
8480 remove_AT (new_decl, DW_AT_signature);
8481 add_AT_specification (die, new_decl);
8485 return orig_parent;
8488 /* Generate the skeleton ancestor tree for the given NODE, then clone
8489 the DIE and add the clone into the tree. */
8491 static void
8492 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8494 if (node->new_die != NULL)
8495 return;
8497 node->new_die = clone_as_declaration (node->old_die);
8499 if (node->parent != NULL)
8501 generate_skeleton_ancestor_tree (node->parent);
8502 add_child_die (node->parent->new_die, node->new_die);
8506 /* Generate a skeleton tree of DIEs containing any declarations that are
8507 found in the original tree. We traverse the tree looking for declaration
8508 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8510 static void
8511 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8513 skeleton_chain_node node;
8514 dw_die_ref c;
8515 dw_die_ref first;
8516 dw_die_ref prev = NULL;
8517 dw_die_ref next = NULL;
8519 node.parent = parent;
8521 first = c = parent->old_die->die_child;
8522 if (c)
8523 next = c->die_sib;
8524 if (c) do {
8525 if (prev == NULL || prev->die_sib == c)
8526 prev = c;
8527 c = next;
8528 next = (c == first ? NULL : c->die_sib);
8529 node.old_die = c;
8530 node.new_die = NULL;
8531 if (is_declaration_die (c))
8533 if (is_template_instantiation (c))
8535 /* Instantiated templates do not need to be cloned into the
8536 type unit. Just move the DIE and its children back to
8537 the skeleton tree (in the main CU). */
8538 remove_child_with_prev (c, prev);
8539 add_child_die (parent->new_die, c);
8540 c = prev;
8542 else if (c->comdat_type_p)
8544 /* This is the skeleton of earlier break_out_comdat_types
8545 type. Clone the existing DIE, but keep the children
8546 under the original (which is in the main CU). */
8547 dw_die_ref clone = clone_die (c);
8549 replace_child (c, clone, prev);
8550 generate_skeleton_ancestor_tree (parent);
8551 add_child_die (parent->new_die, c);
8552 c = clone;
8553 continue;
8555 else
8557 /* Clone the existing DIE, move the original to the skeleton
8558 tree (which is in the main CU), and put the clone, with
8559 all the original's children, where the original came from
8560 (which is about to be moved to the type unit). */
8561 dw_die_ref clone = clone_die (c);
8562 move_all_children (c, clone);
8564 /* If the original has a DW_AT_object_pointer attribute,
8565 it would now point to a child DIE just moved to the
8566 cloned tree, so we need to remove that attribute from
8567 the original. */
8568 remove_AT (c, DW_AT_object_pointer);
8570 replace_child (c, clone, prev);
8571 generate_skeleton_ancestor_tree (parent);
8572 add_child_die (parent->new_die, c);
8573 node.old_die = clone;
8574 node.new_die = c;
8575 c = clone;
8578 generate_skeleton_bottom_up (&node);
8579 } while (next != NULL);
8582 /* Wrapper function for generate_skeleton_bottom_up. */
8584 static dw_die_ref
8585 generate_skeleton (dw_die_ref die)
8587 skeleton_chain_node node;
8589 node.old_die = die;
8590 node.new_die = NULL;
8591 node.parent = NULL;
8593 /* If this type definition is nested inside another type,
8594 and is not an instantiation of a template, always leave
8595 at least a declaration in its place. */
8596 if (die->die_parent != NULL
8597 && is_type_die (die->die_parent)
8598 && !is_template_instantiation (die))
8599 node.new_die = clone_as_declaration (die);
8601 generate_skeleton_bottom_up (&node);
8602 return node.new_die;
8605 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8606 declaration. The original DIE is moved to a new compile unit so that
8607 existing references to it follow it to the new location. If any of the
8608 original DIE's descendants is a declaration, we need to replace the
8609 original DIE with a skeleton tree and move the declarations back into the
8610 skeleton tree. */
8612 static dw_die_ref
8613 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8614 dw_die_ref prev)
8616 dw_die_ref skeleton, orig_parent;
8618 /* Copy the declaration context to the type unit DIE. If the returned
8619 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8620 that DIE. */
8621 orig_parent = copy_declaration_context (unit, child);
8623 skeleton = generate_skeleton (child);
8624 if (skeleton == NULL)
8625 remove_child_with_prev (child, prev);
8626 else
8628 skeleton->comdat_type_p = true;
8629 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8631 /* If the original DIE was a specification, we need to put
8632 the skeleton under the parent DIE of the declaration.
8633 This leaves the original declaration in the tree, but
8634 it will be pruned later since there are no longer any
8635 references to it. */
8636 if (orig_parent != NULL)
8638 remove_child_with_prev (child, prev);
8639 add_child_die (orig_parent, skeleton);
8641 else
8642 replace_child (child, skeleton, prev);
8645 return skeleton;
8648 static void
8649 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8650 comdat_type_node *type_node,
8651 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8653 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8654 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8655 DWARF procedure references in the DW_AT_location attribute. */
8657 static dw_die_ref
8658 copy_dwarf_procedure (dw_die_ref die,
8659 comdat_type_node *type_node,
8660 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8662 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8664 /* DWARF procedures are not supposed to have children... */
8665 gcc_assert (die->die_child == NULL);
8667 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8668 gcc_assert (vec_safe_length (die->die_attr) == 1
8669 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8671 /* Do not copy more than once DWARF procedures. */
8672 bool existed;
8673 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8674 if (existed)
8675 return die_copy;
8677 die_copy = clone_die (die);
8678 add_child_die (type_node->root_die, die_copy);
8679 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8680 return die_copy;
8683 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8684 procedures in DIE's attributes. */
8686 static void
8687 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8688 comdat_type_node *type_node,
8689 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8691 dw_attr_node *a;
8692 unsigned i;
8694 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8696 dw_loc_descr_ref loc;
8698 if (a->dw_attr_val.val_class != dw_val_class_loc)
8699 continue;
8701 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8703 switch (loc->dw_loc_opc)
8705 case DW_OP_call2:
8706 case DW_OP_call4:
8707 case DW_OP_call_ref:
8708 gcc_assert (loc->dw_loc_oprnd1.val_class
8709 == dw_val_class_die_ref);
8710 loc->dw_loc_oprnd1.v.val_die_ref.die
8711 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8712 type_node,
8713 copied_dwarf_procs);
8715 default:
8716 break;
8722 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8723 rewrite references to point to the copies.
8725 References are looked for in DIE's attributes and recursively in all its
8726 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8727 mapping from old DWARF procedures to their copy. It is used not to copy
8728 twice the same DWARF procedure under TYPE_NODE. */
8730 static void
8731 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8732 comdat_type_node *type_node,
8733 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8735 dw_die_ref c;
8737 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8738 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8739 type_node,
8740 copied_dwarf_procs));
8743 /* Traverse the DIE and set up additional .debug_types or .debug_info
8744 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8745 section. */
8747 static void
8748 break_out_comdat_types (dw_die_ref die)
8750 dw_die_ref c;
8751 dw_die_ref first;
8752 dw_die_ref prev = NULL;
8753 dw_die_ref next = NULL;
8754 dw_die_ref unit = NULL;
8756 first = c = die->die_child;
8757 if (c)
8758 next = c->die_sib;
8759 if (c) do {
8760 if (prev == NULL || prev->die_sib == c)
8761 prev = c;
8762 c = next;
8763 next = (c == first ? NULL : c->die_sib);
8764 if (should_move_die_to_comdat (c))
8766 dw_die_ref replacement;
8767 comdat_type_node *type_node;
8769 /* Break out nested types into their own type units. */
8770 break_out_comdat_types (c);
8772 /* Create a new type unit DIE as the root for the new tree. */
8773 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8774 add_AT_unsigned (unit, DW_AT_language,
8775 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8777 /* Add the new unit's type DIE into the comdat type list. */
8778 type_node = ggc_cleared_alloc<comdat_type_node> ();
8779 type_node->root_die = unit;
8780 type_node->next = comdat_type_list;
8781 comdat_type_list = type_node;
8783 /* Generate the type signature. */
8784 generate_type_signature (c, type_node);
8786 /* Copy the declaration context, attributes, and children of the
8787 declaration into the new type unit DIE, then remove this DIE
8788 from the main CU (or replace it with a skeleton if necessary). */
8789 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8790 type_node->skeleton_die = replacement;
8792 /* Add the DIE to the new compunit. */
8793 add_child_die (unit, c);
8795 /* Types can reference DWARF procedures for type size or data location
8796 expressions. Calls in DWARF expressions cannot target procedures
8797 that are not in the same section. So we must copy DWARF procedures
8798 along with this type and then rewrite references to them. */
8799 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8800 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8802 if (replacement != NULL)
8803 c = replacement;
8805 else if (c->die_tag == DW_TAG_namespace
8806 || c->die_tag == DW_TAG_class_type
8807 || c->die_tag == DW_TAG_structure_type
8808 || c->die_tag == DW_TAG_union_type)
8810 /* Look for nested types that can be broken out. */
8811 break_out_comdat_types (c);
8813 } while (next != NULL);
8816 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8817 Enter all the cloned children into the hash table decl_table. */
8819 static dw_die_ref
8820 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8822 dw_die_ref c;
8823 dw_die_ref clone;
8824 struct decl_table_entry *entry;
8825 decl_table_entry **slot;
8827 if (die->die_tag == DW_TAG_subprogram)
8828 clone = clone_as_declaration (die);
8829 else
8830 clone = clone_die (die);
8832 slot = decl_table->find_slot_with_hash (die,
8833 htab_hash_pointer (die), INSERT);
8835 /* Assert that DIE isn't in the hash table yet. If it would be there
8836 before, the ancestors would be necessarily there as well, therefore
8837 clone_tree_partial wouldn't be called. */
8838 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8840 entry = XCNEW (struct decl_table_entry);
8841 entry->orig = die;
8842 entry->copy = clone;
8843 *slot = entry;
8845 if (die->die_tag != DW_TAG_subprogram)
8846 FOR_EACH_CHILD (die, c,
8847 add_child_die (clone, clone_tree_partial (c, decl_table)));
8849 return clone;
8852 /* Walk the DIE and its children, looking for references to incomplete
8853 or trivial types that are unmarked (i.e., that are not in the current
8854 type_unit). */
8856 static void
8857 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8859 dw_die_ref c;
8860 dw_attr_node *a;
8861 unsigned ix;
8863 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8865 if (AT_class (a) == dw_val_class_die_ref)
8867 dw_die_ref targ = AT_ref (a);
8868 decl_table_entry **slot;
8869 struct decl_table_entry *entry;
8871 if (targ->die_mark != 0 || targ->comdat_type_p)
8872 continue;
8874 slot = decl_table->find_slot_with_hash (targ,
8875 htab_hash_pointer (targ),
8876 INSERT);
8878 if (*slot != HTAB_EMPTY_ENTRY)
8880 /* TARG has already been copied, so we just need to
8881 modify the reference to point to the copy. */
8882 entry = *slot;
8883 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8885 else
8887 dw_die_ref parent = unit;
8888 dw_die_ref copy = clone_die (targ);
8890 /* Record in DECL_TABLE that TARG has been copied.
8891 Need to do this now, before the recursive call,
8892 because DECL_TABLE may be expanded and SLOT
8893 would no longer be a valid pointer. */
8894 entry = XCNEW (struct decl_table_entry);
8895 entry->orig = targ;
8896 entry->copy = copy;
8897 *slot = entry;
8899 /* If TARG is not a declaration DIE, we need to copy its
8900 children. */
8901 if (!is_declaration_die (targ))
8903 FOR_EACH_CHILD (
8904 targ, c,
8905 add_child_die (copy,
8906 clone_tree_partial (c, decl_table)));
8909 /* Make sure the cloned tree is marked as part of the
8910 type unit. */
8911 mark_dies (copy);
8913 /* If TARG has surrounding context, copy its ancestor tree
8914 into the new type unit. */
8915 if (targ->die_parent != NULL
8916 && !is_unit_die (targ->die_parent))
8917 parent = copy_ancestor_tree (unit, targ->die_parent,
8918 decl_table);
8920 add_child_die (parent, copy);
8921 a->dw_attr_val.v.val_die_ref.die = copy;
8923 /* Make sure the newly-copied DIE is walked. If it was
8924 installed in a previously-added context, it won't
8925 get visited otherwise. */
8926 if (parent != unit)
8928 /* Find the highest point of the newly-added tree,
8929 mark each node along the way, and walk from there. */
8930 parent->die_mark = 1;
8931 while (parent->die_parent
8932 && parent->die_parent->die_mark == 0)
8934 parent = parent->die_parent;
8935 parent->die_mark = 1;
8937 copy_decls_walk (unit, parent, decl_table);
8943 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8946 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8947 and record them in DECL_TABLE. */
8949 static void
8950 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8952 dw_die_ref c;
8954 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8956 dw_die_ref targ = AT_ref (a);
8957 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8958 decl_table_entry **slot
8959 = decl_table->find_slot_with_hash (targ,
8960 htab_hash_pointer (targ),
8961 INSERT);
8962 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8963 /* Record in DECL_TABLE that TARG has been already copied
8964 by remove_child_or_replace_with_skeleton. */
8965 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8966 entry->orig = targ;
8967 entry->copy = die;
8968 *slot = entry;
8970 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8973 /* Copy declarations for "unworthy" types into the new comdat section.
8974 Incomplete types, modified types, and certain other types aren't broken
8975 out into comdat sections of their own, so they don't have a signature,
8976 and we need to copy the declaration into the same section so that we
8977 don't have an external reference. */
8979 static void
8980 copy_decls_for_unworthy_types (dw_die_ref unit)
8982 mark_dies (unit);
8983 decl_hash_type decl_table (10);
8984 collect_skeleton_dies (unit, &decl_table);
8985 copy_decls_walk (unit, unit, &decl_table);
8986 unmark_dies (unit);
8989 /* Traverse the DIE and add a sibling attribute if it may have the
8990 effect of speeding up access to siblings. To save some space,
8991 avoid generating sibling attributes for DIE's without children. */
8993 static void
8994 add_sibling_attributes (dw_die_ref die)
8996 dw_die_ref c;
8998 if (! die->die_child)
8999 return;
9001 if (die->die_parent && die != die->die_parent->die_child)
9002 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
9004 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
9007 /* Output all location lists for the DIE and its children. */
9009 static void
9010 output_location_lists (dw_die_ref die)
9012 dw_die_ref c;
9013 dw_attr_node *a;
9014 unsigned ix;
9016 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9017 if (AT_class (a) == dw_val_class_loc_list)
9018 output_loc_list (AT_loc_list (a));
9020 FOR_EACH_CHILD (die, c, output_location_lists (c));
9023 /* During assign_location_list_indexes and output_loclists_offset the
9024 current index, after it the number of assigned indexes (i.e. how
9025 large the .debug_loclists* offset table should be). */
9026 static unsigned int loc_list_idx;
9028 /* Output all location list offsets for the DIE and its children. */
9030 static void
9031 output_loclists_offsets (dw_die_ref die)
9033 dw_die_ref c;
9034 dw_attr_node *a;
9035 unsigned ix;
9037 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9038 if (AT_class (a) == dw_val_class_loc_list)
9040 dw_loc_list_ref l = AT_loc_list (a);
9041 if (l->offset_emitted)
9042 continue;
9043 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
9044 loc_section_label, NULL);
9045 gcc_assert (l->hash == loc_list_idx);
9046 loc_list_idx++;
9047 l->offset_emitted = true;
9050 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
9053 /* Recursively set indexes of location lists. */
9055 static void
9056 assign_location_list_indexes (dw_die_ref die)
9058 dw_die_ref c;
9059 dw_attr_node *a;
9060 unsigned ix;
9062 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9063 if (AT_class (a) == dw_val_class_loc_list)
9065 dw_loc_list_ref list = AT_loc_list (a);
9066 if (!list->num_assigned)
9068 list->num_assigned = true;
9069 list->hash = loc_list_idx++;
9073 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
9076 /* We want to limit the number of external references, because they are
9077 larger than local references: a relocation takes multiple words, and
9078 even a sig8 reference is always eight bytes, whereas a local reference
9079 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
9080 So if we encounter multiple external references to the same type DIE, we
9081 make a local typedef stub for it and redirect all references there.
9083 This is the element of the hash table for keeping track of these
9084 references. */
9086 struct external_ref
9088 dw_die_ref type;
9089 dw_die_ref stub;
9090 unsigned n_refs;
9093 /* Hashtable helpers. */
9095 struct external_ref_hasher : free_ptr_hash <external_ref>
9097 static inline hashval_t hash (const external_ref *);
9098 static inline bool equal (const external_ref *, const external_ref *);
9101 inline hashval_t
9102 external_ref_hasher::hash (const external_ref *r)
9104 dw_die_ref die = r->type;
9105 hashval_t h = 0;
9107 /* We can't use the address of the DIE for hashing, because
9108 that will make the order of the stub DIEs non-deterministic. */
9109 if (! die->comdat_type_p)
9110 /* We have a symbol; use it to compute a hash. */
9111 h = htab_hash_string (die->die_id.die_symbol);
9112 else
9114 /* We have a type signature; use a subset of the bits as the hash.
9115 The 8-byte signature is at least as large as hashval_t. */
9116 comdat_type_node *type_node = die->die_id.die_type_node;
9117 memcpy (&h, type_node->signature, sizeof (h));
9119 return h;
9122 inline bool
9123 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9125 return r1->type == r2->type;
9128 typedef hash_table<external_ref_hasher> external_ref_hash_type;
9130 /* Return a pointer to the external_ref for references to DIE. */
9132 static struct external_ref *
9133 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9135 struct external_ref ref, *ref_p;
9136 external_ref **slot;
9138 ref.type = die;
9139 slot = map->find_slot (&ref, INSERT);
9140 if (*slot != HTAB_EMPTY_ENTRY)
9141 return *slot;
9143 ref_p = XCNEW (struct external_ref);
9144 ref_p->type = die;
9145 *slot = ref_p;
9146 return ref_p;
9149 /* Subroutine of optimize_external_refs, below.
9151 If we see a type skeleton, record it as our stub. If we see external
9152 references, remember how many we've seen. */
9154 static void
9155 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9157 dw_die_ref c;
9158 dw_attr_node *a;
9159 unsigned ix;
9160 struct external_ref *ref_p;
9162 if (is_type_die (die)
9163 && (c = get_AT_ref (die, DW_AT_signature)))
9165 /* This is a local skeleton; use it for local references. */
9166 ref_p = lookup_external_ref (map, c);
9167 ref_p->stub = die;
9170 /* Scan the DIE references, and remember any that refer to DIEs from
9171 other CUs (i.e. those which are not marked). */
9172 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9173 if (AT_class (a) == dw_val_class_die_ref
9174 && (c = AT_ref (a))->die_mark == 0
9175 && is_type_die (c))
9177 ref_p = lookup_external_ref (map, c);
9178 ref_p->n_refs++;
9181 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9184 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9185 points to an external_ref, DATA is the CU we're processing. If we don't
9186 already have a local stub, and we have multiple refs, build a stub. */
9189 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9191 struct external_ref *ref_p = *slot;
9193 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9195 /* We have multiple references to this type, so build a small stub.
9196 Both of these forms are a bit dodgy from the perspective of the
9197 DWARF standard, since technically they should have names. */
9198 dw_die_ref cu = data;
9199 dw_die_ref type = ref_p->type;
9200 dw_die_ref stub = NULL;
9202 if (type->comdat_type_p)
9204 /* If we refer to this type via sig8, use AT_signature. */
9205 stub = new_die (type->die_tag, cu, NULL_TREE);
9206 add_AT_die_ref (stub, DW_AT_signature, type);
9208 else
9210 /* Otherwise, use a typedef with no name. */
9211 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9212 add_AT_die_ref (stub, DW_AT_type, type);
9215 stub->die_mark++;
9216 ref_p->stub = stub;
9218 return 1;
9221 /* DIE is a unit; look through all the DIE references to see if there are
9222 any external references to types, and if so, create local stubs for
9223 them which will be applied in build_abbrev_table. This is useful because
9224 references to local DIEs are smaller. */
9226 static external_ref_hash_type *
9227 optimize_external_refs (dw_die_ref die)
9229 external_ref_hash_type *map = new external_ref_hash_type (10);
9230 optimize_external_refs_1 (die, map);
9231 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9232 return map;
9235 /* The following 3 variables are temporaries that are computed only during the
9236 build_abbrev_table call and used and released during the following
9237 optimize_abbrev_table call. */
9239 /* First abbrev_id that can be optimized based on usage. */
9240 static unsigned int abbrev_opt_start;
9242 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9243 abbrev_id smaller than this, because they must be already sized
9244 during build_abbrev_table). */
9245 static unsigned int abbrev_opt_base_type_end;
9247 /* Vector of usage counts during build_abbrev_table. Indexed by
9248 abbrev_id - abbrev_opt_start. */
9249 static vec<unsigned int> abbrev_usage_count;
9251 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9252 static vec<dw_die_ref> sorted_abbrev_dies;
9254 /* The format of each DIE (and its attribute value pairs) is encoded in an
9255 abbreviation table. This routine builds the abbreviation table and assigns
9256 a unique abbreviation id for each abbreviation entry. The children of each
9257 die are visited recursively. */
9259 static void
9260 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9262 unsigned int abbrev_id = 0;
9263 dw_die_ref c;
9264 dw_attr_node *a;
9265 unsigned ix;
9266 dw_die_ref abbrev;
9268 /* Scan the DIE references, and replace any that refer to
9269 DIEs from other CUs (i.e. those which are not marked) with
9270 the local stubs we built in optimize_external_refs. */
9271 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9272 if (AT_class (a) == dw_val_class_die_ref
9273 && (c = AT_ref (a))->die_mark == 0)
9275 struct external_ref *ref_p;
9276 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9278 if (is_type_die (c)
9279 && (ref_p = lookup_external_ref (extern_map, c))
9280 && ref_p->stub && ref_p->stub != die)
9282 gcc_assert (a->dw_attr != DW_AT_signature);
9283 change_AT_die_ref (a, ref_p->stub);
9285 else
9286 /* We aren't changing this reference, so mark it external. */
9287 set_AT_ref_external (a, 1);
9290 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9292 dw_attr_node *die_a, *abbrev_a;
9293 unsigned ix;
9294 bool ok = true;
9296 if (abbrev_id == 0)
9297 continue;
9298 if (abbrev->die_tag != die->die_tag)
9299 continue;
9300 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9301 continue;
9303 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9304 continue;
9306 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9308 abbrev_a = &(*abbrev->die_attr)[ix];
9309 if ((abbrev_a->dw_attr != die_a->dw_attr)
9310 || (value_format (abbrev_a) != value_format (die_a)))
9312 ok = false;
9313 break;
9316 if (ok)
9317 break;
9320 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9322 vec_safe_push (abbrev_die_table, die);
9323 if (abbrev_opt_start)
9324 abbrev_usage_count.safe_push (0);
9326 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9328 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9329 sorted_abbrev_dies.safe_push (die);
9332 die->die_abbrev = abbrev_id;
9333 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9336 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9337 by die_abbrev's usage count, from the most commonly used
9338 abbreviation to the least. */
9340 static int
9341 die_abbrev_cmp (const void *p1, const void *p2)
9343 dw_die_ref die1 = *(const dw_die_ref *) p1;
9344 dw_die_ref die2 = *(const dw_die_ref *) p2;
9346 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9347 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9349 if (die1->die_abbrev >= abbrev_opt_base_type_end
9350 && die2->die_abbrev >= abbrev_opt_base_type_end)
9352 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9353 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9354 return -1;
9355 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9356 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9357 return 1;
9360 /* Stabilize the sort. */
9361 if (die1->die_abbrev < die2->die_abbrev)
9362 return -1;
9363 if (die1->die_abbrev > die2->die_abbrev)
9364 return 1;
9366 return 0;
9369 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9370 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9371 into dw_val_class_const_implicit or
9372 dw_val_class_unsigned_const_implicit. */
9374 static void
9375 optimize_implicit_const (unsigned int first_id, unsigned int end,
9376 vec<bool> &implicit_consts)
9378 /* It never makes sense if there is just one DIE using the abbreviation. */
9379 if (end < first_id + 2)
9380 return;
9382 dw_attr_node *a;
9383 unsigned ix, i;
9384 dw_die_ref die = sorted_abbrev_dies[first_id];
9385 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9386 if (implicit_consts[ix])
9388 enum dw_val_class new_class = dw_val_class_none;
9389 switch (AT_class (a))
9391 case dw_val_class_unsigned_const:
9392 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9393 continue;
9395 /* The .debug_abbrev section will grow by
9396 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9397 in all the DIEs using that abbreviation. */
9398 if (constant_size (AT_unsigned (a)) * (end - first_id)
9399 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9400 continue;
9402 new_class = dw_val_class_unsigned_const_implicit;
9403 break;
9405 case dw_val_class_const:
9406 new_class = dw_val_class_const_implicit;
9407 break;
9409 case dw_val_class_file:
9410 new_class = dw_val_class_file_implicit;
9411 break;
9413 default:
9414 continue;
9416 for (i = first_id; i < end; i++)
9417 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9418 = new_class;
9422 /* Attempt to optimize abbreviation table from abbrev_opt_start
9423 abbreviation above. */
9425 static void
9426 optimize_abbrev_table (void)
9428 if (abbrev_opt_start
9429 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9430 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9432 auto_vec<bool, 32> implicit_consts;
9433 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9435 unsigned int abbrev_id = abbrev_opt_start - 1;
9436 unsigned int first_id = ~0U;
9437 unsigned int last_abbrev_id = 0;
9438 unsigned int i;
9439 dw_die_ref die;
9440 if (abbrev_opt_base_type_end > abbrev_opt_start)
9441 abbrev_id = abbrev_opt_base_type_end - 1;
9442 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9443 most commonly used abbreviations come first. */
9444 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9446 dw_attr_node *a;
9447 unsigned ix;
9449 /* If calc_base_type_die_sizes has been called, the CU and
9450 base types after it can't be optimized, because we've already
9451 calculated their DIE offsets. We've sorted them first. */
9452 if (die->die_abbrev < abbrev_opt_base_type_end)
9453 continue;
9454 if (die->die_abbrev != last_abbrev_id)
9456 last_abbrev_id = die->die_abbrev;
9457 if (dwarf_version >= 5 && first_id != ~0U)
9458 optimize_implicit_const (first_id, i, implicit_consts);
9459 abbrev_id++;
9460 (*abbrev_die_table)[abbrev_id] = die;
9461 if (dwarf_version >= 5)
9463 first_id = i;
9464 implicit_consts.truncate (0);
9466 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9467 switch (AT_class (a))
9469 case dw_val_class_const:
9470 case dw_val_class_unsigned_const:
9471 case dw_val_class_file:
9472 implicit_consts.safe_push (true);
9473 break;
9474 default:
9475 implicit_consts.safe_push (false);
9476 break;
9480 else if (dwarf_version >= 5)
9482 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9483 if (!implicit_consts[ix])
9484 continue;
9485 else
9487 dw_attr_node *other_a
9488 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9489 if (!dw_val_equal_p (&a->dw_attr_val,
9490 &other_a->dw_attr_val))
9491 implicit_consts[ix] = false;
9494 die->die_abbrev = abbrev_id;
9496 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9497 if (dwarf_version >= 5 && first_id != ~0U)
9498 optimize_implicit_const (first_id, i, implicit_consts);
9501 abbrev_opt_start = 0;
9502 abbrev_opt_base_type_end = 0;
9503 abbrev_usage_count.release ();
9504 sorted_abbrev_dies.release ();
9507 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9509 static int
9510 constant_size (unsigned HOST_WIDE_INT value)
9512 int log;
9514 if (value == 0)
9515 log = 0;
9516 else
9517 log = floor_log2 (value);
9519 log = log / 8;
9520 log = 1 << (floor_log2 (log) + 1);
9522 return log;
9525 /* Return the size of a DIE as it is represented in the
9526 .debug_info section. */
9528 static unsigned long
9529 size_of_die (dw_die_ref die)
9531 unsigned long size = 0;
9532 dw_attr_node *a;
9533 unsigned ix;
9534 enum dwarf_form form;
9536 size += size_of_uleb128 (die->die_abbrev);
9537 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9539 switch (AT_class (a))
9541 case dw_val_class_addr:
9542 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9544 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9545 size += size_of_uleb128 (AT_index (a));
9547 else
9548 size += DWARF2_ADDR_SIZE;
9549 break;
9550 case dw_val_class_offset:
9551 size += dwarf_offset_size;
9552 break;
9553 case dw_val_class_loc:
9555 unsigned long lsize = size_of_locs (AT_loc (a));
9557 /* Block length. */
9558 if (dwarf_version >= 4)
9559 size += size_of_uleb128 (lsize);
9560 else
9561 size += constant_size (lsize);
9562 size += lsize;
9564 break;
9565 case dw_val_class_loc_list:
9566 if (dwarf_split_debug_info && dwarf_version >= 5)
9568 gcc_assert (AT_loc_list (a)->num_assigned);
9569 size += size_of_uleb128 (AT_loc_list (a)->hash);
9571 else
9572 size += dwarf_offset_size;
9573 break;
9574 case dw_val_class_view_list:
9575 size += dwarf_offset_size;
9576 break;
9577 case dw_val_class_range_list:
9578 if (value_format (a) == DW_FORM_rnglistx)
9580 gcc_assert (rnglist_idx);
9581 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9582 size += size_of_uleb128 (r->idx);
9584 else
9585 size += dwarf_offset_size;
9586 break;
9587 case dw_val_class_const:
9588 size += size_of_sleb128 (AT_int (a));
9589 break;
9590 case dw_val_class_unsigned_const:
9592 int csize = constant_size (AT_unsigned (a));
9593 if (dwarf_version == 3
9594 && a->dw_attr == DW_AT_data_member_location
9595 && csize >= 4)
9596 size += size_of_uleb128 (AT_unsigned (a));
9597 else
9598 size += csize;
9600 break;
9601 case dw_val_class_symview:
9602 if (symview_upper_bound <= 0xff)
9603 size += 1;
9604 else if (symview_upper_bound <= 0xffff)
9605 size += 2;
9606 else if (symview_upper_bound <= 0xffffffff)
9607 size += 4;
9608 else
9609 size += 8;
9610 break;
9611 case dw_val_class_const_implicit:
9612 case dw_val_class_unsigned_const_implicit:
9613 case dw_val_class_file_implicit:
9614 /* These occupy no size in the DIE, just an extra sleb128 in
9615 .debug_abbrev. */
9616 break;
9617 case dw_val_class_const_double:
9618 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9619 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9620 size++; /* block */
9621 break;
9622 case dw_val_class_wide_int:
9623 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9624 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9625 if (get_full_len (*a->dw_attr_val.v.val_wide)
9626 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9627 size++; /* block */
9628 break;
9629 case dw_val_class_vec:
9630 size += constant_size (a->dw_attr_val.v.val_vec.length
9631 * a->dw_attr_val.v.val_vec.elt_size)
9632 + a->dw_attr_val.v.val_vec.length
9633 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9634 break;
9635 case dw_val_class_flag:
9636 if (dwarf_version >= 4)
9637 /* Currently all add_AT_flag calls pass in 1 as last argument,
9638 so DW_FORM_flag_present can be used. If that ever changes,
9639 we'll need to use DW_FORM_flag and have some optimization
9640 in build_abbrev_table that will change those to
9641 DW_FORM_flag_present if it is set to 1 in all DIEs using
9642 the same abbrev entry. */
9643 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9644 else
9645 size += 1;
9646 break;
9647 case dw_val_class_die_ref:
9648 if (AT_ref_external (a))
9650 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9651 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9652 is sized by target address length, whereas in DWARF3
9653 it's always sized as an offset. */
9654 if (AT_ref (a)->comdat_type_p)
9655 size += DWARF_TYPE_SIGNATURE_SIZE;
9656 else if (dwarf_version == 2)
9657 size += DWARF2_ADDR_SIZE;
9658 else
9659 size += dwarf_offset_size;
9661 else
9662 size += dwarf_offset_size;
9663 break;
9664 case dw_val_class_fde_ref:
9665 size += dwarf_offset_size;
9666 break;
9667 case dw_val_class_lbl_id:
9668 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9670 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9671 size += size_of_uleb128 (AT_index (a));
9673 else
9674 size += DWARF2_ADDR_SIZE;
9675 break;
9676 case dw_val_class_lineptr:
9677 case dw_val_class_macptr:
9678 case dw_val_class_loclistsptr:
9679 size += dwarf_offset_size;
9680 break;
9681 case dw_val_class_str:
9682 form = AT_string_form (a);
9683 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9684 size += dwarf_offset_size;
9685 else if (form == dwarf_FORM (DW_FORM_strx))
9686 size += size_of_uleb128 (AT_index (a));
9687 else
9688 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9689 break;
9690 case dw_val_class_file:
9691 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9692 break;
9693 case dw_val_class_data8:
9694 size += 8;
9695 break;
9696 case dw_val_class_vms_delta:
9697 size += dwarf_offset_size;
9698 break;
9699 case dw_val_class_high_pc:
9700 size += DWARF2_ADDR_SIZE;
9701 break;
9702 case dw_val_class_discr_value:
9703 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9704 break;
9705 case dw_val_class_discr_list:
9707 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9709 /* This is a block, so we have the block length and then its
9710 data. */
9711 size += constant_size (block_size) + block_size;
9713 break;
9714 default:
9715 gcc_unreachable ();
9719 return size;
9722 /* Size the debugging information associated with a given DIE. Visits the
9723 DIE's children recursively. Updates the global variable next_die_offset, on
9724 each time through. Uses the current value of next_die_offset to update the
9725 die_offset field in each DIE. */
9727 static void
9728 calc_die_sizes (dw_die_ref die)
9730 dw_die_ref c;
9732 gcc_assert (die->die_offset == 0
9733 || (unsigned long int) die->die_offset == next_die_offset);
9734 die->die_offset = next_die_offset;
9735 next_die_offset += size_of_die (die);
9737 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9739 if (die->die_child != NULL)
9740 /* Count the null byte used to terminate sibling lists. */
9741 next_die_offset += 1;
9744 /* Size just the base type children at the start of the CU.
9745 This is needed because build_abbrev needs to size locs
9746 and sizing of type based stack ops needs to know die_offset
9747 values for the base types. */
9749 static void
9750 calc_base_type_die_sizes (void)
9752 unsigned long die_offset = (dwarf_split_debug_info
9753 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9754 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9755 unsigned int i;
9756 dw_die_ref base_type;
9757 #if ENABLE_ASSERT_CHECKING
9758 dw_die_ref prev = comp_unit_die ()->die_child;
9759 #endif
9761 die_offset += size_of_die (comp_unit_die ());
9762 for (i = 0; base_types.iterate (i, &base_type); i++)
9764 #if ENABLE_ASSERT_CHECKING
9765 gcc_assert (base_type->die_offset == 0
9766 && prev->die_sib == base_type
9767 && base_type->die_child == NULL
9768 && base_type->die_abbrev);
9769 prev = base_type;
9770 #endif
9771 if (abbrev_opt_start
9772 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9773 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9774 base_type->die_offset = die_offset;
9775 die_offset += size_of_die (base_type);
9779 /* Set the marks for a die and its children. We do this so
9780 that we know whether or not a reference needs to use FORM_ref_addr; only
9781 DIEs in the same CU will be marked. We used to clear out the offset
9782 and use that as the flag, but ran into ordering problems. */
9784 static void
9785 mark_dies (dw_die_ref die)
9787 dw_die_ref c;
9789 gcc_assert (!die->die_mark);
9791 die->die_mark = 1;
9792 FOR_EACH_CHILD (die, c, mark_dies (c));
9795 /* Clear the marks for a die and its children. */
9797 static void
9798 unmark_dies (dw_die_ref die)
9800 dw_die_ref c;
9802 if (! use_debug_types)
9803 gcc_assert (die->die_mark);
9805 die->die_mark = 0;
9806 FOR_EACH_CHILD (die, c, unmark_dies (c));
9809 /* Clear the marks for a die, its children and referred dies. */
9811 static void
9812 unmark_all_dies (dw_die_ref die)
9814 dw_die_ref c;
9815 dw_attr_node *a;
9816 unsigned ix;
9818 if (!die->die_mark)
9819 return;
9820 die->die_mark = 0;
9822 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9824 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9825 if (AT_class (a) == dw_val_class_die_ref)
9826 unmark_all_dies (AT_ref (a));
9829 /* Calculate if the entry should appear in the final output file. It may be
9830 from a pruned a type. */
9832 static bool
9833 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9835 /* By limiting gnu pubnames to definitions only, gold can generate a
9836 gdb index without entries for declarations, which don't include
9837 enough information to be useful. */
9838 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9839 return false;
9841 if (table == pubname_table)
9843 /* Enumerator names are part of the pubname table, but the
9844 parent DW_TAG_enumeration_type die may have been pruned.
9845 Don't output them if that is the case. */
9846 if (p->die->die_tag == DW_TAG_enumerator &&
9847 (p->die->die_parent == NULL
9848 || !p->die->die_parent->die_perennial_p))
9849 return false;
9851 /* Everything else in the pubname table is included. */
9852 return true;
9855 /* The pubtypes table shouldn't include types that have been
9856 pruned. */
9857 return (p->die->die_offset != 0
9858 || !flag_eliminate_unused_debug_types);
9861 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9862 generated for the compilation unit. */
9864 static unsigned long
9865 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9867 unsigned long size;
9868 unsigned i;
9869 pubname_entry *p;
9870 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9872 size = DWARF_PUBNAMES_HEADER_SIZE;
9873 FOR_EACH_VEC_ELT (*names, i, p)
9874 if (include_pubname_in_output (names, p))
9875 size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9877 size += dwarf_offset_size;
9878 return size;
9881 /* Return the size of the information in the .debug_aranges section. */
9883 static unsigned long
9884 size_of_aranges (void)
9886 unsigned long size;
9888 size = DWARF_ARANGES_HEADER_SIZE;
9890 /* Count the address/length pair for this compilation unit. */
9891 if (switch_text_ranges)
9892 size += 2 * DWARF2_ADDR_SIZE
9893 * (vec_safe_length (switch_text_ranges) / 2 + 1);
9894 if (switch_cold_ranges)
9895 size += 2 * DWARF2_ADDR_SIZE
9896 * (vec_safe_length (switch_cold_ranges) / 2 + 1);
9897 if (have_multiple_function_sections)
9899 unsigned fde_idx;
9900 dw_fde_ref fde;
9902 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9904 if (fde->ignored_debug)
9905 continue;
9906 if (!fde->in_std_section)
9907 size += 2 * DWARF2_ADDR_SIZE;
9908 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9909 size += 2 * DWARF2_ADDR_SIZE;
9913 /* Count the two zero words used to terminated the address range table. */
9914 size += 2 * DWARF2_ADDR_SIZE;
9915 return size;
9918 /* Select the encoding of an attribute value. */
9920 static enum dwarf_form
9921 value_format (dw_attr_node *a)
9923 switch (AT_class (a))
9925 case dw_val_class_addr:
9926 /* Only very few attributes allow DW_FORM_addr. */
9927 switch (a->dw_attr)
9929 case DW_AT_low_pc:
9930 case DW_AT_high_pc:
9931 case DW_AT_entry_pc:
9932 case DW_AT_trampoline:
9933 return (AT_index (a) == NOT_INDEXED
9934 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9935 default:
9936 break;
9938 switch (DWARF2_ADDR_SIZE)
9940 case 1:
9941 return DW_FORM_data1;
9942 case 2:
9943 return DW_FORM_data2;
9944 case 4:
9945 return DW_FORM_data4;
9946 case 8:
9947 return DW_FORM_data8;
9948 default:
9949 gcc_unreachable ();
9951 case dw_val_class_loc_list:
9952 if (dwarf_split_debug_info
9953 && dwarf_version >= 5
9954 && AT_loc_list (a)->num_assigned)
9955 return DW_FORM_loclistx;
9956 /* FALLTHRU */
9957 case dw_val_class_view_list:
9958 case dw_val_class_range_list:
9959 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9960 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9961 care about sizes of .debug* sections in shared libraries and
9962 executables and don't take into account relocations that affect just
9963 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9964 table in the .debug_rnglists section. */
9965 if (dwarf_split_debug_info
9966 && dwarf_version >= 5
9967 && AT_class (a) == dw_val_class_range_list
9968 && rnglist_idx
9969 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9970 return DW_FORM_rnglistx;
9971 if (dwarf_version >= 4)
9972 return DW_FORM_sec_offset;
9973 /* FALLTHRU */
9974 case dw_val_class_vms_delta:
9975 case dw_val_class_offset:
9976 switch (dwarf_offset_size)
9978 case 4:
9979 return DW_FORM_data4;
9980 case 8:
9981 return DW_FORM_data8;
9982 default:
9983 gcc_unreachable ();
9985 case dw_val_class_loc:
9986 if (dwarf_version >= 4)
9987 return DW_FORM_exprloc;
9988 switch (constant_size (size_of_locs (AT_loc (a))))
9990 case 1:
9991 return DW_FORM_block1;
9992 case 2:
9993 return DW_FORM_block2;
9994 case 4:
9995 return DW_FORM_block4;
9996 default:
9997 gcc_unreachable ();
9999 case dw_val_class_const:
10000 return DW_FORM_sdata;
10001 case dw_val_class_unsigned_const:
10002 switch (constant_size (AT_unsigned (a)))
10004 case 1:
10005 return DW_FORM_data1;
10006 case 2:
10007 return DW_FORM_data2;
10008 case 4:
10009 /* In DWARF3 DW_AT_data_member_location with
10010 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
10011 constant, so we need to use DW_FORM_udata if we need
10012 a large constant. */
10013 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
10014 return DW_FORM_udata;
10015 return DW_FORM_data4;
10016 case 8:
10017 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
10018 return DW_FORM_udata;
10019 return DW_FORM_data8;
10020 default:
10021 gcc_unreachable ();
10023 case dw_val_class_const_implicit:
10024 case dw_val_class_unsigned_const_implicit:
10025 case dw_val_class_file_implicit:
10026 return DW_FORM_implicit_const;
10027 case dw_val_class_const_double:
10028 switch (HOST_BITS_PER_WIDE_INT)
10030 case 8:
10031 return DW_FORM_data2;
10032 case 16:
10033 return DW_FORM_data4;
10034 case 32:
10035 return DW_FORM_data8;
10036 case 64:
10037 if (dwarf_version >= 5)
10038 return DW_FORM_data16;
10039 /* FALLTHRU */
10040 default:
10041 return DW_FORM_block1;
10043 case dw_val_class_wide_int:
10044 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
10046 case 8:
10047 return DW_FORM_data1;
10048 case 16:
10049 return DW_FORM_data2;
10050 case 32:
10051 return DW_FORM_data4;
10052 case 64:
10053 return DW_FORM_data8;
10054 case 128:
10055 if (dwarf_version >= 5)
10056 return DW_FORM_data16;
10057 /* FALLTHRU */
10058 default:
10059 return DW_FORM_block1;
10061 case dw_val_class_symview:
10062 /* ??? We might use uleb128, but then we'd have to compute
10063 .debug_info offsets in the assembler. */
10064 if (symview_upper_bound <= 0xff)
10065 return DW_FORM_data1;
10066 else if (symview_upper_bound <= 0xffff)
10067 return DW_FORM_data2;
10068 else if (symview_upper_bound <= 0xffffffff)
10069 return DW_FORM_data4;
10070 else
10071 return DW_FORM_data8;
10072 case dw_val_class_vec:
10073 switch (constant_size (a->dw_attr_val.v.val_vec.length
10074 * a->dw_attr_val.v.val_vec.elt_size))
10076 case 1:
10077 return DW_FORM_block1;
10078 case 2:
10079 return DW_FORM_block2;
10080 case 4:
10081 return DW_FORM_block4;
10082 default:
10083 gcc_unreachable ();
10085 case dw_val_class_flag:
10086 if (dwarf_version >= 4)
10088 /* Currently all add_AT_flag calls pass in 1 as last argument,
10089 so DW_FORM_flag_present can be used. If that ever changes,
10090 we'll need to use DW_FORM_flag and have some optimization
10091 in build_abbrev_table that will change those to
10092 DW_FORM_flag_present if it is set to 1 in all DIEs using
10093 the same abbrev entry. */
10094 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10095 return DW_FORM_flag_present;
10097 return DW_FORM_flag;
10098 case dw_val_class_die_ref:
10099 if (AT_ref_external (a))
10101 if (AT_ref (a)->comdat_type_p)
10102 return DW_FORM_ref_sig8;
10103 else
10104 return DW_FORM_ref_addr;
10106 else
10107 return DW_FORM_ref;
10108 case dw_val_class_fde_ref:
10109 return DW_FORM_data;
10110 case dw_val_class_lbl_id:
10111 return (AT_index (a) == NOT_INDEXED
10112 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
10113 case dw_val_class_lineptr:
10114 case dw_val_class_macptr:
10115 case dw_val_class_loclistsptr:
10116 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10117 case dw_val_class_str:
10118 return AT_string_form (a);
10119 case dw_val_class_file:
10120 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10122 case 1:
10123 return DW_FORM_data1;
10124 case 2:
10125 return DW_FORM_data2;
10126 case 4:
10127 return DW_FORM_data4;
10128 default:
10129 gcc_unreachable ();
10132 case dw_val_class_data8:
10133 return DW_FORM_data8;
10135 case dw_val_class_high_pc:
10136 switch (DWARF2_ADDR_SIZE)
10138 case 1:
10139 return DW_FORM_data1;
10140 case 2:
10141 return DW_FORM_data2;
10142 case 4:
10143 return DW_FORM_data4;
10144 case 8:
10145 return DW_FORM_data8;
10146 default:
10147 gcc_unreachable ();
10150 case dw_val_class_discr_value:
10151 return (a->dw_attr_val.v.val_discr_value.pos
10152 ? DW_FORM_udata
10153 : DW_FORM_sdata);
10154 case dw_val_class_discr_list:
10155 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10157 case 1:
10158 return DW_FORM_block1;
10159 case 2:
10160 return DW_FORM_block2;
10161 case 4:
10162 return DW_FORM_block4;
10163 default:
10164 gcc_unreachable ();
10167 default:
10168 gcc_unreachable ();
10172 /* Output the encoding of an attribute value. */
10174 static void
10175 output_value_format (dw_attr_node *a)
10177 enum dwarf_form form = value_format (a);
10179 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10182 /* Given a die and id, produce the appropriate abbreviations. */
10184 static void
10185 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10187 unsigned ix;
10188 dw_attr_node *a_attr;
10190 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10191 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10192 dwarf_tag_name (abbrev->die_tag));
10194 if (abbrev->die_child != NULL)
10195 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10196 else
10197 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10199 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10201 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10202 dwarf_attr_name (a_attr->dw_attr));
10203 output_value_format (a_attr);
10204 if (value_format (a_attr) == DW_FORM_implicit_const)
10206 if (AT_class (a_attr) == dw_val_class_file_implicit)
10208 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10209 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10210 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10212 else
10213 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10217 dw2_asm_output_data (1, 0, NULL);
10218 dw2_asm_output_data (1, 0, NULL);
10222 /* Output the .debug_abbrev section which defines the DIE abbreviation
10223 table. */
10225 static void
10226 output_abbrev_section (void)
10228 unsigned int abbrev_id;
10229 dw_die_ref abbrev;
10231 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10232 if (abbrev_id != 0)
10233 output_die_abbrevs (abbrev_id, abbrev);
10235 /* Terminate the table. */
10236 dw2_asm_output_data (1, 0, NULL);
10239 /* Return a new location list, given the begin and end range, and the
10240 expression. */
10242 static inline dw_loc_list_ref
10243 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10244 const char *end, var_loc_view vend,
10245 const char *section)
10247 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10249 retlist->begin = begin;
10250 retlist->begin_entry = NULL;
10251 retlist->end = end;
10252 retlist->end_entry = NULL;
10253 retlist->expr = expr;
10254 retlist->section = section;
10255 retlist->vbegin = vbegin;
10256 retlist->vend = vend;
10258 return retlist;
10261 /* Return true iff there's any nonzero view number in the loc list.
10263 ??? When views are not enabled, we'll often extend a single range
10264 to the entire function, so that we emit a single location
10265 expression rather than a location list. With views, even with a
10266 single range, we'll output a list if start or end have a nonzero
10267 view. If we change this, we may want to stop splitting a single
10268 range in dw_loc_list just because of a nonzero view, even if it
10269 straddles across hot/cold partitions. */
10271 static bool
10272 loc_list_has_views (dw_loc_list_ref list)
10274 if (!debug_variable_location_views)
10275 return false;
10277 for (dw_loc_list_ref loc = list;
10278 loc != NULL; loc = loc->dw_loc_next)
10279 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10280 return true;
10282 return false;
10285 /* Generate a new internal symbol for this location list node, if it
10286 hasn't got one yet. */
10288 static inline void
10289 gen_llsym (dw_loc_list_ref list)
10291 gcc_assert (!list->ll_symbol);
10292 list->ll_symbol = gen_internal_sym ("LLST");
10294 if (!loc_list_has_views (list))
10295 return;
10297 if (dwarf2out_locviews_in_attribute ())
10299 /* Use the same label_num for the view list. */
10300 label_num--;
10301 list->vl_symbol = gen_internal_sym ("LVUS");
10303 else
10304 list->vl_symbol = list->ll_symbol;
10307 /* Generate a symbol for the list, but only if we really want to emit
10308 it as a list. */
10310 static inline void
10311 maybe_gen_llsym (dw_loc_list_ref list)
10313 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10314 return;
10316 gen_llsym (list);
10319 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10320 NULL, don't consider size of the location expression. If we're not
10321 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10322 representation in *SIZEP. */
10324 static bool
10325 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10327 /* Don't output an entry that starts and ends at the same address. */
10328 if (strcmp (curr->begin, curr->end) == 0
10329 && curr->vbegin == curr->vend && !curr->force)
10330 return true;
10332 if (!sizep)
10333 return false;
10335 unsigned long size = size_of_locs (curr->expr);
10337 /* If the expression is too large, drop it on the floor. We could
10338 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10339 in the expression, but >= 64KB expressions for a single value
10340 in a single range are unlikely very useful. */
10341 if (dwarf_version < 5 && size > 0xffff)
10342 return true;
10344 *sizep = size;
10346 return false;
10349 /* Output a view pair loclist entry for CURR, if it requires one. */
10351 static void
10352 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10354 if (!dwarf2out_locviews_in_loclist ())
10355 return;
10357 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10358 return;
10360 #ifdef DW_LLE_view_pair
10361 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10363 if (dwarf2out_as_locview_support)
10365 if (ZERO_VIEW_P (curr->vbegin))
10366 dw2_asm_output_data_uleb128 (0, "Location view begin");
10367 else
10369 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10370 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10371 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10374 if (ZERO_VIEW_P (curr->vend))
10375 dw2_asm_output_data_uleb128 (0, "Location view end");
10376 else
10378 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10379 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10380 dw2_asm_output_symname_uleb128 (label, "Location view end");
10383 else
10385 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10386 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10388 #endif /* DW_LLE_view_pair */
10390 return;
10393 /* Output the location list given to us. */
10395 static void
10396 output_loc_list (dw_loc_list_ref list_head)
10398 int vcount = 0, lcount = 0;
10400 if (list_head->emitted)
10401 return;
10402 list_head->emitted = true;
10404 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10406 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10408 for (dw_loc_list_ref curr = list_head; curr != NULL;
10409 curr = curr->dw_loc_next)
10411 unsigned long size;
10413 if (skip_loc_list_entry (curr, &size))
10414 continue;
10416 vcount++;
10418 /* ?? dwarf_split_debug_info? */
10419 if (dwarf2out_as_locview_support)
10421 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10423 if (!ZERO_VIEW_P (curr->vbegin))
10425 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10426 dw2_asm_output_symname_uleb128 (label,
10427 "View list begin (%s)",
10428 list_head->vl_symbol);
10430 else
10431 dw2_asm_output_data_uleb128 (0,
10432 "View list begin (%s)",
10433 list_head->vl_symbol);
10435 if (!ZERO_VIEW_P (curr->vend))
10437 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10438 dw2_asm_output_symname_uleb128 (label,
10439 "View list end (%s)",
10440 list_head->vl_symbol);
10442 else
10443 dw2_asm_output_data_uleb128 (0,
10444 "View list end (%s)",
10445 list_head->vl_symbol);
10447 else
10449 dw2_asm_output_data_uleb128 (curr->vbegin,
10450 "View list begin (%s)",
10451 list_head->vl_symbol);
10452 dw2_asm_output_data_uleb128 (curr->vend,
10453 "View list end (%s)",
10454 list_head->vl_symbol);
10459 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10461 const char *last_section = NULL;
10462 const char *base_label = NULL;
10464 /* Walk the location list, and output each range + expression. */
10465 for (dw_loc_list_ref curr = list_head; curr != NULL;
10466 curr = curr->dw_loc_next)
10468 unsigned long size;
10470 /* Skip this entry? If we skip it here, we must skip it in the
10471 view list above as well. */
10472 if (skip_loc_list_entry (curr, &size))
10473 continue;
10475 lcount++;
10477 if (dwarf_version >= 5)
10479 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10481 dwarf2out_maybe_output_loclist_view_pair (curr);
10482 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10483 uleb128 index into .debug_addr and uleb128 length. */
10484 dw2_asm_output_data (1, DW_LLE_startx_length,
10485 "DW_LLE_startx_length (%s)",
10486 list_head->ll_symbol);
10487 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10488 "Location list range start index "
10489 "(%s)", curr->begin);
10490 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10491 "Location list length (%s)",
10492 list_head->ll_symbol);
10494 else if (dwarf_split_debug_info)
10496 dwarf2out_maybe_output_loclist_view_pair (curr);
10497 /* For -gsplit-dwarf without usable .uleb128 support, emit
10498 DW_LLE_startx_endx, which has two uleb128 indexes into
10499 .debug_addr. */
10500 dw2_asm_output_data (1, DW_LLE_startx_endx,
10501 "DW_LLE_startx_endx (%s)",
10502 list_head->ll_symbol);
10503 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10504 "Location list range start index "
10505 "(%s)", curr->begin);
10506 dw2_asm_output_data_uleb128 (curr->end_entry->index,
10507 "Location list range end index "
10508 "(%s)", curr->end);
10510 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10512 dwarf2out_maybe_output_loclist_view_pair (curr);
10513 /* If all code is in .text section, the base address is
10514 already provided by the CU attributes. Use
10515 DW_LLE_offset_pair where both addresses are uleb128 encoded
10516 offsets against that base. */
10517 dw2_asm_output_data (1, DW_LLE_offset_pair,
10518 "DW_LLE_offset_pair (%s)",
10519 list_head->ll_symbol);
10520 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10521 "Location list begin address (%s)",
10522 list_head->ll_symbol);
10523 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10524 "Location list end address (%s)",
10525 list_head->ll_symbol);
10527 else if (HAVE_AS_LEB128)
10529 /* Otherwise, find out how many consecutive entries could share
10530 the same base entry. If just one, emit DW_LLE_start_length,
10531 otherwise emit DW_LLE_base_address for the base address
10532 followed by a series of DW_LLE_offset_pair. */
10533 if (last_section == NULL || curr->section != last_section)
10535 dw_loc_list_ref curr2;
10536 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10537 curr2 = curr2->dw_loc_next)
10539 if (strcmp (curr2->begin, curr2->end) == 0
10540 && !curr2->force)
10541 continue;
10542 break;
10544 if (curr2 == NULL || curr->section != curr2->section)
10545 last_section = NULL;
10546 else
10548 last_section = curr->section;
10549 base_label = curr->begin;
10550 dw2_asm_output_data (1, DW_LLE_base_address,
10551 "DW_LLE_base_address (%s)",
10552 list_head->ll_symbol);
10553 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10554 "Base address (%s)",
10555 list_head->ll_symbol);
10558 /* Only one entry with the same base address. Use
10559 DW_LLE_start_length with absolute address and uleb128
10560 length. */
10561 if (last_section == NULL)
10563 dwarf2out_maybe_output_loclist_view_pair (curr);
10564 dw2_asm_output_data (1, DW_LLE_start_length,
10565 "DW_LLE_start_length (%s)",
10566 list_head->ll_symbol);
10567 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10568 "Location list begin address (%s)",
10569 list_head->ll_symbol);
10570 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10571 "Location list length "
10572 "(%s)", list_head->ll_symbol);
10574 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10575 DW_LLE_base_address. */
10576 else
10578 dwarf2out_maybe_output_loclist_view_pair (curr);
10579 dw2_asm_output_data (1, DW_LLE_offset_pair,
10580 "DW_LLE_offset_pair (%s)",
10581 list_head->ll_symbol);
10582 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10583 "Location list begin address "
10584 "(%s)", list_head->ll_symbol);
10585 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10586 "Location list end address "
10587 "(%s)", list_head->ll_symbol);
10590 /* The assembler does not support .uleb128 directive. Emit
10591 DW_LLE_start_end with a pair of absolute addresses. */
10592 else
10594 dwarf2out_maybe_output_loclist_view_pair (curr);
10595 dw2_asm_output_data (1, DW_LLE_start_end,
10596 "DW_LLE_start_end (%s)",
10597 list_head->ll_symbol);
10598 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10599 "Location list begin address (%s)",
10600 list_head->ll_symbol);
10601 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10602 "Location list end address (%s)",
10603 list_head->ll_symbol);
10606 else if (dwarf_split_debug_info)
10608 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10609 and 4 byte length. */
10610 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10611 "Location list start/length entry (%s)",
10612 list_head->ll_symbol);
10613 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10614 "Location list range start index (%s)",
10615 curr->begin);
10616 /* The length field is 4 bytes. If we ever need to support
10617 an 8-byte length, we can add a new DW_LLE code or fall back
10618 to DW_LLE_GNU_start_end_entry. */
10619 dw2_asm_output_delta (4, curr->end, curr->begin,
10620 "Location list range length (%s)",
10621 list_head->ll_symbol);
10623 else if (!have_multiple_function_sections)
10625 /* Pair of relative addresses against start of text section. */
10626 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10627 "Location list begin address (%s)",
10628 list_head->ll_symbol);
10629 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10630 "Location list end address (%s)",
10631 list_head->ll_symbol);
10633 else
10635 /* Pair of absolute addresses. */
10636 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10637 "Location list begin address (%s)",
10638 list_head->ll_symbol);
10639 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10640 "Location list end address (%s)",
10641 list_head->ll_symbol);
10644 /* Output the block length for this list of location operations. */
10645 if (dwarf_version >= 5)
10646 dw2_asm_output_data_uleb128 (size, "Location expression size");
10647 else
10649 gcc_assert (size <= 0xffff);
10650 dw2_asm_output_data (2, size, "Location expression size");
10653 output_loc_sequence (curr->expr, -1);
10656 /* And finally list termination. */
10657 if (dwarf_version >= 5)
10658 dw2_asm_output_data (1, DW_LLE_end_of_list,
10659 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10660 else if (dwarf_split_debug_info)
10661 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10662 "Location list terminator (%s)",
10663 list_head->ll_symbol);
10664 else
10666 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10667 "Location list terminator begin (%s)",
10668 list_head->ll_symbol);
10669 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10670 "Location list terminator end (%s)",
10671 list_head->ll_symbol);
10674 gcc_assert (!list_head->vl_symbol
10675 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10678 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10679 section. Emit a relocated reference if val_entry is NULL, otherwise,
10680 emit an indirect reference. */
10682 static void
10683 output_range_list_offset (dw_attr_node *a)
10685 const char *name = dwarf_attr_name (a->dw_attr);
10687 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10689 if (dwarf_version >= 5)
10691 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10692 dw2_asm_output_offset (dwarf_offset_size, r->label,
10693 debug_ranges_section, "%s", name);
10695 else
10697 char *p = strchr (ranges_section_label, '\0');
10698 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10699 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10700 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10701 debug_ranges_section, "%s", name);
10702 *p = '\0';
10705 else if (dwarf_version >= 5)
10707 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10708 gcc_assert (rnglist_idx);
10709 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10711 else
10712 dw2_asm_output_data (dwarf_offset_size,
10713 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10714 "%s (offset from %s)", name, ranges_section_label);
10717 /* Output the offset into the debug_loc section. */
10719 static void
10720 output_loc_list_offset (dw_attr_node *a)
10722 char *sym = AT_loc_list (a)->ll_symbol;
10724 gcc_assert (sym);
10725 if (!dwarf_split_debug_info)
10726 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10727 "%s", dwarf_attr_name (a->dw_attr));
10728 else if (dwarf_version >= 5)
10730 gcc_assert (AT_loc_list (a)->num_assigned);
10731 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10732 dwarf_attr_name (a->dw_attr),
10733 sym);
10735 else
10736 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10737 "%s", dwarf_attr_name (a->dw_attr));
10740 /* Output the offset into the debug_loc section. */
10742 static void
10743 output_view_list_offset (dw_attr_node *a)
10745 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10747 gcc_assert (sym);
10748 if (dwarf_split_debug_info)
10749 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10750 "%s", dwarf_attr_name (a->dw_attr));
10751 else
10752 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10753 "%s", dwarf_attr_name (a->dw_attr));
10756 /* Output an attribute's index or value appropriately. */
10758 static void
10759 output_attr_index_or_value (dw_attr_node *a)
10761 const char *name = dwarf_attr_name (a->dw_attr);
10763 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10765 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10766 return;
10768 switch (AT_class (a))
10770 case dw_val_class_addr:
10771 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10772 break;
10773 case dw_val_class_high_pc:
10774 case dw_val_class_lbl_id:
10775 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10776 break;
10777 default:
10778 gcc_unreachable ();
10782 /* Output a type signature. */
10784 static inline void
10785 output_signature (const char *sig, const char *name)
10787 int i;
10789 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10790 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10793 /* Output a discriminant value. */
10795 static inline void
10796 output_discr_value (dw_discr_value *discr_value, const char *name)
10798 if (discr_value->pos)
10799 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10800 else
10801 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10804 /* Output the DIE and its attributes. Called recursively to generate
10805 the definitions of each child DIE. */
10807 static void
10808 output_die (dw_die_ref die)
10810 dw_attr_node *a;
10811 dw_die_ref c;
10812 unsigned long size;
10813 unsigned ix;
10815 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10816 (unsigned long)die->die_offset,
10817 dwarf_tag_name (die->die_tag));
10819 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10821 const char *name = dwarf_attr_name (a->dw_attr);
10823 switch (AT_class (a))
10825 case dw_val_class_addr:
10826 output_attr_index_or_value (a);
10827 break;
10829 case dw_val_class_offset:
10830 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10831 "%s", name);
10832 break;
10834 case dw_val_class_range_list:
10835 output_range_list_offset (a);
10836 break;
10838 case dw_val_class_loc:
10839 size = size_of_locs (AT_loc (a));
10841 /* Output the block length for this list of location operations. */
10842 if (dwarf_version >= 4)
10843 dw2_asm_output_data_uleb128 (size, "%s", name);
10844 else
10845 dw2_asm_output_data (constant_size (size), size, "%s", name);
10847 output_loc_sequence (AT_loc (a), -1);
10848 break;
10850 case dw_val_class_const:
10851 /* ??? It would be slightly more efficient to use a scheme like is
10852 used for unsigned constants below, but gdb 4.x does not sign
10853 extend. Gdb 5.x does sign extend. */
10854 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10855 break;
10857 case dw_val_class_unsigned_const:
10859 int csize = constant_size (AT_unsigned (a));
10860 if (dwarf_version == 3
10861 && a->dw_attr == DW_AT_data_member_location
10862 && csize >= 4)
10863 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10864 else
10865 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10867 break;
10869 case dw_val_class_symview:
10871 int vsize;
10872 if (symview_upper_bound <= 0xff)
10873 vsize = 1;
10874 else if (symview_upper_bound <= 0xffff)
10875 vsize = 2;
10876 else if (symview_upper_bound <= 0xffffffff)
10877 vsize = 4;
10878 else
10879 vsize = 8;
10880 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10881 "%s", name);
10883 break;
10885 case dw_val_class_const_implicit:
10886 if (flag_debug_asm)
10887 fprintf (asm_out_file, "\t\t\t%s %s ("
10888 HOST_WIDE_INT_PRINT_DEC ")\n",
10889 ASM_COMMENT_START, name, AT_int (a));
10890 break;
10892 case dw_val_class_unsigned_const_implicit:
10893 if (flag_debug_asm)
10894 fprintf (asm_out_file, "\t\t\t%s %s ("
10895 HOST_WIDE_INT_PRINT_HEX ")\n",
10896 ASM_COMMENT_START, name, AT_unsigned (a));
10897 break;
10899 case dw_val_class_const_double:
10901 unsigned HOST_WIDE_INT first, second;
10903 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10904 dw2_asm_output_data (1,
10905 HOST_BITS_PER_DOUBLE_INT
10906 / HOST_BITS_PER_CHAR,
10907 NULL);
10909 if (WORDS_BIG_ENDIAN)
10911 first = a->dw_attr_val.v.val_double.high;
10912 second = a->dw_attr_val.v.val_double.low;
10914 else
10916 first = a->dw_attr_val.v.val_double.low;
10917 second = a->dw_attr_val.v.val_double.high;
10920 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10921 first, "%s", name);
10922 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10923 second, NULL);
10925 break;
10927 case dw_val_class_wide_int:
10929 int i;
10930 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10931 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10932 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10933 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10934 * l, NULL);
10936 if (WORDS_BIG_ENDIAN)
10937 for (i = len - 1; i >= 0; --i)
10939 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10940 "%s", name);
10941 name = "";
10943 else
10944 for (i = 0; i < len; ++i)
10946 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10947 "%s", name);
10948 name = "";
10951 break;
10953 case dw_val_class_vec:
10955 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10956 unsigned int len = a->dw_attr_val.v.val_vec.length;
10957 unsigned int i;
10958 unsigned char *p;
10960 dw2_asm_output_data (constant_size (len * elt_size),
10961 len * elt_size, "%s", name);
10962 if (elt_size > sizeof (HOST_WIDE_INT))
10964 elt_size /= 2;
10965 len *= 2;
10967 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10968 i < len;
10969 i++, p += elt_size)
10970 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10971 "fp or vector constant word %u", i);
10972 break;
10975 case dw_val_class_flag:
10976 if (dwarf_version >= 4)
10978 /* Currently all add_AT_flag calls pass in 1 as last argument,
10979 so DW_FORM_flag_present can be used. If that ever changes,
10980 we'll need to use DW_FORM_flag and have some optimization
10981 in build_abbrev_table that will change those to
10982 DW_FORM_flag_present if it is set to 1 in all DIEs using
10983 the same abbrev entry. */
10984 gcc_assert (AT_flag (a) == 1);
10985 if (flag_debug_asm)
10986 fprintf (asm_out_file, "\t\t\t%s %s\n",
10987 ASM_COMMENT_START, name);
10988 break;
10990 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10991 break;
10993 case dw_val_class_loc_list:
10994 output_loc_list_offset (a);
10995 break;
10997 case dw_val_class_view_list:
10998 output_view_list_offset (a);
10999 break;
11001 case dw_val_class_die_ref:
11002 if (AT_ref_external (a))
11004 if (AT_ref (a)->comdat_type_p)
11006 comdat_type_node *type_node
11007 = AT_ref (a)->die_id.die_type_node;
11009 gcc_assert (type_node);
11010 output_signature (type_node->signature, name);
11012 else
11014 const char *sym = AT_ref (a)->die_id.die_symbol;
11015 int size;
11017 gcc_assert (sym);
11018 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11019 length, whereas in DWARF3 it's always sized as an
11020 offset. */
11021 if (dwarf_version == 2)
11022 size = DWARF2_ADDR_SIZE;
11023 else
11024 size = dwarf_offset_size;
11025 /* ??? We cannot unconditionally output die_offset if
11026 non-zero - others might create references to those
11027 DIEs via symbols.
11028 And we do not clear its DIE offset after outputting it
11029 (and the label refers to the actual DIEs, not the
11030 DWARF CU unit header which is when using label + offset
11031 would be the correct thing to do).
11032 ??? This is the reason for the with_offset flag. */
11033 if (AT_ref (a)->with_offset)
11034 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
11035 debug_info_section, "%s", name);
11036 else
11037 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11038 name);
11041 else
11043 gcc_assert (AT_ref (a)->die_offset);
11044 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
11045 "%s", name);
11047 break;
11049 case dw_val_class_fde_ref:
11051 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11053 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11054 a->dw_attr_val.v.val_fde_index * 2);
11055 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
11056 "%s", name);
11058 break;
11060 case dw_val_class_vms_delta:
11061 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
11062 dw2_asm_output_vms_delta (dwarf_offset_size,
11063 AT_vms_delta2 (a), AT_vms_delta1 (a),
11064 "%s", name);
11065 #else
11066 dw2_asm_output_delta (dwarf_offset_size,
11067 AT_vms_delta2 (a), AT_vms_delta1 (a),
11068 "%s", name);
11069 #endif
11070 break;
11072 case dw_val_class_lbl_id:
11073 output_attr_index_or_value (a);
11074 break;
11076 case dw_val_class_lineptr:
11077 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11078 debug_line_section, "%s", name);
11079 break;
11081 case dw_val_class_macptr:
11082 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11083 debug_macinfo_section, "%s", name);
11084 break;
11086 case dw_val_class_loclistsptr:
11087 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11088 debug_loc_section, "%s", name);
11089 break;
11091 case dw_val_class_str:
11092 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11093 dw2_asm_output_offset (dwarf_offset_size,
11094 a->dw_attr_val.v.val_str->label,
11095 debug_str_section,
11096 "%s: \"%s\"", name, AT_string (a));
11097 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11098 dw2_asm_output_offset (dwarf_offset_size,
11099 a->dw_attr_val.v.val_str->label,
11100 debug_line_str_section,
11101 "%s: \"%s\"", name, AT_string (a));
11102 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
11103 dw2_asm_output_data_uleb128 (AT_index (a),
11104 "%s: \"%s\"", name, AT_string (a));
11105 else
11106 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11107 break;
11109 case dw_val_class_file:
11111 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11113 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11114 a->dw_attr_val.v.val_file->filename);
11115 break;
11118 case dw_val_class_file_implicit:
11119 if (flag_debug_asm)
11120 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
11121 ASM_COMMENT_START, name,
11122 maybe_emit_file (a->dw_attr_val.v.val_file),
11123 a->dw_attr_val.v.val_file->filename);
11124 break;
11126 case dw_val_class_data8:
11128 int i;
11130 for (i = 0; i < 8; i++)
11131 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11132 i == 0 ? "%s" : NULL, name);
11133 break;
11136 case dw_val_class_high_pc:
11137 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11138 get_AT_low_pc (die), "DW_AT_high_pc");
11139 break;
11141 case dw_val_class_discr_value:
11142 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11143 break;
11145 case dw_val_class_discr_list:
11147 dw_discr_list_ref list = AT_discr_list (a);
11148 const int size = size_of_discr_list (list);
11150 /* This is a block, so output its length first. */
11151 dw2_asm_output_data (constant_size (size), size,
11152 "%s: block size", name);
11154 for (; list != NULL; list = list->dw_discr_next)
11156 /* One byte for the discriminant value descriptor, and then as
11157 many LEB128 numbers as required. */
11158 if (list->dw_discr_range)
11159 dw2_asm_output_data (1, DW_DSC_range,
11160 "%s: DW_DSC_range", name);
11161 else
11162 dw2_asm_output_data (1, DW_DSC_label,
11163 "%s: DW_DSC_label", name);
11165 output_discr_value (&list->dw_discr_lower_bound, name);
11166 if (list->dw_discr_range)
11167 output_discr_value (&list->dw_discr_upper_bound, name);
11169 break;
11172 default:
11173 gcc_unreachable ();
11177 FOR_EACH_CHILD (die, c, output_die (c));
11179 /* Add null byte to terminate sibling list. */
11180 if (die->die_child != NULL)
11181 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11182 (unsigned long) die->die_offset);
11185 /* Output the dwarf version number. */
11187 static void
11188 output_dwarf_version ()
11190 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11191 views in loclist. That will change eventually. */
11192 if (dwarf_version == 6)
11194 static bool once;
11195 if (!once)
11197 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11198 "incompatibilities");
11199 once = true;
11201 dw2_asm_output_data (2, 5, "DWARF version number");
11203 else
11204 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11207 /* Output the compilation unit that appears at the beginning of the
11208 .debug_info section, and precedes the DIE descriptions. */
11210 static void
11211 output_compilation_unit_header (enum dwarf_unit_type ut)
11213 if (!XCOFF_DEBUGGING_INFO)
11215 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11216 dw2_asm_output_data (4, 0xffffffff,
11217 "Initial length escape value indicating 64-bit DWARF extension");
11218 dw2_asm_output_data (dwarf_offset_size,
11219 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11220 "Length of Compilation Unit Info");
11223 output_dwarf_version ();
11224 if (dwarf_version >= 5)
11226 const char *name;
11227 switch (ut)
11229 case DW_UT_compile: name = "DW_UT_compile"; break;
11230 case DW_UT_type: name = "DW_UT_type"; break;
11231 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11232 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11233 default: gcc_unreachable ();
11235 dw2_asm_output_data (1, ut, "%s", name);
11236 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11238 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11239 debug_abbrev_section,
11240 "Offset Into Abbrev. Section");
11241 if (dwarf_version < 5)
11242 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11245 /* Output the compilation unit DIE and its children. */
11247 static void
11248 output_comp_unit (dw_die_ref die, int output_if_empty,
11249 const unsigned char *dwo_id)
11251 const char *secname, *oldsym;
11252 char *tmp;
11254 /* Unless we are outputting main CU, we may throw away empty ones. */
11255 if (!output_if_empty && die->die_child == NULL)
11256 return;
11258 /* Even if there are no children of this DIE, we must output the information
11259 about the compilation unit. Otherwise, on an empty translation unit, we
11260 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11261 will then complain when examining the file. First mark all the DIEs in
11262 this CU so we know which get local refs. */
11263 mark_dies (die);
11265 external_ref_hash_type *extern_map = optimize_external_refs (die);
11267 /* For now, optimize only the main CU, in order to optimize the rest
11268 we'd need to see all of them earlier. Leave the rest for post-linking
11269 tools like DWZ. */
11270 if (die == comp_unit_die ())
11271 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11273 build_abbrev_table (die, extern_map);
11275 optimize_abbrev_table ();
11277 delete extern_map;
11279 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11280 next_die_offset = (dwo_id
11281 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11282 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11283 calc_die_sizes (die);
11285 oldsym = die->die_id.die_symbol;
11286 if (oldsym && die->comdat_type_p)
11288 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11290 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11291 secname = tmp;
11292 die->die_id.die_symbol = NULL;
11293 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11295 else
11297 switch_to_section (debug_info_section);
11298 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11299 info_section_emitted = true;
11302 /* For LTO cross unit DIE refs we want a symbol on the start of the
11303 debuginfo section, not on the CU DIE. */
11304 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11306 /* ??? No way to get visibility assembled without a decl. */
11307 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11308 get_identifier (oldsym), char_type_node);
11309 TREE_PUBLIC (decl) = true;
11310 TREE_STATIC (decl) = true;
11311 DECL_ARTIFICIAL (decl) = true;
11312 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11313 DECL_VISIBILITY_SPECIFIED (decl) = true;
11314 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11315 #ifdef ASM_WEAKEN_LABEL
11316 /* We prefer a .weak because that handles duplicates from duplicate
11317 archive members in a graceful way. */
11318 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11319 #else
11320 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11321 #endif
11322 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11325 /* Output debugging information. */
11326 output_compilation_unit_header (dwo_id
11327 ? DW_UT_split_compile : DW_UT_compile);
11328 if (dwarf_version >= 5)
11330 if (dwo_id != NULL)
11331 for (int i = 0; i < 8; i++)
11332 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11334 output_die (die);
11336 /* Leave the marks on the main CU, so we can check them in
11337 output_pubnames. */
11338 if (oldsym)
11340 unmark_dies (die);
11341 die->die_id.die_symbol = oldsym;
11345 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11346 and .debug_pubtypes. This is configured per-target, but can be
11347 overridden by the -gpubnames or -gno-pubnames options. */
11349 static inline bool
11350 want_pubnames (void)
11352 if (debug_info_level <= DINFO_LEVEL_TERSE
11353 /* Names and types go to the early debug part only. */
11354 || in_lto_p)
11355 return false;
11356 if (debug_generate_pub_sections != -1)
11357 return debug_generate_pub_sections;
11358 return targetm.want_debug_pub_sections;
11361 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11363 static void
11364 add_AT_pubnames (dw_die_ref die)
11366 if (want_pubnames ())
11367 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11370 /* Add a string attribute value to a skeleton DIE. */
11372 static inline void
11373 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11374 const char *str)
11376 dw_attr_node attr;
11377 struct indirect_string_node *node;
11379 if (! skeleton_debug_str_hash)
11380 skeleton_debug_str_hash
11381 = hash_table<indirect_string_hasher>::create_ggc (10);
11383 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11384 find_string_form (node);
11385 if (node->form == dwarf_FORM (DW_FORM_strx))
11386 node->form = DW_FORM_strp;
11388 attr.dw_attr = attr_kind;
11389 attr.dw_attr_val.val_class = dw_val_class_str;
11390 attr.dw_attr_val.val_entry = NULL;
11391 attr.dw_attr_val.v.val_str = node;
11392 add_dwarf_attr (die, &attr);
11395 /* Helper function to generate top-level dies for skeleton debug_info and
11396 debug_types. */
11398 static void
11399 add_top_level_skeleton_die_attrs (dw_die_ref die)
11401 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11402 const char *comp_dir = comp_dir_string ();
11404 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11405 if (comp_dir != NULL)
11406 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11407 add_AT_pubnames (die);
11408 if (addr_index_table != NULL && addr_index_table->size () > 0)
11409 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11412 /* Output skeleton debug sections that point to the dwo file. */
11414 static void
11415 output_skeleton_debug_sections (dw_die_ref comp_unit,
11416 const unsigned char *dwo_id)
11418 /* These attributes will be found in the full debug_info section. */
11419 remove_AT (comp_unit, DW_AT_producer);
11420 remove_AT (comp_unit, DW_AT_language);
11422 switch_to_section (debug_skeleton_info_section);
11423 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11425 /* Produce the skeleton compilation-unit header. This one differs enough from
11426 a normal CU header that it's better not to call output_compilation_unit
11427 header. */
11428 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11429 dw2_asm_output_data (4, 0xffffffff,
11430 "Initial length escape value indicating 64-bit "
11431 "DWARF extension");
11433 dw2_asm_output_data (dwarf_offset_size,
11434 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11435 - DWARF_INITIAL_LENGTH_SIZE
11436 + size_of_die (comp_unit),
11437 "Length of Compilation Unit Info");
11438 output_dwarf_version ();
11439 if (dwarf_version >= 5)
11441 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11442 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11444 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11445 debug_skeleton_abbrev_section,
11446 "Offset Into Abbrev. Section");
11447 if (dwarf_version < 5)
11448 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11449 else
11450 for (int i = 0; i < 8; i++)
11451 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11453 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11454 output_die (comp_unit);
11456 /* Build the skeleton debug_abbrev section. */
11457 switch_to_section (debug_skeleton_abbrev_section);
11458 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11460 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11462 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11465 /* Output a comdat type unit DIE and its children. */
11467 static void
11468 output_comdat_type_unit (comdat_type_node *node,
11469 bool early_lto_debug ATTRIBUTE_UNUSED)
11471 const char *secname;
11472 char *tmp;
11473 int i;
11474 #if defined (OBJECT_FORMAT_ELF)
11475 tree comdat_key;
11476 #endif
11478 /* First mark all the DIEs in this CU so we know which get local refs. */
11479 mark_dies (node->root_die);
11481 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11483 build_abbrev_table (node->root_die, extern_map);
11485 delete extern_map;
11486 extern_map = NULL;
11488 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11489 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11490 calc_die_sizes (node->root_die);
11492 #if defined (OBJECT_FORMAT_ELF)
11493 if (dwarf_version >= 5)
11495 if (!dwarf_split_debug_info)
11496 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11497 else
11498 secname = (early_lto_debug
11499 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11501 else if (!dwarf_split_debug_info)
11502 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11503 else
11504 secname = (early_lto_debug
11505 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11507 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11508 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11509 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11510 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11511 comdat_key = get_identifier (tmp);
11512 targetm.asm_out.named_section (secname,
11513 SECTION_DEBUG | SECTION_LINKONCE,
11514 comdat_key);
11515 #else
11516 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11517 sprintf (tmp, (dwarf_version >= 5
11518 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11519 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11520 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11521 secname = tmp;
11522 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11523 #endif
11525 /* Output debugging information. */
11526 output_compilation_unit_header (dwarf_split_debug_info
11527 ? DW_UT_split_type : DW_UT_type);
11528 output_signature (node->signature, "Type Signature");
11529 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11530 "Offset to Type DIE");
11531 output_die (node->root_die);
11533 unmark_dies (node->root_die);
11536 /* Return the DWARF2/3 pubname associated with a decl. */
11538 static const char *
11539 dwarf2_name (tree decl, int scope)
11541 if (DECL_NAMELESS (decl))
11542 return NULL;
11543 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11546 /* Add a new entry to .debug_pubnames if appropriate. */
11548 static void
11549 add_pubname_string (const char *str, dw_die_ref die)
11551 pubname_entry e;
11553 e.die = die;
11554 e.name = xstrdup (str);
11555 vec_safe_push (pubname_table, e);
11558 static void
11559 add_pubname (tree decl, dw_die_ref die)
11561 if (!want_pubnames ())
11562 return;
11564 /* Don't add items to the table when we expect that the consumer will have
11565 just read the enclosing die. For example, if the consumer is looking at a
11566 class_member, it will either be inside the class already, or will have just
11567 looked up the class to find the member. Either way, searching the class is
11568 faster than searching the index. */
11569 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11570 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11572 const char *name = dwarf2_name (decl, 1);
11574 if (name)
11575 add_pubname_string (name, die);
11579 /* Add an enumerator to the pubnames section. */
11581 static void
11582 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11584 pubname_entry e;
11586 gcc_assert (scope_name);
11587 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11588 e.die = die;
11589 vec_safe_push (pubname_table, e);
11592 /* Add a new entry to .debug_pubtypes if appropriate. */
11594 static void
11595 add_pubtype (tree decl, dw_die_ref die)
11597 pubname_entry e;
11599 if (!want_pubnames ())
11600 return;
11602 if ((TREE_PUBLIC (decl)
11603 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11604 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11606 tree scope = NULL;
11607 const char *scope_name = "";
11608 const char *sep = is_cxx () ? "::" : ".";
11609 const char *name;
11611 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11612 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11614 scope_name = lang_hooks.dwarf_name (scope, 1);
11615 if (scope_name != NULL && scope_name[0] != '\0')
11616 scope_name = concat (scope_name, sep, NULL);
11617 else
11618 scope_name = "";
11621 if (TYPE_P (decl))
11622 name = type_tag (decl);
11623 else
11624 name = lang_hooks.dwarf_name (decl, 1);
11626 /* If we don't have a name for the type, there's no point in adding
11627 it to the table. */
11628 if (name != NULL && name[0] != '\0')
11630 e.die = die;
11631 e.name = concat (scope_name, name, NULL);
11632 vec_safe_push (pubtype_table, e);
11635 /* Although it might be more consistent to add the pubinfo for the
11636 enumerators as their dies are created, they should only be added if the
11637 enum type meets the criteria above. So rather than re-check the parent
11638 enum type whenever an enumerator die is created, just output them all
11639 here. This isn't protected by the name conditional because anonymous
11640 enums don't have names. */
11641 if (die->die_tag == DW_TAG_enumeration_type)
11643 dw_die_ref c;
11645 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11650 /* Output a single entry in the pubnames table. */
11652 static void
11653 output_pubname (dw_offset die_offset, pubname_entry *entry)
11655 dw_die_ref die = entry->die;
11656 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11658 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11660 if (debug_generate_pub_sections == 2)
11662 /* This logic follows gdb's method for determining the value of the flag
11663 byte. */
11664 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11665 switch (die->die_tag)
11667 case DW_TAG_typedef:
11668 case DW_TAG_base_type:
11669 case DW_TAG_subrange_type:
11670 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11671 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11672 break;
11673 case DW_TAG_enumerator:
11674 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11675 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11676 if (!is_cxx ())
11677 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11678 break;
11679 case DW_TAG_subprogram:
11680 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11681 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11682 if (!is_ada ())
11683 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11684 break;
11685 case DW_TAG_constant:
11686 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11687 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11688 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11689 break;
11690 case DW_TAG_variable:
11691 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11692 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11693 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11694 break;
11695 case DW_TAG_namespace:
11696 case DW_TAG_imported_declaration:
11697 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11698 break;
11699 case DW_TAG_class_type:
11700 case DW_TAG_interface_type:
11701 case DW_TAG_structure_type:
11702 case DW_TAG_union_type:
11703 case DW_TAG_enumeration_type:
11704 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11705 if (!is_cxx ())
11706 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11707 break;
11708 default:
11709 /* An unusual tag. Leave the flag-byte empty. */
11710 break;
11712 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11713 "GDB-index flags");
11716 dw2_asm_output_nstring (entry->name, -1, "external name");
11720 /* Output the public names table used to speed up access to externally
11721 visible names; or the public types table used to find type definitions. */
11723 static void
11724 output_pubnames (vec<pubname_entry, va_gc> *names)
11726 unsigned i;
11727 unsigned long pubnames_length = size_of_pubnames (names);
11728 pubname_entry *pub;
11730 if (!XCOFF_DEBUGGING_INFO)
11732 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11733 dw2_asm_output_data (4, 0xffffffff,
11734 "Initial length escape value indicating 64-bit DWARF extension");
11735 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11736 "Pub Info Length");
11739 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11740 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11742 if (dwarf_split_debug_info)
11743 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11744 debug_skeleton_info_section,
11745 "Offset of Compilation Unit Info");
11746 else
11747 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11748 debug_info_section,
11749 "Offset of Compilation Unit Info");
11750 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11751 "Compilation Unit Length");
11753 FOR_EACH_VEC_ELT (*names, i, pub)
11755 if (include_pubname_in_output (names, pub))
11757 dw_offset die_offset = pub->die->die_offset;
11759 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11760 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11761 gcc_assert (pub->die->die_mark);
11763 /* If we're putting types in their own .debug_types sections,
11764 the .debug_pubtypes table will still point to the compile
11765 unit (not the type unit), so we want to use the offset of
11766 the skeleton DIE (if there is one). */
11767 if (pub->die->comdat_type_p && names == pubtype_table)
11769 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11771 if (type_node != NULL)
11772 die_offset = (type_node->skeleton_die != NULL
11773 ? type_node->skeleton_die->die_offset
11774 : comp_unit_die ()->die_offset);
11777 output_pubname (die_offset, pub);
11781 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11784 /* Output public names and types tables if necessary. */
11786 static void
11787 output_pubtables (void)
11789 if (!want_pubnames () || !info_section_emitted)
11790 return;
11792 switch_to_section (debug_pubnames_section);
11793 output_pubnames (pubname_table);
11794 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11795 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11796 simply won't look for the section. */
11797 switch_to_section (debug_pubtypes_section);
11798 output_pubnames (pubtype_table);
11802 /* Output the information that goes into the .debug_aranges table.
11803 Namely, define the beginning and ending address range of the
11804 text section generated for this compilation unit. */
11806 static void
11807 output_aranges (void)
11809 unsigned i;
11810 unsigned long aranges_length = size_of_aranges ();
11812 if (!XCOFF_DEBUGGING_INFO)
11814 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11815 dw2_asm_output_data (4, 0xffffffff,
11816 "Initial length escape value indicating 64-bit DWARF extension");
11817 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11818 "Length of Address Ranges Info");
11821 /* Version number for aranges is still 2, even up to DWARF5. */
11822 dw2_asm_output_data (2, 2, "DWARF aranges version");
11823 if (dwarf_split_debug_info)
11824 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11825 debug_skeleton_info_section,
11826 "Offset of Compilation Unit Info");
11827 else
11828 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11829 debug_info_section,
11830 "Offset of Compilation Unit Info");
11831 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11832 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11834 /* We need to align to twice the pointer size here. */
11835 if (DWARF_ARANGES_PAD_SIZE)
11837 /* Pad using a 2 byte words so that padding is correct for any
11838 pointer size. */
11839 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11840 2 * DWARF2_ADDR_SIZE);
11841 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11842 dw2_asm_output_data (2, 0, NULL);
11845 /* It is necessary not to output these entries if the sections were
11846 not used; if the sections were not used, the length will be 0 and
11847 the address may end up as 0 if the section is discarded by ld
11848 --gc-sections, leaving an invalid (0, 0) entry that can be
11849 confused with the terminator. */
11850 if (switch_text_ranges)
11852 const char *prev_loc = text_section_label;
11853 const char *loc;
11854 unsigned idx;
11856 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11857 if (prev_loc)
11859 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11860 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11861 prev_loc = NULL;
11863 else
11864 prev_loc = loc;
11866 if (prev_loc)
11868 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11869 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11870 prev_loc, "Length");
11874 if (switch_cold_ranges)
11876 const char *prev_loc = cold_text_section_label;
11877 const char *loc;
11878 unsigned idx;
11880 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11881 if (prev_loc)
11883 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11884 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11885 prev_loc = NULL;
11887 else
11888 prev_loc = loc;
11890 if (prev_loc)
11892 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11893 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11894 prev_loc, "Length");
11898 if (have_multiple_function_sections)
11900 unsigned fde_idx;
11901 dw_fde_ref fde;
11903 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11905 if (fde->ignored_debug)
11906 continue;
11907 if (!fde->in_std_section)
11909 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11910 "Address");
11911 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11912 fde->dw_fde_begin, "Length");
11914 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11916 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11917 "Address");
11918 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11919 fde->dw_fde_second_begin, "Length");
11924 /* Output the terminator words. */
11925 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11926 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11929 /* Add a new entry to .debug_ranges. Return its index into
11930 ranges_table vector. */
11932 static unsigned int
11933 add_ranges_num (int num, bool maybe_new_sec)
11935 dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11936 vec_safe_push (ranges_table, r);
11937 return vec_safe_length (ranges_table) - 1;
11940 /* Add a new entry to .debug_ranges corresponding to a block, or a
11941 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11942 this entry might be in a different section from previous range. */
11944 static unsigned int
11945 add_ranges (const_tree block, bool maybe_new_sec)
11947 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11950 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11951 chain, or middle entry of a chain that will be directly referred to. */
11953 static void
11954 note_rnglist_head (unsigned int offset)
11956 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11957 return;
11958 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11961 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11962 When using dwarf_split_debug_info, address attributes in dies destined
11963 for the final executable should be direct references--setting the
11964 parameter force_direct ensures this behavior. */
11966 static void
11967 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11968 bool *added, bool force_direct)
11970 unsigned int in_use = vec_safe_length (ranges_by_label);
11971 unsigned int offset;
11972 dw_ranges_by_label rbl = { begin, end };
11973 vec_safe_push (ranges_by_label, rbl);
11974 offset = add_ranges_num (-(int)in_use - 1, true);
11975 if (!*added)
11977 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11978 *added = true;
11979 note_rnglist_head (offset);
11980 if (dwarf_split_debug_info && force_direct)
11981 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11985 /* Emit .debug_ranges section. */
11987 static void
11988 output_ranges (void)
11990 unsigned i;
11991 static const char *const start_fmt = "Offset %#x";
11992 const char *fmt = start_fmt;
11993 dw_ranges *r;
11995 switch_to_section (debug_ranges_section);
11996 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11997 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11999 int block_num = r->num;
12001 if (block_num > 0)
12003 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12004 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12006 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12007 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12009 /* If all code is in the text section, then the compilation
12010 unit base address defaults to DW_AT_low_pc, which is the
12011 base of the text section. */
12012 if (!have_multiple_function_sections)
12014 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
12015 text_section_label,
12016 fmt, i * 2 * DWARF2_ADDR_SIZE);
12017 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
12018 text_section_label, NULL);
12021 /* Otherwise, the compilation unit base address is zero,
12022 which allows us to use absolute addresses, and not worry
12023 about whether the target supports cross-section
12024 arithmetic. */
12025 else
12027 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12028 fmt, i * 2 * DWARF2_ADDR_SIZE);
12029 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12032 fmt = NULL;
12035 /* Negative block_num stands for an index into ranges_by_label. */
12036 else if (block_num < 0)
12038 int lab_idx = - block_num - 1;
12040 if (!have_multiple_function_sections)
12042 gcc_unreachable ();
12043 #if 0
12044 /* If we ever use add_ranges_by_labels () for a single
12045 function section, all we have to do is to take out
12046 the #if 0 above. */
12047 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12048 (*ranges_by_label)[lab_idx].begin,
12049 text_section_label,
12050 fmt, i * 2 * DWARF2_ADDR_SIZE);
12051 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12052 (*ranges_by_label)[lab_idx].end,
12053 text_section_label, NULL);
12054 #endif
12056 else
12058 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12059 (*ranges_by_label)[lab_idx].begin,
12060 fmt, i * 2 * DWARF2_ADDR_SIZE);
12061 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12062 (*ranges_by_label)[lab_idx].end,
12063 NULL);
12066 else
12068 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12069 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12070 fmt = start_fmt;
12075 /* Non-zero if .debug_line_str should be used for .debug_line section
12076 strings or strings that are likely shareable with those. */
12077 #define DWARF5_USE_DEBUG_LINE_STR \
12078 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
12079 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
12080 /* FIXME: there is no .debug_line_str.dwo section, \
12081 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
12082 && !dwarf_split_debug_info)
12085 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
12086 DWARF5 .debug_line tables using .debug_line_str or we generate
12087 it ourselves, except for split-dwarf which doesn't have a
12088 .debug_line_str. */
12089 static bool
12090 asm_outputs_debug_line_str (void)
12092 if (dwarf_version >= 5
12093 && ! output_asm_line_debug_info ()
12094 && DWARF5_USE_DEBUG_LINE_STR)
12095 return true;
12096 else
12098 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12099 return !dwarf_split_debug_info && dwarf_version >= 5;
12100 #else
12101 return false;
12102 #endif
12106 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12107 I is index of the following range. */
12109 static bool
12110 use_distinct_base_address_for_range (unsigned int i)
12112 if (i >= vec_safe_length (ranges_table))
12113 return false;
12115 dw_ranges *r2 = &(*ranges_table)[i];
12116 /* Use DW_RLE_base_address{,x} if there is a next range in the
12117 range list and is guaranteed to be in the same section. */
12118 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12121 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12122 section when needed. */
12124 static void
12125 index_rnglists (void)
12127 unsigned i;
12128 dw_ranges *r;
12129 bool base = false;
12131 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12133 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12134 r->idx = rnglist_idx++;
12136 int block_num = r->num;
12137 if ((HAVE_AS_LEB128 || block_num < 0)
12138 && !have_multiple_function_sections)
12139 continue;
12140 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12141 base = false;
12142 if (block_num > 0)
12144 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12145 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12147 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12148 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12150 if (HAVE_AS_LEB128)
12152 if (!base && use_distinct_base_address_for_range (i + 1))
12154 r->begin_entry = add_addr_table_entry (xstrdup (blabel),
12155 ate_kind_label);
12156 base = true;
12158 if (base)
12159 /* If we have a base, no need for further
12160 begin_entry/end_entry, as DW_RLE_offset_pair will be
12161 used. */
12162 continue;
12163 r->begin_entry
12164 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12165 /* No need for end_entry, DW_RLE_start{,x}_length will use
12166 length as opposed to a pair of addresses. */
12168 else
12170 r->begin_entry
12171 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12172 r->end_entry
12173 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12177 /* Negative block_num stands for an index into ranges_by_label. */
12178 else if (block_num < 0)
12180 int lab_idx = - block_num - 1;
12181 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12182 const char *elabel = (*ranges_by_label)[lab_idx].end;
12184 r->begin_entry
12185 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12186 if (!HAVE_AS_LEB128)
12187 r->end_entry
12188 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12193 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12195 static bool
12196 output_rnglists (unsigned generation, bool dwo)
12198 unsigned i;
12199 dw_ranges *r;
12200 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12201 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12202 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12204 if (dwo)
12205 switch_to_section (debug_ranges_dwo_section);
12206 else
12208 switch_to_section (debug_ranges_section);
12209 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12211 /* There are up to 4 unique ranges labels per generation.
12212 See also init_sections_and_labels. */
12213 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12214 2 + 2 * dwo + generation * 6);
12215 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12216 3 + 2 * dwo + generation * 6);
12217 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12218 dw2_asm_output_data (4, 0xffffffff,
12219 "Initial length escape value indicating "
12220 "64-bit DWARF extension");
12221 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12222 "Length of Range Lists");
12223 ASM_OUTPUT_LABEL (asm_out_file, l1);
12224 output_dwarf_version ();
12225 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12226 dw2_asm_output_data (1, 0, "Segment Size");
12227 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12228 about relocation sizes and primarily care about the size of .debug*
12229 sections in linked shared libraries and executables, then
12230 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12231 into it are usually larger than just DW_FORM_sec_offset offsets
12232 into the .debug_rnglists section. */
12233 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12234 "Offset Entry Count");
12235 if (dwo)
12237 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12238 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12239 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12240 dw2_asm_output_delta (dwarf_offset_size, r->label,
12241 ranges_base_label, NULL);
12244 const char *lab = "";
12245 const char *base = NULL;
12246 bool skipping = false;
12247 bool ret = false;
12248 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12250 int block_num = r->num;
12252 if (r->label)
12254 if (dwarf_split_debug_info
12255 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12257 ret = true;
12258 skipping = true;
12259 continue;
12261 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12262 lab = r->label;
12264 if (skipping)
12266 if (block_num == 0)
12267 skipping = false;
12268 continue;
12270 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12271 base = NULL;
12272 if (block_num > 0)
12274 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12275 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12277 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12278 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12280 if (HAVE_AS_LEB128)
12282 /* If all code is in the text section, then the compilation
12283 unit base address defaults to DW_AT_low_pc, which is the
12284 base of the text section. */
12285 if (!have_multiple_function_sections)
12287 dw2_asm_output_data (1, DW_RLE_offset_pair,
12288 "DW_RLE_offset_pair (%s)", lab);
12289 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12290 "Range begin address (%s)", lab);
12291 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12292 "Range end address (%s)", lab);
12293 continue;
12295 if (base == NULL && use_distinct_base_address_for_range (i + 1))
12297 if (dwarf_split_debug_info)
12299 dw2_asm_output_data (1, DW_RLE_base_addressx,
12300 "DW_RLE_base_addressx (%s)", lab);
12301 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12302 "Base address index (%s)",
12303 blabel);
12305 else
12307 dw2_asm_output_data (1, DW_RLE_base_address,
12308 "DW_RLE_base_address (%s)", lab);
12309 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12310 "Base address (%s)", lab);
12312 strcpy (basebuf, blabel);
12313 base = basebuf;
12315 if (base)
12317 dw2_asm_output_data (1, DW_RLE_offset_pair,
12318 "DW_RLE_offset_pair (%s)", lab);
12319 dw2_asm_output_delta_uleb128 (blabel, base,
12320 "Range begin address (%s)", lab);
12321 dw2_asm_output_delta_uleb128 (elabel, base,
12322 "Range end address (%s)", lab);
12323 continue;
12325 if (dwarf_split_debug_info)
12327 dw2_asm_output_data (1, DW_RLE_startx_length,
12328 "DW_RLE_startx_length (%s)", lab);
12329 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12330 "Range begin address index "
12331 "(%s)", blabel);
12333 else
12335 dw2_asm_output_data (1, DW_RLE_start_length,
12336 "DW_RLE_start_length (%s)", lab);
12337 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12338 "Range begin address (%s)", lab);
12340 dw2_asm_output_delta_uleb128 (elabel, blabel,
12341 "Range length (%s)", lab);
12343 else if (dwarf_split_debug_info)
12345 dw2_asm_output_data (1, DW_RLE_startx_endx,
12346 "DW_RLE_startx_endx (%s)", lab);
12347 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12348 "Range begin address index "
12349 "(%s)", blabel);
12350 dw2_asm_output_data_uleb128 (r->end_entry->index,
12351 "Range end address index "
12352 "(%s)", elabel);
12354 else
12356 dw2_asm_output_data (1, DW_RLE_start_end,
12357 "DW_RLE_start_end (%s)", lab);
12358 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12359 "Range begin address (%s)", lab);
12360 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12361 "Range end address (%s)", lab);
12365 /* Negative block_num stands for an index into ranges_by_label. */
12366 else if (block_num < 0)
12368 int lab_idx = - block_num - 1;
12369 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12370 const char *elabel = (*ranges_by_label)[lab_idx].end;
12372 if (!have_multiple_function_sections)
12373 gcc_unreachable ();
12374 if (HAVE_AS_LEB128)
12376 if (dwarf_split_debug_info)
12378 dw2_asm_output_data (1, DW_RLE_startx_length,
12379 "DW_RLE_startx_length (%s)", lab);
12380 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12381 "Range begin address index "
12382 "(%s)", blabel);
12384 else
12386 dw2_asm_output_data (1, DW_RLE_start_length,
12387 "DW_RLE_start_length (%s)", lab);
12388 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12389 "Range begin address (%s)", lab);
12391 dw2_asm_output_delta_uleb128 (elabel, blabel,
12392 "Range length (%s)", lab);
12394 else if (dwarf_split_debug_info)
12396 dw2_asm_output_data (1, DW_RLE_startx_endx,
12397 "DW_RLE_startx_endx (%s)", lab);
12398 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12399 "Range begin address index "
12400 "(%s)", blabel);
12401 dw2_asm_output_data_uleb128 (r->end_entry->index,
12402 "Range end address index "
12403 "(%s)", elabel);
12405 else
12407 dw2_asm_output_data (1, DW_RLE_start_end,
12408 "DW_RLE_start_end (%s)", lab);
12409 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12410 "Range begin address (%s)", lab);
12411 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12412 "Range end address (%s)", lab);
12415 else
12416 dw2_asm_output_data (1, DW_RLE_end_of_list,
12417 "DW_RLE_end_of_list (%s)", lab);
12419 ASM_OUTPUT_LABEL (asm_out_file, l2);
12420 return ret;
12423 /* Data structure containing information about input files. */
12424 struct file_info
12426 const char *path; /* Complete file name. */
12427 const char *fname; /* File name part. */
12428 int length; /* Length of entire string. */
12429 struct dwarf_file_data * file_idx; /* Index in input file table. */
12430 int dir_idx; /* Index in directory table. */
12433 /* Data structure containing information about directories with source
12434 files. */
12435 struct dir_info
12437 const char *path; /* Path including directory name. */
12438 int length; /* Path length. */
12439 int prefix; /* Index of directory entry which is a prefix. */
12440 int count; /* Number of files in this directory. */
12441 int dir_idx; /* Index of directory used as base. */
12444 /* Callback function for file_info comparison. We sort by looking at
12445 the directories in the path. */
12447 static int
12448 file_info_cmp (const void *p1, const void *p2)
12450 const struct file_info *const s1 = (const struct file_info *) p1;
12451 const struct file_info *const s2 = (const struct file_info *) p2;
12452 const unsigned char *cp1;
12453 const unsigned char *cp2;
12455 /* Take care of file names without directories. We need to make sure that
12456 we return consistent values to qsort since some will get confused if
12457 we return the same value when identical operands are passed in opposite
12458 orders. So if neither has a directory, return 0 and otherwise return
12459 1 or -1 depending on which one has the directory. We want the one with
12460 the directory to sort after the one without, so all no directory files
12461 are at the start (normally only the compilation unit file). */
12462 if ((s1->path == s1->fname || s2->path == s2->fname))
12463 return (s2->path == s2->fname) - (s1->path == s1->fname);
12465 cp1 = (const unsigned char *) s1->path;
12466 cp2 = (const unsigned char *) s2->path;
12468 while (1)
12470 ++cp1;
12471 ++cp2;
12472 /* Reached the end of the first path? If so, handle like above,
12473 but now we want longer directory prefixes before shorter ones. */
12474 if ((cp1 == (const unsigned char *) s1->fname)
12475 || (cp2 == (const unsigned char *) s2->fname))
12476 return ((cp1 == (const unsigned char *) s1->fname)
12477 - (cp2 == (const unsigned char *) s2->fname));
12479 /* Character of current path component the same? */
12480 else if (*cp1 != *cp2)
12481 return *cp1 - *cp2;
12485 struct file_name_acquire_data
12487 struct file_info *files;
12488 int used_files;
12489 int max_files;
12492 /* Traversal function for the hash table. */
12495 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12497 struct dwarf_file_data *d = *slot;
12498 struct file_info *fi;
12499 const char *f;
12501 gcc_assert (fnad->max_files >= d->emitted_number);
12503 if (! d->emitted_number)
12504 return 1;
12506 gcc_assert (fnad->max_files != fnad->used_files);
12508 fi = fnad->files + fnad->used_files++;
12510 f = d->filename;
12512 /* Skip all leading "./". */
12513 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12514 f += 2;
12516 /* Create a new array entry. */
12517 fi->path = f;
12518 fi->length = strlen (f);
12519 fi->file_idx = d;
12521 /* Search for the file name part. */
12522 f = strrchr (f, DIR_SEPARATOR);
12523 #if defined (DIR_SEPARATOR_2)
12525 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12527 if (g != NULL)
12529 if (f == NULL || f < g)
12530 f = g;
12533 #endif
12535 fi->fname = f == NULL ? fi->path : f + 1;
12536 return 1;
12539 /* Helper function for output_file_names. Emit a FORM encoded
12540 string STR, with assembly comment start ENTRY_KIND and
12541 index IDX */
12543 static void
12544 output_line_string (enum dwarf_form form, const char *str,
12545 const char *entry_kind, unsigned int idx)
12547 switch (form)
12549 case DW_FORM_string:
12550 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12551 break;
12552 case DW_FORM_line_strp:
12553 if (!debug_line_str_hash)
12554 debug_line_str_hash
12555 = hash_table<indirect_string_hasher>::create_ggc (10);
12557 struct indirect_string_node *node;
12558 node = find_AT_string_in_table (str, debug_line_str_hash);
12559 set_indirect_string (node);
12560 node->form = form;
12561 dw2_asm_output_offset (dwarf_offset_size, node->label,
12562 debug_line_str_section, "%s: %#x: \"%s\"",
12563 entry_kind, 0, node->str);
12564 break;
12565 default:
12566 gcc_unreachable ();
12570 /* Output the directory table and the file name table. We try to minimize
12571 the total amount of memory needed. A heuristic is used to avoid large
12572 slowdowns with many input files. */
12574 static void
12575 output_file_names (void)
12577 struct file_name_acquire_data fnad;
12578 int numfiles;
12579 struct file_info *files;
12580 struct dir_info *dirs;
12581 int *saved;
12582 int *savehere;
12583 int *backmap;
12584 int ndirs;
12585 int idx_offset;
12586 int i;
12588 if (!last_emitted_file)
12590 if (dwarf_version >= 5)
12592 const char *comp_dir = comp_dir_string ();
12593 if (comp_dir == NULL)
12594 comp_dir = "";
12595 dw2_asm_output_data (1, 1, "Directory entry format count");
12596 enum dwarf_form str_form = DW_FORM_string;
12597 if (DWARF5_USE_DEBUG_LINE_STR)
12598 str_form = DW_FORM_line_strp;
12599 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12600 dw2_asm_output_data_uleb128 (str_form, "%s",
12601 get_DW_FORM_name (str_form));
12602 dw2_asm_output_data_uleb128 (1, "Directories count");
12603 if (str_form == DW_FORM_string)
12604 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12605 else
12606 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12607 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12608 if (filename0 == NULL)
12609 filename0 = "";
12610 #ifdef VMS_DEBUGGING_INFO
12611 dw2_asm_output_data (1, 4, "File name entry format count");
12612 #else
12613 dw2_asm_output_data (1, 2, "File name entry format count");
12614 #endif
12615 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12616 dw2_asm_output_data_uleb128 (str_form, "%s",
12617 get_DW_FORM_name (str_form));
12618 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12619 "DW_LNCT_directory_index");
12620 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12621 get_DW_FORM_name (DW_FORM_data1));
12622 #ifdef VMS_DEBUGGING_INFO
12623 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12624 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12625 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12626 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12627 #endif
12628 dw2_asm_output_data_uleb128 (1, "File names count");
12630 output_line_string (str_form, filename0, "File Entry", 0);
12631 dw2_asm_output_data (1, 0, NULL);
12632 #ifdef VMS_DEBUGGING_INFO
12633 dw2_asm_output_data_uleb128 (0, NULL);
12634 dw2_asm_output_data_uleb128 (0, NULL);
12635 #endif
12637 else
12639 dw2_asm_output_data (1, 0, "End directory table");
12640 dw2_asm_output_data (1, 0, "End file name table");
12642 return;
12645 numfiles = last_emitted_file->emitted_number;
12647 /* Allocate the various arrays we need. */
12648 files = XALLOCAVEC (struct file_info, numfiles);
12649 dirs = XALLOCAVEC (struct dir_info, numfiles);
12651 fnad.files = files;
12652 fnad.used_files = 0;
12653 fnad.max_files = numfiles;
12654 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12655 gcc_assert (fnad.used_files == fnad.max_files);
12657 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12659 /* Find all the different directories used. */
12660 dirs[0].path = files[0].path;
12661 dirs[0].length = files[0].fname - files[0].path;
12662 dirs[0].prefix = -1;
12663 dirs[0].count = 1;
12664 dirs[0].dir_idx = 0;
12665 files[0].dir_idx = 0;
12666 ndirs = 1;
12668 for (i = 1; i < numfiles; i++)
12669 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12670 && memcmp (dirs[ndirs - 1].path, files[i].path,
12671 dirs[ndirs - 1].length) == 0)
12673 /* Same directory as last entry. */
12674 files[i].dir_idx = ndirs - 1;
12675 ++dirs[ndirs - 1].count;
12677 else
12679 int j;
12681 /* This is a new directory. */
12682 dirs[ndirs].path = files[i].path;
12683 dirs[ndirs].length = files[i].fname - files[i].path;
12684 dirs[ndirs].count = 1;
12685 dirs[ndirs].dir_idx = ndirs;
12686 files[i].dir_idx = ndirs;
12688 /* Search for a prefix. */
12689 dirs[ndirs].prefix = -1;
12690 for (j = 0; j < ndirs; j++)
12691 if (dirs[j].length < dirs[ndirs].length
12692 && dirs[j].length > 1
12693 && (dirs[ndirs].prefix == -1
12694 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12695 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12696 dirs[ndirs].prefix = j;
12698 ++ndirs;
12701 /* Now to the actual work. We have to find a subset of the directories which
12702 allow expressing the file name using references to the directory table
12703 with the least amount of characters. We do not do an exhaustive search
12704 where we would have to check out every combination of every single
12705 possible prefix. Instead we use a heuristic which provides nearly optimal
12706 results in most cases and never is much off. */
12707 saved = XALLOCAVEC (int, ndirs);
12708 savehere = XALLOCAVEC (int, ndirs);
12710 memset (saved, '\0', ndirs * sizeof (saved[0]));
12711 for (i = 0; i < ndirs; i++)
12713 int j;
12714 int total;
12716 /* We can always save some space for the current directory. But this
12717 does not mean it will be enough to justify adding the directory. */
12718 savehere[i] = dirs[i].length;
12719 total = (savehere[i] - saved[i]) * dirs[i].count;
12721 for (j = i + 1; j < ndirs; j++)
12723 savehere[j] = 0;
12724 if (saved[j] < dirs[i].length)
12726 /* Determine whether the dirs[i] path is a prefix of the
12727 dirs[j] path. */
12728 int k;
12730 k = dirs[j].prefix;
12731 while (k != -1 && k != (int) i)
12732 k = dirs[k].prefix;
12734 if (k == (int) i)
12736 /* Yes it is. We can possibly save some memory by
12737 writing the filenames in dirs[j] relative to
12738 dirs[i]. */
12739 savehere[j] = dirs[i].length;
12740 total += (savehere[j] - saved[j]) * dirs[j].count;
12745 /* Check whether we can save enough to justify adding the dirs[i]
12746 directory. */
12747 if (total > dirs[i].length + 1)
12749 /* It's worthwhile adding. */
12750 for (j = i; j < ndirs; j++)
12751 if (savehere[j] > 0)
12753 /* Remember how much we saved for this directory so far. */
12754 saved[j] = savehere[j];
12756 /* Remember the prefix directory. */
12757 dirs[j].dir_idx = i;
12762 /* Emit the directory name table. */
12763 idx_offset = dirs[0].length > 0 ? 1 : 0;
12764 enum dwarf_form str_form = DW_FORM_string;
12765 enum dwarf_form idx_form = DW_FORM_udata;
12766 if (dwarf_version >= 5)
12768 const char *comp_dir = comp_dir_string ();
12769 if (comp_dir == NULL)
12770 comp_dir = "";
12771 dw2_asm_output_data (1, 1, "Directory entry format count");
12772 if (DWARF5_USE_DEBUG_LINE_STR)
12773 str_form = DW_FORM_line_strp;
12774 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12775 dw2_asm_output_data_uleb128 (str_form, "%s",
12776 get_DW_FORM_name (str_form));
12777 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12778 if (str_form == DW_FORM_string)
12780 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12781 for (i = 1 - idx_offset; i < ndirs; i++)
12782 dw2_asm_output_nstring (dirs[i].path,
12783 dirs[i].length
12784 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12785 "Directory Entry: %#x", i + idx_offset);
12787 else
12789 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12790 for (i = 1 - idx_offset; i < ndirs; i++)
12792 const char *str
12793 = ggc_alloc_string (dirs[i].path,
12794 dirs[i].length
12795 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12796 output_line_string (str_form, str, "Directory Entry",
12797 (unsigned) i + idx_offset);
12801 else
12803 for (i = 1 - idx_offset; i < ndirs; i++)
12804 dw2_asm_output_nstring (dirs[i].path,
12805 dirs[i].length
12806 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12807 "Directory Entry: %#x", i + idx_offset);
12809 dw2_asm_output_data (1, 0, "End directory table");
12812 /* We have to emit them in the order of emitted_number since that's
12813 used in the debug info generation. To do this efficiently we
12814 generate a back-mapping of the indices first. */
12815 backmap = XALLOCAVEC (int, numfiles);
12816 for (i = 0; i < numfiles; i++)
12817 backmap[files[i].file_idx->emitted_number - 1] = i;
12819 if (dwarf_version >= 5)
12821 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12822 if (filename0 == NULL)
12823 filename0 = "";
12824 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12825 DW_FORM_data2. Choose one based on the number of directories
12826 and how much space would they occupy in each encoding.
12827 If we have at most 256 directories, all indexes fit into
12828 a single byte, so DW_FORM_data1 is most compact (if there
12829 are at most 128 directories, DW_FORM_udata would be as
12830 compact as that, but not shorter and slower to decode). */
12831 if (ndirs + idx_offset <= 256)
12832 idx_form = DW_FORM_data1;
12833 /* If there are more than 65536 directories, we have to use
12834 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12835 Otherwise, compute what space would occupy if all the indexes
12836 used DW_FORM_udata - sum - and compare that to how large would
12837 be DW_FORM_data2 encoding, and pick the more efficient one. */
12838 else if (ndirs + idx_offset <= 65536)
12840 unsigned HOST_WIDE_INT sum = 1;
12841 for (i = 0; i < numfiles; i++)
12843 int file_idx = backmap[i];
12844 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12845 sum += size_of_uleb128 (dir_idx);
12847 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12848 idx_form = DW_FORM_data2;
12850 #ifdef VMS_DEBUGGING_INFO
12851 dw2_asm_output_data (1, 4, "File name entry format count");
12852 #else
12853 dw2_asm_output_data (1, 2, "File name entry format count");
12854 #endif
12855 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12856 dw2_asm_output_data_uleb128 (str_form, "%s",
12857 get_DW_FORM_name (str_form));
12858 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12859 "DW_LNCT_directory_index");
12860 dw2_asm_output_data_uleb128 (idx_form, "%s",
12861 get_DW_FORM_name (idx_form));
12862 #ifdef VMS_DEBUGGING_INFO
12863 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12864 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12865 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12866 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12867 #endif
12868 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12870 output_line_string (str_form, filename0, "File Entry", 0);
12872 /* Include directory index. */
12873 if (idx_form != DW_FORM_udata)
12874 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12875 0, NULL);
12876 else
12877 dw2_asm_output_data_uleb128 (0, NULL);
12879 #ifdef VMS_DEBUGGING_INFO
12880 dw2_asm_output_data_uleb128 (0, NULL);
12881 dw2_asm_output_data_uleb128 (0, NULL);
12882 #endif
12885 /* Now write all the file names. */
12886 for (i = 0; i < numfiles; i++)
12888 int file_idx = backmap[i];
12889 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12891 #ifdef VMS_DEBUGGING_INFO
12892 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12894 /* Setting these fields can lead to debugger miscomparisons,
12895 but VMS Debug requires them to be set correctly. */
12897 int ver;
12898 long long cdt;
12899 long siz;
12900 int maxfilelen = (strlen (files[file_idx].path)
12901 + dirs[dir_idx].length
12902 + MAX_VMS_VERSION_LEN + 1);
12903 char *filebuf = XALLOCAVEC (char, maxfilelen);
12905 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12906 snprintf (filebuf, maxfilelen, "%s;%d",
12907 files[file_idx].path + dirs[dir_idx].length, ver);
12909 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12911 /* Include directory index. */
12912 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12913 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12914 dir_idx + idx_offset, NULL);
12915 else
12916 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12918 /* Modification time. */
12919 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12920 &cdt, 0, 0, 0) == 0)
12921 ? cdt : 0, NULL);
12923 /* File length in bytes. */
12924 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12925 0, &siz, 0, 0) == 0)
12926 ? siz : 0, NULL);
12927 #else
12928 output_line_string (str_form,
12929 files[file_idx].path + dirs[dir_idx].length,
12930 "File Entry", (unsigned) i + 1);
12932 /* Include directory index. */
12933 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12934 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12935 dir_idx + idx_offset, NULL);
12936 else
12937 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12939 if (dwarf_version >= 5)
12940 continue;
12942 /* Modification time. */
12943 dw2_asm_output_data_uleb128 (0, NULL);
12945 /* File length in bytes. */
12946 dw2_asm_output_data_uleb128 (0, NULL);
12947 #endif /* VMS_DEBUGGING_INFO */
12950 if (dwarf_version < 5)
12951 dw2_asm_output_data (1, 0, "End file name table");
12955 /* Output one line number table into the .debug_line section. */
12957 static void
12958 output_one_line_info_table (dw_line_info_table *table)
12960 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12961 unsigned int current_line = 1;
12962 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12963 dw_line_info_entry *ent, *prev_addr = NULL;
12964 size_t i;
12965 unsigned int view;
12967 view = 0;
12969 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12971 switch (ent->opcode)
12973 case LI_set_address:
12974 /* ??? Unfortunately, we have little choice here currently, and
12975 must always use the most general form. GCC does not know the
12976 address delta itself, so we can't use DW_LNS_advance_pc. Many
12977 ports do have length attributes which will give an upper bound
12978 on the address range. We could perhaps use length attributes
12979 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12980 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12982 view = 0;
12984 /* This can handle any delta. This takes
12985 4+DWARF2_ADDR_SIZE bytes. */
12986 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12987 debug_variable_location_views
12988 ? ", reset view to 0" : "");
12989 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12990 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12991 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12993 prev_addr = ent;
12994 break;
12996 case LI_adv_address:
12998 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12999 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
13000 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
13002 view++;
13004 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
13005 dw2_asm_output_delta (2, line_label, prev_label,
13006 "from %s to %s", prev_label, line_label);
13008 prev_addr = ent;
13009 break;
13012 case LI_set_line:
13013 if (ent->val == current_line)
13015 /* We still need to start a new row, so output a copy insn. */
13016 dw2_asm_output_data (1, DW_LNS_copy,
13017 "copy line %u", current_line);
13019 else
13021 int line_offset = ent->val - current_line;
13022 int line_delta = line_offset - DWARF_LINE_BASE;
13024 current_line = ent->val;
13025 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
13027 /* This can handle deltas from -10 to 234, using the current
13028 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
13029 This takes 1 byte. */
13030 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
13031 "line %u", current_line);
13033 else
13035 /* This can handle any delta. This takes at least 4 bytes,
13036 depending on the value being encoded. */
13037 dw2_asm_output_data (1, DW_LNS_advance_line,
13038 "advance to line %u", current_line);
13039 dw2_asm_output_data_sleb128 (line_offset, NULL);
13040 dw2_asm_output_data (1, DW_LNS_copy, NULL);
13043 break;
13045 case LI_set_file:
13046 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
13047 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13048 break;
13050 case LI_set_column:
13051 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
13052 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13053 break;
13055 case LI_negate_stmt:
13056 current_is_stmt = !current_is_stmt;
13057 dw2_asm_output_data (1, DW_LNS_negate_stmt,
13058 "is_stmt %d", current_is_stmt);
13059 break;
13061 case LI_set_prologue_end:
13062 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
13063 "set prologue end");
13064 break;
13066 case LI_set_epilogue_begin:
13067 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
13068 "set epilogue begin");
13069 break;
13071 case LI_set_discriminator:
13072 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
13073 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
13074 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
13075 dw2_asm_output_data_uleb128 (ent->val, NULL);
13076 break;
13080 /* Emit debug info for the address of the end of the table. */
13081 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
13082 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
13083 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13084 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13086 dw2_asm_output_data (1, 0, "end sequence");
13087 dw2_asm_output_data_uleb128 (1, NULL);
13088 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13091 static unsigned int output_line_info_generation;
13093 /* Output the source line number correspondence information. This
13094 information goes into the .debug_line section. */
13096 static void
13097 output_line_info (bool prologue_only)
13099 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13100 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13101 bool saw_one = false;
13102 int opc;
13104 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13105 output_line_info_generation);
13106 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13107 output_line_info_generation);
13108 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13109 output_line_info_generation);
13110 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13111 output_line_info_generation++);
13113 if (!XCOFF_DEBUGGING_INFO)
13115 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13116 dw2_asm_output_data (4, 0xffffffff,
13117 "Initial length escape value indicating 64-bit DWARF extension");
13118 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13119 "Length of Source Line Info");
13122 ASM_OUTPUT_LABEL (asm_out_file, l1);
13124 output_dwarf_version ();
13125 if (dwarf_version >= 5)
13127 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13128 dw2_asm_output_data (1, 0, "Segment Size");
13130 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13131 ASM_OUTPUT_LABEL (asm_out_file, p1);
13133 /* Define the architecture-dependent minimum instruction length (in bytes).
13134 In this implementation of DWARF, this field is used for information
13135 purposes only. Since GCC generates assembly language, we have no
13136 a priori knowledge of how many instruction bytes are generated for each
13137 source line, and therefore can use only the DW_LNE_set_address and
13138 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13139 this as '1', which is "correct enough" for all architectures,
13140 and don't let the target override. */
13141 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13143 if (dwarf_version >= 4)
13144 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13145 "Maximum Operations Per Instruction");
13146 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13147 "Default is_stmt_start flag");
13148 dw2_asm_output_data (1, DWARF_LINE_BASE,
13149 "Line Base Value (Special Opcodes)");
13150 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13151 "Line Range Value (Special Opcodes)");
13152 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13153 "Special Opcode Base");
13155 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13157 int n_op_args;
13158 switch (opc)
13160 case DW_LNS_advance_pc:
13161 case DW_LNS_advance_line:
13162 case DW_LNS_set_file:
13163 case DW_LNS_set_column:
13164 case DW_LNS_fixed_advance_pc:
13165 case DW_LNS_set_isa:
13166 n_op_args = 1;
13167 break;
13168 default:
13169 n_op_args = 0;
13170 break;
13173 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13174 opc, n_op_args);
13177 /* Write out the information about the files we use. */
13178 output_file_names ();
13179 ASM_OUTPUT_LABEL (asm_out_file, p2);
13180 if (prologue_only)
13182 /* Output the marker for the end of the line number info. */
13183 ASM_OUTPUT_LABEL (asm_out_file, l2);
13184 return;
13187 if (separate_line_info)
13189 dw_line_info_table *table;
13190 size_t i;
13192 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13193 if (table->in_use)
13195 output_one_line_info_table (table);
13196 saw_one = true;
13199 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13201 output_one_line_info_table (cold_text_section_line_info);
13202 saw_one = true;
13205 /* ??? Some Darwin linkers crash on a .debug_line section with no
13206 sequences. Further, merely a DW_LNE_end_sequence entry is not
13207 sufficient -- the address column must also be initialized.
13208 Make sure to output at least one set_address/end_sequence pair,
13209 choosing .text since that section is always present. */
13210 if (text_section_line_info->in_use || !saw_one)
13211 output_one_line_info_table (text_section_line_info);
13213 /* Output the marker for the end of the line number info. */
13214 ASM_OUTPUT_LABEL (asm_out_file, l2);
13217 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13219 static inline bool
13220 need_endianity_attribute_p (bool reverse)
13222 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13225 /* Given a pointer to a tree node for some base type, return a pointer to
13226 a DIE that describes the given type. REVERSE is true if the type is
13227 to be interpreted in the reverse storage order wrt the target order.
13229 This routine must only be called for GCC type nodes that correspond to
13230 Dwarf base (fundamental) types. */
13232 dw_die_ref
13233 base_type_die (tree type, bool reverse)
13235 dw_die_ref base_type_result;
13236 enum dwarf_type encoding;
13237 bool fpt_used = false;
13238 struct fixed_point_type_info fpt_info;
13239 tree type_bias = NULL_TREE;
13241 /* If this is a subtype that should not be emitted as a subrange type,
13242 use the base type. See subrange_type_for_debug_p. */
13243 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13244 type = TREE_TYPE (type);
13246 switch (TREE_CODE (type))
13248 case INTEGER_TYPE:
13249 if ((dwarf_version >= 4 || !dwarf_strict)
13250 && TYPE_NAME (type)
13251 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13252 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13253 && DECL_NAME (TYPE_NAME (type)))
13255 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13256 if (strcmp (name, "char16_t") == 0
13257 || strcmp (name, "char8_t") == 0
13258 || strcmp (name, "char32_t") == 0)
13260 encoding = DW_ATE_UTF;
13261 break;
13264 if ((dwarf_version >= 3 || !dwarf_strict)
13265 && lang_hooks.types.get_fixed_point_type_info)
13267 memset (&fpt_info, 0, sizeof (fpt_info));
13268 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13270 fpt_used = true;
13271 encoding = ((TYPE_UNSIGNED (type))
13272 ? DW_ATE_unsigned_fixed
13273 : DW_ATE_signed_fixed);
13274 break;
13277 if (TYPE_STRING_FLAG (type))
13279 if ((dwarf_version >= 4 || !dwarf_strict)
13280 && is_rust ()
13281 && int_size_in_bytes (type) == 4)
13282 encoding = DW_ATE_UTF;
13283 else if (TYPE_UNSIGNED (type))
13284 encoding = DW_ATE_unsigned_char;
13285 else
13286 encoding = DW_ATE_signed_char;
13288 else if (TYPE_UNSIGNED (type))
13289 encoding = DW_ATE_unsigned;
13290 else
13291 encoding = DW_ATE_signed;
13293 if (!dwarf_strict
13294 && lang_hooks.types.get_type_bias)
13295 type_bias = lang_hooks.types.get_type_bias (type);
13296 break;
13298 case REAL_TYPE:
13299 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13301 if (dwarf_version >= 3 || !dwarf_strict)
13302 encoding = DW_ATE_decimal_float;
13303 else
13304 encoding = DW_ATE_lo_user;
13306 else
13307 encoding = DW_ATE_float;
13308 break;
13310 case FIXED_POINT_TYPE:
13311 if (!(dwarf_version >= 3 || !dwarf_strict))
13312 encoding = DW_ATE_lo_user;
13313 else if (TYPE_UNSIGNED (type))
13314 encoding = DW_ATE_unsigned_fixed;
13315 else
13316 encoding = DW_ATE_signed_fixed;
13317 break;
13319 /* Dwarf2 doesn't know anything about complex ints, so use
13320 a user defined type for it. */
13321 case COMPLEX_TYPE:
13322 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (type)))
13323 encoding = DW_ATE_complex_float;
13324 else
13325 encoding = DW_ATE_lo_user;
13326 break;
13328 case BOOLEAN_TYPE:
13329 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13330 encoding = DW_ATE_boolean;
13331 break;
13333 case BITINT_TYPE:
13334 /* C23 _BitInt(N). */
13335 if (TYPE_UNSIGNED (type))
13336 encoding = DW_ATE_unsigned;
13337 else
13338 encoding = DW_ATE_signed;
13339 break;
13341 default:
13342 /* No other TREE_CODEs are Dwarf fundamental types. */
13343 gcc_unreachable ();
13346 base_type_result = new_die_raw (DW_TAG_base_type);
13348 add_AT_unsigned (base_type_result, DW_AT_byte_size,
13349 int_size_in_bytes (type));
13350 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13351 if (TREE_CODE (type) == BITINT_TYPE)
13352 add_AT_unsigned (base_type_result, DW_AT_bit_size, TYPE_PRECISION (type));
13354 if (need_endianity_attribute_p (reverse))
13355 add_AT_unsigned (base_type_result, DW_AT_endianity,
13356 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13358 add_alignment_attribute (base_type_result, type);
13360 if (fpt_used)
13362 switch (fpt_info.scale_factor_kind)
13364 case fixed_point_scale_factor_binary:
13365 add_AT_int (base_type_result, DW_AT_binary_scale,
13366 fpt_info.scale_factor.binary);
13367 break;
13369 case fixed_point_scale_factor_decimal:
13370 add_AT_int (base_type_result, DW_AT_decimal_scale,
13371 fpt_info.scale_factor.decimal);
13372 break;
13374 case fixed_point_scale_factor_arbitrary:
13375 /* Arbitrary scale factors cannot be described in standard DWARF. */
13376 if (!dwarf_strict)
13378 /* Describe the scale factor as a rational constant. */
13379 const dw_die_ref scale_factor
13380 = new_die (DW_TAG_constant, comp_unit_die (), type);
13382 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13383 fpt_info.scale_factor.arbitrary.numerator,
13384 dw_scalar_form_constant, NULL);
13385 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13386 fpt_info.scale_factor.arbitrary.denominator,
13387 dw_scalar_form_constant, NULL);
13389 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13391 break;
13393 default:
13394 gcc_unreachable ();
13398 if (type_bias)
13399 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13400 dw_scalar_form_constant
13401 | dw_scalar_form_exprloc
13402 | dw_scalar_form_reference,
13403 NULL);
13405 return base_type_result;
13408 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13409 named 'auto' in its type: return true for it, false otherwise. */
13411 static inline bool
13412 is_cxx_auto (tree type)
13414 if (is_cxx ())
13416 tree name = TYPE_IDENTIFIER (type);
13417 if (name == get_identifier ("auto")
13418 || name == get_identifier ("decltype(auto)"))
13419 return true;
13421 return false;
13424 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if the
13425 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13427 static inline bool
13428 is_base_type (tree type)
13430 switch (TREE_CODE (type))
13432 case INTEGER_TYPE:
13433 case REAL_TYPE:
13434 case FIXED_POINT_TYPE:
13435 case COMPLEX_TYPE:
13436 case BOOLEAN_TYPE:
13437 case BITINT_TYPE:
13438 return true;
13440 case VOID_TYPE:
13441 case OPAQUE_TYPE:
13442 case ARRAY_TYPE:
13443 case RECORD_TYPE:
13444 case UNION_TYPE:
13445 case QUAL_UNION_TYPE:
13446 case ENUMERAL_TYPE:
13447 case FUNCTION_TYPE:
13448 case METHOD_TYPE:
13449 case POINTER_TYPE:
13450 case REFERENCE_TYPE:
13451 case NULLPTR_TYPE:
13452 case OFFSET_TYPE:
13453 case LANG_TYPE:
13454 case VECTOR_TYPE:
13455 return false;
13457 default:
13458 if (is_cxx_auto (type))
13459 return false;
13460 gcc_unreachable ();
13464 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13465 node, return the size in bits for the type if it is a constant, or else
13466 return the alignment for the type if the type's size is not constant, or
13467 else return BITS_PER_WORD if the type actually turns out to be an
13468 ERROR_MARK node. */
13470 static inline unsigned HOST_WIDE_INT
13471 simple_type_size_in_bits (const_tree type)
13473 if (TREE_CODE (type) == ERROR_MARK)
13474 return BITS_PER_WORD;
13475 else if (TYPE_SIZE (type) == NULL_TREE)
13476 return 0;
13477 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13478 return tree_to_uhwi (TYPE_SIZE (type));
13479 else
13480 return TYPE_ALIGN (type);
13483 /* Similarly, but return an offset_int instead of UHWI. */
13485 static inline offset_int
13486 offset_int_type_size_in_bits (const_tree type)
13488 if (TREE_CODE (type) == ERROR_MARK)
13489 return BITS_PER_WORD;
13490 else if (TYPE_SIZE (type) == NULL_TREE)
13491 return 0;
13492 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13493 return wi::to_offset (TYPE_SIZE (type));
13494 else
13495 return TYPE_ALIGN (type);
13498 /* Given a pointer to a tree node for a subrange type, return a pointer
13499 to a DIE that describes the given type. */
13501 static dw_die_ref
13502 subrange_type_die (tree type, tree low, tree high, tree bias,
13503 dw_die_ref context_die)
13505 dw_die_ref subrange_die;
13506 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13508 if (context_die == NULL)
13509 context_die = comp_unit_die ();
13511 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13513 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13515 /* The size of the subrange type and its base type do not match,
13516 so we need to generate a size attribute for the subrange type. */
13517 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13520 add_alignment_attribute (subrange_die, type);
13522 if (low)
13523 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13524 if (high)
13525 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13526 if (bias && !dwarf_strict)
13527 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13528 dw_scalar_form_constant
13529 | dw_scalar_form_exprloc
13530 | dw_scalar_form_reference,
13531 NULL);
13533 return subrange_die;
13536 /* Returns the (const and/or volatile) cv_qualifiers associated with
13537 the decl node. This will normally be augmented with the
13538 cv_qualifiers of the underlying type in add_type_attribute. */
13540 static int
13541 decl_quals (const_tree decl)
13543 return ((TREE_READONLY (decl)
13544 /* The C++ front-end correctly marks reference-typed
13545 variables as readonly, but from a language (and debug
13546 info) standpoint they are not const-qualified. */
13547 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13548 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13549 | (TREE_THIS_VOLATILE (decl)
13550 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13553 /* Determine the TYPE whose qualifiers match the largest strict subset
13554 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13555 qualifiers outside QUAL_MASK. */
13557 static int
13558 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13560 tree t;
13561 int best_rank = 0, best_qual = 0, max_rank;
13563 type_quals &= qual_mask;
13564 max_rank = popcount_hwi (type_quals) - 1;
13566 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13567 t = TYPE_NEXT_VARIANT (t))
13569 int q = TYPE_QUALS (t) & qual_mask;
13571 if ((q & type_quals) == q && q != type_quals
13572 && check_base_type (t, type))
13574 int rank = popcount_hwi (q);
13576 if (rank > best_rank)
13578 best_rank = rank;
13579 best_qual = q;
13584 return best_qual;
13587 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13588 static const dwarf_qual_info_t dwarf_qual_info[] =
13590 { TYPE_QUAL_CONST, DW_TAG_const_type },
13591 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13592 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13593 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13595 static const unsigned int dwarf_qual_info_size = ARRAY_SIZE (dwarf_qual_info);
13597 /* If DIE is a qualified DIE of some base DIE with the same parent,
13598 return the base DIE, otherwise return NULL. Set MASK to the
13599 qualifiers added compared to the returned DIE. */
13601 static dw_die_ref
13602 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13604 unsigned int i;
13605 for (i = 0; i < dwarf_qual_info_size; i++)
13606 if (die->die_tag == dwarf_qual_info[i].t)
13607 break;
13608 if (i == dwarf_qual_info_size)
13609 return NULL;
13610 if (vec_safe_length (die->die_attr) != 1)
13611 return NULL;
13612 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13613 if (type == NULL || type->die_parent != die->die_parent)
13614 return NULL;
13615 *mask |= dwarf_qual_info[i].q;
13616 if (depth)
13618 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13619 if (ret)
13620 return ret;
13622 return type;
13625 /* If TYPE is long double or complex long double that
13626 should be emitted as artificial typedef to _Float128 or
13627 complex _Float128, return the type it should be emitted as.
13628 This is done in case the target already supports 16-byte
13629 composite floating point type (ibm_extended_format). */
13631 static tree
13632 long_double_as_float128 (tree type)
13634 if (type != long_double_type_node
13635 && type != complex_long_double_type_node)
13636 return NULL_TREE;
13638 machine_mode mode, fmode;
13639 if (TREE_CODE (type) == COMPLEX_TYPE)
13640 mode = TYPE_MODE (TREE_TYPE (type));
13641 else
13642 mode = TYPE_MODE (type);
13643 if (known_eq (GET_MODE_SIZE (mode), 16) && !MODE_COMPOSITE_P (mode))
13644 FOR_EACH_MODE_IN_CLASS (fmode, MODE_FLOAT)
13645 if (known_eq (GET_MODE_SIZE (fmode), 16)
13646 && MODE_COMPOSITE_P (fmode))
13648 if (type == long_double_type_node)
13650 if (float128_type_node
13651 && (TYPE_MODE (float128_type_node)
13652 == TYPE_MODE (type)))
13653 return float128_type_node;
13654 return NULL_TREE;
13656 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
13657 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
13658 && (TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i))
13659 == TYPE_MODE (type)))
13660 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
13663 return NULL_TREE;
13666 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13667 entry that chains the modifiers specified by CV_QUALS in front of the
13668 given type. REVERSE is true if the type is to be interpreted in the
13669 reverse storage order wrt the target order. */
13671 static dw_die_ref
13672 modified_type_die (tree type, int cv_quals, bool reverse,
13673 dw_die_ref context_die)
13675 enum tree_code code = TREE_CODE (type);
13676 dw_die_ref mod_type_die;
13677 dw_die_ref sub_die = NULL;
13678 tree item_type = NULL;
13679 tree qualified_type;
13680 tree name, low, high;
13681 dw_die_ref mod_scope;
13682 struct array_descr_info info;
13683 /* Only these cv-qualifiers are currently handled. */
13684 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13685 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13686 ENCODE_QUAL_ADDR_SPACE(~0U));
13687 /* DW_AT_endianity is specified only for base types in the standard. */
13688 const bool reverse_type
13689 = need_endianity_attribute_p (reverse)
13690 && (is_base_type (type)
13691 || (TREE_CODE (type) == ENUMERAL_TYPE && !dwarf_strict));
13693 if (code == ERROR_MARK)
13694 return NULL;
13696 if (lang_hooks.types.get_debug_type)
13698 tree debug_type = lang_hooks.types.get_debug_type (type);
13700 if (debug_type != NULL_TREE && debug_type != type)
13701 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13704 cv_quals &= cv_qual_mask;
13706 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13707 tag modifier (and not an attribute) old consumers won't be able
13708 to handle it. */
13709 if (dwarf_version < 3)
13710 cv_quals &= ~TYPE_QUAL_RESTRICT;
13712 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13713 if (dwarf_version < 5)
13714 cv_quals &= ~TYPE_QUAL_ATOMIC;
13716 /* See if we already have the appropriately qualified variant of
13717 this type. */
13718 qualified_type = get_qualified_type (type, cv_quals);
13720 if (qualified_type == sizetype)
13722 /* Try not to expose the internal sizetype type's name. */
13723 if (TYPE_NAME (qualified_type)
13724 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13726 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13728 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13729 && (TYPE_PRECISION (t)
13730 == TYPE_PRECISION (qualified_type))
13731 && (TYPE_UNSIGNED (t)
13732 == TYPE_UNSIGNED (qualified_type)));
13733 qualified_type = t;
13735 else if (qualified_type == sizetype
13736 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13737 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13738 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13739 qualified_type = size_type_node;
13740 if (type == sizetype)
13741 type = qualified_type;
13744 /* If we do, then we can just use its DIE, if it exists. */
13745 if (qualified_type)
13747 mod_type_die = lookup_type_die (qualified_type);
13749 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13750 dealt with specially: the DIE with the attribute, if it exists, is
13751 placed immediately after the regular DIE for the same type. */
13752 if (mod_type_die
13753 && (!reverse_type
13754 || ((mod_type_die = mod_type_die->die_sib) != NULL
13755 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13756 return mod_type_die;
13759 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13761 /* Handle C typedef types. */
13762 if (name
13763 && TREE_CODE (name) == TYPE_DECL
13764 && DECL_ORIGINAL_TYPE (name)
13765 && !DECL_ARTIFICIAL (name))
13767 tree dtype = TREE_TYPE (name);
13769 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13770 if (qualified_type == dtype && !reverse_type)
13772 tree origin = decl_ultimate_origin (name);
13774 /* Typedef variants that have an abstract origin don't get their own
13775 type DIE (see gen_typedef_die), so fall back on the ultimate
13776 abstract origin instead. */
13777 if (origin != NULL && origin != name)
13778 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13779 context_die);
13781 /* For a named type, use the typedef. */
13782 gen_type_die (qualified_type, context_die);
13783 return lookup_type_die (qualified_type);
13785 else
13787 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13788 dquals &= cv_qual_mask;
13789 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13790 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13791 /* cv-unqualified version of named type. Just use
13792 the unnamed type to which it refers. */
13793 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13794 reverse, context_die);
13795 /* Else cv-qualified version of named type; fall through. */
13799 mod_scope = scope_die_for (type, context_die);
13801 if (cv_quals)
13803 int sub_quals = 0, first_quals = 0;
13804 unsigned i;
13805 dw_die_ref first = NULL, last = NULL;
13807 /* Determine a lesser qualified type that most closely matches
13808 this one. Then generate DW_TAG_* entries for the remaining
13809 qualifiers. */
13810 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13811 cv_qual_mask);
13812 if (sub_quals && use_debug_types)
13814 bool needed = false;
13815 /* If emitting type units, make sure the order of qualifiers
13816 is canonical. Thus, start from unqualified type if
13817 an earlier qualifier is missing in sub_quals, but some later
13818 one is present there. */
13819 for (i = 0; i < dwarf_qual_info_size; i++)
13820 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13821 needed = true;
13822 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13824 sub_quals = 0;
13825 break;
13828 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13829 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13831 /* As not all intermediate qualified DIEs have corresponding
13832 tree types, ensure that qualified DIEs in the same scope
13833 as their DW_AT_type are emitted after their DW_AT_type,
13834 only with other qualified DIEs for the same type possibly
13835 in between them. Determine the range of such qualified
13836 DIEs now (first being the base type, last being corresponding
13837 last qualified DIE for it). */
13838 unsigned int count = 0;
13839 first = qualified_die_p (mod_type_die, &first_quals,
13840 dwarf_qual_info_size);
13841 if (first == NULL)
13842 first = mod_type_die;
13843 gcc_assert ((first_quals & ~sub_quals) == 0);
13844 for (count = 0, last = first;
13845 count < (1U << dwarf_qual_info_size);
13846 count++, last = last->die_sib)
13848 int quals = 0;
13849 if (last == mod_scope->die_child)
13850 break;
13851 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13852 != first)
13853 break;
13857 for (i = 0; i < dwarf_qual_info_size; i++)
13858 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13860 dw_die_ref d;
13861 if (first && first != last)
13863 for (d = first->die_sib; ; d = d->die_sib)
13865 int quals = 0;
13866 qualified_die_p (d, &quals, dwarf_qual_info_size);
13867 if (quals == (first_quals | dwarf_qual_info[i].q))
13868 break;
13869 if (d == last)
13871 d = NULL;
13872 break;
13875 if (d)
13877 mod_type_die = d;
13878 continue;
13881 if (first)
13883 d = new_die_raw (dwarf_qual_info[i].t);
13884 add_child_die_after (mod_scope, d, last);
13885 last = d;
13887 else
13888 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13889 if (mod_type_die)
13890 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13891 mod_type_die = d;
13892 first_quals |= dwarf_qual_info[i].q;
13895 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13897 dwarf_tag tag = DW_TAG_pointer_type;
13898 if (code == REFERENCE_TYPE)
13900 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13901 tag = DW_TAG_rvalue_reference_type;
13902 else
13903 tag = DW_TAG_reference_type;
13905 mod_type_die = new_die (tag, mod_scope, type);
13907 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13908 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13909 add_alignment_attribute (mod_type_die, type);
13910 item_type = TREE_TYPE (type);
13912 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13913 if (!ADDR_SPACE_GENERIC_P (as))
13915 int action = targetm.addr_space.debug (as);
13916 if (action >= 0)
13918 /* Positive values indicate an address_class. */
13919 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13921 else
13923 /* Negative values indicate an (inverted) segment base reg. */
13924 dw_loc_descr_ref d
13925 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13926 add_AT_loc (mod_type_die, DW_AT_segment, d);
13930 else if (code == ARRAY_TYPE
13931 || (lang_hooks.types.get_array_descr_info
13932 && lang_hooks.types.get_array_descr_info (type, &info)))
13934 gen_type_die (type, context_die);
13935 return lookup_type_die (type);
13937 else if (code == INTEGER_TYPE
13938 && TREE_TYPE (type) != NULL_TREE
13939 && subrange_type_for_debug_p (type, &low, &high))
13941 tree bias = NULL_TREE;
13942 if (lang_hooks.types.get_type_bias)
13943 bias = lang_hooks.types.get_type_bias (type);
13944 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13945 item_type = TREE_TYPE (type);
13947 else if (is_base_type (type))
13949 /* If a target supports long double as different floating point
13950 modes with the same 16-byte size, use normal DW_TAG_base_type
13951 only for the composite (ibm_extended_real_format) type and
13952 for the other for the time being emit instead a "_Float128"
13953 or "complex _Float128" DW_TAG_base_type and a "long double"
13954 or "complex long double" typedef to it. */
13955 if (tree other_type = long_double_as_float128 (type))
13957 dw_die_ref other_die;
13958 if (TYPE_NAME (other_type))
13959 other_die
13960 = modified_type_die (other_type, TYPE_UNQUALIFIED, reverse,
13961 context_die);
13962 else
13964 other_die = base_type_die (type, reverse);
13965 add_child_die (comp_unit_die (), other_die);
13966 add_name_attribute (other_die,
13967 TREE_CODE (type) == COMPLEX_TYPE
13968 ? "complex _Float128" : "_Float128");
13970 mod_type_die = new_die_raw (DW_TAG_typedef);
13971 add_AT_die_ref (mod_type_die, DW_AT_type, other_die);
13973 else
13974 mod_type_die = base_type_die (type, reverse);
13976 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13977 if (reverse_type)
13979 dw_die_ref after_die
13980 = modified_type_die (type, cv_quals, false, context_die);
13981 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13983 else
13984 add_child_die (comp_unit_die (), mod_type_die);
13986 add_pubtype (type, mod_type_die);
13988 else
13990 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13991 if (reverse_type)
13993 dw_die_ref after_die
13994 = modified_type_die (type, cv_quals, false, context_die);
13995 gen_type_die (type, context_die, true);
13996 gcc_assert (after_die->die_sib
13997 && get_AT_unsigned (after_die->die_sib, DW_AT_endianity));
13998 return after_die->die_sib;
14001 gen_type_die (type, context_die);
14003 /* We have to get the type_main_variant here (and pass that to the
14004 `lookup_type_die' routine) because the ..._TYPE node we have
14005 might simply be a *copy* of some original type node (where the
14006 copy was created to help us keep track of typedef names) and
14007 that copy might have a different TYPE_UID from the original
14008 ..._TYPE node. */
14009 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
14011 /* For function/method types, can't just use type_main_variant here,
14012 because that can have different ref-qualifiers for C++,
14013 but try to canonicalize. */
14014 tree main = TYPE_MAIN_VARIANT (type);
14015 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
14016 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
14017 && check_base_type (t, main)
14018 && check_lang_type (t, type))
14019 return lookup_type_die (t);
14020 return lookup_type_die (type);
14022 /* Vectors have the debugging information in the type,
14023 not the main variant. */
14024 else if (code == VECTOR_TYPE)
14025 return lookup_type_die (type);
14026 else
14027 return lookup_type_die (type_main_variant (type));
14030 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
14031 don't output a DW_TAG_typedef, since there isn't one in the
14032 user's program; just attach a DW_AT_name to the type.
14033 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
14034 if the base type already has the same name. */
14035 if (name
14036 && ((TREE_CODE (name) != TYPE_DECL
14037 && (qualified_type == TYPE_MAIN_VARIANT (type)
14038 || (cv_quals == TYPE_UNQUALIFIED)))
14039 || (TREE_CODE (name) == TYPE_DECL
14040 && TREE_TYPE (name) == qualified_type
14041 && DECL_NAME (name))))
14043 if (TREE_CODE (name) == TYPE_DECL)
14044 /* Could just call add_name_and_src_coords_attributes here,
14045 but since this is a builtin type it doesn't have any
14046 useful source coordinates anyway. */
14047 name = DECL_NAME (name);
14048 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
14050 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
14052 if (TREE_CODE (type) == BITINT_TYPE)
14054 char name_buf[sizeof ("unsigned _BitInt(2147483647)")];
14055 snprintf (name_buf, sizeof (name_buf),
14056 "%s_BitInt(%d)", TYPE_UNSIGNED (type) ? "unsigned " : "",
14057 TYPE_PRECISION (type));
14058 add_name_attribute (mod_type_die, name_buf);
14060 else
14062 /* This probably indicates a bug. */
14063 name = TYPE_IDENTIFIER (type);
14064 add_name_attribute (mod_type_die,
14065 name
14066 ? IDENTIFIER_POINTER (name) : "__unknown__");
14070 if (qualified_type && !reverse_type)
14071 equate_type_number_to_die (qualified_type, mod_type_die);
14073 if (item_type)
14074 /* We must do this after the equate_type_number_to_die call, in case
14075 this is a recursive type. This ensures that the modified_type_die
14076 recursion will terminate even if the type is recursive. Recursive
14077 types are possible in Ada. */
14078 sub_die = modified_type_die (item_type,
14079 TYPE_QUALS_NO_ADDR_SPACE (item_type),
14080 reverse,
14081 context_die);
14083 if (sub_die != NULL)
14084 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
14086 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
14087 if (TYPE_ARTIFICIAL (type))
14088 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
14090 return mod_type_die;
14093 /* Generate DIEs for the generic parameters of T.
14094 T must be either a generic type or a generic function.
14095 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
14097 static void
14098 gen_generic_params_dies (tree t)
14100 tree parms, args;
14101 int parms_num, i;
14102 dw_die_ref die = NULL;
14103 int non_default;
14105 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
14106 return;
14108 if (TYPE_P (t))
14109 die = lookup_type_die (t);
14110 else if (DECL_P (t))
14111 die = lookup_decl_die (t);
14113 gcc_assert (die);
14115 parms = lang_hooks.get_innermost_generic_parms (t);
14116 if (!parms)
14117 /* T has no generic parameter. It means T is neither a generic type
14118 or function. End of story. */
14119 return;
14121 parms_num = TREE_VEC_LENGTH (parms);
14122 args = lang_hooks.get_innermost_generic_args (t);
14123 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
14124 non_default = int_cst_value (TREE_CHAIN (args));
14125 else
14126 non_default = TREE_VEC_LENGTH (args);
14127 for (i = 0; i < parms_num; i++)
14129 tree parm, arg, arg_pack_elems;
14130 dw_die_ref parm_die;
14132 parm = TREE_VEC_ELT (parms, i);
14133 arg = TREE_VEC_ELT (args, i);
14134 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
14135 gcc_assert (parm && TREE_VALUE (parm) && arg);
14137 if (parm && TREE_VALUE (parm) && arg)
14139 /* If PARM represents a template parameter pack,
14140 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
14141 by DW_TAG_template_*_parameter DIEs for the argument
14142 pack elements of ARG. Note that ARG would then be
14143 an argument pack. */
14144 if (arg_pack_elems)
14145 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
14146 arg_pack_elems,
14147 die);
14148 else
14149 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
14150 true /* emit name */, die);
14151 if (i >= non_default)
14152 add_AT_flag (parm_die, DW_AT_default_value, 1);
14157 /* Create and return a DIE for PARM which should be
14158 the representation of a generic type parameter.
14159 For instance, in the C++ front end, PARM would be a template parameter.
14160 ARG is the argument to PARM.
14161 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
14162 name of the PARM.
14163 PARENT_DIE is the parent DIE which the new created DIE should be added to,
14164 as a child node. */
14166 static dw_die_ref
14167 generic_parameter_die (tree parm, tree arg,
14168 bool emit_name_p,
14169 dw_die_ref parent_die)
14171 dw_die_ref tmpl_die = NULL;
14172 const char *name = NULL;
14174 /* C++20 accepts class literals as template parameters, and var
14175 decls with initializers represent them. The VAR_DECLs would be
14176 rejected, but we can take the DECL_INITIAL constructor and
14177 attempt to expand it. */
14178 if (arg && VAR_P (arg))
14179 arg = DECL_INITIAL (arg);
14181 if (!parm || !DECL_NAME (parm) || !arg)
14182 return NULL;
14184 /* We support non-type generic parameters and arguments,
14185 type generic parameters and arguments, as well as
14186 generic generic parameters (a.k.a. template template parameters in C++)
14187 and arguments. */
14188 if (TREE_CODE (parm) == PARM_DECL)
14189 /* PARM is a nontype generic parameter */
14190 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
14191 else if (TREE_CODE (parm) == TYPE_DECL)
14192 /* PARM is a type generic parameter. */
14193 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14194 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14195 /* PARM is a generic generic parameter.
14196 Its DIE is a GNU extension. It shall have a
14197 DW_AT_name attribute to represent the name of the template template
14198 parameter, and a DW_AT_GNU_template_name attribute to represent the
14199 name of the template template argument. */
14200 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14201 parent_die, parm);
14202 else
14203 gcc_unreachable ();
14205 if (tmpl_die)
14207 tree tmpl_type;
14209 /* If PARM is a generic parameter pack, it means we are
14210 emitting debug info for a template argument pack element.
14211 In other terms, ARG is a template argument pack element.
14212 In that case, we don't emit any DW_AT_name attribute for
14213 the die. */
14214 if (emit_name_p)
14216 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14217 gcc_assert (name);
14218 add_AT_string (tmpl_die, DW_AT_name, name);
14221 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14223 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14224 TMPL_DIE should have a child DW_AT_type attribute that is set
14225 to the type of the argument to PARM, which is ARG.
14226 If PARM is a type generic parameter, TMPL_DIE should have a
14227 child DW_AT_type that is set to ARG. */
14228 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14229 add_type_attribute (tmpl_die, tmpl_type,
14230 (TREE_THIS_VOLATILE (tmpl_type)
14231 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14232 false, parent_die);
14234 else
14236 /* So TMPL_DIE is a DIE representing a
14237 a generic generic template parameter, a.k.a template template
14238 parameter in C++ and arg is a template. */
14240 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14241 to the name of the argument. */
14242 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14243 if (name)
14244 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14247 if (TREE_CODE (parm) == PARM_DECL)
14248 /* So PARM is a non-type generic parameter.
14249 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14250 attribute of TMPL_DIE which value represents the value
14251 of ARG.
14252 We must be careful here:
14253 The value of ARG might reference some function decls.
14254 We might currently be emitting debug info for a generic
14255 type and types are emitted before function decls, we don't
14256 know if the function decls referenced by ARG will actually be
14257 emitted after cgraph computations.
14258 So must defer the generation of the DW_AT_const_value to
14259 after cgraph is ready. */
14260 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14263 return tmpl_die;
14266 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14267 PARM_PACK must be a template parameter pack. The returned DIE
14268 will be child DIE of PARENT_DIE. */
14270 static dw_die_ref
14271 template_parameter_pack_die (tree parm_pack,
14272 tree parm_pack_args,
14273 dw_die_ref parent_die)
14275 dw_die_ref die;
14276 int j;
14278 gcc_assert (parent_die && parm_pack);
14280 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14281 add_name_and_src_coords_attributes (die, parm_pack);
14282 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14283 generic_parameter_die (parm_pack,
14284 TREE_VEC_ELT (parm_pack_args, j),
14285 false /* Don't emit DW_AT_name */,
14286 die);
14287 return die;
14290 /* Return the debugger register number described by a given RTL node. */
14292 static unsigned int
14293 debugger_reg_number (const_rtx rtl)
14295 unsigned regno = REGNO (rtl);
14297 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14299 #ifdef LEAF_REG_REMAP
14300 if (crtl->uses_only_leaf_regs)
14302 int leaf_reg = LEAF_REG_REMAP (regno);
14303 if (leaf_reg != -1)
14304 regno = (unsigned) leaf_reg;
14306 #endif
14308 regno = DEBUGGER_REGNO (regno);
14309 gcc_assert (regno != INVALID_REGNUM);
14310 return regno;
14313 /* Optionally add a DW_OP_piece term to a location description expression.
14314 DW_OP_piece is only added if the location description expression already
14315 doesn't end with DW_OP_piece. */
14317 static void
14318 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14320 dw_loc_descr_ref loc;
14322 if (*list_head != NULL)
14324 /* Find the end of the chain. */
14325 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14328 if (loc->dw_loc_opc != DW_OP_piece)
14329 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14333 /* Return a location descriptor that designates a machine register or
14334 zero if there is none. */
14336 static dw_loc_descr_ref
14337 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14339 rtx regs;
14341 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14342 return 0;
14344 /* We only use "frame base" when we're sure we're talking about the
14345 post-prologue local stack frame. We do this by *not* running
14346 register elimination until this point, and recognizing the special
14347 argument pointer and soft frame pointer rtx's.
14348 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14349 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14350 && (ira_use_lra_p
14351 ? lra_eliminate_regs (rtl, VOIDmode, NULL_RTX)
14352 : eliminate_regs (rtl, VOIDmode, NULL_RTX)) != rtl)
14354 dw_loc_descr_ref result = NULL;
14356 if (dwarf_version >= 4 || !dwarf_strict)
14358 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14359 initialized);
14360 if (result)
14361 add_loc_descr (&result,
14362 new_loc_descr (DW_OP_stack_value, 0, 0));
14364 return result;
14367 regs = targetm.dwarf_register_span (rtl);
14369 if (REG_NREGS (rtl) > 1 || regs)
14370 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14371 else
14373 unsigned int debugger_regnum = debugger_reg_number (rtl);
14374 if (debugger_regnum == IGNORED_DWARF_REGNUM)
14375 return 0;
14376 return one_reg_loc_descriptor (debugger_regnum, initialized);
14380 /* Return a location descriptor that designates a machine register for
14381 a given hard register number. */
14383 static dw_loc_descr_ref
14384 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14386 dw_loc_descr_ref reg_loc_descr;
14388 if (regno <= 31)
14389 reg_loc_descr
14390 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14391 else
14392 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14394 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14395 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14397 return reg_loc_descr;
14400 /* Given an RTL of a register, return a location descriptor that
14401 designates a value that spans more than one register. */
14403 static dw_loc_descr_ref
14404 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14405 enum var_init_status initialized)
14407 int size, i;
14408 dw_loc_descr_ref loc_result = NULL;
14410 /* Simple, contiguous registers. */
14411 if (regs == NULL_RTX)
14413 unsigned reg = REGNO (rtl);
14414 int nregs;
14416 #ifdef LEAF_REG_REMAP
14417 if (crtl->uses_only_leaf_regs)
14419 int leaf_reg = LEAF_REG_REMAP (reg);
14420 if (leaf_reg != -1)
14421 reg = (unsigned) leaf_reg;
14423 #endif
14425 gcc_assert ((unsigned) DEBUGGER_REGNO (reg) == debugger_reg_number (rtl));
14426 nregs = REG_NREGS (rtl);
14428 /* At present we only track constant-sized pieces. */
14429 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14430 return NULL;
14431 size /= nregs;
14433 loc_result = NULL;
14434 while (nregs--)
14436 dw_loc_descr_ref t;
14438 t = one_reg_loc_descriptor (DEBUGGER_REGNO (reg),
14439 VAR_INIT_STATUS_INITIALIZED);
14440 add_loc_descr (&loc_result, t);
14441 add_loc_descr_op_piece (&loc_result, size);
14442 ++reg;
14444 return loc_result;
14447 /* Now onto stupid register sets in non contiguous locations. */
14449 gcc_assert (GET_CODE (regs) == PARALLEL);
14451 /* At present we only track constant-sized pieces. */
14452 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14453 return NULL;
14454 loc_result = NULL;
14456 for (i = 0; i < XVECLEN (regs, 0); ++i)
14458 dw_loc_descr_ref t;
14460 t = one_reg_loc_descriptor (debugger_reg_number (XVECEXP (regs, 0, i)),
14461 VAR_INIT_STATUS_INITIALIZED);
14462 add_loc_descr (&loc_result, t);
14463 add_loc_descr_op_piece (&loc_result, size);
14466 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14467 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14468 return loc_result;
14471 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14473 /* Return a location descriptor that designates a constant i,
14474 as a compound operation from constant (i >> shift), constant shift
14475 and DW_OP_shl. */
14477 static dw_loc_descr_ref
14478 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14480 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14481 add_loc_descr (&ret, int_loc_descriptor (shift));
14482 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14483 return ret;
14486 /* Return a location descriptor that designates constant POLY_I. */
14488 static dw_loc_descr_ref
14489 int_loc_descriptor (poly_int64 poly_i)
14491 enum dwarf_location_atom op;
14493 HOST_WIDE_INT i;
14494 if (!poly_i.is_constant (&i))
14496 /* Create location descriptions for the non-constant part and
14497 add any constant offset at the end. */
14498 dw_loc_descr_ref ret = NULL;
14499 HOST_WIDE_INT constant = poly_i.coeffs[0];
14500 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14502 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14503 if (coeff != 0)
14505 dw_loc_descr_ref start = ret;
14506 unsigned int factor;
14507 int bias;
14508 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14509 (j, &factor, &bias);
14511 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14512 add COEFF * (REGNO / FACTOR) now and subtract
14513 COEFF * BIAS from the final constant part. */
14514 constant -= coeff * bias;
14515 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14516 if (coeff % factor == 0)
14517 coeff /= factor;
14518 else
14520 int amount = exact_log2 (factor);
14521 gcc_assert (amount >= 0);
14522 add_loc_descr (&ret, int_loc_descriptor (amount));
14523 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14525 if (coeff != 1)
14527 add_loc_descr (&ret, int_loc_descriptor (coeff));
14528 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14530 if (start)
14531 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14534 loc_descr_plus_const (&ret, constant);
14535 return ret;
14538 /* Pick the smallest representation of a constant, rather than just
14539 defaulting to the LEB encoding. */
14540 if (i >= 0)
14542 int clz = clz_hwi (i);
14543 int ctz = ctz_hwi (i);
14544 if (i <= 31)
14545 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14546 else if (i <= 0xff)
14547 op = DW_OP_const1u;
14548 else if (i <= 0xffff)
14549 op = DW_OP_const2u;
14550 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14551 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14552 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14553 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14554 while DW_OP_const4u is 5 bytes. */
14555 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14556 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14557 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14558 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14559 while DW_OP_const4u is 5 bytes. */
14560 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14562 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14563 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14564 <= 4)
14566 /* As i >= 2**31, the double cast above will yield a negative number.
14567 Since wrapping is defined in DWARF expressions we can output big
14568 positive integers as small negative ones, regardless of the size
14569 of host wide ints.
14571 Here, since the evaluator will handle 32-bit values and since i >=
14572 2**31, we know it's going to be interpreted as a negative literal:
14573 store it this way if we can do better than 5 bytes this way. */
14574 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14576 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14577 op = DW_OP_const4u;
14579 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14580 least 6 bytes: see if we can do better before falling back to it. */
14581 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14582 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14583 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14584 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14585 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14586 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14587 >= HOST_BITS_PER_WIDE_INT)
14588 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14589 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14590 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14591 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14592 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14593 && size_of_uleb128 (i) > 6)
14594 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14595 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14596 else
14597 op = DW_OP_constu;
14599 else
14601 if (i >= -0x80)
14602 op = DW_OP_const1s;
14603 else if (i >= -0x8000)
14604 op = DW_OP_const2s;
14605 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14607 if (size_of_int_loc_descriptor (i) < 5)
14609 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14610 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14611 return ret;
14613 op = DW_OP_const4s;
14615 else
14617 if (size_of_int_loc_descriptor (i)
14618 < (unsigned long) 1 + size_of_sleb128 (i))
14620 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14621 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14622 return ret;
14624 op = DW_OP_consts;
14628 return new_loc_descr (op, i, 0);
14631 /* Likewise, for unsigned constants. */
14633 static dw_loc_descr_ref
14634 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14636 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14637 const unsigned HOST_WIDE_INT max_uint
14638 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14640 /* If possible, use the clever signed constants handling. */
14641 if (i <= max_int)
14642 return int_loc_descriptor ((HOST_WIDE_INT) i);
14644 /* Here, we are left with positive numbers that cannot be represented as
14645 HOST_WIDE_INT, i.e.:
14646 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14648 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14649 whereas may be better to output a negative integer: thanks to integer
14650 wrapping, we know that:
14651 x = x - 2 ** DWARF2_ADDR_SIZE
14652 = x - 2 * (max (HOST_WIDE_INT) + 1)
14653 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14654 small negative integers. Let's try that in cases it will clearly improve
14655 the encoding: there is no gain turning DW_OP_const4u into
14656 DW_OP_const4s. */
14657 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14658 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14659 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14661 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14663 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14664 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14665 const HOST_WIDE_INT second_shift
14666 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14668 /* So we finally have:
14669 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14670 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14671 return int_loc_descriptor (second_shift);
14674 /* Last chance: fallback to a simple constant operation. */
14675 return new_loc_descr
14676 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14677 ? DW_OP_const4u
14678 : DW_OP_const8u,
14679 i, 0);
14682 /* Generate and return a location description that computes the unsigned
14683 comparison of the two stack top entries (a OP b where b is the top-most
14684 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14685 LE_EXPR, GT_EXPR or GE_EXPR. */
14687 static dw_loc_descr_ref
14688 uint_comparison_loc_list (enum tree_code kind)
14690 enum dwarf_location_atom op, flip_op;
14691 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14693 switch (kind)
14695 case LT_EXPR:
14696 op = DW_OP_lt;
14697 break;
14698 case LE_EXPR:
14699 op = DW_OP_le;
14700 break;
14701 case GT_EXPR:
14702 op = DW_OP_gt;
14703 break;
14704 case GE_EXPR:
14705 op = DW_OP_ge;
14706 break;
14707 default:
14708 gcc_unreachable ();
14711 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14712 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14714 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14715 possible to perform unsigned comparisons: we just have to distinguish
14716 three cases:
14718 1. when a and b have the same sign (as signed integers); then we should
14719 return: a OP(signed) b;
14721 2. when a is a negative signed integer while b is a positive one, then a
14722 is a greater unsigned integer than b; likewise when a and b's roles
14723 are flipped.
14725 So first, compare the sign of the two operands. */
14726 ret = new_loc_descr (DW_OP_over, 0, 0);
14727 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14728 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14729 /* If they have different signs (i.e. they have different sign bits), then
14730 the stack top value has now the sign bit set and thus it's smaller than
14731 zero. */
14732 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14733 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14734 add_loc_descr (&ret, bra_node);
14736 /* We are in case 1. At this point, we know both operands have the same
14737 sign, to it's safe to use the built-in signed comparison. */
14738 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14739 add_loc_descr (&ret, jmp_node);
14741 /* We are in case 2. Here, we know both operands do not have the same sign,
14742 so we have to flip the signed comparison. */
14743 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14744 tmp = new_loc_descr (flip_op, 0, 0);
14745 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14746 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14747 add_loc_descr (&ret, tmp);
14749 /* This dummy operation is necessary to make the two branches join. */
14750 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14751 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14752 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14753 add_loc_descr (&ret, tmp);
14755 return ret;
14758 /* Likewise, but takes the location description lists (might be destructive on
14759 them). Return NULL if either is NULL or if concatenation fails. */
14761 static dw_loc_list_ref
14762 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14763 enum tree_code kind)
14765 if (left == NULL || right == NULL)
14766 return NULL;
14768 add_loc_list (&left, right);
14769 if (left == NULL)
14770 return NULL;
14772 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14773 return left;
14776 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14777 without actually allocating it. */
14779 static unsigned long
14780 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14782 return size_of_int_loc_descriptor (i >> shift)
14783 + size_of_int_loc_descriptor (shift)
14784 + 1;
14787 /* Return size_of_locs (int_loc_descriptor (i)) without
14788 actually allocating it. */
14790 static unsigned long
14791 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14793 unsigned long s;
14795 if (i >= 0)
14797 int clz, ctz;
14798 if (i <= 31)
14799 return 1;
14800 else if (i <= 0xff)
14801 return 2;
14802 else if (i <= 0xffff)
14803 return 3;
14804 clz = clz_hwi (i);
14805 ctz = ctz_hwi (i);
14806 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14807 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14808 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14809 - clz - 5);
14810 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14811 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14812 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14813 - clz - 8);
14814 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14815 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14816 <= 4)
14817 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14818 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14819 return 5;
14820 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14821 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14822 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14823 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14824 - clz - 8);
14825 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14826 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14827 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14828 - clz - 16);
14829 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14830 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14831 && s > 6)
14832 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14833 - clz - 32);
14834 else
14835 return 1 + s;
14837 else
14839 if (i >= -0x80)
14840 return 2;
14841 else if (i >= -0x8000)
14842 return 3;
14843 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14845 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14847 s = size_of_int_loc_descriptor (-i) + 1;
14848 if (s < 5)
14849 return s;
14851 return 5;
14853 else
14855 unsigned long r = 1 + size_of_sleb128 (i);
14856 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14858 s = size_of_int_loc_descriptor (-i) + 1;
14859 if (s < r)
14860 return s;
14862 return r;
14867 /* Return loc description representing "address" of integer value.
14868 This can appear only as toplevel expression. */
14870 static dw_loc_descr_ref
14871 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14873 int litsize;
14874 dw_loc_descr_ref loc_result = NULL;
14876 if (!(dwarf_version >= 4 || !dwarf_strict))
14877 return NULL;
14879 litsize = size_of_int_loc_descriptor (i);
14880 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14881 is more compact. For DW_OP_stack_value we need:
14882 litsize + 1 (DW_OP_stack_value)
14883 and for DW_OP_implicit_value:
14884 1 (DW_OP_implicit_value) + 1 (length) + size. */
14885 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14887 loc_result = int_loc_descriptor (i);
14888 add_loc_descr (&loc_result,
14889 new_loc_descr (DW_OP_stack_value, 0, 0));
14890 return loc_result;
14893 loc_result = new_loc_descr (DW_OP_implicit_value,
14894 size, 0);
14895 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14896 loc_result->dw_loc_oprnd2.v.val_int = i;
14897 return loc_result;
14900 /* Return a location descriptor that designates a base+offset location. */
14902 static dw_loc_descr_ref
14903 based_loc_descr (rtx reg, poly_int64 offset,
14904 enum var_init_status initialized)
14906 unsigned int regno;
14907 dw_loc_descr_ref result;
14908 dw_fde_ref fde = cfun->fde;
14910 /* We only use "frame base" when we're sure we're talking about the
14911 post-prologue local stack frame. We do this by *not* running
14912 register elimination until this point, and recognizing the special
14913 argument pointer and soft frame pointer rtx's. */
14914 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14916 rtx elim = (ira_use_lra_p
14917 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14918 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14920 if (elim != reg)
14922 /* Allow hard frame pointer here even if frame pointer
14923 isn't used since hard frame pointer is encoded with
14924 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14925 not hard frame pointer directly. */
14926 elim = strip_offset_and_add (elim, &offset);
14927 gcc_assert (elim == hard_frame_pointer_rtx
14928 || elim == stack_pointer_rtx);
14930 /* If drap register is used to align stack, use frame
14931 pointer + offset to access stack variables. If stack
14932 is aligned without drap, use stack pointer + offset to
14933 access stack variables. */
14934 if (crtl->stack_realign_tried
14935 && reg == frame_pointer_rtx)
14937 int base_reg
14938 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14939 ? HARD_FRAME_POINTER_REGNUM
14940 : REGNO (elim));
14941 return new_reg_loc_descr (base_reg, offset);
14944 gcc_assert (frame_pointer_fb_offset_valid);
14945 offset += frame_pointer_fb_offset;
14946 HOST_WIDE_INT const_offset;
14947 if (offset.is_constant (&const_offset))
14948 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14949 else
14951 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14952 loc_descr_plus_const (&ret, offset);
14953 return ret;
14958 regno = REGNO (reg);
14959 #ifdef LEAF_REG_REMAP
14960 if (crtl->uses_only_leaf_regs)
14962 int leaf_reg = LEAF_REG_REMAP (regno);
14963 if (leaf_reg != -1)
14964 regno = (unsigned) leaf_reg;
14966 #endif
14967 regno = DWARF_FRAME_REGNUM (regno);
14969 HOST_WIDE_INT const_offset;
14970 if (!optimize && fde
14971 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14972 && offset.is_constant (&const_offset))
14974 /* Use cfa+offset to represent the location of arguments passed
14975 on the stack when drap is used to align stack.
14976 Only do this when not optimizing, for optimized code var-tracking
14977 is supposed to track where the arguments live and the register
14978 used as vdrap or drap in some spot might be used for something
14979 else in other part of the routine. */
14980 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14983 result = new_reg_loc_descr (regno, offset);
14985 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14986 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14988 return result;
14991 /* Return true if this RTL expression describes a base+offset calculation. */
14993 static inline bool
14994 is_based_loc (const_rtx rtl)
14996 return (GET_CODE (rtl) == PLUS
14997 && ((REG_P (XEXP (rtl, 0))
14998 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14999 && CONST_INT_P (XEXP (rtl, 1)))));
15002 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
15003 failed. */
15005 static dw_loc_descr_ref
15006 tls_mem_loc_descriptor (rtx mem)
15008 tree base;
15009 dw_loc_descr_ref loc_result;
15011 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
15012 return NULL;
15014 base = get_base_address (MEM_EXPR (mem));
15015 if (base == NULL
15016 || !VAR_P (base)
15017 || !DECL_THREAD_LOCAL_P (base))
15018 return NULL;
15020 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
15021 if (loc_result == NULL)
15022 return NULL;
15024 if (maybe_ne (MEM_OFFSET (mem), 0))
15025 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
15027 return loc_result;
15030 /* Output debug info about reason why we failed to expand expression as dwarf
15031 expression. */
15033 static void
15034 expansion_failed (tree expr, rtx rtl, char const *reason)
15036 if (dump_file && (dump_flags & TDF_DETAILS))
15038 fprintf (dump_file, "Failed to expand as dwarf: ");
15039 if (expr)
15040 print_generic_expr (dump_file, expr, dump_flags);
15041 if (rtl)
15043 fprintf (dump_file, "\n");
15044 print_rtl (dump_file, rtl);
15046 fprintf (dump_file, "\nReason: %s\n", reason);
15050 /* Helper function for const_ok_for_output. */
15052 static bool
15053 const_ok_for_output_1 (rtx rtl)
15055 if (targetm.const_not_ok_for_debug_p (rtl))
15057 if (GET_CODE (rtl) != UNSPEC)
15059 expansion_failed (NULL_TREE, rtl,
15060 "Expression rejected for debug by the backend.\n");
15061 return false;
15064 /* If delegitimize_address couldn't do anything with the UNSPEC, and
15065 the target hook doesn't explicitly allow it in debug info, assume
15066 we can't express it in the debug info. */
15067 /* Don't complain about TLS UNSPECs, those are just too hard to
15068 delegitimize. Note this could be a non-decl SYMBOL_REF such as
15069 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
15070 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
15071 if (flag_checking
15072 && (XVECLEN (rtl, 0) == 0
15073 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
15074 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
15075 inform (current_function_decl
15076 ? DECL_SOURCE_LOCATION (current_function_decl)
15077 : UNKNOWN_LOCATION,
15078 #if NUM_UNSPEC_VALUES > 0
15079 "non-delegitimized UNSPEC %s (%d) found in variable location",
15080 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
15081 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
15082 #else
15083 "non-delegitimized UNSPEC %d found in variable location",
15084 #endif
15085 XINT (rtl, 1));
15086 expansion_failed (NULL_TREE, rtl,
15087 "UNSPEC hasn't been delegitimized.\n");
15088 return false;
15091 if (CONST_POLY_INT_P (rtl))
15092 return false;
15094 /* FIXME: Refer to PR60655. It is possible for simplification
15095 of rtl expressions in var tracking to produce such expressions.
15096 We should really identify / validate expressions
15097 enclosed in CONST that can be handled by assemblers on various
15098 targets and only handle legitimate cases here. */
15099 switch (GET_CODE (rtl))
15101 case SYMBOL_REF:
15102 break;
15103 case NOT:
15104 case NEG:
15105 return false;
15106 case PLUS:
15108 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
15109 operands. */
15110 subrtx_var_iterator::array_type array;
15111 bool first = false;
15112 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15113 if (SYMBOL_REF_P (*iter)
15114 || LABEL_P (*iter)
15115 || GET_CODE (*iter) == UNSPEC)
15117 first = true;
15118 break;
15120 if (!first)
15121 return true;
15122 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15123 if (SYMBOL_REF_P (*iter)
15124 || LABEL_P (*iter)
15125 || GET_CODE (*iter) == UNSPEC)
15126 return false;
15127 return true;
15129 case MINUS:
15131 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
15132 appear in the second operand of MINUS. */
15133 subrtx_var_iterator::array_type array;
15134 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15135 if (SYMBOL_REF_P (*iter)
15136 || LABEL_P (*iter)
15137 || GET_CODE (*iter) == UNSPEC)
15138 return false;
15139 return true;
15141 default:
15142 return true;
15145 if (CONSTANT_POOL_ADDRESS_P (rtl))
15147 bool marked;
15148 get_pool_constant_mark (rtl, &marked);
15149 /* If all references to this pool constant were optimized away,
15150 it was not output and thus we can't represent it. */
15151 if (!marked)
15153 expansion_failed (NULL_TREE, rtl,
15154 "Constant was removed from constant pool.\n");
15155 return false;
15159 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15160 return false;
15162 /* Avoid references to external symbols in debug info, on several targets
15163 the linker might even refuse to link when linking a shared library,
15164 and in many other cases the relocations for .debug_info/.debug_loc are
15165 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
15166 to be defined within the same shared library or executable are fine. */
15167 if (SYMBOL_REF_EXTERNAL_P (rtl))
15169 tree decl = SYMBOL_REF_DECL (rtl);
15171 if (decl == NULL || !targetm.binds_local_p (decl))
15173 expansion_failed (NULL_TREE, rtl,
15174 "Symbol not defined in current TU.\n");
15175 return false;
15179 return true;
15182 /* Return true if constant RTL can be emitted in DW_OP_addr or
15183 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
15184 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
15186 static bool
15187 const_ok_for_output (rtx rtl)
15189 if (GET_CODE (rtl) == SYMBOL_REF)
15190 return const_ok_for_output_1 (rtl);
15192 if (GET_CODE (rtl) == CONST)
15194 subrtx_var_iterator::array_type array;
15195 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15196 if (!const_ok_for_output_1 (*iter))
15197 return false;
15198 return true;
15201 return true;
15204 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15205 if possible, NULL otherwise. */
15207 static dw_die_ref
15208 base_type_for_mode (machine_mode mode, bool unsignedp)
15210 dw_die_ref type_die;
15211 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15213 if (type == NULL)
15214 return NULL;
15215 switch (TREE_CODE (type))
15217 case INTEGER_TYPE:
15218 case REAL_TYPE:
15219 break;
15220 default:
15221 return NULL;
15223 type_die = lookup_type_die (type);
15224 if (!type_die)
15225 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15226 comp_unit_die ());
15227 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15228 return NULL;
15229 return type_die;
15232 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15233 type matching MODE, or, if MODE is narrower than or as wide as
15234 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15235 possible. */
15237 static dw_loc_descr_ref
15238 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15240 machine_mode outer_mode = mode;
15241 dw_die_ref type_die;
15242 dw_loc_descr_ref cvt;
15244 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15246 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15247 return op;
15249 type_die = base_type_for_mode (outer_mode, 1);
15250 if (type_die == NULL)
15251 return NULL;
15252 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15253 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15254 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15255 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15256 add_loc_descr (&op, cvt);
15257 return op;
15260 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15262 static dw_loc_descr_ref
15263 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15264 dw_loc_descr_ref op1)
15266 dw_loc_descr_ref ret = op0;
15267 add_loc_descr (&ret, op1);
15268 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15269 if (STORE_FLAG_VALUE != 1)
15271 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15272 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15274 return ret;
15277 /* Subroutine of scompare_loc_descriptor for the case in which we're
15278 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15279 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15281 static dw_loc_descr_ref
15282 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15283 scalar_int_mode op_mode,
15284 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15286 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15287 dw_loc_descr_ref cvt;
15289 if (type_die == NULL)
15290 return NULL;
15291 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15292 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15293 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15294 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15295 add_loc_descr (&op0, cvt);
15296 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15297 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15298 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15299 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15300 add_loc_descr (&op1, cvt);
15301 return compare_loc_descriptor (op, op0, op1);
15304 /* Subroutine of scompare_loc_descriptor for the case in which we're
15305 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15306 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15308 static dw_loc_descr_ref
15309 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15310 scalar_int_mode op_mode,
15311 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15313 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15314 /* For eq/ne, if the operands are known to be zero-extended,
15315 there is no need to do the fancy shifting up. */
15316 if (op == DW_OP_eq || op == DW_OP_ne)
15318 dw_loc_descr_ref last0, last1;
15319 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15321 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15323 /* deref_size zero extends, and for constants we can check
15324 whether they are zero extended or not. */
15325 if (((last0->dw_loc_opc == DW_OP_deref_size
15326 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15327 || (CONST_INT_P (XEXP (rtl, 0))
15328 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15329 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15330 && ((last1->dw_loc_opc == DW_OP_deref_size
15331 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15332 || (CONST_INT_P (XEXP (rtl, 1))
15333 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15334 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15335 return compare_loc_descriptor (op, op0, op1);
15337 /* EQ/NE comparison against constant in narrower type than
15338 DWARF2_ADDR_SIZE can be performed either as
15339 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15340 DW_OP_{eq,ne}
15342 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15343 DW_OP_{eq,ne}. Pick whatever is shorter. */
15344 if (CONST_INT_P (XEXP (rtl, 1))
15345 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15346 && (size_of_int_loc_descriptor (shift) + 1
15347 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15348 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15349 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15350 & GET_MODE_MASK (op_mode))))
15352 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15353 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15354 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15355 & GET_MODE_MASK (op_mode));
15356 return compare_loc_descriptor (op, op0, op1);
15359 add_loc_descr (&op0, int_loc_descriptor (shift));
15360 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15361 if (CONST_INT_P (XEXP (rtl, 1)))
15362 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15363 else
15365 add_loc_descr (&op1, int_loc_descriptor (shift));
15366 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15368 return compare_loc_descriptor (op, op0, op1);
15371 /* Return location descriptor for signed comparison OP RTL. */
15373 static dw_loc_descr_ref
15374 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15375 machine_mode mem_mode)
15377 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15378 dw_loc_descr_ref op0, op1;
15380 if (op_mode == VOIDmode)
15381 op_mode = GET_MODE (XEXP (rtl, 1));
15382 if (op_mode == VOIDmode)
15383 return NULL;
15385 scalar_int_mode int_op_mode;
15386 if (dwarf_strict
15387 && dwarf_version < 5
15388 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15389 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15390 return NULL;
15392 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15393 VAR_INIT_STATUS_INITIALIZED);
15394 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15395 VAR_INIT_STATUS_INITIALIZED);
15397 if (op0 == NULL || op1 == NULL)
15398 return NULL;
15400 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15402 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15403 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15405 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15406 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15408 return compare_loc_descriptor (op, op0, op1);
15411 /* Return location descriptor for unsigned comparison OP RTL. */
15413 static dw_loc_descr_ref
15414 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15415 machine_mode mem_mode)
15417 dw_loc_descr_ref op0, op1;
15419 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15420 if (test_op_mode == VOIDmode)
15421 test_op_mode = GET_MODE (XEXP (rtl, 1));
15423 scalar_int_mode op_mode;
15424 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15425 return NULL;
15427 if (dwarf_strict
15428 && dwarf_version < 5
15429 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15430 return NULL;
15432 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15433 VAR_INIT_STATUS_INITIALIZED);
15434 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15435 VAR_INIT_STATUS_INITIALIZED);
15437 if (op0 == NULL || op1 == NULL)
15438 return NULL;
15440 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15442 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15443 dw_loc_descr_ref last0, last1;
15444 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15446 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15448 if (CONST_INT_P (XEXP (rtl, 0)))
15449 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15450 /* deref_size zero extends, so no need to mask it again. */
15451 else if (last0->dw_loc_opc != DW_OP_deref_size
15452 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15454 add_loc_descr (&op0, int_loc_descriptor (mask));
15455 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15457 if (CONST_INT_P (XEXP (rtl, 1)))
15458 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15459 /* deref_size zero extends, so no need to mask it again. */
15460 else if (last1->dw_loc_opc != DW_OP_deref_size
15461 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15463 add_loc_descr (&op1, int_loc_descriptor (mask));
15464 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15467 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15469 HOST_WIDE_INT bias = 1;
15470 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15471 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15472 if (CONST_INT_P (XEXP (rtl, 1)))
15473 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15474 + INTVAL (XEXP (rtl, 1)));
15475 else
15476 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15477 bias, 0));
15479 return compare_loc_descriptor (op, op0, op1);
15482 /* Return location descriptor for {U,S}{MIN,MAX}. */
15484 static dw_loc_descr_ref
15485 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15486 machine_mode mem_mode)
15488 enum dwarf_location_atom op;
15489 dw_loc_descr_ref op0, op1, ret;
15490 dw_loc_descr_ref bra_node, drop_node;
15492 scalar_int_mode int_mode;
15493 if (dwarf_strict
15494 && dwarf_version < 5
15495 && (!is_a <scalar_int_mode> (mode, &int_mode)
15496 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15497 return NULL;
15499 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15500 VAR_INIT_STATUS_INITIALIZED);
15501 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15502 VAR_INIT_STATUS_INITIALIZED);
15504 if (op0 == NULL || op1 == NULL)
15505 return NULL;
15507 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15508 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15509 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15510 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15512 /* Checked by the caller. */
15513 int_mode = as_a <scalar_int_mode> (mode);
15514 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15516 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15517 add_loc_descr (&op0, int_loc_descriptor (mask));
15518 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15519 add_loc_descr (&op1, int_loc_descriptor (mask));
15520 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15522 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15524 HOST_WIDE_INT bias = 1;
15525 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15526 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15527 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15530 else if (is_a <scalar_int_mode> (mode, &int_mode)
15531 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15533 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15534 add_loc_descr (&op0, int_loc_descriptor (shift));
15535 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15536 add_loc_descr (&op1, int_loc_descriptor (shift));
15537 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15539 else if (is_a <scalar_int_mode> (mode, &int_mode)
15540 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15542 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15543 dw_loc_descr_ref cvt;
15544 if (type_die == NULL)
15545 return NULL;
15546 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15547 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15548 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15549 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15550 add_loc_descr (&op0, cvt);
15551 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15552 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15553 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15554 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15555 add_loc_descr (&op1, cvt);
15558 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15559 op = DW_OP_lt;
15560 else
15561 op = DW_OP_gt;
15562 ret = op0;
15563 add_loc_descr (&ret, op1);
15564 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15565 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15566 add_loc_descr (&ret, bra_node);
15567 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15568 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15569 add_loc_descr (&ret, drop_node);
15570 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15571 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15572 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15573 && is_a <scalar_int_mode> (mode, &int_mode)
15574 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15575 ret = convert_descriptor_to_mode (int_mode, ret);
15576 return ret;
15579 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15580 but after converting arguments to type_die, afterwards
15581 convert back to unsigned. */
15583 static dw_loc_descr_ref
15584 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15585 scalar_int_mode mode, machine_mode mem_mode)
15587 dw_loc_descr_ref cvt, op0, op1;
15589 if (type_die == NULL)
15590 return NULL;
15591 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15592 VAR_INIT_STATUS_INITIALIZED);
15593 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15594 VAR_INIT_STATUS_INITIALIZED);
15595 if (op0 == NULL || op1 == NULL)
15596 return NULL;
15597 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15598 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15599 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15600 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15601 add_loc_descr (&op0, cvt);
15602 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15603 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15604 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15605 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15606 add_loc_descr (&op1, cvt);
15607 add_loc_descr (&op0, op1);
15608 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15609 return convert_descriptor_to_mode (mode, op0);
15612 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15613 const0 is DW_OP_lit0 or corresponding typed constant,
15614 const1 is DW_OP_lit1 or corresponding typed constant
15615 and constMSB is constant with just the MSB bit set
15616 for the mode):
15617 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15618 L1: const0 DW_OP_swap
15619 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15620 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15621 L3: DW_OP_drop
15622 L4: DW_OP_nop
15624 CTZ is similar:
15625 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15626 L1: const0 DW_OP_swap
15627 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15628 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15629 L3: DW_OP_drop
15630 L4: DW_OP_nop
15632 FFS is similar:
15633 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15634 L1: const1 DW_OP_swap
15635 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15636 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15637 L3: DW_OP_drop
15638 L4: DW_OP_nop */
15640 static dw_loc_descr_ref
15641 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15642 machine_mode mem_mode)
15644 dw_loc_descr_ref op0, ret, tmp;
15645 HOST_WIDE_INT valv;
15646 dw_loc_descr_ref l1jump, l1label;
15647 dw_loc_descr_ref l2jump, l2label;
15648 dw_loc_descr_ref l3jump, l3label;
15649 dw_loc_descr_ref l4jump, l4label;
15650 rtx msb;
15652 if (GET_MODE (XEXP (rtl, 0)) != mode)
15653 return NULL;
15655 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15656 VAR_INIT_STATUS_INITIALIZED);
15657 if (op0 == NULL)
15658 return NULL;
15659 ret = op0;
15660 if (GET_CODE (rtl) == CLZ)
15662 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15663 valv = GET_MODE_BITSIZE (mode);
15665 else if (GET_CODE (rtl) == FFS)
15666 valv = 0;
15667 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15668 valv = GET_MODE_BITSIZE (mode);
15669 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15670 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15671 add_loc_descr (&ret, l1jump);
15672 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15673 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15674 VAR_INIT_STATUS_INITIALIZED);
15675 if (tmp == NULL)
15676 return NULL;
15677 add_loc_descr (&ret, tmp);
15678 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15679 add_loc_descr (&ret, l4jump);
15680 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15681 ? const1_rtx : const0_rtx,
15682 mode, mem_mode,
15683 VAR_INIT_STATUS_INITIALIZED);
15684 if (l1label == NULL)
15685 return NULL;
15686 add_loc_descr (&ret, l1label);
15687 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15688 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15689 add_loc_descr (&ret, l2label);
15690 if (GET_CODE (rtl) != CLZ)
15691 msb = const1_rtx;
15692 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15693 msb = GEN_INT (HOST_WIDE_INT_1U
15694 << (GET_MODE_BITSIZE (mode) - 1));
15695 else
15696 msb = immed_wide_int_const
15697 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15698 GET_MODE_PRECISION (mode)), mode);
15699 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15700 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15701 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15702 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15703 else
15704 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15705 VAR_INIT_STATUS_INITIALIZED);
15706 if (tmp == NULL)
15707 return NULL;
15708 add_loc_descr (&ret, tmp);
15709 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15710 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15711 add_loc_descr (&ret, l3jump);
15712 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15713 VAR_INIT_STATUS_INITIALIZED);
15714 if (tmp == NULL)
15715 return NULL;
15716 add_loc_descr (&ret, tmp);
15717 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15718 ? DW_OP_shl : DW_OP_shr, 0, 0));
15719 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15720 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15721 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15722 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15723 add_loc_descr (&ret, l2jump);
15724 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15725 add_loc_descr (&ret, l3label);
15726 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15727 add_loc_descr (&ret, l4label);
15728 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15729 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15730 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15731 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15732 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15733 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15734 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15735 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15736 return ret;
15739 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15740 const1 is DW_OP_lit1 or corresponding typed constant):
15741 const0 DW_OP_swap
15742 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15743 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15744 L2: DW_OP_drop
15746 PARITY is similar:
15747 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15748 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15749 L2: DW_OP_drop */
15751 static dw_loc_descr_ref
15752 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15753 machine_mode mem_mode)
15755 dw_loc_descr_ref op0, ret, tmp;
15756 dw_loc_descr_ref l1jump, l1label;
15757 dw_loc_descr_ref l2jump, l2label;
15759 if (GET_MODE (XEXP (rtl, 0)) != mode)
15760 return NULL;
15762 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15763 VAR_INIT_STATUS_INITIALIZED);
15764 if (op0 == NULL)
15765 return NULL;
15766 ret = op0;
15767 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15768 VAR_INIT_STATUS_INITIALIZED);
15769 if (tmp == NULL)
15770 return NULL;
15771 add_loc_descr (&ret, tmp);
15772 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15773 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15774 add_loc_descr (&ret, l1label);
15775 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15776 add_loc_descr (&ret, l2jump);
15777 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15778 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15779 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15780 VAR_INIT_STATUS_INITIALIZED);
15781 if (tmp == NULL)
15782 return NULL;
15783 add_loc_descr (&ret, tmp);
15784 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15785 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15786 ? DW_OP_plus : DW_OP_xor, 0, 0));
15787 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15788 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15789 VAR_INIT_STATUS_INITIALIZED);
15790 add_loc_descr (&ret, tmp);
15791 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15792 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15793 add_loc_descr (&ret, l1jump);
15794 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15795 add_loc_descr (&ret, l2label);
15796 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15797 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15798 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15799 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15800 return ret;
15803 /* BSWAP (constS is initial shift count, either 56 or 24):
15804 constS const0
15805 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15806 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15807 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15808 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15809 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15811 static dw_loc_descr_ref
15812 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15813 machine_mode mem_mode)
15815 dw_loc_descr_ref op0, ret, tmp;
15816 dw_loc_descr_ref l1jump, l1label;
15817 dw_loc_descr_ref l2jump, l2label;
15819 if (BITS_PER_UNIT != 8
15820 || (GET_MODE_BITSIZE (mode) != 32
15821 && GET_MODE_BITSIZE (mode) != 64))
15822 return NULL;
15824 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15825 VAR_INIT_STATUS_INITIALIZED);
15826 if (op0 == NULL)
15827 return NULL;
15829 ret = op0;
15830 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15831 mode, mem_mode,
15832 VAR_INIT_STATUS_INITIALIZED);
15833 if (tmp == NULL)
15834 return NULL;
15835 add_loc_descr (&ret, tmp);
15836 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15837 VAR_INIT_STATUS_INITIALIZED);
15838 if (tmp == NULL)
15839 return NULL;
15840 add_loc_descr (&ret, tmp);
15841 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15842 add_loc_descr (&ret, l1label);
15843 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15844 mode, mem_mode,
15845 VAR_INIT_STATUS_INITIALIZED);
15846 add_loc_descr (&ret, tmp);
15847 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15848 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15849 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15850 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15851 VAR_INIT_STATUS_INITIALIZED);
15852 if (tmp == NULL)
15853 return NULL;
15854 add_loc_descr (&ret, tmp);
15855 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15856 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15857 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15858 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15859 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15860 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15861 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15862 VAR_INIT_STATUS_INITIALIZED);
15863 add_loc_descr (&ret, tmp);
15864 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15865 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15866 add_loc_descr (&ret, l2jump);
15867 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15868 VAR_INIT_STATUS_INITIALIZED);
15869 add_loc_descr (&ret, tmp);
15870 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15871 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15872 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15873 add_loc_descr (&ret, l1jump);
15874 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15875 add_loc_descr (&ret, l2label);
15876 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15877 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15878 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15879 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15880 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15881 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15882 return ret;
15885 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15886 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15887 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15888 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15890 ROTATERT is similar:
15891 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15892 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15893 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15895 static dw_loc_descr_ref
15896 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15897 machine_mode mem_mode)
15899 rtx rtlop1 = XEXP (rtl, 1);
15900 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15901 int i;
15903 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15904 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15905 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15906 VAR_INIT_STATUS_INITIALIZED);
15907 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15908 VAR_INIT_STATUS_INITIALIZED);
15909 if (op0 == NULL || op1 == NULL)
15910 return NULL;
15911 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15912 for (i = 0; i < 2; i++)
15914 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15915 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15916 mode, mem_mode,
15917 VAR_INIT_STATUS_INITIALIZED);
15918 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15919 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15920 ? DW_OP_const4u
15921 : HOST_BITS_PER_WIDE_INT == 64
15922 ? DW_OP_const8u : DW_OP_constu,
15923 GET_MODE_MASK (mode), 0);
15924 else
15925 mask[i] = NULL;
15926 if (mask[i] == NULL)
15927 return NULL;
15928 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15930 ret = op0;
15931 add_loc_descr (&ret, op1);
15932 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15933 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15934 if (GET_CODE (rtl) == ROTATERT)
15936 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15937 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15938 GET_MODE_BITSIZE (mode), 0));
15940 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15941 if (mask[0] != NULL)
15942 add_loc_descr (&ret, mask[0]);
15943 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15944 if (mask[1] != NULL)
15946 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15947 add_loc_descr (&ret, mask[1]);
15948 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15950 if (GET_CODE (rtl) == ROTATE)
15952 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15953 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15954 GET_MODE_BITSIZE (mode), 0));
15956 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15957 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15958 return ret;
15961 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15962 for DEBUG_PARAMETER_REF RTL. */
15964 static dw_loc_descr_ref
15965 parameter_ref_descriptor (rtx rtl)
15967 dw_loc_descr_ref ret;
15968 dw_die_ref ref;
15970 if (dwarf_strict)
15971 return NULL;
15972 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15973 /* With LTO during LTRANS we get the late DIE that refers to the early
15974 DIE, thus we add another indirection here. This seems to confuse
15975 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15976 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15977 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15978 if (ref)
15980 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15981 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15982 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15984 else
15986 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15987 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15989 return ret;
15992 /* The following routine converts the RTL for a variable or parameter
15993 (resident in memory) into an equivalent Dwarf representation of a
15994 mechanism for getting the address of that same variable onto the top of a
15995 hypothetical "address evaluation" stack.
15997 When creating memory location descriptors, we are effectively transforming
15998 the RTL for a memory-resident object into its Dwarf postfix expression
15999 equivalent. This routine recursively descends an RTL tree, turning
16000 it into Dwarf postfix code as it goes.
16002 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
16004 MEM_MODE is the mode of the memory reference, needed to handle some
16005 autoincrement addressing modes.
16007 Return 0 if we can't represent the location. */
16009 dw_loc_descr_ref
16010 mem_loc_descriptor (rtx rtl, machine_mode mode,
16011 machine_mode mem_mode,
16012 enum var_init_status initialized)
16014 dw_loc_descr_ref mem_loc_result = NULL;
16015 enum dwarf_location_atom op;
16016 dw_loc_descr_ref op0, op1;
16017 rtx inner = NULL_RTX;
16019 if (mode == VOIDmode)
16020 mode = GET_MODE (rtl);
16022 /* Note that for a dynamically sized array, the location we will generate a
16023 description of here will be the lowest numbered location which is
16024 actually within the array. That's *not* necessarily the same as the
16025 zeroth element of the array. */
16027 rtl = targetm.delegitimize_address (rtl);
16029 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
16030 return NULL;
16032 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
16033 switch (GET_CODE (rtl))
16035 case POST_INC:
16036 case POST_DEC:
16037 case POST_MODIFY:
16038 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
16040 case SUBREG:
16041 /* The case of a subreg may arise when we have a local (register)
16042 variable or a formal (register) parameter which doesn't quite fill
16043 up an entire register. For now, just assume that it is
16044 legitimate to make the Dwarf info refer to the whole register which
16045 contains the given subreg. */
16046 if (!subreg_lowpart_p (rtl))
16047 break;
16048 inner = SUBREG_REG (rtl);
16049 /* FALLTHRU */
16050 case TRUNCATE:
16051 if (inner == NULL_RTX)
16052 inner = XEXP (rtl, 0);
16053 if (is_a <scalar_int_mode> (mode, &int_mode)
16054 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
16055 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16056 #ifdef POINTERS_EXTEND_UNSIGNED
16057 || (int_mode == Pmode && mem_mode != VOIDmode)
16058 #endif
16060 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
16062 mem_loc_result = mem_loc_descriptor (inner,
16063 inner_mode,
16064 mem_mode, initialized);
16065 break;
16067 if (dwarf_strict && dwarf_version < 5)
16068 break;
16069 if (is_a <scalar_int_mode> (mode, &int_mode)
16070 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
16071 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
16072 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16074 dw_die_ref type_die;
16075 dw_loc_descr_ref cvt;
16077 mem_loc_result = mem_loc_descriptor (inner,
16078 GET_MODE (inner),
16079 mem_mode, initialized);
16080 if (mem_loc_result == NULL)
16081 break;
16082 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16083 if (type_die == NULL)
16085 mem_loc_result = NULL;
16086 break;
16088 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16089 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16090 else
16091 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
16092 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16093 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16094 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16095 add_loc_descr (&mem_loc_result, cvt);
16096 if (is_a <scalar_int_mode> (mode, &int_mode)
16097 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16099 /* Convert it to untyped afterwards. */
16100 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16101 add_loc_descr (&mem_loc_result, cvt);
16104 break;
16106 case REG:
16107 if (!is_a <scalar_int_mode> (mode, &int_mode)
16108 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16109 && rtl != arg_pointer_rtx
16110 && rtl != frame_pointer_rtx
16111 #ifdef POINTERS_EXTEND_UNSIGNED
16112 && (int_mode != Pmode || mem_mode == VOIDmode)
16113 #endif
16116 dw_die_ref type_die;
16117 unsigned int debugger_regnum;
16119 if (dwarf_strict && dwarf_version < 5)
16120 break;
16121 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
16122 break;
16123 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16124 if (type_die == NULL)
16125 break;
16127 debugger_regnum = debugger_reg_number (rtl);
16128 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16129 break;
16130 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
16131 debugger_regnum, 0);
16132 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16133 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16134 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
16135 break;
16137 /* Whenever a register number forms a part of the description of the
16138 method for calculating the (dynamic) address of a memory resident
16139 object, DWARF rules require the register number be referred to as
16140 a "base register". This distinction is not based in any way upon
16141 what category of register the hardware believes the given register
16142 belongs to. This is strictly DWARF terminology we're dealing with
16143 here. Note that in cases where the location of a memory-resident
16144 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
16145 OP_CONST (0)) the actual DWARF location descriptor that we generate
16146 may just be OP_BASEREG (basereg). This may look deceptively like
16147 the object in question was allocated to a register (rather than in
16148 memory) so DWARF consumers need to be aware of the subtle
16149 distinction between OP_REG and OP_BASEREG. */
16150 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
16151 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
16152 else if (stack_realign_drap
16153 && crtl->drap_reg
16154 && crtl->args.internal_arg_pointer == rtl
16155 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
16157 /* If RTL is internal_arg_pointer, which has been optimized
16158 out, use DRAP instead. */
16159 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
16160 VAR_INIT_STATUS_INITIALIZED);
16162 break;
16164 case SIGN_EXTEND:
16165 case ZERO_EXTEND:
16166 if (!is_a <scalar_int_mode> (mode, &int_mode)
16167 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
16168 break;
16169 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16170 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16171 if (op0 == 0)
16172 break;
16173 else if (GET_CODE (rtl) == ZERO_EXTEND
16174 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16175 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
16176 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
16177 to expand zero extend as two shifts instead of
16178 masking. */
16179 && GET_MODE_SIZE (inner_mode) <= 4)
16181 mem_loc_result = op0;
16182 add_loc_descr (&mem_loc_result,
16183 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
16184 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
16186 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16188 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
16189 shift *= BITS_PER_UNIT;
16190 if (GET_CODE (rtl) == SIGN_EXTEND)
16191 op = DW_OP_shra;
16192 else
16193 op = DW_OP_shr;
16194 mem_loc_result = op0;
16195 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16196 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16197 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16198 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16200 else if (!dwarf_strict || dwarf_version >= 5)
16202 dw_die_ref type_die1, type_die2;
16203 dw_loc_descr_ref cvt;
16205 type_die1 = base_type_for_mode (inner_mode,
16206 GET_CODE (rtl) == ZERO_EXTEND);
16207 if (type_die1 == NULL)
16208 break;
16209 type_die2 = base_type_for_mode (int_mode, 1);
16210 if (type_die2 == NULL)
16211 break;
16212 mem_loc_result = op0;
16213 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16214 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16215 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16216 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16217 add_loc_descr (&mem_loc_result, cvt);
16218 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16219 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16220 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16221 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16222 add_loc_descr (&mem_loc_result, cvt);
16224 break;
16226 case MEM:
16228 rtx new_rtl = avoid_constant_pool_reference (rtl);
16229 if (new_rtl != rtl)
16231 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16232 initialized);
16233 if (mem_loc_result != NULL)
16234 return mem_loc_result;
16237 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16238 get_address_mode (rtl), mode,
16239 VAR_INIT_STATUS_INITIALIZED);
16240 if (mem_loc_result == NULL)
16241 mem_loc_result = tls_mem_loc_descriptor (rtl);
16242 if (mem_loc_result != NULL)
16244 if (!is_a <scalar_int_mode> (mode, &int_mode)
16245 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16247 dw_die_ref type_die;
16248 dw_loc_descr_ref deref;
16249 HOST_WIDE_INT size;
16251 if (dwarf_strict && dwarf_version < 5)
16252 return NULL;
16253 if (!GET_MODE_SIZE (mode).is_constant (&size))
16254 return NULL;
16255 type_die
16256 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16257 if (type_die == NULL)
16258 return NULL;
16259 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16260 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16261 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16262 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16263 add_loc_descr (&mem_loc_result, deref);
16265 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16266 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16267 else
16268 add_loc_descr (&mem_loc_result,
16269 new_loc_descr (DW_OP_deref_size,
16270 GET_MODE_SIZE (int_mode), 0));
16272 break;
16274 case LO_SUM:
16275 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16277 case LABEL_REF:
16278 /* Some ports can transform a symbol ref into a label ref, because
16279 the symbol ref is too far away and has to be dumped into a constant
16280 pool. */
16281 case CONST:
16282 case SYMBOL_REF:
16283 case UNSPEC:
16284 if (!is_a <scalar_int_mode> (mode, &int_mode)
16285 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16286 #ifdef POINTERS_EXTEND_UNSIGNED
16287 && (int_mode != Pmode || mem_mode == VOIDmode)
16288 #endif
16290 break;
16292 if (GET_CODE (rtl) == UNSPEC)
16294 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16295 can't express it in the debug info. This can happen e.g. with some
16296 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16297 approves. */
16298 bool not_ok = false;
16299 subrtx_var_iterator::array_type array;
16300 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16301 if (*iter != rtl && !CONSTANT_P (*iter))
16303 not_ok = true;
16304 break;
16307 if (not_ok)
16308 break;
16310 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16311 if (!const_ok_for_output_1 (*iter))
16313 not_ok = true;
16314 break;
16317 if (not_ok)
16318 break;
16320 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16321 goto symref;
16324 if (GET_CODE (rtl) == SYMBOL_REF
16325 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16327 dw_loc_descr_ref temp;
16329 /* If this is not defined, we have no way to emit the data. */
16330 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16331 break;
16333 temp = new_addr_loc_descr (rtl, dtprel_true);
16335 /* We check for DWARF 5 here because gdb did not implement
16336 DW_OP_form_tls_address until after 7.12. */
16337 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16338 ? DW_OP_form_tls_address
16339 : DW_OP_GNU_push_tls_address),
16340 0, 0);
16341 add_loc_descr (&mem_loc_result, temp);
16343 break;
16346 if (!const_ok_for_output (rtl))
16348 if (GET_CODE (rtl) == CONST)
16349 switch (GET_CODE (XEXP (rtl, 0)))
16351 case NOT:
16352 op = DW_OP_not;
16353 goto try_const_unop;
16354 case NEG:
16355 op = DW_OP_neg;
16356 goto try_const_unop;
16357 try_const_unop:
16358 rtx arg;
16359 arg = XEXP (XEXP (rtl, 0), 0);
16360 if (!CONSTANT_P (arg))
16361 arg = gen_rtx_CONST (int_mode, arg);
16362 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16363 initialized);
16364 if (op0)
16366 mem_loc_result = op0;
16367 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16369 break;
16370 default:
16371 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16372 mem_mode, initialized);
16373 break;
16375 break;
16378 symref:
16379 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16380 vec_safe_push (used_rtx_array, rtl);
16381 break;
16383 case CONCAT:
16384 case CONCATN:
16385 case VAR_LOCATION:
16386 case DEBUG_IMPLICIT_PTR:
16387 expansion_failed (NULL_TREE, rtl,
16388 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16389 return 0;
16391 case ENTRY_VALUE:
16392 if (dwarf_strict && dwarf_version < 5)
16393 return NULL;
16394 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16396 if (!is_a <scalar_int_mode> (mode, &int_mode)
16397 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16398 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16399 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16400 else
16402 unsigned int debugger_regnum = debugger_reg_number (ENTRY_VALUE_EXP (rtl));
16403 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16404 return NULL;
16405 op0 = one_reg_loc_descriptor (debugger_regnum,
16406 VAR_INIT_STATUS_INITIALIZED);
16409 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16410 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16412 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16413 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16414 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16415 return NULL;
16417 else
16418 gcc_unreachable ();
16419 if (op0 == NULL)
16420 return NULL;
16421 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16422 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16423 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16424 break;
16426 case DEBUG_PARAMETER_REF:
16427 mem_loc_result = parameter_ref_descriptor (rtl);
16428 break;
16430 case PRE_MODIFY:
16431 /* Extract the PLUS expression nested inside and fall into
16432 PLUS code below. */
16433 rtl = XEXP (rtl, 1);
16434 goto plus;
16436 case PRE_INC:
16437 case PRE_DEC:
16438 /* Turn these into a PLUS expression and fall into the PLUS code
16439 below. */
16440 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16441 gen_int_mode (GET_CODE (rtl) == PRE_INC
16442 ? GET_MODE_UNIT_SIZE (mem_mode)
16443 : -GET_MODE_UNIT_SIZE (mem_mode),
16444 mode));
16446 /* fall through */
16448 case PLUS:
16449 plus:
16450 if (is_based_loc (rtl)
16451 && is_a <scalar_int_mode> (mode, &int_mode)
16452 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16453 || XEXP (rtl, 0) == arg_pointer_rtx
16454 || XEXP (rtl, 0) == frame_pointer_rtx))
16455 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16456 INTVAL (XEXP (rtl, 1)),
16457 VAR_INIT_STATUS_INITIALIZED);
16458 else
16460 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16461 VAR_INIT_STATUS_INITIALIZED);
16462 if (mem_loc_result == 0)
16463 break;
16465 if (CONST_INT_P (XEXP (rtl, 1))
16466 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16467 <= DWARF2_ADDR_SIZE))
16468 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16469 else
16471 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16472 VAR_INIT_STATUS_INITIALIZED);
16473 if (op1 == 0)
16474 return NULL;
16475 add_loc_descr (&mem_loc_result, op1);
16476 add_loc_descr (&mem_loc_result,
16477 new_loc_descr (DW_OP_plus, 0, 0));
16480 break;
16482 /* If a pseudo-reg is optimized away, it is possible for it to
16483 be replaced with a MEM containing a multiply or shift. */
16484 case MINUS:
16485 op = DW_OP_minus;
16486 goto do_binop;
16488 case MULT:
16489 op = DW_OP_mul;
16490 goto do_binop;
16492 case DIV:
16493 if ((!dwarf_strict || dwarf_version >= 5)
16494 && is_a <scalar_int_mode> (mode, &int_mode)
16495 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16497 mem_loc_result = typed_binop (DW_OP_div, rtl,
16498 base_type_for_mode (mode, 0),
16499 int_mode, mem_mode);
16500 break;
16502 op = DW_OP_div;
16503 goto do_binop;
16505 case UMOD:
16506 op = DW_OP_mod;
16507 goto do_binop;
16509 case ASHIFT:
16510 op = DW_OP_shl;
16511 goto do_shift;
16513 case ASHIFTRT:
16514 op = DW_OP_shra;
16515 goto do_shift;
16517 case LSHIFTRT:
16518 op = DW_OP_shr;
16519 goto do_shift;
16521 do_shift:
16522 if (!is_a <scalar_int_mode> (mode, &int_mode))
16523 break;
16524 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16525 VAR_INIT_STATUS_INITIALIZED);
16527 rtx rtlop1 = XEXP (rtl, 1);
16528 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16529 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16530 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16531 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16532 VAR_INIT_STATUS_INITIALIZED);
16535 if (op0 == 0 || op1 == 0)
16536 break;
16538 mem_loc_result = op0;
16539 add_loc_descr (&mem_loc_result, op1);
16540 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16541 break;
16543 case AND:
16544 op = DW_OP_and;
16545 goto do_binop;
16547 case IOR:
16548 op = DW_OP_or;
16549 goto do_binop;
16551 case XOR:
16552 op = DW_OP_xor;
16553 goto do_binop;
16555 do_binop:
16556 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16557 VAR_INIT_STATUS_INITIALIZED);
16558 if (XEXP (rtl, 0) == XEXP (rtl, 1))
16560 if (op0 == 0)
16561 break;
16562 mem_loc_result = op0;
16563 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_dup, 0, 0));
16564 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16565 break;
16567 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16568 VAR_INIT_STATUS_INITIALIZED);
16570 if (op0 == 0 || op1 == 0)
16571 break;
16573 mem_loc_result = op0;
16574 add_loc_descr (&mem_loc_result, op1);
16575 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16576 break;
16578 case MOD:
16579 if ((!dwarf_strict || dwarf_version >= 5)
16580 && is_a <scalar_int_mode> (mode, &int_mode)
16581 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16583 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16584 base_type_for_mode (mode, 0),
16585 int_mode, mem_mode);
16586 break;
16589 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16590 VAR_INIT_STATUS_INITIALIZED);
16591 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16592 VAR_INIT_STATUS_INITIALIZED);
16594 if (op0 == 0 || op1 == 0)
16595 break;
16597 mem_loc_result = op0;
16598 add_loc_descr (&mem_loc_result, op1);
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_over, 0, 0));
16601 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16602 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16603 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16604 break;
16606 case UDIV:
16607 if ((!dwarf_strict || dwarf_version >= 5)
16608 && is_a <scalar_int_mode> (mode, &int_mode))
16610 /* We can use a signed divide if the sign bit is not set. */
16611 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16613 op = DW_OP_div;
16614 goto do_binop;
16617 mem_loc_result = typed_binop (DW_OP_div, rtl,
16618 base_type_for_mode (int_mode, 1),
16619 int_mode, mem_mode);
16621 break;
16623 case NOT:
16624 op = DW_OP_not;
16625 goto do_unop;
16627 case ABS:
16628 op = DW_OP_abs;
16629 goto do_unop;
16631 case NEG:
16632 op = DW_OP_neg;
16633 goto do_unop;
16635 do_unop:
16636 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16637 VAR_INIT_STATUS_INITIALIZED);
16639 if (op0 == 0)
16640 break;
16642 mem_loc_result = op0;
16643 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16644 break;
16646 case CONST_INT:
16647 if (!is_a <scalar_int_mode> (mode, &int_mode)
16648 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16649 #ifdef POINTERS_EXTEND_UNSIGNED
16650 || (int_mode == Pmode
16651 && mem_mode != VOIDmode
16652 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16653 #endif
16656 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16657 break;
16659 if ((!dwarf_strict || dwarf_version >= 5)
16660 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16661 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16663 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16664 scalar_int_mode amode;
16665 if (type_die == NULL)
16666 return NULL;
16667 if (INTVAL (rtl) >= 0
16668 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16669 .exists (&amode))
16670 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16671 /* const DW_OP_convert <XXX> vs.
16672 DW_OP_const_type <XXX, 1, const>. */
16673 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16674 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16676 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16677 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16678 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16679 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16680 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16681 add_loc_descr (&mem_loc_result, op0);
16682 return mem_loc_result;
16684 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16685 INTVAL (rtl));
16686 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16687 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16688 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16689 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16690 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16691 else
16693 mem_loc_result->dw_loc_oprnd2.val_class
16694 = dw_val_class_const_double;
16695 mem_loc_result->dw_loc_oprnd2.v.val_double
16696 = double_int::from_shwi (INTVAL (rtl));
16699 break;
16701 case CONST_DOUBLE:
16702 if (!dwarf_strict || dwarf_version >= 5)
16704 dw_die_ref type_die;
16706 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16707 CONST_DOUBLE rtx could represent either a large integer
16708 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16709 the value is always a floating point constant.
16711 When it is an integer, a CONST_DOUBLE is used whenever
16712 the constant requires 2 HWIs to be adequately represented.
16713 We output CONST_DOUBLEs as blocks. */
16714 if (mode == VOIDmode
16715 || (GET_MODE (rtl) == VOIDmode
16716 && maybe_ne (GET_MODE_BITSIZE (mode),
16717 HOST_BITS_PER_DOUBLE_INT)))
16718 break;
16719 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16720 if (type_die == NULL)
16721 return NULL;
16722 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16723 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16724 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16725 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16726 #if TARGET_SUPPORTS_WIDE_INT == 0
16727 if (!SCALAR_FLOAT_MODE_P (mode))
16729 mem_loc_result->dw_loc_oprnd2.val_class
16730 = dw_val_class_const_double;
16731 mem_loc_result->dw_loc_oprnd2.v.val_double
16732 = rtx_to_double_int (rtl);
16734 else
16735 #endif
16737 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16738 unsigned int length = GET_MODE_SIZE (float_mode);
16739 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16740 unsigned int elt_size = insert_float (rtl, array);
16742 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16743 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16744 = length / elt_size;
16745 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16746 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16749 break;
16751 case CONST_WIDE_INT:
16752 if (!dwarf_strict || dwarf_version >= 5)
16754 dw_die_ref type_die;
16756 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16757 if (type_die == NULL)
16758 return NULL;
16759 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16760 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16761 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16762 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16763 mem_loc_result->dw_loc_oprnd2.val_class
16764 = dw_val_class_wide_int;
16765 mem_loc_result->dw_loc_oprnd2.v.val_wide
16766 = alloc_dw_wide_int (rtx_mode_t (rtl, mode));
16768 break;
16770 case CONST_POLY_INT:
16771 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16772 break;
16774 case EQ:
16775 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16776 break;
16778 case GE:
16779 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16780 break;
16782 case GT:
16783 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16784 break;
16786 case LE:
16787 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16788 break;
16790 case LT:
16791 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16792 break;
16794 case NE:
16795 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16796 break;
16798 case GEU:
16799 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16800 break;
16802 case GTU:
16803 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16804 break;
16806 case LEU:
16807 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16808 break;
16810 case LTU:
16811 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16812 break;
16814 case UMIN:
16815 case UMAX:
16816 if (!SCALAR_INT_MODE_P (mode))
16817 break;
16818 /* FALLTHRU */
16819 case SMIN:
16820 case SMAX:
16821 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16822 break;
16824 case ZERO_EXTRACT:
16825 case SIGN_EXTRACT:
16826 if (CONST_INT_P (XEXP (rtl, 1))
16827 && CONST_INT_P (XEXP (rtl, 2))
16828 && is_a <scalar_int_mode> (mode, &int_mode)
16829 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16830 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16831 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16832 && ((unsigned) INTVAL (XEXP (rtl, 1))
16833 + (unsigned) INTVAL (XEXP (rtl, 2))
16834 <= GET_MODE_BITSIZE (int_mode)))
16836 int shift, size;
16837 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16838 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16839 if (op0 == 0)
16840 break;
16841 if (GET_CODE (rtl) == SIGN_EXTRACT)
16842 op = DW_OP_shra;
16843 else
16844 op = DW_OP_shr;
16845 mem_loc_result = op0;
16846 size = INTVAL (XEXP (rtl, 1));
16847 shift = INTVAL (XEXP (rtl, 2));
16848 if (BITS_BIG_ENDIAN)
16849 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16850 if (shift + size != (int) DWARF2_ADDR_SIZE)
16852 add_loc_descr (&mem_loc_result,
16853 int_loc_descriptor (DWARF2_ADDR_SIZE
16854 - shift - size));
16855 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16857 if (size != (int) DWARF2_ADDR_SIZE)
16859 add_loc_descr (&mem_loc_result,
16860 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16861 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16864 break;
16866 case IF_THEN_ELSE:
16868 dw_loc_descr_ref op2, bra_node, drop_node;
16869 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16870 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16871 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16872 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16873 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16874 VAR_INIT_STATUS_INITIALIZED);
16875 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16876 VAR_INIT_STATUS_INITIALIZED);
16877 if (op0 == NULL || op1 == NULL || op2 == NULL)
16878 break;
16880 mem_loc_result = op1;
16881 add_loc_descr (&mem_loc_result, op2);
16882 add_loc_descr (&mem_loc_result, op0);
16883 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16884 add_loc_descr (&mem_loc_result, bra_node);
16885 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16886 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16887 add_loc_descr (&mem_loc_result, drop_node);
16888 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16889 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16891 break;
16893 case FLOAT_EXTEND:
16894 case FLOAT_TRUNCATE:
16895 case FLOAT:
16896 case UNSIGNED_FLOAT:
16897 case FIX:
16898 case UNSIGNED_FIX:
16899 if (!dwarf_strict || dwarf_version >= 5)
16901 dw_die_ref type_die;
16902 dw_loc_descr_ref cvt;
16904 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16905 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16906 if (op0 == NULL)
16907 break;
16908 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16909 && (GET_CODE (rtl) == FLOAT
16910 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16912 type_die = base_type_for_mode (int_mode,
16913 GET_CODE (rtl) == UNSIGNED_FLOAT);
16914 if (type_die == NULL)
16915 break;
16916 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16917 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16918 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16919 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16920 add_loc_descr (&op0, cvt);
16922 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16923 if (type_die == NULL)
16924 break;
16925 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16926 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16927 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16928 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16929 add_loc_descr (&op0, cvt);
16930 if (is_a <scalar_int_mode> (mode, &int_mode)
16931 && (GET_CODE (rtl) == FIX
16932 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16934 op0 = convert_descriptor_to_mode (int_mode, op0);
16935 if (op0 == NULL)
16936 break;
16938 mem_loc_result = op0;
16940 break;
16942 case CLZ:
16943 case CTZ:
16944 case FFS:
16945 if (is_a <scalar_int_mode> (mode, &int_mode))
16946 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16947 break;
16949 case POPCOUNT:
16950 case PARITY:
16951 if (is_a <scalar_int_mode> (mode, &int_mode))
16952 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16953 break;
16955 case BSWAP:
16956 if (is_a <scalar_int_mode> (mode, &int_mode))
16957 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16958 break;
16960 case ROTATE:
16961 case ROTATERT:
16962 if (is_a <scalar_int_mode> (mode, &int_mode))
16963 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16964 break;
16966 case COMPARE:
16967 /* In theory, we could implement the above. */
16968 /* DWARF cannot represent the unsigned compare operations
16969 natively. */
16970 case SS_MULT:
16971 case US_MULT:
16972 case SS_DIV:
16973 case US_DIV:
16974 case SS_PLUS:
16975 case US_PLUS:
16976 case SS_MINUS:
16977 case US_MINUS:
16978 case SS_NEG:
16979 case US_NEG:
16980 case SS_ABS:
16981 case SS_ASHIFT:
16982 case US_ASHIFT:
16983 case SS_TRUNCATE:
16984 case US_TRUNCATE:
16985 case UNORDERED:
16986 case ORDERED:
16987 case UNEQ:
16988 case UNGE:
16989 case UNGT:
16990 case UNLE:
16991 case UNLT:
16992 case LTGT:
16993 case FRACT_CONVERT:
16994 case UNSIGNED_FRACT_CONVERT:
16995 case SAT_FRACT:
16996 case UNSIGNED_SAT_FRACT:
16997 case SQRT:
16998 case ASM_OPERANDS:
16999 case VEC_MERGE:
17000 case VEC_SELECT:
17001 case VEC_CONCAT:
17002 case VEC_DUPLICATE:
17003 case VEC_SERIES:
17004 case HIGH:
17005 case FMA:
17006 case STRICT_LOW_PART:
17007 case CONST_VECTOR:
17008 case CONST_FIXED:
17009 case CLRSB:
17010 case CLOBBER:
17011 case SMUL_HIGHPART:
17012 case UMUL_HIGHPART:
17013 case BITREVERSE:
17014 case COPYSIGN:
17015 break;
17017 case CONST_STRING:
17018 resolve_one_addr (&rtl);
17019 goto symref;
17021 /* RTL sequences inside PARALLEL record a series of DWARF operations for
17022 the expression. An UNSPEC rtx represents a raw DWARF operation,
17023 new_loc_descr is called for it to build the operation directly.
17024 Otherwise mem_loc_descriptor is called recursively. */
17025 case PARALLEL:
17027 int index = 0;
17028 dw_loc_descr_ref exp_result = NULL;
17030 for (; index < XVECLEN (rtl, 0); index++)
17032 rtx elem = XVECEXP (rtl, 0, index);
17033 if (GET_CODE (elem) == UNSPEC)
17035 /* Each DWARF operation UNSPEC contain two operands, if
17036 one operand is not used for the operation, const0_rtx is
17037 passed. */
17038 gcc_assert (XVECLEN (elem, 0) == 2);
17040 HOST_WIDE_INT dw_op = XINT (elem, 1);
17041 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
17042 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
17043 exp_result
17044 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
17045 oprnd2);
17047 else
17048 exp_result
17049 = mem_loc_descriptor (elem, mode, mem_mode,
17050 VAR_INIT_STATUS_INITIALIZED);
17052 if (!mem_loc_result)
17053 mem_loc_result = exp_result;
17054 else
17055 add_loc_descr (&mem_loc_result, exp_result);
17058 break;
17061 default:
17062 if (flag_checking)
17064 print_rtl (stderr, rtl);
17065 gcc_unreachable ();
17067 break;
17070 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17071 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17073 return mem_loc_result;
17076 /* Return a descriptor that describes the concatenation of two locations.
17077 This is typically a complex variable. */
17079 static dw_loc_descr_ref
17080 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
17082 /* At present we only track constant-sized pieces. */
17083 unsigned int size0, size1;
17084 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
17085 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
17086 return 0;
17088 dw_loc_descr_ref cc_loc_result = NULL;
17089 dw_loc_descr_ref x0_ref
17090 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17091 dw_loc_descr_ref x1_ref
17092 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17094 if (x0_ref == 0 || x1_ref == 0)
17095 return 0;
17097 cc_loc_result = x0_ref;
17098 add_loc_descr_op_piece (&cc_loc_result, size0);
17100 add_loc_descr (&cc_loc_result, x1_ref);
17101 add_loc_descr_op_piece (&cc_loc_result, size1);
17103 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
17104 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17106 return cc_loc_result;
17109 /* Return a descriptor that describes the concatenation of N
17110 locations. */
17112 static dw_loc_descr_ref
17113 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
17115 unsigned int i;
17116 dw_loc_descr_ref cc_loc_result = NULL;
17117 unsigned int n = XVECLEN (concatn, 0);
17118 unsigned int size;
17120 for (i = 0; i < n; ++i)
17122 dw_loc_descr_ref ref;
17123 rtx x = XVECEXP (concatn, 0, i);
17125 /* At present we only track constant-sized pieces. */
17126 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
17127 return NULL;
17129 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17130 if (ref == NULL)
17131 return NULL;
17133 add_loc_descr (&cc_loc_result, ref);
17134 add_loc_descr_op_piece (&cc_loc_result, size);
17137 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17138 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17140 return cc_loc_result;
17143 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
17144 for DEBUG_IMPLICIT_PTR RTL. */
17146 static dw_loc_descr_ref
17147 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
17149 dw_loc_descr_ref ret;
17150 dw_die_ref ref;
17152 if (dwarf_strict && dwarf_version < 5)
17153 return NULL;
17154 gcc_assert (VAR_P (DEBUG_IMPLICIT_PTR_DECL (rtl))
17155 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
17156 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
17157 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
17158 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
17159 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
17160 if (ref)
17162 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17163 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17164 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17166 else
17168 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17169 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
17171 return ret;
17174 /* Output a proper Dwarf location descriptor for a variable or parameter
17175 which is either allocated in a register or in a memory location. For a
17176 register, we just generate an OP_REG and the register number. For a
17177 memory location we provide a Dwarf postfix expression describing how to
17178 generate the (dynamic) address of the object onto the address stack.
17180 MODE is mode of the decl if this loc_descriptor is going to be used in
17181 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
17182 allowed, VOIDmode otherwise.
17184 If we don't know how to describe it, return 0. */
17186 static dw_loc_descr_ref
17187 loc_descriptor (rtx rtl, machine_mode mode,
17188 enum var_init_status initialized)
17190 dw_loc_descr_ref loc_result = NULL;
17191 scalar_int_mode int_mode;
17193 switch (GET_CODE (rtl))
17195 case SUBREG:
17196 /* The case of a subreg may arise when we have a local (register)
17197 variable or a formal (register) parameter which doesn't quite fill
17198 up an entire register. For now, just assume that it is
17199 legitimate to make the Dwarf info refer to the whole register which
17200 contains the given subreg. */
17201 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
17202 loc_result = loc_descriptor (SUBREG_REG (rtl),
17203 GET_MODE (SUBREG_REG (rtl)), initialized);
17204 else
17205 goto do_default;
17206 break;
17208 case REG:
17209 loc_result = reg_loc_descriptor (rtl, initialized);
17210 break;
17212 case MEM:
17213 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17214 GET_MODE (rtl), initialized);
17215 if (loc_result == NULL)
17216 loc_result = tls_mem_loc_descriptor (rtl);
17217 if (loc_result == NULL)
17219 rtx new_rtl = avoid_constant_pool_reference (rtl);
17220 if (new_rtl != rtl)
17221 loc_result = loc_descriptor (new_rtl, mode, initialized);
17223 break;
17225 case CONCAT:
17226 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17227 initialized);
17228 break;
17230 case CONCATN:
17231 loc_result = concatn_loc_descriptor (rtl, initialized);
17232 break;
17234 case VAR_LOCATION:
17235 /* Single part. */
17236 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17238 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17239 if (GET_CODE (loc) == EXPR_LIST)
17240 loc = XEXP (loc, 0);
17241 loc_result = loc_descriptor (loc, mode, initialized);
17242 break;
17245 rtl = XEXP (rtl, 1);
17246 /* FALLTHRU */
17248 case PARALLEL:
17250 rtvec par_elems = XVEC (rtl, 0);
17251 int num_elem = GET_NUM_ELEM (par_elems);
17252 machine_mode mode;
17253 int i, size;
17255 /* Create the first one, so we have something to add to. */
17256 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17257 VOIDmode, initialized);
17258 if (loc_result == NULL)
17259 return NULL;
17260 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17261 /* At present we only track constant-sized pieces. */
17262 if (!GET_MODE_SIZE (mode).is_constant (&size))
17263 return NULL;
17264 add_loc_descr_op_piece (&loc_result, size);
17265 for (i = 1; i < num_elem; i++)
17267 dw_loc_descr_ref temp;
17269 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17270 VOIDmode, initialized);
17271 if (temp == NULL)
17272 return NULL;
17273 add_loc_descr (&loc_result, temp);
17274 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17275 /* At present we only track constant-sized pieces. */
17276 if (!GET_MODE_SIZE (mode).is_constant (&size))
17277 return NULL;
17278 add_loc_descr_op_piece (&loc_result, size);
17281 break;
17283 case CONST_INT:
17284 if (mode != VOIDmode && mode != BLKmode)
17286 int_mode = as_a <scalar_int_mode> (mode);
17287 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17288 INTVAL (rtl));
17290 break;
17292 case CONST_DOUBLE:
17293 if (mode == VOIDmode)
17294 mode = GET_MODE (rtl);
17296 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17298 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17300 /* Note that a CONST_DOUBLE rtx could represent either an integer
17301 or a floating-point constant. A CONST_DOUBLE is used whenever
17302 the constant requires more than one word in order to be
17303 adequately represented. We output CONST_DOUBLEs as blocks. */
17304 scalar_mode smode = as_a <scalar_mode> (mode);
17305 loc_result = new_loc_descr (DW_OP_implicit_value,
17306 GET_MODE_SIZE (smode), 0);
17307 #if TARGET_SUPPORTS_WIDE_INT == 0
17308 if (!SCALAR_FLOAT_MODE_P (smode))
17310 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17311 loc_result->dw_loc_oprnd2.v.val_double
17312 = rtx_to_double_int (rtl);
17314 else
17315 #endif
17317 unsigned int length = GET_MODE_SIZE (smode);
17318 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17319 unsigned int elt_size = insert_float (rtl, array);
17321 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17322 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17323 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17324 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17327 break;
17329 case CONST_WIDE_INT:
17330 if (mode == VOIDmode)
17331 mode = GET_MODE (rtl);
17333 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17335 int_mode = as_a <scalar_int_mode> (mode);
17336 loc_result = new_loc_descr (DW_OP_implicit_value,
17337 GET_MODE_SIZE (int_mode), 0);
17338 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17339 loc_result->dw_loc_oprnd2.v.val_wide
17340 = alloc_dw_wide_int (rtx_mode_t (rtl, int_mode));
17342 break;
17344 case CONST_VECTOR:
17345 if (mode == VOIDmode)
17346 mode = GET_MODE (rtl);
17348 if (mode != VOIDmode
17349 /* The combination of a length and byte elt_size doesn't extend
17350 naturally to boolean vectors, where several elements are packed
17351 into the same byte. */
17352 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17353 && (dwarf_version >= 4 || !dwarf_strict))
17355 unsigned int length;
17356 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17357 return NULL;
17359 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17360 unsigned char *array
17361 = ggc_vec_alloc<unsigned char> (length * elt_size);
17362 unsigned int i;
17363 unsigned char *p;
17364 machine_mode imode = GET_MODE_INNER (mode);
17366 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17367 switch (GET_MODE_CLASS (mode))
17369 case MODE_VECTOR_INT:
17370 for (i = 0, p = array; i < length; i++, p += elt_size)
17372 rtx elt = CONST_VECTOR_ELT (rtl, i);
17373 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17375 break;
17377 case MODE_VECTOR_FLOAT:
17378 for (i = 0, p = array; i < length; i++, p += elt_size)
17380 rtx elt = CONST_VECTOR_ELT (rtl, i);
17381 insert_float (elt, p);
17383 break;
17385 default:
17386 gcc_unreachable ();
17389 loc_result = new_loc_descr (DW_OP_implicit_value,
17390 length * elt_size, 0);
17391 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17392 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17393 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17394 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17396 break;
17398 case CONST:
17399 if (mode == VOIDmode
17400 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17401 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17402 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17404 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17405 break;
17407 /* FALLTHROUGH */
17408 case SYMBOL_REF:
17409 if (!const_ok_for_output (rtl))
17410 break;
17411 /* FALLTHROUGH */
17412 case LABEL_REF:
17413 if (is_a <scalar_int_mode> (mode, &int_mode)
17414 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17415 && (dwarf_version >= 4 || !dwarf_strict))
17417 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17418 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17419 vec_safe_push (used_rtx_array, rtl);
17421 break;
17423 case DEBUG_IMPLICIT_PTR:
17424 loc_result = implicit_ptr_descriptor (rtl, 0);
17425 break;
17427 case PLUS:
17428 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17429 && CONST_INT_P (XEXP (rtl, 1)))
17431 loc_result
17432 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17433 break;
17435 /* FALLTHRU */
17436 do_default:
17437 default:
17438 if ((is_a <scalar_int_mode> (mode, &int_mode)
17439 && GET_MODE (rtl) == int_mode
17440 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17441 && dwarf_version >= 4)
17442 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17444 /* Value expression. */
17445 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17446 if (loc_result)
17447 add_loc_descr (&loc_result,
17448 new_loc_descr (DW_OP_stack_value, 0, 0));
17450 break;
17453 return loc_result;
17456 /* We need to figure out what section we should use as the base for the
17457 address ranges where a given location is valid.
17458 1. If this particular DECL has a section associated with it, use that.
17459 2. If this function has a section associated with it, use that.
17460 3. Otherwise, use the text section.
17461 XXX: If you split a variable across multiple sections, we won't notice. */
17463 static const char *
17464 secname_for_decl (const_tree decl)
17466 const char *secname;
17468 if (VAR_OR_FUNCTION_DECL_P (decl)
17469 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17470 && DECL_SECTION_NAME (decl))
17471 secname = DECL_SECTION_NAME (decl);
17472 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17474 if (in_cold_section_p)
17476 section *sec = current_function_section ();
17477 if (sec->common.flags & SECTION_NAMED)
17478 return sec->named.name;
17480 secname = DECL_SECTION_NAME (current_function_decl);
17482 else if (cfun && in_cold_section_p)
17483 secname = crtl->subsections.cold_section_label;
17484 else
17485 secname = text_section_label;
17487 return secname;
17490 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17492 static bool
17493 decl_by_reference_p (tree decl)
17495 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17496 || VAR_P (decl))
17497 && DECL_BY_REFERENCE (decl));
17500 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17501 for VARLOC. */
17503 static dw_loc_descr_ref
17504 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17505 enum var_init_status initialized)
17507 int have_address = 0;
17508 dw_loc_descr_ref descr;
17509 machine_mode mode;
17511 if (want_address != 2)
17513 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17514 /* Single part. */
17515 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17517 varloc = PAT_VAR_LOCATION_LOC (varloc);
17518 if (GET_CODE (varloc) == EXPR_LIST)
17519 varloc = XEXP (varloc, 0);
17520 mode = GET_MODE (varloc);
17521 if (MEM_P (varloc))
17523 rtx addr = XEXP (varloc, 0);
17524 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17525 mode, initialized);
17526 if (descr)
17527 have_address = 1;
17528 else
17530 rtx x = avoid_constant_pool_reference (varloc);
17531 if (x != varloc)
17532 descr = mem_loc_descriptor (x, mode, VOIDmode,
17533 initialized);
17536 else
17537 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17539 else
17540 return 0;
17542 else
17544 if (GET_CODE (varloc) == VAR_LOCATION)
17545 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17546 else
17547 mode = DECL_MODE (loc);
17548 descr = loc_descriptor (varloc, mode, initialized);
17549 have_address = 1;
17552 if (!descr)
17553 return 0;
17555 if (want_address == 2 && !have_address
17556 && (dwarf_version >= 4 || !dwarf_strict))
17558 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17560 expansion_failed (loc, NULL_RTX,
17561 "DWARF address size mismatch");
17562 return 0;
17564 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17565 have_address = 1;
17567 /* Show if we can't fill the request for an address. */
17568 if (want_address && !have_address)
17570 expansion_failed (loc, NULL_RTX,
17571 "Want address and only have value");
17572 return 0;
17575 /* If we've got an address and don't want one, dereference. */
17576 if (!want_address && have_address)
17578 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17579 enum dwarf_location_atom op;
17581 if (size > DWARF2_ADDR_SIZE || size == -1)
17583 expansion_failed (loc, NULL_RTX,
17584 "DWARF address size mismatch");
17585 return 0;
17587 else if (size == DWARF2_ADDR_SIZE)
17588 op = DW_OP_deref;
17589 else
17590 op = DW_OP_deref_size;
17592 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17595 return descr;
17598 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17599 if it is not possible. */
17601 static dw_loc_descr_ref
17602 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17604 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17605 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17606 else if (dwarf_version >= 3 || !dwarf_strict)
17607 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17608 else
17609 return NULL;
17612 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17613 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17615 static dw_loc_descr_ref
17616 dw_sra_loc_expr (tree decl, rtx loc)
17618 rtx p;
17619 unsigned HOST_WIDE_INT padsize = 0;
17620 dw_loc_descr_ref descr, *descr_tail;
17621 unsigned HOST_WIDE_INT decl_size;
17622 rtx varloc;
17623 enum var_init_status initialized;
17625 if (DECL_SIZE (decl) == NULL
17626 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17627 return NULL;
17629 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17630 descr = NULL;
17631 descr_tail = &descr;
17633 for (p = loc; p; p = XEXP (p, 1))
17635 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17636 rtx loc_note = *decl_piece_varloc_ptr (p);
17637 dw_loc_descr_ref cur_descr;
17638 dw_loc_descr_ref *tail, last = NULL;
17639 unsigned HOST_WIDE_INT opsize = 0;
17641 if (loc_note == NULL_RTX
17642 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17644 padsize += bitsize;
17645 continue;
17647 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17648 varloc = NOTE_VAR_LOCATION (loc_note);
17649 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17650 if (cur_descr == NULL)
17652 padsize += bitsize;
17653 continue;
17656 /* Check that cur_descr either doesn't use
17657 DW_OP_*piece operations, or their sum is equal
17658 to bitsize. Otherwise we can't embed it. */
17659 for (tail = &cur_descr; *tail != NULL;
17660 tail = &(*tail)->dw_loc_next)
17661 if ((*tail)->dw_loc_opc == DW_OP_piece)
17663 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17664 * BITS_PER_UNIT;
17665 last = *tail;
17667 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17669 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17670 last = *tail;
17673 if (last != NULL && opsize != bitsize)
17675 padsize += bitsize;
17676 /* Discard the current piece of the descriptor and release any
17677 addr_table entries it uses. */
17678 remove_loc_list_addr_table_entries (cur_descr);
17679 continue;
17682 /* If there is a hole, add DW_OP_*piece after empty DWARF
17683 expression, which means that those bits are optimized out. */
17684 if (padsize)
17686 if (padsize > decl_size)
17688 remove_loc_list_addr_table_entries (cur_descr);
17689 goto discard_descr;
17691 decl_size -= padsize;
17692 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17693 if (*descr_tail == NULL)
17695 remove_loc_list_addr_table_entries (cur_descr);
17696 goto discard_descr;
17698 descr_tail = &(*descr_tail)->dw_loc_next;
17699 padsize = 0;
17701 *descr_tail = cur_descr;
17702 descr_tail = tail;
17703 if (bitsize > decl_size)
17704 goto discard_descr;
17705 decl_size -= bitsize;
17706 if (last == NULL)
17708 HOST_WIDE_INT offset = 0;
17709 if (GET_CODE (varloc) == VAR_LOCATION
17710 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17712 varloc = PAT_VAR_LOCATION_LOC (varloc);
17713 if (GET_CODE (varloc) == EXPR_LIST)
17714 varloc = XEXP (varloc, 0);
17718 if (GET_CODE (varloc) == CONST
17719 || GET_CODE (varloc) == SIGN_EXTEND
17720 || GET_CODE (varloc) == ZERO_EXTEND)
17721 varloc = XEXP (varloc, 0);
17722 else if (GET_CODE (varloc) == SUBREG)
17723 varloc = SUBREG_REG (varloc);
17724 else
17725 break;
17727 while (1);
17728 /* DW_OP_bit_size offset should be zero for register
17729 or implicit location descriptions and empty location
17730 descriptions, but for memory addresses needs big endian
17731 adjustment. */
17732 if (MEM_P (varloc))
17734 unsigned HOST_WIDE_INT memsize;
17735 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17736 goto discard_descr;
17737 memsize *= BITS_PER_UNIT;
17738 if (memsize != bitsize)
17740 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17741 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17742 goto discard_descr;
17743 if (memsize < bitsize)
17744 goto discard_descr;
17745 if (BITS_BIG_ENDIAN)
17746 offset = memsize - bitsize;
17750 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17751 if (*descr_tail == NULL)
17752 goto discard_descr;
17753 descr_tail = &(*descr_tail)->dw_loc_next;
17757 /* If there were any non-empty expressions, add padding till the end of
17758 the decl. */
17759 if (descr != NULL && decl_size != 0)
17761 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17762 if (*descr_tail == NULL)
17763 goto discard_descr;
17765 return descr;
17767 discard_descr:
17768 /* Discard the descriptor and release any addr_table entries it uses. */
17769 remove_loc_list_addr_table_entries (descr);
17770 return NULL;
17773 /* Return the dwarf representation of the location list LOC_LIST of
17774 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17775 function. */
17777 static dw_loc_list_ref
17778 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17780 const char *endname, *secname;
17781 var_loc_view endview;
17782 rtx varloc;
17783 enum var_init_status initialized;
17784 struct var_loc_node *node;
17785 dw_loc_descr_ref descr;
17786 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17787 dw_loc_list_ref list = NULL;
17788 dw_loc_list_ref *listp = &list;
17790 /* Now that we know what section we are using for a base,
17791 actually construct the list of locations.
17792 The first location information is what is passed to the
17793 function that creates the location list, and the remaining
17794 locations just get added on to that list.
17795 Note that we only know the start address for a location
17796 (IE location changes), so to build the range, we use
17797 the range [current location start, next location start].
17798 This means we have to special case the last node, and generate
17799 a range of [last location start, end of function label]. */
17801 if (cfun && crtl->has_bb_partition)
17803 bool save_in_cold_section_p = in_cold_section_p;
17804 in_cold_section_p = first_function_block_is_cold;
17805 if (loc_list->last_before_switch == NULL)
17806 in_cold_section_p = !in_cold_section_p;
17807 secname = secname_for_decl (decl);
17808 in_cold_section_p = save_in_cold_section_p;
17810 else
17811 secname = secname_for_decl (decl);
17813 for (node = loc_list->first; node; node = node->next)
17815 bool range_across_switch = false;
17816 if (GET_CODE (node->loc) == EXPR_LIST
17817 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17819 if (GET_CODE (node->loc) == EXPR_LIST)
17821 descr = NULL;
17822 /* This requires DW_OP_{,bit_}piece, which is not usable
17823 inside DWARF expressions. */
17824 if (want_address == 2)
17825 descr = dw_sra_loc_expr (decl, node->loc);
17827 else
17829 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17830 varloc = NOTE_VAR_LOCATION (node->loc);
17831 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17833 if (descr)
17835 /* If section switch happens in between node->label
17836 and node->next->label (or end of function) and
17837 we can't emit it as a single entry list,
17838 emit two ranges, first one ending at the end
17839 of first partition and second one starting at the
17840 beginning of second partition. */
17841 if (node == loc_list->last_before_switch
17842 && (node != loc_list->first || loc_list->first->next
17843 /* If we are to emit a view number, we will emit
17844 a loclist rather than a single location
17845 expression for the entire function (see
17846 loc_list_has_views), so we have to split the
17847 range that straddles across partitions. */
17848 || !ZERO_VIEW_P (node->view))
17849 && current_function_decl)
17851 endname = cfun->fde->dw_fde_end;
17852 endview = 0;
17853 range_across_switch = true;
17855 /* The variable has a location between NODE->LABEL and
17856 NODE->NEXT->LABEL. */
17857 else if (node->next)
17858 endname = node->next->label, endview = node->next->view;
17859 /* If the variable has a location at the last label
17860 it keeps its location until the end of function. */
17861 else if (!current_function_decl)
17862 endname = text_end_label, endview = 0;
17863 else
17865 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17866 current_function_funcdef_no);
17867 endname = ggc_strdup (label_id);
17868 endview = 0;
17871 *listp = new_loc_list (descr, node->label, node->view,
17872 endname, endview, secname);
17873 if (TREE_CODE (decl) == PARM_DECL
17874 && node == loc_list->first
17875 && NOTE_P (node->loc)
17876 && strcmp (node->label, endname) == 0)
17877 (*listp)->force = true;
17878 listp = &(*listp)->dw_loc_next;
17882 if (cfun
17883 && crtl->has_bb_partition
17884 && node == loc_list->last_before_switch)
17886 bool save_in_cold_section_p = in_cold_section_p;
17887 in_cold_section_p = !first_function_block_is_cold;
17888 secname = secname_for_decl (decl);
17889 in_cold_section_p = save_in_cold_section_p;
17892 if (range_across_switch)
17894 if (GET_CODE (node->loc) == EXPR_LIST)
17895 descr = dw_sra_loc_expr (decl, node->loc);
17896 else
17898 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17899 varloc = NOTE_VAR_LOCATION (node->loc);
17900 descr = dw_loc_list_1 (decl, varloc, want_address,
17901 initialized);
17903 gcc_assert (descr);
17904 /* The variable has a location between NODE->LABEL and
17905 NODE->NEXT->LABEL. */
17906 if (node->next)
17907 endname = node->next->label, endview = node->next->view;
17908 else
17909 endname = cfun->fde->dw_fde_second_end, endview = 0;
17910 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17911 endname, endview, secname);
17912 listp = &(*listp)->dw_loc_next;
17916 /* Try to avoid the overhead of a location list emitting a location
17917 expression instead, but only if we didn't have more than one
17918 location entry in the first place. If some entries were not
17919 representable, we don't want to pretend a single entry that was
17920 applies to the entire scope in which the variable is
17921 available. */
17922 if (list && loc_list->first->next)
17923 gen_llsym (list);
17924 else
17925 maybe_gen_llsym (list);
17927 return list;
17930 /* Return true if the loc_list has only single element and thus
17931 can be represented as location description. */
17933 static bool
17934 single_element_loc_list_p (dw_loc_list_ref list)
17936 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17937 return !list->ll_symbol;
17940 /* Duplicate a single element of location list. */
17942 static inline dw_loc_descr_ref
17943 copy_loc_descr (dw_loc_descr_ref ref)
17945 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17946 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17947 return copy;
17950 /* To each location in list LIST append loc descr REF. */
17952 static void
17953 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17955 dw_loc_descr_ref copy;
17956 add_loc_descr (&list->expr, ref);
17957 list = list->dw_loc_next;
17958 while (list)
17960 copy = copy_loc_descr (ref);
17961 add_loc_descr (&list->expr, copy);
17962 while (copy->dw_loc_next)
17963 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17964 list = list->dw_loc_next;
17968 /* To each location in list LIST prepend loc descr REF. */
17970 static void
17971 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17973 dw_loc_descr_ref copy;
17974 dw_loc_descr_ref ref_end = list->expr;
17975 add_loc_descr (&ref, list->expr);
17976 list->expr = ref;
17977 list = list->dw_loc_next;
17978 while (list)
17980 dw_loc_descr_ref end = list->expr;
17981 list->expr = copy = copy_loc_descr (ref);
17982 while (copy->dw_loc_next != ref_end)
17983 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17984 copy->dw_loc_next = end;
17985 list = list->dw_loc_next;
17989 /* Given two lists RET and LIST
17990 produce location list that is result of adding expression in LIST
17991 to expression in RET on each position in program.
17992 Might be destructive on both RET and LIST.
17994 TODO: We handle only simple cases of RET or LIST having at most one
17995 element. General case would involve sorting the lists in program order
17996 and merging them that will need some additional work.
17997 Adding that will improve quality of debug info especially for SRA-ed
17998 structures. */
18000 static void
18001 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
18003 if (!list)
18004 return;
18005 if (!*ret)
18007 *ret = list;
18008 return;
18010 if (!list->dw_loc_next)
18012 add_loc_descr_to_each (*ret, list->expr);
18013 return;
18015 if (!(*ret)->dw_loc_next)
18017 prepend_loc_descr_to_each (list, (*ret)->expr);
18018 *ret = list;
18019 return;
18021 expansion_failed (NULL_TREE, NULL_RTX,
18022 "Don't know how to merge two non-trivial"
18023 " location lists.\n");
18024 *ret = NULL;
18025 return;
18028 /* LOC is constant expression. Try a luck, look it up in constant
18029 pool and return its loc_descr of its address. */
18031 static dw_loc_descr_ref
18032 cst_pool_loc_descr (tree loc)
18034 /* Get an RTL for this, if something has been emitted. */
18035 rtx rtl = lookup_constant_def (loc);
18037 if (!rtl || !MEM_P (rtl))
18039 gcc_assert (!rtl);
18040 return 0;
18042 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
18044 /* TODO: We might get more coverage if we was actually delaying expansion
18045 of all expressions till end of compilation when constant pools are fully
18046 populated. */
18047 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
18049 expansion_failed (loc, NULL_RTX,
18050 "CST value in contant pool but not marked.");
18051 return 0;
18053 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
18054 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
18057 /* Return dw_loc_list representing address of addr_expr LOC
18058 by looking for inner INDIRECT_REF expression and turning
18059 it into simple arithmetics.
18061 See loc_list_from_tree for the meaning of CONTEXT. */
18063 static dw_loc_list_ref
18064 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
18065 loc_descr_context *context)
18067 tree obj, offset;
18068 poly_int64 bitsize, bitpos, bytepos;
18069 machine_mode mode;
18070 int unsignedp, reversep, volatilep = 0;
18071 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18073 obj = get_inner_reference (TREE_OPERAND (loc, 0),
18074 &bitsize, &bitpos, &offset, &mode,
18075 &unsignedp, &reversep, &volatilep);
18076 STRIP_NOPS (obj);
18077 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
18079 expansion_failed (loc, NULL_RTX, "bitfield access");
18080 return 0;
18082 if (!INDIRECT_REF_P (obj))
18084 expansion_failed (obj,
18085 NULL_RTX, "no indirect ref in inner refrence");
18086 return 0;
18088 if (!offset && known_eq (bitpos, 0))
18089 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
18090 context);
18091 else if (toplev
18092 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
18093 && (dwarf_version >= 4 || !dwarf_strict))
18095 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
18096 if (!list_ret)
18097 return 0;
18098 if (offset)
18100 /* Variable offset. */
18101 list_ret1 = loc_list_from_tree (offset, 0, context);
18102 if (list_ret1 == 0)
18103 return 0;
18104 add_loc_list (&list_ret, list_ret1);
18105 if (!list_ret)
18106 return 0;
18107 add_loc_descr_to_each (list_ret,
18108 new_loc_descr (DW_OP_plus, 0, 0));
18110 HOST_WIDE_INT value;
18111 if (bytepos.is_constant (&value) && value > 0)
18112 add_loc_descr_to_each (list_ret,
18113 new_loc_descr (DW_OP_plus_uconst, value, 0));
18114 else if (maybe_ne (bytepos, 0))
18115 loc_list_plus_const (list_ret, bytepos);
18116 add_loc_descr_to_each (list_ret,
18117 new_loc_descr (DW_OP_stack_value, 0, 0));
18119 return list_ret;
18122 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
18123 all operations from LOC are nops, move to the last one. Insert in NOPS all
18124 operations that are skipped. */
18126 static void
18127 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
18128 hash_set<dw_loc_descr_ref> &nops)
18130 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
18132 nops.add (loc);
18133 loc = loc->dw_loc_next;
18137 /* Helper for loc_descr_without_nops: free the location description operation
18138 P. */
18140 bool
18141 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
18143 ggc_free (loc);
18144 return true;
18147 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
18148 finishes LOC. */
18150 static void
18151 loc_descr_without_nops (dw_loc_descr_ref &loc)
18153 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
18154 return;
18156 /* Set of all DW_OP_nop operations we remove. */
18157 hash_set<dw_loc_descr_ref> nops;
18159 /* First, strip all prefix NOP operations in order to keep the head of the
18160 operations list. */
18161 loc_descr_to_next_no_nop (loc, nops);
18163 for (dw_loc_descr_ref cur = loc; cur != NULL;)
18165 /* For control flow operations: strip "prefix" nops in destination
18166 labels. */
18167 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
18168 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
18169 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
18170 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
18172 /* Do the same for the operations that follow, then move to the next
18173 iteration. */
18174 if (cur->dw_loc_next != NULL)
18175 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
18176 cur = cur->dw_loc_next;
18179 nops.traverse<void *, free_loc_descr> (NULL);
18183 struct dwarf_procedure_info;
18185 /* Helper structure for location descriptions generation. */
18186 struct loc_descr_context
18188 /* The type that is implicitly referenced by DW_OP_push_object_address, or
18189 NULL_TREE if DW_OP_push_object_address in invalid for this location
18190 description. This is used when processing PLACEHOLDER_EXPR nodes. */
18191 tree context_type;
18192 /* The ..._DECL node that should be translated as a
18193 DW_OP_push_object_address operation. */
18194 tree base_decl;
18195 /* Information about the DWARF procedure we are currently generating. NULL if
18196 we are not generating a DWARF procedure. */
18197 struct dwarf_procedure_info *dpi;
18198 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
18199 by consumer. Used for DW_TAG_generic_subrange attributes. */
18200 bool placeholder_arg;
18201 /* True if PLACEHOLDER_EXPR has been seen. */
18202 bool placeholder_seen;
18203 /* True if strict preservation of signedness has been requested. */
18204 bool strict_signedness;
18207 /* DWARF procedures generation
18209 DWARF expressions (aka. location descriptions) are used to encode variable
18210 things such as sizes or offsets. Such computations can have redundant parts
18211 that can be factorized in order to reduce the size of the output debug
18212 information. This is the whole point of DWARF procedures.
18214 Thanks to stor-layout.cc, size and offset expressions in GENERIC trees are
18215 already factorized into functions ("size functions") in order to handle very
18216 big and complex types. Such functions are quite simple: they have integral
18217 arguments, they return an integral result and their body contains only a
18218 return statement with arithmetic expressions. This is the only kind of
18219 function we are interested in translating into DWARF procedures, here.
18221 DWARF expressions and DWARF procedure are executed using a stack, so we have
18222 to define some calling convention for them to interact. Let's say that:
18224 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18225 all arguments in reverse order (right-to-left) so that when the DWARF
18226 procedure execution starts, the first argument is the top of the stack.
18228 - Then, when returning, the DWARF procedure must have consumed all arguments
18229 on the stack, must have pushed the result and touched nothing else.
18231 - Each integral argument and the result are integral types can be hold in a
18232 single stack slot.
18234 - We call "frame offset" the number of stack slots that are "under DWARF
18235 procedure control": it includes the arguments slots, the temporaries and
18236 the result slot. Thus, it is equal to the number of arguments when the
18237 procedure execution starts and must be equal to one (the result) when it
18238 returns. */
18240 /* Helper structure used when generating operations for a DWARF procedure. */
18241 struct dwarf_procedure_info
18243 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18244 currently translated. */
18245 tree fndecl;
18246 /* The number of arguments FNDECL takes. */
18247 unsigned args_count;
18250 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18251 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18252 equate it to this DIE. */
18254 static dw_die_ref
18255 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18256 dw_die_ref parent_die)
18258 dw_die_ref dwarf_proc_die;
18260 if ((dwarf_version < 3 && dwarf_strict)
18261 || location == NULL)
18262 return NULL;
18264 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18265 if (fndecl)
18266 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18267 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18268 return dwarf_proc_die;
18271 /* Return whether TYPE is a supported type as a DWARF procedure argument
18272 type or return type (we handle only scalar types and pointer types that
18273 aren't wider than the DWARF expression evaluation stack). */
18275 static bool
18276 is_handled_procedure_type (tree type)
18278 return ((INTEGRAL_TYPE_P (type)
18279 || TREE_CODE (type) == OFFSET_TYPE
18280 || TREE_CODE (type) == POINTER_TYPE)
18281 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18284 /* Helper for resolve_args_picking: do the same but stop when coming across
18285 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18286 offset *before* evaluating the corresponding operation. */
18288 static bool
18289 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18290 struct dwarf_procedure_info *dpi,
18291 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18293 /* The "frame_offset" identifier is already used to name a macro... */
18294 unsigned frame_offset_ = initial_frame_offset;
18295 dw_loc_descr_ref l;
18297 for (l = loc; l != NULL;)
18299 bool existed;
18300 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18302 /* If we already met this node, there is nothing to compute anymore. */
18303 if (existed)
18305 /* Make sure that the stack size is consistent wherever the execution
18306 flow comes from. */
18307 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18308 break;
18310 l_frame_offset = frame_offset_;
18312 /* If needed, relocate the picking offset with respect to the frame
18313 offset. */
18314 if (l->frame_offset_rel)
18316 unsigned HOST_WIDE_INT off;
18317 switch (l->dw_loc_opc)
18319 case DW_OP_pick:
18320 off = l->dw_loc_oprnd1.v.val_unsigned;
18321 break;
18322 case DW_OP_dup:
18323 off = 0;
18324 break;
18325 case DW_OP_over:
18326 off = 1;
18327 break;
18328 default:
18329 gcc_unreachable ();
18331 /* frame_offset_ is the size of the current stack frame, including
18332 incoming arguments. Besides, the arguments are pushed
18333 right-to-left. Thus, in order to access the Nth argument from
18334 this operation node, the picking has to skip temporaries *plus*
18335 one stack slot per argument (0 for the first one, 1 for the second
18336 one, etc.).
18338 The targetted argument number (N) is already set as the operand,
18339 and the number of temporaries can be computed with:
18340 frame_offsets_ - dpi->args_count */
18341 off += frame_offset_ - dpi->args_count;
18343 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18344 if (off > 255)
18345 return false;
18347 if (off == 0)
18349 l->dw_loc_opc = DW_OP_dup;
18350 l->dw_loc_oprnd1.v.val_unsigned = 0;
18352 else if (off == 1)
18354 l->dw_loc_opc = DW_OP_over;
18355 l->dw_loc_oprnd1.v.val_unsigned = 0;
18357 else
18359 l->dw_loc_opc = DW_OP_pick;
18360 l->dw_loc_oprnd1.v.val_unsigned = off;
18364 /* Update frame_offset according to the effect the current operation has
18365 on the stack. */
18366 switch (l->dw_loc_opc)
18368 case DW_OP_deref:
18369 case DW_OP_swap:
18370 case DW_OP_rot:
18371 case DW_OP_abs:
18372 case DW_OP_neg:
18373 case DW_OP_not:
18374 case DW_OP_plus_uconst:
18375 case DW_OP_skip:
18376 case DW_OP_reg0:
18377 case DW_OP_reg1:
18378 case DW_OP_reg2:
18379 case DW_OP_reg3:
18380 case DW_OP_reg4:
18381 case DW_OP_reg5:
18382 case DW_OP_reg6:
18383 case DW_OP_reg7:
18384 case DW_OP_reg8:
18385 case DW_OP_reg9:
18386 case DW_OP_reg10:
18387 case DW_OP_reg11:
18388 case DW_OP_reg12:
18389 case DW_OP_reg13:
18390 case DW_OP_reg14:
18391 case DW_OP_reg15:
18392 case DW_OP_reg16:
18393 case DW_OP_reg17:
18394 case DW_OP_reg18:
18395 case DW_OP_reg19:
18396 case DW_OP_reg20:
18397 case DW_OP_reg21:
18398 case DW_OP_reg22:
18399 case DW_OP_reg23:
18400 case DW_OP_reg24:
18401 case DW_OP_reg25:
18402 case DW_OP_reg26:
18403 case DW_OP_reg27:
18404 case DW_OP_reg28:
18405 case DW_OP_reg29:
18406 case DW_OP_reg30:
18407 case DW_OP_reg31:
18408 case DW_OP_bregx:
18409 case DW_OP_piece:
18410 case DW_OP_deref_size:
18411 case DW_OP_nop:
18412 case DW_OP_bit_piece:
18413 case DW_OP_implicit_value:
18414 case DW_OP_stack_value:
18415 case DW_OP_deref_type:
18416 case DW_OP_convert:
18417 case DW_OP_reinterpret:
18418 case DW_OP_GNU_deref_type:
18419 case DW_OP_GNU_convert:
18420 case DW_OP_GNU_reinterpret:
18421 break;
18423 case DW_OP_addr:
18424 case DW_OP_const1u:
18425 case DW_OP_const1s:
18426 case DW_OP_const2u:
18427 case DW_OP_const2s:
18428 case DW_OP_const4u:
18429 case DW_OP_const4s:
18430 case DW_OP_const8u:
18431 case DW_OP_const8s:
18432 case DW_OP_constu:
18433 case DW_OP_consts:
18434 case DW_OP_dup:
18435 case DW_OP_over:
18436 case DW_OP_pick:
18437 case DW_OP_lit0:
18438 case DW_OP_lit1:
18439 case DW_OP_lit2:
18440 case DW_OP_lit3:
18441 case DW_OP_lit4:
18442 case DW_OP_lit5:
18443 case DW_OP_lit6:
18444 case DW_OP_lit7:
18445 case DW_OP_lit8:
18446 case DW_OP_lit9:
18447 case DW_OP_lit10:
18448 case DW_OP_lit11:
18449 case DW_OP_lit12:
18450 case DW_OP_lit13:
18451 case DW_OP_lit14:
18452 case DW_OP_lit15:
18453 case DW_OP_lit16:
18454 case DW_OP_lit17:
18455 case DW_OP_lit18:
18456 case DW_OP_lit19:
18457 case DW_OP_lit20:
18458 case DW_OP_lit21:
18459 case DW_OP_lit22:
18460 case DW_OP_lit23:
18461 case DW_OP_lit24:
18462 case DW_OP_lit25:
18463 case DW_OP_lit26:
18464 case DW_OP_lit27:
18465 case DW_OP_lit28:
18466 case DW_OP_lit29:
18467 case DW_OP_lit30:
18468 case DW_OP_lit31:
18469 case DW_OP_breg0:
18470 case DW_OP_breg1:
18471 case DW_OP_breg2:
18472 case DW_OP_breg3:
18473 case DW_OP_breg4:
18474 case DW_OP_breg5:
18475 case DW_OP_breg6:
18476 case DW_OP_breg7:
18477 case DW_OP_breg8:
18478 case DW_OP_breg9:
18479 case DW_OP_breg10:
18480 case DW_OP_breg11:
18481 case DW_OP_breg12:
18482 case DW_OP_breg13:
18483 case DW_OP_breg14:
18484 case DW_OP_breg15:
18485 case DW_OP_breg16:
18486 case DW_OP_breg17:
18487 case DW_OP_breg18:
18488 case DW_OP_breg19:
18489 case DW_OP_breg20:
18490 case DW_OP_breg21:
18491 case DW_OP_breg22:
18492 case DW_OP_breg23:
18493 case DW_OP_breg24:
18494 case DW_OP_breg25:
18495 case DW_OP_breg26:
18496 case DW_OP_breg27:
18497 case DW_OP_breg28:
18498 case DW_OP_breg29:
18499 case DW_OP_breg30:
18500 case DW_OP_breg31:
18501 case DW_OP_fbreg:
18502 case DW_OP_push_object_address:
18503 case DW_OP_call_frame_cfa:
18504 case DW_OP_GNU_variable_value:
18505 case DW_OP_GNU_addr_index:
18506 case DW_OP_GNU_const_index:
18507 ++frame_offset_;
18508 break;
18510 case DW_OP_drop:
18511 case DW_OP_xderef:
18512 case DW_OP_and:
18513 case DW_OP_div:
18514 case DW_OP_minus:
18515 case DW_OP_mod:
18516 case DW_OP_mul:
18517 case DW_OP_or:
18518 case DW_OP_plus:
18519 case DW_OP_shl:
18520 case DW_OP_shr:
18521 case DW_OP_shra:
18522 case DW_OP_xor:
18523 case DW_OP_bra:
18524 case DW_OP_eq:
18525 case DW_OP_ge:
18526 case DW_OP_gt:
18527 case DW_OP_le:
18528 case DW_OP_lt:
18529 case DW_OP_ne:
18530 case DW_OP_regx:
18531 case DW_OP_xderef_size:
18532 --frame_offset_;
18533 break;
18535 case DW_OP_call2:
18536 case DW_OP_call4:
18537 case DW_OP_call_ref:
18539 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18540 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18542 if (stack_usage == NULL)
18543 return false;
18544 frame_offset_ += *stack_usage;
18545 break;
18548 case DW_OP_implicit_pointer:
18549 case DW_OP_entry_value:
18550 case DW_OP_const_type:
18551 case DW_OP_regval_type:
18552 case DW_OP_form_tls_address:
18553 case DW_OP_GNU_push_tls_address:
18554 case DW_OP_GNU_uninit:
18555 case DW_OP_GNU_encoded_addr:
18556 case DW_OP_GNU_implicit_pointer:
18557 case DW_OP_GNU_entry_value:
18558 case DW_OP_GNU_const_type:
18559 case DW_OP_GNU_regval_type:
18560 case DW_OP_GNU_parameter_ref:
18561 /* loc_list_from_tree will probably not output these operations for
18562 size functions, so assume they will not appear here. */
18563 /* Fall through... */
18565 default:
18566 gcc_unreachable ();
18569 /* Now, follow the control flow (except subroutine calls). */
18570 switch (l->dw_loc_opc)
18572 case DW_OP_bra:
18573 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18574 frame_offsets))
18575 return false;
18576 /* Fall through. */
18578 case DW_OP_skip:
18579 l = l->dw_loc_oprnd1.v.val_loc;
18580 break;
18582 case DW_OP_stack_value:
18583 return true;
18585 default:
18586 l = l->dw_loc_next;
18587 break;
18591 return true;
18594 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18595 operations) in order to resolve the operand of DW_OP_pick operations that
18596 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18597 offset *before* LOC is executed. Return if all relocations were
18598 successful. */
18600 static bool
18601 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18602 struct dwarf_procedure_info *dpi)
18604 /* Associate to all visited operations the frame offset *before* evaluating
18605 this operation. */
18606 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18608 return
18609 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18612 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18613 Return NULL if it is not possible. */
18615 static dw_die_ref
18616 function_to_dwarf_procedure (tree fndecl)
18618 struct dwarf_procedure_info dpi;
18619 struct loc_descr_context ctx = {
18620 NULL_TREE, /* context_type */
18621 NULL_TREE, /* base_decl */
18622 &dpi, /* dpi */
18623 false, /* placeholder_arg */
18624 false, /* placeholder_seen */
18625 true /* strict_signedness */
18627 dw_die_ref dwarf_proc_die;
18628 tree tree_body = DECL_SAVED_TREE (fndecl);
18629 dw_loc_descr_ref loc_body, epilogue;
18631 tree cursor;
18632 unsigned i;
18634 /* Do not generate multiple DWARF procedures for the same function
18635 declaration. */
18636 dwarf_proc_die = lookup_decl_die (fndecl);
18637 if (dwarf_proc_die != NULL)
18638 return dwarf_proc_die;
18640 /* DWARF procedures are available starting with the DWARFv3 standard. */
18641 if (dwarf_version < 3 && dwarf_strict)
18642 return NULL;
18644 /* We handle only functions for which we still have a body, that return a
18645 supported type and that takes arguments with supported types. Note that
18646 there is no point translating functions that return nothing. */
18647 if (tree_body == NULL_TREE
18648 || DECL_RESULT (fndecl) == NULL_TREE
18649 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18650 return NULL;
18652 for (cursor = DECL_ARGUMENTS (fndecl);
18653 cursor != NULL_TREE;
18654 cursor = TREE_CHAIN (cursor))
18655 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18656 return NULL;
18658 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18659 if (TREE_CODE (tree_body) != RETURN_EXPR)
18660 return NULL;
18661 tree_body = TREE_OPERAND (tree_body, 0);
18662 if (TREE_CODE (tree_body) != MODIFY_EXPR
18663 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18664 return NULL;
18665 tree_body = TREE_OPERAND (tree_body, 1);
18667 /* Try to translate the body expression itself. Note that this will probably
18668 cause an infinite recursion if its call graph has a cycle. This is very
18669 unlikely for size functions, however, so don't bother with such things at
18670 the moment. */
18671 dpi.fndecl = fndecl;
18672 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18673 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18674 if (!loc_body)
18675 return NULL;
18677 /* After evaluating all operands in "loc_body", we should still have on the
18678 stack all arguments plus the desired function result (top of the stack).
18679 Generate code in order to keep only the result in our stack frame. */
18680 epilogue = NULL;
18681 for (i = 0; i < dpi.args_count; ++i)
18683 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18684 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18685 op_couple->dw_loc_next->dw_loc_next = epilogue;
18686 epilogue = op_couple;
18688 add_loc_descr (&loc_body, epilogue);
18689 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18690 return NULL;
18692 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18693 because they are considered useful. Now there is an epilogue, they are
18694 not anymore, so give it another try. */
18695 loc_descr_without_nops (loc_body);
18697 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18698 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18699 though, given that size functions do not come from source, so they should
18700 not have a dedicated DW_TAG_subprogram DIE. */
18701 dwarf_proc_die
18702 = new_dwarf_proc_die (loc_body, fndecl,
18703 get_context_die (DECL_CONTEXT (fndecl)));
18705 /* The called DWARF procedure consumes one stack slot per argument and
18706 returns one stack slot. */
18707 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18709 return dwarf_proc_die;
18712 /* Helper function for loc_list_from_tree. Perform OP binary op,
18713 but after converting arguments to type_die, afterwards convert
18714 back to unsigned. */
18716 static dw_loc_list_ref
18717 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18718 dw_die_ref type_die, scalar_int_mode mode,
18719 struct loc_descr_context *context)
18721 dw_loc_list_ref op0, op1;
18722 dw_loc_descr_ref cvt, binop;
18724 if (type_die == NULL)
18725 return NULL;
18727 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18728 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18729 if (op0 == NULL || op1 == NULL)
18730 return NULL;
18732 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18733 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18734 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18735 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18736 add_loc_descr_to_each (op0, cvt);
18738 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18739 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18740 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18741 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18742 add_loc_descr_to_each (op1, cvt);
18744 add_loc_list (&op0, op1);
18745 if (op0 == NULL)
18746 return NULL;
18748 binop = new_loc_descr (op, 0, 0);
18749 convert_descriptor_to_mode (mode, binop);
18750 add_loc_descr_to_each (op0, binop);
18752 return op0;
18755 /* Generate Dwarf location list representing LOC.
18756 If WANT_ADDRESS is false, expression computing LOC will be computed
18757 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18758 if WANT_ADDRESS is 2, expression computing address useable in location
18759 will be returned (i.e. DW_OP_reg can be used
18760 to refer to register values).
18762 CONTEXT provides information to customize the location descriptions
18763 generation. Its context_type field specifies what type is implicitly
18764 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18765 will not be generated.
18767 Its DPI field determines whether we are generating a DWARF expression for a
18768 DWARF procedure, so PARM_DECL references are processed specifically.
18770 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18771 and dpi fields were null. */
18773 static dw_loc_list_ref
18774 loc_list_from_tree_1 (tree loc, int want_address,
18775 struct loc_descr_context *context)
18777 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18778 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18779 int have_address = 0;
18780 enum dwarf_location_atom op;
18782 /* ??? Most of the time we do not take proper care for sign/zero
18783 extending the values properly. Hopefully this won't be a real
18784 problem... */
18786 if (context != NULL
18787 && context->base_decl == loc
18788 && want_address == 0)
18790 if (dwarf_version >= 3 || !dwarf_strict)
18791 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18792 NULL, 0, NULL, 0, NULL);
18793 else
18794 return NULL;
18797 switch (TREE_CODE (loc))
18799 case ERROR_MARK:
18800 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18801 return 0;
18803 case PLACEHOLDER_EXPR:
18804 /* This case involves extracting fields from an object to determine the
18805 position of other fields. It is supposed to appear only as the first
18806 operand of COMPONENT_REF nodes and to reference precisely the type
18807 that the context allows or its enclosing type. */
18808 if (context != NULL
18809 && (TREE_TYPE (loc) == context->context_type
18810 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18811 && want_address >= 1)
18813 if (dwarf_version >= 3 || !dwarf_strict)
18815 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18816 have_address = 1;
18817 break;
18819 else
18820 return NULL;
18822 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18823 the single argument passed by consumer. */
18824 else if (context != NULL
18825 && context->placeholder_arg
18826 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18827 && want_address == 0)
18829 ret = new_loc_descr (DW_OP_pick, 0, 0);
18830 ret->frame_offset_rel = 1;
18831 context->placeholder_seen = true;
18832 break;
18834 else
18835 expansion_failed (loc, NULL_RTX,
18836 "PLACEHOLDER_EXPR for an unexpected type");
18837 break;
18839 case CALL_EXPR:
18841 tree callee = get_callee_fndecl (loc);
18842 dw_die_ref dwarf_proc;
18844 if (callee
18845 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18846 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18848 /* DWARF procedures are used for size functions, which are built
18849 when size expressions contain conditional constructs, so we
18850 request strict preservation of signedness for comparisons. */
18851 bool old_strict_signedness;
18852 if (context)
18854 old_strict_signedness = context->strict_signedness;
18855 context->strict_signedness = true;
18858 /* Evaluate arguments right-to-left so that the first argument
18859 will be the top-most one on the stack. */
18860 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18862 tree arg = CALL_EXPR_ARG (loc, i);
18863 ret1 = loc_descriptor_from_tree (arg, 0, context);
18864 if (!ret1)
18866 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18867 return NULL;
18869 add_loc_descr (&ret, ret1);
18872 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18873 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18874 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18875 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18876 add_loc_descr (&ret, ret1);
18877 if (context)
18878 context->strict_signedness = old_strict_signedness;
18880 else
18881 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18882 break;
18885 case PREINCREMENT_EXPR:
18886 case PREDECREMENT_EXPR:
18887 case POSTINCREMENT_EXPR:
18888 case POSTDECREMENT_EXPR:
18889 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18890 /* There are no opcodes for these operations. */
18891 return 0;
18893 case ADDR_EXPR:
18894 /* If we already want an address, see if there is INDIRECT_REF inside
18895 e.g. for &this->field. */
18896 if (want_address)
18898 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18899 (loc, want_address == 2, context);
18900 if (list_ret)
18901 have_address = 1;
18902 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18903 && (ret = cst_pool_loc_descr (loc)))
18904 have_address = 1;
18906 /* Otherwise, process the argument and look for the address. */
18907 if (!list_ret && !ret)
18908 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18909 else
18911 if (want_address)
18912 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18913 return NULL;
18915 break;
18917 case VAR_DECL:
18918 if (DECL_THREAD_LOCAL_P (loc))
18920 rtx rtl;
18921 enum dwarf_location_atom tls_op;
18922 enum dtprel_bool dtprel = dtprel_false;
18924 if (targetm.have_tls)
18926 /* If this is not defined, we have no way to emit the
18927 data. */
18928 if (!targetm.asm_out.output_dwarf_dtprel)
18929 return 0;
18931 /* The way DW_OP_GNU_push_tls_address is specified, we
18932 can only look up addresses of objects in the current
18933 module. We used DW_OP_addr as first op, but that's
18934 wrong, because DW_OP_addr is relocated by the debug
18935 info consumer, while DW_OP_GNU_push_tls_address
18936 operand shouldn't be. */
18937 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18938 return 0;
18939 dtprel = dtprel_true;
18940 /* We check for DWARF 5 here because gdb did not implement
18941 DW_OP_form_tls_address until after 7.12. */
18942 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18943 : DW_OP_GNU_push_tls_address);
18945 else
18947 if (!targetm.emutls.debug_form_tls_address
18948 || !(dwarf_version >= 3 || !dwarf_strict))
18949 return 0;
18950 /* We stuffed the control variable into the DECL_VALUE_EXPR
18951 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18952 no longer appear in gimple code. We used the control
18953 variable in specific so that we could pick it up here. */
18954 loc = DECL_VALUE_EXPR (loc);
18955 tls_op = DW_OP_form_tls_address;
18958 rtl = rtl_for_decl_location (loc);
18959 if (rtl == NULL_RTX)
18960 return 0;
18962 if (!MEM_P (rtl))
18963 return 0;
18964 rtl = XEXP (rtl, 0);
18965 if (! CONSTANT_P (rtl))
18966 return 0;
18968 ret = new_addr_loc_descr (rtl, dtprel);
18969 ret1 = new_loc_descr (tls_op, 0, 0);
18970 add_loc_descr (&ret, ret1);
18972 have_address = 1;
18973 break;
18975 /* FALLTHRU */
18977 case PARM_DECL:
18978 if (context != NULL && context->dpi != NULL
18979 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18981 /* We are generating code for a DWARF procedure and we want to access
18982 one of its arguments: find the appropriate argument offset and let
18983 the resolve_args_picking pass compute the offset that complies
18984 with the stack frame size. */
18985 unsigned i = 0;
18986 tree cursor;
18988 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18989 cursor != NULL_TREE && cursor != loc;
18990 cursor = TREE_CHAIN (cursor), ++i)
18992 /* If we are translating a DWARF procedure, all referenced parameters
18993 must belong to the current function. */
18994 gcc_assert (cursor != NULL_TREE);
18996 ret = new_loc_descr (DW_OP_pick, i, 0);
18997 ret->frame_offset_rel = 1;
18998 break;
19000 /* FALLTHRU */
19002 case RESULT_DECL:
19003 if (DECL_HAS_VALUE_EXPR_P (loc))
19005 tree value_expr = DECL_VALUE_EXPR (loc);
19007 /* Non-local frame structures are DECL_IGNORED_P variables so we need
19008 to wait until they get an RTX in order to reference them. */
19009 if (early_dwarf
19010 && TREE_CODE (value_expr) == COMPONENT_REF
19011 && VAR_P (TREE_OPERAND (value_expr, 0))
19012 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
19014 else
19015 return loc_list_from_tree_1 (value_expr, want_address, context);
19018 /* FALLTHRU */
19020 case FUNCTION_DECL:
19022 rtx rtl;
19023 var_loc_list *loc_list = lookup_decl_loc (loc);
19025 if (loc_list && loc_list->first)
19027 list_ret = dw_loc_list (loc_list, loc, want_address);
19028 have_address = want_address != 0;
19029 break;
19031 rtl = rtl_for_decl_location (loc);
19032 if (rtl == NULL_RTX)
19034 if (TREE_CODE (loc) != FUNCTION_DECL
19035 && early_dwarf
19036 && want_address != 1
19037 && ! DECL_IGNORED_P (loc)
19038 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
19039 || POINTER_TYPE_P (TREE_TYPE (loc)))
19040 && TYPE_MODE (TREE_TYPE (loc)) != BLKmode
19041 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
19042 <= DWARF2_ADDR_SIZE))
19044 dw_die_ref ref = lookup_decl_die (loc);
19045 if (ref)
19047 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
19048 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19049 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
19050 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
19052 else if (current_function_decl
19053 && DECL_CONTEXT (loc) == current_function_decl)
19055 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
19056 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19057 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
19059 break;
19061 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
19062 return 0;
19064 else if (CONST_INT_P (rtl))
19066 HOST_WIDE_INT val = INTVAL (rtl);
19067 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19068 val &= GET_MODE_MASK (DECL_MODE (loc));
19069 ret = int_loc_descriptor (val);
19071 else if (GET_CODE (rtl) == CONST_STRING)
19073 expansion_failed (loc, NULL_RTX, "CONST_STRING");
19074 return 0;
19076 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
19077 ret = new_addr_loc_descr (rtl, dtprel_false);
19078 else
19080 machine_mode mode, mem_mode;
19082 /* Certain constructs can only be represented at top-level. */
19083 if (want_address == 2)
19085 ret = loc_descriptor (rtl, VOIDmode,
19086 VAR_INIT_STATUS_INITIALIZED);
19087 have_address = 1;
19089 else
19091 mode = GET_MODE (rtl);
19092 mem_mode = VOIDmode;
19093 if (MEM_P (rtl))
19095 mem_mode = mode;
19096 mode = get_address_mode (rtl);
19097 rtl = XEXP (rtl, 0);
19098 have_address = 1;
19100 ret = mem_loc_descriptor (rtl, mode, mem_mode,
19101 VAR_INIT_STATUS_INITIALIZED);
19103 if (!ret)
19104 expansion_failed (loc, rtl,
19105 "failed to produce loc descriptor for rtl");
19108 break;
19110 case MEM_REF:
19111 if (!integer_zerop (TREE_OPERAND (loc, 1)))
19113 have_address = 1;
19114 goto do_plus;
19116 /* Fallthru. */
19117 case INDIRECT_REF:
19118 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19119 have_address = 1;
19120 break;
19122 case TARGET_MEM_REF:
19123 case SSA_NAME:
19124 case DEBUG_EXPR_DECL:
19125 return NULL;
19127 case COMPOUND_EXPR:
19128 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
19129 context);
19131 CASE_CONVERT:
19132 case VIEW_CONVERT_EXPR:
19133 case SAVE_EXPR:
19134 case MODIFY_EXPR:
19135 case NON_LVALUE_EXPR:
19136 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
19137 context);
19139 case COMPONENT_REF:
19140 case BIT_FIELD_REF:
19141 case ARRAY_REF:
19142 case ARRAY_RANGE_REF:
19143 case REALPART_EXPR:
19144 case IMAGPART_EXPR:
19146 tree obj, offset;
19147 poly_int64 bitsize, bitpos, bytepos;
19148 machine_mode mode;
19149 int unsignedp, reversep, volatilep = 0;
19151 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
19152 &unsignedp, &reversep, &volatilep);
19154 gcc_assert (obj != loc);
19156 list_ret = loc_list_from_tree_1 (obj,
19157 want_address == 2
19158 && known_eq (bitpos, 0)
19159 && !offset ? 2 : 1,
19160 context);
19161 /* TODO: We can extract value of the small expression via shifting even
19162 for nonzero bitpos. */
19163 if (list_ret == 0)
19164 return 0;
19165 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
19166 || !multiple_p (bitsize, BITS_PER_UNIT))
19168 expansion_failed (loc, NULL_RTX,
19169 "bitfield access");
19170 return 0;
19173 if (offset != NULL_TREE)
19175 /* Variable offset. */
19176 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
19177 if (list_ret1 == 0)
19178 return 0;
19179 add_loc_list (&list_ret, list_ret1);
19180 if (!list_ret)
19181 return 0;
19182 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
19185 HOST_WIDE_INT value;
19186 if (bytepos.is_constant (&value) && value > 0)
19187 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
19188 value, 0));
19189 else if (maybe_ne (bytepos, 0))
19190 loc_list_plus_const (list_ret, bytepos);
19192 have_address = 1;
19193 break;
19196 case INTEGER_CST:
19197 if ((want_address || !tree_fits_shwi_p (loc))
19198 && (ret = cst_pool_loc_descr (loc)))
19199 have_address = 1;
19200 else if (want_address == 2
19201 && tree_fits_shwi_p (loc)
19202 && (ret = address_of_int_loc_descriptor
19203 (int_size_in_bytes (TREE_TYPE (loc)),
19204 tree_to_shwi (loc))))
19205 have_address = 1;
19206 else if (tree_fits_shwi_p (loc))
19207 ret = int_loc_descriptor (tree_to_shwi (loc));
19208 else if (tree_fits_uhwi_p (loc))
19209 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19210 else
19212 expansion_failed (loc, NULL_RTX,
19213 "Integer operand is not host integer");
19214 return 0;
19216 break;
19218 case POLY_INT_CST:
19220 if (want_address)
19222 expansion_failed (loc, NULL_RTX,
19223 "constant address with a runtime component");
19224 return 0;
19226 poly_int64 value;
19227 if (!poly_int_tree_p (loc, &value))
19229 expansion_failed (loc, NULL_RTX, "constant too big");
19230 return 0;
19232 ret = int_loc_descriptor (value);
19234 break;
19236 case CONSTRUCTOR:
19237 case REAL_CST:
19238 case STRING_CST:
19239 case COMPLEX_CST:
19240 if ((ret = cst_pool_loc_descr (loc)))
19241 have_address = 1;
19242 else if (TREE_CODE (loc) == CONSTRUCTOR)
19244 tree type = TREE_TYPE (loc);
19245 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19246 unsigned HOST_WIDE_INT offset = 0;
19247 unsigned HOST_WIDE_INT cnt;
19248 constructor_elt *ce;
19250 if (TREE_CODE (type) == RECORD_TYPE)
19252 /* This is very limited, but it's enough to output
19253 pointers to member functions, as long as the
19254 referenced function is defined in the current
19255 translation unit. */
19256 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19258 tree val = ce->value;
19260 tree field = ce->index;
19262 if (val)
19263 STRIP_NOPS (val);
19265 if (!field || DECL_BIT_FIELD (field))
19267 expansion_failed (loc, NULL_RTX,
19268 "bitfield in record type constructor");
19269 size = offset = (unsigned HOST_WIDE_INT)-1;
19270 ret = NULL;
19271 break;
19274 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19275 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19276 gcc_assert (pos + fieldsize <= size);
19277 if (pos < offset)
19279 expansion_failed (loc, NULL_RTX,
19280 "out-of-order fields in record constructor");
19281 size = offset = (unsigned HOST_WIDE_INT)-1;
19282 ret = NULL;
19283 break;
19285 if (pos > offset)
19287 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19288 add_loc_descr (&ret, ret1);
19289 offset = pos;
19291 if (val && fieldsize != 0)
19293 ret1 = loc_descriptor_from_tree (val, want_address, context);
19294 if (!ret1)
19296 expansion_failed (loc, NULL_RTX,
19297 "unsupported expression in field");
19298 size = offset = (unsigned HOST_WIDE_INT)-1;
19299 ret = NULL;
19300 break;
19302 add_loc_descr (&ret, ret1);
19304 if (fieldsize)
19306 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19307 add_loc_descr (&ret, ret1);
19308 offset = pos + fieldsize;
19312 if (offset != size)
19314 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19315 add_loc_descr (&ret, ret1);
19316 offset = size;
19319 have_address = !!want_address;
19321 else
19322 expansion_failed (loc, NULL_RTX,
19323 "constructor of non-record type");
19325 else
19326 /* We can construct small constants here using int_loc_descriptor. */
19327 expansion_failed (loc, NULL_RTX,
19328 "constructor or constant not in constant pool");
19329 break;
19331 case TRUTH_AND_EXPR:
19332 case TRUTH_ANDIF_EXPR:
19333 case BIT_AND_EXPR:
19334 op = DW_OP_and;
19335 goto do_binop;
19337 case TRUTH_XOR_EXPR:
19338 case BIT_XOR_EXPR:
19339 op = DW_OP_xor;
19340 goto do_binop;
19342 case TRUTH_OR_EXPR:
19343 case TRUTH_ORIF_EXPR:
19344 case BIT_IOR_EXPR:
19345 op = DW_OP_or;
19346 goto do_binop;
19348 case EXACT_DIV_EXPR:
19349 case FLOOR_DIV_EXPR:
19350 case TRUNC_DIV_EXPR:
19351 /* Turn a divide by a power of 2 into a shift when possible. */
19352 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19353 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19355 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19356 if (log2 > 0)
19358 list_ret
19359 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19360 if (list_ret == 0)
19361 return 0;
19363 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19364 add_loc_descr_to_each (list_ret,
19365 new_loc_descr (DW_OP_shr, 0, 0));
19366 break;
19370 /* fall through */
19372 case CEIL_DIV_EXPR:
19373 case ROUND_DIV_EXPR:
19374 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19376 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19377 scalar_int_mode int_mode;
19379 if ((dwarf_strict && dwarf_version < 5)
19380 || !is_a <scalar_int_mode> (mode, &int_mode))
19381 return 0;
19383 /* We can use a signed divide if the sign bit is not set. */
19384 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19386 op = DW_OP_div;
19387 goto do_binop;
19390 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19391 base_type_for_mode (int_mode, 1),
19392 int_mode, context);
19393 break;
19395 op = DW_OP_div;
19396 goto do_binop;
19398 case MINUS_EXPR:
19399 op = DW_OP_minus;
19400 goto do_binop;
19402 case FLOOR_MOD_EXPR:
19403 case CEIL_MOD_EXPR:
19404 case ROUND_MOD_EXPR:
19405 case TRUNC_MOD_EXPR:
19406 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19408 op = DW_OP_mod;
19409 goto do_binop;
19411 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19412 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19413 if (list_ret == 0 || list_ret1 == 0)
19414 return 0;
19416 add_loc_list (&list_ret, list_ret1);
19417 if (list_ret == 0)
19418 return 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_over, 0, 0));
19421 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19422 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19423 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19424 break;
19426 case MULT_EXPR:
19427 op = DW_OP_mul;
19428 goto do_binop;
19430 case LSHIFT_EXPR:
19431 op = DW_OP_shl;
19432 goto do_binop;
19434 case RSHIFT_EXPR:
19435 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19436 goto do_binop;
19438 case POINTER_PLUS_EXPR:
19439 case PLUS_EXPR:
19440 do_plus:
19441 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19443 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19444 smarter to encode their opposite. The DW_OP_plus_uconst operation
19445 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19446 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19447 bytes, Y being the size of the operation that pushes the opposite
19448 of the addend. So let's choose the smallest representation. */
19449 const tree tree_addend = TREE_OPERAND (loc, 1);
19450 offset_int wi_addend;
19451 HOST_WIDE_INT shwi_addend;
19452 dw_loc_descr_ref loc_naddend;
19454 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19455 if (list_ret == 0)
19456 return 0;
19458 /* Try to get the literal to push. It is the opposite of the addend,
19459 so as we rely on wrapping during DWARF evaluation, first decode
19460 the literal as a "DWARF-sized" signed number. */
19461 wi_addend = wi::to_offset (tree_addend);
19462 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19463 shwi_addend = wi_addend.to_shwi ();
19464 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19465 ? int_loc_descriptor (-shwi_addend)
19466 : NULL;
19468 if (loc_naddend != NULL
19469 && ((unsigned) size_of_uleb128 (shwi_addend)
19470 > size_of_loc_descr (loc_naddend)))
19472 add_loc_descr_to_each (list_ret, loc_naddend);
19473 add_loc_descr_to_each (list_ret,
19474 new_loc_descr (DW_OP_minus, 0, 0));
19476 else
19478 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19480 loc_naddend = loc_cur;
19481 loc_cur = loc_cur->dw_loc_next;
19482 ggc_free (loc_naddend);
19484 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19486 break;
19489 op = DW_OP_plus;
19490 goto do_binop;
19492 case LE_EXPR:
19493 op = DW_OP_le;
19494 goto do_comp_binop;
19496 case GE_EXPR:
19497 op = DW_OP_ge;
19498 goto do_comp_binop;
19500 case LT_EXPR:
19501 op = DW_OP_lt;
19502 goto do_comp_binop;
19504 case GT_EXPR:
19505 op = DW_OP_gt;
19506 goto do_comp_binop;
19508 do_comp_binop:
19509 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19511 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19512 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19513 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19514 TREE_CODE (loc));
19515 break;
19517 else
19518 goto do_binop;
19520 case EQ_EXPR:
19521 op = DW_OP_eq;
19522 goto do_binop;
19524 case NE_EXPR:
19525 op = DW_OP_ne;
19526 goto do_binop;
19528 do_binop:
19529 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19530 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19531 if (list_ret == 0 || list_ret1 == 0)
19532 return 0;
19534 add_loc_list (&list_ret, list_ret1);
19535 if (list_ret == 0)
19536 return 0;
19537 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19538 break;
19540 case TRUTH_NOT_EXPR:
19541 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19542 if (list_ret == 0)
19543 return 0;
19545 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_lit0, 0, 0));
19546 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_eq, 0, 0));
19547 break;
19549 case BIT_NOT_EXPR:
19550 op = DW_OP_not;
19551 goto do_unop;
19553 case ABS_EXPR:
19554 op = DW_OP_abs;
19555 goto do_unop;
19557 case NEGATE_EXPR:
19558 op = DW_OP_neg;
19559 goto do_unop;
19561 do_unop:
19562 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19563 if (list_ret == 0)
19564 return 0;
19566 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19567 break;
19569 case MIN_EXPR:
19570 case MAX_EXPR:
19572 const enum tree_code code =
19573 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19575 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19576 build2 (code, integer_type_node,
19577 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19578 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19581 /* fall through */
19583 case COND_EXPR:
19585 dw_loc_descr_ref lhs
19586 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19587 dw_loc_list_ref rhs
19588 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19589 dw_loc_descr_ref bra_node, jump_node, tmp;
19591 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19592 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19593 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19594 list_ret
19595 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19596 0, context);
19597 /* Likewise, swap the operands for a logically negated condition. */
19598 else if (TREE_CODE (TREE_OPERAND (loc, 0)) == TRUTH_NOT_EXPR)
19600 lhs = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0, context);
19601 rhs = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19602 list_ret
19603 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19604 0, context);
19606 else
19607 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19608 if (list_ret == 0 || lhs == 0 || rhs == 0)
19609 return 0;
19611 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19612 add_loc_descr_to_each (list_ret, bra_node);
19614 add_loc_list (&list_ret, rhs);
19615 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19616 add_loc_descr_to_each (list_ret, jump_node);
19618 add_loc_descr_to_each (list_ret, lhs);
19619 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19620 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19622 /* ??? Need a node to point the skip at. Use a nop. */
19623 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19624 add_loc_descr_to_each (list_ret, tmp);
19625 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19626 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19628 break;
19630 case FIX_TRUNC_EXPR:
19631 return 0;
19633 case COMPOUND_LITERAL_EXPR:
19634 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19635 0, context);
19637 default:
19638 /* Leave front-end specific codes as simply unknown. This comes
19639 up, for instance, with the C STMT_EXPR. */
19640 if ((unsigned int) TREE_CODE (loc)
19641 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19643 expansion_failed (loc, NULL_RTX,
19644 "language specific tree node");
19645 return 0;
19648 /* Otherwise this is a generic code; we should just lists all of
19649 these explicitly. We forgot one. */
19650 if (flag_checking)
19651 gcc_unreachable ();
19653 /* In a release build, we want to degrade gracefully: better to
19654 generate incomplete debugging information than to crash. */
19655 return NULL;
19658 if (!ret && !list_ret)
19659 return 0;
19661 if (want_address == 2 && !have_address
19662 && (dwarf_version >= 4 || !dwarf_strict))
19664 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19666 expansion_failed (loc, NULL_RTX,
19667 "DWARF address size mismatch");
19668 return 0;
19670 if (ret)
19671 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19672 else
19673 add_loc_descr_to_each (list_ret,
19674 new_loc_descr (DW_OP_stack_value, 0, 0));
19675 have_address = 1;
19677 /* Show if we can't fill the request for an address. */
19678 if (want_address && !have_address)
19680 expansion_failed (loc, NULL_RTX,
19681 "Want address and only have value");
19682 return 0;
19685 gcc_assert (!ret || !list_ret);
19687 /* If we've got an address and don't want one, dereference. */
19688 if (!want_address && have_address)
19690 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19691 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19692 scalar_int_mode int_mode;
19693 dw_die_ref type_die;
19694 dw_loc_descr_ref deref;
19696 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19697 if (size > DWARF2_ADDR_SIZE || size == -1)
19699 expansion_failed (loc, NULL_RTX,
19700 "DWARF address size mismatch");
19701 return 0;
19704 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19705 else if (size == DWARF2_ADDR_SIZE)
19706 deref = new_loc_descr (DW_OP_deref, size, 0);
19708 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19709 extend the value, which is really OK for unsigned types only. */
19710 else if (!(context && context->strict_signedness)
19711 || TYPE_UNSIGNED (TREE_TYPE (loc))
19712 || (dwarf_strict && dwarf_version < 5)
19713 || !is_a <scalar_int_mode> (mode, &int_mode)
19714 || !(type_die = base_type_for_mode (mode, false)))
19715 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19717 /* Use DW_OP_deref_type for signed integral types if possible, but
19718 convert back to the generic type to avoid type mismatches later. */
19719 else
19721 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19722 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19723 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19724 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19725 add_loc_descr (&deref,
19726 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19729 if (ret)
19730 add_loc_descr (&ret, deref);
19731 else
19732 add_loc_descr_to_each (list_ret, deref);
19735 if (ret)
19736 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19738 return list_ret;
19741 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19742 expressions. */
19744 static dw_loc_list_ref
19745 loc_list_from_tree (tree loc, int want_address,
19746 struct loc_descr_context *context)
19748 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19750 for (dw_loc_list_ref loc_cur = result;
19751 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19752 loc_descr_without_nops (loc_cur->expr);
19753 return result;
19756 /* Same as above but return only single location expression. */
19757 static dw_loc_descr_ref
19758 loc_descriptor_from_tree (tree loc, int want_address,
19759 struct loc_descr_context *context)
19761 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19762 if (!ret)
19763 return NULL;
19764 if (ret->dw_loc_next)
19766 expansion_failed (loc, NULL_RTX,
19767 "Location list where only loc descriptor needed");
19768 return NULL;
19770 return ret->expr;
19773 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19774 pointer to the declared type for the relevant field variable, or return
19775 `integer_type_node' if the given node turns out to be an
19776 ERROR_MARK node. */
19778 static inline tree
19779 field_type (const_tree decl)
19781 tree type;
19783 if (TREE_CODE (decl) == ERROR_MARK)
19784 return integer_type_node;
19786 type = DECL_BIT_FIELD_TYPE (decl);
19787 if (type == NULL_TREE)
19788 type = TREE_TYPE (decl);
19790 return type;
19793 /* Given a pointer to a tree node, return the alignment in bits for
19794 it, or else return BITS_PER_WORD if the node actually turns out to
19795 be an ERROR_MARK node. */
19797 static inline unsigned
19798 simple_type_align_in_bits (const_tree type)
19800 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19803 static inline unsigned
19804 simple_decl_align_in_bits (const_tree decl)
19806 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19809 /* Return the result of rounding T up to ALIGN. */
19811 static inline offset_int
19812 round_up_to_align (const offset_int &t, unsigned int align)
19814 return wi::udiv_trunc (t + align - 1, align) * align;
19817 /* Helper structure for RECORD_TYPE processing. */
19818 struct vlr_context
19820 /* Root RECORD_TYPE. It is needed to generate data member location
19821 descriptions in variable-length records (VLR), but also to cope with
19822 variants, which are composed of nested structures multiplexed with
19823 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19824 function processing a FIELD_DECL, it is required to be non null. */
19825 tree struct_type;
19827 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19828 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19829 this variant part as part of the root record (in storage units). For
19830 regular records, it must be NULL_TREE. */
19831 tree variant_part_offset;
19834 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19835 addressed byte of the "containing object" for the given FIELD_DECL. If
19836 possible, return a native constant through CST_OFFSET (in which case NULL is
19837 returned); otherwise return a DWARF expression that computes the offset.
19839 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19840 that offset is, either because the argument turns out to be a pointer to an
19841 ERROR_MARK node, or because the offset expression is too complex for us.
19843 CTX is required: see the comment for VLR_CONTEXT. */
19845 static dw_loc_descr_ref
19846 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19847 HOST_WIDE_INT *cst_offset)
19849 tree tree_result;
19850 dw_loc_list_ref loc_result;
19852 *cst_offset = 0;
19854 if (TREE_CODE (decl) == ERROR_MARK)
19855 return NULL;
19856 else
19857 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19859 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19860 case. */
19861 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19862 return NULL;
19864 /* We used to handle only constant offsets in all cases. Now, we handle
19865 properly dynamic byte offsets only when PCC bitfield type doesn't
19866 matter. */
19867 if (PCC_BITFIELD_TYPE_MATTERS
19868 && DECL_BIT_FIELD_TYPE (decl)
19869 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19871 offset_int object_offset_in_bits;
19872 offset_int object_offset_in_bytes;
19873 offset_int bitpos_int;
19874 tree type;
19875 tree field_size_tree;
19876 offset_int deepest_bitpos;
19877 offset_int field_size_in_bits;
19878 unsigned int type_align_in_bits;
19879 unsigned int decl_align_in_bits;
19880 offset_int type_size_in_bits;
19882 bitpos_int = wi::to_offset (bit_position (decl));
19883 type = field_type (decl);
19884 type_size_in_bits = offset_int_type_size_in_bits (type);
19885 type_align_in_bits = simple_type_align_in_bits (type);
19887 field_size_tree = DECL_SIZE (decl);
19889 /* The size could be unspecified if there was an error, or for
19890 a flexible array member. */
19891 if (!field_size_tree)
19892 field_size_tree = bitsize_zero_node;
19894 /* If the size of the field is not constant, use the type size. */
19895 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19896 field_size_in_bits = wi::to_offset (field_size_tree);
19897 else
19898 field_size_in_bits = type_size_in_bits;
19900 decl_align_in_bits = simple_decl_align_in_bits (decl);
19902 /* The GCC front-end doesn't make any attempt to keep track of the
19903 starting bit offset (relative to the start of the containing
19904 structure type) of the hypothetical "containing object" for a
19905 bit-field. Thus, when computing the byte offset value for the
19906 start of the "containing object" of a bit-field, we must deduce
19907 this information on our own. This can be rather tricky to do in
19908 some cases. For example, handling the following structure type
19909 definition when compiling for an i386/i486 target (which only
19910 aligns long long's to 32-bit boundaries) can be very tricky:
19912 struct S { int field1; long long field2:31; };
19914 Fortunately, there is a simple rule-of-thumb which can be used
19915 in such cases. When compiling for an i386/i486, GCC will
19916 allocate 8 bytes for the structure shown above. It decides to
19917 do this based upon one simple rule for bit-field allocation.
19918 GCC allocates each "containing object" for each bit-field at
19919 the first (i.e. lowest addressed) legitimate alignment boundary
19920 (based upon the required minimum alignment for the declared
19921 type of the field) which it can possibly use, subject to the
19922 condition that there is still enough available space remaining
19923 in the containing object (when allocated at the selected point)
19924 to fully accommodate all of the bits of the bit-field itself.
19926 This simple rule makes it obvious why GCC allocates 8 bytes for
19927 each object of the structure type shown above. When looking
19928 for a place to allocate the "containing object" for `field2',
19929 the compiler simply tries to allocate a 64-bit "containing
19930 object" at each successive 32-bit boundary (starting at zero)
19931 until it finds a place to allocate that 64- bit field such that
19932 at least 31 contiguous (and previously unallocated) bits remain
19933 within that selected 64 bit field. (As it turns out, for the
19934 example above, the compiler finds it is OK to allocate the
19935 "containing object" 64-bit field at bit-offset zero within the
19936 structure type.)
19938 Here we attempt to work backwards from the limited set of facts
19939 we're given, and we try to deduce from those facts, where GCC
19940 must have believed that the containing object started (within
19941 the structure type). The value we deduce is then used (by the
19942 callers of this routine) to generate DW_AT_location and
19943 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19944 the case of DW_AT_location, regular fields as well). */
19946 /* Figure out the bit-distance from the start of the structure to
19947 the "deepest" bit of the bit-field. */
19948 deepest_bitpos = bitpos_int + field_size_in_bits;
19950 /* This is the tricky part. Use some fancy footwork to deduce
19951 where the lowest addressed bit of the containing object must
19952 be. */
19953 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19955 /* Round up to type_align by default. This works best for
19956 bitfields. */
19957 object_offset_in_bits
19958 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19960 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19962 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19964 /* Round up to decl_align instead. */
19965 object_offset_in_bits
19966 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19969 object_offset_in_bytes
19970 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19971 if (ctx->variant_part_offset == NULL_TREE)
19973 *cst_offset = object_offset_in_bytes.to_shwi ();
19974 return NULL;
19976 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19978 else
19979 tree_result = byte_position (decl);
19981 if (ctx->variant_part_offset != NULL_TREE)
19982 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19983 ctx->variant_part_offset, tree_result);
19985 /* If the byte offset is a constant, it's simplier to handle a native
19986 constant rather than a DWARF expression. */
19987 if (TREE_CODE (tree_result) == INTEGER_CST)
19989 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19990 return NULL;
19993 struct loc_descr_context loc_ctx = {
19994 ctx->struct_type, /* context_type */
19995 NULL_TREE, /* base_decl */
19996 NULL, /* dpi */
19997 false, /* placeholder_arg */
19998 false, /* placeholder_seen */
19999 false /* strict_signedness */
20001 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
20003 /* We want a DWARF expression: abort if we only have a location list with
20004 multiple elements. */
20005 if (!loc_result || !single_element_loc_list_p (loc_result))
20006 return NULL;
20007 else
20008 return loc_result->expr;
20011 /* The following routines define various Dwarf attributes and any data
20012 associated with them. */
20014 /* Add a location description attribute value to a DIE.
20016 This emits location attributes suitable for whole variables and
20017 whole parameters. Note that the location attributes for struct fields are
20018 generated by the routine `data_member_location_attribute' below. */
20020 static inline void
20021 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
20022 dw_loc_list_ref descr)
20024 bool check_no_locviews = true;
20025 if (descr == 0)
20026 return;
20027 if (single_element_loc_list_p (descr))
20028 add_AT_loc (die, attr_kind, descr->expr);
20029 else
20031 add_AT_loc_list (die, attr_kind, descr);
20032 gcc_assert (descr->ll_symbol);
20033 if (attr_kind == DW_AT_location && descr->vl_symbol
20034 && dwarf2out_locviews_in_attribute ())
20036 add_AT_view_list (die, DW_AT_GNU_locviews);
20037 check_no_locviews = false;
20041 if (check_no_locviews)
20042 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
20045 /* Add DW_AT_accessibility attribute to DIE if needed. */
20047 static void
20048 add_accessibility_attribute (dw_die_ref die, tree decl)
20050 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20051 children, otherwise the default is DW_ACCESS_public. In DWARF2
20052 the default has always been DW_ACCESS_public. */
20053 if (TREE_PROTECTED (decl))
20054 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20055 else if (TREE_PRIVATE (decl))
20057 if (dwarf_version == 2
20058 || die->die_parent == NULL
20059 || die->die_parent->die_tag != DW_TAG_class_type)
20060 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20062 else if (dwarf_version > 2
20063 && die->die_parent
20064 && die->die_parent->die_tag == DW_TAG_class_type)
20065 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20068 /* Attach the specialized form of location attribute used for data members of
20069 struct and union types. In the special case of a FIELD_DECL node which
20070 represents a bit-field, the "offset" part of this special location
20071 descriptor must indicate the distance in bytes from the lowest-addressed
20072 byte of the containing struct or union type to the lowest-addressed byte of
20073 the "containing object" for the bit-field. (See the `field_byte_offset'
20074 function above).
20076 For any given bit-field, the "containing object" is a hypothetical object
20077 (of some integral or enum type) within which the given bit-field lives. The
20078 type of this hypothetical "containing object" is always the same as the
20079 declared type of the individual bit-field itself (for GCC anyway... the
20080 DWARF spec doesn't actually mandate this). Note that it is the size (in
20081 bytes) of the hypothetical "containing object" which will be given in the
20082 DW_AT_byte_size attribute for this bit-field. (See the
20083 `byte_size_attribute' function below.) It is also used when calculating the
20084 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
20085 function below.)
20087 CTX is required: see the comment for VLR_CONTEXT. */
20089 static void
20090 add_data_member_location_attribute (dw_die_ref die,
20091 tree decl,
20092 struct vlr_context *ctx)
20094 HOST_WIDE_INT offset;
20095 dw_loc_descr_ref loc_descr = 0;
20097 if (TREE_CODE (decl) == TREE_BINFO)
20099 /* We're working on the TAG_inheritance for a base class. */
20100 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
20102 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
20103 aren't at a fixed offset from all (sub)objects of the same
20104 type. We need to extract the appropriate offset from our
20105 vtable. The following dwarf expression means
20107 BaseAddr = ObAddr + *((*ObAddr) - Offset)
20109 This is specific to the V3 ABI, of course. */
20111 dw_loc_descr_ref tmp;
20113 /* Make a copy of the object address. */
20114 tmp = new_loc_descr (DW_OP_dup, 0, 0);
20115 add_loc_descr (&loc_descr, tmp);
20117 /* Extract the vtable address. */
20118 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20119 add_loc_descr (&loc_descr, tmp);
20121 /* Calculate the address of the offset. */
20122 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
20123 gcc_assert (offset < 0);
20125 tmp = int_loc_descriptor (-offset);
20126 add_loc_descr (&loc_descr, tmp);
20127 tmp = new_loc_descr (DW_OP_minus, 0, 0);
20128 add_loc_descr (&loc_descr, tmp);
20130 /* Extract the offset. */
20131 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20132 add_loc_descr (&loc_descr, tmp);
20134 /* Add it to the object address. */
20135 tmp = new_loc_descr (DW_OP_plus, 0, 0);
20136 add_loc_descr (&loc_descr, tmp);
20138 else
20139 offset = tree_to_shwi (BINFO_OFFSET (decl));
20141 else
20143 loc_descr = field_byte_offset (decl, ctx, &offset);
20145 if (!loc_descr)
20148 /* If loc_descr is available, then we know the offset is dynamic. */
20149 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
20151 loc_descr = NULL;
20152 offset = 0;
20155 /* Data member location evaluation starts with the base address on the
20156 stack. Compute the field offset and add it to this base address. */
20157 else
20158 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
20161 if (!loc_descr)
20163 /* While DW_AT_data_bit_offset has been added already in DWARF4,
20164 e.g. GDB only added support to it in November 2016. For DWARF5
20165 we need newer debug info consumers anyway. We might change this
20166 to dwarf_version >= 4 once most consumers catched up. */
20167 if (dwarf_version >= 5
20168 && TREE_CODE (decl) == FIELD_DECL
20169 && DECL_BIT_FIELD_TYPE (decl)
20170 && (ctx->variant_part_offset == NULL_TREE
20171 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
20173 tree off = bit_position (decl);
20174 if (ctx->variant_part_offset)
20175 off = bit_from_pos (ctx->variant_part_offset, off);
20176 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
20178 remove_AT (die, DW_AT_byte_size);
20179 remove_AT (die, DW_AT_bit_offset);
20180 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
20181 return;
20184 if (dwarf_version > 2)
20186 /* Don't need to output a location expression, just the constant. */
20187 if (offset < 0)
20188 add_AT_int (die, DW_AT_data_member_location, offset);
20189 else
20190 add_AT_unsigned (die, DW_AT_data_member_location, offset);
20191 return;
20193 else
20195 enum dwarf_location_atom op;
20197 /* The DWARF2 standard says that we should assume that the structure
20198 address is already on the stack, so we can specify a structure
20199 field address by using DW_OP_plus_uconst. */
20200 op = DW_OP_plus_uconst;
20201 loc_descr = new_loc_descr (op, offset, 0);
20205 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
20208 /* Writes integer values to dw_vec_const array. */
20210 static void
20211 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
20213 while (size != 0)
20215 *dest++ = val & 0xff;
20216 val >>= 8;
20217 --size;
20221 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20223 static HOST_WIDE_INT
20224 extract_int (const unsigned char *src, unsigned int size)
20226 HOST_WIDE_INT val = 0;
20228 src += size;
20229 while (size != 0)
20231 val <<= 8;
20232 val |= *--src & 0xff;
20233 --size;
20235 return val;
20238 /* Writes wide_int values to dw_vec_const array. */
20240 static void
20241 insert_wide_int (const wide_int_ref &val, unsigned char *dest, int elt_size)
20243 int i;
20245 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20247 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20248 return;
20251 /* We'd have to extend this code to support odd sizes. */
20252 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20254 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20256 if (WORDS_BIG_ENDIAN)
20257 for (i = n - 1; i >= 0; i--)
20259 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20260 dest += sizeof (HOST_WIDE_INT);
20262 else
20263 for (i = 0; i < n; i++)
20265 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20266 dest += sizeof (HOST_WIDE_INT);
20270 /* Writes floating point values to dw_vec_const array. */
20272 static unsigned
20273 insert_float (const_rtx rtl, unsigned char *array)
20275 long val[4];
20276 int i;
20277 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20279 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20281 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20282 if (GET_MODE_SIZE (mode) < 4)
20284 gcc_assert (GET_MODE_SIZE (mode) == 2);
20285 insert_int (val[0], 2, array);
20286 return 2;
20289 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20291 insert_int (val[i], 4, array);
20292 array += 4;
20294 return 4;
20297 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20298 does not have a "location" either in memory or in a register. These
20299 things can arise in GNU C when a constant is passed as an actual parameter
20300 to an inlined function. They can also arise in C++ where declared
20301 constants do not necessarily get memory "homes". */
20303 static bool
20304 add_const_value_attribute (dw_die_ref die, machine_mode mode, rtx rtl)
20306 scalar_mode int_mode;
20308 switch (GET_CODE (rtl))
20310 case CONST_INT:
20312 HOST_WIDE_INT val = INTVAL (rtl);
20314 if (val < 0)
20315 add_AT_int (die, DW_AT_const_value, val);
20316 else
20317 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20319 return true;
20321 case CONST_WIDE_INT:
20322 if (is_int_mode (mode, &int_mode)
20323 && (GET_MODE_PRECISION (int_mode)
20324 & (HOST_BITS_PER_WIDE_INT - 1)) == 0)
20326 add_AT_wide (die, DW_AT_const_value, rtx_mode_t (rtl, int_mode));
20327 return true;
20329 return false;
20331 case CONST_DOUBLE:
20332 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20333 floating-point constant. A CONST_DOUBLE is used whenever the
20334 constant requires more than one word in order to be adequately
20335 represented. */
20336 if (TARGET_SUPPORTS_WIDE_INT == 0
20337 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20338 add_AT_double (die, DW_AT_const_value,
20339 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20340 else
20342 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20343 unsigned int length = GET_MODE_SIZE (mode);
20344 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20345 unsigned int elt_size = insert_float (rtl, array);
20347 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20348 array);
20350 return true;
20352 case CONST_VECTOR:
20354 unsigned int length;
20355 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20356 return false;
20358 machine_mode mode = GET_MODE (rtl);
20359 /* The combination of a length and byte elt_size doesn't extend
20360 naturally to boolean vectors, where several elements are packed
20361 into the same byte. */
20362 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20363 return false;
20365 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20366 unsigned char *array
20367 = ggc_vec_alloc<unsigned char> (length * elt_size);
20368 unsigned int i;
20369 unsigned char *p;
20370 machine_mode imode = GET_MODE_INNER (mode);
20372 switch (GET_MODE_CLASS (mode))
20374 case MODE_VECTOR_INT:
20375 for (i = 0, p = array; i < length; i++, p += elt_size)
20377 rtx elt = CONST_VECTOR_ELT (rtl, i);
20378 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20380 break;
20382 case MODE_VECTOR_FLOAT:
20383 for (i = 0, p = array; i < length; i++, p += elt_size)
20385 rtx elt = CONST_VECTOR_ELT (rtl, i);
20386 insert_float (elt, p);
20388 break;
20390 default:
20391 gcc_unreachable ();
20394 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20396 return true;
20398 case CONST_STRING:
20399 if (dwarf_version >= 4 || !dwarf_strict)
20401 dw_loc_descr_ref loc_result;
20402 resolve_one_addr (&rtl);
20403 rtl_addr:
20404 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20405 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20406 add_AT_loc (die, DW_AT_location, loc_result);
20407 vec_safe_push (used_rtx_array, rtl);
20408 return true;
20410 return false;
20412 case CONST:
20413 if (CONSTANT_P (XEXP (rtl, 0)))
20414 return add_const_value_attribute (die, mode, XEXP (rtl, 0));
20415 /* FALLTHROUGH */
20416 case SYMBOL_REF:
20417 if (!const_ok_for_output (rtl))
20418 return false;
20419 /* FALLTHROUGH */
20420 case LABEL_REF:
20421 if (dwarf_version >= 4 || !dwarf_strict)
20422 goto rtl_addr;
20423 return false;
20425 case PLUS:
20426 /* In cases where an inlined instance of an inline function is passed
20427 the address of an `auto' variable (which is local to the caller) we
20428 can get a situation where the DECL_RTL of the artificial local
20429 variable (for the inlining) which acts as a stand-in for the
20430 corresponding formal parameter (of the inline function) will look
20431 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20432 exactly a compile-time constant expression, but it isn't the address
20433 of the (artificial) local variable either. Rather, it represents the
20434 *value* which the artificial local variable always has during its
20435 lifetime. We currently have no way to represent such quasi-constant
20436 values in Dwarf, so for now we just punt and generate nothing. */
20437 return false;
20439 case HIGH:
20440 case CONST_FIXED:
20441 case MINUS:
20442 case SIGN_EXTEND:
20443 case ZERO_EXTEND:
20444 case CONST_POLY_INT:
20445 return false;
20447 case MEM:
20448 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20449 && MEM_READONLY_P (rtl)
20450 && GET_MODE (rtl) == BLKmode)
20452 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20453 return true;
20455 return false;
20457 default:
20458 /* No other kinds of rtx should be possible here. */
20459 gcc_unreachable ();
20463 /* Determine whether the evaluation of EXPR references any variables
20464 or functions which aren't otherwise used (and therefore may not be
20465 output). */
20466 static tree
20467 reference_to_unused (tree * tp, int * walk_subtrees,
20468 void * data ATTRIBUTE_UNUSED)
20470 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20471 *walk_subtrees = 0;
20473 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20474 && ! TREE_ASM_WRITTEN (*tp))
20475 return *tp;
20476 /* ??? The C++ FE emits debug information for using decls, so
20477 putting gcc_unreachable here falls over. See PR31899. For now
20478 be conservative. */
20479 else if (!symtab->global_info_ready && VAR_P (*tp))
20480 return *tp;
20481 else if (VAR_P (*tp))
20483 varpool_node *node = varpool_node::get (*tp);
20484 if (!node || !node->definition)
20485 return *tp;
20487 else if (TREE_CODE (*tp) == FUNCTION_DECL
20488 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20490 /* The call graph machinery must have finished analyzing,
20491 optimizing and gimplifying the CU by now.
20492 So if *TP has no call graph node associated
20493 to it, it means *TP will not be emitted. */
20494 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20495 return *tp;
20497 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20498 return *tp;
20500 return NULL_TREE;
20503 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20504 for use in a later add_const_value_attribute call. */
20506 static rtx
20507 rtl_for_decl_init (tree init, tree type)
20509 rtx rtl = NULL_RTX;
20511 STRIP_NOPS (init);
20513 /* If a variable is initialized with a string constant without embedded
20514 zeros, build CONST_STRING. */
20515 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20517 tree enttype = TREE_TYPE (type);
20518 tree domain = TYPE_DOMAIN (type);
20519 scalar_int_mode mode;
20521 if (is_int_mode (TYPE_MODE (enttype), &mode)
20522 && GET_MODE_SIZE (mode) == 1
20523 && domain
20524 && TYPE_MAX_VALUE (domain)
20525 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20526 && integer_zerop (TYPE_MIN_VALUE (domain))
20527 && compare_tree_int (TYPE_MAX_VALUE (domain),
20528 TREE_STRING_LENGTH (init) - 1) == 0
20529 && ((size_t) TREE_STRING_LENGTH (init)
20530 == strlen (TREE_STRING_POINTER (init)) + 1))
20532 rtl = gen_rtx_CONST_STRING (VOIDmode,
20533 ggc_strdup (TREE_STRING_POINTER (init)));
20534 rtl = gen_rtx_MEM (BLKmode, rtl);
20535 MEM_READONLY_P (rtl) = 1;
20538 /* Other aggregates, and complex values, could be represented using
20539 CONCAT: FIXME!
20540 If this changes, please adjust tree_add_const_value_attribute
20541 so that for early_dwarf it will for such initializers mangle referenced
20542 decls. */
20543 else if (AGGREGATE_TYPE_P (type)
20544 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20545 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20546 || TREE_CODE (type) == COMPLEX_TYPE)
20548 /* Vectors only work if their mode is supported by the target.
20549 FIXME: generic vectors ought to work too. */
20550 else if (TREE_CODE (type) == VECTOR_TYPE
20551 && !VECTOR_MODE_P (TYPE_MODE (type)))
20553 /* If the initializer is something that we know will expand into an
20554 immediate RTL constant, expand it now. We must be careful not to
20555 reference variables which won't be output. */
20556 else if (initializer_constant_valid_p (init, type)
20557 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20559 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20560 possible. */
20561 if (TREE_CODE (type) == VECTOR_TYPE)
20562 switch (TREE_CODE (init))
20564 case VECTOR_CST:
20565 break;
20566 case CONSTRUCTOR:
20567 if (TREE_CONSTANT (init))
20569 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20570 bool constant_p = true;
20571 tree value;
20572 unsigned HOST_WIDE_INT ix;
20574 /* Even when ctor is constant, it might contain non-*_CST
20575 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20576 belong into VECTOR_CST nodes. */
20577 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20578 if (!CONSTANT_CLASS_P (value))
20580 constant_p = false;
20581 break;
20584 if (constant_p)
20586 init = build_vector_from_ctor (type, elts);
20587 break;
20590 /* FALLTHRU */
20592 default:
20593 return NULL;
20596 /* Large _BitInt BLKmode INTEGER_CSTs would yield a MEM. */
20597 if (TREE_CODE (init) == INTEGER_CST
20598 && TREE_CODE (TREE_TYPE (init)) == BITINT_TYPE
20599 && TYPE_MODE (TREE_TYPE (init)) == BLKmode)
20601 if (tree_fits_shwi_p (init))
20602 return GEN_INT (tree_to_shwi (init));
20603 else
20604 return NULL;
20607 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20609 /* If expand_expr returns a MEM, it wasn't immediate. */
20610 gcc_assert (!rtl || !MEM_P (rtl));
20613 return rtl;
20616 /* Generate RTL for the variable DECL to represent its location. */
20618 static rtx
20619 rtl_for_decl_location (tree decl)
20621 rtx rtl;
20623 /* Here we have to decide where we are going to say the parameter "lives"
20624 (as far as the debugger is concerned). We only have a couple of
20625 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20627 DECL_RTL normally indicates where the parameter lives during most of the
20628 activation of the function. If optimization is enabled however, this
20629 could be either NULL or else a pseudo-reg. Both of those cases indicate
20630 that the parameter doesn't really live anywhere (as far as the code
20631 generation parts of GCC are concerned) during most of the function's
20632 activation. That will happen (for example) if the parameter is never
20633 referenced within the function.
20635 We could just generate a location descriptor here for all non-NULL
20636 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20637 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20638 where DECL_RTL is NULL or is a pseudo-reg.
20640 Note however that we can only get away with using DECL_INCOMING_RTL as
20641 a backup substitute for DECL_RTL in certain limited cases. In cases
20642 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20643 we can be sure that the parameter was passed using the same type as it is
20644 declared to have within the function, and that its DECL_INCOMING_RTL
20645 points us to a place where a value of that type is passed.
20647 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20648 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20649 because in these cases DECL_INCOMING_RTL points us to a value of some
20650 type which is *different* from the type of the parameter itself. Thus,
20651 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20652 such cases, the debugger would end up (for example) trying to fetch a
20653 `float' from a place which actually contains the first part of a
20654 `double'. That would lead to really incorrect and confusing
20655 output at debug-time.
20657 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20658 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20659 are a couple of exceptions however. On little-endian machines we can
20660 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20661 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20662 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20663 when (on a little-endian machine) a non-prototyped function has a
20664 parameter declared to be of type `short' or `char'. In such cases,
20665 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20666 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20667 passed `int' value. If the debugger then uses that address to fetch
20668 a `short' or a `char' (on a little-endian machine) the result will be
20669 the correct data, so we allow for such exceptional cases below.
20671 Note that our goal here is to describe the place where the given formal
20672 parameter lives during most of the function's activation (i.e. between the
20673 end of the prologue and the start of the epilogue). We'll do that as best
20674 as we can. Note however that if the given formal parameter is modified
20675 sometime during the execution of the function, then a stack backtrace (at
20676 debug-time) will show the function as having been called with the *new*
20677 value rather than the value which was originally passed in. This happens
20678 rarely enough that it is not a major problem, but it *is* a problem, and
20679 I'd like to fix it.
20681 A future version of dwarf2out.cc may generate two additional attributes for
20682 any given DW_TAG_formal_parameter DIE which will describe the "passed
20683 type" and the "passed location" for the given formal parameter in addition
20684 to the attributes we now generate to indicate the "declared type" and the
20685 "active location" for each parameter. This additional set of attributes
20686 could be used by debuggers for stack backtraces. Separately, note that
20687 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20688 This happens (for example) for inlined-instances of inline function formal
20689 parameters which are never referenced. This really shouldn't be
20690 happening. All PARM_DECL nodes should get valid non-NULL
20691 DECL_INCOMING_RTL values. FIXME. */
20693 /* Use DECL_RTL as the "location" unless we find something better. */
20694 rtl = DECL_RTL_IF_SET (decl);
20696 /* When generating abstract instances, ignore everything except
20697 constants, symbols living in memory, and symbols living in
20698 fixed registers. */
20699 if (! reload_completed)
20701 if (rtl
20702 && (CONSTANT_P (rtl)
20703 || (MEM_P (rtl)
20704 && CONSTANT_P (XEXP (rtl, 0)))
20705 || (REG_P (rtl)
20706 && VAR_P (decl)
20707 && TREE_STATIC (decl))))
20709 rtl = targetm.delegitimize_address (rtl);
20710 return rtl;
20712 rtl = NULL_RTX;
20714 else if (TREE_CODE (decl) == PARM_DECL)
20716 if (rtl == NULL_RTX
20717 || is_pseudo_reg (rtl)
20718 || (MEM_P (rtl)
20719 && is_pseudo_reg (XEXP (rtl, 0))
20720 && DECL_INCOMING_RTL (decl)
20721 && MEM_P (DECL_INCOMING_RTL (decl))
20722 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20724 tree declared_type = TREE_TYPE (decl);
20725 tree passed_type = DECL_ARG_TYPE (decl);
20726 machine_mode dmode = TYPE_MODE (declared_type);
20727 machine_mode pmode = TYPE_MODE (passed_type);
20729 /* This decl represents a formal parameter which was optimized out.
20730 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20731 all cases where (rtl == NULL_RTX) just below. */
20732 if (dmode == pmode)
20733 rtl = DECL_INCOMING_RTL (decl);
20734 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20735 && SCALAR_INT_MODE_P (dmode)
20736 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20737 && DECL_INCOMING_RTL (decl))
20739 rtx inc = DECL_INCOMING_RTL (decl);
20740 if (REG_P (inc))
20741 rtl = inc;
20742 else if (MEM_P (inc))
20744 if (BYTES_BIG_ENDIAN)
20745 rtl = adjust_address_nv (inc, dmode,
20746 GET_MODE_SIZE (pmode)
20747 - GET_MODE_SIZE (dmode));
20748 else
20749 rtl = inc;
20754 /* If the parm was passed in registers, but lives on the stack, then
20755 make a big endian correction if the mode of the type of the
20756 parameter is not the same as the mode of the rtl. */
20757 /* ??? This is the same series of checks that are made in dbxout.cc before
20758 we reach the big endian correction code there. It isn't clear if all
20759 of these checks are necessary here, but keeping them all is the safe
20760 thing to do. */
20761 else if (MEM_P (rtl)
20762 && XEXP (rtl, 0) != const0_rtx
20763 && ! CONSTANT_P (XEXP (rtl, 0))
20764 /* Not passed in memory. */
20765 && !MEM_P (DECL_INCOMING_RTL (decl))
20766 /* Not passed by invisible reference. */
20767 && (!REG_P (XEXP (rtl, 0))
20768 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20769 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20770 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20771 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20772 #endif
20774 /* Big endian correction check. */
20775 && BYTES_BIG_ENDIAN
20776 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20777 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20778 UNITS_PER_WORD))
20780 machine_mode addr_mode = get_address_mode (rtl);
20781 poly_int64 offset = (UNITS_PER_WORD
20782 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20784 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20785 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20788 else if (VAR_P (decl)
20789 && rtl
20790 && MEM_P (rtl)
20791 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20793 machine_mode addr_mode = get_address_mode (rtl);
20794 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20795 GET_MODE (rtl));
20797 /* If a variable is declared "register" yet is smaller than
20798 a register, then if we store the variable to memory, it
20799 looks like we're storing a register-sized value, when in
20800 fact we are not. We need to adjust the offset of the
20801 storage location to reflect the actual value's bytes,
20802 else gdb will not be able to display it. */
20803 if (maybe_ne (offset, 0))
20804 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20805 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20808 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20809 and will have been substituted directly into all expressions that use it.
20810 C does not have such a concept, but C++ and other languages do. */
20811 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20812 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20814 if (rtl)
20815 rtl = targetm.delegitimize_address (rtl);
20817 /* If we don't look past the constant pool, we risk emitting a
20818 reference to a constant pool entry that isn't referenced from
20819 code, and thus is not emitted. */
20820 if (rtl)
20821 rtl = avoid_constant_pool_reference (rtl);
20823 /* Try harder to get a rtl. If this symbol ends up not being emitted
20824 in the current CU, resolve_addr will remove the expression referencing
20825 it. */
20826 if (rtl == NULL_RTX
20827 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20828 && VAR_P (decl)
20829 && !DECL_EXTERNAL (decl)
20830 && TREE_STATIC (decl)
20831 && DECL_NAME (decl)
20832 && !DECL_HARD_REGISTER (decl)
20833 && DECL_MODE (decl) != VOIDmode)
20835 rtl = make_decl_rtl_for_debug (decl);
20836 if (!MEM_P (rtl)
20837 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20838 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20839 rtl = NULL_RTX;
20842 return rtl;
20845 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20846 returned. If so, the decl for the COMMON block is returned, and the
20847 value is the offset into the common block for the symbol. */
20849 static tree
20850 fortran_common (tree decl, HOST_WIDE_INT *value)
20852 tree val_expr, cvar;
20853 machine_mode mode;
20854 poly_int64 bitsize, bitpos;
20855 tree offset;
20856 HOST_WIDE_INT cbitpos;
20857 int unsignedp, reversep, volatilep = 0;
20859 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20860 it does not have a value (the offset into the common area), or if it
20861 is thread local (as opposed to global) then it isn't common, and shouldn't
20862 be handled as such. */
20863 if (!VAR_P (decl)
20864 || !TREE_STATIC (decl)
20865 || !DECL_HAS_VALUE_EXPR_P (decl)
20866 || !is_fortran ())
20867 return NULL_TREE;
20869 val_expr = DECL_VALUE_EXPR (decl);
20870 if (TREE_CODE (val_expr) != COMPONENT_REF)
20871 return NULL_TREE;
20873 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20874 &unsignedp, &reversep, &volatilep);
20876 if (cvar == NULL_TREE
20877 || !VAR_P (cvar)
20878 || DECL_ARTIFICIAL (cvar)
20879 || !TREE_PUBLIC (cvar)
20880 /* We don't expect to have to cope with variable offsets,
20881 since at present all static data must have a constant size. */
20882 || !bitpos.is_constant (&cbitpos))
20883 return NULL_TREE;
20885 *value = 0;
20886 if (offset != NULL)
20888 if (!tree_fits_shwi_p (offset))
20889 return NULL_TREE;
20890 *value = tree_to_shwi (offset);
20892 if (cbitpos != 0)
20893 *value += cbitpos / BITS_PER_UNIT;
20895 return cvar;
20898 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20899 data attribute for a variable or a parameter. We generate the
20900 DW_AT_const_value attribute only in those cases where the given variable
20901 or parameter does not have a true "location" either in memory or in a
20902 register. This can happen (for example) when a constant is passed as an
20903 actual argument in a call to an inline function. (It's possible that
20904 these things can crop up in other ways also.) Note that one type of
20905 constant value which can be passed into an inlined function is a constant
20906 pointer. This can happen for example if an actual argument in an inlined
20907 function call evaluates to a compile-time constant address.
20909 CACHE_P is true if it is worth caching the location list for DECL,
20910 so that future calls can reuse it rather than regenerate it from scratch.
20911 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20912 since we will need to refer to them each time the function is inlined. */
20914 static bool
20915 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20917 rtx rtl;
20918 dw_loc_list_ref list;
20919 var_loc_list *loc_list;
20920 cached_dw_loc_list *cache;
20922 if (early_dwarf)
20923 return false;
20925 if (TREE_CODE (decl) == ERROR_MARK)
20926 return false;
20928 if (get_AT (die, DW_AT_location)
20929 || get_AT (die, DW_AT_const_value))
20930 return true;
20932 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20933 || TREE_CODE (decl) == RESULT_DECL);
20935 /* Try to get some constant RTL for this decl, and use that as the value of
20936 the location. */
20938 rtl = rtl_for_decl_location (decl);
20939 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20940 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20941 return true;
20943 /* See if we have single element location list that is equivalent to
20944 a constant value. That way we are better to use add_const_value_attribute
20945 rather than expanding constant value equivalent. */
20946 loc_list = lookup_decl_loc (decl);
20947 if (loc_list
20948 && loc_list->first
20949 && loc_list->first->next == NULL
20950 && NOTE_P (loc_list->first->loc)
20951 && NOTE_VAR_LOCATION (loc_list->first->loc)
20952 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20954 struct var_loc_node *node;
20956 node = loc_list->first;
20957 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20958 if (GET_CODE (rtl) == EXPR_LIST)
20959 rtl = XEXP (rtl, 0);
20960 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20961 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20962 return true;
20964 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20965 list several times. See if we've already cached the contents. */
20966 list = NULL;
20967 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20968 cache_p = false;
20969 if (cache_p)
20971 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20972 if (cache)
20973 list = cache->loc_list;
20975 if (list == NULL)
20977 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20978 NULL);
20979 /* It is usually worth caching this result if the decl is from
20980 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20981 if (cache_p && list && list->dw_loc_next)
20983 cached_dw_loc_list **slot
20984 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20985 DECL_UID (decl),
20986 INSERT);
20987 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20988 cache->decl_id = DECL_UID (decl);
20989 cache->loc_list = list;
20990 *slot = cache;
20993 if (list)
20995 add_AT_location_description (die, DW_AT_location, list);
20996 return true;
20998 /* None of that worked, so it must not really have a location;
20999 try adding a constant value attribute from the DECL_INITIAL. */
21000 return tree_add_const_value_attribute_for_decl (die, decl);
21003 /* Mangle referenced decls. */
21004 static tree
21005 mangle_referenced_decls (tree *tp, int *walk_subtrees, void *)
21007 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
21008 *walk_subtrees = 0;
21010 if (VAR_OR_FUNCTION_DECL_P (*tp))
21011 assign_assembler_name_if_needed (*tp);
21013 return NULL_TREE;
21016 /* Attach a DW_AT_const_value attribute to DIE. The value of the
21017 attribute is the const value T. */
21019 static bool
21020 tree_add_const_value_attribute (dw_die_ref die, tree t)
21022 tree init;
21023 tree type = TREE_TYPE (t);
21025 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
21026 return false;
21028 init = t;
21029 gcc_assert (!DECL_P (init));
21031 if (TREE_CODE (init) == INTEGER_CST)
21033 if (tree_fits_uhwi_p (init))
21035 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
21036 return true;
21038 if (tree_fits_shwi_p (init))
21040 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
21041 return true;
21044 if (!early_dwarf)
21046 rtx rtl = rtl_for_decl_init (init, type);
21047 if (rtl)
21048 return add_const_value_attribute (die, TYPE_MODE (type), rtl);
21050 else
21052 /* For early_dwarf force mangling of all referenced symbols. */
21053 tree initializer = init;
21054 STRIP_NOPS (initializer);
21055 /* rtl_for_decl_init punts on other aggregates, and complex values. */
21056 if (AGGREGATE_TYPE_P (type)
21057 || (TREE_CODE (initializer) == VIEW_CONVERT_EXPR
21058 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (initializer, 0))))
21059 || TREE_CODE (type) == COMPLEX_TYPE)
21061 else if (initializer_constant_valid_p (initializer, type))
21062 walk_tree (&initializer, mangle_referenced_decls, NULL, NULL);
21064 /* If the host and target are sane, try harder. */
21065 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
21066 && initializer_constant_valid_p (init, type))
21068 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
21069 if (size > 0 && (int) size == size)
21071 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
21073 if (native_encode_initializer (init, array, size) == size)
21075 add_AT_vec (die, DW_AT_const_value, size, 1, array);
21076 return true;
21078 ggc_free (array);
21081 return false;
21084 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
21085 attribute is the const value of T, where T is an integral constant
21086 variable with static storage duration
21087 (so it can't be a PARM_DECL or a RESULT_DECL). */
21089 static bool
21090 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
21093 if (!decl
21094 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
21095 || (VAR_P (decl) && !TREE_STATIC (decl)))
21096 return false;
21098 if (TREE_READONLY (decl)
21099 && ! TREE_THIS_VOLATILE (decl)
21100 && DECL_INITIAL (decl))
21101 /* OK */;
21102 else
21103 return false;
21105 /* Don't add DW_AT_const_value if abstract origin already has one. */
21106 if (get_AT (var_die, DW_AT_const_value))
21107 return false;
21109 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
21112 /* Convert the CFI instructions for the current function into a
21113 location list. This is used for DW_AT_frame_base when we targeting
21114 a dwarf2 consumer that does not support the dwarf3
21115 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
21116 expressions. */
21118 static dw_loc_list_ref
21119 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
21121 int ix;
21122 dw_fde_ref fde;
21123 dw_loc_list_ref list, *list_tail;
21124 dw_cfi_ref cfi;
21125 dw_cfa_location last_cfa, next_cfa;
21126 const char *start_label, *last_label, *section;
21127 dw_cfa_location remember;
21129 fde = cfun->fde;
21130 gcc_assert (fde != NULL);
21132 section = secname_for_decl (current_function_decl);
21133 list_tail = &list;
21134 list = NULL;
21136 memset (&next_cfa, 0, sizeof (next_cfa));
21137 next_cfa.reg.set_by_dwreg (INVALID_REGNUM);
21138 remember = next_cfa;
21140 start_label = fde->dw_fde_begin;
21142 /* ??? Bald assumption that the CIE opcode list does not contain
21143 advance opcodes. */
21144 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
21145 lookup_cfa_1 (cfi, &next_cfa, &remember);
21147 last_cfa = next_cfa;
21148 last_label = start_label;
21150 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
21152 /* If the first partition contained no CFI adjustments, the
21153 CIE opcodes apply to the whole first partition. */
21154 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21155 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
21156 list_tail =&(*list_tail)->dw_loc_next;
21157 start_label = last_label = fde->dw_fde_second_begin;
21160 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
21162 switch (cfi->dw_cfi_opc)
21164 case DW_CFA_set_loc:
21165 case DW_CFA_advance_loc1:
21166 case DW_CFA_advance_loc2:
21167 case DW_CFA_advance_loc4:
21168 if (!cfa_equal_p (&last_cfa, &next_cfa))
21170 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21171 start_label, 0, last_label, 0, section);
21173 list_tail = &(*list_tail)->dw_loc_next;
21174 last_cfa = next_cfa;
21175 start_label = last_label;
21177 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
21178 break;
21180 case DW_CFA_advance_loc:
21181 /* The encoding is complex enough that we should never emit this. */
21182 gcc_unreachable ();
21184 default:
21185 lookup_cfa_1 (cfi, &next_cfa, &remember);
21186 break;
21188 if (ix + 1 == fde->dw_fde_switch_cfi_index)
21190 if (!cfa_equal_p (&last_cfa, &next_cfa))
21192 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21193 start_label, 0, last_label, 0, section);
21195 list_tail = &(*list_tail)->dw_loc_next;
21196 last_cfa = next_cfa;
21197 start_label = last_label;
21199 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21200 start_label, 0, fde->dw_fde_end, 0, section);
21201 list_tail = &(*list_tail)->dw_loc_next;
21202 start_label = last_label = fde->dw_fde_second_begin;
21206 if (!cfa_equal_p (&last_cfa, &next_cfa))
21208 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21209 start_label, 0, last_label, 0, section);
21210 list_tail = &(*list_tail)->dw_loc_next;
21211 start_label = last_label;
21214 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
21215 start_label, 0,
21216 fde->dw_fde_second_begin
21217 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
21218 section);
21220 maybe_gen_llsym (list);
21222 return list;
21225 /* Compute a displacement from the "steady-state frame pointer" to the
21226 frame base (often the same as the CFA), and store it in
21227 frame_pointer_fb_offset. OFFSET is added to the displacement
21228 before the latter is negated. */
21230 static void
21231 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
21233 rtx reg, elim;
21235 #ifdef FRAME_POINTER_CFA_OFFSET
21236 reg = frame_pointer_rtx;
21237 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
21238 #else
21239 reg = arg_pointer_rtx;
21240 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
21241 #endif
21243 elim = (ira_use_lra_p
21244 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
21245 : eliminate_regs (reg, VOIDmode, NULL_RTX));
21246 elim = strip_offset_and_add (elim, &offset);
21248 frame_pointer_fb_offset = -offset;
21250 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
21251 in which to eliminate. This is because it's stack pointer isn't
21252 directly accessible as a register within the ISA. To work around
21253 this, assume that while we cannot provide a proper value for
21254 frame_pointer_fb_offset, we won't need one either. We can use
21255 hard frame pointer in debug info even if frame pointer isn't used
21256 since hard frame pointer in debug info is encoded with DW_OP_fbreg
21257 which uses the DW_AT_frame_base attribute, not hard frame pointer
21258 directly. */
21259 frame_pointer_fb_offset_valid
21260 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21263 /* Generate a DW_AT_name attribute given some string value to be included as
21264 the value of the attribute. */
21266 void
21267 add_name_attribute (dw_die_ref die, const char *name_string)
21269 if (name_string != NULL && *name_string != 0)
21271 if (demangle_name_func)
21272 name_string = (*demangle_name_func) (name_string);
21274 add_AT_string (die, DW_AT_name, name_string);
21278 /* Generate a DW_AT_name attribute given some string value representing a
21279 file or filepath to be included as value of the attribute. */
21280 static void
21281 add_filename_attribute (dw_die_ref die, const char *name_string)
21283 if (name_string != NULL && *name_string != 0)
21284 add_filepath_AT_string (die, DW_AT_name, name_string);
21287 /* Generate a DW_AT_description attribute given some string value to be included
21288 as the value of the attribute. */
21290 static void
21291 add_desc_attribute (dw_die_ref die, const char *name_string)
21293 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21294 return;
21296 if (name_string == NULL || *name_string == 0)
21297 return;
21299 if (demangle_name_func)
21300 name_string = (*demangle_name_func) (name_string);
21302 add_AT_string (die, DW_AT_description, name_string);
21305 /* Generate a DW_AT_description attribute given some decl to be included
21306 as the value of the attribute. */
21308 static void
21309 add_desc_attribute (dw_die_ref die, tree decl)
21311 tree decl_name;
21313 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21314 return;
21316 if (decl == NULL_TREE || !DECL_P (decl))
21317 return;
21318 decl_name = DECL_NAME (decl);
21320 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21322 const char *name = dwarf2_name (decl, 0);
21323 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21325 else
21327 char *desc = print_generic_expr_to_str (decl);
21328 add_desc_attribute (die, desc);
21329 free (desc);
21333 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21334 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21335 of TYPE accordingly.
21337 ??? This is a temporary measure until after we're able to generate
21338 regular DWARF for the complex Ada type system. */
21340 static void
21341 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21342 dw_die_ref context_die)
21344 tree dtype;
21345 dw_die_ref dtype_die;
21347 if (!lang_hooks.types.descriptive_type)
21348 return;
21350 dtype = lang_hooks.types.descriptive_type (type);
21351 if (!dtype)
21352 return;
21354 dtype_die = lookup_type_die (dtype);
21355 if (!dtype_die)
21357 gen_type_die (dtype, context_die);
21358 dtype_die = lookup_type_die (dtype);
21359 gcc_assert (dtype_die);
21362 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21365 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21367 static const char *
21368 comp_dir_string (void)
21370 const char *wd;
21371 char *wd_plus_sep = NULL;
21372 static const char *cached_wd = NULL;
21374 if (cached_wd != NULL)
21375 return cached_wd;
21377 wd = get_src_pwd ();
21378 if (wd == NULL)
21379 return NULL;
21381 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21383 size_t wdlen = strlen (wd);
21384 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21385 strcpy (wd_plus_sep, wd);
21386 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21387 wd_plus_sep [wdlen + 1] = 0;
21388 wd = wd_plus_sep;
21391 cached_wd = remap_debug_filename (wd);
21393 /* remap_debug_filename can just pass through wd or return a new gc string.
21394 These two types can't be both stored in a GTY(())-tagged string, but since
21395 the cached value lives forever just copy it if needed. */
21396 if (cached_wd != wd)
21398 cached_wd = xstrdup (cached_wd);
21399 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21400 free (wd_plus_sep);
21403 return cached_wd;
21406 /* Generate a DW_AT_comp_dir attribute for DIE. */
21408 static void
21409 add_comp_dir_attribute (dw_die_ref die)
21411 const char * wd = comp_dir_string ();
21412 if (wd != NULL)
21413 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21416 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21417 pointer computation, ...), output a representation for that bound according
21418 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21419 loc_list_from_tree for the meaning of CONTEXT. */
21421 static void
21422 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21423 int forms, struct loc_descr_context *context)
21425 dw_die_ref context_die, decl_die = NULL;
21426 dw_loc_list_ref list;
21427 bool strip_conversions = true;
21428 bool placeholder_seen = false;
21430 while (strip_conversions)
21431 switch (TREE_CODE (value))
21433 case ERROR_MARK:
21434 case SAVE_EXPR:
21435 return;
21437 CASE_CONVERT:
21438 case VIEW_CONVERT_EXPR:
21439 value = TREE_OPERAND (value, 0);
21440 break;
21442 default:
21443 strip_conversions = false;
21444 break;
21447 /* If possible and permitted, output the attribute as a constant. */
21448 if ((forms & dw_scalar_form_constant) != 0
21449 && TREE_CODE (value) == INTEGER_CST)
21451 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21453 /* If HOST_WIDE_INT is big enough then represent the bound as
21454 a constant value. We need to choose a form based on
21455 whether the type is signed or unsigned. We cannot just
21456 call add_AT_unsigned if the value itself is positive
21457 (add_AT_unsigned might add the unsigned value encoded as
21458 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21459 bounds type and then sign extend any unsigned values found
21460 for signed types. This is needed only for
21461 DW_AT_{lower,upper}_bound, since for most other attributes,
21462 consumers will treat DW_FORM_data[1248] as unsigned values,
21463 regardless of the underlying type. */
21464 if (prec <= HOST_BITS_PER_WIDE_INT
21465 || tree_fits_uhwi_p (value))
21467 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21468 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21469 else
21470 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21472 else if (dwarf_version >= 5
21473 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21474 /* Otherwise represent the bound as an unsigned value with
21475 the precision of its type. The precision and signedness
21476 of the type will be necessary to re-interpret it
21477 unambiguously. */
21478 add_AT_wide (die, attr, wi::to_wide (value));
21479 else
21481 rtx v = immed_wide_int_const (wi::to_wide (value),
21482 TYPE_MODE (TREE_TYPE (value)));
21483 dw_loc_descr_ref loc
21484 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21485 VAR_INIT_STATUS_INITIALIZED);
21486 if (loc)
21487 add_AT_loc (die, attr, loc);
21489 return;
21492 /* Otherwise, if it's possible and permitted too, output a reference to
21493 another DIE. */
21494 if ((forms & dw_scalar_form_reference) != 0)
21496 tree decl = NULL_TREE;
21498 /* Some type attributes reference an outer type. For instance, the upper
21499 bound of an array may reference an embedding record (this happens in
21500 Ada). */
21501 if (TREE_CODE (value) == COMPONENT_REF
21502 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21503 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21504 decl = TREE_OPERAND (value, 1);
21506 else if (VAR_P (value)
21507 || TREE_CODE (value) == PARM_DECL
21508 || TREE_CODE (value) == RESULT_DECL)
21509 decl = value;
21511 if (decl != NULL_TREE)
21513 decl_die = lookup_decl_die (decl);
21515 /* ??? Can this happen, or should the variable have been bound
21516 first? Probably it can, since I imagine that we try to create
21517 the types of parameters in the order in which they exist in
21518 the list, and won't have created a forward reference to a
21519 later parameter. */
21520 if (decl_die != NULL)
21522 if (get_AT (decl_die, DW_AT_location)
21523 || get_AT (decl_die, DW_AT_data_member_location)
21524 || get_AT (decl_die, DW_AT_data_bit_offset)
21525 || get_AT (decl_die, DW_AT_const_value))
21527 add_AT_die_ref (die, attr, decl_die);
21528 return;
21534 /* Last chance: try to create a stack operation procedure to evaluate the
21535 value. Do nothing if even that is not possible or permitted. */
21536 if ((forms & dw_scalar_form_exprloc) == 0)
21537 return;
21539 list = loc_list_from_tree (value, 2, context);
21540 if (context && context->placeholder_arg)
21542 placeholder_seen = context->placeholder_seen;
21543 context->placeholder_seen = false;
21545 if (list == NULL || single_element_loc_list_p (list))
21547 /* If this attribute is not a reference nor constant, it is
21548 a DWARF expression rather than location description. For that
21549 loc_list_from_tree (value, 0, &context) is needed. */
21550 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21551 if (list2 && single_element_loc_list_p (list2))
21553 if (placeholder_seen)
21555 struct dwarf_procedure_info dpi;
21556 dpi.fndecl = NULL_TREE;
21557 dpi.args_count = 1;
21558 if (!resolve_args_picking (list2->expr, 1, &dpi))
21559 return;
21561 add_AT_loc (die, attr, list2->expr);
21562 return;
21566 /* If that failed to give a single element location list, fall back to
21567 outputting this as a reference... still if permitted. */
21568 if (list == NULL
21569 || (forms & dw_scalar_form_reference) == 0
21570 || placeholder_seen)
21571 return;
21573 if (!decl_die)
21575 if (current_function_decl == 0)
21576 context_die = comp_unit_die ();
21577 else
21578 context_die = lookup_decl_die (current_function_decl);
21580 decl_die = new_die (DW_TAG_variable, context_die, value);
21581 add_AT_flag (decl_die, DW_AT_artificial, 1);
21582 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21583 context_die);
21586 add_AT_location_description (decl_die, DW_AT_location, list);
21587 add_AT_die_ref (die, attr, decl_die);
21590 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21591 default. */
21593 static int
21594 lower_bound_default (void)
21596 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21598 case DW_LANG_C:
21599 case DW_LANG_C89:
21600 case DW_LANG_C99:
21601 case DW_LANG_C11:
21602 case DW_LANG_C_plus_plus:
21603 case DW_LANG_C_plus_plus_11:
21604 case DW_LANG_C_plus_plus_14:
21605 case DW_LANG_ObjC:
21606 case DW_LANG_ObjC_plus_plus:
21607 return 0;
21608 case DW_LANG_Fortran77:
21609 case DW_LANG_Fortran90:
21610 case DW_LANG_Fortran95:
21611 case DW_LANG_Fortran03:
21612 case DW_LANG_Fortran08:
21613 return 1;
21614 case DW_LANG_UPC:
21615 case DW_LANG_D:
21616 case DW_LANG_Python:
21617 return dwarf_version >= 4 ? 0 : -1;
21618 case DW_LANG_Ada95:
21619 case DW_LANG_Ada83:
21620 case DW_LANG_Cobol74:
21621 case DW_LANG_Cobol85:
21622 case DW_LANG_Modula2:
21623 case DW_LANG_PLI:
21624 return dwarf_version >= 4 ? 1 : -1;
21625 default:
21626 return -1;
21630 /* Given a tree node describing an array bound (either lower or upper) output
21631 a representation for that bound. */
21633 static void
21634 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21635 tree bound, struct loc_descr_context *context)
21637 int dflt;
21639 while (1)
21640 switch (TREE_CODE (bound))
21642 /* Strip all conversions. */
21643 CASE_CONVERT:
21644 case VIEW_CONVERT_EXPR:
21645 bound = TREE_OPERAND (bound, 0);
21646 break;
21648 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21649 are even omitted when they are the default. */
21650 case INTEGER_CST:
21651 /* If the value for this bound is the default one, we can even omit the
21652 attribute. */
21653 if (bound_attr == DW_AT_lower_bound
21654 && tree_fits_shwi_p (bound)
21655 && (dflt = lower_bound_default ()) != -1
21656 && tree_to_shwi (bound) == dflt)
21657 return;
21659 /* FALLTHRU */
21661 default:
21662 /* Let GNAT encodings do the magic for self-referential bounds. */
21663 if (is_ada ()
21664 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21665 && contains_placeholder_p (bound))
21666 return;
21668 add_scalar_info (subrange_die, bound_attr, bound,
21669 dw_scalar_form_constant
21670 | dw_scalar_form_exprloc
21671 | dw_scalar_form_reference,
21672 context);
21673 return;
21677 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21678 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21680 This function reuses previously set type and bound information if
21681 available. */
21683 static void
21684 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21686 dw_die_ref child = type_die->die_child;
21687 struct array_descr_info info;
21688 int dimension_number;
21690 if (lang_hooks.types.get_array_descr_info)
21692 memset (&info, 0, sizeof (info));
21693 if (lang_hooks.types.get_array_descr_info (type, &info))
21694 /* Fortran sometimes emits array types with no dimension. */
21695 gcc_assert (info.ndimensions >= 0
21696 && info.ndimensions
21697 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21699 else
21700 info.ndimensions = 0;
21702 for (dimension_number = 0;
21703 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21704 type = TREE_TYPE (type), dimension_number++)
21706 tree domain = TYPE_DOMAIN (type);
21708 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21709 break;
21711 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21712 and (in GNU C only) variable bounds. Handle all three forms
21713 here. */
21715 /* Find and reuse a previously generated DW_TAG_subrange_type if
21716 available.
21718 For multi-dimensional arrays, as we iterate through the
21719 various dimensions in the enclosing for loop above, we also
21720 iterate through the DIE children and pick at each
21721 DW_TAG_subrange_type previously generated (if available).
21722 Each child DW_TAG_subrange_type DIE describes the range of
21723 the current dimension. At this point we should have as many
21724 DW_TAG_subrange_type's as we have dimensions in the
21725 array. */
21726 dw_die_ref subrange_die = NULL;
21727 if (child)
21728 while (1)
21730 child = child->die_sib;
21731 if (child->die_tag == DW_TAG_subrange_type)
21732 subrange_die = child;
21733 if (child == type_die->die_child)
21735 /* If we wrapped around, stop looking next time. */
21736 child = NULL;
21737 break;
21739 if (child->die_tag == DW_TAG_subrange_type)
21740 break;
21742 if (!subrange_die)
21743 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21745 if (domain)
21747 /* We have an array type with specified bounds. */
21748 tree lower = TYPE_MIN_VALUE (domain);
21749 tree upper = TYPE_MAX_VALUE (domain);
21750 tree index_type = TREE_TYPE (domain);
21752 if (dimension_number <= info.ndimensions - 1)
21754 lower = info.dimen[dimension_number].lower_bound;
21755 upper = info.dimen[dimension_number].upper_bound;
21756 index_type = info.dimen[dimension_number].bounds_type;
21759 /* Define the index type. */
21760 if (index_type && !get_AT (subrange_die, DW_AT_type))
21761 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21762 false, type_die);
21764 /* ??? If upper is NULL, the array has unspecified length,
21765 but it does have a lower bound. This happens with Fortran
21766 dimension arr(N:*)
21767 Since the debugger is definitely going to need to know N
21768 to produce useful results, go ahead and output the lower
21769 bound solo, and hope the debugger can cope. */
21771 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21772 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21774 if (!get_AT (subrange_die, DW_AT_upper_bound)
21775 && !get_AT (subrange_die, DW_AT_count))
21777 if (upper)
21778 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21779 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21780 /* Zero-length array. */
21781 add_bound_info (subrange_die, DW_AT_count,
21782 build_int_cst (TREE_TYPE (lower), 0), NULL);
21786 /* Otherwise we have an array type with an unspecified length. The
21787 DWARF-2 spec does not say how to handle this; let's just leave out the
21788 bounds. */
21792 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21794 static void
21795 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21797 dw_die_ref decl_die;
21798 HOST_WIDE_INT size;
21800 switch (TREE_CODE (tree_node))
21802 case ERROR_MARK:
21803 size = 0;
21804 break;
21805 case ENUMERAL_TYPE:
21806 case RECORD_TYPE:
21807 case UNION_TYPE:
21808 case QUAL_UNION_TYPE:
21809 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21810 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21812 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21813 return;
21815 size = int_size_in_bytes (tree_node);
21816 break;
21817 case FIELD_DECL:
21818 /* For a data member of a struct or union, the DW_AT_byte_size is
21819 generally given as the number of bytes normally allocated for an
21820 object of the *declared* type of the member itself. This is true
21821 even for bit-fields. */
21822 size = int_size_in_bytes (field_type (tree_node));
21823 break;
21824 default:
21825 gcc_unreachable ();
21828 /* Note that `size' might be -1 when we get to this point. If it is, that
21829 indicates that the byte size of the entity in question is variable. */
21830 if (size >= 0)
21831 add_AT_unsigned (die, DW_AT_byte_size, size);
21833 /* Support for dynamically-sized objects was introduced in DWARF3. */
21834 else if (TYPE_P (tree_node)
21835 && (dwarf_version >= 3 || !dwarf_strict)
21836 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21838 struct loc_descr_context ctx = {
21839 const_cast<tree> (tree_node), /* context_type */
21840 NULL_TREE, /* base_decl */
21841 NULL, /* dpi */
21842 false, /* placeholder_arg */
21843 false, /* placeholder_seen */
21844 false /* strict_signedness */
21847 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21848 add_scalar_info (die, DW_AT_byte_size, tree_size,
21849 dw_scalar_form_constant
21850 | dw_scalar_form_exprloc
21851 | dw_scalar_form_reference,
21852 &ctx);
21856 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21857 alignment. */
21859 static void
21860 add_alignment_attribute (dw_die_ref die, tree tree_node)
21862 if (dwarf_version < 5 && dwarf_strict)
21863 return;
21865 unsigned align;
21867 if (DECL_P (tree_node))
21869 if (!DECL_USER_ALIGN (tree_node))
21870 return;
21872 align = DECL_ALIGN_UNIT (tree_node);
21874 else if (TYPE_P (tree_node))
21876 if (!TYPE_USER_ALIGN (tree_node))
21877 return;
21879 align = TYPE_ALIGN_UNIT (tree_node);
21881 else
21882 gcc_unreachable ();
21884 add_AT_unsigned (die, DW_AT_alignment, align);
21887 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21888 which specifies the distance in bits from the highest order bit of the
21889 "containing object" for the bit-field to the highest order bit of the
21890 bit-field itself.
21892 For any given bit-field, the "containing object" is a hypothetical object
21893 (of some integral or enum type) within which the given bit-field lives. The
21894 type of this hypothetical "containing object" is always the same as the
21895 declared type of the individual bit-field itself. The determination of the
21896 exact location of the "containing object" for a bit-field is rather
21897 complicated. It's handled by the `field_byte_offset' function (above).
21899 Note that it is the size (in bytes) of the hypothetical "containing object"
21900 which will be given in the DW_AT_byte_size attribute for this bit-field.
21901 (See `byte_size_attribute' above). */
21903 static inline void
21904 add_bit_offset_attribute (dw_die_ref die, tree decl)
21906 HOST_WIDE_INT object_offset_in_bytes;
21907 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21908 HOST_WIDE_INT bitpos_int;
21909 HOST_WIDE_INT highest_order_object_bit_offset;
21910 HOST_WIDE_INT highest_order_field_bit_offset;
21911 HOST_WIDE_INT bit_offset;
21913 /* The containing object is within the DECL_CONTEXT. */
21914 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21916 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21918 /* Must be a field and a bit field. */
21919 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21921 /* We can't yet handle bit-fields whose offsets are variable, so if we
21922 encounter such things, just return without generating any attribute
21923 whatsoever. Likewise for variable or too large size. */
21924 if (! tree_fits_shwi_p (bit_position (decl))
21925 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21926 return;
21928 bitpos_int = int_bit_position (decl);
21930 /* Note that the bit offset is always the distance (in bits) from the
21931 highest-order bit of the "containing object" to the highest-order bit of
21932 the bit-field itself. Since the "high-order end" of any object or field
21933 is different on big-endian and little-endian machines, the computation
21934 below must take account of these differences. */
21935 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21936 highest_order_field_bit_offset = bitpos_int;
21938 if (! BYTES_BIG_ENDIAN)
21940 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21941 highest_order_object_bit_offset +=
21942 simple_type_size_in_bits (original_type);
21945 bit_offset
21946 = (! BYTES_BIG_ENDIAN
21947 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21948 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21950 if (bit_offset < 0)
21951 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21952 else
21953 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21956 /* For a FIELD_DECL node which represents a bit field, output an attribute
21957 which specifies the length in bits of the given field. */
21959 static inline void
21960 add_bit_size_attribute (dw_die_ref die, tree decl)
21962 /* Must be a field and a bit field. */
21963 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21964 && DECL_BIT_FIELD_TYPE (decl));
21966 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21967 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21970 /* If the compiled language is ANSI C, then add a 'prototyped'
21971 attribute, if arg types are given for the parameters of a function. */
21973 static inline void
21974 add_prototyped_attribute (dw_die_ref die, tree func_type)
21976 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21978 case DW_LANG_C:
21979 case DW_LANG_C89:
21980 case DW_LANG_C99:
21981 case DW_LANG_C11:
21982 case DW_LANG_ObjC:
21983 if (prototype_p (func_type))
21984 add_AT_flag (die, DW_AT_prototyped, 1);
21985 break;
21986 default:
21987 break;
21991 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21992 by looking in the type declaration, the object declaration equate table or
21993 the block mapping. */
21995 static inline void
21996 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21998 dw_die_ref origin_die = NULL;
22000 /* For late LTO debug output we want to refer directly to the abstract
22001 DIE in the early debug rather to the possibly existing concrete
22002 instance and avoid creating that just for this purpose. */
22003 sym_off_pair *desc;
22004 if (in_lto_p
22005 && external_die_map
22006 && (desc = external_die_map->get (origin)))
22008 add_AT_external_die_ref (die, DW_AT_abstract_origin,
22009 desc->sym, desc->off);
22010 return;
22013 if (DECL_P (origin))
22014 origin_die = lookup_decl_die (origin);
22015 else if (TYPE_P (origin))
22016 origin_die = lookup_type_die (origin);
22017 else if (TREE_CODE (origin) == BLOCK)
22018 origin_die = lookup_block_die (origin);
22020 /* XXX: Functions that are never lowered don't always have correct block
22021 trees (in the case of java, they simply have no block tree, in some other
22022 languages). For these functions, there is nothing we can really do to
22023 output correct debug info for inlined functions in all cases. Rather
22024 than die, we'll just produce deficient debug info now, in that we will
22025 have variables without a proper abstract origin. In the future, when all
22026 functions are lowered, we should re-add a gcc_assert (origin_die)
22027 here. */
22029 if (origin_die)
22031 dw_attr_node *a;
22032 /* Like above, if we already created a concrete instance DIE
22033 do not use that for the abstract origin but the early DIE
22034 if present. */
22035 if (in_lto_p
22036 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
22037 origin_die = AT_ref (a);
22038 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
22042 /* We do not currently support the pure_virtual attribute. */
22044 static inline void
22045 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
22047 if (DECL_VINDEX (func_decl))
22049 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22051 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
22052 add_AT_loc (die, DW_AT_vtable_elem_location,
22053 new_loc_descr (DW_OP_constu,
22054 tree_to_shwi (DECL_VINDEX (func_decl)),
22055 0));
22057 /* GNU extension: Record what type this method came from originally. */
22058 if (debug_info_level > DINFO_LEVEL_TERSE
22059 && DECL_CONTEXT (func_decl))
22060 add_AT_die_ref (die, DW_AT_containing_type,
22061 lookup_type_die (DECL_CONTEXT (func_decl)));
22065 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
22066 given decl. This used to be a vendor extension until after DWARF 4
22067 standardized it. */
22069 static void
22070 add_linkage_attr (dw_die_ref die, tree decl)
22072 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
22074 /* Mimic what assemble_name_raw does with a leading '*'. */
22075 if (name[0] == '*')
22076 name = &name[1];
22078 if (dwarf_version >= 4)
22079 add_AT_string (die, DW_AT_linkage_name, name);
22080 else
22081 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
22084 /* Add source coordinate attributes for the given decl. */
22086 static void
22087 add_src_coords_attributes (dw_die_ref die, tree decl)
22089 expanded_location s;
22091 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
22092 return;
22093 s = expand_location (DECL_SOURCE_LOCATION (decl));
22094 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
22095 add_AT_unsigned (die, DW_AT_decl_line, s.line);
22096 if (debug_column_info && s.column)
22097 add_AT_unsigned (die, DW_AT_decl_column, s.column);
22100 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
22102 static void
22103 add_linkage_name_raw (dw_die_ref die, tree decl)
22105 /* Defer until we have an assembler name set. */
22106 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
22108 limbo_die_node *asm_name;
22110 asm_name = ggc_cleared_alloc<limbo_die_node> ();
22111 asm_name->die = die;
22112 asm_name->created_for = decl;
22113 asm_name->next = deferred_asm_name;
22114 deferred_asm_name = asm_name;
22116 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22117 add_linkage_attr (die, decl);
22120 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
22122 static void
22123 add_linkage_name (dw_die_ref die, tree decl)
22125 if (debug_info_level > DINFO_LEVEL_NONE
22126 && VAR_OR_FUNCTION_DECL_P (decl)
22127 && TREE_PUBLIC (decl)
22128 && !(VAR_P (decl) && DECL_REGISTER (decl))
22129 && die->die_tag != DW_TAG_member)
22130 add_linkage_name_raw (die, decl);
22133 /* Add a DW_AT_name attribute and source coordinate attribute for the
22134 given decl, but only if it actually has a name. */
22136 static void
22137 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
22138 bool no_linkage_name)
22140 tree decl_name;
22142 decl_name = DECL_NAME (decl);
22143 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
22145 const char *name = dwarf2_name (decl, 0);
22146 if (name)
22147 add_name_attribute (die, name);
22148 else
22149 add_desc_attribute (die, decl);
22151 if (! DECL_ARTIFICIAL (decl))
22152 add_src_coords_attributes (die, decl);
22154 if (!no_linkage_name)
22155 add_linkage_name (die, decl);
22157 else
22158 add_desc_attribute (die, decl);
22160 #ifdef VMS_DEBUGGING_INFO
22161 /* Get the function's name, as described by its RTL. This may be different
22162 from the DECL_NAME name used in the source file. */
22163 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
22165 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
22166 XEXP (DECL_RTL (decl), 0), false);
22167 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
22169 #endif /* VMS_DEBUGGING_INFO */
22172 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
22174 static void
22175 add_discr_value (dw_die_ref die, dw_discr_value *value)
22177 dw_attr_node attr;
22179 attr.dw_attr = DW_AT_discr_value;
22180 attr.dw_attr_val.val_class = dw_val_class_discr_value;
22181 attr.dw_attr_val.val_entry = NULL;
22182 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
22183 if (value->pos)
22184 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
22185 else
22186 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
22187 add_dwarf_attr (die, &attr);
22190 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
22192 static void
22193 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
22195 dw_attr_node attr;
22197 attr.dw_attr = DW_AT_discr_list;
22198 attr.dw_attr_val.val_class = dw_val_class_discr_list;
22199 attr.dw_attr_val.val_entry = NULL;
22200 attr.dw_attr_val.v.val_discr_list = discr_list;
22201 add_dwarf_attr (die, &attr);
22204 static inline dw_discr_list_ref
22205 AT_discr_list (dw_attr_node *attr)
22207 return attr->dw_attr_val.v.val_discr_list;
22210 #ifdef VMS_DEBUGGING_INFO
22211 /* Output the debug main pointer die for VMS */
22213 void
22214 dwarf2out_vms_debug_main_pointer (void)
22216 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22217 dw_die_ref die;
22219 /* Allocate the VMS debug main subprogram die. */
22220 die = new_die_raw (DW_TAG_subprogram);
22221 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
22222 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
22223 current_function_funcdef_no);
22224 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22226 /* Make it the first child of comp_unit_die (). */
22227 die->die_parent = comp_unit_die ();
22228 if (comp_unit_die ()->die_child)
22230 die->die_sib = comp_unit_die ()->die_child->die_sib;
22231 comp_unit_die ()->die_child->die_sib = die;
22233 else
22235 die->die_sib = die;
22236 comp_unit_die ()->die_child = die;
22239 #endif /* VMS_DEBUGGING_INFO */
22241 /* walk_tree helper function for uses_local_type, below. */
22243 static tree
22244 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
22246 if (!TYPE_P (*tp))
22247 *walk_subtrees = 0;
22248 else
22250 tree name = TYPE_NAME (*tp);
22251 if (name && DECL_P (name) && decl_function_context (name))
22252 return *tp;
22254 return NULL_TREE;
22257 /* If TYPE involves a function-local type (including a local typedef to a
22258 non-local type), returns that type; otherwise returns NULL_TREE. */
22260 static tree
22261 uses_local_type (tree type)
22263 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22264 return used;
22267 /* Return the DIE for the scope that immediately contains this type.
22268 Non-named types that do not involve a function-local type get global
22269 scope. Named types nested in namespaces or other types get their
22270 containing scope. All other types (i.e. function-local named types) get
22271 the current active scope. */
22273 static dw_die_ref
22274 scope_die_for (tree t, dw_die_ref context_die)
22276 dw_die_ref scope_die = NULL;
22277 tree containing_scope;
22279 /* Non-types always go in the current scope. */
22280 gcc_assert (TYPE_P (t));
22282 /* Use the scope of the typedef, rather than the scope of the type
22283 it refers to. */
22284 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22285 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22286 else
22287 containing_scope = TYPE_CONTEXT (t);
22289 /* Use the containing namespace if there is one. */
22290 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22292 if (context_die == lookup_decl_die (containing_scope))
22293 /* OK */;
22294 else if (debug_info_level > DINFO_LEVEL_TERSE)
22295 context_die = get_context_die (containing_scope);
22296 else
22297 containing_scope = NULL_TREE;
22300 /* Ignore function type "scopes" from the C frontend. They mean that
22301 a tagged type is local to a parmlist of a function declarator, but
22302 that isn't useful to DWARF. */
22303 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22304 containing_scope = NULL_TREE;
22306 if (SCOPE_FILE_SCOPE_P (containing_scope))
22308 /* If T uses a local type keep it local as well, to avoid references
22309 to function-local DIEs from outside the function. */
22310 if (current_function_decl && uses_local_type (t))
22311 scope_die = context_die;
22312 else
22313 scope_die = comp_unit_die ();
22315 else if (TYPE_P (containing_scope))
22317 /* For types, we can just look up the appropriate DIE. */
22318 if (debug_info_level > DINFO_LEVEL_TERSE)
22319 scope_die = get_context_die (containing_scope);
22320 else
22322 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22323 if (scope_die == NULL)
22324 scope_die = comp_unit_die ();
22327 else
22328 scope_die = context_die;
22330 return scope_die;
22333 /* Returns true if CONTEXT_DIE is internal to a function. */
22335 static inline bool
22336 local_scope_p (dw_die_ref context_die)
22338 for (; context_die; context_die = context_die->die_parent)
22339 if (context_die->die_tag == DW_TAG_inlined_subroutine
22340 || context_die->die_tag == DW_TAG_subprogram)
22341 return true;
22343 return false;
22346 /* Returns true if CONTEXT_DIE is a class. */
22348 static inline bool
22349 class_scope_p (dw_die_ref context_die)
22351 return (context_die
22352 && (context_die->die_tag == DW_TAG_structure_type
22353 || context_die->die_tag == DW_TAG_class_type
22354 || context_die->die_tag == DW_TAG_interface_type
22355 || context_die->die_tag == DW_TAG_union_type));
22358 /* Returns true if CONTEXT_DIE is a class or namespace, for deciding
22359 whether or not to treat a DIE in this context as a declaration. */
22361 static inline bool
22362 class_or_namespace_scope_p (dw_die_ref context_die)
22364 return (class_scope_p (context_die)
22365 || (context_die && context_die->die_tag == DW_TAG_namespace));
22368 /* Many forms of DIEs require a "type description" attribute. This
22369 routine locates the proper "type descriptor" die for the type given
22370 by 'type' plus any additional qualifiers given by 'cv_quals', and
22371 adds a DW_AT_type attribute below the given die. */
22373 static void
22374 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22375 bool reverse, dw_die_ref context_die)
22377 enum tree_code code = TREE_CODE (type);
22378 dw_die_ref type_die = NULL;
22380 if (debug_info_level <= DINFO_LEVEL_TERSE)
22381 return;
22383 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22384 or fixed-point type, use the inner type. This is because we have no
22385 support for unnamed types in base_type_die. This can happen if this is
22386 an Ada subrange type. Correct solution is emit a subrange type die. */
22387 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22388 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22389 type = TREE_TYPE (type), code = TREE_CODE (type);
22391 if (code == ERROR_MARK
22392 /* Handle a special case. For functions whose return type is void, we
22393 generate *no* type attribute. (Note that no object may have type
22394 `void', so this only applies to function return types). */
22395 || code == VOID_TYPE)
22396 return;
22398 type_die = modified_type_die (type,
22399 cv_quals | TYPE_QUALS (type),
22400 reverse,
22401 context_die);
22403 if (type_die != NULL)
22404 add_AT_die_ref (object_die, DW_AT_type, type_die);
22407 /* Given an object die, add the calling convention attribute for the
22408 function call type. */
22409 static void
22410 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22412 enum dwarf_calling_convention value = DW_CC_normal;
22414 value = ((enum dwarf_calling_convention)
22415 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22417 if (is_fortran ()
22418 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22420 /* DWARF 2 doesn't provide a way to identify a program's source-level
22421 entry point. DW_AT_calling_convention attributes are only meant
22422 to describe functions' calling conventions. However, lacking a
22423 better way to signal the Fortran main program, we used this for
22424 a long time, following existing custom. Now, DWARF 4 has
22425 DW_AT_main_subprogram, which we add below, but some tools still
22426 rely on the old way, which we thus keep. */
22427 value = DW_CC_program;
22429 if (dwarf_version >= 4 || !dwarf_strict)
22430 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22433 /* Only add the attribute if the backend requests it, and
22434 is not DW_CC_normal. */
22435 if (value && (value != DW_CC_normal))
22436 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22439 /* Given a tree pointer to a struct, class, union, or enum type node, return
22440 a pointer to the (string) tag name for the given type, or zero if the type
22441 was declared without a tag. */
22443 static const char *
22444 type_tag (const_tree type)
22446 const char *name = 0;
22448 if (TYPE_NAME (type) != 0)
22450 tree t = 0;
22452 /* Find the IDENTIFIER_NODE for the type name. */
22453 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22454 && !TYPE_NAMELESS (type))
22455 t = TYPE_NAME (type);
22457 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22458 a TYPE_DECL node, regardless of whether or not a `typedef' was
22459 involved. */
22460 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22461 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22463 /* We want to be extra verbose. Don't call dwarf_name if
22464 DECL_NAME isn't set. The default hook for decl_printable_name
22465 doesn't like that, and in this context it's correct to return
22466 0, instead of "<anonymous>" or the like. */
22467 if (DECL_NAME (TYPE_NAME (type))
22468 && !DECL_NAMELESS (TYPE_NAME (type)))
22469 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22472 /* Now get the name as a string, or invent one. */
22473 if (!name && t != 0)
22474 name = IDENTIFIER_POINTER (t);
22477 return (name == 0 || *name == '\0') ? 0 : name;
22480 /* Return the type associated with a data member, make a special check
22481 for bit field types. */
22483 static inline tree
22484 member_declared_type (const_tree member)
22486 return (DECL_BIT_FIELD_TYPE (member)
22487 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22490 /* Get the decl's label, as described by its RTL. This may be different
22491 from the DECL_NAME name used in the source file. */
22493 #if 0
22494 static const char *
22495 decl_start_label (tree decl)
22497 rtx x;
22498 const char *fnname;
22500 x = DECL_RTL (decl);
22501 gcc_assert (MEM_P (x));
22503 x = XEXP (x, 0);
22504 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22506 fnname = XSTR (x, 0);
22507 return fnname;
22509 #endif
22511 /* For variable-length arrays that have been previously generated, but
22512 may be incomplete due to missing subscript info, fill the subscript
22513 info. Return TRUE if this is one of those cases. */
22515 static bool
22516 fill_variable_array_bounds (tree type)
22518 if (TREE_ASM_WRITTEN (type)
22519 && TREE_CODE (type) == ARRAY_TYPE
22520 && variably_modified_type_p (type, NULL))
22522 dw_die_ref array_die = lookup_type_die (type);
22523 if (!array_die)
22524 return false;
22525 add_subscript_info (array_die, type, !is_ada ());
22526 return true;
22528 return false;
22531 /* These routines generate the internal representation of the DIE's for
22532 the compilation unit. Debugging information is collected by walking
22533 the declaration trees passed in from dwarf2out_decl(). */
22535 static void
22536 gen_array_type_die (tree type, dw_die_ref context_die)
22538 dw_die_ref array_die;
22540 /* GNU compilers represent multidimensional array types as sequences of one
22541 dimensional array types whose element types are themselves array types.
22542 We sometimes squish that down to a single array_type DIE with multiple
22543 subscripts in the Dwarf debugging info. The draft Dwarf specification
22544 say that we are allowed to do this kind of compression in C, because
22545 there is no difference between an array of arrays and a multidimensional
22546 array. We don't do this for Ada to remain as close as possible to the
22547 actual representation, which is especially important against the language
22548 flexibilty wrt arrays of variable size. */
22550 bool collapse_nested_arrays = !is_ada ();
22552 if (fill_variable_array_bounds (type))
22553 return;
22555 dw_die_ref scope_die = scope_die_for (type, context_die);
22556 tree element_type;
22558 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22559 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22560 if (TREE_CODE (type) == ARRAY_TYPE
22561 && TYPE_STRING_FLAG (type)
22562 && is_fortran ()
22563 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22565 HOST_WIDE_INT size;
22567 array_die = new_die (DW_TAG_string_type, scope_die, type);
22568 add_name_attribute (array_die, type_tag (type));
22569 equate_type_number_to_die (type, array_die);
22570 size = int_size_in_bytes (type);
22571 if (size >= 0)
22572 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22573 /* ??? We can't annotate types late, but for LTO we may not
22574 generate a location early either (gfortran.dg/save_6.f90). */
22575 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22576 && TYPE_DOMAIN (type) != NULL_TREE
22577 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22579 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22580 tree rszdecl = szdecl;
22582 size = int_size_in_bytes (TREE_TYPE (szdecl));
22583 if (!DECL_P (szdecl))
22585 if (INDIRECT_REF_P (szdecl)
22586 && DECL_P (TREE_OPERAND (szdecl, 0)))
22588 rszdecl = TREE_OPERAND (szdecl, 0);
22589 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22590 != DWARF2_ADDR_SIZE)
22591 size = 0;
22593 else
22594 size = 0;
22596 if (size > 0)
22598 dw_loc_list_ref loc
22599 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22600 NULL);
22601 if (loc)
22603 add_AT_location_description (array_die, DW_AT_string_length,
22604 loc);
22605 if (size != DWARF2_ADDR_SIZE)
22606 add_AT_unsigned (array_die, dwarf_version >= 5
22607 ? DW_AT_string_length_byte_size
22608 : DW_AT_byte_size, size);
22612 return;
22615 array_die = new_die (DW_TAG_array_type, scope_die, type);
22616 add_name_attribute (array_die, type_tag (type));
22617 equate_type_number_to_die (type, array_die);
22619 if (VECTOR_TYPE_P (type))
22620 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22622 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22623 if (is_fortran ()
22624 && TREE_CODE (type) == ARRAY_TYPE
22625 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22626 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22627 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22629 #if 0
22630 /* We default the array ordering. Debuggers will probably do the right
22631 things even if DW_AT_ordering is not present. It's not even an issue
22632 until we start to get into multidimensional arrays anyway. If a debugger
22633 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22634 then we'll have to put the DW_AT_ordering attribute back in. (But if
22635 and when we find out that we need to put these in, we will only do so
22636 for multidimensional arrays. */
22637 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22638 #endif
22640 if (VECTOR_TYPE_P (type))
22642 /* For VECTOR_TYPEs we use an array DIE with appropriate bounds. */
22643 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22644 int lb = lower_bound_default ();
22645 if (lb == -1)
22646 lb = 0;
22647 add_bound_info (subrange_die, DW_AT_lower_bound, size_int (lb), NULL);
22648 add_bound_info (subrange_die, DW_AT_upper_bound,
22649 size_int (lb + TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22651 else
22652 add_subscript_info (array_die, type, collapse_nested_arrays);
22654 /* Add representation of the type of the elements of this array type and
22655 emit the corresponding DIE if we haven't done it already. */
22656 element_type = TREE_TYPE (type);
22657 if (collapse_nested_arrays)
22658 while (TREE_CODE (element_type) == ARRAY_TYPE)
22660 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22661 break;
22662 element_type = TREE_TYPE (element_type);
22665 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22666 TREE_CODE (type) == ARRAY_TYPE
22667 && TYPE_REVERSE_STORAGE_ORDER (type),
22668 context_die);
22670 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22671 if (TYPE_ARTIFICIAL (type))
22672 add_AT_flag (array_die, DW_AT_artificial, 1);
22674 if (get_AT (array_die, DW_AT_name))
22675 add_pubtype (type, array_die);
22677 add_alignment_attribute (array_die, type);
22680 /* This routine generates DIE for array with hidden descriptor, details
22681 are filled into *info by a langhook. */
22683 static void
22684 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22685 dw_die_ref context_die)
22687 const dw_die_ref scope_die = scope_die_for (type, context_die);
22688 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22689 struct loc_descr_context context = {
22690 type, /* context_type */
22691 info->base_decl, /* base_decl */
22692 NULL, /* dpi */
22693 false, /* placeholder_arg */
22694 false, /* placeholder_seen */
22695 false /* strict_signedness */
22697 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22698 int dim;
22700 add_name_attribute (array_die, type_tag (type));
22701 equate_type_number_to_die (type, array_die);
22703 if (info->ndimensions > 1)
22704 switch (info->ordering)
22706 case array_descr_ordering_row_major:
22707 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22708 break;
22709 case array_descr_ordering_column_major:
22710 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22711 break;
22712 default:
22713 break;
22716 if (dwarf_version >= 3 || !dwarf_strict)
22718 if (info->data_location)
22719 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22720 dw_scalar_form_exprloc, &context);
22721 if (info->associated)
22722 add_scalar_info (array_die, DW_AT_associated, info->associated,
22723 dw_scalar_form_constant
22724 | dw_scalar_form_exprloc
22725 | dw_scalar_form_reference, &context);
22726 if (info->allocated)
22727 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22728 dw_scalar_form_constant
22729 | dw_scalar_form_exprloc
22730 | dw_scalar_form_reference, &context);
22731 if (info->stride)
22733 const enum dwarf_attribute attr
22734 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22735 const int forms
22736 = (info->stride_in_bits)
22737 ? dw_scalar_form_constant
22738 : (dw_scalar_form_constant
22739 | dw_scalar_form_exprloc
22740 | dw_scalar_form_reference);
22742 add_scalar_info (array_die, attr, info->stride, forms, &context);
22745 if (dwarf_version >= 5)
22747 if (info->rank)
22749 add_scalar_info (array_die, DW_AT_rank, info->rank,
22750 dw_scalar_form_constant
22751 | dw_scalar_form_exprloc, &context);
22752 subrange_tag = DW_TAG_generic_subrange;
22753 context.placeholder_arg = true;
22757 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22759 for (dim = 0; dim < info->ndimensions; dim++)
22761 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22763 if (info->dimen[dim].bounds_type)
22764 add_type_attribute (subrange_die,
22765 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22766 false, context_die);
22767 if (info->dimen[dim].lower_bound)
22768 add_bound_info (subrange_die, DW_AT_lower_bound,
22769 info->dimen[dim].lower_bound, &context);
22770 if (info->dimen[dim].upper_bound)
22771 add_bound_info (subrange_die, DW_AT_upper_bound,
22772 info->dimen[dim].upper_bound, &context);
22773 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22774 add_scalar_info (subrange_die, DW_AT_byte_stride,
22775 info->dimen[dim].stride,
22776 dw_scalar_form_constant
22777 | dw_scalar_form_exprloc
22778 | dw_scalar_form_reference,
22779 &context);
22782 gen_type_die (info->element_type, context_die);
22783 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22784 TREE_CODE (type) == ARRAY_TYPE
22785 && TYPE_REVERSE_STORAGE_ORDER (type),
22786 context_die);
22788 if (get_AT (array_die, DW_AT_name))
22789 add_pubtype (type, array_die);
22791 add_alignment_attribute (array_die, type);
22794 #if 0
22795 static void
22796 gen_entry_point_die (tree decl, dw_die_ref context_die)
22798 tree origin = decl_ultimate_origin (decl);
22799 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22801 if (origin != NULL)
22802 add_abstract_origin_attribute (decl_die, origin);
22803 else
22805 add_name_and_src_coords_attributes (decl_die, decl);
22806 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22807 TYPE_UNQUALIFIED, false, context_die);
22810 if (DECL_ABSTRACT_P (decl))
22811 equate_decl_number_to_die (decl, decl_die);
22812 else
22813 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22815 #endif
22817 /* Walk through the list of incomplete types again, trying once more to
22818 emit full debugging info for them. */
22820 static void
22821 retry_incomplete_types (void)
22823 set_early_dwarf s;
22824 int i;
22826 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22827 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22828 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22829 vec_safe_truncate (incomplete_types, 0);
22832 /* Determine what tag to use for a record type. */
22834 static enum dwarf_tag
22835 record_type_tag (tree type)
22837 if (! lang_hooks.types.classify_record)
22838 return DW_TAG_structure_type;
22840 switch (lang_hooks.types.classify_record (type))
22842 case RECORD_IS_STRUCT:
22843 return DW_TAG_structure_type;
22845 case RECORD_IS_CLASS:
22846 return DW_TAG_class_type;
22848 case RECORD_IS_INTERFACE:
22849 if (dwarf_version >= 3 || !dwarf_strict)
22850 return DW_TAG_interface_type;
22851 return DW_TAG_structure_type;
22853 default:
22854 gcc_unreachable ();
22858 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22859 include all of the information about the enumeration values also. Each
22860 enumerated type name/value is listed as a child of the enumerated type
22861 DIE. REVERSE is true if the type is to be interpreted in the reverse
22862 storage order wrt the target order. */
22864 static dw_die_ref
22865 gen_enumeration_type_die (tree type, dw_die_ref context_die, bool reverse)
22867 dw_die_ref type_die = lookup_type_die (type);
22868 dw_die_ref orig_type_die = type_die;
22870 if (type_die == NULL || reverse)
22872 dw_die_ref scope_die = scope_die_for (type, context_die);
22874 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
22875 if (reverse)
22877 gcc_assert (type_die);
22878 dw_die_ref after_die = type_die;
22879 type_die = new_die_raw (DW_TAG_enumeration_type);
22880 add_child_die_after (scope_die, type_die, after_die);
22882 else
22884 type_die = new_die (DW_TAG_enumeration_type, scope_die, type);
22885 equate_type_number_to_die (type, type_die);
22887 add_name_attribute (type_die, type_tag (type));
22888 if ((dwarf_version >= 4 || !dwarf_strict)
22889 && ENUM_IS_SCOPED (type))
22890 add_AT_flag (type_die, DW_AT_enum_class, 1);
22891 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22892 add_AT_flag (type_die, DW_AT_declaration, 1);
22893 if (!dwarf_strict)
22894 add_AT_unsigned (type_die, DW_AT_encoding,
22895 TYPE_UNSIGNED (type)
22896 ? DW_ATE_unsigned
22897 : DW_ATE_signed);
22898 if (reverse)
22899 add_AT_unsigned (type_die, DW_AT_endianity,
22900 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
22902 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22903 return type_die;
22904 else
22905 remove_AT (type_die, DW_AT_declaration);
22907 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22908 given enum type is incomplete, do not generate the DW_AT_byte_size
22909 attribute or the DW_AT_element_list attribute. */
22910 if (TYPE_SIZE (type))
22912 tree link;
22914 if (!ENUM_IS_OPAQUE (type))
22915 TREE_ASM_WRITTEN (type) = 1;
22916 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22917 add_byte_size_attribute (type_die, type);
22918 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22919 add_alignment_attribute (type_die, type);
22920 if ((dwarf_version >= 3 || !dwarf_strict)
22921 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22923 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22924 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22925 context_die);
22927 if (TYPE_STUB_DECL (type) != NULL_TREE)
22929 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22930 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22931 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22932 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22935 /* If the first reference to this type was as the return type of an
22936 inline function, then it may not have a parent. Fix this now. */
22937 if (type_die->die_parent == NULL)
22938 add_child_die (scope_die_for (type, context_die), type_die);
22940 for (link = TYPE_VALUES (type);
22941 link != NULL; link = TREE_CHAIN (link))
22943 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22944 tree value = TREE_VALUE (link);
22946 if (DECL_P (value))
22947 equate_decl_number_to_die (value, enum_die);
22949 gcc_assert (!ENUM_IS_OPAQUE (type));
22950 add_name_attribute (enum_die,
22951 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22953 if (TREE_CODE (value) == CONST_DECL)
22954 value = DECL_INITIAL (value);
22956 if (simple_type_size_in_bits (TREE_TYPE (value))
22957 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22959 /* For constant forms created by add_AT_unsigned DWARF
22960 consumers (GDB, elfutils, etc.) always zero extend
22961 the value. Only when the actual value is negative
22962 do we need to use add_AT_int to generate a constant
22963 form that can represent negative values. */
22964 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22965 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22966 add_AT_unsigned (enum_die, DW_AT_const_value,
22967 (unsigned HOST_WIDE_INT) val);
22968 else
22969 add_AT_int (enum_die, DW_AT_const_value, val);
22971 else
22972 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22973 that here. TODO: This should be re-worked to use correct
22974 signed/unsigned double tags for all cases. */
22975 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22978 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22979 if (TYPE_ARTIFICIAL (type)
22980 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22981 add_AT_flag (type_die, DW_AT_artificial, 1);
22983 else
22984 add_AT_flag (type_die, DW_AT_declaration, 1);
22986 add_pubtype (type, type_die);
22988 return type_die;
22991 /* Generate a DIE to represent either a real live formal parameter decl or to
22992 represent just the type of some formal parameter position in some function
22993 type.
22995 Note that this routine is a bit unusual because its argument may be a
22996 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22997 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22998 node. If it's the former then this function is being called to output a
22999 DIE to represent a formal parameter object (or some inlining thereof). If
23000 it's the latter, then this function is only being called to output a
23001 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
23002 argument type of some subprogram type.
23003 If EMIT_NAME_P is true, name and source coordinate attributes
23004 are emitted. */
23006 static dw_die_ref
23007 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
23008 dw_die_ref context_die)
23010 tree node_or_origin = node ? node : origin;
23011 tree ultimate_origin;
23012 dw_die_ref parm_die = NULL;
23014 if (DECL_P (node_or_origin))
23016 parm_die = lookup_decl_die (node);
23018 /* If the contexts differ, we may not be talking about the same
23019 thing.
23020 ??? When in LTO the DIE parent is the "abstract" copy and the
23021 context_die is the specification "copy". */
23022 if (parm_die
23023 && parm_die->die_parent != context_die
23024 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
23025 || parm_die->die_parent->die_parent != context_die)
23026 && !in_lto_p)
23028 gcc_assert (!DECL_ABSTRACT_P (node));
23029 /* This can happen when creating a concrete instance, in
23030 which case we need to create a new DIE that will get
23031 annotated with DW_AT_abstract_origin. */
23032 parm_die = NULL;
23035 if (parm_die && parm_die->die_parent == NULL)
23037 /* Check that parm_die already has the right attributes that
23038 we would have added below. If any attributes are
23039 missing, fall through to add them. */
23040 if (! DECL_ABSTRACT_P (node_or_origin)
23041 && !get_AT (parm_die, DW_AT_location)
23042 && !get_AT (parm_die, DW_AT_const_value))
23043 /* We are missing location info, and are about to add it. */
23045 else
23047 add_child_die (context_die, parm_die);
23048 return parm_die;
23053 /* If we have a previously generated DIE, use it, unless this is an
23054 concrete instance (origin != NULL), in which case we need a new
23055 DIE with a corresponding DW_AT_abstract_origin. */
23056 bool reusing_die;
23057 if (parm_die && origin == NULL)
23058 reusing_die = true;
23059 else
23061 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
23062 reusing_die = false;
23065 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
23067 case tcc_declaration:
23068 ultimate_origin = decl_ultimate_origin (node_or_origin);
23069 if (node || ultimate_origin)
23070 origin = ultimate_origin;
23072 if (reusing_die)
23073 goto add_location;
23075 if (origin != NULL)
23076 add_abstract_origin_attribute (parm_die, origin);
23077 else if (emit_name_p)
23078 add_name_and_src_coords_attributes (parm_die, node);
23079 if (origin == NULL
23080 || (! DECL_ABSTRACT_P (node_or_origin)
23081 && variably_modified_type_p (TREE_TYPE (node_or_origin),
23082 decl_function_context
23083 (node_or_origin))))
23085 tree type = TREE_TYPE (node_or_origin);
23086 if (decl_by_reference_p (node_or_origin))
23087 add_type_attribute (parm_die, TREE_TYPE (type),
23088 TYPE_UNQUALIFIED,
23089 false, context_die);
23090 else
23091 add_type_attribute (parm_die, type,
23092 decl_quals (node_or_origin),
23093 false, context_die);
23095 if (origin == NULL && DECL_ARTIFICIAL (node))
23096 add_AT_flag (parm_die, DW_AT_artificial, 1);
23097 add_location:
23098 if (node && node != origin)
23099 equate_decl_number_to_die (node, parm_die);
23100 if (! DECL_ABSTRACT_P (node_or_origin))
23101 add_location_or_const_value_attribute (parm_die, node_or_origin,
23102 node == NULL);
23104 break;
23106 case tcc_type:
23107 /* We were called with some kind of a ..._TYPE node. */
23108 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
23109 context_die);
23110 break;
23112 default:
23113 gcc_unreachable ();
23116 return parm_die;
23119 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
23120 children DW_TAG_formal_parameter DIEs representing the arguments of the
23121 parameter pack.
23123 PARM_PACK must be a function parameter pack.
23124 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
23125 must point to the subsequent arguments of the function PACK_ARG belongs to.
23126 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
23127 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
23128 following the last one for which a DIE was generated. */
23130 static dw_die_ref
23131 gen_formal_parameter_pack_die (tree parm_pack,
23132 tree pack_arg,
23133 dw_die_ref subr_die,
23134 tree *next_arg)
23136 tree arg;
23137 dw_die_ref parm_pack_die;
23139 gcc_assert (parm_pack
23140 && lang_hooks.function_parameter_pack_p (parm_pack)
23141 && subr_die);
23143 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
23144 add_src_coords_attributes (parm_pack_die, parm_pack);
23146 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
23148 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
23149 parm_pack))
23150 break;
23151 gen_formal_parameter_die (arg, NULL,
23152 false /* Don't emit name attribute. */,
23153 parm_pack_die);
23155 if (next_arg)
23156 *next_arg = arg;
23157 return parm_pack_die;
23160 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
23161 at the end of an (ANSI prototyped) formal parameters list. */
23163 static void
23164 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
23166 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
23169 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
23170 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
23171 parameters as specified in some function type specification (except for
23172 those which appear as part of a function *definition*). */
23174 static void
23175 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
23177 tree link;
23178 tree formal_type = NULL;
23179 tree first_parm_type;
23180 tree arg;
23182 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
23184 arg = DECL_ARGUMENTS (function_or_method_type);
23185 function_or_method_type = TREE_TYPE (function_or_method_type);
23187 else
23188 arg = NULL_TREE;
23190 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
23192 /* Make our first pass over the list of formal parameter types and output a
23193 DW_TAG_formal_parameter DIE for each one. */
23194 for (link = first_parm_type; link; )
23196 dw_die_ref parm_die;
23198 formal_type = TREE_VALUE (link);
23199 if (formal_type == void_type_node)
23200 break;
23202 /* Output a (nameless) DIE to represent the formal parameter itself. */
23203 parm_die = gen_formal_parameter_die (formal_type, NULL,
23204 true /* Emit name attribute. */,
23205 context_die);
23206 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
23207 && link == first_parm_type)
23209 add_AT_flag (parm_die, DW_AT_artificial, 1);
23210 if (dwarf_version >= 3 || !dwarf_strict)
23211 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
23213 else if (arg && DECL_ARTIFICIAL (arg))
23214 add_AT_flag (parm_die, DW_AT_artificial, 1);
23216 link = TREE_CHAIN (link);
23217 if (arg)
23218 arg = DECL_CHAIN (arg);
23221 /* If this function type has an ellipsis, add a
23222 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
23223 if (formal_type != void_type_node)
23224 gen_unspecified_parameters_die (function_or_method_type, context_die);
23226 /* Make our second (and final) pass over the list of formal parameter types
23227 and output DIEs to represent those types (as necessary). */
23228 for (link = TYPE_ARG_TYPES (function_or_method_type);
23229 link && TREE_VALUE (link);
23230 link = TREE_CHAIN (link))
23231 gen_type_die (TREE_VALUE (link), context_die);
23234 /* We want to generate the DIE for TYPE so that we can generate the
23235 die for MEMBER, which has been defined; we will need to refer back
23236 to the member declaration nested within TYPE. If we're trying to
23237 generate minimal debug info for TYPE, processing TYPE won't do the
23238 trick; we need to attach the member declaration by hand. */
23240 static void
23241 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
23243 gen_type_die (type, context_die);
23245 /* If we're trying to avoid duplicate debug info, we may not have
23246 emitted the member decl for this function. Emit it now. */
23247 if (TYPE_STUB_DECL (type)
23248 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
23249 && ! lookup_decl_die (member))
23251 dw_die_ref type_die;
23252 gcc_assert (!decl_ultimate_origin (member));
23254 type_die = lookup_type_die_strip_naming_typedef (type);
23255 if (TREE_CODE (member) == FUNCTION_DECL)
23256 gen_subprogram_die (member, type_die);
23257 else if (TREE_CODE (member) == FIELD_DECL)
23259 /* Ignore the nameless fields that are used to skip bits but handle
23260 C++ anonymous unions and structs. */
23261 if (DECL_NAME (member) != NULL_TREE
23262 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
23263 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
23265 struct vlr_context vlr_ctx = {
23266 DECL_CONTEXT (member), /* struct_type */
23267 NULL_TREE /* variant_part_offset */
23269 gen_type_die (member_declared_type (member), type_die);
23270 gen_field_die (member, &vlr_ctx, type_die);
23273 else
23274 gen_variable_die (member, NULL_TREE, type_die);
23278 /* Forward declare these functions, because they are mutually recursive
23279 with their set_block_* pairing functions. */
23280 static void set_decl_origin_self (tree);
23282 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23283 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23284 that it points to the node itself, thus indicating that the node is its
23285 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23286 the given node is NULL, recursively descend the decl/block tree which
23287 it is the root of, and for each other ..._DECL or BLOCK node contained
23288 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23289 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23290 values to point to themselves. */
23292 static void
23293 set_block_origin_self (tree stmt)
23295 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23297 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23300 tree local_decl;
23302 for (local_decl = BLOCK_VARS (stmt);
23303 local_decl != NULL_TREE;
23304 local_decl = DECL_CHAIN (local_decl))
23305 /* Do not recurse on nested functions since the inlining status
23306 of parent and child can be different as per the DWARF spec. */
23307 if (TREE_CODE (local_decl) != FUNCTION_DECL
23308 && !DECL_EXTERNAL (local_decl))
23309 set_decl_origin_self (local_decl);
23313 tree subblock;
23315 for (subblock = BLOCK_SUBBLOCKS (stmt);
23316 subblock != NULL_TREE;
23317 subblock = BLOCK_CHAIN (subblock))
23318 set_block_origin_self (subblock); /* Recurse. */
23323 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23324 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23325 node to so that it points to the node itself, thus indicating that the
23326 node represents its own (abstract) origin. Additionally, if the
23327 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23328 the decl/block tree of which the given node is the root of, and for
23329 each other ..._DECL or BLOCK node contained therein whose
23330 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23331 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23332 point to themselves. */
23334 static void
23335 set_decl_origin_self (tree decl)
23337 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23339 DECL_ABSTRACT_ORIGIN (decl) = decl;
23340 if (TREE_CODE (decl) == FUNCTION_DECL)
23342 tree arg;
23344 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23345 DECL_ABSTRACT_ORIGIN (arg) = arg;
23346 if (DECL_INITIAL (decl) != NULL_TREE
23347 && DECL_INITIAL (decl) != error_mark_node)
23348 set_block_origin_self (DECL_INITIAL (decl));
23353 /* Mark the early DIE for DECL as the abstract instance. */
23355 static void
23356 dwarf2out_abstract_function (tree decl)
23358 dw_die_ref old_die;
23360 /* Make sure we have the actual abstract inline, not a clone. */
23361 decl = DECL_ORIGIN (decl);
23363 if (DECL_IGNORED_P (decl))
23364 return;
23366 /* In LTO we're all set. We already created abstract instances
23367 early and we want to avoid creating a concrete instance of that
23368 if we don't output it. */
23369 if (in_lto_p)
23370 return;
23372 old_die = lookup_decl_die (decl);
23373 gcc_assert (old_die != NULL);
23374 if (get_AT (old_die, DW_AT_inline))
23375 /* We've already generated the abstract instance. */
23376 return;
23378 /* Go ahead and put DW_AT_inline on the DIE. */
23379 if (DECL_DECLARED_INLINE_P (decl))
23381 if (cgraph_function_possibly_inlined_p (decl))
23382 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23383 else
23384 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23386 else
23388 if (cgraph_function_possibly_inlined_p (decl))
23389 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23390 else
23391 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23394 if (DECL_DECLARED_INLINE_P (decl)
23395 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23396 add_AT_flag (old_die, DW_AT_artificial, 1);
23398 set_decl_origin_self (decl);
23401 /* Helper function of premark_used_types() which gets called through
23402 htab_traverse.
23404 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23405 marked as unused by prune_unused_types. */
23407 bool
23408 premark_used_types_helper (tree const &type, void *)
23410 dw_die_ref die;
23412 die = lookup_type_die (type);
23413 if (die != NULL)
23414 die->die_perennial_p = 1;
23415 return true;
23418 /* Helper function of premark_types_used_by_global_vars which gets called
23419 through htab_traverse.
23421 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23422 marked as unused by prune_unused_types. The DIE of the type is marked
23423 only if the global variable using the type will actually be emitted. */
23426 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23427 void *)
23429 struct types_used_by_vars_entry *entry;
23430 dw_die_ref die;
23432 entry = (struct types_used_by_vars_entry *) *slot;
23433 gcc_assert (entry->type != NULL
23434 && entry->var_decl != NULL);
23435 die = lookup_type_die (entry->type);
23436 if (die)
23438 /* Ask cgraph if the global variable really is to be emitted.
23439 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23440 varpool_node *node = varpool_node::get (entry->var_decl);
23441 if (node && node->definition)
23443 die->die_perennial_p = 1;
23444 /* Keep the parent DIEs as well. */
23445 while ((die = die->die_parent) && die->die_perennial_p == 0)
23446 die->die_perennial_p = 1;
23449 return 1;
23452 /* Mark all members of used_types_hash as perennial. */
23454 static void
23455 premark_used_types (struct function *fun)
23457 if (fun && fun->used_types_hash)
23458 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23461 /* Mark all members of types_used_by_vars_entry as perennial. */
23463 static void
23464 premark_types_used_by_global_vars (void)
23466 if (types_used_by_vars_hash)
23467 types_used_by_vars_hash
23468 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23471 /* Mark all variables used by the symtab as perennial. */
23473 static void
23474 premark_used_variables (void)
23476 /* Mark DIEs in the symtab as used. */
23477 varpool_node *var;
23478 FOR_EACH_VARIABLE (var)
23480 dw_die_ref die = lookup_decl_die (var->decl);
23481 if (die)
23482 die->die_perennial_p = 1;
23486 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23487 for CA_LOC call arg loc node. */
23489 static dw_die_ref
23490 gen_call_site_die (tree decl, dw_die_ref subr_die,
23491 struct call_arg_loc_node *ca_loc)
23493 dw_die_ref stmt_die = NULL, die;
23494 tree block = ca_loc->block;
23496 while (block
23497 && block != DECL_INITIAL (decl)
23498 && TREE_CODE (block) == BLOCK)
23500 stmt_die = lookup_block_die (block);
23501 if (stmt_die)
23502 break;
23503 block = BLOCK_SUPERCONTEXT (block);
23505 if (stmt_die == NULL)
23506 stmt_die = subr_die;
23507 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23508 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23509 if (ca_loc->tail_call_p)
23510 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23511 if (ca_loc->symbol_ref)
23513 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23514 if (tdie)
23515 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23516 else
23517 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23518 false);
23520 return die;
23523 /* Generate a DIE to represent a declared function (either file-scope or
23524 block-local). */
23526 static void
23527 gen_subprogram_die (tree decl, dw_die_ref context_die)
23529 tree origin = decl_ultimate_origin (decl);
23530 dw_die_ref subr_die;
23531 dw_die_ref old_die = lookup_decl_die (decl);
23532 bool old_die_had_no_children = false;
23534 /* This function gets called multiple times for different stages of
23535 the debug process. For example, for func() in this code:
23537 namespace S
23539 void func() { ... }
23542 ...we get called 4 times. Twice in early debug and twice in
23543 late debug:
23545 Early debug
23546 -----------
23548 1. Once while generating func() within the namespace. This is
23549 the declaration. The declaration bit below is set, as the
23550 context is the namespace.
23552 A new DIE will be generated with DW_AT_declaration set.
23554 2. Once for func() itself. This is the specification. The
23555 declaration bit below is clear as the context is the CU.
23557 We will use the cached DIE from (1) to create a new DIE with
23558 DW_AT_specification pointing to the declaration in (1).
23560 Late debug via rest_of_handle_final()
23561 -------------------------------------
23563 3. Once generating func() within the namespace. This is also the
23564 declaration, as in (1), but this time we will early exit below
23565 as we have a cached DIE and a declaration needs no additional
23566 annotations (no locations), as the source declaration line
23567 info is enough.
23569 4. Once for func() itself. As in (2), this is the specification,
23570 but this time we will re-use the cached DIE, and just annotate
23571 it with the location information that should now be available.
23573 For something without namespaces, but with abstract instances, we
23574 are also called a multiple times:
23576 class Base
23578 public:
23579 Base (); // constructor declaration (1)
23582 Base::Base () { } // constructor specification (2)
23584 Early debug
23585 -----------
23587 1. Once for the Base() constructor by virtue of it being a
23588 member of the Base class. This is done via
23589 rest_of_type_compilation.
23591 This is a declaration, so a new DIE will be created with
23592 DW_AT_declaration.
23594 2. Once for the Base() constructor definition, but this time
23595 while generating the abstract instance of the base
23596 constructor (__base_ctor) which is being generated via early
23597 debug of reachable functions.
23599 Even though we have a cached version of the declaration (1),
23600 we will create a DW_AT_specification of the declaration DIE
23601 in (1).
23603 3. Once for the __base_ctor itself, but this time, we generate
23604 an DW_AT_abstract_origin version of the DW_AT_specification in
23605 (2).
23607 Late debug via rest_of_handle_final
23608 -----------------------------------
23610 4. One final time for the __base_ctor (which will have a cached
23611 DIE with DW_AT_abstract_origin created in (3). This time,
23612 we will just annotate the location information now
23613 available.
23615 int declaration = (current_function_decl != decl
23616 || (!DECL_INITIAL (decl) && !origin)
23617 || class_or_namespace_scope_p (context_die));
23619 /* A declaration that has been previously dumped needs no
23620 additional information. */
23621 if (old_die && declaration)
23622 return;
23624 if (in_lto_p && old_die && old_die->die_child == NULL)
23625 old_die_had_no_children = true;
23627 /* Now that the C++ front end lazily declares artificial member fns, we
23628 might need to retrofit the declaration into its class. */
23629 if (!declaration && !origin && !old_die
23630 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23631 && !class_or_namespace_scope_p (context_die)
23632 && debug_info_level > DINFO_LEVEL_TERSE)
23633 old_die = force_decl_die (decl);
23635 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23636 if (origin != NULL)
23638 gcc_assert (!declaration || local_scope_p (context_die));
23640 /* Fixup die_parent for the abstract instance of a nested
23641 inline function. */
23642 if (old_die && old_die->die_parent == NULL)
23643 add_child_die (context_die, old_die);
23645 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23647 /* If we have a DW_AT_abstract_origin we have a working
23648 cached version. */
23649 subr_die = old_die;
23651 else
23653 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23654 add_abstract_origin_attribute (subr_die, origin);
23655 /* This is where the actual code for a cloned function is.
23656 Let's emit linkage name attribute for it. This helps
23657 debuggers to e.g, set breakpoints into
23658 constructors/destructors when the user asks "break
23659 K::K". */
23660 add_linkage_name (subr_die, decl);
23663 /* A cached copy, possibly from early dwarf generation. Reuse as
23664 much as possible. */
23665 else if (old_die)
23667 if (!get_AT_flag (old_die, DW_AT_declaration)
23668 /* We can have a normal definition following an inline one in the
23669 case of redefinition of GNU C extern inlines.
23670 It seems reasonable to use AT_specification in this case. */
23671 && !get_AT (old_die, DW_AT_inline))
23673 /* Detect and ignore this case, where we are trying to output
23674 something we have already output. */
23675 if (get_AT (old_die, DW_AT_low_pc)
23676 || get_AT (old_die, DW_AT_ranges))
23677 return;
23679 /* If we have no location information, this must be a
23680 partially generated DIE from early dwarf generation.
23681 Fall through and generate it. */
23684 /* If the definition comes from the same place as the declaration,
23685 maybe use the old DIE. We always want the DIE for this function
23686 that has the *_pc attributes to be under comp_unit_die so the
23687 debugger can find it. We also need to do this for abstract
23688 instances of inlines, since the spec requires the out-of-line copy
23689 to have the same parent. For local class methods, this doesn't
23690 apply; we just use the old DIE. */
23691 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23692 struct dwarf_file_data * file_index = lookup_filename (s.file);
23693 if (((is_unit_die (old_die->die_parent)
23694 /* This condition fixes the inconsistency/ICE with the
23695 following Fortran test (or some derivative thereof) while
23696 building libgfortran:
23698 module some_m
23699 contains
23700 logical function funky (FLAG)
23701 funky = .true.
23702 end function
23703 end module
23705 || (old_die->die_parent
23706 && old_die->die_parent->die_tag == DW_TAG_module)
23707 || local_scope_p (old_die->die_parent)
23708 || context_die == NULL)
23709 && (DECL_ARTIFICIAL (decl)
23710 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23711 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23712 == (unsigned) s.line)
23713 && (!debug_column_info
23714 || s.column == 0
23715 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23716 == (unsigned) s.column)))))
23717 /* With LTO if there's an abstract instance for
23718 the old DIE, this is a concrete instance and
23719 thus re-use the DIE. */
23720 || get_AT (old_die, DW_AT_abstract_origin))
23722 subr_die = old_die;
23724 /* Clear out the declaration attribute, but leave the
23725 parameters so they can be augmented with location
23726 information later. Unless this was a declaration, in
23727 which case, wipe out the nameless parameters and recreate
23728 them further down. */
23729 if (remove_AT (subr_die, DW_AT_declaration))
23732 remove_AT (subr_die, DW_AT_object_pointer);
23733 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23736 /* Make a specification pointing to the previously built
23737 declaration. */
23738 else
23740 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23741 add_AT_specification (subr_die, old_die);
23742 add_pubname (decl, subr_die);
23743 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23744 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23745 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23746 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23747 if (debug_column_info
23748 && s.column
23749 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23750 != (unsigned) s.column))
23751 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23753 /* If the prototype had an 'auto' or 'decltype(auto)' in
23754 the return type, emit the real type on the definition die. */
23755 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23757 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23758 while (die
23759 && (die->die_tag == DW_TAG_reference_type
23760 || die->die_tag == DW_TAG_rvalue_reference_type
23761 || die->die_tag == DW_TAG_pointer_type
23762 || die->die_tag == DW_TAG_const_type
23763 || die->die_tag == DW_TAG_volatile_type
23764 || die->die_tag == DW_TAG_restrict_type
23765 || die->die_tag == DW_TAG_array_type
23766 || die->die_tag == DW_TAG_ptr_to_member_type
23767 || die->die_tag == DW_TAG_subroutine_type))
23768 die = get_AT_ref (die, DW_AT_type);
23769 if (die == auto_die || die == decltype_auto_die)
23770 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23771 TYPE_UNQUALIFIED, false, context_die);
23774 /* When we process the method declaration, we haven't seen
23775 the out-of-class defaulted definition yet, so we have to
23776 recheck now. */
23777 if ((dwarf_version >= 5 || ! dwarf_strict)
23778 && !get_AT (subr_die, DW_AT_defaulted))
23780 int defaulted
23781 = lang_hooks.decls.decl_dwarf_attribute (decl,
23782 DW_AT_defaulted);
23783 if (defaulted != -1)
23785 /* Other values must have been handled before. */
23786 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23787 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23792 /* Create a fresh DIE for anything else. */
23793 else
23795 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23797 if (TREE_PUBLIC (decl))
23798 add_AT_flag (subr_die, DW_AT_external, 1);
23800 add_name_and_src_coords_attributes (subr_die, decl);
23801 add_pubname (decl, subr_die);
23802 if (debug_info_level > DINFO_LEVEL_TERSE)
23804 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23805 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23806 TYPE_UNQUALIFIED, false, context_die);
23809 add_pure_or_virtual_attribute (subr_die, decl);
23810 if (DECL_ARTIFICIAL (decl))
23811 add_AT_flag (subr_die, DW_AT_artificial, 1);
23813 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23814 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23816 add_alignment_attribute (subr_die, decl);
23818 add_accessibility_attribute (subr_die, decl);
23821 /* Unless we have an existing non-declaration DIE, equate the new
23822 DIE. */
23823 if (!old_die || is_declaration_die (old_die))
23824 equate_decl_number_to_die (decl, subr_die);
23826 if (declaration)
23828 if (!old_die || !get_AT (old_die, DW_AT_inline))
23830 add_AT_flag (subr_die, DW_AT_declaration, 1);
23832 /* If this is an explicit function declaration then generate
23833 a DW_AT_explicit attribute. */
23834 if ((dwarf_version >= 3 || !dwarf_strict)
23835 && lang_hooks.decls.decl_dwarf_attribute (decl,
23836 DW_AT_explicit) == 1)
23837 add_AT_flag (subr_die, DW_AT_explicit, 1);
23839 /* If this is a C++11 deleted special function member then generate
23840 a DW_AT_deleted attribute. */
23841 if ((dwarf_version >= 5 || !dwarf_strict)
23842 && lang_hooks.decls.decl_dwarf_attribute (decl,
23843 DW_AT_deleted) == 1)
23844 add_AT_flag (subr_die, DW_AT_deleted, 1);
23846 /* If this is a C++11 defaulted special function member then
23847 generate a DW_AT_defaulted attribute. */
23848 if (dwarf_version >= 5 || !dwarf_strict)
23850 int defaulted
23851 = lang_hooks.decls.decl_dwarf_attribute (decl,
23852 DW_AT_defaulted);
23853 if (defaulted != -1)
23854 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23857 /* If this is a C++11 non-static member function with & ref-qualifier
23858 then generate a DW_AT_reference attribute. */
23859 if ((dwarf_version >= 5 || !dwarf_strict)
23860 && lang_hooks.decls.decl_dwarf_attribute (decl,
23861 DW_AT_reference) == 1)
23862 add_AT_flag (subr_die, DW_AT_reference, 1);
23864 /* If this is a C++11 non-static member function with &&
23865 ref-qualifier then generate a DW_AT_reference attribute. */
23866 if ((dwarf_version >= 5 || !dwarf_strict)
23867 && lang_hooks.decls.decl_dwarf_attribute (decl,
23868 DW_AT_rvalue_reference)
23869 == 1)
23870 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23873 /* For non DECL_EXTERNALs, if range information is available, fill
23874 the DIE with it. */
23875 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23877 HOST_WIDE_INT cfa_fb_offset;
23879 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23881 if (!crtl->has_bb_partition)
23883 dw_fde_ref fde = fun->fde;
23884 if (fde->dw_fde_begin)
23886 /* We have already generated the labels. */
23887 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23888 fde->dw_fde_end, false);
23890 else
23892 /* Create start/end labels and add the range. */
23893 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23894 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23895 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23896 current_function_funcdef_no);
23897 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23898 current_function_funcdef_no);
23899 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23900 false);
23903 #if VMS_DEBUGGING_INFO
23904 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23905 Section 2.3 Prologue and Epilogue Attributes:
23906 When a breakpoint is set on entry to a function, it is generally
23907 desirable for execution to be suspended, not on the very first
23908 instruction of the function, but rather at a point after the
23909 function's frame has been set up, after any language defined local
23910 declaration processing has been completed, and before execution of
23911 the first statement of the function begins. Debuggers generally
23912 cannot properly determine where this point is. Similarly for a
23913 breakpoint set on exit from a function. The prologue and epilogue
23914 attributes allow a compiler to communicate the location(s) to use. */
23917 if (fde->dw_fde_vms_end_prologue)
23918 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23919 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23921 if (fde->dw_fde_vms_begin_epilogue)
23922 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23923 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23925 #endif
23928 else
23930 /* Generate pubnames entries for the split function code ranges. */
23931 dw_fde_ref fde = fun->fde;
23933 if (fde->dw_fde_second_begin)
23935 if (dwarf_version >= 3 || !dwarf_strict)
23937 /* We should use ranges for non-contiguous code section
23938 addresses. Use the actual code range for the initial
23939 section, since the HOT/COLD labels might precede an
23940 alignment offset. */
23941 bool range_list_added = false;
23942 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23943 fde->dw_fde_end, &range_list_added,
23944 false);
23945 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23946 fde->dw_fde_second_end,
23947 &range_list_added, false);
23948 if (range_list_added)
23949 add_ranges (NULL);
23951 else
23953 /* There is no real support in DW2 for this .. so we make
23954 a work-around. First, emit the pub name for the segment
23955 containing the function label. Then make and emit a
23956 simplified subprogram DIE for the second segment with the
23957 name pre-fixed by __hot/cold_sect_of_. We use the same
23958 linkage name for the second die so that gdb will find both
23959 sections when given "b foo". */
23960 const char *name = NULL;
23961 tree decl_name = DECL_NAME (decl);
23962 dw_die_ref seg_die;
23964 /* Do the 'primary' section. */
23965 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23966 fde->dw_fde_end, false);
23968 /* Build a minimal DIE for the secondary section. */
23969 seg_die = new_die (DW_TAG_subprogram,
23970 subr_die->die_parent, decl);
23972 if (TREE_PUBLIC (decl))
23973 add_AT_flag (seg_die, DW_AT_external, 1);
23975 if (decl_name != NULL
23976 && IDENTIFIER_POINTER (decl_name) != NULL)
23978 name = dwarf2_name (decl, 1);
23979 if (! DECL_ARTIFICIAL (decl))
23980 add_src_coords_attributes (seg_die, decl);
23982 add_linkage_name (seg_die, decl);
23984 gcc_assert (name != NULL);
23985 add_pure_or_virtual_attribute (seg_die, decl);
23986 if (DECL_ARTIFICIAL (decl))
23987 add_AT_flag (seg_die, DW_AT_artificial, 1);
23989 name = concat ("__second_sect_of_", name, NULL);
23990 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23991 fde->dw_fde_second_end, false);
23992 add_name_attribute (seg_die, name);
23993 if (want_pubnames ())
23994 add_pubname_string (name, seg_die);
23997 else
23998 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23999 false);
24002 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
24004 /* We define the "frame base" as the function's CFA. This is more
24005 convenient for several reasons: (1) It's stable across the prologue
24006 and epilogue, which makes it better than just a frame pointer,
24007 (2) With dwarf3, there exists a one-byte encoding that allows us
24008 to reference the .debug_frame data by proxy, but failing that,
24009 (3) We can at least reuse the code inspection and interpretation
24010 code that determines the CFA position at various points in the
24011 function. */
24012 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
24014 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
24015 add_AT_loc (subr_die, DW_AT_frame_base, op);
24017 else
24019 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
24020 if (list->dw_loc_next)
24021 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
24022 else
24023 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
24026 /* Compute a displacement from the "steady-state frame pointer" to
24027 the CFA. The former is what all stack slots and argument slots
24028 will reference in the rtl; the latter is what we've told the
24029 debugger about. We'll need to adjust all frame_base references
24030 by this displacement. */
24031 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
24033 if (fun->static_chain_decl)
24035 /* DWARF requires here a location expression that computes the
24036 address of the enclosing subprogram's frame base. The machinery
24037 in tree-nested.cc is supposed to store this specific address in the
24038 last field of the FRAME record. */
24039 const tree frame_type
24040 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
24041 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
24043 tree fb_expr
24044 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
24045 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
24046 fb_expr, fb_decl, NULL_TREE);
24048 add_AT_location_description (subr_die, DW_AT_static_link,
24049 loc_list_from_tree (fb_expr, 0, NULL));
24052 resolve_variable_values ();
24055 /* Generate child dies for template parameters. */
24056 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
24057 gen_generic_params_dies (decl);
24059 /* Now output descriptions of the arguments for this function. This gets
24060 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
24061 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
24062 `...' at the end of the formal parameter list. In order to find out if
24063 there was a trailing ellipsis or not, we must instead look at the type
24064 associated with the FUNCTION_DECL. This will be a node of type
24065 FUNCTION_TYPE. If the chain of type nodes hanging off of this
24066 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
24067 an ellipsis at the end. */
24069 /* In the case where we are describing a mere function declaration, all we
24070 need to do here (and all we *can* do here) is to describe the *types* of
24071 its formal parameters. */
24072 if (debug_info_level <= DINFO_LEVEL_TERSE)
24074 else if (declaration)
24075 gen_formal_types_die (decl, subr_die);
24076 else
24078 /* Generate DIEs to represent all known formal parameters. */
24079 tree parm = DECL_ARGUMENTS (decl);
24080 tree generic_decl = early_dwarf
24081 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
24082 tree generic_decl_parm = generic_decl
24083 ? DECL_ARGUMENTS (generic_decl)
24084 : NULL;
24086 /* Now we want to walk the list of parameters of the function and
24087 emit their relevant DIEs.
24089 We consider the case of DECL being an instance of a generic function
24090 as well as it being a normal function.
24092 If DECL is an instance of a generic function we walk the
24093 parameters of the generic function declaration _and_ the parameters of
24094 DECL itself. This is useful because we want to emit specific DIEs for
24095 function parameter packs and those are declared as part of the
24096 generic function declaration. In that particular case,
24097 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
24098 That DIE has children DIEs representing the set of arguments
24099 of the pack. Note that the set of pack arguments can be empty.
24100 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
24101 children DIE.
24103 Otherwise, we just consider the parameters of DECL. */
24104 while (generic_decl_parm || parm)
24106 if (generic_decl_parm
24107 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
24108 gen_formal_parameter_pack_die (generic_decl_parm,
24109 parm, subr_die,
24110 &parm);
24111 else if (parm)
24113 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
24115 if (early_dwarf
24116 && parm == DECL_ARGUMENTS (decl)
24117 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
24118 && parm_die
24119 && (dwarf_version >= 3 || !dwarf_strict))
24120 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
24122 parm = DECL_CHAIN (parm);
24125 if (generic_decl_parm)
24126 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
24129 /* Decide whether we need an unspecified_parameters DIE at the end.
24130 There are 2 more cases to do this for: 1) the ansi ... declaration -
24131 this is detectable when the end of the arg list is not a
24132 void_type_node 2) an unprototyped function declaration (not a
24133 definition). This just means that we have no info about the
24134 parameters at all. */
24135 if (early_dwarf)
24137 if (prototype_p (TREE_TYPE (decl)))
24139 /* This is the prototyped case, check for.... */
24140 if (stdarg_p (TREE_TYPE (decl)))
24141 gen_unspecified_parameters_die (decl, subr_die);
24143 else if (DECL_INITIAL (decl) == NULL_TREE)
24144 gen_unspecified_parameters_die (decl, subr_die);
24146 else if ((subr_die != old_die || old_die_had_no_children)
24147 && prototype_p (TREE_TYPE (decl))
24148 && stdarg_p (TREE_TYPE (decl)))
24149 gen_unspecified_parameters_die (decl, subr_die);
24152 if (subr_die != old_die)
24153 /* Add the calling convention attribute if requested. */
24154 add_calling_convention_attribute (subr_die, decl);
24156 /* Output Dwarf info for all of the stuff within the body of the function
24157 (if it has one - it may be just a declaration).
24159 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
24160 a function. This BLOCK actually represents the outermost binding contour
24161 for the function, i.e. the contour in which the function's formal
24162 parameters and labels get declared. Curiously, it appears that the front
24163 end doesn't actually put the PARM_DECL nodes for the current function onto
24164 the BLOCK_VARS list for this outer scope, but are strung off of the
24165 DECL_ARGUMENTS list for the function instead.
24167 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
24168 the LABEL_DECL nodes for the function however, and we output DWARF info
24169 for those in decls_for_scope. Just within the `outer_scope' there will be
24170 a BLOCK node representing the function's outermost pair of curly braces,
24171 and any blocks used for the base and member initializers of a C++
24172 constructor function. */
24173 tree outer_scope = DECL_INITIAL (decl);
24174 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
24176 int call_site_note_count = 0;
24177 int tail_call_site_note_count = 0;
24179 /* Emit a DW_TAG_variable DIE for a named return value. */
24180 if (DECL_NAME (DECL_RESULT (decl)))
24181 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
24183 /* The first time through decls_for_scope we will generate the
24184 DIEs for the locals. The second time, we fill in the
24185 location info. */
24186 decls_for_scope (outer_scope, subr_die);
24188 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
24190 struct call_arg_loc_node *ca_loc;
24191 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
24193 dw_die_ref die = NULL;
24194 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
24195 rtx arg, next_arg;
24196 tree arg_decl = NULL_TREE;
24198 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
24199 ? XEXP (ca_loc->call_arg_loc_note, 0)
24200 : NULL_RTX);
24201 arg; arg = next_arg)
24203 dw_loc_descr_ref reg, val;
24204 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
24205 dw_die_ref cdie, tdie = NULL;
24207 next_arg = XEXP (arg, 1);
24208 if (REG_P (XEXP (XEXP (arg, 0), 0))
24209 && next_arg
24210 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
24211 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
24212 && REGNO (XEXP (XEXP (arg, 0), 0))
24213 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
24214 next_arg = XEXP (next_arg, 1);
24215 if (mode == VOIDmode)
24217 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
24218 if (mode == VOIDmode)
24219 mode = GET_MODE (XEXP (arg, 0));
24221 if (mode == VOIDmode || mode == BLKmode)
24222 continue;
24223 /* Get dynamic information about call target only if we
24224 have no static information: we cannot generate both
24225 DW_AT_call_origin and DW_AT_call_target
24226 attributes. */
24227 if (ca_loc->symbol_ref == NULL_RTX)
24229 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
24231 tloc = XEXP (XEXP (arg, 0), 1);
24232 continue;
24234 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
24235 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
24237 tlocc = XEXP (XEXP (arg, 0), 1);
24238 continue;
24241 reg = NULL;
24242 if (REG_P (XEXP (XEXP (arg, 0), 0)))
24243 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
24244 VAR_INIT_STATUS_INITIALIZED);
24245 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
24247 rtx mem = XEXP (XEXP (arg, 0), 0);
24248 reg = mem_loc_descriptor (XEXP (mem, 0),
24249 get_address_mode (mem),
24250 GET_MODE (mem),
24251 VAR_INIT_STATUS_INITIALIZED);
24253 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
24254 == DEBUG_PARAMETER_REF)
24256 tree tdecl
24257 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
24258 tdie = lookup_decl_die (tdecl);
24259 if (tdie == NULL)
24260 continue;
24261 arg_decl = tdecl;
24263 else
24264 continue;
24265 if (reg == NULL
24266 && GET_CODE (XEXP (XEXP (arg, 0), 0))
24267 != DEBUG_PARAMETER_REF)
24268 continue;
24269 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
24270 VOIDmode,
24271 VAR_INIT_STATUS_INITIALIZED);
24272 if (val == NULL)
24273 continue;
24274 if (die == NULL)
24275 die = gen_call_site_die (decl, subr_die, ca_loc);
24276 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
24277 NULL_TREE);
24278 add_desc_attribute (cdie, arg_decl);
24279 if (reg != NULL)
24280 add_AT_loc (cdie, DW_AT_location, reg);
24281 else if (tdie != NULL)
24282 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24283 tdie);
24284 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24285 if (next_arg != XEXP (arg, 1))
24287 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24288 if (mode == VOIDmode)
24289 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24290 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24291 0), 1),
24292 mode, VOIDmode,
24293 VAR_INIT_STATUS_INITIALIZED);
24294 if (val != NULL)
24295 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24296 val);
24299 if (die == NULL
24300 && (ca_loc->symbol_ref || tloc))
24301 die = gen_call_site_die (decl, subr_die, ca_loc);
24302 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24304 dw_loc_descr_ref tval = NULL;
24306 if (tloc != NULL_RTX)
24307 tval = mem_loc_descriptor (tloc,
24308 GET_MODE (tloc) == VOIDmode
24309 ? Pmode : GET_MODE (tloc),
24310 VOIDmode,
24311 VAR_INIT_STATUS_INITIALIZED);
24312 if (tval)
24313 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24314 else if (tlocc != NULL_RTX)
24316 tval = mem_loc_descriptor (tlocc,
24317 GET_MODE (tlocc) == VOIDmode
24318 ? Pmode : GET_MODE (tlocc),
24319 VOIDmode,
24320 VAR_INIT_STATUS_INITIALIZED);
24321 if (tval)
24322 add_AT_loc (die,
24323 dwarf_AT (DW_AT_call_target_clobbered),
24324 tval);
24327 if (die != NULL)
24329 call_site_note_count++;
24330 if (ca_loc->tail_call_p)
24331 tail_call_site_note_count++;
24335 call_arg_locations = NULL;
24336 call_arg_loc_last = NULL;
24337 if (tail_call_site_count >= 0
24338 && tail_call_site_count == tail_call_site_note_count
24339 && (!dwarf_strict || dwarf_version >= 5))
24341 if (call_site_count >= 0
24342 && call_site_count == call_site_note_count)
24343 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24344 else
24345 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24347 call_site_count = -1;
24348 tail_call_site_count = -1;
24351 /* Mark used types after we have created DIEs for the functions scopes. */
24352 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24355 /* Returns a hash value for X (which really is a die_struct). */
24357 hashval_t
24358 block_die_hasher::hash (die_struct *d)
24360 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24363 /* Return true if decl_id and die_parent of die_struct X is the same
24364 as decl_id and die_parent of die_struct Y. */
24366 bool
24367 block_die_hasher::equal (die_struct *x, die_struct *y)
24369 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24372 /* Hold information about markers for inlined entry points. */
24373 struct GTY ((for_user)) inline_entry_data
24375 /* The block that's the inlined_function_outer_scope for an inlined
24376 function. */
24377 tree block;
24379 /* The label at the inlined entry point. */
24380 const char *label_pfx;
24381 unsigned int label_num;
24383 /* The view number to be used as the inlined entry point. */
24384 var_loc_view view;
24387 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24389 typedef tree compare_type;
24390 static inline hashval_t hash (const inline_entry_data *);
24391 static inline bool equal (const inline_entry_data *, const_tree);
24394 /* Hash table routines for inline_entry_data. */
24396 inline hashval_t
24397 inline_entry_data_hasher::hash (const inline_entry_data *data)
24399 return htab_hash_pointer (data->block);
24402 inline bool
24403 inline_entry_data_hasher::equal (const inline_entry_data *data,
24404 const_tree block)
24406 return data->block == block;
24409 /* Inlined entry points pending DIE creation in this compilation unit. */
24411 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24414 /* Return TRUE if DECL, which may have been previously generated as
24415 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24416 true if decl (or its origin) is either an extern declaration or a
24417 class/namespace scoped declaration.
24419 The declare_in_namespace support causes us to get two DIEs for one
24420 variable, both of which are declarations. We want to avoid
24421 considering one to be a specification, so we must test for
24422 DECLARATION and DW_AT_declaration. */
24423 static inline bool
24424 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24426 return (old_die && TREE_STATIC (decl) && !declaration
24427 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24430 /* Return true if DECL is a local static. */
24432 static inline bool
24433 local_function_static (tree decl)
24435 gcc_assert (VAR_P (decl));
24436 return TREE_STATIC (decl)
24437 && DECL_CONTEXT (decl)
24438 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24441 /* Return true iff DECL overrides (presumably completes) the type of
24442 OLD_DIE within CONTEXT_DIE. */
24444 static bool
24445 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24446 dw_die_ref context_die)
24448 tree type = TREE_TYPE (decl);
24449 int cv_quals;
24451 if (decl_by_reference_p (decl))
24453 type = TREE_TYPE (type);
24454 cv_quals = TYPE_UNQUALIFIED;
24456 else
24457 cv_quals = decl_quals (decl);
24459 dw_die_ref type_die = modified_type_die (type,
24460 cv_quals | TYPE_QUALS (type),
24461 false,
24462 context_die);
24464 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24466 return type_die != old_type_die;
24469 /* Generate a DIE to represent a declared data object.
24470 Either DECL or ORIGIN must be non-null. */
24472 static void
24473 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24475 HOST_WIDE_INT off = 0;
24476 tree com_decl;
24477 tree decl_or_origin = decl ? decl : origin;
24478 tree ultimate_origin;
24479 dw_die_ref var_die;
24480 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24481 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24482 || class_or_namespace_scope_p (context_die));
24483 bool specialization_p = false;
24484 bool no_linkage_name = false;
24486 /* While C++ inline static data members have definitions inside of the
24487 class, force the first DIE to be a declaration, then let gen_member_die
24488 reparent it to the class context and call gen_variable_die again
24489 to create the outside of the class DIE for the definition. */
24490 if (!declaration
24491 && old_die == NULL
24492 && decl
24493 && DECL_CONTEXT (decl)
24494 && TYPE_P (DECL_CONTEXT (decl))
24495 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24497 declaration = true;
24498 if (dwarf_version < 5)
24499 no_linkage_name = true;
24502 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24503 if (decl || ultimate_origin)
24504 origin = ultimate_origin;
24505 com_decl = fortran_common (decl_or_origin, &off);
24507 /* Symbol in common gets emitted as a child of the common block, in the form
24508 of a data member. */
24509 if (com_decl)
24511 dw_die_ref com_die;
24512 dw_loc_list_ref loc = NULL;
24513 die_node com_die_arg;
24515 var_die = lookup_decl_die (decl_or_origin);
24516 if (var_die)
24518 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24520 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24521 if (loc)
24523 if (off)
24525 /* Optimize the common case. */
24526 if (single_element_loc_list_p (loc)
24527 && loc->expr->dw_loc_opc == DW_OP_addr
24528 && loc->expr->dw_loc_next == NULL
24529 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24530 == SYMBOL_REF)
24532 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24533 loc->expr->dw_loc_oprnd1.v.val_addr
24534 = plus_constant (GET_MODE (x), x , off);
24536 else
24537 loc_list_plus_const (loc, off);
24539 add_AT_location_description (var_die, DW_AT_location, loc);
24540 remove_AT (var_die, DW_AT_declaration);
24543 return;
24546 if (common_block_die_table == NULL)
24547 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24549 com_die_arg.decl_id = DECL_UID (com_decl);
24550 com_die_arg.die_parent = context_die;
24551 com_die = common_block_die_table->find (&com_die_arg);
24552 if (! early_dwarf)
24553 loc = loc_list_from_tree (com_decl, 2, NULL);
24554 if (com_die == NULL)
24556 const char *cnam
24557 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24558 die_node **slot;
24560 com_die = new_die (DW_TAG_common_block, context_die, decl);
24561 add_name_and_src_coords_attributes (com_die, com_decl);
24562 if (loc)
24564 add_AT_location_description (com_die, DW_AT_location, loc);
24565 /* Avoid sharing the same loc descriptor between
24566 DW_TAG_common_block and DW_TAG_variable. */
24567 loc = loc_list_from_tree (com_decl, 2, NULL);
24569 else if (DECL_EXTERNAL (decl_or_origin))
24570 add_AT_flag (com_die, DW_AT_declaration, 1);
24571 if (want_pubnames ())
24572 add_pubname_string (cnam, com_die); /* ??? needed? */
24573 com_die->decl_id = DECL_UID (com_decl);
24574 slot = common_block_die_table->find_slot (com_die, INSERT);
24575 *slot = com_die;
24577 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24579 add_AT_location_description (com_die, DW_AT_location, loc);
24580 loc = loc_list_from_tree (com_decl, 2, NULL);
24581 remove_AT (com_die, DW_AT_declaration);
24583 var_die = new_die (DW_TAG_variable, com_die, decl);
24584 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24585 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24586 decl_quals (decl_or_origin), false,
24587 context_die);
24588 add_alignment_attribute (var_die, decl);
24589 add_AT_flag (var_die, DW_AT_external, 1);
24590 if (loc)
24592 if (off)
24594 /* Optimize the common case. */
24595 if (single_element_loc_list_p (loc)
24596 && loc->expr->dw_loc_opc == DW_OP_addr
24597 && loc->expr->dw_loc_next == NULL
24598 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24600 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24601 loc->expr->dw_loc_oprnd1.v.val_addr
24602 = plus_constant (GET_MODE (x), x, off);
24604 else
24605 loc_list_plus_const (loc, off);
24607 add_AT_location_description (var_die, DW_AT_location, loc);
24609 else if (DECL_EXTERNAL (decl_or_origin))
24610 add_AT_flag (var_die, DW_AT_declaration, 1);
24611 if (decl)
24612 equate_decl_number_to_die (decl, var_die);
24613 return;
24616 if (old_die)
24618 if (declaration)
24620 /* A declaration that has been previously dumped, needs no
24621 further annotations, since it doesn't need location on
24622 the second pass. */
24623 return;
24625 else if (decl_will_get_specification_p (old_die, decl, declaration)
24626 && !get_AT (old_die, DW_AT_specification))
24628 /* Fall-thru so we can make a new variable die along with a
24629 DW_AT_specification. */
24631 else if (origin && old_die->die_parent != context_die)
24633 /* If we will be creating an inlined instance, we need a
24634 new DIE that will get annotated with
24635 DW_AT_abstract_origin. */
24636 gcc_assert (!DECL_ABSTRACT_P (decl));
24638 else
24640 /* If a DIE was dumped early, it still needs location info.
24641 Skip to where we fill the location bits. */
24642 var_die = old_die;
24644 /* ??? In LTRANS we cannot annotate early created variably
24645 modified type DIEs without copying them and adjusting all
24646 references to them. Thus we dumped them again. Also add a
24647 reference to them but beware of -g0 compile and -g link
24648 in which case the reference will be already present. */
24649 tree type = TREE_TYPE (decl_or_origin);
24650 if (in_lto_p
24651 && ! get_AT (var_die, DW_AT_type)
24652 && variably_modified_type_p
24653 (type, decl_function_context (decl_or_origin)))
24655 if (decl_by_reference_p (decl_or_origin))
24656 add_type_attribute (var_die, TREE_TYPE (type),
24657 TYPE_UNQUALIFIED, false, context_die);
24658 else
24659 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24660 false, context_die);
24663 goto gen_variable_die_location;
24667 /* For static data members, the declaration in the class is supposed
24668 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24669 also in DWARF2; the specification should still be DW_TAG_variable
24670 referencing the DW_TAG_member DIE. */
24671 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24672 var_die = new_die (DW_TAG_member, context_die, decl);
24673 else
24674 var_die = new_die (DW_TAG_variable, context_die, decl);
24676 if (origin != NULL)
24677 add_abstract_origin_attribute (var_die, origin);
24679 /* Loop unrolling can create multiple blocks that refer to the same
24680 static variable, so we must test for the DW_AT_declaration flag.
24682 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24683 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24684 sharing them.
24686 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24687 else if (decl_will_get_specification_p (old_die, decl, declaration))
24689 /* This is a definition of a C++ class level static. */
24690 add_AT_specification (var_die, old_die);
24691 specialization_p = true;
24692 if (DECL_NAME (decl))
24694 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24695 struct dwarf_file_data * file_index = lookup_filename (s.file);
24697 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24698 add_AT_file (var_die, DW_AT_decl_file, file_index);
24700 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24701 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24703 if (debug_column_info
24704 && s.column
24705 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24706 != (unsigned) s.column))
24707 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24709 if (old_die->die_tag == DW_TAG_member)
24710 add_linkage_name (var_die, decl);
24713 else
24714 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24716 if ((origin == NULL && !specialization_p)
24717 || (origin != NULL
24718 && !DECL_ABSTRACT_P (decl_or_origin)
24719 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24720 decl_function_context
24721 (decl_or_origin)))
24722 || (old_die && specialization_p
24723 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24725 tree type = TREE_TYPE (decl_or_origin);
24727 if (decl_by_reference_p (decl_or_origin))
24728 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24729 context_die);
24730 else
24731 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24732 context_die);
24735 if (origin == NULL && !specialization_p)
24737 if (TREE_PUBLIC (decl))
24738 add_AT_flag (var_die, DW_AT_external, 1);
24740 if (DECL_ARTIFICIAL (decl))
24741 add_AT_flag (var_die, DW_AT_artificial, 1);
24743 add_alignment_attribute (var_die, decl);
24745 add_accessibility_attribute (var_die, decl);
24748 if (declaration)
24749 add_AT_flag (var_die, DW_AT_declaration, 1);
24751 if (decl && (DECL_ABSTRACT_P (decl)
24752 || !old_die || is_declaration_die (old_die)))
24753 equate_decl_number_to_die (decl, var_die);
24755 gen_variable_die_location:
24756 if (! declaration
24757 && (! DECL_ABSTRACT_P (decl_or_origin)
24758 /* Local static vars are shared between all clones/inlines,
24759 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24760 already set. */
24761 || (VAR_P (decl_or_origin)
24762 && TREE_STATIC (decl_or_origin)
24763 && DECL_RTL_SET_P (decl_or_origin))))
24765 if (early_dwarf)
24767 add_pubname (decl_or_origin, var_die);
24768 /* For global register variables, emit DW_AT_location if possible
24769 already during early_dwarf, as late_global_decl won't be usually
24770 called. */
24771 if (DECL_HARD_REGISTER (decl_or_origin)
24772 && TREE_STATIC (decl_or_origin)
24773 && !decl_by_reference_p (decl_or_origin)
24774 && !get_AT (var_die, DW_AT_location)
24775 && !get_AT (var_die, DW_AT_const_value)
24776 && DECL_RTL_SET_P (decl_or_origin)
24777 && REG_P (DECL_RTL (decl_or_origin)))
24779 dw_loc_descr_ref descr
24780 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24781 VAR_INIT_STATUS_INITIALIZED);
24782 if (descr)
24783 add_AT_loc (var_die, DW_AT_location, descr);
24786 else
24787 add_location_or_const_value_attribute (var_die, decl_or_origin,
24788 decl == NULL);
24790 else
24791 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24793 if ((dwarf_version >= 4 || !dwarf_strict)
24794 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24795 DW_AT_const_expr) == 1
24796 && !get_AT (var_die, DW_AT_const_expr)
24797 && !specialization_p)
24798 add_AT_flag (var_die, DW_AT_const_expr, 1);
24800 if (!dwarf_strict)
24802 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24803 DW_AT_inline);
24804 if (inl != -1
24805 && !get_AT (var_die, DW_AT_inline)
24806 && !specialization_p)
24807 add_AT_unsigned (var_die, DW_AT_inline, inl);
24811 /* Generate a DIE to represent a named constant. */
24813 static void
24814 gen_const_die (tree decl, dw_die_ref context_die)
24816 dw_die_ref const_die;
24817 tree type = TREE_TYPE (decl);
24819 const_die = lookup_decl_die (decl);
24820 if (const_die)
24821 return;
24823 const_die = new_die (DW_TAG_constant, context_die, decl);
24824 equate_decl_number_to_die (decl, const_die);
24825 add_name_and_src_coords_attributes (const_die, decl);
24826 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24827 if (TREE_PUBLIC (decl))
24828 add_AT_flag (const_die, DW_AT_external, 1);
24829 if (DECL_ARTIFICIAL (decl))
24830 add_AT_flag (const_die, DW_AT_artificial, 1);
24831 tree_add_const_value_attribute_for_decl (const_die, decl);
24834 /* Generate a DIE to represent a label identifier. */
24836 static void
24837 gen_label_die (tree decl, dw_die_ref context_die)
24839 tree origin = decl_ultimate_origin (decl);
24840 dw_die_ref lbl_die = lookup_decl_die (decl);
24841 rtx insn;
24842 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24844 if (!lbl_die)
24846 lbl_die = new_die (DW_TAG_label, context_die, decl);
24847 equate_decl_number_to_die (decl, lbl_die);
24849 if (origin != NULL)
24850 add_abstract_origin_attribute (lbl_die, origin);
24851 else
24852 add_name_and_src_coords_attributes (lbl_die, decl);
24855 if (DECL_ABSTRACT_P (decl))
24856 equate_decl_number_to_die (decl, lbl_die);
24857 else if (! early_dwarf)
24859 insn = DECL_RTL_IF_SET (decl);
24861 /* Deleted labels are programmer specified labels which have been
24862 eliminated because of various optimizations. We still emit them
24863 here so that it is possible to put breakpoints on them. */
24864 if (insn
24865 && (LABEL_P (insn)
24866 || ((NOTE_P (insn)
24867 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24869 /* When optimization is enabled (via -O) some parts of the compiler
24870 (e.g. jump.cc and cse.cc) may try to delete CODE_LABEL insns which
24871 represent source-level labels which were explicitly declared by
24872 the user. This really shouldn't be happening though, so catch
24873 it if it ever does happen. */
24874 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24876 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24877 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24879 else if (insn
24880 && NOTE_P (insn)
24881 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24882 && CODE_LABEL_NUMBER (insn) != -1)
24884 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24885 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24890 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24891 attributes to the DIE for a block STMT, to describe where the inlined
24892 function was called from. This is similar to add_src_coords_attributes. */
24894 static inline void
24895 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24897 /* We can end up with BUILTINS_LOCATION here. */
24898 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24899 return;
24901 location_t locus = BLOCK_SOURCE_LOCATION (stmt);
24902 expanded_location s = expand_location (locus);
24904 if (dwarf_version >= 3 || !dwarf_strict)
24906 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24907 add_AT_unsigned (die, DW_AT_call_line, s.line);
24908 if (debug_column_info && s.column)
24909 add_AT_unsigned (die, DW_AT_call_column, s.column);
24910 unsigned discr = get_discriminator_from_loc (locus);
24911 if (discr != 0)
24912 add_AT_unsigned (die, DW_AT_GNU_discriminator, discr);
24917 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24918 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24920 static inline void
24921 add_high_low_attributes (tree stmt, dw_die_ref die)
24923 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24925 if (inline_entry_data **iedp
24926 = !inline_entry_data_table ? NULL
24927 : inline_entry_data_table->find_slot_with_hash (stmt,
24928 htab_hash_pointer (stmt),
24929 NO_INSERT))
24931 inline_entry_data *ied = *iedp;
24932 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24933 gcc_assert (debug_inline_points);
24934 gcc_assert (inlined_function_outer_scope_p (stmt));
24936 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24937 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24939 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24940 && !dwarf_strict)
24942 if (!output_asm_line_debug_info ())
24943 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24944 else
24946 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24947 /* FIXME: this will resolve to a small number. Could we
24948 possibly emit smaller data? Ideally we'd emit a
24949 uleb128, but that would make the size of DIEs
24950 impossible for the compiler to compute, since it's
24951 the assembler that computes the value of the view
24952 label in this case. Ideally, we'd have a single form
24953 encompassing both the address and the view, and
24954 indirecting them through a table might make things
24955 easier, but even that would be more wasteful,
24956 space-wise, than what we have now. */
24957 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24961 inline_entry_data_table->clear_slot (iedp);
24964 if (BLOCK_FRAGMENT_CHAIN (stmt)
24965 && (dwarf_version >= 3 || !dwarf_strict))
24967 tree chain, superblock = NULL_TREE;
24968 dw_die_ref pdie;
24969 dw_attr_node *attr = NULL;
24971 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24973 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24974 BLOCK_NUMBER (stmt));
24975 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24978 /* Optimize duplicate .debug_ranges lists or even tails of
24979 lists. If this BLOCK has same ranges as its supercontext,
24980 lookup DW_AT_ranges attribute in the supercontext (and
24981 recursively so), verify that the ranges_table contains the
24982 right values and use it instead of adding a new .debug_range. */
24983 for (chain = stmt, pdie = die;
24984 BLOCK_SAME_RANGE (chain);
24985 chain = BLOCK_SUPERCONTEXT (chain))
24987 dw_attr_node *new_attr;
24989 pdie = pdie->die_parent;
24990 if (pdie == NULL)
24991 break;
24992 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24993 break;
24994 new_attr = get_AT (pdie, DW_AT_ranges);
24995 if (new_attr == NULL
24996 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24997 break;
24998 attr = new_attr;
24999 superblock = BLOCK_SUPERCONTEXT (chain);
25001 if (attr != NULL
25002 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
25003 == (int)BLOCK_NUMBER (superblock))
25004 && BLOCK_FRAGMENT_CHAIN (superblock))
25006 unsigned long off = attr->dw_attr_val.v.val_offset;
25007 unsigned long supercnt = 0, thiscnt = 0;
25008 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
25009 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
25011 ++supercnt;
25012 gcc_checking_assert ((*ranges_table)[off + supercnt].num
25013 == (int)BLOCK_NUMBER (chain));
25015 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
25016 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
25017 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
25018 ++thiscnt;
25019 gcc_assert (supercnt >= thiscnt);
25020 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
25021 false);
25022 note_rnglist_head (off + supercnt - thiscnt);
25023 return;
25026 unsigned int offset = add_ranges (stmt, true);
25027 add_AT_range_list (die, DW_AT_ranges, offset, false);
25028 note_rnglist_head (offset);
25030 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
25031 chain = BLOCK_FRAGMENT_CHAIN (stmt);
25034 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
25035 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
25036 chain = BLOCK_FRAGMENT_CHAIN (chain);
25038 while (chain);
25039 add_ranges (NULL);
25041 else
25043 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
25044 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
25045 BLOCK_NUMBER (stmt));
25046 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
25047 BLOCK_NUMBER (stmt));
25048 add_AT_low_high_pc (die, label, label_high, false);
25052 /* Generate a DIE for a lexical block. */
25054 static void
25055 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
25057 dw_die_ref old_die = lookup_block_die (stmt);
25058 dw_die_ref stmt_die = NULL;
25059 if (!old_die)
25061 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
25062 equate_block_to_die (stmt, stmt_die);
25065 if (BLOCK_ABSTRACT_ORIGIN (stmt))
25067 /* If this is an inlined or conrecte instance, create a new lexical
25068 die for anything below to attach DW_AT_abstract_origin to. */
25069 if (old_die)
25070 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
25072 tree origin = block_ultimate_origin (stmt);
25073 if (origin != NULL_TREE && (origin != stmt || old_die))
25074 add_abstract_origin_attribute (stmt_die, origin);
25076 old_die = NULL;
25079 if (old_die)
25080 stmt_die = old_die;
25082 /* A non abstract block whose blocks have already been reordered
25083 should have the instruction range for this block. If so, set the
25084 high/low attributes. */
25085 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
25087 gcc_assert (stmt_die);
25088 add_high_low_attributes (stmt, stmt_die);
25091 decls_for_scope (stmt, stmt_die);
25094 /* Generate a DIE for an inlined subprogram. */
25096 static void
25097 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
25099 tree decl = block_ultimate_origin (stmt);
25101 /* Make sure any inlined functions are known to be inlineable. */
25102 gcc_checking_assert (DECL_ABSTRACT_P (decl)
25103 || cgraph_function_possibly_inlined_p (decl));
25105 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
25107 if (call_arg_locations || debug_inline_points)
25108 equate_block_to_die (stmt, subr_die);
25109 add_abstract_origin_attribute (subr_die, decl);
25110 if (TREE_ASM_WRITTEN (stmt))
25111 add_high_low_attributes (stmt, subr_die);
25112 add_call_src_coords_attributes (stmt, subr_die);
25114 /* The inliner creates an extra BLOCK for the parameter setup,
25115 we want to merge that with the actual outermost BLOCK of the
25116 inlined function to avoid duplicate locals in consumers.
25117 Do that by doing the recursion to subblocks on the single subblock
25118 of STMT. */
25119 bool unwrap_one = false;
25120 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
25122 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
25123 if (origin
25124 && TREE_CODE (origin) == BLOCK
25125 && BLOCK_SUPERCONTEXT (origin) == decl)
25126 unwrap_one = true;
25128 decls_for_scope (stmt, subr_die, !unwrap_one);
25129 if (unwrap_one)
25130 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
25133 /* Generate a DIE for a field in a record, or structure. CTX is required: see
25134 the comment for VLR_CONTEXT. */
25136 static void
25137 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
25139 dw_die_ref decl_die;
25141 if (TREE_TYPE (decl) == error_mark_node)
25142 return;
25144 decl_die = new_die (DW_TAG_member, context_die, decl);
25145 add_name_and_src_coords_attributes (decl_die, decl);
25146 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
25147 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
25148 context_die);
25150 if (DECL_BIT_FIELD_TYPE (decl))
25152 add_byte_size_attribute (decl_die, decl);
25153 add_bit_size_attribute (decl_die, decl);
25154 add_bit_offset_attribute (decl_die, decl);
25157 add_alignment_attribute (decl_die, decl);
25159 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
25160 add_data_member_location_attribute (decl_die, decl, ctx);
25162 if (DECL_ARTIFICIAL (decl))
25163 add_AT_flag (decl_die, DW_AT_artificial, 1);
25165 add_accessibility_attribute (decl_die, decl);
25167 /* Add DW_AT_export_symbols to anonymous unions or structs. */
25168 if ((dwarf_version >= 5 || !dwarf_strict) && DECL_NAME (decl) == NULL_TREE)
25169 if (tree type = member_declared_type (decl))
25170 if (lang_hooks.types.type_dwarf_attribute (TYPE_MAIN_VARIANT (type),
25171 DW_AT_export_symbols) != -1)
25173 dw_die_ref type_die = lookup_type_die (TYPE_MAIN_VARIANT (type));
25174 if (type_die && get_AT (type_die, DW_AT_export_symbols) == NULL)
25175 add_AT_flag (type_die, DW_AT_export_symbols, 1);
25178 /* Equate decl number to die, so that we can look up this decl later on. */
25179 equate_decl_number_to_die (decl, decl_die);
25182 /* Generate a DIE for a pointer to a member type. TYPE can be an
25183 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
25184 pointer to member function. */
25186 static void
25187 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
25189 if (lookup_type_die (type))
25190 return;
25192 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
25193 scope_die_for (type, context_die), type);
25195 equate_type_number_to_die (type, ptr_die);
25196 add_AT_die_ref (ptr_die, DW_AT_containing_type,
25197 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
25198 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
25199 context_die);
25200 add_alignment_attribute (ptr_die, type);
25202 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
25203 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
25205 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
25206 add_AT_loc (ptr_die, DW_AT_use_location, op);
25210 static char *producer_string;
25212 /* Given a C and/or C++ language/version string return the "highest".
25213 C++ is assumed to be "higher" than C in this case. Used for merging
25214 LTO translation unit languages. */
25215 static const char *
25216 highest_c_language (const char *lang1, const char *lang2)
25218 if (strcmp ("GNU C++26", lang1) == 0 || strcmp ("GNU C++26", lang2) == 0)
25219 return "GNU C++26";
25220 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
25221 return "GNU C++23";
25222 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
25223 return "GNU C++20";
25224 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
25225 return "GNU C++17";
25226 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
25227 return "GNU C++14";
25228 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
25229 return "GNU C++11";
25230 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
25231 return "GNU C++98";
25233 if (strcmp ("GNU C23", lang1) == 0 || strcmp ("GNU C23", lang2) == 0)
25234 return "GNU C23";
25235 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
25236 return "GNU C17";
25237 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
25238 return "GNU C11";
25239 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
25240 return "GNU C99";
25241 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
25242 return "GNU C89";
25244 gcc_unreachable ();
25248 /* Generate the DIE for the compilation unit. */
25250 static dw_die_ref
25251 gen_compile_unit_die (const char *filename)
25253 dw_die_ref die;
25254 const char *language_string = lang_hooks.name;
25255 int language;
25257 die = new_die (DW_TAG_compile_unit, NULL, NULL);
25259 if (filename)
25261 add_filename_attribute (die, filename);
25262 /* Don't add cwd for <built-in>. */
25263 if (filename[0] != '<')
25264 add_comp_dir_attribute (die);
25267 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
25269 /* If our producer is LTO try to figure out a common language to use
25270 from the global list of translation units. */
25271 if (strcmp (language_string, "GNU GIMPLE") == 0)
25273 unsigned i;
25274 tree t;
25275 const char *common_lang = NULL;
25277 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
25279 if (!TRANSLATION_UNIT_LANGUAGE (t))
25280 continue;
25281 if (!common_lang)
25282 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
25283 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
25285 else if (startswith (common_lang, "GNU C")
25286 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
25287 /* Mixing C and C++ is ok, use C++ in that case. */
25288 common_lang = highest_c_language (common_lang,
25289 TRANSLATION_UNIT_LANGUAGE (t));
25290 else
25292 /* Fall back to C. */
25293 common_lang = NULL;
25294 break;
25298 if (common_lang)
25299 language_string = common_lang;
25302 language = DW_LANG_C;
25303 if (startswith (language_string, "GNU C")
25304 && ISDIGIT (language_string[5]))
25306 language = DW_LANG_C89;
25307 if (dwarf_version >= 3 || !dwarf_strict)
25309 if (strcmp (language_string, "GNU C89") != 0)
25310 language = DW_LANG_C99;
25312 if (dwarf_version >= 5 /* || !dwarf_strict */)
25313 if (strcmp (language_string, "GNU C11") == 0
25314 || strcmp (language_string, "GNU C17") == 0
25315 || strcmp (language_string, "GNU C23") == 0)
25316 language = DW_LANG_C11;
25319 else if (startswith (language_string, "GNU C++"))
25321 language = DW_LANG_C_plus_plus;
25322 if (dwarf_version >= 5 /* || !dwarf_strict */)
25324 if (strcmp (language_string, "GNU C++11") == 0)
25325 language = DW_LANG_C_plus_plus_11;
25326 else if (strcmp (language_string, "GNU C++14") == 0)
25327 language = DW_LANG_C_plus_plus_14;
25328 else if (strcmp (language_string, "GNU C++17") == 0
25329 || strcmp (language_string, "GNU C++20") == 0
25330 || strcmp (language_string, "GNU C++23") == 0
25331 || strcmp (language_string, "GNU C++26") == 0)
25332 /* For now. */
25333 language = DW_LANG_C_plus_plus_14;
25336 else if (strcmp (language_string, "GNU F77") == 0)
25337 language = DW_LANG_Fortran77;
25338 else if (strcmp (language_string, "GNU Modula-2") == 0)
25339 language = DW_LANG_Modula2;
25340 else if (dwarf_version >= 3 || !dwarf_strict)
25342 if (strcmp (language_string, "GNU Ada") == 0)
25343 language = DW_LANG_Ada95;
25344 else if (startswith (language_string, "GNU Fortran"))
25346 language = DW_LANG_Fortran95;
25347 if (dwarf_version >= 5 /* || !dwarf_strict */)
25349 if (strcmp (language_string, "GNU Fortran2003") == 0)
25350 language = DW_LANG_Fortran03;
25351 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25352 language = DW_LANG_Fortran08;
25355 else if (strcmp (language_string, "GNU Objective-C") == 0)
25356 language = DW_LANG_ObjC;
25357 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25358 language = DW_LANG_ObjC_plus_plus;
25359 else if (strcmp (language_string, "GNU D") == 0)
25360 language = DW_LANG_D;
25361 else if (dwarf_version >= 5 || !dwarf_strict)
25363 if (strcmp (language_string, "GNU Go") == 0)
25364 language = DW_LANG_Go;
25365 else if (strcmp (language_string, "GNU Rust") == 0)
25366 language = DW_LANG_Rust;
25369 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25370 else if (startswith (language_string, "GNU Fortran"))
25371 language = DW_LANG_Fortran90;
25372 /* Likewise for Ada. */
25373 else if (strcmp (language_string, "GNU Ada") == 0)
25374 language = DW_LANG_Ada83;
25376 add_AT_unsigned (die, DW_AT_language, language);
25378 switch (language)
25380 case DW_LANG_Fortran77:
25381 case DW_LANG_Fortran90:
25382 case DW_LANG_Fortran95:
25383 case DW_LANG_Fortran03:
25384 case DW_LANG_Fortran08:
25385 /* Fortran has case insensitive identifiers and the front-end
25386 lowercases everything. */
25387 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25388 break;
25389 default:
25390 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25391 break;
25393 return die;
25396 /* Generate the DIE for a base class. */
25398 static void
25399 gen_inheritance_die (tree binfo, tree access, tree type,
25400 dw_die_ref context_die)
25402 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25403 struct vlr_context ctx = { type, NULL };
25405 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25406 context_die);
25407 add_data_member_location_attribute (die, binfo, &ctx);
25409 if (BINFO_VIRTUAL_P (binfo))
25410 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25412 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25413 children, otherwise the default is DW_ACCESS_public. In DWARF2
25414 the default has always been DW_ACCESS_private. */
25415 if (access == access_public_node)
25417 if (dwarf_version == 2
25418 || context_die->die_tag == DW_TAG_class_type)
25419 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25421 else if (access == access_protected_node)
25422 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25423 else if (dwarf_version > 2
25424 && context_die->die_tag != DW_TAG_class_type)
25425 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25428 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25429 structure. */
25431 static bool
25432 is_variant_part (tree decl)
25434 return (TREE_CODE (decl) == FIELD_DECL
25435 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25438 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25439 return the FIELD_DECL. Return NULL_TREE otherwise. */
25441 static tree
25442 analyze_discr_in_predicate (tree operand, tree struct_type)
25444 while (CONVERT_EXPR_P (operand))
25445 operand = TREE_OPERAND (operand, 0);
25447 /* Match field access to members of struct_type only. */
25448 if (TREE_CODE (operand) == COMPONENT_REF
25449 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25450 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25451 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25452 return TREE_OPERAND (operand, 1);
25453 else
25454 return NULL_TREE;
25457 /* Check that SRC is a constant integer that can be represented as a native
25458 integer constant (either signed or unsigned). If so, store it into DEST and
25459 return true. Return false otherwise. */
25461 static bool
25462 get_discr_value (tree src, dw_discr_value *dest)
25464 tree discr_type = TREE_TYPE (src);
25466 if (lang_hooks.types.get_debug_type)
25468 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25469 if (debug_type != NULL)
25470 discr_type = debug_type;
25473 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25474 return false;
25476 /* Signedness can vary between the original type and the debug type. This
25477 can happen for character types in Ada for instance: the character type
25478 used for code generation can be signed, to be compatible with the C one,
25479 but from a debugger point of view, it must be unsigned. */
25480 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25481 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25483 if (is_orig_unsigned != is_debug_unsigned)
25484 src = fold_convert (discr_type, src);
25486 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25487 return false;
25489 dest->pos = is_debug_unsigned;
25490 if (is_debug_unsigned)
25491 dest->v.uval = tree_to_uhwi (src);
25492 else
25493 dest->v.sval = tree_to_shwi (src);
25495 return true;
25498 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25499 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25500 store NULL_TREE in DISCR_DECL. Otherwise:
25502 - store the discriminant field in STRUCT_TYPE that controls the variant
25503 part to *DISCR_DECL
25505 - put in *DISCR_LISTS_P an array where for each variant, the item
25506 represents the corresponding matching list of discriminant values.
25508 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25509 the above array.
25511 Note that when the array is allocated (i.e. when the analysis is
25512 successful), it is up to the caller to free the array. */
25514 static void
25515 analyze_variants_discr (tree variant_part_decl,
25516 tree struct_type,
25517 tree *discr_decl,
25518 dw_discr_list_ref **discr_lists_p,
25519 unsigned *discr_lists_length)
25521 tree variant_part_type = TREE_TYPE (variant_part_decl);
25522 tree variant;
25523 dw_discr_list_ref *discr_lists;
25524 unsigned i;
25526 /* Compute how many variants there are in this variant part. */
25527 *discr_lists_length = 0;
25528 for (variant = TYPE_FIELDS (variant_part_type);
25529 variant != NULL_TREE;
25530 variant = DECL_CHAIN (variant))
25531 ++*discr_lists_length;
25533 *discr_decl = NULL_TREE;
25534 *discr_lists_p
25535 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25536 sizeof (**discr_lists_p));
25537 discr_lists = *discr_lists_p;
25539 /* And then analyze all variants to extract discriminant information for all
25540 of them. This analysis is conservative: as soon as we detect something we
25541 do not support, abort everything and pretend we found nothing. */
25542 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25543 variant != NULL_TREE;
25544 variant = DECL_CHAIN (variant), ++i)
25546 tree match_expr = DECL_QUALIFIER (variant);
25548 /* Now, try to analyze the predicate and deduce a discriminant for
25549 it. */
25550 if (match_expr == boolean_true_node)
25551 /* Typically happens for the default variant: it matches all cases that
25552 previous variants rejected. Don't output any matching value for
25553 this one. */
25554 continue;
25556 /* The following loop tries to iterate over each discriminant
25557 possibility: single values or ranges. */
25558 while (match_expr != NULL_TREE)
25560 tree next_round_match_expr;
25561 tree candidate_discr = NULL_TREE;
25562 dw_discr_list_ref new_node = NULL;
25564 /* Possibilities are matched one after the other by nested
25565 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25566 continue with the rest at next iteration. */
25567 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25569 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25570 match_expr = TREE_OPERAND (match_expr, 1);
25572 else
25573 next_round_match_expr = NULL_TREE;
25575 if (match_expr == boolean_false_node)
25576 /* This sub-expression matches nothing: just wait for the next
25577 one. */
25580 else if (TREE_CODE (match_expr) == EQ_EXPR)
25582 /* We are matching: <discr_field> == <integer_cst>
25583 This sub-expression matches a single value. */
25584 tree integer_cst = TREE_OPERAND (match_expr, 1);
25586 candidate_discr
25587 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25588 struct_type);
25590 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25591 if (!get_discr_value (integer_cst,
25592 &new_node->dw_discr_lower_bound))
25593 goto abort;
25594 new_node->dw_discr_range = false;
25597 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25599 /* We are matching:
25600 <discr_field> > <integer_cst>
25601 && <discr_field> < <integer_cst>.
25602 This sub-expression matches the range of values between the
25603 two matched integer constants. Note that comparisons can be
25604 inclusive or exclusive. */
25605 tree candidate_discr_1, candidate_discr_2;
25606 tree lower_cst, upper_cst;
25607 bool lower_cst_included, upper_cst_included;
25608 tree lower_op = TREE_OPERAND (match_expr, 0);
25609 tree upper_op = TREE_OPERAND (match_expr, 1);
25611 /* When the comparison is exclusive, the integer constant is not
25612 the discriminant range bound we are looking for: we will have
25613 to increment or decrement it. */
25614 if (TREE_CODE (lower_op) == GE_EXPR)
25615 lower_cst_included = true;
25616 else if (TREE_CODE (lower_op) == GT_EXPR)
25617 lower_cst_included = false;
25618 else
25619 goto abort;
25621 if (TREE_CODE (upper_op) == LE_EXPR)
25622 upper_cst_included = true;
25623 else if (TREE_CODE (upper_op) == LT_EXPR)
25624 upper_cst_included = false;
25625 else
25626 goto abort;
25628 /* Extract the discriminant from the first operand and check it
25629 is consistant with the same analysis in the second
25630 operand. */
25631 candidate_discr_1
25632 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25633 struct_type);
25634 candidate_discr_2
25635 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25636 struct_type);
25637 if (candidate_discr_1 == candidate_discr_2)
25638 candidate_discr = candidate_discr_1;
25639 else
25640 goto abort;
25642 /* Extract bounds from both. */
25643 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25644 lower_cst = TREE_OPERAND (lower_op, 1);
25645 upper_cst = TREE_OPERAND (upper_op, 1);
25647 if (!lower_cst_included)
25648 lower_cst
25649 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25650 build_int_cst (TREE_TYPE (lower_cst), 1));
25651 if (!upper_cst_included)
25652 upper_cst
25653 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25654 build_int_cst (TREE_TYPE (upper_cst), 1));
25656 if (!get_discr_value (lower_cst,
25657 &new_node->dw_discr_lower_bound)
25658 || !get_discr_value (upper_cst,
25659 &new_node->dw_discr_upper_bound))
25660 goto abort;
25662 new_node->dw_discr_range = true;
25665 else if ((candidate_discr
25666 = analyze_discr_in_predicate (match_expr, struct_type))
25667 && (TREE_TYPE (candidate_discr) == boolean_type_node
25668 || TREE_TYPE (TREE_TYPE (candidate_discr))
25669 == boolean_type_node))
25671 /* We are matching: <discr_field> for a boolean discriminant.
25672 This sub-expression matches boolean_true_node. */
25673 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25674 if (!get_discr_value (boolean_true_node,
25675 &new_node->dw_discr_lower_bound))
25676 goto abort;
25677 new_node->dw_discr_range = false;
25680 else
25681 /* Unsupported sub-expression: we cannot determine the set of
25682 matching discriminant values. Abort everything. */
25683 goto abort;
25685 /* If the discriminant info is not consistant with what we saw so
25686 far, consider the analysis failed and abort everything. */
25687 if (candidate_discr == NULL_TREE
25688 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25689 goto abort;
25690 else
25691 *discr_decl = candidate_discr;
25693 if (new_node != NULL)
25695 new_node->dw_discr_next = discr_lists[i];
25696 discr_lists[i] = new_node;
25698 match_expr = next_round_match_expr;
25702 /* If we reach this point, we could match everything we were interested
25703 in. */
25704 return;
25706 abort:
25707 /* Clean all data structure and return no result. */
25708 free (*discr_lists_p);
25709 *discr_lists_p = NULL;
25710 *discr_decl = NULL_TREE;
25713 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25714 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25715 under CONTEXT_DIE.
25717 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25718 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25719 this type, which are record types, represent the available variants and each
25720 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25721 values are inferred from these attributes.
25723 In trees, the offsets for the fields inside these sub-records are relative
25724 to the variant part itself, whereas the corresponding DIEs should have
25725 offset attributes that are relative to the embedding record base address.
25726 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25727 must be an expression that computes the offset of the variant part to
25728 describe in DWARF. */
25730 static void
25731 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25732 dw_die_ref context_die)
25734 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25735 tree variant_part_offset = vlr_ctx->variant_part_offset;
25737 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25738 NULL_TREE if there is no such field. */
25739 tree discr_decl = NULL_TREE;
25740 dw_discr_list_ref *discr_lists;
25741 unsigned discr_lists_length = 0;
25742 unsigned i;
25744 dw_die_ref dwarf_proc_die = NULL;
25745 dw_die_ref variant_part_die
25746 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25748 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25750 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25751 &discr_decl, &discr_lists, &discr_lists_length);
25753 if (discr_decl != NULL_TREE)
25755 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25757 if (discr_die)
25758 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25759 else
25760 /* We have no DIE for the discriminant, so just discard all
25761 discrimimant information in the output. */
25762 discr_decl = NULL_TREE;
25765 /* If the offset for this variant part is more complex than a constant,
25766 create a DWARF procedure for it so that we will not have to generate
25767 DWARF expressions for it for each member. */
25768 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25769 && (dwarf_version >= 3 || !dwarf_strict))
25771 struct loc_descr_context ctx = {
25772 vlr_ctx->struct_type, /* context_type */
25773 NULL_TREE, /* base_decl */
25774 NULL, /* dpi */
25775 false, /* placeholder_arg */
25776 false, /* placeholder_seen */
25777 false /* strict_signedness */
25779 const tree dwarf_proc_fndecl
25780 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25781 build_function_type (TREE_TYPE (variant_part_offset),
25782 NULL_TREE));
25783 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25784 const dw_loc_descr_ref dwarf_proc_body
25785 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25787 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25788 dwarf_proc_fndecl, context_die);
25789 if (dwarf_proc_die != NULL)
25790 variant_part_offset = dwarf_proc_call;
25793 /* Output DIEs for all variants. */
25794 i = 0;
25795 for (tree variant = TYPE_FIELDS (variant_part_type);
25796 variant != NULL_TREE;
25797 variant = DECL_CHAIN (variant), ++i)
25799 tree variant_type = TREE_TYPE (variant);
25800 dw_die_ref variant_die;
25802 /* All variants (i.e. members of a variant part) are supposed to be
25803 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25804 under these records. */
25805 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25807 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25808 equate_decl_number_to_die (variant, variant_die);
25810 /* Output discriminant values this variant matches, if any. */
25811 if (discr_decl == NULL || discr_lists[i] == NULL)
25812 /* In the case we have discriminant information at all, this is
25813 probably the default variant: as the standard says, don't
25814 output any discriminant value/list attribute. */
25816 else if (discr_lists[i]->dw_discr_next == NULL
25817 && !discr_lists[i]->dw_discr_range)
25818 /* If there is only one accepted value, don't bother outputting a
25819 list. */
25820 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25821 else
25822 add_discr_list (variant_die, discr_lists[i]);
25824 for (tree member = TYPE_FIELDS (variant_type);
25825 member != NULL_TREE;
25826 member = DECL_CHAIN (member))
25828 struct vlr_context vlr_sub_ctx = {
25829 vlr_ctx->struct_type, /* struct_type */
25830 NULL /* variant_part_offset */
25832 if (is_variant_part (member))
25834 /* All offsets for fields inside variant parts are relative to
25835 the top-level embedding RECORD_TYPE's base address. On the
25836 other hand, offsets in GCC's types are relative to the
25837 nested-most variant part. So we have to sum offsets each time
25838 we recurse. */
25840 vlr_sub_ctx.variant_part_offset
25841 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25842 variant_part_offset, byte_position (member));
25843 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25845 else
25847 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25848 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25853 free (discr_lists);
25856 /* Generate a DIE for a class member. */
25858 static void
25859 gen_member_die (tree type, dw_die_ref context_die)
25861 tree member;
25862 tree binfo = TYPE_BINFO (type);
25864 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25866 /* If this is not an incomplete type, output descriptions of each of its
25867 members. Note that as we output the DIEs necessary to represent the
25868 members of this record or union type, we will also be trying to output
25869 DIEs to represent the *types* of those members. However the `type'
25870 function (above) will specifically avoid generating type DIEs for member
25871 types *within* the list of member DIEs for this (containing) type except
25872 for those types (of members) which are explicitly marked as also being
25873 members of this (containing) type themselves. The g++ front- end can
25874 force any given type to be treated as a member of some other (containing)
25875 type by setting the TYPE_CONTEXT of the given (member) type to point to
25876 the TREE node representing the appropriate (containing) type. */
25878 /* First output info about the base classes. */
25879 if (binfo && early_dwarf)
25881 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25882 int i;
25883 tree base;
25885 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25886 gen_inheritance_die (base,
25887 (accesses ? (*accesses)[i] : access_public_node),
25888 type,
25889 context_die);
25892 /* Now output info about the members. */
25893 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25895 /* Ignore clones. */
25896 if (DECL_ABSTRACT_ORIGIN (member))
25897 continue;
25899 struct vlr_context vlr_ctx = { type, NULL_TREE };
25900 bool static_inline_p
25901 = (VAR_P (member)
25902 && TREE_STATIC (member)
25903 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25904 != -1));
25906 /* If we thought we were generating minimal debug info for TYPE
25907 and then changed our minds, some of the member declarations
25908 may have already been defined. Don't define them again, but
25909 do put them in the right order. */
25911 if (dw_die_ref child = lookup_decl_die (member))
25913 /* Handle inline static data members, which only have in-class
25914 declarations. */
25915 bool splice = true;
25917 dw_die_ref ref = NULL;
25918 if (child->die_tag == DW_TAG_variable
25919 && child->die_parent == comp_unit_die ())
25921 ref = get_AT_ref (child, DW_AT_specification);
25923 /* For C++17 inline static data members followed by redundant
25924 out of class redeclaration, we might get here with
25925 child being the DIE created for the out of class
25926 redeclaration and with its DW_AT_specification being
25927 the DIE created for in-class definition. We want to
25928 reparent the latter, and don't want to create another
25929 DIE with DW_AT_specification in that case, because
25930 we already have one. */
25931 if (ref
25932 && static_inline_p
25933 && ref->die_tag == DW_TAG_variable
25934 && ref->die_parent == comp_unit_die ()
25935 && get_AT (ref, DW_AT_specification) == NULL)
25937 child = ref;
25938 ref = NULL;
25939 static_inline_p = false;
25942 if (!ref)
25944 reparent_child (child, context_die);
25945 if (dwarf_version < 5)
25946 child->die_tag = DW_TAG_member;
25947 splice = false;
25950 else if (child->die_tag == DW_TAG_enumerator)
25951 /* Enumerators remain under their enumeration even if
25952 their names are introduced in the enclosing scope. */
25953 splice = false;
25955 if (splice)
25956 splice_child_die (context_die, child);
25959 /* Do not generate DWARF for variant parts if we are generating the
25960 corresponding GNAT encodings: DIEs generated for the two schemes
25961 would conflict in our mappings. */
25962 else if (is_variant_part (member)
25963 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
25965 vlr_ctx.variant_part_offset = byte_position (member);
25966 gen_variant_part (member, &vlr_ctx, context_die);
25968 else
25970 vlr_ctx.variant_part_offset = NULL_TREE;
25971 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25974 /* For C++ inline static data members emit immediately a DW_TAG_variable
25975 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25976 DW_AT_specification. */
25977 if (static_inline_p)
25979 int old_extern = DECL_EXTERNAL (member);
25980 DECL_EXTERNAL (member) = 0;
25981 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25982 DECL_EXTERNAL (member) = old_extern;
25987 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25988 is set, we pretend that the type was never defined, so we only get the
25989 member DIEs needed by later specification DIEs. */
25991 static void
25992 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25993 enum debug_info_usage usage)
25995 if (TREE_ASM_WRITTEN (type))
25997 /* Fill in the bound of variable-length fields in late dwarf if
25998 still incomplete. */
25999 if (!early_dwarf && variably_modified_type_p (type, NULL))
26000 for (tree member = TYPE_FIELDS (type);
26001 member;
26002 member = DECL_CHAIN (member))
26003 fill_variable_array_bounds (TREE_TYPE (member));
26004 return;
26007 dw_die_ref type_die = lookup_type_die (type);
26008 dw_die_ref scope_die = 0;
26009 bool nested = false;
26010 bool complete = (TYPE_SIZE (type)
26011 && (! TYPE_STUB_DECL (type)
26012 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
26013 bool ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
26014 complete = complete && should_emit_struct_debug (type, usage);
26016 if (type_die && ! complete)
26017 return;
26019 if (TYPE_CONTEXT (type) != NULL_TREE
26020 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26021 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
26022 nested = true;
26024 scope_die = scope_die_for (type, context_die);
26026 /* Generate child dies for template parameters. */
26027 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
26028 schedule_generic_params_dies_gen (type);
26030 if (! type_die || (nested && is_cu_die (scope_die)))
26031 /* First occurrence of type or toplevel definition of nested class. */
26033 dw_die_ref old_die = type_die;
26035 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
26036 ? record_type_tag (type) : DW_TAG_union_type,
26037 scope_die, type);
26038 equate_type_number_to_die (type, type_die);
26039 if (old_die)
26040 add_AT_specification (type_die, old_die);
26041 else
26042 add_name_attribute (type_die, type_tag (type));
26044 else
26045 remove_AT (type_die, DW_AT_declaration);
26047 /* If this type has been completed, then give it a byte_size attribute and
26048 then give a list of members. */
26049 if (complete && !ns_decl)
26051 /* Prevent infinite recursion in cases where the type of some member of
26052 this type is expressed in terms of this type itself. */
26053 TREE_ASM_WRITTEN (type) = 1;
26054 add_byte_size_attribute (type_die, type);
26055 add_alignment_attribute (type_die, type);
26056 if (TYPE_STUB_DECL (type) != NULL_TREE)
26058 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
26059 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
26062 /* If the first reference to this type was as the return type of an
26063 inline function, then it may not have a parent. Fix this now. */
26064 if (type_die->die_parent == NULL)
26065 add_child_die (scope_die, type_die);
26067 gen_member_die (type, type_die);
26069 add_gnat_descriptive_type_attribute (type_die, type, context_die);
26070 if (TYPE_ARTIFICIAL (type))
26071 add_AT_flag (type_die, DW_AT_artificial, 1);
26073 /* GNU extension: Record what type our vtable lives in. */
26074 if (TYPE_VFIELD (type))
26076 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
26078 gen_type_die (vtype, context_die);
26079 add_AT_die_ref (type_die, DW_AT_containing_type,
26080 lookup_type_die (vtype));
26083 else
26085 add_AT_flag (type_die, DW_AT_declaration, 1);
26087 /* We don't need to do this for function-local types. */
26088 if (TYPE_STUB_DECL (type)
26089 && ! decl_function_context (TYPE_STUB_DECL (type)))
26090 vec_safe_push (incomplete_types, type);
26093 if (get_AT (type_die, DW_AT_name))
26094 add_pubtype (type, type_die);
26097 /* Generate a DIE for a subroutine _type_. */
26099 static void
26100 gen_subroutine_type_die (tree type, dw_die_ref context_die)
26102 tree return_type = TREE_TYPE (type);
26103 dw_die_ref subr_die
26104 = new_die (DW_TAG_subroutine_type,
26105 scope_die_for (type, context_die), type);
26107 equate_type_number_to_die (type, subr_die);
26108 add_prototyped_attribute (subr_die, type);
26109 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
26110 context_die);
26111 add_alignment_attribute (subr_die, type);
26112 gen_formal_types_die (type, subr_die);
26114 if (get_AT (subr_die, DW_AT_name))
26115 add_pubtype (type, subr_die);
26116 if ((dwarf_version >= 5 || !dwarf_strict)
26117 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
26118 add_AT_flag (subr_die, DW_AT_reference, 1);
26119 if ((dwarf_version >= 5 || !dwarf_strict)
26120 && lang_hooks.types.type_dwarf_attribute (type,
26121 DW_AT_rvalue_reference) != -1)
26122 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
26125 /* Generate a DIE for a type definition. */
26127 static void
26128 gen_typedef_die (tree decl, dw_die_ref context_die)
26130 dw_die_ref type_die;
26131 tree type;
26133 if (TREE_ASM_WRITTEN (decl))
26135 if (DECL_ORIGINAL_TYPE (decl))
26136 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
26137 return;
26140 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
26141 checks in process_scope_var and modified_type_die), this should be called
26142 only for original types. */
26143 gcc_assert (decl_ultimate_origin (decl) == NULL
26144 || decl_ultimate_origin (decl) == decl);
26146 TREE_ASM_WRITTEN (decl) = 1;
26147 type_die = new_die (DW_TAG_typedef, context_die, decl);
26149 add_name_and_src_coords_attributes (type_die, decl);
26150 if (DECL_ORIGINAL_TYPE (decl))
26152 type = DECL_ORIGINAL_TYPE (decl);
26153 if (type == error_mark_node)
26154 return;
26156 gcc_assert (type != TREE_TYPE (decl));
26157 equate_type_number_to_die (TREE_TYPE (decl), type_die);
26159 else
26161 type = TREE_TYPE (decl);
26162 if (type == error_mark_node)
26163 return;
26165 if (is_naming_typedef_decl (TYPE_NAME (type)))
26167 /* Here, we are in the case of decl being a typedef naming
26168 an anonymous type, e.g:
26169 typedef struct {...} foo;
26170 In that case TREE_TYPE (decl) is not a typedef variant
26171 type and TYPE_NAME of the anonymous type is set to the
26172 TYPE_DECL of the typedef. This construct is emitted by
26173 the C++ FE.
26175 TYPE is the anonymous struct named by the typedef
26176 DECL. As we need the DW_AT_type attribute of the
26177 DW_TAG_typedef to point to the DIE of TYPE, let's
26178 generate that DIE right away. add_type_attribute
26179 called below will then pick (via lookup_type_die) that
26180 anonymous struct DIE. */
26181 if (!TREE_ASM_WRITTEN (type))
26182 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
26184 /* This is a GNU Extension. We are adding a
26185 DW_AT_linkage_name attribute to the DIE of the
26186 anonymous struct TYPE. The value of that attribute
26187 is the name of the typedef decl naming the anonymous
26188 struct. This greatly eases the work of consumers of
26189 this debug info. */
26190 add_linkage_name_raw (lookup_type_die (type), decl);
26194 add_type_attribute (type_die, type, decl_quals (decl), false,
26195 context_die);
26197 if (is_naming_typedef_decl (decl))
26198 /* We want that all subsequent calls to lookup_type_die with
26199 TYPE in argument yield the DW_TAG_typedef we have just
26200 created. */
26201 equate_type_number_to_die (type, type_die);
26203 add_alignment_attribute (type_die, TREE_TYPE (decl));
26205 add_accessibility_attribute (type_die, decl);
26207 if (DECL_ABSTRACT_P (decl))
26208 equate_decl_number_to_die (decl, type_die);
26210 if (get_AT (type_die, DW_AT_name))
26211 add_pubtype (decl, type_die);
26214 /* Generate a DIE for a struct, class, enum or union type. */
26216 static void
26217 gen_tagged_type_die (tree type,
26218 dw_die_ref context_die,
26219 enum debug_info_usage usage,
26220 bool reverse)
26222 if (type == NULL_TREE
26223 || !is_tagged_type (type))
26224 return;
26226 if (TREE_ASM_WRITTEN (type))
26228 /* If this is a nested type whose containing class hasn't been written
26229 out yet, writing it out will cover this one, too. This does not apply
26230 to instantiations of member class templates; they need to be added to
26231 the containing class as they are generated. FIXME: This hurts the
26232 idea of combining type decls from multiple TUs, since we can't predict
26233 what set of template instantiations we'll get. */
26234 else if (TYPE_CONTEXT (type)
26235 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26236 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
26238 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
26240 if (TREE_ASM_WRITTEN (type))
26241 return;
26243 /* If that failed, attach ourselves to the stub. */
26244 context_die = lookup_type_die (TYPE_CONTEXT (type));
26246 else if (TYPE_CONTEXT (type) != NULL_TREE
26247 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
26249 /* If this type is local to a function that hasn't been written
26250 out yet, use a NULL context for now; it will be fixed up in
26251 decls_for_scope. */
26252 context_die = lookup_decl_die (TYPE_CONTEXT (type));
26253 /* A declaration DIE doesn't count; nested types need to go in the
26254 specification. */
26255 if (context_die && is_declaration_die (context_die))
26256 context_die = NULL;
26258 else
26259 context_die = declare_in_namespace (type, context_die);
26261 if (TREE_CODE (type) == ENUMERAL_TYPE)
26263 /* This might have been written out by the call to
26264 declare_in_namespace. */
26265 if (!TREE_ASM_WRITTEN (type) || reverse)
26266 gen_enumeration_type_die (type, context_die, reverse);
26268 else
26269 gen_struct_or_union_type_die (type, context_die, usage);
26271 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
26272 it up if it is ever completed. gen_*_type_die will set it for us
26273 when appropriate. */
26276 /* Generate a type description DIE. */
26278 static void
26279 gen_type_die_with_usage (tree type, dw_die_ref context_die,
26280 enum debug_info_usage usage, bool reverse)
26282 struct array_descr_info info;
26284 if (type == NULL_TREE || type == error_mark_node)
26285 return;
26287 if (flag_checking && type)
26288 verify_type (type);
26290 if (TYPE_NAME (type) != NULL_TREE
26291 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
26292 && is_redundant_typedef (TYPE_NAME (type))
26293 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
26294 /* The DECL of this type is a typedef we don't want to emit debug
26295 info for but we want debug info for its underlying typedef.
26296 This can happen for e.g, the injected-class-name of a C++
26297 type. */
26298 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
26300 /* If TYPE is a typedef type variant, let's generate debug info
26301 for the parent typedef which TYPE is a type of. */
26302 if (typedef_variant_p (type))
26304 if (TREE_ASM_WRITTEN (type))
26305 return;
26307 tree name = TYPE_NAME (type);
26308 tree origin = decl_ultimate_origin (name);
26309 if (origin != NULL && origin != name)
26311 gen_decl_die (origin, NULL, NULL, context_die);
26312 return;
26315 /* Prevent broken recursion; we can't hand off to the same type. */
26316 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26318 /* Give typedefs the right scope. */
26319 context_die = scope_die_for (type, context_die);
26321 TREE_ASM_WRITTEN (type) = 1;
26323 gen_decl_die (name, NULL, NULL, context_die);
26324 return;
26327 /* If type is an anonymous tagged type named by a typedef, let's
26328 generate debug info for the typedef. */
26329 if (is_naming_typedef_decl (TYPE_NAME (type)))
26331 /* Give typedefs the right scope. */
26332 context_die = scope_die_for (type, context_die);
26334 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26335 return;
26338 if (lang_hooks.types.get_debug_type)
26340 tree debug_type = lang_hooks.types.get_debug_type (type);
26342 if (debug_type != NULL_TREE && debug_type != type)
26344 gen_type_die_with_usage (debug_type, context_die, usage, reverse);
26345 return;
26349 /* We are going to output a DIE to represent the unqualified version
26350 of this type (i.e. without any const or volatile qualifiers) so
26351 get the main variant (i.e. the unqualified version) of this type
26352 now. (Vectors and arrays are special because the debugging info is in the
26353 cloned type itself. Similarly function/method types can contain extra
26354 ref-qualification). */
26355 if (FUNC_OR_METHOD_TYPE_P (type))
26357 /* For function/method types, can't use type_main_variant here,
26358 because that can have different ref-qualifiers for C++,
26359 but try to canonicalize. */
26360 tree main = TYPE_MAIN_VARIANT (type);
26361 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26362 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26363 && check_base_type (t, main)
26364 && check_lang_type (t, type))
26366 type = t;
26367 break;
26370 else if (TREE_CODE (type) != VECTOR_TYPE
26371 && TREE_CODE (type) != ARRAY_TYPE)
26372 type = type_main_variant (type);
26374 /* If this is an array type with hidden descriptor, handle it first. */
26375 if (!TREE_ASM_WRITTEN (type)
26376 && lang_hooks.types.get_array_descr_info)
26378 memset (&info, 0, sizeof (info));
26379 if (lang_hooks.types.get_array_descr_info (type, &info))
26381 /* Fortran sometimes emits array types with no dimension. */
26382 gcc_assert (info.ndimensions >= 0
26383 && (info.ndimensions
26384 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26385 gen_descr_array_type_die (type, &info, context_die);
26386 TREE_ASM_WRITTEN (type) = 1;
26387 return;
26391 if (TREE_ASM_WRITTEN (type) && !reverse)
26393 /* Variable-length types may be incomplete even if
26394 TREE_ASM_WRITTEN. For such types, fall through to
26395 gen_array_type_die() and possibly fill in
26396 DW_AT_{upper,lower}_bound attributes. */
26397 if ((TREE_CODE (type) != ARRAY_TYPE
26398 && TREE_CODE (type) != RECORD_TYPE
26399 && TREE_CODE (type) != UNION_TYPE
26400 && TREE_CODE (type) != QUAL_UNION_TYPE)
26401 || !variably_modified_type_p (type, NULL))
26402 return;
26405 switch (TREE_CODE (type))
26407 case ERROR_MARK:
26408 break;
26410 case POINTER_TYPE:
26411 case REFERENCE_TYPE:
26412 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26413 ensures that the gen_type_die recursion will terminate even if the
26414 type is recursive. Recursive types are possible in Ada. */
26415 /* ??? We could perhaps do this for all types before the switch
26416 statement. */
26417 TREE_ASM_WRITTEN (type) = 1;
26419 /* For these types, all that is required is that we output a DIE (or a
26420 set of DIEs) to represent the "basis" type. */
26421 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26422 DINFO_USAGE_IND_USE);
26423 break;
26425 case OFFSET_TYPE:
26426 /* This code is used for C++ pointer-to-data-member types.
26427 Output a description of the relevant class type. */
26428 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26429 DINFO_USAGE_IND_USE);
26431 /* Output a description of the type of the object pointed to. */
26432 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26433 DINFO_USAGE_IND_USE);
26435 /* Now output a DIE to represent this pointer-to-data-member type
26436 itself. */
26437 gen_ptr_to_mbr_type_die (type, context_die);
26438 break;
26440 case FUNCTION_TYPE:
26441 /* Force out return type (in case it wasn't forced out already). */
26442 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26443 DINFO_USAGE_DIR_USE);
26444 gen_subroutine_type_die (type, context_die);
26445 break;
26447 case METHOD_TYPE:
26448 /* Force out return type (in case it wasn't forced out already). */
26449 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26450 DINFO_USAGE_DIR_USE);
26451 gen_subroutine_type_die (type, context_die);
26452 break;
26454 case ARRAY_TYPE:
26455 case VECTOR_TYPE:
26456 gen_array_type_die (type, context_die);
26457 break;
26459 case ENUMERAL_TYPE:
26460 case RECORD_TYPE:
26461 case UNION_TYPE:
26462 case QUAL_UNION_TYPE:
26463 gen_tagged_type_die (type, context_die, usage, reverse);
26464 return;
26466 case VOID_TYPE:
26467 case OPAQUE_TYPE:
26468 case INTEGER_TYPE:
26469 case REAL_TYPE:
26470 case FIXED_POINT_TYPE:
26471 case COMPLEX_TYPE:
26472 case BOOLEAN_TYPE:
26473 case BITINT_TYPE:
26474 /* No DIEs needed for fundamental types. */
26475 break;
26477 case NULLPTR_TYPE:
26478 case LANG_TYPE:
26479 /* Just use DW_TAG_unspecified_type. */
26481 dw_die_ref type_die = lookup_type_die (type);
26482 if (type_die == NULL)
26484 tree name = TYPE_IDENTIFIER (type);
26485 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26486 type);
26487 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26488 equate_type_number_to_die (type, type_die);
26491 break;
26493 default:
26494 if (is_cxx_auto (type))
26496 tree name = TYPE_IDENTIFIER (type);
26497 dw_die_ref *die = (name == get_identifier ("auto")
26498 ? &auto_die : &decltype_auto_die);
26499 if (!*die)
26501 *die = new_die (DW_TAG_unspecified_type,
26502 comp_unit_die (), NULL_TREE);
26503 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26505 equate_type_number_to_die (type, *die);
26506 break;
26508 gcc_unreachable ();
26511 TREE_ASM_WRITTEN (type) = 1;
26514 static void
26515 gen_type_die (tree type, dw_die_ref context_die, bool reverse)
26517 if (type != error_mark_node)
26519 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE, reverse);
26520 if (flag_checking)
26522 dw_die_ref die = lookup_type_die (type);
26523 if (die)
26524 check_die (die);
26529 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26530 things which are local to the given block. */
26532 static void
26533 gen_block_die (tree stmt, dw_die_ref context_die)
26535 int must_output_die = 0;
26536 bool inlined_func;
26538 /* Ignore blocks that are NULL. */
26539 if (stmt == NULL_TREE)
26540 return;
26542 inlined_func = inlined_function_outer_scope_p (stmt);
26544 /* If the block is one fragment of a non-contiguous block, do not
26545 process the variables, since they will have been done by the
26546 origin block. Do process subblocks. */
26547 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26549 tree sub;
26551 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26552 gen_block_die (sub, context_die);
26554 return;
26557 /* Determine if we need to output any Dwarf DIEs at all to represent this
26558 block. */
26559 if (inlined_func)
26560 /* The outer scopes for inlinings *must* always be represented. We
26561 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26562 must_output_die = 1;
26563 else if (lookup_block_die (stmt))
26564 /* If we already have a DIE then it was filled early. Meanwhile
26565 we might have pruned all BLOCK_VARS as optimized out but we
26566 still want to generate high/low PC attributes so output it. */
26567 must_output_die = 1;
26568 else if (TREE_USED (stmt)
26569 || TREE_ASM_WRITTEN (stmt))
26571 /* Determine if this block directly contains any "significant"
26572 local declarations which we will need to output DIEs for. */
26573 if (debug_info_level > DINFO_LEVEL_TERSE)
26575 /* We are not in terse mode so any local declaration that
26576 is not ignored for debug purposes counts as being a
26577 "significant" one. */
26578 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26579 must_output_die = 1;
26580 else
26581 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26582 if (!DECL_IGNORED_P (var))
26584 must_output_die = 1;
26585 break;
26588 else if (!dwarf2out_ignore_block (stmt))
26589 must_output_die = 1;
26592 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26593 DIE for any block which contains no significant local declarations at
26594 all. Rather, in such cases we just call `decls_for_scope' so that any
26595 needed Dwarf info for any sub-blocks will get properly generated. Note
26596 that in terse mode, our definition of what constitutes a "significant"
26597 local declaration gets restricted to include only inlined function
26598 instances and local (nested) function definitions. */
26599 if (must_output_die)
26601 if (inlined_func)
26602 gen_inlined_subroutine_die (stmt, context_die);
26603 else
26604 gen_lexical_block_die (stmt, context_die);
26606 else
26607 decls_for_scope (stmt, context_die);
26610 /* Process variable DECL (or variable with origin ORIGIN) within
26611 block STMT and add it to CONTEXT_DIE. */
26612 static void
26613 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26615 dw_die_ref die;
26616 tree decl_or_origin = decl ? decl : origin;
26618 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26619 die = lookup_decl_die (decl_or_origin);
26620 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26622 if (TYPE_DECL_IS_STUB (decl_or_origin))
26623 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26624 else
26625 die = lookup_decl_die (decl_or_origin);
26626 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26627 if (! die && ! early_dwarf)
26628 return;
26630 else
26631 die = NULL;
26633 /* Avoid creating DIEs for local typedefs and concrete static variables that
26634 will only be pruned later. */
26635 if ((origin || decl_ultimate_origin (decl))
26636 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26637 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26639 origin = decl_ultimate_origin (decl_or_origin);
26640 if (decl && VAR_P (decl) && die != NULL)
26642 die = lookup_decl_die (origin);
26643 if (die != NULL)
26644 equate_decl_number_to_die (decl, die);
26646 return;
26649 if (die != NULL && die->die_parent == NULL)
26650 add_child_die (context_die, die);
26652 if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26654 if (early_dwarf)
26655 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26656 stmt, context_die);
26658 else
26660 if (decl && DECL_P (decl))
26662 die = lookup_decl_die (decl);
26664 /* Early created DIEs do not have a parent as the decls refer
26665 to the function as DECL_CONTEXT rather than the BLOCK. */
26666 if (die && die->die_parent == NULL)
26668 gcc_assert (in_lto_p);
26669 add_child_die (context_die, die);
26673 gen_decl_die (decl, origin, NULL, context_die);
26677 /* Generate all of the decls declared within a given scope and (recursively)
26678 all of its sub-blocks. */
26680 static void
26681 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26683 tree decl;
26684 unsigned int i;
26685 tree subblocks;
26687 /* Ignore NULL blocks. */
26688 if (stmt == NULL_TREE)
26689 return;
26691 /* Output the DIEs to represent all of the data objects and typedefs
26692 declared directly within this block but not within any nested
26693 sub-blocks. Also, nested function and tag DIEs have been
26694 generated with a parent of NULL; fix that up now. We don't
26695 have to do this if we're at -g1. */
26696 if (debug_info_level > DINFO_LEVEL_TERSE)
26698 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26699 process_scope_var (stmt, decl, NULL_TREE, context_die);
26700 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26701 origin - avoid doing this twice as we have no good way to see
26702 if we've done it once already. */
26703 if (! early_dwarf)
26704 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26706 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26707 if (decl == current_function_decl)
26708 /* Ignore declarations of the current function, while they
26709 are declarations, gen_subprogram_die would treat them
26710 as definitions again, because they are equal to
26711 current_function_decl and endlessly recurse. */;
26712 else if (TREE_CODE (decl) == FUNCTION_DECL)
26713 process_scope_var (stmt, decl, NULL_TREE, context_die);
26714 else
26715 process_scope_var (stmt, NULL_TREE, decl, context_die);
26719 /* Even if we're at -g1, we need to process the subblocks in order to get
26720 inlined call information. */
26722 /* Output the DIEs to represent all sub-blocks (and the items declared
26723 therein) of this block. */
26724 if (recurse)
26725 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26726 subblocks != NULL;
26727 subblocks = BLOCK_CHAIN (subblocks))
26728 gen_block_die (subblocks, context_die);
26731 /* Is this a typedef we can avoid emitting? */
26733 static bool
26734 is_redundant_typedef (const_tree decl)
26736 if (TYPE_DECL_IS_STUB (decl))
26737 return true;
26739 if (DECL_ARTIFICIAL (decl)
26740 && DECL_CONTEXT (decl)
26741 && is_tagged_type (DECL_CONTEXT (decl))
26742 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26743 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26744 /* Also ignore the artificial member typedef for the class name. */
26745 return true;
26747 return false;
26750 /* Return TRUE if TYPE is a typedef that names a type for linkage
26751 purposes. This kind of typedefs is produced by the C++ FE for
26752 constructs like:
26754 typedef struct {...} foo;
26756 In that case, there is no typedef variant type produced for foo.
26757 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26758 struct type. */
26760 static bool
26761 is_naming_typedef_decl (const_tree decl)
26763 if (decl == NULL_TREE
26764 || TREE_CODE (decl) != TYPE_DECL
26765 || DECL_NAMELESS (decl)
26766 || !is_tagged_type (TREE_TYPE (decl))
26767 || DECL_IS_UNDECLARED_BUILTIN (decl)
26768 || is_redundant_typedef (decl)
26769 /* It looks like Ada produces TYPE_DECLs that are very similar
26770 to C++ naming typedefs but that have different
26771 semantics. Let's be specific to c++ for now. */
26772 || !is_cxx (decl))
26773 return false;
26775 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26776 && TYPE_NAME (TREE_TYPE (decl)) == decl
26777 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26778 != TYPE_NAME (TREE_TYPE (decl))));
26781 /* Looks up the DIE for a context. */
26783 static inline dw_die_ref
26784 lookup_context_die (tree context)
26786 if (context)
26788 /* Find die that represents this context. */
26789 if (TYPE_P (context))
26791 context = TYPE_MAIN_VARIANT (context);
26792 dw_die_ref ctx = lookup_type_die (context);
26793 if (!ctx)
26794 return NULL;
26795 return strip_naming_typedef (context, ctx);
26797 else
26798 return lookup_decl_die (context);
26800 return comp_unit_die ();
26803 /* Returns the DIE for a context. */
26805 static inline dw_die_ref
26806 get_context_die (tree context)
26808 if (context)
26810 /* Find die that represents this context. */
26811 if (TYPE_P (context))
26813 context = TYPE_MAIN_VARIANT (context);
26814 return strip_naming_typedef (context, force_type_die (context));
26816 else
26817 return force_decl_die (context);
26819 return comp_unit_die ();
26822 /* Returns the DIE for decl. A DIE will always be returned. */
26824 static dw_die_ref
26825 force_decl_die (tree decl)
26827 dw_die_ref decl_die;
26828 unsigned saved_external_flag;
26829 tree save_fn = NULL_TREE;
26830 decl_die = lookup_decl_die (decl);
26831 if (!decl_die)
26833 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26835 decl_die = lookup_decl_die (decl);
26836 if (decl_die)
26837 return decl_die;
26839 switch (TREE_CODE (decl))
26841 case FUNCTION_DECL:
26842 /* Clear current_function_decl, so that gen_subprogram_die thinks
26843 that this is a declaration. At this point, we just want to force
26844 declaration die. */
26845 save_fn = current_function_decl;
26846 current_function_decl = NULL_TREE;
26847 gen_subprogram_die (decl, context_die);
26848 current_function_decl = save_fn;
26849 break;
26851 case VAR_DECL:
26852 /* Set external flag to force declaration die. Restore it after
26853 gen_decl_die() call. */
26854 saved_external_flag = DECL_EXTERNAL (decl);
26855 DECL_EXTERNAL (decl) = 1;
26856 gen_decl_die (decl, NULL, NULL, context_die);
26857 DECL_EXTERNAL (decl) = saved_external_flag;
26858 break;
26860 case NAMESPACE_DECL:
26861 if (dwarf_version >= 3 || !dwarf_strict)
26862 dwarf2out_decl (decl);
26863 else
26864 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26865 decl_die = comp_unit_die ();
26866 break;
26868 case CONST_DECL:
26869 /* Enumerators shouldn't need force_decl_die. */
26870 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26871 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26872 gen_decl_die (decl, NULL, NULL, context_die);
26873 break;
26875 case TRANSLATION_UNIT_DECL:
26876 decl_die = comp_unit_die ();
26877 break;
26879 default:
26880 gcc_unreachable ();
26883 /* We should be able to find the DIE now. */
26884 if (!decl_die)
26885 decl_die = lookup_decl_die (decl);
26886 gcc_assert (decl_die);
26889 return decl_die;
26892 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26893 always returned. */
26895 static dw_die_ref
26896 force_type_die (tree type)
26898 dw_die_ref type_die;
26900 type_die = lookup_type_die (type);
26901 if (!type_die)
26903 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26905 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26906 false, context_die);
26907 gcc_assert (type_die);
26909 return type_die;
26912 /* Force out any required namespaces to be able to output DECL,
26913 and return the new context_die for it, if it's changed. */
26915 static dw_die_ref
26916 setup_namespace_context (tree thing, dw_die_ref context_die)
26918 tree context = (DECL_P (thing)
26919 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26920 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26921 /* Force out the namespace. */
26922 context_die = force_decl_die (context);
26924 return context_die;
26927 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26928 type) within its namespace, if appropriate.
26930 For compatibility with older debuggers, namespace DIEs only contain
26931 declarations; all definitions are emitted at CU scope, with
26932 DW_AT_specification pointing to the declaration (like with class
26933 members). */
26935 static dw_die_ref
26936 declare_in_namespace (tree thing, dw_die_ref context_die)
26938 dw_die_ref ns_context;
26940 if (debug_info_level <= DINFO_LEVEL_TERSE)
26941 return context_die;
26943 /* External declarations in the local scope only need to be emitted
26944 once, not once in the namespace and once in the scope.
26946 This avoids declaring the `extern' below in the
26947 namespace DIE as well as in the innermost scope:
26949 namespace S
26951 int i=5;
26952 int foo()
26954 int i=8;
26955 extern int i;
26956 return i;
26960 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26961 return context_die;
26963 /* If this decl is from an inlined function, then don't try to emit it in its
26964 namespace, as we will get confused. It would have already been emitted
26965 when the abstract instance of the inline function was emitted anyways. */
26966 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26967 return context_die;
26969 ns_context = setup_namespace_context (thing, context_die);
26971 if (ns_context != context_die)
26973 if (is_fortran () || is_dlang ())
26974 return ns_context;
26975 if (DECL_P (thing))
26976 gen_decl_die (thing, NULL, NULL, ns_context);
26977 else
26978 gen_type_die (thing, ns_context);
26980 return context_die;
26983 /* Generate a DIE for a namespace or namespace alias. */
26985 static void
26986 gen_namespace_die (tree decl, dw_die_ref context_die)
26988 dw_die_ref namespace_die;
26990 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26991 they are an alias of. */
26992 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26994 /* Output a real namespace or module. */
26995 context_die = setup_namespace_context (decl, comp_unit_die ());
26996 namespace_die = new_die (is_fortran () || is_dlang ()
26997 ? DW_TAG_module : DW_TAG_namespace,
26998 context_die, decl);
26999 /* For Fortran modules defined in different CU don't add src coords. */
27000 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
27002 const char *name = dwarf2_name (decl, 0);
27003 if (name)
27004 add_name_attribute (namespace_die, name);
27006 else
27007 add_name_and_src_coords_attributes (namespace_die, decl);
27008 if (DECL_EXTERNAL (decl))
27009 add_AT_flag (namespace_die, DW_AT_declaration, 1);
27010 equate_decl_number_to_die (decl, namespace_die);
27012 else
27014 /* Output a namespace alias. */
27016 /* Force out the namespace we are an alias of, if necessary. */
27017 dw_die_ref origin_die
27018 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
27020 if (DECL_FILE_SCOPE_P (decl)
27021 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
27022 context_die = setup_namespace_context (decl, comp_unit_die ());
27023 /* Now create the namespace alias DIE. */
27024 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
27025 add_name_and_src_coords_attributes (namespace_die, decl);
27026 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
27027 equate_decl_number_to_die (decl, namespace_die);
27029 if ((dwarf_version >= 5 || !dwarf_strict)
27030 && lang_hooks.decls.decl_dwarf_attribute (decl,
27031 DW_AT_export_symbols) == 1)
27032 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
27034 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
27035 if (want_pubnames ())
27036 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
27039 /* Generate Dwarf debug information for a decl described by DECL.
27040 The return value is currently only meaningful for PARM_DECLs,
27041 for all other decls it returns NULL.
27043 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
27044 It can be NULL otherwise. */
27046 static dw_die_ref
27047 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
27048 dw_die_ref context_die)
27050 tree decl_or_origin = decl ? decl : origin;
27051 tree class_origin = NULL, ultimate_origin;
27053 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
27054 return NULL;
27056 switch (TREE_CODE (decl_or_origin))
27058 case ERROR_MARK:
27059 break;
27061 case CONST_DECL:
27062 if (!is_fortran () && !is_ada () && !is_dlang ())
27064 /* The individual enumerators of an enum type get output when we output
27065 the Dwarf representation of the relevant enum type itself. */
27066 break;
27069 /* Emit its type. */
27070 gen_type_die (TREE_TYPE (decl), context_die);
27072 /* And its containing namespace. */
27073 context_die = declare_in_namespace (decl, context_die);
27075 gen_const_die (decl, context_die);
27076 break;
27078 case FUNCTION_DECL:
27079 #if 0
27080 /* FIXME */
27081 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
27082 on local redeclarations of global functions. That seems broken. */
27083 if (current_function_decl != decl)
27084 /* This is only a declaration. */;
27085 #endif
27087 /* We should have abstract copies already and should not generate
27088 stray type DIEs in late LTO dumping. */
27089 if (! early_dwarf)
27092 /* If we're emitting a clone, emit info for the abstract instance. */
27093 else if (origin || DECL_ORIGIN (decl) != decl)
27094 dwarf2out_abstract_function (origin
27095 ? DECL_ORIGIN (origin)
27096 : DECL_ABSTRACT_ORIGIN (decl));
27098 /* If we're emitting a possibly inlined function emit it as
27099 abstract instance. */
27100 else if (cgraph_function_possibly_inlined_p (decl)
27101 && ! DECL_ABSTRACT_P (decl)
27102 && ! class_or_namespace_scope_p (context_die)
27103 /* dwarf2out_abstract_function won't emit a die if this is just
27104 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
27105 that case, because that works only if we have a die. */
27106 && DECL_INITIAL (decl) != NULL_TREE)
27107 dwarf2out_abstract_function (decl);
27109 /* Otherwise we're emitting the primary DIE for this decl. */
27110 else if (debug_info_level > DINFO_LEVEL_TERSE)
27112 /* Before we describe the FUNCTION_DECL itself, make sure that we
27113 have its containing type. */
27114 if (!origin)
27115 origin = decl_class_context (decl);
27116 if (origin != NULL_TREE)
27117 gen_type_die (origin, context_die);
27119 /* And its return type. */
27120 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
27122 /* And its virtual context. */
27123 if (DECL_VINDEX (decl) != NULL_TREE)
27124 gen_type_die (DECL_CONTEXT (decl), context_die);
27126 /* Make sure we have a member DIE for decl. */
27127 if (origin != NULL_TREE)
27128 gen_type_die_for_member (origin, decl, context_die);
27130 /* And its containing namespace. */
27131 context_die = declare_in_namespace (decl, context_die);
27134 /* Now output a DIE to represent the function itself. */
27135 if (decl)
27136 gen_subprogram_die (decl, context_die);
27137 break;
27139 case TYPE_DECL:
27140 /* If we are in terse mode, don't generate any DIEs to represent any
27141 actual typedefs. */
27142 if (debug_info_level <= DINFO_LEVEL_TERSE)
27143 break;
27145 /* In the special case of a TYPE_DECL node representing the declaration
27146 of some type tag, if the given TYPE_DECL is marked as having been
27147 instantiated from some other (original) TYPE_DECL node (e.g. one which
27148 was generated within the original definition of an inline function) we
27149 used to generate a special (abbreviated) DW_TAG_structure_type,
27150 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
27151 should be actually referencing those DIEs, as variable DIEs with that
27152 type would be emitted already in the abstract origin, so it was always
27153 removed during unused type prunning. Don't add anything in this
27154 case. */
27155 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
27156 break;
27158 if (is_redundant_typedef (decl))
27159 gen_type_die (TREE_TYPE (decl), context_die);
27160 else
27161 /* Output a DIE to represent the typedef itself. */
27162 gen_typedef_die (decl, context_die);
27163 break;
27165 case LABEL_DECL:
27166 if (debug_info_level >= DINFO_LEVEL_NORMAL)
27167 gen_label_die (decl, context_die);
27168 break;
27170 case VAR_DECL:
27171 case RESULT_DECL:
27172 /* If we are in terse mode, don't generate any DIEs to represent any
27173 variable declarations or definitions unless it is external. */
27174 if (debug_info_level < DINFO_LEVEL_TERSE
27175 || (debug_info_level == DINFO_LEVEL_TERSE
27176 && !TREE_PUBLIC (decl_or_origin)))
27177 break;
27179 if (debug_info_level > DINFO_LEVEL_TERSE)
27181 /* Avoid generating stray type DIEs during late dwarf dumping.
27182 All types have been dumped early. */
27183 if (early_dwarf
27184 /* ??? But in LTRANS we cannot annotate early created variably
27185 modified type DIEs without copying them and adjusting all
27186 references to them. Dump them again as happens for inlining
27187 which copies both the decl and the types. */
27188 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27189 in VLA bound information for example. */
27190 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27191 current_function_decl)))
27193 /* Output any DIEs that are needed to specify the type of this data
27194 object. */
27195 if (decl_by_reference_p (decl_or_origin))
27196 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27197 else
27198 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27201 if (early_dwarf)
27203 /* And its containing type. */
27204 class_origin = decl_class_context (decl_or_origin);
27205 if (class_origin != NULL_TREE)
27206 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
27208 /* And its containing namespace. */
27209 context_die = declare_in_namespace (decl_or_origin, context_die);
27213 /* Now output the DIE to represent the data object itself. This gets
27214 complicated because of the possibility that the VAR_DECL really
27215 represents an inlined instance of a formal parameter for an inline
27216 function. */
27217 ultimate_origin = decl_ultimate_origin (decl_or_origin);
27218 if (ultimate_origin != NULL_TREE
27219 && TREE_CODE (ultimate_origin) == PARM_DECL)
27220 gen_formal_parameter_die (decl, origin,
27221 true /* Emit name attribute. */,
27222 context_die);
27223 else
27224 gen_variable_die (decl, origin, context_die);
27225 break;
27227 case FIELD_DECL:
27228 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
27229 /* Ignore the nameless fields that are used to skip bits but handle C++
27230 anonymous unions and structs. */
27231 if (DECL_NAME (decl) != NULL_TREE
27232 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
27233 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
27235 gen_type_die (member_declared_type (decl), context_die);
27236 gen_field_die (decl, ctx, context_die);
27238 break;
27240 case PARM_DECL:
27241 /* Avoid generating stray type DIEs during late dwarf dumping.
27242 All types have been dumped early. */
27243 if (early_dwarf
27244 /* ??? But in LTRANS we cannot annotate early created variably
27245 modified type DIEs without copying them and adjusting all
27246 references to them. Dump them again as happens for inlining
27247 which copies both the decl and the types. */
27248 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27249 in VLA bound information for example. */
27250 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27251 current_function_decl)))
27253 if (DECL_BY_REFERENCE (decl_or_origin))
27254 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27255 else
27256 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27258 return gen_formal_parameter_die (decl, origin,
27259 true /* Emit name attribute. */,
27260 context_die);
27262 case NAMESPACE_DECL:
27263 if (dwarf_version >= 3 || !dwarf_strict)
27264 gen_namespace_die (decl, context_die);
27265 break;
27267 case IMPORTED_DECL:
27268 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
27269 DECL_CONTEXT (decl), context_die);
27270 break;
27272 case NAMELIST_DECL:
27273 gen_namelist_decl (DECL_NAME (decl), context_die,
27274 NAMELIST_DECL_ASSOCIATED_DECL (decl));
27275 break;
27277 default:
27278 /* Probably some frontend-internal decl. Assume we don't care. */
27279 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
27280 break;
27283 return NULL;
27286 /* Output initial debug information for global DECL. Called at the
27287 end of the parsing process.
27289 This is the initial debug generation process. As such, the DIEs
27290 generated may be incomplete. A later debug generation pass
27291 (dwarf2out_late_global_decl) will augment the information generated
27292 in this pass (e.g., with complete location info). */
27294 static void
27295 dwarf2out_early_global_decl (tree decl)
27297 set_early_dwarf s;
27299 /* gen_decl_die() will set DECL_ABSTRACT because
27300 cgraph_function_possibly_inlined_p() returns true. This is in
27301 turn will cause DW_AT_inline attributes to be set.
27303 This happens because at early dwarf generation, there is no
27304 cgraph information, causing cgraph_function_possibly_inlined_p()
27305 to return true. Trick cgraph_function_possibly_inlined_p()
27306 while we generate dwarf early. */
27307 bool save = symtab->global_info_ready;
27308 symtab->global_info_ready = true;
27310 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27311 other DECLs and they can point to template types or other things
27312 that dwarf2out can't handle when done via dwarf2out_decl. */
27313 if (TREE_CODE (decl) != TYPE_DECL
27314 && TREE_CODE (decl) != PARM_DECL)
27316 if (TREE_CODE (decl) == FUNCTION_DECL)
27318 tree save_fndecl = current_function_decl;
27320 /* For nested functions, make sure we have DIEs for the parents first
27321 so that all nested DIEs are generated at the proper scope in the
27322 first shot. */
27323 tree context = decl_function_context (decl);
27324 if (context != NULL)
27326 dw_die_ref context_die = lookup_decl_die (context);
27327 current_function_decl = context;
27329 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27330 enough so that it lands in its own context. This avoids type
27331 pruning issues later on. */
27332 if (context_die == NULL || is_declaration_die (context_die))
27333 dwarf2out_early_global_decl (context);
27336 /* Emit an abstract origin of a function first. This happens
27337 with C++ constructor clones for example and makes
27338 dwarf2out_abstract_function happy which requires the early
27339 DIE of the abstract instance to be present. */
27340 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27341 dw_die_ref origin_die;
27342 if (origin != NULL
27343 /* Do not emit the DIE multiple times but make sure to
27344 process it fully here in case we just saw a declaration. */
27345 && ((origin_die = lookup_decl_die (origin)) == NULL
27346 || is_declaration_die (origin_die)))
27348 current_function_decl = origin;
27349 dwarf2out_decl (origin);
27352 /* Emit the DIE for decl but avoid doing that multiple times. */
27353 dw_die_ref old_die;
27354 if ((old_die = lookup_decl_die (decl)) == NULL
27355 || is_declaration_die (old_die))
27357 current_function_decl = decl;
27358 dwarf2out_decl (decl);
27361 current_function_decl = save_fndecl;
27363 else
27364 dwarf2out_decl (decl);
27366 symtab->global_info_ready = save;
27369 /* Return whether EXPR is an expression with the following pattern:
27370 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27372 static bool
27373 is_trivial_indirect_ref (tree expr)
27375 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27376 return false;
27378 tree nop = TREE_OPERAND (expr, 0);
27379 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27380 return false;
27382 tree int_cst = TREE_OPERAND (nop, 0);
27383 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27386 /* Output debug information for global decl DECL. Called from
27387 toplev.cc after compilation proper has finished. */
27389 static void
27390 dwarf2out_late_global_decl (tree decl)
27392 /* Fill-in any location information we were unable to determine
27393 on the first pass. */
27394 if (VAR_P (decl))
27396 dw_die_ref die = lookup_decl_die (decl);
27398 /* We may have to generate full debug late for LTO in case debug
27399 was not enabled at compile-time or the target doesn't support
27400 the LTO early debug scheme. */
27401 if (! die && in_lto_p
27402 /* Function scope variables are emitted when emitting the
27403 DIE for the function. */
27404 && ! local_function_static (decl))
27405 dwarf2out_decl (decl);
27406 else if (die)
27408 /* We get called via the symtab code invoking late_global_decl
27409 for symbols that are optimized out.
27411 Do not add locations for those, except if they have a
27412 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27413 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27414 INDIRECT_REF expression, as this could generate relocations to
27415 text symbols in LTO object files, which is invalid. */
27416 varpool_node *node = varpool_node::get (decl);
27417 if ((! node || ! node->definition)
27418 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27419 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27420 tree_add_const_value_attribute_for_decl (die, decl);
27421 else
27422 add_location_or_const_value_attribute (die, decl, false);
27427 /* Output debug information for type decl DECL. Called from toplev.cc
27428 and from language front ends (to record built-in types). */
27429 static void
27430 dwarf2out_type_decl (tree decl, int local)
27432 if (!local)
27434 set_early_dwarf s;
27435 dwarf2out_decl (decl);
27439 /* Output debug information for imported module or decl DECL.
27440 NAME is non-NULL name in the lexical block if the decl has been renamed.
27441 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27442 that DECL belongs to.
27443 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27444 static void
27445 dwarf2out_imported_module_or_decl_1 (tree decl,
27446 tree name,
27447 tree lexical_block,
27448 dw_die_ref lexical_block_die)
27450 expanded_location xloc;
27451 dw_die_ref imported_die = NULL;
27452 dw_die_ref at_import_die;
27454 if (TREE_CODE (decl) == IMPORTED_DECL)
27456 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27457 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27458 gcc_assert (decl);
27460 else
27461 xloc = expand_location (input_location);
27463 if (TREE_CODE (decl) == TYPE_DECL)
27465 at_import_die = force_type_die (TREE_TYPE (decl));
27466 /* For namespace N { typedef void T; } using N::T; base_type_die
27467 returns NULL, but DW_TAG_imported_declaration requires
27468 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27469 if (!at_import_die)
27471 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27472 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27473 at_import_die = lookup_type_die (TREE_TYPE (decl));
27474 gcc_assert (at_import_die);
27477 else
27479 at_import_die = lookup_decl_die (decl);
27480 if (!at_import_die)
27482 /* If we're trying to avoid duplicate debug info, we may not have
27483 emitted the member decl for this field. Emit it now. */
27484 if (TREE_CODE (decl) == FIELD_DECL)
27486 tree type = DECL_CONTEXT (decl);
27488 if (TYPE_CONTEXT (type)
27489 && TYPE_P (TYPE_CONTEXT (type))
27490 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27491 DINFO_USAGE_DIR_USE))
27492 return;
27493 gen_type_die_for_member (type, decl,
27494 get_context_die (TYPE_CONTEXT (type)));
27496 if (TREE_CODE (decl) == CONST_DECL)
27498 /* Individual enumerators of an enum type do not get output here
27499 (see gen_decl_die), so we cannot call force_decl_die. */
27500 if (!is_fortran () && !is_ada () && !is_dlang ())
27501 return;
27503 if (TREE_CODE (decl) == NAMELIST_DECL)
27504 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27505 get_context_die (DECL_CONTEXT (decl)),
27506 NULL_TREE);
27507 else
27508 at_import_die = force_decl_die (decl);
27512 if (TREE_CODE (decl) == NAMESPACE_DECL)
27514 if (dwarf_version >= 3 || !dwarf_strict)
27515 imported_die = new_die (DW_TAG_imported_module,
27516 lexical_block_die,
27517 lexical_block);
27518 else
27519 return;
27521 else
27522 imported_die = new_die (DW_TAG_imported_declaration,
27523 lexical_block_die,
27524 lexical_block);
27526 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27527 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27528 if (debug_column_info && xloc.column)
27529 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27530 if (name)
27531 add_AT_string (imported_die, DW_AT_name,
27532 IDENTIFIER_POINTER (name));
27533 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27536 /* Output debug information for imported module or decl DECL.
27537 NAME is non-NULL name in context if the decl has been renamed.
27538 CHILD is true if decl is one of the renamed decls as part of
27539 importing whole module.
27540 IMPLICIT is set if this hook is called for an implicit import
27541 such as inline namespace. */
27543 static void
27544 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27545 bool child, bool implicit)
27547 /* dw_die_ref at_import_die; */
27548 dw_die_ref scope_die;
27550 if (debug_info_level <= DINFO_LEVEL_TERSE)
27551 return;
27553 gcc_assert (decl);
27555 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27556 should be enough, for DWARF4 and older even if we emit as extension
27557 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27558 for the benefit of consumers unaware of DW_AT_export_symbols. */
27559 if (implicit
27560 && dwarf_version >= 5
27561 && lang_hooks.decls.decl_dwarf_attribute (decl,
27562 DW_AT_export_symbols) == 1)
27563 return;
27565 set_early_dwarf s;
27567 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27568 We need decl DIE for reference and scope die. First, get DIE for the decl
27569 itself. */
27571 /* Get the scope die for decl context. Use comp_unit_die for global module
27572 or decl. If die is not found for non globals, force new die. */
27573 if (context
27574 && TYPE_P (context)
27575 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27576 return;
27578 scope_die = get_context_die (context);
27580 if (child)
27582 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27583 there is nothing we can do, here. */
27584 if (dwarf_version < 3 && dwarf_strict)
27585 return;
27587 gcc_assert (scope_die->die_child);
27588 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27589 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27590 scope_die = scope_die->die_child;
27593 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27594 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27597 /* Output debug information for namelists. */
27599 static dw_die_ref
27600 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27602 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27603 tree value;
27604 unsigned i;
27606 if (debug_info_level <= DINFO_LEVEL_TERSE)
27607 return NULL;
27609 gcc_assert (scope_die != NULL);
27610 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27611 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27613 /* If there are no item_decls, we have a nondefining namelist, e.g.
27614 with USE association; hence, set DW_AT_declaration. */
27615 if (item_decls == NULL_TREE)
27617 add_AT_flag (nml_die, DW_AT_declaration, 1);
27618 return nml_die;
27621 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27623 nml_item_ref_die = lookup_decl_die (value);
27624 if (!nml_item_ref_die)
27625 nml_item_ref_die = force_decl_die (value);
27627 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27628 add_AT_die_ref (nml_item_die, DW_AT_namelist_item, nml_item_ref_die);
27630 return nml_die;
27634 /* Write the debugging output for DECL and return the DIE. */
27636 static void
27637 dwarf2out_decl (tree decl)
27639 dw_die_ref context_die = comp_unit_die ();
27641 switch (TREE_CODE (decl))
27643 case ERROR_MARK:
27644 return;
27646 case FUNCTION_DECL:
27647 /* If we're a nested function, initially use a parent of NULL; if we're
27648 a plain function, this will be fixed up in decls_for_scope. If
27649 we're a method, it will be ignored, since we already have a DIE.
27650 Avoid doing this late though since clones of class methods may
27651 otherwise end up in limbo and create type DIEs late. */
27652 if (early_dwarf
27653 && decl_function_context (decl)
27654 /* But if we're in terse mode, we don't care about scope. */
27655 && debug_info_level > DINFO_LEVEL_TERSE)
27656 context_die = NULL;
27657 break;
27659 case VAR_DECL:
27660 /* For local statics lookup proper context die. */
27661 if (local_function_static (decl))
27662 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27664 /* If we are in terse mode, don't generate any DIEs to represent any
27665 variable declarations or definitions unless it is external. */
27666 if (debug_info_level < DINFO_LEVEL_TERSE
27667 || (debug_info_level == DINFO_LEVEL_TERSE
27668 && !TREE_PUBLIC (decl)))
27669 return;
27670 break;
27672 case CONST_DECL:
27673 if (debug_info_level <= DINFO_LEVEL_TERSE)
27674 return;
27675 if (!is_fortran () && !is_ada () && !is_dlang ())
27676 return;
27677 if (TREE_STATIC (decl) && decl_function_context (decl))
27678 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27679 break;
27681 case NAMESPACE_DECL:
27682 case IMPORTED_DECL:
27683 if (debug_info_level <= DINFO_LEVEL_TERSE)
27684 return;
27685 if (lookup_decl_die (decl) != NULL)
27686 return;
27687 break;
27689 case TYPE_DECL:
27690 /* Don't emit stubs for types unless they are needed by other DIEs. */
27691 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27692 return;
27694 /* Don't bother trying to generate any DIEs to represent any of the
27695 normal built-in types for the language we are compiling. */
27696 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27697 return;
27699 /* If we are in terse mode, don't generate any DIEs for types. */
27700 if (debug_info_level <= DINFO_LEVEL_TERSE)
27701 return;
27703 /* If we're a function-scope tag, initially use a parent of NULL;
27704 this will be fixed up in decls_for_scope. */
27705 if (decl_function_context (decl))
27706 context_die = NULL;
27708 break;
27710 case NAMELIST_DECL:
27711 break;
27713 default:
27714 return;
27717 gen_decl_die (decl, NULL, NULL, context_die);
27719 if (flag_checking)
27721 dw_die_ref die = lookup_decl_die (decl);
27722 if (die)
27723 check_die (die);
27727 /* Write the debugging output for DECL. */
27729 static void
27730 dwarf2out_function_decl (tree decl)
27732 dwarf2out_decl (decl);
27733 call_arg_locations = NULL;
27734 call_arg_loc_last = NULL;
27735 call_site_count = -1;
27736 tail_call_site_count = -1;
27737 decl_loc_table->empty ();
27738 cached_dw_loc_list_table->empty ();
27741 /* Output a marker (i.e. a label) for the beginning of the generated code for
27742 a lexical block. */
27744 static void
27745 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27746 unsigned int blocknum)
27748 switch_to_section (current_function_section ());
27749 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27752 /* Output a marker (i.e. a label) for the end of the generated code for a
27753 lexical block. */
27755 static void
27756 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27758 switch_to_section (current_function_section ());
27759 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27762 /* Returns true if it is appropriate not to emit any debugging
27763 information for BLOCK, because it doesn't contain any instructions.
27765 Don't allow this for blocks with nested functions or local classes
27766 as we would end up with orphans, and in the presence of scheduling
27767 we may end up calling them anyway. */
27769 static bool
27770 dwarf2out_ignore_block (const_tree block)
27772 tree decl;
27773 unsigned int i;
27775 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27776 if (TREE_CODE (decl) == FUNCTION_DECL
27777 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27778 return false;
27779 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27781 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27782 if (TREE_CODE (decl) == FUNCTION_DECL
27783 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27784 return false;
27787 return true;
27790 /* Hash table routines for file_hash. */
27792 bool
27793 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27795 return filename_cmp (p1->key, p2) == 0;
27798 hashval_t
27799 dwarf_file_hasher::hash (dwarf_file_data *p)
27801 return htab_hash_string (p->key);
27804 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27805 dwarf2out.cc) and return its "index". The index of each (known) filename is
27806 just a unique number which is associated with only that one filename. We
27807 need such numbers for the sake of generating labels (in the .debug_sfnames
27808 section) and references to those files numbers (in the .debug_srcinfo
27809 and .debug_macinfo sections). If the filename given as an argument is not
27810 found in our current list, add it to the list and assign it the next
27811 available unique index number. */
27813 static struct dwarf_file_data *
27814 lookup_filename (const char *file_name)
27816 struct dwarf_file_data * created;
27818 if (!file_name)
27819 return NULL;
27821 if (!file_name[0])
27822 file_name = "<stdin>";
27824 dwarf_file_data **slot
27825 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27826 INSERT);
27827 if (*slot)
27828 return *slot;
27830 created = ggc_alloc<dwarf_file_data> ();
27831 created->key = file_name;
27832 created->filename = remap_debug_filename (file_name);
27833 created->emitted_number = 0;
27834 *slot = created;
27835 return created;
27838 /* If the assembler will construct the file table, then translate the compiler
27839 internal file table number into the assembler file table number, and emit
27840 a .file directive if we haven't already emitted one yet. The file table
27841 numbers are different because we prune debug info for unused variables and
27842 types, which may include filenames. */
27844 static int
27845 maybe_emit_file (struct dwarf_file_data * fd)
27847 if (! fd->emitted_number)
27849 if (last_emitted_file)
27850 fd->emitted_number = last_emitted_file->emitted_number + 1;
27851 else
27852 fd->emitted_number = 1;
27853 last_emitted_file = fd;
27855 if (output_asm_line_debug_info ())
27857 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27858 output_quoted_string (asm_out_file, fd->filename);
27859 fputc ('\n', asm_out_file);
27863 return fd->emitted_number;
27866 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27867 That generation should happen after function debug info has been
27868 generated. The value of the attribute is the constant value of ARG. */
27870 static void
27871 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27873 die_arg_entry entry;
27875 if (!die || !arg)
27876 return;
27878 gcc_assert (early_dwarf);
27880 if (!tmpl_value_parm_die_table)
27881 vec_alloc (tmpl_value_parm_die_table, 32);
27883 entry.die = die;
27884 entry.arg = arg;
27885 vec_safe_push (tmpl_value_parm_die_table, entry);
27888 /* Return TRUE if T is an instance of generic type, FALSE
27889 otherwise. */
27891 static bool
27892 generic_type_p (tree t)
27894 if (t == NULL_TREE || !TYPE_P (t))
27895 return false;
27896 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27899 /* Schedule the generation of the generic parameter dies for the
27900 instance of generic type T. The proper generation itself is later
27901 done by gen_scheduled_generic_parms_dies. */
27903 static void
27904 schedule_generic_params_dies_gen (tree t)
27906 if (!generic_type_p (t))
27907 return;
27909 gcc_assert (early_dwarf);
27911 if (!generic_type_instances)
27912 vec_alloc (generic_type_instances, 256);
27914 vec_safe_push (generic_type_instances, t);
27917 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27918 by append_entry_to_tmpl_value_parm_die_table. This function must
27919 be called after function DIEs have been generated. */
27921 static void
27922 gen_remaining_tmpl_value_param_die_attribute (void)
27924 if (tmpl_value_parm_die_table)
27926 unsigned i, j;
27927 die_arg_entry *e;
27929 /* We do this in two phases - first get the cases we can
27930 handle during early-finish, preserving those we cannot
27931 (containing symbolic constants where we don't yet know
27932 whether we are going to output the referenced symbols).
27933 For those we try again at late-finish. */
27934 j = 0;
27935 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27937 if (!e->die->removed
27938 && !tree_add_const_value_attribute (e->die, e->arg))
27940 dw_loc_descr_ref loc = NULL;
27941 if (! early_dwarf
27942 && (dwarf_version >= 5 || !dwarf_strict))
27943 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27944 if (loc)
27945 add_AT_loc (e->die, DW_AT_location, loc);
27946 else
27947 (*tmpl_value_parm_die_table)[j++] = *e;
27950 tmpl_value_parm_die_table->truncate (j);
27954 /* Generate generic parameters DIEs for instances of generic types
27955 that have been previously scheduled by
27956 schedule_generic_params_dies_gen. This function must be called
27957 after all the types of the CU have been laid out. */
27959 static void
27960 gen_scheduled_generic_parms_dies (void)
27962 unsigned i;
27963 tree t;
27965 if (!generic_type_instances)
27966 return;
27968 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27969 if (COMPLETE_TYPE_P (t))
27970 gen_generic_params_dies (t);
27972 generic_type_instances = NULL;
27976 /* Replace DW_AT_name for the decl with name. */
27978 static void
27979 dwarf2out_set_name (tree decl, tree name)
27981 dw_die_ref die;
27982 dw_attr_node *attr;
27983 const char *dname;
27985 die = TYPE_SYMTAB_DIE (decl);
27986 if (!die)
27987 return;
27989 dname = dwarf2_name (name, 0);
27990 if (!dname)
27991 return;
27993 attr = get_AT (die, DW_AT_name);
27994 if (attr)
27996 struct indirect_string_node *node;
27998 node = find_AT_string (dname);
27999 /* replace the string. */
28000 attr->dw_attr_val.v.val_str = node;
28003 else
28004 add_name_attribute (die, dname);
28007 /* True if before or during processing of the first function being emitted. */
28008 static bool in_first_function_p = true;
28009 /* True if loc_note during dwarf2out_var_location call might still be
28010 before first real instruction at address equal to .Ltext0. */
28011 static bool maybe_at_text_label_p = true;
28012 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
28013 static unsigned int first_loclabel_num_not_at_text_label;
28015 /* Look ahead for a real insn. */
28017 static rtx_insn *
28018 dwarf2out_next_real_insn (rtx_insn *loc_note)
28020 rtx_insn *next_real = NEXT_INSN (loc_note);
28022 while (next_real)
28023 if (INSN_P (next_real))
28024 break;
28025 else
28026 next_real = NEXT_INSN (next_real);
28028 return next_real;
28031 /* Called by the final INSN scan whenever we see a var location. We
28032 use it to drop labels in the right places, and throw the location in
28033 our lookup table. */
28035 static void
28036 dwarf2out_var_location (rtx_insn *loc_note)
28038 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
28039 struct var_loc_node *newloc;
28040 rtx_insn *next_real;
28041 rtx_insn *call_insn = NULL;
28042 static const char *last_label;
28043 static const char *last_postcall_label;
28044 static bool last_in_cold_section_p;
28045 static rtx_insn *expected_next_loc_note;
28046 tree decl;
28047 bool var_loc_p;
28048 var_loc_view view = 0;
28050 if (!NOTE_P (loc_note))
28052 if (CALL_P (loc_note))
28054 maybe_reset_location_view (loc_note, cur_line_info_table);
28055 call_site_count++;
28056 if (SIBLING_CALL_P (loc_note))
28057 tail_call_site_count++;
28058 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
28060 call_insn = loc_note;
28061 loc_note = NULL;
28062 var_loc_p = false;
28064 next_real = dwarf2out_next_real_insn (call_insn);
28065 cached_next_real_insn = NULL;
28066 goto create_label;
28068 if (optimize == 0 && !flag_var_tracking)
28070 /* When the var-tracking pass is not running, there is no note
28071 for indirect calls whose target is compile-time known. In this
28072 case, process such calls specifically so that we generate call
28073 sites for them anyway. */
28074 rtx x = PATTERN (loc_note);
28075 if (GET_CODE (x) == PARALLEL)
28076 x = XVECEXP (x, 0, 0);
28077 if (GET_CODE (x) == SET)
28078 x = SET_SRC (x);
28079 if (GET_CODE (x) == CALL)
28080 x = XEXP (x, 0);
28081 if (!MEM_P (x)
28082 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
28083 || !SYMBOL_REF_DECL (XEXP (x, 0))
28084 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
28085 != FUNCTION_DECL))
28087 call_insn = loc_note;
28088 loc_note = NULL;
28089 var_loc_p = false;
28091 next_real = dwarf2out_next_real_insn (call_insn);
28092 cached_next_real_insn = NULL;
28093 goto create_label;
28097 else if (!debug_variable_location_views)
28098 gcc_unreachable ();
28099 else
28100 maybe_reset_location_view (loc_note, cur_line_info_table);
28102 return;
28105 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
28106 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
28107 return;
28109 /* Optimize processing a large consecutive sequence of location
28110 notes so we don't spend too much time in next_real_insn. If the
28111 next insn is another location note, remember the next_real_insn
28112 calculation for next time. */
28113 next_real = cached_next_real_insn;
28114 if (next_real)
28116 if (expected_next_loc_note != loc_note)
28117 next_real = NULL;
28120 if (! next_real)
28121 next_real = dwarf2out_next_real_insn (loc_note);
28123 if (next_real)
28125 rtx_insn *next_note = NEXT_INSN (loc_note);
28126 while (next_note != next_real)
28128 if (! next_note->deleted ()
28129 && NOTE_P (next_note)
28130 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
28131 break;
28132 next_note = NEXT_INSN (next_note);
28135 if (next_note == next_real)
28136 cached_next_real_insn = NULL;
28137 else
28139 expected_next_loc_note = next_note;
28140 cached_next_real_insn = next_real;
28143 else
28144 cached_next_real_insn = NULL;
28146 /* If there are no instructions which would be affected by this note,
28147 don't do anything. */
28148 if (var_loc_p
28149 && next_real == NULL_RTX
28150 && !NOTE_DURING_CALL_P (loc_note))
28151 return;
28153 create_label:
28155 if (next_real == NULL_RTX)
28156 next_real = get_last_insn ();
28158 /* If there were any real insns between note we processed last time
28159 and this note (or if it is the first note), clear
28160 last_{,postcall_}label so that they are not reused this time. */
28161 if (last_var_location_insn == NULL_RTX
28162 || last_var_location_insn != next_real
28163 || last_in_cold_section_p != in_cold_section_p)
28165 last_label = NULL;
28166 last_postcall_label = NULL;
28169 if (var_loc_p)
28171 const char *label
28172 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
28173 view = cur_line_info_table->view;
28174 decl = NOTE_VAR_LOCATION_DECL (loc_note);
28175 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
28176 if (newloc == NULL)
28177 return;
28179 else
28181 decl = NULL_TREE;
28182 newloc = NULL;
28185 /* If there were no real insns between note we processed last time
28186 and this note, use the label we emitted last time. Otherwise
28187 create a new label and emit it. */
28188 if (last_label == NULL)
28190 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
28191 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
28192 loclabel_num++;
28193 last_label = ggc_strdup (loclabel);
28194 /* See if loclabel might be equal to .Ltext0. If yes,
28195 bump first_loclabel_num_not_at_text_label. */
28196 if (!have_multiple_function_sections
28197 && in_first_function_p
28198 && maybe_at_text_label_p)
28200 static rtx_insn *last_start;
28201 rtx_insn *insn;
28202 for (insn = loc_note; insn; insn = previous_insn (insn))
28203 if (insn == last_start)
28204 break;
28205 else if (!NONDEBUG_INSN_P (insn))
28206 continue;
28207 else
28209 rtx body = PATTERN (insn);
28210 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
28211 continue;
28212 /* Inline asm could occupy zero bytes. */
28213 else if (GET_CODE (body) == ASM_INPUT
28214 || asm_noperands (body) >= 0)
28215 continue;
28216 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
28217 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
28218 continue;
28219 #endif
28220 else
28222 /* Assume insn has non-zero length. */
28223 maybe_at_text_label_p = false;
28224 break;
28227 if (maybe_at_text_label_p)
28229 last_start = loc_note;
28230 first_loclabel_num_not_at_text_label = loclabel_num;
28235 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
28236 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
28238 if (!var_loc_p)
28240 struct call_arg_loc_node *ca_loc
28241 = ggc_cleared_alloc<call_arg_loc_node> ();
28242 rtx_insn *prev = call_insn;
28244 ca_loc->call_arg_loc_note
28245 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
28246 ca_loc->next = NULL;
28247 ca_loc->label = last_label;
28248 gcc_assert (prev
28249 && (CALL_P (prev)
28250 || (NONJUMP_INSN_P (prev)
28251 && GET_CODE (PATTERN (prev)) == SEQUENCE
28252 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
28253 if (!CALL_P (prev))
28254 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
28255 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
28257 /* Look for a SYMBOL_REF in the "prev" instruction. */
28258 rtx x = get_call_rtx_from (prev);
28259 if (x)
28261 /* Try to get the call symbol, if any. */
28262 if (MEM_P (XEXP (x, 0)))
28263 x = XEXP (x, 0);
28264 /* First, look for a memory access to a symbol_ref. */
28265 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
28266 && SYMBOL_REF_DECL (XEXP (x, 0))
28267 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
28268 ca_loc->symbol_ref = XEXP (x, 0);
28269 /* Otherwise, look at a compile-time known user-level function
28270 declaration. */
28271 else if (MEM_P (x)
28272 && MEM_EXPR (x)
28273 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
28274 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
28277 ca_loc->block = insn_scope (prev);
28278 if (call_arg_locations)
28279 call_arg_loc_last->next = ca_loc;
28280 else
28281 call_arg_locations = ca_loc;
28282 call_arg_loc_last = ca_loc;
28284 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
28286 newloc->label = last_label;
28287 newloc->view = view;
28289 else
28291 if (!last_postcall_label)
28293 sprintf (loclabel, "%s-1", last_label);
28294 last_postcall_label = ggc_strdup (loclabel);
28296 newloc->label = last_postcall_label;
28297 /* ??? This view is at last_label, not last_label-1, but we
28298 could only assume view at last_label-1 is zero if we could
28299 assume calls always have length greater than one. This is
28300 probably true in general, though there might be a rare
28301 exception to this rule, e.g. if a call insn is optimized out
28302 by target magic. Then, even the -1 in the label will be
28303 wrong, which might invalidate the range. Anyway, using view,
28304 though technically possibly incorrect, will work as far as
28305 ranges go: since L-1 is in the middle of the call insn,
28306 (L-1).0 and (L-1).V shouldn't make any difference, and having
28307 the loclist entry refer to the .loc entry might be useful, so
28308 leave it like this. */
28309 newloc->view = view;
28312 if (var_loc_p && flag_debug_asm)
28314 const char *name, *sep, *patstr;
28315 if (decl && DECL_NAME (decl))
28316 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28317 else
28318 name = "";
28319 if (NOTE_VAR_LOCATION_LOC (loc_note))
28321 sep = " => ";
28322 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28324 else
28326 sep = " ";
28327 patstr = "RESET";
28329 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28330 name, sep, patstr);
28333 last_var_location_insn = next_real;
28334 last_in_cold_section_p = in_cold_section_p;
28337 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28338 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28339 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28340 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28341 BLOCK_FRAGMENT_ORIGIN links. */
28342 static bool
28343 block_within_block_p (tree block, tree outer, bool bothways)
28345 if (block == outer)
28346 return true;
28348 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28349 for (tree context = BLOCK_SUPERCONTEXT (block);
28350 context != outer;
28351 context = BLOCK_SUPERCONTEXT (context))
28352 if (!context || TREE_CODE (context) != BLOCK)
28353 return false;
28355 if (!bothways)
28356 return true;
28358 /* Now check that each block is actually referenced by its
28359 parent. */
28360 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28361 context = BLOCK_SUPERCONTEXT (context))
28363 if (BLOCK_FRAGMENT_ORIGIN (context))
28365 gcc_assert (!BLOCK_SUBBLOCKS (context));
28366 context = BLOCK_FRAGMENT_ORIGIN (context);
28368 for (tree sub = BLOCK_SUBBLOCKS (context);
28369 sub != block;
28370 sub = BLOCK_CHAIN (sub))
28371 if (!sub)
28372 return false;
28373 if (context == outer)
28374 return true;
28375 else
28376 block = context;
28380 /* Called during final while assembling the marker of the entry point
28381 for an inlined function. */
28383 static void
28384 dwarf2out_inline_entry (tree block)
28386 gcc_assert (debug_inline_points);
28388 /* If we can't represent it, don't bother. */
28389 if (!(dwarf_version >= 3 || !dwarf_strict))
28390 return;
28392 gcc_assert (DECL_P (block_ultimate_origin (block)));
28394 /* Sanity check the block tree. This would catch a case in which
28395 BLOCK got removed from the tree reachable from the outermost
28396 lexical block, but got retained in markers. It would still link
28397 back to its parents, but some ancestor would be missing a link
28398 down the path to the sub BLOCK. If the block got removed, its
28399 BLOCK_NUMBER will not be a usable value. */
28400 if (flag_checking)
28401 gcc_assert (block_within_block_p (block,
28402 DECL_INITIAL (current_function_decl),
28403 true));
28405 gcc_assert (inlined_function_outer_scope_p (block));
28406 gcc_assert (!lookup_block_die (block));
28408 if (BLOCK_FRAGMENT_ORIGIN (block))
28409 block = BLOCK_FRAGMENT_ORIGIN (block);
28410 /* Can the entry point ever not be at the beginning of an
28411 unfragmented lexical block? */
28412 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28413 || (cur_line_info_table
28414 && !ZERO_VIEW_P (cur_line_info_table->view))))
28415 return;
28417 if (!inline_entry_data_table)
28418 inline_entry_data_table
28419 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28422 inline_entry_data **iedp
28423 = inline_entry_data_table->find_slot_with_hash (block,
28424 htab_hash_pointer (block),
28425 INSERT);
28426 if (*iedp)
28427 /* ??? Ideally, we'd record all entry points for the same inlined
28428 function (some may have been duplicated by e.g. unrolling), but
28429 we have no way to represent that ATM. */
28430 return;
28432 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28433 ied->block = block;
28434 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28435 ied->label_num = BLOCK_NUMBER (block);
28436 if (cur_line_info_table)
28437 ied->view = cur_line_info_table->view;
28439 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28440 BLOCK_NUMBER (block));
28443 /* Called from finalize_size_functions for size functions so that their body
28444 can be encoded in the debug info to describe the layout of variable-length
28445 structures. */
28447 static void
28448 dwarf2out_size_function (tree decl)
28450 set_early_dwarf s;
28451 function_to_dwarf_procedure (decl);
28454 /* Note in one location list that text section has changed. */
28457 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28459 var_loc_list *list = *slot;
28460 if (list->first)
28461 list->last_before_switch
28462 = list->last->next ? list->last->next : list->last;
28463 return 1;
28466 /* Note in all location lists that text section has changed. */
28468 static void
28469 var_location_switch_text_section (void)
28471 if (decl_loc_table == NULL)
28472 return;
28474 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28477 /* Create a new line number table. */
28479 static dw_line_info_table *
28480 new_line_info_table (void)
28482 dw_line_info_table *table;
28484 table = ggc_cleared_alloc<dw_line_info_table> ();
28485 table->file_num = 1;
28486 table->line_num = 1;
28487 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28488 FORCE_RESET_NEXT_VIEW (table->view);
28489 table->symviews_since_reset = 0;
28491 return table;
28494 /* Lookup the "current" table into which we emit line info, so
28495 that we don't have to do it for every source line. */
28497 static void
28498 set_cur_line_info_table (section *sec)
28500 dw_line_info_table *table;
28502 if (sec == text_section)
28503 table = text_section_line_info;
28504 else if (sec == cold_text_section)
28506 table = cold_text_section_line_info;
28507 if (!table)
28509 cold_text_section_line_info = table = new_line_info_table ();
28510 table->end_label = cold_end_label;
28513 else
28515 const char *end_label;
28517 if (crtl->has_bb_partition)
28519 if (in_cold_section_p)
28520 end_label = crtl->subsections.cold_section_end_label;
28521 else
28522 end_label = crtl->subsections.hot_section_end_label;
28524 else
28526 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28527 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28528 current_function_funcdef_no);
28529 end_label = ggc_strdup (label);
28532 table = new_line_info_table ();
28533 table->end_label = end_label;
28535 vec_safe_push (separate_line_info, table);
28538 if (output_asm_line_debug_info ())
28539 table->is_stmt = (cur_line_info_table
28540 ? cur_line_info_table->is_stmt
28541 : DWARF_LINE_DEFAULT_IS_STMT_START);
28542 cur_line_info_table = table;
28546 /* We need to reset the locations at the beginning of each
28547 function. We can't do this in the end_function hook, because the
28548 declarations that use the locations won't have been output when
28549 that hook is called. Also compute have_multiple_function_sections here. */
28551 static void
28552 dwarf2out_begin_function (tree fun)
28554 section *sec = function_section (fun);
28556 if (sec != text_section)
28557 have_multiple_function_sections = true;
28559 if (crtl->has_bb_partition && !cold_text_section)
28561 gcc_assert (current_function_decl == fun);
28562 cold_text_section = unlikely_text_section ();
28563 switch_to_section (cold_text_section);
28564 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28565 switch_to_section (sec);
28568 call_site_count = 0;
28569 tail_call_site_count = 0;
28571 set_cur_line_info_table (sec);
28572 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28575 /* Helper function of dwarf2out_end_function, called only after emitting
28576 the very first function into assembly. Check if some .debug_loc range
28577 might end with a .LVL* label that could be equal to .Ltext0.
28578 In that case we must force using absolute addresses in .debug_loc ranges,
28579 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28580 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28581 list terminator.
28582 Set have_multiple_function_sections to true in that case and
28583 terminate htab traversal. */
28586 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28588 var_loc_list *entry = *slot;
28589 struct var_loc_node *node;
28591 node = entry->first;
28592 if (node && node->next && node->next->label)
28594 unsigned int i;
28595 const char *label = node->next->label;
28596 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28598 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28600 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28601 if (strcmp (label, loclabel) == 0)
28603 have_multiple_function_sections = true;
28604 return 0;
28608 return 1;
28611 /* Hook called after emitting a function into assembly.
28612 This does something only for the very first function emitted. */
28614 static void
28615 dwarf2out_end_function (unsigned int)
28617 if (in_first_function_p
28618 && !have_multiple_function_sections
28619 && first_loclabel_num_not_at_text_label
28620 && decl_loc_table)
28621 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28622 in_first_function_p = false;
28623 maybe_at_text_label_p = false;
28626 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28627 front-ends register a translation unit even before dwarf2out_init is
28628 called. */
28629 static tree main_translation_unit = NULL_TREE;
28631 /* Hook called by front-ends after they built their main translation unit.
28632 Associate comp_unit_die to UNIT. */
28634 static void
28635 dwarf2out_register_main_translation_unit (tree unit)
28637 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28638 && main_translation_unit == NULL_TREE);
28639 main_translation_unit = unit;
28640 /* If dwarf2out_init has not been called yet, it will perform the association
28641 itself looking at main_translation_unit. */
28642 if (decl_die_table != NULL)
28643 equate_decl_number_to_die (unit, comp_unit_die ());
28646 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28648 static void
28649 push_dw_line_info_entry (dw_line_info_table *table,
28650 enum dw_line_info_opcode opcode, unsigned int val)
28652 dw_line_info_entry e;
28653 e.opcode = opcode;
28654 e.val = val;
28655 vec_safe_push (table->entries, e);
28658 /* Output a label to mark the beginning of a source code line entry
28659 and record information relating to this source line, in
28660 'line_info_table' for later output of the .debug_line section. */
28661 /* ??? The discriminator parameter ought to be unsigned. */
28663 static void
28664 dwarf2out_source_line (unsigned int line, unsigned int column,
28665 const char *filename,
28666 int discriminator, bool is_stmt)
28668 unsigned int file_num;
28669 dw_line_info_table *table;
28670 static var_loc_view lvugid;
28672 /* 'line_info_table' information gathering is not needed when the debug
28673 info level is set to the lowest value. Also, the current DWARF-based
28674 debug formats do not use this info. */
28675 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28676 return;
28678 table = cur_line_info_table;
28680 if (line == 0)
28682 if (debug_variable_location_views
28683 && output_asm_line_debug_info ()
28684 && table && !RESETTING_VIEW_P (table->view))
28686 /* If we're using the assembler to compute view numbers, we
28687 can't issue a .loc directive for line zero, so we can't
28688 get a view number at this point. We might attempt to
28689 compute it from the previous view, or equate it to a
28690 subsequent view (though it might not be there!), but
28691 since we're omitting the line number entry, we might as
28692 well omit the view number as well. That means pretending
28693 it's a view number zero, which might very well turn out
28694 to be correct. ??? Extend the assembler so that the
28695 compiler could emit e.g. ".locview .LVU#", to output a
28696 view without changing line number information. We'd then
28697 have to count it in symviews_since_reset; when it's omitted,
28698 it doesn't count. */
28699 if (!zero_view_p)
28700 zero_view_p = BITMAP_GGC_ALLOC ();
28701 bitmap_set_bit (zero_view_p, table->view);
28702 if (flag_debug_asm)
28704 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28705 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28706 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28707 ASM_COMMENT_START);
28708 assemble_name (asm_out_file, label);
28709 putc ('\n', asm_out_file);
28711 table->view = ++lvugid;
28713 return;
28716 /* The discriminator column was added in dwarf4. Simplify the below
28717 by simply removing it if we're not supposed to output it. */
28718 if (dwarf_version < 4 && dwarf_strict)
28719 discriminator = 0;
28721 if (!debug_column_info)
28722 column = 0;
28724 file_num = maybe_emit_file (lookup_filename (filename));
28726 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28727 the debugger has used the second (possibly duplicate) line number
28728 at the beginning of the function to mark the end of the prologue.
28729 We could eliminate any other duplicates within the function. For
28730 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28731 that second line number entry. */
28732 /* Recall that this end-of-prologue indication is *not* the same thing
28733 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28734 to which the hook corresponds, follows the last insn that was
28735 emitted by gen_prologue. What we need is to precede the first insn
28736 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28737 insn that corresponds to something the user wrote. These may be
28738 very different locations once scheduling is enabled. */
28740 if (0 && file_num == table->file_num
28741 && line == table->line_num
28742 && column == table->column_num
28743 && discriminator == table->discrim_num
28744 && is_stmt == table->is_stmt)
28745 return;
28747 switch_to_section (current_function_section ());
28749 /* If requested, emit something human-readable. */
28750 if (flag_debug_asm)
28752 if (debug_column_info)
28753 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28754 filename, line, column);
28755 else
28756 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28757 filename, line);
28760 if (output_asm_line_debug_info ())
28762 /* Emit the .loc directive understood by GNU as. */
28763 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28764 file_num, line, is_stmt, discriminator */
28765 fputs ("\t.loc ", asm_out_file);
28766 fprint_ul (asm_out_file, file_num);
28767 putc (' ', asm_out_file);
28768 fprint_ul (asm_out_file, line);
28769 putc (' ', asm_out_file);
28770 fprint_ul (asm_out_file, column);
28772 if (is_stmt != table->is_stmt)
28774 #if HAVE_GAS_LOC_STMT
28775 fputs (" is_stmt ", asm_out_file);
28776 putc (is_stmt ? '1' : '0', asm_out_file);
28777 #endif
28779 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28781 gcc_assert (discriminator > 0);
28782 fputs (" discriminator ", asm_out_file);
28783 fprint_ul (asm_out_file, (unsigned long) discriminator);
28785 if (debug_variable_location_views)
28787 if (!RESETTING_VIEW_P (table->view))
28789 table->symviews_since_reset++;
28790 if (table->symviews_since_reset > symview_upper_bound)
28791 symview_upper_bound = table->symviews_since_reset;
28792 /* When we're using the assembler to compute view
28793 numbers, we output symbolic labels after "view" in
28794 .loc directives, and the assembler will set them for
28795 us, so that we can refer to the view numbers in
28796 location lists. The only exceptions are when we know
28797 a view will be zero: "-0" is a forced reset, used
28798 e.g. in the beginning of functions, whereas "0" tells
28799 the assembler to check that there was a PC change
28800 since the previous view, in a way that implicitly
28801 resets the next view. */
28802 fputs (" view ", asm_out_file);
28803 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28804 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28805 assemble_name (asm_out_file, label);
28806 table->view = ++lvugid;
28808 else
28810 table->symviews_since_reset = 0;
28811 if (FORCE_RESETTING_VIEW_P (table->view))
28812 fputs (" view -0", asm_out_file);
28813 else
28814 fputs (" view 0", asm_out_file);
28815 /* Mark the present view as a zero view. Earlier debug
28816 binds may have already added its id to loclists to be
28817 emitted later, so we can't reuse the id for something
28818 else. However, it's good to know whether a view is
28819 known to be zero, because then we may be able to
28820 optimize out locviews that are all zeros, so take
28821 note of it in zero_view_p. */
28822 if (!zero_view_p)
28823 zero_view_p = BITMAP_GGC_ALLOC ();
28824 bitmap_set_bit (zero_view_p, lvugid);
28825 table->view = ++lvugid;
28828 putc ('\n', asm_out_file);
28830 else
28832 unsigned int label_num = ++line_info_label_num;
28834 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28836 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28837 push_dw_line_info_entry (table, LI_adv_address, label_num);
28838 else
28839 push_dw_line_info_entry (table, LI_set_address, label_num);
28840 if (debug_variable_location_views)
28842 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28843 if (resetting)
28844 table->view = 0;
28846 if (flag_debug_asm)
28847 fprintf (asm_out_file, "\t%s view %s%d\n",
28848 ASM_COMMENT_START,
28849 resetting ? "-" : "",
28850 table->view);
28852 table->view++;
28854 if (file_num != table->file_num)
28855 push_dw_line_info_entry (table, LI_set_file, file_num);
28856 if (discriminator != table->discrim_num)
28857 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28858 if (is_stmt != table->is_stmt)
28859 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28860 push_dw_line_info_entry (table, LI_set_line, line);
28861 if (debug_column_info)
28862 push_dw_line_info_entry (table, LI_set_column, column);
28865 table->file_num = file_num;
28866 table->line_num = line;
28867 table->column_num = column;
28868 table->discrim_num = discriminator;
28869 table->is_stmt = is_stmt;
28870 table->in_use = true;
28873 /* Record a source file location for a DECL_IGNORED_P function. */
28875 static void
28876 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28877 const char *filename)
28879 dw_fde_ref fde = cfun->fde;
28881 fde->ignored_debug = false;
28882 set_cur_line_info_table (function_section (fde->decl));
28884 dwarf2out_source_line (line, column, filename, 0, true);
28887 /* Record the beginning of a new source file. */
28889 static void
28890 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28892 #ifdef CODEVIEW_DEBUGGING_INFO
28893 if (codeview_debuginfo_p ())
28894 codeview_start_source_file (filename);
28895 #endif
28897 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28899 macinfo_entry e;
28900 e.code = DW_MACINFO_start_file;
28901 e.lineno = lineno;
28902 e.info = ggc_strdup (filename);
28903 vec_safe_push (macinfo_table, e);
28907 /* Record the end of a source file. */
28909 static void
28910 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28912 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28914 macinfo_entry e;
28915 e.code = DW_MACINFO_end_file;
28916 e.lineno = lineno;
28917 e.info = NULL;
28918 vec_safe_push (macinfo_table, e);
28922 /* Called from debug_define in toplev.cc. The `buffer' parameter contains
28923 the tail part of the directive line, i.e. the part which is past the
28924 initial whitespace, #, whitespace, directive-name, whitespace part. */
28926 static void
28927 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28928 const char *buffer ATTRIBUTE_UNUSED)
28930 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28932 macinfo_entry e;
28933 /* Insert a dummy first entry to be able to optimize the whole
28934 predefined macro block using DW_MACRO_import. */
28935 if (macinfo_table->is_empty () && lineno <= 1)
28937 e.code = 0;
28938 e.lineno = 0;
28939 e.info = NULL;
28940 vec_safe_push (macinfo_table, e);
28942 e.code = DW_MACINFO_define;
28943 e.lineno = lineno;
28944 e.info = ggc_strdup (buffer);
28945 vec_safe_push (macinfo_table, e);
28949 /* Called from debug_undef in toplev.cc. The `buffer' parameter contains
28950 the tail part of the directive line, i.e. the part which is past the
28951 initial whitespace, #, whitespace, directive-name, whitespace part. */
28953 static void
28954 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28955 const char *buffer ATTRIBUTE_UNUSED)
28957 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28959 macinfo_entry e;
28960 /* Insert a dummy first entry to be able to optimize the whole
28961 predefined macro block using DW_MACRO_import. */
28962 if (macinfo_table->is_empty () && lineno <= 1)
28964 e.code = 0;
28965 e.lineno = 0;
28966 e.info = NULL;
28967 vec_safe_push (macinfo_table, e);
28969 e.code = DW_MACINFO_undef;
28970 e.lineno = lineno;
28971 e.info = ggc_strdup (buffer);
28972 vec_safe_push (macinfo_table, e);
28976 /* Helpers to manipulate hash table of CUs. */
28978 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28980 static inline hashval_t hash (const macinfo_entry *);
28981 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28984 inline hashval_t
28985 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28987 return htab_hash_string (entry->info);
28990 inline bool
28991 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28992 const macinfo_entry *entry2)
28994 return !strcmp (entry1->info, entry2->info);
28997 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28999 /* Output a single .debug_macinfo entry. */
29001 static void
29002 output_macinfo_op (macinfo_entry *ref)
29004 int file_num;
29005 size_t len;
29006 struct indirect_string_node *node;
29007 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29008 struct dwarf_file_data *fd;
29010 switch (ref->code)
29012 case DW_MACINFO_start_file:
29013 fd = lookup_filename (ref->info);
29014 file_num = maybe_emit_file (fd);
29015 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
29016 dw2_asm_output_data_uleb128 (ref->lineno,
29017 "Included from line number "
29018 HOST_WIDE_INT_PRINT_UNSIGNED,
29019 ref->lineno);
29020 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
29021 break;
29022 case DW_MACINFO_end_file:
29023 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
29024 break;
29025 case DW_MACINFO_define:
29026 case DW_MACINFO_undef:
29027 len = strlen (ref->info) + 1;
29028 if ((!dwarf_strict || dwarf_version >= 5)
29029 && len > (size_t) dwarf_offset_size
29030 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29031 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29033 if (dwarf_split_debug_info && dwarf_version >= 5)
29034 ref->code = ref->code == DW_MACINFO_define
29035 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
29036 else
29037 ref->code = ref->code == DW_MACINFO_define
29038 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
29039 output_macinfo_op (ref);
29040 return;
29042 dw2_asm_output_data (1, ref->code,
29043 ref->code == DW_MACINFO_define
29044 ? "Define macro" : "Undefine macro");
29045 dw2_asm_output_data_uleb128 (ref->lineno,
29046 "At line number "
29047 HOST_WIDE_INT_PRINT_UNSIGNED,
29048 ref->lineno);
29049 dw2_asm_output_nstring (ref->info, -1, "The macro");
29050 break;
29051 case DW_MACRO_define_strp:
29052 dw2_asm_output_data (1, ref->code, "Define macro strp");
29053 goto do_DW_MACRO_define_strpx;
29054 case DW_MACRO_undef_strp:
29055 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
29056 goto do_DW_MACRO_define_strpx;
29057 case DW_MACRO_define_strx:
29058 dw2_asm_output_data (1, ref->code, "Define macro strx");
29059 goto do_DW_MACRO_define_strpx;
29060 case DW_MACRO_undef_strx:
29061 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
29062 /* FALLTHRU */
29063 do_DW_MACRO_define_strpx:
29064 /* NB: dwarf2out_finish performs:
29065 1. save_macinfo_strings
29066 2. hash table traverse of index_string
29067 3. output_macinfo -> output_macinfo_op
29068 4. output_indirect_strings
29069 -> hash table traverse of output_index_string
29071 When output_macinfo_op is called, all index strings have been
29072 added to hash table by save_macinfo_strings and we can't pass
29073 INSERT to find_slot_with_hash which may expand hash table, even
29074 if no insertion is needed, and change hash table traverse order
29075 between index_string and output_index_string. */
29076 node = find_AT_string (ref->info, NO_INSERT);
29077 gcc_assert (node
29078 && (node->form == DW_FORM_strp
29079 || node->form == dwarf_FORM (DW_FORM_strx)));
29080 dw2_asm_output_data_uleb128 (ref->lineno,
29081 "At line number "
29082 HOST_WIDE_INT_PRINT_UNSIGNED,
29083 ref->lineno);
29084 if (node->form == DW_FORM_strp)
29085 dw2_asm_output_offset (dwarf_offset_size, node->label,
29086 debug_str_section, "The macro: \"%s\"",
29087 ref->info);
29088 else
29089 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
29090 ref->info);
29091 break;
29092 case DW_MACRO_import:
29093 dw2_asm_output_data (1, ref->code, "Import");
29094 ASM_GENERATE_INTERNAL_LABEL (label,
29095 DEBUG_MACRO_SECTION_LABEL,
29096 ref->lineno + macinfo_label_base);
29097 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
29098 break;
29099 default:
29100 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
29101 ASM_COMMENT_START, (unsigned long) ref->code);
29102 break;
29106 /* Attempt to make a sequence of define/undef macinfo ops shareable with
29107 other compilation unit .debug_macinfo sections. IDX is the first
29108 index of a define/undef, return the number of ops that should be
29109 emitted in a comdat .debug_macinfo section and emit
29110 a DW_MACRO_import entry referencing it.
29111 If the define/undef entry should be emitted normally, return 0. */
29113 static unsigned
29114 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
29115 macinfo_hash_type **macinfo_htab)
29117 macinfo_entry *first, *second, *cur, *inc;
29118 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
29119 unsigned char checksum[16];
29120 struct md5_ctx ctx;
29121 char *grp_name, *tail;
29122 const char *base;
29123 unsigned int i, count, encoded_filename_len, linebuf_len;
29124 macinfo_entry **slot;
29126 first = &(*macinfo_table)[idx];
29127 second = &(*macinfo_table)[idx + 1];
29129 /* Optimize only if there are at least two consecutive define/undef ops,
29130 and either all of them are before first DW_MACINFO_start_file
29131 with lineno {0,1} (i.e. predefined macro block), or all of them are
29132 in some included header file. */
29133 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
29134 return 0;
29135 if (vec_safe_is_empty (files))
29137 if (first->lineno > 1 || second->lineno > 1)
29138 return 0;
29140 else if (first->lineno == 0)
29141 return 0;
29143 /* Find the last define/undef entry that can be grouped together
29144 with first and at the same time compute md5 checksum of their
29145 codes, linenumbers and strings. */
29146 md5_init_ctx (&ctx);
29147 for (i = idx; macinfo_table->iterate (i, &cur); i++)
29148 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
29149 break;
29150 else if (vec_safe_is_empty (files) && cur->lineno > 1)
29151 break;
29152 else
29154 unsigned char code = cur->code;
29155 md5_process_bytes (&code, 1, &ctx);
29156 checksum_uleb128 (cur->lineno, &ctx);
29157 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
29159 md5_finish_ctx (&ctx, checksum);
29160 count = i - idx;
29162 /* From the containing include filename (if any) pick up just
29163 usable characters from its basename. */
29164 if (vec_safe_is_empty (files))
29165 base = "";
29166 else
29167 base = lbasename (files->last ().info);
29168 for (encoded_filename_len = 0, i = 0; base[i]; i++)
29169 if (ISIDNUM (base[i]) || base[i] == '.')
29170 encoded_filename_len++;
29171 /* Count . at the end. */
29172 if (encoded_filename_len)
29173 encoded_filename_len++;
29175 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
29176 linebuf_len = strlen (linebuf);
29178 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
29179 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
29180 + 16 * 2 + 1);
29181 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
29182 tail = grp_name + 4;
29183 if (encoded_filename_len)
29185 for (i = 0; base[i]; i++)
29186 if (ISIDNUM (base[i]) || base[i] == '.')
29187 *tail++ = base[i];
29188 *tail++ = '.';
29190 memcpy (tail, linebuf, linebuf_len);
29191 tail += linebuf_len;
29192 *tail++ = '.';
29193 for (i = 0; i < 16; i++)
29194 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
29196 /* Construct a macinfo_entry for DW_MACRO_import
29197 in the empty vector entry before the first define/undef. */
29198 inc = &(*macinfo_table)[idx - 1];
29199 inc->code = DW_MACRO_import;
29200 inc->lineno = 0;
29201 inc->info = ggc_strdup (grp_name);
29202 if (!*macinfo_htab)
29203 *macinfo_htab = new macinfo_hash_type (10);
29204 /* Avoid emitting duplicates. */
29205 slot = (*macinfo_htab)->find_slot (inc, INSERT);
29206 if (*slot != NULL)
29208 inc->code = 0;
29209 inc->info = NULL;
29210 /* If such an entry has been used before, just emit
29211 a DW_MACRO_import op. */
29212 inc = *slot;
29213 output_macinfo_op (inc);
29214 /* And clear all macinfo_entry in the range to avoid emitting them
29215 in the second pass. */
29216 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
29218 cur->code = 0;
29219 cur->info = NULL;
29222 else
29224 *slot = inc;
29225 inc->lineno = (*macinfo_htab)->elements ();
29226 output_macinfo_op (inc);
29228 return count;
29231 /* Save any strings needed by the macinfo table in the debug str
29232 table. All strings must be collected into the table by the time
29233 index_string is called. */
29235 static void
29236 save_macinfo_strings (void)
29238 unsigned len;
29239 unsigned i;
29240 macinfo_entry *ref;
29242 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
29244 switch (ref->code)
29246 /* Match the logic in output_macinfo_op to decide on
29247 indirect strings. */
29248 case DW_MACINFO_define:
29249 case DW_MACINFO_undef:
29250 len = strlen (ref->info) + 1;
29251 if ((!dwarf_strict || dwarf_version >= 5)
29252 && len > (unsigned) dwarf_offset_size
29253 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29254 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29255 set_indirect_string (find_AT_string (ref->info));
29256 break;
29257 case DW_MACINFO_start_file:
29258 /* -gsplit-dwarf -g3 will also output filename as indirect
29259 string. */
29260 if (!dwarf_split_debug_info)
29261 break;
29262 /* Fall through. */
29263 case DW_MACRO_define_strp:
29264 case DW_MACRO_undef_strp:
29265 case DW_MACRO_define_strx:
29266 case DW_MACRO_undef_strx:
29267 set_indirect_string (find_AT_string (ref->info));
29268 break;
29269 default:
29270 break;
29275 /* Output macinfo section(s). */
29277 static void
29278 output_macinfo (const char *debug_line_label, bool early_lto_debug)
29280 unsigned i;
29281 unsigned long length = vec_safe_length (macinfo_table);
29282 macinfo_entry *ref;
29283 vec<macinfo_entry, va_gc> *files = NULL;
29284 macinfo_hash_type *macinfo_htab = NULL;
29285 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29287 if (! length)
29288 return;
29290 /* output_macinfo* uses these interchangeably. */
29291 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
29292 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
29293 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
29294 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
29296 /* AIX Assembler inserts the length, so adjust the reference to match the
29297 offset expected by debuggers. */
29298 strcpy (dl_section_ref, debug_line_label);
29299 if (XCOFF_DEBUGGING_INFO)
29300 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29302 /* For .debug_macro emit the section header. */
29303 if (!dwarf_strict || dwarf_version >= 5)
29305 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29306 "DWARF macro version number");
29307 if (dwarf_offset_size == 8)
29308 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
29309 else
29310 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
29311 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
29312 debug_line_section, NULL);
29315 /* In the first loop, it emits the primary .debug_macinfo section
29316 and after each emitted op the macinfo_entry is cleared.
29317 If a longer range of define/undef ops can be optimized using
29318 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29319 the vector before the first define/undef in the range and the
29320 whole range of define/undef ops is not emitted and kept. */
29321 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29323 switch (ref->code)
29325 case DW_MACINFO_start_file:
29326 vec_safe_push (files, *ref);
29327 break;
29328 case DW_MACINFO_end_file:
29329 if (!vec_safe_is_empty (files))
29330 files->pop ();
29331 break;
29332 case DW_MACINFO_define:
29333 case DW_MACINFO_undef:
29334 if ((!dwarf_strict || dwarf_version >= 5)
29335 && !dwarf_split_debug_info
29336 && HAVE_COMDAT_GROUP
29337 && vec_safe_length (files) != 1
29338 && i > 0
29339 && i + 1 < length
29340 && (*macinfo_table)[i - 1].code == 0)
29342 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29343 if (count)
29345 i += count - 1;
29346 continue;
29349 break;
29350 case 0:
29351 /* A dummy entry may be inserted at the beginning to be able
29352 to optimize the whole block of predefined macros. */
29353 if (i == 0)
29354 continue;
29355 default:
29356 break;
29358 output_macinfo_op (ref);
29359 ref->info = NULL;
29360 ref->code = 0;
29363 if (!macinfo_htab)
29364 return;
29366 /* Save the number of transparent includes so we can adjust the
29367 label number for the fat LTO object DWARF. */
29368 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29370 delete macinfo_htab;
29371 macinfo_htab = NULL;
29373 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29374 terminate the current chain and switch to a new comdat .debug_macinfo
29375 section and emit the define/undef entries within it. */
29376 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29377 switch (ref->code)
29379 case 0:
29380 continue;
29381 case DW_MACRO_import:
29383 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29384 tree comdat_key = get_identifier (ref->info);
29385 /* Terminate the previous .debug_macinfo section. */
29386 dw2_asm_output_data (1, 0, "End compilation unit");
29387 targetm.asm_out.named_section (debug_macinfo_section_name,
29388 SECTION_DEBUG
29389 | SECTION_LINKONCE
29390 | (early_lto_debug
29391 ? SECTION_EXCLUDE : 0),
29392 comdat_key);
29393 ASM_GENERATE_INTERNAL_LABEL (label,
29394 DEBUG_MACRO_SECTION_LABEL,
29395 ref->lineno + macinfo_label_base);
29396 ASM_OUTPUT_LABEL (asm_out_file, label);
29397 ref->code = 0;
29398 ref->info = NULL;
29399 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29400 "DWARF macro version number");
29401 if (dwarf_offset_size == 8)
29402 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29403 else
29404 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29406 break;
29407 case DW_MACINFO_define:
29408 case DW_MACINFO_undef:
29409 output_macinfo_op (ref);
29410 ref->code = 0;
29411 ref->info = NULL;
29412 break;
29413 default:
29414 gcc_unreachable ();
29417 macinfo_label_base += macinfo_label_base_adj;
29420 /* As init_sections_and_labels may get called multiple times, have a
29421 generation count for labels. */
29422 static unsigned init_sections_and_labels_generation;
29424 /* Initialize the various sections and labels for dwarf output and prefix
29425 them with PREFIX if non-NULL. Returns the generation (zero based
29426 number of times function was called). */
29428 static unsigned
29429 init_sections_and_labels (bool early_lto_debug)
29431 if (early_lto_debug)
29433 if (!dwarf_split_debug_info)
29435 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29436 SECTION_DEBUG | SECTION_EXCLUDE,
29437 NULL);
29438 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29439 SECTION_DEBUG | SECTION_EXCLUDE,
29440 NULL);
29441 debug_macinfo_section_name
29442 = ((dwarf_strict && dwarf_version < 5)
29443 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29444 debug_macinfo_section = get_section (debug_macinfo_section_name,
29445 SECTION_DEBUG
29446 | SECTION_EXCLUDE, NULL);
29448 else
29450 /* ??? Which of the following do we need early? */
29451 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29452 SECTION_DEBUG | SECTION_EXCLUDE,
29453 NULL);
29454 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29455 SECTION_DEBUG | SECTION_EXCLUDE,
29456 NULL);
29457 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29458 SECTION_DEBUG
29459 | SECTION_EXCLUDE, NULL);
29460 debug_skeleton_abbrev_section
29461 = get_section (DEBUG_LTO_ABBREV_SECTION,
29462 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29463 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29464 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29465 init_sections_and_labels_generation);
29467 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29468 stay in the main .o, but the skeleton_line goes into the split
29469 off dwo. */
29470 debug_skeleton_line_section
29471 = get_section (DEBUG_LTO_LINE_SECTION,
29472 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29473 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29474 DEBUG_SKELETON_LINE_SECTION_LABEL,
29475 init_sections_and_labels_generation);
29476 debug_str_offsets_section
29477 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29478 SECTION_DEBUG | SECTION_EXCLUDE,
29479 NULL);
29480 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29481 DEBUG_SKELETON_INFO_SECTION_LABEL,
29482 init_sections_and_labels_generation);
29483 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29484 DEBUG_STR_DWO_SECTION_FLAGS,
29485 NULL);
29486 debug_macinfo_section_name
29487 = ((dwarf_strict && dwarf_version < 5)
29488 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29489 debug_macinfo_section = get_section (debug_macinfo_section_name,
29490 SECTION_DEBUG | SECTION_EXCLUDE,
29491 NULL);
29493 /* For macro info and the file table we have to refer to a
29494 debug_line section. */
29495 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29496 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29497 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29498 DEBUG_LINE_SECTION_LABEL,
29499 init_sections_and_labels_generation);
29501 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29502 DEBUG_STR_SECTION_FLAGS
29503 | SECTION_EXCLUDE, NULL);
29504 if (!dwarf_split_debug_info)
29505 debug_line_str_section
29506 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29507 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29509 else
29511 if (!dwarf_split_debug_info)
29513 debug_info_section = get_section (DEBUG_INFO_SECTION,
29514 SECTION_DEBUG, NULL);
29515 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29516 SECTION_DEBUG, NULL);
29517 debug_loc_section = get_section (dwarf_version >= 5
29518 ? DEBUG_LOCLISTS_SECTION
29519 : DEBUG_LOC_SECTION,
29520 SECTION_DEBUG, NULL);
29521 debug_macinfo_section_name
29522 = ((dwarf_strict && dwarf_version < 5)
29523 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29524 debug_macinfo_section = get_section (debug_macinfo_section_name,
29525 SECTION_DEBUG, NULL);
29527 else
29529 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29530 SECTION_DEBUG | SECTION_EXCLUDE,
29531 NULL);
29532 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29533 SECTION_DEBUG | SECTION_EXCLUDE,
29534 NULL);
29535 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29536 SECTION_DEBUG, NULL);
29537 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29538 SECTION_DEBUG, NULL);
29539 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29540 SECTION_DEBUG, NULL);
29541 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29542 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29543 init_sections_and_labels_generation);
29545 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29546 stay in the main .o, but the skeleton_line goes into the
29547 split off dwo. */
29548 debug_skeleton_line_section
29549 = get_section (DEBUG_DWO_LINE_SECTION,
29550 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29551 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29552 DEBUG_SKELETON_LINE_SECTION_LABEL,
29553 init_sections_and_labels_generation);
29554 debug_str_offsets_section
29555 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29556 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29557 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29558 DEBUG_SKELETON_INFO_SECTION_LABEL,
29559 init_sections_and_labels_generation);
29560 debug_loc_section = get_section (dwarf_version >= 5
29561 ? DEBUG_DWO_LOCLISTS_SECTION
29562 : DEBUG_DWO_LOC_SECTION,
29563 SECTION_DEBUG | SECTION_EXCLUDE,
29564 NULL);
29565 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29566 DEBUG_STR_DWO_SECTION_FLAGS,
29567 NULL);
29568 debug_macinfo_section_name
29569 = ((dwarf_strict && dwarf_version < 5)
29570 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29571 debug_macinfo_section = get_section (debug_macinfo_section_name,
29572 SECTION_DEBUG | SECTION_EXCLUDE,
29573 NULL);
29574 if (dwarf_version >= 5)
29575 debug_ranges_dwo_section
29576 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29577 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29579 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29580 SECTION_DEBUG, NULL);
29581 debug_line_section = get_section (DEBUG_LINE_SECTION,
29582 SECTION_DEBUG, NULL);
29583 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29584 SECTION_DEBUG, NULL);
29585 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29586 SECTION_DEBUG, NULL);
29587 debug_str_section = get_section (DEBUG_STR_SECTION,
29588 DEBUG_STR_SECTION_FLAGS, NULL);
29589 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29590 || asm_outputs_debug_line_str ())
29591 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29592 DEBUG_STR_SECTION_FLAGS, NULL);
29594 debug_ranges_section = get_section (dwarf_version >= 5
29595 ? DEBUG_RNGLISTS_SECTION
29596 : DEBUG_RANGES_SECTION,
29597 SECTION_DEBUG, NULL);
29598 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29599 SECTION_DEBUG, NULL);
29602 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29603 DEBUG_ABBREV_SECTION_LABEL,
29604 init_sections_and_labels_generation);
29605 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29606 DEBUG_INFO_SECTION_LABEL,
29607 init_sections_and_labels_generation);
29608 info_section_emitted = false;
29609 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29610 DEBUG_LINE_SECTION_LABEL,
29611 init_sections_and_labels_generation);
29612 /* There are up to 6 unique ranges labels per generation.
29613 See also output_rnglists. */
29614 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29615 DEBUG_RANGES_SECTION_LABEL,
29616 init_sections_and_labels_generation * 6);
29617 if (dwarf_version >= 5 && dwarf_split_debug_info)
29618 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29619 DEBUG_RANGES_SECTION_LABEL,
29620 1 + init_sections_and_labels_generation * 6);
29621 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29622 DEBUG_ADDR_SECTION_LABEL,
29623 init_sections_and_labels_generation);
29624 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29625 (dwarf_strict && dwarf_version < 5)
29626 ? DEBUG_MACINFO_SECTION_LABEL
29627 : DEBUG_MACRO_SECTION_LABEL,
29628 init_sections_and_labels_generation);
29629 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29630 init_sections_and_labels_generation);
29632 ++init_sections_and_labels_generation;
29633 return init_sections_and_labels_generation - 1;
29636 /* Set up for Dwarf output at the start of compilation. */
29638 static void
29639 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29641 /* Allocate the file_table. */
29642 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29644 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29645 /* Allocate the decl_die_table. */
29646 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29648 /* Allocate the decl_loc_table. */
29649 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29651 /* Allocate the cached_dw_loc_list_table. */
29652 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29654 /* Allocate the initial hunk of the abbrev_die_table. */
29655 vec_alloc (abbrev_die_table, 256);
29656 /* Zero-th entry is allocated, but unused. */
29657 abbrev_die_table->quick_push (NULL);
29659 /* Allocate the dwarf_proc_stack_usage_map. */
29660 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29662 /* Allocate the pubtypes and pubnames vectors. */
29663 vec_alloc (pubname_table, 32);
29664 vec_alloc (pubtype_table, 32);
29666 vec_alloc (incomplete_types, 64);
29668 vec_alloc (used_rtx_array, 32);
29670 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29671 vec_alloc (macinfo_table, 64);
29672 #endif
29674 /* If front-ends already registered a main translation unit but we were not
29675 ready to perform the association, do this now. */
29676 if (main_translation_unit != NULL_TREE)
29677 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29680 /* Called before compile () starts outputtting functions, variables
29681 and toplevel asms into assembly. */
29683 static void
29684 dwarf2out_assembly_start (void)
29686 if (text_section_line_info)
29687 return;
29689 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29690 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29691 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29692 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29693 COLD_TEXT_SECTION_LABEL, 0);
29694 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29696 switch_to_section (text_section);
29697 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29698 #endif
29700 /* Make sure the line number table for .text always exists. */
29701 text_section_line_info = new_line_info_table ();
29702 text_section_line_info->end_label = text_end_label;
29704 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29705 cur_line_info_table = text_section_line_info;
29706 #endif
29708 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29709 && dwarf2out_do_cfi_asm ()
29710 && !dwarf2out_do_eh_frame ())
29711 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29713 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29714 if (output_asm_line_debug_info () && dwarf_version >= 5)
29716 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29717 tell it the comp_dir and main file name for the zero entry
29718 line table. */
29719 const char *comp_dir, *filename0;
29721 comp_dir = comp_dir_string ();
29722 if (comp_dir == NULL)
29723 comp_dir = "";
29725 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29726 if (filename0 == NULL)
29727 filename0 = "";
29729 fprintf (asm_out_file, "\t.file 0 ");
29730 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29731 fputc (' ', asm_out_file);
29732 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29733 fputc ('\n', asm_out_file);
29735 else
29736 #endif
29737 /* Work around for PR101575: output a dummy .file directive. */
29738 if (!last_emitted_file && dwarf_debuginfo_p ()
29739 && debug_info_level >= DINFO_LEVEL_TERSE)
29741 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29743 if (filename0 == NULL)
29744 filename0 = "<dummy>";
29745 maybe_emit_file (lookup_filename (filename0));
29749 /* A helper function for dwarf2out_finish called through
29750 htab_traverse. Assign a string its index. All strings must be
29751 collected into the table by the time index_string is called,
29752 because the indexing code relies on htab_traverse to traverse nodes
29753 in the same order for each run. */
29756 index_string (indirect_string_node **h, unsigned int *index)
29758 indirect_string_node *node = *h;
29760 find_string_form (node);
29761 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29763 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29764 node->index = *index;
29765 *index += 1;
29767 return 1;
29770 /* A helper function for output_indirect_strings called through
29771 htab_traverse. Output the offset to a string and update the
29772 current offset. */
29775 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29777 indirect_string_node *node = *h;
29779 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29781 /* Assert that this node has been assigned an index. */
29782 gcc_assert (node->index != NO_INDEX_ASSIGNED
29783 && node->index != NOT_INDEXED);
29784 dw2_asm_output_data (dwarf_offset_size, *offset,
29785 "indexed string 0x%x: %s", node->index, node->str);
29786 *offset += strlen (node->str) + 1;
29788 return 1;
29791 /* A helper function for dwarf2out_finish called through
29792 htab_traverse. Output the indexed string. */
29795 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29797 struct indirect_string_node *node = *h;
29799 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29801 /* Assert that the strings are output in the same order as their
29802 indexes were assigned. */
29803 gcc_assert (*cur_idx == node->index);
29804 assemble_string (node->str, strlen (node->str) + 1);
29805 *cur_idx += 1;
29807 return 1;
29810 /* A helper function for output_indirect_strings. Counts the number
29811 of index strings offsets. Must match the logic of the functions
29812 output_index_string[_offsets] above. */
29814 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29816 struct indirect_string_node *node = *h;
29818 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29819 *last_idx += 1;
29820 return 1;
29823 /* A helper function for dwarf2out_finish called through
29824 htab_traverse. Emit one queued .debug_str string. */
29827 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29829 struct indirect_string_node *node = *h;
29831 node->form = find_string_form (node);
29832 if (node->form == form && node->refcount > 0)
29834 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29835 assemble_string (node->str, strlen (node->str) + 1);
29838 return 1;
29841 /* Output the indexed string table. */
29843 static void
29844 output_indirect_strings (void)
29846 switch_to_section (debug_str_section);
29847 if (!dwarf_split_debug_info)
29848 debug_str_hash->traverse<enum dwarf_form,
29849 output_indirect_string> (DW_FORM_strp);
29850 else
29852 unsigned int offset = 0;
29853 unsigned int cur_idx = 0;
29855 if (skeleton_debug_str_hash)
29856 skeleton_debug_str_hash->traverse<enum dwarf_form,
29857 output_indirect_string> (DW_FORM_strp);
29859 switch_to_section (debug_str_offsets_section);
29860 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29861 header. Note that we don't need to generate a label to the
29862 actual index table following the header here, because this is
29863 for the split dwarf case only. In an .dwo file there is only
29864 one string offsets table (and one debug info section). But
29865 if we would start using string offset tables for the main (or
29866 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29867 pointing to the actual index after the header. Split dwarf
29868 units will never have a string offsets base attribute. When
29869 a split unit is moved into a .dwp file the string offsets can
29870 be found through the .debug_cu_index section table. */
29871 if (dwarf_version >= 5)
29873 unsigned int last_idx = 0;
29874 unsigned long str_offsets_length;
29876 debug_str_hash->traverse_noresize
29877 <unsigned int *, count_index_strings> (&last_idx);
29878 str_offsets_length = last_idx * dwarf_offset_size + 4;
29879 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29880 dw2_asm_output_data (4, 0xffffffff,
29881 "Escape value for 64-bit DWARF extension");
29882 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29883 "Length of string offsets unit");
29884 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29885 dw2_asm_output_data (2, 0, "Header zero padding");
29887 debug_str_hash->traverse_noresize
29888 <unsigned int *, output_index_string_offset> (&offset);
29889 switch_to_section (debug_str_dwo_section);
29890 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29891 (&cur_idx);
29895 /* Callback for htab_traverse to assign an index to an entry in the
29896 table, and to write that entry to the .debug_addr section. */
29899 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29901 addr_table_entry *entry = *slot;
29903 if (entry->refcount == 0)
29905 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29906 || entry->index == NOT_INDEXED);
29907 return 1;
29910 gcc_assert (entry->index == *cur_index);
29911 (*cur_index)++;
29913 switch (entry->kind)
29915 case ate_kind_rtx:
29916 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29917 "0x%x", entry->index);
29918 break;
29919 case ate_kind_rtx_dtprel:
29920 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29921 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29922 DWARF2_ADDR_SIZE,
29923 entry->addr.rtl);
29924 fputc ('\n', asm_out_file);
29925 break;
29926 case ate_kind_label:
29927 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29928 "0x%x", entry->index);
29929 break;
29930 default:
29931 gcc_unreachable ();
29933 return 1;
29936 /* A helper function for dwarf2out_finish. Counts the number
29937 of indexed addresses. Must match the logic of the functions
29938 output_addr_table_entry above. */
29940 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29942 addr_table_entry *entry = *slot;
29944 if (entry->refcount > 0)
29945 *last_idx += 1;
29946 return 1;
29949 /* Produce the .debug_addr section. */
29951 static void
29952 output_addr_table (void)
29954 unsigned int index = 0;
29955 if (addr_index_table == NULL || addr_index_table->size () == 0)
29956 return;
29958 switch_to_section (debug_addr_section);
29959 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29960 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29961 before DWARF5, didn't have a header for .debug_addr units.
29962 DWARF5 specifies a small header when address tables are used. */
29963 if (dwarf_version >= 5)
29965 unsigned int last_idx = 0;
29966 unsigned long addrs_length;
29968 addr_index_table->traverse_noresize
29969 <unsigned int *, count_index_addrs> (&last_idx);
29970 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29972 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29973 dw2_asm_output_data (4, 0xffffffff,
29974 "Escape value for 64-bit DWARF extension");
29975 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29976 "Length of Address Unit");
29977 dw2_asm_output_data (2, 5, "DWARF addr version");
29978 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29979 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29981 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29983 addr_index_table
29984 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29987 #if ENABLE_ASSERT_CHECKING
29988 /* Verify that all marks are clear. */
29990 static void
29991 verify_marks_clear (dw_die_ref die)
29993 dw_die_ref c;
29995 gcc_assert (! die->die_mark);
29996 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29998 #endif /* ENABLE_ASSERT_CHECKING */
30000 /* Clear the marks for a die and its children.
30001 Be cool if the mark isn't set. */
30003 static void
30004 prune_unmark_dies (dw_die_ref die)
30006 dw_die_ref c;
30008 if (die->die_mark)
30009 die->die_mark = 0;
30010 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
30013 /* Given LOC that is referenced by a DIE we're marking as used, find all
30014 referenced DWARF procedures it references and mark them as used. */
30016 static void
30017 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
30019 for (; loc != NULL; loc = loc->dw_loc_next)
30020 switch (loc->dw_loc_opc)
30022 case DW_OP_implicit_pointer:
30023 case DW_OP_convert:
30024 case DW_OP_reinterpret:
30025 case DW_OP_GNU_implicit_pointer:
30026 case DW_OP_GNU_convert:
30027 case DW_OP_GNU_reinterpret:
30028 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
30029 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
30030 break;
30031 case DW_OP_GNU_variable_value:
30032 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30034 dw_die_ref ref
30035 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30036 if (ref == NULL)
30037 break;
30038 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30039 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30040 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30042 /* FALLTHRU */
30043 case DW_OP_call2:
30044 case DW_OP_call4:
30045 case DW_OP_call_ref:
30046 case DW_OP_const_type:
30047 case DW_OP_GNU_const_type:
30048 case DW_OP_GNU_parameter_ref:
30049 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
30050 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
30051 break;
30052 case DW_OP_regval_type:
30053 case DW_OP_deref_type:
30054 case DW_OP_GNU_regval_type:
30055 case DW_OP_GNU_deref_type:
30056 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
30057 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
30058 break;
30059 case DW_OP_entry_value:
30060 case DW_OP_GNU_entry_value:
30061 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
30062 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
30063 break;
30064 default:
30065 break;
30069 /* Given DIE that we're marking as used, find any other dies
30070 it references as attributes and mark them as used. */
30072 static void
30073 prune_unused_types_walk_attribs (dw_die_ref die)
30075 dw_attr_node *a;
30076 unsigned ix;
30078 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30080 switch (AT_class (a))
30082 /* Make sure DWARF procedures referenced by location descriptions will
30083 get emitted. */
30084 case dw_val_class_loc:
30085 prune_unused_types_walk_loc_descr (AT_loc (a));
30086 break;
30087 case dw_val_class_loc_list:
30088 for (dw_loc_list_ref list = AT_loc_list (a);
30089 list != NULL;
30090 list = list->dw_loc_next)
30091 prune_unused_types_walk_loc_descr (list->expr);
30092 break;
30094 case dw_val_class_view_list:
30095 /* This points to a loc_list in another attribute, so it's
30096 already covered. */
30097 break;
30099 case dw_val_class_die_ref:
30100 /* A reference to another DIE.
30101 Make sure that it will get emitted.
30102 If it was broken out into a comdat group, don't follow it. */
30103 if (! AT_ref (a)->comdat_type_p
30104 || a->dw_attr == DW_AT_specification)
30105 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
30106 break;
30108 case dw_val_class_str:
30109 /* Set the string's refcount to 0 so that prune_unused_types_mark
30110 accounts properly for it. */
30111 a->dw_attr_val.v.val_str->refcount = 0;
30112 break;
30114 default:
30115 break;
30120 /* Mark the generic parameters and arguments children DIEs of DIE. */
30122 static void
30123 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
30125 dw_die_ref c;
30127 if (die == NULL || die->die_child == NULL)
30128 return;
30129 c = die->die_child;
30132 if (is_template_parameter (c))
30133 prune_unused_types_mark (c, 1);
30134 c = c->die_sib;
30135 } while (c && c != die->die_child);
30138 /* Mark DIE as being used. If DOKIDS is true, then walk down
30139 to DIE's children. */
30141 static void
30142 prune_unused_types_mark (dw_die_ref die, int dokids)
30144 dw_die_ref c;
30146 if (die->die_mark == 0)
30148 /* We haven't done this node yet. Mark it as used. */
30149 die->die_mark = 1;
30150 /* If this is the DIE of a generic type instantiation,
30151 mark the children DIEs that describe its generic parms and
30152 args. */
30153 prune_unused_types_mark_generic_parms_dies (die);
30155 /* We also have to mark its parents as used.
30156 (But we don't want to mark our parent's kids due to this,
30157 unless it is a class.) */
30158 if (die->die_parent)
30159 prune_unused_types_mark (die->die_parent,
30160 class_scope_p (die->die_parent));
30162 /* Mark any referenced nodes. */
30163 prune_unused_types_walk_attribs (die);
30165 /* If this node is a specification,
30166 also mark the definition, if it exists. */
30167 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
30168 prune_unused_types_mark (die->die_definition, 1);
30171 if (dokids && die->die_mark != 2)
30173 /* We need to walk the children, but haven't done so yet.
30174 Remember that we've walked the kids. */
30175 die->die_mark = 2;
30177 /* If this is an array type, we need to make sure our
30178 kids get marked, even if they're types. If we're
30179 breaking out types into comdat sections, do this
30180 for all type definitions. */
30181 if (die->die_tag == DW_TAG_array_type
30182 || (use_debug_types
30183 && is_type_die (die) && ! is_declaration_die (die)))
30184 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
30185 else
30186 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30190 /* For local classes, look if any static member functions were emitted
30191 and if so, mark them. */
30193 static void
30194 prune_unused_types_walk_local_classes (dw_die_ref die)
30196 dw_die_ref c;
30198 if (die->die_mark == 2)
30199 return;
30201 switch (die->die_tag)
30203 case DW_TAG_structure_type:
30204 case DW_TAG_union_type:
30205 case DW_TAG_class_type:
30206 case DW_TAG_interface_type:
30207 break;
30209 case DW_TAG_subprogram:
30210 if (!get_AT_flag (die, DW_AT_declaration)
30211 || die->die_definition != NULL)
30212 prune_unused_types_mark (die, 1);
30213 return;
30215 default:
30216 return;
30219 /* Mark children. */
30220 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
30223 /* Walk the tree DIE and mark types that we actually use. */
30225 static void
30226 prune_unused_types_walk (dw_die_ref die)
30228 dw_die_ref c;
30230 /* Don't do anything if this node is already marked and
30231 children have been marked as well. */
30232 if (die->die_mark == 2)
30233 return;
30235 switch (die->die_tag)
30237 case DW_TAG_structure_type:
30238 case DW_TAG_union_type:
30239 case DW_TAG_class_type:
30240 case DW_TAG_interface_type:
30241 if (die->die_perennial_p)
30242 break;
30244 for (c = die->die_parent; c; c = c->die_parent)
30245 if (c->die_tag == DW_TAG_subprogram)
30246 break;
30248 /* Finding used static member functions inside of classes
30249 is needed just for local classes, because for other classes
30250 static member function DIEs with DW_AT_specification
30251 are emitted outside of the DW_TAG_*_type. If we ever change
30252 it, we'd need to call this even for non-local classes. */
30253 if (c)
30254 prune_unused_types_walk_local_classes (die);
30256 /* It's a type node --- don't mark it. */
30257 return;
30259 case DW_TAG_const_type:
30260 case DW_TAG_packed_type:
30261 case DW_TAG_pointer_type:
30262 case DW_TAG_reference_type:
30263 case DW_TAG_rvalue_reference_type:
30264 case DW_TAG_volatile_type:
30265 case DW_TAG_restrict_type:
30266 case DW_TAG_shared_type:
30267 case DW_TAG_atomic_type:
30268 case DW_TAG_immutable_type:
30269 case DW_TAG_typedef:
30270 case DW_TAG_array_type:
30271 case DW_TAG_coarray_type:
30272 case DW_TAG_friend:
30273 case DW_TAG_enumeration_type:
30274 case DW_TAG_subroutine_type:
30275 case DW_TAG_string_type:
30276 case DW_TAG_set_type:
30277 case DW_TAG_subrange_type:
30278 case DW_TAG_ptr_to_member_type:
30279 case DW_TAG_file_type:
30280 case DW_TAG_unspecified_type:
30281 case DW_TAG_dynamic_type:
30282 /* Type nodes are useful only when other DIEs reference them --- don't
30283 mark them. */
30284 /* FALLTHROUGH */
30286 case DW_TAG_dwarf_procedure:
30287 /* Likewise for DWARF procedures. */
30289 if (die->die_perennial_p)
30290 break;
30292 return;
30294 case DW_TAG_variable:
30295 if (flag_debug_only_used_symbols)
30297 if (die->die_perennial_p)
30298 break;
30300 /* For static data members, the declaration in the class is supposed
30301 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
30302 DWARF5. DW_TAG_member will be marked, so mark even such
30303 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
30304 attribute. */
30305 if (dwarf_version >= 5
30306 && class_scope_p (die->die_parent)
30307 && get_AT (die, DW_AT_const_value))
30308 break;
30310 /* premark_used_variables marks external variables --- don't mark
30311 them here. But function-local externals are always considered
30312 used. */
30313 if (get_AT (die, DW_AT_external))
30315 for (c = die->die_parent; c; c = c->die_parent)
30316 if (c->die_tag == DW_TAG_subprogram)
30317 break;
30318 if (!c)
30319 return;
30322 /* FALLTHROUGH */
30324 default:
30325 /* Mark everything else. */
30326 break;
30329 if (die->die_mark == 0)
30331 die->die_mark = 1;
30333 /* Now, mark any dies referenced from here. */
30334 prune_unused_types_walk_attribs (die);
30337 die->die_mark = 2;
30339 /* Mark children. */
30340 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30343 /* Increment the string counts on strings referred to from DIE's
30344 attributes. */
30346 static void
30347 prune_unused_types_update_strings (dw_die_ref die)
30349 dw_attr_node *a;
30350 unsigned ix;
30352 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30353 if (AT_class (a) == dw_val_class_str)
30355 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30356 s->refcount++;
30357 /* Avoid unnecessarily putting strings that are used less than
30358 twice in the hash table. */
30359 if (s->form != DW_FORM_line_strp
30360 && (s->refcount
30361 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30363 indirect_string_node **slot
30364 = debug_str_hash->find_slot_with_hash (s->str,
30365 htab_hash_string (s->str),
30366 INSERT);
30367 gcc_assert (*slot == NULL);
30368 *slot = s;
30373 /* Mark DIE and its children as removed. */
30375 static void
30376 mark_removed (dw_die_ref die)
30378 dw_die_ref c;
30379 die->removed = true;
30380 FOR_EACH_CHILD (die, c, mark_removed (c));
30383 /* Remove from the tree DIE any dies that aren't marked. */
30385 static void
30386 prune_unused_types_prune (dw_die_ref die)
30388 dw_die_ref c;
30390 gcc_assert (die->die_mark);
30391 prune_unused_types_update_strings (die);
30393 if (! die->die_child)
30394 return;
30396 c = die->die_child;
30397 do {
30398 dw_die_ref prev = c, next;
30399 for (c = c->die_sib; ! c->die_mark; c = next)
30400 if (c == die->die_child)
30402 /* No marked children between 'prev' and the end of the list. */
30403 if (prev == c)
30404 /* No marked children at all. */
30405 die->die_child = NULL;
30406 else
30408 prev->die_sib = c->die_sib;
30409 die->die_child = prev;
30411 c->die_sib = NULL;
30412 mark_removed (c);
30413 return;
30415 else
30417 next = c->die_sib;
30418 c->die_sib = NULL;
30419 mark_removed (c);
30422 if (c != prev->die_sib)
30423 prev->die_sib = c;
30424 prune_unused_types_prune (c);
30425 } while (c != die->die_child);
30428 /* Remove dies representing declarations that we never use. */
30430 static void
30431 prune_unused_types (void)
30433 unsigned int i;
30434 limbo_die_node *node;
30435 comdat_type_node *ctnode;
30436 pubname_entry *pub;
30437 dw_die_ref base_type;
30439 #if ENABLE_ASSERT_CHECKING
30440 /* All the marks should already be clear. */
30441 verify_marks_clear (comp_unit_die ());
30442 for (node = limbo_die_list; node; node = node->next)
30443 verify_marks_clear (node->die);
30444 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30445 verify_marks_clear (ctnode->root_die);
30446 #endif /* ENABLE_ASSERT_CHECKING */
30448 /* Mark types that are used in global variables. */
30449 premark_types_used_by_global_vars ();
30451 /* Mark variables used in the symtab. */
30452 if (flag_debug_only_used_symbols)
30453 premark_used_variables ();
30455 /* Set the mark on nodes that are actually used. */
30456 prune_unused_types_walk (comp_unit_die ());
30457 for (node = limbo_die_list; node; node = node->next)
30458 prune_unused_types_walk (node->die);
30459 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30461 prune_unused_types_walk (ctnode->root_die);
30462 prune_unused_types_mark (ctnode->type_die, 1);
30465 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30466 are unusual in that they are pubnames that are the children of pubtypes.
30467 They should only be marked via their parent DW_TAG_enumeration_type die,
30468 not as roots in themselves. */
30469 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30470 if (pub->die->die_tag != DW_TAG_enumerator)
30471 prune_unused_types_mark (pub->die, 1);
30472 for (i = 0; base_types.iterate (i, &base_type); i++)
30473 prune_unused_types_mark (base_type, 1);
30475 /* Also set the mark on nodes that could be referenced by
30476 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30477 by DW_TAG_inlined_subroutine origins. */
30478 cgraph_node *cnode;
30479 FOR_EACH_FUNCTION (cnode)
30480 if (cnode->referred_to_p (false))
30482 dw_die_ref die = lookup_decl_die (cnode->decl);
30483 if (die == NULL || die->die_mark)
30484 continue;
30485 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30486 if (e->caller != cnode)
30488 prune_unused_types_mark (die, 1);
30489 break;
30493 if (debug_str_hash)
30494 debug_str_hash->empty ();
30495 if (skeleton_debug_str_hash)
30496 skeleton_debug_str_hash->empty ();
30497 prune_unused_types_prune (comp_unit_die ());
30498 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30500 node = *pnode;
30501 if (!node->die->die_mark)
30502 *pnode = node->next;
30503 else
30505 prune_unused_types_prune (node->die);
30506 pnode = &node->next;
30509 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30510 prune_unused_types_prune (ctnode->root_die);
30512 /* Leave the marks clear. */
30513 prune_unmark_dies (comp_unit_die ());
30514 for (node = limbo_die_list; node; node = node->next)
30515 prune_unmark_dies (node->die);
30516 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30517 prune_unmark_dies (ctnode->root_die);
30520 /* Helpers to manipulate hash table of comdat type units. */
30522 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30524 static inline hashval_t hash (const comdat_type_node *);
30525 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30528 inline hashval_t
30529 comdat_type_hasher::hash (const comdat_type_node *type_node)
30531 hashval_t h;
30532 memcpy (&h, type_node->signature, sizeof (h));
30533 return h;
30536 inline bool
30537 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30538 const comdat_type_node *type_node_2)
30540 return (! memcmp (type_node_1->signature, type_node_2->signature,
30541 DWARF_TYPE_SIGNATURE_SIZE));
30544 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30545 to the location it would have been added, should we know its
30546 DECL_ASSEMBLER_NAME when we added other attributes. This will
30547 probably improve compactness of debug info, removing equivalent
30548 abbrevs, and hide any differences caused by deferring the
30549 computation of the assembler name, triggered by e.g. PCH. */
30551 static inline void
30552 move_linkage_attr (dw_die_ref die)
30554 unsigned ix = vec_safe_length (die->die_attr);
30555 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30557 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30558 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30560 while (--ix > 0)
30562 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30564 if (prev->dw_attr == DW_AT_decl_line
30565 || prev->dw_attr == DW_AT_decl_column
30566 || prev->dw_attr == DW_AT_name)
30567 break;
30570 if (ix != vec_safe_length (die->die_attr) - 1)
30572 die->die_attr->pop ();
30573 die->die_attr->quick_insert (ix, linkage);
30577 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30578 referenced from typed stack ops and count how often they are used. */
30580 static void
30581 mark_base_types (dw_loc_descr_ref loc)
30583 dw_die_ref base_type = NULL;
30585 for (; loc; loc = loc->dw_loc_next)
30587 switch (loc->dw_loc_opc)
30589 case DW_OP_regval_type:
30590 case DW_OP_deref_type:
30591 case DW_OP_GNU_regval_type:
30592 case DW_OP_GNU_deref_type:
30593 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30594 break;
30595 case DW_OP_convert:
30596 case DW_OP_reinterpret:
30597 case DW_OP_GNU_convert:
30598 case DW_OP_GNU_reinterpret:
30599 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30600 continue;
30601 /* FALLTHRU */
30602 case DW_OP_const_type:
30603 case DW_OP_GNU_const_type:
30604 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30605 break;
30606 case DW_OP_entry_value:
30607 case DW_OP_GNU_entry_value:
30608 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30609 continue;
30610 default:
30611 continue;
30613 gcc_assert (base_type->die_parent == comp_unit_die ());
30614 if (base_type->die_mark)
30615 base_type->die_mark++;
30616 else
30618 base_types.safe_push (base_type);
30619 base_type->die_mark = 1;
30624 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30625 referenced from typed stack ops and count how often they are used. */
30627 static void
30628 mark_base_types (dw_die_ref die)
30630 dw_die_ref c;
30631 dw_attr_node *a;
30632 dw_loc_list_ref *curr;
30633 unsigned ix;
30635 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30636 switch (AT_class (a))
30638 case dw_val_class_loc_list:
30639 curr = AT_loc_list_ptr (a);
30640 while (*curr)
30642 mark_base_types ((*curr)->expr);
30643 curr = &(*curr)->dw_loc_next;
30645 break;
30647 case dw_val_class_loc:
30648 mark_base_types (AT_loc (a));
30649 break;
30651 default:
30652 break;
30655 FOR_EACH_CHILD (die, c, mark_base_types (c));
30658 /* Comparison function for sorting marked base types. */
30660 static int
30661 base_type_cmp (const void *x, const void *y)
30663 dw_die_ref dx = *(const dw_die_ref *) x;
30664 dw_die_ref dy = *(const dw_die_ref *) y;
30665 unsigned int byte_size1, byte_size2;
30666 unsigned int encoding1, encoding2;
30667 unsigned int align1, align2;
30668 if (dx->die_mark > dy->die_mark)
30669 return -1;
30670 if (dx->die_mark < dy->die_mark)
30671 return 1;
30672 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30673 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30674 if (byte_size1 < byte_size2)
30675 return 1;
30676 if (byte_size1 > byte_size2)
30677 return -1;
30678 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30679 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30680 if (encoding1 < encoding2)
30681 return 1;
30682 if (encoding1 > encoding2)
30683 return -1;
30684 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30685 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30686 if (align1 < align2)
30687 return 1;
30688 if (align1 > align2)
30689 return -1;
30690 return 0;
30693 /* Move base types marked by mark_base_types as early as possible
30694 in the CU, sorted by decreasing usage count both to make the
30695 uleb128 references as small as possible and to make sure they
30696 will have die_offset already computed by calc_die_sizes when
30697 sizes of typed stack loc ops is computed. */
30699 static void
30700 move_marked_base_types (void)
30702 unsigned int i;
30703 dw_die_ref base_type, die, c;
30705 if (base_types.is_empty ())
30706 return;
30708 /* Sort by decreasing usage count, they will be added again in that
30709 order later on. */
30710 base_types.qsort (base_type_cmp);
30711 die = comp_unit_die ();
30712 c = die->die_child;
30715 dw_die_ref prev = c;
30716 c = c->die_sib;
30717 while (c->die_mark)
30719 remove_child_with_prev (c, prev);
30720 /* As base types got marked, there must be at least
30721 one node other than DW_TAG_base_type. */
30722 gcc_assert (die->die_child != NULL);
30723 c = prev->die_sib;
30726 while (c != die->die_child);
30727 gcc_assert (die->die_child);
30728 c = die->die_child;
30729 for (i = 0; base_types.iterate (i, &base_type); i++)
30731 base_type->die_mark = 0;
30732 base_type->die_sib = c->die_sib;
30733 c->die_sib = base_type;
30734 c = base_type;
30738 /* Helper function for resolve_addr, attempt to resolve
30739 one CONST_STRING, return true if successful. Similarly verify that
30740 SYMBOL_REFs refer to variables emitted in the current CU. */
30742 static bool
30743 resolve_one_addr (rtx *addr)
30745 rtx rtl = *addr;
30747 if (GET_CODE (rtl) == CONST_STRING)
30749 size_t len = strlen (XSTR (rtl, 0)) + 1;
30750 tree t = build_string (len, XSTR (rtl, 0));
30751 tree tlen = size_int (len - 1);
30752 TREE_TYPE (t)
30753 = build_array_type (char_type_node, build_index_type (tlen));
30754 rtl = lookup_constant_def (t);
30755 if (!rtl || !MEM_P (rtl))
30756 return false;
30757 rtl = XEXP (rtl, 0);
30758 if (GET_CODE (rtl) == SYMBOL_REF
30759 && SYMBOL_REF_DECL (rtl)
30760 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30761 return false;
30762 vec_safe_push (used_rtx_array, rtl);
30763 *addr = rtl;
30764 return true;
30767 if (GET_CODE (rtl) == SYMBOL_REF
30768 && SYMBOL_REF_DECL (rtl))
30770 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30772 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30773 return false;
30775 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30776 return false;
30779 if (GET_CODE (rtl) == CONST)
30781 subrtx_ptr_iterator::array_type array;
30782 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30783 if (!resolve_one_addr (*iter))
30784 return false;
30787 return true;
30790 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30791 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30792 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30794 static rtx
30795 string_cst_pool_decl (tree t)
30797 rtx rtl = output_constant_def (t, 1);
30798 unsigned char *array;
30799 dw_loc_descr_ref l;
30800 tree decl;
30801 size_t len;
30802 dw_die_ref ref;
30804 if (!rtl || !MEM_P (rtl))
30805 return NULL_RTX;
30806 rtl = XEXP (rtl, 0);
30807 if (GET_CODE (rtl) != SYMBOL_REF
30808 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30809 return NULL_RTX;
30811 decl = SYMBOL_REF_DECL (rtl);
30812 if (!lookup_decl_die (decl))
30814 len = TREE_STRING_LENGTH (t);
30815 vec_safe_push (used_rtx_array, rtl);
30816 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30817 array = ggc_vec_alloc<unsigned char> (len);
30818 memcpy (array, TREE_STRING_POINTER (t), len);
30819 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30820 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30821 l->dw_loc_oprnd2.v.val_vec.length = len;
30822 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30823 l->dw_loc_oprnd2.v.val_vec.array = array;
30824 add_AT_loc (ref, DW_AT_location, l);
30825 equate_decl_number_to_die (decl, ref);
30827 return rtl;
30830 /* Helper function of resolve_addr_in_expr. LOC is
30831 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30832 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30833 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30834 with DW_OP_implicit_pointer if possible
30835 and return true, if unsuccessful, return false. */
30837 static bool
30838 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30840 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30841 HOST_WIDE_INT offset = 0;
30842 dw_die_ref ref = NULL;
30843 tree decl;
30845 if (GET_CODE (rtl) == CONST
30846 && GET_CODE (XEXP (rtl, 0)) == PLUS
30847 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30849 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30850 rtl = XEXP (XEXP (rtl, 0), 0);
30852 if (GET_CODE (rtl) == CONST_STRING)
30854 size_t len = strlen (XSTR (rtl, 0)) + 1;
30855 tree t = build_string (len, XSTR (rtl, 0));
30856 tree tlen = size_int (len - 1);
30858 TREE_TYPE (t)
30859 = build_array_type (char_type_node, build_index_type (tlen));
30860 rtl = string_cst_pool_decl (t);
30861 if (!rtl)
30862 return false;
30864 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30866 decl = SYMBOL_REF_DECL (rtl);
30867 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30869 ref = lookup_decl_die (decl);
30870 if (ref && (get_AT (ref, DW_AT_location)
30871 || get_AT (ref, DW_AT_const_value)))
30873 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30874 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30875 loc->dw_loc_oprnd1.val_entry = NULL;
30876 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30877 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30878 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30879 loc->dw_loc_oprnd2.v.val_int = offset;
30880 return true;
30884 return false;
30887 /* Helper function for resolve_addr, handle one location
30888 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30889 the location list couldn't be resolved. */
30891 static bool
30892 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30894 dw_loc_descr_ref keep = NULL;
30895 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30896 switch (loc->dw_loc_opc)
30898 case DW_OP_addr:
30899 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30901 if ((prev == NULL
30902 || prev->dw_loc_opc == DW_OP_piece
30903 || prev->dw_loc_opc == DW_OP_bit_piece)
30904 && loc->dw_loc_next
30905 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30906 && (!dwarf_strict || dwarf_version >= 5)
30907 && optimize_one_addr_into_implicit_ptr (loc))
30908 break;
30909 return false;
30911 break;
30912 case DW_OP_GNU_addr_index:
30913 case DW_OP_addrx:
30914 case DW_OP_GNU_const_index:
30915 case DW_OP_constx:
30916 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30917 || loc->dw_loc_opc == DW_OP_addrx)
30918 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30919 || loc->dw_loc_opc == DW_OP_constx)
30920 && loc->dtprel))
30922 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30923 if (!resolve_one_addr (&rtl))
30924 return false;
30925 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30926 loc->dw_loc_oprnd1.val_entry
30927 = add_addr_table_entry (rtl, ate_kind_rtx);
30929 break;
30930 case DW_OP_const4u:
30931 case DW_OP_const8u:
30932 if (loc->dtprel
30933 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30934 return false;
30935 break;
30936 case DW_OP_plus_uconst:
30937 if (size_of_loc_descr (loc)
30938 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30940 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30942 dw_loc_descr_ref repl
30943 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30944 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30945 add_loc_descr (&repl, loc->dw_loc_next);
30946 *loc = *repl;
30948 break;
30949 case DW_OP_implicit_value:
30950 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30951 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30952 return false;
30953 break;
30954 case DW_OP_implicit_pointer:
30955 case DW_OP_GNU_implicit_pointer:
30956 case DW_OP_GNU_parameter_ref:
30957 case DW_OP_GNU_variable_value:
30958 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30960 dw_die_ref ref
30961 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30962 if (ref == NULL)
30963 return false;
30964 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30965 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30966 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30968 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30970 if (prev == NULL
30971 && loc->dw_loc_next == NULL
30972 && AT_class (a) == dw_val_class_loc)
30973 switch (a->dw_attr)
30975 /* Following attributes allow both exprloc and reference,
30976 so if the whole expression is DW_OP_GNU_variable_value
30977 alone we could transform it into reference. */
30978 case DW_AT_byte_size:
30979 case DW_AT_bit_size:
30980 case DW_AT_lower_bound:
30981 case DW_AT_upper_bound:
30982 case DW_AT_bit_stride:
30983 case DW_AT_count:
30984 case DW_AT_allocated:
30985 case DW_AT_associated:
30986 case DW_AT_byte_stride:
30987 a->dw_attr_val.val_class = dw_val_class_die_ref;
30988 a->dw_attr_val.val_entry = NULL;
30989 a->dw_attr_val.v.val_die_ref.die
30990 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30991 a->dw_attr_val.v.val_die_ref.external = 0;
30992 return true;
30993 default:
30994 break;
30996 if (dwarf_strict)
30997 return false;
30999 break;
31000 case DW_OP_const_type:
31001 case DW_OP_regval_type:
31002 case DW_OP_deref_type:
31003 case DW_OP_convert:
31004 case DW_OP_reinterpret:
31005 case DW_OP_GNU_const_type:
31006 case DW_OP_GNU_regval_type:
31007 case DW_OP_GNU_deref_type:
31008 case DW_OP_GNU_convert:
31009 case DW_OP_GNU_reinterpret:
31010 while (loc->dw_loc_next
31011 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
31012 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
31014 dw_die_ref base1, base2;
31015 unsigned enc1, enc2, size1, size2;
31016 if (loc->dw_loc_opc == DW_OP_regval_type
31017 || loc->dw_loc_opc == DW_OP_deref_type
31018 || loc->dw_loc_opc == DW_OP_GNU_regval_type
31019 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
31020 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
31021 else if (loc->dw_loc_oprnd1.val_class
31022 == dw_val_class_unsigned_const)
31023 break;
31024 else
31025 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
31026 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
31027 == dw_val_class_unsigned_const)
31028 break;
31029 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
31030 gcc_assert (base1->die_tag == DW_TAG_base_type
31031 && base2->die_tag == DW_TAG_base_type);
31032 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
31033 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
31034 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
31035 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
31036 if (size1 == size2
31037 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
31038 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
31039 && loc != keep)
31040 || enc1 == enc2))
31042 /* Optimize away next DW_OP_convert after
31043 adjusting LOC's base type die reference. */
31044 if (loc->dw_loc_opc == DW_OP_regval_type
31045 || loc->dw_loc_opc == DW_OP_deref_type
31046 || loc->dw_loc_opc == DW_OP_GNU_regval_type
31047 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
31048 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
31049 else
31050 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
31051 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
31052 continue;
31054 /* Don't change integer DW_OP_convert after e.g. floating
31055 point typed stack entry. */
31056 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
31057 keep = loc->dw_loc_next;
31058 break;
31060 break;
31061 default:
31062 break;
31064 return true;
31067 /* Helper function of resolve_addr. DIE had DW_AT_location of
31068 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
31069 and DW_OP_addr couldn't be resolved. resolve_addr has already
31070 removed the DW_AT_location attribute. This function attempts to
31071 add a new DW_AT_location attribute with DW_OP_implicit_pointer
31072 to it or DW_AT_const_value attribute, if possible. */
31074 static void
31075 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
31077 if (!VAR_P (decl)
31078 || lookup_decl_die (decl) != die
31079 || DECL_EXTERNAL (decl)
31080 || !TREE_STATIC (decl)
31081 || DECL_INITIAL (decl) == NULL_TREE
31082 || DECL_P (DECL_INITIAL (decl))
31083 || get_AT (die, DW_AT_const_value))
31084 return;
31086 tree init = DECL_INITIAL (decl);
31087 HOST_WIDE_INT offset = 0;
31088 /* For variables that have been optimized away and thus
31089 don't have a memory location, see if we can emit
31090 DW_AT_const_value instead. */
31091 if (tree_add_const_value_attribute (die, init))
31092 return;
31093 if (dwarf_strict && dwarf_version < 5)
31094 return;
31095 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
31096 and ADDR_EXPR refers to a decl that has DW_AT_location or
31097 DW_AT_const_value (but isn't addressable, otherwise
31098 resolving the original DW_OP_addr wouldn't fail), see if
31099 we can add DW_OP_implicit_pointer. */
31100 STRIP_NOPS (init);
31101 if (TREE_CODE (init) == POINTER_PLUS_EXPR
31102 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
31104 offset = tree_to_shwi (TREE_OPERAND (init, 1));
31105 init = TREE_OPERAND (init, 0);
31106 STRIP_NOPS (init);
31108 if (TREE_CODE (init) != ADDR_EXPR)
31109 return;
31110 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
31111 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
31112 || (VAR_P (TREE_OPERAND (init, 0))
31113 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
31114 && TREE_OPERAND (init, 0) != decl))
31116 dw_die_ref ref;
31117 dw_loc_descr_ref l;
31119 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
31121 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
31122 if (!rtl)
31123 return;
31124 decl = SYMBOL_REF_DECL (rtl);
31126 else
31127 decl = TREE_OPERAND (init, 0);
31128 ref = lookup_decl_die (decl);
31129 if (ref == NULL
31130 || (!get_AT (ref, DW_AT_location)
31131 && !get_AT (ref, DW_AT_const_value)))
31132 return;
31133 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
31134 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31135 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
31136 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31137 add_AT_loc (die, DW_AT_location, l);
31141 /* Return NULL if l is a DWARF expression, or first op that is not
31142 valid DWARF expression. */
31144 static dw_loc_descr_ref
31145 non_dwarf_expression (dw_loc_descr_ref l)
31147 while (l)
31149 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31150 return l;
31151 switch (l->dw_loc_opc)
31153 case DW_OP_regx:
31154 case DW_OP_implicit_value:
31155 case DW_OP_stack_value:
31156 case DW_OP_implicit_pointer:
31157 case DW_OP_GNU_implicit_pointer:
31158 case DW_OP_GNU_parameter_ref:
31159 case DW_OP_piece:
31160 case DW_OP_bit_piece:
31161 return l;
31162 default:
31163 break;
31165 l = l->dw_loc_next;
31167 return NULL;
31170 /* Return adjusted copy of EXPR:
31171 If it is empty DWARF expression, return it.
31172 If it is valid non-empty DWARF expression,
31173 return copy of EXPR with DW_OP_deref appended to it.
31174 If it is DWARF expression followed by DW_OP_reg{N,x}, return
31175 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
31176 If it is DWARF expression followed by DW_OP_stack_value, return
31177 copy of the DWARF expression without anything appended.
31178 Otherwise, return NULL. */
31180 static dw_loc_descr_ref
31181 copy_deref_exprloc (dw_loc_descr_ref expr)
31183 dw_loc_descr_ref tail = NULL;
31185 if (expr == NULL)
31186 return NULL;
31188 dw_loc_descr_ref l = non_dwarf_expression (expr);
31189 if (l && l->dw_loc_next)
31190 return NULL;
31192 if (l)
31194 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31195 tail = new_loc_descr ((enum dwarf_location_atom)
31196 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
31197 0, 0);
31198 else
31199 switch (l->dw_loc_opc)
31201 case DW_OP_regx:
31202 tail = new_loc_descr (DW_OP_bregx,
31203 l->dw_loc_oprnd1.v.val_unsigned, 0);
31204 break;
31205 case DW_OP_stack_value:
31206 break;
31207 default:
31208 return NULL;
31211 else
31212 tail = new_loc_descr (DW_OP_deref, 0, 0);
31214 dw_loc_descr_ref ret = NULL, *p = &ret;
31215 while (expr != l)
31217 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
31218 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
31219 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
31220 p = &(*p)->dw_loc_next;
31221 expr = expr->dw_loc_next;
31223 *p = tail;
31224 return ret;
31227 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
31228 reference to a variable or argument, adjust it if needed and return:
31229 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
31230 attribute if present should be removed
31231 0 keep the attribute perhaps with minor modifications, no need to rescan
31232 1 if the attribute has been successfully adjusted. */
31234 static int
31235 optimize_string_length (dw_attr_node *a)
31237 dw_loc_descr_ref l = AT_loc (a), lv;
31238 dw_die_ref die;
31239 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31241 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
31242 die = lookup_decl_die (decl);
31243 if (die)
31245 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31246 l->dw_loc_oprnd1.v.val_die_ref.die = die;
31247 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31249 else
31250 return -1;
31252 else
31253 die = l->dw_loc_oprnd1.v.val_die_ref.die;
31255 /* DWARF5 allows reference class, so we can then reference the DIE.
31256 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
31257 if (l->dw_loc_next != NULL && dwarf_version >= 5)
31259 a->dw_attr_val.val_class = dw_val_class_die_ref;
31260 a->dw_attr_val.val_entry = NULL;
31261 a->dw_attr_val.v.val_die_ref.die = die;
31262 a->dw_attr_val.v.val_die_ref.external = 0;
31263 return 0;
31266 dw_attr_node *av = get_AT (die, DW_AT_location);
31267 dw_loc_list_ref d;
31268 bool non_dwarf_expr = false;
31270 if (av == NULL)
31271 return dwarf_strict ? -1 : 0;
31272 switch (AT_class (av))
31274 case dw_val_class_loc_list:
31275 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31276 if (d->expr && non_dwarf_expression (d->expr))
31277 non_dwarf_expr = true;
31278 break;
31279 case dw_val_class_view_list:
31280 gcc_unreachable ();
31281 case dw_val_class_loc:
31282 lv = AT_loc (av);
31283 if (lv == NULL)
31284 return dwarf_strict ? -1 : 0;
31285 if (non_dwarf_expression (lv))
31286 non_dwarf_expr = true;
31287 break;
31288 default:
31289 return dwarf_strict ? -1 : 0;
31292 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
31293 into DW_OP_call4 or DW_OP_GNU_variable_value into
31294 DW_OP_call4 DW_OP_deref, do so. */
31295 if (!non_dwarf_expr
31296 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
31298 l->dw_loc_opc = DW_OP_call4;
31299 if (l->dw_loc_next)
31300 l->dw_loc_next = NULL;
31301 else
31302 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
31303 return 0;
31306 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
31307 copy over the DW_AT_location attribute from die to a. */
31308 if (l->dw_loc_next != NULL)
31310 a->dw_attr_val = av->dw_attr_val;
31311 return 1;
31314 dw_loc_list_ref list, *p;
31315 switch (AT_class (av))
31317 case dw_val_class_loc_list:
31318 p = &list;
31319 list = NULL;
31320 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31322 lv = copy_deref_exprloc (d->expr);
31323 if (lv)
31325 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
31326 p = &(*p)->dw_loc_next;
31328 else if (!dwarf_strict && d->expr)
31329 return 0;
31331 if (list == NULL)
31332 return dwarf_strict ? -1 : 0;
31333 a->dw_attr_val.val_class = dw_val_class_loc_list;
31334 gen_llsym (list);
31335 *AT_loc_list_ptr (a) = list;
31336 return 1;
31337 case dw_val_class_loc:
31338 lv = copy_deref_exprloc (AT_loc (av));
31339 if (lv == NULL)
31340 return dwarf_strict ? -1 : 0;
31341 a->dw_attr_val.v.val_loc = lv;
31342 return 1;
31343 default:
31344 gcc_unreachable ();
31348 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31349 an address in .rodata section if the string literal is emitted there,
31350 or remove the containing location list or replace DW_AT_const_value
31351 with DW_AT_location and empty location expression, if it isn't found
31352 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31353 to something that has been emitted in the current CU. */
31355 static void
31356 resolve_addr (dw_die_ref die)
31358 dw_die_ref c;
31359 dw_attr_node *a;
31360 dw_loc_list_ref *curr, *start, loc;
31361 unsigned ix;
31362 bool remove_AT_byte_size = false;
31364 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31365 switch (AT_class (a))
31367 case dw_val_class_loc_list:
31368 start = curr = AT_loc_list_ptr (a);
31369 loc = *curr;
31370 gcc_assert (loc);
31371 /* The same list can be referenced more than once. See if we have
31372 already recorded the result from a previous pass. */
31373 if (loc->replaced)
31374 *curr = loc->dw_loc_next;
31375 else if (!loc->resolved_addr)
31377 /* As things stand, we do not expect or allow one die to
31378 reference a suffix of another die's location list chain.
31379 References must be identical or completely separate.
31380 There is therefore no need to cache the result of this
31381 pass on any list other than the first; doing so
31382 would lead to unnecessary writes. */
31383 while (*curr)
31385 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31386 if (!resolve_addr_in_expr (a, (*curr)->expr))
31388 dw_loc_list_ref next = (*curr)->dw_loc_next;
31389 dw_loc_descr_ref l = (*curr)->expr;
31391 if (next && (*curr)->ll_symbol)
31393 gcc_assert (!next->ll_symbol);
31394 next->ll_symbol = (*curr)->ll_symbol;
31395 next->vl_symbol = (*curr)->vl_symbol;
31397 if (dwarf_split_debug_info)
31398 remove_loc_list_addr_table_entries (l);
31399 *curr = next;
31401 else
31403 mark_base_types ((*curr)->expr);
31404 curr = &(*curr)->dw_loc_next;
31407 if (loc == *start)
31408 loc->resolved_addr = 1;
31409 else
31411 loc->replaced = 1;
31412 loc->dw_loc_next = *start;
31415 if (!*start)
31417 remove_AT (die, a->dw_attr);
31418 ix--;
31420 break;
31421 case dw_val_class_view_list:
31423 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31424 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31425 dw_val_node *llnode
31426 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31427 /* If we no longer have a loclist, or it no longer needs
31428 views, drop this attribute. */
31429 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31431 remove_AT (die, a->dw_attr);
31432 ix--;
31434 break;
31436 case dw_val_class_loc:
31438 dw_loc_descr_ref l = AT_loc (a);
31439 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31440 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31441 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31442 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31443 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31444 with DW_FORM_ref referencing the same DIE as
31445 DW_OP_GNU_variable_value used to reference. */
31446 if (a->dw_attr == DW_AT_string_length
31447 && l
31448 && l->dw_loc_opc == DW_OP_GNU_variable_value
31449 && (l->dw_loc_next == NULL
31450 || (l->dw_loc_next->dw_loc_next == NULL
31451 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31453 switch (optimize_string_length (a))
31455 case -1:
31456 remove_AT (die, a->dw_attr);
31457 ix--;
31458 /* If we drop DW_AT_string_length, we need to drop also
31459 DW_AT_{string_length_,}byte_size. */
31460 remove_AT_byte_size = true;
31461 continue;
31462 default:
31463 break;
31464 case 1:
31465 /* Even if we keep the optimized DW_AT_string_length,
31466 it might have changed AT_class, so process it again. */
31467 ix--;
31468 continue;
31471 /* For -gdwarf-2 don't attempt to optimize
31472 DW_AT_data_member_location containing
31473 DW_OP_plus_uconst - older consumers might
31474 rely on it being that op instead of a more complex,
31475 but shorter, location description. */
31476 if ((dwarf_version > 2
31477 || a->dw_attr != DW_AT_data_member_location
31478 || l == NULL
31479 || l->dw_loc_opc != DW_OP_plus_uconst
31480 || l->dw_loc_next != NULL)
31481 && !resolve_addr_in_expr (a, l))
31483 if (dwarf_split_debug_info)
31484 remove_loc_list_addr_table_entries (l);
31485 if (l != NULL
31486 && l->dw_loc_next == NULL
31487 && l->dw_loc_opc == DW_OP_addr
31488 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31489 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31490 && a->dw_attr == DW_AT_location)
31492 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31493 remove_AT (die, a->dw_attr);
31494 ix--;
31495 optimize_location_into_implicit_ptr (die, decl);
31496 break;
31498 if (a->dw_attr == DW_AT_string_length)
31499 /* If we drop DW_AT_string_length, we need to drop also
31500 DW_AT_{string_length_,}byte_size. */
31501 remove_AT_byte_size = true;
31502 remove_AT (die, a->dw_attr);
31503 ix--;
31505 else
31506 mark_base_types (l);
31508 break;
31509 case dw_val_class_addr:
31510 if (a->dw_attr == DW_AT_const_value
31511 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31513 if (AT_index (a) != NOT_INDEXED)
31514 remove_addr_table_entry (a->dw_attr_val.val_entry);
31515 remove_AT (die, a->dw_attr);
31516 ix--;
31518 if ((die->die_tag == DW_TAG_call_site
31519 && a->dw_attr == DW_AT_call_origin)
31520 || (die->die_tag == DW_TAG_GNU_call_site
31521 && a->dw_attr == DW_AT_abstract_origin))
31523 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31524 dw_die_ref tdie = lookup_decl_die (tdecl);
31525 dw_die_ref cdie;
31526 if (tdie == NULL
31527 && DECL_EXTERNAL (tdecl)
31528 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31529 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31531 dw_die_ref pdie = cdie;
31532 /* Make sure we don't add these DIEs into type units.
31533 We could emit skeleton DIEs for context (namespaces,
31534 outer structs/classes) and a skeleton DIE for the
31535 innermost context with DW_AT_signature pointing to the
31536 type unit. See PR78835. */
31537 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31538 pdie = pdie->die_parent;
31539 if (pdie == NULL)
31541 /* Creating a full DIE for tdecl is overly expensive and
31542 at this point even wrong when in the LTO phase
31543 as it can end up generating new type DIEs we didn't
31544 output and thus optimize_external_refs will crash. */
31545 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31546 add_AT_flag (tdie, DW_AT_external, 1);
31547 add_AT_flag (tdie, DW_AT_declaration, 1);
31548 add_linkage_attr (tdie, tdecl);
31549 add_name_and_src_coords_attributes (tdie, tdecl, true);
31550 equate_decl_number_to_die (tdecl, tdie);
31553 if (tdie)
31555 a->dw_attr_val.val_class = dw_val_class_die_ref;
31556 a->dw_attr_val.v.val_die_ref.die = tdie;
31557 a->dw_attr_val.v.val_die_ref.external = 0;
31559 else
31561 if (AT_index (a) != NOT_INDEXED)
31562 remove_addr_table_entry (a->dw_attr_val.val_entry);
31563 remove_AT (die, a->dw_attr);
31564 ix--;
31567 break;
31568 default:
31569 break;
31572 if (remove_AT_byte_size)
31573 remove_AT (die, dwarf_version >= 5
31574 ? DW_AT_string_length_byte_size
31575 : DW_AT_byte_size);
31577 FOR_EACH_CHILD (die, c, resolve_addr (c));
31580 /* Helper routines for optimize_location_lists.
31581 This pass tries to share identical local lists in .debug_loc
31582 section. */
31584 /* Iteratively hash operands of LOC opcode into HSTATE. */
31586 static void
31587 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31589 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31590 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31592 switch (loc->dw_loc_opc)
31594 case DW_OP_const4u:
31595 case DW_OP_const8u:
31596 if (loc->dtprel)
31597 goto hash_addr;
31598 /* FALLTHRU */
31599 case DW_OP_const1u:
31600 case DW_OP_const1s:
31601 case DW_OP_const2u:
31602 case DW_OP_const2s:
31603 case DW_OP_const4s:
31604 case DW_OP_const8s:
31605 case DW_OP_constu:
31606 case DW_OP_consts:
31607 case DW_OP_pick:
31608 case DW_OP_plus_uconst:
31609 case DW_OP_breg0:
31610 case DW_OP_breg1:
31611 case DW_OP_breg2:
31612 case DW_OP_breg3:
31613 case DW_OP_breg4:
31614 case DW_OP_breg5:
31615 case DW_OP_breg6:
31616 case DW_OP_breg7:
31617 case DW_OP_breg8:
31618 case DW_OP_breg9:
31619 case DW_OP_breg10:
31620 case DW_OP_breg11:
31621 case DW_OP_breg12:
31622 case DW_OP_breg13:
31623 case DW_OP_breg14:
31624 case DW_OP_breg15:
31625 case DW_OP_breg16:
31626 case DW_OP_breg17:
31627 case DW_OP_breg18:
31628 case DW_OP_breg19:
31629 case DW_OP_breg20:
31630 case DW_OP_breg21:
31631 case DW_OP_breg22:
31632 case DW_OP_breg23:
31633 case DW_OP_breg24:
31634 case DW_OP_breg25:
31635 case DW_OP_breg26:
31636 case DW_OP_breg27:
31637 case DW_OP_breg28:
31638 case DW_OP_breg29:
31639 case DW_OP_breg30:
31640 case DW_OP_breg31:
31641 case DW_OP_regx:
31642 case DW_OP_fbreg:
31643 case DW_OP_piece:
31644 case DW_OP_deref_size:
31645 case DW_OP_xderef_size:
31646 hstate.add_object (val1->v.val_int);
31647 break;
31648 case DW_OP_skip:
31649 case DW_OP_bra:
31651 int offset;
31653 gcc_assert (val1->val_class == dw_val_class_loc);
31654 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31655 hstate.add_object (offset);
31657 break;
31658 case DW_OP_implicit_value:
31659 hstate.add_object (val1->v.val_unsigned);
31660 switch (val2->val_class)
31662 case dw_val_class_const:
31663 hstate.add_object (val2->v.val_int);
31664 break;
31665 case dw_val_class_vec:
31667 unsigned int elt_size = val2->v.val_vec.elt_size;
31668 unsigned int len = val2->v.val_vec.length;
31670 hstate.add_int (elt_size);
31671 hstate.add_int (len);
31672 hstate.add (val2->v.val_vec.array, len * elt_size);
31674 break;
31675 case dw_val_class_const_double:
31676 hstate.add_object (val2->v.val_double.low);
31677 hstate.add_object (val2->v.val_double.high);
31678 break;
31679 case dw_val_class_wide_int:
31680 hstate.add (val2->v.val_wide->get_val (),
31681 get_full_len (*val2->v.val_wide)
31682 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31683 break;
31684 case dw_val_class_addr:
31685 inchash::add_rtx (val2->v.val_addr, hstate);
31686 break;
31687 default:
31688 gcc_unreachable ();
31690 break;
31691 case DW_OP_bregx:
31692 case DW_OP_bit_piece:
31693 hstate.add_object (val1->v.val_int);
31694 hstate.add_object (val2->v.val_int);
31695 break;
31696 case DW_OP_addr:
31697 hash_addr:
31698 if (loc->dtprel)
31700 unsigned char dtprel = 0xd1;
31701 hstate.add_object (dtprel);
31703 inchash::add_rtx (val1->v.val_addr, hstate);
31704 break;
31705 case DW_OP_GNU_addr_index:
31706 case DW_OP_addrx:
31707 case DW_OP_GNU_const_index:
31708 case DW_OP_constx:
31710 if (loc->dtprel)
31712 unsigned char dtprel = 0xd1;
31713 hstate.add_object (dtprel);
31715 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31717 break;
31718 case DW_OP_implicit_pointer:
31719 case DW_OP_GNU_implicit_pointer:
31720 hstate.add_int (val2->v.val_int);
31721 break;
31722 case DW_OP_entry_value:
31723 case DW_OP_GNU_entry_value:
31724 hstate.add_object (val1->v.val_loc);
31725 break;
31726 case DW_OP_regval_type:
31727 case DW_OP_deref_type:
31728 case DW_OP_GNU_regval_type:
31729 case DW_OP_GNU_deref_type:
31731 unsigned int byte_size
31732 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31733 unsigned int encoding
31734 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31735 hstate.add_object (val1->v.val_int);
31736 hstate.add_object (byte_size);
31737 hstate.add_object (encoding);
31739 break;
31740 case DW_OP_convert:
31741 case DW_OP_reinterpret:
31742 case DW_OP_GNU_convert:
31743 case DW_OP_GNU_reinterpret:
31744 if (val1->val_class == dw_val_class_unsigned_const)
31746 hstate.add_object (val1->v.val_unsigned);
31747 break;
31749 /* FALLTHRU */
31750 case DW_OP_const_type:
31751 case DW_OP_GNU_const_type:
31753 unsigned int byte_size
31754 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31755 unsigned int encoding
31756 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31757 hstate.add_object (byte_size);
31758 hstate.add_object (encoding);
31759 if (loc->dw_loc_opc != DW_OP_const_type
31760 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31761 break;
31762 hstate.add_object (val2->val_class);
31763 switch (val2->val_class)
31765 case dw_val_class_const:
31766 hstate.add_object (val2->v.val_int);
31767 break;
31768 case dw_val_class_vec:
31770 unsigned int elt_size = val2->v.val_vec.elt_size;
31771 unsigned int len = val2->v.val_vec.length;
31773 hstate.add_object (elt_size);
31774 hstate.add_object (len);
31775 hstate.add (val2->v.val_vec.array, len * elt_size);
31777 break;
31778 case dw_val_class_const_double:
31779 hstate.add_object (val2->v.val_double.low);
31780 hstate.add_object (val2->v.val_double.high);
31781 break;
31782 case dw_val_class_wide_int:
31783 hstate.add (val2->v.val_wide->get_val (),
31784 get_full_len (*val2->v.val_wide)
31785 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31786 break;
31787 default:
31788 gcc_unreachable ();
31791 break;
31793 default:
31794 /* Other codes have no operands. */
31795 break;
31799 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31801 static inline void
31802 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31804 dw_loc_descr_ref l;
31805 bool sizes_computed = false;
31806 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31807 size_of_locs (loc);
31809 for (l = loc; l != NULL; l = l->dw_loc_next)
31811 enum dwarf_location_atom opc = l->dw_loc_opc;
31812 hstate.add_object (opc);
31813 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31815 size_of_locs (loc);
31816 sizes_computed = true;
31818 hash_loc_operands (l, hstate);
31822 /* Compute hash of the whole location list LIST_HEAD. */
31824 static inline void
31825 hash_loc_list (dw_loc_list_ref list_head)
31827 dw_loc_list_ref curr = list_head;
31828 inchash::hash hstate;
31830 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31832 hstate.add (curr->begin, strlen (curr->begin) + 1);
31833 hstate.add (curr->end, strlen (curr->end) + 1);
31834 hstate.add_object (curr->vbegin);
31835 hstate.add_object (curr->vend);
31836 if (curr->section)
31837 hstate.add (curr->section, strlen (curr->section) + 1);
31838 hash_locs (curr->expr, hstate);
31840 list_head->hash = hstate.end ();
31843 /* Return true if X and Y opcodes have the same operands. */
31845 static inline bool
31846 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31848 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31849 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31850 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31851 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31853 switch (x->dw_loc_opc)
31855 case DW_OP_const4u:
31856 case DW_OP_const8u:
31857 if (x->dtprel)
31858 goto hash_addr;
31859 /* FALLTHRU */
31860 case DW_OP_const1u:
31861 case DW_OP_const1s:
31862 case DW_OP_const2u:
31863 case DW_OP_const2s:
31864 case DW_OP_const4s:
31865 case DW_OP_const8s:
31866 case DW_OP_constu:
31867 case DW_OP_consts:
31868 case DW_OP_pick:
31869 case DW_OP_plus_uconst:
31870 case DW_OP_breg0:
31871 case DW_OP_breg1:
31872 case DW_OP_breg2:
31873 case DW_OP_breg3:
31874 case DW_OP_breg4:
31875 case DW_OP_breg5:
31876 case DW_OP_breg6:
31877 case DW_OP_breg7:
31878 case DW_OP_breg8:
31879 case DW_OP_breg9:
31880 case DW_OP_breg10:
31881 case DW_OP_breg11:
31882 case DW_OP_breg12:
31883 case DW_OP_breg13:
31884 case DW_OP_breg14:
31885 case DW_OP_breg15:
31886 case DW_OP_breg16:
31887 case DW_OP_breg17:
31888 case DW_OP_breg18:
31889 case DW_OP_breg19:
31890 case DW_OP_breg20:
31891 case DW_OP_breg21:
31892 case DW_OP_breg22:
31893 case DW_OP_breg23:
31894 case DW_OP_breg24:
31895 case DW_OP_breg25:
31896 case DW_OP_breg26:
31897 case DW_OP_breg27:
31898 case DW_OP_breg28:
31899 case DW_OP_breg29:
31900 case DW_OP_breg30:
31901 case DW_OP_breg31:
31902 case DW_OP_regx:
31903 case DW_OP_fbreg:
31904 case DW_OP_piece:
31905 case DW_OP_deref_size:
31906 case DW_OP_xderef_size:
31907 return valx1->v.val_int == valy1->v.val_int;
31908 case DW_OP_skip:
31909 case DW_OP_bra:
31910 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31911 can cause irrelevant differences in dw_loc_addr. */
31912 gcc_assert (valx1->val_class == dw_val_class_loc
31913 && valy1->val_class == dw_val_class_loc
31914 && (dwarf_split_debug_info
31915 || x->dw_loc_addr == y->dw_loc_addr));
31916 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31917 case DW_OP_implicit_value:
31918 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31919 || valx2->val_class != valy2->val_class)
31920 return false;
31921 switch (valx2->val_class)
31923 case dw_val_class_const:
31924 return valx2->v.val_int == valy2->v.val_int;
31925 case dw_val_class_vec:
31926 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31927 && valx2->v.val_vec.length == valy2->v.val_vec.length
31928 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31929 valx2->v.val_vec.elt_size
31930 * valx2->v.val_vec.length) == 0;
31931 case dw_val_class_const_double:
31932 return valx2->v.val_double.low == valy2->v.val_double.low
31933 && valx2->v.val_double.high == valy2->v.val_double.high;
31934 case dw_val_class_wide_int:
31935 return *valx2->v.val_wide == *valy2->v.val_wide;
31936 case dw_val_class_addr:
31937 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31938 default:
31939 gcc_unreachable ();
31941 case DW_OP_bregx:
31942 case DW_OP_bit_piece:
31943 return valx1->v.val_int == valy1->v.val_int
31944 && valx2->v.val_int == valy2->v.val_int;
31945 case DW_OP_addr:
31946 hash_addr:
31947 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31948 case DW_OP_GNU_addr_index:
31949 case DW_OP_addrx:
31950 case DW_OP_GNU_const_index:
31951 case DW_OP_constx:
31953 rtx ax1 = valx1->val_entry->addr.rtl;
31954 rtx ay1 = valy1->val_entry->addr.rtl;
31955 return rtx_equal_p (ax1, ay1);
31957 case DW_OP_implicit_pointer:
31958 case DW_OP_GNU_implicit_pointer:
31959 return valx1->val_class == dw_val_class_die_ref
31960 && valx1->val_class == valy1->val_class
31961 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31962 && valx2->v.val_int == valy2->v.val_int;
31963 case DW_OP_entry_value:
31964 case DW_OP_GNU_entry_value:
31965 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31966 case DW_OP_const_type:
31967 case DW_OP_GNU_const_type:
31968 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31969 || valx2->val_class != valy2->val_class)
31970 return false;
31971 switch (valx2->val_class)
31973 case dw_val_class_const:
31974 return valx2->v.val_int == valy2->v.val_int;
31975 case dw_val_class_vec:
31976 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31977 && valx2->v.val_vec.length == valy2->v.val_vec.length
31978 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31979 valx2->v.val_vec.elt_size
31980 * valx2->v.val_vec.length) == 0;
31981 case dw_val_class_const_double:
31982 return valx2->v.val_double.low == valy2->v.val_double.low
31983 && valx2->v.val_double.high == valy2->v.val_double.high;
31984 case dw_val_class_wide_int:
31985 return *valx2->v.val_wide == *valy2->v.val_wide;
31986 default:
31987 gcc_unreachable ();
31989 case DW_OP_regval_type:
31990 case DW_OP_deref_type:
31991 case DW_OP_GNU_regval_type:
31992 case DW_OP_GNU_deref_type:
31993 return valx1->v.val_int == valy1->v.val_int
31994 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31995 case DW_OP_convert:
31996 case DW_OP_reinterpret:
31997 case DW_OP_GNU_convert:
31998 case DW_OP_GNU_reinterpret:
31999 if (valx1->val_class != valy1->val_class)
32000 return false;
32001 if (valx1->val_class == dw_val_class_unsigned_const)
32002 return valx1->v.val_unsigned == valy1->v.val_unsigned;
32003 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
32004 case DW_OP_GNU_parameter_ref:
32005 return valx1->val_class == dw_val_class_die_ref
32006 && valx1->val_class == valy1->val_class
32007 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
32008 default:
32009 /* Other codes have no operands. */
32010 return true;
32014 /* Return true if DWARF location expressions X and Y are the same. */
32016 static inline bool
32017 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
32019 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
32020 if (x->dw_loc_opc != y->dw_loc_opc
32021 || x->dtprel != y->dtprel
32022 || !compare_loc_operands (x, y))
32023 break;
32024 return x == NULL && y == NULL;
32027 /* Hashtable helpers. */
32029 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
32031 static inline hashval_t hash (const dw_loc_list_struct *);
32032 static inline bool equal (const dw_loc_list_struct *,
32033 const dw_loc_list_struct *);
32036 /* Return precomputed hash of location list X. */
32038 inline hashval_t
32039 loc_list_hasher::hash (const dw_loc_list_struct *x)
32041 return x->hash;
32044 /* Return true if location lists A and B are the same. */
32046 inline bool
32047 loc_list_hasher::equal (const dw_loc_list_struct *a,
32048 const dw_loc_list_struct *b)
32050 if (a == b)
32051 return true;
32052 if (a->hash != b->hash)
32053 return false;
32054 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
32055 if (strcmp (a->begin, b->begin) != 0
32056 || strcmp (a->end, b->end) != 0
32057 || (a->section == NULL) != (b->section == NULL)
32058 || (a->section && strcmp (a->section, b->section) != 0)
32059 || a->vbegin != b->vbegin || a->vend != b->vend
32060 || !compare_locs (a->expr, b->expr))
32061 break;
32062 return a == NULL && b == NULL;
32065 typedef hash_table<loc_list_hasher> loc_list_hash_type;
32068 /* Recursively optimize location lists referenced from DIE
32069 children and share them whenever possible. */
32071 static void
32072 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
32074 dw_die_ref c;
32075 dw_attr_node *a;
32076 unsigned ix;
32077 dw_loc_list_struct **slot;
32078 bool drop_locviews = false;
32079 bool has_locviews = false;
32081 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32082 if (AT_class (a) == dw_val_class_loc_list)
32084 dw_loc_list_ref list = AT_loc_list (a);
32085 /* TODO: perform some optimizations here, before hashing
32086 it and storing into the hash table. */
32087 hash_loc_list (list);
32088 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
32089 if (*slot == NULL)
32091 *slot = list;
32092 if (loc_list_has_views (list))
32093 gcc_assert (list->vl_symbol);
32094 else if (list->vl_symbol)
32096 drop_locviews = true;
32097 list->vl_symbol = NULL;
32100 else
32102 if (list->vl_symbol && !(*slot)->vl_symbol)
32103 drop_locviews = true;
32104 a->dw_attr_val.v.val_loc_list = *slot;
32107 else if (AT_class (a) == dw_val_class_view_list)
32109 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
32110 has_locviews = true;
32114 if (drop_locviews && has_locviews)
32115 remove_AT (die, DW_AT_GNU_locviews);
32117 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
32121 /* Recursively assign each location list a unique index into the debug_addr
32122 section. */
32124 static void
32125 index_location_lists (dw_die_ref die)
32127 dw_die_ref c;
32128 dw_attr_node *a;
32129 unsigned ix;
32131 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32132 if (AT_class (a) == dw_val_class_loc_list)
32134 dw_loc_list_ref list = AT_loc_list (a);
32135 dw_loc_list_ref curr;
32136 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
32138 /* Don't index an entry that has already been indexed
32139 or won't be output. Make sure skip_loc_list_entry doesn't
32140 call size_of_locs, because that might cause circular dependency,
32141 index_location_lists requiring address table indexes to be
32142 computed, but adding new indexes through add_addr_table_entry
32143 and address table index computation requiring no new additions
32144 to the hash table. In the rare case of DWARF[234] >= 64KB
32145 location expression, we'll just waste unused address table entry
32146 for it. */
32147 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
32148 continue;
32150 curr->begin_entry
32151 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
32152 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
32153 curr->end_entry
32154 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
32158 FOR_EACH_CHILD (die, c, index_location_lists (c));
32161 /* Optimize location lists referenced from DIE
32162 children and share them whenever possible. */
32164 static void
32165 optimize_location_lists (dw_die_ref die)
32167 loc_list_hash_type htab (500);
32168 optimize_location_lists_1 (die, &htab);
32171 /* Traverse the limbo die list, and add parent/child links. The only
32172 dies without parents that should be here are concrete instances of
32173 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
32174 For concrete instances, we can get the parent die from the abstract
32175 instance. */
32177 static void
32178 flush_limbo_die_list (void)
32180 limbo_die_node *node;
32182 /* get_context_die calls force_decl_die, which can put new DIEs on the
32183 limbo list in LTO mode when nested functions are put in a different
32184 partition than that of their parent function. */
32185 while ((node = limbo_die_list))
32187 dw_die_ref die = node->die;
32188 limbo_die_list = node->next;
32190 if (die->die_parent == NULL)
32192 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
32194 if (origin && origin->die_parent)
32195 add_child_die (origin->die_parent, die);
32196 else if (is_cu_die (die))
32198 else if (seen_error ())
32199 /* It's OK to be confused by errors in the input. */
32200 add_child_die (comp_unit_die (), die);
32201 else
32203 /* In certain situations, the lexical block containing a
32204 nested function can be optimized away, which results
32205 in the nested function die being orphaned. Likewise
32206 with the return type of that nested function. Force
32207 this to be a child of the containing function.
32209 It may happen that even the containing function got fully
32210 inlined and optimized out. In that case we are lost and
32211 assign the empty child. This should not be big issue as
32212 the function is likely unreachable too. */
32213 gcc_assert (node->created_for);
32215 if (DECL_P (node->created_for))
32216 origin = get_context_die (DECL_CONTEXT (node->created_for));
32217 else if (TYPE_P (node->created_for))
32218 origin = scope_die_for (node->created_for, comp_unit_die ());
32219 else
32220 origin = comp_unit_die ();
32222 add_child_die (origin, die);
32228 /* Reset DIEs so we can output them again. */
32230 static void
32231 reset_dies (dw_die_ref die)
32233 dw_die_ref c;
32235 /* Remove stuff we re-generate. */
32236 die->die_mark = 0;
32237 die->die_offset = 0;
32238 die->die_abbrev = 0;
32239 remove_AT (die, DW_AT_sibling);
32241 FOR_EACH_CHILD (die, c, reset_dies (c));
32244 /* reset_indirect_string removed the references coming from DW_AT_name
32245 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
32246 .debug_line_str strings again. */
32248 static void
32249 adjust_name_comp_dir (dw_die_ref die)
32251 for (int i = 0; i < 2; i++)
32253 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
32254 dw_attr_node *a = get_AT (die, attr_kind);
32255 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
32256 continue;
32258 if (!debug_line_str_hash)
32259 debug_line_str_hash
32260 = hash_table<indirect_string_hasher>::create_ggc (10);
32262 struct indirect_string_node *node
32263 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
32264 debug_line_str_hash);
32265 set_indirect_string (node);
32266 node->form = DW_FORM_line_strp;
32267 a->dw_attr_val.v.val_str = node;
32271 /* Output stuff that dwarf requires at the end of every file,
32272 and generate the DWARF-2 debugging info. */
32274 static void
32275 dwarf2out_finish (const char *filename)
32277 comdat_type_node *ctnode;
32278 dw_die_ref main_comp_unit_die;
32279 unsigned char checksum[16];
32280 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32282 /* Generate CTF/BTF debug info. */
32283 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32284 || btf_debuginfo_p ()) && lang_GNU_C ())
32285 ctf_debug_finish (filename);
32287 #ifdef CODEVIEW_DEBUGGING_INFO
32288 if (codeview_debuginfo_p ())
32289 codeview_debug_finish ();
32290 #endif
32292 /* Skip emitting DWARF if not required. */
32293 if (!dwarf_debuginfo_p ())
32294 return;
32296 /* Flush out any latecomers to the limbo party. */
32297 flush_limbo_die_list ();
32299 if (inline_entry_data_table)
32300 gcc_assert (inline_entry_data_table->is_empty ());
32302 if (flag_checking)
32304 verify_die (comp_unit_die ());
32305 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32306 verify_die (node->die);
32309 /* We shouldn't have any symbols with delayed asm names for
32310 DIEs generated after early finish. */
32311 gcc_assert (deferred_asm_name == NULL);
32313 gen_remaining_tmpl_value_param_die_attribute ();
32315 if (flag_generate_lto || flag_generate_offload)
32317 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
32319 /* Prune stuff so that dwarf2out_finish runs successfully
32320 for the fat part of the object. */
32321 reset_dies (comp_unit_die ());
32322 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32323 reset_dies (node->die);
32324 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32326 /* Remove the pointer to the line table. */
32327 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32328 if (debug_info_level >= DINFO_LEVEL_TERSE)
32329 reset_dies (ctnode->root_die);
32332 /* Reset die CU symbol so we don't output it twice. */
32333 comp_unit_die ()->die_id.die_symbol = NULL;
32335 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32336 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32337 if (have_macinfo)
32338 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32340 /* Remove indirect string decisions. */
32341 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32342 if (debug_line_str_hash)
32344 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32345 debug_line_str_hash = NULL;
32346 if (asm_outputs_debug_line_str ())
32348 adjust_name_comp_dir (comp_unit_die ());
32349 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32350 adjust_name_comp_dir (node->die);
32355 #if ENABLE_ASSERT_CHECKING
32357 dw_die_ref die = comp_unit_die (), c;
32358 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32360 #endif
32361 base_types.truncate (0);
32362 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32363 resolve_addr (ctnode->root_die);
32364 resolve_addr (comp_unit_die ());
32365 move_marked_base_types ();
32367 if (dump_file)
32369 fprintf (dump_file, "DWARF for %s\n", filename);
32370 print_die (comp_unit_die (), dump_file);
32373 /* Initialize sections and labels used for actual assembler output. */
32374 unsigned generation = init_sections_and_labels (false);
32376 /* Traverse the DIE's and add sibling attributes to those DIE's that
32377 have children. */
32378 add_sibling_attributes (comp_unit_die ());
32379 limbo_die_node *node;
32380 for (node = cu_die_list; node; node = node->next)
32381 add_sibling_attributes (node->die);
32382 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32383 add_sibling_attributes (ctnode->root_die);
32385 /* When splitting DWARF info, we put some attributes in the
32386 skeleton compile_unit DIE that remains in the .o, while
32387 most attributes go in the DWO compile_unit_die. */
32388 if (dwarf_split_debug_info)
32390 limbo_die_node *cu;
32391 main_comp_unit_die = gen_compile_unit_die (NULL);
32392 if (dwarf_version >= 5)
32393 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32394 cu = limbo_die_list;
32395 gcc_assert (cu->die == main_comp_unit_die);
32396 limbo_die_list = limbo_die_list->next;
32397 cu->next = cu_die_list;
32398 cu_die_list = cu;
32400 else
32401 main_comp_unit_die = comp_unit_die ();
32403 /* Output a terminator label for the .text section. */
32404 switch_to_section (text_section);
32405 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32406 if (cold_text_section)
32408 switch_to_section (cold_text_section);
32409 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32412 /* We can only use the low/high_pc attributes if all of the code was
32413 in .text. */
32414 if ((!have_multiple_function_sections
32415 && vec_safe_length (switch_text_ranges) < 2)
32416 || (dwarf_version < 3 && dwarf_strict))
32418 const char *end_label = text_end_label;
32419 if (vec_safe_length (switch_text_ranges) == 1)
32420 end_label = (*switch_text_ranges)[0];
32421 /* Don't add if the CU has no associated code. */
32422 if (switch_text_ranges)
32423 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32424 end_label, true);
32426 else
32428 unsigned fde_idx;
32429 dw_fde_ref fde;
32430 bool range_list_added = false;
32431 if (switch_text_ranges)
32433 const char *prev_loc = text_section_label;
32434 const char *loc;
32435 unsigned idx;
32437 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32438 if (prev_loc)
32440 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32441 loc, &range_list_added, true);
32442 prev_loc = NULL;
32444 else
32445 prev_loc = loc;
32447 if (prev_loc)
32448 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32449 text_end_label, &range_list_added, true);
32452 if (switch_cold_ranges)
32454 const char *prev_loc = cold_text_section_label;
32455 const char *loc;
32456 unsigned idx;
32458 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32459 if (prev_loc)
32461 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32462 loc, &range_list_added, true);
32463 prev_loc = NULL;
32465 else
32466 prev_loc = loc;
32468 if (prev_loc)
32469 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32470 cold_end_label, &range_list_added, true);
32473 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32475 if (fde->ignored_debug)
32476 continue;
32477 if (!fde->in_std_section)
32478 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32479 fde->dw_fde_end, &range_list_added,
32480 true);
32481 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32482 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32483 fde->dw_fde_second_end, &range_list_added,
32484 true);
32487 if (range_list_added)
32489 /* We need to give .debug_loc and .debug_ranges an appropriate
32490 "base address". Use zero so that these addresses become
32491 absolute. Historically, we've emitted the unexpected
32492 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32493 Emit both to give time for other tools to adapt. */
32494 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32495 if (! dwarf_strict && dwarf_version < 4)
32496 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32498 add_ranges (NULL);
32499 have_multiple_function_sections = true;
32503 /* AIX Assembler inserts the length, so adjust the reference to match the
32504 offset expected by debuggers. */
32505 strcpy (dl_section_ref, debug_line_section_label);
32506 if (XCOFF_DEBUGGING_INFO)
32507 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32509 if (debug_info_level >= DINFO_LEVEL_TERSE)
32510 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32511 dl_section_ref);
32513 if (have_macinfo)
32514 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32515 macinfo_section_label);
32517 if (dwarf_split_debug_info)
32519 if (have_location_lists)
32521 /* Since we generate the loclists in the split DWARF .dwo
32522 file itself, we don't need to generate a loclists_base
32523 attribute for the split compile unit DIE. That attribute
32524 (and using relocatable sec_offset FORMs) isn't allowed
32525 for a split compile unit. Only if the .debug_loclists
32526 section was in the main file, would we need to generate a
32527 loclists_base attribute here (for the full or skeleton
32528 unit DIE). */
32530 /* optimize_location_lists calculates the size of the lists,
32531 so index them first, and assign indices to the entries.
32532 Although optimize_location_lists will remove entries from
32533 the table, it only does so for duplicates, and therefore
32534 only reduces ref_counts to 1. */
32535 index_location_lists (comp_unit_die ());
32538 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32539 index_rnglists ();
32541 if (addr_index_table != NULL)
32543 unsigned int index = 0;
32544 addr_index_table
32545 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32546 (&index);
32550 loc_list_idx = 0;
32551 if (have_location_lists)
32553 optimize_location_lists (comp_unit_die ());
32554 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32555 if (dwarf_version >= 5 && dwarf_split_debug_info)
32556 assign_location_list_indexes (comp_unit_die ());
32559 save_macinfo_strings ();
32561 if (dwarf_split_debug_info)
32563 unsigned int index = 0;
32565 /* Add attributes common to skeleton compile_units and
32566 type_units. Because these attributes include strings, it
32567 must be done before freezing the string table. Top-level
32568 skeleton die attrs are added when the skeleton type unit is
32569 created, so ensure it is created by this point. */
32570 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32571 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32574 /* Output all of the compilation units. We put the main one last so that
32575 the offsets are available to output_pubnames. */
32576 for (node = cu_die_list; node; node = node->next)
32577 output_comp_unit (node->die, 0, NULL);
32579 hash_table<comdat_type_hasher> comdat_type_table (100);
32580 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32582 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32584 /* Don't output duplicate types. */
32585 if (*slot != HTAB_EMPTY_ENTRY)
32586 continue;
32588 /* Add a pointer to the line table for the main compilation unit
32589 so that the debugger can make sense of DW_AT_decl_file
32590 attributes. */
32591 if (debug_info_level >= DINFO_LEVEL_TERSE)
32592 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32593 (!dwarf_split_debug_info
32594 ? dl_section_ref
32595 : debug_skeleton_line_section_label));
32597 output_comdat_type_unit (ctnode, false);
32598 *slot = ctnode;
32601 if (dwarf_split_debug_info)
32603 int mark;
32604 struct md5_ctx ctx;
32606 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32607 md5_init_ctx (&ctx);
32608 mark = 0;
32609 die_checksum (comp_unit_die (), &ctx, &mark);
32610 unmark_all_dies (comp_unit_die ());
32611 md5_finish_ctx (&ctx, checksum);
32613 if (dwarf_version < 5)
32615 /* Use the first 8 bytes of the checksum as the dwo_id,
32616 and add it to both comp-unit DIEs. */
32617 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32618 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32621 /* Add the base offset of the ranges table to the skeleton
32622 comp-unit DIE. */
32623 if (!vec_safe_is_empty (ranges_table))
32625 if (dwarf_version < 5)
32626 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32627 ranges_section_label);
32630 output_addr_table ();
32633 /* Output the main compilation unit if non-empty or if .debug_macinfo
32634 or .debug_macro will be emitted. */
32635 output_comp_unit (comp_unit_die (), have_macinfo,
32636 dwarf_split_debug_info ? checksum : NULL);
32638 if (dwarf_split_debug_info && info_section_emitted)
32639 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32641 /* Output the abbreviation table. */
32642 if (vec_safe_length (abbrev_die_table) != 1)
32644 switch_to_section (debug_abbrev_section);
32645 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32646 output_abbrev_section ();
32649 /* Output location list section if necessary. */
32650 if (have_location_lists)
32652 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32653 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32654 /* Output the location lists info. */
32655 switch_to_section (debug_loc_section);
32656 if (dwarf_version >= 5)
32658 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32659 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32660 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32661 dw2_asm_output_data (4, 0xffffffff,
32662 "Initial length escape value indicating "
32663 "64-bit DWARF extension");
32664 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32665 "Length of Location Lists");
32666 ASM_OUTPUT_LABEL (asm_out_file, l1);
32667 output_dwarf_version ();
32668 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32669 dw2_asm_output_data (1, 0, "Segment Size");
32670 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32671 "Offset Entry Count");
32673 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32674 if (dwarf_version >= 5 && dwarf_split_debug_info)
32676 unsigned int save_loc_list_idx = loc_list_idx;
32677 loc_list_idx = 0;
32678 output_loclists_offsets (comp_unit_die ());
32679 gcc_assert (save_loc_list_idx == loc_list_idx);
32681 output_location_lists (comp_unit_die ());
32682 if (dwarf_version >= 5)
32683 ASM_OUTPUT_LABEL (asm_out_file, l2);
32686 output_pubtables ();
32688 /* Output the address range information if a CU (.debug_info section)
32689 was emitted. We output an empty table even if we had no functions
32690 to put in it. This because the consumer has no way to tell the
32691 difference between an empty table that we omitted and failure to
32692 generate a table that would have contained data. */
32693 if (info_section_emitted)
32695 switch_to_section (debug_aranges_section);
32696 output_aranges ();
32699 /* Output ranges section if necessary. */
32700 if (!vec_safe_is_empty (ranges_table))
32702 if (dwarf_version >= 5)
32704 if (dwarf_split_debug_info)
32706 /* We don't know right now whether there are any
32707 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32708 Depending on into which of those two belongs the first
32709 ranges_table entry, emit that section first and that
32710 output_rnglists call will return true if the other kind of
32711 ranges needs to be emitted as well. */
32712 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32713 if (output_rnglists (generation, dwo))
32714 output_rnglists (generation, !dwo);
32716 else
32717 output_rnglists (generation, false);
32719 else
32720 output_ranges ();
32723 /* Have to end the macro section. */
32724 if (have_macinfo)
32726 switch_to_section (debug_macinfo_section);
32727 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32728 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32729 : debug_skeleton_line_section_label, false);
32730 dw2_asm_output_data (1, 0, "End compilation unit");
32733 /* Output the source line correspondence table. We must do this
32734 even if there is no line information. Otherwise, on an empty
32735 translation unit, we will generate a present, but empty,
32736 .debug_info section. IRIX 6.5 `nm' will then complain when
32737 examining the file. This is done late so that any filenames
32738 used by the debug_info section are marked as 'used'. */
32739 switch_to_section (debug_line_section);
32740 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32741 if (! output_asm_line_debug_info ())
32742 output_line_info (false);
32744 if (dwarf_split_debug_info && info_section_emitted)
32746 switch_to_section (debug_skeleton_line_section);
32747 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32748 output_line_info (true);
32751 /* If we emitted any indirect strings, output the string table too. */
32752 if (debug_str_hash || skeleton_debug_str_hash)
32753 output_indirect_strings ();
32754 if (debug_line_str_hash)
32756 switch_to_section (debug_line_str_section);
32757 const enum dwarf_form form = DW_FORM_line_strp;
32758 debug_line_str_hash->traverse<enum dwarf_form,
32759 output_indirect_string> (form);
32762 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32763 symview_upper_bound = 0;
32764 if (zero_view_p)
32765 bitmap_clear (zero_view_p);
32768 /* Returns a hash value for X (which really is a variable_value_struct). */
32770 inline hashval_t
32771 variable_value_hasher::hash (variable_value_struct *x)
32773 return (hashval_t) x->decl_id;
32776 /* Return true if decl_id of variable_value_struct X is the same as
32777 UID of decl Y. */
32779 inline bool
32780 variable_value_hasher::equal (variable_value_struct *x, tree y)
32782 return x->decl_id == DECL_UID (y);
32785 /* Helper function for resolve_variable_value, handle
32786 DW_OP_GNU_variable_value in one location expression.
32787 Return true if exprloc has been changed into loclist. */
32789 static bool
32790 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32792 dw_loc_descr_ref next;
32793 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32795 next = loc->dw_loc_next;
32796 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32797 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32798 continue;
32800 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32801 if (DECL_CONTEXT (decl) != current_function_decl)
32802 continue;
32804 dw_die_ref ref = lookup_decl_die (decl);
32805 if (ref)
32807 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32808 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32809 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32810 continue;
32812 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32813 if (l == NULL)
32814 continue;
32815 if (l->dw_loc_next)
32817 if (AT_class (a) != dw_val_class_loc)
32818 continue;
32819 switch (a->dw_attr)
32821 /* Following attributes allow both exprloc and loclist
32822 classes, so we can change them into a loclist. */
32823 case DW_AT_location:
32824 case DW_AT_string_length:
32825 case DW_AT_return_addr:
32826 case DW_AT_data_member_location:
32827 case DW_AT_frame_base:
32828 case DW_AT_segment:
32829 case DW_AT_static_link:
32830 case DW_AT_use_location:
32831 case DW_AT_vtable_elem_location:
32832 if (prev)
32834 prev->dw_loc_next = NULL;
32835 prepend_loc_descr_to_each (l, AT_loc (a));
32837 if (next)
32838 add_loc_descr_to_each (l, next);
32839 a->dw_attr_val.val_class = dw_val_class_loc_list;
32840 a->dw_attr_val.val_entry = NULL;
32841 a->dw_attr_val.v.val_loc_list = l;
32842 have_location_lists = true;
32843 return true;
32844 /* Following attributes allow both exprloc and reference,
32845 so if the whole expression is DW_OP_GNU_variable_value alone
32846 we could transform it into reference. */
32847 case DW_AT_byte_size:
32848 case DW_AT_bit_size:
32849 case DW_AT_lower_bound:
32850 case DW_AT_upper_bound:
32851 case DW_AT_bit_stride:
32852 case DW_AT_count:
32853 case DW_AT_allocated:
32854 case DW_AT_associated:
32855 case DW_AT_byte_stride:
32856 if (prev == NULL && next == NULL)
32857 break;
32858 /* FALLTHRU */
32859 default:
32860 if (dwarf_strict)
32861 continue;
32862 break;
32864 /* Create DW_TAG_variable that we can refer to. */
32865 gen_decl_die (decl, NULL_TREE, NULL,
32866 lookup_decl_die (current_function_decl));
32867 ref = lookup_decl_die (decl);
32868 if (ref)
32870 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32871 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32872 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32874 continue;
32876 if (prev)
32878 prev->dw_loc_next = l->expr;
32879 add_loc_descr (&prev->dw_loc_next, next);
32880 free_loc_descr (loc, NULL);
32881 next = prev->dw_loc_next;
32883 else
32885 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32886 add_loc_descr (&loc, next);
32887 next = loc;
32889 loc = prev;
32891 return false;
32894 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32896 static void
32897 resolve_variable_value (dw_die_ref die)
32899 dw_attr_node *a;
32900 dw_loc_list_ref loc;
32901 unsigned ix;
32903 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32904 switch (AT_class (a))
32906 case dw_val_class_loc:
32907 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32908 break;
32909 /* FALLTHRU */
32910 case dw_val_class_loc_list:
32911 loc = AT_loc_list (a);
32912 gcc_assert (loc);
32913 for (; loc; loc = loc->dw_loc_next)
32914 resolve_variable_value_in_expr (a, loc->expr);
32915 break;
32916 default:
32917 break;
32921 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32922 temporaries in the current function. */
32924 static void
32925 resolve_variable_values (void)
32927 if (!variable_value_hash || !current_function_decl)
32928 return;
32930 struct variable_value_struct *node
32931 = variable_value_hash->find_with_hash (current_function_decl,
32932 DECL_UID (current_function_decl));
32934 if (node == NULL)
32935 return;
32937 unsigned int i;
32938 dw_die_ref die;
32939 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32940 resolve_variable_value (die);
32943 /* Helper function for note_variable_value, handle one location
32944 expression. */
32946 static void
32947 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32949 for (; loc; loc = loc->dw_loc_next)
32950 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32951 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32953 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32954 dw_die_ref ref = lookup_decl_die (decl);
32955 if (! ref && (flag_generate_lto || flag_generate_offload))
32957 /* ??? This is somewhat a hack because we do not create DIEs
32958 for variables not in BLOCK trees early but when generating
32959 early LTO output we need the dw_val_class_decl_ref to be
32960 fully resolved. For fat LTO objects we'd also like to
32961 undo this after LTO dwarf output. */
32962 gcc_assert (DECL_CONTEXT (decl));
32963 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32964 gcc_assert (ctx != NULL);
32965 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32966 ref = lookup_decl_die (decl);
32967 gcc_assert (ref != NULL);
32969 if (ref)
32971 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32972 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32973 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32974 continue;
32976 if (VAR_P (decl)
32977 && DECL_CONTEXT (decl)
32978 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32979 && lookup_decl_die (DECL_CONTEXT (decl)))
32981 if (!variable_value_hash)
32982 variable_value_hash
32983 = hash_table<variable_value_hasher>::create_ggc (10);
32985 tree fndecl = DECL_CONTEXT (decl);
32986 struct variable_value_struct *node;
32987 struct variable_value_struct **slot
32988 = variable_value_hash->find_slot_with_hash (fndecl,
32989 DECL_UID (fndecl),
32990 INSERT);
32991 if (*slot == NULL)
32993 node = ggc_cleared_alloc<variable_value_struct> ();
32994 node->decl_id = DECL_UID (fndecl);
32995 *slot = node;
32997 else
32998 node = *slot;
33000 vec_safe_push (node->dies, die);
33005 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
33006 with dw_val_class_decl_ref operand. */
33008 static void
33009 note_variable_value (dw_die_ref die)
33011 dw_die_ref c;
33012 dw_attr_node *a;
33013 dw_loc_list_ref loc;
33014 unsigned ix;
33016 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
33017 switch (AT_class (a))
33019 case dw_val_class_loc_list:
33020 loc = AT_loc_list (a);
33021 gcc_assert (loc);
33022 if (!loc->noted_variable_value)
33024 loc->noted_variable_value = 1;
33025 for (; loc; loc = loc->dw_loc_next)
33026 note_variable_value_in_expr (die, loc->expr);
33028 break;
33029 case dw_val_class_loc:
33030 note_variable_value_in_expr (die, AT_loc (a));
33031 break;
33032 default:
33033 break;
33036 /* Mark children. */
33037 FOR_EACH_CHILD (die, c, note_variable_value (c));
33040 /* Process DWARF dies for CTF generation. */
33042 static void
33043 ctf_debug_do_cu (dw_die_ref die)
33045 dw_die_ref c;
33047 if (!ctf_do_die (die))
33048 return;
33050 FOR_EACH_CHILD (die, c, ctf_do_die (c));
33053 /* Perform any cleanups needed after the early debug generation pass
33054 has run. */
33056 static void
33057 dwarf2out_early_finish (const char *filename)
33059 comdat_type_node *ctnode;
33060 set_early_dwarf s;
33061 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
33063 /* PCH might result in DW_AT_producer string being restored from the
33064 header compilation, so always fill it with empty string initially
33065 and overwrite only here. */
33066 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
33068 if (dwarf_record_gcc_switches)
33069 producer_string = gen_producer_string (lang_hooks.name,
33070 save_decoded_options,
33071 save_decoded_options_count);
33072 else
33073 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
33075 producer->dw_attr_val.v.val_str->refcount--;
33076 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
33078 /* Add the name for the main input file now. We delayed this from
33079 dwarf2out_init to avoid complications with PCH. */
33080 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
33081 add_comp_dir_attribute (comp_unit_die ());
33083 /* With LTO early dwarf was really finished at compile-time, so make
33084 sure to adjust the phase after annotating the LTRANS CU DIE. */
33085 if (in_lto_p)
33087 early_dwarf_finished = true;
33088 if (dump_file)
33090 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
33091 print_die (comp_unit_die (), dump_file);
33093 return;
33096 /* Walk through the list of incomplete types again, trying once more to
33097 emit full debugging info for them. */
33098 retry_incomplete_types ();
33100 gen_scheduled_generic_parms_dies ();
33101 gen_remaining_tmpl_value_param_die_attribute ();
33103 /* The point here is to flush out the limbo list so that it is empty
33104 and we don't need to stream it for LTO. */
33105 flush_limbo_die_list ();
33107 /* Add DW_AT_linkage_name for all deferred DIEs. */
33108 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
33110 tree decl = node->created_for;
33111 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
33112 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
33113 ended up in deferred_asm_name before we knew it was
33114 constant and never written to disk. */
33115 && DECL_ASSEMBLER_NAME (decl))
33117 add_linkage_attr (node->die, decl);
33118 move_linkage_attr (node->die);
33121 deferred_asm_name = NULL;
33123 if (flag_eliminate_unused_debug_types)
33124 prune_unused_types ();
33126 /* Generate separate COMDAT sections for type DIEs. */
33127 if (use_debug_types)
33129 break_out_comdat_types (comp_unit_die ());
33131 /* Each new type_unit DIE was added to the limbo die list when created.
33132 Since these have all been added to comdat_type_list, clear the
33133 limbo die list. */
33134 limbo_die_list = NULL;
33136 /* For each new comdat type unit, copy declarations for incomplete
33137 types to make the new unit self-contained (i.e., no direct
33138 references to the main compile unit). */
33139 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33140 copy_decls_for_unworthy_types (ctnode->root_die);
33141 copy_decls_for_unworthy_types (comp_unit_die ());
33143 /* In the process of copying declarations from one unit to another,
33144 we may have left some declarations behind that are no longer
33145 referenced. Prune them. */
33146 prune_unused_types ();
33149 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
33150 with dw_val_class_decl_ref operand. */
33151 note_variable_value (comp_unit_die ());
33152 for (limbo_die_node *node = cu_die_list; node; node = node->next)
33153 note_variable_value (node->die);
33154 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33155 note_variable_value (ctnode->root_die);
33156 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33157 note_variable_value (node->die);
33159 /* The AT_pubnames attribute needs to go in all skeleton dies, including
33160 both the main_cu and all skeleton TUs. Making this call unconditional
33161 would end up either adding a second copy of the AT_pubnames attribute, or
33162 requiring a special case in add_top_level_skeleton_die_attrs. */
33163 if (!dwarf_split_debug_info)
33164 add_AT_pubnames (comp_unit_die ());
33166 /* The early debug phase is now finished. */
33167 early_dwarf_finished = true;
33168 if (dump_file)
33170 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
33171 print_die (comp_unit_die (), dump_file);
33174 /* Generate CTF/BTF debug info. */
33175 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
33176 || btf_debuginfo_p ()) && lang_GNU_C ())
33178 ctf_debug_init ();
33179 ctf_debug_do_cu (comp_unit_die ());
33180 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33181 ctf_debug_do_cu (node->die);
33182 /* Post process the debug data in the CTF container if necessary. */
33183 ctf_debug_init_postprocess (btf_debuginfo_p ());
33185 ctf_debug_early_finish (filename);
33188 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
33189 if ((!flag_generate_lto && !flag_generate_offload)
33190 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
33191 copy_lto_debug_sections operation of the simple object support in
33192 libiberty is not implemented for them yet. */
33193 || TARGET_PECOFF || TARGET_COFF)
33194 return;
33196 /* Now as we are going to output for LTO initialize sections and labels
33197 to the LTO variants. We don't need a random-seed postfix as other
33198 LTO sections as linking the LTO debug sections into one in a partial
33199 link is fine. */
33200 init_sections_and_labels (true);
33202 /* The output below is modeled after dwarf2out_finish with all
33203 location related output removed and some LTO specific changes.
33204 Some refactoring might make both smaller and easier to match up. */
33206 base_types.truncate (0);
33207 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33208 mark_base_types (ctnode->root_die);
33209 mark_base_types (comp_unit_die ());
33210 move_marked_base_types ();
33212 /* Traverse the DIE's and add sibling attributes to those DIE's
33213 that have children. */
33214 add_sibling_attributes (comp_unit_die ());
33215 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33216 add_sibling_attributes (node->die);
33217 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33218 add_sibling_attributes (ctnode->root_die);
33220 /* AIX Assembler inserts the length, so adjust the reference to match the
33221 offset expected by debuggers. */
33222 strcpy (dl_section_ref, debug_line_section_label);
33223 if (XCOFF_DEBUGGING_INFO)
33224 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
33226 if (debug_info_level >= DINFO_LEVEL_TERSE)
33227 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
33229 if (have_macinfo)
33230 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
33231 macinfo_section_label);
33233 save_macinfo_strings ();
33235 if (dwarf_split_debug_info)
33237 unsigned int index = 0;
33238 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
33241 /* Output all of the compilation units. We put the main one last so that
33242 the offsets are available to output_pubnames. */
33243 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33244 output_comp_unit (node->die, 0, NULL);
33246 hash_table<comdat_type_hasher> comdat_type_table (100);
33247 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33249 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
33251 /* Don't output duplicate types. */
33252 if (*slot != HTAB_EMPTY_ENTRY)
33253 continue;
33255 /* Add a pointer to the line table for the main compilation unit
33256 so that the debugger can make sense of DW_AT_decl_file
33257 attributes. */
33258 if (debug_info_level >= DINFO_LEVEL_TERSE)
33259 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
33260 (!dwarf_split_debug_info
33261 ? debug_line_section_label
33262 : debug_skeleton_line_section_label));
33264 output_comdat_type_unit (ctnode, true);
33265 *slot = ctnode;
33268 /* Stick a unique symbol to the main debuginfo section. */
33269 compute_comp_unit_symbol (comp_unit_die ());
33271 /* Output the main compilation unit. We always need it if only for
33272 the CU symbol. */
33273 output_comp_unit (comp_unit_die (), true, NULL);
33275 /* Output the abbreviation table. */
33276 if (vec_safe_length (abbrev_die_table) != 1)
33278 switch_to_section (debug_abbrev_section);
33279 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
33280 output_abbrev_section ();
33283 /* Have to end the macro section. */
33284 if (have_macinfo)
33286 /* We have to save macinfo state if we need to output it again
33287 for the FAT part of the object. */
33288 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
33289 if (flag_fat_lto_objects)
33290 macinfo_table = macinfo_table->copy ();
33292 switch_to_section (debug_macinfo_section);
33293 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
33294 output_macinfo (debug_line_section_label, true);
33295 dw2_asm_output_data (1, 0, "End compilation unit");
33297 if (flag_fat_lto_objects)
33299 vec_free (macinfo_table);
33300 macinfo_table = saved_macinfo_table;
33304 /* Emit a skeleton debug_line section. */
33305 switch_to_section (debug_line_section);
33306 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
33307 output_line_info (true);
33309 /* If we emitted any indirect strings, output the string table too. */
33310 if (debug_str_hash || skeleton_debug_str_hash)
33311 output_indirect_strings ();
33312 if (debug_line_str_hash)
33314 switch_to_section (debug_line_str_section);
33315 const enum dwarf_form form = DW_FORM_line_strp;
33316 debug_line_str_hash->traverse<enum dwarf_form,
33317 output_indirect_string> (form);
33320 /* Switch back to the text section. */
33321 switch_to_section (text_section);
33324 /* Reset all state within dwarf2out.cc so that we can rerun the compiler
33325 within the same process. For use by toplev::finalize. */
33327 void
33328 dwarf2out_cc_finalize (void)
33330 last_var_location_insn = NULL;
33331 cached_next_real_insn = NULL;
33332 used_rtx_array = NULL;
33333 incomplete_types = NULL;
33334 debug_info_section = NULL;
33335 debug_skeleton_info_section = NULL;
33336 debug_abbrev_section = NULL;
33337 debug_skeleton_abbrev_section = NULL;
33338 debug_aranges_section = NULL;
33339 debug_addr_section = NULL;
33340 debug_macinfo_section = NULL;
33341 debug_line_section = NULL;
33342 debug_skeleton_line_section = NULL;
33343 debug_loc_section = NULL;
33344 debug_pubnames_section = NULL;
33345 debug_pubtypes_section = NULL;
33346 debug_str_section = NULL;
33347 debug_line_str_section = NULL;
33348 debug_str_dwo_section = NULL;
33349 debug_str_offsets_section = NULL;
33350 debug_ranges_section = NULL;
33351 debug_ranges_dwo_section = NULL;
33352 debug_frame_section = NULL;
33353 fde_vec = NULL;
33354 debug_str_hash = NULL;
33355 debug_line_str_hash = NULL;
33356 skeleton_debug_str_hash = NULL;
33357 dw2_string_counter = 0;
33358 have_multiple_function_sections = false;
33359 in_text_section_p = false;
33360 cold_text_section = NULL;
33361 last_text_label = NULL;
33362 last_cold_label = NULL;
33363 switch_text_ranges = NULL;
33364 switch_cold_ranges = NULL;
33365 current_unit_personality = NULL;
33367 early_dwarf = false;
33368 early_dwarf_finished = false;
33370 next_die_offset = 0;
33371 single_comp_unit_die = NULL;
33372 comdat_type_list = NULL;
33373 limbo_die_list = NULL;
33374 file_table = NULL;
33375 decl_die_table = NULL;
33376 common_block_die_table = NULL;
33377 decl_loc_table = NULL;
33378 call_arg_locations = NULL;
33379 call_arg_loc_last = NULL;
33380 call_site_count = -1;
33381 tail_call_site_count = -1;
33382 cached_dw_loc_list_table = NULL;
33383 abbrev_die_table = NULL;
33384 delete dwarf_proc_stack_usage_map;
33385 dwarf_proc_stack_usage_map = NULL;
33386 line_info_label_num = 0;
33387 cur_line_info_table = NULL;
33388 text_section_line_info = NULL;
33389 cold_text_section_line_info = NULL;
33390 separate_line_info = NULL;
33391 info_section_emitted = false;
33392 pubname_table = NULL;
33393 pubtype_table = NULL;
33394 macinfo_table = NULL;
33395 ranges_table = NULL;
33396 ranges_by_label = NULL;
33397 rnglist_idx = 0;
33398 have_location_lists = false;
33399 loclabel_num = 0;
33400 poc_label_num = 0;
33401 last_emitted_file = NULL;
33402 label_num = 0;
33403 tmpl_value_parm_die_table = NULL;
33404 generic_type_instances = NULL;
33405 frame_pointer_fb_offset = 0;
33406 frame_pointer_fb_offset_valid = false;
33407 base_types.release ();
33408 XDELETEVEC (producer_string);
33409 producer_string = NULL;
33410 output_line_info_generation = 0;
33411 init_sections_and_labels_generation = 0;
33414 #include "gt-dwarf2out.h"