Emit .file 0 directive earlier in DWARF 5
[official-gcc.git] / gcc / dwarf2out.c
blob9a91981acb078556b677b9cdb230fffa206ec395
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2021 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "print-rtl.h"
87 #include "debug.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "lra.h"
91 #include "dumpfile.h"
92 #include "opts.h"
93 #include "tree-dfa.h"
94 #include "gdb/gdb-index.h"
95 #include "rtl-iter.h"
96 #include "stringpool.h"
97 #include "attribs.h"
98 #include "file-prefix-map.h" /* remap_debug_filename() */
100 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
101 int, bool);
102 static rtx_insn *last_var_location_insn;
103 static rtx_insn *cached_next_real_insn;
104 static void dwarf2out_decl (tree);
105 static bool is_redundant_typedef (const_tree);
107 #ifndef XCOFF_DEBUGGING_INFO
108 #define XCOFF_DEBUGGING_INFO 0
109 #endif
111 #ifndef HAVE_XCOFF_DWARF_EXTRAS
112 #define HAVE_XCOFF_DWARF_EXTRAS 0
113 #endif
115 #ifdef VMS_DEBUGGING_INFO
116 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
118 /* Define this macro to be a nonzero value if the directory specifications
119 which are output in the debug info should end with a separator. */
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
121 /* Define this macro to evaluate to a nonzero value if GCC should refrain
122 from generating indirect strings in DWARF2 debug information, for instance
123 if your target is stuck with an old version of GDB that is unable to
124 process them properly or uses VMS Debug. */
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
126 #else
127 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
128 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
129 #endif
131 /* ??? Poison these here until it can be done generically. They've been
132 totally replaced in this file; make sure it stays that way. */
133 #undef DWARF2_UNWIND_INFO
134 #undef DWARF2_FRAME_INFO
135 #if (GCC_VERSION >= 3000)
136 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
137 #endif
139 /* The size of the target's pointer type. */
140 #ifndef PTR_SIZE
141 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
142 #endif
144 /* Array of RTXes referenced by the debugging information, which therefore
145 must be kept around forever. */
146 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
148 /* A pointer to the base of a list of incomplete types which might be
149 completed at some later time. incomplete_types_list needs to be a
150 vec<tree, va_gc> *because we want to tell the garbage collector about
151 it. */
152 static GTY(()) vec<tree, va_gc> *incomplete_types;
154 /* Pointers to various DWARF2 sections. */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static const char *debug_macinfo_section_name;
163 static unsigned macinfo_label_base = 1;
164 static GTY(()) section *debug_line_section;
165 static GTY(()) section *debug_skeleton_line_section;
166 static GTY(()) section *debug_loc_section;
167 static GTY(()) section *debug_pubnames_section;
168 static GTY(()) section *debug_pubtypes_section;
169 static GTY(()) section *debug_str_section;
170 static GTY(()) section *debug_line_str_section;
171 static GTY(()) section *debug_str_dwo_section;
172 static GTY(()) section *debug_str_offsets_section;
173 static GTY(()) section *debug_ranges_section;
174 static GTY(()) section *debug_ranges_dwo_section;
175 static GTY(()) section *debug_frame_section;
177 /* Maximum size (in bytes) of an artificially generated label. */
178 #define MAX_ARTIFICIAL_LABEL_BYTES 40
180 /* According to the (draft) DWARF 3 specification, the initial length
181 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
182 bytes are 0xffffffff, followed by the length stored in the next 8
183 bytes.
185 However, the SGI/MIPS ABI uses an initial length which is equal to
186 dwarf_offset_size. It is defined (elsewhere) accordingly. */
188 #ifndef DWARF_INITIAL_LENGTH_SIZE
189 #define DWARF_INITIAL_LENGTH_SIZE (dwarf_offset_size == 4 ? 4 : 12)
190 #endif
192 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
193 #define DWARF_INITIAL_LENGTH_SIZE_STR (dwarf_offset_size == 4 ? "-4" : "-12")
194 #endif
196 /* Round SIZE up to the nearest BOUNDARY. */
197 #define DWARF_ROUND(SIZE,BOUNDARY) \
198 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
200 /* CIE identifier. */
201 #if HOST_BITS_PER_WIDE_INT >= 64
202 #define DWARF_CIE_ID \
203 (unsigned HOST_WIDE_INT) (dwarf_offset_size == 4 ? DW_CIE_ID : DW64_CIE_ID)
204 #else
205 #define DWARF_CIE_ID DW_CIE_ID
206 #endif
209 /* A vector for a table that contains frame description
210 information for each routine. */
211 #define NOT_INDEXED (-1U)
212 #define NO_INDEX_ASSIGNED (-2U)
214 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
216 struct GTY((for_user)) indirect_string_node {
217 const char *str;
218 unsigned int refcount;
219 enum dwarf_form form;
220 char *label;
221 unsigned int index;
224 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
226 typedef const char *compare_type;
228 static hashval_t hash (indirect_string_node *);
229 static bool equal (indirect_string_node *, const char *);
232 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
234 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
236 /* With split_debug_info, both the comp_dir and dwo_name go in the
237 main object file, rather than the dwo, similar to the force_direct
238 parameter elsewhere but with additional complications:
240 1) The string is needed in both the main object file and the dwo.
241 That is, the comp_dir and dwo_name will appear in both places.
243 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
244 DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
246 3) GCC chooses the form to use late, depending on the size and
247 reference count.
249 Rather than forcing the all debug string handling functions and
250 callers to deal with these complications, simply use a separate,
251 special-cased string table for any attribute that should go in the
252 main object file. This limits the complexity to just the places
253 that need it. */
255 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
257 static GTY(()) int dw2_string_counter;
259 /* True if the compilation unit places functions in more than one section. */
260 static GTY(()) bool have_multiple_function_sections = false;
262 /* The default cold text section. */
263 static GTY(()) section *cold_text_section;
265 /* True if currently in text section. */
266 static GTY(()) bool in_text_section_p = false;
268 /* Last debug-on location in corresponding section. */
269 static GTY(()) const char *last_text_label;
270 static GTY(()) const char *last_cold_label;
272 /* Mark debug-on/off locations per section.
273 NULL means the section is not used at all. */
274 static GTY(()) vec<const char *, va_gc> *switch_text_ranges;
275 static GTY(()) vec<const char *, va_gc> *switch_cold_ranges;
277 /* The DIE for C++14 'auto' in a function return type. */
278 static GTY(()) dw_die_ref auto_die;
280 /* The DIE for C++14 'decltype(auto)' in a function return type. */
281 static GTY(()) dw_die_ref decltype_auto_die;
283 /* Forward declarations for functions defined in this file. */
285 static void output_call_frame_info (int);
287 /* Personality decl of current unit. Used only when assembler does not support
288 personality CFI. */
289 static GTY(()) rtx current_unit_personality;
291 /* Whether an eh_frame section is required. */
292 static GTY(()) bool do_eh_frame = false;
294 /* .debug_rnglists next index. */
295 static unsigned int rnglist_idx;
297 /* Data and reference forms for relocatable data. */
298 #define DW_FORM_data (dwarf_offset_size == 8 ? DW_FORM_data8 : DW_FORM_data4)
299 #define DW_FORM_ref (dwarf_offset_size == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
301 #ifndef DEBUG_FRAME_SECTION
302 #define DEBUG_FRAME_SECTION ".debug_frame"
303 #endif
305 #ifndef FUNC_BEGIN_LABEL
306 #define FUNC_BEGIN_LABEL "LFB"
307 #endif
309 #ifndef FUNC_SECOND_SECT_LABEL
310 #define FUNC_SECOND_SECT_LABEL "LFSB"
311 #endif
313 #ifndef FUNC_END_LABEL
314 #define FUNC_END_LABEL "LFE"
315 #endif
317 #ifndef PROLOGUE_END_LABEL
318 #define PROLOGUE_END_LABEL "LPE"
319 #endif
321 #ifndef EPILOGUE_BEGIN_LABEL
322 #define EPILOGUE_BEGIN_LABEL "LEB"
323 #endif
325 #ifndef FRAME_BEGIN_LABEL
326 #define FRAME_BEGIN_LABEL "Lframe"
327 #endif
328 #define CIE_AFTER_SIZE_LABEL "LSCIE"
329 #define CIE_END_LABEL "LECIE"
330 #define FDE_LABEL "LSFDE"
331 #define FDE_AFTER_SIZE_LABEL "LASFDE"
332 #define FDE_END_LABEL "LEFDE"
333 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
334 #define LINE_NUMBER_END_LABEL "LELT"
335 #define LN_PROLOG_AS_LABEL "LASLTP"
336 #define LN_PROLOG_END_LABEL "LELTP"
337 #define DIE_LABEL_PREFIX "DW"
339 /* Match the base name of a file to the base name of a compilation unit. */
341 static int
342 matches_main_base (const char *path)
344 /* Cache the last query. */
345 static const char *last_path = NULL;
346 static int last_match = 0;
347 if (path != last_path)
349 const char *base;
350 int length = base_of_path (path, &base);
351 last_path = path;
352 last_match = (length == main_input_baselength
353 && memcmp (base, main_input_basename, length) == 0);
355 return last_match;
358 #ifdef DEBUG_DEBUG_STRUCT
360 static int
361 dump_struct_debug (tree type, enum debug_info_usage usage,
362 enum debug_struct_file criterion, int generic,
363 int matches, int result)
365 /* Find the type name. */
366 tree type_decl = TYPE_STUB_DECL (type);
367 tree t = type_decl;
368 const char *name = 0;
369 if (TREE_CODE (t) == TYPE_DECL)
370 t = DECL_NAME (t);
371 if (t)
372 name = IDENTIFIER_POINTER (t);
374 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
375 criterion,
376 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
377 matches ? "bas" : "hdr",
378 generic ? "gen" : "ord",
379 usage == DINFO_USAGE_DFN ? ";" :
380 usage == DINFO_USAGE_DIR_USE ? "." : "*",
381 result,
382 (void*) type_decl, name);
383 return result;
385 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
386 dump_struct_debug (type, usage, criterion, generic, matches, result)
388 #else
390 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
391 (result)
393 #endif
395 /* Get the number of HOST_WIDE_INTs needed to represent the precision
396 of the number. */
398 static unsigned int
399 get_full_len (const wide_int &op)
401 int prec = wi::get_precision (op);
402 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
403 / 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.c 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 /* Data about a single source file. */
1336 struct GTY((for_user)) dwarf_file_data {
1337 const char * filename;
1338 int emitted_number;
1341 /* Describe an entry into the .debug_addr section. */
1343 enum ate_kind {
1344 ate_kind_rtx,
1345 ate_kind_rtx_dtprel,
1346 ate_kind_label
1349 struct GTY((for_user)) addr_table_entry {
1350 enum ate_kind kind;
1351 unsigned int refcount;
1352 unsigned int index;
1353 union addr_table_entry_struct_union
1355 rtx GTY ((tag ("0"))) rtl;
1356 char * GTY ((tag ("1"))) label;
1358 GTY ((desc ("%1.kind"))) addr;
1361 typedef unsigned int var_loc_view;
1363 /* Location lists are ranges + location descriptions for that range,
1364 so you can track variables that are in different places over
1365 their entire life. */
1366 typedef struct GTY(()) dw_loc_list_struct {
1367 dw_loc_list_ref dw_loc_next;
1368 const char *begin; /* Label and addr_entry for start of range */
1369 addr_table_entry *begin_entry;
1370 const char *end; /* Label for end of range */
1371 addr_table_entry *end_entry;
1372 char *ll_symbol; /* Label for beginning of location list.
1373 Only on head of list. */
1374 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1375 const char *section; /* Section this loclist is relative to */
1376 dw_loc_descr_ref expr;
1377 var_loc_view vbegin, vend;
1378 hashval_t hash;
1379 /* True if all addresses in this and subsequent lists are known to be
1380 resolved. */
1381 bool resolved_addr;
1382 /* True if this list has been replaced by dw_loc_next. */
1383 bool replaced;
1384 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1385 section. */
1386 unsigned char emitted : 1;
1387 /* True if hash field is index rather than hash value. */
1388 unsigned char num_assigned : 1;
1389 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1390 unsigned char offset_emitted : 1;
1391 /* True if note_variable_value_in_expr has been called on it. */
1392 unsigned char noted_variable_value : 1;
1393 /* True if the range should be emitted even if begin and end
1394 are the same. */
1395 bool force;
1396 } dw_loc_list_node;
1398 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1399 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1401 /* Convert a DWARF stack opcode into its string name. */
1403 static const char *
1404 dwarf_stack_op_name (unsigned int op)
1406 const char *name = get_DW_OP_name (op);
1408 if (name != NULL)
1409 return name;
1411 return "OP_<unknown>";
1414 /* Return TRUE iff we're to output location view lists as a separate
1415 attribute next to the location lists, as an extension compatible
1416 with DWARF 2 and above. */
1418 static inline bool
1419 dwarf2out_locviews_in_attribute ()
1421 return debug_variable_location_views == 1;
1424 /* Return TRUE iff we're to output location view lists as part of the
1425 location lists, as proposed for standardization after DWARF 5. */
1427 static inline bool
1428 dwarf2out_locviews_in_loclist ()
1430 #ifndef DW_LLE_view_pair
1431 return false;
1432 #else
1433 return debug_variable_location_views == -1;
1434 #endif
1437 /* Return a pointer to a newly allocated location description. Location
1438 descriptions are simple expression terms that can be strung
1439 together to form more complicated location (address) descriptions. */
1441 static inline dw_loc_descr_ref
1442 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1443 unsigned HOST_WIDE_INT oprnd2)
1445 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1447 descr->dw_loc_opc = op;
1448 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1449 descr->dw_loc_oprnd1.val_entry = NULL;
1450 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1451 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1452 descr->dw_loc_oprnd2.val_entry = NULL;
1453 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1455 return descr;
1458 /* Add a location description term to a location description expression. */
1460 static inline void
1461 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1463 dw_loc_descr_ref *d;
1465 /* Find the end of the chain. */
1466 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1469 *d = descr;
1472 /* Compare two location operands for exact equality. */
1474 static bool
1475 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1477 if (a->val_class != b->val_class)
1478 return false;
1479 switch (a->val_class)
1481 case dw_val_class_none:
1482 return true;
1483 case dw_val_class_addr:
1484 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1486 case dw_val_class_offset:
1487 case dw_val_class_unsigned_const:
1488 case dw_val_class_const:
1489 case dw_val_class_unsigned_const_implicit:
1490 case dw_val_class_const_implicit:
1491 case dw_val_class_range_list:
1492 /* These are all HOST_WIDE_INT, signed or unsigned. */
1493 return a->v.val_unsigned == b->v.val_unsigned;
1495 case dw_val_class_loc:
1496 return a->v.val_loc == b->v.val_loc;
1497 case dw_val_class_loc_list:
1498 return a->v.val_loc_list == b->v.val_loc_list;
1499 case dw_val_class_view_list:
1500 return a->v.val_view_list == b->v.val_view_list;
1501 case dw_val_class_die_ref:
1502 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1503 case dw_val_class_fde_ref:
1504 return a->v.val_fde_index == b->v.val_fde_index;
1505 case dw_val_class_symview:
1506 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1507 case dw_val_class_lbl_id:
1508 case dw_val_class_lineptr:
1509 case dw_val_class_macptr:
1510 case dw_val_class_loclistsptr:
1511 case dw_val_class_high_pc:
1512 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1513 case dw_val_class_str:
1514 return a->v.val_str == b->v.val_str;
1515 case dw_val_class_flag:
1516 return a->v.val_flag == b->v.val_flag;
1517 case dw_val_class_file:
1518 case dw_val_class_file_implicit:
1519 return a->v.val_file == b->v.val_file;
1520 case dw_val_class_decl_ref:
1521 return a->v.val_decl_ref == b->v.val_decl_ref;
1523 case dw_val_class_const_double:
1524 return (a->v.val_double.high == b->v.val_double.high
1525 && a->v.val_double.low == b->v.val_double.low);
1527 case dw_val_class_wide_int:
1528 return *a->v.val_wide == *b->v.val_wide;
1530 case dw_val_class_vec:
1532 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1533 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1535 return (a_len == b_len
1536 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1539 case dw_val_class_data8:
1540 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1542 case dw_val_class_vms_delta:
1543 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1544 && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1546 case dw_val_class_discr_value:
1547 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1548 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1549 case dw_val_class_discr_list:
1550 /* It makes no sense comparing two discriminant value lists. */
1551 return false;
1553 gcc_unreachable ();
1556 /* Compare two location atoms for exact equality. */
1558 static bool
1559 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1561 if (a->dw_loc_opc != b->dw_loc_opc)
1562 return false;
1564 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1565 address size, but since we always allocate cleared storage it
1566 should be zero for other types of locations. */
1567 if (a->dtprel != b->dtprel)
1568 return false;
1570 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1571 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1574 /* Compare two complete location expressions for exact equality. */
1576 bool
1577 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1579 while (1)
1581 if (a == b)
1582 return true;
1583 if (a == NULL || b == NULL)
1584 return false;
1585 if (!loc_descr_equal_p_1 (a, b))
1586 return false;
1588 a = a->dw_loc_next;
1589 b = b->dw_loc_next;
1594 /* Add a constant POLY_OFFSET to a location expression. */
1596 static void
1597 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1599 dw_loc_descr_ref loc;
1600 HOST_WIDE_INT *p;
1602 gcc_assert (*list_head != NULL);
1604 if (known_eq (poly_offset, 0))
1605 return;
1607 /* Find the end of the chain. */
1608 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1611 HOST_WIDE_INT offset;
1612 if (!poly_offset.is_constant (&offset))
1614 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1615 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1616 return;
1619 p = NULL;
1620 if (loc->dw_loc_opc == DW_OP_fbreg
1621 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1622 p = &loc->dw_loc_oprnd1.v.val_int;
1623 else if (loc->dw_loc_opc == DW_OP_bregx)
1624 p = &loc->dw_loc_oprnd2.v.val_int;
1626 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1627 offset. Don't optimize if an signed integer overflow would happen. */
1628 if (p != NULL
1629 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1630 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1631 *p += offset;
1633 else if (offset > 0)
1634 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1636 else
1638 loc->dw_loc_next
1639 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1640 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1644 /* Return a pointer to a newly allocated location description for
1645 REG and OFFSET. */
1647 static inline dw_loc_descr_ref
1648 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1650 HOST_WIDE_INT const_offset;
1651 if (offset.is_constant (&const_offset))
1653 if (reg <= 31)
1654 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1655 const_offset, 0);
1656 else
1657 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1659 else
1661 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1662 loc_descr_plus_const (&ret, offset);
1663 return ret;
1667 /* Add a constant OFFSET to a location list. */
1669 static void
1670 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1672 dw_loc_list_ref d;
1673 for (d = list_head; d != NULL; d = d->dw_loc_next)
1674 loc_descr_plus_const (&d->expr, offset);
1677 #define DWARF_REF_SIZE \
1678 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : dwarf_offset_size)
1680 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1681 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1682 DW_FORM_data16 with 128 bits. */
1683 #define DWARF_LARGEST_DATA_FORM_BITS \
1684 (dwarf_version >= 5 ? 128 : 64)
1686 /* Utility inline function for construction of ops that were GNU extension
1687 before DWARF 5. */
1688 static inline enum dwarf_location_atom
1689 dwarf_OP (enum dwarf_location_atom op)
1691 switch (op)
1693 case DW_OP_implicit_pointer:
1694 if (dwarf_version < 5)
1695 return DW_OP_GNU_implicit_pointer;
1696 break;
1698 case DW_OP_entry_value:
1699 if (dwarf_version < 5)
1700 return DW_OP_GNU_entry_value;
1701 break;
1703 case DW_OP_const_type:
1704 if (dwarf_version < 5)
1705 return DW_OP_GNU_const_type;
1706 break;
1708 case DW_OP_regval_type:
1709 if (dwarf_version < 5)
1710 return DW_OP_GNU_regval_type;
1711 break;
1713 case DW_OP_deref_type:
1714 if (dwarf_version < 5)
1715 return DW_OP_GNU_deref_type;
1716 break;
1718 case DW_OP_convert:
1719 if (dwarf_version < 5)
1720 return DW_OP_GNU_convert;
1721 break;
1723 case DW_OP_reinterpret:
1724 if (dwarf_version < 5)
1725 return DW_OP_GNU_reinterpret;
1726 break;
1728 case DW_OP_addrx:
1729 if (dwarf_version < 5)
1730 return DW_OP_GNU_addr_index;
1731 break;
1733 case DW_OP_constx:
1734 if (dwarf_version < 5)
1735 return DW_OP_GNU_const_index;
1736 break;
1738 default:
1739 break;
1741 return op;
1744 /* Similarly for attributes. */
1745 static inline enum dwarf_attribute
1746 dwarf_AT (enum dwarf_attribute at)
1748 switch (at)
1750 case DW_AT_call_return_pc:
1751 if (dwarf_version < 5)
1752 return DW_AT_low_pc;
1753 break;
1755 case DW_AT_call_tail_call:
1756 if (dwarf_version < 5)
1757 return DW_AT_GNU_tail_call;
1758 break;
1760 case DW_AT_call_origin:
1761 if (dwarf_version < 5)
1762 return DW_AT_abstract_origin;
1763 break;
1765 case DW_AT_call_target:
1766 if (dwarf_version < 5)
1767 return DW_AT_GNU_call_site_target;
1768 break;
1770 case DW_AT_call_target_clobbered:
1771 if (dwarf_version < 5)
1772 return DW_AT_GNU_call_site_target_clobbered;
1773 break;
1775 case DW_AT_call_parameter:
1776 if (dwarf_version < 5)
1777 return DW_AT_abstract_origin;
1778 break;
1780 case DW_AT_call_value:
1781 if (dwarf_version < 5)
1782 return DW_AT_GNU_call_site_value;
1783 break;
1785 case DW_AT_call_data_value:
1786 if (dwarf_version < 5)
1787 return DW_AT_GNU_call_site_data_value;
1788 break;
1790 case DW_AT_call_all_calls:
1791 if (dwarf_version < 5)
1792 return DW_AT_GNU_all_call_sites;
1793 break;
1795 case DW_AT_call_all_tail_calls:
1796 if (dwarf_version < 5)
1797 return DW_AT_GNU_all_tail_call_sites;
1798 break;
1800 case DW_AT_dwo_name:
1801 if (dwarf_version < 5)
1802 return DW_AT_GNU_dwo_name;
1803 break;
1805 case DW_AT_addr_base:
1806 if (dwarf_version < 5)
1807 return DW_AT_GNU_addr_base;
1808 break;
1810 default:
1811 break;
1813 return at;
1816 /* And similarly for tags. */
1817 static inline enum dwarf_tag
1818 dwarf_TAG (enum dwarf_tag tag)
1820 switch (tag)
1822 case DW_TAG_call_site:
1823 if (dwarf_version < 5)
1824 return DW_TAG_GNU_call_site;
1825 break;
1827 case DW_TAG_call_site_parameter:
1828 if (dwarf_version < 5)
1829 return DW_TAG_GNU_call_site_parameter;
1830 break;
1832 default:
1833 break;
1835 return tag;
1838 /* And similarly for forms. */
1839 static inline enum dwarf_form
1840 dwarf_FORM (enum dwarf_form form)
1842 switch (form)
1844 case DW_FORM_addrx:
1845 if (dwarf_version < 5)
1846 return DW_FORM_GNU_addr_index;
1847 break;
1849 case DW_FORM_strx:
1850 if (dwarf_version < 5)
1851 return DW_FORM_GNU_str_index;
1852 break;
1854 default:
1855 break;
1857 return form;
1860 static unsigned long int get_base_type_offset (dw_die_ref);
1862 /* Return the size of a location descriptor. */
1864 static unsigned long
1865 size_of_loc_descr (dw_loc_descr_ref loc)
1867 unsigned long size = 1;
1869 switch (loc->dw_loc_opc)
1871 case DW_OP_addr:
1872 size += DWARF2_ADDR_SIZE;
1873 break;
1874 case DW_OP_GNU_addr_index:
1875 case DW_OP_addrx:
1876 case DW_OP_GNU_const_index:
1877 case DW_OP_constx:
1878 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1879 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1880 break;
1881 case DW_OP_const1u:
1882 case DW_OP_const1s:
1883 size += 1;
1884 break;
1885 case DW_OP_const2u:
1886 case DW_OP_const2s:
1887 size += 2;
1888 break;
1889 case DW_OP_const4u:
1890 case DW_OP_const4s:
1891 size += 4;
1892 break;
1893 case DW_OP_const8u:
1894 case DW_OP_const8s:
1895 size += 8;
1896 break;
1897 case DW_OP_constu:
1898 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1899 break;
1900 case DW_OP_consts:
1901 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1902 break;
1903 case DW_OP_pick:
1904 size += 1;
1905 break;
1906 case DW_OP_plus_uconst:
1907 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1908 break;
1909 case DW_OP_skip:
1910 case DW_OP_bra:
1911 size += 2;
1912 break;
1913 case DW_OP_breg0:
1914 case DW_OP_breg1:
1915 case DW_OP_breg2:
1916 case DW_OP_breg3:
1917 case DW_OP_breg4:
1918 case DW_OP_breg5:
1919 case DW_OP_breg6:
1920 case DW_OP_breg7:
1921 case DW_OP_breg8:
1922 case DW_OP_breg9:
1923 case DW_OP_breg10:
1924 case DW_OP_breg11:
1925 case DW_OP_breg12:
1926 case DW_OP_breg13:
1927 case DW_OP_breg14:
1928 case DW_OP_breg15:
1929 case DW_OP_breg16:
1930 case DW_OP_breg17:
1931 case DW_OP_breg18:
1932 case DW_OP_breg19:
1933 case DW_OP_breg20:
1934 case DW_OP_breg21:
1935 case DW_OP_breg22:
1936 case DW_OP_breg23:
1937 case DW_OP_breg24:
1938 case DW_OP_breg25:
1939 case DW_OP_breg26:
1940 case DW_OP_breg27:
1941 case DW_OP_breg28:
1942 case DW_OP_breg29:
1943 case DW_OP_breg30:
1944 case DW_OP_breg31:
1945 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1946 break;
1947 case DW_OP_regx:
1948 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1949 break;
1950 case DW_OP_fbreg:
1951 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1952 break;
1953 case DW_OP_bregx:
1954 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1955 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1956 break;
1957 case DW_OP_piece:
1958 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1959 break;
1960 case DW_OP_bit_piece:
1961 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1962 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1963 break;
1964 case DW_OP_deref_size:
1965 case DW_OP_xderef_size:
1966 size += 1;
1967 break;
1968 case DW_OP_call2:
1969 size += 2;
1970 break;
1971 case DW_OP_call4:
1972 size += 4;
1973 break;
1974 case DW_OP_call_ref:
1975 case DW_OP_GNU_variable_value:
1976 size += DWARF_REF_SIZE;
1977 break;
1978 case DW_OP_implicit_value:
1979 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1980 + loc->dw_loc_oprnd1.v.val_unsigned;
1981 break;
1982 case DW_OP_implicit_pointer:
1983 case DW_OP_GNU_implicit_pointer:
1984 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1985 break;
1986 case DW_OP_entry_value:
1987 case DW_OP_GNU_entry_value:
1989 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1990 size += size_of_uleb128 (op_size) + op_size;
1991 break;
1993 case DW_OP_const_type:
1994 case DW_OP_GNU_const_type:
1996 unsigned long o
1997 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1998 size += size_of_uleb128 (o) + 1;
1999 switch (loc->dw_loc_oprnd2.val_class)
2001 case dw_val_class_vec:
2002 size += loc->dw_loc_oprnd2.v.val_vec.length
2003 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
2004 break;
2005 case dw_val_class_const:
2006 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
2007 break;
2008 case dw_val_class_const_double:
2009 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
2010 break;
2011 case dw_val_class_wide_int:
2012 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
2013 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
2014 break;
2015 default:
2016 gcc_unreachable ();
2018 break;
2020 case DW_OP_regval_type:
2021 case DW_OP_GNU_regval_type:
2023 unsigned long o
2024 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2025 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
2026 + size_of_uleb128 (o);
2028 break;
2029 case DW_OP_deref_type:
2030 case DW_OP_GNU_deref_type:
2032 unsigned long o
2033 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2034 size += 1 + size_of_uleb128 (o);
2036 break;
2037 case DW_OP_convert:
2038 case DW_OP_reinterpret:
2039 case DW_OP_GNU_convert:
2040 case DW_OP_GNU_reinterpret:
2041 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2042 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2043 else
2045 unsigned long o
2046 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2047 size += size_of_uleb128 (o);
2049 break;
2050 case DW_OP_GNU_parameter_ref:
2051 size += 4;
2052 break;
2053 default:
2054 break;
2057 return size;
2060 /* Return the size of a series of location descriptors. */
2062 unsigned long
2063 size_of_locs (dw_loc_descr_ref loc)
2065 dw_loc_descr_ref l;
2066 unsigned long size;
2068 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2069 field, to avoid writing to a PCH file. */
2070 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2072 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2073 break;
2074 size += size_of_loc_descr (l);
2076 if (! l)
2077 return size;
2079 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2081 l->dw_loc_addr = size;
2082 size += size_of_loc_descr (l);
2085 return size;
2088 /* Return the size of the value in a DW_AT_discr_value attribute. */
2090 static int
2091 size_of_discr_value (dw_discr_value *discr_value)
2093 if (discr_value->pos)
2094 return size_of_uleb128 (discr_value->v.uval);
2095 else
2096 return size_of_sleb128 (discr_value->v.sval);
2099 /* Return the size of the value in a DW_AT_discr_list attribute. */
2101 static int
2102 size_of_discr_list (dw_discr_list_ref discr_list)
2104 int size = 0;
2106 for (dw_discr_list_ref list = discr_list;
2107 list != NULL;
2108 list = list->dw_discr_next)
2110 /* One byte for the discriminant value descriptor, and then one or two
2111 LEB128 numbers, depending on whether it's a single case label or a
2112 range label. */
2113 size += 1;
2114 size += size_of_discr_value (&list->dw_discr_lower_bound);
2115 if (list->dw_discr_range != 0)
2116 size += size_of_discr_value (&list->dw_discr_upper_bound);
2118 return size;
2121 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2122 static void get_ref_die_offset_label (char *, dw_die_ref);
2123 static unsigned long int get_ref_die_offset (dw_die_ref);
2125 /* Output location description stack opcode's operands (if any).
2126 The for_eh_or_skip parameter controls whether register numbers are
2127 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2128 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2129 info). This should be suppressed for the cases that have not been converted
2130 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2132 static void
2133 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2135 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2136 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2138 switch (loc->dw_loc_opc)
2140 #ifdef DWARF2_DEBUGGING_INFO
2141 case DW_OP_const2u:
2142 case DW_OP_const2s:
2143 dw2_asm_output_data (2, val1->v.val_int, NULL);
2144 break;
2145 case DW_OP_const4u:
2146 if (loc->dtprel)
2148 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2149 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2150 val1->v.val_addr);
2151 fputc ('\n', asm_out_file);
2152 break;
2154 /* FALLTHRU */
2155 case DW_OP_const4s:
2156 dw2_asm_output_data (4, val1->v.val_int, NULL);
2157 break;
2158 case DW_OP_const8u:
2159 if (loc->dtprel)
2161 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2162 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2163 val1->v.val_addr);
2164 fputc ('\n', asm_out_file);
2165 break;
2167 /* FALLTHRU */
2168 case DW_OP_const8s:
2169 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2170 dw2_asm_output_data (8, val1->v.val_int, NULL);
2171 break;
2172 case DW_OP_skip:
2173 case DW_OP_bra:
2175 int offset;
2177 gcc_assert (val1->val_class == dw_val_class_loc);
2178 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2180 dw2_asm_output_data (2, offset, NULL);
2182 break;
2183 case DW_OP_implicit_value:
2184 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2185 switch (val2->val_class)
2187 case dw_val_class_const:
2188 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2189 break;
2190 case dw_val_class_vec:
2192 unsigned int elt_size = val2->v.val_vec.elt_size;
2193 unsigned int len = val2->v.val_vec.length;
2194 unsigned int i;
2195 unsigned char *p;
2197 if (elt_size > sizeof (HOST_WIDE_INT))
2199 elt_size /= 2;
2200 len *= 2;
2202 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2203 i < len;
2204 i++, p += elt_size)
2205 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2206 "fp or vector constant word %u", i);
2208 break;
2209 case dw_val_class_const_double:
2211 unsigned HOST_WIDE_INT first, second;
2213 if (WORDS_BIG_ENDIAN)
2215 first = val2->v.val_double.high;
2216 second = val2->v.val_double.low;
2218 else
2220 first = val2->v.val_double.low;
2221 second = val2->v.val_double.high;
2223 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2224 first, NULL);
2225 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2226 second, NULL);
2228 break;
2229 case dw_val_class_wide_int:
2231 int i;
2232 int len = get_full_len (*val2->v.val_wide);
2233 if (WORDS_BIG_ENDIAN)
2234 for (i = len - 1; i >= 0; --i)
2235 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2236 val2->v.val_wide->elt (i), NULL);
2237 else
2238 for (i = 0; i < len; ++i)
2239 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2240 val2->v.val_wide->elt (i), NULL);
2242 break;
2243 case dw_val_class_addr:
2244 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2245 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2246 break;
2247 default:
2248 gcc_unreachable ();
2250 break;
2251 #else
2252 case DW_OP_const2u:
2253 case DW_OP_const2s:
2254 case DW_OP_const4u:
2255 case DW_OP_const4s:
2256 case DW_OP_const8u:
2257 case DW_OP_const8s:
2258 case DW_OP_skip:
2259 case DW_OP_bra:
2260 case DW_OP_implicit_value:
2261 /* We currently don't make any attempt to make sure these are
2262 aligned properly like we do for the main unwind info, so
2263 don't support emitting things larger than a byte if we're
2264 only doing unwinding. */
2265 gcc_unreachable ();
2266 #endif
2267 case DW_OP_const1u:
2268 case DW_OP_const1s:
2269 dw2_asm_output_data (1, val1->v.val_int, NULL);
2270 break;
2271 case DW_OP_constu:
2272 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2273 break;
2274 case DW_OP_consts:
2275 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2276 break;
2277 case DW_OP_pick:
2278 dw2_asm_output_data (1, val1->v.val_int, NULL);
2279 break;
2280 case DW_OP_plus_uconst:
2281 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2282 break;
2283 case DW_OP_breg0:
2284 case DW_OP_breg1:
2285 case DW_OP_breg2:
2286 case DW_OP_breg3:
2287 case DW_OP_breg4:
2288 case DW_OP_breg5:
2289 case DW_OP_breg6:
2290 case DW_OP_breg7:
2291 case DW_OP_breg8:
2292 case DW_OP_breg9:
2293 case DW_OP_breg10:
2294 case DW_OP_breg11:
2295 case DW_OP_breg12:
2296 case DW_OP_breg13:
2297 case DW_OP_breg14:
2298 case DW_OP_breg15:
2299 case DW_OP_breg16:
2300 case DW_OP_breg17:
2301 case DW_OP_breg18:
2302 case DW_OP_breg19:
2303 case DW_OP_breg20:
2304 case DW_OP_breg21:
2305 case DW_OP_breg22:
2306 case DW_OP_breg23:
2307 case DW_OP_breg24:
2308 case DW_OP_breg25:
2309 case DW_OP_breg26:
2310 case DW_OP_breg27:
2311 case DW_OP_breg28:
2312 case DW_OP_breg29:
2313 case DW_OP_breg30:
2314 case DW_OP_breg31:
2315 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2316 break;
2317 case DW_OP_regx:
2319 unsigned r = val1->v.val_unsigned;
2320 if (for_eh_or_skip >= 0)
2321 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2322 gcc_assert (size_of_uleb128 (r)
2323 == size_of_uleb128 (val1->v.val_unsigned));
2324 dw2_asm_output_data_uleb128 (r, NULL);
2326 break;
2327 case DW_OP_fbreg:
2328 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2329 break;
2330 case DW_OP_bregx:
2332 unsigned r = val1->v.val_unsigned;
2333 if (for_eh_or_skip >= 0)
2334 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2335 gcc_assert (size_of_uleb128 (r)
2336 == size_of_uleb128 (val1->v.val_unsigned));
2337 dw2_asm_output_data_uleb128 (r, NULL);
2338 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2340 break;
2341 case DW_OP_piece:
2342 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2343 break;
2344 case DW_OP_bit_piece:
2345 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2346 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2347 break;
2348 case DW_OP_deref_size:
2349 case DW_OP_xderef_size:
2350 dw2_asm_output_data (1, val1->v.val_int, NULL);
2351 break;
2353 case DW_OP_addr:
2354 if (loc->dtprel)
2356 if (targetm.asm_out.output_dwarf_dtprel)
2358 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2359 DWARF2_ADDR_SIZE,
2360 val1->v.val_addr);
2361 fputc ('\n', asm_out_file);
2363 else
2364 gcc_unreachable ();
2366 else
2368 #ifdef DWARF2_DEBUGGING_INFO
2369 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2370 #else
2371 gcc_unreachable ();
2372 #endif
2374 break;
2376 case DW_OP_GNU_addr_index:
2377 case DW_OP_addrx:
2378 case DW_OP_GNU_const_index:
2379 case DW_OP_constx:
2380 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2381 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2382 "(index into .debug_addr)");
2383 break;
2385 case DW_OP_call2:
2386 case DW_OP_call4:
2388 unsigned long die_offset
2389 = get_ref_die_offset (val1->v.val_die_ref.die);
2390 /* Make sure the offset has been computed and that we can encode it as
2391 an operand. */
2392 gcc_assert (die_offset > 0
2393 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2394 ? 0xffff
2395 : 0xffffffff));
2396 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2397 die_offset, NULL);
2399 break;
2401 case DW_OP_call_ref:
2402 case DW_OP_GNU_variable_value:
2404 char label[MAX_ARTIFICIAL_LABEL_BYTES
2405 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2406 gcc_assert (val1->val_class == dw_val_class_die_ref);
2407 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2408 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2410 break;
2412 case DW_OP_implicit_pointer:
2413 case DW_OP_GNU_implicit_pointer:
2415 char label[MAX_ARTIFICIAL_LABEL_BYTES
2416 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2417 gcc_assert (val1->val_class == dw_val_class_die_ref);
2418 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2419 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2420 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2422 break;
2424 case DW_OP_entry_value:
2425 case DW_OP_GNU_entry_value:
2426 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2427 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2428 break;
2430 case DW_OP_const_type:
2431 case DW_OP_GNU_const_type:
2433 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2434 gcc_assert (o);
2435 dw2_asm_output_data_uleb128 (o, NULL);
2436 switch (val2->val_class)
2438 case dw_val_class_const:
2439 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2440 dw2_asm_output_data (1, l, NULL);
2441 dw2_asm_output_data (l, val2->v.val_int, NULL);
2442 break;
2443 case dw_val_class_vec:
2445 unsigned int elt_size = val2->v.val_vec.elt_size;
2446 unsigned int len = val2->v.val_vec.length;
2447 unsigned int i;
2448 unsigned char *p;
2450 l = len * elt_size;
2451 dw2_asm_output_data (1, l, NULL);
2452 if (elt_size > sizeof (HOST_WIDE_INT))
2454 elt_size /= 2;
2455 len *= 2;
2457 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2458 i < len;
2459 i++, p += elt_size)
2460 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2461 "fp or vector constant word %u", i);
2463 break;
2464 case dw_val_class_const_double:
2466 unsigned HOST_WIDE_INT first, second;
2467 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2469 dw2_asm_output_data (1, 2 * l, NULL);
2470 if (WORDS_BIG_ENDIAN)
2472 first = val2->v.val_double.high;
2473 second = val2->v.val_double.low;
2475 else
2477 first = val2->v.val_double.low;
2478 second = val2->v.val_double.high;
2480 dw2_asm_output_data (l, first, NULL);
2481 dw2_asm_output_data (l, second, NULL);
2483 break;
2484 case dw_val_class_wide_int:
2486 int i;
2487 int len = get_full_len (*val2->v.val_wide);
2488 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2490 dw2_asm_output_data (1, len * l, NULL);
2491 if (WORDS_BIG_ENDIAN)
2492 for (i = len - 1; i >= 0; --i)
2493 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2494 else
2495 for (i = 0; i < len; ++i)
2496 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2498 break;
2499 default:
2500 gcc_unreachable ();
2503 break;
2504 case DW_OP_regval_type:
2505 case DW_OP_GNU_regval_type:
2507 unsigned r = val1->v.val_unsigned;
2508 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2509 gcc_assert (o);
2510 if (for_eh_or_skip >= 0)
2512 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2513 gcc_assert (size_of_uleb128 (r)
2514 == size_of_uleb128 (val1->v.val_unsigned));
2516 dw2_asm_output_data_uleb128 (r, NULL);
2517 dw2_asm_output_data_uleb128 (o, NULL);
2519 break;
2520 case DW_OP_deref_type:
2521 case DW_OP_GNU_deref_type:
2523 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2524 gcc_assert (o);
2525 dw2_asm_output_data (1, val1->v.val_int, NULL);
2526 dw2_asm_output_data_uleb128 (o, NULL);
2528 break;
2529 case DW_OP_convert:
2530 case DW_OP_reinterpret:
2531 case DW_OP_GNU_convert:
2532 case DW_OP_GNU_reinterpret:
2533 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2534 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2535 else
2537 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2538 gcc_assert (o);
2539 dw2_asm_output_data_uleb128 (o, NULL);
2541 break;
2543 case DW_OP_GNU_parameter_ref:
2545 unsigned long o;
2546 gcc_assert (val1->val_class == dw_val_class_die_ref);
2547 o = get_ref_die_offset (val1->v.val_die_ref.die);
2548 dw2_asm_output_data (4, o, NULL);
2550 break;
2552 default:
2553 /* Other codes have no operands. */
2554 break;
2558 /* Output a sequence of location operations.
2559 The for_eh_or_skip parameter controls whether register numbers are
2560 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2561 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2562 info). This should be suppressed for the cases that have not been converted
2563 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2565 void
2566 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2568 for (; loc != NULL; loc = loc->dw_loc_next)
2570 enum dwarf_location_atom opc = loc->dw_loc_opc;
2571 /* Output the opcode. */
2572 if (for_eh_or_skip >= 0
2573 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2575 unsigned r = (opc - DW_OP_breg0);
2576 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2577 gcc_assert (r <= 31);
2578 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2580 else if (for_eh_or_skip >= 0
2581 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2583 unsigned r = (opc - DW_OP_reg0);
2584 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2585 gcc_assert (r <= 31);
2586 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2589 dw2_asm_output_data (1, opc,
2590 "%s", dwarf_stack_op_name (opc));
2592 /* Output the operand(s) (if any). */
2593 output_loc_operands (loc, for_eh_or_skip);
2597 /* Output location description stack opcode's operands (if any).
2598 The output is single bytes on a line, suitable for .cfi_escape. */
2600 static void
2601 output_loc_operands_raw (dw_loc_descr_ref loc)
2603 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2604 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2606 switch (loc->dw_loc_opc)
2608 case DW_OP_addr:
2609 case DW_OP_GNU_addr_index:
2610 case DW_OP_addrx:
2611 case DW_OP_GNU_const_index:
2612 case DW_OP_constx:
2613 case DW_OP_implicit_value:
2614 /* We cannot output addresses in .cfi_escape, only bytes. */
2615 gcc_unreachable ();
2617 case DW_OP_const1u:
2618 case DW_OP_const1s:
2619 case DW_OP_pick:
2620 case DW_OP_deref_size:
2621 case DW_OP_xderef_size:
2622 fputc (',', asm_out_file);
2623 dw2_asm_output_data_raw (1, val1->v.val_int);
2624 break;
2626 case DW_OP_const2u:
2627 case DW_OP_const2s:
2628 fputc (',', asm_out_file);
2629 dw2_asm_output_data_raw (2, val1->v.val_int);
2630 break;
2632 case DW_OP_const4u:
2633 case DW_OP_const4s:
2634 fputc (',', asm_out_file);
2635 dw2_asm_output_data_raw (4, val1->v.val_int);
2636 break;
2638 case DW_OP_const8u:
2639 case DW_OP_const8s:
2640 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2641 fputc (',', asm_out_file);
2642 dw2_asm_output_data_raw (8, val1->v.val_int);
2643 break;
2645 case DW_OP_skip:
2646 case DW_OP_bra:
2648 int offset;
2650 gcc_assert (val1->val_class == dw_val_class_loc);
2651 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2653 fputc (',', asm_out_file);
2654 dw2_asm_output_data_raw (2, offset);
2656 break;
2658 case DW_OP_regx:
2660 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2661 gcc_assert (size_of_uleb128 (r)
2662 == size_of_uleb128 (val1->v.val_unsigned));
2663 fputc (',', asm_out_file);
2664 dw2_asm_output_data_uleb128_raw (r);
2666 break;
2668 case DW_OP_constu:
2669 case DW_OP_plus_uconst:
2670 case DW_OP_piece:
2671 fputc (',', asm_out_file);
2672 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2673 break;
2675 case DW_OP_bit_piece:
2676 fputc (',', asm_out_file);
2677 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2678 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2679 break;
2681 case DW_OP_consts:
2682 case DW_OP_breg0:
2683 case DW_OP_breg1:
2684 case DW_OP_breg2:
2685 case DW_OP_breg3:
2686 case DW_OP_breg4:
2687 case DW_OP_breg5:
2688 case DW_OP_breg6:
2689 case DW_OP_breg7:
2690 case DW_OP_breg8:
2691 case DW_OP_breg9:
2692 case DW_OP_breg10:
2693 case DW_OP_breg11:
2694 case DW_OP_breg12:
2695 case DW_OP_breg13:
2696 case DW_OP_breg14:
2697 case DW_OP_breg15:
2698 case DW_OP_breg16:
2699 case DW_OP_breg17:
2700 case DW_OP_breg18:
2701 case DW_OP_breg19:
2702 case DW_OP_breg20:
2703 case DW_OP_breg21:
2704 case DW_OP_breg22:
2705 case DW_OP_breg23:
2706 case DW_OP_breg24:
2707 case DW_OP_breg25:
2708 case DW_OP_breg26:
2709 case DW_OP_breg27:
2710 case DW_OP_breg28:
2711 case DW_OP_breg29:
2712 case DW_OP_breg30:
2713 case DW_OP_breg31:
2714 case DW_OP_fbreg:
2715 fputc (',', asm_out_file);
2716 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2717 break;
2719 case DW_OP_bregx:
2721 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2722 gcc_assert (size_of_uleb128 (r)
2723 == size_of_uleb128 (val1->v.val_unsigned));
2724 fputc (',', asm_out_file);
2725 dw2_asm_output_data_uleb128_raw (r);
2726 fputc (',', asm_out_file);
2727 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2729 break;
2731 case DW_OP_implicit_pointer:
2732 case DW_OP_entry_value:
2733 case DW_OP_const_type:
2734 case DW_OP_regval_type:
2735 case DW_OP_deref_type:
2736 case DW_OP_convert:
2737 case DW_OP_reinterpret:
2738 case DW_OP_GNU_implicit_pointer:
2739 case DW_OP_GNU_entry_value:
2740 case DW_OP_GNU_const_type:
2741 case DW_OP_GNU_regval_type:
2742 case DW_OP_GNU_deref_type:
2743 case DW_OP_GNU_convert:
2744 case DW_OP_GNU_reinterpret:
2745 case DW_OP_GNU_parameter_ref:
2746 gcc_unreachable ();
2747 break;
2749 default:
2750 /* Other codes have no operands. */
2751 break;
2755 void
2756 output_loc_sequence_raw (dw_loc_descr_ref loc)
2758 while (1)
2760 enum dwarf_location_atom opc = loc->dw_loc_opc;
2761 /* Output the opcode. */
2762 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2764 unsigned r = (opc - DW_OP_breg0);
2765 r = DWARF2_FRAME_REG_OUT (r, 1);
2766 gcc_assert (r <= 31);
2767 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2769 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2771 unsigned r = (opc - DW_OP_reg0);
2772 r = DWARF2_FRAME_REG_OUT (r, 1);
2773 gcc_assert (r <= 31);
2774 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2776 /* Output the opcode. */
2777 fprintf (asm_out_file, "%#x", opc);
2778 output_loc_operands_raw (loc);
2780 if (!loc->dw_loc_next)
2781 break;
2782 loc = loc->dw_loc_next;
2784 fputc (',', asm_out_file);
2788 /* This function builds a dwarf location descriptor sequence from a
2789 dw_cfa_location, adding the given OFFSET to the result of the
2790 expression. */
2792 struct dw_loc_descr_node *
2793 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2795 struct dw_loc_descr_node *head, *tmp;
2797 offset += cfa->offset;
2799 if (cfa->indirect)
2801 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2802 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2803 head->dw_loc_oprnd1.val_entry = NULL;
2804 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2805 add_loc_descr (&head, tmp);
2806 loc_descr_plus_const (&head, offset);
2808 else
2809 head = new_reg_loc_descr (cfa->reg, offset);
2811 return head;
2814 /* This function builds a dwarf location descriptor sequence for
2815 the address at OFFSET from the CFA when stack is aligned to
2816 ALIGNMENT byte. */
2818 struct dw_loc_descr_node *
2819 build_cfa_aligned_loc (dw_cfa_location *cfa,
2820 poly_int64 offset, HOST_WIDE_INT alignment)
2822 struct dw_loc_descr_node *head;
2823 unsigned int dwarf_fp
2824 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2826 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2827 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2829 head = new_reg_loc_descr (dwarf_fp, 0);
2830 add_loc_descr (&head, int_loc_descriptor (alignment));
2831 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2832 loc_descr_plus_const (&head, offset);
2834 else
2835 head = new_reg_loc_descr (dwarf_fp, offset);
2836 return head;
2839 /* And now, the support for symbolic debugging information. */
2841 /* .debug_str support. */
2843 static void dwarf2out_init (const char *);
2844 static void dwarf2out_finish (const char *);
2845 static void dwarf2out_early_finish (const char *);
2846 static void dwarf2out_assembly_start (void);
2847 static void dwarf2out_define (unsigned int, const char *);
2848 static void dwarf2out_undef (unsigned int, const char *);
2849 static void dwarf2out_start_source_file (unsigned, const char *);
2850 static void dwarf2out_end_source_file (unsigned);
2851 static void dwarf2out_function_decl (tree);
2852 static void dwarf2out_begin_block (unsigned, unsigned);
2853 static void dwarf2out_end_block (unsigned, unsigned);
2854 static bool dwarf2out_ignore_block (const_tree);
2855 static void dwarf2out_set_ignored_loc (unsigned, unsigned, const char *);
2856 static void dwarf2out_early_global_decl (tree);
2857 static void dwarf2out_late_global_decl (tree);
2858 static void dwarf2out_type_decl (tree, int);
2859 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2860 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2861 dw_die_ref);
2862 static void dwarf2out_abstract_function (tree);
2863 static void dwarf2out_var_location (rtx_insn *);
2864 static void dwarf2out_inline_entry (tree);
2865 static void dwarf2out_size_function (tree);
2866 static void dwarf2out_begin_function (tree);
2867 static void dwarf2out_end_function (unsigned int);
2868 static void dwarf2out_register_main_translation_unit (tree unit);
2869 static void dwarf2out_set_name (tree, tree);
2870 static void dwarf2out_register_external_die (tree decl, const char *sym,
2871 unsigned HOST_WIDE_INT off);
2872 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2873 unsigned HOST_WIDE_INT *off);
2875 /* The debug hooks structure. */
2877 const struct gcc_debug_hooks dwarf2_debug_hooks =
2879 dwarf2out_init,
2880 dwarf2out_finish,
2881 dwarf2out_early_finish,
2882 dwarf2out_assembly_start,
2883 dwarf2out_define,
2884 dwarf2out_undef,
2885 dwarf2out_start_source_file,
2886 dwarf2out_end_source_file,
2887 dwarf2out_begin_block,
2888 dwarf2out_end_block,
2889 dwarf2out_ignore_block,
2890 dwarf2out_source_line,
2891 dwarf2out_set_ignored_loc,
2892 dwarf2out_begin_prologue,
2893 #if VMS_DEBUGGING_INFO
2894 dwarf2out_vms_end_prologue,
2895 dwarf2out_vms_begin_epilogue,
2896 #else
2897 debug_nothing_int_charstar,
2898 debug_nothing_int_charstar,
2899 #endif
2900 dwarf2out_end_epilogue,
2901 dwarf2out_begin_function,
2902 dwarf2out_end_function, /* end_function */
2903 dwarf2out_register_main_translation_unit,
2904 dwarf2out_function_decl, /* function_decl */
2905 dwarf2out_early_global_decl,
2906 dwarf2out_late_global_decl,
2907 dwarf2out_type_decl, /* type_decl */
2908 dwarf2out_imported_module_or_decl,
2909 dwarf2out_die_ref_for_decl,
2910 dwarf2out_register_external_die,
2911 debug_nothing_tree, /* deferred_inline_function */
2912 /* The DWARF 2 backend tries to reduce debugging bloat by not
2913 emitting the abstract description of inline functions until
2914 something tries to reference them. */
2915 dwarf2out_abstract_function, /* outlining_inline_function */
2916 debug_nothing_rtx_code_label, /* label */
2917 debug_nothing_int, /* handle_pch */
2918 dwarf2out_var_location,
2919 dwarf2out_inline_entry, /* inline_entry */
2920 dwarf2out_size_function, /* size_function */
2921 dwarf2out_switch_text_section,
2922 dwarf2out_set_name,
2923 1, /* start_end_main_source_file */
2924 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2927 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2929 dwarf2out_init,
2930 debug_nothing_charstar,
2931 debug_nothing_charstar,
2932 dwarf2out_assembly_start,
2933 debug_nothing_int_charstar,
2934 debug_nothing_int_charstar,
2935 debug_nothing_int_charstar,
2936 debug_nothing_int,
2937 debug_nothing_int_int, /* begin_block */
2938 debug_nothing_int_int, /* end_block */
2939 debug_true_const_tree, /* ignore_block */
2940 dwarf2out_source_line, /* source_line */
2941 debug_nothing_int_int_charstar, /* set_ignored_loc */
2942 debug_nothing_int_int_charstar, /* begin_prologue */
2943 debug_nothing_int_charstar, /* end_prologue */
2944 debug_nothing_int_charstar, /* begin_epilogue */
2945 debug_nothing_int_charstar, /* end_epilogue */
2946 debug_nothing_tree, /* begin_function */
2947 debug_nothing_int, /* end_function */
2948 debug_nothing_tree, /* register_main_translation_unit */
2949 debug_nothing_tree, /* function_decl */
2950 debug_nothing_tree, /* early_global_decl */
2951 debug_nothing_tree, /* late_global_decl */
2952 debug_nothing_tree_int, /* type_decl */
2953 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2954 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2955 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2956 debug_nothing_tree, /* deferred_inline_function */
2957 debug_nothing_tree, /* outlining_inline_function */
2958 debug_nothing_rtx_code_label, /* label */
2959 debug_nothing_int, /* handle_pch */
2960 debug_nothing_rtx_insn, /* var_location */
2961 debug_nothing_tree, /* inline_entry */
2962 debug_nothing_tree, /* size_function */
2963 debug_nothing_void, /* switch_text_section */
2964 debug_nothing_tree_tree, /* set_name */
2965 0, /* start_end_main_source_file */
2966 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2969 /* NOTE: In the comments in this file, many references are made to
2970 "Debugging Information Entries". This term is abbreviated as `DIE'
2971 throughout the remainder of this file. */
2973 /* An internal representation of the DWARF output is built, and then
2974 walked to generate the DWARF debugging info. The walk of the internal
2975 representation is done after the entire program has been compiled.
2976 The types below are used to describe the internal representation. */
2978 /* Whether to put type DIEs into their own section .debug_types instead
2979 of making them part of the .debug_info section. Only supported for
2980 Dwarf V4 or higher and the user didn't disable them through
2981 -fno-debug-types-section. It is more efficient to put them in a
2982 separate comdat sections since the linker will then be able to
2983 remove duplicates. But not all tools support .debug_types sections
2984 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2985 it is DW_UT_type unit type in .debug_info section. For late LTO
2986 debug there should be almost no types emitted so avoid enabling
2987 -fdebug-types-section there. */
2989 #define use_debug_types (dwarf_version >= 4 \
2990 && flag_debug_types_section \
2991 && !in_lto_p)
2993 /* Various DIE's use offsets relative to the beginning of the
2994 .debug_info section to refer to each other. */
2996 typedef long int dw_offset;
2998 struct comdat_type_node;
3000 /* The entries in the line_info table more-or-less mirror the opcodes
3001 that are used in the real dwarf line table. Arrays of these entries
3002 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
3003 supported. */
3005 enum dw_line_info_opcode {
3006 /* Emit DW_LNE_set_address; the operand is the label index. */
3007 LI_set_address,
3009 /* Emit a row to the matrix with the given line. This may be done
3010 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3011 special opcodes. */
3012 LI_set_line,
3014 /* Emit a DW_LNS_set_file. */
3015 LI_set_file,
3017 /* Emit a DW_LNS_set_column. */
3018 LI_set_column,
3020 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
3021 LI_negate_stmt,
3023 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
3024 LI_set_prologue_end,
3025 LI_set_epilogue_begin,
3027 /* Emit a DW_LNE_set_discriminator. */
3028 LI_set_discriminator,
3030 /* Output a Fixed Advance PC; the target PC is the label index; the
3031 base PC is the previous LI_adv_address or LI_set_address entry.
3032 We only use this when emitting debug views without assembler
3033 support, at explicit user request. Ideally, we should only use
3034 it when the offset might be zero but we can't tell: it's the only
3035 way to maybe change the PC without resetting the view number. */
3036 LI_adv_address
3039 typedef struct GTY(()) dw_line_info_struct {
3040 enum dw_line_info_opcode opcode;
3041 unsigned int val;
3042 } dw_line_info_entry;
3045 struct GTY(()) dw_line_info_table {
3046 /* The label that marks the end of this section. */
3047 const char *end_label;
3049 /* The values for the last row of the matrix, as collected in the table.
3050 These are used to minimize the changes to the next row. */
3051 unsigned int file_num;
3052 unsigned int line_num;
3053 unsigned int column_num;
3054 int discrim_num;
3055 bool is_stmt;
3056 bool in_use;
3058 /* This denotes the NEXT view number.
3060 If it is 0, it is known that the NEXT view will be the first view
3061 at the given PC.
3063 If it is -1, we're forcing the view number to be reset, e.g. at a
3064 function entry.
3066 The meaning of other nonzero values depends on whether we're
3067 computing views internally or leaving it for the assembler to do
3068 so. If we're emitting them internally, view denotes the view
3069 number since the last known advance of PC. If we're leaving it
3070 for the assembler, it denotes the LVU label number that we're
3071 going to ask the assembler to assign. */
3072 var_loc_view view;
3074 /* This counts the number of symbolic views emitted in this table
3075 since the latest view reset. Its max value, over all tables,
3076 sets symview_upper_bound. */
3077 var_loc_view symviews_since_reset;
3079 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3080 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3081 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3082 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3084 vec<dw_line_info_entry, va_gc> *entries;
3087 /* This is an upper bound for view numbers that the assembler may
3088 assign to symbolic views output in this translation. It is used to
3089 decide how big a field to use to represent view numbers in
3090 symview-classed attributes. */
3092 static var_loc_view symview_upper_bound;
3094 /* If we're keep track of location views and their reset points, and
3095 INSN is a reset point (i.e., it necessarily advances the PC), mark
3096 the next view in TABLE as reset. */
3098 static void
3099 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3101 if (!debug_internal_reset_location_views)
3102 return;
3104 /* Maybe turn (part of?) this test into a default target hook. */
3105 int reset = 0;
3107 if (targetm.reset_location_view)
3108 reset = targetm.reset_location_view (insn);
3110 if (reset)
3112 else if (JUMP_TABLE_DATA_P (insn))
3113 reset = 1;
3114 else if (GET_CODE (insn) == USE
3115 || GET_CODE (insn) == CLOBBER
3116 || GET_CODE (insn) == ASM_INPUT
3117 || asm_noperands (insn) >= 0)
3119 else if (get_attr_min_length (insn) > 0)
3120 reset = 1;
3122 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3123 RESET_NEXT_VIEW (table->view);
3126 /* Each DIE attribute has a field specifying the attribute kind,
3127 a link to the next attribute in the chain, and an attribute value.
3128 Attributes are typically linked below the DIE they modify. */
3130 typedef struct GTY(()) dw_attr_struct {
3131 enum dwarf_attribute dw_attr;
3132 dw_val_node dw_attr_val;
3134 dw_attr_node;
3137 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3138 The children of each node form a circular list linked by
3139 die_sib. die_child points to the node *before* the "first" child node. */
3141 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3142 union die_symbol_or_type_node
3144 const char * GTY ((tag ("0"))) die_symbol;
3145 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3147 GTY ((desc ("%0.comdat_type_p"))) die_id;
3148 vec<dw_attr_node, va_gc> *die_attr;
3149 dw_die_ref die_parent;
3150 dw_die_ref die_child;
3151 dw_die_ref die_sib;
3152 dw_die_ref die_definition; /* ref from a specification to its definition */
3153 dw_offset die_offset;
3154 unsigned long die_abbrev;
3155 int die_mark;
3156 unsigned int decl_id;
3157 enum dwarf_tag die_tag;
3158 /* Die is used and must not be pruned as unused. */
3159 BOOL_BITFIELD die_perennial_p : 1;
3160 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3161 /* For an external ref to die_symbol if die_offset contains an extra
3162 offset to that symbol. */
3163 BOOL_BITFIELD with_offset : 1;
3164 /* Whether this DIE was removed from the DIE tree, for example via
3165 prune_unused_types. We don't consider those present from the
3166 DIE lookup routines. */
3167 BOOL_BITFIELD removed : 1;
3168 /* Lots of spare bits. */
3170 die_node;
3172 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3173 static bool early_dwarf;
3174 static bool early_dwarf_finished;
3175 class set_early_dwarf {
3176 public:
3177 bool saved;
3178 set_early_dwarf () : saved(early_dwarf)
3180 gcc_assert (! early_dwarf_finished);
3181 early_dwarf = true;
3183 ~set_early_dwarf () { early_dwarf = saved; }
3186 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3187 #define FOR_EACH_CHILD(die, c, expr) do { \
3188 c = die->die_child; \
3189 if (c) do { \
3190 c = c->die_sib; \
3191 expr; \
3192 } while (c != die->die_child); \
3193 } while (0)
3195 /* The pubname structure */
3197 typedef struct GTY(()) pubname_struct {
3198 dw_die_ref die;
3199 const char *name;
3201 pubname_entry;
3204 struct GTY(()) dw_ranges {
3205 const char *label;
3206 /* If this is positive, it's a block number, otherwise it's a
3207 bitwise-negated index into dw_ranges_by_label. */
3208 int num;
3209 /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3210 into .debug_rnglists section rather than .debug_rnglists.dwo
3211 for -gsplit-dwarf and DWARF >= 5. */
3212 #define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3213 /* Index for the range list for DW_FORM_rnglistx. */
3214 unsigned int idx : 31;
3215 /* True if this range might be possibly in a different section
3216 from previous entry. */
3217 unsigned int maybe_new_sec : 1;
3218 addr_table_entry *begin_entry;
3219 addr_table_entry *end_entry;
3222 /* A structure to hold a macinfo entry. */
3224 typedef struct GTY(()) macinfo_struct {
3225 unsigned char code;
3226 unsigned HOST_WIDE_INT lineno;
3227 const char *info;
3229 macinfo_entry;
3232 struct GTY(()) dw_ranges_by_label {
3233 const char *begin;
3234 const char *end;
3237 /* The comdat type node structure. */
3238 struct GTY(()) comdat_type_node
3240 dw_die_ref root_die;
3241 dw_die_ref type_die;
3242 dw_die_ref skeleton_die;
3243 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3244 comdat_type_node *next;
3247 /* A list of DIEs for which we can't determine ancestry (parent_die
3248 field) just yet. Later in dwarf2out_finish we will fill in the
3249 missing bits. */
3250 typedef struct GTY(()) limbo_die_struct {
3251 dw_die_ref die;
3252 /* The tree for which this DIE was created. We use this to
3253 determine ancestry later. */
3254 tree created_for;
3255 struct limbo_die_struct *next;
3257 limbo_die_node;
3259 typedef struct skeleton_chain_struct
3261 dw_die_ref old_die;
3262 dw_die_ref new_die;
3263 struct skeleton_chain_struct *parent;
3265 skeleton_chain_node;
3267 /* Define a macro which returns nonzero for a TYPE_DECL which was
3268 implicitly generated for a type.
3270 Note that, unlike the C front-end (which generates a NULL named
3271 TYPE_DECL node for each complete tagged type, each array type,
3272 and each function type node created) the C++ front-end generates
3273 a _named_ TYPE_DECL node for each tagged type node created.
3274 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3275 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3276 front-end, but for each type, tagged or not. */
3278 #define TYPE_DECL_IS_STUB(decl) \
3279 (DECL_NAME (decl) == NULL_TREE \
3280 || (DECL_ARTIFICIAL (decl) \
3281 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3282 /* This is necessary for stub decls that \
3283 appear in nested inline functions. */ \
3284 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3285 && (decl_ultimate_origin (decl) \
3286 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3288 /* Information concerning the compilation unit's programming
3289 language, and compiler version. */
3291 /* Fixed size portion of the DWARF compilation unit header. */
3292 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3293 (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size \
3294 + (dwarf_version >= 5 ? 4 : 3))
3296 /* Fixed size portion of the DWARF comdat type unit header. */
3297 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3298 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3299 + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3301 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3302 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3303 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3305 /* Fixed size portion of public names info. */
3306 #define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3308 /* Fixed size portion of the address range info. */
3309 #define DWARF_ARANGES_HEADER_SIZE \
3310 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3311 DWARF2_ADDR_SIZE * 2) \
3312 - DWARF_INITIAL_LENGTH_SIZE)
3314 /* Size of padding portion in the address range info. It must be
3315 aligned to twice the pointer size. */
3316 #define DWARF_ARANGES_PAD_SIZE \
3317 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3318 DWARF2_ADDR_SIZE * 2) \
3319 - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3321 /* Use assembler line directives if available. */
3322 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3323 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3324 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3325 #else
3326 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3327 #endif
3328 #endif
3330 /* Use assembler views in line directives if available. */
3331 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3332 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3333 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3334 #else
3335 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3336 #endif
3337 #endif
3339 /* Return true if GCC configure detected assembler support for .loc. */
3341 bool
3342 dwarf2out_default_as_loc_support (void)
3344 return DWARF2_ASM_LINE_DEBUG_INFO;
3345 #if (GCC_VERSION >= 3000)
3346 # undef DWARF2_ASM_LINE_DEBUG_INFO
3347 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3348 #endif
3351 /* Return true if GCC configure detected assembler support for views
3352 in .loc directives. */
3354 bool
3355 dwarf2out_default_as_locview_support (void)
3357 return DWARF2_ASM_VIEW_DEBUG_INFO;
3358 #if (GCC_VERSION >= 3000)
3359 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3360 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3361 #endif
3364 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3365 view computation, and it refers to a view identifier for which we
3366 will not emit a label because it is known to map to a view number
3367 zero. We won't allocate the bitmap if we're not using assembler
3368 support for location views, but we have to make the variable
3369 visible for GGC and for code that will be optimized out for lack of
3370 support but that's still parsed and compiled. We could abstract it
3371 out with macros, but it's not worth it. */
3372 static GTY(()) bitmap zero_view_p;
3374 /* Evaluate to TRUE iff N is known to identify the first location view
3375 at its PC. When not using assembler location view computation,
3376 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3377 and views label numbers recorded in it are the ones known to be
3378 zero. */
3379 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3380 || (N) == (var_loc_view)-1 \
3381 || (zero_view_p \
3382 && bitmap_bit_p (zero_view_p, (N))))
3384 /* Return true iff we're to emit .loc directives for the assembler to
3385 generate line number sections.
3387 When we're not emitting views, all we need from the assembler is
3388 support for .loc directives.
3390 If we are emitting views, we can only use the assembler's .loc
3391 support if it also supports views.
3393 When the compiler is emitting the line number programs and
3394 computing view numbers itself, it resets view numbers at known PC
3395 changes and counts from that, and then it emits view numbers as
3396 literal constants in locviewlists. There are cases in which the
3397 compiler is not sure about PC changes, e.g. when extra alignment is
3398 requested for a label. In these cases, the compiler may not reset
3399 the view counter, and the potential PC advance in the line number
3400 program will use an opcode that does not reset the view counter
3401 even if the PC actually changes, so that compiler and debug info
3402 consumer can keep view numbers in sync.
3404 When the compiler defers view computation to the assembler, it
3405 emits symbolic view numbers in locviewlists, with the exception of
3406 views known to be zero (forced resets, or reset after
3407 compiler-visible PC changes): instead of emitting symbols for
3408 these, we emit literal zero and assert the assembler agrees with
3409 the compiler's assessment. We could use symbolic views everywhere,
3410 instead of special-casing zero views, but then we'd be unable to
3411 optimize out locviewlists that contain only zeros. */
3413 static bool
3414 output_asm_line_debug_info (void)
3416 return (dwarf2out_as_loc_support
3417 && (dwarf2out_as_locview_support
3418 || !debug_variable_location_views));
3421 static bool asm_outputs_debug_line_str (void);
3423 /* Minimum line offset in a special line info. opcode.
3424 This value was chosen to give a reasonable range of values. */
3425 #define DWARF_LINE_BASE -10
3427 /* First special line opcode - leave room for the standard opcodes. */
3428 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3430 /* Range of line offsets in a special line info. opcode. */
3431 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3433 /* Flag that indicates the initial value of the is_stmt_start flag.
3434 In the present implementation, we do not mark any lines as
3435 the beginning of a source statement, because that information
3436 is not made available by the GCC front-end. */
3437 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3439 /* Maximum number of operations per instruction bundle. */
3440 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3441 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3442 #endif
3444 /* This location is used by calc_die_sizes() to keep track
3445 the offset of each DIE within the .debug_info section. */
3446 static unsigned long next_die_offset;
3448 /* Record the root of the DIE's built for the current compilation unit. */
3449 static GTY(()) dw_die_ref single_comp_unit_die;
3451 /* A list of type DIEs that have been separated into comdat sections. */
3452 static GTY(()) comdat_type_node *comdat_type_list;
3454 /* A list of CU DIEs that have been separated. */
3455 static GTY(()) limbo_die_node *cu_die_list;
3457 /* A list of DIEs with a NULL parent waiting to be relocated. */
3458 static GTY(()) limbo_die_node *limbo_die_list;
3460 /* A list of DIEs for which we may have to generate
3461 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3462 static GTY(()) limbo_die_node *deferred_asm_name;
3464 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3466 typedef const char *compare_type;
3468 static hashval_t hash (dwarf_file_data *);
3469 static bool equal (dwarf_file_data *, const char *);
3472 /* Filenames referenced by this compilation unit. */
3473 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3475 struct decl_die_hasher : ggc_ptr_hash<die_node>
3477 typedef tree compare_type;
3479 static hashval_t hash (die_node *);
3480 static bool equal (die_node *, tree);
3482 /* A hash table of references to DIE's that describe declarations.
3483 The key is a DECL_UID() which is a unique number identifying each decl. */
3484 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3486 struct GTY ((for_user)) variable_value_struct {
3487 unsigned int decl_id;
3488 vec<dw_die_ref, va_gc> *dies;
3491 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3493 typedef tree compare_type;
3495 static hashval_t hash (variable_value_struct *);
3496 static bool equal (variable_value_struct *, tree);
3498 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3499 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3500 DECL_CONTEXT of the referenced VAR_DECLs. */
3501 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3503 struct block_die_hasher : ggc_ptr_hash<die_struct>
3505 static hashval_t hash (die_struct *);
3506 static bool equal (die_struct *, die_struct *);
3509 /* A hash table of references to DIE's that describe COMMON blocks.
3510 The key is DECL_UID() ^ die_parent. */
3511 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3513 typedef struct GTY(()) die_arg_entry_struct {
3514 dw_die_ref die;
3515 tree arg;
3516 } die_arg_entry;
3519 /* Node of the variable location list. */
3520 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3521 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3522 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3523 in mode of the EXPR_LIST node and first EXPR_LIST operand
3524 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3525 location or NULL for padding. For larger bitsizes,
3526 mode is 0 and first operand is a CONCAT with bitsize
3527 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3528 NULL as second operand. */
3529 rtx GTY (()) loc;
3530 const char * GTY (()) label;
3531 struct var_loc_node * GTY (()) next;
3532 var_loc_view view;
3535 /* Variable location list. */
3536 struct GTY ((for_user)) var_loc_list_def {
3537 struct var_loc_node * GTY (()) first;
3539 /* Pointer to the last but one or last element of the
3540 chained list. If the list is empty, both first and
3541 last are NULL, if the list contains just one node
3542 or the last node certainly is not redundant, it points
3543 to the last node, otherwise points to the last but one.
3544 Do not mark it for GC because it is marked through the chain. */
3545 struct var_loc_node * GTY ((skip ("%h"))) last;
3547 /* Pointer to the last element before section switch,
3548 if NULL, either sections weren't switched or first
3549 is after section switch. */
3550 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3552 /* DECL_UID of the variable decl. */
3553 unsigned int decl_id;
3555 typedef struct var_loc_list_def var_loc_list;
3557 /* Call argument location list. */
3558 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3559 rtx GTY (()) call_arg_loc_note;
3560 const char * GTY (()) label;
3561 tree GTY (()) block;
3562 bool tail_call_p;
3563 rtx GTY (()) symbol_ref;
3564 struct call_arg_loc_node * GTY (()) next;
3568 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3570 typedef const_tree compare_type;
3572 static hashval_t hash (var_loc_list *);
3573 static bool equal (var_loc_list *, const_tree);
3576 /* Table of decl location linked lists. */
3577 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3579 /* Head and tail of call_arg_loc chain. */
3580 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3581 static struct call_arg_loc_node *call_arg_loc_last;
3583 /* Number of call sites in the current function. */
3584 static int call_site_count = -1;
3585 /* Number of tail call sites in the current function. */
3586 static int tail_call_site_count = -1;
3588 /* A cached location list. */
3589 struct GTY ((for_user)) cached_dw_loc_list_def {
3590 /* The DECL_UID of the decl that this entry describes. */
3591 unsigned int decl_id;
3593 /* The cached location list. */
3594 dw_loc_list_ref loc_list;
3596 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3598 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3601 typedef const_tree compare_type;
3603 static hashval_t hash (cached_dw_loc_list *);
3604 static bool equal (cached_dw_loc_list *, const_tree);
3607 /* Table of cached location lists. */
3608 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3610 /* A vector of references to DIE's that are uniquely identified by their tag,
3611 presence/absence of children DIE's, and list of attribute/value pairs. */
3612 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3614 /* A hash map to remember the stack usage for DWARF procedures. The value
3615 stored is the stack size difference between before the DWARF procedure
3616 invokation and after it returned. In other words, for a DWARF procedure
3617 that consumes N stack slots and that pushes M ones, this stores M - N. */
3618 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3620 /* A global counter for generating labels for line number data. */
3621 static unsigned int line_info_label_num;
3623 /* The current table to which we should emit line number information
3624 for the current function. This will be set up at the beginning of
3625 assembly for the function. */
3626 static GTY(()) dw_line_info_table *cur_line_info_table;
3628 /* The two default tables of line number info. */
3629 static GTY(()) dw_line_info_table *text_section_line_info;
3630 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3632 /* The set of all non-default tables of line number info. */
3633 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3635 /* A flag to tell pubnames/types export if there is an info section to
3636 refer to. */
3637 static bool info_section_emitted;
3639 /* A pointer to the base of a table that contains a list of publicly
3640 accessible names. */
3641 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3643 /* A pointer to the base of a table that contains a list of publicly
3644 accessible types. */
3645 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3647 /* A pointer to the base of a table that contains a list of macro
3648 defines/undefines (and file start/end markers). */
3649 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3651 /* True if .debug_macinfo or .debug_macros section is going to be
3652 emitted. */
3653 #define have_macinfo \
3654 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3655 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3656 && !macinfo_table->is_empty ())
3658 /* Vector of dies for which we should generate .debug_ranges info. */
3659 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3661 /* Vector of pairs of labels referenced in ranges_table. */
3662 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3664 /* Whether we have location lists that need outputting */
3665 static GTY(()) bool have_location_lists;
3667 /* Unique label counter. */
3668 static GTY(()) unsigned int loclabel_num;
3670 /* Unique label counter for point-of-call tables. */
3671 static GTY(()) unsigned int poc_label_num;
3673 /* The last file entry emitted by maybe_emit_file(). */
3674 static GTY(()) struct dwarf_file_data * last_emitted_file;
3676 /* Number of internal labels generated by gen_internal_sym(). */
3677 static GTY(()) int label_num;
3679 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3681 /* Instances of generic types for which we need to generate debug
3682 info that describe their generic parameters and arguments. That
3683 generation needs to happen once all types are properly laid out so
3684 we do it at the end of compilation. */
3685 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3687 /* Offset from the "steady-state frame pointer" to the frame base,
3688 within the current function. */
3689 static poly_int64 frame_pointer_fb_offset;
3690 static bool frame_pointer_fb_offset_valid;
3692 static vec<dw_die_ref> base_types;
3694 /* Flags to represent a set of attribute classes for attributes that represent
3695 a scalar value (bounds, pointers, ...). */
3696 enum dw_scalar_form
3698 dw_scalar_form_constant = 0x01,
3699 dw_scalar_form_exprloc = 0x02,
3700 dw_scalar_form_reference = 0x04
3703 /* Forward declarations for functions defined in this file. */
3705 static int is_pseudo_reg (const_rtx);
3706 static tree type_main_variant (tree);
3707 static int is_tagged_type (const_tree);
3708 static const char *dwarf_tag_name (unsigned);
3709 static const char *dwarf_attr_name (unsigned);
3710 static const char *dwarf_form_name (unsigned);
3711 static tree decl_ultimate_origin (const_tree);
3712 static tree decl_class_context (tree);
3713 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3714 static inline enum dw_val_class AT_class (dw_attr_node *);
3715 static inline unsigned int AT_index (dw_attr_node *);
3716 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3717 static inline unsigned AT_flag (dw_attr_node *);
3718 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3719 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3720 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3721 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3722 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3723 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3724 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3725 unsigned int, unsigned char *);
3726 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3727 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3728 static inline const char *AT_string (dw_attr_node *);
3729 static enum dwarf_form AT_string_form (dw_attr_node *);
3730 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3731 static void add_AT_specification (dw_die_ref, dw_die_ref);
3732 static inline dw_die_ref AT_ref (dw_attr_node *);
3733 static inline int AT_ref_external (dw_attr_node *);
3734 static inline void set_AT_ref_external (dw_attr_node *, int);
3735 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3736 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3737 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3738 dw_loc_list_ref);
3739 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3740 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3741 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3742 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3743 static void remove_addr_table_entry (addr_table_entry *);
3744 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3745 static inline rtx AT_addr (dw_attr_node *);
3746 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3747 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3748 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3749 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3750 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3751 unsigned long, bool);
3752 static inline const char *AT_lbl (dw_attr_node *);
3753 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3754 static const char *get_AT_low_pc (dw_die_ref);
3755 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3756 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3757 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3758 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3759 static bool is_c (void);
3760 static bool is_cxx (void);
3761 static bool is_cxx (const_tree);
3762 static bool is_fortran (void);
3763 static bool is_ada (void);
3764 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3765 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3766 static void add_child_die (dw_die_ref, dw_die_ref);
3767 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3768 static dw_die_ref lookup_type_die (tree);
3769 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3770 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3771 static void equate_type_number_to_die (tree, dw_die_ref);
3772 static dw_die_ref lookup_decl_die (tree);
3773 static var_loc_list *lookup_decl_loc (const_tree);
3774 static void equate_decl_number_to_die (tree, dw_die_ref);
3775 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3776 static void print_spaces (FILE *);
3777 static void print_die (dw_die_ref, FILE *);
3778 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3779 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3780 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3781 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3782 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3783 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3784 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3785 struct md5_ctx *, int *);
3786 struct checksum_attributes;
3787 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3788 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3789 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3790 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3791 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3792 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3793 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3794 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3795 static int is_type_die (dw_die_ref);
3796 static inline bool is_template_instantiation (dw_die_ref);
3797 static int is_declaration_die (dw_die_ref);
3798 static int should_move_die_to_comdat (dw_die_ref);
3799 static dw_die_ref clone_as_declaration (dw_die_ref);
3800 static dw_die_ref clone_die (dw_die_ref);
3801 static dw_die_ref clone_tree (dw_die_ref);
3802 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3803 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3804 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3805 static dw_die_ref generate_skeleton (dw_die_ref);
3806 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3807 dw_die_ref,
3808 dw_die_ref);
3809 static void break_out_comdat_types (dw_die_ref);
3810 static void copy_decls_for_unworthy_types (dw_die_ref);
3812 static void add_sibling_attributes (dw_die_ref);
3813 static void output_location_lists (dw_die_ref);
3814 static int constant_size (unsigned HOST_WIDE_INT);
3815 static unsigned long size_of_die (dw_die_ref);
3816 static void calc_die_sizes (dw_die_ref);
3817 static void calc_base_type_die_sizes (void);
3818 static void mark_dies (dw_die_ref);
3819 static void unmark_dies (dw_die_ref);
3820 static void unmark_all_dies (dw_die_ref);
3821 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3822 static unsigned long size_of_aranges (void);
3823 static enum dwarf_form value_format (dw_attr_node *);
3824 static void output_value_format (dw_attr_node *);
3825 static void output_abbrev_section (void);
3826 static void output_die_abbrevs (unsigned long, dw_die_ref);
3827 static void output_die (dw_die_ref);
3828 static void output_compilation_unit_header (enum dwarf_unit_type);
3829 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3830 static void output_comdat_type_unit (comdat_type_node *, bool);
3831 static const char *dwarf2_name (tree, int);
3832 static void add_pubname (tree, dw_die_ref);
3833 static void add_enumerator_pubname (const char *, dw_die_ref);
3834 static void add_pubname_string (const char *, dw_die_ref);
3835 static void add_pubtype (tree, dw_die_ref);
3836 static void output_pubnames (vec<pubname_entry, va_gc> *);
3837 static void output_aranges (void);
3838 static unsigned int add_ranges (const_tree, bool = false);
3839 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3840 bool *, bool);
3841 static void output_ranges (void);
3842 static dw_line_info_table *new_line_info_table (void);
3843 static void output_line_info (bool);
3844 static void output_file_names (void);
3845 static dw_die_ref base_type_die (tree, bool);
3846 static int is_base_type (tree);
3847 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3848 static int decl_quals (const_tree);
3849 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3850 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3851 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3852 static unsigned int dbx_reg_number (const_rtx);
3853 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3854 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3855 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3856 enum var_init_status);
3857 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3858 enum var_init_status);
3859 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3860 enum var_init_status);
3861 static int is_based_loc (const_rtx);
3862 static bool resolve_one_addr (rtx *);
3863 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3864 enum var_init_status);
3865 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3866 enum var_init_status);
3867 struct loc_descr_context;
3868 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3869 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3870 static dw_loc_list_ref loc_list_from_tree (tree, int,
3871 struct loc_descr_context *);
3872 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3873 struct loc_descr_context *);
3874 static tree field_type (const_tree);
3875 static unsigned int simple_type_align_in_bits (const_tree);
3876 static unsigned int simple_decl_align_in_bits (const_tree);
3877 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3878 struct vlr_context;
3879 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3880 HOST_WIDE_INT *);
3881 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3882 dw_loc_list_ref);
3883 static void add_data_member_location_attribute (dw_die_ref, tree,
3884 struct vlr_context *);
3885 static bool add_const_value_attribute (dw_die_ref, rtx);
3886 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3887 static void insert_wide_int (const wide_int &, unsigned char *, int);
3888 static unsigned insert_float (const_rtx, unsigned char *);
3889 static rtx rtl_for_decl_location (tree);
3890 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3891 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3892 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3893 static void add_name_attribute (dw_die_ref, const char *);
3894 static void add_desc_attribute (dw_die_ref, tree);
3895 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3896 static void add_comp_dir_attribute (dw_die_ref);
3897 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3898 struct loc_descr_context *);
3899 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3900 struct loc_descr_context *);
3901 static void add_subscript_info (dw_die_ref, tree, bool);
3902 static void add_byte_size_attribute (dw_die_ref, tree);
3903 static void add_alignment_attribute (dw_die_ref, tree);
3904 static void add_bit_offset_attribute (dw_die_ref, tree);
3905 static void add_bit_size_attribute (dw_die_ref, tree);
3906 static void add_prototyped_attribute (dw_die_ref, tree);
3907 static void add_abstract_origin_attribute (dw_die_ref, tree);
3908 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3909 static void add_src_coords_attributes (dw_die_ref, tree);
3910 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3911 static void add_discr_value (dw_die_ref, dw_discr_value *);
3912 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3913 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3914 static dw_die_ref scope_die_for (tree, dw_die_ref);
3915 static inline int local_scope_p (dw_die_ref);
3916 static inline int class_scope_p (dw_die_ref);
3917 static inline int class_or_namespace_scope_p (dw_die_ref);
3918 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3919 static void add_calling_convention_attribute (dw_die_ref, tree);
3920 static const char *type_tag (const_tree);
3921 static tree member_declared_type (const_tree);
3922 #if 0
3923 static const char *decl_start_label (tree);
3924 #endif
3925 static void gen_array_type_die (tree, dw_die_ref);
3926 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3927 #if 0
3928 static void gen_entry_point_die (tree, dw_die_ref);
3929 #endif
3930 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3931 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3932 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3933 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3934 static void gen_formal_types_die (tree, dw_die_ref);
3935 static void gen_subprogram_die (tree, dw_die_ref);
3936 static void gen_variable_die (tree, tree, dw_die_ref);
3937 static void gen_const_die (tree, dw_die_ref);
3938 static void gen_label_die (tree, dw_die_ref);
3939 static void gen_lexical_block_die (tree, dw_die_ref);
3940 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3941 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3942 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3943 static dw_die_ref gen_compile_unit_die (const char *);
3944 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3945 static void gen_member_die (tree, dw_die_ref);
3946 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3947 enum debug_info_usage);
3948 static void gen_subroutine_type_die (tree, dw_die_ref);
3949 static void gen_typedef_die (tree, dw_die_ref);
3950 static void gen_type_die (tree, dw_die_ref);
3951 static void gen_block_die (tree, dw_die_ref);
3952 static void decls_for_scope (tree, dw_die_ref, bool = true);
3953 static bool is_naming_typedef_decl (const_tree);
3954 static inline dw_die_ref get_context_die (tree);
3955 static void gen_namespace_die (tree, dw_die_ref);
3956 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3957 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3958 static dw_die_ref force_decl_die (tree);
3959 static dw_die_ref force_type_die (tree);
3960 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3961 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3962 static struct dwarf_file_data * lookup_filename (const char *);
3963 static void retry_incomplete_types (void);
3964 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3965 static void gen_generic_params_dies (tree);
3966 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3967 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3968 static void splice_child_die (dw_die_ref, dw_die_ref);
3969 static int file_info_cmp (const void *, const void *);
3970 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3971 const char *, var_loc_view, const char *);
3972 static void output_loc_list (dw_loc_list_ref);
3973 static char *gen_internal_sym (const char *);
3974 static bool want_pubnames (void);
3976 static void prune_unmark_dies (dw_die_ref);
3977 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3978 static void prune_unused_types_mark (dw_die_ref, int);
3979 static void prune_unused_types_walk (dw_die_ref);
3980 static void prune_unused_types_walk_attribs (dw_die_ref);
3981 static void prune_unused_types_prune (dw_die_ref);
3982 static void prune_unused_types (void);
3983 static int maybe_emit_file (struct dwarf_file_data *fd);
3984 static inline const char *AT_vms_delta1 (dw_attr_node *);
3985 static inline const char *AT_vms_delta2 (dw_attr_node *);
3986 #if VMS_DEBUGGING_INFO
3987 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3988 const char *, const char *);
3989 #endif
3990 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3991 static void gen_remaining_tmpl_value_param_die_attribute (void);
3992 static bool generic_type_p (tree);
3993 static void schedule_generic_params_dies_gen (tree t);
3994 static void gen_scheduled_generic_parms_dies (void);
3995 static void resolve_variable_values (void);
3997 static const char *comp_dir_string (void);
3999 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
4001 /* enum for tracking thread-local variables whose address is really an offset
4002 relative to the TLS pointer, which will need link-time relocation, but will
4003 not need relocation by the DWARF consumer. */
4005 enum dtprel_bool
4007 dtprel_false = 0,
4008 dtprel_true = 1
4011 /* Return the operator to use for an address of a variable. For dtprel_true, we
4012 use DW_OP_const*. For regular variables, which need both link-time
4013 relocation and consumer-level relocation (e.g., to account for shared objects
4014 loaded at a random address), we use DW_OP_addr*. */
4016 static inline enum dwarf_location_atom
4017 dw_addr_op (enum dtprel_bool dtprel)
4019 if (dtprel == dtprel_true)
4020 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
4021 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
4022 else
4023 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
4026 /* Return a pointer to a newly allocated address location description. If
4027 dwarf_split_debug_info is true, then record the address with the appropriate
4028 relocation. */
4029 static inline dw_loc_descr_ref
4030 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
4032 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
4034 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
4035 ref->dw_loc_oprnd1.v.val_addr = addr;
4036 ref->dtprel = dtprel;
4037 if (dwarf_split_debug_info)
4038 ref->dw_loc_oprnd1.val_entry
4039 = add_addr_table_entry (addr,
4040 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
4041 else
4042 ref->dw_loc_oprnd1.val_entry = NULL;
4044 return ref;
4047 /* Section names used to hold DWARF debugging information. */
4049 #ifndef DEBUG_INFO_SECTION
4050 #define DEBUG_INFO_SECTION ".debug_info"
4051 #endif
4052 #ifndef DEBUG_DWO_INFO_SECTION
4053 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
4054 #endif
4055 #ifndef DEBUG_LTO_INFO_SECTION
4056 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
4057 #endif
4058 #ifndef DEBUG_LTO_DWO_INFO_SECTION
4059 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
4060 #endif
4061 #ifndef DEBUG_ABBREV_SECTION
4062 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4063 #endif
4064 #ifndef DEBUG_LTO_ABBREV_SECTION
4065 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
4066 #endif
4067 #ifndef DEBUG_DWO_ABBREV_SECTION
4068 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
4069 #endif
4070 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
4071 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
4072 #endif
4073 #ifndef DEBUG_ARANGES_SECTION
4074 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4075 #endif
4076 #ifndef DEBUG_ADDR_SECTION
4077 #define DEBUG_ADDR_SECTION ".debug_addr"
4078 #endif
4079 #ifndef DEBUG_MACINFO_SECTION
4080 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4081 #endif
4082 #ifndef DEBUG_LTO_MACINFO_SECTION
4083 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4084 #endif
4085 #ifndef DEBUG_DWO_MACINFO_SECTION
4086 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4087 #endif
4088 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4089 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4090 #endif
4091 #ifndef DEBUG_MACRO_SECTION
4092 #define DEBUG_MACRO_SECTION ".debug_macro"
4093 #endif
4094 #ifndef DEBUG_LTO_MACRO_SECTION
4095 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4096 #endif
4097 #ifndef DEBUG_DWO_MACRO_SECTION
4098 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4099 #endif
4100 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4101 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4102 #endif
4103 #ifndef DEBUG_LINE_SECTION
4104 #define DEBUG_LINE_SECTION ".debug_line"
4105 #endif
4106 #ifndef DEBUG_LTO_LINE_SECTION
4107 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4108 #endif
4109 #ifndef DEBUG_DWO_LINE_SECTION
4110 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4111 #endif
4112 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4113 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4114 #endif
4115 #ifndef DEBUG_LOC_SECTION
4116 #define DEBUG_LOC_SECTION ".debug_loc"
4117 #endif
4118 #ifndef DEBUG_DWO_LOC_SECTION
4119 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4120 #endif
4121 #ifndef DEBUG_LOCLISTS_SECTION
4122 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4123 #endif
4124 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4125 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4126 #endif
4127 #ifndef DEBUG_PUBNAMES_SECTION
4128 #define DEBUG_PUBNAMES_SECTION \
4129 ((debug_generate_pub_sections == 2) \
4130 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4131 #endif
4132 #ifndef DEBUG_PUBTYPES_SECTION
4133 #define DEBUG_PUBTYPES_SECTION \
4134 ((debug_generate_pub_sections == 2) \
4135 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4136 #endif
4137 #ifndef DEBUG_STR_OFFSETS_SECTION
4138 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4139 #endif
4140 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4141 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4142 #endif
4143 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4144 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4145 #endif
4146 #ifndef DEBUG_STR_SECTION
4147 #define DEBUG_STR_SECTION ".debug_str"
4148 #endif
4149 #ifndef DEBUG_LTO_STR_SECTION
4150 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4151 #endif
4152 #ifndef DEBUG_STR_DWO_SECTION
4153 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4154 #endif
4155 #ifndef DEBUG_LTO_STR_DWO_SECTION
4156 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4157 #endif
4158 #ifndef DEBUG_RANGES_SECTION
4159 #define DEBUG_RANGES_SECTION ".debug_ranges"
4160 #endif
4161 #ifndef DEBUG_RNGLISTS_SECTION
4162 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4163 #endif
4164 #ifndef DEBUG_DWO_RNGLISTS_SECTION
4165 #define DEBUG_DWO_RNGLISTS_SECTION ".debug_rnglists.dwo"
4166 #endif
4167 #ifndef DEBUG_LINE_STR_SECTION
4168 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4169 #endif
4170 #ifndef DEBUG_LTO_LINE_STR_SECTION
4171 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4172 #endif
4174 /* Standard ELF section names for compiled code and data. */
4175 #ifndef TEXT_SECTION_NAME
4176 #define TEXT_SECTION_NAME ".text"
4177 #endif
4179 /* Section flags for .debug_str section. */
4180 #define DEBUG_STR_SECTION_FLAGS \
4181 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4182 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4183 : SECTION_DEBUG)
4185 /* Section flags for .debug_str.dwo section. */
4186 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4188 /* Attribute used to refer to the macro section. */
4189 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4190 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4192 /* Labels we insert at beginning sections we can reference instead of
4193 the section names themselves. */
4195 #ifndef TEXT_SECTION_LABEL
4196 #define TEXT_SECTION_LABEL "Ltext"
4197 #endif
4198 #ifndef COLD_TEXT_SECTION_LABEL
4199 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4200 #endif
4201 #ifndef DEBUG_LINE_SECTION_LABEL
4202 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4203 #endif
4204 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4205 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4206 #endif
4207 #ifndef DEBUG_INFO_SECTION_LABEL
4208 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4209 #endif
4210 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4211 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4212 #endif
4213 #ifndef DEBUG_ABBREV_SECTION_LABEL
4214 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4215 #endif
4216 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4217 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4218 #endif
4219 #ifndef DEBUG_ADDR_SECTION_LABEL
4220 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4221 #endif
4222 #ifndef DEBUG_LOC_SECTION_LABEL
4223 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4224 #endif
4225 #ifndef DEBUG_RANGES_SECTION_LABEL
4226 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4227 #endif
4228 #ifndef DEBUG_MACINFO_SECTION_LABEL
4229 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4230 #endif
4231 #ifndef DEBUG_MACRO_SECTION_LABEL
4232 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4233 #endif
4234 #define SKELETON_COMP_DIE_ABBREV 1
4235 #define SKELETON_TYPE_DIE_ABBREV 2
4237 /* Definitions of defaults for formats and names of various special
4238 (artificial) labels which may be generated within this file (when the -g
4239 options is used and DWARF2_DEBUGGING_INFO is in effect.
4240 If necessary, these may be overridden from within the tm.h file, but
4241 typically, overriding these defaults is unnecessary. */
4243 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4244 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4245 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4246 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4247 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4248 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4249 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4250 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4251 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4252 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4253 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4254 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4255 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4256 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4257 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4259 #ifndef TEXT_END_LABEL
4260 #define TEXT_END_LABEL "Letext"
4261 #endif
4262 #ifndef COLD_END_LABEL
4263 #define COLD_END_LABEL "Letext_cold"
4264 #endif
4265 #ifndef BLOCK_BEGIN_LABEL
4266 #define BLOCK_BEGIN_LABEL "LBB"
4267 #endif
4268 #ifndef BLOCK_INLINE_ENTRY_LABEL
4269 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4270 #endif
4271 #ifndef BLOCK_END_LABEL
4272 #define BLOCK_END_LABEL "LBE"
4273 #endif
4274 #ifndef LINE_CODE_LABEL
4275 #define LINE_CODE_LABEL "LM"
4276 #endif
4279 /* Return the root of the DIE's built for the current compilation unit. */
4280 static dw_die_ref
4281 comp_unit_die (void)
4283 if (!single_comp_unit_die)
4284 single_comp_unit_die = gen_compile_unit_die (NULL);
4285 return single_comp_unit_die;
4288 /* We allow a language front-end to designate a function that is to be
4289 called to "demangle" any name before it is put into a DIE. */
4291 static const char *(*demangle_name_func) (const char *);
4293 void
4294 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4296 demangle_name_func = func;
4299 /* Test if rtl node points to a pseudo register. */
4301 static inline int
4302 is_pseudo_reg (const_rtx rtl)
4304 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4305 || (GET_CODE (rtl) == SUBREG
4306 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4309 /* Return a reference to a type, with its const and volatile qualifiers
4310 removed. */
4312 static inline tree
4313 type_main_variant (tree type)
4315 type = TYPE_MAIN_VARIANT (type);
4317 /* ??? There really should be only one main variant among any group of
4318 variants of a given type (and all of the MAIN_VARIANT values for all
4319 members of the group should point to that one type) but sometimes the C
4320 front-end messes this up for array types, so we work around that bug
4321 here. */
4322 if (TREE_CODE (type) == ARRAY_TYPE)
4323 while (type != TYPE_MAIN_VARIANT (type))
4324 type = TYPE_MAIN_VARIANT (type);
4326 return type;
4329 /* Return nonzero if the given type node represents a tagged type. */
4331 static inline int
4332 is_tagged_type (const_tree type)
4334 enum tree_code code = TREE_CODE (type);
4336 return (code == RECORD_TYPE || code == UNION_TYPE
4337 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4340 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4342 static void
4343 get_ref_die_offset_label (char *label, dw_die_ref ref)
4345 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4348 /* Return die_offset of a DIE reference to a base type. */
4350 static unsigned long int
4351 get_base_type_offset (dw_die_ref ref)
4353 if (ref->die_offset)
4354 return ref->die_offset;
4355 if (comp_unit_die ()->die_abbrev)
4357 calc_base_type_die_sizes ();
4358 gcc_assert (ref->die_offset);
4360 return ref->die_offset;
4363 /* Return die_offset of a DIE reference other than base type. */
4365 static unsigned long int
4366 get_ref_die_offset (dw_die_ref ref)
4368 gcc_assert (ref->die_offset);
4369 return ref->die_offset;
4372 /* Convert a DIE tag into its string name. */
4374 static const char *
4375 dwarf_tag_name (unsigned int tag)
4377 const char *name = get_DW_TAG_name (tag);
4379 if (name != NULL)
4380 return name;
4382 return "DW_TAG_<unknown>";
4385 /* Convert a DWARF attribute code into its string name. */
4387 static const char *
4388 dwarf_attr_name (unsigned int attr)
4390 const char *name;
4392 switch (attr)
4394 #if VMS_DEBUGGING_INFO
4395 case DW_AT_HP_prologue:
4396 return "DW_AT_HP_prologue";
4397 #else
4398 case DW_AT_MIPS_loop_unroll_factor:
4399 return "DW_AT_MIPS_loop_unroll_factor";
4400 #endif
4402 #if VMS_DEBUGGING_INFO
4403 case DW_AT_HP_epilogue:
4404 return "DW_AT_HP_epilogue";
4405 #else
4406 case DW_AT_MIPS_stride:
4407 return "DW_AT_MIPS_stride";
4408 #endif
4411 name = get_DW_AT_name (attr);
4413 if (name != NULL)
4414 return name;
4416 return "DW_AT_<unknown>";
4419 /* Convert a DWARF value form code into its string name. */
4421 static const char *
4422 dwarf_form_name (unsigned int form)
4424 const char *name = get_DW_FORM_name (form);
4426 if (name != NULL)
4427 return name;
4429 return "DW_FORM_<unknown>";
4432 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4433 instance of an inlined instance of a decl which is local to an inline
4434 function, so we have to trace all of the way back through the origin chain
4435 to find out what sort of node actually served as the original seed for the
4436 given block. */
4438 static tree
4439 decl_ultimate_origin (const_tree decl)
4441 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4442 return NULL_TREE;
4444 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4445 we're trying to output the abstract instance of this function. */
4446 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4447 return NULL_TREE;
4449 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4450 most distant ancestor, this should never happen. */
4451 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4453 return DECL_ABSTRACT_ORIGIN (decl);
4456 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4457 of a virtual function may refer to a base class, so we check the 'this'
4458 parameter. */
4460 static tree
4461 decl_class_context (tree decl)
4463 tree context = NULL_TREE;
4465 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4466 context = DECL_CONTEXT (decl);
4467 else
4468 context = TYPE_MAIN_VARIANT
4469 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4471 if (context && !TYPE_P (context))
4472 context = NULL_TREE;
4474 return context;
4477 /* Add an attribute/value pair to a DIE. */
4479 static inline void
4480 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4482 /* Maybe this should be an assert? */
4483 if (die == NULL)
4484 return;
4486 if (flag_checking)
4488 /* Check we do not add duplicate attrs. Can't use get_AT here
4489 because that recurses to the specification/abstract origin DIE. */
4490 dw_attr_node *a;
4491 unsigned ix;
4492 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4493 gcc_assert (a->dw_attr != attr->dw_attr);
4496 vec_safe_reserve (die->die_attr, 1);
4497 vec_safe_push (die->die_attr, *attr);
4500 static inline enum dw_val_class
4501 AT_class (dw_attr_node *a)
4503 return a->dw_attr_val.val_class;
4506 /* Return the index for any attribute that will be referenced with a
4507 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4508 indices are stored in dw_attr_val.v.val_str for reference counting
4509 pruning. */
4511 static inline unsigned int
4512 AT_index (dw_attr_node *a)
4514 if (AT_class (a) == dw_val_class_str)
4515 return a->dw_attr_val.v.val_str->index;
4516 else if (a->dw_attr_val.val_entry != NULL)
4517 return a->dw_attr_val.val_entry->index;
4518 return NOT_INDEXED;
4521 /* Add a flag value attribute to a DIE. */
4523 static inline void
4524 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4526 dw_attr_node attr;
4528 attr.dw_attr = attr_kind;
4529 attr.dw_attr_val.val_class = dw_val_class_flag;
4530 attr.dw_attr_val.val_entry = NULL;
4531 attr.dw_attr_val.v.val_flag = flag;
4532 add_dwarf_attr (die, &attr);
4535 static inline unsigned
4536 AT_flag (dw_attr_node *a)
4538 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4539 return a->dw_attr_val.v.val_flag;
4542 /* Add a signed integer attribute value to a DIE. */
4544 static inline void
4545 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4547 dw_attr_node attr;
4549 attr.dw_attr = attr_kind;
4550 attr.dw_attr_val.val_class = dw_val_class_const;
4551 attr.dw_attr_val.val_entry = NULL;
4552 attr.dw_attr_val.v.val_int = int_val;
4553 add_dwarf_attr (die, &attr);
4556 static inline HOST_WIDE_INT
4557 AT_int (dw_attr_node *a)
4559 gcc_assert (a && (AT_class (a) == dw_val_class_const
4560 || AT_class (a) == dw_val_class_const_implicit));
4561 return a->dw_attr_val.v.val_int;
4564 /* Add an unsigned integer attribute value to a DIE. */
4566 static inline void
4567 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4568 unsigned HOST_WIDE_INT unsigned_val)
4570 dw_attr_node attr;
4572 attr.dw_attr = attr_kind;
4573 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4574 attr.dw_attr_val.val_entry = NULL;
4575 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4576 add_dwarf_attr (die, &attr);
4579 static inline unsigned HOST_WIDE_INT
4580 AT_unsigned (dw_attr_node *a)
4582 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4583 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4584 return a->dw_attr_val.v.val_unsigned;
4587 /* Add an unsigned wide integer attribute value to a DIE. */
4589 static inline void
4590 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4591 const wide_int& w)
4593 dw_attr_node attr;
4595 attr.dw_attr = attr_kind;
4596 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4597 attr.dw_attr_val.val_entry = NULL;
4598 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4599 *attr.dw_attr_val.v.val_wide = w;
4600 add_dwarf_attr (die, &attr);
4603 /* Add an unsigned double integer attribute value to a DIE. */
4605 static inline void
4606 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4607 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4609 dw_attr_node attr;
4611 attr.dw_attr = attr_kind;
4612 attr.dw_attr_val.val_class = dw_val_class_const_double;
4613 attr.dw_attr_val.val_entry = NULL;
4614 attr.dw_attr_val.v.val_double.high = high;
4615 attr.dw_attr_val.v.val_double.low = low;
4616 add_dwarf_attr (die, &attr);
4619 /* Add a floating point attribute value to a DIE and return it. */
4621 static inline void
4622 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4623 unsigned int length, unsigned int elt_size, unsigned char *array)
4625 dw_attr_node attr;
4627 attr.dw_attr = attr_kind;
4628 attr.dw_attr_val.val_class = dw_val_class_vec;
4629 attr.dw_attr_val.val_entry = NULL;
4630 attr.dw_attr_val.v.val_vec.length = length;
4631 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4632 attr.dw_attr_val.v.val_vec.array = array;
4633 add_dwarf_attr (die, &attr);
4636 /* Add an 8-byte data attribute value to a DIE. */
4638 static inline void
4639 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4640 unsigned char data8[8])
4642 dw_attr_node attr;
4644 attr.dw_attr = attr_kind;
4645 attr.dw_attr_val.val_class = dw_val_class_data8;
4646 attr.dw_attr_val.val_entry = NULL;
4647 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4648 add_dwarf_attr (die, &attr);
4651 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4652 dwarf_split_debug_info, address attributes in dies destined for the
4653 final executable have force_direct set to avoid using indexed
4654 references. */
4656 static inline void
4657 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4658 bool force_direct)
4660 dw_attr_node attr;
4661 char * lbl_id;
4663 lbl_id = xstrdup (lbl_low);
4664 attr.dw_attr = DW_AT_low_pc;
4665 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4666 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4667 if (dwarf_split_debug_info && !force_direct)
4668 attr.dw_attr_val.val_entry
4669 = add_addr_table_entry (lbl_id, ate_kind_label);
4670 else
4671 attr.dw_attr_val.val_entry = NULL;
4672 add_dwarf_attr (die, &attr);
4674 attr.dw_attr = DW_AT_high_pc;
4675 if (dwarf_version < 4)
4676 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4677 else
4678 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4679 lbl_id = xstrdup (lbl_high);
4680 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4681 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4682 && dwarf_split_debug_info && !force_direct)
4683 attr.dw_attr_val.val_entry
4684 = add_addr_table_entry (lbl_id, ate_kind_label);
4685 else
4686 attr.dw_attr_val.val_entry = NULL;
4687 add_dwarf_attr (die, &attr);
4690 /* Hash and equality functions for debug_str_hash. */
4692 hashval_t
4693 indirect_string_hasher::hash (indirect_string_node *x)
4695 return htab_hash_string (x->str);
4698 bool
4699 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4701 return strcmp (x1->str, x2) == 0;
4704 /* Add STR to the given string hash table. */
4706 static struct indirect_string_node *
4707 find_AT_string_in_table (const char *str,
4708 hash_table<indirect_string_hasher> *table,
4709 enum insert_option insert = INSERT)
4711 struct indirect_string_node *node;
4713 indirect_string_node **slot
4714 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4715 if (*slot == NULL)
4717 node = ggc_cleared_alloc<indirect_string_node> ();
4718 node->str = ggc_strdup (str);
4719 *slot = node;
4721 else
4722 node = *slot;
4724 node->refcount++;
4725 return node;
4728 /* Add STR to the indirect string hash table. */
4730 static struct indirect_string_node *
4731 find_AT_string (const char *str, enum insert_option insert = INSERT)
4733 if (! debug_str_hash)
4734 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4736 return find_AT_string_in_table (str, debug_str_hash, insert);
4739 /* Add a string attribute value to a DIE. */
4741 static inline void
4742 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4744 dw_attr_node attr;
4745 struct indirect_string_node *node;
4747 node = find_AT_string (str);
4749 attr.dw_attr = attr_kind;
4750 attr.dw_attr_val.val_class = dw_val_class_str;
4751 attr.dw_attr_val.val_entry = NULL;
4752 attr.dw_attr_val.v.val_str = node;
4753 add_dwarf_attr (die, &attr);
4756 static inline const char *
4757 AT_string (dw_attr_node *a)
4759 gcc_assert (a && AT_class (a) == dw_val_class_str);
4760 return a->dw_attr_val.v.val_str->str;
4763 /* Call this function directly to bypass AT_string_form's logic to put
4764 the string inline in the die. */
4766 static void
4767 set_indirect_string (struct indirect_string_node *node)
4769 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4770 /* Already indirect is a no op. */
4771 if (node->form == DW_FORM_strp
4772 || node->form == DW_FORM_line_strp
4773 || node->form == dwarf_FORM (DW_FORM_strx))
4775 gcc_assert (node->label);
4776 return;
4778 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4779 ++dw2_string_counter;
4780 node->label = xstrdup (label);
4782 if (!dwarf_split_debug_info)
4784 node->form = DW_FORM_strp;
4785 node->index = NOT_INDEXED;
4787 else
4789 node->form = dwarf_FORM (DW_FORM_strx);
4790 node->index = NO_INDEX_ASSIGNED;
4794 /* A helper function for dwarf2out_finish, called to reset indirect
4795 string decisions done for early LTO dwarf output before fat object
4796 dwarf output. */
4799 reset_indirect_string (indirect_string_node **h, void *)
4801 struct indirect_string_node *node = *h;
4802 if (node->form == DW_FORM_strp
4803 || node->form == DW_FORM_line_strp
4804 || node->form == dwarf_FORM (DW_FORM_strx))
4806 free (node->label);
4807 node->label = NULL;
4808 node->form = (dwarf_form) 0;
4809 node->index = 0;
4811 return 1;
4814 /* Add a string representing a file or filepath attribute value to a DIE. */
4816 static inline void
4817 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4818 const char *str)
4820 if (! asm_outputs_debug_line_str ())
4821 add_AT_string (die, attr_kind, str);
4822 else
4824 dw_attr_node attr;
4825 struct indirect_string_node *node;
4827 if (!debug_line_str_hash)
4828 debug_line_str_hash
4829 = hash_table<indirect_string_hasher>::create_ggc (10);
4831 node = find_AT_string_in_table (str, debug_line_str_hash);
4832 set_indirect_string (node);
4833 node->form = DW_FORM_line_strp;
4835 attr.dw_attr = attr_kind;
4836 attr.dw_attr_val.val_class = dw_val_class_str;
4837 attr.dw_attr_val.val_entry = NULL;
4838 attr.dw_attr_val.v.val_str = node;
4839 add_dwarf_attr (die, &attr);
4843 /* Find out whether a string should be output inline in DIE
4844 or out-of-line in .debug_str section. */
4846 static enum dwarf_form
4847 find_string_form (struct indirect_string_node *node)
4849 unsigned int len;
4851 if (node->form)
4852 return node->form;
4854 len = strlen (node->str) + 1;
4856 /* If the string is shorter or equal to the size of the reference, it is
4857 always better to put it inline. */
4858 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4859 return node->form = DW_FORM_string;
4861 /* If we cannot expect the linker to merge strings in .debug_str
4862 section, only put it into .debug_str if it is worth even in this
4863 single module. */
4864 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4865 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4866 && (len - dwarf_offset_size) * node->refcount <= len))
4867 return node->form = DW_FORM_string;
4869 set_indirect_string (node);
4871 return node->form;
4874 /* Find out whether the string referenced from the attribute should be
4875 output inline in DIE or out-of-line in .debug_str section. */
4877 static enum dwarf_form
4878 AT_string_form (dw_attr_node *a)
4880 gcc_assert (a && AT_class (a) == dw_val_class_str);
4881 return find_string_form (a->dw_attr_val.v.val_str);
4884 /* Add a DIE reference attribute value to a DIE. */
4886 static inline void
4887 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4889 dw_attr_node attr;
4890 gcc_checking_assert (targ_die != NULL);
4892 /* With LTO we can end up trying to reference something we didn't create
4893 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4894 if (targ_die == NULL)
4895 return;
4897 attr.dw_attr = attr_kind;
4898 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4899 attr.dw_attr_val.val_entry = NULL;
4900 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4901 attr.dw_attr_val.v.val_die_ref.external = 0;
4902 add_dwarf_attr (die, &attr);
4905 /* Change DIE reference REF to point to NEW_DIE instead. */
4907 static inline void
4908 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4910 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4911 ref->dw_attr_val.v.val_die_ref.die = new_die;
4912 ref->dw_attr_val.v.val_die_ref.external = 0;
4915 /* Add an AT_specification attribute to a DIE, and also make the back
4916 pointer from the specification to the definition. */
4918 static inline void
4919 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4921 add_AT_die_ref (die, DW_AT_specification, targ_die);
4922 gcc_assert (!targ_die->die_definition);
4923 targ_die->die_definition = die;
4926 static inline dw_die_ref
4927 AT_ref (dw_attr_node *a)
4929 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4930 return a->dw_attr_val.v.val_die_ref.die;
4933 static inline int
4934 AT_ref_external (dw_attr_node *a)
4936 if (a && AT_class (a) == dw_val_class_die_ref)
4937 return a->dw_attr_val.v.val_die_ref.external;
4939 return 0;
4942 static inline void
4943 set_AT_ref_external (dw_attr_node *a, int i)
4945 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4946 a->dw_attr_val.v.val_die_ref.external = i;
4949 /* Add a location description attribute value to a DIE. */
4951 static inline void
4952 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4954 dw_attr_node attr;
4956 attr.dw_attr = attr_kind;
4957 attr.dw_attr_val.val_class = dw_val_class_loc;
4958 attr.dw_attr_val.val_entry = NULL;
4959 attr.dw_attr_val.v.val_loc = loc;
4960 add_dwarf_attr (die, &attr);
4963 static inline dw_loc_descr_ref
4964 AT_loc (dw_attr_node *a)
4966 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4967 return a->dw_attr_val.v.val_loc;
4970 static inline void
4971 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4973 dw_attr_node attr;
4975 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4976 return;
4978 attr.dw_attr = attr_kind;
4979 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4980 attr.dw_attr_val.val_entry = NULL;
4981 attr.dw_attr_val.v.val_loc_list = loc_list;
4982 add_dwarf_attr (die, &attr);
4983 have_location_lists = true;
4986 static inline dw_loc_list_ref
4987 AT_loc_list (dw_attr_node *a)
4989 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4990 return a->dw_attr_val.v.val_loc_list;
4993 /* Add a view list attribute to DIE. It must have a DW_AT_location
4994 attribute, because the view list complements the location list. */
4996 static inline void
4997 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
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_view_list;
5006 attr.dw_attr_val.val_entry = NULL;
5007 attr.dw_attr_val.v.val_view_list = die;
5008 add_dwarf_attr (die, &attr);
5009 gcc_checking_assert (get_AT (die, DW_AT_location));
5010 gcc_assert (have_location_lists);
5013 /* Return a pointer to the location list referenced by the attribute.
5014 If the named attribute is a view list, look up the corresponding
5015 DW_AT_location attribute and return its location list. */
5017 static inline dw_loc_list_ref *
5018 AT_loc_list_ptr (dw_attr_node *a)
5020 gcc_assert (a);
5021 switch (AT_class (a))
5023 case dw_val_class_loc_list:
5024 return &a->dw_attr_val.v.val_loc_list;
5025 case dw_val_class_view_list:
5027 dw_attr_node *l;
5028 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5029 if (!l)
5030 return NULL;
5031 gcc_checking_assert (l + 1 == a);
5032 return AT_loc_list_ptr (l);
5034 default:
5035 gcc_unreachable ();
5039 /* Return the location attribute value associated with a view list
5040 attribute value. */
5042 static inline dw_val_node *
5043 view_list_to_loc_list_val_node (dw_val_node *val)
5045 gcc_assert (val->val_class == dw_val_class_view_list);
5046 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5047 if (!loc)
5048 return NULL;
5049 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5050 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5051 return &loc->dw_attr_val;
5054 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5056 static hashval_t hash (addr_table_entry *);
5057 static bool equal (addr_table_entry *, addr_table_entry *);
5060 /* Table of entries into the .debug_addr section. */
5062 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5064 /* Hash an address_table_entry. */
5066 hashval_t
5067 addr_hasher::hash (addr_table_entry *a)
5069 inchash::hash hstate;
5070 switch (a->kind)
5072 case ate_kind_rtx:
5073 hstate.add_int (0);
5074 break;
5075 case ate_kind_rtx_dtprel:
5076 hstate.add_int (1);
5077 break;
5078 case ate_kind_label:
5079 return htab_hash_string (a->addr.label);
5080 default:
5081 gcc_unreachable ();
5083 inchash::add_rtx (a->addr.rtl, hstate);
5084 return hstate.end ();
5087 /* Determine equality for two address_table_entries. */
5089 bool
5090 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5092 if (a1->kind != a2->kind)
5093 return 0;
5094 switch (a1->kind)
5096 case ate_kind_rtx:
5097 case ate_kind_rtx_dtprel:
5098 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5099 case ate_kind_label:
5100 return strcmp (a1->addr.label, a2->addr.label) == 0;
5101 default:
5102 gcc_unreachable ();
5106 /* Initialize an addr_table_entry. */
5108 void
5109 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5111 e->kind = kind;
5112 switch (kind)
5114 case ate_kind_rtx:
5115 case ate_kind_rtx_dtprel:
5116 e->addr.rtl = (rtx) addr;
5117 break;
5118 case ate_kind_label:
5119 e->addr.label = (char *) addr;
5120 break;
5122 e->refcount = 0;
5123 e->index = NO_INDEX_ASSIGNED;
5126 /* Add attr to the address table entry to the table. Defer setting an
5127 index until output time. */
5129 static addr_table_entry *
5130 add_addr_table_entry (void *addr, enum ate_kind kind)
5132 addr_table_entry *node;
5133 addr_table_entry finder;
5135 gcc_assert (dwarf_split_debug_info);
5136 if (! addr_index_table)
5137 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5138 init_addr_table_entry (&finder, kind, addr);
5139 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5141 if (*slot == HTAB_EMPTY_ENTRY)
5143 node = ggc_cleared_alloc<addr_table_entry> ();
5144 init_addr_table_entry (node, kind, addr);
5145 *slot = node;
5147 else
5148 node = *slot;
5150 node->refcount++;
5151 return node;
5154 /* Remove an entry from the addr table by decrementing its refcount.
5155 Strictly, decrementing the refcount would be enough, but the
5156 assertion that the entry is actually in the table has found
5157 bugs. */
5159 static void
5160 remove_addr_table_entry (addr_table_entry *entry)
5162 gcc_assert (dwarf_split_debug_info && addr_index_table);
5163 /* After an index is assigned, the table is frozen. */
5164 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5165 entry->refcount--;
5168 /* Given a location list, remove all addresses it refers to from the
5169 address_table. */
5171 static void
5172 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5174 for (; descr; descr = descr->dw_loc_next)
5175 if (descr->dw_loc_oprnd1.val_entry != NULL)
5177 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5178 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5182 /* A helper function for dwarf2out_finish called through
5183 htab_traverse. Assign an addr_table_entry its index. All entries
5184 must be collected into the table when this function is called,
5185 because the indexing code relies on htab_traverse to traverse nodes
5186 in the same order for each run. */
5189 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5191 addr_table_entry *node = *h;
5193 /* Don't index unreferenced nodes. */
5194 if (node->refcount == 0)
5195 return 1;
5197 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5198 node->index = *index;
5199 *index += 1;
5201 return 1;
5204 /* Add an address constant attribute value to a DIE. When using
5205 dwarf_split_debug_info, address attributes in dies destined for the
5206 final executable should be direct references--setting the parameter
5207 force_direct ensures this behavior. */
5209 static inline void
5210 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5211 bool force_direct)
5213 dw_attr_node attr;
5215 attr.dw_attr = attr_kind;
5216 attr.dw_attr_val.val_class = dw_val_class_addr;
5217 attr.dw_attr_val.v.val_addr = addr;
5218 if (dwarf_split_debug_info && !force_direct)
5219 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5220 else
5221 attr.dw_attr_val.val_entry = NULL;
5222 add_dwarf_attr (die, &attr);
5225 /* Get the RTX from to an address DIE attribute. */
5227 static inline rtx
5228 AT_addr (dw_attr_node *a)
5230 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5231 return a->dw_attr_val.v.val_addr;
5234 /* Add a file attribute value to a DIE. */
5236 static inline void
5237 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5238 struct dwarf_file_data *fd)
5240 dw_attr_node attr;
5242 attr.dw_attr = attr_kind;
5243 attr.dw_attr_val.val_class = dw_val_class_file;
5244 attr.dw_attr_val.val_entry = NULL;
5245 attr.dw_attr_val.v.val_file = fd;
5246 add_dwarf_attr (die, &attr);
5249 /* Get the dwarf_file_data from a file DIE attribute. */
5251 static inline struct dwarf_file_data *
5252 AT_file (dw_attr_node *a)
5254 gcc_assert (a && (AT_class (a) == dw_val_class_file
5255 || AT_class (a) == dw_val_class_file_implicit));
5256 return a->dw_attr_val.v.val_file;
5259 #if VMS_DEBUGGING_INFO
5260 /* Add a vms delta attribute value to a DIE. */
5262 static inline void
5263 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5264 const char *lbl1, const char *lbl2)
5266 dw_attr_node attr;
5268 attr.dw_attr = attr_kind;
5269 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5270 attr.dw_attr_val.val_entry = NULL;
5271 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5272 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5273 add_dwarf_attr (die, &attr);
5275 #endif
5277 /* Add a symbolic view identifier attribute value to a DIE. */
5279 static inline void
5280 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5281 const char *view_label)
5283 dw_attr_node attr;
5285 attr.dw_attr = attr_kind;
5286 attr.dw_attr_val.val_class = dw_val_class_symview;
5287 attr.dw_attr_val.val_entry = NULL;
5288 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5289 add_dwarf_attr (die, &attr);
5292 /* Add a label identifier attribute value to a DIE. */
5294 static inline void
5295 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5296 const char *lbl_id)
5298 dw_attr_node attr;
5300 attr.dw_attr = attr_kind;
5301 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5302 attr.dw_attr_val.val_entry = NULL;
5303 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5304 if (dwarf_split_debug_info)
5305 attr.dw_attr_val.val_entry
5306 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5307 ate_kind_label);
5308 add_dwarf_attr (die, &attr);
5311 /* Add a section offset attribute value to a DIE, an offset into the
5312 debug_line section. */
5314 static inline void
5315 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5316 const char *label)
5318 dw_attr_node attr;
5320 attr.dw_attr = attr_kind;
5321 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5322 attr.dw_attr_val.val_entry = NULL;
5323 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5324 add_dwarf_attr (die, &attr);
5327 /* Add a section offset attribute value to a DIE, an offset into the
5328 debug_macinfo section. */
5330 static inline void
5331 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5332 const char *label)
5334 dw_attr_node attr;
5336 attr.dw_attr = attr_kind;
5337 attr.dw_attr_val.val_class = dw_val_class_macptr;
5338 attr.dw_attr_val.val_entry = NULL;
5339 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5340 add_dwarf_attr (die, &attr);
5343 /* Add a range_list attribute value to a DIE. When using
5344 dwarf_split_debug_info, address attributes in dies destined for the
5345 final executable should be direct references--setting the parameter
5346 force_direct ensures this behavior. */
5348 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5349 #define RELOCATED_OFFSET (NULL)
5351 static void
5352 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5353 long unsigned int offset, bool force_direct)
5355 dw_attr_node attr;
5357 attr.dw_attr = attr_kind;
5358 attr.dw_attr_val.val_class = dw_val_class_range_list;
5359 /* For the range_list attribute, use val_entry to store whether the
5360 offset should follow split-debug-info or normal semantics. This
5361 value is read in output_range_list_offset. */
5362 if (dwarf_split_debug_info && !force_direct)
5363 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5364 else
5365 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5366 attr.dw_attr_val.v.val_offset = offset;
5367 add_dwarf_attr (die, &attr);
5370 /* Return the start label of a delta attribute. */
5372 static inline const char *
5373 AT_vms_delta1 (dw_attr_node *a)
5375 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5376 return a->dw_attr_val.v.val_vms_delta.lbl1;
5379 /* Return the end label of a delta attribute. */
5381 static inline const char *
5382 AT_vms_delta2 (dw_attr_node *a)
5384 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5385 return a->dw_attr_val.v.val_vms_delta.lbl2;
5388 static inline const char *
5389 AT_lbl (dw_attr_node *a)
5391 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5392 || AT_class (a) == dw_val_class_lineptr
5393 || AT_class (a) == dw_val_class_macptr
5394 || AT_class (a) == dw_val_class_loclistsptr
5395 || AT_class (a) == dw_val_class_high_pc));
5396 return a->dw_attr_val.v.val_lbl_id;
5399 /* Get the attribute of type attr_kind. */
5401 static dw_attr_node *
5402 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5404 dw_attr_node *a;
5405 unsigned ix;
5406 dw_die_ref spec = NULL;
5408 if (! die)
5409 return NULL;
5411 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5412 if (a->dw_attr == attr_kind)
5413 return a;
5414 else if (a->dw_attr == DW_AT_specification
5415 || a->dw_attr == DW_AT_abstract_origin)
5416 spec = AT_ref (a);
5418 if (spec)
5419 return get_AT (spec, attr_kind);
5421 return NULL;
5424 /* Returns the parent of the declaration of DIE. */
5426 static dw_die_ref
5427 get_die_parent (dw_die_ref die)
5429 dw_die_ref t;
5431 if (!die)
5432 return NULL;
5434 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5435 || (t = get_AT_ref (die, DW_AT_specification)))
5436 die = t;
5438 return die->die_parent;
5441 /* Return the "low pc" attribute value, typically associated with a subprogram
5442 DIE. Return null if the "low pc" attribute is either not present, or if it
5443 cannot be represented as an assembler label identifier. */
5445 static inline const char *
5446 get_AT_low_pc (dw_die_ref die)
5448 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5450 return a ? AT_lbl (a) : NULL;
5453 /* Return the value of the string attribute designated by ATTR_KIND, or
5454 NULL if it is not present. */
5456 static inline const char *
5457 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5459 dw_attr_node *a = get_AT (die, attr_kind);
5461 return a ? AT_string (a) : NULL;
5464 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5465 if it is not present. */
5467 static inline int
5468 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5470 dw_attr_node *a = get_AT (die, attr_kind);
5472 return a ? AT_flag (a) : 0;
5475 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5476 if it is not present. */
5478 static inline unsigned
5479 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5481 dw_attr_node *a = get_AT (die, attr_kind);
5483 return a ? AT_unsigned (a) : 0;
5486 static inline dw_die_ref
5487 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5489 dw_attr_node *a = get_AT (die, attr_kind);
5491 return a ? AT_ref (a) : NULL;
5494 static inline struct dwarf_file_data *
5495 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5497 dw_attr_node *a = get_AT (die, attr_kind);
5499 return a ? AT_file (a) : NULL;
5502 /* Return TRUE if the language is C. */
5504 static inline bool
5505 is_c (void)
5507 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5509 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5510 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5515 /* Return TRUE if the language is C++. */
5517 static inline bool
5518 is_cxx (void)
5520 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5522 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5523 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5526 /* Return TRUE if DECL was created by the C++ frontend. */
5528 static bool
5529 is_cxx (const_tree decl)
5531 if (in_lto_p)
5533 const_tree context = get_ultimate_context (decl);
5534 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5535 return startswith (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++");
5537 return is_cxx ();
5540 /* Return TRUE if the language is Fortran. */
5542 static inline bool
5543 is_fortran (void)
5545 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5547 return (lang == DW_LANG_Fortran77
5548 || lang == DW_LANG_Fortran90
5549 || lang == DW_LANG_Fortran95
5550 || lang == DW_LANG_Fortran03
5551 || lang == DW_LANG_Fortran08);
5554 static inline bool
5555 is_fortran (const_tree decl)
5557 if (in_lto_p)
5559 const_tree context = get_ultimate_context (decl);
5560 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5561 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5562 "GNU Fortran", 11) == 0
5563 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5564 "GNU F77") == 0);
5566 return is_fortran ();
5569 /* Return TRUE if the language is Ada. */
5571 static inline bool
5572 is_ada (void)
5574 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5576 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5579 /* Return TRUE if the language is D. */
5581 static inline bool
5582 is_dlang (void)
5584 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5586 return lang == DW_LANG_D;
5589 /* Remove the specified attribute if present. Return TRUE if removal
5590 was successful. */
5592 static bool
5593 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5595 dw_attr_node *a;
5596 unsigned ix;
5598 if (! die)
5599 return false;
5601 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5602 if (a->dw_attr == attr_kind)
5604 if (AT_class (a) == dw_val_class_str)
5605 if (a->dw_attr_val.v.val_str->refcount)
5606 a->dw_attr_val.v.val_str->refcount--;
5608 /* vec::ordered_remove should help reduce the number of abbrevs
5609 that are needed. */
5610 die->die_attr->ordered_remove (ix);
5611 return true;
5613 return false;
5616 /* Remove CHILD from its parent. PREV must have the property that
5617 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5619 static void
5620 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5622 gcc_assert (child->die_parent == prev->die_parent);
5623 gcc_assert (prev->die_sib == child);
5624 if (prev == child)
5626 gcc_assert (child->die_parent->die_child == child);
5627 prev = NULL;
5629 else
5630 prev->die_sib = child->die_sib;
5631 if (child->die_parent->die_child == child)
5632 child->die_parent->die_child = prev;
5633 child->die_sib = NULL;
5636 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5637 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5639 static void
5640 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5642 dw_die_ref parent = old_child->die_parent;
5644 gcc_assert (parent == prev->die_parent);
5645 gcc_assert (prev->die_sib == old_child);
5647 new_child->die_parent = parent;
5648 if (prev == old_child)
5650 gcc_assert (parent->die_child == old_child);
5651 new_child->die_sib = new_child;
5653 else
5655 prev->die_sib = new_child;
5656 new_child->die_sib = old_child->die_sib;
5658 if (old_child->die_parent->die_child == old_child)
5659 old_child->die_parent->die_child = new_child;
5660 old_child->die_sib = NULL;
5663 /* Move all children from OLD_PARENT to NEW_PARENT. */
5665 static void
5666 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5668 dw_die_ref c;
5669 new_parent->die_child = old_parent->die_child;
5670 old_parent->die_child = NULL;
5671 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5674 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5675 matches TAG. */
5677 static void
5678 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5680 dw_die_ref c;
5682 c = die->die_child;
5683 if (c) do {
5684 dw_die_ref prev = c;
5685 c = c->die_sib;
5686 while (c->die_tag == tag)
5688 remove_child_with_prev (c, prev);
5689 c->die_parent = NULL;
5690 /* Might have removed every child. */
5691 if (die->die_child == NULL)
5692 return;
5693 c = prev->die_sib;
5695 } while (c != die->die_child);
5698 /* Add a CHILD_DIE as the last child of DIE. */
5700 static void
5701 add_child_die (dw_die_ref die, dw_die_ref child_die)
5703 /* FIXME this should probably be an assert. */
5704 if (! die || ! child_die)
5705 return;
5706 gcc_assert (die != child_die);
5708 child_die->die_parent = die;
5709 if (die->die_child)
5711 child_die->die_sib = die->die_child->die_sib;
5712 die->die_child->die_sib = child_die;
5714 else
5715 child_die->die_sib = child_die;
5716 die->die_child = child_die;
5719 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5721 static void
5722 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5723 dw_die_ref after_die)
5725 gcc_assert (die
5726 && child_die
5727 && after_die
5728 && die->die_child
5729 && die != child_die);
5731 child_die->die_parent = die;
5732 child_die->die_sib = after_die->die_sib;
5733 after_die->die_sib = child_die;
5734 if (die->die_child == after_die)
5735 die->die_child = child_die;
5738 /* Unassociate CHILD from its parent, and make its parent be
5739 NEW_PARENT. */
5741 static void
5742 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5744 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5745 if (p->die_sib == child)
5747 remove_child_with_prev (child, p);
5748 break;
5750 add_child_die (new_parent, child);
5753 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5754 is the specification, to the end of PARENT's list of children.
5755 This is done by removing and re-adding it. */
5757 static void
5758 splice_child_die (dw_die_ref parent, dw_die_ref child)
5760 /* We want the declaration DIE from inside the class, not the
5761 specification DIE at toplevel. */
5762 if (child->die_parent != parent)
5764 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5766 if (tmp)
5767 child = tmp;
5770 gcc_assert (child->die_parent == parent
5771 || (child->die_parent
5772 == get_AT_ref (parent, DW_AT_specification)));
5774 reparent_child (child, parent);
5777 /* Create and return a new die with TAG_VALUE as tag. */
5779 static inline dw_die_ref
5780 new_die_raw (enum dwarf_tag tag_value)
5782 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5783 die->die_tag = tag_value;
5784 return die;
5787 /* Create and return a new die with a parent of PARENT_DIE. If
5788 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5789 associated tree T must be supplied to determine parenthood
5790 later. */
5792 static inline dw_die_ref
5793 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5795 dw_die_ref die = new_die_raw (tag_value);
5797 if (parent_die != NULL)
5798 add_child_die (parent_die, die);
5799 else
5801 limbo_die_node *limbo_node;
5803 /* No DIEs created after early dwarf should end up in limbo,
5804 because the limbo list should not persist past LTO
5805 streaming. */
5806 if (tag_value != DW_TAG_compile_unit
5807 /* These are allowed because they're generated while
5808 breaking out COMDAT units late. */
5809 && tag_value != DW_TAG_type_unit
5810 && tag_value != DW_TAG_skeleton_unit
5811 && !early_dwarf
5812 /* Allow nested functions to live in limbo because they will
5813 only temporarily live there, as decls_for_scope will fix
5814 them up. */
5815 && (TREE_CODE (t) != FUNCTION_DECL
5816 || !decl_function_context (t))
5817 /* Same as nested functions above but for types. Types that
5818 are local to a function will be fixed in
5819 decls_for_scope. */
5820 && (!RECORD_OR_UNION_TYPE_P (t)
5821 || !TYPE_CONTEXT (t)
5822 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5823 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5824 especially in the ltrans stage, but once we implement LTO
5825 dwarf streaming, we should remove this exception. */
5826 && !in_lto_p)
5828 fprintf (stderr, "symbol ended up in limbo too late:");
5829 debug_generic_stmt (t);
5830 gcc_unreachable ();
5833 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5834 limbo_node->die = die;
5835 limbo_node->created_for = t;
5836 limbo_node->next = limbo_die_list;
5837 limbo_die_list = limbo_node;
5840 return die;
5843 /* Return the DIE associated with the given type specifier. */
5845 static inline dw_die_ref
5846 lookup_type_die (tree type)
5848 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5849 if (die && die->removed)
5851 TYPE_SYMTAB_DIE (type) = NULL;
5852 return NULL;
5854 return die;
5857 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5858 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5859 anonymous type instead the one of the naming typedef. */
5861 static inline dw_die_ref
5862 strip_naming_typedef (tree type, dw_die_ref type_die)
5864 if (type
5865 && TREE_CODE (type) == RECORD_TYPE
5866 && type_die
5867 && type_die->die_tag == DW_TAG_typedef
5868 && is_naming_typedef_decl (TYPE_NAME (type)))
5869 type_die = get_AT_ref (type_die, DW_AT_type);
5870 return type_die;
5873 /* Like lookup_type_die, but if type is an anonymous type named by a
5874 typedef[1], return the DIE of the anonymous type instead the one of
5875 the naming typedef. This is because in gen_typedef_die, we did
5876 equate the anonymous struct named by the typedef with the DIE of
5877 the naming typedef. So by default, lookup_type_die on an anonymous
5878 struct yields the DIE of the naming typedef.
5880 [1]: Read the comment of is_naming_typedef_decl to learn about what
5881 a naming typedef is. */
5883 static inline dw_die_ref
5884 lookup_type_die_strip_naming_typedef (tree type)
5886 dw_die_ref die = lookup_type_die (type);
5887 return strip_naming_typedef (type, die);
5890 /* Equate a DIE to a given type specifier. */
5892 static inline void
5893 equate_type_number_to_die (tree type, dw_die_ref type_die)
5895 TYPE_SYMTAB_DIE (type) = type_die;
5898 static dw_die_ref maybe_create_die_with_external_ref (tree);
5899 struct GTY(()) sym_off_pair
5901 const char * GTY((skip)) sym;
5902 unsigned HOST_WIDE_INT off;
5904 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5906 /* Returns a hash value for X (which really is a die_struct). */
5908 inline hashval_t
5909 decl_die_hasher::hash (die_node *x)
5911 return (hashval_t) x->decl_id;
5914 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5916 inline bool
5917 decl_die_hasher::equal (die_node *x, tree y)
5919 return (x->decl_id == DECL_UID (y));
5922 /* Return the DIE associated with a given declaration. */
5924 static inline dw_die_ref
5925 lookup_decl_die (tree decl)
5927 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5928 NO_INSERT);
5929 if (!die)
5931 if (in_lto_p)
5932 return maybe_create_die_with_external_ref (decl);
5933 return NULL;
5935 if ((*die)->removed)
5937 decl_die_table->clear_slot (die);
5938 return NULL;
5940 return *die;
5944 /* Return the DIE associated with BLOCK. */
5946 static inline dw_die_ref
5947 lookup_block_die (tree block)
5949 dw_die_ref die = BLOCK_DIE (block);
5950 if (!die && in_lto_p)
5951 return maybe_create_die_with_external_ref (block);
5952 return die;
5955 /* Associate DIE with BLOCK. */
5957 static inline void
5958 equate_block_to_die (tree block, dw_die_ref die)
5960 BLOCK_DIE (block) = die;
5962 #undef BLOCK_DIE
5965 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5966 style reference. Return true if we found one refering to a DIE for
5967 DECL, otherwise return false. */
5969 static bool
5970 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5971 unsigned HOST_WIDE_INT *off)
5973 dw_die_ref die;
5975 if (in_lto_p)
5977 /* During WPA stage and incremental linking we use a hash-map
5978 to store the decl <-> label + offset map. */
5979 if (!external_die_map)
5980 return false;
5981 sym_off_pair *desc = external_die_map->get (decl);
5982 if (!desc)
5983 return false;
5984 *sym = desc->sym;
5985 *off = desc->off;
5986 return true;
5989 if (TREE_CODE (decl) == BLOCK)
5990 die = lookup_block_die (decl);
5991 else
5992 die = lookup_decl_die (decl);
5993 if (!die)
5994 return false;
5996 /* Similar to get_ref_die_offset_label, but using the "correct"
5997 label. */
5998 *off = die->die_offset;
5999 while (die->die_parent)
6000 die = die->die_parent;
6001 /* For the containing CU DIE we compute a die_symbol in
6002 compute_comp_unit_symbol. */
6003 gcc_assert (die->die_tag == DW_TAG_compile_unit
6004 && die->die_id.die_symbol != NULL);
6005 *sym = die->die_id.die_symbol;
6006 return true;
6009 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6011 static void
6012 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6013 const char *symbol, HOST_WIDE_INT offset)
6015 /* Create a fake DIE that contains the reference. Don't use
6016 new_die because we don't want to end up in the limbo list. */
6017 /* ??? We probably want to share these, thus put a ref to the DIE
6018 we create here to the external_die_map entry. */
6019 dw_die_ref ref = new_die_raw (die->die_tag);
6020 ref->die_id.die_symbol = symbol;
6021 ref->die_offset = offset;
6022 ref->with_offset = 1;
6023 add_AT_die_ref (die, attr_kind, ref);
6026 /* Create a DIE for DECL if required and add a reference to a DIE
6027 at SYMBOL + OFFSET which contains attributes dumped early. */
6029 static void
6030 dwarf2out_register_external_die (tree decl, const char *sym,
6031 unsigned HOST_WIDE_INT off)
6033 if (debug_info_level == DINFO_LEVEL_NONE)
6034 return;
6036 if (!external_die_map)
6037 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
6038 gcc_checking_assert (!external_die_map->get (decl));
6039 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
6040 external_die_map->put (decl, p);
6043 /* If we have a registered external DIE for DECL return a new DIE for
6044 the concrete instance with an appropriate abstract origin. */
6046 static dw_die_ref
6047 maybe_create_die_with_external_ref (tree decl)
6049 if (!external_die_map)
6050 return NULL;
6051 sym_off_pair *desc = external_die_map->get (decl);
6052 if (!desc)
6053 return NULL;
6055 const char *sym = desc->sym;
6056 unsigned HOST_WIDE_INT off = desc->off;
6057 external_die_map->remove (decl);
6059 in_lto_p = false;
6060 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6061 ? lookup_block_die (decl) : lookup_decl_die (decl));
6062 gcc_assert (!die);
6063 in_lto_p = true;
6065 tree ctx;
6066 dw_die_ref parent = NULL;
6067 /* Need to lookup a DIE for the decls context - the containing
6068 function or translation unit. */
6069 if (TREE_CODE (decl) == BLOCK)
6071 ctx = BLOCK_SUPERCONTEXT (decl);
6072 /* ??? We do not output DIEs for all scopes thus skip as
6073 many DIEs as needed. */
6074 while (TREE_CODE (ctx) == BLOCK
6075 && !lookup_block_die (ctx))
6076 ctx = BLOCK_SUPERCONTEXT (ctx);
6078 else
6079 ctx = DECL_CONTEXT (decl);
6080 /* Peel types in the context stack. */
6081 while (ctx && TYPE_P (ctx))
6082 ctx = TYPE_CONTEXT (ctx);
6083 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6084 if (debug_info_level <= DINFO_LEVEL_TERSE)
6085 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6086 ctx = DECL_CONTEXT (ctx);
6087 if (ctx)
6089 if (TREE_CODE (ctx) == BLOCK)
6090 parent = lookup_block_die (ctx);
6091 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6092 /* Keep the 1:1 association during WPA. */
6093 && !flag_wpa
6094 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6095 /* Otherwise all late annotations go to the main CU which
6096 imports the original CUs. */
6097 parent = comp_unit_die ();
6098 else if (TREE_CODE (ctx) == FUNCTION_DECL
6099 && TREE_CODE (decl) != FUNCTION_DECL
6100 && TREE_CODE (decl) != PARM_DECL
6101 && TREE_CODE (decl) != RESULT_DECL
6102 && TREE_CODE (decl) != BLOCK)
6103 /* Leave function local entities parent determination to when
6104 we process scope vars. */
6106 else
6107 parent = lookup_decl_die (ctx);
6109 else
6110 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6111 Handle this case gracefully by globalizing stuff. */
6112 parent = comp_unit_die ();
6113 /* Create a DIE "stub". */
6114 switch (TREE_CODE (decl))
6116 case TRANSLATION_UNIT_DECL:
6118 die = comp_unit_die ();
6119 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6120 to create a DIE for the original CUs. */
6121 return die;
6123 case NAMESPACE_DECL:
6124 if (is_fortran (decl))
6125 die = new_die (DW_TAG_module, parent, decl);
6126 else
6127 die = new_die (DW_TAG_namespace, parent, decl);
6128 break;
6129 case FUNCTION_DECL:
6130 die = new_die (DW_TAG_subprogram, parent, decl);
6131 break;
6132 case VAR_DECL:
6133 die = new_die (DW_TAG_variable, parent, decl);
6134 break;
6135 case RESULT_DECL:
6136 die = new_die (DW_TAG_variable, parent, decl);
6137 break;
6138 case PARM_DECL:
6139 die = new_die (DW_TAG_formal_parameter, parent, decl);
6140 break;
6141 case CONST_DECL:
6142 die = new_die (DW_TAG_constant, parent, decl);
6143 break;
6144 case LABEL_DECL:
6145 die = new_die (DW_TAG_label, parent, decl);
6146 break;
6147 case BLOCK:
6148 die = new_die (DW_TAG_lexical_block, parent, decl);
6149 break;
6150 default:
6151 gcc_unreachable ();
6153 if (TREE_CODE (decl) == BLOCK)
6154 equate_block_to_die (decl, die);
6155 else
6156 equate_decl_number_to_die (decl, die);
6158 add_desc_attribute (die, decl);
6160 /* Add a reference to the DIE providing early debug at $sym + off. */
6161 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6163 return die;
6166 /* Returns a hash value for X (which really is a var_loc_list). */
6168 inline hashval_t
6169 decl_loc_hasher::hash (var_loc_list *x)
6171 return (hashval_t) x->decl_id;
6174 /* Return nonzero if decl_id of var_loc_list X is the same as
6175 UID of decl *Y. */
6177 inline bool
6178 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6180 return (x->decl_id == DECL_UID (y));
6183 /* Return the var_loc list associated with a given declaration. */
6185 static inline var_loc_list *
6186 lookup_decl_loc (const_tree decl)
6188 if (!decl_loc_table)
6189 return NULL;
6190 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6193 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6195 inline hashval_t
6196 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6198 return (hashval_t) x->decl_id;
6201 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6202 UID of decl *Y. */
6204 inline bool
6205 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6207 return (x->decl_id == DECL_UID (y));
6210 /* Equate a DIE to a particular declaration. */
6212 static void
6213 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6215 unsigned int decl_id = DECL_UID (decl);
6217 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6218 decl_die->decl_id = decl_id;
6221 /* Return how many bits covers PIECE EXPR_LIST. */
6223 static HOST_WIDE_INT
6224 decl_piece_bitsize (rtx piece)
6226 int ret = (int) GET_MODE (piece);
6227 if (ret)
6228 return ret;
6229 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6230 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6231 return INTVAL (XEXP (XEXP (piece, 0), 0));
6234 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6236 static rtx *
6237 decl_piece_varloc_ptr (rtx piece)
6239 if ((int) GET_MODE (piece))
6240 return &XEXP (piece, 0);
6241 else
6242 return &XEXP (XEXP (piece, 0), 1);
6245 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6246 Next is the chain of following piece nodes. */
6248 static rtx_expr_list *
6249 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6251 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6252 return alloc_EXPR_LIST (bitsize, loc_note, next);
6253 else
6254 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6255 GEN_INT (bitsize),
6256 loc_note), next);
6259 /* Return rtx that should be stored into loc field for
6260 LOC_NOTE and BITPOS/BITSIZE. */
6262 static rtx
6263 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6264 HOST_WIDE_INT bitsize)
6266 if (bitsize != -1)
6268 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6269 if (bitpos != 0)
6270 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6272 return loc_note;
6275 /* This function either modifies location piece list *DEST in
6276 place (if SRC and INNER is NULL), or copies location piece list
6277 *SRC to *DEST while modifying it. Location BITPOS is modified
6278 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6279 not copied and if needed some padding around it is added.
6280 When modifying in place, DEST should point to EXPR_LIST where
6281 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6282 to the start of the whole list and INNER points to the EXPR_LIST
6283 where earlier pieces cover PIECE_BITPOS bits. */
6285 static void
6286 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6287 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6288 HOST_WIDE_INT bitsize, rtx loc_note)
6290 HOST_WIDE_INT diff;
6291 bool copy = inner != NULL;
6293 if (copy)
6295 /* First copy all nodes preceding the current bitpos. */
6296 while (src != inner)
6298 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6299 decl_piece_bitsize (*src), NULL_RTX);
6300 dest = &XEXP (*dest, 1);
6301 src = &XEXP (*src, 1);
6304 /* Add padding if needed. */
6305 if (bitpos != piece_bitpos)
6307 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6308 copy ? NULL_RTX : *dest);
6309 dest = &XEXP (*dest, 1);
6311 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6313 gcc_assert (!copy);
6314 /* A piece with correct bitpos and bitsize already exist,
6315 just update the location for it and return. */
6316 *decl_piece_varloc_ptr (*dest) = loc_note;
6317 return;
6319 /* Add the piece that changed. */
6320 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6321 dest = &XEXP (*dest, 1);
6322 /* Skip over pieces that overlap it. */
6323 diff = bitpos - piece_bitpos + bitsize;
6324 if (!copy)
6325 src = dest;
6326 while (diff > 0 && *src)
6328 rtx piece = *src;
6329 diff -= decl_piece_bitsize (piece);
6330 if (copy)
6331 src = &XEXP (piece, 1);
6332 else
6334 *src = XEXP (piece, 1);
6335 free_EXPR_LIST_node (piece);
6338 /* Add padding if needed. */
6339 if (diff < 0 && *src)
6341 if (!copy)
6342 dest = src;
6343 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6344 dest = &XEXP (*dest, 1);
6346 if (!copy)
6347 return;
6348 /* Finally copy all nodes following it. */
6349 while (*src)
6351 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6352 decl_piece_bitsize (*src), NULL_RTX);
6353 dest = &XEXP (*dest, 1);
6354 src = &XEXP (*src, 1);
6358 /* Add a variable location node to the linked list for DECL. */
6360 static struct var_loc_node *
6361 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6363 unsigned int decl_id;
6364 var_loc_list *temp;
6365 struct var_loc_node *loc = NULL;
6366 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6368 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6370 tree realdecl = DECL_DEBUG_EXPR (decl);
6371 if (handled_component_p (realdecl)
6372 || (TREE_CODE (realdecl) == MEM_REF
6373 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6375 bool reverse;
6376 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6377 &bitsize, &reverse);
6378 if (!innerdecl
6379 || !DECL_P (innerdecl)
6380 || DECL_IGNORED_P (innerdecl)
6381 || TREE_STATIC (innerdecl)
6382 || bitsize == 0
6383 || bitpos + bitsize > 256)
6384 return NULL;
6385 decl = innerdecl;
6389 decl_id = DECL_UID (decl);
6390 var_loc_list **slot
6391 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6392 if (*slot == NULL)
6394 temp = ggc_cleared_alloc<var_loc_list> ();
6395 temp->decl_id = decl_id;
6396 *slot = temp;
6398 else
6399 temp = *slot;
6401 /* For PARM_DECLs try to keep around the original incoming value,
6402 even if that means we'll emit a zero-range .debug_loc entry. */
6403 if (temp->last
6404 && temp->first == temp->last
6405 && TREE_CODE (decl) == PARM_DECL
6406 && NOTE_P (temp->first->loc)
6407 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6408 && DECL_INCOMING_RTL (decl)
6409 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6410 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6411 == GET_CODE (DECL_INCOMING_RTL (decl))
6412 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6413 && (bitsize != -1
6414 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6415 NOTE_VAR_LOCATION_LOC (loc_note))
6416 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6417 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6419 loc = ggc_cleared_alloc<var_loc_node> ();
6420 temp->first->next = loc;
6421 temp->last = loc;
6422 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6424 else if (temp->last)
6426 struct var_loc_node *last = temp->last, *unused = NULL;
6427 rtx *piece_loc = NULL, last_loc_note;
6428 HOST_WIDE_INT piece_bitpos = 0;
6429 if (last->next)
6431 last = last->next;
6432 gcc_assert (last->next == NULL);
6434 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6436 piece_loc = &last->loc;
6439 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6440 if (piece_bitpos + cur_bitsize > bitpos)
6441 break;
6442 piece_bitpos += cur_bitsize;
6443 piece_loc = &XEXP (*piece_loc, 1);
6445 while (*piece_loc);
6447 /* TEMP->LAST here is either pointer to the last but one or
6448 last element in the chained list, LAST is pointer to the
6449 last element. */
6450 if (label && strcmp (last->label, label) == 0 && last->view == view)
6452 /* For SRA optimized variables if there weren't any real
6453 insns since last note, just modify the last node. */
6454 if (piece_loc != NULL)
6456 adjust_piece_list (piece_loc, NULL, NULL,
6457 bitpos, piece_bitpos, bitsize, loc_note);
6458 return NULL;
6460 /* If the last note doesn't cover any instructions, remove it. */
6461 if (temp->last != last)
6463 temp->last->next = NULL;
6464 unused = last;
6465 last = temp->last;
6466 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6468 else
6470 gcc_assert (temp->first == temp->last
6471 || (temp->first->next == temp->last
6472 && TREE_CODE (decl) == PARM_DECL));
6473 memset (temp->last, '\0', sizeof (*temp->last));
6474 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6475 return temp->last;
6478 if (bitsize == -1 && NOTE_P (last->loc))
6479 last_loc_note = last->loc;
6480 else if (piece_loc != NULL
6481 && *piece_loc != NULL_RTX
6482 && piece_bitpos == bitpos
6483 && decl_piece_bitsize (*piece_loc) == bitsize)
6484 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6485 else
6486 last_loc_note = NULL_RTX;
6487 /* If the current location is the same as the end of the list,
6488 and either both or neither of the locations is uninitialized,
6489 we have nothing to do. */
6490 if (last_loc_note == NULL_RTX
6491 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6492 NOTE_VAR_LOCATION_LOC (loc_note)))
6493 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6494 != NOTE_VAR_LOCATION_STATUS (loc_note))
6495 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6496 == VAR_INIT_STATUS_UNINITIALIZED)
6497 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6498 == VAR_INIT_STATUS_UNINITIALIZED))))
6500 /* Add LOC to the end of list and update LAST. If the last
6501 element of the list has been removed above, reuse its
6502 memory for the new node, otherwise allocate a new one. */
6503 if (unused)
6505 loc = unused;
6506 memset (loc, '\0', sizeof (*loc));
6508 else
6509 loc = ggc_cleared_alloc<var_loc_node> ();
6510 if (bitsize == -1 || piece_loc == NULL)
6511 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6512 else
6513 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6514 bitpos, piece_bitpos, bitsize, loc_note);
6515 last->next = loc;
6516 /* Ensure TEMP->LAST will point either to the new last but one
6517 element of the chain, or to the last element in it. */
6518 if (last != temp->last)
6519 temp->last = last;
6521 else if (unused)
6522 ggc_free (unused);
6524 else
6526 loc = ggc_cleared_alloc<var_loc_node> ();
6527 temp->first = loc;
6528 temp->last = loc;
6529 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6531 return loc;
6534 /* Keep track of the number of spaces used to indent the
6535 output of the debugging routines that print the structure of
6536 the DIE internal representation. */
6537 static int print_indent;
6539 /* Indent the line the number of spaces given by print_indent. */
6541 static inline void
6542 print_spaces (FILE *outfile)
6544 fprintf (outfile, "%*s", print_indent, "");
6547 /* Print a type signature in hex. */
6549 static inline void
6550 print_signature (FILE *outfile, char *sig)
6552 int i;
6554 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6555 fprintf (outfile, "%02x", sig[i] & 0xff);
6558 static inline void
6559 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6561 if (discr_value->pos)
6562 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6563 else
6564 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6567 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6569 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6570 RECURSE, output location descriptor operations. */
6572 static void
6573 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6575 switch (val->val_class)
6577 case dw_val_class_addr:
6578 fprintf (outfile, "address");
6579 break;
6580 case dw_val_class_offset:
6581 fprintf (outfile, "offset");
6582 break;
6583 case dw_val_class_loc:
6584 fprintf (outfile, "location descriptor");
6585 if (val->v.val_loc == NULL)
6586 fprintf (outfile, " -> <null>\n");
6587 else if (recurse)
6589 fprintf (outfile, ":\n");
6590 print_indent += 4;
6591 print_loc_descr (val->v.val_loc, outfile);
6592 print_indent -= 4;
6594 else
6596 if (flag_dump_noaddr || flag_dump_unnumbered)
6597 fprintf (outfile, " #\n");
6598 else
6599 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6601 break;
6602 case dw_val_class_loc_list:
6603 fprintf (outfile, "location list -> label:%s",
6604 val->v.val_loc_list->ll_symbol);
6605 break;
6606 case dw_val_class_view_list:
6607 val = view_list_to_loc_list_val_node (val);
6608 fprintf (outfile, "location list with views -> labels:%s and %s",
6609 val->v.val_loc_list->ll_symbol,
6610 val->v.val_loc_list->vl_symbol);
6611 break;
6612 case dw_val_class_range_list:
6613 fprintf (outfile, "range list");
6614 break;
6615 case dw_val_class_const:
6616 case dw_val_class_const_implicit:
6617 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6618 break;
6619 case dw_val_class_unsigned_const:
6620 case dw_val_class_unsigned_const_implicit:
6621 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6622 break;
6623 case dw_val_class_const_double:
6624 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6625 HOST_WIDE_INT_PRINT_UNSIGNED")",
6626 val->v.val_double.high,
6627 val->v.val_double.low);
6628 break;
6629 case dw_val_class_wide_int:
6631 int i = val->v.val_wide->get_len ();
6632 fprintf (outfile, "constant (");
6633 gcc_assert (i > 0);
6634 if (val->v.val_wide->elt (i - 1) == 0)
6635 fprintf (outfile, "0x");
6636 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6637 val->v.val_wide->elt (--i));
6638 while (--i >= 0)
6639 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6640 val->v.val_wide->elt (i));
6641 fprintf (outfile, ")");
6642 break;
6644 case dw_val_class_vec:
6645 fprintf (outfile, "floating-point or vector constant");
6646 break;
6647 case dw_val_class_flag:
6648 fprintf (outfile, "%u", val->v.val_flag);
6649 break;
6650 case dw_val_class_die_ref:
6651 if (val->v.val_die_ref.die != NULL)
6653 dw_die_ref die = val->v.val_die_ref.die;
6655 if (die->comdat_type_p)
6657 fprintf (outfile, "die -> signature: ");
6658 print_signature (outfile,
6659 die->die_id.die_type_node->signature);
6661 else if (die->die_id.die_symbol)
6663 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6664 if (die->with_offset)
6665 fprintf (outfile, " + %ld", die->die_offset);
6667 else
6668 fprintf (outfile, "die -> %ld", die->die_offset);
6669 if (flag_dump_noaddr || flag_dump_unnumbered)
6670 fprintf (outfile, " #");
6671 else
6672 fprintf (outfile, " (%p)", (void *) die);
6674 else
6675 fprintf (outfile, "die -> <null>");
6676 break;
6677 case dw_val_class_vms_delta:
6678 fprintf (outfile, "delta: @slotcount(%s-%s)",
6679 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6680 break;
6681 case dw_val_class_symview:
6682 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6683 break;
6684 case dw_val_class_lbl_id:
6685 case dw_val_class_lineptr:
6686 case dw_val_class_macptr:
6687 case dw_val_class_loclistsptr:
6688 case dw_val_class_high_pc:
6689 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6690 break;
6691 case dw_val_class_str:
6692 if (val->v.val_str->str != NULL)
6693 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6694 else
6695 fprintf (outfile, "<null>");
6696 break;
6697 case dw_val_class_file:
6698 case dw_val_class_file_implicit:
6699 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6700 val->v.val_file->emitted_number);
6701 break;
6702 case dw_val_class_data8:
6704 int i;
6706 for (i = 0; i < 8; i++)
6707 fprintf (outfile, "%02x", val->v.val_data8[i]);
6708 break;
6710 case dw_val_class_discr_value:
6711 print_discr_value (outfile, &val->v.val_discr_value);
6712 break;
6713 case dw_val_class_discr_list:
6714 for (dw_discr_list_ref node = val->v.val_discr_list;
6715 node != NULL;
6716 node = node->dw_discr_next)
6718 if (node->dw_discr_range)
6720 fprintf (outfile, " .. ");
6721 print_discr_value (outfile, &node->dw_discr_lower_bound);
6722 print_discr_value (outfile, &node->dw_discr_upper_bound);
6724 else
6725 print_discr_value (outfile, &node->dw_discr_lower_bound);
6727 if (node->dw_discr_next != NULL)
6728 fprintf (outfile, " | ");
6730 default:
6731 break;
6735 /* Likewise, for a DIE attribute. */
6737 static void
6738 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6740 print_dw_val (&a->dw_attr_val, recurse, outfile);
6744 /* Print the list of operands in the LOC location description to OUTFILE. This
6745 routine is a debugging aid only. */
6747 static void
6748 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6750 dw_loc_descr_ref l = loc;
6752 if (loc == NULL)
6754 print_spaces (outfile);
6755 fprintf (outfile, "<null>\n");
6756 return;
6759 for (l = loc; l != NULL; l = l->dw_loc_next)
6761 print_spaces (outfile);
6762 if (flag_dump_noaddr || flag_dump_unnumbered)
6763 fprintf (outfile, "#");
6764 else
6765 fprintf (outfile, "(%p)", (void *) l);
6766 fprintf (outfile, " %s",
6767 dwarf_stack_op_name (l->dw_loc_opc));
6768 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6770 fprintf (outfile, " ");
6771 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6773 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6775 fprintf (outfile, ", ");
6776 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6778 fprintf (outfile, "\n");
6782 /* Print the information associated with a given DIE, and its children.
6783 This routine is a debugging aid only. */
6785 static void
6786 print_die (dw_die_ref die, FILE *outfile)
6788 dw_attr_node *a;
6789 dw_die_ref c;
6790 unsigned ix;
6792 print_spaces (outfile);
6793 fprintf (outfile, "DIE %4ld: %s ",
6794 die->die_offset, dwarf_tag_name (die->die_tag));
6795 if (flag_dump_noaddr || flag_dump_unnumbered)
6796 fprintf (outfile, "#\n");
6797 else
6798 fprintf (outfile, "(%p)\n", (void*) die);
6799 print_spaces (outfile);
6800 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6801 fprintf (outfile, " offset: %ld", die->die_offset);
6802 fprintf (outfile, " mark: %d\n", die->die_mark);
6804 if (die->comdat_type_p)
6806 print_spaces (outfile);
6807 fprintf (outfile, " signature: ");
6808 print_signature (outfile, die->die_id.die_type_node->signature);
6809 fprintf (outfile, "\n");
6812 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6814 print_spaces (outfile);
6815 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6817 print_attribute (a, true, outfile);
6818 fprintf (outfile, "\n");
6821 if (die->die_child != NULL)
6823 print_indent += 4;
6824 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6825 print_indent -= 4;
6827 if (print_indent == 0)
6828 fprintf (outfile, "\n");
6831 /* Print the list of operations in the LOC location description. */
6833 DEBUG_FUNCTION void
6834 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6836 print_loc_descr (loc, stderr);
6839 /* Print the information collected for a given DIE. */
6841 DEBUG_FUNCTION void
6842 debug_dwarf_die (dw_die_ref die)
6844 print_die (die, stderr);
6847 DEBUG_FUNCTION void
6848 debug (die_struct &ref)
6850 print_die (&ref, stderr);
6853 DEBUG_FUNCTION void
6854 debug (die_struct *ptr)
6856 if (ptr)
6857 debug (*ptr);
6858 else
6859 fprintf (stderr, "<nil>\n");
6863 /* Print all DWARF information collected for the compilation unit.
6864 This routine is a debugging aid only. */
6866 DEBUG_FUNCTION void
6867 debug_dwarf (void)
6869 print_indent = 0;
6870 print_die (comp_unit_die (), stderr);
6873 /* Verify the DIE tree structure. */
6875 DEBUG_FUNCTION void
6876 verify_die (dw_die_ref die)
6878 gcc_assert (!die->die_mark);
6879 if (die->die_parent == NULL
6880 && die->die_sib == NULL)
6881 return;
6882 /* Verify the die_sib list is cyclic. */
6883 dw_die_ref x = die;
6886 x->die_mark = 1;
6887 x = x->die_sib;
6889 while (x && !x->die_mark);
6890 gcc_assert (x == die);
6891 x = die;
6894 /* Verify all dies have the same parent. */
6895 gcc_assert (x->die_parent == die->die_parent);
6896 if (x->die_child)
6898 /* Verify the child has the proper parent and recurse. */
6899 gcc_assert (x->die_child->die_parent == x);
6900 verify_die (x->die_child);
6902 x->die_mark = 0;
6903 x = x->die_sib;
6905 while (x && x->die_mark);
6908 /* Sanity checks on DIEs. */
6910 static void
6911 check_die (dw_die_ref die)
6913 unsigned ix;
6914 dw_attr_node *a;
6915 bool inline_found = false;
6916 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6917 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6918 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6920 switch (a->dw_attr)
6922 case DW_AT_inline:
6923 if (a->dw_attr_val.v.val_unsigned)
6924 inline_found = true;
6925 break;
6926 case DW_AT_location:
6927 ++n_location;
6928 break;
6929 case DW_AT_low_pc:
6930 ++n_low_pc;
6931 break;
6932 case DW_AT_high_pc:
6933 ++n_high_pc;
6934 break;
6935 case DW_AT_artificial:
6936 ++n_artificial;
6937 break;
6938 case DW_AT_decl_column:
6939 ++n_decl_column;
6940 break;
6941 case DW_AT_decl_line:
6942 ++n_decl_line;
6943 break;
6944 case DW_AT_decl_file:
6945 ++n_decl_file;
6946 break;
6947 default:
6948 break;
6951 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6952 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6954 fprintf (stderr, "Duplicate attributes in DIE:\n");
6955 debug_dwarf_die (die);
6956 gcc_unreachable ();
6958 if (inline_found)
6960 /* A debugging information entry that is a member of an abstract
6961 instance tree [that has DW_AT_inline] should not contain any
6962 attributes which describe aspects of the subroutine which vary
6963 between distinct inlined expansions or distinct out-of-line
6964 expansions. */
6965 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6966 gcc_assert (a->dw_attr != DW_AT_low_pc
6967 && a->dw_attr != DW_AT_high_pc
6968 && a->dw_attr != DW_AT_location
6969 && a->dw_attr != DW_AT_frame_base
6970 && a->dw_attr != DW_AT_call_all_calls
6971 && a->dw_attr != DW_AT_GNU_all_call_sites);
6975 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6976 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6977 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6979 /* Calculate the checksum of a location expression. */
6981 static inline void
6982 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6984 int tem;
6985 inchash::hash hstate;
6986 hashval_t hash;
6988 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6989 CHECKSUM (tem);
6990 hash_loc_operands (loc, hstate);
6991 hash = hstate.end();
6992 CHECKSUM (hash);
6995 /* Calculate the checksum of an attribute. */
6997 static void
6998 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
7000 dw_loc_descr_ref loc;
7001 rtx r;
7003 CHECKSUM (at->dw_attr);
7005 /* We don't care that this was compiled with a different compiler
7006 snapshot; if the output is the same, that's what matters. */
7007 if (at->dw_attr == DW_AT_producer)
7008 return;
7010 switch (AT_class (at))
7012 case dw_val_class_const:
7013 case dw_val_class_const_implicit:
7014 CHECKSUM (at->dw_attr_val.v.val_int);
7015 break;
7016 case dw_val_class_unsigned_const:
7017 case dw_val_class_unsigned_const_implicit:
7018 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7019 break;
7020 case dw_val_class_const_double:
7021 CHECKSUM (at->dw_attr_val.v.val_double);
7022 break;
7023 case dw_val_class_wide_int:
7024 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7025 get_full_len (*at->dw_attr_val.v.val_wide)
7026 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7027 break;
7028 case dw_val_class_vec:
7029 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7030 (at->dw_attr_val.v.val_vec.length
7031 * at->dw_attr_val.v.val_vec.elt_size));
7032 break;
7033 case dw_val_class_flag:
7034 CHECKSUM (at->dw_attr_val.v.val_flag);
7035 break;
7036 case dw_val_class_str:
7037 CHECKSUM_STRING (AT_string (at));
7038 break;
7040 case dw_val_class_addr:
7041 r = AT_addr (at);
7042 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7043 CHECKSUM_STRING (XSTR (r, 0));
7044 break;
7046 case dw_val_class_offset:
7047 CHECKSUM (at->dw_attr_val.v.val_offset);
7048 break;
7050 case dw_val_class_loc:
7051 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7052 loc_checksum (loc, ctx);
7053 break;
7055 case dw_val_class_die_ref:
7056 die_checksum (AT_ref (at), ctx, mark);
7057 break;
7059 case dw_val_class_fde_ref:
7060 case dw_val_class_vms_delta:
7061 case dw_val_class_symview:
7062 case dw_val_class_lbl_id:
7063 case dw_val_class_lineptr:
7064 case dw_val_class_macptr:
7065 case dw_val_class_loclistsptr:
7066 case dw_val_class_high_pc:
7067 break;
7069 case dw_val_class_file:
7070 case dw_val_class_file_implicit:
7071 CHECKSUM_STRING (AT_file (at)->filename);
7072 break;
7074 case dw_val_class_data8:
7075 CHECKSUM (at->dw_attr_val.v.val_data8);
7076 break;
7078 default:
7079 break;
7083 /* Calculate the checksum of a DIE. */
7085 static void
7086 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7088 dw_die_ref c;
7089 dw_attr_node *a;
7090 unsigned ix;
7092 /* To avoid infinite recursion. */
7093 if (die->die_mark)
7095 CHECKSUM (die->die_mark);
7096 return;
7098 die->die_mark = ++(*mark);
7100 CHECKSUM (die->die_tag);
7102 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7103 attr_checksum (a, ctx, mark);
7105 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7108 #undef CHECKSUM
7109 #undef CHECKSUM_BLOCK
7110 #undef CHECKSUM_STRING
7112 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7113 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7114 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7115 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7116 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7117 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7118 #define CHECKSUM_ATTR(FOO) \
7119 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7121 /* Calculate the checksum of a number in signed LEB128 format. */
7123 static void
7124 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7126 unsigned char byte;
7127 bool more;
7129 while (1)
7131 byte = (value & 0x7f);
7132 value >>= 7;
7133 more = !((value == 0 && (byte & 0x40) == 0)
7134 || (value == -1 && (byte & 0x40) != 0));
7135 if (more)
7136 byte |= 0x80;
7137 CHECKSUM (byte);
7138 if (!more)
7139 break;
7143 /* Calculate the checksum of a number in unsigned LEB128 format. */
7145 static void
7146 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7148 while (1)
7150 unsigned char byte = (value & 0x7f);
7151 value >>= 7;
7152 if (value != 0)
7153 /* More bytes to follow. */
7154 byte |= 0x80;
7155 CHECKSUM (byte);
7156 if (value == 0)
7157 break;
7161 /* Checksum the context of the DIE. This adds the names of any
7162 surrounding namespaces or structures to the checksum. */
7164 static void
7165 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7167 const char *name;
7168 dw_die_ref spec;
7169 int tag = die->die_tag;
7171 if (tag != DW_TAG_namespace
7172 && tag != DW_TAG_structure_type
7173 && tag != DW_TAG_class_type)
7174 return;
7176 name = get_AT_string (die, DW_AT_name);
7178 spec = get_AT_ref (die, DW_AT_specification);
7179 if (spec != NULL)
7180 die = spec;
7182 if (die->die_parent != NULL)
7183 checksum_die_context (die->die_parent, ctx);
7185 CHECKSUM_ULEB128 ('C');
7186 CHECKSUM_ULEB128 (tag);
7187 if (name != NULL)
7188 CHECKSUM_STRING (name);
7191 /* Calculate the checksum of a location expression. */
7193 static inline void
7194 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7196 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7197 were emitted as a DW_FORM_sdata instead of a location expression. */
7198 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7200 CHECKSUM_ULEB128 (DW_FORM_sdata);
7201 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7202 return;
7205 /* Otherwise, just checksum the raw location expression. */
7206 while (loc != NULL)
7208 inchash::hash hstate;
7209 hashval_t hash;
7211 CHECKSUM_ULEB128 (loc->dtprel);
7212 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7213 hash_loc_operands (loc, hstate);
7214 hash = hstate.end ();
7215 CHECKSUM (hash);
7216 loc = loc->dw_loc_next;
7220 /* Calculate the checksum of an attribute. */
7222 static void
7223 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7224 struct md5_ctx *ctx, int *mark)
7226 dw_loc_descr_ref loc;
7227 rtx r;
7229 if (AT_class (at) == dw_val_class_die_ref)
7231 dw_die_ref target_die = AT_ref (at);
7233 /* For pointer and reference types, we checksum only the (qualified)
7234 name of the target type (if there is a name). For friend entries,
7235 we checksum only the (qualified) name of the target type or function.
7236 This allows the checksum to remain the same whether the target type
7237 is complete or not. */
7238 if ((at->dw_attr == DW_AT_type
7239 && (tag == DW_TAG_pointer_type
7240 || tag == DW_TAG_reference_type
7241 || tag == DW_TAG_rvalue_reference_type
7242 || tag == DW_TAG_ptr_to_member_type))
7243 || (at->dw_attr == DW_AT_friend
7244 && tag == DW_TAG_friend))
7246 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7248 if (name_attr != NULL)
7250 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7252 if (decl == NULL)
7253 decl = target_die;
7254 CHECKSUM_ULEB128 ('N');
7255 CHECKSUM_ULEB128 (at->dw_attr);
7256 if (decl->die_parent != NULL)
7257 checksum_die_context (decl->die_parent, ctx);
7258 CHECKSUM_ULEB128 ('E');
7259 CHECKSUM_STRING (AT_string (name_attr));
7260 return;
7264 /* For all other references to another DIE, we check to see if the
7265 target DIE has already been visited. If it has, we emit a
7266 backward reference; if not, we descend recursively. */
7267 if (target_die->die_mark > 0)
7269 CHECKSUM_ULEB128 ('R');
7270 CHECKSUM_ULEB128 (at->dw_attr);
7271 CHECKSUM_ULEB128 (target_die->die_mark);
7273 else
7275 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7277 if (decl == NULL)
7278 decl = target_die;
7279 target_die->die_mark = ++(*mark);
7280 CHECKSUM_ULEB128 ('T');
7281 CHECKSUM_ULEB128 (at->dw_attr);
7282 if (decl->die_parent != NULL)
7283 checksum_die_context (decl->die_parent, ctx);
7284 die_checksum_ordered (target_die, ctx, mark);
7286 return;
7289 CHECKSUM_ULEB128 ('A');
7290 CHECKSUM_ULEB128 (at->dw_attr);
7292 switch (AT_class (at))
7294 case dw_val_class_const:
7295 case dw_val_class_const_implicit:
7296 CHECKSUM_ULEB128 (DW_FORM_sdata);
7297 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7298 break;
7300 case dw_val_class_unsigned_const:
7301 case dw_val_class_unsigned_const_implicit:
7302 CHECKSUM_ULEB128 (DW_FORM_sdata);
7303 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7304 break;
7306 case dw_val_class_const_double:
7307 CHECKSUM_ULEB128 (DW_FORM_block);
7308 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7309 CHECKSUM (at->dw_attr_val.v.val_double);
7310 break;
7312 case dw_val_class_wide_int:
7313 CHECKSUM_ULEB128 (DW_FORM_block);
7314 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7315 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7316 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7317 get_full_len (*at->dw_attr_val.v.val_wide)
7318 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7319 break;
7321 case dw_val_class_vec:
7322 CHECKSUM_ULEB128 (DW_FORM_block);
7323 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7324 * at->dw_attr_val.v.val_vec.elt_size);
7325 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7326 (at->dw_attr_val.v.val_vec.length
7327 * at->dw_attr_val.v.val_vec.elt_size));
7328 break;
7330 case dw_val_class_flag:
7331 CHECKSUM_ULEB128 (DW_FORM_flag);
7332 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7333 break;
7335 case dw_val_class_str:
7336 CHECKSUM_ULEB128 (DW_FORM_string);
7337 CHECKSUM_STRING (AT_string (at));
7338 break;
7340 case dw_val_class_addr:
7341 r = AT_addr (at);
7342 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7343 CHECKSUM_ULEB128 (DW_FORM_string);
7344 CHECKSUM_STRING (XSTR (r, 0));
7345 break;
7347 case dw_val_class_offset:
7348 CHECKSUM_ULEB128 (DW_FORM_sdata);
7349 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7350 break;
7352 case dw_val_class_loc:
7353 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7354 loc_checksum_ordered (loc, ctx);
7355 break;
7357 case dw_val_class_fde_ref:
7358 case dw_val_class_symview:
7359 case dw_val_class_lbl_id:
7360 case dw_val_class_lineptr:
7361 case dw_val_class_macptr:
7362 case dw_val_class_loclistsptr:
7363 case dw_val_class_high_pc:
7364 break;
7366 case dw_val_class_file:
7367 case dw_val_class_file_implicit:
7368 CHECKSUM_ULEB128 (DW_FORM_string);
7369 CHECKSUM_STRING (AT_file (at)->filename);
7370 break;
7372 case dw_val_class_data8:
7373 CHECKSUM (at->dw_attr_val.v.val_data8);
7374 break;
7376 default:
7377 break;
7381 struct checksum_attributes
7383 dw_attr_node *at_name;
7384 dw_attr_node *at_type;
7385 dw_attr_node *at_friend;
7386 dw_attr_node *at_accessibility;
7387 dw_attr_node *at_address_class;
7388 dw_attr_node *at_alignment;
7389 dw_attr_node *at_allocated;
7390 dw_attr_node *at_artificial;
7391 dw_attr_node *at_associated;
7392 dw_attr_node *at_binary_scale;
7393 dw_attr_node *at_bit_offset;
7394 dw_attr_node *at_bit_size;
7395 dw_attr_node *at_bit_stride;
7396 dw_attr_node *at_byte_size;
7397 dw_attr_node *at_byte_stride;
7398 dw_attr_node *at_const_value;
7399 dw_attr_node *at_containing_type;
7400 dw_attr_node *at_count;
7401 dw_attr_node *at_data_location;
7402 dw_attr_node *at_data_member_location;
7403 dw_attr_node *at_decimal_scale;
7404 dw_attr_node *at_decimal_sign;
7405 dw_attr_node *at_default_value;
7406 dw_attr_node *at_digit_count;
7407 dw_attr_node *at_discr;
7408 dw_attr_node *at_discr_list;
7409 dw_attr_node *at_discr_value;
7410 dw_attr_node *at_encoding;
7411 dw_attr_node *at_endianity;
7412 dw_attr_node *at_explicit;
7413 dw_attr_node *at_is_optional;
7414 dw_attr_node *at_location;
7415 dw_attr_node *at_lower_bound;
7416 dw_attr_node *at_mutable;
7417 dw_attr_node *at_ordering;
7418 dw_attr_node *at_picture_string;
7419 dw_attr_node *at_prototyped;
7420 dw_attr_node *at_small;
7421 dw_attr_node *at_segment;
7422 dw_attr_node *at_string_length;
7423 dw_attr_node *at_string_length_bit_size;
7424 dw_attr_node *at_string_length_byte_size;
7425 dw_attr_node *at_threads_scaled;
7426 dw_attr_node *at_upper_bound;
7427 dw_attr_node *at_use_location;
7428 dw_attr_node *at_use_UTF8;
7429 dw_attr_node *at_variable_parameter;
7430 dw_attr_node *at_virtuality;
7431 dw_attr_node *at_visibility;
7432 dw_attr_node *at_vtable_elem_location;
7435 /* Collect the attributes that we will want to use for the checksum. */
7437 static void
7438 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7440 dw_attr_node *a;
7441 unsigned ix;
7443 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7445 switch (a->dw_attr)
7447 case DW_AT_name:
7448 attrs->at_name = a;
7449 break;
7450 case DW_AT_type:
7451 attrs->at_type = a;
7452 break;
7453 case DW_AT_friend:
7454 attrs->at_friend = a;
7455 break;
7456 case DW_AT_accessibility:
7457 attrs->at_accessibility = a;
7458 break;
7459 case DW_AT_address_class:
7460 attrs->at_address_class = a;
7461 break;
7462 case DW_AT_alignment:
7463 attrs->at_alignment = a;
7464 break;
7465 case DW_AT_allocated:
7466 attrs->at_allocated = a;
7467 break;
7468 case DW_AT_artificial:
7469 attrs->at_artificial = a;
7470 break;
7471 case DW_AT_associated:
7472 attrs->at_associated = a;
7473 break;
7474 case DW_AT_binary_scale:
7475 attrs->at_binary_scale = a;
7476 break;
7477 case DW_AT_bit_offset:
7478 attrs->at_bit_offset = a;
7479 break;
7480 case DW_AT_bit_size:
7481 attrs->at_bit_size = a;
7482 break;
7483 case DW_AT_bit_stride:
7484 attrs->at_bit_stride = a;
7485 break;
7486 case DW_AT_byte_size:
7487 attrs->at_byte_size = a;
7488 break;
7489 case DW_AT_byte_stride:
7490 attrs->at_byte_stride = a;
7491 break;
7492 case DW_AT_const_value:
7493 attrs->at_const_value = a;
7494 break;
7495 case DW_AT_containing_type:
7496 attrs->at_containing_type = a;
7497 break;
7498 case DW_AT_count:
7499 attrs->at_count = a;
7500 break;
7501 case DW_AT_data_location:
7502 attrs->at_data_location = a;
7503 break;
7504 case DW_AT_data_member_location:
7505 attrs->at_data_member_location = a;
7506 break;
7507 case DW_AT_decimal_scale:
7508 attrs->at_decimal_scale = a;
7509 break;
7510 case DW_AT_decimal_sign:
7511 attrs->at_decimal_sign = a;
7512 break;
7513 case DW_AT_default_value:
7514 attrs->at_default_value = a;
7515 break;
7516 case DW_AT_digit_count:
7517 attrs->at_digit_count = a;
7518 break;
7519 case DW_AT_discr:
7520 attrs->at_discr = a;
7521 break;
7522 case DW_AT_discr_list:
7523 attrs->at_discr_list = a;
7524 break;
7525 case DW_AT_discr_value:
7526 attrs->at_discr_value = a;
7527 break;
7528 case DW_AT_encoding:
7529 attrs->at_encoding = a;
7530 break;
7531 case DW_AT_endianity:
7532 attrs->at_endianity = a;
7533 break;
7534 case DW_AT_explicit:
7535 attrs->at_explicit = a;
7536 break;
7537 case DW_AT_is_optional:
7538 attrs->at_is_optional = a;
7539 break;
7540 case DW_AT_location:
7541 attrs->at_location = a;
7542 break;
7543 case DW_AT_lower_bound:
7544 attrs->at_lower_bound = a;
7545 break;
7546 case DW_AT_mutable:
7547 attrs->at_mutable = a;
7548 break;
7549 case DW_AT_ordering:
7550 attrs->at_ordering = a;
7551 break;
7552 case DW_AT_picture_string:
7553 attrs->at_picture_string = a;
7554 break;
7555 case DW_AT_prototyped:
7556 attrs->at_prototyped = a;
7557 break;
7558 case DW_AT_small:
7559 attrs->at_small = a;
7560 break;
7561 case DW_AT_segment:
7562 attrs->at_segment = a;
7563 break;
7564 case DW_AT_string_length:
7565 attrs->at_string_length = a;
7566 break;
7567 case DW_AT_string_length_bit_size:
7568 attrs->at_string_length_bit_size = a;
7569 break;
7570 case DW_AT_string_length_byte_size:
7571 attrs->at_string_length_byte_size = a;
7572 break;
7573 case DW_AT_threads_scaled:
7574 attrs->at_threads_scaled = a;
7575 break;
7576 case DW_AT_upper_bound:
7577 attrs->at_upper_bound = a;
7578 break;
7579 case DW_AT_use_location:
7580 attrs->at_use_location = a;
7581 break;
7582 case DW_AT_use_UTF8:
7583 attrs->at_use_UTF8 = a;
7584 break;
7585 case DW_AT_variable_parameter:
7586 attrs->at_variable_parameter = a;
7587 break;
7588 case DW_AT_virtuality:
7589 attrs->at_virtuality = a;
7590 break;
7591 case DW_AT_visibility:
7592 attrs->at_visibility = a;
7593 break;
7594 case DW_AT_vtable_elem_location:
7595 attrs->at_vtable_elem_location = a;
7596 break;
7597 default:
7598 break;
7603 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7605 static void
7606 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7608 dw_die_ref c;
7609 dw_die_ref decl;
7610 struct checksum_attributes attrs;
7612 CHECKSUM_ULEB128 ('D');
7613 CHECKSUM_ULEB128 (die->die_tag);
7615 memset (&attrs, 0, sizeof (attrs));
7617 decl = get_AT_ref (die, DW_AT_specification);
7618 if (decl != NULL)
7619 collect_checksum_attributes (&attrs, decl);
7620 collect_checksum_attributes (&attrs, die);
7622 CHECKSUM_ATTR (attrs.at_name);
7623 CHECKSUM_ATTR (attrs.at_accessibility);
7624 CHECKSUM_ATTR (attrs.at_address_class);
7625 CHECKSUM_ATTR (attrs.at_allocated);
7626 CHECKSUM_ATTR (attrs.at_artificial);
7627 CHECKSUM_ATTR (attrs.at_associated);
7628 CHECKSUM_ATTR (attrs.at_binary_scale);
7629 CHECKSUM_ATTR (attrs.at_bit_offset);
7630 CHECKSUM_ATTR (attrs.at_bit_size);
7631 CHECKSUM_ATTR (attrs.at_bit_stride);
7632 CHECKSUM_ATTR (attrs.at_byte_size);
7633 CHECKSUM_ATTR (attrs.at_byte_stride);
7634 CHECKSUM_ATTR (attrs.at_const_value);
7635 CHECKSUM_ATTR (attrs.at_containing_type);
7636 CHECKSUM_ATTR (attrs.at_count);
7637 CHECKSUM_ATTR (attrs.at_data_location);
7638 CHECKSUM_ATTR (attrs.at_data_member_location);
7639 CHECKSUM_ATTR (attrs.at_decimal_scale);
7640 CHECKSUM_ATTR (attrs.at_decimal_sign);
7641 CHECKSUM_ATTR (attrs.at_default_value);
7642 CHECKSUM_ATTR (attrs.at_digit_count);
7643 CHECKSUM_ATTR (attrs.at_discr);
7644 CHECKSUM_ATTR (attrs.at_discr_list);
7645 CHECKSUM_ATTR (attrs.at_discr_value);
7646 CHECKSUM_ATTR (attrs.at_encoding);
7647 CHECKSUM_ATTR (attrs.at_endianity);
7648 CHECKSUM_ATTR (attrs.at_explicit);
7649 CHECKSUM_ATTR (attrs.at_is_optional);
7650 CHECKSUM_ATTR (attrs.at_location);
7651 CHECKSUM_ATTR (attrs.at_lower_bound);
7652 CHECKSUM_ATTR (attrs.at_mutable);
7653 CHECKSUM_ATTR (attrs.at_ordering);
7654 CHECKSUM_ATTR (attrs.at_picture_string);
7655 CHECKSUM_ATTR (attrs.at_prototyped);
7656 CHECKSUM_ATTR (attrs.at_small);
7657 CHECKSUM_ATTR (attrs.at_segment);
7658 CHECKSUM_ATTR (attrs.at_string_length);
7659 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7660 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7661 CHECKSUM_ATTR (attrs.at_threads_scaled);
7662 CHECKSUM_ATTR (attrs.at_upper_bound);
7663 CHECKSUM_ATTR (attrs.at_use_location);
7664 CHECKSUM_ATTR (attrs.at_use_UTF8);
7665 CHECKSUM_ATTR (attrs.at_variable_parameter);
7666 CHECKSUM_ATTR (attrs.at_virtuality);
7667 CHECKSUM_ATTR (attrs.at_visibility);
7668 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7669 CHECKSUM_ATTR (attrs.at_type);
7670 CHECKSUM_ATTR (attrs.at_friend);
7671 CHECKSUM_ATTR (attrs.at_alignment);
7673 /* Checksum the child DIEs. */
7674 c = die->die_child;
7675 if (c) do {
7676 dw_attr_node *name_attr;
7678 c = c->die_sib;
7679 name_attr = get_AT (c, DW_AT_name);
7680 if (is_template_instantiation (c))
7682 /* Ignore instantiations of member type and function templates. */
7684 else if (name_attr != NULL
7685 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7687 /* Use a shallow checksum for named nested types and member
7688 functions. */
7689 CHECKSUM_ULEB128 ('S');
7690 CHECKSUM_ULEB128 (c->die_tag);
7691 CHECKSUM_STRING (AT_string (name_attr));
7693 else
7695 /* Use a deep checksum for other children. */
7696 /* Mark this DIE so it gets processed when unmarking. */
7697 if (c->die_mark == 0)
7698 c->die_mark = -1;
7699 die_checksum_ordered (c, ctx, mark);
7701 } while (c != die->die_child);
7703 CHECKSUM_ULEB128 (0);
7706 /* Add a type name and tag to a hash. */
7707 static void
7708 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7710 CHECKSUM_ULEB128 (tag);
7711 CHECKSUM_STRING (name);
7714 #undef CHECKSUM
7715 #undef CHECKSUM_STRING
7716 #undef CHECKSUM_ATTR
7717 #undef CHECKSUM_LEB128
7718 #undef CHECKSUM_ULEB128
7720 /* Generate the type signature for DIE. This is computed by generating an
7721 MD5 checksum over the DIE's tag, its relevant attributes, and its
7722 children. Attributes that are references to other DIEs are processed
7723 by recursion, using the MARK field to prevent infinite recursion.
7724 If the DIE is nested inside a namespace or another type, we also
7725 need to include that context in the signature. The lower 64 bits
7726 of the resulting MD5 checksum comprise the signature. */
7728 static void
7729 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7731 int mark;
7732 const char *name;
7733 unsigned char checksum[16];
7734 struct md5_ctx ctx;
7735 dw_die_ref decl;
7736 dw_die_ref parent;
7738 name = get_AT_string (die, DW_AT_name);
7739 decl = get_AT_ref (die, DW_AT_specification);
7740 parent = get_die_parent (die);
7742 /* First, compute a signature for just the type name (and its surrounding
7743 context, if any. This is stored in the type unit DIE for link-time
7744 ODR (one-definition rule) checking. */
7746 if (is_cxx () && name != NULL)
7748 md5_init_ctx (&ctx);
7750 /* Checksum the names of surrounding namespaces and structures. */
7751 if (parent != NULL)
7752 checksum_die_context (parent, &ctx);
7754 /* Checksum the current DIE. */
7755 die_odr_checksum (die->die_tag, name, &ctx);
7756 md5_finish_ctx (&ctx, checksum);
7758 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7761 /* Next, compute the complete type signature. */
7763 md5_init_ctx (&ctx);
7764 mark = 1;
7765 die->die_mark = mark;
7767 /* Checksum the names of surrounding namespaces and structures. */
7768 if (parent != NULL)
7769 checksum_die_context (parent, &ctx);
7771 /* Checksum the DIE and its children. */
7772 die_checksum_ordered (die, &ctx, &mark);
7773 unmark_all_dies (die);
7774 md5_finish_ctx (&ctx, checksum);
7776 /* Store the signature in the type node and link the type DIE and the
7777 type node together. */
7778 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7779 DWARF_TYPE_SIGNATURE_SIZE);
7780 die->comdat_type_p = true;
7781 die->die_id.die_type_node = type_node;
7782 type_node->type_die = die;
7784 /* If the DIE is a specification, link its declaration to the type node
7785 as well. */
7786 if (decl != NULL)
7788 decl->comdat_type_p = true;
7789 decl->die_id.die_type_node = type_node;
7793 /* Do the location expressions look same? */
7794 static inline int
7795 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7797 return loc1->dw_loc_opc == loc2->dw_loc_opc
7798 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7799 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7802 /* Do the values look the same? */
7803 static int
7804 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7806 dw_loc_descr_ref loc1, loc2;
7807 rtx r1, r2;
7809 if (v1->val_class != v2->val_class)
7810 return 0;
7812 switch (v1->val_class)
7814 case dw_val_class_const:
7815 case dw_val_class_const_implicit:
7816 return v1->v.val_int == v2->v.val_int;
7817 case dw_val_class_unsigned_const:
7818 case dw_val_class_unsigned_const_implicit:
7819 return v1->v.val_unsigned == v2->v.val_unsigned;
7820 case dw_val_class_const_double:
7821 return v1->v.val_double.high == v2->v.val_double.high
7822 && v1->v.val_double.low == v2->v.val_double.low;
7823 case dw_val_class_wide_int:
7824 return *v1->v.val_wide == *v2->v.val_wide;
7825 case dw_val_class_vec:
7826 if (v1->v.val_vec.length != v2->v.val_vec.length
7827 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7828 return 0;
7829 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7830 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7831 return 0;
7832 return 1;
7833 case dw_val_class_flag:
7834 return v1->v.val_flag == v2->v.val_flag;
7835 case dw_val_class_str:
7836 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7838 case dw_val_class_addr:
7839 r1 = v1->v.val_addr;
7840 r2 = v2->v.val_addr;
7841 if (GET_CODE (r1) != GET_CODE (r2))
7842 return 0;
7843 return !rtx_equal_p (r1, r2);
7845 case dw_val_class_offset:
7846 return v1->v.val_offset == v2->v.val_offset;
7848 case dw_val_class_loc:
7849 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7850 loc1 && loc2;
7851 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7852 if (!same_loc_p (loc1, loc2, mark))
7853 return 0;
7854 return !loc1 && !loc2;
7856 case dw_val_class_die_ref:
7857 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7859 case dw_val_class_symview:
7860 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7862 case dw_val_class_fde_ref:
7863 case dw_val_class_vms_delta:
7864 case dw_val_class_lbl_id:
7865 case dw_val_class_lineptr:
7866 case dw_val_class_macptr:
7867 case dw_val_class_loclistsptr:
7868 case dw_val_class_high_pc:
7869 return 1;
7871 case dw_val_class_file:
7872 case dw_val_class_file_implicit:
7873 return v1->v.val_file == v2->v.val_file;
7875 case dw_val_class_data8:
7876 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7878 default:
7879 return 1;
7883 /* Do the attributes look the same? */
7885 static int
7886 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7888 if (at1->dw_attr != at2->dw_attr)
7889 return 0;
7891 /* We don't care that this was compiled with a different compiler
7892 snapshot; if the output is the same, that's what matters. */
7893 if (at1->dw_attr == DW_AT_producer)
7894 return 1;
7896 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7899 /* Do the dies look the same? */
7901 static int
7902 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7904 dw_die_ref c1, c2;
7905 dw_attr_node *a1;
7906 unsigned ix;
7908 /* To avoid infinite recursion. */
7909 if (die1->die_mark)
7910 return die1->die_mark == die2->die_mark;
7911 die1->die_mark = die2->die_mark = ++(*mark);
7913 if (die1->die_tag != die2->die_tag)
7914 return 0;
7916 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7917 return 0;
7919 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7920 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7921 return 0;
7923 c1 = die1->die_child;
7924 c2 = die2->die_child;
7925 if (! c1)
7927 if (c2)
7928 return 0;
7930 else
7931 for (;;)
7933 if (!same_die_p (c1, c2, mark))
7934 return 0;
7935 c1 = c1->die_sib;
7936 c2 = c2->die_sib;
7937 if (c1 == die1->die_child)
7939 if (c2 == die2->die_child)
7940 break;
7941 else
7942 return 0;
7946 return 1;
7949 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7950 children, and set die_symbol. */
7952 static void
7953 compute_comp_unit_symbol (dw_die_ref unit_die)
7955 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7956 const char *base = die_name ? lbasename (die_name) : "anonymous";
7957 char *name = XALLOCAVEC (char, strlen (base) + 64);
7958 char *p;
7959 int i, mark;
7960 unsigned char checksum[16];
7961 struct md5_ctx ctx;
7963 /* Compute the checksum of the DIE, then append part of it as hex digits to
7964 the name filename of the unit. */
7966 md5_init_ctx (&ctx);
7967 mark = 0;
7968 die_checksum (unit_die, &ctx, &mark);
7969 unmark_all_dies (unit_die);
7970 md5_finish_ctx (&ctx, checksum);
7972 /* When we this for comp_unit_die () we have a DW_AT_name that might
7973 not start with a letter but with anything valid for filenames and
7974 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7975 character is not a letter. */
7976 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7977 clean_symbol_name (name);
7979 p = name + strlen (name);
7980 for (i = 0; i < 4; i++)
7982 sprintf (p, "%.2x", checksum[i]);
7983 p += 2;
7986 unit_die->die_id.die_symbol = xstrdup (name);
7989 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7991 static int
7992 is_type_die (dw_die_ref die)
7994 switch (die->die_tag)
7996 case DW_TAG_array_type:
7997 case DW_TAG_class_type:
7998 case DW_TAG_interface_type:
7999 case DW_TAG_enumeration_type:
8000 case DW_TAG_pointer_type:
8001 case DW_TAG_reference_type:
8002 case DW_TAG_rvalue_reference_type:
8003 case DW_TAG_string_type:
8004 case DW_TAG_structure_type:
8005 case DW_TAG_subroutine_type:
8006 case DW_TAG_union_type:
8007 case DW_TAG_ptr_to_member_type:
8008 case DW_TAG_set_type:
8009 case DW_TAG_subrange_type:
8010 case DW_TAG_base_type:
8011 case DW_TAG_const_type:
8012 case DW_TAG_file_type:
8013 case DW_TAG_packed_type:
8014 case DW_TAG_volatile_type:
8015 case DW_TAG_typedef:
8016 return 1;
8017 default:
8018 return 0;
8022 /* Returns true iff C is a compile-unit DIE. */
8024 static inline bool
8025 is_cu_die (dw_die_ref c)
8027 return c && (c->die_tag == DW_TAG_compile_unit
8028 || c->die_tag == DW_TAG_skeleton_unit);
8031 /* Returns true iff C is a unit DIE of some sort. */
8033 static inline bool
8034 is_unit_die (dw_die_ref c)
8036 return c && (c->die_tag == DW_TAG_compile_unit
8037 || c->die_tag == DW_TAG_partial_unit
8038 || c->die_tag == DW_TAG_type_unit
8039 || c->die_tag == DW_TAG_skeleton_unit);
8042 /* Returns true iff C is a namespace DIE. */
8044 static inline bool
8045 is_namespace_die (dw_die_ref c)
8047 return c && c->die_tag == DW_TAG_namespace;
8050 /* Return non-zero if this DIE is a template parameter. */
8052 static inline bool
8053 is_template_parameter (dw_die_ref die)
8055 switch (die->die_tag)
8057 case DW_TAG_template_type_param:
8058 case DW_TAG_template_value_param:
8059 case DW_TAG_GNU_template_template_param:
8060 case DW_TAG_GNU_template_parameter_pack:
8061 return true;
8062 default:
8063 return false;
8067 /* Return non-zero if this DIE represents a template instantiation. */
8069 static inline bool
8070 is_template_instantiation (dw_die_ref die)
8072 dw_die_ref c;
8074 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8075 return false;
8076 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8077 return false;
8080 static char *
8081 gen_internal_sym (const char *prefix)
8083 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8085 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8086 return xstrdup (buf);
8089 /* Return non-zero if this DIE is a declaration. */
8091 static int
8092 is_declaration_die (dw_die_ref die)
8094 dw_attr_node *a;
8095 unsigned ix;
8097 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8098 if (a->dw_attr == DW_AT_declaration)
8099 return 1;
8101 return 0;
8104 /* Return non-zero if this DIE is nested inside a subprogram. */
8106 static int
8107 is_nested_in_subprogram (dw_die_ref die)
8109 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8111 if (decl == NULL)
8112 decl = die;
8113 return local_scope_p (decl);
8116 /* Return non-zero if this DIE contains a defining declaration of a
8117 subprogram. */
8119 static int
8120 contains_subprogram_definition (dw_die_ref die)
8122 dw_die_ref c;
8124 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8125 return 1;
8126 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8127 return 0;
8130 /* Return non-zero if this is a type DIE that should be moved to a
8131 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8132 unit type. */
8134 static int
8135 should_move_die_to_comdat (dw_die_ref die)
8137 switch (die->die_tag)
8139 case DW_TAG_class_type:
8140 case DW_TAG_structure_type:
8141 case DW_TAG_enumeration_type:
8142 case DW_TAG_union_type:
8143 /* Don't move declarations, inlined instances, types nested in a
8144 subprogram, or types that contain subprogram definitions. */
8145 if (is_declaration_die (die)
8146 || get_AT (die, DW_AT_abstract_origin)
8147 || is_nested_in_subprogram (die)
8148 || contains_subprogram_definition (die))
8149 return 0;
8150 return 1;
8151 case DW_TAG_array_type:
8152 case DW_TAG_interface_type:
8153 case DW_TAG_pointer_type:
8154 case DW_TAG_reference_type:
8155 case DW_TAG_rvalue_reference_type:
8156 case DW_TAG_string_type:
8157 case DW_TAG_subroutine_type:
8158 case DW_TAG_ptr_to_member_type:
8159 case DW_TAG_set_type:
8160 case DW_TAG_subrange_type:
8161 case DW_TAG_base_type:
8162 case DW_TAG_const_type:
8163 case DW_TAG_file_type:
8164 case DW_TAG_packed_type:
8165 case DW_TAG_volatile_type:
8166 case DW_TAG_typedef:
8167 default:
8168 return 0;
8172 /* Make a clone of DIE. */
8174 static dw_die_ref
8175 clone_die (dw_die_ref die)
8177 dw_die_ref clone = new_die_raw (die->die_tag);
8178 dw_attr_node *a;
8179 unsigned ix;
8181 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8182 add_dwarf_attr (clone, a);
8184 return clone;
8187 /* Make a clone of the tree rooted at DIE. */
8189 static dw_die_ref
8190 clone_tree (dw_die_ref die)
8192 dw_die_ref c;
8193 dw_die_ref clone = clone_die (die);
8195 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8197 return clone;
8200 /* Make a clone of DIE as a declaration. */
8202 static dw_die_ref
8203 clone_as_declaration (dw_die_ref die)
8205 dw_die_ref clone;
8206 dw_die_ref decl;
8207 dw_attr_node *a;
8208 unsigned ix;
8210 /* If the DIE is already a declaration, just clone it. */
8211 if (is_declaration_die (die))
8212 return clone_die (die);
8214 /* If the DIE is a specification, just clone its declaration DIE. */
8215 decl = get_AT_ref (die, DW_AT_specification);
8216 if (decl != NULL)
8218 clone = clone_die (decl);
8219 if (die->comdat_type_p)
8220 add_AT_die_ref (clone, DW_AT_signature, die);
8221 return clone;
8224 clone = new_die_raw (die->die_tag);
8226 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8228 /* We don't want to copy over all attributes.
8229 For example we don't want DW_AT_byte_size because otherwise we will no
8230 longer have a declaration and GDB will treat it as a definition. */
8232 switch (a->dw_attr)
8234 case DW_AT_abstract_origin:
8235 case DW_AT_artificial:
8236 case DW_AT_containing_type:
8237 case DW_AT_external:
8238 case DW_AT_name:
8239 case DW_AT_type:
8240 case DW_AT_virtuality:
8241 case DW_AT_linkage_name:
8242 case DW_AT_MIPS_linkage_name:
8243 add_dwarf_attr (clone, a);
8244 break;
8245 case DW_AT_byte_size:
8246 case DW_AT_alignment:
8247 default:
8248 break;
8252 if (die->comdat_type_p)
8253 add_AT_die_ref (clone, DW_AT_signature, die);
8255 add_AT_flag (clone, DW_AT_declaration, 1);
8256 return clone;
8260 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8262 struct decl_table_entry
8264 dw_die_ref orig;
8265 dw_die_ref copy;
8268 /* Helpers to manipulate hash table of copied declarations. */
8270 /* Hashtable helpers. */
8272 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8274 typedef die_struct *compare_type;
8275 static inline hashval_t hash (const decl_table_entry *);
8276 static inline bool equal (const decl_table_entry *, const die_struct *);
8279 inline hashval_t
8280 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8282 return htab_hash_pointer (entry->orig);
8285 inline bool
8286 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8287 const die_struct *entry2)
8289 return entry1->orig == entry2;
8292 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8294 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8295 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8296 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8297 to check if the ancestor has already been copied into UNIT. */
8299 static dw_die_ref
8300 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8301 decl_hash_type *decl_table)
8303 dw_die_ref parent = die->die_parent;
8304 dw_die_ref new_parent = unit;
8305 dw_die_ref copy;
8306 decl_table_entry **slot = NULL;
8307 struct decl_table_entry *entry = NULL;
8309 /* If DIE refers to a stub unfold that so we get the appropriate
8310 DIE registered as orig in decl_table. */
8311 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8312 die = c;
8314 if (decl_table)
8316 /* Check if the entry has already been copied to UNIT. */
8317 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8318 INSERT);
8319 if (*slot != HTAB_EMPTY_ENTRY)
8321 entry = *slot;
8322 return entry->copy;
8325 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8326 entry = XCNEW (struct decl_table_entry);
8327 entry->orig = die;
8328 entry->copy = NULL;
8329 *slot = entry;
8332 if (parent != NULL)
8334 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8335 if (spec != NULL)
8336 parent = spec;
8337 if (!is_unit_die (parent))
8338 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8341 copy = clone_as_declaration (die);
8342 add_child_die (new_parent, copy);
8344 if (decl_table)
8346 /* Record the pointer to the copy. */
8347 entry->copy = copy;
8350 return copy;
8352 /* Copy the declaration context to the new type unit DIE. This includes
8353 any surrounding namespace or type declarations. If the DIE has an
8354 AT_specification attribute, it also includes attributes and children
8355 attached to the specification, and returns a pointer to the original
8356 parent of the declaration DIE. Returns NULL otherwise. */
8358 static dw_die_ref
8359 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8361 dw_die_ref decl;
8362 dw_die_ref new_decl;
8363 dw_die_ref orig_parent = NULL;
8365 decl = get_AT_ref (die, DW_AT_specification);
8366 if (decl == NULL)
8367 decl = die;
8368 else
8370 unsigned ix;
8371 dw_die_ref c;
8372 dw_attr_node *a;
8374 /* The original DIE will be changed to a declaration, and must
8375 be moved to be a child of the original declaration DIE. */
8376 orig_parent = decl->die_parent;
8378 /* Copy the type node pointer from the new DIE to the original
8379 declaration DIE so we can forward references later. */
8380 decl->comdat_type_p = true;
8381 decl->die_id.die_type_node = die->die_id.die_type_node;
8383 remove_AT (die, DW_AT_specification);
8385 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8387 if (a->dw_attr != DW_AT_name
8388 && a->dw_attr != DW_AT_declaration
8389 && a->dw_attr != DW_AT_external)
8390 add_dwarf_attr (die, a);
8393 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8396 if (decl->die_parent != NULL
8397 && !is_unit_die (decl->die_parent))
8399 new_decl = copy_ancestor_tree (unit, decl, NULL);
8400 if (new_decl != NULL)
8402 remove_AT (new_decl, DW_AT_signature);
8403 add_AT_specification (die, new_decl);
8407 return orig_parent;
8410 /* Generate the skeleton ancestor tree for the given NODE, then clone
8411 the DIE and add the clone into the tree. */
8413 static void
8414 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8416 if (node->new_die != NULL)
8417 return;
8419 node->new_die = clone_as_declaration (node->old_die);
8421 if (node->parent != NULL)
8423 generate_skeleton_ancestor_tree (node->parent);
8424 add_child_die (node->parent->new_die, node->new_die);
8428 /* Generate a skeleton tree of DIEs containing any declarations that are
8429 found in the original tree. We traverse the tree looking for declaration
8430 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8432 static void
8433 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8435 skeleton_chain_node node;
8436 dw_die_ref c;
8437 dw_die_ref first;
8438 dw_die_ref prev = NULL;
8439 dw_die_ref next = NULL;
8441 node.parent = parent;
8443 first = c = parent->old_die->die_child;
8444 if (c)
8445 next = c->die_sib;
8446 if (c) do {
8447 if (prev == NULL || prev->die_sib == c)
8448 prev = c;
8449 c = next;
8450 next = (c == first ? NULL : c->die_sib);
8451 node.old_die = c;
8452 node.new_die = NULL;
8453 if (is_declaration_die (c))
8455 if (is_template_instantiation (c))
8457 /* Instantiated templates do not need to be cloned into the
8458 type unit. Just move the DIE and its children back to
8459 the skeleton tree (in the main CU). */
8460 remove_child_with_prev (c, prev);
8461 add_child_die (parent->new_die, c);
8462 c = prev;
8464 else if (c->comdat_type_p)
8466 /* This is the skeleton of earlier break_out_comdat_types
8467 type. Clone the existing DIE, but keep the children
8468 under the original (which is in the main CU). */
8469 dw_die_ref clone = clone_die (c);
8471 replace_child (c, clone, prev);
8472 generate_skeleton_ancestor_tree (parent);
8473 add_child_die (parent->new_die, c);
8474 c = clone;
8475 continue;
8477 else
8479 /* Clone the existing DIE, move the original to the skeleton
8480 tree (which is in the main CU), and put the clone, with
8481 all the original's children, where the original came from
8482 (which is about to be moved to the type unit). */
8483 dw_die_ref clone = clone_die (c);
8484 move_all_children (c, clone);
8486 /* If the original has a DW_AT_object_pointer attribute,
8487 it would now point to a child DIE just moved to the
8488 cloned tree, so we need to remove that attribute from
8489 the original. */
8490 remove_AT (c, DW_AT_object_pointer);
8492 replace_child (c, clone, prev);
8493 generate_skeleton_ancestor_tree (parent);
8494 add_child_die (parent->new_die, c);
8495 node.old_die = clone;
8496 node.new_die = c;
8497 c = clone;
8500 generate_skeleton_bottom_up (&node);
8501 } while (next != NULL);
8504 /* Wrapper function for generate_skeleton_bottom_up. */
8506 static dw_die_ref
8507 generate_skeleton (dw_die_ref die)
8509 skeleton_chain_node node;
8511 node.old_die = die;
8512 node.new_die = NULL;
8513 node.parent = NULL;
8515 /* If this type definition is nested inside another type,
8516 and is not an instantiation of a template, always leave
8517 at least a declaration in its place. */
8518 if (die->die_parent != NULL
8519 && is_type_die (die->die_parent)
8520 && !is_template_instantiation (die))
8521 node.new_die = clone_as_declaration (die);
8523 generate_skeleton_bottom_up (&node);
8524 return node.new_die;
8527 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8528 declaration. The original DIE is moved to a new compile unit so that
8529 existing references to it follow it to the new location. If any of the
8530 original DIE's descendants is a declaration, we need to replace the
8531 original DIE with a skeleton tree and move the declarations back into the
8532 skeleton tree. */
8534 static dw_die_ref
8535 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8536 dw_die_ref prev)
8538 dw_die_ref skeleton, orig_parent;
8540 /* Copy the declaration context to the type unit DIE. If the returned
8541 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8542 that DIE. */
8543 orig_parent = copy_declaration_context (unit, child);
8545 skeleton = generate_skeleton (child);
8546 if (skeleton == NULL)
8547 remove_child_with_prev (child, prev);
8548 else
8550 skeleton->comdat_type_p = true;
8551 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8553 /* If the original DIE was a specification, we need to put
8554 the skeleton under the parent DIE of the declaration.
8555 This leaves the original declaration in the tree, but
8556 it will be pruned later since there are no longer any
8557 references to it. */
8558 if (orig_parent != NULL)
8560 remove_child_with_prev (child, prev);
8561 add_child_die (orig_parent, skeleton);
8563 else
8564 replace_child (child, skeleton, prev);
8567 return skeleton;
8570 static void
8571 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8572 comdat_type_node *type_node,
8573 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8575 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8576 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8577 DWARF procedure references in the DW_AT_location attribute. */
8579 static dw_die_ref
8580 copy_dwarf_procedure (dw_die_ref die,
8581 comdat_type_node *type_node,
8582 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8584 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8586 /* DWARF procedures are not supposed to have children... */
8587 gcc_assert (die->die_child == NULL);
8589 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8590 gcc_assert (vec_safe_length (die->die_attr) == 1
8591 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8593 /* Do not copy more than once DWARF procedures. */
8594 bool existed;
8595 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8596 if (existed)
8597 return die_copy;
8599 die_copy = clone_die (die);
8600 add_child_die (type_node->root_die, die_copy);
8601 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8602 return die_copy;
8605 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8606 procedures in DIE's attributes. */
8608 static void
8609 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8610 comdat_type_node *type_node,
8611 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8613 dw_attr_node *a;
8614 unsigned i;
8616 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8618 dw_loc_descr_ref loc;
8620 if (a->dw_attr_val.val_class != dw_val_class_loc)
8621 continue;
8623 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8625 switch (loc->dw_loc_opc)
8627 case DW_OP_call2:
8628 case DW_OP_call4:
8629 case DW_OP_call_ref:
8630 gcc_assert (loc->dw_loc_oprnd1.val_class
8631 == dw_val_class_die_ref);
8632 loc->dw_loc_oprnd1.v.val_die_ref.die
8633 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8634 type_node,
8635 copied_dwarf_procs);
8637 default:
8638 break;
8644 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8645 rewrite references to point to the copies.
8647 References are looked for in DIE's attributes and recursively in all its
8648 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8649 mapping from old DWARF procedures to their copy. It is used not to copy
8650 twice the same DWARF procedure under TYPE_NODE. */
8652 static void
8653 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8654 comdat_type_node *type_node,
8655 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8657 dw_die_ref c;
8659 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8660 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8661 type_node,
8662 copied_dwarf_procs));
8665 /* Traverse the DIE and set up additional .debug_types or .debug_info
8666 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8667 section. */
8669 static void
8670 break_out_comdat_types (dw_die_ref die)
8672 dw_die_ref c;
8673 dw_die_ref first;
8674 dw_die_ref prev = NULL;
8675 dw_die_ref next = NULL;
8676 dw_die_ref unit = NULL;
8678 first = c = die->die_child;
8679 if (c)
8680 next = c->die_sib;
8681 if (c) do {
8682 if (prev == NULL || prev->die_sib == c)
8683 prev = c;
8684 c = next;
8685 next = (c == first ? NULL : c->die_sib);
8686 if (should_move_die_to_comdat (c))
8688 dw_die_ref replacement;
8689 comdat_type_node *type_node;
8691 /* Break out nested types into their own type units. */
8692 break_out_comdat_types (c);
8694 /* Create a new type unit DIE as the root for the new tree. */
8695 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8696 add_AT_unsigned (unit, DW_AT_language,
8697 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8699 /* Add the new unit's type DIE into the comdat type list. */
8700 type_node = ggc_cleared_alloc<comdat_type_node> ();
8701 type_node->root_die = unit;
8702 type_node->next = comdat_type_list;
8703 comdat_type_list = type_node;
8705 /* Generate the type signature. */
8706 generate_type_signature (c, type_node);
8708 /* Copy the declaration context, attributes, and children of the
8709 declaration into the new type unit DIE, then remove this DIE
8710 from the main CU (or replace it with a skeleton if necessary). */
8711 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8712 type_node->skeleton_die = replacement;
8714 /* Add the DIE to the new compunit. */
8715 add_child_die (unit, c);
8717 /* Types can reference DWARF procedures for type size or data location
8718 expressions. Calls in DWARF expressions cannot target procedures
8719 that are not in the same section. So we must copy DWARF procedures
8720 along with this type and then rewrite references to them. */
8721 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8722 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8724 if (replacement != NULL)
8725 c = replacement;
8727 else if (c->die_tag == DW_TAG_namespace
8728 || c->die_tag == DW_TAG_class_type
8729 || c->die_tag == DW_TAG_structure_type
8730 || c->die_tag == DW_TAG_union_type)
8732 /* Look for nested types that can be broken out. */
8733 break_out_comdat_types (c);
8735 } while (next != NULL);
8738 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8739 Enter all the cloned children into the hash table decl_table. */
8741 static dw_die_ref
8742 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8744 dw_die_ref c;
8745 dw_die_ref clone;
8746 struct decl_table_entry *entry;
8747 decl_table_entry **slot;
8749 if (die->die_tag == DW_TAG_subprogram)
8750 clone = clone_as_declaration (die);
8751 else
8752 clone = clone_die (die);
8754 slot = decl_table->find_slot_with_hash (die,
8755 htab_hash_pointer (die), INSERT);
8757 /* Assert that DIE isn't in the hash table yet. If it would be there
8758 before, the ancestors would be necessarily there as well, therefore
8759 clone_tree_partial wouldn't be called. */
8760 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8762 entry = XCNEW (struct decl_table_entry);
8763 entry->orig = die;
8764 entry->copy = clone;
8765 *slot = entry;
8767 if (die->die_tag != DW_TAG_subprogram)
8768 FOR_EACH_CHILD (die, c,
8769 add_child_die (clone, clone_tree_partial (c, decl_table)));
8771 return clone;
8774 /* Walk the DIE and its children, looking for references to incomplete
8775 or trivial types that are unmarked (i.e., that are not in the current
8776 type_unit). */
8778 static void
8779 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8781 dw_die_ref c;
8782 dw_attr_node *a;
8783 unsigned ix;
8785 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8787 if (AT_class (a) == dw_val_class_die_ref)
8789 dw_die_ref targ = AT_ref (a);
8790 decl_table_entry **slot;
8791 struct decl_table_entry *entry;
8793 if (targ->die_mark != 0 || targ->comdat_type_p)
8794 continue;
8796 slot = decl_table->find_slot_with_hash (targ,
8797 htab_hash_pointer (targ),
8798 INSERT);
8800 if (*slot != HTAB_EMPTY_ENTRY)
8802 /* TARG has already been copied, so we just need to
8803 modify the reference to point to the copy. */
8804 entry = *slot;
8805 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8807 else
8809 dw_die_ref parent = unit;
8810 dw_die_ref copy = clone_die (targ);
8812 /* Record in DECL_TABLE that TARG has been copied.
8813 Need to do this now, before the recursive call,
8814 because DECL_TABLE may be expanded and SLOT
8815 would no longer be a valid pointer. */
8816 entry = XCNEW (struct decl_table_entry);
8817 entry->orig = targ;
8818 entry->copy = copy;
8819 *slot = entry;
8821 /* If TARG is not a declaration DIE, we need to copy its
8822 children. */
8823 if (!is_declaration_die (targ))
8825 FOR_EACH_CHILD (
8826 targ, c,
8827 add_child_die (copy,
8828 clone_tree_partial (c, decl_table)));
8831 /* Make sure the cloned tree is marked as part of the
8832 type unit. */
8833 mark_dies (copy);
8835 /* If TARG has surrounding context, copy its ancestor tree
8836 into the new type unit. */
8837 if (targ->die_parent != NULL
8838 && !is_unit_die (targ->die_parent))
8839 parent = copy_ancestor_tree (unit, targ->die_parent,
8840 decl_table);
8842 add_child_die (parent, copy);
8843 a->dw_attr_val.v.val_die_ref.die = copy;
8845 /* Make sure the newly-copied DIE is walked. If it was
8846 installed in a previously-added context, it won't
8847 get visited otherwise. */
8848 if (parent != unit)
8850 /* Find the highest point of the newly-added tree,
8851 mark each node along the way, and walk from there. */
8852 parent->die_mark = 1;
8853 while (parent->die_parent
8854 && parent->die_parent->die_mark == 0)
8856 parent = parent->die_parent;
8857 parent->die_mark = 1;
8859 copy_decls_walk (unit, parent, decl_table);
8865 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8868 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8869 and record them in DECL_TABLE. */
8871 static void
8872 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8874 dw_die_ref c;
8876 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8878 dw_die_ref targ = AT_ref (a);
8879 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8880 decl_table_entry **slot
8881 = decl_table->find_slot_with_hash (targ,
8882 htab_hash_pointer (targ),
8883 INSERT);
8884 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8885 /* Record in DECL_TABLE that TARG has been already copied
8886 by remove_child_or_replace_with_skeleton. */
8887 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8888 entry->orig = targ;
8889 entry->copy = die;
8890 *slot = entry;
8892 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8895 /* Copy declarations for "unworthy" types into the new comdat section.
8896 Incomplete types, modified types, and certain other types aren't broken
8897 out into comdat sections of their own, so they don't have a signature,
8898 and we need to copy the declaration into the same section so that we
8899 don't have an external reference. */
8901 static void
8902 copy_decls_for_unworthy_types (dw_die_ref unit)
8904 mark_dies (unit);
8905 decl_hash_type decl_table (10);
8906 collect_skeleton_dies (unit, &decl_table);
8907 copy_decls_walk (unit, unit, &decl_table);
8908 unmark_dies (unit);
8911 /* Traverse the DIE and add a sibling attribute if it may have the
8912 effect of speeding up access to siblings. To save some space,
8913 avoid generating sibling attributes for DIE's without children. */
8915 static void
8916 add_sibling_attributes (dw_die_ref die)
8918 dw_die_ref c;
8920 if (! die->die_child)
8921 return;
8923 if (die->die_parent && die != die->die_parent->die_child)
8924 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8926 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8929 /* Output all location lists for the DIE and its children. */
8931 static void
8932 output_location_lists (dw_die_ref die)
8934 dw_die_ref c;
8935 dw_attr_node *a;
8936 unsigned ix;
8938 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8939 if (AT_class (a) == dw_val_class_loc_list)
8940 output_loc_list (AT_loc_list (a));
8942 FOR_EACH_CHILD (die, c, output_location_lists (c));
8945 /* During assign_location_list_indexes and output_loclists_offset the
8946 current index, after it the number of assigned indexes (i.e. how
8947 large the .debug_loclists* offset table should be). */
8948 static unsigned int loc_list_idx;
8950 /* Output all location list offsets for the DIE and its children. */
8952 static void
8953 output_loclists_offsets (dw_die_ref die)
8955 dw_die_ref c;
8956 dw_attr_node *a;
8957 unsigned ix;
8959 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8960 if (AT_class (a) == dw_val_class_loc_list)
8962 dw_loc_list_ref l = AT_loc_list (a);
8963 if (l->offset_emitted)
8964 continue;
8965 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
8966 loc_section_label, NULL);
8967 gcc_assert (l->hash == loc_list_idx);
8968 loc_list_idx++;
8969 l->offset_emitted = true;
8972 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8975 /* Recursively set indexes of location lists. */
8977 static void
8978 assign_location_list_indexes (dw_die_ref die)
8980 dw_die_ref c;
8981 dw_attr_node *a;
8982 unsigned ix;
8984 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8985 if (AT_class (a) == dw_val_class_loc_list)
8987 dw_loc_list_ref list = AT_loc_list (a);
8988 if (!list->num_assigned)
8990 list->num_assigned = true;
8991 list->hash = loc_list_idx++;
8995 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8998 /* We want to limit the number of external references, because they are
8999 larger than local references: a relocation takes multiple words, and
9000 even a sig8 reference is always eight bytes, whereas a local reference
9001 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
9002 So if we encounter multiple external references to the same type DIE, we
9003 make a local typedef stub for it and redirect all references there.
9005 This is the element of the hash table for keeping track of these
9006 references. */
9008 struct external_ref
9010 dw_die_ref type;
9011 dw_die_ref stub;
9012 unsigned n_refs;
9015 /* Hashtable helpers. */
9017 struct external_ref_hasher : free_ptr_hash <external_ref>
9019 static inline hashval_t hash (const external_ref *);
9020 static inline bool equal (const external_ref *, const external_ref *);
9023 inline hashval_t
9024 external_ref_hasher::hash (const external_ref *r)
9026 dw_die_ref die = r->type;
9027 hashval_t h = 0;
9029 /* We can't use the address of the DIE for hashing, because
9030 that will make the order of the stub DIEs non-deterministic. */
9031 if (! die->comdat_type_p)
9032 /* We have a symbol; use it to compute a hash. */
9033 h = htab_hash_string (die->die_id.die_symbol);
9034 else
9036 /* We have a type signature; use a subset of the bits as the hash.
9037 The 8-byte signature is at least as large as hashval_t. */
9038 comdat_type_node *type_node = die->die_id.die_type_node;
9039 memcpy (&h, type_node->signature, sizeof (h));
9041 return h;
9044 inline bool
9045 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9047 return r1->type == r2->type;
9050 typedef hash_table<external_ref_hasher> external_ref_hash_type;
9052 /* Return a pointer to the external_ref for references to DIE. */
9054 static struct external_ref *
9055 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9057 struct external_ref ref, *ref_p;
9058 external_ref **slot;
9060 ref.type = die;
9061 slot = map->find_slot (&ref, INSERT);
9062 if (*slot != HTAB_EMPTY_ENTRY)
9063 return *slot;
9065 ref_p = XCNEW (struct external_ref);
9066 ref_p->type = die;
9067 *slot = ref_p;
9068 return ref_p;
9071 /* Subroutine of optimize_external_refs, below.
9073 If we see a type skeleton, record it as our stub. If we see external
9074 references, remember how many we've seen. */
9076 static void
9077 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9079 dw_die_ref c;
9080 dw_attr_node *a;
9081 unsigned ix;
9082 struct external_ref *ref_p;
9084 if (is_type_die (die)
9085 && (c = get_AT_ref (die, DW_AT_signature)))
9087 /* This is a local skeleton; use it for local references. */
9088 ref_p = lookup_external_ref (map, c);
9089 ref_p->stub = die;
9092 /* Scan the DIE references, and remember any that refer to DIEs from
9093 other CUs (i.e. those which are not marked). */
9094 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9095 if (AT_class (a) == dw_val_class_die_ref
9096 && (c = AT_ref (a))->die_mark == 0
9097 && is_type_die (c))
9099 ref_p = lookup_external_ref (map, c);
9100 ref_p->n_refs++;
9103 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9106 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9107 points to an external_ref, DATA is the CU we're processing. If we don't
9108 already have a local stub, and we have multiple refs, build a stub. */
9111 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9113 struct external_ref *ref_p = *slot;
9115 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9117 /* We have multiple references to this type, so build a small stub.
9118 Both of these forms are a bit dodgy from the perspective of the
9119 DWARF standard, since technically they should have names. */
9120 dw_die_ref cu = data;
9121 dw_die_ref type = ref_p->type;
9122 dw_die_ref stub = NULL;
9124 if (type->comdat_type_p)
9126 /* If we refer to this type via sig8, use AT_signature. */
9127 stub = new_die (type->die_tag, cu, NULL_TREE);
9128 add_AT_die_ref (stub, DW_AT_signature, type);
9130 else
9132 /* Otherwise, use a typedef with no name. */
9133 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9134 add_AT_die_ref (stub, DW_AT_type, type);
9137 stub->die_mark++;
9138 ref_p->stub = stub;
9140 return 1;
9143 /* DIE is a unit; look through all the DIE references to see if there are
9144 any external references to types, and if so, create local stubs for
9145 them which will be applied in build_abbrev_table. This is useful because
9146 references to local DIEs are smaller. */
9148 static external_ref_hash_type *
9149 optimize_external_refs (dw_die_ref die)
9151 external_ref_hash_type *map = new external_ref_hash_type (10);
9152 optimize_external_refs_1 (die, map);
9153 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9154 return map;
9157 /* The following 3 variables are temporaries that are computed only during the
9158 build_abbrev_table call and used and released during the following
9159 optimize_abbrev_table call. */
9161 /* First abbrev_id that can be optimized based on usage. */
9162 static unsigned int abbrev_opt_start;
9164 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9165 abbrev_id smaller than this, because they must be already sized
9166 during build_abbrev_table). */
9167 static unsigned int abbrev_opt_base_type_end;
9169 /* Vector of usage counts during build_abbrev_table. Indexed by
9170 abbrev_id - abbrev_opt_start. */
9171 static vec<unsigned int> abbrev_usage_count;
9173 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9174 static vec<dw_die_ref> sorted_abbrev_dies;
9176 /* The format of each DIE (and its attribute value pairs) is encoded in an
9177 abbreviation table. This routine builds the abbreviation table and assigns
9178 a unique abbreviation id for each abbreviation entry. The children of each
9179 die are visited recursively. */
9181 static void
9182 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9184 unsigned int abbrev_id = 0;
9185 dw_die_ref c;
9186 dw_attr_node *a;
9187 unsigned ix;
9188 dw_die_ref abbrev;
9190 /* Scan the DIE references, and replace any that refer to
9191 DIEs from other CUs (i.e. those which are not marked) with
9192 the local stubs we built in optimize_external_refs. */
9193 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9194 if (AT_class (a) == dw_val_class_die_ref
9195 && (c = AT_ref (a))->die_mark == 0)
9197 struct external_ref *ref_p;
9198 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9200 if (is_type_die (c)
9201 && (ref_p = lookup_external_ref (extern_map, c))
9202 && ref_p->stub && ref_p->stub != die)
9204 gcc_assert (a->dw_attr != DW_AT_signature);
9205 change_AT_die_ref (a, ref_p->stub);
9207 else
9208 /* We aren't changing this reference, so mark it external. */
9209 set_AT_ref_external (a, 1);
9212 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9214 dw_attr_node *die_a, *abbrev_a;
9215 unsigned ix;
9216 bool ok = true;
9218 if (abbrev_id == 0)
9219 continue;
9220 if (abbrev->die_tag != die->die_tag)
9221 continue;
9222 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9223 continue;
9225 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9226 continue;
9228 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9230 abbrev_a = &(*abbrev->die_attr)[ix];
9231 if ((abbrev_a->dw_attr != die_a->dw_attr)
9232 || (value_format (abbrev_a) != value_format (die_a)))
9234 ok = false;
9235 break;
9238 if (ok)
9239 break;
9242 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9244 vec_safe_push (abbrev_die_table, die);
9245 if (abbrev_opt_start)
9246 abbrev_usage_count.safe_push (0);
9248 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9250 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9251 sorted_abbrev_dies.safe_push (die);
9254 die->die_abbrev = abbrev_id;
9255 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9258 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9259 by die_abbrev's usage count, from the most commonly used
9260 abbreviation to the least. */
9262 static int
9263 die_abbrev_cmp (const void *p1, const void *p2)
9265 dw_die_ref die1 = *(const dw_die_ref *) p1;
9266 dw_die_ref die2 = *(const dw_die_ref *) p2;
9268 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9269 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9271 if (die1->die_abbrev >= abbrev_opt_base_type_end
9272 && die2->die_abbrev >= abbrev_opt_base_type_end)
9274 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9275 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9276 return -1;
9277 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9278 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9279 return 1;
9282 /* Stabilize the sort. */
9283 if (die1->die_abbrev < die2->die_abbrev)
9284 return -1;
9285 if (die1->die_abbrev > die2->die_abbrev)
9286 return 1;
9288 return 0;
9291 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9292 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9293 into dw_val_class_const_implicit or
9294 dw_val_class_unsigned_const_implicit. */
9296 static void
9297 optimize_implicit_const (unsigned int first_id, unsigned int end,
9298 vec<bool> &implicit_consts)
9300 /* It never makes sense if there is just one DIE using the abbreviation. */
9301 if (end < first_id + 2)
9302 return;
9304 dw_attr_node *a;
9305 unsigned ix, i;
9306 dw_die_ref die = sorted_abbrev_dies[first_id];
9307 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9308 if (implicit_consts[ix])
9310 enum dw_val_class new_class = dw_val_class_none;
9311 switch (AT_class (a))
9313 case dw_val_class_unsigned_const:
9314 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9315 continue;
9317 /* The .debug_abbrev section will grow by
9318 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9319 in all the DIEs using that abbreviation. */
9320 if (constant_size (AT_unsigned (a)) * (end - first_id)
9321 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9322 continue;
9324 new_class = dw_val_class_unsigned_const_implicit;
9325 break;
9327 case dw_val_class_const:
9328 new_class = dw_val_class_const_implicit;
9329 break;
9331 case dw_val_class_file:
9332 new_class = dw_val_class_file_implicit;
9333 break;
9335 default:
9336 continue;
9338 for (i = first_id; i < end; i++)
9339 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9340 = new_class;
9344 /* Attempt to optimize abbreviation table from abbrev_opt_start
9345 abbreviation above. */
9347 static void
9348 optimize_abbrev_table (void)
9350 if (abbrev_opt_start
9351 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9352 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9354 auto_vec<bool, 32> implicit_consts;
9355 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9357 unsigned int abbrev_id = abbrev_opt_start - 1;
9358 unsigned int first_id = ~0U;
9359 unsigned int last_abbrev_id = 0;
9360 unsigned int i;
9361 dw_die_ref die;
9362 if (abbrev_opt_base_type_end > abbrev_opt_start)
9363 abbrev_id = abbrev_opt_base_type_end - 1;
9364 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9365 most commonly used abbreviations come first. */
9366 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9368 dw_attr_node *a;
9369 unsigned ix;
9371 /* If calc_base_type_die_sizes has been called, the CU and
9372 base types after it can't be optimized, because we've already
9373 calculated their DIE offsets. We've sorted them first. */
9374 if (die->die_abbrev < abbrev_opt_base_type_end)
9375 continue;
9376 if (die->die_abbrev != last_abbrev_id)
9378 last_abbrev_id = die->die_abbrev;
9379 if (dwarf_version >= 5 && first_id != ~0U)
9380 optimize_implicit_const (first_id, i, implicit_consts);
9381 abbrev_id++;
9382 (*abbrev_die_table)[abbrev_id] = die;
9383 if (dwarf_version >= 5)
9385 first_id = i;
9386 implicit_consts.truncate (0);
9388 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9389 switch (AT_class (a))
9391 case dw_val_class_const:
9392 case dw_val_class_unsigned_const:
9393 case dw_val_class_file:
9394 implicit_consts.safe_push (true);
9395 break;
9396 default:
9397 implicit_consts.safe_push (false);
9398 break;
9402 else if (dwarf_version >= 5)
9404 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9405 if (!implicit_consts[ix])
9406 continue;
9407 else
9409 dw_attr_node *other_a
9410 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9411 if (!dw_val_equal_p (&a->dw_attr_val,
9412 &other_a->dw_attr_val))
9413 implicit_consts[ix] = false;
9416 die->die_abbrev = abbrev_id;
9418 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9419 if (dwarf_version >= 5 && first_id != ~0U)
9420 optimize_implicit_const (first_id, i, implicit_consts);
9423 abbrev_opt_start = 0;
9424 abbrev_opt_base_type_end = 0;
9425 abbrev_usage_count.release ();
9426 sorted_abbrev_dies.release ();
9429 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9431 static int
9432 constant_size (unsigned HOST_WIDE_INT value)
9434 int log;
9436 if (value == 0)
9437 log = 0;
9438 else
9439 log = floor_log2 (value);
9441 log = log / 8;
9442 log = 1 << (floor_log2 (log) + 1);
9444 return log;
9447 /* Return the size of a DIE as it is represented in the
9448 .debug_info section. */
9450 static unsigned long
9451 size_of_die (dw_die_ref die)
9453 unsigned long size = 0;
9454 dw_attr_node *a;
9455 unsigned ix;
9456 enum dwarf_form form;
9458 size += size_of_uleb128 (die->die_abbrev);
9459 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9461 switch (AT_class (a))
9463 case dw_val_class_addr:
9464 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9466 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9467 size += size_of_uleb128 (AT_index (a));
9469 else
9470 size += DWARF2_ADDR_SIZE;
9471 break;
9472 case dw_val_class_offset:
9473 size += dwarf_offset_size;
9474 break;
9475 case dw_val_class_loc:
9477 unsigned long lsize = size_of_locs (AT_loc (a));
9479 /* Block length. */
9480 if (dwarf_version >= 4)
9481 size += size_of_uleb128 (lsize);
9482 else
9483 size += constant_size (lsize);
9484 size += lsize;
9486 break;
9487 case dw_val_class_loc_list:
9488 if (dwarf_split_debug_info && dwarf_version >= 5)
9490 gcc_assert (AT_loc_list (a)->num_assigned);
9491 size += size_of_uleb128 (AT_loc_list (a)->hash);
9493 else
9494 size += dwarf_offset_size;
9495 break;
9496 case dw_val_class_view_list:
9497 size += dwarf_offset_size;
9498 break;
9499 case dw_val_class_range_list:
9500 if (value_format (a) == DW_FORM_rnglistx)
9502 gcc_assert (rnglist_idx);
9503 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9504 size += size_of_uleb128 (r->idx);
9506 else
9507 size += dwarf_offset_size;
9508 break;
9509 case dw_val_class_const:
9510 size += size_of_sleb128 (AT_int (a));
9511 break;
9512 case dw_val_class_unsigned_const:
9514 int csize = constant_size (AT_unsigned (a));
9515 if (dwarf_version == 3
9516 && a->dw_attr == DW_AT_data_member_location
9517 && csize >= 4)
9518 size += size_of_uleb128 (AT_unsigned (a));
9519 else
9520 size += csize;
9522 break;
9523 case dw_val_class_symview:
9524 if (symview_upper_bound <= 0xff)
9525 size += 1;
9526 else if (symview_upper_bound <= 0xffff)
9527 size += 2;
9528 else if (symview_upper_bound <= 0xffffffff)
9529 size += 4;
9530 else
9531 size += 8;
9532 break;
9533 case dw_val_class_const_implicit:
9534 case dw_val_class_unsigned_const_implicit:
9535 case dw_val_class_file_implicit:
9536 /* These occupy no size in the DIE, just an extra sleb128 in
9537 .debug_abbrev. */
9538 break;
9539 case dw_val_class_const_double:
9540 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9541 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9542 size++; /* block */
9543 break;
9544 case dw_val_class_wide_int:
9545 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9546 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9547 if (get_full_len (*a->dw_attr_val.v.val_wide)
9548 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9549 size++; /* block */
9550 break;
9551 case dw_val_class_vec:
9552 size += constant_size (a->dw_attr_val.v.val_vec.length
9553 * a->dw_attr_val.v.val_vec.elt_size)
9554 + a->dw_attr_val.v.val_vec.length
9555 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9556 break;
9557 case dw_val_class_flag:
9558 if (dwarf_version >= 4)
9559 /* Currently all add_AT_flag calls pass in 1 as last argument,
9560 so DW_FORM_flag_present can be used. If that ever changes,
9561 we'll need to use DW_FORM_flag and have some optimization
9562 in build_abbrev_table that will change those to
9563 DW_FORM_flag_present if it is set to 1 in all DIEs using
9564 the same abbrev entry. */
9565 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9566 else
9567 size += 1;
9568 break;
9569 case dw_val_class_die_ref:
9570 if (AT_ref_external (a))
9572 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9573 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9574 is sized by target address length, whereas in DWARF3
9575 it's always sized as an offset. */
9576 if (AT_ref (a)->comdat_type_p)
9577 size += DWARF_TYPE_SIGNATURE_SIZE;
9578 else if (dwarf_version == 2)
9579 size += DWARF2_ADDR_SIZE;
9580 else
9581 size += dwarf_offset_size;
9583 else
9584 size += dwarf_offset_size;
9585 break;
9586 case dw_val_class_fde_ref:
9587 size += dwarf_offset_size;
9588 break;
9589 case dw_val_class_lbl_id:
9590 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9592 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9593 size += size_of_uleb128 (AT_index (a));
9595 else
9596 size += DWARF2_ADDR_SIZE;
9597 break;
9598 case dw_val_class_lineptr:
9599 case dw_val_class_macptr:
9600 case dw_val_class_loclistsptr:
9601 size += dwarf_offset_size;
9602 break;
9603 case dw_val_class_str:
9604 form = AT_string_form (a);
9605 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9606 size += dwarf_offset_size;
9607 else if (form == dwarf_FORM (DW_FORM_strx))
9608 size += size_of_uleb128 (AT_index (a));
9609 else
9610 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9611 break;
9612 case dw_val_class_file:
9613 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9614 break;
9615 case dw_val_class_data8:
9616 size += 8;
9617 break;
9618 case dw_val_class_vms_delta:
9619 size += dwarf_offset_size;
9620 break;
9621 case dw_val_class_high_pc:
9622 size += DWARF2_ADDR_SIZE;
9623 break;
9624 case dw_val_class_discr_value:
9625 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9626 break;
9627 case dw_val_class_discr_list:
9629 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9631 /* This is a block, so we have the block length and then its
9632 data. */
9633 size += constant_size (block_size) + block_size;
9635 break;
9636 default:
9637 gcc_unreachable ();
9641 return size;
9644 /* Size the debugging information associated with a given DIE. Visits the
9645 DIE's children recursively. Updates the global variable next_die_offset, on
9646 each time through. Uses the current value of next_die_offset to update the
9647 die_offset field in each DIE. */
9649 static void
9650 calc_die_sizes (dw_die_ref die)
9652 dw_die_ref c;
9654 gcc_assert (die->die_offset == 0
9655 || (unsigned long int) die->die_offset == next_die_offset);
9656 die->die_offset = next_die_offset;
9657 next_die_offset += size_of_die (die);
9659 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9661 if (die->die_child != NULL)
9662 /* Count the null byte used to terminate sibling lists. */
9663 next_die_offset += 1;
9666 /* Size just the base type children at the start of the CU.
9667 This is needed because build_abbrev needs to size locs
9668 and sizing of type based stack ops needs to know die_offset
9669 values for the base types. */
9671 static void
9672 calc_base_type_die_sizes (void)
9674 unsigned long die_offset = (dwarf_split_debug_info
9675 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9676 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9677 unsigned int i;
9678 dw_die_ref base_type;
9679 #if ENABLE_ASSERT_CHECKING
9680 dw_die_ref prev = comp_unit_die ()->die_child;
9681 #endif
9683 die_offset += size_of_die (comp_unit_die ());
9684 for (i = 0; base_types.iterate (i, &base_type); i++)
9686 #if ENABLE_ASSERT_CHECKING
9687 gcc_assert (base_type->die_offset == 0
9688 && prev->die_sib == base_type
9689 && base_type->die_child == NULL
9690 && base_type->die_abbrev);
9691 prev = base_type;
9692 #endif
9693 if (abbrev_opt_start
9694 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9695 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9696 base_type->die_offset = die_offset;
9697 die_offset += size_of_die (base_type);
9701 /* Set the marks for a die and its children. We do this so
9702 that we know whether or not a reference needs to use FORM_ref_addr; only
9703 DIEs in the same CU will be marked. We used to clear out the offset
9704 and use that as the flag, but ran into ordering problems. */
9706 static void
9707 mark_dies (dw_die_ref die)
9709 dw_die_ref c;
9711 gcc_assert (!die->die_mark);
9713 die->die_mark = 1;
9714 FOR_EACH_CHILD (die, c, mark_dies (c));
9717 /* Clear the marks for a die and its children. */
9719 static void
9720 unmark_dies (dw_die_ref die)
9722 dw_die_ref c;
9724 if (! use_debug_types)
9725 gcc_assert (die->die_mark);
9727 die->die_mark = 0;
9728 FOR_EACH_CHILD (die, c, unmark_dies (c));
9731 /* Clear the marks for a die, its children and referred dies. */
9733 static void
9734 unmark_all_dies (dw_die_ref die)
9736 dw_die_ref c;
9737 dw_attr_node *a;
9738 unsigned ix;
9740 if (!die->die_mark)
9741 return;
9742 die->die_mark = 0;
9744 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9746 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9747 if (AT_class (a) == dw_val_class_die_ref)
9748 unmark_all_dies (AT_ref (a));
9751 /* Calculate if the entry should appear in the final output file. It may be
9752 from a pruned a type. */
9754 static bool
9755 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9757 /* By limiting gnu pubnames to definitions only, gold can generate a
9758 gdb index without entries for declarations, which don't include
9759 enough information to be useful. */
9760 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9761 return false;
9763 if (table == pubname_table)
9765 /* Enumerator names are part of the pubname table, but the
9766 parent DW_TAG_enumeration_type die may have been pruned.
9767 Don't output them if that is the case. */
9768 if (p->die->die_tag == DW_TAG_enumerator &&
9769 (p->die->die_parent == NULL
9770 || !p->die->die_parent->die_perennial_p))
9771 return false;
9773 /* Everything else in the pubname table is included. */
9774 return true;
9777 /* The pubtypes table shouldn't include types that have been
9778 pruned. */
9779 return (p->die->die_offset != 0
9780 || !flag_eliminate_unused_debug_types);
9783 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9784 generated for the compilation unit. */
9786 static unsigned long
9787 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9789 unsigned long size;
9790 unsigned i;
9791 pubname_entry *p;
9792 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9794 size = DWARF_PUBNAMES_HEADER_SIZE;
9795 FOR_EACH_VEC_ELT (*names, i, p)
9796 if (include_pubname_in_output (names, p))
9797 size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9799 size += dwarf_offset_size;
9800 return size;
9803 /* Return the size of the information in the .debug_aranges section. */
9805 static unsigned long
9806 size_of_aranges (void)
9808 unsigned long size;
9810 size = DWARF_ARANGES_HEADER_SIZE;
9812 /* Count the address/length pair for this compilation unit. */
9813 if (switch_text_ranges)
9814 size += 2 * DWARF2_ADDR_SIZE
9815 * (vec_safe_length (switch_text_ranges) / 2 + 1);
9816 if (switch_cold_ranges)
9817 size += 2 * DWARF2_ADDR_SIZE
9818 * (vec_safe_length (switch_cold_ranges) / 2 + 1);
9819 if (have_multiple_function_sections)
9821 unsigned fde_idx;
9822 dw_fde_ref fde;
9824 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9826 if (fde->ignored_debug)
9827 continue;
9828 if (!fde->in_std_section)
9829 size += 2 * DWARF2_ADDR_SIZE;
9830 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9831 size += 2 * DWARF2_ADDR_SIZE;
9835 /* Count the two zero words used to terminated the address range table. */
9836 size += 2 * DWARF2_ADDR_SIZE;
9837 return size;
9840 /* Select the encoding of an attribute value. */
9842 static enum dwarf_form
9843 value_format (dw_attr_node *a)
9845 switch (AT_class (a))
9847 case dw_val_class_addr:
9848 /* Only very few attributes allow DW_FORM_addr. */
9849 switch (a->dw_attr)
9851 case DW_AT_low_pc:
9852 case DW_AT_high_pc:
9853 case DW_AT_entry_pc:
9854 case DW_AT_trampoline:
9855 return (AT_index (a) == NOT_INDEXED
9856 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9857 default:
9858 break;
9860 switch (DWARF2_ADDR_SIZE)
9862 case 1:
9863 return DW_FORM_data1;
9864 case 2:
9865 return DW_FORM_data2;
9866 case 4:
9867 return DW_FORM_data4;
9868 case 8:
9869 return DW_FORM_data8;
9870 default:
9871 gcc_unreachable ();
9873 case dw_val_class_loc_list:
9874 if (dwarf_split_debug_info
9875 && dwarf_version >= 5
9876 && AT_loc_list (a)->num_assigned)
9877 return DW_FORM_loclistx;
9878 /* FALLTHRU */
9879 case dw_val_class_view_list:
9880 case dw_val_class_range_list:
9881 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9882 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9883 care about sizes of .debug* sections in shared libraries and
9884 executables and don't take into account relocations that affect just
9885 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9886 table in the .debug_rnglists section. */
9887 if (dwarf_split_debug_info
9888 && dwarf_version >= 5
9889 && AT_class (a) == dw_val_class_range_list
9890 && rnglist_idx
9891 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9892 return DW_FORM_rnglistx;
9893 if (dwarf_version >= 4)
9894 return DW_FORM_sec_offset;
9895 /* FALLTHRU */
9896 case dw_val_class_vms_delta:
9897 case dw_val_class_offset:
9898 switch (dwarf_offset_size)
9900 case 4:
9901 return DW_FORM_data4;
9902 case 8:
9903 return DW_FORM_data8;
9904 default:
9905 gcc_unreachable ();
9907 case dw_val_class_loc:
9908 if (dwarf_version >= 4)
9909 return DW_FORM_exprloc;
9910 switch (constant_size (size_of_locs (AT_loc (a))))
9912 case 1:
9913 return DW_FORM_block1;
9914 case 2:
9915 return DW_FORM_block2;
9916 case 4:
9917 return DW_FORM_block4;
9918 default:
9919 gcc_unreachable ();
9921 case dw_val_class_const:
9922 return DW_FORM_sdata;
9923 case dw_val_class_unsigned_const:
9924 switch (constant_size (AT_unsigned (a)))
9926 case 1:
9927 return DW_FORM_data1;
9928 case 2:
9929 return DW_FORM_data2;
9930 case 4:
9931 /* In DWARF3 DW_AT_data_member_location with
9932 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9933 constant, so we need to use DW_FORM_udata if we need
9934 a large constant. */
9935 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9936 return DW_FORM_udata;
9937 return DW_FORM_data4;
9938 case 8:
9939 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9940 return DW_FORM_udata;
9941 return DW_FORM_data8;
9942 default:
9943 gcc_unreachable ();
9945 case dw_val_class_const_implicit:
9946 case dw_val_class_unsigned_const_implicit:
9947 case dw_val_class_file_implicit:
9948 return DW_FORM_implicit_const;
9949 case dw_val_class_const_double:
9950 switch (HOST_BITS_PER_WIDE_INT)
9952 case 8:
9953 return DW_FORM_data2;
9954 case 16:
9955 return DW_FORM_data4;
9956 case 32:
9957 return DW_FORM_data8;
9958 case 64:
9959 if (dwarf_version >= 5)
9960 return DW_FORM_data16;
9961 /* FALLTHRU */
9962 default:
9963 return DW_FORM_block1;
9965 case dw_val_class_wide_int:
9966 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9968 case 8:
9969 return DW_FORM_data1;
9970 case 16:
9971 return DW_FORM_data2;
9972 case 32:
9973 return DW_FORM_data4;
9974 case 64:
9975 return DW_FORM_data8;
9976 case 128:
9977 if (dwarf_version >= 5)
9978 return DW_FORM_data16;
9979 /* FALLTHRU */
9980 default:
9981 return DW_FORM_block1;
9983 case dw_val_class_symview:
9984 /* ??? We might use uleb128, but then we'd have to compute
9985 .debug_info offsets in the assembler. */
9986 if (symview_upper_bound <= 0xff)
9987 return DW_FORM_data1;
9988 else if (symview_upper_bound <= 0xffff)
9989 return DW_FORM_data2;
9990 else if (symview_upper_bound <= 0xffffffff)
9991 return DW_FORM_data4;
9992 else
9993 return DW_FORM_data8;
9994 case dw_val_class_vec:
9995 switch (constant_size (a->dw_attr_val.v.val_vec.length
9996 * a->dw_attr_val.v.val_vec.elt_size))
9998 case 1:
9999 return DW_FORM_block1;
10000 case 2:
10001 return DW_FORM_block2;
10002 case 4:
10003 return DW_FORM_block4;
10004 default:
10005 gcc_unreachable ();
10007 case dw_val_class_flag:
10008 if (dwarf_version >= 4)
10010 /* Currently all add_AT_flag calls pass in 1 as last argument,
10011 so DW_FORM_flag_present can be used. If that ever changes,
10012 we'll need to use DW_FORM_flag and have some optimization
10013 in build_abbrev_table that will change those to
10014 DW_FORM_flag_present if it is set to 1 in all DIEs using
10015 the same abbrev entry. */
10016 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10017 return DW_FORM_flag_present;
10019 return DW_FORM_flag;
10020 case dw_val_class_die_ref:
10021 if (AT_ref_external (a))
10023 if (AT_ref (a)->comdat_type_p)
10024 return DW_FORM_ref_sig8;
10025 else
10026 return DW_FORM_ref_addr;
10028 else
10029 return DW_FORM_ref;
10030 case dw_val_class_fde_ref:
10031 return DW_FORM_data;
10032 case dw_val_class_lbl_id:
10033 return (AT_index (a) == NOT_INDEXED
10034 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
10035 case dw_val_class_lineptr:
10036 case dw_val_class_macptr:
10037 case dw_val_class_loclistsptr:
10038 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10039 case dw_val_class_str:
10040 return AT_string_form (a);
10041 case dw_val_class_file:
10042 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10044 case 1:
10045 return DW_FORM_data1;
10046 case 2:
10047 return DW_FORM_data2;
10048 case 4:
10049 return DW_FORM_data4;
10050 default:
10051 gcc_unreachable ();
10054 case dw_val_class_data8:
10055 return DW_FORM_data8;
10057 case dw_val_class_high_pc:
10058 switch (DWARF2_ADDR_SIZE)
10060 case 1:
10061 return DW_FORM_data1;
10062 case 2:
10063 return DW_FORM_data2;
10064 case 4:
10065 return DW_FORM_data4;
10066 case 8:
10067 return DW_FORM_data8;
10068 default:
10069 gcc_unreachable ();
10072 case dw_val_class_discr_value:
10073 return (a->dw_attr_val.v.val_discr_value.pos
10074 ? DW_FORM_udata
10075 : DW_FORM_sdata);
10076 case dw_val_class_discr_list:
10077 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10079 case 1:
10080 return DW_FORM_block1;
10081 case 2:
10082 return DW_FORM_block2;
10083 case 4:
10084 return DW_FORM_block4;
10085 default:
10086 gcc_unreachable ();
10089 default:
10090 gcc_unreachable ();
10094 /* Output the encoding of an attribute value. */
10096 static void
10097 output_value_format (dw_attr_node *a)
10099 enum dwarf_form form = value_format (a);
10101 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10104 /* Given a die and id, produce the appropriate abbreviations. */
10106 static void
10107 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10109 unsigned ix;
10110 dw_attr_node *a_attr;
10112 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10113 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10114 dwarf_tag_name (abbrev->die_tag));
10116 if (abbrev->die_child != NULL)
10117 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10118 else
10119 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10121 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10123 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10124 dwarf_attr_name (a_attr->dw_attr));
10125 output_value_format (a_attr);
10126 if (value_format (a_attr) == DW_FORM_implicit_const)
10128 if (AT_class (a_attr) == dw_val_class_file_implicit)
10130 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10131 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10132 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10134 else
10135 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10139 dw2_asm_output_data (1, 0, NULL);
10140 dw2_asm_output_data (1, 0, NULL);
10144 /* Output the .debug_abbrev section which defines the DIE abbreviation
10145 table. */
10147 static void
10148 output_abbrev_section (void)
10150 unsigned int abbrev_id;
10151 dw_die_ref abbrev;
10153 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10154 if (abbrev_id != 0)
10155 output_die_abbrevs (abbrev_id, abbrev);
10157 /* Terminate the table. */
10158 dw2_asm_output_data (1, 0, NULL);
10161 /* Return a new location list, given the begin and end range, and the
10162 expression. */
10164 static inline dw_loc_list_ref
10165 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10166 const char *end, var_loc_view vend,
10167 const char *section)
10169 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10171 retlist->begin = begin;
10172 retlist->begin_entry = NULL;
10173 retlist->end = end;
10174 retlist->end_entry = NULL;
10175 retlist->expr = expr;
10176 retlist->section = section;
10177 retlist->vbegin = vbegin;
10178 retlist->vend = vend;
10180 return retlist;
10183 /* Return true iff there's any nonzero view number in the loc list.
10185 ??? When views are not enabled, we'll often extend a single range
10186 to the entire function, so that we emit a single location
10187 expression rather than a location list. With views, even with a
10188 single range, we'll output a list if start or end have a nonzero
10189 view. If we change this, we may want to stop splitting a single
10190 range in dw_loc_list just because of a nonzero view, even if it
10191 straddles across hot/cold partitions. */
10193 static bool
10194 loc_list_has_views (dw_loc_list_ref list)
10196 if (!debug_variable_location_views)
10197 return false;
10199 for (dw_loc_list_ref loc = list;
10200 loc != NULL; loc = loc->dw_loc_next)
10201 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10202 return true;
10204 return false;
10207 /* Generate a new internal symbol for this location list node, if it
10208 hasn't got one yet. */
10210 static inline void
10211 gen_llsym (dw_loc_list_ref list)
10213 gcc_assert (!list->ll_symbol);
10214 list->ll_symbol = gen_internal_sym ("LLST");
10216 if (!loc_list_has_views (list))
10217 return;
10219 if (dwarf2out_locviews_in_attribute ())
10221 /* Use the same label_num for the view list. */
10222 label_num--;
10223 list->vl_symbol = gen_internal_sym ("LVUS");
10225 else
10226 list->vl_symbol = list->ll_symbol;
10229 /* Generate a symbol for the list, but only if we really want to emit
10230 it as a list. */
10232 static inline void
10233 maybe_gen_llsym (dw_loc_list_ref list)
10235 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10236 return;
10238 gen_llsym (list);
10241 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10242 NULL, don't consider size of the location expression. If we're not
10243 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10244 representation in *SIZEP. */
10246 static bool
10247 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10249 /* Don't output an entry that starts and ends at the same address. */
10250 if (strcmp (curr->begin, curr->end) == 0
10251 && curr->vbegin == curr->vend && !curr->force)
10252 return true;
10254 if (!sizep)
10255 return false;
10257 unsigned long size = size_of_locs (curr->expr);
10259 /* If the expression is too large, drop it on the floor. We could
10260 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10261 in the expression, but >= 64KB expressions for a single value
10262 in a single range are unlikely very useful. */
10263 if (dwarf_version < 5 && size > 0xffff)
10264 return true;
10266 *sizep = size;
10268 return false;
10271 /* Output a view pair loclist entry for CURR, if it requires one. */
10273 static void
10274 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10276 if (!dwarf2out_locviews_in_loclist ())
10277 return;
10279 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10280 return;
10282 #ifdef DW_LLE_view_pair
10283 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10285 if (dwarf2out_as_locview_support)
10287 if (ZERO_VIEW_P (curr->vbegin))
10288 dw2_asm_output_data_uleb128 (0, "Location view begin");
10289 else
10291 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10292 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10293 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10296 if (ZERO_VIEW_P (curr->vend))
10297 dw2_asm_output_data_uleb128 (0, "Location view end");
10298 else
10300 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10301 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10302 dw2_asm_output_symname_uleb128 (label, "Location view end");
10305 else
10307 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10308 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10310 #endif /* DW_LLE_view_pair */
10312 return;
10315 /* Output the location list given to us. */
10317 static void
10318 output_loc_list (dw_loc_list_ref list_head)
10320 int vcount = 0, lcount = 0;
10322 if (list_head->emitted)
10323 return;
10324 list_head->emitted = true;
10326 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10328 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10330 for (dw_loc_list_ref curr = list_head; curr != NULL;
10331 curr = curr->dw_loc_next)
10333 unsigned long size;
10335 if (skip_loc_list_entry (curr, &size))
10336 continue;
10338 vcount++;
10340 /* ?? dwarf_split_debug_info? */
10341 if (dwarf2out_as_locview_support)
10343 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10345 if (!ZERO_VIEW_P (curr->vbegin))
10347 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10348 dw2_asm_output_symname_uleb128 (label,
10349 "View list begin (%s)",
10350 list_head->vl_symbol);
10352 else
10353 dw2_asm_output_data_uleb128 (0,
10354 "View list begin (%s)",
10355 list_head->vl_symbol);
10357 if (!ZERO_VIEW_P (curr->vend))
10359 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10360 dw2_asm_output_symname_uleb128 (label,
10361 "View list end (%s)",
10362 list_head->vl_symbol);
10364 else
10365 dw2_asm_output_data_uleb128 (0,
10366 "View list end (%s)",
10367 list_head->vl_symbol);
10369 else
10371 dw2_asm_output_data_uleb128 (curr->vbegin,
10372 "View list begin (%s)",
10373 list_head->vl_symbol);
10374 dw2_asm_output_data_uleb128 (curr->vend,
10375 "View list end (%s)",
10376 list_head->vl_symbol);
10381 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10383 const char *last_section = NULL;
10384 const char *base_label = NULL;
10386 /* Walk the location list, and output each range + expression. */
10387 for (dw_loc_list_ref curr = list_head; curr != NULL;
10388 curr = curr->dw_loc_next)
10390 unsigned long size;
10392 /* Skip this entry? If we skip it here, we must skip it in the
10393 view list above as well. */
10394 if (skip_loc_list_entry (curr, &size))
10395 continue;
10397 lcount++;
10399 if (dwarf_version >= 5)
10401 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10403 dwarf2out_maybe_output_loclist_view_pair (curr);
10404 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10405 uleb128 index into .debug_addr and uleb128 length. */
10406 dw2_asm_output_data (1, DW_LLE_startx_length,
10407 "DW_LLE_startx_length (%s)",
10408 list_head->ll_symbol);
10409 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10410 "Location list range start index "
10411 "(%s)", curr->begin);
10412 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10413 "Location list length (%s)",
10414 list_head->ll_symbol);
10416 else if (dwarf_split_debug_info)
10418 dwarf2out_maybe_output_loclist_view_pair (curr);
10419 /* For -gsplit-dwarf without usable .uleb128 support, emit
10420 DW_LLE_startx_endx, which has two uleb128 indexes into
10421 .debug_addr. */
10422 dw2_asm_output_data (1, DW_LLE_startx_endx,
10423 "DW_LLE_startx_endx (%s)",
10424 list_head->ll_symbol);
10425 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10426 "Location list range start index "
10427 "(%s)", curr->begin);
10428 dw2_asm_output_data_uleb128 (curr->end_entry->index,
10429 "Location list range end index "
10430 "(%s)", curr->end);
10432 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10434 dwarf2out_maybe_output_loclist_view_pair (curr);
10435 /* If all code is in .text section, the base address is
10436 already provided by the CU attributes. Use
10437 DW_LLE_offset_pair where both addresses are uleb128 encoded
10438 offsets against that base. */
10439 dw2_asm_output_data (1, DW_LLE_offset_pair,
10440 "DW_LLE_offset_pair (%s)",
10441 list_head->ll_symbol);
10442 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10443 "Location list begin address (%s)",
10444 list_head->ll_symbol);
10445 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10446 "Location list end address (%s)",
10447 list_head->ll_symbol);
10449 else if (HAVE_AS_LEB128)
10451 /* Otherwise, find out how many consecutive entries could share
10452 the same base entry. If just one, emit DW_LLE_start_length,
10453 otherwise emit DW_LLE_base_address for the base address
10454 followed by a series of DW_LLE_offset_pair. */
10455 if (last_section == NULL || curr->section != last_section)
10457 dw_loc_list_ref curr2;
10458 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10459 curr2 = curr2->dw_loc_next)
10461 if (strcmp (curr2->begin, curr2->end) == 0
10462 && !curr2->force)
10463 continue;
10464 break;
10466 if (curr2 == NULL || curr->section != curr2->section)
10467 last_section = NULL;
10468 else
10470 last_section = curr->section;
10471 base_label = curr->begin;
10472 dw2_asm_output_data (1, DW_LLE_base_address,
10473 "DW_LLE_base_address (%s)",
10474 list_head->ll_symbol);
10475 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10476 "Base address (%s)",
10477 list_head->ll_symbol);
10480 /* Only one entry with the same base address. Use
10481 DW_LLE_start_length with absolute address and uleb128
10482 length. */
10483 if (last_section == NULL)
10485 dwarf2out_maybe_output_loclist_view_pair (curr);
10486 dw2_asm_output_data (1, DW_LLE_start_length,
10487 "DW_LLE_start_length (%s)",
10488 list_head->ll_symbol);
10489 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10490 "Location list begin address (%s)",
10491 list_head->ll_symbol);
10492 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10493 "Location list length "
10494 "(%s)", list_head->ll_symbol);
10496 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10497 DW_LLE_base_address. */
10498 else
10500 dwarf2out_maybe_output_loclist_view_pair (curr);
10501 dw2_asm_output_data (1, DW_LLE_offset_pair,
10502 "DW_LLE_offset_pair (%s)",
10503 list_head->ll_symbol);
10504 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10505 "Location list begin address "
10506 "(%s)", list_head->ll_symbol);
10507 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10508 "Location list end address "
10509 "(%s)", list_head->ll_symbol);
10512 /* The assembler does not support .uleb128 directive. Emit
10513 DW_LLE_start_end with a pair of absolute addresses. */
10514 else
10516 dwarf2out_maybe_output_loclist_view_pair (curr);
10517 dw2_asm_output_data (1, DW_LLE_start_end,
10518 "DW_LLE_start_end (%s)",
10519 list_head->ll_symbol);
10520 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10521 "Location list begin address (%s)",
10522 list_head->ll_symbol);
10523 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10524 "Location list end address (%s)",
10525 list_head->ll_symbol);
10528 else if (dwarf_split_debug_info)
10530 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10531 and 4 byte length. */
10532 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10533 "Location list start/length entry (%s)",
10534 list_head->ll_symbol);
10535 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10536 "Location list range start index (%s)",
10537 curr->begin);
10538 /* The length field is 4 bytes. If we ever need to support
10539 an 8-byte length, we can add a new DW_LLE code or fall back
10540 to DW_LLE_GNU_start_end_entry. */
10541 dw2_asm_output_delta (4, curr->end, curr->begin,
10542 "Location list range length (%s)",
10543 list_head->ll_symbol);
10545 else if (!have_multiple_function_sections)
10547 /* Pair of relative addresses against start of text section. */
10548 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10549 "Location list begin address (%s)",
10550 list_head->ll_symbol);
10551 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10552 "Location list end address (%s)",
10553 list_head->ll_symbol);
10555 else
10557 /* Pair of absolute addresses. */
10558 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10559 "Location list begin address (%s)",
10560 list_head->ll_symbol);
10561 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10562 "Location list end address (%s)",
10563 list_head->ll_symbol);
10566 /* Output the block length for this list of location operations. */
10567 if (dwarf_version >= 5)
10568 dw2_asm_output_data_uleb128 (size, "Location expression size");
10569 else
10571 gcc_assert (size <= 0xffff);
10572 dw2_asm_output_data (2, size, "Location expression size");
10575 output_loc_sequence (curr->expr, -1);
10578 /* And finally list termination. */
10579 if (dwarf_version >= 5)
10580 dw2_asm_output_data (1, DW_LLE_end_of_list,
10581 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10582 else if (dwarf_split_debug_info)
10583 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10584 "Location list terminator (%s)",
10585 list_head->ll_symbol);
10586 else
10588 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10589 "Location list terminator begin (%s)",
10590 list_head->ll_symbol);
10591 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10592 "Location list terminator end (%s)",
10593 list_head->ll_symbol);
10596 gcc_assert (!list_head->vl_symbol
10597 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10600 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10601 section. Emit a relocated reference if val_entry is NULL, otherwise,
10602 emit an indirect reference. */
10604 static void
10605 output_range_list_offset (dw_attr_node *a)
10607 const char *name = dwarf_attr_name (a->dw_attr);
10609 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10611 if (dwarf_version >= 5)
10613 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10614 dw2_asm_output_offset (dwarf_offset_size, r->label,
10615 debug_ranges_section, "%s", name);
10617 else
10619 char *p = strchr (ranges_section_label, '\0');
10620 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10621 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10622 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10623 debug_ranges_section, "%s", name);
10624 *p = '\0';
10627 else if (dwarf_version >= 5)
10629 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10630 gcc_assert (rnglist_idx);
10631 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10633 else
10634 dw2_asm_output_data (dwarf_offset_size,
10635 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10636 "%s (offset from %s)", name, ranges_section_label);
10639 /* Output the offset into the debug_loc section. */
10641 static void
10642 output_loc_list_offset (dw_attr_node *a)
10644 char *sym = AT_loc_list (a)->ll_symbol;
10646 gcc_assert (sym);
10647 if (!dwarf_split_debug_info)
10648 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10649 "%s", dwarf_attr_name (a->dw_attr));
10650 else if (dwarf_version >= 5)
10652 gcc_assert (AT_loc_list (a)->num_assigned);
10653 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10654 dwarf_attr_name (a->dw_attr),
10655 sym);
10657 else
10658 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10659 "%s", dwarf_attr_name (a->dw_attr));
10662 /* Output the offset into the debug_loc section. */
10664 static void
10665 output_view_list_offset (dw_attr_node *a)
10667 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10669 gcc_assert (sym);
10670 if (dwarf_split_debug_info)
10671 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10672 "%s", dwarf_attr_name (a->dw_attr));
10673 else
10674 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10675 "%s", dwarf_attr_name (a->dw_attr));
10678 /* Output an attribute's index or value appropriately. */
10680 static void
10681 output_attr_index_or_value (dw_attr_node *a)
10683 const char *name = dwarf_attr_name (a->dw_attr);
10685 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10687 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10688 return;
10690 switch (AT_class (a))
10692 case dw_val_class_addr:
10693 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10694 break;
10695 case dw_val_class_high_pc:
10696 case dw_val_class_lbl_id:
10697 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10698 break;
10699 default:
10700 gcc_unreachable ();
10704 /* Output a type signature. */
10706 static inline void
10707 output_signature (const char *sig, const char *name)
10709 int i;
10711 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10712 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10715 /* Output a discriminant value. */
10717 static inline void
10718 output_discr_value (dw_discr_value *discr_value, const char *name)
10720 if (discr_value->pos)
10721 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10722 else
10723 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10726 /* Output the DIE and its attributes. Called recursively to generate
10727 the definitions of each child DIE. */
10729 static void
10730 output_die (dw_die_ref die)
10732 dw_attr_node *a;
10733 dw_die_ref c;
10734 unsigned long size;
10735 unsigned ix;
10737 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10738 (unsigned long)die->die_offset,
10739 dwarf_tag_name (die->die_tag));
10741 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10743 const char *name = dwarf_attr_name (a->dw_attr);
10745 switch (AT_class (a))
10747 case dw_val_class_addr:
10748 output_attr_index_or_value (a);
10749 break;
10751 case dw_val_class_offset:
10752 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10753 "%s", name);
10754 break;
10756 case dw_val_class_range_list:
10757 output_range_list_offset (a);
10758 break;
10760 case dw_val_class_loc:
10761 size = size_of_locs (AT_loc (a));
10763 /* Output the block length for this list of location operations. */
10764 if (dwarf_version >= 4)
10765 dw2_asm_output_data_uleb128 (size, "%s", name);
10766 else
10767 dw2_asm_output_data (constant_size (size), size, "%s", name);
10769 output_loc_sequence (AT_loc (a), -1);
10770 break;
10772 case dw_val_class_const:
10773 /* ??? It would be slightly more efficient to use a scheme like is
10774 used for unsigned constants below, but gdb 4.x does not sign
10775 extend. Gdb 5.x does sign extend. */
10776 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10777 break;
10779 case dw_val_class_unsigned_const:
10781 int csize = constant_size (AT_unsigned (a));
10782 if (dwarf_version == 3
10783 && a->dw_attr == DW_AT_data_member_location
10784 && csize >= 4)
10785 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10786 else
10787 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10789 break;
10791 case dw_val_class_symview:
10793 int vsize;
10794 if (symview_upper_bound <= 0xff)
10795 vsize = 1;
10796 else if (symview_upper_bound <= 0xffff)
10797 vsize = 2;
10798 else if (symview_upper_bound <= 0xffffffff)
10799 vsize = 4;
10800 else
10801 vsize = 8;
10802 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10803 "%s", name);
10805 break;
10807 case dw_val_class_const_implicit:
10808 if (flag_debug_asm)
10809 fprintf (asm_out_file, "\t\t\t%s %s ("
10810 HOST_WIDE_INT_PRINT_DEC ")\n",
10811 ASM_COMMENT_START, name, AT_int (a));
10812 break;
10814 case dw_val_class_unsigned_const_implicit:
10815 if (flag_debug_asm)
10816 fprintf (asm_out_file, "\t\t\t%s %s ("
10817 HOST_WIDE_INT_PRINT_HEX ")\n",
10818 ASM_COMMENT_START, name, AT_unsigned (a));
10819 break;
10821 case dw_val_class_const_double:
10823 unsigned HOST_WIDE_INT first, second;
10825 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10826 dw2_asm_output_data (1,
10827 HOST_BITS_PER_DOUBLE_INT
10828 / HOST_BITS_PER_CHAR,
10829 NULL);
10831 if (WORDS_BIG_ENDIAN)
10833 first = a->dw_attr_val.v.val_double.high;
10834 second = a->dw_attr_val.v.val_double.low;
10836 else
10838 first = a->dw_attr_val.v.val_double.low;
10839 second = a->dw_attr_val.v.val_double.high;
10842 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10843 first, "%s", name);
10844 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10845 second, NULL);
10847 break;
10849 case dw_val_class_wide_int:
10851 int i;
10852 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10853 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10854 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10855 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10856 * l, NULL);
10858 if (WORDS_BIG_ENDIAN)
10859 for (i = len - 1; i >= 0; --i)
10861 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10862 "%s", name);
10863 name = "";
10865 else
10866 for (i = 0; i < len; ++i)
10868 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10869 "%s", name);
10870 name = "";
10873 break;
10875 case dw_val_class_vec:
10877 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10878 unsigned int len = a->dw_attr_val.v.val_vec.length;
10879 unsigned int i;
10880 unsigned char *p;
10882 dw2_asm_output_data (constant_size (len * elt_size),
10883 len * elt_size, "%s", name);
10884 if (elt_size > sizeof (HOST_WIDE_INT))
10886 elt_size /= 2;
10887 len *= 2;
10889 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10890 i < len;
10891 i++, p += elt_size)
10892 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10893 "fp or vector constant word %u", i);
10894 break;
10897 case dw_val_class_flag:
10898 if (dwarf_version >= 4)
10900 /* Currently all add_AT_flag calls pass in 1 as last argument,
10901 so DW_FORM_flag_present can be used. If that ever changes,
10902 we'll need to use DW_FORM_flag and have some optimization
10903 in build_abbrev_table that will change those to
10904 DW_FORM_flag_present if it is set to 1 in all DIEs using
10905 the same abbrev entry. */
10906 gcc_assert (AT_flag (a) == 1);
10907 if (flag_debug_asm)
10908 fprintf (asm_out_file, "\t\t\t%s %s\n",
10909 ASM_COMMENT_START, name);
10910 break;
10912 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10913 break;
10915 case dw_val_class_loc_list:
10916 output_loc_list_offset (a);
10917 break;
10919 case dw_val_class_view_list:
10920 output_view_list_offset (a);
10921 break;
10923 case dw_val_class_die_ref:
10924 if (AT_ref_external (a))
10926 if (AT_ref (a)->comdat_type_p)
10928 comdat_type_node *type_node
10929 = AT_ref (a)->die_id.die_type_node;
10931 gcc_assert (type_node);
10932 output_signature (type_node->signature, name);
10934 else
10936 const char *sym = AT_ref (a)->die_id.die_symbol;
10937 int size;
10939 gcc_assert (sym);
10940 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10941 length, whereas in DWARF3 it's always sized as an
10942 offset. */
10943 if (dwarf_version == 2)
10944 size = DWARF2_ADDR_SIZE;
10945 else
10946 size = dwarf_offset_size;
10947 /* ??? We cannot unconditionally output die_offset if
10948 non-zero - others might create references to those
10949 DIEs via symbols.
10950 And we do not clear its DIE offset after outputting it
10951 (and the label refers to the actual DIEs, not the
10952 DWARF CU unit header which is when using label + offset
10953 would be the correct thing to do).
10954 ??? This is the reason for the with_offset flag. */
10955 if (AT_ref (a)->with_offset)
10956 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10957 debug_info_section, "%s", name);
10958 else
10959 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10960 name);
10963 else
10965 gcc_assert (AT_ref (a)->die_offset);
10966 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
10967 "%s", name);
10969 break;
10971 case dw_val_class_fde_ref:
10973 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10975 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10976 a->dw_attr_val.v.val_fde_index * 2);
10977 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
10978 "%s", name);
10980 break;
10982 case dw_val_class_vms_delta:
10983 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10984 dw2_asm_output_vms_delta (dwarf_offset_size,
10985 AT_vms_delta2 (a), AT_vms_delta1 (a),
10986 "%s", name);
10987 #else
10988 dw2_asm_output_delta (dwarf_offset_size,
10989 AT_vms_delta2 (a), AT_vms_delta1 (a),
10990 "%s", name);
10991 #endif
10992 break;
10994 case dw_val_class_lbl_id:
10995 output_attr_index_or_value (a);
10996 break;
10998 case dw_val_class_lineptr:
10999 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11000 debug_line_section, "%s", name);
11001 break;
11003 case dw_val_class_macptr:
11004 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11005 debug_macinfo_section, "%s", name);
11006 break;
11008 case dw_val_class_loclistsptr:
11009 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11010 debug_loc_section, "%s", name);
11011 break;
11013 case dw_val_class_str:
11014 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11015 dw2_asm_output_offset (dwarf_offset_size,
11016 a->dw_attr_val.v.val_str->label,
11017 debug_str_section,
11018 "%s: \"%s\"", name, AT_string (a));
11019 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11020 dw2_asm_output_offset (dwarf_offset_size,
11021 a->dw_attr_val.v.val_str->label,
11022 debug_line_str_section,
11023 "%s: \"%s\"", name, AT_string (a));
11024 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
11025 dw2_asm_output_data_uleb128 (AT_index (a),
11026 "%s: \"%s\"", name, AT_string (a));
11027 else
11028 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11029 break;
11031 case dw_val_class_file:
11033 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11035 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11036 a->dw_attr_val.v.val_file->filename);
11037 break;
11040 case dw_val_class_file_implicit:
11041 if (flag_debug_asm)
11042 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
11043 ASM_COMMENT_START, name,
11044 maybe_emit_file (a->dw_attr_val.v.val_file),
11045 a->dw_attr_val.v.val_file->filename);
11046 break;
11048 case dw_val_class_data8:
11050 int i;
11052 for (i = 0; i < 8; i++)
11053 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11054 i == 0 ? "%s" : NULL, name);
11055 break;
11058 case dw_val_class_high_pc:
11059 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11060 get_AT_low_pc (die), "DW_AT_high_pc");
11061 break;
11063 case dw_val_class_discr_value:
11064 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11065 break;
11067 case dw_val_class_discr_list:
11069 dw_discr_list_ref list = AT_discr_list (a);
11070 const int size = size_of_discr_list (list);
11072 /* This is a block, so output its length first. */
11073 dw2_asm_output_data (constant_size (size), size,
11074 "%s: block size", name);
11076 for (; list != NULL; list = list->dw_discr_next)
11078 /* One byte for the discriminant value descriptor, and then as
11079 many LEB128 numbers as required. */
11080 if (list->dw_discr_range)
11081 dw2_asm_output_data (1, DW_DSC_range,
11082 "%s: DW_DSC_range", name);
11083 else
11084 dw2_asm_output_data (1, DW_DSC_label,
11085 "%s: DW_DSC_label", name);
11087 output_discr_value (&list->dw_discr_lower_bound, name);
11088 if (list->dw_discr_range)
11089 output_discr_value (&list->dw_discr_upper_bound, name);
11091 break;
11094 default:
11095 gcc_unreachable ();
11099 FOR_EACH_CHILD (die, c, output_die (c));
11101 /* Add null byte to terminate sibling list. */
11102 if (die->die_child != NULL)
11103 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11104 (unsigned long) die->die_offset);
11107 /* Output the dwarf version number. */
11109 static void
11110 output_dwarf_version ()
11112 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11113 views in loclist. That will change eventually. */
11114 if (dwarf_version == 6)
11116 static bool once;
11117 if (!once)
11119 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11120 "incompatibilities");
11121 once = true;
11123 dw2_asm_output_data (2, 5, "DWARF version number");
11125 else
11126 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11129 /* Output the compilation unit that appears at the beginning of the
11130 .debug_info section, and precedes the DIE descriptions. */
11132 static void
11133 output_compilation_unit_header (enum dwarf_unit_type ut)
11135 if (!XCOFF_DEBUGGING_INFO)
11137 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11138 dw2_asm_output_data (4, 0xffffffff,
11139 "Initial length escape value indicating 64-bit DWARF extension");
11140 dw2_asm_output_data (dwarf_offset_size,
11141 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11142 "Length of Compilation Unit Info");
11145 output_dwarf_version ();
11146 if (dwarf_version >= 5)
11148 const char *name;
11149 switch (ut)
11151 case DW_UT_compile: name = "DW_UT_compile"; break;
11152 case DW_UT_type: name = "DW_UT_type"; break;
11153 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11154 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11155 default: gcc_unreachable ();
11157 dw2_asm_output_data (1, ut, "%s", name);
11158 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11160 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11161 debug_abbrev_section,
11162 "Offset Into Abbrev. Section");
11163 if (dwarf_version < 5)
11164 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11167 /* Output the compilation unit DIE and its children. */
11169 static void
11170 output_comp_unit (dw_die_ref die, int output_if_empty,
11171 const unsigned char *dwo_id)
11173 const char *secname, *oldsym;
11174 char *tmp;
11176 /* Unless we are outputting main CU, we may throw away empty ones. */
11177 if (!output_if_empty && die->die_child == NULL)
11178 return;
11180 /* Even if there are no children of this DIE, we must output the information
11181 about the compilation unit. Otherwise, on an empty translation unit, we
11182 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11183 will then complain when examining the file. First mark all the DIEs in
11184 this CU so we know which get local refs. */
11185 mark_dies (die);
11187 external_ref_hash_type *extern_map = optimize_external_refs (die);
11189 /* For now, optimize only the main CU, in order to optimize the rest
11190 we'd need to see all of them earlier. Leave the rest for post-linking
11191 tools like DWZ. */
11192 if (die == comp_unit_die ())
11193 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11195 build_abbrev_table (die, extern_map);
11197 optimize_abbrev_table ();
11199 delete extern_map;
11201 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11202 next_die_offset = (dwo_id
11203 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11204 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11205 calc_die_sizes (die);
11207 oldsym = die->die_id.die_symbol;
11208 if (oldsym && die->comdat_type_p)
11210 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11212 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11213 secname = tmp;
11214 die->die_id.die_symbol = NULL;
11215 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11217 else
11219 switch_to_section (debug_info_section);
11220 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11221 info_section_emitted = true;
11224 /* For LTO cross unit DIE refs we want a symbol on the start of the
11225 debuginfo section, not on the CU DIE. */
11226 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11228 /* ??? No way to get visibility assembled without a decl. */
11229 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11230 get_identifier (oldsym), char_type_node);
11231 TREE_PUBLIC (decl) = true;
11232 TREE_STATIC (decl) = true;
11233 DECL_ARTIFICIAL (decl) = true;
11234 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11235 DECL_VISIBILITY_SPECIFIED (decl) = true;
11236 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11237 #ifdef ASM_WEAKEN_LABEL
11238 /* We prefer a .weak because that handles duplicates from duplicate
11239 archive members in a graceful way. */
11240 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11241 #else
11242 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11243 #endif
11244 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11247 /* Output debugging information. */
11248 output_compilation_unit_header (dwo_id
11249 ? DW_UT_split_compile : DW_UT_compile);
11250 if (dwarf_version >= 5)
11252 if (dwo_id != NULL)
11253 for (int i = 0; i < 8; i++)
11254 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11256 output_die (die);
11258 /* Leave the marks on the main CU, so we can check them in
11259 output_pubnames. */
11260 if (oldsym)
11262 unmark_dies (die);
11263 die->die_id.die_symbol = oldsym;
11267 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11268 and .debug_pubtypes. This is configured per-target, but can be
11269 overridden by the -gpubnames or -gno-pubnames options. */
11271 static inline bool
11272 want_pubnames (void)
11274 if (debug_info_level <= DINFO_LEVEL_TERSE
11275 /* Names and types go to the early debug part only. */
11276 || in_lto_p)
11277 return false;
11278 if (debug_generate_pub_sections != -1)
11279 return debug_generate_pub_sections;
11280 return targetm.want_debug_pub_sections;
11283 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11285 static void
11286 add_AT_pubnames (dw_die_ref die)
11288 if (want_pubnames ())
11289 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11292 /* Add a string attribute value to a skeleton DIE. */
11294 static inline void
11295 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11296 const char *str)
11298 dw_attr_node attr;
11299 struct indirect_string_node *node;
11301 if (! skeleton_debug_str_hash)
11302 skeleton_debug_str_hash
11303 = hash_table<indirect_string_hasher>::create_ggc (10);
11305 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11306 find_string_form (node);
11307 if (node->form == dwarf_FORM (DW_FORM_strx))
11308 node->form = DW_FORM_strp;
11310 attr.dw_attr = attr_kind;
11311 attr.dw_attr_val.val_class = dw_val_class_str;
11312 attr.dw_attr_val.val_entry = NULL;
11313 attr.dw_attr_val.v.val_str = node;
11314 add_dwarf_attr (die, &attr);
11317 /* Helper function to generate top-level dies for skeleton debug_info and
11318 debug_types. */
11320 static void
11321 add_top_level_skeleton_die_attrs (dw_die_ref die)
11323 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11324 const char *comp_dir = comp_dir_string ();
11326 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11327 if (comp_dir != NULL)
11328 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11329 add_AT_pubnames (die);
11330 if (addr_index_table != NULL && addr_index_table->size () > 0)
11331 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11334 /* Output skeleton debug sections that point to the dwo file. */
11336 static void
11337 output_skeleton_debug_sections (dw_die_ref comp_unit,
11338 const unsigned char *dwo_id)
11340 /* These attributes will be found in the full debug_info section. */
11341 remove_AT (comp_unit, DW_AT_producer);
11342 remove_AT (comp_unit, DW_AT_language);
11344 switch_to_section (debug_skeleton_info_section);
11345 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11347 /* Produce the skeleton compilation-unit header. This one differs enough from
11348 a normal CU header that it's better not to call output_compilation_unit
11349 header. */
11350 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11351 dw2_asm_output_data (4, 0xffffffff,
11352 "Initial length escape value indicating 64-bit "
11353 "DWARF extension");
11355 dw2_asm_output_data (dwarf_offset_size,
11356 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11357 - DWARF_INITIAL_LENGTH_SIZE
11358 + size_of_die (comp_unit),
11359 "Length of Compilation Unit Info");
11360 output_dwarf_version ();
11361 if (dwarf_version >= 5)
11363 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11364 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11366 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11367 debug_skeleton_abbrev_section,
11368 "Offset Into Abbrev. Section");
11369 if (dwarf_version < 5)
11370 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11371 else
11372 for (int i = 0; i < 8; i++)
11373 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11375 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11376 output_die (comp_unit);
11378 /* Build the skeleton debug_abbrev section. */
11379 switch_to_section (debug_skeleton_abbrev_section);
11380 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11382 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11384 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11387 /* Output a comdat type unit DIE and its children. */
11389 static void
11390 output_comdat_type_unit (comdat_type_node *node,
11391 bool early_lto_debug ATTRIBUTE_UNUSED)
11393 const char *secname;
11394 char *tmp;
11395 int i;
11396 #if defined (OBJECT_FORMAT_ELF)
11397 tree comdat_key;
11398 #endif
11400 /* First mark all the DIEs in this CU so we know which get local refs. */
11401 mark_dies (node->root_die);
11403 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11405 build_abbrev_table (node->root_die, extern_map);
11407 delete extern_map;
11408 extern_map = NULL;
11410 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11411 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11412 calc_die_sizes (node->root_die);
11414 #if defined (OBJECT_FORMAT_ELF)
11415 if (dwarf_version >= 5)
11417 if (!dwarf_split_debug_info)
11418 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11419 else
11420 secname = (early_lto_debug
11421 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11423 else if (!dwarf_split_debug_info)
11424 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11425 else
11426 secname = (early_lto_debug
11427 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11429 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11430 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11431 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11432 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11433 comdat_key = get_identifier (tmp);
11434 targetm.asm_out.named_section (secname,
11435 SECTION_DEBUG | SECTION_LINKONCE,
11436 comdat_key);
11437 #else
11438 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11439 sprintf (tmp, (dwarf_version >= 5
11440 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11441 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11442 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11443 secname = tmp;
11444 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11445 #endif
11447 /* Output debugging information. */
11448 output_compilation_unit_header (dwarf_split_debug_info
11449 ? DW_UT_split_type : DW_UT_type);
11450 output_signature (node->signature, "Type Signature");
11451 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11452 "Offset to Type DIE");
11453 output_die (node->root_die);
11455 unmark_dies (node->root_die);
11458 /* Return the DWARF2/3 pubname associated with a decl. */
11460 static const char *
11461 dwarf2_name (tree decl, int scope)
11463 if (DECL_NAMELESS (decl))
11464 return NULL;
11465 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11468 /* Add a new entry to .debug_pubnames if appropriate. */
11470 static void
11471 add_pubname_string (const char *str, dw_die_ref die)
11473 pubname_entry e;
11475 e.die = die;
11476 e.name = xstrdup (str);
11477 vec_safe_push (pubname_table, e);
11480 static void
11481 add_pubname (tree decl, dw_die_ref die)
11483 if (!want_pubnames ())
11484 return;
11486 /* Don't add items to the table when we expect that the consumer will have
11487 just read the enclosing die. For example, if the consumer is looking at a
11488 class_member, it will either be inside the class already, or will have just
11489 looked up the class to find the member. Either way, searching the class is
11490 faster than searching the index. */
11491 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11492 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11494 const char *name = dwarf2_name (decl, 1);
11496 if (name)
11497 add_pubname_string (name, die);
11501 /* Add an enumerator to the pubnames section. */
11503 static void
11504 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11506 pubname_entry e;
11508 gcc_assert (scope_name);
11509 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11510 e.die = die;
11511 vec_safe_push (pubname_table, e);
11514 /* Add a new entry to .debug_pubtypes if appropriate. */
11516 static void
11517 add_pubtype (tree decl, dw_die_ref die)
11519 pubname_entry e;
11521 if (!want_pubnames ())
11522 return;
11524 if ((TREE_PUBLIC (decl)
11525 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11526 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11528 tree scope = NULL;
11529 const char *scope_name = "";
11530 const char *sep = is_cxx () ? "::" : ".";
11531 const char *name;
11533 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11534 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11536 scope_name = lang_hooks.dwarf_name (scope, 1);
11537 if (scope_name != NULL && scope_name[0] != '\0')
11538 scope_name = concat (scope_name, sep, NULL);
11539 else
11540 scope_name = "";
11543 if (TYPE_P (decl))
11544 name = type_tag (decl);
11545 else
11546 name = lang_hooks.dwarf_name (decl, 1);
11548 /* If we don't have a name for the type, there's no point in adding
11549 it to the table. */
11550 if (name != NULL && name[0] != '\0')
11552 e.die = die;
11553 e.name = concat (scope_name, name, NULL);
11554 vec_safe_push (pubtype_table, e);
11557 /* Although it might be more consistent to add the pubinfo for the
11558 enumerators as their dies are created, they should only be added if the
11559 enum type meets the criteria above. So rather than re-check the parent
11560 enum type whenever an enumerator die is created, just output them all
11561 here. This isn't protected by the name conditional because anonymous
11562 enums don't have names. */
11563 if (die->die_tag == DW_TAG_enumeration_type)
11565 dw_die_ref c;
11567 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11572 /* Output a single entry in the pubnames table. */
11574 static void
11575 output_pubname (dw_offset die_offset, pubname_entry *entry)
11577 dw_die_ref die = entry->die;
11578 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11580 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11582 if (debug_generate_pub_sections == 2)
11584 /* This logic follows gdb's method for determining the value of the flag
11585 byte. */
11586 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11587 switch (die->die_tag)
11589 case DW_TAG_typedef:
11590 case DW_TAG_base_type:
11591 case DW_TAG_subrange_type:
11592 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11593 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11594 break;
11595 case DW_TAG_enumerator:
11596 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11597 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11598 if (!is_cxx ())
11599 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11600 break;
11601 case DW_TAG_subprogram:
11602 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11603 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11604 if (!is_ada ())
11605 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11606 break;
11607 case DW_TAG_constant:
11608 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11609 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11610 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11611 break;
11612 case DW_TAG_variable:
11613 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11614 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11615 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11616 break;
11617 case DW_TAG_namespace:
11618 case DW_TAG_imported_declaration:
11619 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11620 break;
11621 case DW_TAG_class_type:
11622 case DW_TAG_interface_type:
11623 case DW_TAG_structure_type:
11624 case DW_TAG_union_type:
11625 case DW_TAG_enumeration_type:
11626 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11627 if (!is_cxx ())
11628 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11629 break;
11630 default:
11631 /* An unusual tag. Leave the flag-byte empty. */
11632 break;
11634 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11635 "GDB-index flags");
11638 dw2_asm_output_nstring (entry->name, -1, "external name");
11642 /* Output the public names table used to speed up access to externally
11643 visible names; or the public types table used to find type definitions. */
11645 static void
11646 output_pubnames (vec<pubname_entry, va_gc> *names)
11648 unsigned i;
11649 unsigned long pubnames_length = size_of_pubnames (names);
11650 pubname_entry *pub;
11652 if (!XCOFF_DEBUGGING_INFO)
11654 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11655 dw2_asm_output_data (4, 0xffffffff,
11656 "Initial length escape value indicating 64-bit DWARF extension");
11657 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11658 "Pub Info Length");
11661 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11662 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11664 if (dwarf_split_debug_info)
11665 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11666 debug_skeleton_info_section,
11667 "Offset of Compilation Unit Info");
11668 else
11669 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11670 debug_info_section,
11671 "Offset of Compilation Unit Info");
11672 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11673 "Compilation Unit Length");
11675 FOR_EACH_VEC_ELT (*names, i, pub)
11677 if (include_pubname_in_output (names, pub))
11679 dw_offset die_offset = pub->die->die_offset;
11681 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11682 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11683 gcc_assert (pub->die->die_mark);
11685 /* If we're putting types in their own .debug_types sections,
11686 the .debug_pubtypes table will still point to the compile
11687 unit (not the type unit), so we want to use the offset of
11688 the skeleton DIE (if there is one). */
11689 if (pub->die->comdat_type_p && names == pubtype_table)
11691 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11693 if (type_node != NULL)
11694 die_offset = (type_node->skeleton_die != NULL
11695 ? type_node->skeleton_die->die_offset
11696 : comp_unit_die ()->die_offset);
11699 output_pubname (die_offset, pub);
11703 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11706 /* Output public names and types tables if necessary. */
11708 static void
11709 output_pubtables (void)
11711 if (!want_pubnames () || !info_section_emitted)
11712 return;
11714 switch_to_section (debug_pubnames_section);
11715 output_pubnames (pubname_table);
11716 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11717 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11718 simply won't look for the section. */
11719 switch_to_section (debug_pubtypes_section);
11720 output_pubnames (pubtype_table);
11724 /* Output the information that goes into the .debug_aranges table.
11725 Namely, define the beginning and ending address range of the
11726 text section generated for this compilation unit. */
11728 static void
11729 output_aranges (void)
11731 unsigned i;
11732 unsigned long aranges_length = size_of_aranges ();
11734 if (!XCOFF_DEBUGGING_INFO)
11736 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11737 dw2_asm_output_data (4, 0xffffffff,
11738 "Initial length escape value indicating 64-bit DWARF extension");
11739 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11740 "Length of Address Ranges Info");
11743 /* Version number for aranges is still 2, even up to DWARF5. */
11744 dw2_asm_output_data (2, 2, "DWARF aranges version");
11745 if (dwarf_split_debug_info)
11746 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11747 debug_skeleton_info_section,
11748 "Offset of Compilation Unit Info");
11749 else
11750 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11751 debug_info_section,
11752 "Offset of Compilation Unit Info");
11753 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11754 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11756 /* We need to align to twice the pointer size here. */
11757 if (DWARF_ARANGES_PAD_SIZE)
11759 /* Pad using a 2 byte words so that padding is correct for any
11760 pointer size. */
11761 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11762 2 * DWARF2_ADDR_SIZE);
11763 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11764 dw2_asm_output_data (2, 0, NULL);
11767 /* It is necessary not to output these entries if the sections were
11768 not used; if the sections were not used, the length will be 0 and
11769 the address may end up as 0 if the section is discarded by ld
11770 --gc-sections, leaving an invalid (0, 0) entry that can be
11771 confused with the terminator. */
11772 if (switch_text_ranges)
11774 const char *prev_loc = text_section_label;
11775 const char *loc;
11776 unsigned idx;
11778 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11779 if (prev_loc)
11781 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11782 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11783 prev_loc = NULL;
11785 else
11786 prev_loc = loc;
11788 if (prev_loc)
11790 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11791 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11792 prev_loc, "Length");
11796 if (switch_cold_ranges)
11798 const char *prev_loc = cold_text_section_label;
11799 const char *loc;
11800 unsigned idx;
11802 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11803 if (prev_loc)
11805 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11806 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11807 prev_loc = NULL;
11809 else
11810 prev_loc = loc;
11812 if (prev_loc)
11814 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11815 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11816 prev_loc, "Length");
11820 if (have_multiple_function_sections)
11822 unsigned fde_idx;
11823 dw_fde_ref fde;
11825 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11827 if (fde->ignored_debug)
11828 continue;
11829 if (!fde->in_std_section)
11831 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11832 "Address");
11833 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11834 fde->dw_fde_begin, "Length");
11836 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11838 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11839 "Address");
11840 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11841 fde->dw_fde_second_begin, "Length");
11846 /* Output the terminator words. */
11847 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11848 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11851 /* Add a new entry to .debug_ranges. Return its index into
11852 ranges_table vector. */
11854 static unsigned int
11855 add_ranges_num (int num, bool maybe_new_sec)
11857 dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11858 vec_safe_push (ranges_table, r);
11859 return vec_safe_length (ranges_table) - 1;
11862 /* Add a new entry to .debug_ranges corresponding to a block, or a
11863 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11864 this entry might be in a different section from previous range. */
11866 static unsigned int
11867 add_ranges (const_tree block, bool maybe_new_sec)
11869 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11872 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11873 chain, or middle entry of a chain that will be directly referred to. */
11875 static void
11876 note_rnglist_head (unsigned int offset)
11878 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11879 return;
11880 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11883 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11884 When using dwarf_split_debug_info, address attributes in dies destined
11885 for the final executable should be direct references--setting the
11886 parameter force_direct ensures this behavior. */
11888 static void
11889 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11890 bool *added, bool force_direct)
11892 unsigned int in_use = vec_safe_length (ranges_by_label);
11893 unsigned int offset;
11894 dw_ranges_by_label rbl = { begin, end };
11895 vec_safe_push (ranges_by_label, rbl);
11896 offset = add_ranges_num (-(int)in_use - 1, true);
11897 if (!*added)
11899 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11900 *added = true;
11901 note_rnglist_head (offset);
11902 if (dwarf_split_debug_info && force_direct)
11903 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11907 /* Emit .debug_ranges section. */
11909 static void
11910 output_ranges (void)
11912 unsigned i;
11913 static const char *const start_fmt = "Offset %#x";
11914 const char *fmt = start_fmt;
11915 dw_ranges *r;
11917 switch_to_section (debug_ranges_section);
11918 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11919 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11921 int block_num = r->num;
11923 if (block_num > 0)
11925 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11926 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11928 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11929 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11931 /* If all code is in the text section, then the compilation
11932 unit base address defaults to DW_AT_low_pc, which is the
11933 base of the text section. */
11934 if (!have_multiple_function_sections)
11936 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11937 text_section_label,
11938 fmt, i * 2 * DWARF2_ADDR_SIZE);
11939 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11940 text_section_label, NULL);
11943 /* Otherwise, the compilation unit base address is zero,
11944 which allows us to use absolute addresses, and not worry
11945 about whether the target supports cross-section
11946 arithmetic. */
11947 else
11949 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11950 fmt, i * 2 * DWARF2_ADDR_SIZE);
11951 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11954 fmt = NULL;
11957 /* Negative block_num stands for an index into ranges_by_label. */
11958 else if (block_num < 0)
11960 int lab_idx = - block_num - 1;
11962 if (!have_multiple_function_sections)
11964 gcc_unreachable ();
11965 #if 0
11966 /* If we ever use add_ranges_by_labels () for a single
11967 function section, all we have to do is to take out
11968 the #if 0 above. */
11969 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11970 (*ranges_by_label)[lab_idx].begin,
11971 text_section_label,
11972 fmt, i * 2 * DWARF2_ADDR_SIZE);
11973 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11974 (*ranges_by_label)[lab_idx].end,
11975 text_section_label, NULL);
11976 #endif
11978 else
11980 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11981 (*ranges_by_label)[lab_idx].begin,
11982 fmt, i * 2 * DWARF2_ADDR_SIZE);
11983 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11984 (*ranges_by_label)[lab_idx].end,
11985 NULL);
11988 else
11990 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11991 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11992 fmt = start_fmt;
11997 /* Non-zero if .debug_line_str should be used for .debug_line section
11998 strings or strings that are likely shareable with those. */
11999 #define DWARF5_USE_DEBUG_LINE_STR \
12000 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
12001 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
12002 /* FIXME: there is no .debug_line_str.dwo section, \
12003 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
12004 && !dwarf_split_debug_info)
12007 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
12008 DWARF5 .debug_line tables using .debug_line_str or we generate
12009 it ourselves, except for split-dwarf which doesn't have a
12010 .debug_line_str. */
12011 static bool
12012 asm_outputs_debug_line_str (void)
12014 if (dwarf_version >= 5
12015 && ! output_asm_line_debug_info ()
12016 && DWARF5_USE_DEBUG_LINE_STR)
12017 return true;
12018 else
12020 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12021 return !dwarf_split_debug_info && dwarf_version >= 5;
12022 #else
12023 return false;
12024 #endif
12028 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12029 I is index of the following range. */
12031 static bool
12032 use_distinct_base_address_for_range (unsigned int i)
12034 if (i >= vec_safe_length (ranges_table))
12035 return false;
12037 dw_ranges *r2 = &(*ranges_table)[i];
12038 /* Use DW_RLE_base_address{,x} if there is a next range in the
12039 range list and is guaranteed to be in the same section. */
12040 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12043 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12044 section when needed. */
12046 static void
12047 index_rnglists (void)
12049 unsigned i;
12050 dw_ranges *r;
12051 bool base = false;
12053 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12055 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12056 r->idx = rnglist_idx++;
12058 if (!have_multiple_function_sections)
12059 continue;
12060 int block_num = r->num;
12061 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12062 base = false;
12063 if (block_num > 0)
12065 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12066 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12068 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12069 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12071 if (HAVE_AS_LEB128)
12073 if (!base && use_distinct_base_address_for_range (i + 1))
12075 r->begin_entry = add_addr_table_entry (xstrdup (blabel),
12076 ate_kind_label);
12077 base = true;
12079 if (base)
12080 /* If we have a base, no need for further
12081 begin_entry/end_entry, as DW_RLE_offset_pair will be
12082 used. */
12083 continue;
12084 r->begin_entry
12085 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12086 /* No need for end_entry, DW_RLE_start{,x}_length will use
12087 length as opposed to a pair of addresses. */
12089 else
12091 r->begin_entry
12092 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12093 r->end_entry
12094 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12098 /* Negative block_num stands for an index into ranges_by_label. */
12099 else if (block_num < 0)
12101 int lab_idx = - block_num - 1;
12102 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12103 const char *elabel = (*ranges_by_label)[lab_idx].end;
12105 r->begin_entry
12106 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12107 if (!HAVE_AS_LEB128)
12108 r->end_entry
12109 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12114 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12116 static bool
12117 output_rnglists (unsigned generation, bool dwo)
12119 unsigned i;
12120 dw_ranges *r;
12121 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12122 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12123 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12125 if (dwo)
12126 switch_to_section (debug_ranges_dwo_section);
12127 else
12129 switch_to_section (debug_ranges_section);
12130 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12132 /* There are up to 4 unique ranges labels per generation.
12133 See also init_sections_and_labels. */
12134 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12135 2 + 2 * dwo + generation * 6);
12136 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12137 3 + 2 * dwo + generation * 6);
12138 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12139 dw2_asm_output_data (4, 0xffffffff,
12140 "Initial length escape value indicating "
12141 "64-bit DWARF extension");
12142 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12143 "Length of Range Lists");
12144 ASM_OUTPUT_LABEL (asm_out_file, l1);
12145 output_dwarf_version ();
12146 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12147 dw2_asm_output_data (1, 0, "Segment Size");
12148 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12149 about relocation sizes and primarily care about the size of .debug*
12150 sections in linked shared libraries and executables, then
12151 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12152 into it are usually larger than just DW_FORM_sec_offset offsets
12153 into the .debug_rnglists section. */
12154 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12155 "Offset Entry Count");
12156 if (dwo)
12158 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12159 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12160 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12161 dw2_asm_output_delta (dwarf_offset_size, r->label,
12162 ranges_base_label, NULL);
12165 const char *lab = "";
12166 const char *base = NULL;
12167 bool skipping = false;
12168 bool ret = false;
12169 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12171 int block_num = r->num;
12173 if (r->label)
12175 if (dwarf_split_debug_info
12176 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12178 ret = true;
12179 skipping = true;
12180 continue;
12182 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12183 lab = r->label;
12185 if (skipping)
12187 if (block_num == 0)
12188 skipping = false;
12189 continue;
12191 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12192 base = NULL;
12193 if (block_num > 0)
12195 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12196 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12198 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12199 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12201 if (HAVE_AS_LEB128)
12203 /* If all code is in the text section, then the compilation
12204 unit base address defaults to DW_AT_low_pc, which is the
12205 base of the text section. */
12206 if (!have_multiple_function_sections)
12208 dw2_asm_output_data (1, DW_RLE_offset_pair,
12209 "DW_RLE_offset_pair (%s)", lab);
12210 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12211 "Range begin address (%s)", lab);
12212 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12213 "Range end address (%s)", lab);
12214 continue;
12216 if (base == NULL && use_distinct_base_address_for_range (i + 1))
12218 if (dwarf_split_debug_info)
12220 dw2_asm_output_data (1, DW_RLE_base_addressx,
12221 "DW_RLE_base_addressx (%s)", lab);
12222 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12223 "Base address index (%s)",
12224 blabel);
12226 else
12228 dw2_asm_output_data (1, DW_RLE_base_address,
12229 "DW_RLE_base_address (%s)", lab);
12230 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12231 "Base address (%s)", lab);
12233 strcpy (basebuf, blabel);
12234 base = basebuf;
12236 if (base)
12238 dw2_asm_output_data (1, DW_RLE_offset_pair,
12239 "DW_RLE_offset_pair (%s)", lab);
12240 dw2_asm_output_delta_uleb128 (blabel, base,
12241 "Range begin address (%s)", lab);
12242 dw2_asm_output_delta_uleb128 (elabel, base,
12243 "Range end address (%s)", lab);
12244 continue;
12246 if (dwarf_split_debug_info)
12248 dw2_asm_output_data (1, DW_RLE_startx_length,
12249 "DW_RLE_startx_length (%s)", lab);
12250 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12251 "Range begin address index "
12252 "(%s)", blabel);
12254 else
12256 dw2_asm_output_data (1, DW_RLE_start_length,
12257 "DW_RLE_start_length (%s)", lab);
12258 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12259 "Range begin address (%s)", lab);
12261 dw2_asm_output_delta_uleb128 (elabel, blabel,
12262 "Range length (%s)", lab);
12264 else if (dwarf_split_debug_info)
12266 dw2_asm_output_data (1, DW_RLE_startx_endx,
12267 "DW_RLE_startx_endx (%s)", lab);
12268 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12269 "Range begin address index "
12270 "(%s)", blabel);
12271 dw2_asm_output_data_uleb128 (r->end_entry->index,
12272 "Range end address index "
12273 "(%s)", elabel);
12275 else
12277 dw2_asm_output_data (1, DW_RLE_start_end,
12278 "DW_RLE_start_end (%s)", lab);
12279 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12280 "Range begin address (%s)", lab);
12281 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12282 "Range end address (%s)", lab);
12286 /* Negative block_num stands for an index into ranges_by_label. */
12287 else if (block_num < 0)
12289 int lab_idx = - block_num - 1;
12290 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12291 const char *elabel = (*ranges_by_label)[lab_idx].end;
12293 if (!have_multiple_function_sections)
12294 gcc_unreachable ();
12295 if (HAVE_AS_LEB128)
12297 if (dwarf_split_debug_info)
12299 dw2_asm_output_data (1, DW_RLE_startx_length,
12300 "DW_RLE_startx_length (%s)", lab);
12301 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12302 "Range begin address index "
12303 "(%s)", blabel);
12305 else
12307 dw2_asm_output_data (1, DW_RLE_start_length,
12308 "DW_RLE_start_length (%s)", lab);
12309 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12310 "Range begin address (%s)", lab);
12312 dw2_asm_output_delta_uleb128 (elabel, blabel,
12313 "Range length (%s)", lab);
12315 else if (dwarf_split_debug_info)
12317 dw2_asm_output_data (1, DW_RLE_startx_endx,
12318 "DW_RLE_startx_endx (%s)", lab);
12319 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12320 "Range begin address index "
12321 "(%s)", blabel);
12322 dw2_asm_output_data_uleb128 (r->end_entry->index,
12323 "Range end address index "
12324 "(%s)", elabel);
12326 else
12328 dw2_asm_output_data (1, DW_RLE_start_end,
12329 "DW_RLE_start_end (%s)", lab);
12330 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12331 "Range begin address (%s)", lab);
12332 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12333 "Range end address (%s)", lab);
12336 else
12337 dw2_asm_output_data (1, DW_RLE_end_of_list,
12338 "DW_RLE_end_of_list (%s)", lab);
12340 ASM_OUTPUT_LABEL (asm_out_file, l2);
12341 return ret;
12344 /* Data structure containing information about input files. */
12345 struct file_info
12347 const char *path; /* Complete file name. */
12348 const char *fname; /* File name part. */
12349 int length; /* Length of entire string. */
12350 struct dwarf_file_data * file_idx; /* Index in input file table. */
12351 int dir_idx; /* Index in directory table. */
12354 /* Data structure containing information about directories with source
12355 files. */
12356 struct dir_info
12358 const char *path; /* Path including directory name. */
12359 int length; /* Path length. */
12360 int prefix; /* Index of directory entry which is a prefix. */
12361 int count; /* Number of files in this directory. */
12362 int dir_idx; /* Index of directory used as base. */
12365 /* Callback function for file_info comparison. We sort by looking at
12366 the directories in the path. */
12368 static int
12369 file_info_cmp (const void *p1, const void *p2)
12371 const struct file_info *const s1 = (const struct file_info *) p1;
12372 const struct file_info *const s2 = (const struct file_info *) p2;
12373 const unsigned char *cp1;
12374 const unsigned char *cp2;
12376 /* Take care of file names without directories. We need to make sure that
12377 we return consistent values to qsort since some will get confused if
12378 we return the same value when identical operands are passed in opposite
12379 orders. So if neither has a directory, return 0 and otherwise return
12380 1 or -1 depending on which one has the directory. We want the one with
12381 the directory to sort after the one without, so all no directory files
12382 are at the start (normally only the compilation unit file). */
12383 if ((s1->path == s1->fname || s2->path == s2->fname))
12384 return (s2->path == s2->fname) - (s1->path == s1->fname);
12386 cp1 = (const unsigned char *) s1->path;
12387 cp2 = (const unsigned char *) s2->path;
12389 while (1)
12391 ++cp1;
12392 ++cp2;
12393 /* Reached the end of the first path? If so, handle like above,
12394 but now we want longer directory prefixes before shorter ones. */
12395 if ((cp1 == (const unsigned char *) s1->fname)
12396 || (cp2 == (const unsigned char *) s2->fname))
12397 return ((cp1 == (const unsigned char *) s1->fname)
12398 - (cp2 == (const unsigned char *) s2->fname));
12400 /* Character of current path component the same? */
12401 else if (*cp1 != *cp2)
12402 return *cp1 - *cp2;
12406 struct file_name_acquire_data
12408 struct file_info *files;
12409 int used_files;
12410 int max_files;
12413 /* Traversal function for the hash table. */
12416 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12418 struct dwarf_file_data *d = *slot;
12419 struct file_info *fi;
12420 const char *f;
12422 gcc_assert (fnad->max_files >= d->emitted_number);
12424 if (! d->emitted_number)
12425 return 1;
12427 gcc_assert (fnad->max_files != fnad->used_files);
12429 fi = fnad->files + fnad->used_files++;
12431 f = remap_debug_filename (d->filename);
12433 /* Skip all leading "./". */
12434 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12435 f += 2;
12437 /* Create a new array entry. */
12438 fi->path = f;
12439 fi->length = strlen (f);
12440 fi->file_idx = d;
12442 /* Search for the file name part. */
12443 f = strrchr (f, DIR_SEPARATOR);
12444 #if defined (DIR_SEPARATOR_2)
12446 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12448 if (g != NULL)
12450 if (f == NULL || f < g)
12451 f = g;
12454 #endif
12456 fi->fname = f == NULL ? fi->path : f + 1;
12457 return 1;
12460 /* Helper function for output_file_names. Emit a FORM encoded
12461 string STR, with assembly comment start ENTRY_KIND and
12462 index IDX */
12464 static void
12465 output_line_string (enum dwarf_form form, const char *str,
12466 const char *entry_kind, unsigned int idx)
12468 switch (form)
12470 case DW_FORM_string:
12471 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12472 break;
12473 case DW_FORM_line_strp:
12474 if (!debug_line_str_hash)
12475 debug_line_str_hash
12476 = hash_table<indirect_string_hasher>::create_ggc (10);
12478 struct indirect_string_node *node;
12479 node = find_AT_string_in_table (str, debug_line_str_hash);
12480 set_indirect_string (node);
12481 node->form = form;
12482 dw2_asm_output_offset (dwarf_offset_size, node->label,
12483 debug_line_str_section, "%s: %#x: \"%s\"",
12484 entry_kind, 0, node->str);
12485 break;
12486 default:
12487 gcc_unreachable ();
12491 /* Output the directory table and the file name table. We try to minimize
12492 the total amount of memory needed. A heuristic is used to avoid large
12493 slowdowns with many input files. */
12495 static void
12496 output_file_names (void)
12498 struct file_name_acquire_data fnad;
12499 int numfiles;
12500 struct file_info *files;
12501 struct dir_info *dirs;
12502 int *saved;
12503 int *savehere;
12504 int *backmap;
12505 int ndirs;
12506 int idx_offset;
12507 int i;
12509 if (!last_emitted_file)
12511 if (dwarf_version >= 5)
12513 const char *comp_dir = comp_dir_string ();
12514 if (comp_dir == NULL)
12515 comp_dir = "";
12516 dw2_asm_output_data (1, 1, "Directory entry format count");
12517 enum dwarf_form str_form = DW_FORM_string;
12518 if (DWARF5_USE_DEBUG_LINE_STR)
12519 str_form = DW_FORM_line_strp;
12520 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12521 dw2_asm_output_data_uleb128 (str_form, "%s",
12522 get_DW_FORM_name (str_form));
12523 dw2_asm_output_data_uleb128 (1, "Directories count");
12524 if (str_form == DW_FORM_string)
12525 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12526 else
12527 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12528 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12529 if (filename0 == NULL)
12530 filename0 = "";
12531 #ifdef VMS_DEBUGGING_INFO
12532 dw2_asm_output_data (1, 4, "File name entry format count");
12533 #else
12534 dw2_asm_output_data (1, 2, "File name entry format count");
12535 #endif
12536 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12537 dw2_asm_output_data_uleb128 (str_form, "%s",
12538 get_DW_FORM_name (str_form));
12539 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12540 "DW_LNCT_directory_index");
12541 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12542 get_DW_FORM_name (DW_FORM_data1));
12543 #ifdef VMS_DEBUGGING_INFO
12544 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12545 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12546 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12547 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12548 #endif
12549 dw2_asm_output_data_uleb128 (1, "File names count");
12551 output_line_string (str_form, filename0, "File Entry", 0);
12552 dw2_asm_output_data (1, 0, NULL);
12553 #ifdef VMS_DEBUGGING_INFO
12554 dw2_asm_output_data_uleb128 (0, NULL);
12555 dw2_asm_output_data_uleb128 (0, NULL);
12556 #endif
12558 else
12560 dw2_asm_output_data (1, 0, "End directory table");
12561 dw2_asm_output_data (1, 0, "End file name table");
12563 return;
12566 numfiles = last_emitted_file->emitted_number;
12568 /* Allocate the various arrays we need. */
12569 files = XALLOCAVEC (struct file_info, numfiles);
12570 dirs = XALLOCAVEC (struct dir_info, numfiles);
12572 fnad.files = files;
12573 fnad.used_files = 0;
12574 fnad.max_files = numfiles;
12575 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12576 gcc_assert (fnad.used_files == fnad.max_files);
12578 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12580 /* Find all the different directories used. */
12581 dirs[0].path = files[0].path;
12582 dirs[0].length = files[0].fname - files[0].path;
12583 dirs[0].prefix = -1;
12584 dirs[0].count = 1;
12585 dirs[0].dir_idx = 0;
12586 files[0].dir_idx = 0;
12587 ndirs = 1;
12589 for (i = 1; i < numfiles; i++)
12590 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12591 && memcmp (dirs[ndirs - 1].path, files[i].path,
12592 dirs[ndirs - 1].length) == 0)
12594 /* Same directory as last entry. */
12595 files[i].dir_idx = ndirs - 1;
12596 ++dirs[ndirs - 1].count;
12598 else
12600 int j;
12602 /* This is a new directory. */
12603 dirs[ndirs].path = files[i].path;
12604 dirs[ndirs].length = files[i].fname - files[i].path;
12605 dirs[ndirs].count = 1;
12606 dirs[ndirs].dir_idx = ndirs;
12607 files[i].dir_idx = ndirs;
12609 /* Search for a prefix. */
12610 dirs[ndirs].prefix = -1;
12611 for (j = 0; j < ndirs; j++)
12612 if (dirs[j].length < dirs[ndirs].length
12613 && dirs[j].length > 1
12614 && (dirs[ndirs].prefix == -1
12615 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12616 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12617 dirs[ndirs].prefix = j;
12619 ++ndirs;
12622 /* Now to the actual work. We have to find a subset of the directories which
12623 allow expressing the file name using references to the directory table
12624 with the least amount of characters. We do not do an exhaustive search
12625 where we would have to check out every combination of every single
12626 possible prefix. Instead we use a heuristic which provides nearly optimal
12627 results in most cases and never is much off. */
12628 saved = XALLOCAVEC (int, ndirs);
12629 savehere = XALLOCAVEC (int, ndirs);
12631 memset (saved, '\0', ndirs * sizeof (saved[0]));
12632 for (i = 0; i < ndirs; i++)
12634 int j;
12635 int total;
12637 /* We can always save some space for the current directory. But this
12638 does not mean it will be enough to justify adding the directory. */
12639 savehere[i] = dirs[i].length;
12640 total = (savehere[i] - saved[i]) * dirs[i].count;
12642 for (j = i + 1; j < ndirs; j++)
12644 savehere[j] = 0;
12645 if (saved[j] < dirs[i].length)
12647 /* Determine whether the dirs[i] path is a prefix of the
12648 dirs[j] path. */
12649 int k;
12651 k = dirs[j].prefix;
12652 while (k != -1 && k != (int) i)
12653 k = dirs[k].prefix;
12655 if (k == (int) i)
12657 /* Yes it is. We can possibly save some memory by
12658 writing the filenames in dirs[j] relative to
12659 dirs[i]. */
12660 savehere[j] = dirs[i].length;
12661 total += (savehere[j] - saved[j]) * dirs[j].count;
12666 /* Check whether we can save enough to justify adding the dirs[i]
12667 directory. */
12668 if (total > dirs[i].length + 1)
12670 /* It's worthwhile adding. */
12671 for (j = i; j < ndirs; j++)
12672 if (savehere[j] > 0)
12674 /* Remember how much we saved for this directory so far. */
12675 saved[j] = savehere[j];
12677 /* Remember the prefix directory. */
12678 dirs[j].dir_idx = i;
12683 /* Emit the directory name table. */
12684 idx_offset = dirs[0].length > 0 ? 1 : 0;
12685 enum dwarf_form str_form = DW_FORM_string;
12686 enum dwarf_form idx_form = DW_FORM_udata;
12687 if (dwarf_version >= 5)
12689 const char *comp_dir = comp_dir_string ();
12690 if (comp_dir == NULL)
12691 comp_dir = "";
12692 dw2_asm_output_data (1, 1, "Directory entry format count");
12693 if (DWARF5_USE_DEBUG_LINE_STR)
12694 str_form = DW_FORM_line_strp;
12695 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12696 dw2_asm_output_data_uleb128 (str_form, "%s",
12697 get_DW_FORM_name (str_form));
12698 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12699 if (str_form == DW_FORM_string)
12701 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12702 for (i = 1 - idx_offset; i < ndirs; i++)
12703 dw2_asm_output_nstring (dirs[i].path,
12704 dirs[i].length
12705 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12706 "Directory Entry: %#x", i + idx_offset);
12708 else
12710 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12711 for (i = 1 - idx_offset; i < ndirs; i++)
12713 const char *str
12714 = ggc_alloc_string (dirs[i].path,
12715 dirs[i].length
12716 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12717 output_line_string (str_form, str, "Directory Entry",
12718 (unsigned) i + idx_offset);
12722 else
12724 for (i = 1 - idx_offset; i < ndirs; i++)
12725 dw2_asm_output_nstring (dirs[i].path,
12726 dirs[i].length
12727 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12728 "Directory Entry: %#x", i + idx_offset);
12730 dw2_asm_output_data (1, 0, "End directory table");
12733 /* We have to emit them in the order of emitted_number since that's
12734 used in the debug info generation. To do this efficiently we
12735 generate a back-mapping of the indices first. */
12736 backmap = XALLOCAVEC (int, numfiles);
12737 for (i = 0; i < numfiles; i++)
12738 backmap[files[i].file_idx->emitted_number - 1] = i;
12740 if (dwarf_version >= 5)
12742 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12743 if (filename0 == NULL)
12744 filename0 = "";
12745 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12746 DW_FORM_data2. Choose one based on the number of directories
12747 and how much space would they occupy in each encoding.
12748 If we have at most 256 directories, all indexes fit into
12749 a single byte, so DW_FORM_data1 is most compact (if there
12750 are at most 128 directories, DW_FORM_udata would be as
12751 compact as that, but not shorter and slower to decode). */
12752 if (ndirs + idx_offset <= 256)
12753 idx_form = DW_FORM_data1;
12754 /* If there are more than 65536 directories, we have to use
12755 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12756 Otherwise, compute what space would occupy if all the indexes
12757 used DW_FORM_udata - sum - and compare that to how large would
12758 be DW_FORM_data2 encoding, and pick the more efficient one. */
12759 else if (ndirs + idx_offset <= 65536)
12761 unsigned HOST_WIDE_INT sum = 1;
12762 for (i = 0; i < numfiles; i++)
12764 int file_idx = backmap[i];
12765 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12766 sum += size_of_uleb128 (dir_idx);
12768 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12769 idx_form = DW_FORM_data2;
12771 #ifdef VMS_DEBUGGING_INFO
12772 dw2_asm_output_data (1, 4, "File name entry format count");
12773 #else
12774 dw2_asm_output_data (1, 2, "File name entry format count");
12775 #endif
12776 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12777 dw2_asm_output_data_uleb128 (str_form, "%s",
12778 get_DW_FORM_name (str_form));
12779 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12780 "DW_LNCT_directory_index");
12781 dw2_asm_output_data_uleb128 (idx_form, "%s",
12782 get_DW_FORM_name (idx_form));
12783 #ifdef VMS_DEBUGGING_INFO
12784 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12785 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12786 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12787 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12788 #endif
12789 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12791 output_line_string (str_form, filename0, "File Entry", 0);
12793 /* Include directory index. */
12794 if (idx_form != DW_FORM_udata)
12795 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12796 0, NULL);
12797 else
12798 dw2_asm_output_data_uleb128 (0, NULL);
12800 #ifdef VMS_DEBUGGING_INFO
12801 dw2_asm_output_data_uleb128 (0, NULL);
12802 dw2_asm_output_data_uleb128 (0, NULL);
12803 #endif
12806 /* Now write all the file names. */
12807 for (i = 0; i < numfiles; i++)
12809 int file_idx = backmap[i];
12810 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12812 #ifdef VMS_DEBUGGING_INFO
12813 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12815 /* Setting these fields can lead to debugger miscomparisons,
12816 but VMS Debug requires them to be set correctly. */
12818 int ver;
12819 long long cdt;
12820 long siz;
12821 int maxfilelen = (strlen (files[file_idx].path)
12822 + dirs[dir_idx].length
12823 + MAX_VMS_VERSION_LEN + 1);
12824 char *filebuf = XALLOCAVEC (char, maxfilelen);
12826 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12827 snprintf (filebuf, maxfilelen, "%s;%d",
12828 files[file_idx].path + dirs[dir_idx].length, ver);
12830 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12832 /* Include directory index. */
12833 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12834 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12835 dir_idx + idx_offset, NULL);
12836 else
12837 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12839 /* Modification time. */
12840 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12841 &cdt, 0, 0, 0) == 0)
12842 ? cdt : 0, NULL);
12844 /* File length in bytes. */
12845 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12846 0, &siz, 0, 0) == 0)
12847 ? siz : 0, NULL);
12848 #else
12849 output_line_string (str_form,
12850 files[file_idx].path + dirs[dir_idx].length,
12851 "File Entry", (unsigned) i + 1);
12853 /* Include directory index. */
12854 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12855 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12856 dir_idx + idx_offset, NULL);
12857 else
12858 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12860 if (dwarf_version >= 5)
12861 continue;
12863 /* Modification time. */
12864 dw2_asm_output_data_uleb128 (0, NULL);
12866 /* File length in bytes. */
12867 dw2_asm_output_data_uleb128 (0, NULL);
12868 #endif /* VMS_DEBUGGING_INFO */
12871 if (dwarf_version < 5)
12872 dw2_asm_output_data (1, 0, "End file name table");
12876 /* Output one line number table into the .debug_line section. */
12878 static void
12879 output_one_line_info_table (dw_line_info_table *table)
12881 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12882 unsigned int current_line = 1;
12883 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12884 dw_line_info_entry *ent, *prev_addr;
12885 size_t i;
12886 unsigned int view;
12888 view = 0;
12890 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12892 switch (ent->opcode)
12894 case LI_set_address:
12895 /* ??? Unfortunately, we have little choice here currently, and
12896 must always use the most general form. GCC does not know the
12897 address delta itself, so we can't use DW_LNS_advance_pc. Many
12898 ports do have length attributes which will give an upper bound
12899 on the address range. We could perhaps use length attributes
12900 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12901 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12903 view = 0;
12905 /* This can handle any delta. This takes
12906 4+DWARF2_ADDR_SIZE bytes. */
12907 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12908 debug_variable_location_views
12909 ? ", reset view to 0" : "");
12910 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12911 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12912 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12914 prev_addr = ent;
12915 break;
12917 case LI_adv_address:
12919 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12920 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12921 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12923 view++;
12925 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12926 dw2_asm_output_delta (2, line_label, prev_label,
12927 "from %s to %s", prev_label, line_label);
12929 prev_addr = ent;
12930 break;
12933 case LI_set_line:
12934 if (ent->val == current_line)
12936 /* We still need to start a new row, so output a copy insn. */
12937 dw2_asm_output_data (1, DW_LNS_copy,
12938 "copy line %u", current_line);
12940 else
12942 int line_offset = ent->val - current_line;
12943 int line_delta = line_offset - DWARF_LINE_BASE;
12945 current_line = ent->val;
12946 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12948 /* This can handle deltas from -10 to 234, using the current
12949 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12950 This takes 1 byte. */
12951 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12952 "line %u", current_line);
12954 else
12956 /* This can handle any delta. This takes at least 4 bytes,
12957 depending on the value being encoded. */
12958 dw2_asm_output_data (1, DW_LNS_advance_line,
12959 "advance to line %u", current_line);
12960 dw2_asm_output_data_sleb128 (line_offset, NULL);
12961 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12964 break;
12966 case LI_set_file:
12967 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12968 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12969 break;
12971 case LI_set_column:
12972 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12973 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12974 break;
12976 case LI_negate_stmt:
12977 current_is_stmt = !current_is_stmt;
12978 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12979 "is_stmt %d", current_is_stmt);
12980 break;
12982 case LI_set_prologue_end:
12983 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12984 "set prologue end");
12985 break;
12987 case LI_set_epilogue_begin:
12988 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12989 "set epilogue begin");
12990 break;
12992 case LI_set_discriminator:
12993 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12994 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12995 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12996 dw2_asm_output_data_uleb128 (ent->val, NULL);
12997 break;
13001 /* Emit debug info for the address of the end of the table. */
13002 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
13003 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
13004 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13005 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13007 dw2_asm_output_data (1, 0, "end sequence");
13008 dw2_asm_output_data_uleb128 (1, NULL);
13009 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13012 static unsigned int output_line_info_generation;
13014 /* Output the source line number correspondence information. This
13015 information goes into the .debug_line section. */
13017 static void
13018 output_line_info (bool prologue_only)
13020 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13021 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13022 bool saw_one = false;
13023 int opc;
13025 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13026 output_line_info_generation);
13027 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13028 output_line_info_generation);
13029 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13030 output_line_info_generation);
13031 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13032 output_line_info_generation++);
13034 if (!XCOFF_DEBUGGING_INFO)
13036 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13037 dw2_asm_output_data (4, 0xffffffff,
13038 "Initial length escape value indicating 64-bit DWARF extension");
13039 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13040 "Length of Source Line Info");
13043 ASM_OUTPUT_LABEL (asm_out_file, l1);
13045 output_dwarf_version ();
13046 if (dwarf_version >= 5)
13048 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13049 dw2_asm_output_data (1, 0, "Segment Size");
13051 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13052 ASM_OUTPUT_LABEL (asm_out_file, p1);
13054 /* Define the architecture-dependent minimum instruction length (in bytes).
13055 In this implementation of DWARF, this field is used for information
13056 purposes only. Since GCC generates assembly language, we have no
13057 a priori knowledge of how many instruction bytes are generated for each
13058 source line, and therefore can use only the DW_LNE_set_address and
13059 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13060 this as '1', which is "correct enough" for all architectures,
13061 and don't let the target override. */
13062 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13064 if (dwarf_version >= 4)
13065 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13066 "Maximum Operations Per Instruction");
13067 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13068 "Default is_stmt_start flag");
13069 dw2_asm_output_data (1, DWARF_LINE_BASE,
13070 "Line Base Value (Special Opcodes)");
13071 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13072 "Line Range Value (Special Opcodes)");
13073 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13074 "Special Opcode Base");
13076 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13078 int n_op_args;
13079 switch (opc)
13081 case DW_LNS_advance_pc:
13082 case DW_LNS_advance_line:
13083 case DW_LNS_set_file:
13084 case DW_LNS_set_column:
13085 case DW_LNS_fixed_advance_pc:
13086 case DW_LNS_set_isa:
13087 n_op_args = 1;
13088 break;
13089 default:
13090 n_op_args = 0;
13091 break;
13094 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13095 opc, n_op_args);
13098 /* Write out the information about the files we use. */
13099 output_file_names ();
13100 ASM_OUTPUT_LABEL (asm_out_file, p2);
13101 if (prologue_only)
13103 /* Output the marker for the end of the line number info. */
13104 ASM_OUTPUT_LABEL (asm_out_file, l2);
13105 return;
13108 if (separate_line_info)
13110 dw_line_info_table *table;
13111 size_t i;
13113 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13114 if (table->in_use)
13116 output_one_line_info_table (table);
13117 saw_one = true;
13120 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13122 output_one_line_info_table (cold_text_section_line_info);
13123 saw_one = true;
13126 /* ??? Some Darwin linkers crash on a .debug_line section with no
13127 sequences. Further, merely a DW_LNE_end_sequence entry is not
13128 sufficient -- the address column must also be initialized.
13129 Make sure to output at least one set_address/end_sequence pair,
13130 choosing .text since that section is always present. */
13131 if (text_section_line_info->in_use || !saw_one)
13132 output_one_line_info_table (text_section_line_info);
13134 /* Output the marker for the end of the line number info. */
13135 ASM_OUTPUT_LABEL (asm_out_file, l2);
13138 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13140 static inline bool
13141 need_endianity_attribute_p (bool reverse)
13143 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13146 /* Given a pointer to a tree node for some base type, return a pointer to
13147 a DIE that describes the given type. REVERSE is true if the type is
13148 to be interpreted in the reverse storage order wrt the target order.
13150 This routine must only be called for GCC type nodes that correspond to
13151 Dwarf base (fundamental) types. */
13153 static dw_die_ref
13154 base_type_die (tree type, bool reverse)
13156 dw_die_ref base_type_result;
13157 enum dwarf_type encoding;
13158 bool fpt_used = false;
13159 struct fixed_point_type_info fpt_info;
13160 tree type_bias = NULL_TREE;
13162 /* If this is a subtype that should not be emitted as a subrange type,
13163 use the base type. See subrange_type_for_debug_p. */
13164 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13165 type = TREE_TYPE (type);
13167 switch (TREE_CODE (type))
13169 case INTEGER_TYPE:
13170 if ((dwarf_version >= 4 || !dwarf_strict)
13171 && TYPE_NAME (type)
13172 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13173 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13174 && DECL_NAME (TYPE_NAME (type)))
13176 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13177 if (strcmp (name, "char16_t") == 0
13178 || strcmp (name, "char32_t") == 0)
13180 encoding = DW_ATE_UTF;
13181 break;
13184 if ((dwarf_version >= 3 || !dwarf_strict)
13185 && lang_hooks.types.get_fixed_point_type_info)
13187 memset (&fpt_info, 0, sizeof (fpt_info));
13188 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13190 fpt_used = true;
13191 encoding = ((TYPE_UNSIGNED (type))
13192 ? DW_ATE_unsigned_fixed
13193 : DW_ATE_signed_fixed);
13194 break;
13197 if (TYPE_STRING_FLAG (type))
13199 if (TYPE_UNSIGNED (type))
13200 encoding = DW_ATE_unsigned_char;
13201 else
13202 encoding = DW_ATE_signed_char;
13204 else if (TYPE_UNSIGNED (type))
13205 encoding = DW_ATE_unsigned;
13206 else
13207 encoding = DW_ATE_signed;
13209 if (!dwarf_strict
13210 && lang_hooks.types.get_type_bias)
13211 type_bias = lang_hooks.types.get_type_bias (type);
13212 break;
13214 case REAL_TYPE:
13215 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13217 if (dwarf_version >= 3 || !dwarf_strict)
13218 encoding = DW_ATE_decimal_float;
13219 else
13220 encoding = DW_ATE_lo_user;
13222 else
13223 encoding = DW_ATE_float;
13224 break;
13226 case FIXED_POINT_TYPE:
13227 if (!(dwarf_version >= 3 || !dwarf_strict))
13228 encoding = DW_ATE_lo_user;
13229 else if (TYPE_UNSIGNED (type))
13230 encoding = DW_ATE_unsigned_fixed;
13231 else
13232 encoding = DW_ATE_signed_fixed;
13233 break;
13235 /* Dwarf2 doesn't know anything about complex ints, so use
13236 a user defined type for it. */
13237 case COMPLEX_TYPE:
13238 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
13239 encoding = DW_ATE_complex_float;
13240 else
13241 encoding = DW_ATE_lo_user;
13242 break;
13244 case BOOLEAN_TYPE:
13245 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13246 encoding = DW_ATE_boolean;
13247 break;
13249 default:
13250 /* No other TREE_CODEs are Dwarf fundamental types. */
13251 gcc_unreachable ();
13254 base_type_result = new_die_raw (DW_TAG_base_type);
13256 add_AT_unsigned (base_type_result, DW_AT_byte_size,
13257 int_size_in_bytes (type));
13258 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13260 if (need_endianity_attribute_p (reverse))
13261 add_AT_unsigned (base_type_result, DW_AT_endianity,
13262 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13264 add_alignment_attribute (base_type_result, type);
13266 if (fpt_used)
13268 switch (fpt_info.scale_factor_kind)
13270 case fixed_point_scale_factor_binary:
13271 add_AT_int (base_type_result, DW_AT_binary_scale,
13272 fpt_info.scale_factor.binary);
13273 break;
13275 case fixed_point_scale_factor_decimal:
13276 add_AT_int (base_type_result, DW_AT_decimal_scale,
13277 fpt_info.scale_factor.decimal);
13278 break;
13280 case fixed_point_scale_factor_arbitrary:
13281 /* Arbitrary scale factors cannot be described in standard DWARF. */
13282 if (!dwarf_strict)
13284 /* Describe the scale factor as a rational constant. */
13285 const dw_die_ref scale_factor
13286 = new_die (DW_TAG_constant, comp_unit_die (), type);
13288 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13289 fpt_info.scale_factor.arbitrary.numerator,
13290 dw_scalar_form_constant, NULL);
13291 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13292 fpt_info.scale_factor.arbitrary.denominator,
13293 dw_scalar_form_constant, NULL);
13295 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13297 break;
13299 default:
13300 gcc_unreachable ();
13304 if (type_bias)
13305 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13306 dw_scalar_form_constant
13307 | dw_scalar_form_exprloc
13308 | dw_scalar_form_reference,
13309 NULL);
13311 return base_type_result;
13314 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13315 named 'auto' in its type: return true for it, false otherwise. */
13317 static inline bool
13318 is_cxx_auto (tree type)
13320 if (is_cxx ())
13322 tree name = TYPE_IDENTIFIER (type);
13323 if (name == get_identifier ("auto")
13324 || name == get_identifier ("decltype(auto)"))
13325 return true;
13327 return false;
13330 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
13331 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13333 static inline int
13334 is_base_type (tree type)
13336 switch (TREE_CODE (type))
13338 case INTEGER_TYPE:
13339 case REAL_TYPE:
13340 case FIXED_POINT_TYPE:
13341 case COMPLEX_TYPE:
13342 case BOOLEAN_TYPE:
13343 return 1;
13345 case VOID_TYPE:
13346 case OPAQUE_TYPE:
13347 case ARRAY_TYPE:
13348 case RECORD_TYPE:
13349 case UNION_TYPE:
13350 case QUAL_UNION_TYPE:
13351 case ENUMERAL_TYPE:
13352 case FUNCTION_TYPE:
13353 case METHOD_TYPE:
13354 case POINTER_TYPE:
13355 case REFERENCE_TYPE:
13356 case NULLPTR_TYPE:
13357 case OFFSET_TYPE:
13358 case LANG_TYPE:
13359 case VECTOR_TYPE:
13360 return 0;
13362 default:
13363 if (is_cxx_auto (type))
13364 return 0;
13365 gcc_unreachable ();
13368 return 0;
13371 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13372 node, return the size in bits for the type if it is a constant, or else
13373 return the alignment for the type if the type's size is not constant, or
13374 else return BITS_PER_WORD if the type actually turns out to be an
13375 ERROR_MARK node. */
13377 static inline unsigned HOST_WIDE_INT
13378 simple_type_size_in_bits (const_tree type)
13380 if (TREE_CODE (type) == ERROR_MARK)
13381 return BITS_PER_WORD;
13382 else if (TYPE_SIZE (type) == NULL_TREE)
13383 return 0;
13384 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13385 return tree_to_uhwi (TYPE_SIZE (type));
13386 else
13387 return TYPE_ALIGN (type);
13390 /* Similarly, but return an offset_int instead of UHWI. */
13392 static inline offset_int
13393 offset_int_type_size_in_bits (const_tree type)
13395 if (TREE_CODE (type) == ERROR_MARK)
13396 return BITS_PER_WORD;
13397 else if (TYPE_SIZE (type) == NULL_TREE)
13398 return 0;
13399 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13400 return wi::to_offset (TYPE_SIZE (type));
13401 else
13402 return TYPE_ALIGN (type);
13405 /* Given a pointer to a tree node for a subrange type, return a pointer
13406 to a DIE that describes the given type. */
13408 static dw_die_ref
13409 subrange_type_die (tree type, tree low, tree high, tree bias,
13410 dw_die_ref context_die)
13412 dw_die_ref subrange_die;
13413 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13415 if (context_die == NULL)
13416 context_die = comp_unit_die ();
13418 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13420 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13422 /* The size of the subrange type and its base type do not match,
13423 so we need to generate a size attribute for the subrange type. */
13424 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13427 add_alignment_attribute (subrange_die, type);
13429 if (low)
13430 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13431 if (high)
13432 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13433 if (bias && !dwarf_strict)
13434 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13435 dw_scalar_form_constant
13436 | dw_scalar_form_exprloc
13437 | dw_scalar_form_reference,
13438 NULL);
13440 return subrange_die;
13443 /* Returns the (const and/or volatile) cv_qualifiers associated with
13444 the decl node. This will normally be augmented with the
13445 cv_qualifiers of the underlying type in add_type_attribute. */
13447 static int
13448 decl_quals (const_tree decl)
13450 return ((TREE_READONLY (decl)
13451 /* The C++ front-end correctly marks reference-typed
13452 variables as readonly, but from a language (and debug
13453 info) standpoint they are not const-qualified. */
13454 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13455 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13456 | (TREE_THIS_VOLATILE (decl)
13457 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13460 /* Determine the TYPE whose qualifiers match the largest strict subset
13461 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13462 qualifiers outside QUAL_MASK. */
13464 static int
13465 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13467 tree t;
13468 int best_rank = 0, best_qual = 0, max_rank;
13470 type_quals &= qual_mask;
13471 max_rank = popcount_hwi (type_quals) - 1;
13473 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13474 t = TYPE_NEXT_VARIANT (t))
13476 int q = TYPE_QUALS (t) & qual_mask;
13478 if ((q & type_quals) == q && q != type_quals
13479 && check_base_type (t, type))
13481 int rank = popcount_hwi (q);
13483 if (rank > best_rank)
13485 best_rank = rank;
13486 best_qual = q;
13491 return best_qual;
13494 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13495 static const dwarf_qual_info_t dwarf_qual_info[] =
13497 { TYPE_QUAL_CONST, DW_TAG_const_type },
13498 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13499 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13500 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13502 static const unsigned int dwarf_qual_info_size
13503 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13505 /* If DIE is a qualified DIE of some base DIE with the same parent,
13506 return the base DIE, otherwise return NULL. Set MASK to the
13507 qualifiers added compared to the returned DIE. */
13509 static dw_die_ref
13510 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13512 unsigned int i;
13513 for (i = 0; i < dwarf_qual_info_size; i++)
13514 if (die->die_tag == dwarf_qual_info[i].t)
13515 break;
13516 if (i == dwarf_qual_info_size)
13517 return NULL;
13518 if (vec_safe_length (die->die_attr) != 1)
13519 return NULL;
13520 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13521 if (type == NULL || type->die_parent != die->die_parent)
13522 return NULL;
13523 *mask |= dwarf_qual_info[i].q;
13524 if (depth)
13526 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13527 if (ret)
13528 return ret;
13530 return type;
13533 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13534 entry that chains the modifiers specified by CV_QUALS in front of the
13535 given type. REVERSE is true if the type is to be interpreted in the
13536 reverse storage order wrt the target order. */
13538 static dw_die_ref
13539 modified_type_die (tree type, int cv_quals, bool reverse,
13540 dw_die_ref context_die)
13542 enum tree_code code = TREE_CODE (type);
13543 dw_die_ref mod_type_die;
13544 dw_die_ref sub_die = NULL;
13545 tree item_type = NULL;
13546 tree qualified_type;
13547 tree name, low, high;
13548 dw_die_ref mod_scope;
13549 /* Only these cv-qualifiers are currently handled. */
13550 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13551 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13552 ENCODE_QUAL_ADDR_SPACE(~0U));
13553 const bool reverse_base_type
13554 = need_endianity_attribute_p (reverse) && is_base_type (type);
13556 if (code == ERROR_MARK)
13557 return NULL;
13559 if (lang_hooks.types.get_debug_type)
13561 tree debug_type = lang_hooks.types.get_debug_type (type);
13563 if (debug_type != NULL_TREE && debug_type != type)
13564 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13567 cv_quals &= cv_qual_mask;
13569 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13570 tag modifier (and not an attribute) old consumers won't be able
13571 to handle it. */
13572 if (dwarf_version < 3)
13573 cv_quals &= ~TYPE_QUAL_RESTRICT;
13575 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13576 if (dwarf_version < 5)
13577 cv_quals &= ~TYPE_QUAL_ATOMIC;
13579 /* See if we already have the appropriately qualified variant of
13580 this type. */
13581 qualified_type = get_qualified_type (type, cv_quals);
13583 if (qualified_type == sizetype)
13585 /* Try not to expose the internal sizetype type's name. */
13586 if (TYPE_NAME (qualified_type)
13587 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13589 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13591 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13592 && (TYPE_PRECISION (t)
13593 == TYPE_PRECISION (qualified_type))
13594 && (TYPE_UNSIGNED (t)
13595 == TYPE_UNSIGNED (qualified_type)));
13596 qualified_type = t;
13598 else if (qualified_type == sizetype
13599 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13600 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13601 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13602 qualified_type = size_type_node;
13603 if (type == sizetype)
13604 type = qualified_type;
13607 /* If we do, then we can just use its DIE, if it exists. */
13608 if (qualified_type)
13610 mod_type_die = lookup_type_die (qualified_type);
13612 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13613 dealt with specially: the DIE with the attribute, if it exists, is
13614 placed immediately after the regular DIE for the same base type. */
13615 if (mod_type_die
13616 && (!reverse_base_type
13617 || ((mod_type_die = mod_type_die->die_sib) != NULL
13618 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13619 return mod_type_die;
13622 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13624 /* Handle C typedef types. */
13625 if (name
13626 && TREE_CODE (name) == TYPE_DECL
13627 && DECL_ORIGINAL_TYPE (name)
13628 && !DECL_ARTIFICIAL (name))
13630 tree dtype = TREE_TYPE (name);
13632 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13633 if (qualified_type == dtype && !reverse_base_type)
13635 tree origin = decl_ultimate_origin (name);
13637 /* Typedef variants that have an abstract origin don't get their own
13638 type DIE (see gen_typedef_die), so fall back on the ultimate
13639 abstract origin instead. */
13640 if (origin != NULL && origin != name)
13641 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13642 context_die);
13644 /* For a named type, use the typedef. */
13645 gen_type_die (qualified_type, context_die);
13646 return lookup_type_die (qualified_type);
13648 else
13650 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13651 dquals &= cv_qual_mask;
13652 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13653 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13654 /* cv-unqualified version of named type. Just use
13655 the unnamed type to which it refers. */
13656 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13657 reverse, context_die);
13658 /* Else cv-qualified version of named type; fall through. */
13662 mod_scope = scope_die_for (type, context_die);
13664 if (cv_quals)
13666 int sub_quals = 0, first_quals = 0;
13667 unsigned i;
13668 dw_die_ref first = NULL, last = NULL;
13670 /* Determine a lesser qualified type that most closely matches
13671 this one. Then generate DW_TAG_* entries for the remaining
13672 qualifiers. */
13673 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13674 cv_qual_mask);
13675 if (sub_quals && use_debug_types)
13677 bool needed = false;
13678 /* If emitting type units, make sure the order of qualifiers
13679 is canonical. Thus, start from unqualified type if
13680 an earlier qualifier is missing in sub_quals, but some later
13681 one is present there. */
13682 for (i = 0; i < dwarf_qual_info_size; i++)
13683 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13684 needed = true;
13685 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13687 sub_quals = 0;
13688 break;
13691 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13692 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13694 /* As not all intermediate qualified DIEs have corresponding
13695 tree types, ensure that qualified DIEs in the same scope
13696 as their DW_AT_type are emitted after their DW_AT_type,
13697 only with other qualified DIEs for the same type possibly
13698 in between them. Determine the range of such qualified
13699 DIEs now (first being the base type, last being corresponding
13700 last qualified DIE for it). */
13701 unsigned int count = 0;
13702 first = qualified_die_p (mod_type_die, &first_quals,
13703 dwarf_qual_info_size);
13704 if (first == NULL)
13705 first = mod_type_die;
13706 gcc_assert ((first_quals & ~sub_quals) == 0);
13707 for (count = 0, last = first;
13708 count < (1U << dwarf_qual_info_size);
13709 count++, last = last->die_sib)
13711 int quals = 0;
13712 if (last == mod_scope->die_child)
13713 break;
13714 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13715 != first)
13716 break;
13720 for (i = 0; i < dwarf_qual_info_size; i++)
13721 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13723 dw_die_ref d;
13724 if (first && first != last)
13726 for (d = first->die_sib; ; d = d->die_sib)
13728 int quals = 0;
13729 qualified_die_p (d, &quals, dwarf_qual_info_size);
13730 if (quals == (first_quals | dwarf_qual_info[i].q))
13731 break;
13732 if (d == last)
13734 d = NULL;
13735 break;
13738 if (d)
13740 mod_type_die = d;
13741 continue;
13744 if (first)
13746 d = new_die_raw (dwarf_qual_info[i].t);
13747 add_child_die_after (mod_scope, d, last);
13748 last = d;
13750 else
13751 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13752 if (mod_type_die)
13753 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13754 mod_type_die = d;
13755 first_quals |= dwarf_qual_info[i].q;
13758 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13760 dwarf_tag tag = DW_TAG_pointer_type;
13761 if (code == REFERENCE_TYPE)
13763 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13764 tag = DW_TAG_rvalue_reference_type;
13765 else
13766 tag = DW_TAG_reference_type;
13768 mod_type_die = new_die (tag, mod_scope, type);
13770 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13771 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13772 add_alignment_attribute (mod_type_die, type);
13773 item_type = TREE_TYPE (type);
13775 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13776 if (!ADDR_SPACE_GENERIC_P (as))
13778 int action = targetm.addr_space.debug (as);
13779 if (action >= 0)
13781 /* Positive values indicate an address_class. */
13782 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13784 else
13786 /* Negative values indicate an (inverted) segment base reg. */
13787 dw_loc_descr_ref d
13788 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13789 add_AT_loc (mod_type_die, DW_AT_segment, d);
13793 else if (code == INTEGER_TYPE
13794 && TREE_TYPE (type) != NULL_TREE
13795 && subrange_type_for_debug_p (type, &low, &high))
13797 tree bias = NULL_TREE;
13798 if (lang_hooks.types.get_type_bias)
13799 bias = lang_hooks.types.get_type_bias (type);
13800 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13801 item_type = TREE_TYPE (type);
13803 else if (is_base_type (type))
13805 mod_type_die = base_type_die (type, reverse);
13807 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13808 if (reverse_base_type)
13810 dw_die_ref after_die
13811 = modified_type_die (type, cv_quals, false, context_die);
13812 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13814 else
13815 add_child_die (comp_unit_die (), mod_type_die);
13817 add_pubtype (type, mod_type_die);
13819 else
13821 gen_type_die (type, context_die);
13823 /* We have to get the type_main_variant here (and pass that to the
13824 `lookup_type_die' routine) because the ..._TYPE node we have
13825 might simply be a *copy* of some original type node (where the
13826 copy was created to help us keep track of typedef names) and
13827 that copy might have a different TYPE_UID from the original
13828 ..._TYPE node. */
13829 if (TREE_CODE (type) == FUNCTION_TYPE
13830 || TREE_CODE (type) == METHOD_TYPE)
13832 /* For function/method types, can't just use type_main_variant here,
13833 because that can have different ref-qualifiers for C++,
13834 but try to canonicalize. */
13835 tree main = TYPE_MAIN_VARIANT (type);
13836 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13837 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13838 && check_base_type (t, main)
13839 && check_lang_type (t, type))
13840 return lookup_type_die (t);
13841 return lookup_type_die (type);
13843 else if (TREE_CODE (type) != VECTOR_TYPE
13844 && TREE_CODE (type) != ARRAY_TYPE)
13845 return lookup_type_die (type_main_variant (type));
13846 else
13847 /* Vectors have the debugging information in the type,
13848 not the main variant. */
13849 return lookup_type_die (type);
13852 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13853 don't output a DW_TAG_typedef, since there isn't one in the
13854 user's program; just attach a DW_AT_name to the type.
13855 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13856 if the base type already has the same name. */
13857 if (name
13858 && ((TREE_CODE (name) != TYPE_DECL
13859 && (qualified_type == TYPE_MAIN_VARIANT (type)
13860 || (cv_quals == TYPE_UNQUALIFIED)))
13861 || (TREE_CODE (name) == TYPE_DECL
13862 && TREE_TYPE (name) == qualified_type
13863 && DECL_NAME (name))))
13865 if (TREE_CODE (name) == TYPE_DECL)
13866 /* Could just call add_name_and_src_coords_attributes here,
13867 but since this is a builtin type it doesn't have any
13868 useful source coordinates anyway. */
13869 name = DECL_NAME (name);
13870 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13872 /* This probably indicates a bug. */
13873 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13875 name = TYPE_IDENTIFIER (type);
13876 add_name_attribute (mod_type_die,
13877 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13880 if (qualified_type && !reverse_base_type)
13881 equate_type_number_to_die (qualified_type, mod_type_die);
13883 if (item_type)
13884 /* We must do this after the equate_type_number_to_die call, in case
13885 this is a recursive type. This ensures that the modified_type_die
13886 recursion will terminate even if the type is recursive. Recursive
13887 types are possible in Ada. */
13888 sub_die = modified_type_die (item_type,
13889 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13890 reverse,
13891 context_die);
13893 if (sub_die != NULL)
13894 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13896 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13897 if (TYPE_ARTIFICIAL (type))
13898 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13900 return mod_type_die;
13903 /* Generate DIEs for the generic parameters of T.
13904 T must be either a generic type or a generic function.
13905 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13907 static void
13908 gen_generic_params_dies (tree t)
13910 tree parms, args;
13911 int parms_num, i;
13912 dw_die_ref die = NULL;
13913 int non_default;
13915 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13916 return;
13918 if (TYPE_P (t))
13919 die = lookup_type_die (t);
13920 else if (DECL_P (t))
13921 die = lookup_decl_die (t);
13923 gcc_assert (die);
13925 parms = lang_hooks.get_innermost_generic_parms (t);
13926 if (!parms)
13927 /* T has no generic parameter. It means T is neither a generic type
13928 or function. End of story. */
13929 return;
13931 parms_num = TREE_VEC_LENGTH (parms);
13932 args = lang_hooks.get_innermost_generic_args (t);
13933 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13934 non_default = int_cst_value (TREE_CHAIN (args));
13935 else
13936 non_default = TREE_VEC_LENGTH (args);
13937 for (i = 0; i < parms_num; i++)
13939 tree parm, arg, arg_pack_elems;
13940 dw_die_ref parm_die;
13942 parm = TREE_VEC_ELT (parms, i);
13943 arg = TREE_VEC_ELT (args, i);
13944 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13945 gcc_assert (parm && TREE_VALUE (parm) && arg);
13947 if (parm && TREE_VALUE (parm) && arg)
13949 /* If PARM represents a template parameter pack,
13950 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13951 by DW_TAG_template_*_parameter DIEs for the argument
13952 pack elements of ARG. Note that ARG would then be
13953 an argument pack. */
13954 if (arg_pack_elems)
13955 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13956 arg_pack_elems,
13957 die);
13958 else
13959 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13960 true /* emit name */, die);
13961 if (i >= non_default)
13962 add_AT_flag (parm_die, DW_AT_default_value, 1);
13967 /* Create and return a DIE for PARM which should be
13968 the representation of a generic type parameter.
13969 For instance, in the C++ front end, PARM would be a template parameter.
13970 ARG is the argument to PARM.
13971 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13972 name of the PARM.
13973 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13974 as a child node. */
13976 static dw_die_ref
13977 generic_parameter_die (tree parm, tree arg,
13978 bool emit_name_p,
13979 dw_die_ref parent_die)
13981 dw_die_ref tmpl_die = NULL;
13982 const char *name = NULL;
13984 /* C++20 accepts class literals as template parameters, and var
13985 decls with initializers represent them. The VAR_DECLs would be
13986 rejected, but we can take the DECL_INITIAL constructor and
13987 attempt to expand it. */
13988 if (arg && VAR_P (arg))
13989 arg = DECL_INITIAL (arg);
13991 if (!parm || !DECL_NAME (parm) || !arg)
13992 return NULL;
13994 /* We support non-type generic parameters and arguments,
13995 type generic parameters and arguments, as well as
13996 generic generic parameters (a.k.a. template template parameters in C++)
13997 and arguments. */
13998 if (TREE_CODE (parm) == PARM_DECL)
13999 /* PARM is a nontype generic parameter */
14000 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
14001 else if (TREE_CODE (parm) == TYPE_DECL)
14002 /* PARM is a type generic parameter. */
14003 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14004 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14005 /* PARM is a generic generic parameter.
14006 Its DIE is a GNU extension. It shall have a
14007 DW_AT_name attribute to represent the name of the template template
14008 parameter, and a DW_AT_GNU_template_name attribute to represent the
14009 name of the template template argument. */
14010 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14011 parent_die, parm);
14012 else
14013 gcc_unreachable ();
14015 if (tmpl_die)
14017 tree tmpl_type;
14019 /* If PARM is a generic parameter pack, it means we are
14020 emitting debug info for a template argument pack element.
14021 In other terms, ARG is a template argument pack element.
14022 In that case, we don't emit any DW_AT_name attribute for
14023 the die. */
14024 if (emit_name_p)
14026 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14027 gcc_assert (name);
14028 add_AT_string (tmpl_die, DW_AT_name, name);
14031 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14033 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14034 TMPL_DIE should have a child DW_AT_type attribute that is set
14035 to the type of the argument to PARM, which is ARG.
14036 If PARM is a type generic parameter, TMPL_DIE should have a
14037 child DW_AT_type that is set to ARG. */
14038 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14039 add_type_attribute (tmpl_die, tmpl_type,
14040 (TREE_THIS_VOLATILE (tmpl_type)
14041 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14042 false, parent_die);
14044 else
14046 /* So TMPL_DIE is a DIE representing a
14047 a generic generic template parameter, a.k.a template template
14048 parameter in C++ and arg is a template. */
14050 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14051 to the name of the argument. */
14052 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14053 if (name)
14054 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14057 if (TREE_CODE (parm) == PARM_DECL)
14058 /* So PARM is a non-type generic parameter.
14059 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14060 attribute of TMPL_DIE which value represents the value
14061 of ARG.
14062 We must be careful here:
14063 The value of ARG might reference some function decls.
14064 We might currently be emitting debug info for a generic
14065 type and types are emitted before function decls, we don't
14066 know if the function decls referenced by ARG will actually be
14067 emitted after cgraph computations.
14068 So must defer the generation of the DW_AT_const_value to
14069 after cgraph is ready. */
14070 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14073 return tmpl_die;
14076 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14077 PARM_PACK must be a template parameter pack. The returned DIE
14078 will be child DIE of PARENT_DIE. */
14080 static dw_die_ref
14081 template_parameter_pack_die (tree parm_pack,
14082 tree parm_pack_args,
14083 dw_die_ref parent_die)
14085 dw_die_ref die;
14086 int j;
14088 gcc_assert (parent_die && parm_pack);
14090 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14091 add_name_and_src_coords_attributes (die, parm_pack);
14092 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14093 generic_parameter_die (parm_pack,
14094 TREE_VEC_ELT (parm_pack_args, j),
14095 false /* Don't emit DW_AT_name */,
14096 die);
14097 return die;
14100 /* Return the DBX register number described by a given RTL node. */
14102 static unsigned int
14103 dbx_reg_number (const_rtx rtl)
14105 unsigned regno = REGNO (rtl);
14107 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14109 #ifdef LEAF_REG_REMAP
14110 if (crtl->uses_only_leaf_regs)
14112 int leaf_reg = LEAF_REG_REMAP (regno);
14113 if (leaf_reg != -1)
14114 regno = (unsigned) leaf_reg;
14116 #endif
14118 regno = DBX_REGISTER_NUMBER (regno);
14119 gcc_assert (regno != INVALID_REGNUM);
14120 return regno;
14123 /* Optionally add a DW_OP_piece term to a location description expression.
14124 DW_OP_piece is only added if the location description expression already
14125 doesn't end with DW_OP_piece. */
14127 static void
14128 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14130 dw_loc_descr_ref loc;
14132 if (*list_head != NULL)
14134 /* Find the end of the chain. */
14135 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14138 if (loc->dw_loc_opc != DW_OP_piece)
14139 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14143 /* Return a location descriptor that designates a machine register or
14144 zero if there is none. */
14146 static dw_loc_descr_ref
14147 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14149 rtx regs;
14151 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14152 return 0;
14154 /* We only use "frame base" when we're sure we're talking about the
14155 post-prologue local stack frame. We do this by *not* running
14156 register elimination until this point, and recognizing the special
14157 argument pointer and soft frame pointer rtx's.
14158 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14159 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14160 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
14162 dw_loc_descr_ref result = NULL;
14164 if (dwarf_version >= 4 || !dwarf_strict)
14166 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14167 initialized);
14168 if (result)
14169 add_loc_descr (&result,
14170 new_loc_descr (DW_OP_stack_value, 0, 0));
14172 return result;
14175 regs = targetm.dwarf_register_span (rtl);
14177 if (REG_NREGS (rtl) > 1 || regs)
14178 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14179 else
14181 unsigned int dbx_regnum = dbx_reg_number (rtl);
14182 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14183 return 0;
14184 return one_reg_loc_descriptor (dbx_regnum, initialized);
14188 /* Return a location descriptor that designates a machine register for
14189 a given hard register number. */
14191 static dw_loc_descr_ref
14192 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14194 dw_loc_descr_ref reg_loc_descr;
14196 if (regno <= 31)
14197 reg_loc_descr
14198 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14199 else
14200 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14202 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14203 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14205 return reg_loc_descr;
14208 /* Given an RTL of a register, return a location descriptor that
14209 designates a value that spans more than one register. */
14211 static dw_loc_descr_ref
14212 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14213 enum var_init_status initialized)
14215 int size, i;
14216 dw_loc_descr_ref loc_result = NULL;
14218 /* Simple, contiguous registers. */
14219 if (regs == NULL_RTX)
14221 unsigned reg = REGNO (rtl);
14222 int nregs;
14224 #ifdef LEAF_REG_REMAP
14225 if (crtl->uses_only_leaf_regs)
14227 int leaf_reg = LEAF_REG_REMAP (reg);
14228 if (leaf_reg != -1)
14229 reg = (unsigned) leaf_reg;
14231 #endif
14233 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
14234 nregs = REG_NREGS (rtl);
14236 /* At present we only track constant-sized pieces. */
14237 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14238 return NULL;
14239 size /= nregs;
14241 loc_result = NULL;
14242 while (nregs--)
14244 dw_loc_descr_ref t;
14246 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
14247 VAR_INIT_STATUS_INITIALIZED);
14248 add_loc_descr (&loc_result, t);
14249 add_loc_descr_op_piece (&loc_result, size);
14250 ++reg;
14252 return loc_result;
14255 /* Now onto stupid register sets in non contiguous locations. */
14257 gcc_assert (GET_CODE (regs) == PARALLEL);
14259 /* At present we only track constant-sized pieces. */
14260 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14261 return NULL;
14262 loc_result = NULL;
14264 for (i = 0; i < XVECLEN (regs, 0); ++i)
14266 dw_loc_descr_ref t;
14268 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
14269 VAR_INIT_STATUS_INITIALIZED);
14270 add_loc_descr (&loc_result, t);
14271 add_loc_descr_op_piece (&loc_result, size);
14274 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14275 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14276 return loc_result;
14279 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14281 /* Return a location descriptor that designates a constant i,
14282 as a compound operation from constant (i >> shift), constant shift
14283 and DW_OP_shl. */
14285 static dw_loc_descr_ref
14286 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14288 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14289 add_loc_descr (&ret, int_loc_descriptor (shift));
14290 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14291 return ret;
14294 /* Return a location descriptor that designates constant POLY_I. */
14296 static dw_loc_descr_ref
14297 int_loc_descriptor (poly_int64 poly_i)
14299 enum dwarf_location_atom op;
14301 HOST_WIDE_INT i;
14302 if (!poly_i.is_constant (&i))
14304 /* Create location descriptions for the non-constant part and
14305 add any constant offset at the end. */
14306 dw_loc_descr_ref ret = NULL;
14307 HOST_WIDE_INT constant = poly_i.coeffs[0];
14308 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14310 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14311 if (coeff != 0)
14313 dw_loc_descr_ref start = ret;
14314 unsigned int factor;
14315 int bias;
14316 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14317 (j, &factor, &bias);
14319 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14320 add COEFF * (REGNO / FACTOR) now and subtract
14321 COEFF * BIAS from the final constant part. */
14322 constant -= coeff * bias;
14323 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14324 if (coeff % factor == 0)
14325 coeff /= factor;
14326 else
14328 int amount = exact_log2 (factor);
14329 gcc_assert (amount >= 0);
14330 add_loc_descr (&ret, int_loc_descriptor (amount));
14331 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14333 if (coeff != 1)
14335 add_loc_descr (&ret, int_loc_descriptor (coeff));
14336 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14338 if (start)
14339 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14342 loc_descr_plus_const (&ret, constant);
14343 return ret;
14346 /* Pick the smallest representation of a constant, rather than just
14347 defaulting to the LEB encoding. */
14348 if (i >= 0)
14350 int clz = clz_hwi (i);
14351 int ctz = ctz_hwi (i);
14352 if (i <= 31)
14353 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14354 else if (i <= 0xff)
14355 op = DW_OP_const1u;
14356 else if (i <= 0xffff)
14357 op = DW_OP_const2u;
14358 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14359 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14360 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14361 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14362 while DW_OP_const4u is 5 bytes. */
14363 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14364 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14365 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14366 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14367 while DW_OP_const4u is 5 bytes. */
14368 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14370 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14371 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14372 <= 4)
14374 /* As i >= 2**31, the double cast above will yield a negative number.
14375 Since wrapping is defined in DWARF expressions we can output big
14376 positive integers as small negative ones, regardless of the size
14377 of host wide ints.
14379 Here, since the evaluator will handle 32-bit values and since i >=
14380 2**31, we know it's going to be interpreted as a negative literal:
14381 store it this way if we can do better than 5 bytes this way. */
14382 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14384 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14385 op = DW_OP_const4u;
14387 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14388 least 6 bytes: see if we can do better before falling back to it. */
14389 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14390 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14391 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14392 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14393 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14394 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14395 >= HOST_BITS_PER_WIDE_INT)
14396 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14397 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14398 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14399 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14400 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14401 && size_of_uleb128 (i) > 6)
14402 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14403 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14404 else
14405 op = DW_OP_constu;
14407 else
14409 if (i >= -0x80)
14410 op = DW_OP_const1s;
14411 else if (i >= -0x8000)
14412 op = DW_OP_const2s;
14413 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14415 if (size_of_int_loc_descriptor (i) < 5)
14417 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14418 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14419 return ret;
14421 op = DW_OP_const4s;
14423 else
14425 if (size_of_int_loc_descriptor (i)
14426 < (unsigned long) 1 + size_of_sleb128 (i))
14428 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14429 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14430 return ret;
14432 op = DW_OP_consts;
14436 return new_loc_descr (op, i, 0);
14439 /* Likewise, for unsigned constants. */
14441 static dw_loc_descr_ref
14442 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14444 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14445 const unsigned HOST_WIDE_INT max_uint
14446 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14448 /* If possible, use the clever signed constants handling. */
14449 if (i <= max_int)
14450 return int_loc_descriptor ((HOST_WIDE_INT) i);
14452 /* Here, we are left with positive numbers that cannot be represented as
14453 HOST_WIDE_INT, i.e.:
14454 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14456 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14457 whereas may be better to output a negative integer: thanks to integer
14458 wrapping, we know that:
14459 x = x - 2 ** DWARF2_ADDR_SIZE
14460 = x - 2 * (max (HOST_WIDE_INT) + 1)
14461 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14462 small negative integers. Let's try that in cases it will clearly improve
14463 the encoding: there is no gain turning DW_OP_const4u into
14464 DW_OP_const4s. */
14465 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14466 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14467 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14469 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14471 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14472 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14473 const HOST_WIDE_INT second_shift
14474 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14476 /* So we finally have:
14477 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14478 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14479 return int_loc_descriptor (second_shift);
14482 /* Last chance: fallback to a simple constant operation. */
14483 return new_loc_descr
14484 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14485 ? DW_OP_const4u
14486 : DW_OP_const8u,
14487 i, 0);
14490 /* Generate and return a location description that computes the unsigned
14491 comparison of the two stack top entries (a OP b where b is the top-most
14492 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14493 LE_EXPR, GT_EXPR or GE_EXPR. */
14495 static dw_loc_descr_ref
14496 uint_comparison_loc_list (enum tree_code kind)
14498 enum dwarf_location_atom op, flip_op;
14499 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14501 switch (kind)
14503 case LT_EXPR:
14504 op = DW_OP_lt;
14505 break;
14506 case LE_EXPR:
14507 op = DW_OP_le;
14508 break;
14509 case GT_EXPR:
14510 op = DW_OP_gt;
14511 break;
14512 case GE_EXPR:
14513 op = DW_OP_ge;
14514 break;
14515 default:
14516 gcc_unreachable ();
14519 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14520 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14522 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14523 possible to perform unsigned comparisons: we just have to distinguish
14524 three cases:
14526 1. when a and b have the same sign (as signed integers); then we should
14527 return: a OP(signed) b;
14529 2. when a is a negative signed integer while b is a positive one, then a
14530 is a greater unsigned integer than b; likewise when a and b's roles
14531 are flipped.
14533 So first, compare the sign of the two operands. */
14534 ret = new_loc_descr (DW_OP_over, 0, 0);
14535 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14536 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14537 /* If they have different signs (i.e. they have different sign bits), then
14538 the stack top value has now the sign bit set and thus it's smaller than
14539 zero. */
14540 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14541 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14542 add_loc_descr (&ret, bra_node);
14544 /* We are in case 1. At this point, we know both operands have the same
14545 sign, to it's safe to use the built-in signed comparison. */
14546 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14547 add_loc_descr (&ret, jmp_node);
14549 /* We are in case 2. Here, we know both operands do not have the same sign,
14550 so we have to flip the signed comparison. */
14551 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14552 tmp = new_loc_descr (flip_op, 0, 0);
14553 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14554 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14555 add_loc_descr (&ret, tmp);
14557 /* This dummy operation is necessary to make the two branches join. */
14558 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14559 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14560 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14561 add_loc_descr (&ret, tmp);
14563 return ret;
14566 /* Likewise, but takes the location description lists (might be destructive on
14567 them). Return NULL if either is NULL or if concatenation fails. */
14569 static dw_loc_list_ref
14570 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14571 enum tree_code kind)
14573 if (left == NULL || right == NULL)
14574 return NULL;
14576 add_loc_list (&left, right);
14577 if (left == NULL)
14578 return NULL;
14580 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14581 return left;
14584 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14585 without actually allocating it. */
14587 static unsigned long
14588 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14590 return size_of_int_loc_descriptor (i >> shift)
14591 + size_of_int_loc_descriptor (shift)
14592 + 1;
14595 /* Return size_of_locs (int_loc_descriptor (i)) without
14596 actually allocating it. */
14598 static unsigned long
14599 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14601 unsigned long s;
14603 if (i >= 0)
14605 int clz, ctz;
14606 if (i <= 31)
14607 return 1;
14608 else if (i <= 0xff)
14609 return 2;
14610 else if (i <= 0xffff)
14611 return 3;
14612 clz = clz_hwi (i);
14613 ctz = ctz_hwi (i);
14614 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14615 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14616 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14617 - clz - 5);
14618 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14619 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14620 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14621 - clz - 8);
14622 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14623 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14624 <= 4)
14625 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14626 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14627 return 5;
14628 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14629 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14630 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14631 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14632 - clz - 8);
14633 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14634 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14635 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14636 - clz - 16);
14637 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14638 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14639 && s > 6)
14640 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14641 - clz - 32);
14642 else
14643 return 1 + s;
14645 else
14647 if (i >= -0x80)
14648 return 2;
14649 else if (i >= -0x8000)
14650 return 3;
14651 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14653 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14655 s = size_of_int_loc_descriptor (-i) + 1;
14656 if (s < 5)
14657 return s;
14659 return 5;
14661 else
14663 unsigned long r = 1 + size_of_sleb128 (i);
14664 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14666 s = size_of_int_loc_descriptor (-i) + 1;
14667 if (s < r)
14668 return s;
14670 return r;
14675 /* Return loc description representing "address" of integer value.
14676 This can appear only as toplevel expression. */
14678 static dw_loc_descr_ref
14679 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14681 int litsize;
14682 dw_loc_descr_ref loc_result = NULL;
14684 if (!(dwarf_version >= 4 || !dwarf_strict))
14685 return NULL;
14687 litsize = size_of_int_loc_descriptor (i);
14688 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14689 is more compact. For DW_OP_stack_value we need:
14690 litsize + 1 (DW_OP_stack_value)
14691 and for DW_OP_implicit_value:
14692 1 (DW_OP_implicit_value) + 1 (length) + size. */
14693 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14695 loc_result = int_loc_descriptor (i);
14696 add_loc_descr (&loc_result,
14697 new_loc_descr (DW_OP_stack_value, 0, 0));
14698 return loc_result;
14701 loc_result = new_loc_descr (DW_OP_implicit_value,
14702 size, 0);
14703 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14704 loc_result->dw_loc_oprnd2.v.val_int = i;
14705 return loc_result;
14708 /* Return a location descriptor that designates a base+offset location. */
14710 static dw_loc_descr_ref
14711 based_loc_descr (rtx reg, poly_int64 offset,
14712 enum var_init_status initialized)
14714 unsigned int regno;
14715 dw_loc_descr_ref result;
14716 dw_fde_ref fde = cfun->fde;
14718 /* We only use "frame base" when we're sure we're talking about the
14719 post-prologue local stack frame. We do this by *not* running
14720 register elimination until this point, and recognizing the special
14721 argument pointer and soft frame pointer rtx's. */
14722 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14724 rtx elim = (ira_use_lra_p
14725 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14726 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14728 if (elim != reg)
14730 /* Allow hard frame pointer here even if frame pointer
14731 isn't used since hard frame pointer is encoded with
14732 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14733 not hard frame pointer directly. */
14734 elim = strip_offset_and_add (elim, &offset);
14735 gcc_assert (elim == hard_frame_pointer_rtx
14736 || elim == stack_pointer_rtx);
14738 /* If drap register is used to align stack, use frame
14739 pointer + offset to access stack variables. If stack
14740 is aligned without drap, use stack pointer + offset to
14741 access stack variables. */
14742 if (crtl->stack_realign_tried
14743 && reg == frame_pointer_rtx)
14745 int base_reg
14746 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14747 ? HARD_FRAME_POINTER_REGNUM
14748 : REGNO (elim));
14749 return new_reg_loc_descr (base_reg, offset);
14752 gcc_assert (frame_pointer_fb_offset_valid);
14753 offset += frame_pointer_fb_offset;
14754 HOST_WIDE_INT const_offset;
14755 if (offset.is_constant (&const_offset))
14756 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14757 else
14759 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14760 loc_descr_plus_const (&ret, offset);
14761 return ret;
14766 regno = REGNO (reg);
14767 #ifdef LEAF_REG_REMAP
14768 if (crtl->uses_only_leaf_regs)
14770 int leaf_reg = LEAF_REG_REMAP (regno);
14771 if (leaf_reg != -1)
14772 regno = (unsigned) leaf_reg;
14774 #endif
14775 regno = DWARF_FRAME_REGNUM (regno);
14777 HOST_WIDE_INT const_offset;
14778 if (!optimize && fde
14779 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14780 && offset.is_constant (&const_offset))
14782 /* Use cfa+offset to represent the location of arguments passed
14783 on the stack when drap is used to align stack.
14784 Only do this when not optimizing, for optimized code var-tracking
14785 is supposed to track where the arguments live and the register
14786 used as vdrap or drap in some spot might be used for something
14787 else in other part of the routine. */
14788 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14791 result = new_reg_loc_descr (regno, offset);
14793 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14794 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14796 return result;
14799 /* Return true if this RTL expression describes a base+offset calculation. */
14801 static inline int
14802 is_based_loc (const_rtx rtl)
14804 return (GET_CODE (rtl) == PLUS
14805 && ((REG_P (XEXP (rtl, 0))
14806 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14807 && CONST_INT_P (XEXP (rtl, 1)))));
14810 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14811 failed. */
14813 static dw_loc_descr_ref
14814 tls_mem_loc_descriptor (rtx mem)
14816 tree base;
14817 dw_loc_descr_ref loc_result;
14819 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14820 return NULL;
14822 base = get_base_address (MEM_EXPR (mem));
14823 if (base == NULL
14824 || !VAR_P (base)
14825 || !DECL_THREAD_LOCAL_P (base))
14826 return NULL;
14828 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14829 if (loc_result == NULL)
14830 return NULL;
14832 if (maybe_ne (MEM_OFFSET (mem), 0))
14833 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14835 return loc_result;
14838 /* Output debug info about reason why we failed to expand expression as dwarf
14839 expression. */
14841 static void
14842 expansion_failed (tree expr, rtx rtl, char const *reason)
14844 if (dump_file && (dump_flags & TDF_DETAILS))
14846 fprintf (dump_file, "Failed to expand as dwarf: ");
14847 if (expr)
14848 print_generic_expr (dump_file, expr, dump_flags);
14849 if (rtl)
14851 fprintf (dump_file, "\n");
14852 print_rtl (dump_file, rtl);
14854 fprintf (dump_file, "\nReason: %s\n", reason);
14858 /* Helper function for const_ok_for_output. */
14860 static bool
14861 const_ok_for_output_1 (rtx rtl)
14863 if (targetm.const_not_ok_for_debug_p (rtl))
14865 if (GET_CODE (rtl) != UNSPEC)
14867 expansion_failed (NULL_TREE, rtl,
14868 "Expression rejected for debug by the backend.\n");
14869 return false;
14872 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14873 the target hook doesn't explicitly allow it in debug info, assume
14874 we can't express it in the debug info. */
14875 /* Don't complain about TLS UNSPECs, those are just too hard to
14876 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14877 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14878 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14879 if (flag_checking
14880 && (XVECLEN (rtl, 0) == 0
14881 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14882 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14883 inform (current_function_decl
14884 ? DECL_SOURCE_LOCATION (current_function_decl)
14885 : UNKNOWN_LOCATION,
14886 #if NUM_UNSPEC_VALUES > 0
14887 "non-delegitimized UNSPEC %s (%d) found in variable location",
14888 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14889 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14890 #else
14891 "non-delegitimized UNSPEC %d found in variable location",
14892 #endif
14893 XINT (rtl, 1));
14894 expansion_failed (NULL_TREE, rtl,
14895 "UNSPEC hasn't been delegitimized.\n");
14896 return false;
14899 if (CONST_POLY_INT_P (rtl))
14900 return false;
14902 /* FIXME: Refer to PR60655. It is possible for simplification
14903 of rtl expressions in var tracking to produce such expressions.
14904 We should really identify / validate expressions
14905 enclosed in CONST that can be handled by assemblers on various
14906 targets and only handle legitimate cases here. */
14907 switch (GET_CODE (rtl))
14909 case SYMBOL_REF:
14910 break;
14911 case NOT:
14912 case NEG:
14913 return false;
14914 case PLUS:
14916 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
14917 operands. */
14918 subrtx_var_iterator::array_type array;
14919 bool first = false;
14920 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14921 if (SYMBOL_REF_P (*iter)
14922 || LABEL_P (*iter)
14923 || GET_CODE (*iter) == UNSPEC)
14925 first = true;
14926 break;
14928 if (!first)
14929 return true;
14930 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14931 if (SYMBOL_REF_P (*iter)
14932 || LABEL_P (*iter)
14933 || GET_CODE (*iter) == UNSPEC)
14934 return false;
14935 return true;
14937 case MINUS:
14939 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
14940 appear in the second operand of MINUS. */
14941 subrtx_var_iterator::array_type array;
14942 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14943 if (SYMBOL_REF_P (*iter)
14944 || LABEL_P (*iter)
14945 || GET_CODE (*iter) == UNSPEC)
14946 return false;
14947 return true;
14949 default:
14950 return true;
14953 if (CONSTANT_POOL_ADDRESS_P (rtl))
14955 bool marked;
14956 get_pool_constant_mark (rtl, &marked);
14957 /* If all references to this pool constant were optimized away,
14958 it was not output and thus we can't represent it. */
14959 if (!marked)
14961 expansion_failed (NULL_TREE, rtl,
14962 "Constant was removed from constant pool.\n");
14963 return false;
14967 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14968 return false;
14970 /* Avoid references to external symbols in debug info, on several targets
14971 the linker might even refuse to link when linking a shared library,
14972 and in many other cases the relocations for .debug_info/.debug_loc are
14973 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14974 to be defined within the same shared library or executable are fine. */
14975 if (SYMBOL_REF_EXTERNAL_P (rtl))
14977 tree decl = SYMBOL_REF_DECL (rtl);
14979 if (decl == NULL || !targetm.binds_local_p (decl))
14981 expansion_failed (NULL_TREE, rtl,
14982 "Symbol not defined in current TU.\n");
14983 return false;
14987 return true;
14990 /* Return true if constant RTL can be emitted in DW_OP_addr or
14991 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14992 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14994 static bool
14995 const_ok_for_output (rtx rtl)
14997 if (GET_CODE (rtl) == SYMBOL_REF)
14998 return const_ok_for_output_1 (rtl);
15000 if (GET_CODE (rtl) == CONST)
15002 subrtx_var_iterator::array_type array;
15003 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15004 if (!const_ok_for_output_1 (*iter))
15005 return false;
15006 return true;
15009 return true;
15012 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15013 if possible, NULL otherwise. */
15015 static dw_die_ref
15016 base_type_for_mode (machine_mode mode, bool unsignedp)
15018 dw_die_ref type_die;
15019 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15021 if (type == NULL)
15022 return NULL;
15023 switch (TREE_CODE (type))
15025 case INTEGER_TYPE:
15026 case REAL_TYPE:
15027 break;
15028 default:
15029 return NULL;
15031 type_die = lookup_type_die (type);
15032 if (!type_die)
15033 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15034 comp_unit_die ());
15035 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15036 return NULL;
15037 return type_die;
15040 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15041 type matching MODE, or, if MODE is narrower than or as wide as
15042 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15043 possible. */
15045 static dw_loc_descr_ref
15046 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15048 machine_mode outer_mode = mode;
15049 dw_die_ref type_die;
15050 dw_loc_descr_ref cvt;
15052 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15054 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15055 return op;
15057 type_die = base_type_for_mode (outer_mode, 1);
15058 if (type_die == NULL)
15059 return NULL;
15060 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15061 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15062 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15063 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15064 add_loc_descr (&op, cvt);
15065 return op;
15068 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15070 static dw_loc_descr_ref
15071 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15072 dw_loc_descr_ref op1)
15074 dw_loc_descr_ref ret = op0;
15075 add_loc_descr (&ret, op1);
15076 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15077 if (STORE_FLAG_VALUE != 1)
15079 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15080 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15082 return ret;
15085 /* Subroutine of scompare_loc_descriptor for the case in which we're
15086 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15087 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15089 static dw_loc_descr_ref
15090 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15091 scalar_int_mode op_mode,
15092 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15094 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15095 dw_loc_descr_ref cvt;
15097 if (type_die == NULL)
15098 return NULL;
15099 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15100 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15101 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15102 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15103 add_loc_descr (&op0, cvt);
15104 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15105 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15106 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15107 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15108 add_loc_descr (&op1, cvt);
15109 return compare_loc_descriptor (op, op0, op1);
15112 /* Subroutine of scompare_loc_descriptor for the case in which we're
15113 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15114 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15116 static dw_loc_descr_ref
15117 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15118 scalar_int_mode op_mode,
15119 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15121 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15122 /* For eq/ne, if the operands are known to be zero-extended,
15123 there is no need to do the fancy shifting up. */
15124 if (op == DW_OP_eq || op == DW_OP_ne)
15126 dw_loc_descr_ref last0, last1;
15127 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15129 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15131 /* deref_size zero extends, and for constants we can check
15132 whether they are zero extended or not. */
15133 if (((last0->dw_loc_opc == DW_OP_deref_size
15134 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15135 || (CONST_INT_P (XEXP (rtl, 0))
15136 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15137 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15138 && ((last1->dw_loc_opc == DW_OP_deref_size
15139 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15140 || (CONST_INT_P (XEXP (rtl, 1))
15141 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15142 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15143 return compare_loc_descriptor (op, op0, op1);
15145 /* EQ/NE comparison against constant in narrower type than
15146 DWARF2_ADDR_SIZE can be performed either as
15147 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15148 DW_OP_{eq,ne}
15150 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15151 DW_OP_{eq,ne}. Pick whatever is shorter. */
15152 if (CONST_INT_P (XEXP (rtl, 1))
15153 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15154 && (size_of_int_loc_descriptor (shift) + 1
15155 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15156 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15157 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15158 & GET_MODE_MASK (op_mode))))
15160 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15161 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15162 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15163 & GET_MODE_MASK (op_mode));
15164 return compare_loc_descriptor (op, op0, op1);
15167 add_loc_descr (&op0, int_loc_descriptor (shift));
15168 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15169 if (CONST_INT_P (XEXP (rtl, 1)))
15170 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15171 else
15173 add_loc_descr (&op1, int_loc_descriptor (shift));
15174 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15176 return compare_loc_descriptor (op, op0, op1);
15179 /* Return location descriptor for signed comparison OP RTL. */
15181 static dw_loc_descr_ref
15182 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15183 machine_mode mem_mode)
15185 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15186 dw_loc_descr_ref op0, op1;
15188 if (op_mode == VOIDmode)
15189 op_mode = GET_MODE (XEXP (rtl, 1));
15190 if (op_mode == VOIDmode)
15191 return NULL;
15193 scalar_int_mode int_op_mode;
15194 if (dwarf_strict
15195 && dwarf_version < 5
15196 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15197 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15198 return NULL;
15200 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15201 VAR_INIT_STATUS_INITIALIZED);
15202 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15203 VAR_INIT_STATUS_INITIALIZED);
15205 if (op0 == NULL || op1 == NULL)
15206 return NULL;
15208 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15210 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15211 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15213 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15214 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15216 return compare_loc_descriptor (op, op0, op1);
15219 /* Return location descriptor for unsigned comparison OP RTL. */
15221 static dw_loc_descr_ref
15222 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15223 machine_mode mem_mode)
15225 dw_loc_descr_ref op0, op1;
15227 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15228 if (test_op_mode == VOIDmode)
15229 test_op_mode = GET_MODE (XEXP (rtl, 1));
15231 scalar_int_mode op_mode;
15232 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15233 return NULL;
15235 if (dwarf_strict
15236 && dwarf_version < 5
15237 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15238 return NULL;
15240 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15241 VAR_INIT_STATUS_INITIALIZED);
15242 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15243 VAR_INIT_STATUS_INITIALIZED);
15245 if (op0 == NULL || op1 == NULL)
15246 return NULL;
15248 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15250 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15251 dw_loc_descr_ref last0, last1;
15252 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15254 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15256 if (CONST_INT_P (XEXP (rtl, 0)))
15257 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15258 /* deref_size zero extends, so no need to mask it again. */
15259 else if (last0->dw_loc_opc != DW_OP_deref_size
15260 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15262 add_loc_descr (&op0, int_loc_descriptor (mask));
15263 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15265 if (CONST_INT_P (XEXP (rtl, 1)))
15266 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15267 /* deref_size zero extends, so no need to mask it again. */
15268 else if (last1->dw_loc_opc != DW_OP_deref_size
15269 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15271 add_loc_descr (&op1, int_loc_descriptor (mask));
15272 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15275 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15277 HOST_WIDE_INT bias = 1;
15278 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15279 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15280 if (CONST_INT_P (XEXP (rtl, 1)))
15281 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15282 + INTVAL (XEXP (rtl, 1)));
15283 else
15284 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15285 bias, 0));
15287 return compare_loc_descriptor (op, op0, op1);
15290 /* Return location descriptor for {U,S}{MIN,MAX}. */
15292 static dw_loc_descr_ref
15293 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15294 machine_mode mem_mode)
15296 enum dwarf_location_atom op;
15297 dw_loc_descr_ref op0, op1, ret;
15298 dw_loc_descr_ref bra_node, drop_node;
15300 scalar_int_mode int_mode;
15301 if (dwarf_strict
15302 && dwarf_version < 5
15303 && (!is_a <scalar_int_mode> (mode, &int_mode)
15304 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15305 return NULL;
15307 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15308 VAR_INIT_STATUS_INITIALIZED);
15309 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15310 VAR_INIT_STATUS_INITIALIZED);
15312 if (op0 == NULL || op1 == NULL)
15313 return NULL;
15315 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15316 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15317 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15318 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15320 /* Checked by the caller. */
15321 int_mode = as_a <scalar_int_mode> (mode);
15322 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15324 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15325 add_loc_descr (&op0, int_loc_descriptor (mask));
15326 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15327 add_loc_descr (&op1, int_loc_descriptor (mask));
15328 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15330 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15332 HOST_WIDE_INT bias = 1;
15333 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15334 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15335 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15338 else if (is_a <scalar_int_mode> (mode, &int_mode)
15339 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15341 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15342 add_loc_descr (&op0, int_loc_descriptor (shift));
15343 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15344 add_loc_descr (&op1, int_loc_descriptor (shift));
15345 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15347 else if (is_a <scalar_int_mode> (mode, &int_mode)
15348 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15350 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15351 dw_loc_descr_ref cvt;
15352 if (type_die == NULL)
15353 return NULL;
15354 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15355 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15356 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15357 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15358 add_loc_descr (&op0, cvt);
15359 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15360 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15361 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15362 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15363 add_loc_descr (&op1, cvt);
15366 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15367 op = DW_OP_lt;
15368 else
15369 op = DW_OP_gt;
15370 ret = op0;
15371 add_loc_descr (&ret, op1);
15372 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15373 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15374 add_loc_descr (&ret, bra_node);
15375 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15376 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15377 add_loc_descr (&ret, drop_node);
15378 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15379 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15380 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15381 && is_a <scalar_int_mode> (mode, &int_mode)
15382 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15383 ret = convert_descriptor_to_mode (int_mode, ret);
15384 return ret;
15387 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15388 but after converting arguments to type_die, afterwards
15389 convert back to unsigned. */
15391 static dw_loc_descr_ref
15392 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15393 scalar_int_mode mode, machine_mode mem_mode)
15395 dw_loc_descr_ref cvt, op0, op1;
15397 if (type_die == NULL)
15398 return NULL;
15399 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15400 VAR_INIT_STATUS_INITIALIZED);
15401 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15402 VAR_INIT_STATUS_INITIALIZED);
15403 if (op0 == NULL || op1 == NULL)
15404 return NULL;
15405 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15406 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15407 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15408 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15409 add_loc_descr (&op0, cvt);
15410 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15411 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15412 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15413 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15414 add_loc_descr (&op1, cvt);
15415 add_loc_descr (&op0, op1);
15416 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15417 return convert_descriptor_to_mode (mode, op0);
15420 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15421 const0 is DW_OP_lit0 or corresponding typed constant,
15422 const1 is DW_OP_lit1 or corresponding typed constant
15423 and constMSB is constant with just the MSB bit set
15424 for the mode):
15425 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15426 L1: const0 DW_OP_swap
15427 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15428 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15429 L3: DW_OP_drop
15430 L4: DW_OP_nop
15432 CTZ is similar:
15433 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15434 L1: const0 DW_OP_swap
15435 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15436 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15437 L3: DW_OP_drop
15438 L4: DW_OP_nop
15440 FFS is similar:
15441 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15442 L1: const1 DW_OP_swap
15443 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15444 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15445 L3: DW_OP_drop
15446 L4: DW_OP_nop */
15448 static dw_loc_descr_ref
15449 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15450 machine_mode mem_mode)
15452 dw_loc_descr_ref op0, ret, tmp;
15453 HOST_WIDE_INT valv;
15454 dw_loc_descr_ref l1jump, l1label;
15455 dw_loc_descr_ref l2jump, l2label;
15456 dw_loc_descr_ref l3jump, l3label;
15457 dw_loc_descr_ref l4jump, l4label;
15458 rtx msb;
15460 if (GET_MODE (XEXP (rtl, 0)) != mode)
15461 return NULL;
15463 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15464 VAR_INIT_STATUS_INITIALIZED);
15465 if (op0 == NULL)
15466 return NULL;
15467 ret = op0;
15468 if (GET_CODE (rtl) == CLZ)
15470 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15471 valv = GET_MODE_BITSIZE (mode);
15473 else if (GET_CODE (rtl) == FFS)
15474 valv = 0;
15475 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15476 valv = GET_MODE_BITSIZE (mode);
15477 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15478 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15479 add_loc_descr (&ret, l1jump);
15480 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15481 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15482 VAR_INIT_STATUS_INITIALIZED);
15483 if (tmp == NULL)
15484 return NULL;
15485 add_loc_descr (&ret, tmp);
15486 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15487 add_loc_descr (&ret, l4jump);
15488 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15489 ? const1_rtx : const0_rtx,
15490 mode, mem_mode,
15491 VAR_INIT_STATUS_INITIALIZED);
15492 if (l1label == NULL)
15493 return NULL;
15494 add_loc_descr (&ret, l1label);
15495 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15496 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15497 add_loc_descr (&ret, l2label);
15498 if (GET_CODE (rtl) != CLZ)
15499 msb = const1_rtx;
15500 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15501 msb = GEN_INT (HOST_WIDE_INT_1U
15502 << (GET_MODE_BITSIZE (mode) - 1));
15503 else
15504 msb = immed_wide_int_const
15505 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15506 GET_MODE_PRECISION (mode)), mode);
15507 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15508 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15509 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15510 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15511 else
15512 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15513 VAR_INIT_STATUS_INITIALIZED);
15514 if (tmp == NULL)
15515 return NULL;
15516 add_loc_descr (&ret, tmp);
15517 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15518 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15519 add_loc_descr (&ret, l3jump);
15520 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15521 VAR_INIT_STATUS_INITIALIZED);
15522 if (tmp == NULL)
15523 return NULL;
15524 add_loc_descr (&ret, tmp);
15525 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15526 ? DW_OP_shl : DW_OP_shr, 0, 0));
15527 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15528 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15529 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15530 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15531 add_loc_descr (&ret, l2jump);
15532 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15533 add_loc_descr (&ret, l3label);
15534 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15535 add_loc_descr (&ret, l4label);
15536 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15537 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15538 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15539 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15540 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15541 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15542 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15543 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15544 return ret;
15547 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15548 const1 is DW_OP_lit1 or corresponding typed constant):
15549 const0 DW_OP_swap
15550 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15551 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15552 L2: DW_OP_drop
15554 PARITY is similar:
15555 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15556 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15557 L2: DW_OP_drop */
15559 static dw_loc_descr_ref
15560 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15561 machine_mode mem_mode)
15563 dw_loc_descr_ref op0, ret, tmp;
15564 dw_loc_descr_ref l1jump, l1label;
15565 dw_loc_descr_ref l2jump, l2label;
15567 if (GET_MODE (XEXP (rtl, 0)) != mode)
15568 return NULL;
15570 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15571 VAR_INIT_STATUS_INITIALIZED);
15572 if (op0 == NULL)
15573 return NULL;
15574 ret = op0;
15575 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15576 VAR_INIT_STATUS_INITIALIZED);
15577 if (tmp == NULL)
15578 return NULL;
15579 add_loc_descr (&ret, tmp);
15580 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15581 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15582 add_loc_descr (&ret, l1label);
15583 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15584 add_loc_descr (&ret, l2jump);
15585 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15586 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15587 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15588 VAR_INIT_STATUS_INITIALIZED);
15589 if (tmp == NULL)
15590 return NULL;
15591 add_loc_descr (&ret, tmp);
15592 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15593 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15594 ? DW_OP_plus : DW_OP_xor, 0, 0));
15595 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15596 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15597 VAR_INIT_STATUS_INITIALIZED);
15598 add_loc_descr (&ret, tmp);
15599 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15600 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15601 add_loc_descr (&ret, l1jump);
15602 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15603 add_loc_descr (&ret, l2label);
15604 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15605 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15606 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15607 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15608 return ret;
15611 /* BSWAP (constS is initial shift count, either 56 or 24):
15612 constS const0
15613 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15614 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15615 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15616 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15617 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15619 static dw_loc_descr_ref
15620 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15621 machine_mode mem_mode)
15623 dw_loc_descr_ref op0, ret, tmp;
15624 dw_loc_descr_ref l1jump, l1label;
15625 dw_loc_descr_ref l2jump, l2label;
15627 if (BITS_PER_UNIT != 8
15628 || (GET_MODE_BITSIZE (mode) != 32
15629 && GET_MODE_BITSIZE (mode) != 64))
15630 return NULL;
15632 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15633 VAR_INIT_STATUS_INITIALIZED);
15634 if (op0 == NULL)
15635 return NULL;
15637 ret = op0;
15638 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15639 mode, mem_mode,
15640 VAR_INIT_STATUS_INITIALIZED);
15641 if (tmp == NULL)
15642 return NULL;
15643 add_loc_descr (&ret, tmp);
15644 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15645 VAR_INIT_STATUS_INITIALIZED);
15646 if (tmp == NULL)
15647 return NULL;
15648 add_loc_descr (&ret, tmp);
15649 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15650 add_loc_descr (&ret, l1label);
15651 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15652 mode, mem_mode,
15653 VAR_INIT_STATUS_INITIALIZED);
15654 add_loc_descr (&ret, tmp);
15655 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15656 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15657 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15658 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15659 VAR_INIT_STATUS_INITIALIZED);
15660 if (tmp == NULL)
15661 return NULL;
15662 add_loc_descr (&ret, tmp);
15663 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15664 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15665 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15666 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15667 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15668 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15669 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15670 VAR_INIT_STATUS_INITIALIZED);
15671 add_loc_descr (&ret, tmp);
15672 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15673 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15674 add_loc_descr (&ret, l2jump);
15675 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15676 VAR_INIT_STATUS_INITIALIZED);
15677 add_loc_descr (&ret, tmp);
15678 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15679 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15680 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15681 add_loc_descr (&ret, l1jump);
15682 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15683 add_loc_descr (&ret, l2label);
15684 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15685 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15686 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15687 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15688 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15689 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15690 return ret;
15693 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15694 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15695 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15696 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15698 ROTATERT is similar:
15699 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15700 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15701 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15703 static dw_loc_descr_ref
15704 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15705 machine_mode mem_mode)
15707 rtx rtlop1 = XEXP (rtl, 1);
15708 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15709 int i;
15711 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15712 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15713 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15714 VAR_INIT_STATUS_INITIALIZED);
15715 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15716 VAR_INIT_STATUS_INITIALIZED);
15717 if (op0 == NULL || op1 == NULL)
15718 return NULL;
15719 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15720 for (i = 0; i < 2; i++)
15722 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15723 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15724 mode, mem_mode,
15725 VAR_INIT_STATUS_INITIALIZED);
15726 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15727 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15728 ? DW_OP_const4u
15729 : HOST_BITS_PER_WIDE_INT == 64
15730 ? DW_OP_const8u : DW_OP_constu,
15731 GET_MODE_MASK (mode), 0);
15732 else
15733 mask[i] = NULL;
15734 if (mask[i] == NULL)
15735 return NULL;
15736 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15738 ret = op0;
15739 add_loc_descr (&ret, op1);
15740 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15741 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15742 if (GET_CODE (rtl) == ROTATERT)
15744 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15745 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15746 GET_MODE_BITSIZE (mode), 0));
15748 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15749 if (mask[0] != NULL)
15750 add_loc_descr (&ret, mask[0]);
15751 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15752 if (mask[1] != NULL)
15754 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15755 add_loc_descr (&ret, mask[1]);
15756 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15758 if (GET_CODE (rtl) == ROTATE)
15760 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15761 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15762 GET_MODE_BITSIZE (mode), 0));
15764 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15765 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15766 return ret;
15769 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15770 for DEBUG_PARAMETER_REF RTL. */
15772 static dw_loc_descr_ref
15773 parameter_ref_descriptor (rtx rtl)
15775 dw_loc_descr_ref ret;
15776 dw_die_ref ref;
15778 if (dwarf_strict)
15779 return NULL;
15780 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15781 /* With LTO during LTRANS we get the late DIE that refers to the early
15782 DIE, thus we add another indirection here. This seems to confuse
15783 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15784 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15785 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15786 if (ref)
15788 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15789 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15790 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15792 else
15794 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15795 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15797 return ret;
15800 /* The following routine converts the RTL for a variable or parameter
15801 (resident in memory) into an equivalent Dwarf representation of a
15802 mechanism for getting the address of that same variable onto the top of a
15803 hypothetical "address evaluation" stack.
15805 When creating memory location descriptors, we are effectively transforming
15806 the RTL for a memory-resident object into its Dwarf postfix expression
15807 equivalent. This routine recursively descends an RTL tree, turning
15808 it into Dwarf postfix code as it goes.
15810 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15812 MEM_MODE is the mode of the memory reference, needed to handle some
15813 autoincrement addressing modes.
15815 Return 0 if we can't represent the location. */
15817 dw_loc_descr_ref
15818 mem_loc_descriptor (rtx rtl, machine_mode mode,
15819 machine_mode mem_mode,
15820 enum var_init_status initialized)
15822 dw_loc_descr_ref mem_loc_result = NULL;
15823 enum dwarf_location_atom op;
15824 dw_loc_descr_ref op0, op1;
15825 rtx inner = NULL_RTX;
15826 poly_int64 offset;
15828 if (mode == VOIDmode)
15829 mode = GET_MODE (rtl);
15831 /* Note that for a dynamically sized array, the location we will generate a
15832 description of here will be the lowest numbered location which is
15833 actually within the array. That's *not* necessarily the same as the
15834 zeroth element of the array. */
15836 rtl = targetm.delegitimize_address (rtl);
15838 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15839 return NULL;
15841 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
15842 switch (GET_CODE (rtl))
15844 case POST_INC:
15845 case POST_DEC:
15846 case POST_MODIFY:
15847 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15849 case SUBREG:
15850 /* The case of a subreg may arise when we have a local (register)
15851 variable or a formal (register) parameter which doesn't quite fill
15852 up an entire register. For now, just assume that it is
15853 legitimate to make the Dwarf info refer to the whole register which
15854 contains the given subreg. */
15855 if (!subreg_lowpart_p (rtl))
15856 break;
15857 inner = SUBREG_REG (rtl);
15858 /* FALLTHRU */
15859 case TRUNCATE:
15860 if (inner == NULL_RTX)
15861 inner = XEXP (rtl, 0);
15862 if (is_a <scalar_int_mode> (mode, &int_mode)
15863 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15864 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15865 #ifdef POINTERS_EXTEND_UNSIGNED
15866 || (int_mode == Pmode && mem_mode != VOIDmode)
15867 #endif
15869 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15871 mem_loc_result = mem_loc_descriptor (inner,
15872 inner_mode,
15873 mem_mode, initialized);
15874 break;
15876 if (dwarf_strict && dwarf_version < 5)
15877 break;
15878 if (is_a <scalar_int_mode> (mode, &int_mode)
15879 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15880 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15881 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15883 dw_die_ref type_die;
15884 dw_loc_descr_ref cvt;
15886 mem_loc_result = mem_loc_descriptor (inner,
15887 GET_MODE (inner),
15888 mem_mode, initialized);
15889 if (mem_loc_result == NULL)
15890 break;
15891 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15892 if (type_die == NULL)
15894 mem_loc_result = NULL;
15895 break;
15897 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15898 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15899 else
15900 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15901 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15902 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15903 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15904 add_loc_descr (&mem_loc_result, cvt);
15905 if (is_a <scalar_int_mode> (mode, &int_mode)
15906 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15908 /* Convert it to untyped afterwards. */
15909 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15910 add_loc_descr (&mem_loc_result, cvt);
15913 break;
15915 case REG:
15916 if (!is_a <scalar_int_mode> (mode, &int_mode)
15917 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15918 && rtl != arg_pointer_rtx
15919 && rtl != frame_pointer_rtx
15920 #ifdef POINTERS_EXTEND_UNSIGNED
15921 && (int_mode != Pmode || mem_mode == VOIDmode)
15922 #endif
15925 dw_die_ref type_die;
15926 unsigned int dbx_regnum;
15928 if (dwarf_strict && dwarf_version < 5)
15929 break;
15930 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
15931 break;
15932 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15933 if (type_die == NULL)
15934 break;
15936 dbx_regnum = dbx_reg_number (rtl);
15937 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15938 break;
15939 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15940 dbx_regnum, 0);
15941 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15942 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15943 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15944 break;
15946 /* Whenever a register number forms a part of the description of the
15947 method for calculating the (dynamic) address of a memory resident
15948 object, DWARF rules require the register number be referred to as
15949 a "base register". This distinction is not based in any way upon
15950 what category of register the hardware believes the given register
15951 belongs to. This is strictly DWARF terminology we're dealing with
15952 here. Note that in cases where the location of a memory-resident
15953 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15954 OP_CONST (0)) the actual DWARF location descriptor that we generate
15955 may just be OP_BASEREG (basereg). This may look deceptively like
15956 the object in question was allocated to a register (rather than in
15957 memory) so DWARF consumers need to be aware of the subtle
15958 distinction between OP_REG and OP_BASEREG. */
15959 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15960 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15961 else if (stack_realign_drap
15962 && crtl->drap_reg
15963 && crtl->args.internal_arg_pointer == rtl
15964 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15966 /* If RTL is internal_arg_pointer, which has been optimized
15967 out, use DRAP instead. */
15968 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15969 VAR_INIT_STATUS_INITIALIZED);
15971 break;
15973 case SIGN_EXTEND:
15974 case ZERO_EXTEND:
15975 if (!is_a <scalar_int_mode> (mode, &int_mode)
15976 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15977 break;
15978 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15979 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15980 if (op0 == 0)
15981 break;
15982 else if (GET_CODE (rtl) == ZERO_EXTEND
15983 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15984 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15985 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15986 to expand zero extend as two shifts instead of
15987 masking. */
15988 && GET_MODE_SIZE (inner_mode) <= 4)
15990 mem_loc_result = op0;
15991 add_loc_descr (&mem_loc_result,
15992 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15993 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15995 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15997 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15998 shift *= BITS_PER_UNIT;
15999 if (GET_CODE (rtl) == SIGN_EXTEND)
16000 op = DW_OP_shra;
16001 else
16002 op = DW_OP_shr;
16003 mem_loc_result = op0;
16004 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16005 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16006 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16007 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16009 else if (!dwarf_strict || dwarf_version >= 5)
16011 dw_die_ref type_die1, type_die2;
16012 dw_loc_descr_ref cvt;
16014 type_die1 = base_type_for_mode (inner_mode,
16015 GET_CODE (rtl) == ZERO_EXTEND);
16016 if (type_die1 == NULL)
16017 break;
16018 type_die2 = base_type_for_mode (int_mode, 1);
16019 if (type_die2 == NULL)
16020 break;
16021 mem_loc_result = op0;
16022 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16023 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16024 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16025 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16026 add_loc_descr (&mem_loc_result, cvt);
16027 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16028 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16029 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16030 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16031 add_loc_descr (&mem_loc_result, cvt);
16033 break;
16035 case MEM:
16037 rtx new_rtl = avoid_constant_pool_reference (rtl);
16038 if (new_rtl != rtl)
16040 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16041 initialized);
16042 if (mem_loc_result != NULL)
16043 return mem_loc_result;
16046 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16047 get_address_mode (rtl), mode,
16048 VAR_INIT_STATUS_INITIALIZED);
16049 if (mem_loc_result == NULL)
16050 mem_loc_result = tls_mem_loc_descriptor (rtl);
16051 if (mem_loc_result != NULL)
16053 if (!is_a <scalar_int_mode> (mode, &int_mode)
16054 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16056 dw_die_ref type_die;
16057 dw_loc_descr_ref deref;
16058 HOST_WIDE_INT size;
16060 if (dwarf_strict && dwarf_version < 5)
16061 return NULL;
16062 if (!GET_MODE_SIZE (mode).is_constant (&size))
16063 return NULL;
16064 type_die
16065 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16066 if (type_die == NULL)
16067 return NULL;
16068 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16069 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16070 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16071 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16072 add_loc_descr (&mem_loc_result, deref);
16074 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16075 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16076 else
16077 add_loc_descr (&mem_loc_result,
16078 new_loc_descr (DW_OP_deref_size,
16079 GET_MODE_SIZE (int_mode), 0));
16081 break;
16083 case LO_SUM:
16084 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16086 case LABEL_REF:
16087 /* Some ports can transform a symbol ref into a label ref, because
16088 the symbol ref is too far away and has to be dumped into a constant
16089 pool. */
16090 case CONST:
16091 case SYMBOL_REF:
16092 case UNSPEC:
16093 if (!is_a <scalar_int_mode> (mode, &int_mode)
16094 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16095 #ifdef POINTERS_EXTEND_UNSIGNED
16096 && (int_mode != Pmode || mem_mode == VOIDmode)
16097 #endif
16099 break;
16101 if (GET_CODE (rtl) == UNSPEC)
16103 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16104 can't express it in the debug info. This can happen e.g. with some
16105 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16106 approves. */
16107 bool not_ok = false;
16108 subrtx_var_iterator::array_type array;
16109 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16110 if (*iter != rtl && !CONSTANT_P (*iter))
16112 not_ok = true;
16113 break;
16116 if (not_ok)
16117 break;
16119 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16120 if (!const_ok_for_output_1 (*iter))
16122 not_ok = true;
16123 break;
16126 if (not_ok)
16127 break;
16129 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16130 goto symref;
16133 if (GET_CODE (rtl) == SYMBOL_REF
16134 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16136 dw_loc_descr_ref temp;
16138 /* If this is not defined, we have no way to emit the data. */
16139 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16140 break;
16142 temp = new_addr_loc_descr (rtl, dtprel_true);
16144 /* We check for DWARF 5 here because gdb did not implement
16145 DW_OP_form_tls_address until after 7.12. */
16146 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16147 ? DW_OP_form_tls_address
16148 : DW_OP_GNU_push_tls_address),
16149 0, 0);
16150 add_loc_descr (&mem_loc_result, temp);
16152 break;
16155 if (!const_ok_for_output (rtl))
16157 if (GET_CODE (rtl) == CONST)
16158 switch (GET_CODE (XEXP (rtl, 0)))
16160 case NOT:
16161 op = DW_OP_not;
16162 goto try_const_unop;
16163 case NEG:
16164 op = DW_OP_neg;
16165 goto try_const_unop;
16166 try_const_unop:
16167 rtx arg;
16168 arg = XEXP (XEXP (rtl, 0), 0);
16169 if (!CONSTANT_P (arg))
16170 arg = gen_rtx_CONST (int_mode, arg);
16171 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16172 initialized);
16173 if (op0)
16175 mem_loc_result = op0;
16176 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16178 break;
16179 default:
16180 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16181 mem_mode, initialized);
16182 break;
16184 break;
16187 symref:
16188 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16189 vec_safe_push (used_rtx_array, rtl);
16190 break;
16192 case CONCAT:
16193 case CONCATN:
16194 case VAR_LOCATION:
16195 case DEBUG_IMPLICIT_PTR:
16196 expansion_failed (NULL_TREE, rtl,
16197 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16198 return 0;
16200 case ENTRY_VALUE:
16201 if (dwarf_strict && dwarf_version < 5)
16202 return NULL;
16203 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16205 if (!is_a <scalar_int_mode> (mode, &int_mode)
16206 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16207 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16208 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16209 else
16211 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
16212 if (dbx_regnum == IGNORED_DWARF_REGNUM)
16213 return NULL;
16214 op0 = one_reg_loc_descriptor (dbx_regnum,
16215 VAR_INIT_STATUS_INITIALIZED);
16218 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16219 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16221 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16222 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16223 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16224 return NULL;
16226 else
16227 gcc_unreachable ();
16228 if (op0 == NULL)
16229 return NULL;
16230 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16231 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16232 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16233 break;
16235 case DEBUG_PARAMETER_REF:
16236 mem_loc_result = parameter_ref_descriptor (rtl);
16237 break;
16239 case PRE_MODIFY:
16240 /* Extract the PLUS expression nested inside and fall into
16241 PLUS code below. */
16242 rtl = XEXP (rtl, 1);
16243 goto plus;
16245 case PRE_INC:
16246 case PRE_DEC:
16247 /* Turn these into a PLUS expression and fall into the PLUS code
16248 below. */
16249 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16250 gen_int_mode (GET_CODE (rtl) == PRE_INC
16251 ? GET_MODE_UNIT_SIZE (mem_mode)
16252 : -GET_MODE_UNIT_SIZE (mem_mode),
16253 mode));
16255 /* fall through */
16257 case PLUS:
16258 plus:
16259 if (is_based_loc (rtl)
16260 && is_a <scalar_int_mode> (mode, &int_mode)
16261 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16262 || XEXP (rtl, 0) == arg_pointer_rtx
16263 || XEXP (rtl, 0) == frame_pointer_rtx))
16264 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16265 INTVAL (XEXP (rtl, 1)),
16266 VAR_INIT_STATUS_INITIALIZED);
16267 else
16269 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16270 VAR_INIT_STATUS_INITIALIZED);
16271 if (mem_loc_result == 0)
16272 break;
16274 if (CONST_INT_P (XEXP (rtl, 1))
16275 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16276 <= DWARF2_ADDR_SIZE))
16277 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16278 else
16280 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16281 VAR_INIT_STATUS_INITIALIZED);
16282 if (op1 == 0)
16283 return NULL;
16284 add_loc_descr (&mem_loc_result, op1);
16285 add_loc_descr (&mem_loc_result,
16286 new_loc_descr (DW_OP_plus, 0, 0));
16289 break;
16291 /* If a pseudo-reg is optimized away, it is possible for it to
16292 be replaced with a MEM containing a multiply or shift. */
16293 case MINUS:
16294 op = DW_OP_minus;
16295 goto do_binop;
16297 case MULT:
16298 op = DW_OP_mul;
16299 goto do_binop;
16301 case DIV:
16302 if ((!dwarf_strict || dwarf_version >= 5)
16303 && is_a <scalar_int_mode> (mode, &int_mode)
16304 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16306 mem_loc_result = typed_binop (DW_OP_div, rtl,
16307 base_type_for_mode (mode, 0),
16308 int_mode, mem_mode);
16309 break;
16311 op = DW_OP_div;
16312 goto do_binop;
16314 case UMOD:
16315 op = DW_OP_mod;
16316 goto do_binop;
16318 case ASHIFT:
16319 op = DW_OP_shl;
16320 goto do_shift;
16322 case ASHIFTRT:
16323 op = DW_OP_shra;
16324 goto do_shift;
16326 case LSHIFTRT:
16327 op = DW_OP_shr;
16328 goto do_shift;
16330 do_shift:
16331 if (!is_a <scalar_int_mode> (mode, &int_mode))
16332 break;
16333 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16334 VAR_INIT_STATUS_INITIALIZED);
16336 rtx rtlop1 = XEXP (rtl, 1);
16337 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16338 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16339 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16340 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16341 VAR_INIT_STATUS_INITIALIZED);
16344 if (op0 == 0 || op1 == 0)
16345 break;
16347 mem_loc_result = op0;
16348 add_loc_descr (&mem_loc_result, op1);
16349 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16350 break;
16352 case AND:
16353 op = DW_OP_and;
16354 goto do_binop;
16356 case IOR:
16357 op = DW_OP_or;
16358 goto do_binop;
16360 case XOR:
16361 op = DW_OP_xor;
16362 goto do_binop;
16364 do_binop:
16365 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16366 VAR_INIT_STATUS_INITIALIZED);
16367 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16368 VAR_INIT_STATUS_INITIALIZED);
16370 if (op0 == 0 || op1 == 0)
16371 break;
16373 mem_loc_result = op0;
16374 add_loc_descr (&mem_loc_result, op1);
16375 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16376 break;
16378 case MOD:
16379 if ((!dwarf_strict || dwarf_version >= 5)
16380 && is_a <scalar_int_mode> (mode, &int_mode)
16381 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16383 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16384 base_type_for_mode (mode, 0),
16385 int_mode, mem_mode);
16386 break;
16389 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16390 VAR_INIT_STATUS_INITIALIZED);
16391 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16392 VAR_INIT_STATUS_INITIALIZED);
16394 if (op0 == 0 || op1 == 0)
16395 break;
16397 mem_loc_result = op0;
16398 add_loc_descr (&mem_loc_result, op1);
16399 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16400 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16401 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16402 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16403 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16404 break;
16406 case UDIV:
16407 if ((!dwarf_strict || dwarf_version >= 5)
16408 && is_a <scalar_int_mode> (mode, &int_mode))
16410 /* We can use a signed divide if the sign bit is not set. */
16411 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16413 op = DW_OP_div;
16414 goto do_binop;
16417 mem_loc_result = typed_binop (DW_OP_div, rtl,
16418 base_type_for_mode (int_mode, 1),
16419 int_mode, mem_mode);
16421 break;
16423 case NOT:
16424 op = DW_OP_not;
16425 goto do_unop;
16427 case ABS:
16428 op = DW_OP_abs;
16429 goto do_unop;
16431 case NEG:
16432 op = DW_OP_neg;
16433 goto do_unop;
16435 do_unop:
16436 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16437 VAR_INIT_STATUS_INITIALIZED);
16439 if (op0 == 0)
16440 break;
16442 mem_loc_result = op0;
16443 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16444 break;
16446 case CONST_INT:
16447 if (!is_a <scalar_int_mode> (mode, &int_mode)
16448 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16449 #ifdef POINTERS_EXTEND_UNSIGNED
16450 || (int_mode == Pmode
16451 && mem_mode != VOIDmode
16452 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16453 #endif
16456 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16457 break;
16459 if ((!dwarf_strict || dwarf_version >= 5)
16460 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16461 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16463 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16464 scalar_int_mode amode;
16465 if (type_die == NULL)
16466 return NULL;
16467 if (INTVAL (rtl) >= 0
16468 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16469 .exists (&amode))
16470 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16471 /* const DW_OP_convert <XXX> vs.
16472 DW_OP_const_type <XXX, 1, const>. */
16473 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16474 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16476 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16477 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16478 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16479 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16480 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16481 add_loc_descr (&mem_loc_result, op0);
16482 return mem_loc_result;
16484 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16485 INTVAL (rtl));
16486 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16487 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16488 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16489 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16490 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16491 else
16493 mem_loc_result->dw_loc_oprnd2.val_class
16494 = dw_val_class_const_double;
16495 mem_loc_result->dw_loc_oprnd2.v.val_double
16496 = double_int::from_shwi (INTVAL (rtl));
16499 break;
16501 case CONST_DOUBLE:
16502 if (!dwarf_strict || dwarf_version >= 5)
16504 dw_die_ref type_die;
16506 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16507 CONST_DOUBLE rtx could represent either a large integer
16508 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16509 the value is always a floating point constant.
16511 When it is an integer, a CONST_DOUBLE is used whenever
16512 the constant requires 2 HWIs to be adequately represented.
16513 We output CONST_DOUBLEs as blocks. */
16514 if (mode == VOIDmode
16515 || (GET_MODE (rtl) == VOIDmode
16516 && maybe_ne (GET_MODE_BITSIZE (mode),
16517 HOST_BITS_PER_DOUBLE_INT)))
16518 break;
16519 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16520 if (type_die == NULL)
16521 return NULL;
16522 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16523 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16524 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16525 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16526 #if TARGET_SUPPORTS_WIDE_INT == 0
16527 if (!SCALAR_FLOAT_MODE_P (mode))
16529 mem_loc_result->dw_loc_oprnd2.val_class
16530 = dw_val_class_const_double;
16531 mem_loc_result->dw_loc_oprnd2.v.val_double
16532 = rtx_to_double_int (rtl);
16534 else
16535 #endif
16537 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16538 unsigned int length = GET_MODE_SIZE (float_mode);
16539 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16540 unsigned int elt_size = insert_float (rtl, array);
16542 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16543 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16544 = length / elt_size;
16545 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16546 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16549 break;
16551 case CONST_WIDE_INT:
16552 if (!dwarf_strict || dwarf_version >= 5)
16554 dw_die_ref type_die;
16556 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16557 if (type_die == NULL)
16558 return NULL;
16559 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16560 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16561 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16562 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16563 mem_loc_result->dw_loc_oprnd2.val_class
16564 = dw_val_class_wide_int;
16565 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16566 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16568 break;
16570 case CONST_POLY_INT:
16571 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16572 break;
16574 case EQ:
16575 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16576 break;
16578 case GE:
16579 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16580 break;
16582 case GT:
16583 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16584 break;
16586 case LE:
16587 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16588 break;
16590 case LT:
16591 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16592 break;
16594 case NE:
16595 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16596 break;
16598 case GEU:
16599 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16600 break;
16602 case GTU:
16603 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16604 break;
16606 case LEU:
16607 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16608 break;
16610 case LTU:
16611 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16612 break;
16614 case UMIN:
16615 case UMAX:
16616 if (!SCALAR_INT_MODE_P (mode))
16617 break;
16618 /* FALLTHRU */
16619 case SMIN:
16620 case SMAX:
16621 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16622 break;
16624 case ZERO_EXTRACT:
16625 case SIGN_EXTRACT:
16626 if (CONST_INT_P (XEXP (rtl, 1))
16627 && CONST_INT_P (XEXP (rtl, 2))
16628 && is_a <scalar_int_mode> (mode, &int_mode)
16629 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16630 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16631 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16632 && ((unsigned) INTVAL (XEXP (rtl, 1))
16633 + (unsigned) INTVAL (XEXP (rtl, 2))
16634 <= GET_MODE_BITSIZE (int_mode)))
16636 int shift, size;
16637 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16638 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16639 if (op0 == 0)
16640 break;
16641 if (GET_CODE (rtl) == SIGN_EXTRACT)
16642 op = DW_OP_shra;
16643 else
16644 op = DW_OP_shr;
16645 mem_loc_result = op0;
16646 size = INTVAL (XEXP (rtl, 1));
16647 shift = INTVAL (XEXP (rtl, 2));
16648 if (BITS_BIG_ENDIAN)
16649 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16650 if (shift + size != (int) DWARF2_ADDR_SIZE)
16652 add_loc_descr (&mem_loc_result,
16653 int_loc_descriptor (DWARF2_ADDR_SIZE
16654 - shift - size));
16655 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16657 if (size != (int) DWARF2_ADDR_SIZE)
16659 add_loc_descr (&mem_loc_result,
16660 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16661 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16664 break;
16666 case IF_THEN_ELSE:
16668 dw_loc_descr_ref op2, bra_node, drop_node;
16669 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16670 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16671 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16672 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16673 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16674 VAR_INIT_STATUS_INITIALIZED);
16675 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16676 VAR_INIT_STATUS_INITIALIZED);
16677 if (op0 == NULL || op1 == NULL || op2 == NULL)
16678 break;
16680 mem_loc_result = op1;
16681 add_loc_descr (&mem_loc_result, op2);
16682 add_loc_descr (&mem_loc_result, op0);
16683 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16684 add_loc_descr (&mem_loc_result, bra_node);
16685 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16686 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16687 add_loc_descr (&mem_loc_result, drop_node);
16688 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16689 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16691 break;
16693 case FLOAT_EXTEND:
16694 case FLOAT_TRUNCATE:
16695 case FLOAT:
16696 case UNSIGNED_FLOAT:
16697 case FIX:
16698 case UNSIGNED_FIX:
16699 if (!dwarf_strict || dwarf_version >= 5)
16701 dw_die_ref type_die;
16702 dw_loc_descr_ref cvt;
16704 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16705 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16706 if (op0 == NULL)
16707 break;
16708 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16709 && (GET_CODE (rtl) == FLOAT
16710 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16712 type_die = base_type_for_mode (int_mode,
16713 GET_CODE (rtl) == UNSIGNED_FLOAT);
16714 if (type_die == NULL)
16715 break;
16716 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16717 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16718 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16719 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16720 add_loc_descr (&op0, cvt);
16722 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16723 if (type_die == NULL)
16724 break;
16725 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16726 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16727 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16728 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16729 add_loc_descr (&op0, cvt);
16730 if (is_a <scalar_int_mode> (mode, &int_mode)
16731 && (GET_CODE (rtl) == FIX
16732 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16734 op0 = convert_descriptor_to_mode (int_mode, op0);
16735 if (op0 == NULL)
16736 break;
16738 mem_loc_result = op0;
16740 break;
16742 case CLZ:
16743 case CTZ:
16744 case FFS:
16745 if (is_a <scalar_int_mode> (mode, &int_mode))
16746 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16747 break;
16749 case POPCOUNT:
16750 case PARITY:
16751 if (is_a <scalar_int_mode> (mode, &int_mode))
16752 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16753 break;
16755 case BSWAP:
16756 if (is_a <scalar_int_mode> (mode, &int_mode))
16757 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16758 break;
16760 case ROTATE:
16761 case ROTATERT:
16762 if (is_a <scalar_int_mode> (mode, &int_mode))
16763 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16764 break;
16766 case COMPARE:
16767 /* In theory, we could implement the above. */
16768 /* DWARF cannot represent the unsigned compare operations
16769 natively. */
16770 case SS_MULT:
16771 case US_MULT:
16772 case SS_DIV:
16773 case US_DIV:
16774 case SS_PLUS:
16775 case US_PLUS:
16776 case SS_MINUS:
16777 case US_MINUS:
16778 case SS_NEG:
16779 case US_NEG:
16780 case SS_ABS:
16781 case SS_ASHIFT:
16782 case US_ASHIFT:
16783 case SS_TRUNCATE:
16784 case US_TRUNCATE:
16785 case UNORDERED:
16786 case ORDERED:
16787 case UNEQ:
16788 case UNGE:
16789 case UNGT:
16790 case UNLE:
16791 case UNLT:
16792 case LTGT:
16793 case FRACT_CONVERT:
16794 case UNSIGNED_FRACT_CONVERT:
16795 case SAT_FRACT:
16796 case UNSIGNED_SAT_FRACT:
16797 case SQRT:
16798 case ASM_OPERANDS:
16799 case VEC_MERGE:
16800 case VEC_SELECT:
16801 case VEC_CONCAT:
16802 case VEC_DUPLICATE:
16803 case VEC_SERIES:
16804 case HIGH:
16805 case FMA:
16806 case STRICT_LOW_PART:
16807 case CONST_VECTOR:
16808 case CONST_FIXED:
16809 case CLRSB:
16810 case CLOBBER:
16811 break;
16813 case CONST_STRING:
16814 resolve_one_addr (&rtl);
16815 goto symref;
16817 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16818 the expression. An UNSPEC rtx represents a raw DWARF operation,
16819 new_loc_descr is called for it to build the operation directly.
16820 Otherwise mem_loc_descriptor is called recursively. */
16821 case PARALLEL:
16823 int index = 0;
16824 dw_loc_descr_ref exp_result = NULL;
16826 for (; index < XVECLEN (rtl, 0); index++)
16828 rtx elem = XVECEXP (rtl, 0, index);
16829 if (GET_CODE (elem) == UNSPEC)
16831 /* Each DWARF operation UNSPEC contain two operands, if
16832 one operand is not used for the operation, const0_rtx is
16833 passed. */
16834 gcc_assert (XVECLEN (elem, 0) == 2);
16836 HOST_WIDE_INT dw_op = XINT (elem, 1);
16837 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16838 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16839 exp_result
16840 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16841 oprnd2);
16843 else
16844 exp_result
16845 = mem_loc_descriptor (elem, mode, mem_mode,
16846 VAR_INIT_STATUS_INITIALIZED);
16848 if (!mem_loc_result)
16849 mem_loc_result = exp_result;
16850 else
16851 add_loc_descr (&mem_loc_result, exp_result);
16854 break;
16857 default:
16858 if (flag_checking)
16860 print_rtl (stderr, rtl);
16861 gcc_unreachable ();
16863 break;
16866 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16867 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16869 return mem_loc_result;
16872 /* Return a descriptor that describes the concatenation of two locations.
16873 This is typically a complex variable. */
16875 static dw_loc_descr_ref
16876 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16878 /* At present we only track constant-sized pieces. */
16879 unsigned int size0, size1;
16880 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16881 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16882 return 0;
16884 dw_loc_descr_ref cc_loc_result = NULL;
16885 dw_loc_descr_ref x0_ref
16886 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16887 dw_loc_descr_ref x1_ref
16888 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16890 if (x0_ref == 0 || x1_ref == 0)
16891 return 0;
16893 cc_loc_result = x0_ref;
16894 add_loc_descr_op_piece (&cc_loc_result, size0);
16896 add_loc_descr (&cc_loc_result, x1_ref);
16897 add_loc_descr_op_piece (&cc_loc_result, size1);
16899 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16900 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16902 return cc_loc_result;
16905 /* Return a descriptor that describes the concatenation of N
16906 locations. */
16908 static dw_loc_descr_ref
16909 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16911 unsigned int i;
16912 dw_loc_descr_ref cc_loc_result = NULL;
16913 unsigned int n = XVECLEN (concatn, 0);
16914 unsigned int size;
16916 for (i = 0; i < n; ++i)
16918 dw_loc_descr_ref ref;
16919 rtx x = XVECEXP (concatn, 0, i);
16921 /* At present we only track constant-sized pieces. */
16922 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16923 return NULL;
16925 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16926 if (ref == NULL)
16927 return NULL;
16929 add_loc_descr (&cc_loc_result, ref);
16930 add_loc_descr_op_piece (&cc_loc_result, size);
16933 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16934 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16936 return cc_loc_result;
16939 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16940 for DEBUG_IMPLICIT_PTR RTL. */
16942 static dw_loc_descr_ref
16943 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16945 dw_loc_descr_ref ret;
16946 dw_die_ref ref;
16948 if (dwarf_strict && dwarf_version < 5)
16949 return NULL;
16950 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16951 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16952 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16953 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16954 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16955 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16956 if (ref)
16958 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16959 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16960 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16962 else
16964 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16965 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16967 return ret;
16970 /* Output a proper Dwarf location descriptor for a variable or parameter
16971 which is either allocated in a register or in a memory location. For a
16972 register, we just generate an OP_REG and the register number. For a
16973 memory location we provide a Dwarf postfix expression describing how to
16974 generate the (dynamic) address of the object onto the address stack.
16976 MODE is mode of the decl if this loc_descriptor is going to be used in
16977 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16978 allowed, VOIDmode otherwise.
16980 If we don't know how to describe it, return 0. */
16982 static dw_loc_descr_ref
16983 loc_descriptor (rtx rtl, machine_mode mode,
16984 enum var_init_status initialized)
16986 dw_loc_descr_ref loc_result = NULL;
16987 scalar_int_mode int_mode;
16989 switch (GET_CODE (rtl))
16991 case SUBREG:
16992 /* The case of a subreg may arise when we have a local (register)
16993 variable or a formal (register) parameter which doesn't quite fill
16994 up an entire register. For now, just assume that it is
16995 legitimate to make the Dwarf info refer to the whole register which
16996 contains the given subreg. */
16997 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16998 loc_result = loc_descriptor (SUBREG_REG (rtl),
16999 GET_MODE (SUBREG_REG (rtl)), initialized);
17000 else
17001 goto do_default;
17002 break;
17004 case REG:
17005 loc_result = reg_loc_descriptor (rtl, initialized);
17006 break;
17008 case MEM:
17009 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17010 GET_MODE (rtl), initialized);
17011 if (loc_result == NULL)
17012 loc_result = tls_mem_loc_descriptor (rtl);
17013 if (loc_result == NULL)
17015 rtx new_rtl = avoid_constant_pool_reference (rtl);
17016 if (new_rtl != rtl)
17017 loc_result = loc_descriptor (new_rtl, mode, initialized);
17019 break;
17021 case CONCAT:
17022 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17023 initialized);
17024 break;
17026 case CONCATN:
17027 loc_result = concatn_loc_descriptor (rtl, initialized);
17028 break;
17030 case VAR_LOCATION:
17031 /* Single part. */
17032 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17034 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17035 if (GET_CODE (loc) == EXPR_LIST)
17036 loc = XEXP (loc, 0);
17037 loc_result = loc_descriptor (loc, mode, initialized);
17038 break;
17041 rtl = XEXP (rtl, 1);
17042 /* FALLTHRU */
17044 case PARALLEL:
17046 rtvec par_elems = XVEC (rtl, 0);
17047 int num_elem = GET_NUM_ELEM (par_elems);
17048 machine_mode mode;
17049 int i, size;
17051 /* Create the first one, so we have something to add to. */
17052 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17053 VOIDmode, initialized);
17054 if (loc_result == NULL)
17055 return NULL;
17056 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17057 /* At present we only track constant-sized pieces. */
17058 if (!GET_MODE_SIZE (mode).is_constant (&size))
17059 return NULL;
17060 add_loc_descr_op_piece (&loc_result, size);
17061 for (i = 1; i < num_elem; i++)
17063 dw_loc_descr_ref temp;
17065 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17066 VOIDmode, initialized);
17067 if (temp == NULL)
17068 return NULL;
17069 add_loc_descr (&loc_result, temp);
17070 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17071 /* At present we only track constant-sized pieces. */
17072 if (!GET_MODE_SIZE (mode).is_constant (&size))
17073 return NULL;
17074 add_loc_descr_op_piece (&loc_result, size);
17077 break;
17079 case CONST_INT:
17080 if (mode != VOIDmode && mode != BLKmode)
17082 int_mode = as_a <scalar_int_mode> (mode);
17083 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17084 INTVAL (rtl));
17086 break;
17088 case CONST_DOUBLE:
17089 if (mode == VOIDmode)
17090 mode = GET_MODE (rtl);
17092 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17094 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17096 /* Note that a CONST_DOUBLE rtx could represent either an integer
17097 or a floating-point constant. A CONST_DOUBLE is used whenever
17098 the constant requires more than one word in order to be
17099 adequately represented. We output CONST_DOUBLEs as blocks. */
17100 scalar_mode smode = as_a <scalar_mode> (mode);
17101 loc_result = new_loc_descr (DW_OP_implicit_value,
17102 GET_MODE_SIZE (smode), 0);
17103 #if TARGET_SUPPORTS_WIDE_INT == 0
17104 if (!SCALAR_FLOAT_MODE_P (smode))
17106 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17107 loc_result->dw_loc_oprnd2.v.val_double
17108 = rtx_to_double_int (rtl);
17110 else
17111 #endif
17113 unsigned int length = GET_MODE_SIZE (smode);
17114 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17115 unsigned int elt_size = insert_float (rtl, array);
17117 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17118 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17119 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17120 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17123 break;
17125 case CONST_WIDE_INT:
17126 if (mode == VOIDmode)
17127 mode = GET_MODE (rtl);
17129 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17131 int_mode = as_a <scalar_int_mode> (mode);
17132 loc_result = new_loc_descr (DW_OP_implicit_value,
17133 GET_MODE_SIZE (int_mode), 0);
17134 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17135 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
17136 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
17138 break;
17140 case CONST_VECTOR:
17141 if (mode == VOIDmode)
17142 mode = GET_MODE (rtl);
17144 if (mode != VOIDmode
17145 /* The combination of a length and byte elt_size doesn't extend
17146 naturally to boolean vectors, where several elements are packed
17147 into the same byte. */
17148 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17149 && (dwarf_version >= 4 || !dwarf_strict))
17151 unsigned int length;
17152 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17153 return NULL;
17155 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17156 unsigned char *array
17157 = ggc_vec_alloc<unsigned char> (length * elt_size);
17158 unsigned int i;
17159 unsigned char *p;
17160 machine_mode imode = GET_MODE_INNER (mode);
17162 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17163 switch (GET_MODE_CLASS (mode))
17165 case MODE_VECTOR_INT:
17166 for (i = 0, p = array; i < length; i++, p += elt_size)
17168 rtx elt = CONST_VECTOR_ELT (rtl, i);
17169 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17171 break;
17173 case MODE_VECTOR_FLOAT:
17174 for (i = 0, p = array; i < length; i++, p += elt_size)
17176 rtx elt = CONST_VECTOR_ELT (rtl, i);
17177 insert_float (elt, p);
17179 break;
17181 default:
17182 gcc_unreachable ();
17185 loc_result = new_loc_descr (DW_OP_implicit_value,
17186 length * elt_size, 0);
17187 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17188 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17189 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17190 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17192 break;
17194 case CONST:
17195 if (mode == VOIDmode
17196 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17197 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17198 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17200 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17201 break;
17203 /* FALLTHROUGH */
17204 case SYMBOL_REF:
17205 if (!const_ok_for_output (rtl))
17206 break;
17207 /* FALLTHROUGH */
17208 case LABEL_REF:
17209 if (is_a <scalar_int_mode> (mode, &int_mode)
17210 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17211 && (dwarf_version >= 4 || !dwarf_strict))
17213 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17214 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17215 vec_safe_push (used_rtx_array, rtl);
17217 break;
17219 case DEBUG_IMPLICIT_PTR:
17220 loc_result = implicit_ptr_descriptor (rtl, 0);
17221 break;
17223 case PLUS:
17224 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17225 && CONST_INT_P (XEXP (rtl, 1)))
17227 loc_result
17228 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17229 break;
17231 /* FALLTHRU */
17232 do_default:
17233 default:
17234 if ((is_a <scalar_int_mode> (mode, &int_mode)
17235 && GET_MODE (rtl) == int_mode
17236 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17237 && dwarf_version >= 4)
17238 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17240 /* Value expression. */
17241 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17242 if (loc_result)
17243 add_loc_descr (&loc_result,
17244 new_loc_descr (DW_OP_stack_value, 0, 0));
17246 break;
17249 return loc_result;
17252 /* We need to figure out what section we should use as the base for the
17253 address ranges where a given location is valid.
17254 1. If this particular DECL has a section associated with it, use that.
17255 2. If this function has a section associated with it, use that.
17256 3. Otherwise, use the text section.
17257 XXX: If you split a variable across multiple sections, we won't notice. */
17259 static const char *
17260 secname_for_decl (const_tree decl)
17262 const char *secname;
17264 if (VAR_OR_FUNCTION_DECL_P (decl)
17265 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17266 && DECL_SECTION_NAME (decl))
17267 secname = DECL_SECTION_NAME (decl);
17268 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17270 if (in_cold_section_p)
17272 section *sec = current_function_section ();
17273 if (sec->common.flags & SECTION_NAMED)
17274 return sec->named.name;
17276 secname = DECL_SECTION_NAME (current_function_decl);
17278 else if (cfun && in_cold_section_p)
17279 secname = crtl->subsections.cold_section_label;
17280 else
17281 secname = text_section_label;
17283 return secname;
17286 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17288 static bool
17289 decl_by_reference_p (tree decl)
17291 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17292 || VAR_P (decl))
17293 && DECL_BY_REFERENCE (decl));
17296 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17297 for VARLOC. */
17299 static dw_loc_descr_ref
17300 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17301 enum var_init_status initialized)
17303 int have_address = 0;
17304 dw_loc_descr_ref descr;
17305 machine_mode mode;
17307 if (want_address != 2)
17309 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17310 /* Single part. */
17311 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17313 varloc = PAT_VAR_LOCATION_LOC (varloc);
17314 if (GET_CODE (varloc) == EXPR_LIST)
17315 varloc = XEXP (varloc, 0);
17316 mode = GET_MODE (varloc);
17317 if (MEM_P (varloc))
17319 rtx addr = XEXP (varloc, 0);
17320 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17321 mode, initialized);
17322 if (descr)
17323 have_address = 1;
17324 else
17326 rtx x = avoid_constant_pool_reference (varloc);
17327 if (x != varloc)
17328 descr = mem_loc_descriptor (x, mode, VOIDmode,
17329 initialized);
17332 else
17333 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17335 else
17336 return 0;
17338 else
17340 if (GET_CODE (varloc) == VAR_LOCATION)
17341 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17342 else
17343 mode = DECL_MODE (loc);
17344 descr = loc_descriptor (varloc, mode, initialized);
17345 have_address = 1;
17348 if (!descr)
17349 return 0;
17351 if (want_address == 2 && !have_address
17352 && (dwarf_version >= 4 || !dwarf_strict))
17354 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17356 expansion_failed (loc, NULL_RTX,
17357 "DWARF address size mismatch");
17358 return 0;
17360 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17361 have_address = 1;
17363 /* Show if we can't fill the request for an address. */
17364 if (want_address && !have_address)
17366 expansion_failed (loc, NULL_RTX,
17367 "Want address and only have value");
17368 return 0;
17371 /* If we've got an address and don't want one, dereference. */
17372 if (!want_address && have_address)
17374 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17375 enum dwarf_location_atom op;
17377 if (size > DWARF2_ADDR_SIZE || size == -1)
17379 expansion_failed (loc, NULL_RTX,
17380 "DWARF address size mismatch");
17381 return 0;
17383 else if (size == DWARF2_ADDR_SIZE)
17384 op = DW_OP_deref;
17385 else
17386 op = DW_OP_deref_size;
17388 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17391 return descr;
17394 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17395 if it is not possible. */
17397 static dw_loc_descr_ref
17398 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17400 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17401 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17402 else if (dwarf_version >= 3 || !dwarf_strict)
17403 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17404 else
17405 return NULL;
17408 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17409 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17411 static dw_loc_descr_ref
17412 dw_sra_loc_expr (tree decl, rtx loc)
17414 rtx p;
17415 unsigned HOST_WIDE_INT padsize = 0;
17416 dw_loc_descr_ref descr, *descr_tail;
17417 unsigned HOST_WIDE_INT decl_size;
17418 rtx varloc;
17419 enum var_init_status initialized;
17421 if (DECL_SIZE (decl) == NULL
17422 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17423 return NULL;
17425 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17426 descr = NULL;
17427 descr_tail = &descr;
17429 for (p = loc; p; p = XEXP (p, 1))
17431 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17432 rtx loc_note = *decl_piece_varloc_ptr (p);
17433 dw_loc_descr_ref cur_descr;
17434 dw_loc_descr_ref *tail, last = NULL;
17435 unsigned HOST_WIDE_INT opsize = 0;
17437 if (loc_note == NULL_RTX
17438 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17440 padsize += bitsize;
17441 continue;
17443 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17444 varloc = NOTE_VAR_LOCATION (loc_note);
17445 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17446 if (cur_descr == NULL)
17448 padsize += bitsize;
17449 continue;
17452 /* Check that cur_descr either doesn't use
17453 DW_OP_*piece operations, or their sum is equal
17454 to bitsize. Otherwise we can't embed it. */
17455 for (tail = &cur_descr; *tail != NULL;
17456 tail = &(*tail)->dw_loc_next)
17457 if ((*tail)->dw_loc_opc == DW_OP_piece)
17459 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17460 * BITS_PER_UNIT;
17461 last = *tail;
17463 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17465 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17466 last = *tail;
17469 if (last != NULL && opsize != bitsize)
17471 padsize += bitsize;
17472 /* Discard the current piece of the descriptor and release any
17473 addr_table entries it uses. */
17474 remove_loc_list_addr_table_entries (cur_descr);
17475 continue;
17478 /* If there is a hole, add DW_OP_*piece after empty DWARF
17479 expression, which means that those bits are optimized out. */
17480 if (padsize)
17482 if (padsize > decl_size)
17484 remove_loc_list_addr_table_entries (cur_descr);
17485 goto discard_descr;
17487 decl_size -= padsize;
17488 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17489 if (*descr_tail == NULL)
17491 remove_loc_list_addr_table_entries (cur_descr);
17492 goto discard_descr;
17494 descr_tail = &(*descr_tail)->dw_loc_next;
17495 padsize = 0;
17497 *descr_tail = cur_descr;
17498 descr_tail = tail;
17499 if (bitsize > decl_size)
17500 goto discard_descr;
17501 decl_size -= bitsize;
17502 if (last == NULL)
17504 HOST_WIDE_INT offset = 0;
17505 if (GET_CODE (varloc) == VAR_LOCATION
17506 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17508 varloc = PAT_VAR_LOCATION_LOC (varloc);
17509 if (GET_CODE (varloc) == EXPR_LIST)
17510 varloc = XEXP (varloc, 0);
17514 if (GET_CODE (varloc) == CONST
17515 || GET_CODE (varloc) == SIGN_EXTEND
17516 || GET_CODE (varloc) == ZERO_EXTEND)
17517 varloc = XEXP (varloc, 0);
17518 else if (GET_CODE (varloc) == SUBREG)
17519 varloc = SUBREG_REG (varloc);
17520 else
17521 break;
17523 while (1);
17524 /* DW_OP_bit_size offset should be zero for register
17525 or implicit location descriptions and empty location
17526 descriptions, but for memory addresses needs big endian
17527 adjustment. */
17528 if (MEM_P (varloc))
17530 unsigned HOST_WIDE_INT memsize;
17531 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17532 goto discard_descr;
17533 memsize *= BITS_PER_UNIT;
17534 if (memsize != bitsize)
17536 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17537 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17538 goto discard_descr;
17539 if (memsize < bitsize)
17540 goto discard_descr;
17541 if (BITS_BIG_ENDIAN)
17542 offset = memsize - bitsize;
17546 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17547 if (*descr_tail == NULL)
17548 goto discard_descr;
17549 descr_tail = &(*descr_tail)->dw_loc_next;
17553 /* If there were any non-empty expressions, add padding till the end of
17554 the decl. */
17555 if (descr != NULL && decl_size != 0)
17557 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17558 if (*descr_tail == NULL)
17559 goto discard_descr;
17561 return descr;
17563 discard_descr:
17564 /* Discard the descriptor and release any addr_table entries it uses. */
17565 remove_loc_list_addr_table_entries (descr);
17566 return NULL;
17569 /* Return the dwarf representation of the location list LOC_LIST of
17570 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17571 function. */
17573 static dw_loc_list_ref
17574 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17576 const char *endname, *secname;
17577 var_loc_view endview;
17578 rtx varloc;
17579 enum var_init_status initialized;
17580 struct var_loc_node *node;
17581 dw_loc_descr_ref descr;
17582 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17583 dw_loc_list_ref list = NULL;
17584 dw_loc_list_ref *listp = &list;
17586 /* Now that we know what section we are using for a base,
17587 actually construct the list of locations.
17588 The first location information is what is passed to the
17589 function that creates the location list, and the remaining
17590 locations just get added on to that list.
17591 Note that we only know the start address for a location
17592 (IE location changes), so to build the range, we use
17593 the range [current location start, next location start].
17594 This means we have to special case the last node, and generate
17595 a range of [last location start, end of function label]. */
17597 if (cfun && crtl->has_bb_partition)
17599 bool save_in_cold_section_p = in_cold_section_p;
17600 in_cold_section_p = first_function_block_is_cold;
17601 if (loc_list->last_before_switch == NULL)
17602 in_cold_section_p = !in_cold_section_p;
17603 secname = secname_for_decl (decl);
17604 in_cold_section_p = save_in_cold_section_p;
17606 else
17607 secname = secname_for_decl (decl);
17609 for (node = loc_list->first; node; node = node->next)
17611 bool range_across_switch = false;
17612 if (GET_CODE (node->loc) == EXPR_LIST
17613 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17615 if (GET_CODE (node->loc) == EXPR_LIST)
17617 descr = NULL;
17618 /* This requires DW_OP_{,bit_}piece, which is not usable
17619 inside DWARF expressions. */
17620 if (want_address == 2)
17621 descr = dw_sra_loc_expr (decl, node->loc);
17623 else
17625 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17626 varloc = NOTE_VAR_LOCATION (node->loc);
17627 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17629 if (descr)
17631 /* If section switch happens in between node->label
17632 and node->next->label (or end of function) and
17633 we can't emit it as a single entry list,
17634 emit two ranges, first one ending at the end
17635 of first partition and second one starting at the
17636 beginning of second partition. */
17637 if (node == loc_list->last_before_switch
17638 && (node != loc_list->first || loc_list->first->next
17639 /* If we are to emit a view number, we will emit
17640 a loclist rather than a single location
17641 expression for the entire function (see
17642 loc_list_has_views), so we have to split the
17643 range that straddles across partitions. */
17644 || !ZERO_VIEW_P (node->view))
17645 && current_function_decl)
17647 endname = cfun->fde->dw_fde_end;
17648 endview = 0;
17649 range_across_switch = true;
17651 /* The variable has a location between NODE->LABEL and
17652 NODE->NEXT->LABEL. */
17653 else if (node->next)
17654 endname = node->next->label, endview = node->next->view;
17655 /* If the variable has a location at the last label
17656 it keeps its location until the end of function. */
17657 else if (!current_function_decl)
17658 endname = text_end_label, endview = 0;
17659 else
17661 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17662 current_function_funcdef_no);
17663 endname = ggc_strdup (label_id);
17664 endview = 0;
17667 *listp = new_loc_list (descr, node->label, node->view,
17668 endname, endview, secname);
17669 if (TREE_CODE (decl) == PARM_DECL
17670 && node == loc_list->first
17671 && NOTE_P (node->loc)
17672 && strcmp (node->label, endname) == 0)
17673 (*listp)->force = true;
17674 listp = &(*listp)->dw_loc_next;
17678 if (cfun
17679 && crtl->has_bb_partition
17680 && node == loc_list->last_before_switch)
17682 bool save_in_cold_section_p = in_cold_section_p;
17683 in_cold_section_p = !first_function_block_is_cold;
17684 secname = secname_for_decl (decl);
17685 in_cold_section_p = save_in_cold_section_p;
17688 if (range_across_switch)
17690 if (GET_CODE (node->loc) == EXPR_LIST)
17691 descr = dw_sra_loc_expr (decl, node->loc);
17692 else
17694 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17695 varloc = NOTE_VAR_LOCATION (node->loc);
17696 descr = dw_loc_list_1 (decl, varloc, want_address,
17697 initialized);
17699 gcc_assert (descr);
17700 /* The variable has a location between NODE->LABEL and
17701 NODE->NEXT->LABEL. */
17702 if (node->next)
17703 endname = node->next->label, endview = node->next->view;
17704 else
17705 endname = cfun->fde->dw_fde_second_end, endview = 0;
17706 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17707 endname, endview, secname);
17708 listp = &(*listp)->dw_loc_next;
17712 /* Try to avoid the overhead of a location list emitting a location
17713 expression instead, but only if we didn't have more than one
17714 location entry in the first place. If some entries were not
17715 representable, we don't want to pretend a single entry that was
17716 applies to the entire scope in which the variable is
17717 available. */
17718 if (list && loc_list->first->next)
17719 gen_llsym (list);
17720 else
17721 maybe_gen_llsym (list);
17723 return list;
17726 /* Return if the loc_list has only single element and thus can be represented
17727 as location description. */
17729 static bool
17730 single_element_loc_list_p (dw_loc_list_ref list)
17732 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17733 return !list->ll_symbol;
17736 /* Duplicate a single element of location list. */
17738 static inline dw_loc_descr_ref
17739 copy_loc_descr (dw_loc_descr_ref ref)
17741 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17742 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17743 return copy;
17746 /* To each location in list LIST append loc descr REF. */
17748 static void
17749 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17751 dw_loc_descr_ref copy;
17752 add_loc_descr (&list->expr, ref);
17753 list = list->dw_loc_next;
17754 while (list)
17756 copy = copy_loc_descr (ref);
17757 add_loc_descr (&list->expr, copy);
17758 while (copy->dw_loc_next)
17759 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17760 list = list->dw_loc_next;
17764 /* To each location in list LIST prepend loc descr REF. */
17766 static void
17767 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17769 dw_loc_descr_ref copy;
17770 dw_loc_descr_ref ref_end = list->expr;
17771 add_loc_descr (&ref, list->expr);
17772 list->expr = ref;
17773 list = list->dw_loc_next;
17774 while (list)
17776 dw_loc_descr_ref end = list->expr;
17777 list->expr = copy = copy_loc_descr (ref);
17778 while (copy->dw_loc_next != ref_end)
17779 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17780 copy->dw_loc_next = end;
17781 list = list->dw_loc_next;
17785 /* Given two lists RET and LIST
17786 produce location list that is result of adding expression in LIST
17787 to expression in RET on each position in program.
17788 Might be destructive on both RET and LIST.
17790 TODO: We handle only simple cases of RET or LIST having at most one
17791 element. General case would involve sorting the lists in program order
17792 and merging them that will need some additional work.
17793 Adding that will improve quality of debug info especially for SRA-ed
17794 structures. */
17796 static void
17797 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17799 if (!list)
17800 return;
17801 if (!*ret)
17803 *ret = list;
17804 return;
17806 if (!list->dw_loc_next)
17808 add_loc_descr_to_each (*ret, list->expr);
17809 return;
17811 if (!(*ret)->dw_loc_next)
17813 prepend_loc_descr_to_each (list, (*ret)->expr);
17814 *ret = list;
17815 return;
17817 expansion_failed (NULL_TREE, NULL_RTX,
17818 "Don't know how to merge two non-trivial"
17819 " location lists.\n");
17820 *ret = NULL;
17821 return;
17824 /* LOC is constant expression. Try a luck, look it up in constant
17825 pool and return its loc_descr of its address. */
17827 static dw_loc_descr_ref
17828 cst_pool_loc_descr (tree loc)
17830 /* Get an RTL for this, if something has been emitted. */
17831 rtx rtl = lookup_constant_def (loc);
17833 if (!rtl || !MEM_P (rtl))
17835 gcc_assert (!rtl);
17836 return 0;
17838 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17840 /* TODO: We might get more coverage if we was actually delaying expansion
17841 of all expressions till end of compilation when constant pools are fully
17842 populated. */
17843 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17845 expansion_failed (loc, NULL_RTX,
17846 "CST value in contant pool but not marked.");
17847 return 0;
17849 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17850 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17853 /* Return dw_loc_list representing address of addr_expr LOC
17854 by looking for inner INDIRECT_REF expression and turning
17855 it into simple arithmetics.
17857 See loc_list_from_tree for the meaning of CONTEXT. */
17859 static dw_loc_list_ref
17860 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17861 loc_descr_context *context)
17863 tree obj, offset;
17864 poly_int64 bitsize, bitpos, bytepos;
17865 machine_mode mode;
17866 int unsignedp, reversep, volatilep = 0;
17867 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17869 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17870 &bitsize, &bitpos, &offset, &mode,
17871 &unsignedp, &reversep, &volatilep);
17872 STRIP_NOPS (obj);
17873 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17875 expansion_failed (loc, NULL_RTX, "bitfield access");
17876 return 0;
17878 if (!INDIRECT_REF_P (obj))
17880 expansion_failed (obj,
17881 NULL_RTX, "no indirect ref in inner refrence");
17882 return 0;
17884 if (!offset && known_eq (bitpos, 0))
17885 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17886 context);
17887 else if (toplev
17888 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17889 && (dwarf_version >= 4 || !dwarf_strict))
17891 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17892 if (!list_ret)
17893 return 0;
17894 if (offset)
17896 /* Variable offset. */
17897 list_ret1 = loc_list_from_tree (offset, 0, context);
17898 if (list_ret1 == 0)
17899 return 0;
17900 add_loc_list (&list_ret, list_ret1);
17901 if (!list_ret)
17902 return 0;
17903 add_loc_descr_to_each (list_ret,
17904 new_loc_descr (DW_OP_plus, 0, 0));
17906 HOST_WIDE_INT value;
17907 if (bytepos.is_constant (&value) && value > 0)
17908 add_loc_descr_to_each (list_ret,
17909 new_loc_descr (DW_OP_plus_uconst, value, 0));
17910 else if (maybe_ne (bytepos, 0))
17911 loc_list_plus_const (list_ret, bytepos);
17912 add_loc_descr_to_each (list_ret,
17913 new_loc_descr (DW_OP_stack_value, 0, 0));
17915 return list_ret;
17918 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17919 all operations from LOC are nops, move to the last one. Insert in NOPS all
17920 operations that are skipped. */
17922 static void
17923 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17924 hash_set<dw_loc_descr_ref> &nops)
17926 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17928 nops.add (loc);
17929 loc = loc->dw_loc_next;
17933 /* Helper for loc_descr_without_nops: free the location description operation
17934 P. */
17936 bool
17937 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17939 ggc_free (loc);
17940 return true;
17943 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17944 finishes LOC. */
17946 static void
17947 loc_descr_without_nops (dw_loc_descr_ref &loc)
17949 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17950 return;
17952 /* Set of all DW_OP_nop operations we remove. */
17953 hash_set<dw_loc_descr_ref> nops;
17955 /* First, strip all prefix NOP operations in order to keep the head of the
17956 operations list. */
17957 loc_descr_to_next_no_nop (loc, nops);
17959 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17961 /* For control flow operations: strip "prefix" nops in destination
17962 labels. */
17963 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17964 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17965 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17966 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17968 /* Do the same for the operations that follow, then move to the next
17969 iteration. */
17970 if (cur->dw_loc_next != NULL)
17971 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17972 cur = cur->dw_loc_next;
17975 nops.traverse<void *, free_loc_descr> (NULL);
17979 struct dwarf_procedure_info;
17981 /* Helper structure for location descriptions generation. */
17982 struct loc_descr_context
17984 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17985 NULL_TREE if DW_OP_push_object_address in invalid for this location
17986 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17987 tree context_type;
17988 /* The ..._DECL node that should be translated as a
17989 DW_OP_push_object_address operation. */
17990 tree base_decl;
17991 /* Information about the DWARF procedure we are currently generating. NULL if
17992 we are not generating a DWARF procedure. */
17993 struct dwarf_procedure_info *dpi;
17994 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17995 by consumer. Used for DW_TAG_generic_subrange attributes. */
17996 bool placeholder_arg;
17997 /* True if PLACEHOLDER_EXPR has been seen. */
17998 bool placeholder_seen;
17999 /* True if strict preservation of signedness has been requested. */
18000 bool strict_signedness;
18003 /* DWARF procedures generation
18005 DWARF expressions (aka. location descriptions) are used to encode variable
18006 things such as sizes or offsets. Such computations can have redundant parts
18007 that can be factorized in order to reduce the size of the output debug
18008 information. This is the whole point of DWARF procedures.
18010 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
18011 already factorized into functions ("size functions") in order to handle very
18012 big and complex types. Such functions are quite simple: they have integral
18013 arguments, they return an integral result and their body contains only a
18014 return statement with arithmetic expressions. This is the only kind of
18015 function we are interested in translating into DWARF procedures, here.
18017 DWARF expressions and DWARF procedure are executed using a stack, so we have
18018 to define some calling convention for them to interact. Let's say that:
18020 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18021 all arguments in reverse order (right-to-left) so that when the DWARF
18022 procedure execution starts, the first argument is the top of the stack.
18024 - Then, when returning, the DWARF procedure must have consumed all arguments
18025 on the stack, must have pushed the result and touched nothing else.
18027 - Each integral argument and the result are integral types can be hold in a
18028 single stack slot.
18030 - We call "frame offset" the number of stack slots that are "under DWARF
18031 procedure control": it includes the arguments slots, the temporaries and
18032 the result slot. Thus, it is equal to the number of arguments when the
18033 procedure execution starts and must be equal to one (the result) when it
18034 returns. */
18036 /* Helper structure used when generating operations for a DWARF procedure. */
18037 struct dwarf_procedure_info
18039 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18040 currently translated. */
18041 tree fndecl;
18042 /* The number of arguments FNDECL takes. */
18043 unsigned args_count;
18046 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18047 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18048 equate it to this DIE. */
18050 static dw_die_ref
18051 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18052 dw_die_ref parent_die)
18054 dw_die_ref dwarf_proc_die;
18056 if ((dwarf_version < 3 && dwarf_strict)
18057 || location == NULL)
18058 return NULL;
18060 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18061 if (fndecl)
18062 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18063 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18064 return dwarf_proc_die;
18067 /* Return whether TYPE is a supported type as a DWARF procedure argument
18068 type or return type (we handle only scalar types and pointer types that
18069 aren't wider than the DWARF expression evaluation stack). */
18071 static bool
18072 is_handled_procedure_type (tree type)
18074 return ((INTEGRAL_TYPE_P (type)
18075 || TREE_CODE (type) == OFFSET_TYPE
18076 || TREE_CODE (type) == POINTER_TYPE)
18077 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18080 /* Helper for resolve_args_picking: do the same but stop when coming across
18081 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18082 offset *before* evaluating the corresponding operation. */
18084 static bool
18085 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18086 struct dwarf_procedure_info *dpi,
18087 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18089 /* The "frame_offset" identifier is already used to name a macro... */
18090 unsigned frame_offset_ = initial_frame_offset;
18091 dw_loc_descr_ref l;
18093 for (l = loc; l != NULL;)
18095 bool existed;
18096 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18098 /* If we already met this node, there is nothing to compute anymore. */
18099 if (existed)
18101 /* Make sure that the stack size is consistent wherever the execution
18102 flow comes from. */
18103 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18104 break;
18106 l_frame_offset = frame_offset_;
18108 /* If needed, relocate the picking offset with respect to the frame
18109 offset. */
18110 if (l->frame_offset_rel)
18112 unsigned HOST_WIDE_INT off;
18113 switch (l->dw_loc_opc)
18115 case DW_OP_pick:
18116 off = l->dw_loc_oprnd1.v.val_unsigned;
18117 break;
18118 case DW_OP_dup:
18119 off = 0;
18120 break;
18121 case DW_OP_over:
18122 off = 1;
18123 break;
18124 default:
18125 gcc_unreachable ();
18127 /* frame_offset_ is the size of the current stack frame, including
18128 incoming arguments. Besides, the arguments are pushed
18129 right-to-left. Thus, in order to access the Nth argument from
18130 this operation node, the picking has to skip temporaries *plus*
18131 one stack slot per argument (0 for the first one, 1 for the second
18132 one, etc.).
18134 The targetted argument number (N) is already set as the operand,
18135 and the number of temporaries can be computed with:
18136 frame_offsets_ - dpi->args_count */
18137 off += frame_offset_ - dpi->args_count;
18139 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18140 if (off > 255)
18141 return false;
18143 if (off == 0)
18145 l->dw_loc_opc = DW_OP_dup;
18146 l->dw_loc_oprnd1.v.val_unsigned = 0;
18148 else if (off == 1)
18150 l->dw_loc_opc = DW_OP_over;
18151 l->dw_loc_oprnd1.v.val_unsigned = 0;
18153 else
18155 l->dw_loc_opc = DW_OP_pick;
18156 l->dw_loc_oprnd1.v.val_unsigned = off;
18160 /* Update frame_offset according to the effect the current operation has
18161 on the stack. */
18162 switch (l->dw_loc_opc)
18164 case DW_OP_deref:
18165 case DW_OP_swap:
18166 case DW_OP_rot:
18167 case DW_OP_abs:
18168 case DW_OP_neg:
18169 case DW_OP_not:
18170 case DW_OP_plus_uconst:
18171 case DW_OP_skip:
18172 case DW_OP_reg0:
18173 case DW_OP_reg1:
18174 case DW_OP_reg2:
18175 case DW_OP_reg3:
18176 case DW_OP_reg4:
18177 case DW_OP_reg5:
18178 case DW_OP_reg6:
18179 case DW_OP_reg7:
18180 case DW_OP_reg8:
18181 case DW_OP_reg9:
18182 case DW_OP_reg10:
18183 case DW_OP_reg11:
18184 case DW_OP_reg12:
18185 case DW_OP_reg13:
18186 case DW_OP_reg14:
18187 case DW_OP_reg15:
18188 case DW_OP_reg16:
18189 case DW_OP_reg17:
18190 case DW_OP_reg18:
18191 case DW_OP_reg19:
18192 case DW_OP_reg20:
18193 case DW_OP_reg21:
18194 case DW_OP_reg22:
18195 case DW_OP_reg23:
18196 case DW_OP_reg24:
18197 case DW_OP_reg25:
18198 case DW_OP_reg26:
18199 case DW_OP_reg27:
18200 case DW_OP_reg28:
18201 case DW_OP_reg29:
18202 case DW_OP_reg30:
18203 case DW_OP_reg31:
18204 case DW_OP_bregx:
18205 case DW_OP_piece:
18206 case DW_OP_deref_size:
18207 case DW_OP_nop:
18208 case DW_OP_bit_piece:
18209 case DW_OP_implicit_value:
18210 case DW_OP_stack_value:
18211 case DW_OP_deref_type:
18212 case DW_OP_convert:
18213 case DW_OP_reinterpret:
18214 case DW_OP_GNU_deref_type:
18215 case DW_OP_GNU_convert:
18216 case DW_OP_GNU_reinterpret:
18217 break;
18219 case DW_OP_addr:
18220 case DW_OP_const1u:
18221 case DW_OP_const1s:
18222 case DW_OP_const2u:
18223 case DW_OP_const2s:
18224 case DW_OP_const4u:
18225 case DW_OP_const4s:
18226 case DW_OP_const8u:
18227 case DW_OP_const8s:
18228 case DW_OP_constu:
18229 case DW_OP_consts:
18230 case DW_OP_dup:
18231 case DW_OP_over:
18232 case DW_OP_pick:
18233 case DW_OP_lit0:
18234 case DW_OP_lit1:
18235 case DW_OP_lit2:
18236 case DW_OP_lit3:
18237 case DW_OP_lit4:
18238 case DW_OP_lit5:
18239 case DW_OP_lit6:
18240 case DW_OP_lit7:
18241 case DW_OP_lit8:
18242 case DW_OP_lit9:
18243 case DW_OP_lit10:
18244 case DW_OP_lit11:
18245 case DW_OP_lit12:
18246 case DW_OP_lit13:
18247 case DW_OP_lit14:
18248 case DW_OP_lit15:
18249 case DW_OP_lit16:
18250 case DW_OP_lit17:
18251 case DW_OP_lit18:
18252 case DW_OP_lit19:
18253 case DW_OP_lit20:
18254 case DW_OP_lit21:
18255 case DW_OP_lit22:
18256 case DW_OP_lit23:
18257 case DW_OP_lit24:
18258 case DW_OP_lit25:
18259 case DW_OP_lit26:
18260 case DW_OP_lit27:
18261 case DW_OP_lit28:
18262 case DW_OP_lit29:
18263 case DW_OP_lit30:
18264 case DW_OP_lit31:
18265 case DW_OP_breg0:
18266 case DW_OP_breg1:
18267 case DW_OP_breg2:
18268 case DW_OP_breg3:
18269 case DW_OP_breg4:
18270 case DW_OP_breg5:
18271 case DW_OP_breg6:
18272 case DW_OP_breg7:
18273 case DW_OP_breg8:
18274 case DW_OP_breg9:
18275 case DW_OP_breg10:
18276 case DW_OP_breg11:
18277 case DW_OP_breg12:
18278 case DW_OP_breg13:
18279 case DW_OP_breg14:
18280 case DW_OP_breg15:
18281 case DW_OP_breg16:
18282 case DW_OP_breg17:
18283 case DW_OP_breg18:
18284 case DW_OP_breg19:
18285 case DW_OP_breg20:
18286 case DW_OP_breg21:
18287 case DW_OP_breg22:
18288 case DW_OP_breg23:
18289 case DW_OP_breg24:
18290 case DW_OP_breg25:
18291 case DW_OP_breg26:
18292 case DW_OP_breg27:
18293 case DW_OP_breg28:
18294 case DW_OP_breg29:
18295 case DW_OP_breg30:
18296 case DW_OP_breg31:
18297 case DW_OP_fbreg:
18298 case DW_OP_push_object_address:
18299 case DW_OP_call_frame_cfa:
18300 case DW_OP_GNU_variable_value:
18301 case DW_OP_GNU_addr_index:
18302 case DW_OP_GNU_const_index:
18303 ++frame_offset_;
18304 break;
18306 case DW_OP_drop:
18307 case DW_OP_xderef:
18308 case DW_OP_and:
18309 case DW_OP_div:
18310 case DW_OP_minus:
18311 case DW_OP_mod:
18312 case DW_OP_mul:
18313 case DW_OP_or:
18314 case DW_OP_plus:
18315 case DW_OP_shl:
18316 case DW_OP_shr:
18317 case DW_OP_shra:
18318 case DW_OP_xor:
18319 case DW_OP_bra:
18320 case DW_OP_eq:
18321 case DW_OP_ge:
18322 case DW_OP_gt:
18323 case DW_OP_le:
18324 case DW_OP_lt:
18325 case DW_OP_ne:
18326 case DW_OP_regx:
18327 case DW_OP_xderef_size:
18328 --frame_offset_;
18329 break;
18331 case DW_OP_call2:
18332 case DW_OP_call4:
18333 case DW_OP_call_ref:
18335 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18336 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18338 if (stack_usage == NULL)
18339 return false;
18340 frame_offset_ += *stack_usage;
18341 break;
18344 case DW_OP_implicit_pointer:
18345 case DW_OP_entry_value:
18346 case DW_OP_const_type:
18347 case DW_OP_regval_type:
18348 case DW_OP_form_tls_address:
18349 case DW_OP_GNU_push_tls_address:
18350 case DW_OP_GNU_uninit:
18351 case DW_OP_GNU_encoded_addr:
18352 case DW_OP_GNU_implicit_pointer:
18353 case DW_OP_GNU_entry_value:
18354 case DW_OP_GNU_const_type:
18355 case DW_OP_GNU_regval_type:
18356 case DW_OP_GNU_parameter_ref:
18357 /* loc_list_from_tree will probably not output these operations for
18358 size functions, so assume they will not appear here. */
18359 /* Fall through... */
18361 default:
18362 gcc_unreachable ();
18365 /* Now, follow the control flow (except subroutine calls). */
18366 switch (l->dw_loc_opc)
18368 case DW_OP_bra:
18369 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18370 frame_offsets))
18371 return false;
18372 /* Fall through. */
18374 case DW_OP_skip:
18375 l = l->dw_loc_oprnd1.v.val_loc;
18376 break;
18378 case DW_OP_stack_value:
18379 return true;
18381 default:
18382 l = l->dw_loc_next;
18383 break;
18387 return true;
18390 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18391 operations) in order to resolve the operand of DW_OP_pick operations that
18392 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18393 offset *before* LOC is executed. Return if all relocations were
18394 successful. */
18396 static bool
18397 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18398 struct dwarf_procedure_info *dpi)
18400 /* Associate to all visited operations the frame offset *before* evaluating
18401 this operation. */
18402 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18404 return
18405 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18408 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18409 Return NULL if it is not possible. */
18411 static dw_die_ref
18412 function_to_dwarf_procedure (tree fndecl)
18414 struct dwarf_procedure_info dpi;
18415 struct loc_descr_context ctx = {
18416 NULL_TREE, /* context_type */
18417 NULL_TREE, /* base_decl */
18418 &dpi, /* dpi */
18419 false, /* placeholder_arg */
18420 false, /* placeholder_seen */
18421 true /* strict_signedness */
18423 dw_die_ref dwarf_proc_die;
18424 tree tree_body = DECL_SAVED_TREE (fndecl);
18425 dw_loc_descr_ref loc_body, epilogue;
18427 tree cursor;
18428 unsigned i;
18430 /* Do not generate multiple DWARF procedures for the same function
18431 declaration. */
18432 dwarf_proc_die = lookup_decl_die (fndecl);
18433 if (dwarf_proc_die != NULL)
18434 return dwarf_proc_die;
18436 /* DWARF procedures are available starting with the DWARFv3 standard. */
18437 if (dwarf_version < 3 && dwarf_strict)
18438 return NULL;
18440 /* We handle only functions for which we still have a body, that return a
18441 supported type and that takes arguments with supported types. Note that
18442 there is no point translating functions that return nothing. */
18443 if (tree_body == NULL_TREE
18444 || DECL_RESULT (fndecl) == NULL_TREE
18445 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18446 return NULL;
18448 for (cursor = DECL_ARGUMENTS (fndecl);
18449 cursor != NULL_TREE;
18450 cursor = TREE_CHAIN (cursor))
18451 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18452 return NULL;
18454 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18455 if (TREE_CODE (tree_body) != RETURN_EXPR)
18456 return NULL;
18457 tree_body = TREE_OPERAND (tree_body, 0);
18458 if (TREE_CODE (tree_body) != MODIFY_EXPR
18459 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18460 return NULL;
18461 tree_body = TREE_OPERAND (tree_body, 1);
18463 /* Try to translate the body expression itself. Note that this will probably
18464 cause an infinite recursion if its call graph has a cycle. This is very
18465 unlikely for size functions, however, so don't bother with such things at
18466 the moment. */
18467 dpi.fndecl = fndecl;
18468 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18469 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18470 if (!loc_body)
18471 return NULL;
18473 /* After evaluating all operands in "loc_body", we should still have on the
18474 stack all arguments plus the desired function result (top of the stack).
18475 Generate code in order to keep only the result in our stack frame. */
18476 epilogue = NULL;
18477 for (i = 0; i < dpi.args_count; ++i)
18479 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18480 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18481 op_couple->dw_loc_next->dw_loc_next = epilogue;
18482 epilogue = op_couple;
18484 add_loc_descr (&loc_body, epilogue);
18485 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18486 return NULL;
18488 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18489 because they are considered useful. Now there is an epilogue, they are
18490 not anymore, so give it another try. */
18491 loc_descr_without_nops (loc_body);
18493 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18494 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18495 though, given that size functions do not come from source, so they should
18496 not have a dedicated DW_TAG_subprogram DIE. */
18497 dwarf_proc_die
18498 = new_dwarf_proc_die (loc_body, fndecl,
18499 get_context_die (DECL_CONTEXT (fndecl)));
18501 /* The called DWARF procedure consumes one stack slot per argument and
18502 returns one stack slot. */
18503 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18505 return dwarf_proc_die;
18508 /* Helper function for loc_list_from_tree. Perform OP binary op,
18509 but after converting arguments to type_die, afterwards convert
18510 back to unsigned. */
18512 static dw_loc_list_ref
18513 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18514 dw_die_ref type_die, scalar_int_mode mode,
18515 struct loc_descr_context *context)
18517 dw_loc_list_ref op0, op1;
18518 dw_loc_descr_ref cvt, binop;
18520 if (type_die == NULL)
18521 return NULL;
18523 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18524 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18525 if (op0 == NULL || op1 == NULL)
18526 return NULL;
18528 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18529 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18530 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18531 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18532 add_loc_descr_to_each (op0, cvt);
18534 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18535 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18536 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18537 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18538 add_loc_descr_to_each (op1, cvt);
18540 add_loc_list (&op0, op1);
18541 if (op0 == NULL)
18542 return NULL;
18544 binop = new_loc_descr (op, 0, 0);
18545 convert_descriptor_to_mode (mode, binop);
18546 add_loc_descr_to_each (op0, binop);
18548 return op0;
18551 /* Generate Dwarf location list representing LOC.
18552 If WANT_ADDRESS is false, expression computing LOC will be computed
18553 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18554 if WANT_ADDRESS is 2, expression computing address useable in location
18555 will be returned (i.e. DW_OP_reg can be used
18556 to refer to register values).
18558 CONTEXT provides information to customize the location descriptions
18559 generation. Its context_type field specifies what type is implicitly
18560 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18561 will not be generated.
18563 Its DPI field determines whether we are generating a DWARF expression for a
18564 DWARF procedure, so PARM_DECL references are processed specifically.
18566 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18567 and dpi fields were null. */
18569 static dw_loc_list_ref
18570 loc_list_from_tree_1 (tree loc, int want_address,
18571 struct loc_descr_context *context)
18573 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18574 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18575 int have_address = 0;
18576 enum dwarf_location_atom op;
18578 /* ??? Most of the time we do not take proper care for sign/zero
18579 extending the values properly. Hopefully this won't be a real
18580 problem... */
18582 if (context != NULL
18583 && context->base_decl == loc
18584 && want_address == 0)
18586 if (dwarf_version >= 3 || !dwarf_strict)
18587 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18588 NULL, 0, NULL, 0, NULL);
18589 else
18590 return NULL;
18593 switch (TREE_CODE (loc))
18595 case ERROR_MARK:
18596 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18597 return 0;
18599 case PLACEHOLDER_EXPR:
18600 /* This case involves extracting fields from an object to determine the
18601 position of other fields. It is supposed to appear only as the first
18602 operand of COMPONENT_REF nodes and to reference precisely the type
18603 that the context allows or its enclosing type. */
18604 if (context != NULL
18605 && (TREE_TYPE (loc) == context->context_type
18606 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18607 && want_address >= 1)
18609 if (dwarf_version >= 3 || !dwarf_strict)
18611 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18612 have_address = 1;
18613 break;
18615 else
18616 return NULL;
18618 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18619 the single argument passed by consumer. */
18620 else if (context != NULL
18621 && context->placeholder_arg
18622 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18623 && want_address == 0)
18625 ret = new_loc_descr (DW_OP_pick, 0, 0);
18626 ret->frame_offset_rel = 1;
18627 context->placeholder_seen = true;
18628 break;
18630 else
18631 expansion_failed (loc, NULL_RTX,
18632 "PLACEHOLDER_EXPR for an unexpected type");
18633 break;
18635 case CALL_EXPR:
18637 tree callee = get_callee_fndecl (loc);
18638 dw_die_ref dwarf_proc;
18640 if (callee
18641 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18642 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18644 /* DWARF procedures are used for size functions, which are built
18645 when size expressions contain conditional constructs, so we
18646 request strict preservation of signedness for comparisons. */
18647 bool old_strict_signedness;
18648 if (context)
18650 old_strict_signedness = context->strict_signedness;
18651 context->strict_signedness = true;
18654 /* Evaluate arguments right-to-left so that the first argument
18655 will be the top-most one on the stack. */
18656 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18658 tree arg = CALL_EXPR_ARG (loc, i);
18659 ret1 = loc_descriptor_from_tree (arg, 0, context);
18660 if (!ret1)
18662 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18663 return NULL;
18665 add_loc_descr (&ret, ret1);
18668 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18669 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18670 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18671 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18672 add_loc_descr (&ret, ret1);
18673 if (context)
18674 context->strict_signedness = old_strict_signedness;
18676 else
18677 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18678 break;
18681 case PREINCREMENT_EXPR:
18682 case PREDECREMENT_EXPR:
18683 case POSTINCREMENT_EXPR:
18684 case POSTDECREMENT_EXPR:
18685 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18686 /* There are no opcodes for these operations. */
18687 return 0;
18689 case ADDR_EXPR:
18690 /* If we already want an address, see if there is INDIRECT_REF inside
18691 e.g. for &this->field. */
18692 if (want_address)
18694 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18695 (loc, want_address == 2, context);
18696 if (list_ret)
18697 have_address = 1;
18698 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18699 && (ret = cst_pool_loc_descr (loc)))
18700 have_address = 1;
18702 /* Otherwise, process the argument and look for the address. */
18703 if (!list_ret && !ret)
18704 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18705 else
18707 if (want_address)
18708 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18709 return NULL;
18711 break;
18713 case VAR_DECL:
18714 if (DECL_THREAD_LOCAL_P (loc))
18716 rtx rtl;
18717 enum dwarf_location_atom tls_op;
18718 enum dtprel_bool dtprel = dtprel_false;
18720 if (targetm.have_tls)
18722 /* If this is not defined, we have no way to emit the
18723 data. */
18724 if (!targetm.asm_out.output_dwarf_dtprel)
18725 return 0;
18727 /* The way DW_OP_GNU_push_tls_address is specified, we
18728 can only look up addresses of objects in the current
18729 module. We used DW_OP_addr as first op, but that's
18730 wrong, because DW_OP_addr is relocated by the debug
18731 info consumer, while DW_OP_GNU_push_tls_address
18732 operand shouldn't be. */
18733 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18734 return 0;
18735 dtprel = dtprel_true;
18736 /* We check for DWARF 5 here because gdb did not implement
18737 DW_OP_form_tls_address until after 7.12. */
18738 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18739 : DW_OP_GNU_push_tls_address);
18741 else
18743 if (!targetm.emutls.debug_form_tls_address
18744 || !(dwarf_version >= 3 || !dwarf_strict))
18745 return 0;
18746 /* We stuffed the control variable into the DECL_VALUE_EXPR
18747 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18748 no longer appear in gimple code. We used the control
18749 variable in specific so that we could pick it up here. */
18750 loc = DECL_VALUE_EXPR (loc);
18751 tls_op = DW_OP_form_tls_address;
18754 rtl = rtl_for_decl_location (loc);
18755 if (rtl == NULL_RTX)
18756 return 0;
18758 if (!MEM_P (rtl))
18759 return 0;
18760 rtl = XEXP (rtl, 0);
18761 if (! CONSTANT_P (rtl))
18762 return 0;
18764 ret = new_addr_loc_descr (rtl, dtprel);
18765 ret1 = new_loc_descr (tls_op, 0, 0);
18766 add_loc_descr (&ret, ret1);
18768 have_address = 1;
18769 break;
18771 /* FALLTHRU */
18773 case PARM_DECL:
18774 if (context != NULL && context->dpi != NULL
18775 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18777 /* We are generating code for a DWARF procedure and we want to access
18778 one of its arguments: find the appropriate argument offset and let
18779 the resolve_args_picking pass compute the offset that complies
18780 with the stack frame size. */
18781 unsigned i = 0;
18782 tree cursor;
18784 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18785 cursor != NULL_TREE && cursor != loc;
18786 cursor = TREE_CHAIN (cursor), ++i)
18788 /* If we are translating a DWARF procedure, all referenced parameters
18789 must belong to the current function. */
18790 gcc_assert (cursor != NULL_TREE);
18792 ret = new_loc_descr (DW_OP_pick, i, 0);
18793 ret->frame_offset_rel = 1;
18794 break;
18796 /* FALLTHRU */
18798 case RESULT_DECL:
18799 if (DECL_HAS_VALUE_EXPR_P (loc))
18801 tree value_expr = DECL_VALUE_EXPR (loc);
18803 /* Non-local frame structures are DECL_IGNORED_P variables so we need
18804 to wait until they get an RTX in order to reference them. */
18805 if (early_dwarf
18806 && TREE_CODE (value_expr) == COMPONENT_REF
18807 && VAR_P (TREE_OPERAND (value_expr, 0))
18808 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
18810 else
18811 return loc_list_from_tree_1 (value_expr, want_address, context);
18814 /* FALLTHRU */
18816 case FUNCTION_DECL:
18818 rtx rtl;
18819 var_loc_list *loc_list = lookup_decl_loc (loc);
18821 if (loc_list && loc_list->first)
18823 list_ret = dw_loc_list (loc_list, loc, want_address);
18824 have_address = want_address != 0;
18825 break;
18827 rtl = rtl_for_decl_location (loc);
18828 if (rtl == NULL_RTX)
18830 if (TREE_CODE (loc) != FUNCTION_DECL
18831 && early_dwarf
18832 && want_address != 1
18833 && ! DECL_IGNORED_P (loc)
18834 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18835 || POINTER_TYPE_P (TREE_TYPE (loc)))
18836 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18837 <= DWARF2_ADDR_SIZE))
18839 dw_die_ref ref = lookup_decl_die (loc);
18840 if (ref)
18842 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18843 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18844 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18845 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18847 else if (current_function_decl
18848 && DECL_CONTEXT (loc) == current_function_decl)
18850 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18851 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18852 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18854 break;
18856 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18857 return 0;
18859 else if (CONST_INT_P (rtl))
18861 HOST_WIDE_INT val = INTVAL (rtl);
18862 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18863 val &= GET_MODE_MASK (DECL_MODE (loc));
18864 ret = int_loc_descriptor (val);
18866 else if (GET_CODE (rtl) == CONST_STRING)
18868 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18869 return 0;
18871 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18872 ret = new_addr_loc_descr (rtl, dtprel_false);
18873 else
18875 machine_mode mode, mem_mode;
18877 /* Certain constructs can only be represented at top-level. */
18878 if (want_address == 2)
18880 ret = loc_descriptor (rtl, VOIDmode,
18881 VAR_INIT_STATUS_INITIALIZED);
18882 have_address = 1;
18884 else
18886 mode = GET_MODE (rtl);
18887 mem_mode = VOIDmode;
18888 if (MEM_P (rtl))
18890 mem_mode = mode;
18891 mode = get_address_mode (rtl);
18892 rtl = XEXP (rtl, 0);
18893 have_address = 1;
18895 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18896 VAR_INIT_STATUS_INITIALIZED);
18898 if (!ret)
18899 expansion_failed (loc, rtl,
18900 "failed to produce loc descriptor for rtl");
18903 break;
18905 case MEM_REF:
18906 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18908 have_address = 1;
18909 goto do_plus;
18911 /* Fallthru. */
18912 case INDIRECT_REF:
18913 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18914 have_address = 1;
18915 break;
18917 case TARGET_MEM_REF:
18918 case SSA_NAME:
18919 case DEBUG_EXPR_DECL:
18920 return NULL;
18922 case COMPOUND_EXPR:
18923 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18924 context);
18926 CASE_CONVERT:
18927 case VIEW_CONVERT_EXPR:
18928 case SAVE_EXPR:
18929 case MODIFY_EXPR:
18930 case NON_LVALUE_EXPR:
18931 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18932 context);
18934 case COMPONENT_REF:
18935 case BIT_FIELD_REF:
18936 case ARRAY_REF:
18937 case ARRAY_RANGE_REF:
18938 case REALPART_EXPR:
18939 case IMAGPART_EXPR:
18941 tree obj, offset;
18942 poly_int64 bitsize, bitpos, bytepos;
18943 machine_mode mode;
18944 int unsignedp, reversep, volatilep = 0;
18946 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18947 &unsignedp, &reversep, &volatilep);
18949 gcc_assert (obj != loc);
18951 list_ret = loc_list_from_tree_1 (obj,
18952 want_address == 2
18953 && known_eq (bitpos, 0)
18954 && !offset ? 2 : 1,
18955 context);
18956 /* TODO: We can extract value of the small expression via shifting even
18957 for nonzero bitpos. */
18958 if (list_ret == 0)
18959 return 0;
18960 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18961 || !multiple_p (bitsize, BITS_PER_UNIT))
18963 expansion_failed (loc, NULL_RTX,
18964 "bitfield access");
18965 return 0;
18968 if (offset != NULL_TREE)
18970 /* Variable offset. */
18971 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18972 if (list_ret1 == 0)
18973 return 0;
18974 add_loc_list (&list_ret, list_ret1);
18975 if (!list_ret)
18976 return 0;
18977 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18980 HOST_WIDE_INT value;
18981 if (bytepos.is_constant (&value) && value > 0)
18982 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18983 value, 0));
18984 else if (maybe_ne (bytepos, 0))
18985 loc_list_plus_const (list_ret, bytepos);
18987 have_address = 1;
18988 break;
18991 case INTEGER_CST:
18992 if ((want_address || !tree_fits_shwi_p (loc))
18993 && (ret = cst_pool_loc_descr (loc)))
18994 have_address = 1;
18995 else if (want_address == 2
18996 && tree_fits_shwi_p (loc)
18997 && (ret = address_of_int_loc_descriptor
18998 (int_size_in_bytes (TREE_TYPE (loc)),
18999 tree_to_shwi (loc))))
19000 have_address = 1;
19001 else if (tree_fits_shwi_p (loc))
19002 ret = int_loc_descriptor (tree_to_shwi (loc));
19003 else if (tree_fits_uhwi_p (loc))
19004 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19005 else
19007 expansion_failed (loc, NULL_RTX,
19008 "Integer operand is not host integer");
19009 return 0;
19011 break;
19013 case POLY_INT_CST:
19015 if (want_address)
19017 expansion_failed (loc, NULL_RTX,
19018 "constant address with a runtime component");
19019 return 0;
19021 poly_int64 value;
19022 if (!poly_int_tree_p (loc, &value))
19024 expansion_failed (loc, NULL_RTX, "constant too big");
19025 return 0;
19027 ret = int_loc_descriptor (value);
19029 break;
19031 case CONSTRUCTOR:
19032 case REAL_CST:
19033 case STRING_CST:
19034 case COMPLEX_CST:
19035 if ((ret = cst_pool_loc_descr (loc)))
19036 have_address = 1;
19037 else if (TREE_CODE (loc) == CONSTRUCTOR)
19039 tree type = TREE_TYPE (loc);
19040 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19041 unsigned HOST_WIDE_INT offset = 0;
19042 unsigned HOST_WIDE_INT cnt;
19043 constructor_elt *ce;
19045 if (TREE_CODE (type) == RECORD_TYPE)
19047 /* This is very limited, but it's enough to output
19048 pointers to member functions, as long as the
19049 referenced function is defined in the current
19050 translation unit. */
19051 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19053 tree val = ce->value;
19055 tree field = ce->index;
19057 if (val)
19058 STRIP_NOPS (val);
19060 if (!field || DECL_BIT_FIELD (field))
19062 expansion_failed (loc, NULL_RTX,
19063 "bitfield in record type constructor");
19064 size = offset = (unsigned HOST_WIDE_INT)-1;
19065 ret = NULL;
19066 break;
19069 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19070 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19071 gcc_assert (pos + fieldsize <= size);
19072 if (pos < offset)
19074 expansion_failed (loc, NULL_RTX,
19075 "out-of-order fields in record constructor");
19076 size = offset = (unsigned HOST_WIDE_INT)-1;
19077 ret = NULL;
19078 break;
19080 if (pos > offset)
19082 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19083 add_loc_descr (&ret, ret1);
19084 offset = pos;
19086 if (val && fieldsize != 0)
19088 ret1 = loc_descriptor_from_tree (val, want_address, context);
19089 if (!ret1)
19091 expansion_failed (loc, NULL_RTX,
19092 "unsupported expression in field");
19093 size = offset = (unsigned HOST_WIDE_INT)-1;
19094 ret = NULL;
19095 break;
19097 add_loc_descr (&ret, ret1);
19099 if (fieldsize)
19101 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19102 add_loc_descr (&ret, ret1);
19103 offset = pos + fieldsize;
19107 if (offset != size)
19109 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19110 add_loc_descr (&ret, ret1);
19111 offset = size;
19114 have_address = !!want_address;
19116 else
19117 expansion_failed (loc, NULL_RTX,
19118 "constructor of non-record type");
19120 else
19121 /* We can construct small constants here using int_loc_descriptor. */
19122 expansion_failed (loc, NULL_RTX,
19123 "constructor or constant not in constant pool");
19124 break;
19126 case TRUTH_AND_EXPR:
19127 case TRUTH_ANDIF_EXPR:
19128 case BIT_AND_EXPR:
19129 op = DW_OP_and;
19130 goto do_binop;
19132 case TRUTH_XOR_EXPR:
19133 case BIT_XOR_EXPR:
19134 op = DW_OP_xor;
19135 goto do_binop;
19137 case TRUTH_OR_EXPR:
19138 case TRUTH_ORIF_EXPR:
19139 case BIT_IOR_EXPR:
19140 op = DW_OP_or;
19141 goto do_binop;
19143 case EXACT_DIV_EXPR:
19144 case FLOOR_DIV_EXPR:
19145 case TRUNC_DIV_EXPR:
19146 /* Turn a divide by a power of 2 into a shift when possible. */
19147 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19148 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19150 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19151 if (log2 > 0)
19153 list_ret
19154 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19155 if (list_ret == 0)
19156 return 0;
19158 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19159 add_loc_descr_to_each (list_ret,
19160 new_loc_descr (DW_OP_shr, 0, 0));
19161 break;
19165 /* fall through */
19167 case CEIL_DIV_EXPR:
19168 case ROUND_DIV_EXPR:
19169 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19171 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19172 scalar_int_mode int_mode;
19174 if ((dwarf_strict && dwarf_version < 5)
19175 || !is_a <scalar_int_mode> (mode, &int_mode))
19176 return 0;
19178 /* We can use a signed divide if the sign bit is not set. */
19179 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19181 op = DW_OP_div;
19182 goto do_binop;
19185 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19186 base_type_for_mode (int_mode, 1),
19187 int_mode, context);
19188 break;
19190 op = DW_OP_div;
19191 goto do_binop;
19193 case MINUS_EXPR:
19194 op = DW_OP_minus;
19195 goto do_binop;
19197 case FLOOR_MOD_EXPR:
19198 case CEIL_MOD_EXPR:
19199 case ROUND_MOD_EXPR:
19200 case TRUNC_MOD_EXPR:
19201 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19203 op = DW_OP_mod;
19204 goto do_binop;
19206 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19207 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19208 if (list_ret == 0 || list_ret1 == 0)
19209 return 0;
19211 add_loc_list (&list_ret, list_ret1);
19212 if (list_ret == 0)
19213 return 0;
19214 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19215 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19216 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19217 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19218 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19219 break;
19221 case MULT_EXPR:
19222 op = DW_OP_mul;
19223 goto do_binop;
19225 case LSHIFT_EXPR:
19226 op = DW_OP_shl;
19227 goto do_binop;
19229 case RSHIFT_EXPR:
19230 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19231 goto do_binop;
19233 case POINTER_PLUS_EXPR:
19234 case PLUS_EXPR:
19235 do_plus:
19236 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19238 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19239 smarter to encode their opposite. The DW_OP_plus_uconst operation
19240 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19241 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19242 bytes, Y being the size of the operation that pushes the opposite
19243 of the addend. So let's choose the smallest representation. */
19244 const tree tree_addend = TREE_OPERAND (loc, 1);
19245 offset_int wi_addend;
19246 HOST_WIDE_INT shwi_addend;
19247 dw_loc_descr_ref loc_naddend;
19249 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19250 if (list_ret == 0)
19251 return 0;
19253 /* Try to get the literal to push. It is the opposite of the addend,
19254 so as we rely on wrapping during DWARF evaluation, first decode
19255 the literal as a "DWARF-sized" signed number. */
19256 wi_addend = wi::to_offset (tree_addend);
19257 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19258 shwi_addend = wi_addend.to_shwi ();
19259 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19260 ? int_loc_descriptor (-shwi_addend)
19261 : NULL;
19263 if (loc_naddend != NULL
19264 && ((unsigned) size_of_uleb128 (shwi_addend)
19265 > size_of_loc_descr (loc_naddend)))
19267 add_loc_descr_to_each (list_ret, loc_naddend);
19268 add_loc_descr_to_each (list_ret,
19269 new_loc_descr (DW_OP_minus, 0, 0));
19271 else
19273 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19275 loc_naddend = loc_cur;
19276 loc_cur = loc_cur->dw_loc_next;
19277 ggc_free (loc_naddend);
19279 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19281 break;
19284 op = DW_OP_plus;
19285 goto do_binop;
19287 case LE_EXPR:
19288 op = DW_OP_le;
19289 goto do_comp_binop;
19291 case GE_EXPR:
19292 op = DW_OP_ge;
19293 goto do_comp_binop;
19295 case LT_EXPR:
19296 op = DW_OP_lt;
19297 goto do_comp_binop;
19299 case GT_EXPR:
19300 op = DW_OP_gt;
19301 goto do_comp_binop;
19303 do_comp_binop:
19304 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19306 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19307 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19308 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19309 TREE_CODE (loc));
19310 break;
19312 else
19313 goto do_binop;
19315 case EQ_EXPR:
19316 op = DW_OP_eq;
19317 goto do_binop;
19319 case NE_EXPR:
19320 op = DW_OP_ne;
19321 goto do_binop;
19323 do_binop:
19324 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19325 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19326 if (list_ret == 0 || list_ret1 == 0)
19327 return 0;
19329 add_loc_list (&list_ret, list_ret1);
19330 if (list_ret == 0)
19331 return 0;
19332 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19333 break;
19335 case TRUTH_NOT_EXPR:
19336 case BIT_NOT_EXPR:
19337 op = DW_OP_not;
19338 goto do_unop;
19340 case ABS_EXPR:
19341 op = DW_OP_abs;
19342 goto do_unop;
19344 case NEGATE_EXPR:
19345 op = DW_OP_neg;
19346 goto do_unop;
19348 do_unop:
19349 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19350 if (list_ret == 0)
19351 return 0;
19353 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19354 break;
19356 case MIN_EXPR:
19357 case MAX_EXPR:
19359 const enum tree_code code =
19360 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19362 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19363 build2 (code, integer_type_node,
19364 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19365 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19368 /* fall through */
19370 case COND_EXPR:
19372 dw_loc_descr_ref lhs
19373 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19374 dw_loc_list_ref rhs
19375 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19376 dw_loc_descr_ref bra_node, jump_node, tmp;
19378 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19379 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19380 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19381 list_ret
19382 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19383 0, context);
19384 else
19385 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19386 if (list_ret == 0 || lhs == 0 || rhs == 0)
19387 return 0;
19389 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19390 add_loc_descr_to_each (list_ret, bra_node);
19392 add_loc_list (&list_ret, rhs);
19393 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19394 add_loc_descr_to_each (list_ret, jump_node);
19396 add_loc_descr_to_each (list_ret, lhs);
19397 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19398 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19400 /* ??? Need a node to point the skip at. Use a nop. */
19401 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19402 add_loc_descr_to_each (list_ret, tmp);
19403 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19404 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19406 break;
19408 case FIX_TRUNC_EXPR:
19409 return 0;
19411 default:
19412 /* Leave front-end specific codes as simply unknown. This comes
19413 up, for instance, with the C STMT_EXPR. */
19414 if ((unsigned int) TREE_CODE (loc)
19415 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19417 expansion_failed (loc, NULL_RTX,
19418 "language specific tree node");
19419 return 0;
19422 /* Otherwise this is a generic code; we should just lists all of
19423 these explicitly. We forgot one. */
19424 if (flag_checking)
19425 gcc_unreachable ();
19427 /* In a release build, we want to degrade gracefully: better to
19428 generate incomplete debugging information than to crash. */
19429 return NULL;
19432 if (!ret && !list_ret)
19433 return 0;
19435 if (want_address == 2 && !have_address
19436 && (dwarf_version >= 4 || !dwarf_strict))
19438 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19440 expansion_failed (loc, NULL_RTX,
19441 "DWARF address size mismatch");
19442 return 0;
19444 if (ret)
19445 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19446 else
19447 add_loc_descr_to_each (list_ret,
19448 new_loc_descr (DW_OP_stack_value, 0, 0));
19449 have_address = 1;
19451 /* Show if we can't fill the request for an address. */
19452 if (want_address && !have_address)
19454 expansion_failed (loc, NULL_RTX,
19455 "Want address and only have value");
19456 return 0;
19459 gcc_assert (!ret || !list_ret);
19461 /* If we've got an address and don't want one, dereference. */
19462 if (!want_address && have_address)
19464 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19465 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19466 scalar_int_mode int_mode;
19467 dw_die_ref type_die;
19468 dw_loc_descr_ref deref;
19470 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19471 if (size > DWARF2_ADDR_SIZE || size == -1)
19473 expansion_failed (loc, NULL_RTX,
19474 "DWARF address size mismatch");
19475 return 0;
19478 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19479 else if (size == DWARF2_ADDR_SIZE)
19480 deref = new_loc_descr (DW_OP_deref, size, 0);
19482 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19483 extend the value, which is really OK for unsigned types only. */
19484 else if (!(context && context->strict_signedness)
19485 || TYPE_UNSIGNED (TREE_TYPE (loc))
19486 || (dwarf_strict && dwarf_version < 5)
19487 || !is_a <scalar_int_mode> (mode, &int_mode)
19488 || !(type_die = base_type_for_mode (mode, false)))
19489 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19491 /* Use DW_OP_deref_type for signed integral types if possible, but
19492 convert back to the generic type to avoid type mismatches later. */
19493 else
19495 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19496 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19497 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19498 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19499 add_loc_descr (&deref,
19500 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19503 if (ret)
19504 add_loc_descr (&ret, deref);
19505 else
19506 add_loc_descr_to_each (list_ret, deref);
19509 if (ret)
19510 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19512 return list_ret;
19515 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19516 expressions. */
19518 static dw_loc_list_ref
19519 loc_list_from_tree (tree loc, int want_address,
19520 struct loc_descr_context *context)
19522 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19524 for (dw_loc_list_ref loc_cur = result;
19525 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19526 loc_descr_without_nops (loc_cur->expr);
19527 return result;
19530 /* Same as above but return only single location expression. */
19531 static dw_loc_descr_ref
19532 loc_descriptor_from_tree (tree loc, int want_address,
19533 struct loc_descr_context *context)
19535 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19536 if (!ret)
19537 return NULL;
19538 if (ret->dw_loc_next)
19540 expansion_failed (loc, NULL_RTX,
19541 "Location list where only loc descriptor needed");
19542 return NULL;
19544 return ret->expr;
19547 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19548 pointer to the declared type for the relevant field variable, or return
19549 `integer_type_node' if the given node turns out to be an
19550 ERROR_MARK node. */
19552 static inline tree
19553 field_type (const_tree decl)
19555 tree type;
19557 if (TREE_CODE (decl) == ERROR_MARK)
19558 return integer_type_node;
19560 type = DECL_BIT_FIELD_TYPE (decl);
19561 if (type == NULL_TREE)
19562 type = TREE_TYPE (decl);
19564 return type;
19567 /* Given a pointer to a tree node, return the alignment in bits for
19568 it, or else return BITS_PER_WORD if the node actually turns out to
19569 be an ERROR_MARK node. */
19571 static inline unsigned
19572 simple_type_align_in_bits (const_tree type)
19574 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19577 static inline unsigned
19578 simple_decl_align_in_bits (const_tree decl)
19580 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19583 /* Return the result of rounding T up to ALIGN. */
19585 static inline offset_int
19586 round_up_to_align (const offset_int &t, unsigned int align)
19588 return wi::udiv_trunc (t + align - 1, align) * align;
19591 /* Helper structure for RECORD_TYPE processing. */
19592 struct vlr_context
19594 /* Root RECORD_TYPE. It is needed to generate data member location
19595 descriptions in variable-length records (VLR), but also to cope with
19596 variants, which are composed of nested structures multiplexed with
19597 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19598 function processing a FIELD_DECL, it is required to be non null. */
19599 tree struct_type;
19601 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19602 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19603 this variant part as part of the root record (in storage units). For
19604 regular records, it must be NULL_TREE. */
19605 tree variant_part_offset;
19608 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19609 addressed byte of the "containing object" for the given FIELD_DECL. If
19610 possible, return a native constant through CST_OFFSET (in which case NULL is
19611 returned); otherwise return a DWARF expression that computes the offset.
19613 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19614 that offset is, either because the argument turns out to be a pointer to an
19615 ERROR_MARK node, or because the offset expression is too complex for us.
19617 CTX is required: see the comment for VLR_CONTEXT. */
19619 static dw_loc_descr_ref
19620 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19621 HOST_WIDE_INT *cst_offset)
19623 tree tree_result;
19624 dw_loc_list_ref loc_result;
19626 *cst_offset = 0;
19628 if (TREE_CODE (decl) == ERROR_MARK)
19629 return NULL;
19630 else
19631 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19633 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19634 case. */
19635 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19636 return NULL;
19638 /* We used to handle only constant offsets in all cases. Now, we handle
19639 properly dynamic byte offsets only when PCC bitfield type doesn't
19640 matter. */
19641 if (PCC_BITFIELD_TYPE_MATTERS
19642 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19644 offset_int object_offset_in_bits;
19645 offset_int object_offset_in_bytes;
19646 offset_int bitpos_int;
19647 tree type;
19648 tree field_size_tree;
19649 offset_int deepest_bitpos;
19650 offset_int field_size_in_bits;
19651 unsigned int type_align_in_bits;
19652 unsigned int decl_align_in_bits;
19653 offset_int type_size_in_bits;
19655 bitpos_int = wi::to_offset (bit_position (decl));
19656 type = field_type (decl);
19657 type_size_in_bits = offset_int_type_size_in_bits (type);
19658 type_align_in_bits = simple_type_align_in_bits (type);
19660 field_size_tree = DECL_SIZE (decl);
19662 /* The size could be unspecified if there was an error, or for
19663 a flexible array member. */
19664 if (!field_size_tree)
19665 field_size_tree = bitsize_zero_node;
19667 /* If the size of the field is not constant, use the type size. */
19668 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19669 field_size_in_bits = wi::to_offset (field_size_tree);
19670 else
19671 field_size_in_bits = type_size_in_bits;
19673 decl_align_in_bits = simple_decl_align_in_bits (decl);
19675 /* The GCC front-end doesn't make any attempt to keep track of the
19676 starting bit offset (relative to the start of the containing
19677 structure type) of the hypothetical "containing object" for a
19678 bit-field. Thus, when computing the byte offset value for the
19679 start of the "containing object" of a bit-field, we must deduce
19680 this information on our own. This can be rather tricky to do in
19681 some cases. For example, handling the following structure type
19682 definition when compiling for an i386/i486 target (which only
19683 aligns long long's to 32-bit boundaries) can be very tricky:
19685 struct S { int field1; long long field2:31; };
19687 Fortunately, there is a simple rule-of-thumb which can be used
19688 in such cases. When compiling for an i386/i486, GCC will
19689 allocate 8 bytes for the structure shown above. It decides to
19690 do this based upon one simple rule for bit-field allocation.
19691 GCC allocates each "containing object" for each bit-field at
19692 the first (i.e. lowest addressed) legitimate alignment boundary
19693 (based upon the required minimum alignment for the declared
19694 type of the field) which it can possibly use, subject to the
19695 condition that there is still enough available space remaining
19696 in the containing object (when allocated at the selected point)
19697 to fully accommodate all of the bits of the bit-field itself.
19699 This simple rule makes it obvious why GCC allocates 8 bytes for
19700 each object of the structure type shown above. When looking
19701 for a place to allocate the "containing object" for `field2',
19702 the compiler simply tries to allocate a 64-bit "containing
19703 object" at each successive 32-bit boundary (starting at zero)
19704 until it finds a place to allocate that 64- bit field such that
19705 at least 31 contiguous (and previously unallocated) bits remain
19706 within that selected 64 bit field. (As it turns out, for the
19707 example above, the compiler finds it is OK to allocate the
19708 "containing object" 64-bit field at bit-offset zero within the
19709 structure type.)
19711 Here we attempt to work backwards from the limited set of facts
19712 we're given, and we try to deduce from those facts, where GCC
19713 must have believed that the containing object started (within
19714 the structure type). The value we deduce is then used (by the
19715 callers of this routine) to generate DW_AT_location and
19716 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19717 the case of DW_AT_location, regular fields as well). */
19719 /* Figure out the bit-distance from the start of the structure to
19720 the "deepest" bit of the bit-field. */
19721 deepest_bitpos = bitpos_int + field_size_in_bits;
19723 /* This is the tricky part. Use some fancy footwork to deduce
19724 where the lowest addressed bit of the containing object must
19725 be. */
19726 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19728 /* Round up to type_align by default. This works best for
19729 bitfields. */
19730 object_offset_in_bits
19731 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19733 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19735 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19737 /* Round up to decl_align instead. */
19738 object_offset_in_bits
19739 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19742 object_offset_in_bytes
19743 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19744 if (ctx->variant_part_offset == NULL_TREE)
19746 *cst_offset = object_offset_in_bytes.to_shwi ();
19747 return NULL;
19749 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19751 else
19752 tree_result = byte_position (decl);
19754 if (ctx->variant_part_offset != NULL_TREE)
19755 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19756 ctx->variant_part_offset, tree_result);
19758 /* If the byte offset is a constant, it's simplier to handle a native
19759 constant rather than a DWARF expression. */
19760 if (TREE_CODE (tree_result) == INTEGER_CST)
19762 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19763 return NULL;
19766 struct loc_descr_context loc_ctx = {
19767 ctx->struct_type, /* context_type */
19768 NULL_TREE, /* base_decl */
19769 NULL, /* dpi */
19770 false, /* placeholder_arg */
19771 false, /* placeholder_seen */
19772 false /* strict_signedness */
19774 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19776 /* We want a DWARF expression: abort if we only have a location list with
19777 multiple elements. */
19778 if (!loc_result || !single_element_loc_list_p (loc_result))
19779 return NULL;
19780 else
19781 return loc_result->expr;
19784 /* The following routines define various Dwarf attributes and any data
19785 associated with them. */
19787 /* Add a location description attribute value to a DIE.
19789 This emits location attributes suitable for whole variables and
19790 whole parameters. Note that the location attributes for struct fields are
19791 generated by the routine `data_member_location_attribute' below. */
19793 static inline void
19794 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19795 dw_loc_list_ref descr)
19797 bool check_no_locviews = true;
19798 if (descr == 0)
19799 return;
19800 if (single_element_loc_list_p (descr))
19801 add_AT_loc (die, attr_kind, descr->expr);
19802 else
19804 add_AT_loc_list (die, attr_kind, descr);
19805 gcc_assert (descr->ll_symbol);
19806 if (attr_kind == DW_AT_location && descr->vl_symbol
19807 && dwarf2out_locviews_in_attribute ())
19809 add_AT_view_list (die, DW_AT_GNU_locviews);
19810 check_no_locviews = false;
19814 if (check_no_locviews)
19815 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19818 /* Add DW_AT_accessibility attribute to DIE if needed. */
19820 static void
19821 add_accessibility_attribute (dw_die_ref die, tree decl)
19823 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19824 children, otherwise the default is DW_ACCESS_public. In DWARF2
19825 the default has always been DW_ACCESS_public. */
19826 if (TREE_PROTECTED (decl))
19827 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19828 else if (TREE_PRIVATE (decl))
19830 if (dwarf_version == 2
19831 || die->die_parent == NULL
19832 || die->die_parent->die_tag != DW_TAG_class_type)
19833 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19835 else if (dwarf_version > 2
19836 && die->die_parent
19837 && die->die_parent->die_tag == DW_TAG_class_type)
19838 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19841 /* Attach the specialized form of location attribute used for data members of
19842 struct and union types. In the special case of a FIELD_DECL node which
19843 represents a bit-field, the "offset" part of this special location
19844 descriptor must indicate the distance in bytes from the lowest-addressed
19845 byte of the containing struct or union type to the lowest-addressed byte of
19846 the "containing object" for the bit-field. (See the `field_byte_offset'
19847 function above).
19849 For any given bit-field, the "containing object" is a hypothetical object
19850 (of some integral or enum type) within which the given bit-field lives. The
19851 type of this hypothetical "containing object" is always the same as the
19852 declared type of the individual bit-field itself (for GCC anyway... the
19853 DWARF spec doesn't actually mandate this). Note that it is the size (in
19854 bytes) of the hypothetical "containing object" which will be given in the
19855 DW_AT_byte_size attribute for this bit-field. (See the
19856 `byte_size_attribute' function below.) It is also used when calculating the
19857 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19858 function below.)
19860 CTX is required: see the comment for VLR_CONTEXT. */
19862 static void
19863 add_data_member_location_attribute (dw_die_ref die,
19864 tree decl,
19865 struct vlr_context *ctx)
19867 HOST_WIDE_INT offset;
19868 dw_loc_descr_ref loc_descr = 0;
19870 if (TREE_CODE (decl) == TREE_BINFO)
19872 /* We're working on the TAG_inheritance for a base class. */
19873 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19875 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19876 aren't at a fixed offset from all (sub)objects of the same
19877 type. We need to extract the appropriate offset from our
19878 vtable. The following dwarf expression means
19880 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19882 This is specific to the V3 ABI, of course. */
19884 dw_loc_descr_ref tmp;
19886 /* Make a copy of the object address. */
19887 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19888 add_loc_descr (&loc_descr, tmp);
19890 /* Extract the vtable address. */
19891 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19892 add_loc_descr (&loc_descr, tmp);
19894 /* Calculate the address of the offset. */
19895 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19896 gcc_assert (offset < 0);
19898 tmp = int_loc_descriptor (-offset);
19899 add_loc_descr (&loc_descr, tmp);
19900 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19901 add_loc_descr (&loc_descr, tmp);
19903 /* Extract the offset. */
19904 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19905 add_loc_descr (&loc_descr, tmp);
19907 /* Add it to the object address. */
19908 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19909 add_loc_descr (&loc_descr, tmp);
19911 else
19912 offset = tree_to_shwi (BINFO_OFFSET (decl));
19914 else
19916 loc_descr = field_byte_offset (decl, ctx, &offset);
19918 /* If loc_descr is available then we know the field offset is dynamic.
19919 However, GDB does not handle dynamic field offsets very well at the
19920 moment. */
19921 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
19923 loc_descr = NULL;
19924 offset = 0;
19927 /* Data member location evalutation starts with the base address on the
19928 stack. Compute the field offset and add it to this base address. */
19929 else if (loc_descr != NULL)
19930 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19933 if (! loc_descr)
19935 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19936 e.g. GDB only added support to it in November 2016. For DWARF5
19937 we need newer debug info consumers anyway. We might change this
19938 to dwarf_version >= 4 once most consumers catched up. */
19939 if (dwarf_version >= 5
19940 && TREE_CODE (decl) == FIELD_DECL
19941 && DECL_BIT_FIELD_TYPE (decl)
19942 && (ctx->variant_part_offset == NULL_TREE
19943 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
19945 tree off = bit_position (decl);
19946 if (ctx->variant_part_offset)
19947 off = bit_from_pos (ctx->variant_part_offset, off);
19948 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19950 remove_AT (die, DW_AT_byte_size);
19951 remove_AT (die, DW_AT_bit_offset);
19952 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19953 return;
19956 if (dwarf_version > 2)
19958 /* Don't need to output a location expression, just the constant. */
19959 if (offset < 0)
19960 add_AT_int (die, DW_AT_data_member_location, offset);
19961 else
19962 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19963 return;
19965 else
19967 enum dwarf_location_atom op;
19969 /* The DWARF2 standard says that we should assume that the structure
19970 address is already on the stack, so we can specify a structure
19971 field address by using DW_OP_plus_uconst. */
19972 op = DW_OP_plus_uconst;
19973 loc_descr = new_loc_descr (op, offset, 0);
19977 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19980 /* Writes integer values to dw_vec_const array. */
19982 static void
19983 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19985 while (size != 0)
19987 *dest++ = val & 0xff;
19988 val >>= 8;
19989 --size;
19993 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
19995 static HOST_WIDE_INT
19996 extract_int (const unsigned char *src, unsigned int size)
19998 HOST_WIDE_INT val = 0;
20000 src += size;
20001 while (size != 0)
20003 val <<= 8;
20004 val |= *--src & 0xff;
20005 --size;
20007 return val;
20010 /* Writes wide_int values to dw_vec_const array. */
20012 static void
20013 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
20015 int i;
20017 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20019 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20020 return;
20023 /* We'd have to extend this code to support odd sizes. */
20024 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20026 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20028 if (WORDS_BIG_ENDIAN)
20029 for (i = n - 1; i >= 0; i--)
20031 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20032 dest += sizeof (HOST_WIDE_INT);
20034 else
20035 for (i = 0; i < n; i++)
20037 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20038 dest += sizeof (HOST_WIDE_INT);
20042 /* Writes floating point values to dw_vec_const array. */
20044 static unsigned
20045 insert_float (const_rtx rtl, unsigned char *array)
20047 long val[4];
20048 int i;
20049 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20051 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20053 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20054 if (GET_MODE_SIZE (mode) < 4)
20056 gcc_assert (GET_MODE_SIZE (mode) == 2);
20057 insert_int (val[0], 2, array);
20058 return 2;
20061 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20063 insert_int (val[i], 4, array);
20064 array += 4;
20066 return 4;
20069 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20070 does not have a "location" either in memory or in a register. These
20071 things can arise in GNU C when a constant is passed as an actual parameter
20072 to an inlined function. They can also arise in C++ where declared
20073 constants do not necessarily get memory "homes". */
20075 static bool
20076 add_const_value_attribute (dw_die_ref die, rtx rtl)
20078 switch (GET_CODE (rtl))
20080 case CONST_INT:
20082 HOST_WIDE_INT val = INTVAL (rtl);
20084 if (val < 0)
20085 add_AT_int (die, DW_AT_const_value, val);
20086 else
20087 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20089 return true;
20091 case CONST_WIDE_INT:
20093 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
20094 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
20095 (unsigned int) CONST_WIDE_INT_NUNITS (rtl)
20096 * HOST_BITS_PER_WIDE_INT);
20097 wide_int w = wide_int::from (w1, prec, UNSIGNED);
20098 add_AT_wide (die, DW_AT_const_value, w);
20100 return true;
20102 case CONST_DOUBLE:
20103 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20104 floating-point constant. A CONST_DOUBLE is used whenever the
20105 constant requires more than one word in order to be adequately
20106 represented. */
20107 if (TARGET_SUPPORTS_WIDE_INT == 0
20108 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20109 add_AT_double (die, DW_AT_const_value,
20110 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20111 else
20113 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20114 unsigned int length = GET_MODE_SIZE (mode);
20115 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20116 unsigned int elt_size = insert_float (rtl, array);
20118 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20119 array);
20121 return true;
20123 case CONST_VECTOR:
20125 unsigned int length;
20126 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20127 return false;
20129 machine_mode mode = GET_MODE (rtl);
20130 /* The combination of a length and byte elt_size doesn't extend
20131 naturally to boolean vectors, where several elements are packed
20132 into the same byte. */
20133 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20134 return false;
20136 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20137 unsigned char *array
20138 = ggc_vec_alloc<unsigned char> (length * elt_size);
20139 unsigned int i;
20140 unsigned char *p;
20141 machine_mode imode = GET_MODE_INNER (mode);
20143 switch (GET_MODE_CLASS (mode))
20145 case MODE_VECTOR_INT:
20146 for (i = 0, p = array; i < length; i++, p += elt_size)
20148 rtx elt = CONST_VECTOR_ELT (rtl, i);
20149 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20151 break;
20153 case MODE_VECTOR_FLOAT:
20154 for (i = 0, p = array; i < length; i++, p += elt_size)
20156 rtx elt = CONST_VECTOR_ELT (rtl, i);
20157 insert_float (elt, p);
20159 break;
20161 default:
20162 gcc_unreachable ();
20165 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20167 return true;
20169 case CONST_STRING:
20170 if (dwarf_version >= 4 || !dwarf_strict)
20172 dw_loc_descr_ref loc_result;
20173 resolve_one_addr (&rtl);
20174 rtl_addr:
20175 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20176 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20177 add_AT_loc (die, DW_AT_location, loc_result);
20178 vec_safe_push (used_rtx_array, rtl);
20179 return true;
20181 return false;
20183 case CONST:
20184 if (CONSTANT_P (XEXP (rtl, 0)))
20185 return add_const_value_attribute (die, XEXP (rtl, 0));
20186 /* FALLTHROUGH */
20187 case SYMBOL_REF:
20188 if (!const_ok_for_output (rtl))
20189 return false;
20190 /* FALLTHROUGH */
20191 case LABEL_REF:
20192 if (dwarf_version >= 4 || !dwarf_strict)
20193 goto rtl_addr;
20194 return false;
20196 case PLUS:
20197 /* In cases where an inlined instance of an inline function is passed
20198 the address of an `auto' variable (which is local to the caller) we
20199 can get a situation where the DECL_RTL of the artificial local
20200 variable (for the inlining) which acts as a stand-in for the
20201 corresponding formal parameter (of the inline function) will look
20202 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20203 exactly a compile-time constant expression, but it isn't the address
20204 of the (artificial) local variable either. Rather, it represents the
20205 *value* which the artificial local variable always has during its
20206 lifetime. We currently have no way to represent such quasi-constant
20207 values in Dwarf, so for now we just punt and generate nothing. */
20208 return false;
20210 case HIGH:
20211 case CONST_FIXED:
20212 case MINUS:
20213 case SIGN_EXTEND:
20214 case ZERO_EXTEND:
20215 case CONST_POLY_INT:
20216 return false;
20218 case MEM:
20219 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20220 && MEM_READONLY_P (rtl)
20221 && GET_MODE (rtl) == BLKmode)
20223 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20224 return true;
20226 return false;
20228 default:
20229 /* No other kinds of rtx should be possible here. */
20230 gcc_unreachable ();
20232 return false;
20235 /* Determine whether the evaluation of EXPR references any variables
20236 or functions which aren't otherwise used (and therefore may not be
20237 output). */
20238 static tree
20239 reference_to_unused (tree * tp, int * walk_subtrees,
20240 void * data ATTRIBUTE_UNUSED)
20242 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20243 *walk_subtrees = 0;
20245 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20246 && ! TREE_ASM_WRITTEN (*tp))
20247 return *tp;
20248 /* ??? The C++ FE emits debug information for using decls, so
20249 putting gcc_unreachable here falls over. See PR31899. For now
20250 be conservative. */
20251 else if (!symtab->global_info_ready && VAR_P (*tp))
20252 return *tp;
20253 else if (VAR_P (*tp))
20255 varpool_node *node = varpool_node::get (*tp);
20256 if (!node || !node->definition)
20257 return *tp;
20259 else if (TREE_CODE (*tp) == FUNCTION_DECL
20260 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20262 /* The call graph machinery must have finished analyzing,
20263 optimizing and gimplifying the CU by now.
20264 So if *TP has no call graph node associated
20265 to it, it means *TP will not be emitted. */
20266 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20267 return *tp;
20269 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20270 return *tp;
20272 return NULL_TREE;
20275 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20276 for use in a later add_const_value_attribute call. */
20278 static rtx
20279 rtl_for_decl_init (tree init, tree type)
20281 rtx rtl = NULL_RTX;
20283 STRIP_NOPS (init);
20285 /* If a variable is initialized with a string constant without embedded
20286 zeros, build CONST_STRING. */
20287 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20289 tree enttype = TREE_TYPE (type);
20290 tree domain = TYPE_DOMAIN (type);
20291 scalar_int_mode mode;
20293 if (is_int_mode (TYPE_MODE (enttype), &mode)
20294 && GET_MODE_SIZE (mode) == 1
20295 && domain
20296 && TYPE_MAX_VALUE (domain)
20297 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20298 && integer_zerop (TYPE_MIN_VALUE (domain))
20299 && compare_tree_int (TYPE_MAX_VALUE (domain),
20300 TREE_STRING_LENGTH (init) - 1) == 0
20301 && ((size_t) TREE_STRING_LENGTH (init)
20302 == strlen (TREE_STRING_POINTER (init)) + 1))
20304 rtl = gen_rtx_CONST_STRING (VOIDmode,
20305 ggc_strdup (TREE_STRING_POINTER (init)));
20306 rtl = gen_rtx_MEM (BLKmode, rtl);
20307 MEM_READONLY_P (rtl) = 1;
20310 /* Other aggregates, and complex values, could be represented using
20311 CONCAT: FIXME! */
20312 else if (AGGREGATE_TYPE_P (type)
20313 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20314 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20315 || TREE_CODE (type) == COMPLEX_TYPE)
20317 /* Vectors only work if their mode is supported by the target.
20318 FIXME: generic vectors ought to work too. */
20319 else if (TREE_CODE (type) == VECTOR_TYPE
20320 && !VECTOR_MODE_P (TYPE_MODE (type)))
20322 /* If the initializer is something that we know will expand into an
20323 immediate RTL constant, expand it now. We must be careful not to
20324 reference variables which won't be output. */
20325 else if (initializer_constant_valid_p (init, type)
20326 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20328 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20329 possible. */
20330 if (TREE_CODE (type) == VECTOR_TYPE)
20331 switch (TREE_CODE (init))
20333 case VECTOR_CST:
20334 break;
20335 case CONSTRUCTOR:
20336 if (TREE_CONSTANT (init))
20338 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20339 bool constant_p = true;
20340 tree value;
20341 unsigned HOST_WIDE_INT ix;
20343 /* Even when ctor is constant, it might contain non-*_CST
20344 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20345 belong into VECTOR_CST nodes. */
20346 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20347 if (!CONSTANT_CLASS_P (value))
20349 constant_p = false;
20350 break;
20353 if (constant_p)
20355 init = build_vector_from_ctor (type, elts);
20356 break;
20359 /* FALLTHRU */
20361 default:
20362 return NULL;
20365 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20367 /* If expand_expr returns a MEM, it wasn't immediate. */
20368 gcc_assert (!rtl || !MEM_P (rtl));
20371 return rtl;
20374 /* Generate RTL for the variable DECL to represent its location. */
20376 static rtx
20377 rtl_for_decl_location (tree decl)
20379 rtx rtl;
20381 /* Here we have to decide where we are going to say the parameter "lives"
20382 (as far as the debugger is concerned). We only have a couple of
20383 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20385 DECL_RTL normally indicates where the parameter lives during most of the
20386 activation of the function. If optimization is enabled however, this
20387 could be either NULL or else a pseudo-reg. Both of those cases indicate
20388 that the parameter doesn't really live anywhere (as far as the code
20389 generation parts of GCC are concerned) during most of the function's
20390 activation. That will happen (for example) if the parameter is never
20391 referenced within the function.
20393 We could just generate a location descriptor here for all non-NULL
20394 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20395 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20396 where DECL_RTL is NULL or is a pseudo-reg.
20398 Note however that we can only get away with using DECL_INCOMING_RTL as
20399 a backup substitute for DECL_RTL in certain limited cases. In cases
20400 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20401 we can be sure that the parameter was passed using the same type as it is
20402 declared to have within the function, and that its DECL_INCOMING_RTL
20403 points us to a place where a value of that type is passed.
20405 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20406 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20407 because in these cases DECL_INCOMING_RTL points us to a value of some
20408 type which is *different* from the type of the parameter itself. Thus,
20409 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20410 such cases, the debugger would end up (for example) trying to fetch a
20411 `float' from a place which actually contains the first part of a
20412 `double'. That would lead to really incorrect and confusing
20413 output at debug-time.
20415 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20416 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20417 are a couple of exceptions however. On little-endian machines we can
20418 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20419 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20420 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20421 when (on a little-endian machine) a non-prototyped function has a
20422 parameter declared to be of type `short' or `char'. In such cases,
20423 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20424 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20425 passed `int' value. If the debugger then uses that address to fetch
20426 a `short' or a `char' (on a little-endian machine) the result will be
20427 the correct data, so we allow for such exceptional cases below.
20429 Note that our goal here is to describe the place where the given formal
20430 parameter lives during most of the function's activation (i.e. between the
20431 end of the prologue and the start of the epilogue). We'll do that as best
20432 as we can. Note however that if the given formal parameter is modified
20433 sometime during the execution of the function, then a stack backtrace (at
20434 debug-time) will show the function as having been called with the *new*
20435 value rather than the value which was originally passed in. This happens
20436 rarely enough that it is not a major problem, but it *is* a problem, and
20437 I'd like to fix it.
20439 A future version of dwarf2out.c may generate two additional attributes for
20440 any given DW_TAG_formal_parameter DIE which will describe the "passed
20441 type" and the "passed location" for the given formal parameter in addition
20442 to the attributes we now generate to indicate the "declared type" and the
20443 "active location" for each parameter. This additional set of attributes
20444 could be used by debuggers for stack backtraces. Separately, note that
20445 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20446 This happens (for example) for inlined-instances of inline function formal
20447 parameters which are never referenced. This really shouldn't be
20448 happening. All PARM_DECL nodes should get valid non-NULL
20449 DECL_INCOMING_RTL values. FIXME. */
20451 /* Use DECL_RTL as the "location" unless we find something better. */
20452 rtl = DECL_RTL_IF_SET (decl);
20454 /* When generating abstract instances, ignore everything except
20455 constants, symbols living in memory, and symbols living in
20456 fixed registers. */
20457 if (! reload_completed)
20459 if (rtl
20460 && (CONSTANT_P (rtl)
20461 || (MEM_P (rtl)
20462 && CONSTANT_P (XEXP (rtl, 0)))
20463 || (REG_P (rtl)
20464 && VAR_P (decl)
20465 && TREE_STATIC (decl))))
20467 rtl = targetm.delegitimize_address (rtl);
20468 return rtl;
20470 rtl = NULL_RTX;
20472 else if (TREE_CODE (decl) == PARM_DECL)
20474 if (rtl == NULL_RTX
20475 || is_pseudo_reg (rtl)
20476 || (MEM_P (rtl)
20477 && is_pseudo_reg (XEXP (rtl, 0))
20478 && DECL_INCOMING_RTL (decl)
20479 && MEM_P (DECL_INCOMING_RTL (decl))
20480 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20482 tree declared_type = TREE_TYPE (decl);
20483 tree passed_type = DECL_ARG_TYPE (decl);
20484 machine_mode dmode = TYPE_MODE (declared_type);
20485 machine_mode pmode = TYPE_MODE (passed_type);
20487 /* This decl represents a formal parameter which was optimized out.
20488 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20489 all cases where (rtl == NULL_RTX) just below. */
20490 if (dmode == pmode)
20491 rtl = DECL_INCOMING_RTL (decl);
20492 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20493 && SCALAR_INT_MODE_P (dmode)
20494 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20495 && DECL_INCOMING_RTL (decl))
20497 rtx inc = DECL_INCOMING_RTL (decl);
20498 if (REG_P (inc))
20499 rtl = inc;
20500 else if (MEM_P (inc))
20502 if (BYTES_BIG_ENDIAN)
20503 rtl = adjust_address_nv (inc, dmode,
20504 GET_MODE_SIZE (pmode)
20505 - GET_MODE_SIZE (dmode));
20506 else
20507 rtl = inc;
20512 /* If the parm was passed in registers, but lives on the stack, then
20513 make a big endian correction if the mode of the type of the
20514 parameter is not the same as the mode of the rtl. */
20515 /* ??? This is the same series of checks that are made in dbxout.c before
20516 we reach the big endian correction code there. It isn't clear if all
20517 of these checks are necessary here, but keeping them all is the safe
20518 thing to do. */
20519 else if (MEM_P (rtl)
20520 && XEXP (rtl, 0) != const0_rtx
20521 && ! CONSTANT_P (XEXP (rtl, 0))
20522 /* Not passed in memory. */
20523 && !MEM_P (DECL_INCOMING_RTL (decl))
20524 /* Not passed by invisible reference. */
20525 && (!REG_P (XEXP (rtl, 0))
20526 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20527 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20528 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20529 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20530 #endif
20532 /* Big endian correction check. */
20533 && BYTES_BIG_ENDIAN
20534 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20535 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20536 UNITS_PER_WORD))
20538 machine_mode addr_mode = get_address_mode (rtl);
20539 poly_int64 offset = (UNITS_PER_WORD
20540 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20542 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20543 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20546 else if (VAR_P (decl)
20547 && rtl
20548 && MEM_P (rtl)
20549 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20551 machine_mode addr_mode = get_address_mode (rtl);
20552 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20553 GET_MODE (rtl));
20555 /* If a variable is declared "register" yet is smaller than
20556 a register, then if we store the variable to memory, it
20557 looks like we're storing a register-sized value, when in
20558 fact we are not. We need to adjust the offset of the
20559 storage location to reflect the actual value's bytes,
20560 else gdb will not be able to display it. */
20561 if (maybe_ne (offset, 0))
20562 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20563 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20566 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20567 and will have been substituted directly into all expressions that use it.
20568 C does not have such a concept, but C++ and other languages do. */
20569 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20570 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20572 if (rtl)
20573 rtl = targetm.delegitimize_address (rtl);
20575 /* If we don't look past the constant pool, we risk emitting a
20576 reference to a constant pool entry that isn't referenced from
20577 code, and thus is not emitted. */
20578 if (rtl)
20579 rtl = avoid_constant_pool_reference (rtl);
20581 /* Try harder to get a rtl. If this symbol ends up not being emitted
20582 in the current CU, resolve_addr will remove the expression referencing
20583 it. */
20584 if (rtl == NULL_RTX
20585 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20586 && VAR_P (decl)
20587 && !DECL_EXTERNAL (decl)
20588 && TREE_STATIC (decl)
20589 && DECL_NAME (decl)
20590 && !DECL_HARD_REGISTER (decl)
20591 && DECL_MODE (decl) != VOIDmode)
20593 rtl = make_decl_rtl_for_debug (decl);
20594 if (!MEM_P (rtl)
20595 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20596 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20597 rtl = NULL_RTX;
20600 return rtl;
20603 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20604 returned. If so, the decl for the COMMON block is returned, and the
20605 value is the offset into the common block for the symbol. */
20607 static tree
20608 fortran_common (tree decl, HOST_WIDE_INT *value)
20610 tree val_expr, cvar;
20611 machine_mode mode;
20612 poly_int64 bitsize, bitpos;
20613 tree offset;
20614 HOST_WIDE_INT cbitpos;
20615 int unsignedp, reversep, volatilep = 0;
20617 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20618 it does not have a value (the offset into the common area), or if it
20619 is thread local (as opposed to global) then it isn't common, and shouldn't
20620 be handled as such. */
20621 if (!VAR_P (decl)
20622 || !TREE_STATIC (decl)
20623 || !DECL_HAS_VALUE_EXPR_P (decl)
20624 || !is_fortran ())
20625 return NULL_TREE;
20627 val_expr = DECL_VALUE_EXPR (decl);
20628 if (TREE_CODE (val_expr) != COMPONENT_REF)
20629 return NULL_TREE;
20631 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20632 &unsignedp, &reversep, &volatilep);
20634 if (cvar == NULL_TREE
20635 || !VAR_P (cvar)
20636 || DECL_ARTIFICIAL (cvar)
20637 || !TREE_PUBLIC (cvar)
20638 /* We don't expect to have to cope with variable offsets,
20639 since at present all static data must have a constant size. */
20640 || !bitpos.is_constant (&cbitpos))
20641 return NULL_TREE;
20643 *value = 0;
20644 if (offset != NULL)
20646 if (!tree_fits_shwi_p (offset))
20647 return NULL_TREE;
20648 *value = tree_to_shwi (offset);
20650 if (cbitpos != 0)
20651 *value += cbitpos / BITS_PER_UNIT;
20653 return cvar;
20656 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20657 data attribute for a variable or a parameter. We generate the
20658 DW_AT_const_value attribute only in those cases where the given variable
20659 or parameter does not have a true "location" either in memory or in a
20660 register. This can happen (for example) when a constant is passed as an
20661 actual argument in a call to an inline function. (It's possible that
20662 these things can crop up in other ways also.) Note that one type of
20663 constant value which can be passed into an inlined function is a constant
20664 pointer. This can happen for example if an actual argument in an inlined
20665 function call evaluates to a compile-time constant address.
20667 CACHE_P is true if it is worth caching the location list for DECL,
20668 so that future calls can reuse it rather than regenerate it from scratch.
20669 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20670 since we will need to refer to them each time the function is inlined. */
20672 static bool
20673 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20675 rtx rtl;
20676 dw_loc_list_ref list;
20677 var_loc_list *loc_list;
20678 cached_dw_loc_list *cache;
20680 if (early_dwarf)
20681 return false;
20683 if (TREE_CODE (decl) == ERROR_MARK)
20684 return false;
20686 if (get_AT (die, DW_AT_location)
20687 || get_AT (die, DW_AT_const_value))
20688 return true;
20690 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20691 || TREE_CODE (decl) == RESULT_DECL);
20693 /* Try to get some constant RTL for this decl, and use that as the value of
20694 the location. */
20696 rtl = rtl_for_decl_location (decl);
20697 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20698 && add_const_value_attribute (die, rtl))
20699 return true;
20701 /* See if we have single element location list that is equivalent to
20702 a constant value. That way we are better to use add_const_value_attribute
20703 rather than expanding constant value equivalent. */
20704 loc_list = lookup_decl_loc (decl);
20705 if (loc_list
20706 && loc_list->first
20707 && loc_list->first->next == NULL
20708 && NOTE_P (loc_list->first->loc)
20709 && NOTE_VAR_LOCATION (loc_list->first->loc)
20710 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20712 struct var_loc_node *node;
20714 node = loc_list->first;
20715 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20716 if (GET_CODE (rtl) == EXPR_LIST)
20717 rtl = XEXP (rtl, 0);
20718 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20719 && add_const_value_attribute (die, rtl))
20720 return true;
20722 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20723 list several times. See if we've already cached the contents. */
20724 list = NULL;
20725 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20726 cache_p = false;
20727 if (cache_p)
20729 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20730 if (cache)
20731 list = cache->loc_list;
20733 if (list == NULL)
20735 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20736 NULL);
20737 /* It is usually worth caching this result if the decl is from
20738 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20739 if (cache_p && list && list->dw_loc_next)
20741 cached_dw_loc_list **slot
20742 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20743 DECL_UID (decl),
20744 INSERT);
20745 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20746 cache->decl_id = DECL_UID (decl);
20747 cache->loc_list = list;
20748 *slot = cache;
20751 if (list)
20753 add_AT_location_description (die, DW_AT_location, list);
20754 return true;
20756 /* None of that worked, so it must not really have a location;
20757 try adding a constant value attribute from the DECL_INITIAL. */
20758 return tree_add_const_value_attribute_for_decl (die, decl);
20761 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20762 attribute is the const value T. */
20764 static bool
20765 tree_add_const_value_attribute (dw_die_ref die, tree t)
20767 tree init;
20768 tree type = TREE_TYPE (t);
20769 rtx rtl;
20771 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20772 return false;
20774 init = t;
20775 gcc_assert (!DECL_P (init));
20777 if (TREE_CODE (init) == INTEGER_CST)
20779 if (tree_fits_uhwi_p (init))
20781 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20782 return true;
20784 if (tree_fits_shwi_p (init))
20786 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20787 return true;
20790 /* Generate the RTL even if early_dwarf to force mangling of all refered to
20791 symbols. */
20792 rtl = rtl_for_decl_init (init, type);
20793 if (rtl && !early_dwarf)
20794 return add_const_value_attribute (die, rtl);
20795 /* If the host and target are sane, try harder. */
20796 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20797 && initializer_constant_valid_p (init, type))
20799 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20800 if (size > 0 && (int) size == size)
20802 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20804 if (native_encode_initializer (init, array, size) == size)
20806 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20807 return true;
20809 ggc_free (array);
20812 return false;
20815 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20816 attribute is the const value of T, where T is an integral constant
20817 variable with static storage duration
20818 (so it can't be a PARM_DECL or a RESULT_DECL). */
20820 static bool
20821 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20824 if (!decl
20825 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20826 || (VAR_P (decl) && !TREE_STATIC (decl)))
20827 return false;
20829 if (TREE_READONLY (decl)
20830 && ! TREE_THIS_VOLATILE (decl)
20831 && DECL_INITIAL (decl))
20832 /* OK */;
20833 else
20834 return false;
20836 /* Don't add DW_AT_const_value if abstract origin already has one. */
20837 if (get_AT (var_die, DW_AT_const_value))
20838 return false;
20840 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20843 /* Convert the CFI instructions for the current function into a
20844 location list. This is used for DW_AT_frame_base when we targeting
20845 a dwarf2 consumer that does not support the dwarf3
20846 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20847 expressions. */
20849 static dw_loc_list_ref
20850 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20852 int ix;
20853 dw_fde_ref fde;
20854 dw_loc_list_ref list, *list_tail;
20855 dw_cfi_ref cfi;
20856 dw_cfa_location last_cfa, next_cfa;
20857 const char *start_label, *last_label, *section;
20858 dw_cfa_location remember;
20860 fde = cfun->fde;
20861 gcc_assert (fde != NULL);
20863 section = secname_for_decl (current_function_decl);
20864 list_tail = &list;
20865 list = NULL;
20867 memset (&next_cfa, 0, sizeof (next_cfa));
20868 next_cfa.reg = INVALID_REGNUM;
20869 remember = next_cfa;
20871 start_label = fde->dw_fde_begin;
20873 /* ??? Bald assumption that the CIE opcode list does not contain
20874 advance opcodes. */
20875 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20876 lookup_cfa_1 (cfi, &next_cfa, &remember);
20878 last_cfa = next_cfa;
20879 last_label = start_label;
20881 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20883 /* If the first partition contained no CFI adjustments, the
20884 CIE opcodes apply to the whole first partition. */
20885 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20886 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20887 list_tail =&(*list_tail)->dw_loc_next;
20888 start_label = last_label = fde->dw_fde_second_begin;
20891 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20893 switch (cfi->dw_cfi_opc)
20895 case DW_CFA_set_loc:
20896 case DW_CFA_advance_loc1:
20897 case DW_CFA_advance_loc2:
20898 case DW_CFA_advance_loc4:
20899 if (!cfa_equal_p (&last_cfa, &next_cfa))
20901 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20902 start_label, 0, last_label, 0, section);
20904 list_tail = &(*list_tail)->dw_loc_next;
20905 last_cfa = next_cfa;
20906 start_label = last_label;
20908 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20909 break;
20911 case DW_CFA_advance_loc:
20912 /* The encoding is complex enough that we should never emit this. */
20913 gcc_unreachable ();
20915 default:
20916 lookup_cfa_1 (cfi, &next_cfa, &remember);
20917 break;
20919 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20921 if (!cfa_equal_p (&last_cfa, &next_cfa))
20923 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20924 start_label, 0, last_label, 0, section);
20926 list_tail = &(*list_tail)->dw_loc_next;
20927 last_cfa = next_cfa;
20928 start_label = last_label;
20930 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20931 start_label, 0, fde->dw_fde_end, 0, section);
20932 list_tail = &(*list_tail)->dw_loc_next;
20933 start_label = last_label = fde->dw_fde_second_begin;
20937 if (!cfa_equal_p (&last_cfa, &next_cfa))
20939 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20940 start_label, 0, last_label, 0, section);
20941 list_tail = &(*list_tail)->dw_loc_next;
20942 start_label = last_label;
20945 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20946 start_label, 0,
20947 fde->dw_fde_second_begin
20948 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20949 section);
20951 maybe_gen_llsym (list);
20953 return list;
20956 /* Compute a displacement from the "steady-state frame pointer" to the
20957 frame base (often the same as the CFA), and store it in
20958 frame_pointer_fb_offset. OFFSET is added to the displacement
20959 before the latter is negated. */
20961 static void
20962 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20964 rtx reg, elim;
20966 #ifdef FRAME_POINTER_CFA_OFFSET
20967 reg = frame_pointer_rtx;
20968 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20969 #else
20970 reg = arg_pointer_rtx;
20971 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20972 #endif
20974 elim = (ira_use_lra_p
20975 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20976 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20977 elim = strip_offset_and_add (elim, &offset);
20979 frame_pointer_fb_offset = -offset;
20981 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20982 in which to eliminate. This is because it's stack pointer isn't
20983 directly accessible as a register within the ISA. To work around
20984 this, assume that while we cannot provide a proper value for
20985 frame_pointer_fb_offset, we won't need one either. We can use
20986 hard frame pointer in debug info even if frame pointer isn't used
20987 since hard frame pointer in debug info is encoded with DW_OP_fbreg
20988 which uses the DW_AT_frame_base attribute, not hard frame pointer
20989 directly. */
20990 frame_pointer_fb_offset_valid
20991 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
20994 /* Generate a DW_AT_name attribute given some string value to be included as
20995 the value of the attribute. */
20997 static void
20998 add_name_attribute (dw_die_ref die, const char *name_string)
21000 if (name_string != NULL && *name_string != 0)
21002 if (demangle_name_func)
21003 name_string = (*demangle_name_func) (name_string);
21005 add_AT_string (die, DW_AT_name, name_string);
21009 /* Generate a DW_AT_name attribute given some string value representing a
21010 file or filepath to be included as value of the attribute. */
21011 static void
21012 add_filename_attribute (dw_die_ref die, const char *name_string)
21014 if (name_string != NULL && *name_string != 0)
21015 add_filepath_AT_string (die, DW_AT_name, name_string);
21018 /* Generate a DW_AT_description attribute given some string value to be included
21019 as the value of the attribute. */
21021 static void
21022 add_desc_attribute (dw_die_ref die, const char *name_string)
21024 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21025 return;
21027 if (name_string == NULL || *name_string == 0)
21028 return;
21030 if (demangle_name_func)
21031 name_string = (*demangle_name_func) (name_string);
21033 add_AT_string (die, DW_AT_description, name_string);
21036 /* Generate a DW_AT_description attribute given some decl to be included
21037 as the value of the attribute. */
21039 static void
21040 add_desc_attribute (dw_die_ref die, tree decl)
21042 tree decl_name;
21044 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21045 return;
21047 if (decl == NULL_TREE || !DECL_P (decl))
21048 return;
21049 decl_name = DECL_NAME (decl);
21051 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21053 const char *name = dwarf2_name (decl, 0);
21054 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21056 else
21058 char *desc = print_generic_expr_to_str (decl);
21059 add_desc_attribute (die, desc);
21060 free (desc);
21064 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21065 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21066 of TYPE accordingly.
21068 ??? This is a temporary measure until after we're able to generate
21069 regular DWARF for the complex Ada type system. */
21071 static void
21072 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21073 dw_die_ref context_die)
21075 tree dtype;
21076 dw_die_ref dtype_die;
21078 if (!lang_hooks.types.descriptive_type)
21079 return;
21081 dtype = lang_hooks.types.descriptive_type (type);
21082 if (!dtype)
21083 return;
21085 dtype_die = lookup_type_die (dtype);
21086 if (!dtype_die)
21088 gen_type_die (dtype, context_die);
21089 dtype_die = lookup_type_die (dtype);
21090 gcc_assert (dtype_die);
21093 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21096 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21098 static const char *
21099 comp_dir_string (void)
21101 const char *wd;
21102 char *wd_plus_sep = NULL;
21103 static const char *cached_wd = NULL;
21105 if (cached_wd != NULL)
21106 return cached_wd;
21108 wd = get_src_pwd ();
21109 if (wd == NULL)
21110 return NULL;
21112 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21114 size_t wdlen = strlen (wd);
21115 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21116 strcpy (wd_plus_sep, wd);
21117 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21118 wd_plus_sep [wdlen + 1] = 0;
21119 wd = wd_plus_sep;
21122 cached_wd = remap_debug_filename (wd);
21124 /* remap_debug_filename can just pass through wd or return a new gc string.
21125 These two types can't be both stored in a GTY(())-tagged string, but since
21126 the cached value lives forever just copy it if needed. */
21127 if (cached_wd != wd)
21129 cached_wd = xstrdup (cached_wd);
21130 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21131 free (wd_plus_sep);
21134 return cached_wd;
21137 /* Generate a DW_AT_comp_dir attribute for DIE. */
21139 static void
21140 add_comp_dir_attribute (dw_die_ref die)
21142 const char * wd = comp_dir_string ();
21143 if (wd != NULL)
21144 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21147 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21148 pointer computation, ...), output a representation for that bound according
21149 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21150 loc_list_from_tree for the meaning of CONTEXT. */
21152 static void
21153 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21154 int forms, struct loc_descr_context *context)
21156 dw_die_ref context_die, decl_die = NULL;
21157 dw_loc_list_ref list;
21158 bool strip_conversions = true;
21159 bool placeholder_seen = false;
21161 while (strip_conversions)
21162 switch (TREE_CODE (value))
21164 case ERROR_MARK:
21165 case SAVE_EXPR:
21166 return;
21168 CASE_CONVERT:
21169 case VIEW_CONVERT_EXPR:
21170 value = TREE_OPERAND (value, 0);
21171 break;
21173 default:
21174 strip_conversions = false;
21175 break;
21178 /* If possible and permitted, output the attribute as a constant. */
21179 if ((forms & dw_scalar_form_constant) != 0
21180 && TREE_CODE (value) == INTEGER_CST)
21182 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21184 /* If HOST_WIDE_INT is big enough then represent the bound as
21185 a constant value. We need to choose a form based on
21186 whether the type is signed or unsigned. We cannot just
21187 call add_AT_unsigned if the value itself is positive
21188 (add_AT_unsigned might add the unsigned value encoded as
21189 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21190 bounds type and then sign extend any unsigned values found
21191 for signed types. This is needed only for
21192 DW_AT_{lower,upper}_bound, since for most other attributes,
21193 consumers will treat DW_FORM_data[1248] as unsigned values,
21194 regardless of the underlying type. */
21195 if (prec <= HOST_BITS_PER_WIDE_INT
21196 || tree_fits_uhwi_p (value))
21198 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21199 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21200 else
21201 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21203 else if (dwarf_version >= 5
21204 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21205 /* Otherwise represent the bound as an unsigned value with
21206 the precision of its type. The precision and signedness
21207 of the type will be necessary to re-interpret it
21208 unambiguously. */
21209 add_AT_wide (die, attr, wi::to_wide (value));
21210 else
21212 rtx v = immed_wide_int_const (wi::to_wide (value),
21213 TYPE_MODE (TREE_TYPE (value)));
21214 dw_loc_descr_ref loc
21215 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21216 VAR_INIT_STATUS_INITIALIZED);
21217 if (loc)
21218 add_AT_loc (die, attr, loc);
21220 return;
21223 /* Otherwise, if it's possible and permitted too, output a reference to
21224 another DIE. */
21225 if ((forms & dw_scalar_form_reference) != 0)
21227 tree decl = NULL_TREE;
21229 /* Some type attributes reference an outer type. For instance, the upper
21230 bound of an array may reference an embedding record (this happens in
21231 Ada). */
21232 if (TREE_CODE (value) == COMPONENT_REF
21233 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21234 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21235 decl = TREE_OPERAND (value, 1);
21237 else if (VAR_P (value)
21238 || TREE_CODE (value) == PARM_DECL
21239 || TREE_CODE (value) == RESULT_DECL)
21240 decl = value;
21242 if (decl != NULL_TREE)
21244 decl_die = lookup_decl_die (decl);
21246 /* ??? Can this happen, or should the variable have been bound
21247 first? Probably it can, since I imagine that we try to create
21248 the types of parameters in the order in which they exist in
21249 the list, and won't have created a forward reference to a
21250 later parameter. */
21251 if (decl_die != NULL)
21253 if (get_AT (decl_die, DW_AT_location)
21254 || get_AT (decl_die, DW_AT_data_member_location)
21255 || get_AT (decl_die, DW_AT_const_value))
21257 add_AT_die_ref (die, attr, decl_die);
21258 return;
21264 /* Last chance: try to create a stack operation procedure to evaluate the
21265 value. Do nothing if even that is not possible or permitted. */
21266 if ((forms & dw_scalar_form_exprloc) == 0)
21267 return;
21269 list = loc_list_from_tree (value, 2, context);
21270 if (context && context->placeholder_arg)
21272 placeholder_seen = context->placeholder_seen;
21273 context->placeholder_seen = false;
21275 if (list == NULL || single_element_loc_list_p (list))
21277 /* If this attribute is not a reference nor constant, it is
21278 a DWARF expression rather than location description. For that
21279 loc_list_from_tree (value, 0, &context) is needed. */
21280 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21281 if (list2 && single_element_loc_list_p (list2))
21283 if (placeholder_seen)
21285 struct dwarf_procedure_info dpi;
21286 dpi.fndecl = NULL_TREE;
21287 dpi.args_count = 1;
21288 if (!resolve_args_picking (list2->expr, 1, &dpi))
21289 return;
21291 add_AT_loc (die, attr, list2->expr);
21292 return;
21296 /* If that failed to give a single element location list, fall back to
21297 outputting this as a reference... still if permitted. */
21298 if (list == NULL
21299 || (forms & dw_scalar_form_reference) == 0
21300 || placeholder_seen)
21301 return;
21303 if (!decl_die)
21305 if (current_function_decl == 0)
21306 context_die = comp_unit_die ();
21307 else
21308 context_die = lookup_decl_die (current_function_decl);
21310 decl_die = new_die (DW_TAG_variable, context_die, value);
21311 add_AT_flag (decl_die, DW_AT_artificial, 1);
21312 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21313 context_die);
21316 add_AT_location_description (decl_die, DW_AT_location, list);
21317 add_AT_die_ref (die, attr, decl_die);
21320 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21321 default. */
21323 static int
21324 lower_bound_default (void)
21326 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21328 case DW_LANG_C:
21329 case DW_LANG_C89:
21330 case DW_LANG_C99:
21331 case DW_LANG_C11:
21332 case DW_LANG_C_plus_plus:
21333 case DW_LANG_C_plus_plus_11:
21334 case DW_LANG_C_plus_plus_14:
21335 case DW_LANG_ObjC:
21336 case DW_LANG_ObjC_plus_plus:
21337 return 0;
21338 case DW_LANG_Fortran77:
21339 case DW_LANG_Fortran90:
21340 case DW_LANG_Fortran95:
21341 case DW_LANG_Fortran03:
21342 case DW_LANG_Fortran08:
21343 return 1;
21344 case DW_LANG_UPC:
21345 case DW_LANG_D:
21346 case DW_LANG_Python:
21347 return dwarf_version >= 4 ? 0 : -1;
21348 case DW_LANG_Ada95:
21349 case DW_LANG_Ada83:
21350 case DW_LANG_Cobol74:
21351 case DW_LANG_Cobol85:
21352 case DW_LANG_Modula2:
21353 case DW_LANG_PLI:
21354 return dwarf_version >= 4 ? 1 : -1;
21355 default:
21356 return -1;
21360 /* Given a tree node describing an array bound (either lower or upper) output
21361 a representation for that bound. */
21363 static void
21364 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21365 tree bound, struct loc_descr_context *context)
21367 int dflt;
21369 while (1)
21370 switch (TREE_CODE (bound))
21372 /* Strip all conversions. */
21373 CASE_CONVERT:
21374 case VIEW_CONVERT_EXPR:
21375 bound = TREE_OPERAND (bound, 0);
21376 break;
21378 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21379 are even omitted when they are the default. */
21380 case INTEGER_CST:
21381 /* If the value for this bound is the default one, we can even omit the
21382 attribute. */
21383 if (bound_attr == DW_AT_lower_bound
21384 && tree_fits_shwi_p (bound)
21385 && (dflt = lower_bound_default ()) != -1
21386 && tree_to_shwi (bound) == dflt)
21387 return;
21389 /* FALLTHRU */
21391 default:
21392 /* Because of the complex interaction there can be with other GNAT
21393 encodings, GDB isn't ready yet to handle proper DWARF description
21394 for self-referencial subrange bounds: let GNAT encodings do the
21395 magic in such a case. */
21396 if (is_ada ()
21397 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
21398 && contains_placeholder_p (bound))
21399 return;
21401 add_scalar_info (subrange_die, bound_attr, bound,
21402 dw_scalar_form_constant
21403 | dw_scalar_form_exprloc
21404 | dw_scalar_form_reference,
21405 context);
21406 return;
21410 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21411 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21413 This function reuses previously set type and bound information if
21414 available. */
21416 static void
21417 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21419 dw_die_ref child = type_die->die_child;
21420 struct array_descr_info info;
21421 int dimension_number;
21423 if (lang_hooks.types.get_array_descr_info)
21425 memset (&info, 0, sizeof (info));
21426 if (lang_hooks.types.get_array_descr_info (type, &info))
21427 /* Fortran sometimes emits array types with no dimension. */
21428 gcc_assert (info.ndimensions >= 0
21429 && info.ndimensions
21430 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21432 else
21433 info.ndimensions = 0;
21435 for (dimension_number = 0;
21436 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21437 type = TREE_TYPE (type), dimension_number++)
21439 tree domain = TYPE_DOMAIN (type);
21441 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21442 break;
21444 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21445 and (in GNU C only) variable bounds. Handle all three forms
21446 here. */
21448 /* Find and reuse a previously generated DW_TAG_subrange_type if
21449 available.
21451 For multi-dimensional arrays, as we iterate through the
21452 various dimensions in the enclosing for loop above, we also
21453 iterate through the DIE children and pick at each
21454 DW_TAG_subrange_type previously generated (if available).
21455 Each child DW_TAG_subrange_type DIE describes the range of
21456 the current dimension. At this point we should have as many
21457 DW_TAG_subrange_type's as we have dimensions in the
21458 array. */
21459 dw_die_ref subrange_die = NULL;
21460 if (child)
21461 while (1)
21463 child = child->die_sib;
21464 if (child->die_tag == DW_TAG_subrange_type)
21465 subrange_die = child;
21466 if (child == type_die->die_child)
21468 /* If we wrapped around, stop looking next time. */
21469 child = NULL;
21470 break;
21472 if (child->die_tag == DW_TAG_subrange_type)
21473 break;
21475 if (!subrange_die)
21476 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21478 if (domain)
21480 /* We have an array type with specified bounds. */
21481 tree lower = TYPE_MIN_VALUE (domain);
21482 tree upper = TYPE_MAX_VALUE (domain);
21483 tree index_type = TREE_TYPE (domain);
21485 if (dimension_number <= info.ndimensions - 1)
21487 lower = info.dimen[dimension_number].lower_bound;
21488 upper = info.dimen[dimension_number].upper_bound;
21489 index_type = info.dimen[dimension_number].bounds_type;
21492 /* Define the index type. */
21493 if (index_type && !get_AT (subrange_die, DW_AT_type))
21494 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21495 false, type_die);
21497 /* ??? If upper is NULL, the array has unspecified length,
21498 but it does have a lower bound. This happens with Fortran
21499 dimension arr(N:*)
21500 Since the debugger is definitely going to need to know N
21501 to produce useful results, go ahead and output the lower
21502 bound solo, and hope the debugger can cope. */
21504 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21505 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21507 if (!get_AT (subrange_die, DW_AT_upper_bound)
21508 && !get_AT (subrange_die, DW_AT_count))
21510 if (upper)
21511 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21512 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21513 /* Zero-length array. */
21514 add_bound_info (subrange_die, DW_AT_count,
21515 build_int_cst (TREE_TYPE (lower), 0), NULL);
21519 /* Otherwise we have an array type with an unspecified length. The
21520 DWARF-2 spec does not say how to handle this; let's just leave out the
21521 bounds. */
21525 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21527 static void
21528 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21530 dw_die_ref decl_die;
21531 HOST_WIDE_INT size;
21533 switch (TREE_CODE (tree_node))
21535 case ERROR_MARK:
21536 size = 0;
21537 break;
21538 case ENUMERAL_TYPE:
21539 case RECORD_TYPE:
21540 case UNION_TYPE:
21541 case QUAL_UNION_TYPE:
21542 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21543 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21545 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21546 return;
21548 size = int_size_in_bytes (tree_node);
21549 break;
21550 case FIELD_DECL:
21551 /* For a data member of a struct or union, the DW_AT_byte_size is
21552 generally given as the number of bytes normally allocated for an
21553 object of the *declared* type of the member itself. This is true
21554 even for bit-fields. */
21555 size = int_size_in_bytes (field_type (tree_node));
21556 break;
21557 default:
21558 gcc_unreachable ();
21561 /* Note that `size' might be -1 when we get to this point. If it is, that
21562 indicates that the byte size of the entity in question is variable. */
21563 if (size >= 0)
21564 add_AT_unsigned (die, DW_AT_byte_size, size);
21566 /* Support for dynamically-sized objects was introduced in DWARF3. */
21567 else if (TYPE_P (tree_node)
21568 && (dwarf_version >= 3 || !dwarf_strict)
21569 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
21571 struct loc_descr_context ctx = {
21572 const_cast<tree> (tree_node), /* context_type */
21573 NULL_TREE, /* base_decl */
21574 NULL, /* dpi */
21575 false, /* placeholder_arg */
21576 false, /* placeholder_seen */
21577 false /* strict_signedness */
21580 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21581 add_scalar_info (die, DW_AT_byte_size, tree_size,
21582 dw_scalar_form_constant
21583 | dw_scalar_form_exprloc
21584 | dw_scalar_form_reference,
21585 &ctx);
21589 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21590 alignment. */
21592 static void
21593 add_alignment_attribute (dw_die_ref die, tree tree_node)
21595 if (dwarf_version < 5 && dwarf_strict)
21596 return;
21598 unsigned align;
21600 if (DECL_P (tree_node))
21602 if (!DECL_USER_ALIGN (tree_node))
21603 return;
21605 align = DECL_ALIGN_UNIT (tree_node);
21607 else if (TYPE_P (tree_node))
21609 if (!TYPE_USER_ALIGN (tree_node))
21610 return;
21612 align = TYPE_ALIGN_UNIT (tree_node);
21614 else
21615 gcc_unreachable ();
21617 add_AT_unsigned (die, DW_AT_alignment, align);
21620 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21621 which specifies the distance in bits from the highest order bit of the
21622 "containing object" for the bit-field to the highest order bit of the
21623 bit-field itself.
21625 For any given bit-field, the "containing object" is a hypothetical object
21626 (of some integral or enum type) within which the given bit-field lives. The
21627 type of this hypothetical "containing object" is always the same as the
21628 declared type of the individual bit-field itself. The determination of the
21629 exact location of the "containing object" for a bit-field is rather
21630 complicated. It's handled by the `field_byte_offset' function (above).
21632 Note that it is the size (in bytes) of the hypothetical "containing object"
21633 which will be given in the DW_AT_byte_size attribute for this bit-field.
21634 (See `byte_size_attribute' above). */
21636 static inline void
21637 add_bit_offset_attribute (dw_die_ref die, tree decl)
21639 HOST_WIDE_INT object_offset_in_bytes;
21640 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21641 HOST_WIDE_INT bitpos_int;
21642 HOST_WIDE_INT highest_order_object_bit_offset;
21643 HOST_WIDE_INT highest_order_field_bit_offset;
21644 HOST_WIDE_INT bit_offset;
21646 /* The containing object is within the DECL_CONTEXT. */
21647 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21649 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21651 /* Must be a field and a bit field. */
21652 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21654 /* We can't yet handle bit-fields whose offsets are variable, so if we
21655 encounter such things, just return without generating any attribute
21656 whatsoever. Likewise for variable or too large size. */
21657 if (! tree_fits_shwi_p (bit_position (decl))
21658 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21659 return;
21661 bitpos_int = int_bit_position (decl);
21663 /* Note that the bit offset is always the distance (in bits) from the
21664 highest-order bit of the "containing object" to the highest-order bit of
21665 the bit-field itself. Since the "high-order end" of any object or field
21666 is different on big-endian and little-endian machines, the computation
21667 below must take account of these differences. */
21668 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21669 highest_order_field_bit_offset = bitpos_int;
21671 if (! BYTES_BIG_ENDIAN)
21673 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21674 highest_order_object_bit_offset +=
21675 simple_type_size_in_bits (original_type);
21678 bit_offset
21679 = (! BYTES_BIG_ENDIAN
21680 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21681 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21683 if (bit_offset < 0)
21684 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21685 else
21686 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21689 /* For a FIELD_DECL node which represents a bit field, output an attribute
21690 which specifies the length in bits of the given field. */
21692 static inline void
21693 add_bit_size_attribute (dw_die_ref die, tree decl)
21695 /* Must be a field and a bit field. */
21696 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21697 && DECL_BIT_FIELD_TYPE (decl));
21699 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21700 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21703 /* If the compiled language is ANSI C, then add a 'prototyped'
21704 attribute, if arg types are given for the parameters of a function. */
21706 static inline void
21707 add_prototyped_attribute (dw_die_ref die, tree func_type)
21709 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21711 case DW_LANG_C:
21712 case DW_LANG_C89:
21713 case DW_LANG_C99:
21714 case DW_LANG_C11:
21715 case DW_LANG_ObjC:
21716 if (prototype_p (func_type))
21717 add_AT_flag (die, DW_AT_prototyped, 1);
21718 break;
21719 default:
21720 break;
21724 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21725 by looking in the type declaration, the object declaration equate table or
21726 the block mapping. */
21728 static inline void
21729 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21731 dw_die_ref origin_die = NULL;
21733 /* For late LTO debug output we want to refer directly to the abstract
21734 DIE in the early debug rather to the possibly existing concrete
21735 instance and avoid creating that just for this purpose. */
21736 sym_off_pair *desc;
21737 if (in_lto_p
21738 && external_die_map
21739 && (desc = external_die_map->get (origin)))
21741 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21742 desc->sym, desc->off);
21743 return;
21746 if (DECL_P (origin))
21747 origin_die = lookup_decl_die (origin);
21748 else if (TYPE_P (origin))
21749 origin_die = lookup_type_die (origin);
21750 else if (TREE_CODE (origin) == BLOCK)
21751 origin_die = lookup_block_die (origin);
21753 /* XXX: Functions that are never lowered don't always have correct block
21754 trees (in the case of java, they simply have no block tree, in some other
21755 languages). For these functions, there is nothing we can really do to
21756 output correct debug info for inlined functions in all cases. Rather
21757 than die, we'll just produce deficient debug info now, in that we will
21758 have variables without a proper abstract origin. In the future, when all
21759 functions are lowered, we should re-add a gcc_assert (origin_die)
21760 here. */
21762 if (origin_die)
21764 dw_attr_node *a;
21765 /* Like above, if we already created a concrete instance DIE
21766 do not use that for the abstract origin but the early DIE
21767 if present. */
21768 if (in_lto_p
21769 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
21770 origin_die = AT_ref (a);
21771 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21775 /* We do not currently support the pure_virtual attribute. */
21777 static inline void
21778 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21780 if (DECL_VINDEX (func_decl))
21782 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21784 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21785 add_AT_loc (die, DW_AT_vtable_elem_location,
21786 new_loc_descr (DW_OP_constu,
21787 tree_to_shwi (DECL_VINDEX (func_decl)),
21788 0));
21790 /* GNU extension: Record what type this method came from originally. */
21791 if (debug_info_level > DINFO_LEVEL_TERSE
21792 && DECL_CONTEXT (func_decl))
21793 add_AT_die_ref (die, DW_AT_containing_type,
21794 lookup_type_die (DECL_CONTEXT (func_decl)));
21798 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21799 given decl. This used to be a vendor extension until after DWARF 4
21800 standardized it. */
21802 static void
21803 add_linkage_attr (dw_die_ref die, tree decl)
21805 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21807 /* Mimic what assemble_name_raw does with a leading '*'. */
21808 if (name[0] == '*')
21809 name = &name[1];
21811 if (dwarf_version >= 4)
21812 add_AT_string (die, DW_AT_linkage_name, name);
21813 else
21814 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21817 /* Add source coordinate attributes for the given decl. */
21819 static void
21820 add_src_coords_attributes (dw_die_ref die, tree decl)
21822 expanded_location s;
21824 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21825 return;
21826 s = expand_location (DECL_SOURCE_LOCATION (decl));
21827 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21828 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21829 if (debug_column_info && s.column)
21830 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21833 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21835 static void
21836 add_linkage_name_raw (dw_die_ref die, tree decl)
21838 /* Defer until we have an assembler name set. */
21839 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21841 limbo_die_node *asm_name;
21843 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21844 asm_name->die = die;
21845 asm_name->created_for = decl;
21846 asm_name->next = deferred_asm_name;
21847 deferred_asm_name = asm_name;
21849 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21850 add_linkage_attr (die, decl);
21853 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21855 static void
21856 add_linkage_name (dw_die_ref die, tree decl)
21858 if (debug_info_level > DINFO_LEVEL_NONE
21859 && VAR_OR_FUNCTION_DECL_P (decl)
21860 && TREE_PUBLIC (decl)
21861 && !(VAR_P (decl) && DECL_REGISTER (decl))
21862 && die->die_tag != DW_TAG_member)
21863 add_linkage_name_raw (die, decl);
21866 /* Add a DW_AT_name attribute and source coordinate attribute for the
21867 given decl, but only if it actually has a name. */
21869 static void
21870 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21871 bool no_linkage_name)
21873 tree decl_name;
21875 decl_name = DECL_NAME (decl);
21876 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21878 const char *name = dwarf2_name (decl, 0);
21879 if (name)
21880 add_name_attribute (die, name);
21881 else
21882 add_desc_attribute (die, decl);
21884 if (! DECL_ARTIFICIAL (decl))
21885 add_src_coords_attributes (die, decl);
21887 if (!no_linkage_name)
21888 add_linkage_name (die, decl);
21890 else
21891 add_desc_attribute (die, decl);
21893 #ifdef VMS_DEBUGGING_INFO
21894 /* Get the function's name, as described by its RTL. This may be different
21895 from the DECL_NAME name used in the source file. */
21896 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21898 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21899 XEXP (DECL_RTL (decl), 0), false);
21900 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21902 #endif /* VMS_DEBUGGING_INFO */
21905 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21907 static void
21908 add_discr_value (dw_die_ref die, dw_discr_value *value)
21910 dw_attr_node attr;
21912 attr.dw_attr = DW_AT_discr_value;
21913 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21914 attr.dw_attr_val.val_entry = NULL;
21915 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21916 if (value->pos)
21917 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21918 else
21919 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21920 add_dwarf_attr (die, &attr);
21923 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21925 static void
21926 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21928 dw_attr_node attr;
21930 attr.dw_attr = DW_AT_discr_list;
21931 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21932 attr.dw_attr_val.val_entry = NULL;
21933 attr.dw_attr_val.v.val_discr_list = discr_list;
21934 add_dwarf_attr (die, &attr);
21937 static inline dw_discr_list_ref
21938 AT_discr_list (dw_attr_node *attr)
21940 return attr->dw_attr_val.v.val_discr_list;
21943 #ifdef VMS_DEBUGGING_INFO
21944 /* Output the debug main pointer die for VMS */
21946 void
21947 dwarf2out_vms_debug_main_pointer (void)
21949 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21950 dw_die_ref die;
21952 /* Allocate the VMS debug main subprogram die. */
21953 die = new_die_raw (DW_TAG_subprogram);
21954 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21955 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21956 current_function_funcdef_no);
21957 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21959 /* Make it the first child of comp_unit_die (). */
21960 die->die_parent = comp_unit_die ();
21961 if (comp_unit_die ()->die_child)
21963 die->die_sib = comp_unit_die ()->die_child->die_sib;
21964 comp_unit_die ()->die_child->die_sib = die;
21966 else
21968 die->die_sib = die;
21969 comp_unit_die ()->die_child = die;
21972 #endif /* VMS_DEBUGGING_INFO */
21974 /* walk_tree helper function for uses_local_type, below. */
21976 static tree
21977 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21979 if (!TYPE_P (*tp))
21980 *walk_subtrees = 0;
21981 else
21983 tree name = TYPE_NAME (*tp);
21984 if (name && DECL_P (name) && decl_function_context (name))
21985 return *tp;
21987 return NULL_TREE;
21990 /* If TYPE involves a function-local type (including a local typedef to a
21991 non-local type), returns that type; otherwise returns NULL_TREE. */
21993 static tree
21994 uses_local_type (tree type)
21996 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21997 return used;
22000 /* Return the DIE for the scope that immediately contains this type.
22001 Non-named types that do not involve a function-local type get global
22002 scope. Named types nested in namespaces or other types get their
22003 containing scope. All other types (i.e. function-local named types) get
22004 the current active scope. */
22006 static dw_die_ref
22007 scope_die_for (tree t, dw_die_ref context_die)
22009 dw_die_ref scope_die = NULL;
22010 tree containing_scope;
22012 /* Non-types always go in the current scope. */
22013 gcc_assert (TYPE_P (t));
22015 /* Use the scope of the typedef, rather than the scope of the type
22016 it refers to. */
22017 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22018 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22019 else
22020 containing_scope = TYPE_CONTEXT (t);
22022 /* Use the containing namespace if there is one. */
22023 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22025 if (context_die == lookup_decl_die (containing_scope))
22026 /* OK */;
22027 else if (debug_info_level > DINFO_LEVEL_TERSE)
22028 context_die = get_context_die (containing_scope);
22029 else
22030 containing_scope = NULL_TREE;
22033 /* Ignore function type "scopes" from the C frontend. They mean that
22034 a tagged type is local to a parmlist of a function declarator, but
22035 that isn't useful to DWARF. */
22036 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22037 containing_scope = NULL_TREE;
22039 if (SCOPE_FILE_SCOPE_P (containing_scope))
22041 /* If T uses a local type keep it local as well, to avoid references
22042 to function-local DIEs from outside the function. */
22043 if (current_function_decl && uses_local_type (t))
22044 scope_die = context_die;
22045 else
22046 scope_die = comp_unit_die ();
22048 else if (TYPE_P (containing_scope))
22050 /* For types, we can just look up the appropriate DIE. */
22051 if (debug_info_level > DINFO_LEVEL_TERSE)
22052 scope_die = get_context_die (containing_scope);
22053 else
22055 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22056 if (scope_die == NULL)
22057 scope_die = comp_unit_die ();
22060 else
22061 scope_die = context_die;
22063 return scope_die;
22066 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
22068 static inline int
22069 local_scope_p (dw_die_ref context_die)
22071 for (; context_die; context_die = context_die->die_parent)
22072 if (context_die->die_tag == DW_TAG_inlined_subroutine
22073 || context_die->die_tag == DW_TAG_subprogram)
22074 return 1;
22076 return 0;
22079 /* Returns nonzero if CONTEXT_DIE is a class. */
22081 static inline int
22082 class_scope_p (dw_die_ref context_die)
22084 return (context_die
22085 && (context_die->die_tag == DW_TAG_structure_type
22086 || context_die->die_tag == DW_TAG_class_type
22087 || context_die->die_tag == DW_TAG_interface_type
22088 || context_die->die_tag == DW_TAG_union_type));
22091 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
22092 whether or not to treat a DIE in this context as a declaration. */
22094 static inline int
22095 class_or_namespace_scope_p (dw_die_ref context_die)
22097 return (class_scope_p (context_die)
22098 || (context_die && context_die->die_tag == DW_TAG_namespace));
22101 /* Many forms of DIEs require a "type description" attribute. This
22102 routine locates the proper "type descriptor" die for the type given
22103 by 'type' plus any additional qualifiers given by 'cv_quals', and
22104 adds a DW_AT_type attribute below the given die. */
22106 static void
22107 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22108 bool reverse, dw_die_ref context_die)
22110 enum tree_code code = TREE_CODE (type);
22111 dw_die_ref type_die = NULL;
22113 if (debug_info_level <= DINFO_LEVEL_TERSE)
22114 return;
22116 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22117 or fixed-point type, use the inner type. This is because we have no
22118 support for unnamed types in base_type_die. This can happen if this is
22119 an Ada subrange type. Correct solution is emit a subrange type die. */
22120 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22121 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22122 type = TREE_TYPE (type), code = TREE_CODE (type);
22124 if (code == ERROR_MARK
22125 /* Handle a special case. For functions whose return type is void, we
22126 generate *no* type attribute. (Note that no object may have type
22127 `void', so this only applies to function return types). */
22128 || code == VOID_TYPE)
22129 return;
22131 type_die = modified_type_die (type,
22132 cv_quals | TYPE_QUALS (type),
22133 reverse,
22134 context_die);
22136 if (type_die != NULL)
22137 add_AT_die_ref (object_die, DW_AT_type, type_die);
22140 /* Given an object die, add the calling convention attribute for the
22141 function call type. */
22142 static void
22143 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22145 enum dwarf_calling_convention value = DW_CC_normal;
22147 value = ((enum dwarf_calling_convention)
22148 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22150 if (is_fortran ()
22151 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22153 /* DWARF 2 doesn't provide a way to identify a program's source-level
22154 entry point. DW_AT_calling_convention attributes are only meant
22155 to describe functions' calling conventions. However, lacking a
22156 better way to signal the Fortran main program, we used this for
22157 a long time, following existing custom. Now, DWARF 4 has
22158 DW_AT_main_subprogram, which we add below, but some tools still
22159 rely on the old way, which we thus keep. */
22160 value = DW_CC_program;
22162 if (dwarf_version >= 4 || !dwarf_strict)
22163 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22166 /* Only add the attribute if the backend requests it, and
22167 is not DW_CC_normal. */
22168 if (value && (value != DW_CC_normal))
22169 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22172 /* Given a tree pointer to a struct, class, union, or enum type node, return
22173 a pointer to the (string) tag name for the given type, or zero if the type
22174 was declared without a tag. */
22176 static const char *
22177 type_tag (const_tree type)
22179 const char *name = 0;
22181 if (TYPE_NAME (type) != 0)
22183 tree t = 0;
22185 /* Find the IDENTIFIER_NODE for the type name. */
22186 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22187 && !TYPE_NAMELESS (type))
22188 t = TYPE_NAME (type);
22190 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22191 a TYPE_DECL node, regardless of whether or not a `typedef' was
22192 involved. */
22193 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22194 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22196 /* We want to be extra verbose. Don't call dwarf_name if
22197 DECL_NAME isn't set. The default hook for decl_printable_name
22198 doesn't like that, and in this context it's correct to return
22199 0, instead of "<anonymous>" or the like. */
22200 if (DECL_NAME (TYPE_NAME (type))
22201 && !DECL_NAMELESS (TYPE_NAME (type)))
22202 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22205 /* Now get the name as a string, or invent one. */
22206 if (!name && t != 0)
22207 name = IDENTIFIER_POINTER (t);
22210 return (name == 0 || *name == '\0') ? 0 : name;
22213 /* Return the type associated with a data member, make a special check
22214 for bit field types. */
22216 static inline tree
22217 member_declared_type (const_tree member)
22219 return (DECL_BIT_FIELD_TYPE (member)
22220 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22223 /* Get the decl's label, as described by its RTL. This may be different
22224 from the DECL_NAME name used in the source file. */
22226 #if 0
22227 static const char *
22228 decl_start_label (tree decl)
22230 rtx x;
22231 const char *fnname;
22233 x = DECL_RTL (decl);
22234 gcc_assert (MEM_P (x));
22236 x = XEXP (x, 0);
22237 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22239 fnname = XSTR (x, 0);
22240 return fnname;
22242 #endif
22244 /* For variable-length arrays that have been previously generated, but
22245 may be incomplete due to missing subscript info, fill the subscript
22246 info. Return TRUE if this is one of those cases. */
22248 static bool
22249 fill_variable_array_bounds (tree type)
22251 if (TREE_ASM_WRITTEN (type)
22252 && TREE_CODE (type) == ARRAY_TYPE
22253 && variably_modified_type_p (type, NULL))
22255 dw_die_ref array_die = lookup_type_die (type);
22256 if (!array_die)
22257 return false;
22258 add_subscript_info (array_die, type, !is_ada ());
22259 return true;
22261 return false;
22264 /* These routines generate the internal representation of the DIE's for
22265 the compilation unit. Debugging information is collected by walking
22266 the declaration trees passed in from dwarf2out_decl(). */
22268 static void
22269 gen_array_type_die (tree type, dw_die_ref context_die)
22271 dw_die_ref array_die;
22273 /* GNU compilers represent multidimensional array types as sequences of one
22274 dimensional array types whose element types are themselves array types.
22275 We sometimes squish that down to a single array_type DIE with multiple
22276 subscripts in the Dwarf debugging info. The draft Dwarf specification
22277 say that we are allowed to do this kind of compression in C, because
22278 there is no difference between an array of arrays and a multidimensional
22279 array. We don't do this for Ada to remain as close as possible to the
22280 actual representation, which is especially important against the language
22281 flexibilty wrt arrays of variable size. */
22283 bool collapse_nested_arrays = !is_ada ();
22285 if (fill_variable_array_bounds (type))
22286 return;
22288 dw_die_ref scope_die = scope_die_for (type, context_die);
22289 tree element_type;
22291 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22292 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22293 if (TREE_CODE (type) == ARRAY_TYPE
22294 && TYPE_STRING_FLAG (type)
22295 && is_fortran ()
22296 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22298 HOST_WIDE_INT size;
22300 array_die = new_die (DW_TAG_string_type, scope_die, type);
22301 add_name_attribute (array_die, type_tag (type));
22302 equate_type_number_to_die (type, array_die);
22303 size = int_size_in_bytes (type);
22304 if (size >= 0)
22305 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22306 /* ??? We can't annotate types late, but for LTO we may not
22307 generate a location early either (gfortran.dg/save_6.f90). */
22308 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22309 && TYPE_DOMAIN (type) != NULL_TREE
22310 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22312 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22313 tree rszdecl = szdecl;
22315 size = int_size_in_bytes (TREE_TYPE (szdecl));
22316 if (!DECL_P (szdecl))
22318 if (TREE_CODE (szdecl) == INDIRECT_REF
22319 && DECL_P (TREE_OPERAND (szdecl, 0)))
22321 rszdecl = TREE_OPERAND (szdecl, 0);
22322 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22323 != DWARF2_ADDR_SIZE)
22324 size = 0;
22326 else
22327 size = 0;
22329 if (size > 0)
22331 dw_loc_list_ref loc
22332 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22333 NULL);
22334 if (loc)
22336 add_AT_location_description (array_die, DW_AT_string_length,
22337 loc);
22338 if (size != DWARF2_ADDR_SIZE)
22339 add_AT_unsigned (array_die, dwarf_version >= 5
22340 ? DW_AT_string_length_byte_size
22341 : DW_AT_byte_size, size);
22345 return;
22348 array_die = new_die (DW_TAG_array_type, scope_die, type);
22349 add_name_attribute (array_die, type_tag (type));
22350 equate_type_number_to_die (type, array_die);
22352 if (TREE_CODE (type) == VECTOR_TYPE)
22353 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22355 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22356 if (is_fortran ()
22357 && TREE_CODE (type) == ARRAY_TYPE
22358 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22359 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22360 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22362 #if 0
22363 /* We default the array ordering. Debuggers will probably do the right
22364 things even if DW_AT_ordering is not present. It's not even an issue
22365 until we start to get into multidimensional arrays anyway. If a debugger
22366 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22367 then we'll have to put the DW_AT_ordering attribute back in. (But if
22368 and when we find out that we need to put these in, we will only do so
22369 for multidimensional arrays. */
22370 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22371 #endif
22373 if (TREE_CODE (type) == VECTOR_TYPE)
22375 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
22376 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22377 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
22378 add_bound_info (subrange_die, DW_AT_upper_bound,
22379 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22381 else
22382 add_subscript_info (array_die, type, collapse_nested_arrays);
22384 /* Add representation of the type of the elements of this array type and
22385 emit the corresponding DIE if we haven't done it already. */
22386 element_type = TREE_TYPE (type);
22387 if (collapse_nested_arrays)
22388 while (TREE_CODE (element_type) == ARRAY_TYPE)
22390 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22391 break;
22392 element_type = TREE_TYPE (element_type);
22395 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22396 TREE_CODE (type) == ARRAY_TYPE
22397 && TYPE_REVERSE_STORAGE_ORDER (type),
22398 context_die);
22400 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22401 if (TYPE_ARTIFICIAL (type))
22402 add_AT_flag (array_die, DW_AT_artificial, 1);
22404 if (get_AT (array_die, DW_AT_name))
22405 add_pubtype (type, array_die);
22407 add_alignment_attribute (array_die, type);
22410 /* This routine generates DIE for array with hidden descriptor, details
22411 are filled into *info by a langhook. */
22413 static void
22414 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22415 dw_die_ref context_die)
22417 const dw_die_ref scope_die = scope_die_for (type, context_die);
22418 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22419 struct loc_descr_context context = {
22420 type, /* context_type */
22421 info->base_decl, /* base_decl */
22422 NULL, /* dpi */
22423 false, /* placeholder_arg */
22424 false, /* placeholder_seen */
22425 false /* strict_signedness */
22427 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22428 int dim;
22430 add_name_attribute (array_die, type_tag (type));
22431 equate_type_number_to_die (type, array_die);
22433 if (info->ndimensions > 1)
22434 switch (info->ordering)
22436 case array_descr_ordering_row_major:
22437 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22438 break;
22439 case array_descr_ordering_column_major:
22440 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22441 break;
22442 default:
22443 break;
22446 if (dwarf_version >= 3 || !dwarf_strict)
22448 if (info->data_location)
22449 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22450 dw_scalar_form_exprloc, &context);
22451 if (info->associated)
22452 add_scalar_info (array_die, DW_AT_associated, info->associated,
22453 dw_scalar_form_constant
22454 | dw_scalar_form_exprloc
22455 | dw_scalar_form_reference, &context);
22456 if (info->allocated)
22457 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22458 dw_scalar_form_constant
22459 | dw_scalar_form_exprloc
22460 | dw_scalar_form_reference, &context);
22461 if (info->stride)
22463 const enum dwarf_attribute attr
22464 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22465 const int forms
22466 = (info->stride_in_bits)
22467 ? dw_scalar_form_constant
22468 : (dw_scalar_form_constant
22469 | dw_scalar_form_exprloc
22470 | dw_scalar_form_reference);
22472 add_scalar_info (array_die, attr, info->stride, forms, &context);
22475 if (dwarf_version >= 5)
22477 if (info->rank)
22479 add_scalar_info (array_die, DW_AT_rank, info->rank,
22480 dw_scalar_form_constant
22481 | dw_scalar_form_exprloc, &context);
22482 subrange_tag = DW_TAG_generic_subrange;
22483 context.placeholder_arg = true;
22487 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22489 for (dim = 0; dim < info->ndimensions; dim++)
22491 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22493 if (info->dimen[dim].bounds_type)
22494 add_type_attribute (subrange_die,
22495 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22496 false, context_die);
22497 if (info->dimen[dim].lower_bound)
22498 add_bound_info (subrange_die, DW_AT_lower_bound,
22499 info->dimen[dim].lower_bound, &context);
22500 if (info->dimen[dim].upper_bound)
22501 add_bound_info (subrange_die, DW_AT_upper_bound,
22502 info->dimen[dim].upper_bound, &context);
22503 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22504 add_scalar_info (subrange_die, DW_AT_byte_stride,
22505 info->dimen[dim].stride,
22506 dw_scalar_form_constant
22507 | dw_scalar_form_exprloc
22508 | dw_scalar_form_reference,
22509 &context);
22512 gen_type_die (info->element_type, context_die);
22513 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22514 TREE_CODE (type) == ARRAY_TYPE
22515 && TYPE_REVERSE_STORAGE_ORDER (type),
22516 context_die);
22518 if (get_AT (array_die, DW_AT_name))
22519 add_pubtype (type, array_die);
22521 add_alignment_attribute (array_die, type);
22524 #if 0
22525 static void
22526 gen_entry_point_die (tree decl, dw_die_ref context_die)
22528 tree origin = decl_ultimate_origin (decl);
22529 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22531 if (origin != NULL)
22532 add_abstract_origin_attribute (decl_die, origin);
22533 else
22535 add_name_and_src_coords_attributes (decl_die, decl);
22536 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22537 TYPE_UNQUALIFIED, false, context_die);
22540 if (DECL_ABSTRACT_P (decl))
22541 equate_decl_number_to_die (decl, decl_die);
22542 else
22543 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22545 #endif
22547 /* Walk through the list of incomplete types again, trying once more to
22548 emit full debugging info for them. */
22550 static void
22551 retry_incomplete_types (void)
22553 set_early_dwarf s;
22554 int i;
22556 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22557 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22558 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22559 vec_safe_truncate (incomplete_types, 0);
22562 /* Determine what tag to use for a record type. */
22564 static enum dwarf_tag
22565 record_type_tag (tree type)
22567 if (! lang_hooks.types.classify_record)
22568 return DW_TAG_structure_type;
22570 switch (lang_hooks.types.classify_record (type))
22572 case RECORD_IS_STRUCT:
22573 return DW_TAG_structure_type;
22575 case RECORD_IS_CLASS:
22576 return DW_TAG_class_type;
22578 case RECORD_IS_INTERFACE:
22579 if (dwarf_version >= 3 || !dwarf_strict)
22580 return DW_TAG_interface_type;
22581 return DW_TAG_structure_type;
22583 default:
22584 gcc_unreachable ();
22588 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22589 include all of the information about the enumeration values also. Each
22590 enumerated type name/value is listed as a child of the enumerated type
22591 DIE. */
22593 static dw_die_ref
22594 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22596 dw_die_ref type_die = lookup_type_die (type);
22597 dw_die_ref orig_type_die = type_die;
22599 if (type_die == NULL)
22601 type_die = new_die (DW_TAG_enumeration_type,
22602 scope_die_for (type, context_die), type);
22603 equate_type_number_to_die (type, type_die);
22604 add_name_attribute (type_die, type_tag (type));
22605 if ((dwarf_version >= 4 || !dwarf_strict)
22606 && ENUM_IS_SCOPED (type))
22607 add_AT_flag (type_die, DW_AT_enum_class, 1);
22608 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22609 add_AT_flag (type_die, DW_AT_declaration, 1);
22610 if (!dwarf_strict)
22611 add_AT_unsigned (type_die, DW_AT_encoding,
22612 TYPE_UNSIGNED (type)
22613 ? DW_ATE_unsigned
22614 : DW_ATE_signed);
22616 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22617 return type_die;
22618 else
22619 remove_AT (type_die, DW_AT_declaration);
22621 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22622 given enum type is incomplete, do not generate the DW_AT_byte_size
22623 attribute or the DW_AT_element_list attribute. */
22624 if (TYPE_SIZE (type))
22626 tree link;
22628 if (!ENUM_IS_OPAQUE (type))
22629 TREE_ASM_WRITTEN (type) = 1;
22630 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22631 add_byte_size_attribute (type_die, type);
22632 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22633 add_alignment_attribute (type_die, type);
22634 if ((dwarf_version >= 3 || !dwarf_strict)
22635 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22637 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22638 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22639 context_die);
22641 if (TYPE_STUB_DECL (type) != NULL_TREE)
22643 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22644 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22645 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22646 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22649 /* If the first reference to this type was as the return type of an
22650 inline function, then it may not have a parent. Fix this now. */
22651 if (type_die->die_parent == NULL)
22652 add_child_die (scope_die_for (type, context_die), type_die);
22654 for (link = TYPE_VALUES (type);
22655 link != NULL; link = TREE_CHAIN (link))
22657 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22658 tree value = TREE_VALUE (link);
22660 if (DECL_P (value))
22661 equate_decl_number_to_die (value, enum_die);
22663 gcc_assert (!ENUM_IS_OPAQUE (type));
22664 add_name_attribute (enum_die,
22665 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22667 if (TREE_CODE (value) == CONST_DECL)
22668 value = DECL_INITIAL (value);
22670 if (simple_type_size_in_bits (TREE_TYPE (value))
22671 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22673 /* For constant forms created by add_AT_unsigned DWARF
22674 consumers (GDB, elfutils, etc.) always zero extend
22675 the value. Only when the actual value is negative
22676 do we need to use add_AT_int to generate a constant
22677 form that can represent negative values. */
22678 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22679 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22680 add_AT_unsigned (enum_die, DW_AT_const_value,
22681 (unsigned HOST_WIDE_INT) val);
22682 else
22683 add_AT_int (enum_die, DW_AT_const_value, val);
22685 else
22686 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22687 that here. TODO: This should be re-worked to use correct
22688 signed/unsigned double tags for all cases. */
22689 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22692 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22693 if (TYPE_ARTIFICIAL (type)
22694 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22695 add_AT_flag (type_die, DW_AT_artificial, 1);
22697 else
22698 add_AT_flag (type_die, DW_AT_declaration, 1);
22700 add_pubtype (type, type_die);
22702 return type_die;
22705 /* Generate a DIE to represent either a real live formal parameter decl or to
22706 represent just the type of some formal parameter position in some function
22707 type.
22709 Note that this routine is a bit unusual because its argument may be a
22710 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22711 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22712 node. If it's the former then this function is being called to output a
22713 DIE to represent a formal parameter object (or some inlining thereof). If
22714 it's the latter, then this function is only being called to output a
22715 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22716 argument type of some subprogram type.
22717 If EMIT_NAME_P is true, name and source coordinate attributes
22718 are emitted. */
22720 static dw_die_ref
22721 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22722 dw_die_ref context_die)
22724 tree node_or_origin = node ? node : origin;
22725 tree ultimate_origin;
22726 dw_die_ref parm_die = NULL;
22728 if (DECL_P (node_or_origin))
22730 parm_die = lookup_decl_die (node);
22732 /* If the contexts differ, we may not be talking about the same
22733 thing.
22734 ??? When in LTO the DIE parent is the "abstract" copy and the
22735 context_die is the specification "copy". */
22736 if (parm_die
22737 && parm_die->die_parent != context_die
22738 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22739 || parm_die->die_parent->die_parent != context_die)
22740 && !in_lto_p)
22742 gcc_assert (!DECL_ABSTRACT_P (node));
22743 /* This can happen when creating a concrete instance, in
22744 which case we need to create a new DIE that will get
22745 annotated with DW_AT_abstract_origin. */
22746 parm_die = NULL;
22749 if (parm_die && parm_die->die_parent == NULL)
22751 /* Check that parm_die already has the right attributes that
22752 we would have added below. If any attributes are
22753 missing, fall through to add them. */
22754 if (! DECL_ABSTRACT_P (node_or_origin)
22755 && !get_AT (parm_die, DW_AT_location)
22756 && !get_AT (parm_die, DW_AT_const_value))
22757 /* We are missing location info, and are about to add it. */
22759 else
22761 add_child_die (context_die, parm_die);
22762 return parm_die;
22767 /* If we have a previously generated DIE, use it, unless this is an
22768 concrete instance (origin != NULL), in which case we need a new
22769 DIE with a corresponding DW_AT_abstract_origin. */
22770 bool reusing_die;
22771 if (parm_die && origin == NULL)
22772 reusing_die = true;
22773 else
22775 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22776 reusing_die = false;
22779 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22781 case tcc_declaration:
22782 ultimate_origin = decl_ultimate_origin (node_or_origin);
22783 if (node || ultimate_origin)
22784 origin = ultimate_origin;
22786 if (reusing_die)
22787 goto add_location;
22789 if (origin != NULL)
22790 add_abstract_origin_attribute (parm_die, origin);
22791 else if (emit_name_p)
22792 add_name_and_src_coords_attributes (parm_die, node);
22793 if (origin == NULL
22794 || (! DECL_ABSTRACT_P (node_or_origin)
22795 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22796 decl_function_context
22797 (node_or_origin))))
22799 tree type = TREE_TYPE (node_or_origin);
22800 if (decl_by_reference_p (node_or_origin))
22801 add_type_attribute (parm_die, TREE_TYPE (type),
22802 TYPE_UNQUALIFIED,
22803 false, context_die);
22804 else
22805 add_type_attribute (parm_die, type,
22806 decl_quals (node_or_origin),
22807 false, context_die);
22809 if (origin == NULL && DECL_ARTIFICIAL (node))
22810 add_AT_flag (parm_die, DW_AT_artificial, 1);
22811 add_location:
22812 if (node && node != origin)
22813 equate_decl_number_to_die (node, parm_die);
22814 if (! DECL_ABSTRACT_P (node_or_origin))
22815 add_location_or_const_value_attribute (parm_die, node_or_origin,
22816 node == NULL);
22818 break;
22820 case tcc_type:
22821 /* We were called with some kind of a ..._TYPE node. */
22822 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22823 context_die);
22824 break;
22826 default:
22827 gcc_unreachable ();
22830 return parm_die;
22833 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22834 children DW_TAG_formal_parameter DIEs representing the arguments of the
22835 parameter pack.
22837 PARM_PACK must be a function parameter pack.
22838 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22839 must point to the subsequent arguments of the function PACK_ARG belongs to.
22840 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22841 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22842 following the last one for which a DIE was generated. */
22844 static dw_die_ref
22845 gen_formal_parameter_pack_die (tree parm_pack,
22846 tree pack_arg,
22847 dw_die_ref subr_die,
22848 tree *next_arg)
22850 tree arg;
22851 dw_die_ref parm_pack_die;
22853 gcc_assert (parm_pack
22854 && lang_hooks.function_parameter_pack_p (parm_pack)
22855 && subr_die);
22857 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22858 add_src_coords_attributes (parm_pack_die, parm_pack);
22860 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22862 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22863 parm_pack))
22864 break;
22865 gen_formal_parameter_die (arg, NULL,
22866 false /* Don't emit name attribute. */,
22867 parm_pack_die);
22869 if (next_arg)
22870 *next_arg = arg;
22871 return parm_pack_die;
22874 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22875 at the end of an (ANSI prototyped) formal parameters list. */
22877 static void
22878 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22880 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22883 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22884 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22885 parameters as specified in some function type specification (except for
22886 those which appear as part of a function *definition*). */
22888 static void
22889 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22891 tree link;
22892 tree formal_type = NULL;
22893 tree first_parm_type;
22894 tree arg;
22896 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22898 arg = DECL_ARGUMENTS (function_or_method_type);
22899 function_or_method_type = TREE_TYPE (function_or_method_type);
22901 else
22902 arg = NULL_TREE;
22904 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22906 /* Make our first pass over the list of formal parameter types and output a
22907 DW_TAG_formal_parameter DIE for each one. */
22908 for (link = first_parm_type; link; )
22910 dw_die_ref parm_die;
22912 formal_type = TREE_VALUE (link);
22913 if (formal_type == void_type_node)
22914 break;
22916 /* Output a (nameless) DIE to represent the formal parameter itself. */
22917 parm_die = gen_formal_parameter_die (formal_type, NULL,
22918 true /* Emit name attribute. */,
22919 context_die);
22920 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22921 && link == first_parm_type)
22923 add_AT_flag (parm_die, DW_AT_artificial, 1);
22924 if (dwarf_version >= 3 || !dwarf_strict)
22925 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22927 else if (arg && DECL_ARTIFICIAL (arg))
22928 add_AT_flag (parm_die, DW_AT_artificial, 1);
22930 link = TREE_CHAIN (link);
22931 if (arg)
22932 arg = DECL_CHAIN (arg);
22935 /* If this function type has an ellipsis, add a
22936 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22937 if (formal_type != void_type_node)
22938 gen_unspecified_parameters_die (function_or_method_type, context_die);
22940 /* Make our second (and final) pass over the list of formal parameter types
22941 and output DIEs to represent those types (as necessary). */
22942 for (link = TYPE_ARG_TYPES (function_or_method_type);
22943 link && TREE_VALUE (link);
22944 link = TREE_CHAIN (link))
22945 gen_type_die (TREE_VALUE (link), context_die);
22948 /* We want to generate the DIE for TYPE so that we can generate the
22949 die for MEMBER, which has been defined; we will need to refer back
22950 to the member declaration nested within TYPE. If we're trying to
22951 generate minimal debug info for TYPE, processing TYPE won't do the
22952 trick; we need to attach the member declaration by hand. */
22954 static void
22955 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22957 gen_type_die (type, context_die);
22959 /* If we're trying to avoid duplicate debug info, we may not have
22960 emitted the member decl for this function. Emit it now. */
22961 if (TYPE_STUB_DECL (type)
22962 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22963 && ! lookup_decl_die (member))
22965 dw_die_ref type_die;
22966 gcc_assert (!decl_ultimate_origin (member));
22968 type_die = lookup_type_die_strip_naming_typedef (type);
22969 if (TREE_CODE (member) == FUNCTION_DECL)
22970 gen_subprogram_die (member, type_die);
22971 else if (TREE_CODE (member) == FIELD_DECL)
22973 /* Ignore the nameless fields that are used to skip bits but handle
22974 C++ anonymous unions and structs. */
22975 if (DECL_NAME (member) != NULL_TREE
22976 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22977 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22979 struct vlr_context vlr_ctx = {
22980 DECL_CONTEXT (member), /* struct_type */
22981 NULL_TREE /* variant_part_offset */
22983 gen_type_die (member_declared_type (member), type_die);
22984 gen_field_die (member, &vlr_ctx, type_die);
22987 else
22988 gen_variable_die (member, NULL_TREE, type_die);
22992 /* Forward declare these functions, because they are mutually recursive
22993 with their set_block_* pairing functions. */
22994 static void set_decl_origin_self (tree);
22996 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22997 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22998 that it points to the node itself, thus indicating that the node is its
22999 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23000 the given node is NULL, recursively descend the decl/block tree which
23001 it is the root of, and for each other ..._DECL or BLOCK node contained
23002 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23003 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23004 values to point to themselves. */
23006 static void
23007 set_block_origin_self (tree stmt)
23009 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23011 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23014 tree local_decl;
23016 for (local_decl = BLOCK_VARS (stmt);
23017 local_decl != NULL_TREE;
23018 local_decl = DECL_CHAIN (local_decl))
23019 /* Do not recurse on nested functions since the inlining status
23020 of parent and child can be different as per the DWARF spec. */
23021 if (TREE_CODE (local_decl) != FUNCTION_DECL
23022 && !DECL_EXTERNAL (local_decl))
23023 set_decl_origin_self (local_decl);
23027 tree subblock;
23029 for (subblock = BLOCK_SUBBLOCKS (stmt);
23030 subblock != NULL_TREE;
23031 subblock = BLOCK_CHAIN (subblock))
23032 set_block_origin_self (subblock); /* Recurse. */
23037 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23038 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23039 node to so that it points to the node itself, thus indicating that the
23040 node represents its own (abstract) origin. Additionally, if the
23041 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23042 the decl/block tree of which the given node is the root of, and for
23043 each other ..._DECL or BLOCK node contained therein whose
23044 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23045 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23046 point to themselves. */
23048 static void
23049 set_decl_origin_self (tree decl)
23051 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23053 DECL_ABSTRACT_ORIGIN (decl) = decl;
23054 if (TREE_CODE (decl) == FUNCTION_DECL)
23056 tree arg;
23058 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23059 DECL_ABSTRACT_ORIGIN (arg) = arg;
23060 if (DECL_INITIAL (decl) != NULL_TREE
23061 && DECL_INITIAL (decl) != error_mark_node)
23062 set_block_origin_self (DECL_INITIAL (decl));
23067 /* Mark the early DIE for DECL as the abstract instance. */
23069 static void
23070 dwarf2out_abstract_function (tree decl)
23072 dw_die_ref old_die;
23074 /* Make sure we have the actual abstract inline, not a clone. */
23075 decl = DECL_ORIGIN (decl);
23077 if (DECL_IGNORED_P (decl))
23078 return;
23080 /* In LTO we're all set. We already created abstract instances
23081 early and we want to avoid creating a concrete instance of that
23082 if we don't output it. */
23083 if (in_lto_p)
23084 return;
23086 old_die = lookup_decl_die (decl);
23087 gcc_assert (old_die != NULL);
23088 if (get_AT (old_die, DW_AT_inline))
23089 /* We've already generated the abstract instance. */
23090 return;
23092 /* Go ahead and put DW_AT_inline on the DIE. */
23093 if (DECL_DECLARED_INLINE_P (decl))
23095 if (cgraph_function_possibly_inlined_p (decl))
23096 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23097 else
23098 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23100 else
23102 if (cgraph_function_possibly_inlined_p (decl))
23103 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23104 else
23105 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23108 if (DECL_DECLARED_INLINE_P (decl)
23109 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23110 add_AT_flag (old_die, DW_AT_artificial, 1);
23112 set_decl_origin_self (decl);
23115 /* Helper function of premark_used_types() which gets called through
23116 htab_traverse.
23118 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23119 marked as unused by prune_unused_types. */
23121 bool
23122 premark_used_types_helper (tree const &type, void *)
23124 dw_die_ref die;
23126 die = lookup_type_die (type);
23127 if (die != NULL)
23128 die->die_perennial_p = 1;
23129 return true;
23132 /* Helper function of premark_types_used_by_global_vars which gets called
23133 through htab_traverse.
23135 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23136 marked as unused by prune_unused_types. The DIE of the type is marked
23137 only if the global variable using the type will actually be emitted. */
23140 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23141 void *)
23143 struct types_used_by_vars_entry *entry;
23144 dw_die_ref die;
23146 entry = (struct types_used_by_vars_entry *) *slot;
23147 gcc_assert (entry->type != NULL
23148 && entry->var_decl != NULL);
23149 die = lookup_type_die (entry->type);
23150 if (die)
23152 /* Ask cgraph if the global variable really is to be emitted.
23153 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23154 varpool_node *node = varpool_node::get (entry->var_decl);
23155 if (node && node->definition)
23157 die->die_perennial_p = 1;
23158 /* Keep the parent DIEs as well. */
23159 while ((die = die->die_parent) && die->die_perennial_p == 0)
23160 die->die_perennial_p = 1;
23163 return 1;
23166 /* Mark all members of used_types_hash as perennial. */
23168 static void
23169 premark_used_types (struct function *fun)
23171 if (fun && fun->used_types_hash)
23172 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23175 /* Mark all members of types_used_by_vars_entry as perennial. */
23177 static void
23178 premark_types_used_by_global_vars (void)
23180 if (types_used_by_vars_hash)
23181 types_used_by_vars_hash
23182 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23185 /* Mark all variables used by the symtab as perennial. */
23187 static void
23188 premark_used_variables (void)
23190 /* Mark DIEs in the symtab as used. */
23191 varpool_node *var;
23192 FOR_EACH_VARIABLE (var)
23194 dw_die_ref die = lookup_decl_die (var->decl);
23195 if (die)
23196 die->die_perennial_p = 1;
23200 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23201 for CA_LOC call arg loc node. */
23203 static dw_die_ref
23204 gen_call_site_die (tree decl, dw_die_ref subr_die,
23205 struct call_arg_loc_node *ca_loc)
23207 dw_die_ref stmt_die = NULL, die;
23208 tree block = ca_loc->block;
23210 while (block
23211 && block != DECL_INITIAL (decl)
23212 && TREE_CODE (block) == BLOCK)
23214 stmt_die = lookup_block_die (block);
23215 if (stmt_die)
23216 break;
23217 block = BLOCK_SUPERCONTEXT (block);
23219 if (stmt_die == NULL)
23220 stmt_die = subr_die;
23221 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23222 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23223 if (ca_loc->tail_call_p)
23224 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23225 if (ca_loc->symbol_ref)
23227 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23228 if (tdie)
23229 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23230 else
23231 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23232 false);
23234 return die;
23237 /* Generate a DIE to represent a declared function (either file-scope or
23238 block-local). */
23240 static void
23241 gen_subprogram_die (tree decl, dw_die_ref context_die)
23243 tree origin = decl_ultimate_origin (decl);
23244 dw_die_ref subr_die;
23245 dw_die_ref old_die = lookup_decl_die (decl);
23246 bool old_die_had_no_children = false;
23248 /* This function gets called multiple times for different stages of
23249 the debug process. For example, for func() in this code:
23251 namespace S
23253 void func() { ... }
23256 ...we get called 4 times. Twice in early debug and twice in
23257 late debug:
23259 Early debug
23260 -----------
23262 1. Once while generating func() within the namespace. This is
23263 the declaration. The declaration bit below is set, as the
23264 context is the namespace.
23266 A new DIE will be generated with DW_AT_declaration set.
23268 2. Once for func() itself. This is the specification. The
23269 declaration bit below is clear as the context is the CU.
23271 We will use the cached DIE from (1) to create a new DIE with
23272 DW_AT_specification pointing to the declaration in (1).
23274 Late debug via rest_of_handle_final()
23275 -------------------------------------
23277 3. Once generating func() within the namespace. This is also the
23278 declaration, as in (1), but this time we will early exit below
23279 as we have a cached DIE and a declaration needs no additional
23280 annotations (no locations), as the source declaration line
23281 info is enough.
23283 4. Once for func() itself. As in (2), this is the specification,
23284 but this time we will re-use the cached DIE, and just annotate
23285 it with the location information that should now be available.
23287 For something without namespaces, but with abstract instances, we
23288 are also called a multiple times:
23290 class Base
23292 public:
23293 Base (); // constructor declaration (1)
23296 Base::Base () { } // constructor specification (2)
23298 Early debug
23299 -----------
23301 1. Once for the Base() constructor by virtue of it being a
23302 member of the Base class. This is done via
23303 rest_of_type_compilation.
23305 This is a declaration, so a new DIE will be created with
23306 DW_AT_declaration.
23308 2. Once for the Base() constructor definition, but this time
23309 while generating the abstract instance of the base
23310 constructor (__base_ctor) which is being generated via early
23311 debug of reachable functions.
23313 Even though we have a cached version of the declaration (1),
23314 we will create a DW_AT_specification of the declaration DIE
23315 in (1).
23317 3. Once for the __base_ctor itself, but this time, we generate
23318 an DW_AT_abstract_origin version of the DW_AT_specification in
23319 (2).
23321 Late debug via rest_of_handle_final
23322 -----------------------------------
23324 4. One final time for the __base_ctor (which will have a cached
23325 DIE with DW_AT_abstract_origin created in (3). This time,
23326 we will just annotate the location information now
23327 available.
23329 int declaration = (current_function_decl != decl
23330 || (!DECL_INITIAL (decl) && !origin)
23331 || class_or_namespace_scope_p (context_die));
23333 /* A declaration that has been previously dumped needs no
23334 additional information. */
23335 if (old_die && declaration)
23336 return;
23338 if (in_lto_p && old_die && old_die->die_child == NULL)
23339 old_die_had_no_children = true;
23341 /* Now that the C++ front end lazily declares artificial member fns, we
23342 might need to retrofit the declaration into its class. */
23343 if (!declaration && !origin && !old_die
23344 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23345 && !class_or_namespace_scope_p (context_die)
23346 && debug_info_level > DINFO_LEVEL_TERSE)
23347 old_die = force_decl_die (decl);
23349 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23350 if (origin != NULL)
23352 gcc_assert (!declaration || local_scope_p (context_die));
23354 /* Fixup die_parent for the abstract instance of a nested
23355 inline function. */
23356 if (old_die && old_die->die_parent == NULL)
23357 add_child_die (context_die, old_die);
23359 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23361 /* If we have a DW_AT_abstract_origin we have a working
23362 cached version. */
23363 subr_die = old_die;
23365 else
23367 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23368 add_abstract_origin_attribute (subr_die, origin);
23369 /* This is where the actual code for a cloned function is.
23370 Let's emit linkage name attribute for it. This helps
23371 debuggers to e.g, set breakpoints into
23372 constructors/destructors when the user asks "break
23373 K::K". */
23374 add_linkage_name (subr_die, decl);
23377 /* A cached copy, possibly from early dwarf generation. Reuse as
23378 much as possible. */
23379 else if (old_die)
23381 if (!get_AT_flag (old_die, DW_AT_declaration)
23382 /* We can have a normal definition following an inline one in the
23383 case of redefinition of GNU C extern inlines.
23384 It seems reasonable to use AT_specification in this case. */
23385 && !get_AT (old_die, DW_AT_inline))
23387 /* Detect and ignore this case, where we are trying to output
23388 something we have already output. */
23389 if (get_AT (old_die, DW_AT_low_pc)
23390 || get_AT (old_die, DW_AT_ranges))
23391 return;
23393 /* If we have no location information, this must be a
23394 partially generated DIE from early dwarf generation.
23395 Fall through and generate it. */
23398 /* If the definition comes from the same place as the declaration,
23399 maybe use the old DIE. We always want the DIE for this function
23400 that has the *_pc attributes to be under comp_unit_die so the
23401 debugger can find it. We also need to do this for abstract
23402 instances of inlines, since the spec requires the out-of-line copy
23403 to have the same parent. For local class methods, this doesn't
23404 apply; we just use the old DIE. */
23405 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23406 struct dwarf_file_data * file_index = lookup_filename (s.file);
23407 if (((is_unit_die (old_die->die_parent)
23408 /* This condition fixes the inconsistency/ICE with the
23409 following Fortran test (or some derivative thereof) while
23410 building libgfortran:
23412 module some_m
23413 contains
23414 logical function funky (FLAG)
23415 funky = .true.
23416 end function
23417 end module
23419 || (old_die->die_parent
23420 && old_die->die_parent->die_tag == DW_TAG_module)
23421 || local_scope_p (old_die->die_parent)
23422 || context_die == NULL)
23423 && (DECL_ARTIFICIAL (decl)
23424 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23425 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23426 == (unsigned) s.line)
23427 && (!debug_column_info
23428 || s.column == 0
23429 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23430 == (unsigned) s.column)))))
23431 /* With LTO if there's an abstract instance for
23432 the old DIE, this is a concrete instance and
23433 thus re-use the DIE. */
23434 || get_AT (old_die, DW_AT_abstract_origin))
23436 subr_die = old_die;
23438 /* Clear out the declaration attribute, but leave the
23439 parameters so they can be augmented with location
23440 information later. Unless this was a declaration, in
23441 which case, wipe out the nameless parameters and recreate
23442 them further down. */
23443 if (remove_AT (subr_die, DW_AT_declaration))
23446 remove_AT (subr_die, DW_AT_object_pointer);
23447 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23450 /* Make a specification pointing to the previously built
23451 declaration. */
23452 else
23454 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23455 add_AT_specification (subr_die, old_die);
23456 add_pubname (decl, subr_die);
23457 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23458 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23459 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23460 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23461 if (debug_column_info
23462 && s.column
23463 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23464 != (unsigned) s.column))
23465 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23467 /* If the prototype had an 'auto' or 'decltype(auto)' in
23468 the return type, emit the real type on the definition die. */
23469 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23471 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23472 while (die
23473 && (die->die_tag == DW_TAG_reference_type
23474 || die->die_tag == DW_TAG_rvalue_reference_type
23475 || die->die_tag == DW_TAG_pointer_type
23476 || die->die_tag == DW_TAG_const_type
23477 || die->die_tag == DW_TAG_volatile_type
23478 || die->die_tag == DW_TAG_restrict_type
23479 || die->die_tag == DW_TAG_array_type
23480 || die->die_tag == DW_TAG_ptr_to_member_type
23481 || die->die_tag == DW_TAG_subroutine_type))
23482 die = get_AT_ref (die, DW_AT_type);
23483 if (die == auto_die || die == decltype_auto_die)
23484 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23485 TYPE_UNQUALIFIED, false, context_die);
23488 /* When we process the method declaration, we haven't seen
23489 the out-of-class defaulted definition yet, so we have to
23490 recheck now. */
23491 if ((dwarf_version >= 5 || ! dwarf_strict)
23492 && !get_AT (subr_die, DW_AT_defaulted))
23494 int defaulted
23495 = lang_hooks.decls.decl_dwarf_attribute (decl,
23496 DW_AT_defaulted);
23497 if (defaulted != -1)
23499 /* Other values must have been handled before. */
23500 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23501 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23506 /* Create a fresh DIE for anything else. */
23507 else
23509 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23511 if (TREE_PUBLIC (decl))
23512 add_AT_flag (subr_die, DW_AT_external, 1);
23514 add_name_and_src_coords_attributes (subr_die, decl);
23515 add_pubname (decl, subr_die);
23516 if (debug_info_level > DINFO_LEVEL_TERSE)
23518 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23519 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23520 TYPE_UNQUALIFIED, false, context_die);
23523 add_pure_or_virtual_attribute (subr_die, decl);
23524 if (DECL_ARTIFICIAL (decl))
23525 add_AT_flag (subr_die, DW_AT_artificial, 1);
23527 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23528 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23530 add_alignment_attribute (subr_die, decl);
23532 add_accessibility_attribute (subr_die, decl);
23535 /* Unless we have an existing non-declaration DIE, equate the new
23536 DIE. */
23537 if (!old_die || is_declaration_die (old_die))
23538 equate_decl_number_to_die (decl, subr_die);
23540 if (declaration)
23542 if (!old_die || !get_AT (old_die, DW_AT_inline))
23544 add_AT_flag (subr_die, DW_AT_declaration, 1);
23546 /* If this is an explicit function declaration then generate
23547 a DW_AT_explicit attribute. */
23548 if ((dwarf_version >= 3 || !dwarf_strict)
23549 && lang_hooks.decls.decl_dwarf_attribute (decl,
23550 DW_AT_explicit) == 1)
23551 add_AT_flag (subr_die, DW_AT_explicit, 1);
23553 /* If this is a C++11 deleted special function member then generate
23554 a DW_AT_deleted attribute. */
23555 if ((dwarf_version >= 5 || !dwarf_strict)
23556 && lang_hooks.decls.decl_dwarf_attribute (decl,
23557 DW_AT_deleted) == 1)
23558 add_AT_flag (subr_die, DW_AT_deleted, 1);
23560 /* If this is a C++11 defaulted special function member then
23561 generate a DW_AT_defaulted attribute. */
23562 if (dwarf_version >= 5 || !dwarf_strict)
23564 int defaulted
23565 = lang_hooks.decls.decl_dwarf_attribute (decl,
23566 DW_AT_defaulted);
23567 if (defaulted != -1)
23568 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23571 /* If this is a C++11 non-static member function with & ref-qualifier
23572 then generate a DW_AT_reference attribute. */
23573 if ((dwarf_version >= 5 || !dwarf_strict)
23574 && lang_hooks.decls.decl_dwarf_attribute (decl,
23575 DW_AT_reference) == 1)
23576 add_AT_flag (subr_die, DW_AT_reference, 1);
23578 /* If this is a C++11 non-static member function with &&
23579 ref-qualifier then generate a DW_AT_reference attribute. */
23580 if ((dwarf_version >= 5 || !dwarf_strict)
23581 && lang_hooks.decls.decl_dwarf_attribute (decl,
23582 DW_AT_rvalue_reference)
23583 == 1)
23584 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23587 /* For non DECL_EXTERNALs, if range information is available, fill
23588 the DIE with it. */
23589 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23591 HOST_WIDE_INT cfa_fb_offset;
23593 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23595 if (!crtl->has_bb_partition)
23597 dw_fde_ref fde = fun->fde;
23598 if (fde->dw_fde_begin)
23600 /* We have already generated the labels. */
23601 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23602 fde->dw_fde_end, false);
23604 else
23606 /* Create start/end labels and add the range. */
23607 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23608 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23609 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23610 current_function_funcdef_no);
23611 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23612 current_function_funcdef_no);
23613 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23614 false);
23617 #if VMS_DEBUGGING_INFO
23618 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23619 Section 2.3 Prologue and Epilogue Attributes:
23620 When a breakpoint is set on entry to a function, it is generally
23621 desirable for execution to be suspended, not on the very first
23622 instruction of the function, but rather at a point after the
23623 function's frame has been set up, after any language defined local
23624 declaration processing has been completed, and before execution of
23625 the first statement of the function begins. Debuggers generally
23626 cannot properly determine where this point is. Similarly for a
23627 breakpoint set on exit from a function. The prologue and epilogue
23628 attributes allow a compiler to communicate the location(s) to use. */
23631 if (fde->dw_fde_vms_end_prologue)
23632 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23633 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23635 if (fde->dw_fde_vms_begin_epilogue)
23636 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23637 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23639 #endif
23642 else
23644 /* Generate pubnames entries for the split function code ranges. */
23645 dw_fde_ref fde = fun->fde;
23647 if (fde->dw_fde_second_begin)
23649 if (dwarf_version >= 3 || !dwarf_strict)
23651 /* We should use ranges for non-contiguous code section
23652 addresses. Use the actual code range for the initial
23653 section, since the HOT/COLD labels might precede an
23654 alignment offset. */
23655 bool range_list_added = false;
23656 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23657 fde->dw_fde_end, &range_list_added,
23658 false);
23659 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23660 fde->dw_fde_second_end,
23661 &range_list_added, false);
23662 if (range_list_added)
23663 add_ranges (NULL);
23665 else
23667 /* There is no real support in DW2 for this .. so we make
23668 a work-around. First, emit the pub name for the segment
23669 containing the function label. Then make and emit a
23670 simplified subprogram DIE for the second segment with the
23671 name pre-fixed by __hot/cold_sect_of_. We use the same
23672 linkage name for the second die so that gdb will find both
23673 sections when given "b foo". */
23674 const char *name = NULL;
23675 tree decl_name = DECL_NAME (decl);
23676 dw_die_ref seg_die;
23678 /* Do the 'primary' section. */
23679 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23680 fde->dw_fde_end, false);
23682 /* Build a minimal DIE for the secondary section. */
23683 seg_die = new_die (DW_TAG_subprogram,
23684 subr_die->die_parent, decl);
23686 if (TREE_PUBLIC (decl))
23687 add_AT_flag (seg_die, DW_AT_external, 1);
23689 if (decl_name != NULL
23690 && IDENTIFIER_POINTER (decl_name) != NULL)
23692 name = dwarf2_name (decl, 1);
23693 if (! DECL_ARTIFICIAL (decl))
23694 add_src_coords_attributes (seg_die, decl);
23696 add_linkage_name (seg_die, decl);
23698 gcc_assert (name != NULL);
23699 add_pure_or_virtual_attribute (seg_die, decl);
23700 if (DECL_ARTIFICIAL (decl))
23701 add_AT_flag (seg_die, DW_AT_artificial, 1);
23703 name = concat ("__second_sect_of_", name, NULL);
23704 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23705 fde->dw_fde_second_end, false);
23706 add_name_attribute (seg_die, name);
23707 if (want_pubnames ())
23708 add_pubname_string (name, seg_die);
23711 else
23712 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23713 false);
23716 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23718 /* We define the "frame base" as the function's CFA. This is more
23719 convenient for several reasons: (1) It's stable across the prologue
23720 and epilogue, which makes it better than just a frame pointer,
23721 (2) With dwarf3, there exists a one-byte encoding that allows us
23722 to reference the .debug_frame data by proxy, but failing that,
23723 (3) We can at least reuse the code inspection and interpretation
23724 code that determines the CFA position at various points in the
23725 function. */
23726 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23728 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23729 add_AT_loc (subr_die, DW_AT_frame_base, op);
23731 else
23733 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23734 if (list->dw_loc_next)
23735 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23736 else
23737 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23740 /* Compute a displacement from the "steady-state frame pointer" to
23741 the CFA. The former is what all stack slots and argument slots
23742 will reference in the rtl; the latter is what we've told the
23743 debugger about. We'll need to adjust all frame_base references
23744 by this displacement. */
23745 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23747 if (fun->static_chain_decl)
23749 /* DWARF requires here a location expression that computes the
23750 address of the enclosing subprogram's frame base. The machinery
23751 in tree-nested.c is supposed to store this specific address in the
23752 last field of the FRAME record. */
23753 const tree frame_type
23754 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23755 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23757 tree fb_expr
23758 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23759 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23760 fb_expr, fb_decl, NULL_TREE);
23762 add_AT_location_description (subr_die, DW_AT_static_link,
23763 loc_list_from_tree (fb_expr, 0, NULL));
23766 resolve_variable_values ();
23769 /* Generate child dies for template parameters. */
23770 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23771 gen_generic_params_dies (decl);
23773 /* Now output descriptions of the arguments for this function. This gets
23774 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23775 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23776 `...' at the end of the formal parameter list. In order to find out if
23777 there was a trailing ellipsis or not, we must instead look at the type
23778 associated with the FUNCTION_DECL. This will be a node of type
23779 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23780 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23781 an ellipsis at the end. */
23783 /* In the case where we are describing a mere function declaration, all we
23784 need to do here (and all we *can* do here) is to describe the *types* of
23785 its formal parameters. */
23786 if (debug_info_level <= DINFO_LEVEL_TERSE)
23788 else if (declaration)
23789 gen_formal_types_die (decl, subr_die);
23790 else
23792 /* Generate DIEs to represent all known formal parameters. */
23793 tree parm = DECL_ARGUMENTS (decl);
23794 tree generic_decl = early_dwarf
23795 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23796 tree generic_decl_parm = generic_decl
23797 ? DECL_ARGUMENTS (generic_decl)
23798 : NULL;
23800 /* Now we want to walk the list of parameters of the function and
23801 emit their relevant DIEs.
23803 We consider the case of DECL being an instance of a generic function
23804 as well as it being a normal function.
23806 If DECL is an instance of a generic function we walk the
23807 parameters of the generic function declaration _and_ the parameters of
23808 DECL itself. This is useful because we want to emit specific DIEs for
23809 function parameter packs and those are declared as part of the
23810 generic function declaration. In that particular case,
23811 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23812 That DIE has children DIEs representing the set of arguments
23813 of the pack. Note that the set of pack arguments can be empty.
23814 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23815 children DIE.
23817 Otherwise, we just consider the parameters of DECL. */
23818 while (generic_decl_parm || parm)
23820 if (generic_decl_parm
23821 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23822 gen_formal_parameter_pack_die (generic_decl_parm,
23823 parm, subr_die,
23824 &parm);
23825 else if (parm)
23827 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23829 if (early_dwarf
23830 && parm == DECL_ARGUMENTS (decl)
23831 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23832 && parm_die
23833 && (dwarf_version >= 3 || !dwarf_strict))
23834 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23836 parm = DECL_CHAIN (parm);
23839 if (generic_decl_parm)
23840 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23843 /* Decide whether we need an unspecified_parameters DIE at the end.
23844 There are 2 more cases to do this for: 1) the ansi ... declaration -
23845 this is detectable when the end of the arg list is not a
23846 void_type_node 2) an unprototyped function declaration (not a
23847 definition). This just means that we have no info about the
23848 parameters at all. */
23849 if (early_dwarf)
23851 if (prototype_p (TREE_TYPE (decl)))
23853 /* This is the prototyped case, check for.... */
23854 if (stdarg_p (TREE_TYPE (decl)))
23855 gen_unspecified_parameters_die (decl, subr_die);
23857 else if (DECL_INITIAL (decl) == NULL_TREE)
23858 gen_unspecified_parameters_die (decl, subr_die);
23860 else if ((subr_die != old_die || old_die_had_no_children)
23861 && prototype_p (TREE_TYPE (decl))
23862 && stdarg_p (TREE_TYPE (decl)))
23863 gen_unspecified_parameters_die (decl, subr_die);
23866 if (subr_die != old_die)
23867 /* Add the calling convention attribute if requested. */
23868 add_calling_convention_attribute (subr_die, decl);
23870 /* Output Dwarf info for all of the stuff within the body of the function
23871 (if it has one - it may be just a declaration).
23873 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23874 a function. This BLOCK actually represents the outermost binding contour
23875 for the function, i.e. the contour in which the function's formal
23876 parameters and labels get declared. Curiously, it appears that the front
23877 end doesn't actually put the PARM_DECL nodes for the current function onto
23878 the BLOCK_VARS list for this outer scope, but are strung off of the
23879 DECL_ARGUMENTS list for the function instead.
23881 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23882 the LABEL_DECL nodes for the function however, and we output DWARF info
23883 for those in decls_for_scope. Just within the `outer_scope' there will be
23884 a BLOCK node representing the function's outermost pair of curly braces,
23885 and any blocks used for the base and member initializers of a C++
23886 constructor function. */
23887 tree outer_scope = DECL_INITIAL (decl);
23888 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23890 int call_site_note_count = 0;
23891 int tail_call_site_note_count = 0;
23893 /* Emit a DW_TAG_variable DIE for a named return value. */
23894 if (DECL_NAME (DECL_RESULT (decl)))
23895 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23897 /* The first time through decls_for_scope we will generate the
23898 DIEs for the locals. The second time, we fill in the
23899 location info. */
23900 decls_for_scope (outer_scope, subr_die);
23902 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23904 struct call_arg_loc_node *ca_loc;
23905 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23907 dw_die_ref die = NULL;
23908 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23909 rtx arg, next_arg;
23910 tree arg_decl = NULL_TREE;
23912 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23913 ? XEXP (ca_loc->call_arg_loc_note, 0)
23914 : NULL_RTX);
23915 arg; arg = next_arg)
23917 dw_loc_descr_ref reg, val;
23918 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23919 dw_die_ref cdie, tdie = NULL;
23921 next_arg = XEXP (arg, 1);
23922 if (REG_P (XEXP (XEXP (arg, 0), 0))
23923 && next_arg
23924 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23925 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23926 && REGNO (XEXP (XEXP (arg, 0), 0))
23927 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23928 next_arg = XEXP (next_arg, 1);
23929 if (mode == VOIDmode)
23931 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23932 if (mode == VOIDmode)
23933 mode = GET_MODE (XEXP (arg, 0));
23935 if (mode == VOIDmode || mode == BLKmode)
23936 continue;
23937 /* Get dynamic information about call target only if we
23938 have no static information: we cannot generate both
23939 DW_AT_call_origin and DW_AT_call_target
23940 attributes. */
23941 if (ca_loc->symbol_ref == NULL_RTX)
23943 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23945 tloc = XEXP (XEXP (arg, 0), 1);
23946 continue;
23948 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23949 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23951 tlocc = XEXP (XEXP (arg, 0), 1);
23952 continue;
23955 reg = NULL;
23956 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23957 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23958 VAR_INIT_STATUS_INITIALIZED);
23959 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23961 rtx mem = XEXP (XEXP (arg, 0), 0);
23962 reg = mem_loc_descriptor (XEXP (mem, 0),
23963 get_address_mode (mem),
23964 GET_MODE (mem),
23965 VAR_INIT_STATUS_INITIALIZED);
23967 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23968 == DEBUG_PARAMETER_REF)
23970 tree tdecl
23971 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23972 tdie = lookup_decl_die (tdecl);
23973 if (tdie == NULL)
23974 continue;
23975 arg_decl = tdecl;
23977 else
23978 continue;
23979 if (reg == NULL
23980 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23981 != DEBUG_PARAMETER_REF)
23982 continue;
23983 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23984 VOIDmode,
23985 VAR_INIT_STATUS_INITIALIZED);
23986 if (val == NULL)
23987 continue;
23988 if (die == NULL)
23989 die = gen_call_site_die (decl, subr_die, ca_loc);
23990 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23991 NULL_TREE);
23992 add_desc_attribute (cdie, arg_decl);
23993 if (reg != NULL)
23994 add_AT_loc (cdie, DW_AT_location, reg);
23995 else if (tdie != NULL)
23996 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23997 tdie);
23998 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23999 if (next_arg != XEXP (arg, 1))
24001 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24002 if (mode == VOIDmode)
24003 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24004 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24005 0), 1),
24006 mode, VOIDmode,
24007 VAR_INIT_STATUS_INITIALIZED);
24008 if (val != NULL)
24009 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24010 val);
24013 if (die == NULL
24014 && (ca_loc->symbol_ref || tloc))
24015 die = gen_call_site_die (decl, subr_die, ca_loc);
24016 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24018 dw_loc_descr_ref tval = NULL;
24020 if (tloc != NULL_RTX)
24021 tval = mem_loc_descriptor (tloc,
24022 GET_MODE (tloc) == VOIDmode
24023 ? Pmode : GET_MODE (tloc),
24024 VOIDmode,
24025 VAR_INIT_STATUS_INITIALIZED);
24026 if (tval)
24027 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24028 else if (tlocc != NULL_RTX)
24030 tval = mem_loc_descriptor (tlocc,
24031 GET_MODE (tlocc) == VOIDmode
24032 ? Pmode : GET_MODE (tlocc),
24033 VOIDmode,
24034 VAR_INIT_STATUS_INITIALIZED);
24035 if (tval)
24036 add_AT_loc (die,
24037 dwarf_AT (DW_AT_call_target_clobbered),
24038 tval);
24041 if (die != NULL)
24043 call_site_note_count++;
24044 if (ca_loc->tail_call_p)
24045 tail_call_site_note_count++;
24049 call_arg_locations = NULL;
24050 call_arg_loc_last = NULL;
24051 if (tail_call_site_count >= 0
24052 && tail_call_site_count == tail_call_site_note_count
24053 && (!dwarf_strict || dwarf_version >= 5))
24055 if (call_site_count >= 0
24056 && call_site_count == call_site_note_count)
24057 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24058 else
24059 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24061 call_site_count = -1;
24062 tail_call_site_count = -1;
24065 /* Mark used types after we have created DIEs for the functions scopes. */
24066 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24069 /* Returns a hash value for X (which really is a die_struct). */
24071 hashval_t
24072 block_die_hasher::hash (die_struct *d)
24074 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24077 /* Return nonzero if decl_id and die_parent of die_struct X is the same
24078 as decl_id and die_parent of die_struct Y. */
24080 bool
24081 block_die_hasher::equal (die_struct *x, die_struct *y)
24083 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24086 /* Hold information about markers for inlined entry points. */
24087 struct GTY ((for_user)) inline_entry_data
24089 /* The block that's the inlined_function_outer_scope for an inlined
24090 function. */
24091 tree block;
24093 /* The label at the inlined entry point. */
24094 const char *label_pfx;
24095 unsigned int label_num;
24097 /* The view number to be used as the inlined entry point. */
24098 var_loc_view view;
24101 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24103 typedef tree compare_type;
24104 static inline hashval_t hash (const inline_entry_data *);
24105 static inline bool equal (const inline_entry_data *, const_tree);
24108 /* Hash table routines for inline_entry_data. */
24110 inline hashval_t
24111 inline_entry_data_hasher::hash (const inline_entry_data *data)
24113 return htab_hash_pointer (data->block);
24116 inline bool
24117 inline_entry_data_hasher::equal (const inline_entry_data *data,
24118 const_tree block)
24120 return data->block == block;
24123 /* Inlined entry points pending DIE creation in this compilation unit. */
24125 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24128 /* Return TRUE if DECL, which may have been previously generated as
24129 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24130 true if decl (or its origin) is either an extern declaration or a
24131 class/namespace scoped declaration.
24133 The declare_in_namespace support causes us to get two DIEs for one
24134 variable, both of which are declarations. We want to avoid
24135 considering one to be a specification, so we must test for
24136 DECLARATION and DW_AT_declaration. */
24137 static inline bool
24138 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24140 return (old_die && TREE_STATIC (decl) && !declaration
24141 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24144 /* Return true if DECL is a local static. */
24146 static inline bool
24147 local_function_static (tree decl)
24149 gcc_assert (VAR_P (decl));
24150 return TREE_STATIC (decl)
24151 && DECL_CONTEXT (decl)
24152 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24155 /* Return true iff DECL overrides (presumably completes) the type of
24156 OLD_DIE within CONTEXT_DIE. */
24158 static bool
24159 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24160 dw_die_ref context_die)
24162 tree type = TREE_TYPE (decl);
24163 int cv_quals;
24165 if (decl_by_reference_p (decl))
24167 type = TREE_TYPE (type);
24168 cv_quals = TYPE_UNQUALIFIED;
24170 else
24171 cv_quals = decl_quals (decl);
24173 dw_die_ref type_die = modified_type_die (type,
24174 cv_quals | TYPE_QUALS (type),
24175 false,
24176 context_die);
24178 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24180 return type_die != old_type_die;
24183 /* Generate a DIE to represent a declared data object.
24184 Either DECL or ORIGIN must be non-null. */
24186 static void
24187 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24189 HOST_WIDE_INT off = 0;
24190 tree com_decl;
24191 tree decl_or_origin = decl ? decl : origin;
24192 tree ultimate_origin;
24193 dw_die_ref var_die;
24194 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24195 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24196 || class_or_namespace_scope_p (context_die));
24197 bool specialization_p = false;
24198 bool no_linkage_name = false;
24200 /* While C++ inline static data members have definitions inside of the
24201 class, force the first DIE to be a declaration, then let gen_member_die
24202 reparent it to the class context and call gen_variable_die again
24203 to create the outside of the class DIE for the definition. */
24204 if (!declaration
24205 && old_die == NULL
24206 && decl
24207 && DECL_CONTEXT (decl)
24208 && TYPE_P (DECL_CONTEXT (decl))
24209 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24211 declaration = true;
24212 if (dwarf_version < 5)
24213 no_linkage_name = true;
24216 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24217 if (decl || ultimate_origin)
24218 origin = ultimate_origin;
24219 com_decl = fortran_common (decl_or_origin, &off);
24221 /* Symbol in common gets emitted as a child of the common block, in the form
24222 of a data member. */
24223 if (com_decl)
24225 dw_die_ref com_die;
24226 dw_loc_list_ref loc = NULL;
24227 die_node com_die_arg;
24229 var_die = lookup_decl_die (decl_or_origin);
24230 if (var_die)
24232 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24234 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24235 if (loc)
24237 if (off)
24239 /* Optimize the common case. */
24240 if (single_element_loc_list_p (loc)
24241 && loc->expr->dw_loc_opc == DW_OP_addr
24242 && loc->expr->dw_loc_next == NULL
24243 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24244 == SYMBOL_REF)
24246 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24247 loc->expr->dw_loc_oprnd1.v.val_addr
24248 = plus_constant (GET_MODE (x), x , off);
24250 else
24251 loc_list_plus_const (loc, off);
24253 add_AT_location_description (var_die, DW_AT_location, loc);
24254 remove_AT (var_die, DW_AT_declaration);
24257 return;
24260 if (common_block_die_table == NULL)
24261 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24263 com_die_arg.decl_id = DECL_UID (com_decl);
24264 com_die_arg.die_parent = context_die;
24265 com_die = common_block_die_table->find (&com_die_arg);
24266 if (! early_dwarf)
24267 loc = loc_list_from_tree (com_decl, 2, NULL);
24268 if (com_die == NULL)
24270 const char *cnam
24271 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24272 die_node **slot;
24274 com_die = new_die (DW_TAG_common_block, context_die, decl);
24275 add_name_and_src_coords_attributes (com_die, com_decl);
24276 if (loc)
24278 add_AT_location_description (com_die, DW_AT_location, loc);
24279 /* Avoid sharing the same loc descriptor between
24280 DW_TAG_common_block and DW_TAG_variable. */
24281 loc = loc_list_from_tree (com_decl, 2, NULL);
24283 else if (DECL_EXTERNAL (decl_or_origin))
24284 add_AT_flag (com_die, DW_AT_declaration, 1);
24285 if (want_pubnames ())
24286 add_pubname_string (cnam, com_die); /* ??? needed? */
24287 com_die->decl_id = DECL_UID (com_decl);
24288 slot = common_block_die_table->find_slot (com_die, INSERT);
24289 *slot = com_die;
24291 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24293 add_AT_location_description (com_die, DW_AT_location, loc);
24294 loc = loc_list_from_tree (com_decl, 2, NULL);
24295 remove_AT (com_die, DW_AT_declaration);
24297 var_die = new_die (DW_TAG_variable, com_die, decl);
24298 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24299 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24300 decl_quals (decl_or_origin), false,
24301 context_die);
24302 add_alignment_attribute (var_die, decl);
24303 add_AT_flag (var_die, DW_AT_external, 1);
24304 if (loc)
24306 if (off)
24308 /* Optimize the common case. */
24309 if (single_element_loc_list_p (loc)
24310 && loc->expr->dw_loc_opc == DW_OP_addr
24311 && loc->expr->dw_loc_next == NULL
24312 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24314 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24315 loc->expr->dw_loc_oprnd1.v.val_addr
24316 = plus_constant (GET_MODE (x), x, off);
24318 else
24319 loc_list_plus_const (loc, off);
24321 add_AT_location_description (var_die, DW_AT_location, loc);
24323 else if (DECL_EXTERNAL (decl_or_origin))
24324 add_AT_flag (var_die, DW_AT_declaration, 1);
24325 if (decl)
24326 equate_decl_number_to_die (decl, var_die);
24327 return;
24330 if (old_die)
24332 if (declaration)
24334 /* A declaration that has been previously dumped, needs no
24335 further annotations, since it doesn't need location on
24336 the second pass. */
24337 return;
24339 else if (decl_will_get_specification_p (old_die, decl, declaration)
24340 && !get_AT (old_die, DW_AT_specification))
24342 /* Fall-thru so we can make a new variable die along with a
24343 DW_AT_specification. */
24345 else if (origin && old_die->die_parent != context_die)
24347 /* If we will be creating an inlined instance, we need a
24348 new DIE that will get annotated with
24349 DW_AT_abstract_origin. */
24350 gcc_assert (!DECL_ABSTRACT_P (decl));
24352 else
24354 /* If a DIE was dumped early, it still needs location info.
24355 Skip to where we fill the location bits. */
24356 var_die = old_die;
24358 /* ??? In LTRANS we cannot annotate early created variably
24359 modified type DIEs without copying them and adjusting all
24360 references to them. Thus we dumped them again. Also add a
24361 reference to them but beware of -g0 compile and -g link
24362 in which case the reference will be already present. */
24363 tree type = TREE_TYPE (decl_or_origin);
24364 if (in_lto_p
24365 && ! get_AT (var_die, DW_AT_type)
24366 && variably_modified_type_p
24367 (type, decl_function_context (decl_or_origin)))
24369 if (decl_by_reference_p (decl_or_origin))
24370 add_type_attribute (var_die, TREE_TYPE (type),
24371 TYPE_UNQUALIFIED, false, context_die);
24372 else
24373 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24374 false, context_die);
24377 goto gen_variable_die_location;
24381 /* For static data members, the declaration in the class is supposed
24382 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24383 also in DWARF2; the specification should still be DW_TAG_variable
24384 referencing the DW_TAG_member DIE. */
24385 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24386 var_die = new_die (DW_TAG_member, context_die, decl);
24387 else
24388 var_die = new_die (DW_TAG_variable, context_die, decl);
24390 if (origin != NULL)
24391 add_abstract_origin_attribute (var_die, origin);
24393 /* Loop unrolling can create multiple blocks that refer to the same
24394 static variable, so we must test for the DW_AT_declaration flag.
24396 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24397 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24398 sharing them.
24400 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24401 else if (decl_will_get_specification_p (old_die, decl, declaration))
24403 /* This is a definition of a C++ class level static. */
24404 add_AT_specification (var_die, old_die);
24405 specialization_p = true;
24406 if (DECL_NAME (decl))
24408 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24409 struct dwarf_file_data * file_index = lookup_filename (s.file);
24411 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24412 add_AT_file (var_die, DW_AT_decl_file, file_index);
24414 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24415 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24417 if (debug_column_info
24418 && s.column
24419 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24420 != (unsigned) s.column))
24421 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24423 if (old_die->die_tag == DW_TAG_member)
24424 add_linkage_name (var_die, decl);
24427 else
24428 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24430 if ((origin == NULL && !specialization_p)
24431 || (origin != NULL
24432 && !DECL_ABSTRACT_P (decl_or_origin)
24433 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24434 decl_function_context
24435 (decl_or_origin)))
24436 || (old_die && specialization_p
24437 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24439 tree type = TREE_TYPE (decl_or_origin);
24441 if (decl_by_reference_p (decl_or_origin))
24442 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24443 context_die);
24444 else
24445 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24446 context_die);
24449 if (origin == NULL && !specialization_p)
24451 if (TREE_PUBLIC (decl))
24452 add_AT_flag (var_die, DW_AT_external, 1);
24454 if (DECL_ARTIFICIAL (decl))
24455 add_AT_flag (var_die, DW_AT_artificial, 1);
24457 add_alignment_attribute (var_die, decl);
24459 add_accessibility_attribute (var_die, decl);
24462 if (declaration)
24463 add_AT_flag (var_die, DW_AT_declaration, 1);
24465 if (decl && (DECL_ABSTRACT_P (decl)
24466 || !old_die || is_declaration_die (old_die)))
24467 equate_decl_number_to_die (decl, var_die);
24469 gen_variable_die_location:
24470 if (! declaration
24471 && (! DECL_ABSTRACT_P (decl_or_origin)
24472 /* Local static vars are shared between all clones/inlines,
24473 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24474 already set. */
24475 || (VAR_P (decl_or_origin)
24476 && TREE_STATIC (decl_or_origin)
24477 && DECL_RTL_SET_P (decl_or_origin))))
24479 if (early_dwarf)
24480 add_pubname (decl_or_origin, var_die);
24481 else
24482 add_location_or_const_value_attribute (var_die, decl_or_origin,
24483 decl == NULL);
24485 else
24486 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24488 if ((dwarf_version >= 4 || !dwarf_strict)
24489 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24490 DW_AT_const_expr) == 1
24491 && !get_AT (var_die, DW_AT_const_expr)
24492 && !specialization_p)
24493 add_AT_flag (var_die, DW_AT_const_expr, 1);
24495 if (!dwarf_strict)
24497 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24498 DW_AT_inline);
24499 if (inl != -1
24500 && !get_AT (var_die, DW_AT_inline)
24501 && !specialization_p)
24502 add_AT_unsigned (var_die, DW_AT_inline, inl);
24506 /* Generate a DIE to represent a named constant. */
24508 static void
24509 gen_const_die (tree decl, dw_die_ref context_die)
24511 dw_die_ref const_die;
24512 tree type = TREE_TYPE (decl);
24514 const_die = lookup_decl_die (decl);
24515 if (const_die)
24516 return;
24518 const_die = new_die (DW_TAG_constant, context_die, decl);
24519 equate_decl_number_to_die (decl, const_die);
24520 add_name_and_src_coords_attributes (const_die, decl);
24521 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24522 if (TREE_PUBLIC (decl))
24523 add_AT_flag (const_die, DW_AT_external, 1);
24524 if (DECL_ARTIFICIAL (decl))
24525 add_AT_flag (const_die, DW_AT_artificial, 1);
24526 tree_add_const_value_attribute_for_decl (const_die, decl);
24529 /* Generate a DIE to represent a label identifier. */
24531 static void
24532 gen_label_die (tree decl, dw_die_ref context_die)
24534 tree origin = decl_ultimate_origin (decl);
24535 dw_die_ref lbl_die = lookup_decl_die (decl);
24536 rtx insn;
24537 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24539 if (!lbl_die)
24541 lbl_die = new_die (DW_TAG_label, context_die, decl);
24542 equate_decl_number_to_die (decl, lbl_die);
24544 if (origin != NULL)
24545 add_abstract_origin_attribute (lbl_die, origin);
24546 else
24547 add_name_and_src_coords_attributes (lbl_die, decl);
24550 if (DECL_ABSTRACT_P (decl))
24551 equate_decl_number_to_die (decl, lbl_die);
24552 else if (! early_dwarf)
24554 insn = DECL_RTL_IF_SET (decl);
24556 /* Deleted labels are programmer specified labels which have been
24557 eliminated because of various optimizations. We still emit them
24558 here so that it is possible to put breakpoints on them. */
24559 if (insn
24560 && (LABEL_P (insn)
24561 || ((NOTE_P (insn)
24562 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24564 /* When optimization is enabled (via -O) some parts of the compiler
24565 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
24566 represent source-level labels which were explicitly declared by
24567 the user. This really shouldn't be happening though, so catch
24568 it if it ever does happen. */
24569 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24571 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24572 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24574 else if (insn
24575 && NOTE_P (insn)
24576 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24577 && CODE_LABEL_NUMBER (insn) != -1)
24579 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24580 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24585 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24586 attributes to the DIE for a block STMT, to describe where the inlined
24587 function was called from. This is similar to add_src_coords_attributes. */
24589 static inline void
24590 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24592 /* We can end up with BUILTINS_LOCATION here. */
24593 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24594 return;
24596 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
24598 if (dwarf_version >= 3 || !dwarf_strict)
24600 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24601 add_AT_unsigned (die, DW_AT_call_line, s.line);
24602 if (debug_column_info && s.column)
24603 add_AT_unsigned (die, DW_AT_call_column, s.column);
24608 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24609 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24611 static inline void
24612 add_high_low_attributes (tree stmt, dw_die_ref die)
24614 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24616 if (inline_entry_data **iedp
24617 = !inline_entry_data_table ? NULL
24618 : inline_entry_data_table->find_slot_with_hash (stmt,
24619 htab_hash_pointer (stmt),
24620 NO_INSERT))
24622 inline_entry_data *ied = *iedp;
24623 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24624 gcc_assert (debug_inline_points);
24625 gcc_assert (inlined_function_outer_scope_p (stmt));
24627 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24628 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24630 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24631 && !dwarf_strict)
24633 if (!output_asm_line_debug_info ())
24634 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24635 else
24637 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24638 /* FIXME: this will resolve to a small number. Could we
24639 possibly emit smaller data? Ideally we'd emit a
24640 uleb128, but that would make the size of DIEs
24641 impossible for the compiler to compute, since it's
24642 the assembler that computes the value of the view
24643 label in this case. Ideally, we'd have a single form
24644 encompassing both the address and the view, and
24645 indirecting them through a table might make things
24646 easier, but even that would be more wasteful,
24647 space-wise, than what we have now. */
24648 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24652 inline_entry_data_table->clear_slot (iedp);
24655 if (BLOCK_FRAGMENT_CHAIN (stmt)
24656 && (dwarf_version >= 3 || !dwarf_strict))
24658 tree chain, superblock = NULL_TREE;
24659 dw_die_ref pdie;
24660 dw_attr_node *attr = NULL;
24662 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24664 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24665 BLOCK_NUMBER (stmt));
24666 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24669 /* Optimize duplicate .debug_ranges lists or even tails of
24670 lists. If this BLOCK has same ranges as its supercontext,
24671 lookup DW_AT_ranges attribute in the supercontext (and
24672 recursively so), verify that the ranges_table contains the
24673 right values and use it instead of adding a new .debug_range. */
24674 for (chain = stmt, pdie = die;
24675 BLOCK_SAME_RANGE (chain);
24676 chain = BLOCK_SUPERCONTEXT (chain))
24678 dw_attr_node *new_attr;
24680 pdie = pdie->die_parent;
24681 if (pdie == NULL)
24682 break;
24683 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24684 break;
24685 new_attr = get_AT (pdie, DW_AT_ranges);
24686 if (new_attr == NULL
24687 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24688 break;
24689 attr = new_attr;
24690 superblock = BLOCK_SUPERCONTEXT (chain);
24692 if (attr != NULL
24693 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24694 == (int)BLOCK_NUMBER (superblock))
24695 && BLOCK_FRAGMENT_CHAIN (superblock))
24697 unsigned long off = attr->dw_attr_val.v.val_offset;
24698 unsigned long supercnt = 0, thiscnt = 0;
24699 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24700 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24702 ++supercnt;
24703 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24704 == (int)BLOCK_NUMBER (chain));
24706 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24707 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24708 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24709 ++thiscnt;
24710 gcc_assert (supercnt >= thiscnt);
24711 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24712 false);
24713 note_rnglist_head (off + supercnt - thiscnt);
24714 return;
24717 unsigned int offset = add_ranges (stmt, true);
24718 add_AT_range_list (die, DW_AT_ranges, offset, false);
24719 note_rnglist_head (offset);
24721 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24722 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24725 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24726 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24727 chain = BLOCK_FRAGMENT_CHAIN (chain);
24729 while (chain);
24730 add_ranges (NULL);
24732 else
24734 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24735 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24736 BLOCK_NUMBER (stmt));
24737 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24738 BLOCK_NUMBER (stmt));
24739 add_AT_low_high_pc (die, label, label_high, false);
24743 /* Generate a DIE for a lexical block. */
24745 static void
24746 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24748 dw_die_ref old_die = lookup_block_die (stmt);
24749 dw_die_ref stmt_die = NULL;
24750 if (!old_die)
24752 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24753 equate_block_to_die (stmt, stmt_die);
24756 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24758 /* If this is an inlined or conrecte instance, create a new lexical
24759 die for anything below to attach DW_AT_abstract_origin to. */
24760 if (old_die)
24761 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24763 tree origin = block_ultimate_origin (stmt);
24764 if (origin != NULL_TREE && (origin != stmt || old_die))
24765 add_abstract_origin_attribute (stmt_die, origin);
24767 old_die = NULL;
24770 if (old_die)
24771 stmt_die = old_die;
24773 /* A non abstract block whose blocks have already been reordered
24774 should have the instruction range for this block. If so, set the
24775 high/low attributes. */
24776 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24778 gcc_assert (stmt_die);
24779 add_high_low_attributes (stmt, stmt_die);
24782 decls_for_scope (stmt, stmt_die);
24785 /* Generate a DIE for an inlined subprogram. */
24787 static void
24788 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24790 tree decl = block_ultimate_origin (stmt);
24792 /* Make sure any inlined functions are known to be inlineable. */
24793 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24794 || cgraph_function_possibly_inlined_p (decl));
24796 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24798 if (call_arg_locations || debug_inline_points)
24799 equate_block_to_die (stmt, subr_die);
24800 add_abstract_origin_attribute (subr_die, decl);
24801 if (TREE_ASM_WRITTEN (stmt))
24802 add_high_low_attributes (stmt, subr_die);
24803 add_call_src_coords_attributes (stmt, subr_die);
24805 /* The inliner creates an extra BLOCK for the parameter setup,
24806 we want to merge that with the actual outermost BLOCK of the
24807 inlined function to avoid duplicate locals in consumers.
24808 Do that by doing the recursion to subblocks on the single subblock
24809 of STMT. */
24810 bool unwrap_one = false;
24811 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24813 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24814 if (origin
24815 && TREE_CODE (origin) == BLOCK
24816 && BLOCK_SUPERCONTEXT (origin) == decl)
24817 unwrap_one = true;
24819 decls_for_scope (stmt, subr_die, !unwrap_one);
24820 if (unwrap_one)
24821 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24824 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24825 the comment for VLR_CONTEXT. */
24827 static void
24828 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24830 dw_die_ref decl_die;
24832 if (TREE_TYPE (decl) == error_mark_node)
24833 return;
24835 decl_die = new_die (DW_TAG_member, context_die, decl);
24836 add_name_and_src_coords_attributes (decl_die, decl);
24837 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24838 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24839 context_die);
24841 if (DECL_BIT_FIELD_TYPE (decl))
24843 add_byte_size_attribute (decl_die, decl);
24844 add_bit_size_attribute (decl_die, decl);
24845 add_bit_offset_attribute (decl_die, decl);
24848 add_alignment_attribute (decl_die, decl);
24850 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24851 add_data_member_location_attribute (decl_die, decl, ctx);
24853 if (DECL_ARTIFICIAL (decl))
24854 add_AT_flag (decl_die, DW_AT_artificial, 1);
24856 add_accessibility_attribute (decl_die, decl);
24858 /* Equate decl number to die, so that we can look up this decl later on. */
24859 equate_decl_number_to_die (decl, decl_die);
24862 /* Generate a DIE for a pointer to a member type. TYPE can be an
24863 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24864 pointer to member function. */
24866 static void
24867 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24869 if (lookup_type_die (type))
24870 return;
24872 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24873 scope_die_for (type, context_die), type);
24875 equate_type_number_to_die (type, ptr_die);
24876 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24877 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24878 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24879 context_die);
24880 add_alignment_attribute (ptr_die, type);
24882 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24883 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24885 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24886 add_AT_loc (ptr_die, DW_AT_use_location, op);
24890 static char *producer_string;
24892 /* Given a C and/or C++ language/version string return the "highest".
24893 C++ is assumed to be "higher" than C in this case. Used for merging
24894 LTO translation unit languages. */
24895 static const char *
24896 highest_c_language (const char *lang1, const char *lang2)
24898 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
24899 return "GNU C++23";
24900 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
24901 return "GNU C++20";
24902 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24903 return "GNU C++17";
24904 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24905 return "GNU C++14";
24906 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24907 return "GNU C++11";
24908 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24909 return "GNU C++98";
24911 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24912 return "GNU C2X";
24913 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24914 return "GNU C17";
24915 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24916 return "GNU C11";
24917 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24918 return "GNU C99";
24919 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24920 return "GNU C89";
24922 gcc_unreachable ();
24926 /* Generate the DIE for the compilation unit. */
24928 static dw_die_ref
24929 gen_compile_unit_die (const char *filename)
24931 dw_die_ref die;
24932 const char *language_string = lang_hooks.name;
24933 int language;
24935 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24937 if (filename)
24939 add_filename_attribute (die, filename);
24940 /* Don't add cwd for <built-in>. */
24941 if (filename[0] != '<')
24942 add_comp_dir_attribute (die);
24945 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24947 /* If our producer is LTO try to figure out a common language to use
24948 from the global list of translation units. */
24949 if (strcmp (language_string, "GNU GIMPLE") == 0)
24951 unsigned i;
24952 tree t;
24953 const char *common_lang = NULL;
24955 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24957 if (!TRANSLATION_UNIT_LANGUAGE (t))
24958 continue;
24959 if (!common_lang)
24960 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24961 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24963 else if (startswith (common_lang, "GNU C")
24964 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
24965 /* Mixing C and C++ is ok, use C++ in that case. */
24966 common_lang = highest_c_language (common_lang,
24967 TRANSLATION_UNIT_LANGUAGE (t));
24968 else
24970 /* Fall back to C. */
24971 common_lang = NULL;
24972 break;
24976 if (common_lang)
24977 language_string = common_lang;
24980 language = DW_LANG_C;
24981 if (startswith (language_string, "GNU C")
24982 && ISDIGIT (language_string[5]))
24984 language = DW_LANG_C89;
24985 if (dwarf_version >= 3 || !dwarf_strict)
24987 if (strcmp (language_string, "GNU C89") != 0)
24988 language = DW_LANG_C99;
24990 if (dwarf_version >= 5 /* || !dwarf_strict */)
24991 if (strcmp (language_string, "GNU C11") == 0
24992 || strcmp (language_string, "GNU C17") == 0
24993 || strcmp (language_string, "GNU C2X") == 0)
24994 language = DW_LANG_C11;
24997 else if (startswith (language_string, "GNU C++"))
24999 language = DW_LANG_C_plus_plus;
25000 if (dwarf_version >= 5 /* || !dwarf_strict */)
25002 if (strcmp (language_string, "GNU C++11") == 0)
25003 language = DW_LANG_C_plus_plus_11;
25004 else if (strcmp (language_string, "GNU C++14") == 0)
25005 language = DW_LANG_C_plus_plus_14;
25006 else if (strcmp (language_string, "GNU C++17") == 0
25007 || strcmp (language_string, "GNU C++20") == 0
25008 || strcmp (language_string, "GNU C++23") == 0)
25009 /* For now. */
25010 language = DW_LANG_C_plus_plus_14;
25013 else if (strcmp (language_string, "GNU F77") == 0)
25014 language = DW_LANG_Fortran77;
25015 else if (dwarf_version >= 3 || !dwarf_strict)
25017 if (strcmp (language_string, "GNU Ada") == 0)
25018 language = DW_LANG_Ada95;
25019 else if (startswith (language_string, "GNU Fortran"))
25021 language = DW_LANG_Fortran95;
25022 if (dwarf_version >= 5 /* || !dwarf_strict */)
25024 if (strcmp (language_string, "GNU Fortran2003") == 0)
25025 language = DW_LANG_Fortran03;
25026 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25027 language = DW_LANG_Fortran08;
25030 else if (strcmp (language_string, "GNU Objective-C") == 0)
25031 language = DW_LANG_ObjC;
25032 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25033 language = DW_LANG_ObjC_plus_plus;
25034 else if (strcmp (language_string, "GNU D") == 0)
25035 language = DW_LANG_D;
25036 else if (dwarf_version >= 5 || !dwarf_strict)
25038 if (strcmp (language_string, "GNU Go") == 0)
25039 language = DW_LANG_Go;
25042 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25043 else if (startswith (language_string, "GNU Fortran"))
25044 language = DW_LANG_Fortran90;
25045 /* Likewise for Ada. */
25046 else if (strcmp (language_string, "GNU Ada") == 0)
25047 language = DW_LANG_Ada83;
25049 add_AT_unsigned (die, DW_AT_language, language);
25051 switch (language)
25053 case DW_LANG_Fortran77:
25054 case DW_LANG_Fortran90:
25055 case DW_LANG_Fortran95:
25056 case DW_LANG_Fortran03:
25057 case DW_LANG_Fortran08:
25058 /* Fortran has case insensitive identifiers and the front-end
25059 lowercases everything. */
25060 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25061 break;
25062 default:
25063 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25064 break;
25066 return die;
25069 /* Generate the DIE for a base class. */
25071 static void
25072 gen_inheritance_die (tree binfo, tree access, tree type,
25073 dw_die_ref context_die)
25075 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25076 struct vlr_context ctx = { type, NULL };
25078 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25079 context_die);
25080 add_data_member_location_attribute (die, binfo, &ctx);
25082 if (BINFO_VIRTUAL_P (binfo))
25083 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25085 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25086 children, otherwise the default is DW_ACCESS_public. In DWARF2
25087 the default has always been DW_ACCESS_private. */
25088 if (access == access_public_node)
25090 if (dwarf_version == 2
25091 || context_die->die_tag == DW_TAG_class_type)
25092 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25094 else if (access == access_protected_node)
25095 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25096 else if (dwarf_version > 2
25097 && context_die->die_tag != DW_TAG_class_type)
25098 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25101 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25102 structure. */
25104 static bool
25105 is_variant_part (tree decl)
25107 return (TREE_CODE (decl) == FIELD_DECL
25108 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25111 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25112 return the FIELD_DECL. Return NULL_TREE otherwise. */
25114 static tree
25115 analyze_discr_in_predicate (tree operand, tree struct_type)
25117 while (CONVERT_EXPR_P (operand))
25118 operand = TREE_OPERAND (operand, 0);
25120 /* Match field access to members of struct_type only. */
25121 if (TREE_CODE (operand) == COMPONENT_REF
25122 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25123 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25124 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25125 return TREE_OPERAND (operand, 1);
25126 else
25127 return NULL_TREE;
25130 /* Check that SRC is a constant integer that can be represented as a native
25131 integer constant (either signed or unsigned). If so, store it into DEST and
25132 return true. Return false otherwise. */
25134 static bool
25135 get_discr_value (tree src, dw_discr_value *dest)
25137 tree discr_type = TREE_TYPE (src);
25139 if (lang_hooks.types.get_debug_type)
25141 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25142 if (debug_type != NULL)
25143 discr_type = debug_type;
25146 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25147 return false;
25149 /* Signedness can vary between the original type and the debug type. This
25150 can happen for character types in Ada for instance: the character type
25151 used for code generation can be signed, to be compatible with the C one,
25152 but from a debugger point of view, it must be unsigned. */
25153 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25154 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25156 if (is_orig_unsigned != is_debug_unsigned)
25157 src = fold_convert (discr_type, src);
25159 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25160 return false;
25162 dest->pos = is_debug_unsigned;
25163 if (is_debug_unsigned)
25164 dest->v.uval = tree_to_uhwi (src);
25165 else
25166 dest->v.sval = tree_to_shwi (src);
25168 return true;
25171 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25172 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25173 store NULL_TREE in DISCR_DECL. Otherwise:
25175 - store the discriminant field in STRUCT_TYPE that controls the variant
25176 part to *DISCR_DECL
25178 - put in *DISCR_LISTS_P an array where for each variant, the item
25179 represents the corresponding matching list of discriminant values.
25181 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25182 the above array.
25184 Note that when the array is allocated (i.e. when the analysis is
25185 successful), it is up to the caller to free the array. */
25187 static void
25188 analyze_variants_discr (tree variant_part_decl,
25189 tree struct_type,
25190 tree *discr_decl,
25191 dw_discr_list_ref **discr_lists_p,
25192 unsigned *discr_lists_length)
25194 tree variant_part_type = TREE_TYPE (variant_part_decl);
25195 tree variant;
25196 dw_discr_list_ref *discr_lists;
25197 unsigned i;
25199 /* Compute how many variants there are in this variant part. */
25200 *discr_lists_length = 0;
25201 for (variant = TYPE_FIELDS (variant_part_type);
25202 variant != NULL_TREE;
25203 variant = DECL_CHAIN (variant))
25204 ++*discr_lists_length;
25206 *discr_decl = NULL_TREE;
25207 *discr_lists_p
25208 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25209 sizeof (**discr_lists_p));
25210 discr_lists = *discr_lists_p;
25212 /* And then analyze all variants to extract discriminant information for all
25213 of them. This analysis is conservative: as soon as we detect something we
25214 do not support, abort everything and pretend we found nothing. */
25215 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25216 variant != NULL_TREE;
25217 variant = DECL_CHAIN (variant), ++i)
25219 tree match_expr = DECL_QUALIFIER (variant);
25221 /* Now, try to analyze the predicate and deduce a discriminant for
25222 it. */
25223 if (match_expr == boolean_true_node)
25224 /* Typically happens for the default variant: it matches all cases that
25225 previous variants rejected. Don't output any matching value for
25226 this one. */
25227 continue;
25229 /* The following loop tries to iterate over each discriminant
25230 possibility: single values or ranges. */
25231 while (match_expr != NULL_TREE)
25233 tree next_round_match_expr;
25234 tree candidate_discr = NULL_TREE;
25235 dw_discr_list_ref new_node = NULL;
25237 /* Possibilities are matched one after the other by nested
25238 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25239 continue with the rest at next iteration. */
25240 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25242 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25243 match_expr = TREE_OPERAND (match_expr, 1);
25245 else
25246 next_round_match_expr = NULL_TREE;
25248 if (match_expr == boolean_false_node)
25249 /* This sub-expression matches nothing: just wait for the next
25250 one. */
25253 else if (TREE_CODE (match_expr) == EQ_EXPR)
25255 /* We are matching: <discr_field> == <integer_cst>
25256 This sub-expression matches a single value. */
25257 tree integer_cst = TREE_OPERAND (match_expr, 1);
25259 candidate_discr
25260 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25261 struct_type);
25263 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25264 if (!get_discr_value (integer_cst,
25265 &new_node->dw_discr_lower_bound))
25266 goto abort;
25267 new_node->dw_discr_range = false;
25270 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25272 /* We are matching:
25273 <discr_field> > <integer_cst>
25274 && <discr_field> < <integer_cst>.
25275 This sub-expression matches the range of values between the
25276 two matched integer constants. Note that comparisons can be
25277 inclusive or exclusive. */
25278 tree candidate_discr_1, candidate_discr_2;
25279 tree lower_cst, upper_cst;
25280 bool lower_cst_included, upper_cst_included;
25281 tree lower_op = TREE_OPERAND (match_expr, 0);
25282 tree upper_op = TREE_OPERAND (match_expr, 1);
25284 /* When the comparison is exclusive, the integer constant is not
25285 the discriminant range bound we are looking for: we will have
25286 to increment or decrement it. */
25287 if (TREE_CODE (lower_op) == GE_EXPR)
25288 lower_cst_included = true;
25289 else if (TREE_CODE (lower_op) == GT_EXPR)
25290 lower_cst_included = false;
25291 else
25292 goto abort;
25294 if (TREE_CODE (upper_op) == LE_EXPR)
25295 upper_cst_included = true;
25296 else if (TREE_CODE (upper_op) == LT_EXPR)
25297 upper_cst_included = false;
25298 else
25299 goto abort;
25301 /* Extract the discriminant from the first operand and check it
25302 is consistant with the same analysis in the second
25303 operand. */
25304 candidate_discr_1
25305 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25306 struct_type);
25307 candidate_discr_2
25308 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25309 struct_type);
25310 if (candidate_discr_1 == candidate_discr_2)
25311 candidate_discr = candidate_discr_1;
25312 else
25313 goto abort;
25315 /* Extract bounds from both. */
25316 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25317 lower_cst = TREE_OPERAND (lower_op, 1);
25318 upper_cst = TREE_OPERAND (upper_op, 1);
25320 if (!lower_cst_included)
25321 lower_cst
25322 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25323 build_int_cst (TREE_TYPE (lower_cst), 1));
25324 if (!upper_cst_included)
25325 upper_cst
25326 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25327 build_int_cst (TREE_TYPE (upper_cst), 1));
25329 if (!get_discr_value (lower_cst,
25330 &new_node->dw_discr_lower_bound)
25331 || !get_discr_value (upper_cst,
25332 &new_node->dw_discr_upper_bound))
25333 goto abort;
25335 new_node->dw_discr_range = true;
25338 else if ((candidate_discr
25339 = analyze_discr_in_predicate (match_expr, struct_type))
25340 && (TREE_TYPE (candidate_discr) == boolean_type_node
25341 || TREE_TYPE (TREE_TYPE (candidate_discr))
25342 == boolean_type_node))
25344 /* We are matching: <discr_field> for a boolean discriminant.
25345 This sub-expression matches boolean_true_node. */
25346 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25347 if (!get_discr_value (boolean_true_node,
25348 &new_node->dw_discr_lower_bound))
25349 goto abort;
25350 new_node->dw_discr_range = false;
25353 else
25354 /* Unsupported sub-expression: we cannot determine the set of
25355 matching discriminant values. Abort everything. */
25356 goto abort;
25358 /* If the discriminant info is not consistant with what we saw so
25359 far, consider the analysis failed and abort everything. */
25360 if (candidate_discr == NULL_TREE
25361 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25362 goto abort;
25363 else
25364 *discr_decl = candidate_discr;
25366 if (new_node != NULL)
25368 new_node->dw_discr_next = discr_lists[i];
25369 discr_lists[i] = new_node;
25371 match_expr = next_round_match_expr;
25375 /* If we reach this point, we could match everything we were interested
25376 in. */
25377 return;
25379 abort:
25380 /* Clean all data structure and return no result. */
25381 free (*discr_lists_p);
25382 *discr_lists_p = NULL;
25383 *discr_decl = NULL_TREE;
25386 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25387 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25388 under CONTEXT_DIE.
25390 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25391 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25392 this type, which are record types, represent the available variants and each
25393 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25394 values are inferred from these attributes.
25396 In trees, the offsets for the fields inside these sub-records are relative
25397 to the variant part itself, whereas the corresponding DIEs should have
25398 offset attributes that are relative to the embedding record base address.
25399 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25400 must be an expression that computes the offset of the variant part to
25401 describe in DWARF. */
25403 static void
25404 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25405 dw_die_ref context_die)
25407 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25408 tree variant_part_offset = vlr_ctx->variant_part_offset;
25410 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25411 NULL_TREE if there is no such field. */
25412 tree discr_decl = NULL_TREE;
25413 dw_discr_list_ref *discr_lists;
25414 unsigned discr_lists_length = 0;
25415 unsigned i;
25417 dw_die_ref dwarf_proc_die = NULL;
25418 dw_die_ref variant_part_die
25419 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25421 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25423 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25424 &discr_decl, &discr_lists, &discr_lists_length);
25426 if (discr_decl != NULL_TREE)
25428 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25430 if (discr_die)
25431 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25432 else
25433 /* We have no DIE for the discriminant, so just discard all
25434 discrimimant information in the output. */
25435 discr_decl = NULL_TREE;
25438 /* If the offset for this variant part is more complex than a constant,
25439 create a DWARF procedure for it so that we will not have to generate
25440 DWARF expressions for it for each member. */
25441 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25442 && (dwarf_version >= 3 || !dwarf_strict))
25444 struct loc_descr_context ctx = {
25445 vlr_ctx->struct_type, /* context_type */
25446 NULL_TREE, /* base_decl */
25447 NULL, /* dpi */
25448 false, /* placeholder_arg */
25449 false, /* placeholder_seen */
25450 false /* strict_signedness */
25452 const tree dwarf_proc_fndecl
25453 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25454 build_function_type (TREE_TYPE (variant_part_offset),
25455 NULL_TREE));
25456 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25457 const dw_loc_descr_ref dwarf_proc_body
25458 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25460 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25461 dwarf_proc_fndecl, context_die);
25462 if (dwarf_proc_die != NULL)
25463 variant_part_offset = dwarf_proc_call;
25466 /* Output DIEs for all variants. */
25467 i = 0;
25468 for (tree variant = TYPE_FIELDS (variant_part_type);
25469 variant != NULL_TREE;
25470 variant = DECL_CHAIN (variant), ++i)
25472 tree variant_type = TREE_TYPE (variant);
25473 dw_die_ref variant_die;
25475 /* All variants (i.e. members of a variant part) are supposed to be
25476 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25477 under these records. */
25478 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25480 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25481 equate_decl_number_to_die (variant, variant_die);
25483 /* Output discriminant values this variant matches, if any. */
25484 if (discr_decl == NULL || discr_lists[i] == NULL)
25485 /* In the case we have discriminant information at all, this is
25486 probably the default variant: as the standard says, don't
25487 output any discriminant value/list attribute. */
25489 else if (discr_lists[i]->dw_discr_next == NULL
25490 && !discr_lists[i]->dw_discr_range)
25491 /* If there is only one accepted value, don't bother outputting a
25492 list. */
25493 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25494 else
25495 add_discr_list (variant_die, discr_lists[i]);
25497 for (tree member = TYPE_FIELDS (variant_type);
25498 member != NULL_TREE;
25499 member = DECL_CHAIN (member))
25501 struct vlr_context vlr_sub_ctx = {
25502 vlr_ctx->struct_type, /* struct_type */
25503 NULL /* variant_part_offset */
25505 if (is_variant_part (member))
25507 /* All offsets for fields inside variant parts are relative to
25508 the top-level embedding RECORD_TYPE's base address. On the
25509 other hand, offsets in GCC's types are relative to the
25510 nested-most variant part. So we have to sum offsets each time
25511 we recurse. */
25513 vlr_sub_ctx.variant_part_offset
25514 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25515 variant_part_offset, byte_position (member));
25516 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25518 else
25520 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25521 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25526 free (discr_lists);
25529 /* Generate a DIE for a class member. */
25531 static void
25532 gen_member_die (tree type, dw_die_ref context_die)
25534 tree member;
25535 tree binfo = TYPE_BINFO (type);
25537 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25539 /* If this is not an incomplete type, output descriptions of each of its
25540 members. Note that as we output the DIEs necessary to represent the
25541 members of this record or union type, we will also be trying to output
25542 DIEs to represent the *types* of those members. However the `type'
25543 function (above) will specifically avoid generating type DIEs for member
25544 types *within* the list of member DIEs for this (containing) type except
25545 for those types (of members) which are explicitly marked as also being
25546 members of this (containing) type themselves. The g++ front- end can
25547 force any given type to be treated as a member of some other (containing)
25548 type by setting the TYPE_CONTEXT of the given (member) type to point to
25549 the TREE node representing the appropriate (containing) type. */
25551 /* First output info about the base classes. */
25552 if (binfo && early_dwarf)
25554 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25555 int i;
25556 tree base;
25558 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25559 gen_inheritance_die (base,
25560 (accesses ? (*accesses)[i] : access_public_node),
25561 type,
25562 context_die);
25565 /* Now output info about the members. */
25566 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25568 /* Ignore clones. */
25569 if (DECL_ABSTRACT_ORIGIN (member))
25570 continue;
25572 struct vlr_context vlr_ctx = { type, NULL_TREE };
25573 bool static_inline_p
25574 = (VAR_P (member)
25575 && TREE_STATIC (member)
25576 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25577 != -1));
25579 /* If we thought we were generating minimal debug info for TYPE
25580 and then changed our minds, some of the member declarations
25581 may have already been defined. Don't define them again, but
25582 do put them in the right order. */
25584 if (dw_die_ref child = lookup_decl_die (member))
25586 /* Handle inline static data members, which only have in-class
25587 declarations. */
25588 bool splice = true;
25590 dw_die_ref ref = NULL;
25591 if (child->die_tag == DW_TAG_variable
25592 && child->die_parent == comp_unit_die ())
25594 ref = get_AT_ref (child, DW_AT_specification);
25596 /* For C++17 inline static data members followed by redundant
25597 out of class redeclaration, we might get here with
25598 child being the DIE created for the out of class
25599 redeclaration and with its DW_AT_specification being
25600 the DIE created for in-class definition. We want to
25601 reparent the latter, and don't want to create another
25602 DIE with DW_AT_specification in that case, because
25603 we already have one. */
25604 if (ref
25605 && static_inline_p
25606 && ref->die_tag == DW_TAG_variable
25607 && ref->die_parent == comp_unit_die ()
25608 && get_AT (ref, DW_AT_specification) == NULL)
25610 child = ref;
25611 ref = NULL;
25612 static_inline_p = false;
25615 if (!ref)
25617 reparent_child (child, context_die);
25618 if (dwarf_version < 5)
25619 child->die_tag = DW_TAG_member;
25620 splice = false;
25623 else if (child->die_tag == DW_TAG_enumerator)
25624 /* Enumerators remain under their enumeration even if
25625 their names are introduced in the enclosing scope. */
25626 splice = false;
25628 if (splice)
25629 splice_child_die (context_die, child);
25632 /* Do not generate standard DWARF for variant parts if we are generating
25633 the corresponding GNAT encodings: DIEs generated for both would
25634 conflict in our mappings. */
25635 else if (is_variant_part (member)
25636 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
25638 vlr_ctx.variant_part_offset = byte_position (member);
25639 gen_variant_part (member, &vlr_ctx, context_die);
25641 else
25643 vlr_ctx.variant_part_offset = NULL_TREE;
25644 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25647 /* For C++ inline static data members emit immediately a DW_TAG_variable
25648 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25649 DW_AT_specification. */
25650 if (static_inline_p)
25652 int old_extern = DECL_EXTERNAL (member);
25653 DECL_EXTERNAL (member) = 0;
25654 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25655 DECL_EXTERNAL (member) = old_extern;
25660 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25661 is set, we pretend that the type was never defined, so we only get the
25662 member DIEs needed by later specification DIEs. */
25664 static void
25665 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25666 enum debug_info_usage usage)
25668 if (TREE_ASM_WRITTEN (type))
25670 /* Fill in the bound of variable-length fields in late dwarf if
25671 still incomplete. */
25672 if (!early_dwarf && variably_modified_type_p (type, NULL))
25673 for (tree member = TYPE_FIELDS (type);
25674 member;
25675 member = DECL_CHAIN (member))
25676 fill_variable_array_bounds (TREE_TYPE (member));
25677 return;
25680 dw_die_ref type_die = lookup_type_die (type);
25681 dw_die_ref scope_die = 0;
25682 int nested = 0;
25683 int complete = (TYPE_SIZE (type)
25684 && (! TYPE_STUB_DECL (type)
25685 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25686 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25687 complete = complete && should_emit_struct_debug (type, usage);
25689 if (type_die && ! complete)
25690 return;
25692 if (TYPE_CONTEXT (type) != NULL_TREE
25693 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25694 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25695 nested = 1;
25697 scope_die = scope_die_for (type, context_die);
25699 /* Generate child dies for template parameters. */
25700 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25701 schedule_generic_params_dies_gen (type);
25703 if (! type_die || (nested && is_cu_die (scope_die)))
25704 /* First occurrence of type or toplevel definition of nested class. */
25706 dw_die_ref old_die = type_die;
25708 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25709 ? record_type_tag (type) : DW_TAG_union_type,
25710 scope_die, type);
25711 equate_type_number_to_die (type, type_die);
25712 if (old_die)
25713 add_AT_specification (type_die, old_die);
25714 else
25715 add_name_attribute (type_die, type_tag (type));
25717 else
25718 remove_AT (type_die, DW_AT_declaration);
25720 /* If this type has been completed, then give it a byte_size attribute and
25721 then give a list of members. */
25722 if (complete && !ns_decl)
25724 /* Prevent infinite recursion in cases where the type of some member of
25725 this type is expressed in terms of this type itself. */
25726 TREE_ASM_WRITTEN (type) = 1;
25727 add_byte_size_attribute (type_die, type);
25728 add_alignment_attribute (type_die, type);
25729 if (TYPE_STUB_DECL (type) != NULL_TREE)
25731 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25732 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25735 /* If the first reference to this type was as the return type of an
25736 inline function, then it may not have a parent. Fix this now. */
25737 if (type_die->die_parent == NULL)
25738 add_child_die (scope_die, type_die);
25740 gen_member_die (type, type_die);
25742 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25743 if (TYPE_ARTIFICIAL (type))
25744 add_AT_flag (type_die, DW_AT_artificial, 1);
25746 /* GNU extension: Record what type our vtable lives in. */
25747 if (TYPE_VFIELD (type))
25749 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25751 gen_type_die (vtype, context_die);
25752 add_AT_die_ref (type_die, DW_AT_containing_type,
25753 lookup_type_die (vtype));
25756 else
25758 add_AT_flag (type_die, DW_AT_declaration, 1);
25760 /* We don't need to do this for function-local types. */
25761 if (TYPE_STUB_DECL (type)
25762 && ! decl_function_context (TYPE_STUB_DECL (type)))
25763 vec_safe_push (incomplete_types, type);
25766 if (get_AT (type_die, DW_AT_name))
25767 add_pubtype (type, type_die);
25770 /* Generate a DIE for a subroutine _type_. */
25772 static void
25773 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25775 tree return_type = TREE_TYPE (type);
25776 dw_die_ref subr_die
25777 = new_die (DW_TAG_subroutine_type,
25778 scope_die_for (type, context_die), type);
25780 equate_type_number_to_die (type, subr_die);
25781 add_prototyped_attribute (subr_die, type);
25782 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25783 context_die);
25784 add_alignment_attribute (subr_die, type);
25785 gen_formal_types_die (type, subr_die);
25787 if (get_AT (subr_die, DW_AT_name))
25788 add_pubtype (type, subr_die);
25789 if ((dwarf_version >= 5 || !dwarf_strict)
25790 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25791 add_AT_flag (subr_die, DW_AT_reference, 1);
25792 if ((dwarf_version >= 5 || !dwarf_strict)
25793 && lang_hooks.types.type_dwarf_attribute (type,
25794 DW_AT_rvalue_reference) != -1)
25795 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25798 /* Generate a DIE for a type definition. */
25800 static void
25801 gen_typedef_die (tree decl, dw_die_ref context_die)
25803 dw_die_ref type_die;
25804 tree type;
25806 if (TREE_ASM_WRITTEN (decl))
25808 if (DECL_ORIGINAL_TYPE (decl))
25809 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25810 return;
25813 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25814 checks in process_scope_var and modified_type_die), this should be called
25815 only for original types. */
25816 gcc_assert (decl_ultimate_origin (decl) == NULL
25817 || decl_ultimate_origin (decl) == decl);
25819 TREE_ASM_WRITTEN (decl) = 1;
25820 type_die = new_die (DW_TAG_typedef, context_die, decl);
25822 add_name_and_src_coords_attributes (type_die, decl);
25823 if (DECL_ORIGINAL_TYPE (decl))
25825 type = DECL_ORIGINAL_TYPE (decl);
25826 if (type == error_mark_node)
25827 return;
25829 gcc_assert (type != TREE_TYPE (decl));
25830 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25832 else
25834 type = TREE_TYPE (decl);
25835 if (type == error_mark_node)
25836 return;
25838 if (is_naming_typedef_decl (TYPE_NAME (type)))
25840 /* Here, we are in the case of decl being a typedef naming
25841 an anonymous type, e.g:
25842 typedef struct {...} foo;
25843 In that case TREE_TYPE (decl) is not a typedef variant
25844 type and TYPE_NAME of the anonymous type is set to the
25845 TYPE_DECL of the typedef. This construct is emitted by
25846 the C++ FE.
25848 TYPE is the anonymous struct named by the typedef
25849 DECL. As we need the DW_AT_type attribute of the
25850 DW_TAG_typedef to point to the DIE of TYPE, let's
25851 generate that DIE right away. add_type_attribute
25852 called below will then pick (via lookup_type_die) that
25853 anonymous struct DIE. */
25854 if (!TREE_ASM_WRITTEN (type))
25855 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25857 /* This is a GNU Extension. We are adding a
25858 DW_AT_linkage_name attribute to the DIE of the
25859 anonymous struct TYPE. The value of that attribute
25860 is the name of the typedef decl naming the anonymous
25861 struct. This greatly eases the work of consumers of
25862 this debug info. */
25863 add_linkage_name_raw (lookup_type_die (type), decl);
25867 add_type_attribute (type_die, type, decl_quals (decl), false,
25868 context_die);
25870 if (is_naming_typedef_decl (decl))
25871 /* We want that all subsequent calls to lookup_type_die with
25872 TYPE in argument yield the DW_TAG_typedef we have just
25873 created. */
25874 equate_type_number_to_die (type, type_die);
25876 add_alignment_attribute (type_die, TREE_TYPE (decl));
25878 add_accessibility_attribute (type_die, decl);
25880 if (DECL_ABSTRACT_P (decl))
25881 equate_decl_number_to_die (decl, type_die);
25883 if (get_AT (type_die, DW_AT_name))
25884 add_pubtype (decl, type_die);
25887 /* Generate a DIE for a struct, class, enum or union type. */
25889 static void
25890 gen_tagged_type_die (tree type,
25891 dw_die_ref context_die,
25892 enum debug_info_usage usage)
25894 if (type == NULL_TREE
25895 || !is_tagged_type (type))
25896 return;
25898 if (TREE_ASM_WRITTEN (type))
25900 /* If this is a nested type whose containing class hasn't been written
25901 out yet, writing it out will cover this one, too. This does not apply
25902 to instantiations of member class templates; they need to be added to
25903 the containing class as they are generated. FIXME: This hurts the
25904 idea of combining type decls from multiple TUs, since we can't predict
25905 what set of template instantiations we'll get. */
25906 else if (TYPE_CONTEXT (type)
25907 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25908 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25910 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25912 if (TREE_ASM_WRITTEN (type))
25913 return;
25915 /* If that failed, attach ourselves to the stub. */
25916 context_die = lookup_type_die (TYPE_CONTEXT (type));
25918 else if (TYPE_CONTEXT (type) != NULL_TREE
25919 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25921 /* If this type is local to a function that hasn't been written
25922 out yet, use a NULL context for now; it will be fixed up in
25923 decls_for_scope. */
25924 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25925 /* A declaration DIE doesn't count; nested types need to go in the
25926 specification. */
25927 if (context_die && is_declaration_die (context_die))
25928 context_die = NULL;
25930 else
25931 context_die = declare_in_namespace (type, context_die);
25933 if (TREE_CODE (type) == ENUMERAL_TYPE)
25935 /* This might have been written out by the call to
25936 declare_in_namespace. */
25937 if (!TREE_ASM_WRITTEN (type))
25938 gen_enumeration_type_die (type, context_die);
25940 else
25941 gen_struct_or_union_type_die (type, context_die, usage);
25943 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25944 it up if it is ever completed. gen_*_type_die will set it for us
25945 when appropriate. */
25948 /* Generate a type description DIE. */
25950 static void
25951 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25952 enum debug_info_usage usage)
25954 struct array_descr_info info;
25956 if (type == NULL_TREE || type == error_mark_node)
25957 return;
25959 if (flag_checking && type)
25960 verify_type (type);
25962 if (TYPE_NAME (type) != NULL_TREE
25963 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25964 && is_redundant_typedef (TYPE_NAME (type))
25965 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25966 /* The DECL of this type is a typedef we don't want to emit debug
25967 info for but we want debug info for its underlying typedef.
25968 This can happen for e.g, the injected-class-name of a C++
25969 type. */
25970 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25972 /* If TYPE is a typedef type variant, let's generate debug info
25973 for the parent typedef which TYPE is a type of. */
25974 if (typedef_variant_p (type))
25976 if (TREE_ASM_WRITTEN (type))
25977 return;
25979 tree name = TYPE_NAME (type);
25980 tree origin = decl_ultimate_origin (name);
25981 if (origin != NULL && origin != name)
25983 gen_decl_die (origin, NULL, NULL, context_die);
25984 return;
25987 /* Prevent broken recursion; we can't hand off to the same type. */
25988 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25990 /* Give typedefs the right scope. */
25991 context_die = scope_die_for (type, context_die);
25993 TREE_ASM_WRITTEN (type) = 1;
25995 gen_decl_die (name, NULL, NULL, context_die);
25996 return;
25999 /* If type is an anonymous tagged type named by a typedef, let's
26000 generate debug info for the typedef. */
26001 if (is_naming_typedef_decl (TYPE_NAME (type)))
26003 /* Give typedefs the right scope. */
26004 context_die = scope_die_for (type, context_die);
26006 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26007 return;
26010 if (lang_hooks.types.get_debug_type)
26012 tree debug_type = lang_hooks.types.get_debug_type (type);
26014 if (debug_type != NULL_TREE && debug_type != type)
26016 gen_type_die_with_usage (debug_type, context_die, usage);
26017 return;
26021 /* We are going to output a DIE to represent the unqualified version
26022 of this type (i.e. without any const or volatile qualifiers) so
26023 get the main variant (i.e. the unqualified version) of this type
26024 now. (Vectors and arrays are special because the debugging info is in the
26025 cloned type itself. Similarly function/method types can contain extra
26026 ref-qualification). */
26027 if (TREE_CODE (type) == FUNCTION_TYPE
26028 || TREE_CODE (type) == METHOD_TYPE)
26030 /* For function/method types, can't use type_main_variant here,
26031 because that can have different ref-qualifiers for C++,
26032 but try to canonicalize. */
26033 tree main = TYPE_MAIN_VARIANT (type);
26034 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26035 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26036 && check_base_type (t, main)
26037 && check_lang_type (t, type))
26039 type = t;
26040 break;
26043 else if (TREE_CODE (type) != VECTOR_TYPE
26044 && TREE_CODE (type) != ARRAY_TYPE)
26045 type = type_main_variant (type);
26047 /* If this is an array type with hidden descriptor, handle it first. */
26048 if (!TREE_ASM_WRITTEN (type)
26049 && lang_hooks.types.get_array_descr_info)
26051 memset (&info, 0, sizeof (info));
26052 if (lang_hooks.types.get_array_descr_info (type, &info))
26054 /* Fortran sometimes emits array types with no dimension. */
26055 gcc_assert (info.ndimensions >= 0
26056 && (info.ndimensions
26057 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26058 gen_descr_array_type_die (type, &info, context_die);
26059 TREE_ASM_WRITTEN (type) = 1;
26060 return;
26064 if (TREE_ASM_WRITTEN (type))
26066 /* Variable-length types may be incomplete even if
26067 TREE_ASM_WRITTEN. For such types, fall through to
26068 gen_array_type_die() and possibly fill in
26069 DW_AT_{upper,lower}_bound attributes. */
26070 if ((TREE_CODE (type) != ARRAY_TYPE
26071 && TREE_CODE (type) != RECORD_TYPE
26072 && TREE_CODE (type) != UNION_TYPE
26073 && TREE_CODE (type) != QUAL_UNION_TYPE)
26074 || !variably_modified_type_p (type, NULL))
26075 return;
26078 switch (TREE_CODE (type))
26080 case ERROR_MARK:
26081 break;
26083 case POINTER_TYPE:
26084 case REFERENCE_TYPE:
26085 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26086 ensures that the gen_type_die recursion will terminate even if the
26087 type is recursive. Recursive types are possible in Ada. */
26088 /* ??? We could perhaps do this for all types before the switch
26089 statement. */
26090 TREE_ASM_WRITTEN (type) = 1;
26092 /* For these types, all that is required is that we output a DIE (or a
26093 set of DIEs) to represent the "basis" type. */
26094 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26095 DINFO_USAGE_IND_USE);
26096 break;
26098 case OFFSET_TYPE:
26099 /* This code is used for C++ pointer-to-data-member types.
26100 Output a description of the relevant class type. */
26101 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26102 DINFO_USAGE_IND_USE);
26104 /* Output a description of the type of the object pointed to. */
26105 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26106 DINFO_USAGE_IND_USE);
26108 /* Now output a DIE to represent this pointer-to-data-member type
26109 itself. */
26110 gen_ptr_to_mbr_type_die (type, context_die);
26111 break;
26113 case FUNCTION_TYPE:
26114 /* Force out return type (in case it wasn't forced out already). */
26115 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26116 DINFO_USAGE_DIR_USE);
26117 gen_subroutine_type_die (type, context_die);
26118 break;
26120 case METHOD_TYPE:
26121 /* Force out return type (in case it wasn't forced out already). */
26122 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26123 DINFO_USAGE_DIR_USE);
26124 gen_subroutine_type_die (type, context_die);
26125 break;
26127 case ARRAY_TYPE:
26128 case VECTOR_TYPE:
26129 gen_array_type_die (type, context_die);
26130 break;
26132 case ENUMERAL_TYPE:
26133 case RECORD_TYPE:
26134 case UNION_TYPE:
26135 case QUAL_UNION_TYPE:
26136 gen_tagged_type_die (type, context_die, usage);
26137 return;
26139 case VOID_TYPE:
26140 case OPAQUE_TYPE:
26141 case INTEGER_TYPE:
26142 case REAL_TYPE:
26143 case FIXED_POINT_TYPE:
26144 case COMPLEX_TYPE:
26145 case BOOLEAN_TYPE:
26146 /* No DIEs needed for fundamental types. */
26147 break;
26149 case NULLPTR_TYPE:
26150 case LANG_TYPE:
26151 /* Just use DW_TAG_unspecified_type. */
26153 dw_die_ref type_die = lookup_type_die (type);
26154 if (type_die == NULL)
26156 tree name = TYPE_IDENTIFIER (type);
26157 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26158 type);
26159 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26160 equate_type_number_to_die (type, type_die);
26163 break;
26165 default:
26166 if (is_cxx_auto (type))
26168 tree name = TYPE_IDENTIFIER (type);
26169 dw_die_ref *die = (name == get_identifier ("auto")
26170 ? &auto_die : &decltype_auto_die);
26171 if (!*die)
26173 *die = new_die (DW_TAG_unspecified_type,
26174 comp_unit_die (), NULL_TREE);
26175 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26177 equate_type_number_to_die (type, *die);
26178 break;
26180 gcc_unreachable ();
26183 TREE_ASM_WRITTEN (type) = 1;
26186 static void
26187 gen_type_die (tree type, dw_die_ref context_die)
26189 if (type != error_mark_node)
26191 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
26192 if (flag_checking)
26194 dw_die_ref die = lookup_type_die (type);
26195 if (die)
26196 check_die (die);
26201 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26202 things which are local to the given block. */
26204 static void
26205 gen_block_die (tree stmt, dw_die_ref context_die)
26207 int must_output_die = 0;
26208 bool inlined_func;
26210 /* Ignore blocks that are NULL. */
26211 if (stmt == NULL_TREE)
26212 return;
26214 inlined_func = inlined_function_outer_scope_p (stmt);
26216 /* If the block is one fragment of a non-contiguous block, do not
26217 process the variables, since they will have been done by the
26218 origin block. Do process subblocks. */
26219 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26221 tree sub;
26223 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26224 gen_block_die (sub, context_die);
26226 return;
26229 /* Determine if we need to output any Dwarf DIEs at all to represent this
26230 block. */
26231 if (inlined_func)
26232 /* The outer scopes for inlinings *must* always be represented. We
26233 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26234 must_output_die = 1;
26235 else if (lookup_block_die (stmt))
26236 /* If we already have a DIE then it was filled early. Meanwhile
26237 we might have pruned all BLOCK_VARS as optimized out but we
26238 still want to generate high/low PC attributes so output it. */
26239 must_output_die = 1;
26240 else if (TREE_USED (stmt)
26241 || TREE_ASM_WRITTEN (stmt))
26243 /* Determine if this block directly contains any "significant"
26244 local declarations which we will need to output DIEs for. */
26245 if (debug_info_level > DINFO_LEVEL_TERSE)
26247 /* We are not in terse mode so any local declaration that
26248 is not ignored for debug purposes counts as being a
26249 "significant" one. */
26250 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26251 must_output_die = 1;
26252 else
26253 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26254 if (!DECL_IGNORED_P (var))
26256 must_output_die = 1;
26257 break;
26260 else if (!dwarf2out_ignore_block (stmt))
26261 must_output_die = 1;
26264 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26265 DIE for any block which contains no significant local declarations at
26266 all. Rather, in such cases we just call `decls_for_scope' so that any
26267 needed Dwarf info for any sub-blocks will get properly generated. Note
26268 that in terse mode, our definition of what constitutes a "significant"
26269 local declaration gets restricted to include only inlined function
26270 instances and local (nested) function definitions. */
26271 if (must_output_die)
26273 if (inlined_func)
26274 gen_inlined_subroutine_die (stmt, context_die);
26275 else
26276 gen_lexical_block_die (stmt, context_die);
26278 else
26279 decls_for_scope (stmt, context_die);
26282 /* Process variable DECL (or variable with origin ORIGIN) within
26283 block STMT and add it to CONTEXT_DIE. */
26284 static void
26285 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26287 dw_die_ref die;
26288 tree decl_or_origin = decl ? decl : origin;
26290 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26291 die = lookup_decl_die (decl_or_origin);
26292 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26294 if (TYPE_DECL_IS_STUB (decl_or_origin))
26295 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26296 else
26297 die = lookup_decl_die (decl_or_origin);
26298 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26299 if (! die && ! early_dwarf)
26300 return;
26302 else
26303 die = NULL;
26305 /* Avoid creating DIEs for local typedefs and concrete static variables that
26306 will only be pruned later. */
26307 if ((origin || decl_ultimate_origin (decl))
26308 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26309 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26311 origin = decl_ultimate_origin (decl_or_origin);
26312 if (decl && VAR_P (decl) && die != NULL)
26314 die = lookup_decl_die (origin);
26315 if (die != NULL)
26316 equate_decl_number_to_die (decl, die);
26318 return;
26321 if (die != NULL && die->die_parent == NULL)
26322 add_child_die (context_die, die);
26323 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26325 if (early_dwarf)
26326 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26327 stmt, context_die);
26329 else
26331 if (decl && DECL_P (decl))
26333 die = lookup_decl_die (decl);
26335 /* Early created DIEs do not have a parent as the decls refer
26336 to the function as DECL_CONTEXT rather than the BLOCK. */
26337 if (die && die->die_parent == NULL)
26339 gcc_assert (in_lto_p);
26340 add_child_die (context_die, die);
26344 gen_decl_die (decl, origin, NULL, context_die);
26348 /* Generate all of the decls declared within a given scope and (recursively)
26349 all of its sub-blocks. */
26351 static void
26352 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26354 tree decl;
26355 unsigned int i;
26356 tree subblocks;
26358 /* Ignore NULL blocks. */
26359 if (stmt == NULL_TREE)
26360 return;
26362 /* Output the DIEs to represent all of the data objects and typedefs
26363 declared directly within this block but not within any nested
26364 sub-blocks. Also, nested function and tag DIEs have been
26365 generated with a parent of NULL; fix that up now. We don't
26366 have to do this if we're at -g1. */
26367 if (debug_info_level > DINFO_LEVEL_TERSE)
26369 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26370 process_scope_var (stmt, decl, NULL_TREE, context_die);
26371 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26372 origin - avoid doing this twice as we have no good way to see
26373 if we've done it once already. */
26374 if (! early_dwarf)
26375 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26377 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26378 if (decl == current_function_decl)
26379 /* Ignore declarations of the current function, while they
26380 are declarations, gen_subprogram_die would treat them
26381 as definitions again, because they are equal to
26382 current_function_decl and endlessly recurse. */;
26383 else if (TREE_CODE (decl) == FUNCTION_DECL)
26384 process_scope_var (stmt, decl, NULL_TREE, context_die);
26385 else
26386 process_scope_var (stmt, NULL_TREE, decl, context_die);
26390 /* Even if we're at -g1, we need to process the subblocks in order to get
26391 inlined call information. */
26393 /* Output the DIEs to represent all sub-blocks (and the items declared
26394 therein) of this block. */
26395 if (recurse)
26396 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26397 subblocks != NULL;
26398 subblocks = BLOCK_CHAIN (subblocks))
26399 gen_block_die (subblocks, context_die);
26402 /* Is this a typedef we can avoid emitting? */
26404 static bool
26405 is_redundant_typedef (const_tree decl)
26407 if (TYPE_DECL_IS_STUB (decl))
26408 return true;
26410 if (DECL_ARTIFICIAL (decl)
26411 && DECL_CONTEXT (decl)
26412 && is_tagged_type (DECL_CONTEXT (decl))
26413 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26414 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26415 /* Also ignore the artificial member typedef for the class name. */
26416 return true;
26418 return false;
26421 /* Return TRUE if TYPE is a typedef that names a type for linkage
26422 purposes. This kind of typedefs is produced by the C++ FE for
26423 constructs like:
26425 typedef struct {...} foo;
26427 In that case, there is no typedef variant type produced for foo.
26428 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26429 struct type. */
26431 static bool
26432 is_naming_typedef_decl (const_tree decl)
26434 if (decl == NULL_TREE
26435 || TREE_CODE (decl) != TYPE_DECL
26436 || DECL_NAMELESS (decl)
26437 || !is_tagged_type (TREE_TYPE (decl))
26438 || DECL_IS_UNDECLARED_BUILTIN (decl)
26439 || is_redundant_typedef (decl)
26440 /* It looks like Ada produces TYPE_DECLs that are very similar
26441 to C++ naming typedefs but that have different
26442 semantics. Let's be specific to c++ for now. */
26443 || !is_cxx (decl))
26444 return FALSE;
26446 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26447 && TYPE_NAME (TREE_TYPE (decl)) == decl
26448 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26449 != TYPE_NAME (TREE_TYPE (decl))));
26452 /* Looks up the DIE for a context. */
26454 static inline dw_die_ref
26455 lookup_context_die (tree context)
26457 if (context)
26459 /* Find die that represents this context. */
26460 if (TYPE_P (context))
26462 context = TYPE_MAIN_VARIANT (context);
26463 dw_die_ref ctx = lookup_type_die (context);
26464 if (!ctx)
26465 return NULL;
26466 return strip_naming_typedef (context, ctx);
26468 else
26469 return lookup_decl_die (context);
26471 return comp_unit_die ();
26474 /* Returns the DIE for a context. */
26476 static inline dw_die_ref
26477 get_context_die (tree context)
26479 if (context)
26481 /* Find die that represents this context. */
26482 if (TYPE_P (context))
26484 context = TYPE_MAIN_VARIANT (context);
26485 return strip_naming_typedef (context, force_type_die (context));
26487 else
26488 return force_decl_die (context);
26490 return comp_unit_die ();
26493 /* Returns the DIE for decl. A DIE will always be returned. */
26495 static dw_die_ref
26496 force_decl_die (tree decl)
26498 dw_die_ref decl_die;
26499 unsigned saved_external_flag;
26500 tree save_fn = NULL_TREE;
26501 decl_die = lookup_decl_die (decl);
26502 if (!decl_die)
26504 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26506 decl_die = lookup_decl_die (decl);
26507 if (decl_die)
26508 return decl_die;
26510 switch (TREE_CODE (decl))
26512 case FUNCTION_DECL:
26513 /* Clear current_function_decl, so that gen_subprogram_die thinks
26514 that this is a declaration. At this point, we just want to force
26515 declaration die. */
26516 save_fn = current_function_decl;
26517 current_function_decl = NULL_TREE;
26518 gen_subprogram_die (decl, context_die);
26519 current_function_decl = save_fn;
26520 break;
26522 case VAR_DECL:
26523 /* Set external flag to force declaration die. Restore it after
26524 gen_decl_die() call. */
26525 saved_external_flag = DECL_EXTERNAL (decl);
26526 DECL_EXTERNAL (decl) = 1;
26527 gen_decl_die (decl, NULL, NULL, context_die);
26528 DECL_EXTERNAL (decl) = saved_external_flag;
26529 break;
26531 case NAMESPACE_DECL:
26532 if (dwarf_version >= 3 || !dwarf_strict)
26533 dwarf2out_decl (decl);
26534 else
26535 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26536 decl_die = comp_unit_die ();
26537 break;
26539 case CONST_DECL:
26540 /* Enumerators shouldn't need force_decl_die. */
26541 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26542 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26543 gen_decl_die (decl, NULL, NULL, context_die);
26544 break;
26546 case TRANSLATION_UNIT_DECL:
26547 decl_die = comp_unit_die ();
26548 break;
26550 default:
26551 gcc_unreachable ();
26554 /* We should be able to find the DIE now. */
26555 if (!decl_die)
26556 decl_die = lookup_decl_die (decl);
26557 gcc_assert (decl_die);
26560 return decl_die;
26563 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26564 always returned. */
26566 static dw_die_ref
26567 force_type_die (tree type)
26569 dw_die_ref type_die;
26571 type_die = lookup_type_die (type);
26572 if (!type_die)
26574 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26576 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26577 false, context_die);
26578 gcc_assert (type_die);
26580 return type_die;
26583 /* Force out any required namespaces to be able to output DECL,
26584 and return the new context_die for it, if it's changed. */
26586 static dw_die_ref
26587 setup_namespace_context (tree thing, dw_die_ref context_die)
26589 tree context = (DECL_P (thing)
26590 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26591 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26592 /* Force out the namespace. */
26593 context_die = force_decl_die (context);
26595 return context_die;
26598 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26599 type) within its namespace, if appropriate.
26601 For compatibility with older debuggers, namespace DIEs only contain
26602 declarations; all definitions are emitted at CU scope, with
26603 DW_AT_specification pointing to the declaration (like with class
26604 members). */
26606 static dw_die_ref
26607 declare_in_namespace (tree thing, dw_die_ref context_die)
26609 dw_die_ref ns_context;
26611 if (debug_info_level <= DINFO_LEVEL_TERSE)
26612 return context_die;
26614 /* External declarations in the local scope only need to be emitted
26615 once, not once in the namespace and once in the scope.
26617 This avoids declaring the `extern' below in the
26618 namespace DIE as well as in the innermost scope:
26620 namespace S
26622 int i=5;
26623 int foo()
26625 int i=8;
26626 extern int i;
26627 return i;
26631 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26632 return context_die;
26634 /* If this decl is from an inlined function, then don't try to emit it in its
26635 namespace, as we will get confused. It would have already been emitted
26636 when the abstract instance of the inline function was emitted anyways. */
26637 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26638 return context_die;
26640 ns_context = setup_namespace_context (thing, context_die);
26642 if (ns_context != context_die)
26644 if (is_fortran () || is_dlang ())
26645 return ns_context;
26646 if (DECL_P (thing))
26647 gen_decl_die (thing, NULL, NULL, ns_context);
26648 else
26649 gen_type_die (thing, ns_context);
26651 return context_die;
26654 /* Generate a DIE for a namespace or namespace alias. */
26656 static void
26657 gen_namespace_die (tree decl, dw_die_ref context_die)
26659 dw_die_ref namespace_die;
26661 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26662 they are an alias of. */
26663 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26665 /* Output a real namespace or module. */
26666 context_die = setup_namespace_context (decl, comp_unit_die ());
26667 namespace_die = new_die (is_fortran () || is_dlang ()
26668 ? DW_TAG_module : DW_TAG_namespace,
26669 context_die, decl);
26670 /* For Fortran modules defined in different CU don't add src coords. */
26671 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26673 const char *name = dwarf2_name (decl, 0);
26674 if (name)
26675 add_name_attribute (namespace_die, name);
26677 else
26678 add_name_and_src_coords_attributes (namespace_die, decl);
26679 if (DECL_EXTERNAL (decl))
26680 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26681 equate_decl_number_to_die (decl, namespace_die);
26683 else
26685 /* Output a namespace alias. */
26687 /* Force out the namespace we are an alias of, if necessary. */
26688 dw_die_ref origin_die
26689 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26691 if (DECL_FILE_SCOPE_P (decl)
26692 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26693 context_die = setup_namespace_context (decl, comp_unit_die ());
26694 /* Now create the namespace alias DIE. */
26695 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26696 add_name_and_src_coords_attributes (namespace_die, decl);
26697 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26698 equate_decl_number_to_die (decl, namespace_die);
26700 if ((dwarf_version >= 5 || !dwarf_strict)
26701 && lang_hooks.decls.decl_dwarf_attribute (decl,
26702 DW_AT_export_symbols) == 1)
26703 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26705 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26706 if (want_pubnames ())
26707 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26710 /* Generate Dwarf debug information for a decl described by DECL.
26711 The return value is currently only meaningful for PARM_DECLs,
26712 for all other decls it returns NULL.
26714 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26715 It can be NULL otherwise. */
26717 static dw_die_ref
26718 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26719 dw_die_ref context_die)
26721 tree decl_or_origin = decl ? decl : origin;
26722 tree class_origin = NULL, ultimate_origin;
26724 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26725 return NULL;
26727 switch (TREE_CODE (decl_or_origin))
26729 case ERROR_MARK:
26730 break;
26732 case CONST_DECL:
26733 if (!is_fortran () && !is_ada () && !is_dlang ())
26735 /* The individual enumerators of an enum type get output when we output
26736 the Dwarf representation of the relevant enum type itself. */
26737 break;
26740 /* Emit its type. */
26741 gen_type_die (TREE_TYPE (decl), context_die);
26743 /* And its containing namespace. */
26744 context_die = declare_in_namespace (decl, context_die);
26746 gen_const_die (decl, context_die);
26747 break;
26749 case FUNCTION_DECL:
26750 #if 0
26751 /* FIXME */
26752 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26753 on local redeclarations of global functions. That seems broken. */
26754 if (current_function_decl != decl)
26755 /* This is only a declaration. */;
26756 #endif
26758 /* We should have abstract copies already and should not generate
26759 stray type DIEs in late LTO dumping. */
26760 if (! early_dwarf)
26763 /* If we're emitting a clone, emit info for the abstract instance. */
26764 else if (origin || DECL_ORIGIN (decl) != decl)
26765 dwarf2out_abstract_function (origin
26766 ? DECL_ORIGIN (origin)
26767 : DECL_ABSTRACT_ORIGIN (decl));
26769 /* If we're emitting a possibly inlined function emit it as
26770 abstract instance. */
26771 else if (cgraph_function_possibly_inlined_p (decl)
26772 && ! DECL_ABSTRACT_P (decl)
26773 && ! class_or_namespace_scope_p (context_die)
26774 /* dwarf2out_abstract_function won't emit a die if this is just
26775 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26776 that case, because that works only if we have a die. */
26777 && DECL_INITIAL (decl) != NULL_TREE)
26778 dwarf2out_abstract_function (decl);
26780 /* Otherwise we're emitting the primary DIE for this decl. */
26781 else if (debug_info_level > DINFO_LEVEL_TERSE)
26783 /* Before we describe the FUNCTION_DECL itself, make sure that we
26784 have its containing type. */
26785 if (!origin)
26786 origin = decl_class_context (decl);
26787 if (origin != NULL_TREE)
26788 gen_type_die (origin, context_die);
26790 /* And its return type. */
26791 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26793 /* And its virtual context. */
26794 if (DECL_VINDEX (decl) != NULL_TREE)
26795 gen_type_die (DECL_CONTEXT (decl), context_die);
26797 /* Make sure we have a member DIE for decl. */
26798 if (origin != NULL_TREE)
26799 gen_type_die_for_member (origin, decl, context_die);
26801 /* And its containing namespace. */
26802 context_die = declare_in_namespace (decl, context_die);
26805 /* Now output a DIE to represent the function itself. */
26806 if (decl)
26807 gen_subprogram_die (decl, context_die);
26808 break;
26810 case TYPE_DECL:
26811 /* If we are in terse mode, don't generate any DIEs to represent any
26812 actual typedefs. */
26813 if (debug_info_level <= DINFO_LEVEL_TERSE)
26814 break;
26816 /* In the special case of a TYPE_DECL node representing the declaration
26817 of some type tag, if the given TYPE_DECL is marked as having been
26818 instantiated from some other (original) TYPE_DECL node (e.g. one which
26819 was generated within the original definition of an inline function) we
26820 used to generate a special (abbreviated) DW_TAG_structure_type,
26821 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26822 should be actually referencing those DIEs, as variable DIEs with that
26823 type would be emitted already in the abstract origin, so it was always
26824 removed during unused type prunning. Don't add anything in this
26825 case. */
26826 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26827 break;
26829 if (is_redundant_typedef (decl))
26830 gen_type_die (TREE_TYPE (decl), context_die);
26831 else
26832 /* Output a DIE to represent the typedef itself. */
26833 gen_typedef_die (decl, context_die);
26834 break;
26836 case LABEL_DECL:
26837 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26838 gen_label_die (decl, context_die);
26839 break;
26841 case VAR_DECL:
26842 case RESULT_DECL:
26843 /* If we are in terse mode, don't generate any DIEs to represent any
26844 variable declarations or definitions unless it is external. */
26845 if (debug_info_level < DINFO_LEVEL_TERSE
26846 || (debug_info_level == DINFO_LEVEL_TERSE
26847 && !TREE_PUBLIC (decl_or_origin)))
26848 break;
26850 if (debug_info_level > DINFO_LEVEL_TERSE)
26852 /* Avoid generating stray type DIEs during late dwarf dumping.
26853 All types have been dumped early. */
26854 if (early_dwarf
26855 /* ??? But in LTRANS we cannot annotate early created variably
26856 modified type DIEs without copying them and adjusting all
26857 references to them. Dump them again as happens for inlining
26858 which copies both the decl and the types. */
26859 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26860 in VLA bound information for example. */
26861 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26862 current_function_decl)))
26864 /* Output any DIEs that are needed to specify the type of this data
26865 object. */
26866 if (decl_by_reference_p (decl_or_origin))
26867 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26868 else
26869 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26872 if (early_dwarf)
26874 /* And its containing type. */
26875 class_origin = decl_class_context (decl_or_origin);
26876 if (class_origin != NULL_TREE)
26877 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26879 /* And its containing namespace. */
26880 context_die = declare_in_namespace (decl_or_origin, context_die);
26884 /* Now output the DIE to represent the data object itself. This gets
26885 complicated because of the possibility that the VAR_DECL really
26886 represents an inlined instance of a formal parameter for an inline
26887 function. */
26888 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26889 if (ultimate_origin != NULL_TREE
26890 && TREE_CODE (ultimate_origin) == PARM_DECL)
26891 gen_formal_parameter_die (decl, origin,
26892 true /* Emit name attribute. */,
26893 context_die);
26894 else
26895 gen_variable_die (decl, origin, context_die);
26896 break;
26898 case FIELD_DECL:
26899 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26900 /* Ignore the nameless fields that are used to skip bits but handle C++
26901 anonymous unions and structs. */
26902 if (DECL_NAME (decl) != NULL_TREE
26903 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26904 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26906 gen_type_die (member_declared_type (decl), context_die);
26907 gen_field_die (decl, ctx, context_die);
26909 break;
26911 case PARM_DECL:
26912 /* Avoid generating stray type DIEs during late dwarf dumping.
26913 All types have been dumped early. */
26914 if (early_dwarf
26915 /* ??? But in LTRANS we cannot annotate early created variably
26916 modified type DIEs without copying them and adjusting all
26917 references to them. Dump them again as happens for inlining
26918 which copies both the decl and the types. */
26919 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26920 in VLA bound information for example. */
26921 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26922 current_function_decl)))
26924 if (DECL_BY_REFERENCE (decl_or_origin))
26925 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26926 else
26927 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26929 return gen_formal_parameter_die (decl, origin,
26930 true /* Emit name attribute. */,
26931 context_die);
26933 case NAMESPACE_DECL:
26934 if (dwarf_version >= 3 || !dwarf_strict)
26935 gen_namespace_die (decl, context_die);
26936 break;
26938 case IMPORTED_DECL:
26939 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26940 DECL_CONTEXT (decl), context_die);
26941 break;
26943 case NAMELIST_DECL:
26944 gen_namelist_decl (DECL_NAME (decl), context_die,
26945 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26946 break;
26948 default:
26949 /* Probably some frontend-internal decl. Assume we don't care. */
26950 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26951 break;
26954 return NULL;
26957 /* Output initial debug information for global DECL. Called at the
26958 end of the parsing process.
26960 This is the initial debug generation process. As such, the DIEs
26961 generated may be incomplete. A later debug generation pass
26962 (dwarf2out_late_global_decl) will augment the information generated
26963 in this pass (e.g., with complete location info). */
26965 static void
26966 dwarf2out_early_global_decl (tree decl)
26968 set_early_dwarf s;
26970 /* gen_decl_die() will set DECL_ABSTRACT because
26971 cgraph_function_possibly_inlined_p() returns true. This is in
26972 turn will cause DW_AT_inline attributes to be set.
26974 This happens because at early dwarf generation, there is no
26975 cgraph information, causing cgraph_function_possibly_inlined_p()
26976 to return true. Trick cgraph_function_possibly_inlined_p()
26977 while we generate dwarf early. */
26978 bool save = symtab->global_info_ready;
26979 symtab->global_info_ready = true;
26981 /* We don't handle TYPE_DECLs. If required, they'll be reached via
26982 other DECLs and they can point to template types or other things
26983 that dwarf2out can't handle when done via dwarf2out_decl. */
26984 if (TREE_CODE (decl) != TYPE_DECL
26985 && TREE_CODE (decl) != PARM_DECL)
26987 if (TREE_CODE (decl) == FUNCTION_DECL)
26989 tree save_fndecl = current_function_decl;
26991 /* For nested functions, make sure we have DIEs for the parents first
26992 so that all nested DIEs are generated at the proper scope in the
26993 first shot. */
26994 tree context = decl_function_context (decl);
26995 if (context != NULL)
26997 dw_die_ref context_die = lookup_decl_die (context);
26998 current_function_decl = context;
27000 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27001 enough so that it lands in its own context. This avoids type
27002 pruning issues later on. */
27003 if (context_die == NULL || is_declaration_die (context_die))
27004 dwarf2out_early_global_decl (context);
27007 /* Emit an abstract origin of a function first. This happens
27008 with C++ constructor clones for example and makes
27009 dwarf2out_abstract_function happy which requires the early
27010 DIE of the abstract instance to be present. */
27011 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27012 dw_die_ref origin_die;
27013 if (origin != NULL
27014 /* Do not emit the DIE multiple times but make sure to
27015 process it fully here in case we just saw a declaration. */
27016 && ((origin_die = lookup_decl_die (origin)) == NULL
27017 || is_declaration_die (origin_die)))
27019 current_function_decl = origin;
27020 dwarf2out_decl (origin);
27023 /* Emit the DIE for decl but avoid doing that multiple times. */
27024 dw_die_ref old_die;
27025 if ((old_die = lookup_decl_die (decl)) == NULL
27026 || is_declaration_die (old_die))
27028 current_function_decl = decl;
27029 dwarf2out_decl (decl);
27032 current_function_decl = save_fndecl;
27034 else
27035 dwarf2out_decl (decl);
27037 symtab->global_info_ready = save;
27040 /* Return whether EXPR is an expression with the following pattern:
27041 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27043 static bool
27044 is_trivial_indirect_ref (tree expr)
27046 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27047 return false;
27049 tree nop = TREE_OPERAND (expr, 0);
27050 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27051 return false;
27053 tree int_cst = TREE_OPERAND (nop, 0);
27054 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27057 /* Output debug information for global decl DECL. Called from
27058 toplev.c after compilation proper has finished. */
27060 static void
27061 dwarf2out_late_global_decl (tree decl)
27063 /* Fill-in any location information we were unable to determine
27064 on the first pass. */
27065 if (VAR_P (decl))
27067 dw_die_ref die = lookup_decl_die (decl);
27069 /* We may have to generate full debug late for LTO in case debug
27070 was not enabled at compile-time or the target doesn't support
27071 the LTO early debug scheme. */
27072 if (! die && in_lto_p)
27073 dwarf2out_decl (decl);
27074 else if (die)
27076 /* We get called via the symtab code invoking late_global_decl
27077 for symbols that are optimized out.
27079 Do not add locations for those, except if they have a
27080 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27081 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27082 INDIRECT_REF expression, as this could generate relocations to
27083 text symbols in LTO object files, which is invalid. */
27084 varpool_node *node = varpool_node::get (decl);
27085 if ((! node || ! node->definition)
27086 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27087 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27088 tree_add_const_value_attribute_for_decl (die, decl);
27089 else
27090 add_location_or_const_value_attribute (die, decl, false);
27095 /* Output debug information for type decl DECL. Called from toplev.c
27096 and from language front ends (to record built-in types). */
27097 static void
27098 dwarf2out_type_decl (tree decl, int local)
27100 if (!local)
27102 set_early_dwarf s;
27103 dwarf2out_decl (decl);
27107 /* Output debug information for imported module or decl DECL.
27108 NAME is non-NULL name in the lexical block if the decl has been renamed.
27109 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27110 that DECL belongs to.
27111 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27112 static void
27113 dwarf2out_imported_module_or_decl_1 (tree decl,
27114 tree name,
27115 tree lexical_block,
27116 dw_die_ref lexical_block_die)
27118 expanded_location xloc;
27119 dw_die_ref imported_die = NULL;
27120 dw_die_ref at_import_die;
27122 if (TREE_CODE (decl) == IMPORTED_DECL)
27124 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27125 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27126 gcc_assert (decl);
27128 else
27129 xloc = expand_location (input_location);
27131 if (TREE_CODE (decl) == TYPE_DECL)
27133 at_import_die = force_type_die (TREE_TYPE (decl));
27134 /* For namespace N { typedef void T; } using N::T; base_type_die
27135 returns NULL, but DW_TAG_imported_declaration requires
27136 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27137 if (!at_import_die)
27139 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27140 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27141 at_import_die = lookup_type_die (TREE_TYPE (decl));
27142 gcc_assert (at_import_die);
27145 else
27147 at_import_die = lookup_decl_die (decl);
27148 if (!at_import_die)
27150 /* If we're trying to avoid duplicate debug info, we may not have
27151 emitted the member decl for this field. Emit it now. */
27152 if (TREE_CODE (decl) == FIELD_DECL)
27154 tree type = DECL_CONTEXT (decl);
27156 if (TYPE_CONTEXT (type)
27157 && TYPE_P (TYPE_CONTEXT (type))
27158 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27159 DINFO_USAGE_DIR_USE))
27160 return;
27161 gen_type_die_for_member (type, decl,
27162 get_context_die (TYPE_CONTEXT (type)));
27164 if (TREE_CODE (decl) == CONST_DECL)
27166 /* Individual enumerators of an enum type do not get output here
27167 (see gen_decl_die), so we cannot call force_decl_die. */
27168 if (!is_fortran () && !is_ada () && !is_dlang ())
27169 return;
27171 if (TREE_CODE (decl) == NAMELIST_DECL)
27172 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27173 get_context_die (DECL_CONTEXT (decl)),
27174 NULL_TREE);
27175 else
27176 at_import_die = force_decl_die (decl);
27180 if (TREE_CODE (decl) == NAMESPACE_DECL)
27182 if (dwarf_version >= 3 || !dwarf_strict)
27183 imported_die = new_die (DW_TAG_imported_module,
27184 lexical_block_die,
27185 lexical_block);
27186 else
27187 return;
27189 else
27190 imported_die = new_die (DW_TAG_imported_declaration,
27191 lexical_block_die,
27192 lexical_block);
27194 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27195 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27196 if (debug_column_info && xloc.column)
27197 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27198 if (name)
27199 add_AT_string (imported_die, DW_AT_name,
27200 IDENTIFIER_POINTER (name));
27201 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27204 /* Output debug information for imported module or decl DECL.
27205 NAME is non-NULL name in context if the decl has been renamed.
27206 CHILD is true if decl is one of the renamed decls as part of
27207 importing whole module.
27208 IMPLICIT is set if this hook is called for an implicit import
27209 such as inline namespace. */
27211 static void
27212 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27213 bool child, bool implicit)
27215 /* dw_die_ref at_import_die; */
27216 dw_die_ref scope_die;
27218 if (debug_info_level <= DINFO_LEVEL_TERSE)
27219 return;
27221 gcc_assert (decl);
27223 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27224 should be enough, for DWARF4 and older even if we emit as extension
27225 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27226 for the benefit of consumers unaware of DW_AT_export_symbols. */
27227 if (implicit
27228 && dwarf_version >= 5
27229 && lang_hooks.decls.decl_dwarf_attribute (decl,
27230 DW_AT_export_symbols) == 1)
27231 return;
27233 set_early_dwarf s;
27235 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27236 We need decl DIE for reference and scope die. First, get DIE for the decl
27237 itself. */
27239 /* Get the scope die for decl context. Use comp_unit_die for global module
27240 or decl. If die is not found for non globals, force new die. */
27241 if (context
27242 && TYPE_P (context)
27243 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27244 return;
27246 scope_die = get_context_die (context);
27248 if (child)
27250 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27251 there is nothing we can do, here. */
27252 if (dwarf_version < 3 && dwarf_strict)
27253 return;
27255 gcc_assert (scope_die->die_child);
27256 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27257 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27258 scope_die = scope_die->die_child;
27261 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27262 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27265 /* Output debug information for namelists. */
27267 static dw_die_ref
27268 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27270 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27271 tree value;
27272 unsigned i;
27274 if (debug_info_level <= DINFO_LEVEL_TERSE)
27275 return NULL;
27277 gcc_assert (scope_die != NULL);
27278 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27279 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27281 /* If there are no item_decls, we have a nondefining namelist, e.g.
27282 with USE association; hence, set DW_AT_declaration. */
27283 if (item_decls == NULL_TREE)
27285 add_AT_flag (nml_die, DW_AT_declaration, 1);
27286 return nml_die;
27289 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27291 nml_item_ref_die = lookup_decl_die (value);
27292 if (!nml_item_ref_die)
27293 nml_item_ref_die = force_decl_die (value);
27295 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27296 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
27298 return nml_die;
27302 /* Write the debugging output for DECL and return the DIE. */
27304 static void
27305 dwarf2out_decl (tree decl)
27307 dw_die_ref context_die = comp_unit_die ();
27309 switch (TREE_CODE (decl))
27311 case ERROR_MARK:
27312 return;
27314 case FUNCTION_DECL:
27315 /* If we're a nested function, initially use a parent of NULL; if we're
27316 a plain function, this will be fixed up in decls_for_scope. If
27317 we're a method, it will be ignored, since we already have a DIE.
27318 Avoid doing this late though since clones of class methods may
27319 otherwise end up in limbo and create type DIEs late. */
27320 if (early_dwarf
27321 && decl_function_context (decl)
27322 /* But if we're in terse mode, we don't care about scope. */
27323 && debug_info_level > DINFO_LEVEL_TERSE)
27324 context_die = NULL;
27325 break;
27327 case VAR_DECL:
27328 /* For local statics lookup proper context die. */
27329 if (local_function_static (decl))
27330 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27332 /* If we are in terse mode, don't generate any DIEs to represent any
27333 variable declarations or definitions unless it is external. */
27334 if (debug_info_level < DINFO_LEVEL_TERSE
27335 || (debug_info_level == DINFO_LEVEL_TERSE
27336 && !TREE_PUBLIC (decl)))
27337 return;
27338 break;
27340 case CONST_DECL:
27341 if (debug_info_level <= DINFO_LEVEL_TERSE)
27342 return;
27343 if (!is_fortran () && !is_ada () && !is_dlang ())
27344 return;
27345 if (TREE_STATIC (decl) && decl_function_context (decl))
27346 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27347 break;
27349 case NAMESPACE_DECL:
27350 case IMPORTED_DECL:
27351 if (debug_info_level <= DINFO_LEVEL_TERSE)
27352 return;
27353 if (lookup_decl_die (decl) != NULL)
27354 return;
27355 break;
27357 case TYPE_DECL:
27358 /* Don't emit stubs for types unless they are needed by other DIEs. */
27359 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27360 return;
27362 /* Don't bother trying to generate any DIEs to represent any of the
27363 normal built-in types for the language we are compiling. */
27364 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27365 return;
27367 /* If we are in terse mode, don't generate any DIEs for types. */
27368 if (debug_info_level <= DINFO_LEVEL_TERSE)
27369 return;
27371 /* If we're a function-scope tag, initially use a parent of NULL;
27372 this will be fixed up in decls_for_scope. */
27373 if (decl_function_context (decl))
27374 context_die = NULL;
27376 break;
27378 case NAMELIST_DECL:
27379 break;
27381 default:
27382 return;
27385 gen_decl_die (decl, NULL, NULL, context_die);
27387 if (flag_checking)
27389 dw_die_ref die = lookup_decl_die (decl);
27390 if (die)
27391 check_die (die);
27395 /* Write the debugging output for DECL. */
27397 static void
27398 dwarf2out_function_decl (tree decl)
27400 dwarf2out_decl (decl);
27401 call_arg_locations = NULL;
27402 call_arg_loc_last = NULL;
27403 call_site_count = -1;
27404 tail_call_site_count = -1;
27405 decl_loc_table->empty ();
27406 cached_dw_loc_list_table->empty ();
27409 /* Output a marker (i.e. a label) for the beginning of the generated code for
27410 a lexical block. */
27412 static void
27413 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27414 unsigned int blocknum)
27416 switch_to_section (current_function_section ());
27417 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27420 /* Output a marker (i.e. a label) for the end of the generated code for a
27421 lexical block. */
27423 static void
27424 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27426 switch_to_section (current_function_section ());
27427 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27430 /* Returns nonzero if it is appropriate not to emit any debugging
27431 information for BLOCK, because it doesn't contain any instructions.
27433 Don't allow this for blocks with nested functions or local classes
27434 as we would end up with orphans, and in the presence of scheduling
27435 we may end up calling them anyway. */
27437 static bool
27438 dwarf2out_ignore_block (const_tree block)
27440 tree decl;
27441 unsigned int i;
27443 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27444 if (TREE_CODE (decl) == FUNCTION_DECL
27445 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27446 return 0;
27447 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27449 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27450 if (TREE_CODE (decl) == FUNCTION_DECL
27451 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27452 return 0;
27455 return 1;
27458 /* Hash table routines for file_hash. */
27460 bool
27461 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27463 return filename_cmp (p1->filename, p2) == 0;
27466 hashval_t
27467 dwarf_file_hasher::hash (dwarf_file_data *p)
27469 return htab_hash_string (p->filename);
27472 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27473 dwarf2out.c) and return its "index". The index of each (known) filename is
27474 just a unique number which is associated with only that one filename. We
27475 need such numbers for the sake of generating labels (in the .debug_sfnames
27476 section) and references to those files numbers (in the .debug_srcinfo
27477 and .debug_macinfo sections). If the filename given as an argument is not
27478 found in our current list, add it to the list and assign it the next
27479 available unique index number. */
27481 static struct dwarf_file_data *
27482 lookup_filename (const char *file_name)
27484 struct dwarf_file_data * created;
27486 if (!file_name)
27487 return NULL;
27489 if (!file_name[0])
27490 file_name = "<stdin>";
27492 dwarf_file_data **slot
27493 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27494 INSERT);
27495 if (*slot)
27496 return *slot;
27498 created = ggc_alloc<dwarf_file_data> ();
27499 created->filename = file_name;
27500 created->emitted_number = 0;
27501 *slot = created;
27502 return created;
27505 /* If the assembler will construct the file table, then translate the compiler
27506 internal file table number into the assembler file table number, and emit
27507 a .file directive if we haven't already emitted one yet. The file table
27508 numbers are different because we prune debug info for unused variables and
27509 types, which may include filenames. */
27511 static int
27512 maybe_emit_file (struct dwarf_file_data * fd)
27514 if (! fd->emitted_number)
27516 if (last_emitted_file)
27517 fd->emitted_number = last_emitted_file->emitted_number + 1;
27518 else
27519 fd->emitted_number = 1;
27520 last_emitted_file = fd;
27522 if (output_asm_line_debug_info ())
27524 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27525 output_quoted_string (asm_out_file,
27526 remap_debug_filename (fd->filename));
27527 fputc ('\n', asm_out_file);
27531 return fd->emitted_number;
27534 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27535 That generation should happen after function debug info has been
27536 generated. The value of the attribute is the constant value of ARG. */
27538 static void
27539 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27541 die_arg_entry entry;
27543 if (!die || !arg)
27544 return;
27546 gcc_assert (early_dwarf);
27548 if (!tmpl_value_parm_die_table)
27549 vec_alloc (tmpl_value_parm_die_table, 32);
27551 entry.die = die;
27552 entry.arg = arg;
27553 vec_safe_push (tmpl_value_parm_die_table, entry);
27556 /* Return TRUE if T is an instance of generic type, FALSE
27557 otherwise. */
27559 static bool
27560 generic_type_p (tree t)
27562 if (t == NULL_TREE || !TYPE_P (t))
27563 return false;
27564 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27567 /* Schedule the generation of the generic parameter dies for the
27568 instance of generic type T. The proper generation itself is later
27569 done by gen_scheduled_generic_parms_dies. */
27571 static void
27572 schedule_generic_params_dies_gen (tree t)
27574 if (!generic_type_p (t))
27575 return;
27577 gcc_assert (early_dwarf);
27579 if (!generic_type_instances)
27580 vec_alloc (generic_type_instances, 256);
27582 vec_safe_push (generic_type_instances, t);
27585 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27586 by append_entry_to_tmpl_value_parm_die_table. This function must
27587 be called after function DIEs have been generated. */
27589 static void
27590 gen_remaining_tmpl_value_param_die_attribute (void)
27592 if (tmpl_value_parm_die_table)
27594 unsigned i, j;
27595 die_arg_entry *e;
27597 /* We do this in two phases - first get the cases we can
27598 handle during early-finish, preserving those we cannot
27599 (containing symbolic constants where we don't yet know
27600 whether we are going to output the referenced symbols).
27601 For those we try again at late-finish. */
27602 j = 0;
27603 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27605 if (!e->die->removed
27606 && !tree_add_const_value_attribute (e->die, e->arg))
27608 dw_loc_descr_ref loc = NULL;
27609 if (! early_dwarf
27610 && (dwarf_version >= 5 || !dwarf_strict))
27611 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27612 if (loc)
27613 add_AT_loc (e->die, DW_AT_location, loc);
27614 else
27615 (*tmpl_value_parm_die_table)[j++] = *e;
27618 tmpl_value_parm_die_table->truncate (j);
27622 /* Generate generic parameters DIEs for instances of generic types
27623 that have been previously scheduled by
27624 schedule_generic_params_dies_gen. This function must be called
27625 after all the types of the CU have been laid out. */
27627 static void
27628 gen_scheduled_generic_parms_dies (void)
27630 unsigned i;
27631 tree t;
27633 if (!generic_type_instances)
27634 return;
27636 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27637 if (COMPLETE_TYPE_P (t))
27638 gen_generic_params_dies (t);
27640 generic_type_instances = NULL;
27644 /* Replace DW_AT_name for the decl with name. */
27646 static void
27647 dwarf2out_set_name (tree decl, tree name)
27649 dw_die_ref die;
27650 dw_attr_node *attr;
27651 const char *dname;
27653 die = TYPE_SYMTAB_DIE (decl);
27654 if (!die)
27655 return;
27657 dname = dwarf2_name (name, 0);
27658 if (!dname)
27659 return;
27661 attr = get_AT (die, DW_AT_name);
27662 if (attr)
27664 struct indirect_string_node *node;
27666 node = find_AT_string (dname);
27667 /* replace the string. */
27668 attr->dw_attr_val.v.val_str = node;
27671 else
27672 add_name_attribute (die, dname);
27675 /* True if before or during processing of the first function being emitted. */
27676 static bool in_first_function_p = true;
27677 /* True if loc_note during dwarf2out_var_location call might still be
27678 before first real instruction at address equal to .Ltext0. */
27679 static bool maybe_at_text_label_p = true;
27680 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27681 static unsigned int first_loclabel_num_not_at_text_label;
27683 /* Look ahead for a real insn. */
27685 static rtx_insn *
27686 dwarf2out_next_real_insn (rtx_insn *loc_note)
27688 rtx_insn *next_real = NEXT_INSN (loc_note);
27690 while (next_real)
27691 if (INSN_P (next_real))
27692 break;
27693 else
27694 next_real = NEXT_INSN (next_real);
27696 return next_real;
27699 /* Called by the final INSN scan whenever we see a var location. We
27700 use it to drop labels in the right places, and throw the location in
27701 our lookup table. */
27703 static void
27704 dwarf2out_var_location (rtx_insn *loc_note)
27706 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27707 struct var_loc_node *newloc;
27708 rtx_insn *next_real;
27709 rtx_insn *call_insn = NULL;
27710 static const char *last_label;
27711 static const char *last_postcall_label;
27712 static bool last_in_cold_section_p;
27713 static rtx_insn *expected_next_loc_note;
27714 tree decl;
27715 bool var_loc_p;
27716 var_loc_view view = 0;
27718 if (!NOTE_P (loc_note))
27720 if (CALL_P (loc_note))
27722 maybe_reset_location_view (loc_note, cur_line_info_table);
27723 call_site_count++;
27724 if (SIBLING_CALL_P (loc_note))
27725 tail_call_site_count++;
27726 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27728 call_insn = loc_note;
27729 loc_note = NULL;
27730 var_loc_p = false;
27732 next_real = dwarf2out_next_real_insn (call_insn);
27733 cached_next_real_insn = NULL;
27734 goto create_label;
27736 if (optimize == 0 && !flag_var_tracking)
27738 /* When the var-tracking pass is not running, there is no note
27739 for indirect calls whose target is compile-time known. In this
27740 case, process such calls specifically so that we generate call
27741 sites for them anyway. */
27742 rtx x = PATTERN (loc_note);
27743 if (GET_CODE (x) == PARALLEL)
27744 x = XVECEXP (x, 0, 0);
27745 if (GET_CODE (x) == SET)
27746 x = SET_SRC (x);
27747 if (GET_CODE (x) == CALL)
27748 x = XEXP (x, 0);
27749 if (!MEM_P (x)
27750 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27751 || !SYMBOL_REF_DECL (XEXP (x, 0))
27752 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27753 != FUNCTION_DECL))
27755 call_insn = loc_note;
27756 loc_note = NULL;
27757 var_loc_p = false;
27759 next_real = dwarf2out_next_real_insn (call_insn);
27760 cached_next_real_insn = NULL;
27761 goto create_label;
27765 else if (!debug_variable_location_views)
27766 gcc_unreachable ();
27767 else
27768 maybe_reset_location_view (loc_note, cur_line_info_table);
27770 return;
27773 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27774 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27775 return;
27777 /* Optimize processing a large consecutive sequence of location
27778 notes so we don't spend too much time in next_real_insn. If the
27779 next insn is another location note, remember the next_real_insn
27780 calculation for next time. */
27781 next_real = cached_next_real_insn;
27782 if (next_real)
27784 if (expected_next_loc_note != loc_note)
27785 next_real = NULL;
27788 if (! next_real)
27789 next_real = dwarf2out_next_real_insn (loc_note);
27791 if (next_real)
27793 rtx_insn *next_note = NEXT_INSN (loc_note);
27794 while (next_note != next_real)
27796 if (! next_note->deleted ()
27797 && NOTE_P (next_note)
27798 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
27799 break;
27800 next_note = NEXT_INSN (next_note);
27803 if (next_note == next_real)
27804 cached_next_real_insn = NULL;
27805 else
27807 expected_next_loc_note = next_note;
27808 cached_next_real_insn = next_real;
27811 else
27812 cached_next_real_insn = NULL;
27814 /* If there are no instructions which would be affected by this note,
27815 don't do anything. */
27816 if (var_loc_p
27817 && next_real == NULL_RTX
27818 && !NOTE_DURING_CALL_P (loc_note))
27819 return;
27821 create_label:
27823 if (next_real == NULL_RTX)
27824 next_real = get_last_insn ();
27826 /* If there were any real insns between note we processed last time
27827 and this note (or if it is the first note), clear
27828 last_{,postcall_}label so that they are not reused this time. */
27829 if (last_var_location_insn == NULL_RTX
27830 || last_var_location_insn != next_real
27831 || last_in_cold_section_p != in_cold_section_p)
27833 last_label = NULL;
27834 last_postcall_label = NULL;
27837 if (var_loc_p)
27839 const char *label
27840 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27841 view = cur_line_info_table->view;
27842 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27843 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27844 if (newloc == NULL)
27845 return;
27847 else
27849 decl = NULL_TREE;
27850 newloc = NULL;
27853 /* If there were no real insns between note we processed last time
27854 and this note, use the label we emitted last time. Otherwise
27855 create a new label and emit it. */
27856 if (last_label == NULL)
27858 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27859 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27860 loclabel_num++;
27861 last_label = ggc_strdup (loclabel);
27862 /* See if loclabel might be equal to .Ltext0. If yes,
27863 bump first_loclabel_num_not_at_text_label. */
27864 if (!have_multiple_function_sections
27865 && in_first_function_p
27866 && maybe_at_text_label_p)
27868 static rtx_insn *last_start;
27869 rtx_insn *insn;
27870 for (insn = loc_note; insn; insn = previous_insn (insn))
27871 if (insn == last_start)
27872 break;
27873 else if (!NONDEBUG_INSN_P (insn))
27874 continue;
27875 else
27877 rtx body = PATTERN (insn);
27878 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27879 continue;
27880 /* Inline asm could occupy zero bytes. */
27881 else if (GET_CODE (body) == ASM_INPUT
27882 || asm_noperands (body) >= 0)
27883 continue;
27884 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27885 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27886 continue;
27887 #endif
27888 else
27890 /* Assume insn has non-zero length. */
27891 maybe_at_text_label_p = false;
27892 break;
27895 if (maybe_at_text_label_p)
27897 last_start = loc_note;
27898 first_loclabel_num_not_at_text_label = loclabel_num;
27903 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27904 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27906 if (!var_loc_p)
27908 struct call_arg_loc_node *ca_loc
27909 = ggc_cleared_alloc<call_arg_loc_node> ();
27910 rtx_insn *prev = call_insn;
27912 ca_loc->call_arg_loc_note
27913 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27914 ca_loc->next = NULL;
27915 ca_loc->label = last_label;
27916 gcc_assert (prev
27917 && (CALL_P (prev)
27918 || (NONJUMP_INSN_P (prev)
27919 && GET_CODE (PATTERN (prev)) == SEQUENCE
27920 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27921 if (!CALL_P (prev))
27922 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27923 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27925 /* Look for a SYMBOL_REF in the "prev" instruction. */
27926 rtx x = get_call_rtx_from (prev);
27927 if (x)
27929 /* Try to get the call symbol, if any. */
27930 if (MEM_P (XEXP (x, 0)))
27931 x = XEXP (x, 0);
27932 /* First, look for a memory access to a symbol_ref. */
27933 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27934 && SYMBOL_REF_DECL (XEXP (x, 0))
27935 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27936 ca_loc->symbol_ref = XEXP (x, 0);
27937 /* Otherwise, look at a compile-time known user-level function
27938 declaration. */
27939 else if (MEM_P (x)
27940 && MEM_EXPR (x)
27941 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27942 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27945 ca_loc->block = insn_scope (prev);
27946 if (call_arg_locations)
27947 call_arg_loc_last->next = ca_loc;
27948 else
27949 call_arg_locations = ca_loc;
27950 call_arg_loc_last = ca_loc;
27952 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27954 newloc->label = last_label;
27955 newloc->view = view;
27957 else
27959 if (!last_postcall_label)
27961 sprintf (loclabel, "%s-1", last_label);
27962 last_postcall_label = ggc_strdup (loclabel);
27964 newloc->label = last_postcall_label;
27965 /* ??? This view is at last_label, not last_label-1, but we
27966 could only assume view at last_label-1 is zero if we could
27967 assume calls always have length greater than one. This is
27968 probably true in general, though there might be a rare
27969 exception to this rule, e.g. if a call insn is optimized out
27970 by target magic. Then, even the -1 in the label will be
27971 wrong, which might invalidate the range. Anyway, using view,
27972 though technically possibly incorrect, will work as far as
27973 ranges go: since L-1 is in the middle of the call insn,
27974 (L-1).0 and (L-1).V shouldn't make any difference, and having
27975 the loclist entry refer to the .loc entry might be useful, so
27976 leave it like this. */
27977 newloc->view = view;
27980 if (var_loc_p && flag_debug_asm)
27982 const char *name, *sep, *patstr;
27983 if (decl && DECL_NAME (decl))
27984 name = IDENTIFIER_POINTER (DECL_NAME (decl));
27985 else
27986 name = "";
27987 if (NOTE_VAR_LOCATION_LOC (loc_note))
27989 sep = " => ";
27990 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27992 else
27994 sep = " ";
27995 patstr = "RESET";
27997 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27998 name, sep, patstr);
28001 last_var_location_insn = next_real;
28002 last_in_cold_section_p = in_cold_section_p;
28005 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28006 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28007 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28008 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28009 BLOCK_FRAGMENT_ORIGIN links. */
28010 static bool
28011 block_within_block_p (tree block, tree outer, bool bothways)
28013 if (block == outer)
28014 return true;
28016 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28017 for (tree context = BLOCK_SUPERCONTEXT (block);
28018 context != outer;
28019 context = BLOCK_SUPERCONTEXT (context))
28020 if (!context || TREE_CODE (context) != BLOCK)
28021 return false;
28023 if (!bothways)
28024 return true;
28026 /* Now check that each block is actually referenced by its
28027 parent. */
28028 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28029 context = BLOCK_SUPERCONTEXT (context))
28031 if (BLOCK_FRAGMENT_ORIGIN (context))
28033 gcc_assert (!BLOCK_SUBBLOCKS (context));
28034 context = BLOCK_FRAGMENT_ORIGIN (context);
28036 for (tree sub = BLOCK_SUBBLOCKS (context);
28037 sub != block;
28038 sub = BLOCK_CHAIN (sub))
28039 if (!sub)
28040 return false;
28041 if (context == outer)
28042 return true;
28043 else
28044 block = context;
28048 /* Called during final while assembling the marker of the entry point
28049 for an inlined function. */
28051 static void
28052 dwarf2out_inline_entry (tree block)
28054 gcc_assert (debug_inline_points);
28056 /* If we can't represent it, don't bother. */
28057 if (!(dwarf_version >= 3 || !dwarf_strict))
28058 return;
28060 gcc_assert (DECL_P (block_ultimate_origin (block)));
28062 /* Sanity check the block tree. This would catch a case in which
28063 BLOCK got removed from the tree reachable from the outermost
28064 lexical block, but got retained in markers. It would still link
28065 back to its parents, but some ancestor would be missing a link
28066 down the path to the sub BLOCK. If the block got removed, its
28067 BLOCK_NUMBER will not be a usable value. */
28068 if (flag_checking)
28069 gcc_assert (block_within_block_p (block,
28070 DECL_INITIAL (current_function_decl),
28071 true));
28073 gcc_assert (inlined_function_outer_scope_p (block));
28074 gcc_assert (!lookup_block_die (block));
28076 if (BLOCK_FRAGMENT_ORIGIN (block))
28077 block = BLOCK_FRAGMENT_ORIGIN (block);
28078 /* Can the entry point ever not be at the beginning of an
28079 unfragmented lexical block? */
28080 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28081 || (cur_line_info_table
28082 && !ZERO_VIEW_P (cur_line_info_table->view))))
28083 return;
28085 if (!inline_entry_data_table)
28086 inline_entry_data_table
28087 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28090 inline_entry_data **iedp
28091 = inline_entry_data_table->find_slot_with_hash (block,
28092 htab_hash_pointer (block),
28093 INSERT);
28094 if (*iedp)
28095 /* ??? Ideally, we'd record all entry points for the same inlined
28096 function (some may have been duplicated by e.g. unrolling), but
28097 we have no way to represent that ATM. */
28098 return;
28100 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28101 ied->block = block;
28102 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28103 ied->label_num = BLOCK_NUMBER (block);
28104 if (cur_line_info_table)
28105 ied->view = cur_line_info_table->view;
28107 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28108 BLOCK_NUMBER (block));
28111 /* Called from finalize_size_functions for size functions so that their body
28112 can be encoded in the debug info to describe the layout of variable-length
28113 structures. */
28115 static void
28116 dwarf2out_size_function (tree decl)
28118 set_early_dwarf s;
28119 function_to_dwarf_procedure (decl);
28122 /* Note in one location list that text section has changed. */
28125 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28127 var_loc_list *list = *slot;
28128 if (list->first)
28129 list->last_before_switch
28130 = list->last->next ? list->last->next : list->last;
28131 return 1;
28134 /* Note in all location lists that text section has changed. */
28136 static void
28137 var_location_switch_text_section (void)
28139 if (decl_loc_table == NULL)
28140 return;
28142 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28145 /* Create a new line number table. */
28147 static dw_line_info_table *
28148 new_line_info_table (void)
28150 dw_line_info_table *table;
28152 table = ggc_cleared_alloc<dw_line_info_table> ();
28153 table->file_num = 1;
28154 table->line_num = 1;
28155 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28156 FORCE_RESET_NEXT_VIEW (table->view);
28157 table->symviews_since_reset = 0;
28159 return table;
28162 /* Lookup the "current" table into which we emit line info, so
28163 that we don't have to do it for every source line. */
28165 static void
28166 set_cur_line_info_table (section *sec)
28168 dw_line_info_table *table;
28170 if (sec == text_section)
28171 table = text_section_line_info;
28172 else if (sec == cold_text_section)
28174 table = cold_text_section_line_info;
28175 if (!table)
28177 cold_text_section_line_info = table = new_line_info_table ();
28178 table->end_label = cold_end_label;
28181 else
28183 const char *end_label;
28185 if (crtl->has_bb_partition)
28187 if (in_cold_section_p)
28188 end_label = crtl->subsections.cold_section_end_label;
28189 else
28190 end_label = crtl->subsections.hot_section_end_label;
28192 else
28194 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28195 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28196 current_function_funcdef_no);
28197 end_label = ggc_strdup (label);
28200 table = new_line_info_table ();
28201 table->end_label = end_label;
28203 vec_safe_push (separate_line_info, table);
28206 if (output_asm_line_debug_info ())
28207 table->is_stmt = (cur_line_info_table
28208 ? cur_line_info_table->is_stmt
28209 : DWARF_LINE_DEFAULT_IS_STMT_START);
28210 cur_line_info_table = table;
28214 /* We need to reset the locations at the beginning of each
28215 function. We can't do this in the end_function hook, because the
28216 declarations that use the locations won't have been output when
28217 that hook is called. Also compute have_multiple_function_sections here. */
28219 static void
28220 dwarf2out_begin_function (tree fun)
28222 section *sec = function_section (fun);
28224 if (sec != text_section)
28225 have_multiple_function_sections = true;
28227 if (crtl->has_bb_partition && !cold_text_section)
28229 gcc_assert (current_function_decl == fun);
28230 cold_text_section = unlikely_text_section ();
28231 switch_to_section (cold_text_section);
28232 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28233 switch_to_section (sec);
28236 call_site_count = 0;
28237 tail_call_site_count = 0;
28239 set_cur_line_info_table (sec);
28240 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28243 /* Helper function of dwarf2out_end_function, called only after emitting
28244 the very first function into assembly. Check if some .debug_loc range
28245 might end with a .LVL* label that could be equal to .Ltext0.
28246 In that case we must force using absolute addresses in .debug_loc ranges,
28247 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28248 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28249 list terminator.
28250 Set have_multiple_function_sections to true in that case and
28251 terminate htab traversal. */
28254 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28256 var_loc_list *entry = *slot;
28257 struct var_loc_node *node;
28259 node = entry->first;
28260 if (node && node->next && node->next->label)
28262 unsigned int i;
28263 const char *label = node->next->label;
28264 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28266 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28268 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28269 if (strcmp (label, loclabel) == 0)
28271 have_multiple_function_sections = true;
28272 return 0;
28276 return 1;
28279 /* Hook called after emitting a function into assembly.
28280 This does something only for the very first function emitted. */
28282 static void
28283 dwarf2out_end_function (unsigned int)
28285 if (in_first_function_p
28286 && !have_multiple_function_sections
28287 && first_loclabel_num_not_at_text_label
28288 && decl_loc_table)
28289 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28290 in_first_function_p = false;
28291 maybe_at_text_label_p = false;
28294 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28295 front-ends register a translation unit even before dwarf2out_init is
28296 called. */
28297 static tree main_translation_unit = NULL_TREE;
28299 /* Hook called by front-ends after they built their main translation unit.
28300 Associate comp_unit_die to UNIT. */
28302 static void
28303 dwarf2out_register_main_translation_unit (tree unit)
28305 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28306 && main_translation_unit == NULL_TREE);
28307 main_translation_unit = unit;
28308 /* If dwarf2out_init has not been called yet, it will perform the association
28309 itself looking at main_translation_unit. */
28310 if (decl_die_table != NULL)
28311 equate_decl_number_to_die (unit, comp_unit_die ());
28314 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28316 static void
28317 push_dw_line_info_entry (dw_line_info_table *table,
28318 enum dw_line_info_opcode opcode, unsigned int val)
28320 dw_line_info_entry e;
28321 e.opcode = opcode;
28322 e.val = val;
28323 vec_safe_push (table->entries, e);
28326 /* Output a label to mark the beginning of a source code line entry
28327 and record information relating to this source line, in
28328 'line_info_table' for later output of the .debug_line section. */
28329 /* ??? The discriminator parameter ought to be unsigned. */
28331 static void
28332 dwarf2out_source_line (unsigned int line, unsigned int column,
28333 const char *filename,
28334 int discriminator, bool is_stmt)
28336 unsigned int file_num;
28337 dw_line_info_table *table;
28338 static var_loc_view lvugid;
28340 if (debug_info_level < DINFO_LEVEL_TERSE)
28341 return;
28343 table = cur_line_info_table;
28345 if (line == 0)
28347 if (debug_variable_location_views
28348 && output_asm_line_debug_info ()
28349 && table && !RESETTING_VIEW_P (table->view))
28351 /* If we're using the assembler to compute view numbers, we
28352 can't issue a .loc directive for line zero, so we can't
28353 get a view number at this point. We might attempt to
28354 compute it from the previous view, or equate it to a
28355 subsequent view (though it might not be there!), but
28356 since we're omitting the line number entry, we might as
28357 well omit the view number as well. That means pretending
28358 it's a view number zero, which might very well turn out
28359 to be correct. ??? Extend the assembler so that the
28360 compiler could emit e.g. ".locview .LVU#", to output a
28361 view without changing line number information. We'd then
28362 have to count it in symviews_since_reset; when it's omitted,
28363 it doesn't count. */
28364 if (!zero_view_p)
28365 zero_view_p = BITMAP_GGC_ALLOC ();
28366 bitmap_set_bit (zero_view_p, table->view);
28367 if (flag_debug_asm)
28369 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28370 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28371 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28372 ASM_COMMENT_START);
28373 assemble_name (asm_out_file, label);
28374 putc ('\n', asm_out_file);
28376 table->view = ++lvugid;
28378 return;
28381 /* The discriminator column was added in dwarf4. Simplify the below
28382 by simply removing it if we're not supposed to output it. */
28383 if (dwarf_version < 4 && dwarf_strict)
28384 discriminator = 0;
28386 if (!debug_column_info)
28387 column = 0;
28389 file_num = maybe_emit_file (lookup_filename (filename));
28391 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28392 the debugger has used the second (possibly duplicate) line number
28393 at the beginning of the function to mark the end of the prologue.
28394 We could eliminate any other duplicates within the function. For
28395 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28396 that second line number entry. */
28397 /* Recall that this end-of-prologue indication is *not* the same thing
28398 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28399 to which the hook corresponds, follows the last insn that was
28400 emitted by gen_prologue. What we need is to precede the first insn
28401 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28402 insn that corresponds to something the user wrote. These may be
28403 very different locations once scheduling is enabled. */
28405 if (0 && file_num == table->file_num
28406 && line == table->line_num
28407 && column == table->column_num
28408 && discriminator == table->discrim_num
28409 && is_stmt == table->is_stmt)
28410 return;
28412 switch_to_section (current_function_section ());
28414 /* If requested, emit something human-readable. */
28415 if (flag_debug_asm)
28417 if (debug_column_info)
28418 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28419 filename, line, column);
28420 else
28421 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28422 filename, line);
28425 if (output_asm_line_debug_info ())
28427 /* Emit the .loc directive understood by GNU as. */
28428 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28429 file_num, line, is_stmt, discriminator */
28430 fputs ("\t.loc ", asm_out_file);
28431 fprint_ul (asm_out_file, file_num);
28432 putc (' ', asm_out_file);
28433 fprint_ul (asm_out_file, line);
28434 putc (' ', asm_out_file);
28435 fprint_ul (asm_out_file, column);
28437 if (is_stmt != table->is_stmt)
28439 #if HAVE_GAS_LOC_STMT
28440 fputs (" is_stmt ", asm_out_file);
28441 putc (is_stmt ? '1' : '0', asm_out_file);
28442 #endif
28444 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28446 gcc_assert (discriminator > 0);
28447 fputs (" discriminator ", asm_out_file);
28448 fprint_ul (asm_out_file, (unsigned long) discriminator);
28450 if (debug_variable_location_views)
28452 if (!RESETTING_VIEW_P (table->view))
28454 table->symviews_since_reset++;
28455 if (table->symviews_since_reset > symview_upper_bound)
28456 symview_upper_bound = table->symviews_since_reset;
28457 /* When we're using the assembler to compute view
28458 numbers, we output symbolic labels after "view" in
28459 .loc directives, and the assembler will set them for
28460 us, so that we can refer to the view numbers in
28461 location lists. The only exceptions are when we know
28462 a view will be zero: "-0" is a forced reset, used
28463 e.g. in the beginning of functions, whereas "0" tells
28464 the assembler to check that there was a PC change
28465 since the previous view, in a way that implicitly
28466 resets the next view. */
28467 fputs (" view ", asm_out_file);
28468 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28469 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28470 assemble_name (asm_out_file, label);
28471 table->view = ++lvugid;
28473 else
28475 table->symviews_since_reset = 0;
28476 if (FORCE_RESETTING_VIEW_P (table->view))
28477 fputs (" view -0", asm_out_file);
28478 else
28479 fputs (" view 0", asm_out_file);
28480 /* Mark the present view as a zero view. Earlier debug
28481 binds may have already added its id to loclists to be
28482 emitted later, so we can't reuse the id for something
28483 else. However, it's good to know whether a view is
28484 known to be zero, because then we may be able to
28485 optimize out locviews that are all zeros, so take
28486 note of it in zero_view_p. */
28487 if (!zero_view_p)
28488 zero_view_p = BITMAP_GGC_ALLOC ();
28489 bitmap_set_bit (zero_view_p, lvugid);
28490 table->view = ++lvugid;
28493 putc ('\n', asm_out_file);
28495 else
28497 unsigned int label_num = ++line_info_label_num;
28499 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28501 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28502 push_dw_line_info_entry (table, LI_adv_address, label_num);
28503 else
28504 push_dw_line_info_entry (table, LI_set_address, label_num);
28505 if (debug_variable_location_views)
28507 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28508 if (resetting)
28509 table->view = 0;
28511 if (flag_debug_asm)
28512 fprintf (asm_out_file, "\t%s view %s%d\n",
28513 ASM_COMMENT_START,
28514 resetting ? "-" : "",
28515 table->view);
28517 table->view++;
28519 if (file_num != table->file_num)
28520 push_dw_line_info_entry (table, LI_set_file, file_num);
28521 if (discriminator != table->discrim_num)
28522 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28523 if (is_stmt != table->is_stmt)
28524 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28525 push_dw_line_info_entry (table, LI_set_line, line);
28526 if (debug_column_info)
28527 push_dw_line_info_entry (table, LI_set_column, column);
28530 table->file_num = file_num;
28531 table->line_num = line;
28532 table->column_num = column;
28533 table->discrim_num = discriminator;
28534 table->is_stmt = is_stmt;
28535 table->in_use = true;
28538 /* Record a source file location for a DECL_IGNORED_P function. */
28540 static void
28541 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28542 const char *filename)
28544 dw_fde_ref fde = cfun->fde;
28546 fde->ignored_debug = false;
28547 set_cur_line_info_table (function_section (fde->decl));
28549 dwarf2out_source_line (line, column, filename, 0, true);
28552 /* Record the beginning of a new source file. */
28554 static void
28555 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28557 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28559 macinfo_entry e;
28560 e.code = DW_MACINFO_start_file;
28561 e.lineno = lineno;
28562 e.info = ggc_strdup (filename);
28563 vec_safe_push (macinfo_table, e);
28567 /* Record the end of a source file. */
28569 static void
28570 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28572 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28574 macinfo_entry e;
28575 e.code = DW_MACINFO_end_file;
28576 e.lineno = lineno;
28577 e.info = NULL;
28578 vec_safe_push (macinfo_table, e);
28582 /* Called from debug_define in toplev.c. The `buffer' parameter contains
28583 the tail part of the directive line, i.e. the part which is past the
28584 initial whitespace, #, whitespace, directive-name, whitespace part. */
28586 static void
28587 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28588 const char *buffer ATTRIBUTE_UNUSED)
28590 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28592 macinfo_entry e;
28593 /* Insert a dummy first entry to be able to optimize the whole
28594 predefined macro block using DW_MACRO_import. */
28595 if (macinfo_table->is_empty () && lineno <= 1)
28597 e.code = 0;
28598 e.lineno = 0;
28599 e.info = NULL;
28600 vec_safe_push (macinfo_table, e);
28602 e.code = DW_MACINFO_define;
28603 e.lineno = lineno;
28604 e.info = ggc_strdup (buffer);
28605 vec_safe_push (macinfo_table, e);
28609 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
28610 the tail part of the directive line, i.e. the part which is past the
28611 initial whitespace, #, whitespace, directive-name, whitespace part. */
28613 static void
28614 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28615 const char *buffer ATTRIBUTE_UNUSED)
28617 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28619 macinfo_entry e;
28620 /* Insert a dummy first entry to be able to optimize the whole
28621 predefined macro block using DW_MACRO_import. */
28622 if (macinfo_table->is_empty () && lineno <= 1)
28624 e.code = 0;
28625 e.lineno = 0;
28626 e.info = NULL;
28627 vec_safe_push (macinfo_table, e);
28629 e.code = DW_MACINFO_undef;
28630 e.lineno = lineno;
28631 e.info = ggc_strdup (buffer);
28632 vec_safe_push (macinfo_table, e);
28636 /* Helpers to manipulate hash table of CUs. */
28638 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28640 static inline hashval_t hash (const macinfo_entry *);
28641 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28644 inline hashval_t
28645 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28647 return htab_hash_string (entry->info);
28650 inline bool
28651 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28652 const macinfo_entry *entry2)
28654 return !strcmp (entry1->info, entry2->info);
28657 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28659 /* Output a single .debug_macinfo entry. */
28661 static void
28662 output_macinfo_op (macinfo_entry *ref)
28664 int file_num;
28665 size_t len;
28666 struct indirect_string_node *node;
28667 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28668 struct dwarf_file_data *fd;
28670 switch (ref->code)
28672 case DW_MACINFO_start_file:
28673 fd = lookup_filename (ref->info);
28674 file_num = maybe_emit_file (fd);
28675 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28676 dw2_asm_output_data_uleb128 (ref->lineno,
28677 "Included from line number %lu",
28678 (unsigned long) ref->lineno);
28679 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28680 break;
28681 case DW_MACINFO_end_file:
28682 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28683 break;
28684 case DW_MACINFO_define:
28685 case DW_MACINFO_undef:
28686 len = strlen (ref->info) + 1;
28687 if ((!dwarf_strict || dwarf_version >= 5)
28688 && len > (size_t) dwarf_offset_size
28689 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28690 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28692 if (dwarf_split_debug_info && dwarf_version >= 5)
28693 ref->code = ref->code == DW_MACINFO_define
28694 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
28695 else
28696 ref->code = ref->code == DW_MACINFO_define
28697 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28698 output_macinfo_op (ref);
28699 return;
28701 dw2_asm_output_data (1, ref->code,
28702 ref->code == DW_MACINFO_define
28703 ? "Define macro" : "Undefine macro");
28704 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28705 (unsigned long) ref->lineno);
28706 dw2_asm_output_nstring (ref->info, -1, "The macro");
28707 break;
28708 case DW_MACRO_define_strp:
28709 dw2_asm_output_data (1, ref->code, "Define macro strp");
28710 goto do_DW_MACRO_define_strpx;
28711 case DW_MACRO_undef_strp:
28712 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
28713 goto do_DW_MACRO_define_strpx;
28714 case DW_MACRO_define_strx:
28715 dw2_asm_output_data (1, ref->code, "Define macro strx");
28716 goto do_DW_MACRO_define_strpx;
28717 case DW_MACRO_undef_strx:
28718 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
28719 /* FALLTHRU */
28720 do_DW_MACRO_define_strpx:
28721 /* NB: dwarf2out_finish performs:
28722 1. save_macinfo_strings
28723 2. hash table traverse of index_string
28724 3. output_macinfo -> output_macinfo_op
28725 4. output_indirect_strings
28726 -> hash table traverse of output_index_string
28728 When output_macinfo_op is called, all index strings have been
28729 added to hash table by save_macinfo_strings and we can't pass
28730 INSERT to find_slot_with_hash which may expand hash table, even
28731 if no insertion is needed, and change hash table traverse order
28732 between index_string and output_index_string. */
28733 node = find_AT_string (ref->info, NO_INSERT);
28734 gcc_assert (node
28735 && (node->form == DW_FORM_strp
28736 || node->form == dwarf_FORM (DW_FORM_strx)));
28737 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28738 (unsigned long) ref->lineno);
28739 if (node->form == DW_FORM_strp)
28740 dw2_asm_output_offset (dwarf_offset_size, node->label,
28741 debug_str_section, "The macro: \"%s\"",
28742 ref->info);
28743 else
28744 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28745 ref->info);
28746 break;
28747 case DW_MACRO_import:
28748 dw2_asm_output_data (1, ref->code, "Import");
28749 ASM_GENERATE_INTERNAL_LABEL (label,
28750 DEBUG_MACRO_SECTION_LABEL,
28751 ref->lineno + macinfo_label_base);
28752 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
28753 break;
28754 default:
28755 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28756 ASM_COMMENT_START, (unsigned long) ref->code);
28757 break;
28761 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28762 other compilation unit .debug_macinfo sections. IDX is the first
28763 index of a define/undef, return the number of ops that should be
28764 emitted in a comdat .debug_macinfo section and emit
28765 a DW_MACRO_import entry referencing it.
28766 If the define/undef entry should be emitted normally, return 0. */
28768 static unsigned
28769 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28770 macinfo_hash_type **macinfo_htab)
28772 macinfo_entry *first, *second, *cur, *inc;
28773 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28774 unsigned char checksum[16];
28775 struct md5_ctx ctx;
28776 char *grp_name, *tail;
28777 const char *base;
28778 unsigned int i, count, encoded_filename_len, linebuf_len;
28779 macinfo_entry **slot;
28781 first = &(*macinfo_table)[idx];
28782 second = &(*macinfo_table)[idx + 1];
28784 /* Optimize only if there are at least two consecutive define/undef ops,
28785 and either all of them are before first DW_MACINFO_start_file
28786 with lineno {0,1} (i.e. predefined macro block), or all of them are
28787 in some included header file. */
28788 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28789 return 0;
28790 if (vec_safe_is_empty (files))
28792 if (first->lineno > 1 || second->lineno > 1)
28793 return 0;
28795 else if (first->lineno == 0)
28796 return 0;
28798 /* Find the last define/undef entry that can be grouped together
28799 with first and at the same time compute md5 checksum of their
28800 codes, linenumbers and strings. */
28801 md5_init_ctx (&ctx);
28802 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28803 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28804 break;
28805 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28806 break;
28807 else
28809 unsigned char code = cur->code;
28810 md5_process_bytes (&code, 1, &ctx);
28811 checksum_uleb128 (cur->lineno, &ctx);
28812 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28814 md5_finish_ctx (&ctx, checksum);
28815 count = i - idx;
28817 /* From the containing include filename (if any) pick up just
28818 usable characters from its basename. */
28819 if (vec_safe_is_empty (files))
28820 base = "";
28821 else
28822 base = lbasename (files->last ().info);
28823 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28824 if (ISIDNUM (base[i]) || base[i] == '.')
28825 encoded_filename_len++;
28826 /* Count . at the end. */
28827 if (encoded_filename_len)
28828 encoded_filename_len++;
28830 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28831 linebuf_len = strlen (linebuf);
28833 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28834 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28835 + 16 * 2 + 1);
28836 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
28837 tail = grp_name + 4;
28838 if (encoded_filename_len)
28840 for (i = 0; base[i]; i++)
28841 if (ISIDNUM (base[i]) || base[i] == '.')
28842 *tail++ = base[i];
28843 *tail++ = '.';
28845 memcpy (tail, linebuf, linebuf_len);
28846 tail += linebuf_len;
28847 *tail++ = '.';
28848 for (i = 0; i < 16; i++)
28849 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28851 /* Construct a macinfo_entry for DW_MACRO_import
28852 in the empty vector entry before the first define/undef. */
28853 inc = &(*macinfo_table)[idx - 1];
28854 inc->code = DW_MACRO_import;
28855 inc->lineno = 0;
28856 inc->info = ggc_strdup (grp_name);
28857 if (!*macinfo_htab)
28858 *macinfo_htab = new macinfo_hash_type (10);
28859 /* Avoid emitting duplicates. */
28860 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28861 if (*slot != NULL)
28863 inc->code = 0;
28864 inc->info = NULL;
28865 /* If such an entry has been used before, just emit
28866 a DW_MACRO_import op. */
28867 inc = *slot;
28868 output_macinfo_op (inc);
28869 /* And clear all macinfo_entry in the range to avoid emitting them
28870 in the second pass. */
28871 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28873 cur->code = 0;
28874 cur->info = NULL;
28877 else
28879 *slot = inc;
28880 inc->lineno = (*macinfo_htab)->elements ();
28881 output_macinfo_op (inc);
28883 return count;
28886 /* Save any strings needed by the macinfo table in the debug str
28887 table. All strings must be collected into the table by the time
28888 index_string is called. */
28890 static void
28891 save_macinfo_strings (void)
28893 unsigned len;
28894 unsigned i;
28895 macinfo_entry *ref;
28897 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28899 switch (ref->code)
28901 /* Match the logic in output_macinfo_op to decide on
28902 indirect strings. */
28903 case DW_MACINFO_define:
28904 case DW_MACINFO_undef:
28905 len = strlen (ref->info) + 1;
28906 if ((!dwarf_strict || dwarf_version >= 5)
28907 && len > (unsigned) dwarf_offset_size
28908 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28909 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28910 set_indirect_string (find_AT_string (ref->info));
28911 break;
28912 case DW_MACINFO_start_file:
28913 /* -gsplit-dwarf -g3 will also output filename as indirect
28914 string. */
28915 if (!dwarf_split_debug_info)
28916 break;
28917 /* Fall through. */
28918 case DW_MACRO_define_strp:
28919 case DW_MACRO_undef_strp:
28920 case DW_MACRO_define_strx:
28921 case DW_MACRO_undef_strx:
28922 set_indirect_string (find_AT_string (ref->info));
28923 break;
28924 default:
28925 break;
28930 /* Output macinfo section(s). */
28932 static void
28933 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28935 unsigned i;
28936 unsigned long length = vec_safe_length (macinfo_table);
28937 macinfo_entry *ref;
28938 vec<macinfo_entry, va_gc> *files = NULL;
28939 macinfo_hash_type *macinfo_htab = NULL;
28940 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28942 if (! length)
28943 return;
28945 /* output_macinfo* uses these interchangeably. */
28946 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28947 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28948 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28949 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28951 /* AIX Assembler inserts the length, so adjust the reference to match the
28952 offset expected by debuggers. */
28953 strcpy (dl_section_ref, debug_line_label);
28954 if (XCOFF_DEBUGGING_INFO)
28955 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28957 /* For .debug_macro emit the section header. */
28958 if (!dwarf_strict || dwarf_version >= 5)
28960 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28961 "DWARF macro version number");
28962 if (dwarf_offset_size == 8)
28963 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28964 else
28965 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28966 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
28967 debug_line_section, NULL);
28970 /* In the first loop, it emits the primary .debug_macinfo section
28971 and after each emitted op the macinfo_entry is cleared.
28972 If a longer range of define/undef ops can be optimized using
28973 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28974 the vector before the first define/undef in the range and the
28975 whole range of define/undef ops is not emitted and kept. */
28976 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28978 switch (ref->code)
28980 case DW_MACINFO_start_file:
28981 vec_safe_push (files, *ref);
28982 break;
28983 case DW_MACINFO_end_file:
28984 if (!vec_safe_is_empty (files))
28985 files->pop ();
28986 break;
28987 case DW_MACINFO_define:
28988 case DW_MACINFO_undef:
28989 if ((!dwarf_strict || dwarf_version >= 5)
28990 && HAVE_COMDAT_GROUP
28991 && vec_safe_length (files) != 1
28992 && i > 0
28993 && i + 1 < length
28994 && (*macinfo_table)[i - 1].code == 0)
28996 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28997 if (count)
28999 i += count - 1;
29000 continue;
29003 break;
29004 case 0:
29005 /* A dummy entry may be inserted at the beginning to be able
29006 to optimize the whole block of predefined macros. */
29007 if (i == 0)
29008 continue;
29009 default:
29010 break;
29012 output_macinfo_op (ref);
29013 ref->info = NULL;
29014 ref->code = 0;
29017 if (!macinfo_htab)
29018 return;
29020 /* Save the number of transparent includes so we can adjust the
29021 label number for the fat LTO object DWARF. */
29022 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29024 delete macinfo_htab;
29025 macinfo_htab = NULL;
29027 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29028 terminate the current chain and switch to a new comdat .debug_macinfo
29029 section and emit the define/undef entries within it. */
29030 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29031 switch (ref->code)
29033 case 0:
29034 continue;
29035 case DW_MACRO_import:
29037 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29038 tree comdat_key = get_identifier (ref->info);
29039 /* Terminate the previous .debug_macinfo section. */
29040 dw2_asm_output_data (1, 0, "End compilation unit");
29041 targetm.asm_out.named_section (debug_macinfo_section_name,
29042 SECTION_DEBUG
29043 | SECTION_LINKONCE
29044 | (early_lto_debug
29045 ? SECTION_EXCLUDE : 0),
29046 comdat_key);
29047 ASM_GENERATE_INTERNAL_LABEL (label,
29048 DEBUG_MACRO_SECTION_LABEL,
29049 ref->lineno + macinfo_label_base);
29050 ASM_OUTPUT_LABEL (asm_out_file, label);
29051 ref->code = 0;
29052 ref->info = NULL;
29053 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29054 "DWARF macro version number");
29055 if (dwarf_offset_size == 8)
29056 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29057 else
29058 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29060 break;
29061 case DW_MACINFO_define:
29062 case DW_MACINFO_undef:
29063 output_macinfo_op (ref);
29064 ref->code = 0;
29065 ref->info = NULL;
29066 break;
29067 default:
29068 gcc_unreachable ();
29071 macinfo_label_base += macinfo_label_base_adj;
29074 /* As init_sections_and_labels may get called multiple times, have a
29075 generation count for labels. */
29076 static unsigned init_sections_and_labels_generation;
29078 /* Initialize the various sections and labels for dwarf output and prefix
29079 them with PREFIX if non-NULL. Returns the generation (zero based
29080 number of times function was called). */
29082 static unsigned
29083 init_sections_and_labels (bool early_lto_debug)
29085 if (early_lto_debug)
29087 if (!dwarf_split_debug_info)
29089 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29090 SECTION_DEBUG | SECTION_EXCLUDE,
29091 NULL);
29092 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29093 SECTION_DEBUG | SECTION_EXCLUDE,
29094 NULL);
29095 debug_macinfo_section_name
29096 = ((dwarf_strict && dwarf_version < 5)
29097 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29098 debug_macinfo_section = get_section (debug_macinfo_section_name,
29099 SECTION_DEBUG
29100 | SECTION_EXCLUDE, NULL);
29102 else
29104 /* ??? Which of the following do we need early? */
29105 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29106 SECTION_DEBUG | SECTION_EXCLUDE,
29107 NULL);
29108 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29109 SECTION_DEBUG | SECTION_EXCLUDE,
29110 NULL);
29111 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29112 SECTION_DEBUG
29113 | SECTION_EXCLUDE, NULL);
29114 debug_skeleton_abbrev_section
29115 = get_section (DEBUG_LTO_ABBREV_SECTION,
29116 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29117 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29118 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29119 init_sections_and_labels_generation);
29121 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29122 stay in the main .o, but the skeleton_line goes into the split
29123 off dwo. */
29124 debug_skeleton_line_section
29125 = get_section (DEBUG_LTO_LINE_SECTION,
29126 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29127 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29128 DEBUG_SKELETON_LINE_SECTION_LABEL,
29129 init_sections_and_labels_generation);
29130 debug_str_offsets_section
29131 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29132 SECTION_DEBUG | SECTION_EXCLUDE,
29133 NULL);
29134 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29135 DEBUG_SKELETON_INFO_SECTION_LABEL,
29136 init_sections_and_labels_generation);
29137 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29138 DEBUG_STR_DWO_SECTION_FLAGS,
29139 NULL);
29140 debug_macinfo_section_name
29141 = ((dwarf_strict && dwarf_version < 5)
29142 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29143 debug_macinfo_section = get_section (debug_macinfo_section_name,
29144 SECTION_DEBUG | SECTION_EXCLUDE,
29145 NULL);
29147 /* For macro info and the file table we have to refer to a
29148 debug_line section. */
29149 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29150 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29151 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29152 DEBUG_LINE_SECTION_LABEL,
29153 init_sections_and_labels_generation);
29155 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29156 DEBUG_STR_SECTION_FLAGS
29157 | SECTION_EXCLUDE, NULL);
29158 if (!dwarf_split_debug_info)
29159 debug_line_str_section
29160 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29161 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29163 else
29165 if (!dwarf_split_debug_info)
29167 debug_info_section = get_section (DEBUG_INFO_SECTION,
29168 SECTION_DEBUG, NULL);
29169 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29170 SECTION_DEBUG, NULL);
29171 debug_loc_section = get_section (dwarf_version >= 5
29172 ? DEBUG_LOCLISTS_SECTION
29173 : DEBUG_LOC_SECTION,
29174 SECTION_DEBUG, NULL);
29175 debug_macinfo_section_name
29176 = ((dwarf_strict && dwarf_version < 5)
29177 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29178 debug_macinfo_section = get_section (debug_macinfo_section_name,
29179 SECTION_DEBUG, NULL);
29181 else
29183 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29184 SECTION_DEBUG | SECTION_EXCLUDE,
29185 NULL);
29186 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29187 SECTION_DEBUG | SECTION_EXCLUDE,
29188 NULL);
29189 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29190 SECTION_DEBUG, NULL);
29191 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29192 SECTION_DEBUG, NULL);
29193 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29194 SECTION_DEBUG, NULL);
29195 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29196 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29197 init_sections_and_labels_generation);
29199 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29200 stay in the main .o, but the skeleton_line goes into the
29201 split off dwo. */
29202 debug_skeleton_line_section
29203 = get_section (DEBUG_DWO_LINE_SECTION,
29204 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29205 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29206 DEBUG_SKELETON_LINE_SECTION_LABEL,
29207 init_sections_and_labels_generation);
29208 debug_str_offsets_section
29209 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29210 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29211 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29212 DEBUG_SKELETON_INFO_SECTION_LABEL,
29213 init_sections_and_labels_generation);
29214 debug_loc_section = get_section (dwarf_version >= 5
29215 ? DEBUG_DWO_LOCLISTS_SECTION
29216 : DEBUG_DWO_LOC_SECTION,
29217 SECTION_DEBUG | SECTION_EXCLUDE,
29218 NULL);
29219 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29220 DEBUG_STR_DWO_SECTION_FLAGS,
29221 NULL);
29222 debug_macinfo_section_name
29223 = ((dwarf_strict && dwarf_version < 5)
29224 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29225 debug_macinfo_section = get_section (debug_macinfo_section_name,
29226 SECTION_DEBUG | SECTION_EXCLUDE,
29227 NULL);
29228 if (dwarf_version >= 5)
29229 debug_ranges_dwo_section
29230 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29231 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29233 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29234 SECTION_DEBUG, NULL);
29235 debug_line_section = get_section (DEBUG_LINE_SECTION,
29236 SECTION_DEBUG, NULL);
29237 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29238 SECTION_DEBUG, NULL);
29239 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29240 SECTION_DEBUG, NULL);
29241 debug_str_section = get_section (DEBUG_STR_SECTION,
29242 DEBUG_STR_SECTION_FLAGS, NULL);
29243 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29244 || asm_outputs_debug_line_str ())
29245 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29246 DEBUG_STR_SECTION_FLAGS, NULL);
29248 debug_ranges_section = get_section (dwarf_version >= 5
29249 ? DEBUG_RNGLISTS_SECTION
29250 : DEBUG_RANGES_SECTION,
29251 SECTION_DEBUG, NULL);
29252 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29253 SECTION_DEBUG, NULL);
29256 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29257 DEBUG_ABBREV_SECTION_LABEL,
29258 init_sections_and_labels_generation);
29259 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29260 DEBUG_INFO_SECTION_LABEL,
29261 init_sections_and_labels_generation);
29262 info_section_emitted = false;
29263 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29264 DEBUG_LINE_SECTION_LABEL,
29265 init_sections_and_labels_generation);
29266 /* There are up to 6 unique ranges labels per generation.
29267 See also output_rnglists. */
29268 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29269 DEBUG_RANGES_SECTION_LABEL,
29270 init_sections_and_labels_generation * 6);
29271 if (dwarf_version >= 5 && dwarf_split_debug_info)
29272 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29273 DEBUG_RANGES_SECTION_LABEL,
29274 1 + init_sections_and_labels_generation * 6);
29275 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29276 DEBUG_ADDR_SECTION_LABEL,
29277 init_sections_and_labels_generation);
29278 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29279 (dwarf_strict && dwarf_version < 5)
29280 ? DEBUG_MACINFO_SECTION_LABEL
29281 : DEBUG_MACRO_SECTION_LABEL,
29282 init_sections_and_labels_generation);
29283 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29284 init_sections_and_labels_generation);
29286 ++init_sections_and_labels_generation;
29287 return init_sections_and_labels_generation - 1;
29290 /* Set up for Dwarf output at the start of compilation. */
29292 static void
29293 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29295 /* Allocate the file_table. */
29296 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29298 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29299 /* Allocate the decl_die_table. */
29300 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29302 /* Allocate the decl_loc_table. */
29303 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29305 /* Allocate the cached_dw_loc_list_table. */
29306 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29308 /* Allocate the initial hunk of the abbrev_die_table. */
29309 vec_alloc (abbrev_die_table, 256);
29310 /* Zero-th entry is allocated, but unused. */
29311 abbrev_die_table->quick_push (NULL);
29313 /* Allocate the dwarf_proc_stack_usage_map. */
29314 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29316 /* Allocate the pubtypes and pubnames vectors. */
29317 vec_alloc (pubname_table, 32);
29318 vec_alloc (pubtype_table, 32);
29320 vec_alloc (incomplete_types, 64);
29322 vec_alloc (used_rtx_array, 32);
29324 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29325 vec_alloc (macinfo_table, 64);
29326 #endif
29328 /* If front-ends already registered a main translation unit but we were not
29329 ready to perform the association, do this now. */
29330 if (main_translation_unit != NULL_TREE)
29331 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29334 /* Called before compile () starts outputtting functions, variables
29335 and toplevel asms into assembly. */
29337 static void
29338 dwarf2out_assembly_start (void)
29340 if (text_section_line_info)
29341 return;
29343 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29344 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29345 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29346 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29347 COLD_TEXT_SECTION_LABEL, 0);
29348 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29350 switch_to_section (text_section);
29351 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29352 #endif
29354 /* Make sure the line number table for .text always exists. */
29355 text_section_line_info = new_line_info_table ();
29356 text_section_line_info->end_label = text_end_label;
29358 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29359 cur_line_info_table = text_section_line_info;
29360 #endif
29362 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29363 && dwarf2out_do_cfi_asm ()
29364 && !dwarf2out_do_eh_frame ())
29365 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29367 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29368 if (output_asm_line_debug_info () && dwarf_version >= 5)
29370 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29371 tell it the comp_dir and main file name for the zero entry
29372 line table. */
29373 const char *comp_dir, *filename0;
29375 comp_dir = comp_dir_string ();
29376 if (comp_dir == NULL)
29377 comp_dir = "";
29379 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29380 if (filename0 == NULL)
29381 filename0 = "";
29383 fprintf (asm_out_file, "\t.file 0 ");
29384 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29385 fputc (' ', asm_out_file);
29386 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29387 fputc ('\n', asm_out_file);
29389 #endif
29392 /* A helper function for dwarf2out_finish called through
29393 htab_traverse. Assign a string its index. All strings must be
29394 collected into the table by the time index_string is called,
29395 because the indexing code relies on htab_traverse to traverse nodes
29396 in the same order for each run. */
29399 index_string (indirect_string_node **h, unsigned int *index)
29401 indirect_string_node *node = *h;
29403 find_string_form (node);
29404 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29406 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29407 node->index = *index;
29408 *index += 1;
29410 return 1;
29413 /* A helper function for output_indirect_strings called through
29414 htab_traverse. Output the offset to a string and update the
29415 current offset. */
29418 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29420 indirect_string_node *node = *h;
29422 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29424 /* Assert that this node has been assigned an index. */
29425 gcc_assert (node->index != NO_INDEX_ASSIGNED
29426 && node->index != NOT_INDEXED);
29427 dw2_asm_output_data (dwarf_offset_size, *offset,
29428 "indexed string 0x%x: %s", node->index, node->str);
29429 *offset += strlen (node->str) + 1;
29431 return 1;
29434 /* A helper function for dwarf2out_finish called through
29435 htab_traverse. Output the indexed string. */
29438 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29440 struct indirect_string_node *node = *h;
29442 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29444 /* Assert that the strings are output in the same order as their
29445 indexes were assigned. */
29446 gcc_assert (*cur_idx == node->index);
29447 assemble_string (node->str, strlen (node->str) + 1);
29448 *cur_idx += 1;
29450 return 1;
29453 /* A helper function for output_indirect_strings. Counts the number
29454 of index strings offsets. Must match the logic of the functions
29455 output_index_string[_offsets] above. */
29457 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29459 struct indirect_string_node *node = *h;
29461 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29462 *last_idx += 1;
29463 return 1;
29466 /* A helper function for dwarf2out_finish called through
29467 htab_traverse. Emit one queued .debug_str string. */
29470 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29472 struct indirect_string_node *node = *h;
29474 node->form = find_string_form (node);
29475 if (node->form == form && node->refcount > 0)
29477 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29478 assemble_string (node->str, strlen (node->str) + 1);
29481 return 1;
29484 /* Output the indexed string table. */
29486 static void
29487 output_indirect_strings (void)
29489 switch_to_section (debug_str_section);
29490 if (!dwarf_split_debug_info)
29491 debug_str_hash->traverse<enum dwarf_form,
29492 output_indirect_string> (DW_FORM_strp);
29493 else
29495 unsigned int offset = 0;
29496 unsigned int cur_idx = 0;
29498 if (skeleton_debug_str_hash)
29499 skeleton_debug_str_hash->traverse<enum dwarf_form,
29500 output_indirect_string> (DW_FORM_strp);
29502 switch_to_section (debug_str_offsets_section);
29503 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29504 header. Note that we don't need to generate a label to the
29505 actual index table following the header here, because this is
29506 for the split dwarf case only. In an .dwo file there is only
29507 one string offsets table (and one debug info section). But
29508 if we would start using string offset tables for the main (or
29509 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29510 pointing to the actual index after the header. Split dwarf
29511 units will never have a string offsets base attribute. When
29512 a split unit is moved into a .dwp file the string offsets can
29513 be found through the .debug_cu_index section table. */
29514 if (dwarf_version >= 5)
29516 unsigned int last_idx = 0;
29517 unsigned long str_offsets_length;
29519 debug_str_hash->traverse_noresize
29520 <unsigned int *, count_index_strings> (&last_idx);
29521 str_offsets_length = last_idx * dwarf_offset_size + 4;
29522 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29523 dw2_asm_output_data (4, 0xffffffff,
29524 "Escape value for 64-bit DWARF extension");
29525 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29526 "Length of string offsets unit");
29527 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29528 dw2_asm_output_data (2, 0, "Header zero padding");
29530 debug_str_hash->traverse_noresize
29531 <unsigned int *, output_index_string_offset> (&offset);
29532 switch_to_section (debug_str_dwo_section);
29533 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29534 (&cur_idx);
29538 /* Callback for htab_traverse to assign an index to an entry in the
29539 table, and to write that entry to the .debug_addr section. */
29542 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29544 addr_table_entry *entry = *slot;
29546 if (entry->refcount == 0)
29548 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29549 || entry->index == NOT_INDEXED);
29550 return 1;
29553 gcc_assert (entry->index == *cur_index);
29554 (*cur_index)++;
29556 switch (entry->kind)
29558 case ate_kind_rtx:
29559 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29560 "0x%x", entry->index);
29561 break;
29562 case ate_kind_rtx_dtprel:
29563 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29564 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29565 DWARF2_ADDR_SIZE,
29566 entry->addr.rtl);
29567 fputc ('\n', asm_out_file);
29568 break;
29569 case ate_kind_label:
29570 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29571 "0x%x", entry->index);
29572 break;
29573 default:
29574 gcc_unreachable ();
29576 return 1;
29579 /* A helper function for dwarf2out_finish. Counts the number
29580 of indexed addresses. Must match the logic of the functions
29581 output_addr_table_entry above. */
29583 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29585 addr_table_entry *entry = *slot;
29587 if (entry->refcount > 0)
29588 *last_idx += 1;
29589 return 1;
29592 /* Produce the .debug_addr section. */
29594 static void
29595 output_addr_table (void)
29597 unsigned int index = 0;
29598 if (addr_index_table == NULL || addr_index_table->size () == 0)
29599 return;
29601 switch_to_section (debug_addr_section);
29602 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29603 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29604 before DWARF5, didn't have a header for .debug_addr units.
29605 DWARF5 specifies a small header when address tables are used. */
29606 if (dwarf_version >= 5)
29608 unsigned int last_idx = 0;
29609 unsigned long addrs_length;
29611 addr_index_table->traverse_noresize
29612 <unsigned int *, count_index_addrs> (&last_idx);
29613 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29615 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29616 dw2_asm_output_data (4, 0xffffffff,
29617 "Escape value for 64-bit DWARF extension");
29618 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29619 "Length of Address Unit");
29620 dw2_asm_output_data (2, 5, "DWARF addr version");
29621 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29622 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29624 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29626 addr_index_table
29627 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29630 #if ENABLE_ASSERT_CHECKING
29631 /* Verify that all marks are clear. */
29633 static void
29634 verify_marks_clear (dw_die_ref die)
29636 dw_die_ref c;
29638 gcc_assert (! die->die_mark);
29639 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29641 #endif /* ENABLE_ASSERT_CHECKING */
29643 /* Clear the marks for a die and its children.
29644 Be cool if the mark isn't set. */
29646 static void
29647 prune_unmark_dies (dw_die_ref die)
29649 dw_die_ref c;
29651 if (die->die_mark)
29652 die->die_mark = 0;
29653 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29656 /* Given LOC that is referenced by a DIE we're marking as used, find all
29657 referenced DWARF procedures it references and mark them as used. */
29659 static void
29660 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29662 for (; loc != NULL; loc = loc->dw_loc_next)
29663 switch (loc->dw_loc_opc)
29665 case DW_OP_implicit_pointer:
29666 case DW_OP_convert:
29667 case DW_OP_reinterpret:
29668 case DW_OP_GNU_implicit_pointer:
29669 case DW_OP_GNU_convert:
29670 case DW_OP_GNU_reinterpret:
29671 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29672 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29673 break;
29674 case DW_OP_GNU_variable_value:
29675 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29677 dw_die_ref ref
29678 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29679 if (ref == NULL)
29680 break;
29681 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29682 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29683 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29685 /* FALLTHRU */
29686 case DW_OP_call2:
29687 case DW_OP_call4:
29688 case DW_OP_call_ref:
29689 case DW_OP_const_type:
29690 case DW_OP_GNU_const_type:
29691 case DW_OP_GNU_parameter_ref:
29692 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29693 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29694 break;
29695 case DW_OP_regval_type:
29696 case DW_OP_deref_type:
29697 case DW_OP_GNU_regval_type:
29698 case DW_OP_GNU_deref_type:
29699 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29700 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29701 break;
29702 case DW_OP_entry_value:
29703 case DW_OP_GNU_entry_value:
29704 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29705 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29706 break;
29707 default:
29708 break;
29712 /* Given DIE that we're marking as used, find any other dies
29713 it references as attributes and mark them as used. */
29715 static void
29716 prune_unused_types_walk_attribs (dw_die_ref die)
29718 dw_attr_node *a;
29719 unsigned ix;
29721 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29723 switch (AT_class (a))
29725 /* Make sure DWARF procedures referenced by location descriptions will
29726 get emitted. */
29727 case dw_val_class_loc:
29728 prune_unused_types_walk_loc_descr (AT_loc (a));
29729 break;
29730 case dw_val_class_loc_list:
29731 for (dw_loc_list_ref list = AT_loc_list (a);
29732 list != NULL;
29733 list = list->dw_loc_next)
29734 prune_unused_types_walk_loc_descr (list->expr);
29735 break;
29737 case dw_val_class_view_list:
29738 /* This points to a loc_list in another attribute, so it's
29739 already covered. */
29740 break;
29742 case dw_val_class_die_ref:
29743 /* A reference to another DIE.
29744 Make sure that it will get emitted.
29745 If it was broken out into a comdat group, don't follow it. */
29746 if (! AT_ref (a)->comdat_type_p
29747 || a->dw_attr == DW_AT_specification)
29748 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29749 break;
29751 case dw_val_class_str:
29752 /* Set the string's refcount to 0 so that prune_unused_types_mark
29753 accounts properly for it. */
29754 a->dw_attr_val.v.val_str->refcount = 0;
29755 break;
29757 default:
29758 break;
29763 /* Mark the generic parameters and arguments children DIEs of DIE. */
29765 static void
29766 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29768 dw_die_ref c;
29770 if (die == NULL || die->die_child == NULL)
29771 return;
29772 c = die->die_child;
29775 if (is_template_parameter (c))
29776 prune_unused_types_mark (c, 1);
29777 c = c->die_sib;
29778 } while (c && c != die->die_child);
29781 /* Mark DIE as being used. If DOKIDS is true, then walk down
29782 to DIE's children. */
29784 static void
29785 prune_unused_types_mark (dw_die_ref die, int dokids)
29787 dw_die_ref c;
29789 if (die->die_mark == 0)
29791 /* We haven't done this node yet. Mark it as used. */
29792 die->die_mark = 1;
29793 /* If this is the DIE of a generic type instantiation,
29794 mark the children DIEs that describe its generic parms and
29795 args. */
29796 prune_unused_types_mark_generic_parms_dies (die);
29798 /* We also have to mark its parents as used.
29799 (But we don't want to mark our parent's kids due to this,
29800 unless it is a class.) */
29801 if (die->die_parent)
29802 prune_unused_types_mark (die->die_parent,
29803 class_scope_p (die->die_parent));
29805 /* Mark any referenced nodes. */
29806 prune_unused_types_walk_attribs (die);
29808 /* If this node is a specification,
29809 also mark the definition, if it exists. */
29810 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29811 prune_unused_types_mark (die->die_definition, 1);
29814 if (dokids && die->die_mark != 2)
29816 /* We need to walk the children, but haven't done so yet.
29817 Remember that we've walked the kids. */
29818 die->die_mark = 2;
29820 /* If this is an array type, we need to make sure our
29821 kids get marked, even if they're types. If we're
29822 breaking out types into comdat sections, do this
29823 for all type definitions. */
29824 if (die->die_tag == DW_TAG_array_type
29825 || (use_debug_types
29826 && is_type_die (die) && ! is_declaration_die (die)))
29827 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29828 else
29829 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29833 /* For local classes, look if any static member functions were emitted
29834 and if so, mark them. */
29836 static void
29837 prune_unused_types_walk_local_classes (dw_die_ref die)
29839 dw_die_ref c;
29841 if (die->die_mark == 2)
29842 return;
29844 switch (die->die_tag)
29846 case DW_TAG_structure_type:
29847 case DW_TAG_union_type:
29848 case DW_TAG_class_type:
29849 case DW_TAG_interface_type:
29850 break;
29852 case DW_TAG_subprogram:
29853 if (!get_AT_flag (die, DW_AT_declaration)
29854 || die->die_definition != NULL)
29855 prune_unused_types_mark (die, 1);
29856 return;
29858 default:
29859 return;
29862 /* Mark children. */
29863 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29866 /* Walk the tree DIE and mark types that we actually use. */
29868 static void
29869 prune_unused_types_walk (dw_die_ref die)
29871 dw_die_ref c;
29873 /* Don't do anything if this node is already marked and
29874 children have been marked as well. */
29875 if (die->die_mark == 2)
29876 return;
29878 switch (die->die_tag)
29880 case DW_TAG_structure_type:
29881 case DW_TAG_union_type:
29882 case DW_TAG_class_type:
29883 case DW_TAG_interface_type:
29884 if (die->die_perennial_p)
29885 break;
29887 for (c = die->die_parent; c; c = c->die_parent)
29888 if (c->die_tag == DW_TAG_subprogram)
29889 break;
29891 /* Finding used static member functions inside of classes
29892 is needed just for local classes, because for other classes
29893 static member function DIEs with DW_AT_specification
29894 are emitted outside of the DW_TAG_*_type. If we ever change
29895 it, we'd need to call this even for non-local classes. */
29896 if (c)
29897 prune_unused_types_walk_local_classes (die);
29899 /* It's a type node --- don't mark it. */
29900 return;
29902 case DW_TAG_const_type:
29903 case DW_TAG_packed_type:
29904 case DW_TAG_pointer_type:
29905 case DW_TAG_reference_type:
29906 case DW_TAG_rvalue_reference_type:
29907 case DW_TAG_volatile_type:
29908 case DW_TAG_typedef:
29909 case DW_TAG_array_type:
29910 case DW_TAG_friend:
29911 case DW_TAG_enumeration_type:
29912 case DW_TAG_subroutine_type:
29913 case DW_TAG_string_type:
29914 case DW_TAG_set_type:
29915 case DW_TAG_subrange_type:
29916 case DW_TAG_ptr_to_member_type:
29917 case DW_TAG_file_type:
29918 /* Type nodes are useful only when other DIEs reference them --- don't
29919 mark them. */
29920 /* FALLTHROUGH */
29922 case DW_TAG_dwarf_procedure:
29923 /* Likewise for DWARF procedures. */
29925 if (die->die_perennial_p)
29926 break;
29928 return;
29930 case DW_TAG_variable:
29931 if (flag_debug_only_used_symbols)
29933 if (die->die_perennial_p)
29934 break;
29936 /* For static data members, the declaration in the class is supposed
29937 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
29938 DWARF5. DW_TAG_member will be marked, so mark even such
29939 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
29940 attribute. */
29941 if (dwarf_version >= 5
29942 && class_scope_p (die->die_parent)
29943 && get_AT (die, DW_AT_const_value))
29944 break;
29946 /* premark_used_variables marks external variables --- don't mark
29947 them here. But function-local externals are always considered
29948 used. */
29949 if (get_AT (die, DW_AT_external))
29951 for (c = die->die_parent; c; c = c->die_parent)
29952 if (c->die_tag == DW_TAG_subprogram)
29953 break;
29954 if (!c)
29955 return;
29958 /* FALLTHROUGH */
29960 default:
29961 /* Mark everything else. */
29962 break;
29965 if (die->die_mark == 0)
29967 die->die_mark = 1;
29969 /* Now, mark any dies referenced from here. */
29970 prune_unused_types_walk_attribs (die);
29973 die->die_mark = 2;
29975 /* Mark children. */
29976 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29979 /* Increment the string counts on strings referred to from DIE's
29980 attributes. */
29982 static void
29983 prune_unused_types_update_strings (dw_die_ref die)
29985 dw_attr_node *a;
29986 unsigned ix;
29988 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29989 if (AT_class (a) == dw_val_class_str)
29991 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
29992 s->refcount++;
29993 /* Avoid unnecessarily putting strings that are used less than
29994 twice in the hash table. */
29995 if (s->form != DW_FORM_line_strp
29996 && (s->refcount
29997 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
29999 indirect_string_node **slot
30000 = debug_str_hash->find_slot_with_hash (s->str,
30001 htab_hash_string (s->str),
30002 INSERT);
30003 gcc_assert (*slot == NULL);
30004 *slot = s;
30009 /* Mark DIE and its children as removed. */
30011 static void
30012 mark_removed (dw_die_ref die)
30014 dw_die_ref c;
30015 die->removed = true;
30016 FOR_EACH_CHILD (die, c, mark_removed (c));
30019 /* Remove from the tree DIE any dies that aren't marked. */
30021 static void
30022 prune_unused_types_prune (dw_die_ref die)
30024 dw_die_ref c;
30026 gcc_assert (die->die_mark);
30027 prune_unused_types_update_strings (die);
30029 if (! die->die_child)
30030 return;
30032 c = die->die_child;
30033 do {
30034 dw_die_ref prev = c, next;
30035 for (c = c->die_sib; ! c->die_mark; c = next)
30036 if (c == die->die_child)
30038 /* No marked children between 'prev' and the end of the list. */
30039 if (prev == c)
30040 /* No marked children at all. */
30041 die->die_child = NULL;
30042 else
30044 prev->die_sib = c->die_sib;
30045 die->die_child = prev;
30047 c->die_sib = NULL;
30048 mark_removed (c);
30049 return;
30051 else
30053 next = c->die_sib;
30054 c->die_sib = NULL;
30055 mark_removed (c);
30058 if (c != prev->die_sib)
30059 prev->die_sib = c;
30060 prune_unused_types_prune (c);
30061 } while (c != die->die_child);
30064 /* Remove dies representing declarations that we never use. */
30066 static void
30067 prune_unused_types (void)
30069 unsigned int i;
30070 limbo_die_node *node;
30071 comdat_type_node *ctnode;
30072 pubname_entry *pub;
30073 dw_die_ref base_type;
30075 #if ENABLE_ASSERT_CHECKING
30076 /* All the marks should already be clear. */
30077 verify_marks_clear (comp_unit_die ());
30078 for (node = limbo_die_list; node; node = node->next)
30079 verify_marks_clear (node->die);
30080 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30081 verify_marks_clear (ctnode->root_die);
30082 #endif /* ENABLE_ASSERT_CHECKING */
30084 /* Mark types that are used in global variables. */
30085 premark_types_used_by_global_vars ();
30087 /* Mark variables used in the symtab. */
30088 if (flag_debug_only_used_symbols)
30089 premark_used_variables ();
30091 /* Set the mark on nodes that are actually used. */
30092 prune_unused_types_walk (comp_unit_die ());
30093 for (node = limbo_die_list; node; node = node->next)
30094 prune_unused_types_walk (node->die);
30095 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30097 prune_unused_types_walk (ctnode->root_die);
30098 prune_unused_types_mark (ctnode->type_die, 1);
30101 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30102 are unusual in that they are pubnames that are the children of pubtypes.
30103 They should only be marked via their parent DW_TAG_enumeration_type die,
30104 not as roots in themselves. */
30105 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30106 if (pub->die->die_tag != DW_TAG_enumerator)
30107 prune_unused_types_mark (pub->die, 1);
30108 for (i = 0; base_types.iterate (i, &base_type); i++)
30109 prune_unused_types_mark (base_type, 1);
30111 /* Also set the mark on nodes that could be referenced by
30112 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30113 by DW_TAG_inlined_subroutine origins. */
30114 cgraph_node *cnode;
30115 FOR_EACH_FUNCTION (cnode)
30116 if (cnode->referred_to_p (false))
30118 dw_die_ref die = lookup_decl_die (cnode->decl);
30119 if (die == NULL || die->die_mark)
30120 continue;
30121 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30122 if (e->caller != cnode)
30124 prune_unused_types_mark (die, 1);
30125 break;
30129 if (debug_str_hash)
30130 debug_str_hash->empty ();
30131 if (skeleton_debug_str_hash)
30132 skeleton_debug_str_hash->empty ();
30133 prune_unused_types_prune (comp_unit_die ());
30134 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30136 node = *pnode;
30137 if (!node->die->die_mark)
30138 *pnode = node->next;
30139 else
30141 prune_unused_types_prune (node->die);
30142 pnode = &node->next;
30145 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30146 prune_unused_types_prune (ctnode->root_die);
30148 /* Leave the marks clear. */
30149 prune_unmark_dies (comp_unit_die ());
30150 for (node = limbo_die_list; node; node = node->next)
30151 prune_unmark_dies (node->die);
30152 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30153 prune_unmark_dies (ctnode->root_die);
30156 /* Helpers to manipulate hash table of comdat type units. */
30158 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30160 static inline hashval_t hash (const comdat_type_node *);
30161 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30164 inline hashval_t
30165 comdat_type_hasher::hash (const comdat_type_node *type_node)
30167 hashval_t h;
30168 memcpy (&h, type_node->signature, sizeof (h));
30169 return h;
30172 inline bool
30173 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30174 const comdat_type_node *type_node_2)
30176 return (! memcmp (type_node_1->signature, type_node_2->signature,
30177 DWARF_TYPE_SIGNATURE_SIZE));
30180 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30181 to the location it would have been added, should we know its
30182 DECL_ASSEMBLER_NAME when we added other attributes. This will
30183 probably improve compactness of debug info, removing equivalent
30184 abbrevs, and hide any differences caused by deferring the
30185 computation of the assembler name, triggered by e.g. PCH. */
30187 static inline void
30188 move_linkage_attr (dw_die_ref die)
30190 unsigned ix = vec_safe_length (die->die_attr);
30191 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30193 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30194 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30196 while (--ix > 0)
30198 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30200 if (prev->dw_attr == DW_AT_decl_line
30201 || prev->dw_attr == DW_AT_decl_column
30202 || prev->dw_attr == DW_AT_name)
30203 break;
30206 if (ix != vec_safe_length (die->die_attr) - 1)
30208 die->die_attr->pop ();
30209 die->die_attr->quick_insert (ix, linkage);
30213 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30214 referenced from typed stack ops and count how often they are used. */
30216 static void
30217 mark_base_types (dw_loc_descr_ref loc)
30219 dw_die_ref base_type = NULL;
30221 for (; loc; loc = loc->dw_loc_next)
30223 switch (loc->dw_loc_opc)
30225 case DW_OP_regval_type:
30226 case DW_OP_deref_type:
30227 case DW_OP_GNU_regval_type:
30228 case DW_OP_GNU_deref_type:
30229 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30230 break;
30231 case DW_OP_convert:
30232 case DW_OP_reinterpret:
30233 case DW_OP_GNU_convert:
30234 case DW_OP_GNU_reinterpret:
30235 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30236 continue;
30237 /* FALLTHRU */
30238 case DW_OP_const_type:
30239 case DW_OP_GNU_const_type:
30240 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30241 break;
30242 case DW_OP_entry_value:
30243 case DW_OP_GNU_entry_value:
30244 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30245 continue;
30246 default:
30247 continue;
30249 gcc_assert (base_type->die_parent == comp_unit_die ());
30250 if (base_type->die_mark)
30251 base_type->die_mark++;
30252 else
30254 base_types.safe_push (base_type);
30255 base_type->die_mark = 1;
30260 /* Comparison function for sorting marked base types. */
30262 static int
30263 base_type_cmp (const void *x, const void *y)
30265 dw_die_ref dx = *(const dw_die_ref *) x;
30266 dw_die_ref dy = *(const dw_die_ref *) y;
30267 unsigned int byte_size1, byte_size2;
30268 unsigned int encoding1, encoding2;
30269 unsigned int align1, align2;
30270 if (dx->die_mark > dy->die_mark)
30271 return -1;
30272 if (dx->die_mark < dy->die_mark)
30273 return 1;
30274 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30275 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30276 if (byte_size1 < byte_size2)
30277 return 1;
30278 if (byte_size1 > byte_size2)
30279 return -1;
30280 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30281 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30282 if (encoding1 < encoding2)
30283 return 1;
30284 if (encoding1 > encoding2)
30285 return -1;
30286 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30287 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30288 if (align1 < align2)
30289 return 1;
30290 if (align1 > align2)
30291 return -1;
30292 return 0;
30295 /* Move base types marked by mark_base_types as early as possible
30296 in the CU, sorted by decreasing usage count both to make the
30297 uleb128 references as small as possible and to make sure they
30298 will have die_offset already computed by calc_die_sizes when
30299 sizes of typed stack loc ops is computed. */
30301 static void
30302 move_marked_base_types (void)
30304 unsigned int i;
30305 dw_die_ref base_type, die, c;
30307 if (base_types.is_empty ())
30308 return;
30310 /* Sort by decreasing usage count, they will be added again in that
30311 order later on. */
30312 base_types.qsort (base_type_cmp);
30313 die = comp_unit_die ();
30314 c = die->die_child;
30317 dw_die_ref prev = c;
30318 c = c->die_sib;
30319 while (c->die_mark)
30321 remove_child_with_prev (c, prev);
30322 /* As base types got marked, there must be at least
30323 one node other than DW_TAG_base_type. */
30324 gcc_assert (die->die_child != NULL);
30325 c = prev->die_sib;
30328 while (c != die->die_child);
30329 gcc_assert (die->die_child);
30330 c = die->die_child;
30331 for (i = 0; base_types.iterate (i, &base_type); i++)
30333 base_type->die_mark = 0;
30334 base_type->die_sib = c->die_sib;
30335 c->die_sib = base_type;
30336 c = base_type;
30340 /* Helper function for resolve_addr, attempt to resolve
30341 one CONST_STRING, return true if successful. Similarly verify that
30342 SYMBOL_REFs refer to variables emitted in the current CU. */
30344 static bool
30345 resolve_one_addr (rtx *addr)
30347 rtx rtl = *addr;
30349 if (GET_CODE (rtl) == CONST_STRING)
30351 size_t len = strlen (XSTR (rtl, 0)) + 1;
30352 tree t = build_string (len, XSTR (rtl, 0));
30353 tree tlen = size_int (len - 1);
30354 TREE_TYPE (t)
30355 = build_array_type (char_type_node, build_index_type (tlen));
30356 rtl = lookup_constant_def (t);
30357 if (!rtl || !MEM_P (rtl))
30358 return false;
30359 rtl = XEXP (rtl, 0);
30360 if (GET_CODE (rtl) == SYMBOL_REF
30361 && SYMBOL_REF_DECL (rtl)
30362 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30363 return false;
30364 vec_safe_push (used_rtx_array, rtl);
30365 *addr = rtl;
30366 return true;
30369 if (GET_CODE (rtl) == SYMBOL_REF
30370 && SYMBOL_REF_DECL (rtl))
30372 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30374 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30375 return false;
30377 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30378 return false;
30381 if (GET_CODE (rtl) == CONST)
30383 subrtx_ptr_iterator::array_type array;
30384 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30385 if (!resolve_one_addr (*iter))
30386 return false;
30389 return true;
30392 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30393 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30394 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30396 static rtx
30397 string_cst_pool_decl (tree t)
30399 rtx rtl = output_constant_def (t, 1);
30400 unsigned char *array;
30401 dw_loc_descr_ref l;
30402 tree decl;
30403 size_t len;
30404 dw_die_ref ref;
30406 if (!rtl || !MEM_P (rtl))
30407 return NULL_RTX;
30408 rtl = XEXP (rtl, 0);
30409 if (GET_CODE (rtl) != SYMBOL_REF
30410 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30411 return NULL_RTX;
30413 decl = SYMBOL_REF_DECL (rtl);
30414 if (!lookup_decl_die (decl))
30416 len = TREE_STRING_LENGTH (t);
30417 vec_safe_push (used_rtx_array, rtl);
30418 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30419 array = ggc_vec_alloc<unsigned char> (len);
30420 memcpy (array, TREE_STRING_POINTER (t), len);
30421 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30422 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30423 l->dw_loc_oprnd2.v.val_vec.length = len;
30424 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30425 l->dw_loc_oprnd2.v.val_vec.array = array;
30426 add_AT_loc (ref, DW_AT_location, l);
30427 equate_decl_number_to_die (decl, ref);
30429 return rtl;
30432 /* Helper function of resolve_addr_in_expr. LOC is
30433 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30434 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30435 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30436 with DW_OP_implicit_pointer if possible
30437 and return true, if unsuccessful, return false. */
30439 static bool
30440 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30442 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30443 HOST_WIDE_INT offset = 0;
30444 dw_die_ref ref = NULL;
30445 tree decl;
30447 if (GET_CODE (rtl) == CONST
30448 && GET_CODE (XEXP (rtl, 0)) == PLUS
30449 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30451 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30452 rtl = XEXP (XEXP (rtl, 0), 0);
30454 if (GET_CODE (rtl) == CONST_STRING)
30456 size_t len = strlen (XSTR (rtl, 0)) + 1;
30457 tree t = build_string (len, XSTR (rtl, 0));
30458 tree tlen = size_int (len - 1);
30460 TREE_TYPE (t)
30461 = build_array_type (char_type_node, build_index_type (tlen));
30462 rtl = string_cst_pool_decl (t);
30463 if (!rtl)
30464 return false;
30466 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30468 decl = SYMBOL_REF_DECL (rtl);
30469 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30471 ref = lookup_decl_die (decl);
30472 if (ref && (get_AT (ref, DW_AT_location)
30473 || get_AT (ref, DW_AT_const_value)))
30475 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30476 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30477 loc->dw_loc_oprnd1.val_entry = NULL;
30478 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30479 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30480 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30481 loc->dw_loc_oprnd2.v.val_int = offset;
30482 return true;
30486 return false;
30489 /* Helper function for resolve_addr, handle one location
30490 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30491 the location list couldn't be resolved. */
30493 static bool
30494 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30496 dw_loc_descr_ref keep = NULL;
30497 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30498 switch (loc->dw_loc_opc)
30500 case DW_OP_addr:
30501 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30503 if ((prev == NULL
30504 || prev->dw_loc_opc == DW_OP_piece
30505 || prev->dw_loc_opc == DW_OP_bit_piece)
30506 && loc->dw_loc_next
30507 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30508 && (!dwarf_strict || dwarf_version >= 5)
30509 && optimize_one_addr_into_implicit_ptr (loc))
30510 break;
30511 return false;
30513 break;
30514 case DW_OP_GNU_addr_index:
30515 case DW_OP_addrx:
30516 case DW_OP_GNU_const_index:
30517 case DW_OP_constx:
30518 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30519 || loc->dw_loc_opc == DW_OP_addrx)
30520 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30521 || loc->dw_loc_opc == DW_OP_constx)
30522 && loc->dtprel))
30524 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30525 if (!resolve_one_addr (&rtl))
30526 return false;
30527 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30528 loc->dw_loc_oprnd1.val_entry
30529 = add_addr_table_entry (rtl, ate_kind_rtx);
30531 break;
30532 case DW_OP_const4u:
30533 case DW_OP_const8u:
30534 if (loc->dtprel
30535 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30536 return false;
30537 break;
30538 case DW_OP_plus_uconst:
30539 if (size_of_loc_descr (loc)
30540 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30542 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30544 dw_loc_descr_ref repl
30545 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30546 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30547 add_loc_descr (&repl, loc->dw_loc_next);
30548 *loc = *repl;
30550 break;
30551 case DW_OP_implicit_value:
30552 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30553 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30554 return false;
30555 break;
30556 case DW_OP_implicit_pointer:
30557 case DW_OP_GNU_implicit_pointer:
30558 case DW_OP_GNU_parameter_ref:
30559 case DW_OP_GNU_variable_value:
30560 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30562 dw_die_ref ref
30563 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30564 if (ref == NULL)
30565 return false;
30566 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30567 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30568 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30570 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30572 if (prev == NULL
30573 && loc->dw_loc_next == NULL
30574 && AT_class (a) == dw_val_class_loc)
30575 switch (a->dw_attr)
30577 /* Following attributes allow both exprloc and reference,
30578 so if the whole expression is DW_OP_GNU_variable_value
30579 alone we could transform it into reference. */
30580 case DW_AT_byte_size:
30581 case DW_AT_bit_size:
30582 case DW_AT_lower_bound:
30583 case DW_AT_upper_bound:
30584 case DW_AT_bit_stride:
30585 case DW_AT_count:
30586 case DW_AT_allocated:
30587 case DW_AT_associated:
30588 case DW_AT_byte_stride:
30589 a->dw_attr_val.val_class = dw_val_class_die_ref;
30590 a->dw_attr_val.val_entry = NULL;
30591 a->dw_attr_val.v.val_die_ref.die
30592 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30593 a->dw_attr_val.v.val_die_ref.external = 0;
30594 return true;
30595 default:
30596 break;
30598 if (dwarf_strict)
30599 return false;
30601 break;
30602 case DW_OP_const_type:
30603 case DW_OP_regval_type:
30604 case DW_OP_deref_type:
30605 case DW_OP_convert:
30606 case DW_OP_reinterpret:
30607 case DW_OP_GNU_const_type:
30608 case DW_OP_GNU_regval_type:
30609 case DW_OP_GNU_deref_type:
30610 case DW_OP_GNU_convert:
30611 case DW_OP_GNU_reinterpret:
30612 while (loc->dw_loc_next
30613 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30614 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30616 dw_die_ref base1, base2;
30617 unsigned enc1, enc2, size1, size2;
30618 if (loc->dw_loc_opc == DW_OP_regval_type
30619 || loc->dw_loc_opc == DW_OP_deref_type
30620 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30621 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30622 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30623 else if (loc->dw_loc_oprnd1.val_class
30624 == dw_val_class_unsigned_const)
30625 break;
30626 else
30627 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30628 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30629 == dw_val_class_unsigned_const)
30630 break;
30631 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30632 gcc_assert (base1->die_tag == DW_TAG_base_type
30633 && base2->die_tag == DW_TAG_base_type);
30634 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30635 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30636 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30637 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30638 if (size1 == size2
30639 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30640 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30641 && loc != keep)
30642 || enc1 == enc2))
30644 /* Optimize away next DW_OP_convert after
30645 adjusting LOC's base type die reference. */
30646 if (loc->dw_loc_opc == DW_OP_regval_type
30647 || loc->dw_loc_opc == DW_OP_deref_type
30648 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30649 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30650 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30651 else
30652 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30653 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30654 continue;
30656 /* Don't change integer DW_OP_convert after e.g. floating
30657 point typed stack entry. */
30658 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30659 keep = loc->dw_loc_next;
30660 break;
30662 break;
30663 default:
30664 break;
30666 return true;
30669 /* Helper function of resolve_addr. DIE had DW_AT_location of
30670 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30671 and DW_OP_addr couldn't be resolved. resolve_addr has already
30672 removed the DW_AT_location attribute. This function attempts to
30673 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30674 to it or DW_AT_const_value attribute, if possible. */
30676 static void
30677 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30679 if (!VAR_P (decl)
30680 || lookup_decl_die (decl) != die
30681 || DECL_EXTERNAL (decl)
30682 || !TREE_STATIC (decl)
30683 || DECL_INITIAL (decl) == NULL_TREE
30684 || DECL_P (DECL_INITIAL (decl))
30685 || get_AT (die, DW_AT_const_value))
30686 return;
30688 tree init = DECL_INITIAL (decl);
30689 HOST_WIDE_INT offset = 0;
30690 /* For variables that have been optimized away and thus
30691 don't have a memory location, see if we can emit
30692 DW_AT_const_value instead. */
30693 if (tree_add_const_value_attribute (die, init))
30694 return;
30695 if (dwarf_strict && dwarf_version < 5)
30696 return;
30697 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30698 and ADDR_EXPR refers to a decl that has DW_AT_location or
30699 DW_AT_const_value (but isn't addressable, otherwise
30700 resolving the original DW_OP_addr wouldn't fail), see if
30701 we can add DW_OP_implicit_pointer. */
30702 STRIP_NOPS (init);
30703 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30704 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30706 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30707 init = TREE_OPERAND (init, 0);
30708 STRIP_NOPS (init);
30710 if (TREE_CODE (init) != ADDR_EXPR)
30711 return;
30712 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30713 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30714 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30715 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30716 && TREE_OPERAND (init, 0) != decl))
30718 dw_die_ref ref;
30719 dw_loc_descr_ref l;
30721 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30723 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30724 if (!rtl)
30725 return;
30726 decl = SYMBOL_REF_DECL (rtl);
30728 else
30729 decl = TREE_OPERAND (init, 0);
30730 ref = lookup_decl_die (decl);
30731 if (ref == NULL
30732 || (!get_AT (ref, DW_AT_location)
30733 && !get_AT (ref, DW_AT_const_value)))
30734 return;
30735 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30736 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30737 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30738 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30739 add_AT_loc (die, DW_AT_location, l);
30743 /* Return NULL if l is a DWARF expression, or first op that is not
30744 valid DWARF expression. */
30746 static dw_loc_descr_ref
30747 non_dwarf_expression (dw_loc_descr_ref l)
30749 while (l)
30751 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30752 return l;
30753 switch (l->dw_loc_opc)
30755 case DW_OP_regx:
30756 case DW_OP_implicit_value:
30757 case DW_OP_stack_value:
30758 case DW_OP_implicit_pointer:
30759 case DW_OP_GNU_implicit_pointer:
30760 case DW_OP_GNU_parameter_ref:
30761 case DW_OP_piece:
30762 case DW_OP_bit_piece:
30763 return l;
30764 default:
30765 break;
30767 l = l->dw_loc_next;
30769 return NULL;
30772 /* Return adjusted copy of EXPR:
30773 If it is empty DWARF expression, return it.
30774 If it is valid non-empty DWARF expression,
30775 return copy of EXPR with DW_OP_deref appended to it.
30776 If it is DWARF expression followed by DW_OP_reg{N,x}, return
30777 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30778 If it is DWARF expression followed by DW_OP_stack_value, return
30779 copy of the DWARF expression without anything appended.
30780 Otherwise, return NULL. */
30782 static dw_loc_descr_ref
30783 copy_deref_exprloc (dw_loc_descr_ref expr)
30785 dw_loc_descr_ref tail = NULL;
30787 if (expr == NULL)
30788 return NULL;
30790 dw_loc_descr_ref l = non_dwarf_expression (expr);
30791 if (l && l->dw_loc_next)
30792 return NULL;
30794 if (l)
30796 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30797 tail = new_loc_descr ((enum dwarf_location_atom)
30798 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30799 0, 0);
30800 else
30801 switch (l->dw_loc_opc)
30803 case DW_OP_regx:
30804 tail = new_loc_descr (DW_OP_bregx,
30805 l->dw_loc_oprnd1.v.val_unsigned, 0);
30806 break;
30807 case DW_OP_stack_value:
30808 break;
30809 default:
30810 return NULL;
30813 else
30814 tail = new_loc_descr (DW_OP_deref, 0, 0);
30816 dw_loc_descr_ref ret = NULL, *p = &ret;
30817 while (expr != l)
30819 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30820 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30821 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30822 p = &(*p)->dw_loc_next;
30823 expr = expr->dw_loc_next;
30825 *p = tail;
30826 return ret;
30829 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30830 reference to a variable or argument, adjust it if needed and return:
30831 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30832 attribute if present should be removed
30833 0 keep the attribute perhaps with minor modifications, no need to rescan
30834 1 if the attribute has been successfully adjusted. */
30836 static int
30837 optimize_string_length (dw_attr_node *a)
30839 dw_loc_descr_ref l = AT_loc (a), lv;
30840 dw_die_ref die;
30841 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30843 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30844 die = lookup_decl_die (decl);
30845 if (die)
30847 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30848 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30849 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30851 else
30852 return -1;
30854 else
30855 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30857 /* DWARF5 allows reference class, so we can then reference the DIE.
30858 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30859 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30861 a->dw_attr_val.val_class = dw_val_class_die_ref;
30862 a->dw_attr_val.val_entry = NULL;
30863 a->dw_attr_val.v.val_die_ref.die = die;
30864 a->dw_attr_val.v.val_die_ref.external = 0;
30865 return 0;
30868 dw_attr_node *av = get_AT (die, DW_AT_location);
30869 dw_loc_list_ref d;
30870 bool non_dwarf_expr = false;
30872 if (av == NULL)
30873 return dwarf_strict ? -1 : 0;
30874 switch (AT_class (av))
30876 case dw_val_class_loc_list:
30877 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30878 if (d->expr && non_dwarf_expression (d->expr))
30879 non_dwarf_expr = true;
30880 break;
30881 case dw_val_class_view_list:
30882 gcc_unreachable ();
30883 case dw_val_class_loc:
30884 lv = AT_loc (av);
30885 if (lv == NULL)
30886 return dwarf_strict ? -1 : 0;
30887 if (non_dwarf_expression (lv))
30888 non_dwarf_expr = true;
30889 break;
30890 default:
30891 return dwarf_strict ? -1 : 0;
30894 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30895 into DW_OP_call4 or DW_OP_GNU_variable_value into
30896 DW_OP_call4 DW_OP_deref, do so. */
30897 if (!non_dwarf_expr
30898 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30900 l->dw_loc_opc = DW_OP_call4;
30901 if (l->dw_loc_next)
30902 l->dw_loc_next = NULL;
30903 else
30904 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30905 return 0;
30908 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30909 copy over the DW_AT_location attribute from die to a. */
30910 if (l->dw_loc_next != NULL)
30912 a->dw_attr_val = av->dw_attr_val;
30913 return 1;
30916 dw_loc_list_ref list, *p;
30917 switch (AT_class (av))
30919 case dw_val_class_loc_list:
30920 p = &list;
30921 list = NULL;
30922 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30924 lv = copy_deref_exprloc (d->expr);
30925 if (lv)
30927 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
30928 p = &(*p)->dw_loc_next;
30930 else if (!dwarf_strict && d->expr)
30931 return 0;
30933 if (list == NULL)
30934 return dwarf_strict ? -1 : 0;
30935 a->dw_attr_val.val_class = dw_val_class_loc_list;
30936 gen_llsym (list);
30937 *AT_loc_list_ptr (a) = list;
30938 return 1;
30939 case dw_val_class_loc:
30940 lv = copy_deref_exprloc (AT_loc (av));
30941 if (lv == NULL)
30942 return dwarf_strict ? -1 : 0;
30943 a->dw_attr_val.v.val_loc = lv;
30944 return 1;
30945 default:
30946 gcc_unreachable ();
30950 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
30951 an address in .rodata section if the string literal is emitted there,
30952 or remove the containing location list or replace DW_AT_const_value
30953 with DW_AT_location and empty location expression, if it isn't found
30954 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
30955 to something that has been emitted in the current CU. */
30957 static void
30958 resolve_addr (dw_die_ref die)
30960 dw_die_ref c;
30961 dw_attr_node *a;
30962 dw_loc_list_ref *curr, *start, loc;
30963 unsigned ix;
30964 bool remove_AT_byte_size = false;
30966 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30967 switch (AT_class (a))
30969 case dw_val_class_loc_list:
30970 start = curr = AT_loc_list_ptr (a);
30971 loc = *curr;
30972 gcc_assert (loc);
30973 /* The same list can be referenced more than once. See if we have
30974 already recorded the result from a previous pass. */
30975 if (loc->replaced)
30976 *curr = loc->dw_loc_next;
30977 else if (!loc->resolved_addr)
30979 /* As things stand, we do not expect or allow one die to
30980 reference a suffix of another die's location list chain.
30981 References must be identical or completely separate.
30982 There is therefore no need to cache the result of this
30983 pass on any list other than the first; doing so
30984 would lead to unnecessary writes. */
30985 while (*curr)
30987 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
30988 if (!resolve_addr_in_expr (a, (*curr)->expr))
30990 dw_loc_list_ref next = (*curr)->dw_loc_next;
30991 dw_loc_descr_ref l = (*curr)->expr;
30993 if (next && (*curr)->ll_symbol)
30995 gcc_assert (!next->ll_symbol);
30996 next->ll_symbol = (*curr)->ll_symbol;
30997 next->vl_symbol = (*curr)->vl_symbol;
30999 if (dwarf_split_debug_info)
31000 remove_loc_list_addr_table_entries (l);
31001 *curr = next;
31003 else
31005 mark_base_types ((*curr)->expr);
31006 curr = &(*curr)->dw_loc_next;
31009 if (loc == *start)
31010 loc->resolved_addr = 1;
31011 else
31013 loc->replaced = 1;
31014 loc->dw_loc_next = *start;
31017 if (!*start)
31019 remove_AT (die, a->dw_attr);
31020 ix--;
31022 break;
31023 case dw_val_class_view_list:
31025 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31026 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31027 dw_val_node *llnode
31028 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31029 /* If we no longer have a loclist, or it no longer needs
31030 views, drop this attribute. */
31031 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31033 remove_AT (die, a->dw_attr);
31034 ix--;
31036 break;
31038 case dw_val_class_loc:
31040 dw_loc_descr_ref l = AT_loc (a);
31041 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31042 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31043 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31044 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31045 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31046 with DW_FORM_ref referencing the same DIE as
31047 DW_OP_GNU_variable_value used to reference. */
31048 if (a->dw_attr == DW_AT_string_length
31049 && l
31050 && l->dw_loc_opc == DW_OP_GNU_variable_value
31051 && (l->dw_loc_next == NULL
31052 || (l->dw_loc_next->dw_loc_next == NULL
31053 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31055 switch (optimize_string_length (a))
31057 case -1:
31058 remove_AT (die, a->dw_attr);
31059 ix--;
31060 /* If we drop DW_AT_string_length, we need to drop also
31061 DW_AT_{string_length_,}byte_size. */
31062 remove_AT_byte_size = true;
31063 continue;
31064 default:
31065 break;
31066 case 1:
31067 /* Even if we keep the optimized DW_AT_string_length,
31068 it might have changed AT_class, so process it again. */
31069 ix--;
31070 continue;
31073 /* For -gdwarf-2 don't attempt to optimize
31074 DW_AT_data_member_location containing
31075 DW_OP_plus_uconst - older consumers might
31076 rely on it being that op instead of a more complex,
31077 but shorter, location description. */
31078 if ((dwarf_version > 2
31079 || a->dw_attr != DW_AT_data_member_location
31080 || l == NULL
31081 || l->dw_loc_opc != DW_OP_plus_uconst
31082 || l->dw_loc_next != NULL)
31083 && !resolve_addr_in_expr (a, l))
31085 if (dwarf_split_debug_info)
31086 remove_loc_list_addr_table_entries (l);
31087 if (l != NULL
31088 && l->dw_loc_next == NULL
31089 && l->dw_loc_opc == DW_OP_addr
31090 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31091 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31092 && a->dw_attr == DW_AT_location)
31094 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31095 remove_AT (die, a->dw_attr);
31096 ix--;
31097 optimize_location_into_implicit_ptr (die, decl);
31098 break;
31100 if (a->dw_attr == DW_AT_string_length)
31101 /* If we drop DW_AT_string_length, we need to drop also
31102 DW_AT_{string_length_,}byte_size. */
31103 remove_AT_byte_size = true;
31104 remove_AT (die, a->dw_attr);
31105 ix--;
31107 else
31108 mark_base_types (l);
31110 break;
31111 case dw_val_class_addr:
31112 if (a->dw_attr == DW_AT_const_value
31113 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31115 if (AT_index (a) != NOT_INDEXED)
31116 remove_addr_table_entry (a->dw_attr_val.val_entry);
31117 remove_AT (die, a->dw_attr);
31118 ix--;
31120 if ((die->die_tag == DW_TAG_call_site
31121 && a->dw_attr == DW_AT_call_origin)
31122 || (die->die_tag == DW_TAG_GNU_call_site
31123 && a->dw_attr == DW_AT_abstract_origin))
31125 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31126 dw_die_ref tdie = lookup_decl_die (tdecl);
31127 dw_die_ref cdie;
31128 if (tdie == NULL
31129 && DECL_EXTERNAL (tdecl)
31130 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31131 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31133 dw_die_ref pdie = cdie;
31134 /* Make sure we don't add these DIEs into type units.
31135 We could emit skeleton DIEs for context (namespaces,
31136 outer structs/classes) and a skeleton DIE for the
31137 innermost context with DW_AT_signature pointing to the
31138 type unit. See PR78835. */
31139 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31140 pdie = pdie->die_parent;
31141 if (pdie == NULL)
31143 /* Creating a full DIE for tdecl is overly expensive and
31144 at this point even wrong when in the LTO phase
31145 as it can end up generating new type DIEs we didn't
31146 output and thus optimize_external_refs will crash. */
31147 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31148 add_AT_flag (tdie, DW_AT_external, 1);
31149 add_AT_flag (tdie, DW_AT_declaration, 1);
31150 add_linkage_attr (tdie, tdecl);
31151 add_name_and_src_coords_attributes (tdie, tdecl, true);
31152 equate_decl_number_to_die (tdecl, tdie);
31155 if (tdie)
31157 a->dw_attr_val.val_class = dw_val_class_die_ref;
31158 a->dw_attr_val.v.val_die_ref.die = tdie;
31159 a->dw_attr_val.v.val_die_ref.external = 0;
31161 else
31163 if (AT_index (a) != NOT_INDEXED)
31164 remove_addr_table_entry (a->dw_attr_val.val_entry);
31165 remove_AT (die, a->dw_attr);
31166 ix--;
31169 break;
31170 default:
31171 break;
31174 if (remove_AT_byte_size)
31175 remove_AT (die, dwarf_version >= 5
31176 ? DW_AT_string_length_byte_size
31177 : DW_AT_byte_size);
31179 FOR_EACH_CHILD (die, c, resolve_addr (c));
31182 /* Helper routines for optimize_location_lists.
31183 This pass tries to share identical local lists in .debug_loc
31184 section. */
31186 /* Iteratively hash operands of LOC opcode into HSTATE. */
31188 static void
31189 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31191 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31192 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31194 switch (loc->dw_loc_opc)
31196 case DW_OP_const4u:
31197 case DW_OP_const8u:
31198 if (loc->dtprel)
31199 goto hash_addr;
31200 /* FALLTHRU */
31201 case DW_OP_const1u:
31202 case DW_OP_const1s:
31203 case DW_OP_const2u:
31204 case DW_OP_const2s:
31205 case DW_OP_const4s:
31206 case DW_OP_const8s:
31207 case DW_OP_constu:
31208 case DW_OP_consts:
31209 case DW_OP_pick:
31210 case DW_OP_plus_uconst:
31211 case DW_OP_breg0:
31212 case DW_OP_breg1:
31213 case DW_OP_breg2:
31214 case DW_OP_breg3:
31215 case DW_OP_breg4:
31216 case DW_OP_breg5:
31217 case DW_OP_breg6:
31218 case DW_OP_breg7:
31219 case DW_OP_breg8:
31220 case DW_OP_breg9:
31221 case DW_OP_breg10:
31222 case DW_OP_breg11:
31223 case DW_OP_breg12:
31224 case DW_OP_breg13:
31225 case DW_OP_breg14:
31226 case DW_OP_breg15:
31227 case DW_OP_breg16:
31228 case DW_OP_breg17:
31229 case DW_OP_breg18:
31230 case DW_OP_breg19:
31231 case DW_OP_breg20:
31232 case DW_OP_breg21:
31233 case DW_OP_breg22:
31234 case DW_OP_breg23:
31235 case DW_OP_breg24:
31236 case DW_OP_breg25:
31237 case DW_OP_breg26:
31238 case DW_OP_breg27:
31239 case DW_OP_breg28:
31240 case DW_OP_breg29:
31241 case DW_OP_breg30:
31242 case DW_OP_breg31:
31243 case DW_OP_regx:
31244 case DW_OP_fbreg:
31245 case DW_OP_piece:
31246 case DW_OP_deref_size:
31247 case DW_OP_xderef_size:
31248 hstate.add_object (val1->v.val_int);
31249 break;
31250 case DW_OP_skip:
31251 case DW_OP_bra:
31253 int offset;
31255 gcc_assert (val1->val_class == dw_val_class_loc);
31256 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31257 hstate.add_object (offset);
31259 break;
31260 case DW_OP_implicit_value:
31261 hstate.add_object (val1->v.val_unsigned);
31262 switch (val2->val_class)
31264 case dw_val_class_const:
31265 hstate.add_object (val2->v.val_int);
31266 break;
31267 case dw_val_class_vec:
31269 unsigned int elt_size = val2->v.val_vec.elt_size;
31270 unsigned int len = val2->v.val_vec.length;
31272 hstate.add_int (elt_size);
31273 hstate.add_int (len);
31274 hstate.add (val2->v.val_vec.array, len * elt_size);
31276 break;
31277 case dw_val_class_const_double:
31278 hstate.add_object (val2->v.val_double.low);
31279 hstate.add_object (val2->v.val_double.high);
31280 break;
31281 case dw_val_class_wide_int:
31282 hstate.add (val2->v.val_wide->get_val (),
31283 get_full_len (*val2->v.val_wide)
31284 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31285 break;
31286 case dw_val_class_addr:
31287 inchash::add_rtx (val2->v.val_addr, hstate);
31288 break;
31289 default:
31290 gcc_unreachable ();
31292 break;
31293 case DW_OP_bregx:
31294 case DW_OP_bit_piece:
31295 hstate.add_object (val1->v.val_int);
31296 hstate.add_object (val2->v.val_int);
31297 break;
31298 case DW_OP_addr:
31299 hash_addr:
31300 if (loc->dtprel)
31302 unsigned char dtprel = 0xd1;
31303 hstate.add_object (dtprel);
31305 inchash::add_rtx (val1->v.val_addr, hstate);
31306 break;
31307 case DW_OP_GNU_addr_index:
31308 case DW_OP_addrx:
31309 case DW_OP_GNU_const_index:
31310 case DW_OP_constx:
31312 if (loc->dtprel)
31314 unsigned char dtprel = 0xd1;
31315 hstate.add_object (dtprel);
31317 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31319 break;
31320 case DW_OP_implicit_pointer:
31321 case DW_OP_GNU_implicit_pointer:
31322 hstate.add_int (val2->v.val_int);
31323 break;
31324 case DW_OP_entry_value:
31325 case DW_OP_GNU_entry_value:
31326 hstate.add_object (val1->v.val_loc);
31327 break;
31328 case DW_OP_regval_type:
31329 case DW_OP_deref_type:
31330 case DW_OP_GNU_regval_type:
31331 case DW_OP_GNU_deref_type:
31333 unsigned int byte_size
31334 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31335 unsigned int encoding
31336 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31337 hstate.add_object (val1->v.val_int);
31338 hstate.add_object (byte_size);
31339 hstate.add_object (encoding);
31341 break;
31342 case DW_OP_convert:
31343 case DW_OP_reinterpret:
31344 case DW_OP_GNU_convert:
31345 case DW_OP_GNU_reinterpret:
31346 if (val1->val_class == dw_val_class_unsigned_const)
31348 hstate.add_object (val1->v.val_unsigned);
31349 break;
31351 /* FALLTHRU */
31352 case DW_OP_const_type:
31353 case DW_OP_GNU_const_type:
31355 unsigned int byte_size
31356 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31357 unsigned int encoding
31358 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31359 hstate.add_object (byte_size);
31360 hstate.add_object (encoding);
31361 if (loc->dw_loc_opc != DW_OP_const_type
31362 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31363 break;
31364 hstate.add_object (val2->val_class);
31365 switch (val2->val_class)
31367 case dw_val_class_const:
31368 hstate.add_object (val2->v.val_int);
31369 break;
31370 case dw_val_class_vec:
31372 unsigned int elt_size = val2->v.val_vec.elt_size;
31373 unsigned int len = val2->v.val_vec.length;
31375 hstate.add_object (elt_size);
31376 hstate.add_object (len);
31377 hstate.add (val2->v.val_vec.array, len * elt_size);
31379 break;
31380 case dw_val_class_const_double:
31381 hstate.add_object (val2->v.val_double.low);
31382 hstate.add_object (val2->v.val_double.high);
31383 break;
31384 case dw_val_class_wide_int:
31385 hstate.add (val2->v.val_wide->get_val (),
31386 get_full_len (*val2->v.val_wide)
31387 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31388 break;
31389 default:
31390 gcc_unreachable ();
31393 break;
31395 default:
31396 /* Other codes have no operands. */
31397 break;
31401 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31403 static inline void
31404 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31406 dw_loc_descr_ref l;
31407 bool sizes_computed = false;
31408 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31409 size_of_locs (loc);
31411 for (l = loc; l != NULL; l = l->dw_loc_next)
31413 enum dwarf_location_atom opc = l->dw_loc_opc;
31414 hstate.add_object (opc);
31415 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31417 size_of_locs (loc);
31418 sizes_computed = true;
31420 hash_loc_operands (l, hstate);
31424 /* Compute hash of the whole location list LIST_HEAD. */
31426 static inline void
31427 hash_loc_list (dw_loc_list_ref list_head)
31429 dw_loc_list_ref curr = list_head;
31430 inchash::hash hstate;
31432 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31434 hstate.add (curr->begin, strlen (curr->begin) + 1);
31435 hstate.add (curr->end, strlen (curr->end) + 1);
31436 hstate.add_object (curr->vbegin);
31437 hstate.add_object (curr->vend);
31438 if (curr->section)
31439 hstate.add (curr->section, strlen (curr->section) + 1);
31440 hash_locs (curr->expr, hstate);
31442 list_head->hash = hstate.end ();
31445 /* Return true if X and Y opcodes have the same operands. */
31447 static inline bool
31448 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31450 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31451 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31452 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31453 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31455 switch (x->dw_loc_opc)
31457 case DW_OP_const4u:
31458 case DW_OP_const8u:
31459 if (x->dtprel)
31460 goto hash_addr;
31461 /* FALLTHRU */
31462 case DW_OP_const1u:
31463 case DW_OP_const1s:
31464 case DW_OP_const2u:
31465 case DW_OP_const2s:
31466 case DW_OP_const4s:
31467 case DW_OP_const8s:
31468 case DW_OP_constu:
31469 case DW_OP_consts:
31470 case DW_OP_pick:
31471 case DW_OP_plus_uconst:
31472 case DW_OP_breg0:
31473 case DW_OP_breg1:
31474 case DW_OP_breg2:
31475 case DW_OP_breg3:
31476 case DW_OP_breg4:
31477 case DW_OP_breg5:
31478 case DW_OP_breg6:
31479 case DW_OP_breg7:
31480 case DW_OP_breg8:
31481 case DW_OP_breg9:
31482 case DW_OP_breg10:
31483 case DW_OP_breg11:
31484 case DW_OP_breg12:
31485 case DW_OP_breg13:
31486 case DW_OP_breg14:
31487 case DW_OP_breg15:
31488 case DW_OP_breg16:
31489 case DW_OP_breg17:
31490 case DW_OP_breg18:
31491 case DW_OP_breg19:
31492 case DW_OP_breg20:
31493 case DW_OP_breg21:
31494 case DW_OP_breg22:
31495 case DW_OP_breg23:
31496 case DW_OP_breg24:
31497 case DW_OP_breg25:
31498 case DW_OP_breg26:
31499 case DW_OP_breg27:
31500 case DW_OP_breg28:
31501 case DW_OP_breg29:
31502 case DW_OP_breg30:
31503 case DW_OP_breg31:
31504 case DW_OP_regx:
31505 case DW_OP_fbreg:
31506 case DW_OP_piece:
31507 case DW_OP_deref_size:
31508 case DW_OP_xderef_size:
31509 return valx1->v.val_int == valy1->v.val_int;
31510 case DW_OP_skip:
31511 case DW_OP_bra:
31512 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31513 can cause irrelevant differences in dw_loc_addr. */
31514 gcc_assert (valx1->val_class == dw_val_class_loc
31515 && valy1->val_class == dw_val_class_loc
31516 && (dwarf_split_debug_info
31517 || x->dw_loc_addr == y->dw_loc_addr));
31518 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31519 case DW_OP_implicit_value:
31520 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31521 || valx2->val_class != valy2->val_class)
31522 return false;
31523 switch (valx2->val_class)
31525 case dw_val_class_const:
31526 return valx2->v.val_int == valy2->v.val_int;
31527 case dw_val_class_vec:
31528 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31529 && valx2->v.val_vec.length == valy2->v.val_vec.length
31530 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31531 valx2->v.val_vec.elt_size
31532 * valx2->v.val_vec.length) == 0;
31533 case dw_val_class_const_double:
31534 return valx2->v.val_double.low == valy2->v.val_double.low
31535 && valx2->v.val_double.high == valy2->v.val_double.high;
31536 case dw_val_class_wide_int:
31537 return *valx2->v.val_wide == *valy2->v.val_wide;
31538 case dw_val_class_addr:
31539 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31540 default:
31541 gcc_unreachable ();
31543 case DW_OP_bregx:
31544 case DW_OP_bit_piece:
31545 return valx1->v.val_int == valy1->v.val_int
31546 && valx2->v.val_int == valy2->v.val_int;
31547 case DW_OP_addr:
31548 hash_addr:
31549 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31550 case DW_OP_GNU_addr_index:
31551 case DW_OP_addrx:
31552 case DW_OP_GNU_const_index:
31553 case DW_OP_constx:
31555 rtx ax1 = valx1->val_entry->addr.rtl;
31556 rtx ay1 = valy1->val_entry->addr.rtl;
31557 return rtx_equal_p (ax1, ay1);
31559 case DW_OP_implicit_pointer:
31560 case DW_OP_GNU_implicit_pointer:
31561 return valx1->val_class == dw_val_class_die_ref
31562 && valx1->val_class == valy1->val_class
31563 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31564 && valx2->v.val_int == valy2->v.val_int;
31565 case DW_OP_entry_value:
31566 case DW_OP_GNU_entry_value:
31567 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31568 case DW_OP_const_type:
31569 case DW_OP_GNU_const_type:
31570 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31571 || valx2->val_class != valy2->val_class)
31572 return false;
31573 switch (valx2->val_class)
31575 case dw_val_class_const:
31576 return valx2->v.val_int == valy2->v.val_int;
31577 case dw_val_class_vec:
31578 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31579 && valx2->v.val_vec.length == valy2->v.val_vec.length
31580 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31581 valx2->v.val_vec.elt_size
31582 * valx2->v.val_vec.length) == 0;
31583 case dw_val_class_const_double:
31584 return valx2->v.val_double.low == valy2->v.val_double.low
31585 && valx2->v.val_double.high == valy2->v.val_double.high;
31586 case dw_val_class_wide_int:
31587 return *valx2->v.val_wide == *valy2->v.val_wide;
31588 default:
31589 gcc_unreachable ();
31591 case DW_OP_regval_type:
31592 case DW_OP_deref_type:
31593 case DW_OP_GNU_regval_type:
31594 case DW_OP_GNU_deref_type:
31595 return valx1->v.val_int == valy1->v.val_int
31596 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31597 case DW_OP_convert:
31598 case DW_OP_reinterpret:
31599 case DW_OP_GNU_convert:
31600 case DW_OP_GNU_reinterpret:
31601 if (valx1->val_class != valy1->val_class)
31602 return false;
31603 if (valx1->val_class == dw_val_class_unsigned_const)
31604 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31605 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31606 case DW_OP_GNU_parameter_ref:
31607 return valx1->val_class == dw_val_class_die_ref
31608 && valx1->val_class == valy1->val_class
31609 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31610 default:
31611 /* Other codes have no operands. */
31612 return true;
31616 /* Return true if DWARF location expressions X and Y are the same. */
31618 static inline bool
31619 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31621 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31622 if (x->dw_loc_opc != y->dw_loc_opc
31623 || x->dtprel != y->dtprel
31624 || !compare_loc_operands (x, y))
31625 break;
31626 return x == NULL && y == NULL;
31629 /* Hashtable helpers. */
31631 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31633 static inline hashval_t hash (const dw_loc_list_struct *);
31634 static inline bool equal (const dw_loc_list_struct *,
31635 const dw_loc_list_struct *);
31638 /* Return precomputed hash of location list X. */
31640 inline hashval_t
31641 loc_list_hasher::hash (const dw_loc_list_struct *x)
31643 return x->hash;
31646 /* Return true if location lists A and B are the same. */
31648 inline bool
31649 loc_list_hasher::equal (const dw_loc_list_struct *a,
31650 const dw_loc_list_struct *b)
31652 if (a == b)
31653 return 1;
31654 if (a->hash != b->hash)
31655 return 0;
31656 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31657 if (strcmp (a->begin, b->begin) != 0
31658 || strcmp (a->end, b->end) != 0
31659 || (a->section == NULL) != (b->section == NULL)
31660 || (a->section && strcmp (a->section, b->section) != 0)
31661 || a->vbegin != b->vbegin || a->vend != b->vend
31662 || !compare_locs (a->expr, b->expr))
31663 break;
31664 return a == NULL && b == NULL;
31667 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31670 /* Recursively optimize location lists referenced from DIE
31671 children and share them whenever possible. */
31673 static void
31674 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31676 dw_die_ref c;
31677 dw_attr_node *a;
31678 unsigned ix;
31679 dw_loc_list_struct **slot;
31680 bool drop_locviews = false;
31681 bool has_locviews = false;
31683 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31684 if (AT_class (a) == dw_val_class_loc_list)
31686 dw_loc_list_ref list = AT_loc_list (a);
31687 /* TODO: perform some optimizations here, before hashing
31688 it and storing into the hash table. */
31689 hash_loc_list (list);
31690 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31691 if (*slot == NULL)
31693 *slot = list;
31694 if (loc_list_has_views (list))
31695 gcc_assert (list->vl_symbol);
31696 else if (list->vl_symbol)
31698 drop_locviews = true;
31699 list->vl_symbol = NULL;
31702 else
31704 if (list->vl_symbol && !(*slot)->vl_symbol)
31705 drop_locviews = true;
31706 a->dw_attr_val.v.val_loc_list = *slot;
31709 else if (AT_class (a) == dw_val_class_view_list)
31711 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31712 has_locviews = true;
31716 if (drop_locviews && has_locviews)
31717 remove_AT (die, DW_AT_GNU_locviews);
31719 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31723 /* Recursively assign each location list a unique index into the debug_addr
31724 section. */
31726 static void
31727 index_location_lists (dw_die_ref die)
31729 dw_die_ref c;
31730 dw_attr_node *a;
31731 unsigned ix;
31733 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31734 if (AT_class (a) == dw_val_class_loc_list)
31736 dw_loc_list_ref list = AT_loc_list (a);
31737 dw_loc_list_ref curr;
31738 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31740 /* Don't index an entry that has already been indexed
31741 or won't be output. Make sure skip_loc_list_entry doesn't
31742 call size_of_locs, because that might cause circular dependency,
31743 index_location_lists requiring address table indexes to be
31744 computed, but adding new indexes through add_addr_table_entry
31745 and address table index computation requiring no new additions
31746 to the hash table. In the rare case of DWARF[234] >= 64KB
31747 location expression, we'll just waste unused address table entry
31748 for it. */
31749 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
31750 continue;
31752 curr->begin_entry
31753 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31754 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
31755 curr->end_entry
31756 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
31760 FOR_EACH_CHILD (die, c, index_location_lists (c));
31763 /* Optimize location lists referenced from DIE
31764 children and share them whenever possible. */
31766 static void
31767 optimize_location_lists (dw_die_ref die)
31769 loc_list_hash_type htab (500);
31770 optimize_location_lists_1 (die, &htab);
31773 /* Traverse the limbo die list, and add parent/child links. The only
31774 dies without parents that should be here are concrete instances of
31775 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
31776 For concrete instances, we can get the parent die from the abstract
31777 instance. */
31779 static void
31780 flush_limbo_die_list (void)
31782 limbo_die_node *node;
31784 /* get_context_die calls force_decl_die, which can put new DIEs on the
31785 limbo list in LTO mode when nested functions are put in a different
31786 partition than that of their parent function. */
31787 while ((node = limbo_die_list))
31789 dw_die_ref die = node->die;
31790 limbo_die_list = node->next;
31792 if (die->die_parent == NULL)
31794 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31796 if (origin && origin->die_parent)
31797 add_child_die (origin->die_parent, die);
31798 else if (is_cu_die (die))
31800 else if (seen_error ())
31801 /* It's OK to be confused by errors in the input. */
31802 add_child_die (comp_unit_die (), die);
31803 else
31805 /* In certain situations, the lexical block containing a
31806 nested function can be optimized away, which results
31807 in the nested function die being orphaned. Likewise
31808 with the return type of that nested function. Force
31809 this to be a child of the containing function.
31811 It may happen that even the containing function got fully
31812 inlined and optimized out. In that case we are lost and
31813 assign the empty child. This should not be big issue as
31814 the function is likely unreachable too. */
31815 gcc_assert (node->created_for);
31817 if (DECL_P (node->created_for))
31818 origin = get_context_die (DECL_CONTEXT (node->created_for));
31819 else if (TYPE_P (node->created_for))
31820 origin = scope_die_for (node->created_for, comp_unit_die ());
31821 else
31822 origin = comp_unit_die ();
31824 add_child_die (origin, die);
31830 /* Reset DIEs so we can output them again. */
31832 static void
31833 reset_dies (dw_die_ref die)
31835 dw_die_ref c;
31837 /* Remove stuff we re-generate. */
31838 die->die_mark = 0;
31839 die->die_offset = 0;
31840 die->die_abbrev = 0;
31841 remove_AT (die, DW_AT_sibling);
31843 FOR_EACH_CHILD (die, c, reset_dies (c));
31846 /* reset_indirect_string removed the references coming from DW_AT_name
31847 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
31848 .debug_line_str strings again. */
31850 static void
31851 adjust_name_comp_dir (dw_die_ref die)
31853 for (int i = 0; i < 2; i++)
31855 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
31856 dw_attr_node *a = get_AT (die, attr_kind);
31857 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
31858 continue;
31860 if (!debug_line_str_hash)
31861 debug_line_str_hash
31862 = hash_table<indirect_string_hasher>::create_ggc (10);
31864 struct indirect_string_node *node
31865 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
31866 debug_line_str_hash);
31867 set_indirect_string (node);
31868 node->form = DW_FORM_line_strp;
31869 a->dw_attr_val.v.val_str = node;
31873 /* Output stuff that dwarf requires at the end of every file,
31874 and generate the DWARF-2 debugging info. */
31876 static void
31877 dwarf2out_finish (const char *filename)
31879 comdat_type_node *ctnode;
31880 dw_die_ref main_comp_unit_die;
31881 unsigned char checksum[16];
31882 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31884 /* Flush out any latecomers to the limbo party. */
31885 flush_limbo_die_list ();
31887 if (inline_entry_data_table)
31888 gcc_assert (inline_entry_data_table->is_empty ());
31890 if (flag_checking)
31892 verify_die (comp_unit_die ());
31893 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31894 verify_die (node->die);
31897 /* We shouldn't have any symbols with delayed asm names for
31898 DIEs generated after early finish. */
31899 gcc_assert (deferred_asm_name == NULL);
31901 gen_remaining_tmpl_value_param_die_attribute ();
31903 if (flag_generate_lto || flag_generate_offload)
31905 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31907 /* Prune stuff so that dwarf2out_finish runs successfully
31908 for the fat part of the object. */
31909 reset_dies (comp_unit_die ());
31910 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31911 reset_dies (node->die);
31913 hash_table<comdat_type_hasher> comdat_type_table (100);
31914 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31916 comdat_type_node **slot
31917 = comdat_type_table.find_slot (ctnode, INSERT);
31919 /* Don't reset types twice. */
31920 if (*slot != HTAB_EMPTY_ENTRY)
31921 continue;
31923 /* Remove the pointer to the line table. */
31924 remove_AT (ctnode->root_die, DW_AT_stmt_list);
31926 if (debug_info_level >= DINFO_LEVEL_TERSE)
31927 reset_dies (ctnode->root_die);
31929 *slot = ctnode;
31932 /* Reset die CU symbol so we don't output it twice. */
31933 comp_unit_die ()->die_id.die_symbol = NULL;
31935 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
31936 remove_AT (comp_unit_die (), DW_AT_stmt_list);
31937 if (have_macinfo)
31938 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
31940 /* Remove indirect string decisions. */
31941 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
31942 if (debug_line_str_hash)
31944 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
31945 debug_line_str_hash = NULL;
31946 if (asm_outputs_debug_line_str ())
31948 adjust_name_comp_dir (comp_unit_die ());
31949 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31950 adjust_name_comp_dir (node->die);
31955 #if ENABLE_ASSERT_CHECKING
31957 dw_die_ref die = comp_unit_die (), c;
31958 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
31960 #endif
31961 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31962 resolve_addr (ctnode->root_die);
31963 resolve_addr (comp_unit_die ());
31964 move_marked_base_types ();
31966 if (dump_file)
31968 fprintf (dump_file, "DWARF for %s\n", filename);
31969 print_die (comp_unit_die (), dump_file);
31972 /* Initialize sections and labels used for actual assembler output. */
31973 unsigned generation = init_sections_and_labels (false);
31975 /* Traverse the DIE's and add sibling attributes to those DIE's that
31976 have children. */
31977 add_sibling_attributes (comp_unit_die ());
31978 limbo_die_node *node;
31979 for (node = cu_die_list; node; node = node->next)
31980 add_sibling_attributes (node->die);
31981 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31982 add_sibling_attributes (ctnode->root_die);
31984 /* When splitting DWARF info, we put some attributes in the
31985 skeleton compile_unit DIE that remains in the .o, while
31986 most attributes go in the DWO compile_unit_die. */
31987 if (dwarf_split_debug_info)
31989 limbo_die_node *cu;
31990 main_comp_unit_die = gen_compile_unit_die (NULL);
31991 if (dwarf_version >= 5)
31992 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
31993 cu = limbo_die_list;
31994 gcc_assert (cu->die == main_comp_unit_die);
31995 limbo_die_list = limbo_die_list->next;
31996 cu->next = cu_die_list;
31997 cu_die_list = cu;
31999 else
32000 main_comp_unit_die = comp_unit_die ();
32002 /* Output a terminator label for the .text section. */
32003 switch_to_section (text_section);
32004 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32005 if (cold_text_section)
32007 switch_to_section (cold_text_section);
32008 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32011 /* We can only use the low/high_pc attributes if all of the code was
32012 in .text. */
32013 if ((!have_multiple_function_sections
32014 && vec_safe_length (switch_text_ranges) < 2)
32015 || (dwarf_version < 3 && dwarf_strict))
32017 const char *end_label = text_end_label;
32018 if (vec_safe_length (switch_text_ranges) == 1)
32019 end_label = (*switch_text_ranges)[0];
32020 /* Don't add if the CU has no associated code. */
32021 if (switch_text_ranges)
32022 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32023 end_label, true);
32025 else
32027 unsigned fde_idx;
32028 dw_fde_ref fde;
32029 bool range_list_added = false;
32030 if (switch_text_ranges)
32032 const char *prev_loc = text_section_label;
32033 const char *loc;
32034 unsigned idx;
32036 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32037 if (prev_loc)
32039 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32040 loc, &range_list_added, true);
32041 prev_loc = NULL;
32043 else
32044 prev_loc = loc;
32046 if (prev_loc)
32047 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32048 text_end_label, &range_list_added, true);
32051 if (switch_cold_ranges)
32053 const char *prev_loc = cold_text_section_label;
32054 const char *loc;
32055 unsigned idx;
32057 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32058 if (prev_loc)
32060 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32061 loc, &range_list_added, true);
32062 prev_loc = NULL;
32064 else
32065 prev_loc = loc;
32067 if (prev_loc)
32068 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32069 cold_end_label, &range_list_added, true);
32072 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32074 if (fde->ignored_debug)
32075 continue;
32076 if (!fde->in_std_section)
32077 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32078 fde->dw_fde_end, &range_list_added,
32079 true);
32080 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32081 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32082 fde->dw_fde_second_end, &range_list_added,
32083 true);
32086 if (range_list_added)
32088 /* We need to give .debug_loc and .debug_ranges an appropriate
32089 "base address". Use zero so that these addresses become
32090 absolute. Historically, we've emitted the unexpected
32091 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32092 Emit both to give time for other tools to adapt. */
32093 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32094 if (! dwarf_strict && dwarf_version < 4)
32095 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32097 add_ranges (NULL);
32098 have_multiple_function_sections = true;
32102 /* AIX Assembler inserts the length, so adjust the reference to match the
32103 offset expected by debuggers. */
32104 strcpy (dl_section_ref, debug_line_section_label);
32105 if (XCOFF_DEBUGGING_INFO)
32106 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32108 if (debug_info_level >= DINFO_LEVEL_TERSE)
32109 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32110 dl_section_ref);
32112 if (have_macinfo)
32113 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32114 macinfo_section_label);
32116 if (dwarf_split_debug_info)
32118 if (have_location_lists)
32120 /* Since we generate the loclists in the split DWARF .dwo
32121 file itself, we don't need to generate a loclists_base
32122 attribute for the split compile unit DIE. That attribute
32123 (and using relocatable sec_offset FORMs) isn't allowed
32124 for a split compile unit. Only if the .debug_loclists
32125 section was in the main file, would we need to generate a
32126 loclists_base attribute here (for the full or skeleton
32127 unit DIE). */
32129 /* optimize_location_lists calculates the size of the lists,
32130 so index them first, and assign indices to the entries.
32131 Although optimize_location_lists will remove entries from
32132 the table, it only does so for duplicates, and therefore
32133 only reduces ref_counts to 1. */
32134 index_location_lists (comp_unit_die ());
32137 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32138 index_rnglists ();
32140 if (addr_index_table != NULL)
32142 unsigned int index = 0;
32143 addr_index_table
32144 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32145 (&index);
32149 loc_list_idx = 0;
32150 if (have_location_lists)
32152 optimize_location_lists (comp_unit_die ());
32153 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32154 if (dwarf_version >= 5 && dwarf_split_debug_info)
32155 assign_location_list_indexes (comp_unit_die ());
32158 save_macinfo_strings ();
32160 if (dwarf_split_debug_info)
32162 unsigned int index = 0;
32164 /* Add attributes common to skeleton compile_units and
32165 type_units. Because these attributes include strings, it
32166 must be done before freezing the string table. Top-level
32167 skeleton die attrs are added when the skeleton type unit is
32168 created, so ensure it is created by this point. */
32169 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32170 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32173 /* Output all of the compilation units. We put the main one last so that
32174 the offsets are available to output_pubnames. */
32175 for (node = cu_die_list; node; node = node->next)
32176 output_comp_unit (node->die, 0, NULL);
32178 hash_table<comdat_type_hasher> comdat_type_table (100);
32179 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32181 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32183 /* Don't output duplicate types. */
32184 if (*slot != HTAB_EMPTY_ENTRY)
32185 continue;
32187 /* Add a pointer to the line table for the main compilation unit
32188 so that the debugger can make sense of DW_AT_decl_file
32189 attributes. */
32190 if (debug_info_level >= DINFO_LEVEL_TERSE)
32191 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32192 (!dwarf_split_debug_info
32193 ? dl_section_ref
32194 : debug_skeleton_line_section_label));
32196 output_comdat_type_unit (ctnode, false);
32197 *slot = ctnode;
32200 if (dwarf_split_debug_info)
32202 int mark;
32203 struct md5_ctx ctx;
32205 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32206 md5_init_ctx (&ctx);
32207 mark = 0;
32208 die_checksum (comp_unit_die (), &ctx, &mark);
32209 unmark_all_dies (comp_unit_die ());
32210 md5_finish_ctx (&ctx, checksum);
32212 if (dwarf_version < 5)
32214 /* Use the first 8 bytes of the checksum as the dwo_id,
32215 and add it to both comp-unit DIEs. */
32216 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32217 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32220 /* Add the base offset of the ranges table to the skeleton
32221 comp-unit DIE. */
32222 if (!vec_safe_is_empty (ranges_table))
32224 if (dwarf_version < 5)
32225 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32226 ranges_section_label);
32229 output_addr_table ();
32232 /* Output the main compilation unit if non-empty or if .debug_macinfo
32233 or .debug_macro will be emitted. */
32234 output_comp_unit (comp_unit_die (), have_macinfo,
32235 dwarf_split_debug_info ? checksum : NULL);
32237 if (dwarf_split_debug_info && info_section_emitted)
32238 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32240 /* Output the abbreviation table. */
32241 if (vec_safe_length (abbrev_die_table) != 1)
32243 switch_to_section (debug_abbrev_section);
32244 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32245 output_abbrev_section ();
32248 /* Output location list section if necessary. */
32249 if (have_location_lists)
32251 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32252 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32253 /* Output the location lists info. */
32254 switch_to_section (debug_loc_section);
32255 if (dwarf_version >= 5)
32257 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32258 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32259 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32260 dw2_asm_output_data (4, 0xffffffff,
32261 "Initial length escape value indicating "
32262 "64-bit DWARF extension");
32263 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32264 "Length of Location Lists");
32265 ASM_OUTPUT_LABEL (asm_out_file, l1);
32266 output_dwarf_version ();
32267 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32268 dw2_asm_output_data (1, 0, "Segment Size");
32269 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32270 "Offset Entry Count");
32272 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32273 if (dwarf_version >= 5 && dwarf_split_debug_info)
32275 unsigned int save_loc_list_idx = loc_list_idx;
32276 loc_list_idx = 0;
32277 output_loclists_offsets (comp_unit_die ());
32278 gcc_assert (save_loc_list_idx == loc_list_idx);
32280 output_location_lists (comp_unit_die ());
32281 if (dwarf_version >= 5)
32282 ASM_OUTPUT_LABEL (asm_out_file, l2);
32285 output_pubtables ();
32287 /* Output the address range information if a CU (.debug_info section)
32288 was emitted. We output an empty table even if we had no functions
32289 to put in it. This because the consumer has no way to tell the
32290 difference between an empty table that we omitted and failure to
32291 generate a table that would have contained data. */
32292 if (info_section_emitted)
32294 switch_to_section (debug_aranges_section);
32295 output_aranges ();
32298 /* Output ranges section if necessary. */
32299 if (!vec_safe_is_empty (ranges_table))
32301 if (dwarf_version >= 5)
32303 if (dwarf_split_debug_info)
32305 /* We don't know right now whether there are any
32306 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32307 Depending on into which of those two belongs the first
32308 ranges_table entry, emit that section first and that
32309 output_rnglists call will return true if the other kind of
32310 ranges needs to be emitted as well. */
32311 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32312 if (output_rnglists (generation, dwo))
32313 output_rnglists (generation, !dwo);
32315 else
32316 output_rnglists (generation, false);
32318 else
32319 output_ranges ();
32322 /* Have to end the macro section. */
32323 if (have_macinfo)
32325 switch_to_section (debug_macinfo_section);
32326 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32327 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32328 : debug_skeleton_line_section_label, false);
32329 dw2_asm_output_data (1, 0, "End compilation unit");
32332 /* Output the source line correspondence table. We must do this
32333 even if there is no line information. Otherwise, on an empty
32334 translation unit, we will generate a present, but empty,
32335 .debug_info section. IRIX 6.5 `nm' will then complain when
32336 examining the file. This is done late so that any filenames
32337 used by the debug_info section are marked as 'used'. */
32338 switch_to_section (debug_line_section);
32339 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32340 if (! output_asm_line_debug_info ())
32341 output_line_info (false);
32343 if (dwarf_split_debug_info && info_section_emitted)
32345 switch_to_section (debug_skeleton_line_section);
32346 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32347 output_line_info (true);
32350 /* If we emitted any indirect strings, output the string table too. */
32351 if (debug_str_hash || skeleton_debug_str_hash)
32352 output_indirect_strings ();
32353 if (debug_line_str_hash)
32355 switch_to_section (debug_line_str_section);
32356 const enum dwarf_form form = DW_FORM_line_strp;
32357 debug_line_str_hash->traverse<enum dwarf_form,
32358 output_indirect_string> (form);
32361 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32362 symview_upper_bound = 0;
32363 if (zero_view_p)
32364 bitmap_clear (zero_view_p);
32367 /* Returns a hash value for X (which really is a variable_value_struct). */
32369 inline hashval_t
32370 variable_value_hasher::hash (variable_value_struct *x)
32372 return (hashval_t) x->decl_id;
32375 /* Return nonzero if decl_id of variable_value_struct X is the same as
32376 UID of decl Y. */
32378 inline bool
32379 variable_value_hasher::equal (variable_value_struct *x, tree y)
32381 return x->decl_id == DECL_UID (y);
32384 /* Helper function for resolve_variable_value, handle
32385 DW_OP_GNU_variable_value in one location expression.
32386 Return true if exprloc has been changed into loclist. */
32388 static bool
32389 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32391 dw_loc_descr_ref next;
32392 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32394 next = loc->dw_loc_next;
32395 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32396 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32397 continue;
32399 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32400 if (DECL_CONTEXT (decl) != current_function_decl)
32401 continue;
32403 dw_die_ref ref = lookup_decl_die (decl);
32404 if (ref)
32406 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32407 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32408 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32409 continue;
32411 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32412 if (l == NULL)
32413 continue;
32414 if (l->dw_loc_next)
32416 if (AT_class (a) != dw_val_class_loc)
32417 continue;
32418 switch (a->dw_attr)
32420 /* Following attributes allow both exprloc and loclist
32421 classes, so we can change them into a loclist. */
32422 case DW_AT_location:
32423 case DW_AT_string_length:
32424 case DW_AT_return_addr:
32425 case DW_AT_data_member_location:
32426 case DW_AT_frame_base:
32427 case DW_AT_segment:
32428 case DW_AT_static_link:
32429 case DW_AT_use_location:
32430 case DW_AT_vtable_elem_location:
32431 if (prev)
32433 prev->dw_loc_next = NULL;
32434 prepend_loc_descr_to_each (l, AT_loc (a));
32436 if (next)
32437 add_loc_descr_to_each (l, next);
32438 a->dw_attr_val.val_class = dw_val_class_loc_list;
32439 a->dw_attr_val.val_entry = NULL;
32440 a->dw_attr_val.v.val_loc_list = l;
32441 have_location_lists = true;
32442 return true;
32443 /* Following attributes allow both exprloc and reference,
32444 so if the whole expression is DW_OP_GNU_variable_value alone
32445 we could transform it into reference. */
32446 case DW_AT_byte_size:
32447 case DW_AT_bit_size:
32448 case DW_AT_lower_bound:
32449 case DW_AT_upper_bound:
32450 case DW_AT_bit_stride:
32451 case DW_AT_count:
32452 case DW_AT_allocated:
32453 case DW_AT_associated:
32454 case DW_AT_byte_stride:
32455 if (prev == NULL && next == NULL)
32456 break;
32457 /* FALLTHRU */
32458 default:
32459 if (dwarf_strict)
32460 continue;
32461 break;
32463 /* Create DW_TAG_variable that we can refer to. */
32464 gen_decl_die (decl, NULL_TREE, NULL,
32465 lookup_decl_die (current_function_decl));
32466 ref = lookup_decl_die (decl);
32467 if (ref)
32469 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32470 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32471 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32473 continue;
32475 if (prev)
32477 prev->dw_loc_next = l->expr;
32478 add_loc_descr (&prev->dw_loc_next, next);
32479 free_loc_descr (loc, NULL);
32480 next = prev->dw_loc_next;
32482 else
32484 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32485 add_loc_descr (&loc, next);
32486 next = loc;
32488 loc = prev;
32490 return false;
32493 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32495 static void
32496 resolve_variable_value (dw_die_ref die)
32498 dw_attr_node *a;
32499 dw_loc_list_ref loc;
32500 unsigned ix;
32502 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32503 switch (AT_class (a))
32505 case dw_val_class_loc:
32506 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32507 break;
32508 /* FALLTHRU */
32509 case dw_val_class_loc_list:
32510 loc = AT_loc_list (a);
32511 gcc_assert (loc);
32512 for (; loc; loc = loc->dw_loc_next)
32513 resolve_variable_value_in_expr (a, loc->expr);
32514 break;
32515 default:
32516 break;
32520 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32521 temporaries in the current function. */
32523 static void
32524 resolve_variable_values (void)
32526 if (!variable_value_hash || !current_function_decl)
32527 return;
32529 struct variable_value_struct *node
32530 = variable_value_hash->find_with_hash (current_function_decl,
32531 DECL_UID (current_function_decl));
32533 if (node == NULL)
32534 return;
32536 unsigned int i;
32537 dw_die_ref die;
32538 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32539 resolve_variable_value (die);
32542 /* Helper function for note_variable_value, handle one location
32543 expression. */
32545 static void
32546 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32548 for (; loc; loc = loc->dw_loc_next)
32549 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32550 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32552 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32553 dw_die_ref ref = lookup_decl_die (decl);
32554 if (! ref && (flag_generate_lto || flag_generate_offload))
32556 /* ??? This is somewhat a hack because we do not create DIEs
32557 for variables not in BLOCK trees early but when generating
32558 early LTO output we need the dw_val_class_decl_ref to be
32559 fully resolved. For fat LTO objects we'd also like to
32560 undo this after LTO dwarf output. */
32561 gcc_assert (DECL_CONTEXT (decl));
32562 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32563 gcc_assert (ctx != NULL);
32564 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32565 ref = lookup_decl_die (decl);
32566 gcc_assert (ref != NULL);
32568 if (ref)
32570 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32571 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32572 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32573 continue;
32575 if (VAR_P (decl)
32576 && DECL_CONTEXT (decl)
32577 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32578 && lookup_decl_die (DECL_CONTEXT (decl)))
32580 if (!variable_value_hash)
32581 variable_value_hash
32582 = hash_table<variable_value_hasher>::create_ggc (10);
32584 tree fndecl = DECL_CONTEXT (decl);
32585 struct variable_value_struct *node;
32586 struct variable_value_struct **slot
32587 = variable_value_hash->find_slot_with_hash (fndecl,
32588 DECL_UID (fndecl),
32589 INSERT);
32590 if (*slot == NULL)
32592 node = ggc_cleared_alloc<variable_value_struct> ();
32593 node->decl_id = DECL_UID (fndecl);
32594 *slot = node;
32596 else
32597 node = *slot;
32599 vec_safe_push (node->dies, die);
32604 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32605 with dw_val_class_decl_ref operand. */
32607 static void
32608 note_variable_value (dw_die_ref die)
32610 dw_die_ref c;
32611 dw_attr_node *a;
32612 dw_loc_list_ref loc;
32613 unsigned ix;
32615 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32616 switch (AT_class (a))
32618 case dw_val_class_loc_list:
32619 loc = AT_loc_list (a);
32620 gcc_assert (loc);
32621 if (!loc->noted_variable_value)
32623 loc->noted_variable_value = 1;
32624 for (; loc; loc = loc->dw_loc_next)
32625 note_variable_value_in_expr (die, loc->expr);
32627 break;
32628 case dw_val_class_loc:
32629 note_variable_value_in_expr (die, AT_loc (a));
32630 break;
32631 default:
32632 break;
32635 /* Mark children. */
32636 FOR_EACH_CHILD (die, c, note_variable_value (c));
32639 /* Perform any cleanups needed after the early debug generation pass
32640 has run. */
32642 static void
32643 dwarf2out_early_finish (const char *filename)
32645 set_early_dwarf s;
32646 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32648 /* PCH might result in DW_AT_producer string being restored from the
32649 header compilation, so always fill it with empty string initially
32650 and overwrite only here. */
32651 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32653 if (dwarf_record_gcc_switches)
32654 producer_string = gen_producer_string (lang_hooks.name,
32655 save_decoded_options,
32656 save_decoded_options_count);
32657 else
32658 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
32660 producer->dw_attr_val.v.val_str->refcount--;
32661 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32663 /* Add the name for the main input file now. We delayed this from
32664 dwarf2out_init to avoid complications with PCH. */
32665 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32666 add_comp_dir_attribute (comp_unit_die ());
32668 /* With LTO early dwarf was really finished at compile-time, so make
32669 sure to adjust the phase after annotating the LTRANS CU DIE. */
32670 if (in_lto_p)
32672 early_dwarf_finished = true;
32673 if (dump_file)
32675 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32676 print_die (comp_unit_die (), dump_file);
32678 return;
32681 /* Walk through the list of incomplete types again, trying once more to
32682 emit full debugging info for them. */
32683 retry_incomplete_types ();
32685 gen_scheduled_generic_parms_dies ();
32686 gen_remaining_tmpl_value_param_die_attribute ();
32688 /* The point here is to flush out the limbo list so that it is empty
32689 and we don't need to stream it for LTO. */
32690 flush_limbo_die_list ();
32692 /* Add DW_AT_linkage_name for all deferred DIEs. */
32693 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32695 tree decl = node->created_for;
32696 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32697 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32698 ended up in deferred_asm_name before we knew it was
32699 constant and never written to disk. */
32700 && DECL_ASSEMBLER_NAME (decl))
32702 add_linkage_attr (node->die, decl);
32703 move_linkage_attr (node->die);
32706 deferred_asm_name = NULL;
32708 if (flag_eliminate_unused_debug_types)
32709 prune_unused_types ();
32711 /* Generate separate COMDAT sections for type DIEs. */
32712 if (use_debug_types)
32714 break_out_comdat_types (comp_unit_die ());
32716 /* Each new type_unit DIE was added to the limbo die list when created.
32717 Since these have all been added to comdat_type_list, clear the
32718 limbo die list. */
32719 limbo_die_list = NULL;
32721 /* For each new comdat type unit, copy declarations for incomplete
32722 types to make the new unit self-contained (i.e., no direct
32723 references to the main compile unit). */
32724 for (comdat_type_node *ctnode = comdat_type_list;
32725 ctnode != NULL; ctnode = ctnode->next)
32726 copy_decls_for_unworthy_types (ctnode->root_die);
32727 copy_decls_for_unworthy_types (comp_unit_die ());
32729 /* In the process of copying declarations from one unit to another,
32730 we may have left some declarations behind that are no longer
32731 referenced. Prune them. */
32732 prune_unused_types ();
32735 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
32736 with dw_val_class_decl_ref operand. */
32737 note_variable_value (comp_unit_die ());
32738 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32739 note_variable_value (node->die);
32740 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
32741 ctnode = ctnode->next)
32742 note_variable_value (ctnode->root_die);
32743 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32744 note_variable_value (node->die);
32746 /* The AT_pubnames attribute needs to go in all skeleton dies, including
32747 both the main_cu and all skeleton TUs. Making this call unconditional
32748 would end up either adding a second copy of the AT_pubnames attribute, or
32749 requiring a special case in add_top_level_skeleton_die_attrs. */
32750 if (!dwarf_split_debug_info)
32751 add_AT_pubnames (comp_unit_die ());
32753 /* The early debug phase is now finished. */
32754 early_dwarf_finished = true;
32755 if (dump_file)
32757 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
32758 print_die (comp_unit_die (), dump_file);
32761 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
32762 if ((!flag_generate_lto && !flag_generate_offload)
32763 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
32764 copy_lto_debug_sections operation of the simple object support in
32765 libiberty is not implemented for them yet. */
32766 || TARGET_PECOFF || TARGET_COFF)
32767 return;
32769 /* Now as we are going to output for LTO initialize sections and labels
32770 to the LTO variants. We don't need a random-seed postfix as other
32771 LTO sections as linking the LTO debug sections into one in a partial
32772 link is fine. */
32773 init_sections_and_labels (true);
32775 /* The output below is modeled after dwarf2out_finish with all
32776 location related output removed and some LTO specific changes.
32777 Some refactoring might make both smaller and easier to match up. */
32779 /* Traverse the DIE's and add sibling attributes to those DIE's
32780 that have children. */
32781 add_sibling_attributes (comp_unit_die ());
32782 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32783 add_sibling_attributes (node->die);
32784 for (comdat_type_node *ctnode = comdat_type_list;
32785 ctnode != NULL; ctnode = ctnode->next)
32786 add_sibling_attributes (ctnode->root_die);
32788 /* AIX Assembler inserts the length, so adjust the reference to match the
32789 offset expected by debuggers. */
32790 strcpy (dl_section_ref, debug_line_section_label);
32791 if (XCOFF_DEBUGGING_INFO)
32792 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32794 if (debug_info_level >= DINFO_LEVEL_TERSE)
32795 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32797 if (have_macinfo)
32798 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32799 macinfo_section_label);
32801 save_macinfo_strings ();
32803 if (dwarf_split_debug_info)
32805 unsigned int index = 0;
32806 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32809 /* Output all of the compilation units. We put the main one last so that
32810 the offsets are available to output_pubnames. */
32811 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32812 output_comp_unit (node->die, 0, NULL);
32814 hash_table<comdat_type_hasher> comdat_type_table (100);
32815 for (comdat_type_node *ctnode = comdat_type_list;
32816 ctnode != NULL; ctnode = ctnode->next)
32818 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32820 /* Don't output duplicate types. */
32821 if (*slot != HTAB_EMPTY_ENTRY)
32822 continue;
32824 /* Add a pointer to the line table for the main compilation unit
32825 so that the debugger can make sense of DW_AT_decl_file
32826 attributes. */
32827 if (debug_info_level >= DINFO_LEVEL_TERSE)
32828 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32829 (!dwarf_split_debug_info
32830 ? debug_line_section_label
32831 : debug_skeleton_line_section_label));
32833 output_comdat_type_unit (ctnode, true);
32834 *slot = ctnode;
32837 /* Stick a unique symbol to the main debuginfo section. */
32838 compute_comp_unit_symbol (comp_unit_die ());
32840 /* Output the main compilation unit. We always need it if only for
32841 the CU symbol. */
32842 output_comp_unit (comp_unit_die (), true, NULL);
32844 /* Output the abbreviation table. */
32845 if (vec_safe_length (abbrev_die_table) != 1)
32847 switch_to_section (debug_abbrev_section);
32848 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32849 output_abbrev_section ();
32852 /* Have to end the macro section. */
32853 if (have_macinfo)
32855 /* We have to save macinfo state if we need to output it again
32856 for the FAT part of the object. */
32857 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32858 if (flag_fat_lto_objects)
32859 macinfo_table = macinfo_table->copy ();
32861 switch_to_section (debug_macinfo_section);
32862 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32863 output_macinfo (debug_line_section_label, true);
32864 dw2_asm_output_data (1, 0, "End compilation unit");
32866 if (flag_fat_lto_objects)
32868 vec_free (macinfo_table);
32869 macinfo_table = saved_macinfo_table;
32873 /* Emit a skeleton debug_line section. */
32874 switch_to_section (debug_line_section);
32875 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32876 output_line_info (true);
32878 /* If we emitted any indirect strings, output the string table too. */
32879 if (debug_str_hash || skeleton_debug_str_hash)
32880 output_indirect_strings ();
32881 if (debug_line_str_hash)
32883 switch_to_section (debug_line_str_section);
32884 const enum dwarf_form form = DW_FORM_line_strp;
32885 debug_line_str_hash->traverse<enum dwarf_form,
32886 output_indirect_string> (form);
32889 /* Switch back to the text section. */
32890 switch_to_section (text_section);
32893 /* Reset all state within dwarf2out.c so that we can rerun the compiler
32894 within the same process. For use by toplev::finalize. */
32896 void
32897 dwarf2out_c_finalize (void)
32899 last_var_location_insn = NULL;
32900 cached_next_real_insn = NULL;
32901 used_rtx_array = NULL;
32902 incomplete_types = NULL;
32903 debug_info_section = NULL;
32904 debug_skeleton_info_section = NULL;
32905 debug_abbrev_section = NULL;
32906 debug_skeleton_abbrev_section = NULL;
32907 debug_aranges_section = NULL;
32908 debug_addr_section = NULL;
32909 debug_macinfo_section = NULL;
32910 debug_line_section = NULL;
32911 debug_skeleton_line_section = NULL;
32912 debug_loc_section = NULL;
32913 debug_pubnames_section = NULL;
32914 debug_pubtypes_section = NULL;
32915 debug_str_section = NULL;
32916 debug_line_str_section = NULL;
32917 debug_str_dwo_section = NULL;
32918 debug_str_offsets_section = NULL;
32919 debug_ranges_section = NULL;
32920 debug_ranges_dwo_section = NULL;
32921 debug_frame_section = NULL;
32922 fde_vec = NULL;
32923 debug_str_hash = NULL;
32924 debug_line_str_hash = NULL;
32925 skeleton_debug_str_hash = NULL;
32926 dw2_string_counter = 0;
32927 have_multiple_function_sections = false;
32928 in_text_section_p = false;
32929 cold_text_section = NULL;
32930 last_text_label = NULL;
32931 last_cold_label = NULL;
32932 switch_text_ranges = NULL;
32933 switch_cold_ranges = NULL;
32934 current_unit_personality = NULL;
32936 early_dwarf = false;
32937 early_dwarf_finished = false;
32939 next_die_offset = 0;
32940 single_comp_unit_die = NULL;
32941 comdat_type_list = NULL;
32942 limbo_die_list = NULL;
32943 file_table = NULL;
32944 decl_die_table = NULL;
32945 common_block_die_table = NULL;
32946 decl_loc_table = NULL;
32947 call_arg_locations = NULL;
32948 call_arg_loc_last = NULL;
32949 call_site_count = -1;
32950 tail_call_site_count = -1;
32951 cached_dw_loc_list_table = NULL;
32952 abbrev_die_table = NULL;
32953 delete dwarf_proc_stack_usage_map;
32954 dwarf_proc_stack_usage_map = NULL;
32955 line_info_label_num = 0;
32956 cur_line_info_table = NULL;
32957 text_section_line_info = NULL;
32958 cold_text_section_line_info = NULL;
32959 separate_line_info = NULL;
32960 info_section_emitted = false;
32961 pubname_table = NULL;
32962 pubtype_table = NULL;
32963 macinfo_table = NULL;
32964 ranges_table = NULL;
32965 ranges_by_label = NULL;
32966 rnglist_idx = 0;
32967 have_location_lists = false;
32968 loclabel_num = 0;
32969 poc_label_num = 0;
32970 last_emitted_file = NULL;
32971 label_num = 0;
32972 tmpl_value_parm_die_table = NULL;
32973 generic_type_instances = NULL;
32974 frame_pointer_fb_offset = 0;
32975 frame_pointer_fb_offset_valid = false;
32976 base_types.release ();
32977 XDELETEVEC (producer_string);
32978 producer_string = NULL;
32979 output_line_info_generation = 0;
32980 init_sections_and_labels_generation = 0;
32983 #include "gt-dwarf2out.h"