[PATCH c++/86881] -Wshadow-local-compatible ICE
[official-gcc.git] / gcc / dwarf2out.c
blob48c50378622543554f074daef708eba7318c0835
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "print-rtl.h"
87 #include "debug.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "lra.h"
91 #include "dumpfile.h"
92 #include "opts.h"
93 #include "tree-dfa.h"
94 #include "gdb/gdb-index.h"
95 #include "rtl-iter.h"
96 #include "stringpool.h"
97 #include "attribs.h"
98 #include "file-prefix-map.h" /* remap_debug_filename() */
100 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
101 int, bool);
102 static rtx_insn *last_var_location_insn;
103 static rtx_insn *cached_next_real_insn;
104 static void dwarf2out_decl (tree);
105 static bool is_redundant_typedef (const_tree);
107 #ifndef XCOFF_DEBUGGING_INFO
108 #define XCOFF_DEBUGGING_INFO 0
109 #endif
111 #ifndef HAVE_XCOFF_DWARF_EXTRAS
112 #define HAVE_XCOFF_DWARF_EXTRAS 0
113 #endif
115 #ifdef VMS_DEBUGGING_INFO
116 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
118 /* Define this macro to be a nonzero value if the directory specifications
119 which are output in the debug info should end with a separator. */
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
121 /* Define this macro to evaluate to a nonzero value if GCC should refrain
122 from generating indirect strings in DWARF2 debug information, for instance
123 if your target is stuck with an old version of GDB that is unable to
124 process them properly or uses VMS Debug. */
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
126 #else
127 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
128 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
129 #endif
131 /* ??? Poison these here until it can be done generically. They've been
132 totally replaced in this file; make sure it stays that way. */
133 #undef DWARF2_UNWIND_INFO
134 #undef DWARF2_FRAME_INFO
135 #if (GCC_VERSION >= 3000)
136 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
137 #endif
139 /* The size of the target's pointer type. */
140 #ifndef PTR_SIZE
141 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
142 #endif
144 /* Array of RTXes referenced by the debugging information, which therefore
145 must be kept around forever. */
146 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
148 /* A pointer to the base of a list of incomplete types which might be
149 completed at some later time. incomplete_types_list needs to be a
150 vec<tree, va_gc> *because we want to tell the garbage collector about
151 it. */
152 static GTY(()) vec<tree, va_gc> *incomplete_types;
154 /* Pointers to various DWARF2 sections. */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static const char *debug_macinfo_section_name;
163 static unsigned macinfo_label_base = 1;
164 static GTY(()) section *debug_line_section;
165 static GTY(()) section *debug_skeleton_line_section;
166 static GTY(()) section *debug_loc_section;
167 static GTY(()) section *debug_pubnames_section;
168 static GTY(()) section *debug_pubtypes_section;
169 static GTY(()) section *debug_str_section;
170 static GTY(()) section *debug_line_str_section;
171 static GTY(()) section *debug_str_dwo_section;
172 static GTY(()) section *debug_str_offsets_section;
173 static GTY(()) section *debug_ranges_section;
174 static GTY(()) section *debug_frame_section;
176 /* Maximum size (in bytes) of an artificially generated label. */
177 #define MAX_ARTIFICIAL_LABEL_BYTES 40
179 /* According to the (draft) DWARF 3 specification, the initial length
180 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
181 bytes are 0xffffffff, followed by the length stored in the next 8
182 bytes.
184 However, the SGI/MIPS ABI uses an initial length which is equal to
185 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
187 #ifndef DWARF_INITIAL_LENGTH_SIZE
188 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
189 #endif
191 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
192 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
193 #endif
195 /* Round SIZE up to the nearest BOUNDARY. */
196 #define DWARF_ROUND(SIZE,BOUNDARY) \
197 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
199 /* CIE identifier. */
200 #if HOST_BITS_PER_WIDE_INT >= 64
201 #define DWARF_CIE_ID \
202 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
203 #else
204 #define DWARF_CIE_ID DW_CIE_ID
205 #endif
208 /* A vector for a table that contains frame description
209 information for each routine. */
210 #define NOT_INDEXED (-1U)
211 #define NO_INDEX_ASSIGNED (-2U)
213 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
215 struct GTY((for_user)) indirect_string_node {
216 const char *str;
217 unsigned int refcount;
218 enum dwarf_form form;
219 char *label;
220 unsigned int index;
223 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
225 typedef const char *compare_type;
227 static hashval_t hash (indirect_string_node *);
228 static bool equal (indirect_string_node *, const char *);
231 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
233 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
235 /* With split_debug_info, both the comp_dir and dwo_name go in the
236 main object file, rather than the dwo, similar to the force_direct
237 parameter elsewhere but with additional complications:
239 1) The string is needed in both the main object file and the dwo.
240 That is, the comp_dir and dwo_name will appear in both places.
242 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
243 DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
245 3) GCC chooses the form to use late, depending on the size and
246 reference count.
248 Rather than forcing the all debug string handling functions and
249 callers to deal with these complications, simply use a separate,
250 special-cased string table for any attribute that should go in the
251 main object file. This limits the complexity to just the places
252 that need it. */
254 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
256 static GTY(()) int dw2_string_counter;
258 /* True if the compilation unit places functions in more than one section. */
259 static GTY(()) bool have_multiple_function_sections = false;
261 /* Whether the default text and cold text sections have been used at all. */
262 static GTY(()) bool text_section_used = false;
263 static GTY(()) bool cold_text_section_used = false;
265 /* The default cold text section. */
266 static GTY(()) section *cold_text_section;
268 /* The DIE for C++14 'auto' in a function return type. */
269 static GTY(()) dw_die_ref auto_die;
271 /* The DIE for C++14 'decltype(auto)' in a function return type. */
272 static GTY(()) dw_die_ref decltype_auto_die;
274 /* Forward declarations for functions defined in this file. */
276 static void output_call_frame_info (int);
277 static void dwarf2out_note_section_used (void);
279 /* Personality decl of current unit. Used only when assembler does not support
280 personality CFI. */
281 static GTY(()) rtx current_unit_personality;
283 /* Whether an eh_frame section is required. */
284 static GTY(()) bool do_eh_frame = false;
286 /* .debug_rnglists next index. */
287 static unsigned int rnglist_idx;
289 /* Data and reference forms for relocatable data. */
290 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
291 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
293 #ifndef DEBUG_FRAME_SECTION
294 #define DEBUG_FRAME_SECTION ".debug_frame"
295 #endif
297 #ifndef FUNC_BEGIN_LABEL
298 #define FUNC_BEGIN_LABEL "LFB"
299 #endif
301 #ifndef FUNC_SECOND_SECT_LABEL
302 #define FUNC_SECOND_SECT_LABEL "LFSB"
303 #endif
305 #ifndef FUNC_END_LABEL
306 #define FUNC_END_LABEL "LFE"
307 #endif
309 #ifndef PROLOGUE_END_LABEL
310 #define PROLOGUE_END_LABEL "LPE"
311 #endif
313 #ifndef EPILOGUE_BEGIN_LABEL
314 #define EPILOGUE_BEGIN_LABEL "LEB"
315 #endif
317 #ifndef FRAME_BEGIN_LABEL
318 #define FRAME_BEGIN_LABEL "Lframe"
319 #endif
320 #define CIE_AFTER_SIZE_LABEL "LSCIE"
321 #define CIE_END_LABEL "LECIE"
322 #define FDE_LABEL "LSFDE"
323 #define FDE_AFTER_SIZE_LABEL "LASFDE"
324 #define FDE_END_LABEL "LEFDE"
325 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
326 #define LINE_NUMBER_END_LABEL "LELT"
327 #define LN_PROLOG_AS_LABEL "LASLTP"
328 #define LN_PROLOG_END_LABEL "LELTP"
329 #define DIE_LABEL_PREFIX "DW"
331 /* Match the base name of a file to the base name of a compilation unit. */
333 static int
334 matches_main_base (const char *path)
336 /* Cache the last query. */
337 static const char *last_path = NULL;
338 static int last_match = 0;
339 if (path != last_path)
341 const char *base;
342 int length = base_of_path (path, &base);
343 last_path = path;
344 last_match = (length == main_input_baselength
345 && memcmp (base, main_input_basename, length) == 0);
347 return last_match;
350 #ifdef DEBUG_DEBUG_STRUCT
352 static int
353 dump_struct_debug (tree type, enum debug_info_usage usage,
354 enum debug_struct_file criterion, int generic,
355 int matches, int result)
357 /* Find the type name. */
358 tree type_decl = TYPE_STUB_DECL (type);
359 tree t = type_decl;
360 const char *name = 0;
361 if (TREE_CODE (t) == TYPE_DECL)
362 t = DECL_NAME (t);
363 if (t)
364 name = IDENTIFIER_POINTER (t);
366 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
367 criterion,
368 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
369 matches ? "bas" : "hdr",
370 generic ? "gen" : "ord",
371 usage == DINFO_USAGE_DFN ? ";" :
372 usage == DINFO_USAGE_DIR_USE ? "." : "*",
373 result,
374 (void*) type_decl, name);
375 return result;
377 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
378 dump_struct_debug (type, usage, criterion, generic, matches, result)
380 #else
382 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
383 (result)
385 #endif
387 /* Get the number of HOST_WIDE_INTs needed to represent the precision
388 of the number. Some constants have a large uniform precision, so
389 we get the precision needed for the actual value of the number. */
391 static unsigned int
392 get_full_len (const wide_int &op)
394 int prec = wi::min_precision (op, UNSIGNED);
395 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
396 / HOST_BITS_PER_WIDE_INT);
399 static bool
400 should_emit_struct_debug (tree type, enum debug_info_usage usage)
402 enum debug_struct_file criterion;
403 tree type_decl;
404 bool generic = lang_hooks.types.generic_p (type);
406 if (generic)
407 criterion = debug_struct_generic[usage];
408 else
409 criterion = debug_struct_ordinary[usage];
411 if (criterion == DINFO_STRUCT_FILE_NONE)
412 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
413 if (criterion == DINFO_STRUCT_FILE_ANY)
414 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
416 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
418 if (type_decl != NULL)
420 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
421 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
423 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
424 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
427 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
430 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
431 switch to the data section instead, and write out a synthetic start label
432 for collect2 the first time around. */
434 static void
435 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
437 if (eh_frame_section == 0)
439 int flags;
441 if (EH_TABLES_CAN_BE_READ_ONLY)
443 int fde_encoding;
444 int per_encoding;
445 int lsda_encoding;
447 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
448 /*global=*/0);
449 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
450 /*global=*/1);
451 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
452 /*global=*/0);
453 flags = ((! flag_pic
454 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
455 && (fde_encoding & 0x70) != DW_EH_PE_aligned
456 && (per_encoding & 0x70) != DW_EH_PE_absptr
457 && (per_encoding & 0x70) != DW_EH_PE_aligned
458 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
459 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
460 ? 0 : SECTION_WRITE);
462 else
463 flags = SECTION_WRITE;
465 #ifdef EH_FRAME_SECTION_NAME
466 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
467 #else
468 eh_frame_section = ((flags == SECTION_WRITE)
469 ? data_section : readonly_data_section);
470 #endif /* EH_FRAME_SECTION_NAME */
473 switch_to_section (eh_frame_section);
475 #ifdef EH_FRAME_THROUGH_COLLECT2
476 /* We have no special eh_frame section. Emit special labels to guide
477 collect2. */
478 if (!back)
480 tree label = get_file_function_name ("F");
481 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
482 targetm.asm_out.globalize_label (asm_out_file,
483 IDENTIFIER_POINTER (label));
484 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
486 #endif
489 /* Switch [BACK] to the eh or debug frame table section, depending on
490 FOR_EH. */
492 static void
493 switch_to_frame_table_section (int for_eh, bool back)
495 if (for_eh)
496 switch_to_eh_frame_section (back);
497 else
499 if (!debug_frame_section)
500 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
501 SECTION_DEBUG, NULL);
502 switch_to_section (debug_frame_section);
506 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
508 enum dw_cfi_oprnd_type
509 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
511 switch (cfi)
513 case DW_CFA_nop:
514 case DW_CFA_GNU_window_save:
515 case DW_CFA_remember_state:
516 case DW_CFA_restore_state:
517 return dw_cfi_oprnd_unused;
519 case DW_CFA_set_loc:
520 case DW_CFA_advance_loc1:
521 case DW_CFA_advance_loc2:
522 case DW_CFA_advance_loc4:
523 case DW_CFA_MIPS_advance_loc8:
524 return dw_cfi_oprnd_addr;
526 case DW_CFA_offset:
527 case DW_CFA_offset_extended:
528 case DW_CFA_def_cfa:
529 case DW_CFA_offset_extended_sf:
530 case DW_CFA_def_cfa_sf:
531 case DW_CFA_restore:
532 case DW_CFA_restore_extended:
533 case DW_CFA_undefined:
534 case DW_CFA_same_value:
535 case DW_CFA_def_cfa_register:
536 case DW_CFA_register:
537 case DW_CFA_expression:
538 case DW_CFA_val_expression:
539 return dw_cfi_oprnd_reg_num;
541 case DW_CFA_def_cfa_offset:
542 case DW_CFA_GNU_args_size:
543 case DW_CFA_def_cfa_offset_sf:
544 return dw_cfi_oprnd_offset;
546 case DW_CFA_def_cfa_expression:
547 return dw_cfi_oprnd_loc;
549 default:
550 gcc_unreachable ();
554 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
556 enum dw_cfi_oprnd_type
557 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
559 switch (cfi)
561 case DW_CFA_def_cfa:
562 case DW_CFA_def_cfa_sf:
563 case DW_CFA_offset:
564 case DW_CFA_offset_extended_sf:
565 case DW_CFA_offset_extended:
566 return dw_cfi_oprnd_offset;
568 case DW_CFA_register:
569 return dw_cfi_oprnd_reg_num;
571 case DW_CFA_expression:
572 case DW_CFA_val_expression:
573 return dw_cfi_oprnd_loc;
575 case DW_CFA_def_cfa_expression:
576 return dw_cfi_oprnd_cfa_loc;
578 default:
579 return dw_cfi_oprnd_unused;
583 /* Output one FDE. */
585 static void
586 output_fde (dw_fde_ref fde, bool for_eh, bool second,
587 char *section_start_label, int fde_encoding, char *augmentation,
588 bool any_lsda_needed, int lsda_encoding)
590 const char *begin, *end;
591 static unsigned int j;
592 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
594 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
595 /* empty */ 0);
596 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
597 for_eh + j);
598 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
599 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
600 if (!XCOFF_DEBUGGING_INFO || for_eh)
602 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
603 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
604 " indicating 64-bit DWARF extension");
605 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
606 "FDE Length");
608 ASM_OUTPUT_LABEL (asm_out_file, l1);
610 if (for_eh)
611 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
612 else
613 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
614 debug_frame_section, "FDE CIE offset");
616 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
617 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
619 if (for_eh)
621 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
622 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
623 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
624 "FDE initial location");
625 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
626 end, begin, "FDE address range");
628 else
630 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
631 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
634 if (augmentation[0])
636 if (any_lsda_needed)
638 int size = size_of_encoded_value (lsda_encoding);
640 if (lsda_encoding == DW_EH_PE_aligned)
642 int offset = ( 4 /* Length */
643 + 4 /* CIE offset */
644 + 2 * size_of_encoded_value (fde_encoding)
645 + 1 /* Augmentation size */ );
646 int pad = -offset & (PTR_SIZE - 1);
648 size += pad;
649 gcc_assert (size_of_uleb128 (size) == 1);
652 dw2_asm_output_data_uleb128 (size, "Augmentation size");
654 if (fde->uses_eh_lsda)
656 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
657 fde->funcdef_number);
658 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
659 gen_rtx_SYMBOL_REF (Pmode, l1),
660 false,
661 "Language Specific Data Area");
663 else
665 if (lsda_encoding == DW_EH_PE_aligned)
666 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
667 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
668 "Language Specific Data Area (none)");
671 else
672 dw2_asm_output_data_uleb128 (0, "Augmentation size");
675 /* Loop through the Call Frame Instructions associated with this FDE. */
676 fde->dw_fde_current_label = begin;
678 size_t from, until, i;
680 from = 0;
681 until = vec_safe_length (fde->dw_fde_cfi);
683 if (fde->dw_fde_second_begin == NULL)
685 else if (!second)
686 until = fde->dw_fde_switch_cfi_index;
687 else
688 from = fde->dw_fde_switch_cfi_index;
690 for (i = from; i < until; i++)
691 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
694 /* If we are to emit a ref/link from function bodies to their frame tables,
695 do it now. This is typically performed to make sure that tables
696 associated with functions are dragged with them and not discarded in
697 garbage collecting links. We need to do this on a per function basis to
698 cope with -ffunction-sections. */
700 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
701 /* Switch to the function section, emit the ref to the tables, and
702 switch *back* into the table section. */
703 switch_to_section (function_section (fde->decl));
704 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
705 switch_to_frame_table_section (for_eh, true);
706 #endif
708 /* Pad the FDE out to an address sized boundary. */
709 ASM_OUTPUT_ALIGN (asm_out_file,
710 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
711 ASM_OUTPUT_LABEL (asm_out_file, l2);
713 j += 2;
716 /* Return true if frame description entry FDE is needed for EH. */
718 static bool
719 fde_needed_for_eh_p (dw_fde_ref fde)
721 if (flag_asynchronous_unwind_tables)
722 return true;
724 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
725 return true;
727 if (fde->uses_eh_lsda)
728 return true;
730 /* If exceptions are enabled, we have collected nothrow info. */
731 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
732 return false;
734 return true;
737 /* Output the call frame information used to record information
738 that relates to calculating the frame pointer, and records the
739 location of saved registers. */
741 static void
742 output_call_frame_info (int for_eh)
744 unsigned int i;
745 dw_fde_ref fde;
746 dw_cfi_ref cfi;
747 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
748 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
749 bool any_lsda_needed = false;
750 char augmentation[6];
751 int augmentation_size;
752 int fde_encoding = DW_EH_PE_absptr;
753 int per_encoding = DW_EH_PE_absptr;
754 int lsda_encoding = DW_EH_PE_absptr;
755 int return_reg;
756 rtx personality = NULL;
757 int dw_cie_version;
759 /* Don't emit a CIE if there won't be any FDEs. */
760 if (!fde_vec)
761 return;
763 /* Nothing to do if the assembler's doing it all. */
764 if (dwarf2out_do_cfi_asm ())
765 return;
767 /* If we don't have any functions we'll want to unwind out of, don't emit
768 any EH unwind information. If we make FDEs linkonce, we may have to
769 emit an empty label for an FDE that wouldn't otherwise be emitted. We
770 want to avoid having an FDE kept around when the function it refers to
771 is discarded. Example where this matters: a primary function template
772 in C++ requires EH information, an explicit specialization doesn't. */
773 if (for_eh)
775 bool any_eh_needed = false;
777 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
779 if (fde->uses_eh_lsda)
780 any_eh_needed = any_lsda_needed = true;
781 else if (fde_needed_for_eh_p (fde))
782 any_eh_needed = true;
783 else if (TARGET_USES_WEAK_UNWIND_INFO)
784 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
787 if (!any_eh_needed)
788 return;
791 /* We're going to be generating comments, so turn on app. */
792 if (flag_debug_asm)
793 app_enable ();
795 /* Switch to the proper frame section, first time. */
796 switch_to_frame_table_section (for_eh, false);
798 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
799 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
801 /* Output the CIE. */
802 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
803 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
804 if (!XCOFF_DEBUGGING_INFO || for_eh)
806 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
807 dw2_asm_output_data (4, 0xffffffff,
808 "Initial length escape value indicating 64-bit DWARF extension");
809 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
810 "Length of Common Information Entry");
812 ASM_OUTPUT_LABEL (asm_out_file, l1);
814 /* Now that the CIE pointer is PC-relative for EH,
815 use 0 to identify the CIE. */
816 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
817 (for_eh ? 0 : DWARF_CIE_ID),
818 "CIE Identifier Tag");
820 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
821 use CIE version 1, unless that would produce incorrect results
822 due to overflowing the return register column. */
823 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
824 dw_cie_version = 1;
825 if (return_reg >= 256 || dwarf_version > 2)
826 dw_cie_version = 3;
827 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
829 augmentation[0] = 0;
830 augmentation_size = 0;
832 personality = current_unit_personality;
833 if (for_eh)
835 char *p;
837 /* Augmentation:
838 z Indicates that a uleb128 is present to size the
839 augmentation section.
840 L Indicates the encoding (and thus presence) of
841 an LSDA pointer in the FDE augmentation.
842 R Indicates a non-default pointer encoding for
843 FDE code pointers.
844 P Indicates the presence of an encoding + language
845 personality routine in the CIE augmentation. */
847 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
848 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
849 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
851 p = augmentation + 1;
852 if (personality)
854 *p++ = 'P';
855 augmentation_size += 1 + size_of_encoded_value (per_encoding);
856 assemble_external_libcall (personality);
858 if (any_lsda_needed)
860 *p++ = 'L';
861 augmentation_size += 1;
863 if (fde_encoding != DW_EH_PE_absptr)
865 *p++ = 'R';
866 augmentation_size += 1;
868 if (p > augmentation + 1)
870 augmentation[0] = 'z';
871 *p = '\0';
874 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
875 if (personality && per_encoding == DW_EH_PE_aligned)
877 int offset = ( 4 /* Length */
878 + 4 /* CIE Id */
879 + 1 /* CIE version */
880 + strlen (augmentation) + 1 /* Augmentation */
881 + size_of_uleb128 (1) /* Code alignment */
882 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
883 + 1 /* RA column */
884 + 1 /* Augmentation size */
885 + 1 /* Personality encoding */ );
886 int pad = -offset & (PTR_SIZE - 1);
888 augmentation_size += pad;
890 /* Augmentations should be small, so there's scarce need to
891 iterate for a solution. Die if we exceed one uleb128 byte. */
892 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
896 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
897 if (dw_cie_version >= 4)
899 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
900 dw2_asm_output_data (1, 0, "CIE Segment Size");
902 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
903 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
904 "CIE Data Alignment Factor");
906 if (dw_cie_version == 1)
907 dw2_asm_output_data (1, return_reg, "CIE RA Column");
908 else
909 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
911 if (augmentation[0])
913 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
914 if (personality)
916 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
917 eh_data_format_name (per_encoding));
918 dw2_asm_output_encoded_addr_rtx (per_encoding,
919 personality,
920 true, NULL);
923 if (any_lsda_needed)
924 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
925 eh_data_format_name (lsda_encoding));
927 if (fde_encoding != DW_EH_PE_absptr)
928 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
929 eh_data_format_name (fde_encoding));
932 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
933 output_cfi (cfi, NULL, for_eh);
935 /* Pad the CIE out to an address sized boundary. */
936 ASM_OUTPUT_ALIGN (asm_out_file,
937 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
938 ASM_OUTPUT_LABEL (asm_out_file, l2);
940 /* Loop through all of the FDE's. */
941 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
943 unsigned int k;
945 /* Don't emit EH unwind info for leaf functions that don't need it. */
946 if (for_eh && !fde_needed_for_eh_p (fde))
947 continue;
949 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
950 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
951 augmentation, any_lsda_needed, lsda_encoding);
954 if (for_eh && targetm.terminate_dw2_eh_frame_info)
955 dw2_asm_output_data (4, 0, "End of Table");
957 /* Turn off app to make assembly quicker. */
958 if (flag_debug_asm)
959 app_disable ();
962 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
964 static void
965 dwarf2out_do_cfi_startproc (bool second)
967 int enc;
968 rtx ref;
970 fprintf (asm_out_file, "\t.cfi_startproc\n");
972 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
973 eh unwinders. */
974 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
975 return;
977 rtx personality = get_personality_function (current_function_decl);
979 if (personality)
981 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
982 ref = personality;
984 /* ??? The GAS support isn't entirely consistent. We have to
985 handle indirect support ourselves, but PC-relative is done
986 in the assembler. Further, the assembler can't handle any
987 of the weirder relocation types. */
988 if (enc & DW_EH_PE_indirect)
989 ref = dw2_force_const_mem (ref, true);
991 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
992 output_addr_const (asm_out_file, ref);
993 fputc ('\n', asm_out_file);
996 if (crtl->uses_eh_lsda)
998 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1000 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1001 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1002 current_function_funcdef_no);
1003 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1004 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1006 if (enc & DW_EH_PE_indirect)
1007 ref = dw2_force_const_mem (ref, true);
1009 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1010 output_addr_const (asm_out_file, ref);
1011 fputc ('\n', asm_out_file);
1015 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1016 this allocation may be done before pass_final. */
1018 dw_fde_ref
1019 dwarf2out_alloc_current_fde (void)
1021 dw_fde_ref fde;
1023 fde = ggc_cleared_alloc<dw_fde_node> ();
1024 fde->decl = current_function_decl;
1025 fde->funcdef_number = current_function_funcdef_no;
1026 fde->fde_index = vec_safe_length (fde_vec);
1027 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1028 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1029 fde->nothrow = crtl->nothrow;
1030 fde->drap_reg = INVALID_REGNUM;
1031 fde->vdrap_reg = INVALID_REGNUM;
1033 /* Record the FDE associated with this function. */
1034 cfun->fde = fde;
1035 vec_safe_push (fde_vec, fde);
1037 return fde;
1040 /* Output a marker (i.e. a label) for the beginning of a function, before
1041 the prologue. */
1043 void
1044 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1045 unsigned int column ATTRIBUTE_UNUSED,
1046 const char *file ATTRIBUTE_UNUSED)
1048 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1049 char * dup_label;
1050 dw_fde_ref fde;
1051 section *fnsec;
1052 bool do_frame;
1054 current_function_func_begin_label = NULL;
1056 do_frame = dwarf2out_do_frame ();
1058 /* ??? current_function_func_begin_label is also used by except.c for
1059 call-site information. We must emit this label if it might be used. */
1060 if (!do_frame
1061 && (!flag_exceptions
1062 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1063 return;
1065 fnsec = function_section (current_function_decl);
1066 switch_to_section (fnsec);
1067 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1068 current_function_funcdef_no);
1069 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1070 current_function_funcdef_no);
1071 dup_label = xstrdup (label);
1072 current_function_func_begin_label = dup_label;
1074 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1075 if (!do_frame)
1076 return;
1078 /* Unlike the debug version, the EH version of frame unwind info is a per-
1079 function setting so we need to record whether we need it for the unit. */
1080 do_eh_frame |= dwarf2out_do_eh_frame ();
1082 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1083 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1084 would include pass_dwarf2_frame. If we've not created the FDE yet,
1085 do so now. */
1086 fde = cfun->fde;
1087 if (fde == NULL)
1088 fde = dwarf2out_alloc_current_fde ();
1090 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1091 fde->dw_fde_begin = dup_label;
1092 fde->dw_fde_current_label = dup_label;
1093 fde->in_std_section = (fnsec == text_section
1094 || (cold_text_section && fnsec == cold_text_section));
1096 /* We only want to output line number information for the genuine dwarf2
1097 prologue case, not the eh frame case. */
1098 #ifdef DWARF2_DEBUGGING_INFO
1099 if (file)
1100 dwarf2out_source_line (line, column, file, 0, true);
1101 #endif
1103 if (dwarf2out_do_cfi_asm ())
1104 dwarf2out_do_cfi_startproc (false);
1105 else
1107 rtx personality = get_personality_function (current_function_decl);
1108 if (!current_unit_personality)
1109 current_unit_personality = personality;
1111 /* We cannot keep a current personality per function as without CFI
1112 asm, at the point where we emit the CFI data, there is no current
1113 function anymore. */
1114 if (personality && current_unit_personality != personality)
1115 sorry ("multiple EH personalities are supported only with assemblers "
1116 "supporting .cfi_personality directive");
1120 /* Output a marker (i.e. a label) for the end of the generated code
1121 for a function prologue. This gets called *after* the prologue code has
1122 been generated. */
1124 void
1125 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1126 const char *file ATTRIBUTE_UNUSED)
1128 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1130 /* Output a label to mark the endpoint of the code generated for this
1131 function. */
1132 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1133 current_function_funcdef_no);
1134 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1135 current_function_funcdef_no);
1136 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1139 /* Output a marker (i.e. a label) for the beginning of the generated code
1140 for a function epilogue. This gets called *before* the prologue code has
1141 been generated. */
1143 void
1144 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1145 const char *file ATTRIBUTE_UNUSED)
1147 dw_fde_ref fde = cfun->fde;
1148 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1150 if (fde->dw_fde_vms_begin_epilogue)
1151 return;
1153 /* Output a label to mark the endpoint of the code generated for this
1154 function. */
1155 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1156 current_function_funcdef_no);
1157 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1158 current_function_funcdef_no);
1159 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1162 /* Output a marker (i.e. a label) for the absolute end of the generated code
1163 for a function definition. This gets called *after* the epilogue code has
1164 been generated. */
1166 void
1167 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1168 const char *file ATTRIBUTE_UNUSED)
1170 dw_fde_ref fde;
1171 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1173 last_var_location_insn = NULL;
1174 cached_next_real_insn = NULL;
1176 if (dwarf2out_do_cfi_asm ())
1177 fprintf (asm_out_file, "\t.cfi_endproc\n");
1179 /* Output a label to mark the endpoint of the code generated for this
1180 function. */
1181 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1182 current_function_funcdef_no);
1183 ASM_OUTPUT_LABEL (asm_out_file, label);
1184 fde = cfun->fde;
1185 gcc_assert (fde != NULL);
1186 if (fde->dw_fde_second_begin == NULL)
1187 fde->dw_fde_end = xstrdup (label);
1190 void
1191 dwarf2out_frame_finish (void)
1193 /* Output call frame information. */
1194 if (targetm.debug_unwind_info () == UI_DWARF2)
1195 output_call_frame_info (0);
1197 /* Output another copy for the unwinder. */
1198 if (do_eh_frame)
1199 output_call_frame_info (1);
1202 /* Note that the current function section is being used for code. */
1204 static void
1205 dwarf2out_note_section_used (void)
1207 section *sec = current_function_section ();
1208 if (sec == text_section)
1209 text_section_used = true;
1210 else if (sec == cold_text_section)
1211 cold_text_section_used = true;
1214 static void var_location_switch_text_section (void);
1215 static void set_cur_line_info_table (section *);
1217 void
1218 dwarf2out_switch_text_section (void)
1220 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1221 section *sect;
1222 dw_fde_ref fde = cfun->fde;
1224 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1226 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1227 current_function_funcdef_no);
1229 fde->dw_fde_second_begin = ggc_strdup (label);
1230 if (!in_cold_section_p)
1232 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1233 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1235 else
1237 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1238 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1240 have_multiple_function_sections = true;
1242 /* There is no need to mark used sections when not debugging. */
1243 if (cold_text_section != NULL)
1244 dwarf2out_note_section_used ();
1246 if (dwarf2out_do_cfi_asm ())
1247 fprintf (asm_out_file, "\t.cfi_endproc\n");
1249 /* Now do the real section switch. */
1250 sect = current_function_section ();
1251 switch_to_section (sect);
1253 fde->second_in_std_section
1254 = (sect == text_section
1255 || (cold_text_section && sect == cold_text_section));
1257 if (dwarf2out_do_cfi_asm ())
1258 dwarf2out_do_cfi_startproc (true);
1260 var_location_switch_text_section ();
1262 if (cold_text_section != NULL)
1263 set_cur_line_info_table (sect);
1266 /* And now, the subset of the debugging information support code necessary
1267 for emitting location expressions. */
1269 /* Data about a single source file. */
1270 struct GTY((for_user)) dwarf_file_data {
1271 const char * filename;
1272 int emitted_number;
1275 /* Describe an entry into the .debug_addr section. */
1277 enum ate_kind {
1278 ate_kind_rtx,
1279 ate_kind_rtx_dtprel,
1280 ate_kind_label
1283 struct GTY((for_user)) addr_table_entry {
1284 enum ate_kind kind;
1285 unsigned int refcount;
1286 unsigned int index;
1287 union addr_table_entry_struct_union
1289 rtx GTY ((tag ("0"))) rtl;
1290 char * GTY ((tag ("1"))) label;
1292 GTY ((desc ("%1.kind"))) addr;
1295 typedef unsigned int var_loc_view;
1297 /* Location lists are ranges + location descriptions for that range,
1298 so you can track variables that are in different places over
1299 their entire life. */
1300 typedef struct GTY(()) dw_loc_list_struct {
1301 dw_loc_list_ref dw_loc_next;
1302 const char *begin; /* Label and addr_entry for start of range */
1303 addr_table_entry *begin_entry;
1304 const char *end; /* Label for end of range */
1305 char *ll_symbol; /* Label for beginning of location list.
1306 Only on head of list. */
1307 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1308 const char *section; /* Section this loclist is relative to */
1309 dw_loc_descr_ref expr;
1310 var_loc_view vbegin, vend;
1311 hashval_t hash;
1312 /* True if all addresses in this and subsequent lists are known to be
1313 resolved. */
1314 bool resolved_addr;
1315 /* True if this list has been replaced by dw_loc_next. */
1316 bool replaced;
1317 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1318 section. */
1319 unsigned char emitted : 1;
1320 /* True if hash field is index rather than hash value. */
1321 unsigned char num_assigned : 1;
1322 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1323 unsigned char offset_emitted : 1;
1324 /* True if note_variable_value_in_expr has been called on it. */
1325 unsigned char noted_variable_value : 1;
1326 /* True if the range should be emitted even if begin and end
1327 are the same. */
1328 bool force;
1329 } dw_loc_list_node;
1331 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1332 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1334 /* Convert a DWARF stack opcode into its string name. */
1336 static const char *
1337 dwarf_stack_op_name (unsigned int op)
1339 const char *name = get_DW_OP_name (op);
1341 if (name != NULL)
1342 return name;
1344 return "OP_<unknown>";
1347 /* Return TRUE iff we're to output location view lists as a separate
1348 attribute next to the location lists, as an extension compatible
1349 with DWARF 2 and above. */
1351 static inline bool
1352 dwarf2out_locviews_in_attribute ()
1354 return debug_variable_location_views == 1;
1357 /* Return TRUE iff we're to output location view lists as part of the
1358 location lists, as proposed for standardization after DWARF 5. */
1360 static inline bool
1361 dwarf2out_locviews_in_loclist ()
1363 #ifndef DW_LLE_view_pair
1364 return false;
1365 #else
1366 return debug_variable_location_views == -1;
1367 #endif
1370 /* Return a pointer to a newly allocated location description. Location
1371 descriptions are simple expression terms that can be strung
1372 together to form more complicated location (address) descriptions. */
1374 static inline dw_loc_descr_ref
1375 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1376 unsigned HOST_WIDE_INT oprnd2)
1378 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1380 descr->dw_loc_opc = op;
1381 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1382 descr->dw_loc_oprnd1.val_entry = NULL;
1383 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1384 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1385 descr->dw_loc_oprnd2.val_entry = NULL;
1386 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1388 return descr;
1391 /* Add a location description term to a location description expression. */
1393 static inline void
1394 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1396 dw_loc_descr_ref *d;
1398 /* Find the end of the chain. */
1399 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1402 *d = descr;
1405 /* Compare two location operands for exact equality. */
1407 static bool
1408 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1410 if (a->val_class != b->val_class)
1411 return false;
1412 switch (a->val_class)
1414 case dw_val_class_none:
1415 return true;
1416 case dw_val_class_addr:
1417 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1419 case dw_val_class_offset:
1420 case dw_val_class_unsigned_const:
1421 case dw_val_class_const:
1422 case dw_val_class_unsigned_const_implicit:
1423 case dw_val_class_const_implicit:
1424 case dw_val_class_range_list:
1425 /* These are all HOST_WIDE_INT, signed or unsigned. */
1426 return a->v.val_unsigned == b->v.val_unsigned;
1428 case dw_val_class_loc:
1429 return a->v.val_loc == b->v.val_loc;
1430 case dw_val_class_loc_list:
1431 return a->v.val_loc_list == b->v.val_loc_list;
1432 case dw_val_class_view_list:
1433 return a->v.val_view_list == b->v.val_view_list;
1434 case dw_val_class_die_ref:
1435 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1436 case dw_val_class_fde_ref:
1437 return a->v.val_fde_index == b->v.val_fde_index;
1438 case dw_val_class_symview:
1439 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1440 case dw_val_class_lbl_id:
1441 case dw_val_class_lineptr:
1442 case dw_val_class_macptr:
1443 case dw_val_class_loclistsptr:
1444 case dw_val_class_high_pc:
1445 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1446 case dw_val_class_str:
1447 return a->v.val_str == b->v.val_str;
1448 case dw_val_class_flag:
1449 return a->v.val_flag == b->v.val_flag;
1450 case dw_val_class_file:
1451 case dw_val_class_file_implicit:
1452 return a->v.val_file == b->v.val_file;
1453 case dw_val_class_decl_ref:
1454 return a->v.val_decl_ref == b->v.val_decl_ref;
1456 case dw_val_class_const_double:
1457 return (a->v.val_double.high == b->v.val_double.high
1458 && a->v.val_double.low == b->v.val_double.low);
1460 case dw_val_class_wide_int:
1461 return *a->v.val_wide == *b->v.val_wide;
1463 case dw_val_class_vec:
1465 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1466 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1468 return (a_len == b_len
1469 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1472 case dw_val_class_data8:
1473 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1475 case dw_val_class_vms_delta:
1476 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1477 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1479 case dw_val_class_discr_value:
1480 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1481 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1482 case dw_val_class_discr_list:
1483 /* It makes no sense comparing two discriminant value lists. */
1484 return false;
1486 gcc_unreachable ();
1489 /* Compare two location atoms for exact equality. */
1491 static bool
1492 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1494 if (a->dw_loc_opc != b->dw_loc_opc)
1495 return false;
1497 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1498 address size, but since we always allocate cleared storage it
1499 should be zero for other types of locations. */
1500 if (a->dtprel != b->dtprel)
1501 return false;
1503 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1504 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1507 /* Compare two complete location expressions for exact equality. */
1509 bool
1510 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1512 while (1)
1514 if (a == b)
1515 return true;
1516 if (a == NULL || b == NULL)
1517 return false;
1518 if (!loc_descr_equal_p_1 (a, b))
1519 return false;
1521 a = a->dw_loc_next;
1522 b = b->dw_loc_next;
1527 /* Add a constant POLY_OFFSET to a location expression. */
1529 static void
1530 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1532 dw_loc_descr_ref loc;
1533 HOST_WIDE_INT *p;
1535 gcc_assert (*list_head != NULL);
1537 if (known_eq (poly_offset, 0))
1538 return;
1540 /* Find the end of the chain. */
1541 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1544 HOST_WIDE_INT offset;
1545 if (!poly_offset.is_constant (&offset))
1547 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1548 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1549 return;
1552 p = NULL;
1553 if (loc->dw_loc_opc == DW_OP_fbreg
1554 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1555 p = &loc->dw_loc_oprnd1.v.val_int;
1556 else if (loc->dw_loc_opc == DW_OP_bregx)
1557 p = &loc->dw_loc_oprnd2.v.val_int;
1559 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1560 offset. Don't optimize if an signed integer overflow would happen. */
1561 if (p != NULL
1562 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1563 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1564 *p += offset;
1566 else if (offset > 0)
1567 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1569 else
1571 loc->dw_loc_next
1572 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1573 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1577 /* Return a pointer to a newly allocated location description for
1578 REG and OFFSET. */
1580 static inline dw_loc_descr_ref
1581 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1583 HOST_WIDE_INT const_offset;
1584 if (offset.is_constant (&const_offset))
1586 if (reg <= 31)
1587 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1588 const_offset, 0);
1589 else
1590 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1592 else
1594 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1595 loc_descr_plus_const (&ret, offset);
1596 return ret;
1600 /* Add a constant OFFSET to a location list. */
1602 static void
1603 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1605 dw_loc_list_ref d;
1606 for (d = list_head; d != NULL; d = d->dw_loc_next)
1607 loc_descr_plus_const (&d->expr, offset);
1610 #define DWARF_REF_SIZE \
1611 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1613 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1614 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1615 DW_FORM_data16 with 128 bits. */
1616 #define DWARF_LARGEST_DATA_FORM_BITS \
1617 (dwarf_version >= 5 ? 128 : 64)
1619 /* Utility inline function for construction of ops that were GNU extension
1620 before DWARF 5. */
1621 static inline enum dwarf_location_atom
1622 dwarf_OP (enum dwarf_location_atom op)
1624 switch (op)
1626 case DW_OP_implicit_pointer:
1627 if (dwarf_version < 5)
1628 return DW_OP_GNU_implicit_pointer;
1629 break;
1631 case DW_OP_entry_value:
1632 if (dwarf_version < 5)
1633 return DW_OP_GNU_entry_value;
1634 break;
1636 case DW_OP_const_type:
1637 if (dwarf_version < 5)
1638 return DW_OP_GNU_const_type;
1639 break;
1641 case DW_OP_regval_type:
1642 if (dwarf_version < 5)
1643 return DW_OP_GNU_regval_type;
1644 break;
1646 case DW_OP_deref_type:
1647 if (dwarf_version < 5)
1648 return DW_OP_GNU_deref_type;
1649 break;
1651 case DW_OP_convert:
1652 if (dwarf_version < 5)
1653 return DW_OP_GNU_convert;
1654 break;
1656 case DW_OP_reinterpret:
1657 if (dwarf_version < 5)
1658 return DW_OP_GNU_reinterpret;
1659 break;
1661 case DW_OP_addrx:
1662 if (dwarf_version < 5)
1663 return DW_OP_GNU_addr_index;
1664 break;
1666 case DW_OP_constx:
1667 if (dwarf_version < 5)
1668 return DW_OP_GNU_const_index;
1669 break;
1671 default:
1672 break;
1674 return op;
1677 /* Similarly for attributes. */
1678 static inline enum dwarf_attribute
1679 dwarf_AT (enum dwarf_attribute at)
1681 switch (at)
1683 case DW_AT_call_return_pc:
1684 if (dwarf_version < 5)
1685 return DW_AT_low_pc;
1686 break;
1688 case DW_AT_call_tail_call:
1689 if (dwarf_version < 5)
1690 return DW_AT_GNU_tail_call;
1691 break;
1693 case DW_AT_call_origin:
1694 if (dwarf_version < 5)
1695 return DW_AT_abstract_origin;
1696 break;
1698 case DW_AT_call_target:
1699 if (dwarf_version < 5)
1700 return DW_AT_GNU_call_site_target;
1701 break;
1703 case DW_AT_call_target_clobbered:
1704 if (dwarf_version < 5)
1705 return DW_AT_GNU_call_site_target_clobbered;
1706 break;
1708 case DW_AT_call_parameter:
1709 if (dwarf_version < 5)
1710 return DW_AT_abstract_origin;
1711 break;
1713 case DW_AT_call_value:
1714 if (dwarf_version < 5)
1715 return DW_AT_GNU_call_site_value;
1716 break;
1718 case DW_AT_call_data_value:
1719 if (dwarf_version < 5)
1720 return DW_AT_GNU_call_site_data_value;
1721 break;
1723 case DW_AT_call_all_calls:
1724 if (dwarf_version < 5)
1725 return DW_AT_GNU_all_call_sites;
1726 break;
1728 case DW_AT_call_all_tail_calls:
1729 if (dwarf_version < 5)
1730 return DW_AT_GNU_all_tail_call_sites;
1731 break;
1733 case DW_AT_dwo_name:
1734 if (dwarf_version < 5)
1735 return DW_AT_GNU_dwo_name;
1736 break;
1738 case DW_AT_addr_base:
1739 if (dwarf_version < 5)
1740 return DW_AT_GNU_addr_base;
1741 break;
1743 default:
1744 break;
1746 return at;
1749 /* And similarly for tags. */
1750 static inline enum dwarf_tag
1751 dwarf_TAG (enum dwarf_tag tag)
1753 switch (tag)
1755 case DW_TAG_call_site:
1756 if (dwarf_version < 5)
1757 return DW_TAG_GNU_call_site;
1758 break;
1760 case DW_TAG_call_site_parameter:
1761 if (dwarf_version < 5)
1762 return DW_TAG_GNU_call_site_parameter;
1763 break;
1765 default:
1766 break;
1768 return tag;
1771 /* And similarly for forms. */
1772 static inline enum dwarf_form
1773 dwarf_FORM (enum dwarf_form form)
1775 switch (form)
1777 case DW_FORM_addrx:
1778 if (dwarf_version < 5)
1779 return DW_FORM_GNU_addr_index;
1780 break;
1782 case DW_FORM_strx:
1783 if (dwarf_version < 5)
1784 return DW_FORM_GNU_str_index;
1785 break;
1787 default:
1788 break;
1790 return form;
1793 static unsigned long int get_base_type_offset (dw_die_ref);
1795 /* Return the size of a location descriptor. */
1797 static unsigned long
1798 size_of_loc_descr (dw_loc_descr_ref loc)
1800 unsigned long size = 1;
1802 switch (loc->dw_loc_opc)
1804 case DW_OP_addr:
1805 size += DWARF2_ADDR_SIZE;
1806 break;
1807 case DW_OP_GNU_addr_index:
1808 case DW_OP_addrx:
1809 case DW_OP_GNU_const_index:
1810 case DW_OP_constx:
1811 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1812 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1813 break;
1814 case DW_OP_const1u:
1815 case DW_OP_const1s:
1816 size += 1;
1817 break;
1818 case DW_OP_const2u:
1819 case DW_OP_const2s:
1820 size += 2;
1821 break;
1822 case DW_OP_const4u:
1823 case DW_OP_const4s:
1824 size += 4;
1825 break;
1826 case DW_OP_const8u:
1827 case DW_OP_const8s:
1828 size += 8;
1829 break;
1830 case DW_OP_constu:
1831 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1832 break;
1833 case DW_OP_consts:
1834 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1835 break;
1836 case DW_OP_pick:
1837 size += 1;
1838 break;
1839 case DW_OP_plus_uconst:
1840 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1841 break;
1842 case DW_OP_skip:
1843 case DW_OP_bra:
1844 size += 2;
1845 break;
1846 case DW_OP_breg0:
1847 case DW_OP_breg1:
1848 case DW_OP_breg2:
1849 case DW_OP_breg3:
1850 case DW_OP_breg4:
1851 case DW_OP_breg5:
1852 case DW_OP_breg6:
1853 case DW_OP_breg7:
1854 case DW_OP_breg8:
1855 case DW_OP_breg9:
1856 case DW_OP_breg10:
1857 case DW_OP_breg11:
1858 case DW_OP_breg12:
1859 case DW_OP_breg13:
1860 case DW_OP_breg14:
1861 case DW_OP_breg15:
1862 case DW_OP_breg16:
1863 case DW_OP_breg17:
1864 case DW_OP_breg18:
1865 case DW_OP_breg19:
1866 case DW_OP_breg20:
1867 case DW_OP_breg21:
1868 case DW_OP_breg22:
1869 case DW_OP_breg23:
1870 case DW_OP_breg24:
1871 case DW_OP_breg25:
1872 case DW_OP_breg26:
1873 case DW_OP_breg27:
1874 case DW_OP_breg28:
1875 case DW_OP_breg29:
1876 case DW_OP_breg30:
1877 case DW_OP_breg31:
1878 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1879 break;
1880 case DW_OP_regx:
1881 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1882 break;
1883 case DW_OP_fbreg:
1884 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1885 break;
1886 case DW_OP_bregx:
1887 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1888 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1889 break;
1890 case DW_OP_piece:
1891 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1892 break;
1893 case DW_OP_bit_piece:
1894 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1895 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1896 break;
1897 case DW_OP_deref_size:
1898 case DW_OP_xderef_size:
1899 size += 1;
1900 break;
1901 case DW_OP_call2:
1902 size += 2;
1903 break;
1904 case DW_OP_call4:
1905 size += 4;
1906 break;
1907 case DW_OP_call_ref:
1908 case DW_OP_GNU_variable_value:
1909 size += DWARF_REF_SIZE;
1910 break;
1911 case DW_OP_implicit_value:
1912 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1913 + loc->dw_loc_oprnd1.v.val_unsigned;
1914 break;
1915 case DW_OP_implicit_pointer:
1916 case DW_OP_GNU_implicit_pointer:
1917 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1918 break;
1919 case DW_OP_entry_value:
1920 case DW_OP_GNU_entry_value:
1922 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1923 size += size_of_uleb128 (op_size) + op_size;
1924 break;
1926 case DW_OP_const_type:
1927 case DW_OP_GNU_const_type:
1929 unsigned long o
1930 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1931 size += size_of_uleb128 (o) + 1;
1932 switch (loc->dw_loc_oprnd2.val_class)
1934 case dw_val_class_vec:
1935 size += loc->dw_loc_oprnd2.v.val_vec.length
1936 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1937 break;
1938 case dw_val_class_const:
1939 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1940 break;
1941 case dw_val_class_const_double:
1942 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1943 break;
1944 case dw_val_class_wide_int:
1945 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1946 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1947 break;
1948 default:
1949 gcc_unreachable ();
1951 break;
1953 case DW_OP_regval_type:
1954 case DW_OP_GNU_regval_type:
1956 unsigned long o
1957 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1958 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1959 + size_of_uleb128 (o);
1961 break;
1962 case DW_OP_deref_type:
1963 case DW_OP_GNU_deref_type:
1965 unsigned long o
1966 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1967 size += 1 + size_of_uleb128 (o);
1969 break;
1970 case DW_OP_convert:
1971 case DW_OP_reinterpret:
1972 case DW_OP_GNU_convert:
1973 case DW_OP_GNU_reinterpret:
1974 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1975 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1976 else
1978 unsigned long o
1979 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1980 size += size_of_uleb128 (o);
1982 break;
1983 case DW_OP_GNU_parameter_ref:
1984 size += 4;
1985 break;
1986 default:
1987 break;
1990 return size;
1993 /* Return the size of a series of location descriptors. */
1995 unsigned long
1996 size_of_locs (dw_loc_descr_ref loc)
1998 dw_loc_descr_ref l;
1999 unsigned long size;
2001 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2002 field, to avoid writing to a PCH file. */
2003 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2005 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2006 break;
2007 size += size_of_loc_descr (l);
2009 if (! l)
2010 return size;
2012 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2014 l->dw_loc_addr = size;
2015 size += size_of_loc_descr (l);
2018 return size;
2021 /* Return the size of the value in a DW_AT_discr_value attribute. */
2023 static int
2024 size_of_discr_value (dw_discr_value *discr_value)
2026 if (discr_value->pos)
2027 return size_of_uleb128 (discr_value->v.uval);
2028 else
2029 return size_of_sleb128 (discr_value->v.sval);
2032 /* Return the size of the value in a DW_AT_discr_list attribute. */
2034 static int
2035 size_of_discr_list (dw_discr_list_ref discr_list)
2037 int size = 0;
2039 for (dw_discr_list_ref list = discr_list;
2040 list != NULL;
2041 list = list->dw_discr_next)
2043 /* One byte for the discriminant value descriptor, and then one or two
2044 LEB128 numbers, depending on whether it's a single case label or a
2045 range label. */
2046 size += 1;
2047 size += size_of_discr_value (&list->dw_discr_lower_bound);
2048 if (list->dw_discr_range != 0)
2049 size += size_of_discr_value (&list->dw_discr_upper_bound);
2051 return size;
2054 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2055 static void get_ref_die_offset_label (char *, dw_die_ref);
2056 static unsigned long int get_ref_die_offset (dw_die_ref);
2058 /* Output location description stack opcode's operands (if any).
2059 The for_eh_or_skip parameter controls whether register numbers are
2060 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2061 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2062 info). This should be suppressed for the cases that have not been converted
2063 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2065 static void
2066 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2068 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2069 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2071 switch (loc->dw_loc_opc)
2073 #ifdef DWARF2_DEBUGGING_INFO
2074 case DW_OP_const2u:
2075 case DW_OP_const2s:
2076 dw2_asm_output_data (2, val1->v.val_int, NULL);
2077 break;
2078 case DW_OP_const4u:
2079 if (loc->dtprel)
2081 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2082 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2083 val1->v.val_addr);
2084 fputc ('\n', asm_out_file);
2085 break;
2087 /* FALLTHRU */
2088 case DW_OP_const4s:
2089 dw2_asm_output_data (4, val1->v.val_int, NULL);
2090 break;
2091 case DW_OP_const8u:
2092 if (loc->dtprel)
2094 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2095 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2096 val1->v.val_addr);
2097 fputc ('\n', asm_out_file);
2098 break;
2100 /* FALLTHRU */
2101 case DW_OP_const8s:
2102 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2103 dw2_asm_output_data (8, val1->v.val_int, NULL);
2104 break;
2105 case DW_OP_skip:
2106 case DW_OP_bra:
2108 int offset;
2110 gcc_assert (val1->val_class == dw_val_class_loc);
2111 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2113 dw2_asm_output_data (2, offset, NULL);
2115 break;
2116 case DW_OP_implicit_value:
2117 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2118 switch (val2->val_class)
2120 case dw_val_class_const:
2121 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2122 break;
2123 case dw_val_class_vec:
2125 unsigned int elt_size = val2->v.val_vec.elt_size;
2126 unsigned int len = val2->v.val_vec.length;
2127 unsigned int i;
2128 unsigned char *p;
2130 if (elt_size > sizeof (HOST_WIDE_INT))
2132 elt_size /= 2;
2133 len *= 2;
2135 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2136 i < len;
2137 i++, p += elt_size)
2138 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2139 "fp or vector constant word %u", i);
2141 break;
2142 case dw_val_class_const_double:
2144 unsigned HOST_WIDE_INT first, second;
2146 if (WORDS_BIG_ENDIAN)
2148 first = val2->v.val_double.high;
2149 second = val2->v.val_double.low;
2151 else
2153 first = val2->v.val_double.low;
2154 second = val2->v.val_double.high;
2156 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2157 first, NULL);
2158 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2159 second, NULL);
2161 break;
2162 case dw_val_class_wide_int:
2164 int i;
2165 int len = get_full_len (*val2->v.val_wide);
2166 if (WORDS_BIG_ENDIAN)
2167 for (i = len - 1; i >= 0; --i)
2168 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2169 val2->v.val_wide->elt (i), NULL);
2170 else
2171 for (i = 0; i < len; ++i)
2172 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2173 val2->v.val_wide->elt (i), NULL);
2175 break;
2176 case dw_val_class_addr:
2177 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2178 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2179 break;
2180 default:
2181 gcc_unreachable ();
2183 break;
2184 #else
2185 case DW_OP_const2u:
2186 case DW_OP_const2s:
2187 case DW_OP_const4u:
2188 case DW_OP_const4s:
2189 case DW_OP_const8u:
2190 case DW_OP_const8s:
2191 case DW_OP_skip:
2192 case DW_OP_bra:
2193 case DW_OP_implicit_value:
2194 /* We currently don't make any attempt to make sure these are
2195 aligned properly like we do for the main unwind info, so
2196 don't support emitting things larger than a byte if we're
2197 only doing unwinding. */
2198 gcc_unreachable ();
2199 #endif
2200 case DW_OP_const1u:
2201 case DW_OP_const1s:
2202 dw2_asm_output_data (1, val1->v.val_int, NULL);
2203 break;
2204 case DW_OP_constu:
2205 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2206 break;
2207 case DW_OP_consts:
2208 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2209 break;
2210 case DW_OP_pick:
2211 dw2_asm_output_data (1, val1->v.val_int, NULL);
2212 break;
2213 case DW_OP_plus_uconst:
2214 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2215 break;
2216 case DW_OP_breg0:
2217 case DW_OP_breg1:
2218 case DW_OP_breg2:
2219 case DW_OP_breg3:
2220 case DW_OP_breg4:
2221 case DW_OP_breg5:
2222 case DW_OP_breg6:
2223 case DW_OP_breg7:
2224 case DW_OP_breg8:
2225 case DW_OP_breg9:
2226 case DW_OP_breg10:
2227 case DW_OP_breg11:
2228 case DW_OP_breg12:
2229 case DW_OP_breg13:
2230 case DW_OP_breg14:
2231 case DW_OP_breg15:
2232 case DW_OP_breg16:
2233 case DW_OP_breg17:
2234 case DW_OP_breg18:
2235 case DW_OP_breg19:
2236 case DW_OP_breg20:
2237 case DW_OP_breg21:
2238 case DW_OP_breg22:
2239 case DW_OP_breg23:
2240 case DW_OP_breg24:
2241 case DW_OP_breg25:
2242 case DW_OP_breg26:
2243 case DW_OP_breg27:
2244 case DW_OP_breg28:
2245 case DW_OP_breg29:
2246 case DW_OP_breg30:
2247 case DW_OP_breg31:
2248 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2249 break;
2250 case DW_OP_regx:
2252 unsigned r = val1->v.val_unsigned;
2253 if (for_eh_or_skip >= 0)
2254 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2255 gcc_assert (size_of_uleb128 (r)
2256 == size_of_uleb128 (val1->v.val_unsigned));
2257 dw2_asm_output_data_uleb128 (r, NULL);
2259 break;
2260 case DW_OP_fbreg:
2261 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2262 break;
2263 case DW_OP_bregx:
2265 unsigned r = val1->v.val_unsigned;
2266 if (for_eh_or_skip >= 0)
2267 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2268 gcc_assert (size_of_uleb128 (r)
2269 == size_of_uleb128 (val1->v.val_unsigned));
2270 dw2_asm_output_data_uleb128 (r, NULL);
2271 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2273 break;
2274 case DW_OP_piece:
2275 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2276 break;
2277 case DW_OP_bit_piece:
2278 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2279 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2280 break;
2281 case DW_OP_deref_size:
2282 case DW_OP_xderef_size:
2283 dw2_asm_output_data (1, val1->v.val_int, NULL);
2284 break;
2286 case DW_OP_addr:
2287 if (loc->dtprel)
2289 if (targetm.asm_out.output_dwarf_dtprel)
2291 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2292 DWARF2_ADDR_SIZE,
2293 val1->v.val_addr);
2294 fputc ('\n', asm_out_file);
2296 else
2297 gcc_unreachable ();
2299 else
2301 #ifdef DWARF2_DEBUGGING_INFO
2302 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2303 #else
2304 gcc_unreachable ();
2305 #endif
2307 break;
2309 case DW_OP_GNU_addr_index:
2310 case DW_OP_addrx:
2311 case DW_OP_GNU_const_index:
2312 case DW_OP_constx:
2313 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2314 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2315 "(index into .debug_addr)");
2316 break;
2318 case DW_OP_call2:
2319 case DW_OP_call4:
2321 unsigned long die_offset
2322 = get_ref_die_offset (val1->v.val_die_ref.die);
2323 /* Make sure the offset has been computed and that we can encode it as
2324 an operand. */
2325 gcc_assert (die_offset > 0
2326 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2327 ? 0xffff
2328 : 0xffffffff));
2329 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2330 die_offset, NULL);
2332 break;
2334 case DW_OP_call_ref:
2335 case DW_OP_GNU_variable_value:
2337 char label[MAX_ARTIFICIAL_LABEL_BYTES
2338 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2339 gcc_assert (val1->val_class == dw_val_class_die_ref);
2340 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2341 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2343 break;
2345 case DW_OP_implicit_pointer:
2346 case DW_OP_GNU_implicit_pointer:
2348 char label[MAX_ARTIFICIAL_LABEL_BYTES
2349 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2350 gcc_assert (val1->val_class == dw_val_class_die_ref);
2351 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2352 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2353 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2355 break;
2357 case DW_OP_entry_value:
2358 case DW_OP_GNU_entry_value:
2359 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2360 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2361 break;
2363 case DW_OP_const_type:
2364 case DW_OP_GNU_const_type:
2366 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2367 gcc_assert (o);
2368 dw2_asm_output_data_uleb128 (o, NULL);
2369 switch (val2->val_class)
2371 case dw_val_class_const:
2372 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2373 dw2_asm_output_data (1, l, NULL);
2374 dw2_asm_output_data (l, val2->v.val_int, NULL);
2375 break;
2376 case dw_val_class_vec:
2378 unsigned int elt_size = val2->v.val_vec.elt_size;
2379 unsigned int len = val2->v.val_vec.length;
2380 unsigned int i;
2381 unsigned char *p;
2383 l = len * elt_size;
2384 dw2_asm_output_data (1, l, NULL);
2385 if (elt_size > sizeof (HOST_WIDE_INT))
2387 elt_size /= 2;
2388 len *= 2;
2390 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2391 i < len;
2392 i++, p += elt_size)
2393 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2394 "fp or vector constant word %u", i);
2396 break;
2397 case dw_val_class_const_double:
2399 unsigned HOST_WIDE_INT first, second;
2400 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2402 dw2_asm_output_data (1, 2 * l, NULL);
2403 if (WORDS_BIG_ENDIAN)
2405 first = val2->v.val_double.high;
2406 second = val2->v.val_double.low;
2408 else
2410 first = val2->v.val_double.low;
2411 second = val2->v.val_double.high;
2413 dw2_asm_output_data (l, first, NULL);
2414 dw2_asm_output_data (l, second, NULL);
2416 break;
2417 case dw_val_class_wide_int:
2419 int i;
2420 int len = get_full_len (*val2->v.val_wide);
2421 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2423 dw2_asm_output_data (1, len * l, NULL);
2424 if (WORDS_BIG_ENDIAN)
2425 for (i = len - 1; i >= 0; --i)
2426 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2427 else
2428 for (i = 0; i < len; ++i)
2429 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2431 break;
2432 default:
2433 gcc_unreachable ();
2436 break;
2437 case DW_OP_regval_type:
2438 case DW_OP_GNU_regval_type:
2440 unsigned r = val1->v.val_unsigned;
2441 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2442 gcc_assert (o);
2443 if (for_eh_or_skip >= 0)
2445 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2446 gcc_assert (size_of_uleb128 (r)
2447 == size_of_uleb128 (val1->v.val_unsigned));
2449 dw2_asm_output_data_uleb128 (r, NULL);
2450 dw2_asm_output_data_uleb128 (o, NULL);
2452 break;
2453 case DW_OP_deref_type:
2454 case DW_OP_GNU_deref_type:
2456 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2457 gcc_assert (o);
2458 dw2_asm_output_data (1, val1->v.val_int, NULL);
2459 dw2_asm_output_data_uleb128 (o, NULL);
2461 break;
2462 case DW_OP_convert:
2463 case DW_OP_reinterpret:
2464 case DW_OP_GNU_convert:
2465 case DW_OP_GNU_reinterpret:
2466 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2467 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2468 else
2470 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2471 gcc_assert (o);
2472 dw2_asm_output_data_uleb128 (o, NULL);
2474 break;
2476 case DW_OP_GNU_parameter_ref:
2478 unsigned long o;
2479 gcc_assert (val1->val_class == dw_val_class_die_ref);
2480 o = get_ref_die_offset (val1->v.val_die_ref.die);
2481 dw2_asm_output_data (4, o, NULL);
2483 break;
2485 default:
2486 /* Other codes have no operands. */
2487 break;
2491 /* Output a sequence of location operations.
2492 The for_eh_or_skip parameter controls whether register numbers are
2493 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2494 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2495 info). This should be suppressed for the cases that have not been converted
2496 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2498 void
2499 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2501 for (; loc != NULL; loc = loc->dw_loc_next)
2503 enum dwarf_location_atom opc = loc->dw_loc_opc;
2504 /* Output the opcode. */
2505 if (for_eh_or_skip >= 0
2506 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2508 unsigned r = (opc - DW_OP_breg0);
2509 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2510 gcc_assert (r <= 31);
2511 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2513 else if (for_eh_or_skip >= 0
2514 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2516 unsigned r = (opc - DW_OP_reg0);
2517 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2518 gcc_assert (r <= 31);
2519 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2522 dw2_asm_output_data (1, opc,
2523 "%s", dwarf_stack_op_name (opc));
2525 /* Output the operand(s) (if any). */
2526 output_loc_operands (loc, for_eh_or_skip);
2530 /* Output location description stack opcode's operands (if any).
2531 The output is single bytes on a line, suitable for .cfi_escape. */
2533 static void
2534 output_loc_operands_raw (dw_loc_descr_ref loc)
2536 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2537 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2539 switch (loc->dw_loc_opc)
2541 case DW_OP_addr:
2542 case DW_OP_GNU_addr_index:
2543 case DW_OP_addrx:
2544 case DW_OP_GNU_const_index:
2545 case DW_OP_constx:
2546 case DW_OP_implicit_value:
2547 /* We cannot output addresses in .cfi_escape, only bytes. */
2548 gcc_unreachable ();
2550 case DW_OP_const1u:
2551 case DW_OP_const1s:
2552 case DW_OP_pick:
2553 case DW_OP_deref_size:
2554 case DW_OP_xderef_size:
2555 fputc (',', asm_out_file);
2556 dw2_asm_output_data_raw (1, val1->v.val_int);
2557 break;
2559 case DW_OP_const2u:
2560 case DW_OP_const2s:
2561 fputc (',', asm_out_file);
2562 dw2_asm_output_data_raw (2, val1->v.val_int);
2563 break;
2565 case DW_OP_const4u:
2566 case DW_OP_const4s:
2567 fputc (',', asm_out_file);
2568 dw2_asm_output_data_raw (4, val1->v.val_int);
2569 break;
2571 case DW_OP_const8u:
2572 case DW_OP_const8s:
2573 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2574 fputc (',', asm_out_file);
2575 dw2_asm_output_data_raw (8, val1->v.val_int);
2576 break;
2578 case DW_OP_skip:
2579 case DW_OP_bra:
2581 int offset;
2583 gcc_assert (val1->val_class == dw_val_class_loc);
2584 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2586 fputc (',', asm_out_file);
2587 dw2_asm_output_data_raw (2, offset);
2589 break;
2591 case DW_OP_regx:
2593 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2594 gcc_assert (size_of_uleb128 (r)
2595 == size_of_uleb128 (val1->v.val_unsigned));
2596 fputc (',', asm_out_file);
2597 dw2_asm_output_data_uleb128_raw (r);
2599 break;
2601 case DW_OP_constu:
2602 case DW_OP_plus_uconst:
2603 case DW_OP_piece:
2604 fputc (',', asm_out_file);
2605 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2606 break;
2608 case DW_OP_bit_piece:
2609 fputc (',', asm_out_file);
2610 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2611 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2612 break;
2614 case DW_OP_consts:
2615 case DW_OP_breg0:
2616 case DW_OP_breg1:
2617 case DW_OP_breg2:
2618 case DW_OP_breg3:
2619 case DW_OP_breg4:
2620 case DW_OP_breg5:
2621 case DW_OP_breg6:
2622 case DW_OP_breg7:
2623 case DW_OP_breg8:
2624 case DW_OP_breg9:
2625 case DW_OP_breg10:
2626 case DW_OP_breg11:
2627 case DW_OP_breg12:
2628 case DW_OP_breg13:
2629 case DW_OP_breg14:
2630 case DW_OP_breg15:
2631 case DW_OP_breg16:
2632 case DW_OP_breg17:
2633 case DW_OP_breg18:
2634 case DW_OP_breg19:
2635 case DW_OP_breg20:
2636 case DW_OP_breg21:
2637 case DW_OP_breg22:
2638 case DW_OP_breg23:
2639 case DW_OP_breg24:
2640 case DW_OP_breg25:
2641 case DW_OP_breg26:
2642 case DW_OP_breg27:
2643 case DW_OP_breg28:
2644 case DW_OP_breg29:
2645 case DW_OP_breg30:
2646 case DW_OP_breg31:
2647 case DW_OP_fbreg:
2648 fputc (',', asm_out_file);
2649 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2650 break;
2652 case DW_OP_bregx:
2654 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2655 gcc_assert (size_of_uleb128 (r)
2656 == size_of_uleb128 (val1->v.val_unsigned));
2657 fputc (',', asm_out_file);
2658 dw2_asm_output_data_uleb128_raw (r);
2659 fputc (',', asm_out_file);
2660 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2662 break;
2664 case DW_OP_implicit_pointer:
2665 case DW_OP_entry_value:
2666 case DW_OP_const_type:
2667 case DW_OP_regval_type:
2668 case DW_OP_deref_type:
2669 case DW_OP_convert:
2670 case DW_OP_reinterpret:
2671 case DW_OP_GNU_implicit_pointer:
2672 case DW_OP_GNU_entry_value:
2673 case DW_OP_GNU_const_type:
2674 case DW_OP_GNU_regval_type:
2675 case DW_OP_GNU_deref_type:
2676 case DW_OP_GNU_convert:
2677 case DW_OP_GNU_reinterpret:
2678 case DW_OP_GNU_parameter_ref:
2679 gcc_unreachable ();
2680 break;
2682 default:
2683 /* Other codes have no operands. */
2684 break;
2688 void
2689 output_loc_sequence_raw (dw_loc_descr_ref loc)
2691 while (1)
2693 enum dwarf_location_atom opc = loc->dw_loc_opc;
2694 /* Output the opcode. */
2695 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2697 unsigned r = (opc - DW_OP_breg0);
2698 r = DWARF2_FRAME_REG_OUT (r, 1);
2699 gcc_assert (r <= 31);
2700 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2702 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2704 unsigned r = (opc - DW_OP_reg0);
2705 r = DWARF2_FRAME_REG_OUT (r, 1);
2706 gcc_assert (r <= 31);
2707 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2709 /* Output the opcode. */
2710 fprintf (asm_out_file, "%#x", opc);
2711 output_loc_operands_raw (loc);
2713 if (!loc->dw_loc_next)
2714 break;
2715 loc = loc->dw_loc_next;
2717 fputc (',', asm_out_file);
2721 /* This function builds a dwarf location descriptor sequence from a
2722 dw_cfa_location, adding the given OFFSET to the result of the
2723 expression. */
2725 struct dw_loc_descr_node *
2726 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2728 struct dw_loc_descr_node *head, *tmp;
2730 offset += cfa->offset;
2732 if (cfa->indirect)
2734 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2735 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2736 head->dw_loc_oprnd1.val_entry = NULL;
2737 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2738 add_loc_descr (&head, tmp);
2739 loc_descr_plus_const (&head, offset);
2741 else
2742 head = new_reg_loc_descr (cfa->reg, offset);
2744 return head;
2747 /* This function builds a dwarf location descriptor sequence for
2748 the address at OFFSET from the CFA when stack is aligned to
2749 ALIGNMENT byte. */
2751 struct dw_loc_descr_node *
2752 build_cfa_aligned_loc (dw_cfa_location *cfa,
2753 poly_int64 offset, HOST_WIDE_INT alignment)
2755 struct dw_loc_descr_node *head;
2756 unsigned int dwarf_fp
2757 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2759 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2760 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2762 head = new_reg_loc_descr (dwarf_fp, 0);
2763 add_loc_descr (&head, int_loc_descriptor (alignment));
2764 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2765 loc_descr_plus_const (&head, offset);
2767 else
2768 head = new_reg_loc_descr (dwarf_fp, offset);
2769 return head;
2772 /* And now, the support for symbolic debugging information. */
2774 /* .debug_str support. */
2776 static void dwarf2out_init (const char *);
2777 static void dwarf2out_finish (const char *);
2778 static void dwarf2out_early_finish (const char *);
2779 static void dwarf2out_assembly_start (void);
2780 static void dwarf2out_define (unsigned int, const char *);
2781 static void dwarf2out_undef (unsigned int, const char *);
2782 static void dwarf2out_start_source_file (unsigned, const char *);
2783 static void dwarf2out_end_source_file (unsigned);
2784 static void dwarf2out_function_decl (tree);
2785 static void dwarf2out_begin_block (unsigned, unsigned);
2786 static void dwarf2out_end_block (unsigned, unsigned);
2787 static bool dwarf2out_ignore_block (const_tree);
2788 static void dwarf2out_early_global_decl (tree);
2789 static void dwarf2out_late_global_decl (tree);
2790 static void dwarf2out_type_decl (tree, int);
2791 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2792 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2793 dw_die_ref);
2794 static void dwarf2out_abstract_function (tree);
2795 static void dwarf2out_var_location (rtx_insn *);
2796 static void dwarf2out_inline_entry (tree);
2797 static void dwarf2out_size_function (tree);
2798 static void dwarf2out_begin_function (tree);
2799 static void dwarf2out_end_function (unsigned int);
2800 static void dwarf2out_register_main_translation_unit (tree unit);
2801 static void dwarf2out_set_name (tree, tree);
2802 static void dwarf2out_register_external_die (tree decl, const char *sym,
2803 unsigned HOST_WIDE_INT off);
2804 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2805 unsigned HOST_WIDE_INT *off);
2807 /* The debug hooks structure. */
2809 const struct gcc_debug_hooks dwarf2_debug_hooks =
2811 dwarf2out_init,
2812 dwarf2out_finish,
2813 dwarf2out_early_finish,
2814 dwarf2out_assembly_start,
2815 dwarf2out_define,
2816 dwarf2out_undef,
2817 dwarf2out_start_source_file,
2818 dwarf2out_end_source_file,
2819 dwarf2out_begin_block,
2820 dwarf2out_end_block,
2821 dwarf2out_ignore_block,
2822 dwarf2out_source_line,
2823 dwarf2out_begin_prologue,
2824 #if VMS_DEBUGGING_INFO
2825 dwarf2out_vms_end_prologue,
2826 dwarf2out_vms_begin_epilogue,
2827 #else
2828 debug_nothing_int_charstar,
2829 debug_nothing_int_charstar,
2830 #endif
2831 dwarf2out_end_epilogue,
2832 dwarf2out_begin_function,
2833 dwarf2out_end_function, /* end_function */
2834 dwarf2out_register_main_translation_unit,
2835 dwarf2out_function_decl, /* function_decl */
2836 dwarf2out_early_global_decl,
2837 dwarf2out_late_global_decl,
2838 dwarf2out_type_decl, /* type_decl */
2839 dwarf2out_imported_module_or_decl,
2840 dwarf2out_die_ref_for_decl,
2841 dwarf2out_register_external_die,
2842 debug_nothing_tree, /* deferred_inline_function */
2843 /* The DWARF 2 backend tries to reduce debugging bloat by not
2844 emitting the abstract description of inline functions until
2845 something tries to reference them. */
2846 dwarf2out_abstract_function, /* outlining_inline_function */
2847 debug_nothing_rtx_code_label, /* label */
2848 debug_nothing_int, /* handle_pch */
2849 dwarf2out_var_location,
2850 dwarf2out_inline_entry, /* inline_entry */
2851 dwarf2out_size_function, /* size_function */
2852 dwarf2out_switch_text_section,
2853 dwarf2out_set_name,
2854 1, /* start_end_main_source_file */
2855 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2858 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2860 dwarf2out_init,
2861 debug_nothing_charstar,
2862 debug_nothing_charstar,
2863 dwarf2out_assembly_start,
2864 debug_nothing_int_charstar,
2865 debug_nothing_int_charstar,
2866 debug_nothing_int_charstar,
2867 debug_nothing_int,
2868 debug_nothing_int_int, /* begin_block */
2869 debug_nothing_int_int, /* end_block */
2870 debug_true_const_tree, /* ignore_block */
2871 dwarf2out_source_line, /* source_line */
2872 debug_nothing_int_int_charstar, /* begin_prologue */
2873 debug_nothing_int_charstar, /* end_prologue */
2874 debug_nothing_int_charstar, /* begin_epilogue */
2875 debug_nothing_int_charstar, /* end_epilogue */
2876 debug_nothing_tree, /* begin_function */
2877 debug_nothing_int, /* end_function */
2878 debug_nothing_tree, /* register_main_translation_unit */
2879 debug_nothing_tree, /* function_decl */
2880 debug_nothing_tree, /* early_global_decl */
2881 debug_nothing_tree, /* late_global_decl */
2882 debug_nothing_tree_int, /* type_decl */
2883 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2884 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2885 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2886 debug_nothing_tree, /* deferred_inline_function */
2887 debug_nothing_tree, /* outlining_inline_function */
2888 debug_nothing_rtx_code_label, /* label */
2889 debug_nothing_int, /* handle_pch */
2890 debug_nothing_rtx_insn, /* var_location */
2891 debug_nothing_tree, /* inline_entry */
2892 debug_nothing_tree, /* size_function */
2893 debug_nothing_void, /* switch_text_section */
2894 debug_nothing_tree_tree, /* set_name */
2895 0, /* start_end_main_source_file */
2896 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2899 /* NOTE: In the comments in this file, many references are made to
2900 "Debugging Information Entries". This term is abbreviated as `DIE'
2901 throughout the remainder of this file. */
2903 /* An internal representation of the DWARF output is built, and then
2904 walked to generate the DWARF debugging info. The walk of the internal
2905 representation is done after the entire program has been compiled.
2906 The types below are used to describe the internal representation. */
2908 /* Whether to put type DIEs into their own section .debug_types instead
2909 of making them part of the .debug_info section. Only supported for
2910 Dwarf V4 or higher and the user didn't disable them through
2911 -fno-debug-types-section. It is more efficient to put them in a
2912 separate comdat sections since the linker will then be able to
2913 remove duplicates. But not all tools support .debug_types sections
2914 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2915 it is DW_UT_type unit type in .debug_info section. */
2917 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2919 /* Various DIE's use offsets relative to the beginning of the
2920 .debug_info section to refer to each other. */
2922 typedef long int dw_offset;
2924 struct comdat_type_node;
2926 /* The entries in the line_info table more-or-less mirror the opcodes
2927 that are used in the real dwarf line table. Arrays of these entries
2928 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2929 supported. */
2931 enum dw_line_info_opcode {
2932 /* Emit DW_LNE_set_address; the operand is the label index. */
2933 LI_set_address,
2935 /* Emit a row to the matrix with the given line. This may be done
2936 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2937 special opcodes. */
2938 LI_set_line,
2940 /* Emit a DW_LNS_set_file. */
2941 LI_set_file,
2943 /* Emit a DW_LNS_set_column. */
2944 LI_set_column,
2946 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2947 LI_negate_stmt,
2949 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2950 LI_set_prologue_end,
2951 LI_set_epilogue_begin,
2953 /* Emit a DW_LNE_set_discriminator. */
2954 LI_set_discriminator,
2956 /* Output a Fixed Advance PC; the target PC is the label index; the
2957 base PC is the previous LI_adv_address or LI_set_address entry.
2958 We only use this when emitting debug views without assembler
2959 support, at explicit user request. Ideally, we should only use
2960 it when the offset might be zero but we can't tell: it's the only
2961 way to maybe change the PC without resetting the view number. */
2962 LI_adv_address
2965 typedef struct GTY(()) dw_line_info_struct {
2966 enum dw_line_info_opcode opcode;
2967 unsigned int val;
2968 } dw_line_info_entry;
2971 struct GTY(()) dw_line_info_table {
2972 /* The label that marks the end of this section. */
2973 const char *end_label;
2975 /* The values for the last row of the matrix, as collected in the table.
2976 These are used to minimize the changes to the next row. */
2977 unsigned int file_num;
2978 unsigned int line_num;
2979 unsigned int column_num;
2980 int discrim_num;
2981 bool is_stmt;
2982 bool in_use;
2984 /* This denotes the NEXT view number.
2986 If it is 0, it is known that the NEXT view will be the first view
2987 at the given PC.
2989 If it is -1, we're forcing the view number to be reset, e.g. at a
2990 function entry.
2992 The meaning of other nonzero values depends on whether we're
2993 computing views internally or leaving it for the assembler to do
2994 so. If we're emitting them internally, view denotes the view
2995 number since the last known advance of PC. If we're leaving it
2996 for the assembler, it denotes the LVU label number that we're
2997 going to ask the assembler to assign. */
2998 var_loc_view view;
3000 /* This counts the number of symbolic views emitted in this table
3001 since the latest view reset. Its max value, over all tables,
3002 sets symview_upper_bound. */
3003 var_loc_view symviews_since_reset;
3005 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3006 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3007 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3008 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3010 vec<dw_line_info_entry, va_gc> *entries;
3013 /* This is an upper bound for view numbers that the assembler may
3014 assign to symbolic views output in this translation. It is used to
3015 decide how big a field to use to represent view numbers in
3016 symview-classed attributes. */
3018 static var_loc_view symview_upper_bound;
3020 /* If we're keep track of location views and their reset points, and
3021 INSN is a reset point (i.e., it necessarily advances the PC), mark
3022 the next view in TABLE as reset. */
3024 static void
3025 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3027 if (!debug_internal_reset_location_views)
3028 return;
3030 /* Maybe turn (part of?) this test into a default target hook. */
3031 int reset = 0;
3033 if (targetm.reset_location_view)
3034 reset = targetm.reset_location_view (insn);
3036 if (reset)
3038 else if (JUMP_TABLE_DATA_P (insn))
3039 reset = 1;
3040 else if (GET_CODE (insn) == USE
3041 || GET_CODE (insn) == CLOBBER
3042 || GET_CODE (insn) == ASM_INPUT
3043 || asm_noperands (insn) >= 0)
3045 else if (get_attr_min_length (insn) > 0)
3046 reset = 1;
3048 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3049 RESET_NEXT_VIEW (table->view);
3052 /* Each DIE attribute has a field specifying the attribute kind,
3053 a link to the next attribute in the chain, and an attribute value.
3054 Attributes are typically linked below the DIE they modify. */
3056 typedef struct GTY(()) dw_attr_struct {
3057 enum dwarf_attribute dw_attr;
3058 dw_val_node dw_attr_val;
3060 dw_attr_node;
3063 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3064 The children of each node form a circular list linked by
3065 die_sib. die_child points to the node *before* the "first" child node. */
3067 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3068 union die_symbol_or_type_node
3070 const char * GTY ((tag ("0"))) die_symbol;
3071 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3073 GTY ((desc ("%0.comdat_type_p"))) die_id;
3074 vec<dw_attr_node, va_gc> *die_attr;
3075 dw_die_ref die_parent;
3076 dw_die_ref die_child;
3077 dw_die_ref die_sib;
3078 dw_die_ref die_definition; /* ref from a specification to its definition */
3079 dw_offset die_offset;
3080 unsigned long die_abbrev;
3081 int die_mark;
3082 unsigned int decl_id;
3083 enum dwarf_tag die_tag;
3084 /* Die is used and must not be pruned as unused. */
3085 BOOL_BITFIELD die_perennial_p : 1;
3086 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3087 /* For an external ref to die_symbol if die_offset contains an extra
3088 offset to that symbol. */
3089 BOOL_BITFIELD with_offset : 1;
3090 /* Whether this DIE was removed from the DIE tree, for example via
3091 prune_unused_types. We don't consider those present from the
3092 DIE lookup routines. */
3093 BOOL_BITFIELD removed : 1;
3094 /* Lots of spare bits. */
3096 die_node;
3098 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3099 static bool early_dwarf;
3100 static bool early_dwarf_finished;
3101 struct set_early_dwarf {
3102 bool saved;
3103 set_early_dwarf () : saved(early_dwarf)
3105 gcc_assert (! early_dwarf_finished);
3106 early_dwarf = true;
3108 ~set_early_dwarf () { early_dwarf = saved; }
3111 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3112 #define FOR_EACH_CHILD(die, c, expr) do { \
3113 c = die->die_child; \
3114 if (c) do { \
3115 c = c->die_sib; \
3116 expr; \
3117 } while (c != die->die_child); \
3118 } while (0)
3120 /* The pubname structure */
3122 typedef struct GTY(()) pubname_struct {
3123 dw_die_ref die;
3124 const char *name;
3126 pubname_entry;
3129 struct GTY(()) dw_ranges {
3130 const char *label;
3131 /* If this is positive, it's a block number, otherwise it's a
3132 bitwise-negated index into dw_ranges_by_label. */
3133 int num;
3134 /* Index for the range list for DW_FORM_rnglistx. */
3135 unsigned int idx : 31;
3136 /* True if this range might be possibly in a different section
3137 from previous entry. */
3138 unsigned int maybe_new_sec : 1;
3141 /* A structure to hold a macinfo entry. */
3143 typedef struct GTY(()) macinfo_struct {
3144 unsigned char code;
3145 unsigned HOST_WIDE_INT lineno;
3146 const char *info;
3148 macinfo_entry;
3151 struct GTY(()) dw_ranges_by_label {
3152 const char *begin;
3153 const char *end;
3156 /* The comdat type node structure. */
3157 struct GTY(()) comdat_type_node
3159 dw_die_ref root_die;
3160 dw_die_ref type_die;
3161 dw_die_ref skeleton_die;
3162 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3163 comdat_type_node *next;
3166 /* A list of DIEs for which we can't determine ancestry (parent_die
3167 field) just yet. Later in dwarf2out_finish we will fill in the
3168 missing bits. */
3169 typedef struct GTY(()) limbo_die_struct {
3170 dw_die_ref die;
3171 /* The tree for which this DIE was created. We use this to
3172 determine ancestry later. */
3173 tree created_for;
3174 struct limbo_die_struct *next;
3176 limbo_die_node;
3178 typedef struct skeleton_chain_struct
3180 dw_die_ref old_die;
3181 dw_die_ref new_die;
3182 struct skeleton_chain_struct *parent;
3184 skeleton_chain_node;
3186 /* Define a macro which returns nonzero for a TYPE_DECL which was
3187 implicitly generated for a type.
3189 Note that, unlike the C front-end (which generates a NULL named
3190 TYPE_DECL node for each complete tagged type, each array type,
3191 and each function type node created) the C++ front-end generates
3192 a _named_ TYPE_DECL node for each tagged type node created.
3193 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3194 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3195 front-end, but for each type, tagged or not. */
3197 #define TYPE_DECL_IS_STUB(decl) \
3198 (DECL_NAME (decl) == NULL_TREE \
3199 || (DECL_ARTIFICIAL (decl) \
3200 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3201 /* This is necessary for stub decls that \
3202 appear in nested inline functions. */ \
3203 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3204 && (decl_ultimate_origin (decl) \
3205 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3207 /* Information concerning the compilation unit's programming
3208 language, and compiler version. */
3210 /* Fixed size portion of the DWARF compilation unit header. */
3211 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3212 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3213 + (dwarf_version >= 5 ? 4 : 3))
3215 /* Fixed size portion of the DWARF comdat type unit header. */
3216 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3217 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3218 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3220 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3221 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3222 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3224 /* Fixed size portion of public names info. */
3225 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3227 /* Fixed size portion of the address range info. */
3228 #define DWARF_ARANGES_HEADER_SIZE \
3229 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3230 DWARF2_ADDR_SIZE * 2) \
3231 - DWARF_INITIAL_LENGTH_SIZE)
3233 /* Size of padding portion in the address range info. It must be
3234 aligned to twice the pointer size. */
3235 #define DWARF_ARANGES_PAD_SIZE \
3236 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3237 DWARF2_ADDR_SIZE * 2) \
3238 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3240 /* Use assembler line directives if available. */
3241 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3242 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3243 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3244 #else
3245 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3246 #endif
3247 #endif
3249 /* Use assembler views in line directives if available. */
3250 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3251 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3252 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3253 #else
3254 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3255 #endif
3256 #endif
3258 /* Return true if GCC configure detected assembler support for .loc. */
3260 bool
3261 dwarf2out_default_as_loc_support (void)
3263 return DWARF2_ASM_LINE_DEBUG_INFO;
3264 #if (GCC_VERSION >= 3000)
3265 # undef DWARF2_ASM_LINE_DEBUG_INFO
3266 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3267 #endif
3270 /* Return true if GCC configure detected assembler support for views
3271 in .loc directives. */
3273 bool
3274 dwarf2out_default_as_locview_support (void)
3276 return DWARF2_ASM_VIEW_DEBUG_INFO;
3277 #if (GCC_VERSION >= 3000)
3278 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3279 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3280 #endif
3283 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3284 view computation, and it refers to a view identifier for which we
3285 will not emit a label because it is known to map to a view number
3286 zero. We won't allocate the bitmap if we're not using assembler
3287 support for location views, but we have to make the variable
3288 visible for GGC and for code that will be optimized out for lack of
3289 support but that's still parsed and compiled. We could abstract it
3290 out with macros, but it's not worth it. */
3291 static GTY(()) bitmap zero_view_p;
3293 /* Evaluate to TRUE iff N is known to identify the first location view
3294 at its PC. When not using assembler location view computation,
3295 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3296 and views label numbers recorded in it are the ones known to be
3297 zero. */
3298 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3299 || (N) == (var_loc_view)-1 \
3300 || (zero_view_p \
3301 && bitmap_bit_p (zero_view_p, (N))))
3303 /* Return true iff we're to emit .loc directives for the assembler to
3304 generate line number sections.
3306 When we're not emitting views, all we need from the assembler is
3307 support for .loc directives.
3309 If we are emitting views, we can only use the assembler's .loc
3310 support if it also supports views.
3312 When the compiler is emitting the line number programs and
3313 computing view numbers itself, it resets view numbers at known PC
3314 changes and counts from that, and then it emits view numbers as
3315 literal constants in locviewlists. There are cases in which the
3316 compiler is not sure about PC changes, e.g. when extra alignment is
3317 requested for a label. In these cases, the compiler may not reset
3318 the view counter, and the potential PC advance in the line number
3319 program will use an opcode that does not reset the view counter
3320 even if the PC actually changes, so that compiler and debug info
3321 consumer can keep view numbers in sync.
3323 When the compiler defers view computation to the assembler, it
3324 emits symbolic view numbers in locviewlists, with the exception of
3325 views known to be zero (forced resets, or reset after
3326 compiler-visible PC changes): instead of emitting symbols for
3327 these, we emit literal zero and assert the assembler agrees with
3328 the compiler's assessment. We could use symbolic views everywhere,
3329 instead of special-casing zero views, but then we'd be unable to
3330 optimize out locviewlists that contain only zeros. */
3332 static bool
3333 output_asm_line_debug_info (void)
3335 return (dwarf2out_as_loc_support
3336 && (dwarf2out_as_locview_support
3337 || !debug_variable_location_views));
3340 /* Minimum line offset in a special line info. opcode.
3341 This value was chosen to give a reasonable range of values. */
3342 #define DWARF_LINE_BASE -10
3344 /* First special line opcode - leave room for the standard opcodes. */
3345 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3347 /* Range of line offsets in a special line info. opcode. */
3348 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3350 /* Flag that indicates the initial value of the is_stmt_start flag.
3351 In the present implementation, we do not mark any lines as
3352 the beginning of a source statement, because that information
3353 is not made available by the GCC front-end. */
3354 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3356 /* Maximum number of operations per instruction bundle. */
3357 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3358 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3359 #endif
3361 /* This location is used by calc_die_sizes() to keep track
3362 the offset of each DIE within the .debug_info section. */
3363 static unsigned long next_die_offset;
3365 /* Record the root of the DIE's built for the current compilation unit. */
3366 static GTY(()) dw_die_ref single_comp_unit_die;
3368 /* A list of type DIEs that have been separated into comdat sections. */
3369 static GTY(()) comdat_type_node *comdat_type_list;
3371 /* A list of CU DIEs that have been separated. */
3372 static GTY(()) limbo_die_node *cu_die_list;
3374 /* A list of DIEs with a NULL parent waiting to be relocated. */
3375 static GTY(()) limbo_die_node *limbo_die_list;
3377 /* A list of DIEs for which we may have to generate
3378 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3379 static GTY(()) limbo_die_node *deferred_asm_name;
3381 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3383 typedef const char *compare_type;
3385 static hashval_t hash (dwarf_file_data *);
3386 static bool equal (dwarf_file_data *, const char *);
3389 /* Filenames referenced by this compilation unit. */
3390 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3392 struct decl_die_hasher : ggc_ptr_hash<die_node>
3394 typedef tree compare_type;
3396 static hashval_t hash (die_node *);
3397 static bool equal (die_node *, tree);
3399 /* A hash table of references to DIE's that describe declarations.
3400 The key is a DECL_UID() which is a unique number identifying each decl. */
3401 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3403 struct GTY ((for_user)) variable_value_struct {
3404 unsigned int decl_id;
3405 vec<dw_die_ref, va_gc> *dies;
3408 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3410 typedef tree compare_type;
3412 static hashval_t hash (variable_value_struct *);
3413 static bool equal (variable_value_struct *, tree);
3415 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3416 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3417 DECL_CONTEXT of the referenced VAR_DECLs. */
3418 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3420 struct block_die_hasher : ggc_ptr_hash<die_struct>
3422 static hashval_t hash (die_struct *);
3423 static bool equal (die_struct *, die_struct *);
3426 /* A hash table of references to DIE's that describe COMMON blocks.
3427 The key is DECL_UID() ^ die_parent. */
3428 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3430 typedef struct GTY(()) die_arg_entry_struct {
3431 dw_die_ref die;
3432 tree arg;
3433 } die_arg_entry;
3436 /* Node of the variable location list. */
3437 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3438 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3439 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3440 in mode of the EXPR_LIST node and first EXPR_LIST operand
3441 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3442 location or NULL for padding. For larger bitsizes,
3443 mode is 0 and first operand is a CONCAT with bitsize
3444 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3445 NULL as second operand. */
3446 rtx GTY (()) loc;
3447 const char * GTY (()) label;
3448 struct var_loc_node * GTY (()) next;
3449 var_loc_view view;
3452 /* Variable location list. */
3453 struct GTY ((for_user)) var_loc_list_def {
3454 struct var_loc_node * GTY (()) first;
3456 /* Pointer to the last but one or last element of the
3457 chained list. If the list is empty, both first and
3458 last are NULL, if the list contains just one node
3459 or the last node certainly is not redundant, it points
3460 to the last node, otherwise points to the last but one.
3461 Do not mark it for GC because it is marked through the chain. */
3462 struct var_loc_node * GTY ((skip ("%h"))) last;
3464 /* Pointer to the last element before section switch,
3465 if NULL, either sections weren't switched or first
3466 is after section switch. */
3467 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3469 /* DECL_UID of the variable decl. */
3470 unsigned int decl_id;
3472 typedef struct var_loc_list_def var_loc_list;
3474 /* Call argument location list. */
3475 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3476 rtx GTY (()) call_arg_loc_note;
3477 const char * GTY (()) label;
3478 tree GTY (()) block;
3479 bool tail_call_p;
3480 rtx GTY (()) symbol_ref;
3481 struct call_arg_loc_node * GTY (()) next;
3485 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3487 typedef const_tree compare_type;
3489 static hashval_t hash (var_loc_list *);
3490 static bool equal (var_loc_list *, const_tree);
3493 /* Table of decl location linked lists. */
3494 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3496 /* Head and tail of call_arg_loc chain. */
3497 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3498 static struct call_arg_loc_node *call_arg_loc_last;
3500 /* Number of call sites in the current function. */
3501 static int call_site_count = -1;
3502 /* Number of tail call sites in the current function. */
3503 static int tail_call_site_count = -1;
3505 /* A cached location list. */
3506 struct GTY ((for_user)) cached_dw_loc_list_def {
3507 /* The DECL_UID of the decl that this entry describes. */
3508 unsigned int decl_id;
3510 /* The cached location list. */
3511 dw_loc_list_ref loc_list;
3513 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3515 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3518 typedef const_tree compare_type;
3520 static hashval_t hash (cached_dw_loc_list *);
3521 static bool equal (cached_dw_loc_list *, const_tree);
3524 /* Table of cached location lists. */
3525 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3527 /* A vector of references to DIE's that are uniquely identified by their tag,
3528 presence/absence of children DIE's, and list of attribute/value pairs. */
3529 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3531 /* A hash map to remember the stack usage for DWARF procedures. The value
3532 stored is the stack size difference between before the DWARF procedure
3533 invokation and after it returned. In other words, for a DWARF procedure
3534 that consumes N stack slots and that pushes M ones, this stores M - N. */
3535 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3537 /* A global counter for generating labels for line number data. */
3538 static unsigned int line_info_label_num;
3540 /* The current table to which we should emit line number information
3541 for the current function. This will be set up at the beginning of
3542 assembly for the function. */
3543 static GTY(()) dw_line_info_table *cur_line_info_table;
3545 /* The two default tables of line number info. */
3546 static GTY(()) dw_line_info_table *text_section_line_info;
3547 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3549 /* The set of all non-default tables of line number info. */
3550 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3552 /* A flag to tell pubnames/types export if there is an info section to
3553 refer to. */
3554 static bool info_section_emitted;
3556 /* A pointer to the base of a table that contains a list of publicly
3557 accessible names. */
3558 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3560 /* A pointer to the base of a table that contains a list of publicly
3561 accessible types. */
3562 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3564 /* A pointer to the base of a table that contains a list of macro
3565 defines/undefines (and file start/end markers). */
3566 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3568 /* True if .debug_macinfo or .debug_macros section is going to be
3569 emitted. */
3570 #define have_macinfo \
3571 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3572 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3573 && !macinfo_table->is_empty ())
3575 /* Vector of dies for which we should generate .debug_ranges info. */
3576 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3578 /* Vector of pairs of labels referenced in ranges_table. */
3579 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3581 /* Whether we have location lists that need outputting */
3582 static GTY(()) bool have_location_lists;
3584 /* Unique label counter. */
3585 static GTY(()) unsigned int loclabel_num;
3587 /* Unique label counter for point-of-call tables. */
3588 static GTY(()) unsigned int poc_label_num;
3590 /* The last file entry emitted by maybe_emit_file(). */
3591 static GTY(()) struct dwarf_file_data * last_emitted_file;
3593 /* Number of internal labels generated by gen_internal_sym(). */
3594 static GTY(()) int label_num;
3596 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3598 /* Instances of generic types for which we need to generate debug
3599 info that describe their generic parameters and arguments. That
3600 generation needs to happen once all types are properly laid out so
3601 we do it at the end of compilation. */
3602 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3604 /* Offset from the "steady-state frame pointer" to the frame base,
3605 within the current function. */
3606 static poly_int64 frame_pointer_fb_offset;
3607 static bool frame_pointer_fb_offset_valid;
3609 static vec<dw_die_ref> base_types;
3611 /* Flags to represent a set of attribute classes for attributes that represent
3612 a scalar value (bounds, pointers, ...). */
3613 enum dw_scalar_form
3615 dw_scalar_form_constant = 0x01,
3616 dw_scalar_form_exprloc = 0x02,
3617 dw_scalar_form_reference = 0x04
3620 /* Forward declarations for functions defined in this file. */
3622 static int is_pseudo_reg (const_rtx);
3623 static tree type_main_variant (tree);
3624 static int is_tagged_type (const_tree);
3625 static const char *dwarf_tag_name (unsigned);
3626 static const char *dwarf_attr_name (unsigned);
3627 static const char *dwarf_form_name (unsigned);
3628 static tree decl_ultimate_origin (const_tree);
3629 static tree decl_class_context (tree);
3630 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3631 static inline enum dw_val_class AT_class (dw_attr_node *);
3632 static inline unsigned int AT_index (dw_attr_node *);
3633 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3634 static inline unsigned AT_flag (dw_attr_node *);
3635 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3636 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3637 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3638 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3639 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3640 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3641 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3642 unsigned int, unsigned char *);
3643 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3644 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3645 static inline const char *AT_string (dw_attr_node *);
3646 static enum dwarf_form AT_string_form (dw_attr_node *);
3647 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3648 static void add_AT_specification (dw_die_ref, dw_die_ref);
3649 static inline dw_die_ref AT_ref (dw_attr_node *);
3650 static inline int AT_ref_external (dw_attr_node *);
3651 static inline void set_AT_ref_external (dw_attr_node *, int);
3652 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3653 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3654 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3655 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3656 dw_loc_list_ref);
3657 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3658 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3659 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3660 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3661 static void remove_addr_table_entry (addr_table_entry *);
3662 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3663 static inline rtx AT_addr (dw_attr_node *);
3664 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3665 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3666 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3667 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3668 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3669 const char *);
3670 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3671 unsigned HOST_WIDE_INT);
3672 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3673 unsigned long, bool);
3674 static inline const char *AT_lbl (dw_attr_node *);
3675 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3676 static const char *get_AT_low_pc (dw_die_ref);
3677 static const char *get_AT_hi_pc (dw_die_ref);
3678 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3679 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3680 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3681 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3682 static bool is_c (void);
3683 static bool is_cxx (void);
3684 static bool is_cxx (const_tree);
3685 static bool is_fortran (void);
3686 static bool is_ada (void);
3687 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3688 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3689 static void add_child_die (dw_die_ref, dw_die_ref);
3690 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3691 static dw_die_ref lookup_type_die (tree);
3692 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3693 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3694 static void equate_type_number_to_die (tree, dw_die_ref);
3695 static dw_die_ref lookup_decl_die (tree);
3696 static var_loc_list *lookup_decl_loc (const_tree);
3697 static void equate_decl_number_to_die (tree, dw_die_ref);
3698 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3699 static void print_spaces (FILE *);
3700 static void print_die (dw_die_ref, FILE *);
3701 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3702 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3703 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3704 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3705 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3706 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3707 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3708 struct md5_ctx *, int *);
3709 struct checksum_attributes;
3710 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3711 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3712 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3713 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3714 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3715 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3716 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3717 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3718 static int is_type_die (dw_die_ref);
3719 static int is_comdat_die (dw_die_ref);
3720 static inline bool is_template_instantiation (dw_die_ref);
3721 static int is_declaration_die (dw_die_ref);
3722 static int should_move_die_to_comdat (dw_die_ref);
3723 static dw_die_ref clone_as_declaration (dw_die_ref);
3724 static dw_die_ref clone_die (dw_die_ref);
3725 static dw_die_ref clone_tree (dw_die_ref);
3726 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3727 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3728 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3729 static dw_die_ref generate_skeleton (dw_die_ref);
3730 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3731 dw_die_ref,
3732 dw_die_ref);
3733 static void break_out_comdat_types (dw_die_ref);
3734 static void copy_decls_for_unworthy_types (dw_die_ref);
3736 static void add_sibling_attributes (dw_die_ref);
3737 static void output_location_lists (dw_die_ref);
3738 static int constant_size (unsigned HOST_WIDE_INT);
3739 static unsigned long size_of_die (dw_die_ref);
3740 static void calc_die_sizes (dw_die_ref);
3741 static void calc_base_type_die_sizes (void);
3742 static void mark_dies (dw_die_ref);
3743 static void unmark_dies (dw_die_ref);
3744 static void unmark_all_dies (dw_die_ref);
3745 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3746 static unsigned long size_of_aranges (void);
3747 static enum dwarf_form value_format (dw_attr_node *);
3748 static void output_value_format (dw_attr_node *);
3749 static void output_abbrev_section (void);
3750 static void output_die_abbrevs (unsigned long, dw_die_ref);
3751 static void output_die (dw_die_ref);
3752 static void output_compilation_unit_header (enum dwarf_unit_type);
3753 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3754 static void output_comdat_type_unit (comdat_type_node *);
3755 static const char *dwarf2_name (tree, int);
3756 static void add_pubname (tree, dw_die_ref);
3757 static void add_enumerator_pubname (const char *, dw_die_ref);
3758 static void add_pubname_string (const char *, dw_die_ref);
3759 static void add_pubtype (tree, dw_die_ref);
3760 static void output_pubnames (vec<pubname_entry, va_gc> *);
3761 static void output_aranges (void);
3762 static unsigned int add_ranges (const_tree, bool = false);
3763 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3764 bool *, bool);
3765 static void output_ranges (void);
3766 static dw_line_info_table *new_line_info_table (void);
3767 static void output_line_info (bool);
3768 static void output_file_names (void);
3769 static dw_die_ref base_type_die (tree, bool);
3770 static int is_base_type (tree);
3771 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3772 static int decl_quals (const_tree);
3773 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3774 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3775 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3776 static int type_is_enum (const_tree);
3777 static unsigned int dbx_reg_number (const_rtx);
3778 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3779 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3780 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3781 enum var_init_status);
3782 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3783 enum var_init_status);
3784 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3785 enum var_init_status);
3786 static int is_based_loc (const_rtx);
3787 static bool resolve_one_addr (rtx *);
3788 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3789 enum var_init_status);
3790 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3791 enum var_init_status);
3792 struct loc_descr_context;
3793 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3794 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3795 static dw_loc_list_ref loc_list_from_tree (tree, int,
3796 struct loc_descr_context *);
3797 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3798 struct loc_descr_context *);
3799 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3800 static tree field_type (const_tree);
3801 static unsigned int simple_type_align_in_bits (const_tree);
3802 static unsigned int simple_decl_align_in_bits (const_tree);
3803 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3804 struct vlr_context;
3805 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3806 HOST_WIDE_INT *);
3807 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3808 dw_loc_list_ref);
3809 static void add_data_member_location_attribute (dw_die_ref, tree,
3810 struct vlr_context *);
3811 static bool add_const_value_attribute (dw_die_ref, rtx);
3812 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3813 static void insert_wide_int (const wide_int &, unsigned char *, int);
3814 static void insert_float (const_rtx, unsigned char *);
3815 static rtx rtl_for_decl_location (tree);
3816 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3817 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3818 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3819 static void add_name_attribute (dw_die_ref, const char *);
3820 static void add_desc_attribute (dw_die_ref, tree);
3821 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3822 static void add_comp_dir_attribute (dw_die_ref);
3823 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3824 struct loc_descr_context *);
3825 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3826 struct loc_descr_context *);
3827 static void add_subscript_info (dw_die_ref, tree, bool);
3828 static void add_byte_size_attribute (dw_die_ref, tree);
3829 static void add_alignment_attribute (dw_die_ref, tree);
3830 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3831 struct vlr_context *);
3832 static void add_bit_size_attribute (dw_die_ref, tree);
3833 static void add_prototyped_attribute (dw_die_ref, tree);
3834 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3835 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3836 static void add_src_coords_attributes (dw_die_ref, tree);
3837 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3838 static void add_discr_value (dw_die_ref, dw_discr_value *);
3839 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3840 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3841 static dw_die_ref scope_die_for (tree, dw_die_ref);
3842 static inline int local_scope_p (dw_die_ref);
3843 static inline int class_scope_p (dw_die_ref);
3844 static inline int class_or_namespace_scope_p (dw_die_ref);
3845 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3846 static void add_calling_convention_attribute (dw_die_ref, tree);
3847 static const char *type_tag (const_tree);
3848 static tree member_declared_type (const_tree);
3849 #if 0
3850 static const char *decl_start_label (tree);
3851 #endif
3852 static void gen_array_type_die (tree, dw_die_ref);
3853 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3854 #if 0
3855 static void gen_entry_point_die (tree, dw_die_ref);
3856 #endif
3857 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3858 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3859 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3860 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3861 static void gen_formal_types_die (tree, dw_die_ref);
3862 static void gen_subprogram_die (tree, dw_die_ref);
3863 static void gen_variable_die (tree, tree, dw_die_ref);
3864 static void gen_const_die (tree, dw_die_ref);
3865 static void gen_label_die (tree, dw_die_ref);
3866 static void gen_lexical_block_die (tree, dw_die_ref);
3867 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3868 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3869 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3870 static dw_die_ref gen_compile_unit_die (const char *);
3871 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3872 static void gen_member_die (tree, dw_die_ref);
3873 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3874 enum debug_info_usage);
3875 static void gen_subroutine_type_die (tree, dw_die_ref);
3876 static void gen_typedef_die (tree, dw_die_ref);
3877 static void gen_type_die (tree, dw_die_ref);
3878 static void gen_block_die (tree, dw_die_ref);
3879 static void decls_for_scope (tree, dw_die_ref);
3880 static bool is_naming_typedef_decl (const_tree);
3881 static inline dw_die_ref get_context_die (tree);
3882 static void gen_namespace_die (tree, dw_die_ref);
3883 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3884 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3885 static dw_die_ref force_decl_die (tree);
3886 static dw_die_ref force_type_die (tree);
3887 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3888 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3889 static struct dwarf_file_data * lookup_filename (const char *);
3890 static void retry_incomplete_types (void);
3891 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3892 static void gen_generic_params_dies (tree);
3893 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3894 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3895 static void splice_child_die (dw_die_ref, dw_die_ref);
3896 static int file_info_cmp (const void *, const void *);
3897 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3898 const char *, var_loc_view, const char *);
3899 static void output_loc_list (dw_loc_list_ref);
3900 static char *gen_internal_sym (const char *);
3901 static bool want_pubnames (void);
3903 static void prune_unmark_dies (dw_die_ref);
3904 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3905 static void prune_unused_types_mark (dw_die_ref, int);
3906 static void prune_unused_types_walk (dw_die_ref);
3907 static void prune_unused_types_walk_attribs (dw_die_ref);
3908 static void prune_unused_types_prune (dw_die_ref);
3909 static void prune_unused_types (void);
3910 static int maybe_emit_file (struct dwarf_file_data *fd);
3911 static inline const char *AT_vms_delta1 (dw_attr_node *);
3912 static inline const char *AT_vms_delta2 (dw_attr_node *);
3913 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3914 const char *, const char *);
3915 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3916 static void gen_remaining_tmpl_value_param_die_attribute (void);
3917 static bool generic_type_p (tree);
3918 static void schedule_generic_params_dies_gen (tree t);
3919 static void gen_scheduled_generic_parms_dies (void);
3920 static void resolve_variable_values (void);
3922 static const char *comp_dir_string (void);
3924 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3926 /* enum for tracking thread-local variables whose address is really an offset
3927 relative to the TLS pointer, which will need link-time relocation, but will
3928 not need relocation by the DWARF consumer. */
3930 enum dtprel_bool
3932 dtprel_false = 0,
3933 dtprel_true = 1
3936 /* Return the operator to use for an address of a variable. For dtprel_true, we
3937 use DW_OP_const*. For regular variables, which need both link-time
3938 relocation and consumer-level relocation (e.g., to account for shared objects
3939 loaded at a random address), we use DW_OP_addr*. */
3941 static inline enum dwarf_location_atom
3942 dw_addr_op (enum dtprel_bool dtprel)
3944 if (dtprel == dtprel_true)
3945 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
3946 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3947 else
3948 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
3951 /* Return a pointer to a newly allocated address location description. If
3952 dwarf_split_debug_info is true, then record the address with the appropriate
3953 relocation. */
3954 static inline dw_loc_descr_ref
3955 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3957 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3959 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3960 ref->dw_loc_oprnd1.v.val_addr = addr;
3961 ref->dtprel = dtprel;
3962 if (dwarf_split_debug_info)
3963 ref->dw_loc_oprnd1.val_entry
3964 = add_addr_table_entry (addr,
3965 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3966 else
3967 ref->dw_loc_oprnd1.val_entry = NULL;
3969 return ref;
3972 /* Section names used to hold DWARF debugging information. */
3974 #ifndef DEBUG_INFO_SECTION
3975 #define DEBUG_INFO_SECTION ".debug_info"
3976 #endif
3977 #ifndef DEBUG_DWO_INFO_SECTION
3978 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3979 #endif
3980 #ifndef DEBUG_LTO_INFO_SECTION
3981 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3982 #endif
3983 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3984 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3985 #endif
3986 #ifndef DEBUG_ABBREV_SECTION
3987 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3988 #endif
3989 #ifndef DEBUG_LTO_ABBREV_SECTION
3990 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3991 #endif
3992 #ifndef DEBUG_DWO_ABBREV_SECTION
3993 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3994 #endif
3995 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3996 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3997 #endif
3998 #ifndef DEBUG_ARANGES_SECTION
3999 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4000 #endif
4001 #ifndef DEBUG_ADDR_SECTION
4002 #define DEBUG_ADDR_SECTION ".debug_addr"
4003 #endif
4004 #ifndef DEBUG_MACINFO_SECTION
4005 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4006 #endif
4007 #ifndef DEBUG_LTO_MACINFO_SECTION
4008 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4009 #endif
4010 #ifndef DEBUG_DWO_MACINFO_SECTION
4011 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4012 #endif
4013 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4014 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4015 #endif
4016 #ifndef DEBUG_MACRO_SECTION
4017 #define DEBUG_MACRO_SECTION ".debug_macro"
4018 #endif
4019 #ifndef DEBUG_LTO_MACRO_SECTION
4020 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4021 #endif
4022 #ifndef DEBUG_DWO_MACRO_SECTION
4023 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4024 #endif
4025 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4026 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4027 #endif
4028 #ifndef DEBUG_LINE_SECTION
4029 #define DEBUG_LINE_SECTION ".debug_line"
4030 #endif
4031 #ifndef DEBUG_LTO_LINE_SECTION
4032 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4033 #endif
4034 #ifndef DEBUG_DWO_LINE_SECTION
4035 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4036 #endif
4037 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4038 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4039 #endif
4040 #ifndef DEBUG_LOC_SECTION
4041 #define DEBUG_LOC_SECTION ".debug_loc"
4042 #endif
4043 #ifndef DEBUG_DWO_LOC_SECTION
4044 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4045 #endif
4046 #ifndef DEBUG_LOCLISTS_SECTION
4047 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4048 #endif
4049 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4050 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4051 #endif
4052 #ifndef DEBUG_PUBNAMES_SECTION
4053 #define DEBUG_PUBNAMES_SECTION \
4054 ((debug_generate_pub_sections == 2) \
4055 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4056 #endif
4057 #ifndef DEBUG_PUBTYPES_SECTION
4058 #define DEBUG_PUBTYPES_SECTION \
4059 ((debug_generate_pub_sections == 2) \
4060 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4061 #endif
4062 #ifndef DEBUG_STR_OFFSETS_SECTION
4063 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4064 #endif
4065 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4066 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4067 #endif
4068 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4069 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4070 #endif
4071 #ifndef DEBUG_STR_SECTION
4072 #define DEBUG_STR_SECTION ".debug_str"
4073 #endif
4074 #ifndef DEBUG_LTO_STR_SECTION
4075 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4076 #endif
4077 #ifndef DEBUG_STR_DWO_SECTION
4078 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4079 #endif
4080 #ifndef DEBUG_LTO_STR_DWO_SECTION
4081 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4082 #endif
4083 #ifndef DEBUG_RANGES_SECTION
4084 #define DEBUG_RANGES_SECTION ".debug_ranges"
4085 #endif
4086 #ifndef DEBUG_RNGLISTS_SECTION
4087 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4088 #endif
4089 #ifndef DEBUG_LINE_STR_SECTION
4090 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4091 #endif
4092 #ifndef DEBUG_LTO_LINE_STR_SECTION
4093 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4094 #endif
4096 /* Standard ELF section names for compiled code and data. */
4097 #ifndef TEXT_SECTION_NAME
4098 #define TEXT_SECTION_NAME ".text"
4099 #endif
4101 /* Section flags for .debug_str section. */
4102 #define DEBUG_STR_SECTION_FLAGS \
4103 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4104 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4105 : SECTION_DEBUG)
4107 /* Section flags for .debug_str.dwo section. */
4108 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4110 /* Attribute used to refer to the macro section. */
4111 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4112 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4114 /* Labels we insert at beginning sections we can reference instead of
4115 the section names themselves. */
4117 #ifndef TEXT_SECTION_LABEL
4118 #define TEXT_SECTION_LABEL "Ltext"
4119 #endif
4120 #ifndef COLD_TEXT_SECTION_LABEL
4121 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4122 #endif
4123 #ifndef DEBUG_LINE_SECTION_LABEL
4124 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4125 #endif
4126 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4127 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4128 #endif
4129 #ifndef DEBUG_INFO_SECTION_LABEL
4130 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4131 #endif
4132 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4133 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4134 #endif
4135 #ifndef DEBUG_ABBREV_SECTION_LABEL
4136 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4137 #endif
4138 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4139 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4140 #endif
4141 #ifndef DEBUG_ADDR_SECTION_LABEL
4142 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4143 #endif
4144 #ifndef DEBUG_LOC_SECTION_LABEL
4145 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4146 #endif
4147 #ifndef DEBUG_RANGES_SECTION_LABEL
4148 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4149 #endif
4150 #ifndef DEBUG_MACINFO_SECTION_LABEL
4151 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4152 #endif
4153 #ifndef DEBUG_MACRO_SECTION_LABEL
4154 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4155 #endif
4156 #define SKELETON_COMP_DIE_ABBREV 1
4157 #define SKELETON_TYPE_DIE_ABBREV 2
4159 /* Definitions of defaults for formats and names of various special
4160 (artificial) labels which may be generated within this file (when the -g
4161 options is used and DWARF2_DEBUGGING_INFO is in effect.
4162 If necessary, these may be overridden from within the tm.h file, but
4163 typically, overriding these defaults is unnecessary. */
4165 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4166 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4167 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4168 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4169 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4170 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4171 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4172 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4173 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4174 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4175 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4176 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4177 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4178 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4179 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4181 #ifndef TEXT_END_LABEL
4182 #define TEXT_END_LABEL "Letext"
4183 #endif
4184 #ifndef COLD_END_LABEL
4185 #define COLD_END_LABEL "Letext_cold"
4186 #endif
4187 #ifndef BLOCK_BEGIN_LABEL
4188 #define BLOCK_BEGIN_LABEL "LBB"
4189 #endif
4190 #ifndef BLOCK_INLINE_ENTRY_LABEL
4191 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4192 #endif
4193 #ifndef BLOCK_END_LABEL
4194 #define BLOCK_END_LABEL "LBE"
4195 #endif
4196 #ifndef LINE_CODE_LABEL
4197 #define LINE_CODE_LABEL "LM"
4198 #endif
4201 /* Return the root of the DIE's built for the current compilation unit. */
4202 static dw_die_ref
4203 comp_unit_die (void)
4205 if (!single_comp_unit_die)
4206 single_comp_unit_die = gen_compile_unit_die (NULL);
4207 return single_comp_unit_die;
4210 /* We allow a language front-end to designate a function that is to be
4211 called to "demangle" any name before it is put into a DIE. */
4213 static const char *(*demangle_name_func) (const char *);
4215 void
4216 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4218 demangle_name_func = func;
4221 /* Test if rtl node points to a pseudo register. */
4223 static inline int
4224 is_pseudo_reg (const_rtx rtl)
4226 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4227 || (GET_CODE (rtl) == SUBREG
4228 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4231 /* Return a reference to a type, with its const and volatile qualifiers
4232 removed. */
4234 static inline tree
4235 type_main_variant (tree type)
4237 type = TYPE_MAIN_VARIANT (type);
4239 /* ??? There really should be only one main variant among any group of
4240 variants of a given type (and all of the MAIN_VARIANT values for all
4241 members of the group should point to that one type) but sometimes the C
4242 front-end messes this up for array types, so we work around that bug
4243 here. */
4244 if (TREE_CODE (type) == ARRAY_TYPE)
4245 while (type != TYPE_MAIN_VARIANT (type))
4246 type = TYPE_MAIN_VARIANT (type);
4248 return type;
4251 /* Return nonzero if the given type node represents a tagged type. */
4253 static inline int
4254 is_tagged_type (const_tree type)
4256 enum tree_code code = TREE_CODE (type);
4258 return (code == RECORD_TYPE || code == UNION_TYPE
4259 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4262 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4264 static void
4265 get_ref_die_offset_label (char *label, dw_die_ref ref)
4267 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4270 /* Return die_offset of a DIE reference to a base type. */
4272 static unsigned long int
4273 get_base_type_offset (dw_die_ref ref)
4275 if (ref->die_offset)
4276 return ref->die_offset;
4277 if (comp_unit_die ()->die_abbrev)
4279 calc_base_type_die_sizes ();
4280 gcc_assert (ref->die_offset);
4282 return ref->die_offset;
4285 /* Return die_offset of a DIE reference other than base type. */
4287 static unsigned long int
4288 get_ref_die_offset (dw_die_ref ref)
4290 gcc_assert (ref->die_offset);
4291 return ref->die_offset;
4294 /* Convert a DIE tag into its string name. */
4296 static const char *
4297 dwarf_tag_name (unsigned int tag)
4299 const char *name = get_DW_TAG_name (tag);
4301 if (name != NULL)
4302 return name;
4304 return "DW_TAG_<unknown>";
4307 /* Convert a DWARF attribute code into its string name. */
4309 static const char *
4310 dwarf_attr_name (unsigned int attr)
4312 const char *name;
4314 switch (attr)
4316 #if VMS_DEBUGGING_INFO
4317 case DW_AT_HP_prologue:
4318 return "DW_AT_HP_prologue";
4319 #else
4320 case DW_AT_MIPS_loop_unroll_factor:
4321 return "DW_AT_MIPS_loop_unroll_factor";
4322 #endif
4324 #if VMS_DEBUGGING_INFO
4325 case DW_AT_HP_epilogue:
4326 return "DW_AT_HP_epilogue";
4327 #else
4328 case DW_AT_MIPS_stride:
4329 return "DW_AT_MIPS_stride";
4330 #endif
4333 name = get_DW_AT_name (attr);
4335 if (name != NULL)
4336 return name;
4338 return "DW_AT_<unknown>";
4341 /* Convert a DWARF value form code into its string name. */
4343 static const char *
4344 dwarf_form_name (unsigned int form)
4346 const char *name = get_DW_FORM_name (form);
4348 if (name != NULL)
4349 return name;
4351 return "DW_FORM_<unknown>";
4354 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4355 instance of an inlined instance of a decl which is local to an inline
4356 function, so we have to trace all of the way back through the origin chain
4357 to find out what sort of node actually served as the original seed for the
4358 given block. */
4360 static tree
4361 decl_ultimate_origin (const_tree decl)
4363 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4364 return NULL_TREE;
4366 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4367 we're trying to output the abstract instance of this function. */
4368 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4369 return NULL_TREE;
4371 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4372 most distant ancestor, this should never happen. */
4373 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4375 return DECL_ABSTRACT_ORIGIN (decl);
4378 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4379 of a virtual function may refer to a base class, so we check the 'this'
4380 parameter. */
4382 static tree
4383 decl_class_context (tree decl)
4385 tree context = NULL_TREE;
4387 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4388 context = DECL_CONTEXT (decl);
4389 else
4390 context = TYPE_MAIN_VARIANT
4391 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4393 if (context && !TYPE_P (context))
4394 context = NULL_TREE;
4396 return context;
4399 /* Add an attribute/value pair to a DIE. */
4401 static inline void
4402 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4404 /* Maybe this should be an assert? */
4405 if (die == NULL)
4406 return;
4408 if (flag_checking)
4410 /* Check we do not add duplicate attrs. Can't use get_AT here
4411 because that recurses to the specification/abstract origin DIE. */
4412 dw_attr_node *a;
4413 unsigned ix;
4414 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4415 gcc_assert (a->dw_attr != attr->dw_attr);
4418 vec_safe_reserve (die->die_attr, 1);
4419 vec_safe_push (die->die_attr, *attr);
4422 static inline enum dw_val_class
4423 AT_class (dw_attr_node *a)
4425 return a->dw_attr_val.val_class;
4428 /* Return the index for any attribute that will be referenced with a
4429 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4430 indices are stored in dw_attr_val.v.val_str for reference counting
4431 pruning. */
4433 static inline unsigned int
4434 AT_index (dw_attr_node *a)
4436 if (AT_class (a) == dw_val_class_str)
4437 return a->dw_attr_val.v.val_str->index;
4438 else if (a->dw_attr_val.val_entry != NULL)
4439 return a->dw_attr_val.val_entry->index;
4440 return NOT_INDEXED;
4443 /* Add a flag value attribute to a DIE. */
4445 static inline void
4446 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4448 dw_attr_node attr;
4450 attr.dw_attr = attr_kind;
4451 attr.dw_attr_val.val_class = dw_val_class_flag;
4452 attr.dw_attr_val.val_entry = NULL;
4453 attr.dw_attr_val.v.val_flag = flag;
4454 add_dwarf_attr (die, &attr);
4457 static inline unsigned
4458 AT_flag (dw_attr_node *a)
4460 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4461 return a->dw_attr_val.v.val_flag;
4464 /* Add a signed integer attribute value to a DIE. */
4466 static inline void
4467 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4469 dw_attr_node attr;
4471 attr.dw_attr = attr_kind;
4472 attr.dw_attr_val.val_class = dw_val_class_const;
4473 attr.dw_attr_val.val_entry = NULL;
4474 attr.dw_attr_val.v.val_int = int_val;
4475 add_dwarf_attr (die, &attr);
4478 static inline HOST_WIDE_INT
4479 AT_int (dw_attr_node *a)
4481 gcc_assert (a && (AT_class (a) == dw_val_class_const
4482 || AT_class (a) == dw_val_class_const_implicit));
4483 return a->dw_attr_val.v.val_int;
4486 /* Add an unsigned integer attribute value to a DIE. */
4488 static inline void
4489 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4490 unsigned HOST_WIDE_INT unsigned_val)
4492 dw_attr_node attr;
4494 attr.dw_attr = attr_kind;
4495 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4496 attr.dw_attr_val.val_entry = NULL;
4497 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4498 add_dwarf_attr (die, &attr);
4501 static inline unsigned HOST_WIDE_INT
4502 AT_unsigned (dw_attr_node *a)
4504 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4505 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4506 return a->dw_attr_val.v.val_unsigned;
4509 /* Add an unsigned wide integer attribute value to a DIE. */
4511 static inline void
4512 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4513 const wide_int& w)
4515 dw_attr_node attr;
4517 attr.dw_attr = attr_kind;
4518 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4519 attr.dw_attr_val.val_entry = NULL;
4520 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4521 *attr.dw_attr_val.v.val_wide = w;
4522 add_dwarf_attr (die, &attr);
4525 /* Add an unsigned double integer attribute value to a DIE. */
4527 static inline void
4528 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4529 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4531 dw_attr_node attr;
4533 attr.dw_attr = attr_kind;
4534 attr.dw_attr_val.val_class = dw_val_class_const_double;
4535 attr.dw_attr_val.val_entry = NULL;
4536 attr.dw_attr_val.v.val_double.high = high;
4537 attr.dw_attr_val.v.val_double.low = low;
4538 add_dwarf_attr (die, &attr);
4541 /* Add a floating point attribute value to a DIE and return it. */
4543 static inline void
4544 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4545 unsigned int length, unsigned int elt_size, unsigned char *array)
4547 dw_attr_node attr;
4549 attr.dw_attr = attr_kind;
4550 attr.dw_attr_val.val_class = dw_val_class_vec;
4551 attr.dw_attr_val.val_entry = NULL;
4552 attr.dw_attr_val.v.val_vec.length = length;
4553 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4554 attr.dw_attr_val.v.val_vec.array = array;
4555 add_dwarf_attr (die, &attr);
4558 /* Add an 8-byte data attribute value to a DIE. */
4560 static inline void
4561 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4562 unsigned char data8[8])
4564 dw_attr_node attr;
4566 attr.dw_attr = attr_kind;
4567 attr.dw_attr_val.val_class = dw_val_class_data8;
4568 attr.dw_attr_val.val_entry = NULL;
4569 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4570 add_dwarf_attr (die, &attr);
4573 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4574 dwarf_split_debug_info, address attributes in dies destined for the
4575 final executable have force_direct set to avoid using indexed
4576 references. */
4578 static inline void
4579 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4580 bool force_direct)
4582 dw_attr_node attr;
4583 char * lbl_id;
4585 lbl_id = xstrdup (lbl_low);
4586 attr.dw_attr = DW_AT_low_pc;
4587 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4588 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4589 if (dwarf_split_debug_info && !force_direct)
4590 attr.dw_attr_val.val_entry
4591 = add_addr_table_entry (lbl_id, ate_kind_label);
4592 else
4593 attr.dw_attr_val.val_entry = NULL;
4594 add_dwarf_attr (die, &attr);
4596 attr.dw_attr = DW_AT_high_pc;
4597 if (dwarf_version < 4)
4598 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4599 else
4600 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4601 lbl_id = xstrdup (lbl_high);
4602 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4603 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4604 && dwarf_split_debug_info && !force_direct)
4605 attr.dw_attr_val.val_entry
4606 = add_addr_table_entry (lbl_id, ate_kind_label);
4607 else
4608 attr.dw_attr_val.val_entry = NULL;
4609 add_dwarf_attr (die, &attr);
4612 /* Hash and equality functions for debug_str_hash. */
4614 hashval_t
4615 indirect_string_hasher::hash (indirect_string_node *x)
4617 return htab_hash_string (x->str);
4620 bool
4621 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4623 return strcmp (x1->str, x2) == 0;
4626 /* Add STR to the given string hash table. */
4628 static struct indirect_string_node *
4629 find_AT_string_in_table (const char *str,
4630 hash_table<indirect_string_hasher> *table)
4632 struct indirect_string_node *node;
4634 indirect_string_node **slot
4635 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4636 if (*slot == NULL)
4638 node = ggc_cleared_alloc<indirect_string_node> ();
4639 node->str = ggc_strdup (str);
4640 *slot = node;
4642 else
4643 node = *slot;
4645 node->refcount++;
4646 return node;
4649 /* Add STR to the indirect string hash table. */
4651 static struct indirect_string_node *
4652 find_AT_string (const char *str)
4654 if (! debug_str_hash)
4655 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4657 return find_AT_string_in_table (str, debug_str_hash);
4660 /* Add a string attribute value to a DIE. */
4662 static inline void
4663 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4665 dw_attr_node attr;
4666 struct indirect_string_node *node;
4668 node = find_AT_string (str);
4670 attr.dw_attr = attr_kind;
4671 attr.dw_attr_val.val_class = dw_val_class_str;
4672 attr.dw_attr_val.val_entry = NULL;
4673 attr.dw_attr_val.v.val_str = node;
4674 add_dwarf_attr (die, &attr);
4677 static inline const char *
4678 AT_string (dw_attr_node *a)
4680 gcc_assert (a && AT_class (a) == dw_val_class_str);
4681 return a->dw_attr_val.v.val_str->str;
4684 /* Call this function directly to bypass AT_string_form's logic to put
4685 the string inline in the die. */
4687 static void
4688 set_indirect_string (struct indirect_string_node *node)
4690 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4691 /* Already indirect is a no op. */
4692 if (node->form == DW_FORM_strp
4693 || node->form == DW_FORM_line_strp
4694 || node->form == dwarf_FORM (DW_FORM_strx))
4696 gcc_assert (node->label);
4697 return;
4699 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4700 ++dw2_string_counter;
4701 node->label = xstrdup (label);
4703 if (!dwarf_split_debug_info)
4705 node->form = DW_FORM_strp;
4706 node->index = NOT_INDEXED;
4708 else
4710 node->form = dwarf_FORM (DW_FORM_strx);
4711 node->index = NO_INDEX_ASSIGNED;
4715 /* A helper function for dwarf2out_finish, called to reset indirect
4716 string decisions done for early LTO dwarf output before fat object
4717 dwarf output. */
4720 reset_indirect_string (indirect_string_node **h, void *)
4722 struct indirect_string_node *node = *h;
4723 if (node->form == DW_FORM_strp || node->form == dwarf_FORM (DW_FORM_strx))
4725 free (node->label);
4726 node->label = NULL;
4727 node->form = (dwarf_form) 0;
4728 node->index = 0;
4730 return 1;
4733 /* Find out whether a string should be output inline in DIE
4734 or out-of-line in .debug_str section. */
4736 static enum dwarf_form
4737 find_string_form (struct indirect_string_node *node)
4739 unsigned int len;
4741 if (node->form)
4742 return node->form;
4744 len = strlen (node->str) + 1;
4746 /* If the string is shorter or equal to the size of the reference, it is
4747 always better to put it inline. */
4748 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4749 return node->form = DW_FORM_string;
4751 /* If we cannot expect the linker to merge strings in .debug_str
4752 section, only put it into .debug_str if it is worth even in this
4753 single module. */
4754 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4755 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4756 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4757 return node->form = DW_FORM_string;
4759 set_indirect_string (node);
4761 return node->form;
4764 /* Find out whether the string referenced from the attribute should be
4765 output inline in DIE or out-of-line in .debug_str section. */
4767 static enum dwarf_form
4768 AT_string_form (dw_attr_node *a)
4770 gcc_assert (a && AT_class (a) == dw_val_class_str);
4771 return find_string_form (a->dw_attr_val.v.val_str);
4774 /* Add a DIE reference attribute value to a DIE. */
4776 static inline void
4777 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4779 dw_attr_node attr;
4780 gcc_checking_assert (targ_die != NULL);
4782 /* With LTO we can end up trying to reference something we didn't create
4783 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4784 if (targ_die == NULL)
4785 return;
4787 attr.dw_attr = attr_kind;
4788 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4789 attr.dw_attr_val.val_entry = NULL;
4790 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4791 attr.dw_attr_val.v.val_die_ref.external = 0;
4792 add_dwarf_attr (die, &attr);
4795 /* Change DIE reference REF to point to NEW_DIE instead. */
4797 static inline void
4798 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4800 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4801 ref->dw_attr_val.v.val_die_ref.die = new_die;
4802 ref->dw_attr_val.v.val_die_ref.external = 0;
4805 /* Add an AT_specification attribute to a DIE, and also make the back
4806 pointer from the specification to the definition. */
4808 static inline void
4809 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4811 add_AT_die_ref (die, DW_AT_specification, targ_die);
4812 gcc_assert (!targ_die->die_definition);
4813 targ_die->die_definition = die;
4816 static inline dw_die_ref
4817 AT_ref (dw_attr_node *a)
4819 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4820 return a->dw_attr_val.v.val_die_ref.die;
4823 static inline int
4824 AT_ref_external (dw_attr_node *a)
4826 if (a && AT_class (a) == dw_val_class_die_ref)
4827 return a->dw_attr_val.v.val_die_ref.external;
4829 return 0;
4832 static inline void
4833 set_AT_ref_external (dw_attr_node *a, int i)
4835 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4836 a->dw_attr_val.v.val_die_ref.external = i;
4839 /* Add an FDE reference attribute value to a DIE. */
4841 static inline void
4842 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4844 dw_attr_node attr;
4846 attr.dw_attr = attr_kind;
4847 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4848 attr.dw_attr_val.val_entry = NULL;
4849 attr.dw_attr_val.v.val_fde_index = targ_fde;
4850 add_dwarf_attr (die, &attr);
4853 /* Add a location description attribute value to a DIE. */
4855 static inline void
4856 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4858 dw_attr_node attr;
4860 attr.dw_attr = attr_kind;
4861 attr.dw_attr_val.val_class = dw_val_class_loc;
4862 attr.dw_attr_val.val_entry = NULL;
4863 attr.dw_attr_val.v.val_loc = loc;
4864 add_dwarf_attr (die, &attr);
4867 static inline dw_loc_descr_ref
4868 AT_loc (dw_attr_node *a)
4870 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4871 return a->dw_attr_val.v.val_loc;
4874 static inline void
4875 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4877 dw_attr_node attr;
4879 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4880 return;
4882 attr.dw_attr = attr_kind;
4883 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4884 attr.dw_attr_val.val_entry = NULL;
4885 attr.dw_attr_val.v.val_loc_list = loc_list;
4886 add_dwarf_attr (die, &attr);
4887 have_location_lists = true;
4890 static inline dw_loc_list_ref
4891 AT_loc_list (dw_attr_node *a)
4893 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4894 return a->dw_attr_val.v.val_loc_list;
4897 /* Add a view list attribute to DIE. It must have a DW_AT_location
4898 attribute, because the view list complements the location list. */
4900 static inline void
4901 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
4903 dw_attr_node attr;
4905 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4906 return;
4908 attr.dw_attr = attr_kind;
4909 attr.dw_attr_val.val_class = dw_val_class_view_list;
4910 attr.dw_attr_val.val_entry = NULL;
4911 attr.dw_attr_val.v.val_view_list = die;
4912 add_dwarf_attr (die, &attr);
4913 gcc_checking_assert (get_AT (die, DW_AT_location));
4914 gcc_assert (have_location_lists);
4917 /* Return a pointer to the location list referenced by the attribute.
4918 If the named attribute is a view list, look up the corresponding
4919 DW_AT_location attribute and return its location list. */
4921 static inline dw_loc_list_ref *
4922 AT_loc_list_ptr (dw_attr_node *a)
4924 gcc_assert (a);
4925 switch (AT_class (a))
4927 case dw_val_class_loc_list:
4928 return &a->dw_attr_val.v.val_loc_list;
4929 case dw_val_class_view_list:
4931 dw_attr_node *l;
4932 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
4933 if (!l)
4934 return NULL;
4935 gcc_checking_assert (l + 1 == a);
4936 return AT_loc_list_ptr (l);
4938 default:
4939 gcc_unreachable ();
4943 /* Return the location attribute value associated with a view list
4944 attribute value. */
4946 static inline dw_val_node *
4947 view_list_to_loc_list_val_node (dw_val_node *val)
4949 gcc_assert (val->val_class == dw_val_class_view_list);
4950 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
4951 if (!loc)
4952 return NULL;
4953 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
4954 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
4955 return &loc->dw_attr_val;
4958 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4960 static hashval_t hash (addr_table_entry *);
4961 static bool equal (addr_table_entry *, addr_table_entry *);
4964 /* Table of entries into the .debug_addr section. */
4966 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4968 /* Hash an address_table_entry. */
4970 hashval_t
4971 addr_hasher::hash (addr_table_entry *a)
4973 inchash::hash hstate;
4974 switch (a->kind)
4976 case ate_kind_rtx:
4977 hstate.add_int (0);
4978 break;
4979 case ate_kind_rtx_dtprel:
4980 hstate.add_int (1);
4981 break;
4982 case ate_kind_label:
4983 return htab_hash_string (a->addr.label);
4984 default:
4985 gcc_unreachable ();
4987 inchash::add_rtx (a->addr.rtl, hstate);
4988 return hstate.end ();
4991 /* Determine equality for two address_table_entries. */
4993 bool
4994 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4996 if (a1->kind != a2->kind)
4997 return 0;
4998 switch (a1->kind)
5000 case ate_kind_rtx:
5001 case ate_kind_rtx_dtprel:
5002 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5003 case ate_kind_label:
5004 return strcmp (a1->addr.label, a2->addr.label) == 0;
5005 default:
5006 gcc_unreachable ();
5010 /* Initialize an addr_table_entry. */
5012 void
5013 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5015 e->kind = kind;
5016 switch (kind)
5018 case ate_kind_rtx:
5019 case ate_kind_rtx_dtprel:
5020 e->addr.rtl = (rtx) addr;
5021 break;
5022 case ate_kind_label:
5023 e->addr.label = (char *) addr;
5024 break;
5026 e->refcount = 0;
5027 e->index = NO_INDEX_ASSIGNED;
5030 /* Add attr to the address table entry to the table. Defer setting an
5031 index until output time. */
5033 static addr_table_entry *
5034 add_addr_table_entry (void *addr, enum ate_kind kind)
5036 addr_table_entry *node;
5037 addr_table_entry finder;
5039 gcc_assert (dwarf_split_debug_info);
5040 if (! addr_index_table)
5041 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5042 init_addr_table_entry (&finder, kind, addr);
5043 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5045 if (*slot == HTAB_EMPTY_ENTRY)
5047 node = ggc_cleared_alloc<addr_table_entry> ();
5048 init_addr_table_entry (node, kind, addr);
5049 *slot = node;
5051 else
5052 node = *slot;
5054 node->refcount++;
5055 return node;
5058 /* Remove an entry from the addr table by decrementing its refcount.
5059 Strictly, decrementing the refcount would be enough, but the
5060 assertion that the entry is actually in the table has found
5061 bugs. */
5063 static void
5064 remove_addr_table_entry (addr_table_entry *entry)
5066 gcc_assert (dwarf_split_debug_info && addr_index_table);
5067 /* After an index is assigned, the table is frozen. */
5068 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5069 entry->refcount--;
5072 /* Given a location list, remove all addresses it refers to from the
5073 address_table. */
5075 static void
5076 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5078 for (; descr; descr = descr->dw_loc_next)
5079 if (descr->dw_loc_oprnd1.val_entry != NULL)
5081 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5082 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5086 /* A helper function for dwarf2out_finish called through
5087 htab_traverse. Assign an addr_table_entry its index. All entries
5088 must be collected into the table when this function is called,
5089 because the indexing code relies on htab_traverse to traverse nodes
5090 in the same order for each run. */
5093 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5095 addr_table_entry *node = *h;
5097 /* Don't index unreferenced nodes. */
5098 if (node->refcount == 0)
5099 return 1;
5101 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5102 node->index = *index;
5103 *index += 1;
5105 return 1;
5108 /* Add an address constant attribute value to a DIE. When using
5109 dwarf_split_debug_info, address attributes in dies destined for the
5110 final executable should be direct references--setting the parameter
5111 force_direct ensures this behavior. */
5113 static inline void
5114 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5115 bool force_direct)
5117 dw_attr_node attr;
5119 attr.dw_attr = attr_kind;
5120 attr.dw_attr_val.val_class = dw_val_class_addr;
5121 attr.dw_attr_val.v.val_addr = addr;
5122 if (dwarf_split_debug_info && !force_direct)
5123 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5124 else
5125 attr.dw_attr_val.val_entry = NULL;
5126 add_dwarf_attr (die, &attr);
5129 /* Get the RTX from to an address DIE attribute. */
5131 static inline rtx
5132 AT_addr (dw_attr_node *a)
5134 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5135 return a->dw_attr_val.v.val_addr;
5138 /* Add a file attribute value to a DIE. */
5140 static inline void
5141 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5142 struct dwarf_file_data *fd)
5144 dw_attr_node attr;
5146 attr.dw_attr = attr_kind;
5147 attr.dw_attr_val.val_class = dw_val_class_file;
5148 attr.dw_attr_val.val_entry = NULL;
5149 attr.dw_attr_val.v.val_file = fd;
5150 add_dwarf_attr (die, &attr);
5153 /* Get the dwarf_file_data from a file DIE attribute. */
5155 static inline struct dwarf_file_data *
5156 AT_file (dw_attr_node *a)
5158 gcc_assert (a && (AT_class (a) == dw_val_class_file
5159 || AT_class (a) == dw_val_class_file_implicit));
5160 return a->dw_attr_val.v.val_file;
5163 /* Add a vms delta attribute value to a DIE. */
5165 static inline void
5166 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5167 const char *lbl1, const char *lbl2)
5169 dw_attr_node attr;
5171 attr.dw_attr = attr_kind;
5172 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5173 attr.dw_attr_val.val_entry = NULL;
5174 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5175 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5176 add_dwarf_attr (die, &attr);
5179 /* Add a symbolic view identifier attribute value to a DIE. */
5181 static inline void
5182 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5183 const char *view_label)
5185 dw_attr_node attr;
5187 attr.dw_attr = attr_kind;
5188 attr.dw_attr_val.val_class = dw_val_class_symview;
5189 attr.dw_attr_val.val_entry = NULL;
5190 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5191 add_dwarf_attr (die, &attr);
5194 /* Add a label identifier attribute value to a DIE. */
5196 static inline void
5197 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5198 const char *lbl_id)
5200 dw_attr_node attr;
5202 attr.dw_attr = attr_kind;
5203 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5204 attr.dw_attr_val.val_entry = NULL;
5205 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5206 if (dwarf_split_debug_info)
5207 attr.dw_attr_val.val_entry
5208 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5209 ate_kind_label);
5210 add_dwarf_attr (die, &attr);
5213 /* Add a section offset attribute value to a DIE, an offset into the
5214 debug_line section. */
5216 static inline void
5217 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5218 const char *label)
5220 dw_attr_node attr;
5222 attr.dw_attr = attr_kind;
5223 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5224 attr.dw_attr_val.val_entry = NULL;
5225 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5226 add_dwarf_attr (die, &attr);
5229 /* Add a section offset attribute value to a DIE, an offset into the
5230 debug_loclists section. */
5232 static inline void
5233 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5234 const char *label)
5236 dw_attr_node attr;
5238 attr.dw_attr = attr_kind;
5239 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
5240 attr.dw_attr_val.val_entry = NULL;
5241 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5242 add_dwarf_attr (die, &attr);
5245 /* Add a section offset attribute value to a DIE, an offset into the
5246 debug_macinfo section. */
5248 static inline void
5249 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5250 const char *label)
5252 dw_attr_node attr;
5254 attr.dw_attr = attr_kind;
5255 attr.dw_attr_val.val_class = dw_val_class_macptr;
5256 attr.dw_attr_val.val_entry = NULL;
5257 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5258 add_dwarf_attr (die, &attr);
5261 /* Add an offset attribute value to a DIE. */
5263 static inline void
5264 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5265 unsigned HOST_WIDE_INT offset)
5267 dw_attr_node attr;
5269 attr.dw_attr = attr_kind;
5270 attr.dw_attr_val.val_class = dw_val_class_offset;
5271 attr.dw_attr_val.val_entry = NULL;
5272 attr.dw_attr_val.v.val_offset = offset;
5273 add_dwarf_attr (die, &attr);
5276 /* Add a range_list attribute value to a DIE. When using
5277 dwarf_split_debug_info, address attributes in dies destined for the
5278 final executable should be direct references--setting the parameter
5279 force_direct ensures this behavior. */
5281 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5282 #define RELOCATED_OFFSET (NULL)
5284 static void
5285 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5286 long unsigned int offset, bool force_direct)
5288 dw_attr_node attr;
5290 attr.dw_attr = attr_kind;
5291 attr.dw_attr_val.val_class = dw_val_class_range_list;
5292 /* For the range_list attribute, use val_entry to store whether the
5293 offset should follow split-debug-info or normal semantics. This
5294 value is read in output_range_list_offset. */
5295 if (dwarf_split_debug_info && !force_direct)
5296 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5297 else
5298 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5299 attr.dw_attr_val.v.val_offset = offset;
5300 add_dwarf_attr (die, &attr);
5303 /* Return the start label of a delta attribute. */
5305 static inline const char *
5306 AT_vms_delta1 (dw_attr_node *a)
5308 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5309 return a->dw_attr_val.v.val_vms_delta.lbl1;
5312 /* Return the end label of a delta attribute. */
5314 static inline const char *
5315 AT_vms_delta2 (dw_attr_node *a)
5317 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5318 return a->dw_attr_val.v.val_vms_delta.lbl2;
5321 static inline const char *
5322 AT_lbl (dw_attr_node *a)
5324 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5325 || AT_class (a) == dw_val_class_lineptr
5326 || AT_class (a) == dw_val_class_macptr
5327 || AT_class (a) == dw_val_class_loclistsptr
5328 || AT_class (a) == dw_val_class_high_pc));
5329 return a->dw_attr_val.v.val_lbl_id;
5332 /* Get the attribute of type attr_kind. */
5334 static dw_attr_node *
5335 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5337 dw_attr_node *a;
5338 unsigned ix;
5339 dw_die_ref spec = NULL;
5341 if (! die)
5342 return NULL;
5344 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5345 if (a->dw_attr == attr_kind)
5346 return a;
5347 else if (a->dw_attr == DW_AT_specification
5348 || a->dw_attr == DW_AT_abstract_origin)
5349 spec = AT_ref (a);
5351 if (spec)
5352 return get_AT (spec, attr_kind);
5354 return NULL;
5357 /* Returns the parent of the declaration of DIE. */
5359 static dw_die_ref
5360 get_die_parent (dw_die_ref die)
5362 dw_die_ref t;
5364 if (!die)
5365 return NULL;
5367 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5368 || (t = get_AT_ref (die, DW_AT_specification)))
5369 die = t;
5371 return die->die_parent;
5374 /* Return the "low pc" attribute value, typically associated with a subprogram
5375 DIE. Return null if the "low pc" attribute is either not present, or if it
5376 cannot be represented as an assembler label identifier. */
5378 static inline const char *
5379 get_AT_low_pc (dw_die_ref die)
5381 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5383 return a ? AT_lbl (a) : NULL;
5386 /* Return the "high pc" attribute value, typically associated with a subprogram
5387 DIE. Return null if the "high pc" attribute is either not present, or if it
5388 cannot be represented as an assembler label identifier. */
5390 static inline const char *
5391 get_AT_hi_pc (dw_die_ref die)
5393 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5395 return a ? AT_lbl (a) : NULL;
5398 /* Return the value of the string attribute designated by ATTR_KIND, or
5399 NULL if it is not present. */
5401 static inline const char *
5402 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5404 dw_attr_node *a = get_AT (die, attr_kind);
5406 return a ? AT_string (a) : NULL;
5409 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5410 if it is not present. */
5412 static inline int
5413 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5415 dw_attr_node *a = get_AT (die, attr_kind);
5417 return a ? AT_flag (a) : 0;
5420 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5421 if it is not present. */
5423 static inline unsigned
5424 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5426 dw_attr_node *a = get_AT (die, attr_kind);
5428 return a ? AT_unsigned (a) : 0;
5431 static inline dw_die_ref
5432 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5434 dw_attr_node *a = get_AT (die, attr_kind);
5436 return a ? AT_ref (a) : NULL;
5439 static inline struct dwarf_file_data *
5440 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5442 dw_attr_node *a = get_AT (die, attr_kind);
5444 return a ? AT_file (a) : NULL;
5447 /* Return TRUE if the language is C. */
5449 static inline bool
5450 is_c (void)
5452 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5454 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5455 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5460 /* Return TRUE if the language is C++. */
5462 static inline bool
5463 is_cxx (void)
5465 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5467 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5468 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5471 /* Return TRUE if DECL was created by the C++ frontend. */
5473 static bool
5474 is_cxx (const_tree decl)
5476 if (in_lto_p)
5478 const_tree context = get_ultimate_context (decl);
5479 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5480 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5482 return is_cxx ();
5485 /* Return TRUE if the language is Fortran. */
5487 static inline bool
5488 is_fortran (void)
5490 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5492 return (lang == DW_LANG_Fortran77
5493 || lang == DW_LANG_Fortran90
5494 || lang == DW_LANG_Fortran95
5495 || lang == DW_LANG_Fortran03
5496 || lang == DW_LANG_Fortran08);
5499 static inline bool
5500 is_fortran (const_tree decl)
5502 if (in_lto_p)
5504 const_tree context = get_ultimate_context (decl);
5505 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5506 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5507 "GNU Fortran", 11) == 0
5508 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5509 "GNU F77") == 0);
5511 return is_fortran ();
5514 /* Return TRUE if the language is Ada. */
5516 static inline bool
5517 is_ada (void)
5519 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5521 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5524 /* Remove the specified attribute if present. Return TRUE if removal
5525 was successful. */
5527 static bool
5528 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5530 dw_attr_node *a;
5531 unsigned ix;
5533 if (! die)
5534 return false;
5536 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5537 if (a->dw_attr == attr_kind)
5539 if (AT_class (a) == dw_val_class_str)
5540 if (a->dw_attr_val.v.val_str->refcount)
5541 a->dw_attr_val.v.val_str->refcount--;
5543 /* vec::ordered_remove should help reduce the number of abbrevs
5544 that are needed. */
5545 die->die_attr->ordered_remove (ix);
5546 return true;
5548 return false;
5551 /* Remove CHILD from its parent. PREV must have the property that
5552 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5554 static void
5555 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5557 gcc_assert (child->die_parent == prev->die_parent);
5558 gcc_assert (prev->die_sib == child);
5559 if (prev == child)
5561 gcc_assert (child->die_parent->die_child == child);
5562 prev = NULL;
5564 else
5565 prev->die_sib = child->die_sib;
5566 if (child->die_parent->die_child == child)
5567 child->die_parent->die_child = prev;
5568 child->die_sib = NULL;
5571 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5572 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5574 static void
5575 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5577 dw_die_ref parent = old_child->die_parent;
5579 gcc_assert (parent == prev->die_parent);
5580 gcc_assert (prev->die_sib == old_child);
5582 new_child->die_parent = parent;
5583 if (prev == old_child)
5585 gcc_assert (parent->die_child == old_child);
5586 new_child->die_sib = new_child;
5588 else
5590 prev->die_sib = new_child;
5591 new_child->die_sib = old_child->die_sib;
5593 if (old_child->die_parent->die_child == old_child)
5594 old_child->die_parent->die_child = new_child;
5595 old_child->die_sib = NULL;
5598 /* Move all children from OLD_PARENT to NEW_PARENT. */
5600 static void
5601 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5603 dw_die_ref c;
5604 new_parent->die_child = old_parent->die_child;
5605 old_parent->die_child = NULL;
5606 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5609 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5610 matches TAG. */
5612 static void
5613 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5615 dw_die_ref c;
5617 c = die->die_child;
5618 if (c) do {
5619 dw_die_ref prev = c;
5620 c = c->die_sib;
5621 while (c->die_tag == tag)
5623 remove_child_with_prev (c, prev);
5624 c->die_parent = NULL;
5625 /* Might have removed every child. */
5626 if (die->die_child == NULL)
5627 return;
5628 c = prev->die_sib;
5630 } while (c != die->die_child);
5633 /* Add a CHILD_DIE as the last child of DIE. */
5635 static void
5636 add_child_die (dw_die_ref die, dw_die_ref child_die)
5638 /* FIXME this should probably be an assert. */
5639 if (! die || ! child_die)
5640 return;
5641 gcc_assert (die != child_die);
5643 child_die->die_parent = die;
5644 if (die->die_child)
5646 child_die->die_sib = die->die_child->die_sib;
5647 die->die_child->die_sib = child_die;
5649 else
5650 child_die->die_sib = child_die;
5651 die->die_child = child_die;
5654 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5656 static void
5657 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5658 dw_die_ref after_die)
5660 gcc_assert (die
5661 && child_die
5662 && after_die
5663 && die->die_child
5664 && die != child_die);
5666 child_die->die_parent = die;
5667 child_die->die_sib = after_die->die_sib;
5668 after_die->die_sib = child_die;
5669 if (die->die_child == after_die)
5670 die->die_child = child_die;
5673 /* Unassociate CHILD from its parent, and make its parent be
5674 NEW_PARENT. */
5676 static void
5677 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5679 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5680 if (p->die_sib == child)
5682 remove_child_with_prev (child, p);
5683 break;
5685 add_child_die (new_parent, child);
5688 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5689 is the specification, to the end of PARENT's list of children.
5690 This is done by removing and re-adding it. */
5692 static void
5693 splice_child_die (dw_die_ref parent, dw_die_ref child)
5695 /* We want the declaration DIE from inside the class, not the
5696 specification DIE at toplevel. */
5697 if (child->die_parent != parent)
5699 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5701 if (tmp)
5702 child = tmp;
5705 gcc_assert (child->die_parent == parent
5706 || (child->die_parent
5707 == get_AT_ref (parent, DW_AT_specification)));
5709 reparent_child (child, parent);
5712 /* Create and return a new die with TAG_VALUE as tag. */
5714 static inline dw_die_ref
5715 new_die_raw (enum dwarf_tag tag_value)
5717 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5718 die->die_tag = tag_value;
5719 return die;
5722 /* Create and return a new die with a parent of PARENT_DIE. If
5723 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5724 associated tree T must be supplied to determine parenthood
5725 later. */
5727 static inline dw_die_ref
5728 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5730 dw_die_ref die = new_die_raw (tag_value);
5732 if (parent_die != NULL)
5733 add_child_die (parent_die, die);
5734 else
5736 limbo_die_node *limbo_node;
5738 /* No DIEs created after early dwarf should end up in limbo,
5739 because the limbo list should not persist past LTO
5740 streaming. */
5741 if (tag_value != DW_TAG_compile_unit
5742 /* These are allowed because they're generated while
5743 breaking out COMDAT units late. */
5744 && tag_value != DW_TAG_type_unit
5745 && tag_value != DW_TAG_skeleton_unit
5746 && !early_dwarf
5747 /* Allow nested functions to live in limbo because they will
5748 only temporarily live there, as decls_for_scope will fix
5749 them up. */
5750 && (TREE_CODE (t) != FUNCTION_DECL
5751 || !decl_function_context (t))
5752 /* Same as nested functions above but for types. Types that
5753 are local to a function will be fixed in
5754 decls_for_scope. */
5755 && (!RECORD_OR_UNION_TYPE_P (t)
5756 || !TYPE_CONTEXT (t)
5757 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5758 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5759 especially in the ltrans stage, but once we implement LTO
5760 dwarf streaming, we should remove this exception. */
5761 && !in_lto_p)
5763 fprintf (stderr, "symbol ended up in limbo too late:");
5764 debug_generic_stmt (t);
5765 gcc_unreachable ();
5768 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5769 limbo_node->die = die;
5770 limbo_node->created_for = t;
5771 limbo_node->next = limbo_die_list;
5772 limbo_die_list = limbo_node;
5775 return die;
5778 /* Return the DIE associated with the given type specifier. */
5780 static inline dw_die_ref
5781 lookup_type_die (tree type)
5783 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5784 if (die && die->removed)
5786 TYPE_SYMTAB_DIE (type) = NULL;
5787 return NULL;
5789 return die;
5792 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5793 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5794 anonymous type instead the one of the naming typedef. */
5796 static inline dw_die_ref
5797 strip_naming_typedef (tree type, dw_die_ref type_die)
5799 if (type
5800 && TREE_CODE (type) == RECORD_TYPE
5801 && type_die
5802 && type_die->die_tag == DW_TAG_typedef
5803 && is_naming_typedef_decl (TYPE_NAME (type)))
5804 type_die = get_AT_ref (type_die, DW_AT_type);
5805 return type_die;
5808 /* Like lookup_type_die, but if type is an anonymous type named by a
5809 typedef[1], return the DIE of the anonymous type instead the one of
5810 the naming typedef. This is because in gen_typedef_die, we did
5811 equate the anonymous struct named by the typedef with the DIE of
5812 the naming typedef. So by default, lookup_type_die on an anonymous
5813 struct yields the DIE of the naming typedef.
5815 [1]: Read the comment of is_naming_typedef_decl to learn about what
5816 a naming typedef is. */
5818 static inline dw_die_ref
5819 lookup_type_die_strip_naming_typedef (tree type)
5821 dw_die_ref die = lookup_type_die (type);
5822 return strip_naming_typedef (type, die);
5825 /* Equate a DIE to a given type specifier. */
5827 static inline void
5828 equate_type_number_to_die (tree type, dw_die_ref type_die)
5830 TYPE_SYMTAB_DIE (type) = type_die;
5833 /* Returns a hash value for X (which really is a die_struct). */
5835 inline hashval_t
5836 decl_die_hasher::hash (die_node *x)
5838 return (hashval_t) x->decl_id;
5841 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5843 inline bool
5844 decl_die_hasher::equal (die_node *x, tree y)
5846 return (x->decl_id == DECL_UID (y));
5849 /* Return the DIE associated with a given declaration. */
5851 static inline dw_die_ref
5852 lookup_decl_die (tree decl)
5854 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5855 NO_INSERT);
5856 if (!die)
5857 return NULL;
5858 if ((*die)->removed)
5860 decl_die_table->clear_slot (die);
5861 return NULL;
5863 return *die;
5867 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5868 style reference. Return true if we found one refering to a DIE for
5869 DECL, otherwise return false. */
5871 static bool
5872 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5873 unsigned HOST_WIDE_INT *off)
5875 dw_die_ref die;
5877 if (in_lto_p && !decl_die_table)
5878 return false;
5880 if (TREE_CODE (decl) == BLOCK)
5881 die = BLOCK_DIE (decl);
5882 else
5883 die = lookup_decl_die (decl);
5884 if (!die)
5885 return false;
5887 /* During WPA stage and incremental linking we currently use DIEs
5888 to store the decl <-> label + offset map. That's quite inefficient
5889 but it works for now. */
5890 if (in_lto_p)
5892 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5893 if (!ref)
5895 gcc_assert (die == comp_unit_die ());
5896 return false;
5898 *off = ref->die_offset;
5899 *sym = ref->die_id.die_symbol;
5900 return true;
5903 /* Similar to get_ref_die_offset_label, but using the "correct"
5904 label. */
5905 *off = die->die_offset;
5906 while (die->die_parent)
5907 die = die->die_parent;
5908 /* For the containing CU DIE we compute a die_symbol in
5909 compute_comp_unit_symbol. */
5910 gcc_assert (die->die_tag == DW_TAG_compile_unit
5911 && die->die_id.die_symbol != NULL);
5912 *sym = die->die_id.die_symbol;
5913 return true;
5916 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5918 static void
5919 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5920 const char *symbol, HOST_WIDE_INT offset)
5922 /* Create a fake DIE that contains the reference. Don't use
5923 new_die because we don't want to end up in the limbo list. */
5924 dw_die_ref ref = new_die_raw (die->die_tag);
5925 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5926 ref->die_offset = offset;
5927 ref->with_offset = 1;
5928 add_AT_die_ref (die, attr_kind, ref);
5931 /* Create a DIE for DECL if required and add a reference to a DIE
5932 at SYMBOL + OFFSET which contains attributes dumped early. */
5934 static void
5935 dwarf2out_register_external_die (tree decl, const char *sym,
5936 unsigned HOST_WIDE_INT off)
5938 if (debug_info_level == DINFO_LEVEL_NONE)
5939 return;
5941 if ((flag_wpa
5942 || flag_incremental_link == INCREMENTAL_LINK_LTO) && !decl_die_table)
5943 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5945 dw_die_ref die
5946 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5947 gcc_assert (!die);
5949 tree ctx;
5950 dw_die_ref parent = NULL;
5951 /* Need to lookup a DIE for the decls context - the containing
5952 function or translation unit. */
5953 if (TREE_CODE (decl) == BLOCK)
5955 ctx = BLOCK_SUPERCONTEXT (decl);
5956 /* ??? We do not output DIEs for all scopes thus skip as
5957 many DIEs as needed. */
5958 while (TREE_CODE (ctx) == BLOCK
5959 && !BLOCK_DIE (ctx))
5960 ctx = BLOCK_SUPERCONTEXT (ctx);
5962 else
5963 ctx = DECL_CONTEXT (decl);
5964 /* Peel types in the context stack. */
5965 while (ctx && TYPE_P (ctx))
5966 ctx = TYPE_CONTEXT (ctx);
5967 /* Likewise namespaces in case we do not want to emit DIEs for them. */
5968 if (debug_info_level <= DINFO_LEVEL_TERSE)
5969 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5970 ctx = DECL_CONTEXT (ctx);
5971 if (ctx)
5973 if (TREE_CODE (ctx) == BLOCK)
5974 parent = BLOCK_DIE (ctx);
5975 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5976 /* Keep the 1:1 association during WPA. */
5977 && !flag_wpa
5978 && flag_incremental_link != INCREMENTAL_LINK_LTO)
5979 /* Otherwise all late annotations go to the main CU which
5980 imports the original CUs. */
5981 parent = comp_unit_die ();
5982 else if (TREE_CODE (ctx) == FUNCTION_DECL
5983 && TREE_CODE (decl) != FUNCTION_DECL
5984 && TREE_CODE (decl) != PARM_DECL
5985 && TREE_CODE (decl) != RESULT_DECL
5986 && TREE_CODE (decl) != BLOCK)
5987 /* Leave function local entities parent determination to when
5988 we process scope vars. */
5990 else
5991 parent = lookup_decl_die (ctx);
5993 else
5994 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5995 Handle this case gracefully by globalizing stuff. */
5996 parent = comp_unit_die ();
5997 /* Create a DIE "stub". */
5998 switch (TREE_CODE (decl))
6000 case TRANSLATION_UNIT_DECL:
6001 if (! flag_wpa && flag_incremental_link != INCREMENTAL_LINK_LTO)
6003 die = comp_unit_die ();
6004 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
6005 add_AT_external_die_ref (import, DW_AT_import, sym, off);
6006 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6007 to create a DIE for the original CUs. */
6008 return;
6010 /* Keep the 1:1 association during WPA. */
6011 die = new_die (DW_TAG_compile_unit, NULL, decl);
6012 break;
6013 case NAMESPACE_DECL:
6014 if (is_fortran (decl))
6015 die = new_die (DW_TAG_module, parent, decl);
6016 else
6017 die = new_die (DW_TAG_namespace, parent, decl);
6018 break;
6019 case FUNCTION_DECL:
6020 die = new_die (DW_TAG_subprogram, parent, decl);
6021 break;
6022 case VAR_DECL:
6023 die = new_die (DW_TAG_variable, parent, decl);
6024 break;
6025 case RESULT_DECL:
6026 die = new_die (DW_TAG_variable, parent, decl);
6027 break;
6028 case PARM_DECL:
6029 die = new_die (DW_TAG_formal_parameter, parent, decl);
6030 break;
6031 case CONST_DECL:
6032 die = new_die (DW_TAG_constant, parent, decl);
6033 break;
6034 case LABEL_DECL:
6035 die = new_die (DW_TAG_label, parent, decl);
6036 break;
6037 case BLOCK:
6038 die = new_die (DW_TAG_lexical_block, parent, decl);
6039 break;
6040 default:
6041 gcc_unreachable ();
6043 if (TREE_CODE (decl) == BLOCK)
6044 BLOCK_DIE (decl) = die;
6045 else
6046 equate_decl_number_to_die (decl, die);
6048 add_desc_attribute (die, decl);
6050 /* Add a reference to the DIE providing early debug at $sym + off. */
6051 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6054 /* Returns a hash value for X (which really is a var_loc_list). */
6056 inline hashval_t
6057 decl_loc_hasher::hash (var_loc_list *x)
6059 return (hashval_t) x->decl_id;
6062 /* Return nonzero if decl_id of var_loc_list X is the same as
6063 UID of decl *Y. */
6065 inline bool
6066 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6068 return (x->decl_id == DECL_UID (y));
6071 /* Return the var_loc list associated with a given declaration. */
6073 static inline var_loc_list *
6074 lookup_decl_loc (const_tree decl)
6076 if (!decl_loc_table)
6077 return NULL;
6078 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6081 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6083 inline hashval_t
6084 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6086 return (hashval_t) x->decl_id;
6089 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6090 UID of decl *Y. */
6092 inline bool
6093 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6095 return (x->decl_id == DECL_UID (y));
6098 /* Equate a DIE to a particular declaration. */
6100 static void
6101 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6103 unsigned int decl_id = DECL_UID (decl);
6105 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6106 decl_die->decl_id = decl_id;
6109 /* Return how many bits covers PIECE EXPR_LIST. */
6111 static HOST_WIDE_INT
6112 decl_piece_bitsize (rtx piece)
6114 int ret = (int) GET_MODE (piece);
6115 if (ret)
6116 return ret;
6117 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6118 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6119 return INTVAL (XEXP (XEXP (piece, 0), 0));
6122 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6124 static rtx *
6125 decl_piece_varloc_ptr (rtx piece)
6127 if ((int) GET_MODE (piece))
6128 return &XEXP (piece, 0);
6129 else
6130 return &XEXP (XEXP (piece, 0), 1);
6133 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6134 Next is the chain of following piece nodes. */
6136 static rtx_expr_list *
6137 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6139 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6140 return alloc_EXPR_LIST (bitsize, loc_note, next);
6141 else
6142 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6143 GEN_INT (bitsize),
6144 loc_note), next);
6147 /* Return rtx that should be stored into loc field for
6148 LOC_NOTE and BITPOS/BITSIZE. */
6150 static rtx
6151 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6152 HOST_WIDE_INT bitsize)
6154 if (bitsize != -1)
6156 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6157 if (bitpos != 0)
6158 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6160 return loc_note;
6163 /* This function either modifies location piece list *DEST in
6164 place (if SRC and INNER is NULL), or copies location piece list
6165 *SRC to *DEST while modifying it. Location BITPOS is modified
6166 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6167 not copied and if needed some padding around it is added.
6168 When modifying in place, DEST should point to EXPR_LIST where
6169 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6170 to the start of the whole list and INNER points to the EXPR_LIST
6171 where earlier pieces cover PIECE_BITPOS bits. */
6173 static void
6174 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6175 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6176 HOST_WIDE_INT bitsize, rtx loc_note)
6178 HOST_WIDE_INT diff;
6179 bool copy = inner != NULL;
6181 if (copy)
6183 /* First copy all nodes preceding the current bitpos. */
6184 while (src != inner)
6186 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6187 decl_piece_bitsize (*src), NULL_RTX);
6188 dest = &XEXP (*dest, 1);
6189 src = &XEXP (*src, 1);
6192 /* Add padding if needed. */
6193 if (bitpos != piece_bitpos)
6195 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6196 copy ? NULL_RTX : *dest);
6197 dest = &XEXP (*dest, 1);
6199 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6201 gcc_assert (!copy);
6202 /* A piece with correct bitpos and bitsize already exist,
6203 just update the location for it and return. */
6204 *decl_piece_varloc_ptr (*dest) = loc_note;
6205 return;
6207 /* Add the piece that changed. */
6208 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6209 dest = &XEXP (*dest, 1);
6210 /* Skip over pieces that overlap it. */
6211 diff = bitpos - piece_bitpos + bitsize;
6212 if (!copy)
6213 src = dest;
6214 while (diff > 0 && *src)
6216 rtx piece = *src;
6217 diff -= decl_piece_bitsize (piece);
6218 if (copy)
6219 src = &XEXP (piece, 1);
6220 else
6222 *src = XEXP (piece, 1);
6223 free_EXPR_LIST_node (piece);
6226 /* Add padding if needed. */
6227 if (diff < 0 && *src)
6229 if (!copy)
6230 dest = src;
6231 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6232 dest = &XEXP (*dest, 1);
6234 if (!copy)
6235 return;
6236 /* Finally copy all nodes following it. */
6237 while (*src)
6239 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6240 decl_piece_bitsize (*src), NULL_RTX);
6241 dest = &XEXP (*dest, 1);
6242 src = &XEXP (*src, 1);
6246 /* Add a variable location node to the linked list for DECL. */
6248 static struct var_loc_node *
6249 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6251 unsigned int decl_id;
6252 var_loc_list *temp;
6253 struct var_loc_node *loc = NULL;
6254 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6256 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6258 tree realdecl = DECL_DEBUG_EXPR (decl);
6259 if (handled_component_p (realdecl)
6260 || (TREE_CODE (realdecl) == MEM_REF
6261 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6263 bool reverse;
6264 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6265 &bitsize, &reverse);
6266 if (!innerdecl
6267 || !DECL_P (innerdecl)
6268 || DECL_IGNORED_P (innerdecl)
6269 || TREE_STATIC (innerdecl)
6270 || bitsize == 0
6271 || bitpos + bitsize > 256)
6272 return NULL;
6273 decl = innerdecl;
6277 decl_id = DECL_UID (decl);
6278 var_loc_list **slot
6279 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6280 if (*slot == NULL)
6282 temp = ggc_cleared_alloc<var_loc_list> ();
6283 temp->decl_id = decl_id;
6284 *slot = temp;
6286 else
6287 temp = *slot;
6289 /* For PARM_DECLs try to keep around the original incoming value,
6290 even if that means we'll emit a zero-range .debug_loc entry. */
6291 if (temp->last
6292 && temp->first == temp->last
6293 && TREE_CODE (decl) == PARM_DECL
6294 && NOTE_P (temp->first->loc)
6295 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6296 && DECL_INCOMING_RTL (decl)
6297 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6298 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6299 == GET_CODE (DECL_INCOMING_RTL (decl))
6300 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6301 && (bitsize != -1
6302 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6303 NOTE_VAR_LOCATION_LOC (loc_note))
6304 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6305 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6307 loc = ggc_cleared_alloc<var_loc_node> ();
6308 temp->first->next = loc;
6309 temp->last = loc;
6310 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6312 else if (temp->last)
6314 struct var_loc_node *last = temp->last, *unused = NULL;
6315 rtx *piece_loc = NULL, last_loc_note;
6316 HOST_WIDE_INT piece_bitpos = 0;
6317 if (last->next)
6319 last = last->next;
6320 gcc_assert (last->next == NULL);
6322 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6324 piece_loc = &last->loc;
6327 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6328 if (piece_bitpos + cur_bitsize > bitpos)
6329 break;
6330 piece_bitpos += cur_bitsize;
6331 piece_loc = &XEXP (*piece_loc, 1);
6333 while (*piece_loc);
6335 /* TEMP->LAST here is either pointer to the last but one or
6336 last element in the chained list, LAST is pointer to the
6337 last element. */
6338 if (label && strcmp (last->label, label) == 0 && last->view == view)
6340 /* For SRA optimized variables if there weren't any real
6341 insns since last note, just modify the last node. */
6342 if (piece_loc != NULL)
6344 adjust_piece_list (piece_loc, NULL, NULL,
6345 bitpos, piece_bitpos, bitsize, loc_note);
6346 return NULL;
6348 /* If the last note doesn't cover any instructions, remove it. */
6349 if (temp->last != last)
6351 temp->last->next = NULL;
6352 unused = last;
6353 last = temp->last;
6354 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6356 else
6358 gcc_assert (temp->first == temp->last
6359 || (temp->first->next == temp->last
6360 && TREE_CODE (decl) == PARM_DECL));
6361 memset (temp->last, '\0', sizeof (*temp->last));
6362 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6363 return temp->last;
6366 if (bitsize == -1 && NOTE_P (last->loc))
6367 last_loc_note = last->loc;
6368 else if (piece_loc != NULL
6369 && *piece_loc != NULL_RTX
6370 && piece_bitpos == bitpos
6371 && decl_piece_bitsize (*piece_loc) == bitsize)
6372 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6373 else
6374 last_loc_note = NULL_RTX;
6375 /* If the current location is the same as the end of the list,
6376 and either both or neither of the locations is uninitialized,
6377 we have nothing to do. */
6378 if (last_loc_note == NULL_RTX
6379 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6380 NOTE_VAR_LOCATION_LOC (loc_note)))
6381 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6382 != NOTE_VAR_LOCATION_STATUS (loc_note))
6383 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6384 == VAR_INIT_STATUS_UNINITIALIZED)
6385 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6386 == VAR_INIT_STATUS_UNINITIALIZED))))
6388 /* Add LOC to the end of list and update LAST. If the last
6389 element of the list has been removed above, reuse its
6390 memory for the new node, otherwise allocate a new one. */
6391 if (unused)
6393 loc = unused;
6394 memset (loc, '\0', sizeof (*loc));
6396 else
6397 loc = ggc_cleared_alloc<var_loc_node> ();
6398 if (bitsize == -1 || piece_loc == NULL)
6399 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6400 else
6401 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6402 bitpos, piece_bitpos, bitsize, loc_note);
6403 last->next = loc;
6404 /* Ensure TEMP->LAST will point either to the new last but one
6405 element of the chain, or to the last element in it. */
6406 if (last != temp->last)
6407 temp->last = last;
6409 else if (unused)
6410 ggc_free (unused);
6412 else
6414 loc = ggc_cleared_alloc<var_loc_node> ();
6415 temp->first = loc;
6416 temp->last = loc;
6417 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6419 return loc;
6422 /* Keep track of the number of spaces used to indent the
6423 output of the debugging routines that print the structure of
6424 the DIE internal representation. */
6425 static int print_indent;
6427 /* Indent the line the number of spaces given by print_indent. */
6429 static inline void
6430 print_spaces (FILE *outfile)
6432 fprintf (outfile, "%*s", print_indent, "");
6435 /* Print a type signature in hex. */
6437 static inline void
6438 print_signature (FILE *outfile, char *sig)
6440 int i;
6442 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6443 fprintf (outfile, "%02x", sig[i] & 0xff);
6446 static inline void
6447 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6449 if (discr_value->pos)
6450 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6451 else
6452 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6455 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6457 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6458 RECURSE, output location descriptor operations. */
6460 static void
6461 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6463 switch (val->val_class)
6465 case dw_val_class_addr:
6466 fprintf (outfile, "address");
6467 break;
6468 case dw_val_class_offset:
6469 fprintf (outfile, "offset");
6470 break;
6471 case dw_val_class_loc:
6472 fprintf (outfile, "location descriptor");
6473 if (val->v.val_loc == NULL)
6474 fprintf (outfile, " -> <null>\n");
6475 else if (recurse)
6477 fprintf (outfile, ":\n");
6478 print_indent += 4;
6479 print_loc_descr (val->v.val_loc, outfile);
6480 print_indent -= 4;
6482 else
6484 if (flag_dump_noaddr || flag_dump_unnumbered)
6485 fprintf (outfile, " #\n");
6486 else
6487 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6489 break;
6490 case dw_val_class_loc_list:
6491 fprintf (outfile, "location list -> label:%s",
6492 val->v.val_loc_list->ll_symbol);
6493 break;
6494 case dw_val_class_view_list:
6495 val = view_list_to_loc_list_val_node (val);
6496 fprintf (outfile, "location list with views -> labels:%s and %s",
6497 val->v.val_loc_list->ll_symbol,
6498 val->v.val_loc_list->vl_symbol);
6499 break;
6500 case dw_val_class_range_list:
6501 fprintf (outfile, "range list");
6502 break;
6503 case dw_val_class_const:
6504 case dw_val_class_const_implicit:
6505 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6506 break;
6507 case dw_val_class_unsigned_const:
6508 case dw_val_class_unsigned_const_implicit:
6509 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6510 break;
6511 case dw_val_class_const_double:
6512 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6513 HOST_WIDE_INT_PRINT_UNSIGNED")",
6514 val->v.val_double.high,
6515 val->v.val_double.low);
6516 break;
6517 case dw_val_class_wide_int:
6519 int i = val->v.val_wide->get_len ();
6520 fprintf (outfile, "constant (");
6521 gcc_assert (i > 0);
6522 if (val->v.val_wide->elt (i - 1) == 0)
6523 fprintf (outfile, "0x");
6524 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6525 val->v.val_wide->elt (--i));
6526 while (--i >= 0)
6527 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6528 val->v.val_wide->elt (i));
6529 fprintf (outfile, ")");
6530 break;
6532 case dw_val_class_vec:
6533 fprintf (outfile, "floating-point or vector constant");
6534 break;
6535 case dw_val_class_flag:
6536 fprintf (outfile, "%u", val->v.val_flag);
6537 break;
6538 case dw_val_class_die_ref:
6539 if (val->v.val_die_ref.die != NULL)
6541 dw_die_ref die = val->v.val_die_ref.die;
6543 if (die->comdat_type_p)
6545 fprintf (outfile, "die -> signature: ");
6546 print_signature (outfile,
6547 die->die_id.die_type_node->signature);
6549 else if (die->die_id.die_symbol)
6551 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6552 if (die->with_offset)
6553 fprintf (outfile, " + %ld", die->die_offset);
6555 else
6556 fprintf (outfile, "die -> %ld", die->die_offset);
6557 if (flag_dump_noaddr || flag_dump_unnumbered)
6558 fprintf (outfile, " #");
6559 else
6560 fprintf (outfile, " (%p)", (void *) die);
6562 else
6563 fprintf (outfile, "die -> <null>");
6564 break;
6565 case dw_val_class_vms_delta:
6566 fprintf (outfile, "delta: @slotcount(%s-%s)",
6567 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6568 break;
6569 case dw_val_class_symview:
6570 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6571 break;
6572 case dw_val_class_lbl_id:
6573 case dw_val_class_lineptr:
6574 case dw_val_class_macptr:
6575 case dw_val_class_loclistsptr:
6576 case dw_val_class_high_pc:
6577 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6578 break;
6579 case dw_val_class_str:
6580 if (val->v.val_str->str != NULL)
6581 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6582 else
6583 fprintf (outfile, "<null>");
6584 break;
6585 case dw_val_class_file:
6586 case dw_val_class_file_implicit:
6587 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6588 val->v.val_file->emitted_number);
6589 break;
6590 case dw_val_class_data8:
6592 int i;
6594 for (i = 0; i < 8; i++)
6595 fprintf (outfile, "%02x", val->v.val_data8[i]);
6596 break;
6598 case dw_val_class_discr_value:
6599 print_discr_value (outfile, &val->v.val_discr_value);
6600 break;
6601 case dw_val_class_discr_list:
6602 for (dw_discr_list_ref node = val->v.val_discr_list;
6603 node != NULL;
6604 node = node->dw_discr_next)
6606 if (node->dw_discr_range)
6608 fprintf (outfile, " .. ");
6609 print_discr_value (outfile, &node->dw_discr_lower_bound);
6610 print_discr_value (outfile, &node->dw_discr_upper_bound);
6612 else
6613 print_discr_value (outfile, &node->dw_discr_lower_bound);
6615 if (node->dw_discr_next != NULL)
6616 fprintf (outfile, " | ");
6618 default:
6619 break;
6623 /* Likewise, for a DIE attribute. */
6625 static void
6626 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6628 print_dw_val (&a->dw_attr_val, recurse, outfile);
6632 /* Print the list of operands in the LOC location description to OUTFILE. This
6633 routine is a debugging aid only. */
6635 static void
6636 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6638 dw_loc_descr_ref l = loc;
6640 if (loc == NULL)
6642 print_spaces (outfile);
6643 fprintf (outfile, "<null>\n");
6644 return;
6647 for (l = loc; l != NULL; l = l->dw_loc_next)
6649 print_spaces (outfile);
6650 if (flag_dump_noaddr || flag_dump_unnumbered)
6651 fprintf (outfile, "#");
6652 else
6653 fprintf (outfile, "(%p)", (void *) l);
6654 fprintf (outfile, " %s",
6655 dwarf_stack_op_name (l->dw_loc_opc));
6656 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6658 fprintf (outfile, " ");
6659 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6661 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6663 fprintf (outfile, ", ");
6664 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6666 fprintf (outfile, "\n");
6670 /* Print the information associated with a given DIE, and its children.
6671 This routine is a debugging aid only. */
6673 static void
6674 print_die (dw_die_ref die, FILE *outfile)
6676 dw_attr_node *a;
6677 dw_die_ref c;
6678 unsigned ix;
6680 print_spaces (outfile);
6681 fprintf (outfile, "DIE %4ld: %s ",
6682 die->die_offset, dwarf_tag_name (die->die_tag));
6683 if (flag_dump_noaddr || flag_dump_unnumbered)
6684 fprintf (outfile, "#\n");
6685 else
6686 fprintf (outfile, "(%p)\n", (void*) die);
6687 print_spaces (outfile);
6688 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6689 fprintf (outfile, " offset: %ld", die->die_offset);
6690 fprintf (outfile, " mark: %d\n", die->die_mark);
6692 if (die->comdat_type_p)
6694 print_spaces (outfile);
6695 fprintf (outfile, " signature: ");
6696 print_signature (outfile, die->die_id.die_type_node->signature);
6697 fprintf (outfile, "\n");
6700 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6702 print_spaces (outfile);
6703 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6705 print_attribute (a, true, outfile);
6706 fprintf (outfile, "\n");
6709 if (die->die_child != NULL)
6711 print_indent += 4;
6712 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6713 print_indent -= 4;
6715 if (print_indent == 0)
6716 fprintf (outfile, "\n");
6719 /* Print the list of operations in the LOC location description. */
6721 DEBUG_FUNCTION void
6722 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6724 print_loc_descr (loc, stderr);
6727 /* Print the information collected for a given DIE. */
6729 DEBUG_FUNCTION void
6730 debug_dwarf_die (dw_die_ref die)
6732 print_die (die, stderr);
6735 DEBUG_FUNCTION void
6736 debug (die_struct &ref)
6738 print_die (&ref, stderr);
6741 DEBUG_FUNCTION void
6742 debug (die_struct *ptr)
6744 if (ptr)
6745 debug (*ptr);
6746 else
6747 fprintf (stderr, "<nil>\n");
6751 /* Print all DWARF information collected for the compilation unit.
6752 This routine is a debugging aid only. */
6754 DEBUG_FUNCTION void
6755 debug_dwarf (void)
6757 print_indent = 0;
6758 print_die (comp_unit_die (), stderr);
6761 /* Verify the DIE tree structure. */
6763 DEBUG_FUNCTION void
6764 verify_die (dw_die_ref die)
6766 gcc_assert (!die->die_mark);
6767 if (die->die_parent == NULL
6768 && die->die_sib == NULL)
6769 return;
6770 /* Verify the die_sib list is cyclic. */
6771 dw_die_ref x = die;
6774 x->die_mark = 1;
6775 x = x->die_sib;
6777 while (x && !x->die_mark);
6778 gcc_assert (x == die);
6779 x = die;
6782 /* Verify all dies have the same parent. */
6783 gcc_assert (x->die_parent == die->die_parent);
6784 if (x->die_child)
6786 /* Verify the child has the proper parent and recurse. */
6787 gcc_assert (x->die_child->die_parent == x);
6788 verify_die (x->die_child);
6790 x->die_mark = 0;
6791 x = x->die_sib;
6793 while (x && x->die_mark);
6796 /* Sanity checks on DIEs. */
6798 static void
6799 check_die (dw_die_ref die)
6801 unsigned ix;
6802 dw_attr_node *a;
6803 bool inline_found = false;
6804 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6805 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6806 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6808 switch (a->dw_attr)
6810 case DW_AT_inline:
6811 if (a->dw_attr_val.v.val_unsigned)
6812 inline_found = true;
6813 break;
6814 case DW_AT_location:
6815 ++n_location;
6816 break;
6817 case DW_AT_low_pc:
6818 ++n_low_pc;
6819 break;
6820 case DW_AT_high_pc:
6821 ++n_high_pc;
6822 break;
6823 case DW_AT_artificial:
6824 ++n_artificial;
6825 break;
6826 case DW_AT_decl_column:
6827 ++n_decl_column;
6828 break;
6829 case DW_AT_decl_line:
6830 ++n_decl_line;
6831 break;
6832 case DW_AT_decl_file:
6833 ++n_decl_file;
6834 break;
6835 default:
6836 break;
6839 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6840 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6842 fprintf (stderr, "Duplicate attributes in DIE:\n");
6843 debug_dwarf_die (die);
6844 gcc_unreachable ();
6846 if (inline_found)
6848 /* A debugging information entry that is a member of an abstract
6849 instance tree [that has DW_AT_inline] should not contain any
6850 attributes which describe aspects of the subroutine which vary
6851 between distinct inlined expansions or distinct out-of-line
6852 expansions. */
6853 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6854 gcc_assert (a->dw_attr != DW_AT_low_pc
6855 && a->dw_attr != DW_AT_high_pc
6856 && a->dw_attr != DW_AT_location
6857 && a->dw_attr != DW_AT_frame_base
6858 && a->dw_attr != DW_AT_call_all_calls
6859 && a->dw_attr != DW_AT_GNU_all_call_sites);
6863 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6864 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6865 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6867 /* Calculate the checksum of a location expression. */
6869 static inline void
6870 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6872 int tem;
6873 inchash::hash hstate;
6874 hashval_t hash;
6876 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6877 CHECKSUM (tem);
6878 hash_loc_operands (loc, hstate);
6879 hash = hstate.end();
6880 CHECKSUM (hash);
6883 /* Calculate the checksum of an attribute. */
6885 static void
6886 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6888 dw_loc_descr_ref loc;
6889 rtx r;
6891 CHECKSUM (at->dw_attr);
6893 /* We don't care that this was compiled with a different compiler
6894 snapshot; if the output is the same, that's what matters. */
6895 if (at->dw_attr == DW_AT_producer)
6896 return;
6898 switch (AT_class (at))
6900 case dw_val_class_const:
6901 case dw_val_class_const_implicit:
6902 CHECKSUM (at->dw_attr_val.v.val_int);
6903 break;
6904 case dw_val_class_unsigned_const:
6905 case dw_val_class_unsigned_const_implicit:
6906 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6907 break;
6908 case dw_val_class_const_double:
6909 CHECKSUM (at->dw_attr_val.v.val_double);
6910 break;
6911 case dw_val_class_wide_int:
6912 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6913 get_full_len (*at->dw_attr_val.v.val_wide)
6914 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6915 break;
6916 case dw_val_class_vec:
6917 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6918 (at->dw_attr_val.v.val_vec.length
6919 * at->dw_attr_val.v.val_vec.elt_size));
6920 break;
6921 case dw_val_class_flag:
6922 CHECKSUM (at->dw_attr_val.v.val_flag);
6923 break;
6924 case dw_val_class_str:
6925 CHECKSUM_STRING (AT_string (at));
6926 break;
6928 case dw_val_class_addr:
6929 r = AT_addr (at);
6930 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6931 CHECKSUM_STRING (XSTR (r, 0));
6932 break;
6934 case dw_val_class_offset:
6935 CHECKSUM (at->dw_attr_val.v.val_offset);
6936 break;
6938 case dw_val_class_loc:
6939 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6940 loc_checksum (loc, ctx);
6941 break;
6943 case dw_val_class_die_ref:
6944 die_checksum (AT_ref (at), ctx, mark);
6945 break;
6947 case dw_val_class_fde_ref:
6948 case dw_val_class_vms_delta:
6949 case dw_val_class_symview:
6950 case dw_val_class_lbl_id:
6951 case dw_val_class_lineptr:
6952 case dw_val_class_macptr:
6953 case dw_val_class_loclistsptr:
6954 case dw_val_class_high_pc:
6955 break;
6957 case dw_val_class_file:
6958 case dw_val_class_file_implicit:
6959 CHECKSUM_STRING (AT_file (at)->filename);
6960 break;
6962 case dw_val_class_data8:
6963 CHECKSUM (at->dw_attr_val.v.val_data8);
6964 break;
6966 default:
6967 break;
6971 /* Calculate the checksum of a DIE. */
6973 static void
6974 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6976 dw_die_ref c;
6977 dw_attr_node *a;
6978 unsigned ix;
6980 /* To avoid infinite recursion. */
6981 if (die->die_mark)
6983 CHECKSUM (die->die_mark);
6984 return;
6986 die->die_mark = ++(*mark);
6988 CHECKSUM (die->die_tag);
6990 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6991 attr_checksum (a, ctx, mark);
6993 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6996 #undef CHECKSUM
6997 #undef CHECKSUM_BLOCK
6998 #undef CHECKSUM_STRING
7000 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7001 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7002 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7003 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7004 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7005 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7006 #define CHECKSUM_ATTR(FOO) \
7007 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7009 /* Calculate the checksum of a number in signed LEB128 format. */
7011 static void
7012 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7014 unsigned char byte;
7015 bool more;
7017 while (1)
7019 byte = (value & 0x7f);
7020 value >>= 7;
7021 more = !((value == 0 && (byte & 0x40) == 0)
7022 || (value == -1 && (byte & 0x40) != 0));
7023 if (more)
7024 byte |= 0x80;
7025 CHECKSUM (byte);
7026 if (!more)
7027 break;
7031 /* Calculate the checksum of a number in unsigned LEB128 format. */
7033 static void
7034 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7036 while (1)
7038 unsigned char byte = (value & 0x7f);
7039 value >>= 7;
7040 if (value != 0)
7041 /* More bytes to follow. */
7042 byte |= 0x80;
7043 CHECKSUM (byte);
7044 if (value == 0)
7045 break;
7049 /* Checksum the context of the DIE. This adds the names of any
7050 surrounding namespaces or structures to the checksum. */
7052 static void
7053 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7055 const char *name;
7056 dw_die_ref spec;
7057 int tag = die->die_tag;
7059 if (tag != DW_TAG_namespace
7060 && tag != DW_TAG_structure_type
7061 && tag != DW_TAG_class_type)
7062 return;
7064 name = get_AT_string (die, DW_AT_name);
7066 spec = get_AT_ref (die, DW_AT_specification);
7067 if (spec != NULL)
7068 die = spec;
7070 if (die->die_parent != NULL)
7071 checksum_die_context (die->die_parent, ctx);
7073 CHECKSUM_ULEB128 ('C');
7074 CHECKSUM_ULEB128 (tag);
7075 if (name != NULL)
7076 CHECKSUM_STRING (name);
7079 /* Calculate the checksum of a location expression. */
7081 static inline void
7082 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7084 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7085 were emitted as a DW_FORM_sdata instead of a location expression. */
7086 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7088 CHECKSUM_ULEB128 (DW_FORM_sdata);
7089 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7090 return;
7093 /* Otherwise, just checksum the raw location expression. */
7094 while (loc != NULL)
7096 inchash::hash hstate;
7097 hashval_t hash;
7099 CHECKSUM_ULEB128 (loc->dtprel);
7100 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7101 hash_loc_operands (loc, hstate);
7102 hash = hstate.end ();
7103 CHECKSUM (hash);
7104 loc = loc->dw_loc_next;
7108 /* Calculate the checksum of an attribute. */
7110 static void
7111 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7112 struct md5_ctx *ctx, int *mark)
7114 dw_loc_descr_ref loc;
7115 rtx r;
7117 if (AT_class (at) == dw_val_class_die_ref)
7119 dw_die_ref target_die = AT_ref (at);
7121 /* For pointer and reference types, we checksum only the (qualified)
7122 name of the target type (if there is a name). For friend entries,
7123 we checksum only the (qualified) name of the target type or function.
7124 This allows the checksum to remain the same whether the target type
7125 is complete or not. */
7126 if ((at->dw_attr == DW_AT_type
7127 && (tag == DW_TAG_pointer_type
7128 || tag == DW_TAG_reference_type
7129 || tag == DW_TAG_rvalue_reference_type
7130 || tag == DW_TAG_ptr_to_member_type))
7131 || (at->dw_attr == DW_AT_friend
7132 && tag == DW_TAG_friend))
7134 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7136 if (name_attr != NULL)
7138 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7140 if (decl == NULL)
7141 decl = target_die;
7142 CHECKSUM_ULEB128 ('N');
7143 CHECKSUM_ULEB128 (at->dw_attr);
7144 if (decl->die_parent != NULL)
7145 checksum_die_context (decl->die_parent, ctx);
7146 CHECKSUM_ULEB128 ('E');
7147 CHECKSUM_STRING (AT_string (name_attr));
7148 return;
7152 /* For all other references to another DIE, we check to see if the
7153 target DIE has already been visited. If it has, we emit a
7154 backward reference; if not, we descend recursively. */
7155 if (target_die->die_mark > 0)
7157 CHECKSUM_ULEB128 ('R');
7158 CHECKSUM_ULEB128 (at->dw_attr);
7159 CHECKSUM_ULEB128 (target_die->die_mark);
7161 else
7163 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7165 if (decl == NULL)
7166 decl = target_die;
7167 target_die->die_mark = ++(*mark);
7168 CHECKSUM_ULEB128 ('T');
7169 CHECKSUM_ULEB128 (at->dw_attr);
7170 if (decl->die_parent != NULL)
7171 checksum_die_context (decl->die_parent, ctx);
7172 die_checksum_ordered (target_die, ctx, mark);
7174 return;
7177 CHECKSUM_ULEB128 ('A');
7178 CHECKSUM_ULEB128 (at->dw_attr);
7180 switch (AT_class (at))
7182 case dw_val_class_const:
7183 case dw_val_class_const_implicit:
7184 CHECKSUM_ULEB128 (DW_FORM_sdata);
7185 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7186 break;
7188 case dw_val_class_unsigned_const:
7189 case dw_val_class_unsigned_const_implicit:
7190 CHECKSUM_ULEB128 (DW_FORM_sdata);
7191 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7192 break;
7194 case dw_val_class_const_double:
7195 CHECKSUM_ULEB128 (DW_FORM_block);
7196 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7197 CHECKSUM (at->dw_attr_val.v.val_double);
7198 break;
7200 case dw_val_class_wide_int:
7201 CHECKSUM_ULEB128 (DW_FORM_block);
7202 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7203 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7204 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7205 get_full_len (*at->dw_attr_val.v.val_wide)
7206 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7207 break;
7209 case dw_val_class_vec:
7210 CHECKSUM_ULEB128 (DW_FORM_block);
7211 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7212 * at->dw_attr_val.v.val_vec.elt_size);
7213 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7214 (at->dw_attr_val.v.val_vec.length
7215 * at->dw_attr_val.v.val_vec.elt_size));
7216 break;
7218 case dw_val_class_flag:
7219 CHECKSUM_ULEB128 (DW_FORM_flag);
7220 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7221 break;
7223 case dw_val_class_str:
7224 CHECKSUM_ULEB128 (DW_FORM_string);
7225 CHECKSUM_STRING (AT_string (at));
7226 break;
7228 case dw_val_class_addr:
7229 r = AT_addr (at);
7230 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7231 CHECKSUM_ULEB128 (DW_FORM_string);
7232 CHECKSUM_STRING (XSTR (r, 0));
7233 break;
7235 case dw_val_class_offset:
7236 CHECKSUM_ULEB128 (DW_FORM_sdata);
7237 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7238 break;
7240 case dw_val_class_loc:
7241 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7242 loc_checksum_ordered (loc, ctx);
7243 break;
7245 case dw_val_class_fde_ref:
7246 case dw_val_class_symview:
7247 case dw_val_class_lbl_id:
7248 case dw_val_class_lineptr:
7249 case dw_val_class_macptr:
7250 case dw_val_class_loclistsptr:
7251 case dw_val_class_high_pc:
7252 break;
7254 case dw_val_class_file:
7255 case dw_val_class_file_implicit:
7256 CHECKSUM_ULEB128 (DW_FORM_string);
7257 CHECKSUM_STRING (AT_file (at)->filename);
7258 break;
7260 case dw_val_class_data8:
7261 CHECKSUM (at->dw_attr_val.v.val_data8);
7262 break;
7264 default:
7265 break;
7269 struct checksum_attributes
7271 dw_attr_node *at_name;
7272 dw_attr_node *at_type;
7273 dw_attr_node *at_friend;
7274 dw_attr_node *at_accessibility;
7275 dw_attr_node *at_address_class;
7276 dw_attr_node *at_alignment;
7277 dw_attr_node *at_allocated;
7278 dw_attr_node *at_artificial;
7279 dw_attr_node *at_associated;
7280 dw_attr_node *at_binary_scale;
7281 dw_attr_node *at_bit_offset;
7282 dw_attr_node *at_bit_size;
7283 dw_attr_node *at_bit_stride;
7284 dw_attr_node *at_byte_size;
7285 dw_attr_node *at_byte_stride;
7286 dw_attr_node *at_const_value;
7287 dw_attr_node *at_containing_type;
7288 dw_attr_node *at_count;
7289 dw_attr_node *at_data_location;
7290 dw_attr_node *at_data_member_location;
7291 dw_attr_node *at_decimal_scale;
7292 dw_attr_node *at_decimal_sign;
7293 dw_attr_node *at_default_value;
7294 dw_attr_node *at_digit_count;
7295 dw_attr_node *at_discr;
7296 dw_attr_node *at_discr_list;
7297 dw_attr_node *at_discr_value;
7298 dw_attr_node *at_encoding;
7299 dw_attr_node *at_endianity;
7300 dw_attr_node *at_explicit;
7301 dw_attr_node *at_is_optional;
7302 dw_attr_node *at_location;
7303 dw_attr_node *at_lower_bound;
7304 dw_attr_node *at_mutable;
7305 dw_attr_node *at_ordering;
7306 dw_attr_node *at_picture_string;
7307 dw_attr_node *at_prototyped;
7308 dw_attr_node *at_small;
7309 dw_attr_node *at_segment;
7310 dw_attr_node *at_string_length;
7311 dw_attr_node *at_string_length_bit_size;
7312 dw_attr_node *at_string_length_byte_size;
7313 dw_attr_node *at_threads_scaled;
7314 dw_attr_node *at_upper_bound;
7315 dw_attr_node *at_use_location;
7316 dw_attr_node *at_use_UTF8;
7317 dw_attr_node *at_variable_parameter;
7318 dw_attr_node *at_virtuality;
7319 dw_attr_node *at_visibility;
7320 dw_attr_node *at_vtable_elem_location;
7323 /* Collect the attributes that we will want to use for the checksum. */
7325 static void
7326 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7328 dw_attr_node *a;
7329 unsigned ix;
7331 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7333 switch (a->dw_attr)
7335 case DW_AT_name:
7336 attrs->at_name = a;
7337 break;
7338 case DW_AT_type:
7339 attrs->at_type = a;
7340 break;
7341 case DW_AT_friend:
7342 attrs->at_friend = a;
7343 break;
7344 case DW_AT_accessibility:
7345 attrs->at_accessibility = a;
7346 break;
7347 case DW_AT_address_class:
7348 attrs->at_address_class = a;
7349 break;
7350 case DW_AT_alignment:
7351 attrs->at_alignment = a;
7352 break;
7353 case DW_AT_allocated:
7354 attrs->at_allocated = a;
7355 break;
7356 case DW_AT_artificial:
7357 attrs->at_artificial = a;
7358 break;
7359 case DW_AT_associated:
7360 attrs->at_associated = a;
7361 break;
7362 case DW_AT_binary_scale:
7363 attrs->at_binary_scale = a;
7364 break;
7365 case DW_AT_bit_offset:
7366 attrs->at_bit_offset = a;
7367 break;
7368 case DW_AT_bit_size:
7369 attrs->at_bit_size = a;
7370 break;
7371 case DW_AT_bit_stride:
7372 attrs->at_bit_stride = a;
7373 break;
7374 case DW_AT_byte_size:
7375 attrs->at_byte_size = a;
7376 break;
7377 case DW_AT_byte_stride:
7378 attrs->at_byte_stride = a;
7379 break;
7380 case DW_AT_const_value:
7381 attrs->at_const_value = a;
7382 break;
7383 case DW_AT_containing_type:
7384 attrs->at_containing_type = a;
7385 break;
7386 case DW_AT_count:
7387 attrs->at_count = a;
7388 break;
7389 case DW_AT_data_location:
7390 attrs->at_data_location = a;
7391 break;
7392 case DW_AT_data_member_location:
7393 attrs->at_data_member_location = a;
7394 break;
7395 case DW_AT_decimal_scale:
7396 attrs->at_decimal_scale = a;
7397 break;
7398 case DW_AT_decimal_sign:
7399 attrs->at_decimal_sign = a;
7400 break;
7401 case DW_AT_default_value:
7402 attrs->at_default_value = a;
7403 break;
7404 case DW_AT_digit_count:
7405 attrs->at_digit_count = a;
7406 break;
7407 case DW_AT_discr:
7408 attrs->at_discr = a;
7409 break;
7410 case DW_AT_discr_list:
7411 attrs->at_discr_list = a;
7412 break;
7413 case DW_AT_discr_value:
7414 attrs->at_discr_value = a;
7415 break;
7416 case DW_AT_encoding:
7417 attrs->at_encoding = a;
7418 break;
7419 case DW_AT_endianity:
7420 attrs->at_endianity = a;
7421 break;
7422 case DW_AT_explicit:
7423 attrs->at_explicit = a;
7424 break;
7425 case DW_AT_is_optional:
7426 attrs->at_is_optional = a;
7427 break;
7428 case DW_AT_location:
7429 attrs->at_location = a;
7430 break;
7431 case DW_AT_lower_bound:
7432 attrs->at_lower_bound = a;
7433 break;
7434 case DW_AT_mutable:
7435 attrs->at_mutable = a;
7436 break;
7437 case DW_AT_ordering:
7438 attrs->at_ordering = a;
7439 break;
7440 case DW_AT_picture_string:
7441 attrs->at_picture_string = a;
7442 break;
7443 case DW_AT_prototyped:
7444 attrs->at_prototyped = a;
7445 break;
7446 case DW_AT_small:
7447 attrs->at_small = a;
7448 break;
7449 case DW_AT_segment:
7450 attrs->at_segment = a;
7451 break;
7452 case DW_AT_string_length:
7453 attrs->at_string_length = a;
7454 break;
7455 case DW_AT_string_length_bit_size:
7456 attrs->at_string_length_bit_size = a;
7457 break;
7458 case DW_AT_string_length_byte_size:
7459 attrs->at_string_length_byte_size = a;
7460 break;
7461 case DW_AT_threads_scaled:
7462 attrs->at_threads_scaled = a;
7463 break;
7464 case DW_AT_upper_bound:
7465 attrs->at_upper_bound = a;
7466 break;
7467 case DW_AT_use_location:
7468 attrs->at_use_location = a;
7469 break;
7470 case DW_AT_use_UTF8:
7471 attrs->at_use_UTF8 = a;
7472 break;
7473 case DW_AT_variable_parameter:
7474 attrs->at_variable_parameter = a;
7475 break;
7476 case DW_AT_virtuality:
7477 attrs->at_virtuality = a;
7478 break;
7479 case DW_AT_visibility:
7480 attrs->at_visibility = a;
7481 break;
7482 case DW_AT_vtable_elem_location:
7483 attrs->at_vtable_elem_location = a;
7484 break;
7485 default:
7486 break;
7491 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7493 static void
7494 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7496 dw_die_ref c;
7497 dw_die_ref decl;
7498 struct checksum_attributes attrs;
7500 CHECKSUM_ULEB128 ('D');
7501 CHECKSUM_ULEB128 (die->die_tag);
7503 memset (&attrs, 0, sizeof (attrs));
7505 decl = get_AT_ref (die, DW_AT_specification);
7506 if (decl != NULL)
7507 collect_checksum_attributes (&attrs, decl);
7508 collect_checksum_attributes (&attrs, die);
7510 CHECKSUM_ATTR (attrs.at_name);
7511 CHECKSUM_ATTR (attrs.at_accessibility);
7512 CHECKSUM_ATTR (attrs.at_address_class);
7513 CHECKSUM_ATTR (attrs.at_allocated);
7514 CHECKSUM_ATTR (attrs.at_artificial);
7515 CHECKSUM_ATTR (attrs.at_associated);
7516 CHECKSUM_ATTR (attrs.at_binary_scale);
7517 CHECKSUM_ATTR (attrs.at_bit_offset);
7518 CHECKSUM_ATTR (attrs.at_bit_size);
7519 CHECKSUM_ATTR (attrs.at_bit_stride);
7520 CHECKSUM_ATTR (attrs.at_byte_size);
7521 CHECKSUM_ATTR (attrs.at_byte_stride);
7522 CHECKSUM_ATTR (attrs.at_const_value);
7523 CHECKSUM_ATTR (attrs.at_containing_type);
7524 CHECKSUM_ATTR (attrs.at_count);
7525 CHECKSUM_ATTR (attrs.at_data_location);
7526 CHECKSUM_ATTR (attrs.at_data_member_location);
7527 CHECKSUM_ATTR (attrs.at_decimal_scale);
7528 CHECKSUM_ATTR (attrs.at_decimal_sign);
7529 CHECKSUM_ATTR (attrs.at_default_value);
7530 CHECKSUM_ATTR (attrs.at_digit_count);
7531 CHECKSUM_ATTR (attrs.at_discr);
7532 CHECKSUM_ATTR (attrs.at_discr_list);
7533 CHECKSUM_ATTR (attrs.at_discr_value);
7534 CHECKSUM_ATTR (attrs.at_encoding);
7535 CHECKSUM_ATTR (attrs.at_endianity);
7536 CHECKSUM_ATTR (attrs.at_explicit);
7537 CHECKSUM_ATTR (attrs.at_is_optional);
7538 CHECKSUM_ATTR (attrs.at_location);
7539 CHECKSUM_ATTR (attrs.at_lower_bound);
7540 CHECKSUM_ATTR (attrs.at_mutable);
7541 CHECKSUM_ATTR (attrs.at_ordering);
7542 CHECKSUM_ATTR (attrs.at_picture_string);
7543 CHECKSUM_ATTR (attrs.at_prototyped);
7544 CHECKSUM_ATTR (attrs.at_small);
7545 CHECKSUM_ATTR (attrs.at_segment);
7546 CHECKSUM_ATTR (attrs.at_string_length);
7547 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7548 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7549 CHECKSUM_ATTR (attrs.at_threads_scaled);
7550 CHECKSUM_ATTR (attrs.at_upper_bound);
7551 CHECKSUM_ATTR (attrs.at_use_location);
7552 CHECKSUM_ATTR (attrs.at_use_UTF8);
7553 CHECKSUM_ATTR (attrs.at_variable_parameter);
7554 CHECKSUM_ATTR (attrs.at_virtuality);
7555 CHECKSUM_ATTR (attrs.at_visibility);
7556 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7557 CHECKSUM_ATTR (attrs.at_type);
7558 CHECKSUM_ATTR (attrs.at_friend);
7559 CHECKSUM_ATTR (attrs.at_alignment);
7561 /* Checksum the child DIEs. */
7562 c = die->die_child;
7563 if (c) do {
7564 dw_attr_node *name_attr;
7566 c = c->die_sib;
7567 name_attr = get_AT (c, DW_AT_name);
7568 if (is_template_instantiation (c))
7570 /* Ignore instantiations of member type and function templates. */
7572 else if (name_attr != NULL
7573 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7575 /* Use a shallow checksum for named nested types and member
7576 functions. */
7577 CHECKSUM_ULEB128 ('S');
7578 CHECKSUM_ULEB128 (c->die_tag);
7579 CHECKSUM_STRING (AT_string (name_attr));
7581 else
7583 /* Use a deep checksum for other children. */
7584 /* Mark this DIE so it gets processed when unmarking. */
7585 if (c->die_mark == 0)
7586 c->die_mark = -1;
7587 die_checksum_ordered (c, ctx, mark);
7589 } while (c != die->die_child);
7591 CHECKSUM_ULEB128 (0);
7594 /* Add a type name and tag to a hash. */
7595 static void
7596 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7598 CHECKSUM_ULEB128 (tag);
7599 CHECKSUM_STRING (name);
7602 #undef CHECKSUM
7603 #undef CHECKSUM_STRING
7604 #undef CHECKSUM_ATTR
7605 #undef CHECKSUM_LEB128
7606 #undef CHECKSUM_ULEB128
7608 /* Generate the type signature for DIE. This is computed by generating an
7609 MD5 checksum over the DIE's tag, its relevant attributes, and its
7610 children. Attributes that are references to other DIEs are processed
7611 by recursion, using the MARK field to prevent infinite recursion.
7612 If the DIE is nested inside a namespace or another type, we also
7613 need to include that context in the signature. The lower 64 bits
7614 of the resulting MD5 checksum comprise the signature. */
7616 static void
7617 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7619 int mark;
7620 const char *name;
7621 unsigned char checksum[16];
7622 struct md5_ctx ctx;
7623 dw_die_ref decl;
7624 dw_die_ref parent;
7626 name = get_AT_string (die, DW_AT_name);
7627 decl = get_AT_ref (die, DW_AT_specification);
7628 parent = get_die_parent (die);
7630 /* First, compute a signature for just the type name (and its surrounding
7631 context, if any. This is stored in the type unit DIE for link-time
7632 ODR (one-definition rule) checking. */
7634 if (is_cxx () && name != NULL)
7636 md5_init_ctx (&ctx);
7638 /* Checksum the names of surrounding namespaces and structures. */
7639 if (parent != NULL)
7640 checksum_die_context (parent, &ctx);
7642 /* Checksum the current DIE. */
7643 die_odr_checksum (die->die_tag, name, &ctx);
7644 md5_finish_ctx (&ctx, checksum);
7646 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7649 /* Next, compute the complete type signature. */
7651 md5_init_ctx (&ctx);
7652 mark = 1;
7653 die->die_mark = mark;
7655 /* Checksum the names of surrounding namespaces and structures. */
7656 if (parent != NULL)
7657 checksum_die_context (parent, &ctx);
7659 /* Checksum the DIE and its children. */
7660 die_checksum_ordered (die, &ctx, &mark);
7661 unmark_all_dies (die);
7662 md5_finish_ctx (&ctx, checksum);
7664 /* Store the signature in the type node and link the type DIE and the
7665 type node together. */
7666 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7667 DWARF_TYPE_SIGNATURE_SIZE);
7668 die->comdat_type_p = true;
7669 die->die_id.die_type_node = type_node;
7670 type_node->type_die = die;
7672 /* If the DIE is a specification, link its declaration to the type node
7673 as well. */
7674 if (decl != NULL)
7676 decl->comdat_type_p = true;
7677 decl->die_id.die_type_node = type_node;
7681 /* Do the location expressions look same? */
7682 static inline int
7683 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7685 return loc1->dw_loc_opc == loc2->dw_loc_opc
7686 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7687 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7690 /* Do the values look the same? */
7691 static int
7692 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7694 dw_loc_descr_ref loc1, loc2;
7695 rtx r1, r2;
7697 if (v1->val_class != v2->val_class)
7698 return 0;
7700 switch (v1->val_class)
7702 case dw_val_class_const:
7703 case dw_val_class_const_implicit:
7704 return v1->v.val_int == v2->v.val_int;
7705 case dw_val_class_unsigned_const:
7706 case dw_val_class_unsigned_const_implicit:
7707 return v1->v.val_unsigned == v2->v.val_unsigned;
7708 case dw_val_class_const_double:
7709 return v1->v.val_double.high == v2->v.val_double.high
7710 && v1->v.val_double.low == v2->v.val_double.low;
7711 case dw_val_class_wide_int:
7712 return *v1->v.val_wide == *v2->v.val_wide;
7713 case dw_val_class_vec:
7714 if (v1->v.val_vec.length != v2->v.val_vec.length
7715 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7716 return 0;
7717 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7718 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7719 return 0;
7720 return 1;
7721 case dw_val_class_flag:
7722 return v1->v.val_flag == v2->v.val_flag;
7723 case dw_val_class_str:
7724 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7726 case dw_val_class_addr:
7727 r1 = v1->v.val_addr;
7728 r2 = v2->v.val_addr;
7729 if (GET_CODE (r1) != GET_CODE (r2))
7730 return 0;
7731 return !rtx_equal_p (r1, r2);
7733 case dw_val_class_offset:
7734 return v1->v.val_offset == v2->v.val_offset;
7736 case dw_val_class_loc:
7737 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7738 loc1 && loc2;
7739 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7740 if (!same_loc_p (loc1, loc2, mark))
7741 return 0;
7742 return !loc1 && !loc2;
7744 case dw_val_class_die_ref:
7745 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7747 case dw_val_class_symview:
7748 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7750 case dw_val_class_fde_ref:
7751 case dw_val_class_vms_delta:
7752 case dw_val_class_lbl_id:
7753 case dw_val_class_lineptr:
7754 case dw_val_class_macptr:
7755 case dw_val_class_loclistsptr:
7756 case dw_val_class_high_pc:
7757 return 1;
7759 case dw_val_class_file:
7760 case dw_val_class_file_implicit:
7761 return v1->v.val_file == v2->v.val_file;
7763 case dw_val_class_data8:
7764 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7766 default:
7767 return 1;
7771 /* Do the attributes look the same? */
7773 static int
7774 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7776 if (at1->dw_attr != at2->dw_attr)
7777 return 0;
7779 /* We don't care that this was compiled with a different compiler
7780 snapshot; if the output is the same, that's what matters. */
7781 if (at1->dw_attr == DW_AT_producer)
7782 return 1;
7784 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7787 /* Do the dies look the same? */
7789 static int
7790 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7792 dw_die_ref c1, c2;
7793 dw_attr_node *a1;
7794 unsigned ix;
7796 /* To avoid infinite recursion. */
7797 if (die1->die_mark)
7798 return die1->die_mark == die2->die_mark;
7799 die1->die_mark = die2->die_mark = ++(*mark);
7801 if (die1->die_tag != die2->die_tag)
7802 return 0;
7804 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7805 return 0;
7807 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7808 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7809 return 0;
7811 c1 = die1->die_child;
7812 c2 = die2->die_child;
7813 if (! c1)
7815 if (c2)
7816 return 0;
7818 else
7819 for (;;)
7821 if (!same_die_p (c1, c2, mark))
7822 return 0;
7823 c1 = c1->die_sib;
7824 c2 = c2->die_sib;
7825 if (c1 == die1->die_child)
7827 if (c2 == die2->die_child)
7828 break;
7829 else
7830 return 0;
7834 return 1;
7837 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7838 children, and set die_symbol. */
7840 static void
7841 compute_comp_unit_symbol (dw_die_ref unit_die)
7843 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7844 const char *base = die_name ? lbasename (die_name) : "anonymous";
7845 char *name = XALLOCAVEC (char, strlen (base) + 64);
7846 char *p;
7847 int i, mark;
7848 unsigned char checksum[16];
7849 struct md5_ctx ctx;
7851 /* Compute the checksum of the DIE, then append part of it as hex digits to
7852 the name filename of the unit. */
7854 md5_init_ctx (&ctx);
7855 mark = 0;
7856 die_checksum (unit_die, &ctx, &mark);
7857 unmark_all_dies (unit_die);
7858 md5_finish_ctx (&ctx, checksum);
7860 /* When we this for comp_unit_die () we have a DW_AT_name that might
7861 not start with a letter but with anything valid for filenames and
7862 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7863 character is not a letter. */
7864 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7865 clean_symbol_name (name);
7867 p = name + strlen (name);
7868 for (i = 0; i < 4; i++)
7870 sprintf (p, "%.2x", checksum[i]);
7871 p += 2;
7874 unit_die->die_id.die_symbol = xstrdup (name);
7877 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7879 static int
7880 is_type_die (dw_die_ref die)
7882 switch (die->die_tag)
7884 case DW_TAG_array_type:
7885 case DW_TAG_class_type:
7886 case DW_TAG_interface_type:
7887 case DW_TAG_enumeration_type:
7888 case DW_TAG_pointer_type:
7889 case DW_TAG_reference_type:
7890 case DW_TAG_rvalue_reference_type:
7891 case DW_TAG_string_type:
7892 case DW_TAG_structure_type:
7893 case DW_TAG_subroutine_type:
7894 case DW_TAG_union_type:
7895 case DW_TAG_ptr_to_member_type:
7896 case DW_TAG_set_type:
7897 case DW_TAG_subrange_type:
7898 case DW_TAG_base_type:
7899 case DW_TAG_const_type:
7900 case DW_TAG_file_type:
7901 case DW_TAG_packed_type:
7902 case DW_TAG_volatile_type:
7903 case DW_TAG_typedef:
7904 return 1;
7905 default:
7906 return 0;
7910 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7911 Basically, we want to choose the bits that are likely to be shared between
7912 compilations (types) and leave out the bits that are specific to individual
7913 compilations (functions). */
7915 static int
7916 is_comdat_die (dw_die_ref c)
7918 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7919 we do for stabs. The advantage is a greater likelihood of sharing between
7920 objects that don't include headers in the same order (and therefore would
7921 put the base types in a different comdat). jason 8/28/00 */
7923 if (c->die_tag == DW_TAG_base_type)
7924 return 0;
7926 if (c->die_tag == DW_TAG_pointer_type
7927 || c->die_tag == DW_TAG_reference_type
7928 || c->die_tag == DW_TAG_rvalue_reference_type
7929 || c->die_tag == DW_TAG_const_type
7930 || c->die_tag == DW_TAG_volatile_type)
7932 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7934 return t ? is_comdat_die (t) : 0;
7937 return is_type_die (c);
7940 /* Returns true iff C is a compile-unit DIE. */
7942 static inline bool
7943 is_cu_die (dw_die_ref c)
7945 return c && (c->die_tag == DW_TAG_compile_unit
7946 || c->die_tag == DW_TAG_skeleton_unit);
7949 /* Returns true iff C is a unit DIE of some sort. */
7951 static inline bool
7952 is_unit_die (dw_die_ref c)
7954 return c && (c->die_tag == DW_TAG_compile_unit
7955 || c->die_tag == DW_TAG_partial_unit
7956 || c->die_tag == DW_TAG_type_unit
7957 || c->die_tag == DW_TAG_skeleton_unit);
7960 /* Returns true iff C is a namespace DIE. */
7962 static inline bool
7963 is_namespace_die (dw_die_ref c)
7965 return c && c->die_tag == DW_TAG_namespace;
7968 /* Returns true iff C is a class or structure DIE. */
7970 static inline bool
7971 is_class_die (dw_die_ref c)
7973 return c && (c->die_tag == DW_TAG_class_type
7974 || c->die_tag == DW_TAG_structure_type);
7977 /* Return non-zero if this DIE is a template parameter. */
7979 static inline bool
7980 is_template_parameter (dw_die_ref die)
7982 switch (die->die_tag)
7984 case DW_TAG_template_type_param:
7985 case DW_TAG_template_value_param:
7986 case DW_TAG_GNU_template_template_param:
7987 case DW_TAG_GNU_template_parameter_pack:
7988 return true;
7989 default:
7990 return false;
7994 /* Return non-zero if this DIE represents a template instantiation. */
7996 static inline bool
7997 is_template_instantiation (dw_die_ref die)
7999 dw_die_ref c;
8001 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8002 return false;
8003 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8004 return false;
8007 static char *
8008 gen_internal_sym (const char *prefix)
8010 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8012 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8013 return xstrdup (buf);
8016 /* Return non-zero if this DIE is a declaration. */
8018 static int
8019 is_declaration_die (dw_die_ref die)
8021 dw_attr_node *a;
8022 unsigned ix;
8024 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8025 if (a->dw_attr == DW_AT_declaration)
8026 return 1;
8028 return 0;
8031 /* Return non-zero if this DIE is nested inside a subprogram. */
8033 static int
8034 is_nested_in_subprogram (dw_die_ref die)
8036 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8038 if (decl == NULL)
8039 decl = die;
8040 return local_scope_p (decl);
8043 /* Return non-zero if this DIE contains a defining declaration of a
8044 subprogram. */
8046 static int
8047 contains_subprogram_definition (dw_die_ref die)
8049 dw_die_ref c;
8051 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8052 return 1;
8053 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8054 return 0;
8057 /* Return non-zero if this is a type DIE that should be moved to a
8058 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8059 unit type. */
8061 static int
8062 should_move_die_to_comdat (dw_die_ref die)
8064 switch (die->die_tag)
8066 case DW_TAG_class_type:
8067 case DW_TAG_structure_type:
8068 case DW_TAG_enumeration_type:
8069 case DW_TAG_union_type:
8070 /* Don't move declarations, inlined instances, types nested in a
8071 subprogram, or types that contain subprogram definitions. */
8072 if (is_declaration_die (die)
8073 || get_AT (die, DW_AT_abstract_origin)
8074 || is_nested_in_subprogram (die)
8075 || contains_subprogram_definition (die))
8076 return 0;
8077 return 1;
8078 case DW_TAG_array_type:
8079 case DW_TAG_interface_type:
8080 case DW_TAG_pointer_type:
8081 case DW_TAG_reference_type:
8082 case DW_TAG_rvalue_reference_type:
8083 case DW_TAG_string_type:
8084 case DW_TAG_subroutine_type:
8085 case DW_TAG_ptr_to_member_type:
8086 case DW_TAG_set_type:
8087 case DW_TAG_subrange_type:
8088 case DW_TAG_base_type:
8089 case DW_TAG_const_type:
8090 case DW_TAG_file_type:
8091 case DW_TAG_packed_type:
8092 case DW_TAG_volatile_type:
8093 case DW_TAG_typedef:
8094 default:
8095 return 0;
8099 /* Make a clone of DIE. */
8101 static dw_die_ref
8102 clone_die (dw_die_ref die)
8104 dw_die_ref clone = new_die_raw (die->die_tag);
8105 dw_attr_node *a;
8106 unsigned ix;
8108 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8109 add_dwarf_attr (clone, a);
8111 return clone;
8114 /* Make a clone of the tree rooted at DIE. */
8116 static dw_die_ref
8117 clone_tree (dw_die_ref die)
8119 dw_die_ref c;
8120 dw_die_ref clone = clone_die (die);
8122 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8124 return clone;
8127 /* Make a clone of DIE as a declaration. */
8129 static dw_die_ref
8130 clone_as_declaration (dw_die_ref die)
8132 dw_die_ref clone;
8133 dw_die_ref decl;
8134 dw_attr_node *a;
8135 unsigned ix;
8137 /* If the DIE is already a declaration, just clone it. */
8138 if (is_declaration_die (die))
8139 return clone_die (die);
8141 /* If the DIE is a specification, just clone its declaration DIE. */
8142 decl = get_AT_ref (die, DW_AT_specification);
8143 if (decl != NULL)
8145 clone = clone_die (decl);
8146 if (die->comdat_type_p)
8147 add_AT_die_ref (clone, DW_AT_signature, die);
8148 return clone;
8151 clone = new_die_raw (die->die_tag);
8153 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8155 /* We don't want to copy over all attributes.
8156 For example we don't want DW_AT_byte_size because otherwise we will no
8157 longer have a declaration and GDB will treat it as a definition. */
8159 switch (a->dw_attr)
8161 case DW_AT_abstract_origin:
8162 case DW_AT_artificial:
8163 case DW_AT_containing_type:
8164 case DW_AT_external:
8165 case DW_AT_name:
8166 case DW_AT_type:
8167 case DW_AT_virtuality:
8168 case DW_AT_linkage_name:
8169 case DW_AT_MIPS_linkage_name:
8170 add_dwarf_attr (clone, a);
8171 break;
8172 case DW_AT_byte_size:
8173 case DW_AT_alignment:
8174 default:
8175 break;
8179 if (die->comdat_type_p)
8180 add_AT_die_ref (clone, DW_AT_signature, die);
8182 add_AT_flag (clone, DW_AT_declaration, 1);
8183 return clone;
8187 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8189 struct decl_table_entry
8191 dw_die_ref orig;
8192 dw_die_ref copy;
8195 /* Helpers to manipulate hash table of copied declarations. */
8197 /* Hashtable helpers. */
8199 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8201 typedef die_struct *compare_type;
8202 static inline hashval_t hash (const decl_table_entry *);
8203 static inline bool equal (const decl_table_entry *, const die_struct *);
8206 inline hashval_t
8207 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8209 return htab_hash_pointer (entry->orig);
8212 inline bool
8213 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8214 const die_struct *entry2)
8216 return entry1->orig == entry2;
8219 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8221 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8222 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8223 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8224 to check if the ancestor has already been copied into UNIT. */
8226 static dw_die_ref
8227 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8228 decl_hash_type *decl_table)
8230 dw_die_ref parent = die->die_parent;
8231 dw_die_ref new_parent = unit;
8232 dw_die_ref copy;
8233 decl_table_entry **slot = NULL;
8234 struct decl_table_entry *entry = NULL;
8236 if (decl_table)
8238 /* Check if the entry has already been copied to UNIT. */
8239 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8240 INSERT);
8241 if (*slot != HTAB_EMPTY_ENTRY)
8243 entry = *slot;
8244 return entry->copy;
8247 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8248 entry = XCNEW (struct decl_table_entry);
8249 entry->orig = die;
8250 entry->copy = NULL;
8251 *slot = entry;
8254 if (parent != NULL)
8256 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8257 if (spec != NULL)
8258 parent = spec;
8259 if (!is_unit_die (parent))
8260 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8263 copy = clone_as_declaration (die);
8264 add_child_die (new_parent, copy);
8266 if (decl_table)
8268 /* Record the pointer to the copy. */
8269 entry->copy = copy;
8272 return copy;
8274 /* Copy the declaration context to the new type unit DIE. This includes
8275 any surrounding namespace or type declarations. If the DIE has an
8276 AT_specification attribute, it also includes attributes and children
8277 attached to the specification, and returns a pointer to the original
8278 parent of the declaration DIE. Returns NULL otherwise. */
8280 static dw_die_ref
8281 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8283 dw_die_ref decl;
8284 dw_die_ref new_decl;
8285 dw_die_ref orig_parent = NULL;
8287 decl = get_AT_ref (die, DW_AT_specification);
8288 if (decl == NULL)
8289 decl = die;
8290 else
8292 unsigned ix;
8293 dw_die_ref c;
8294 dw_attr_node *a;
8296 /* The original DIE will be changed to a declaration, and must
8297 be moved to be a child of the original declaration DIE. */
8298 orig_parent = decl->die_parent;
8300 /* Copy the type node pointer from the new DIE to the original
8301 declaration DIE so we can forward references later. */
8302 decl->comdat_type_p = true;
8303 decl->die_id.die_type_node = die->die_id.die_type_node;
8305 remove_AT (die, DW_AT_specification);
8307 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8309 if (a->dw_attr != DW_AT_name
8310 && a->dw_attr != DW_AT_declaration
8311 && a->dw_attr != DW_AT_external)
8312 add_dwarf_attr (die, a);
8315 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8318 if (decl->die_parent != NULL
8319 && !is_unit_die (decl->die_parent))
8321 new_decl = copy_ancestor_tree (unit, decl, NULL);
8322 if (new_decl != NULL)
8324 remove_AT (new_decl, DW_AT_signature);
8325 add_AT_specification (die, new_decl);
8329 return orig_parent;
8332 /* Generate the skeleton ancestor tree for the given NODE, then clone
8333 the DIE and add the clone into the tree. */
8335 static void
8336 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8338 if (node->new_die != NULL)
8339 return;
8341 node->new_die = clone_as_declaration (node->old_die);
8343 if (node->parent != NULL)
8345 generate_skeleton_ancestor_tree (node->parent);
8346 add_child_die (node->parent->new_die, node->new_die);
8350 /* Generate a skeleton tree of DIEs containing any declarations that are
8351 found in the original tree. We traverse the tree looking for declaration
8352 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8354 static void
8355 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8357 skeleton_chain_node node;
8358 dw_die_ref c;
8359 dw_die_ref first;
8360 dw_die_ref prev = NULL;
8361 dw_die_ref next = NULL;
8363 node.parent = parent;
8365 first = c = parent->old_die->die_child;
8366 if (c)
8367 next = c->die_sib;
8368 if (c) do {
8369 if (prev == NULL || prev->die_sib == c)
8370 prev = c;
8371 c = next;
8372 next = (c == first ? NULL : c->die_sib);
8373 node.old_die = c;
8374 node.new_die = NULL;
8375 if (is_declaration_die (c))
8377 if (is_template_instantiation (c))
8379 /* Instantiated templates do not need to be cloned into the
8380 type unit. Just move the DIE and its children back to
8381 the skeleton tree (in the main CU). */
8382 remove_child_with_prev (c, prev);
8383 add_child_die (parent->new_die, c);
8384 c = prev;
8386 else if (c->comdat_type_p)
8388 /* This is the skeleton of earlier break_out_comdat_types
8389 type. Clone the existing DIE, but keep the children
8390 under the original (which is in the main CU). */
8391 dw_die_ref clone = clone_die (c);
8393 replace_child (c, clone, prev);
8394 generate_skeleton_ancestor_tree (parent);
8395 add_child_die (parent->new_die, c);
8396 c = clone;
8397 continue;
8399 else
8401 /* Clone the existing DIE, move the original to the skeleton
8402 tree (which is in the main CU), and put the clone, with
8403 all the original's children, where the original came from
8404 (which is about to be moved to the type unit). */
8405 dw_die_ref clone = clone_die (c);
8406 move_all_children (c, clone);
8408 /* If the original has a DW_AT_object_pointer attribute,
8409 it would now point to a child DIE just moved to the
8410 cloned tree, so we need to remove that attribute from
8411 the original. */
8412 remove_AT (c, DW_AT_object_pointer);
8414 replace_child (c, clone, prev);
8415 generate_skeleton_ancestor_tree (parent);
8416 add_child_die (parent->new_die, c);
8417 node.old_die = clone;
8418 node.new_die = c;
8419 c = clone;
8422 generate_skeleton_bottom_up (&node);
8423 } while (next != NULL);
8426 /* Wrapper function for generate_skeleton_bottom_up. */
8428 static dw_die_ref
8429 generate_skeleton (dw_die_ref die)
8431 skeleton_chain_node node;
8433 node.old_die = die;
8434 node.new_die = NULL;
8435 node.parent = NULL;
8437 /* If this type definition is nested inside another type,
8438 and is not an instantiation of a template, always leave
8439 at least a declaration in its place. */
8440 if (die->die_parent != NULL
8441 && is_type_die (die->die_parent)
8442 && !is_template_instantiation (die))
8443 node.new_die = clone_as_declaration (die);
8445 generate_skeleton_bottom_up (&node);
8446 return node.new_die;
8449 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8450 declaration. The original DIE is moved to a new compile unit so that
8451 existing references to it follow it to the new location. If any of the
8452 original DIE's descendants is a declaration, we need to replace the
8453 original DIE with a skeleton tree and move the declarations back into the
8454 skeleton tree. */
8456 static dw_die_ref
8457 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8458 dw_die_ref prev)
8460 dw_die_ref skeleton, orig_parent;
8462 /* Copy the declaration context to the type unit DIE. If the returned
8463 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8464 that DIE. */
8465 orig_parent = copy_declaration_context (unit, child);
8467 skeleton = generate_skeleton (child);
8468 if (skeleton == NULL)
8469 remove_child_with_prev (child, prev);
8470 else
8472 skeleton->comdat_type_p = true;
8473 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8475 /* If the original DIE was a specification, we need to put
8476 the skeleton under the parent DIE of the declaration.
8477 This leaves the original declaration in the tree, but
8478 it will be pruned later since there are no longer any
8479 references to it. */
8480 if (orig_parent != NULL)
8482 remove_child_with_prev (child, prev);
8483 add_child_die (orig_parent, skeleton);
8485 else
8486 replace_child (child, skeleton, prev);
8489 return skeleton;
8492 static void
8493 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8494 comdat_type_node *type_node,
8495 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8497 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8498 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8499 DWARF procedure references in the DW_AT_location attribute. */
8501 static dw_die_ref
8502 copy_dwarf_procedure (dw_die_ref die,
8503 comdat_type_node *type_node,
8504 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8506 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8508 /* DWARF procedures are not supposed to have children... */
8509 gcc_assert (die->die_child == NULL);
8511 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8512 gcc_assert (vec_safe_length (die->die_attr) == 1
8513 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8515 /* Do not copy more than once DWARF procedures. */
8516 bool existed;
8517 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8518 if (existed)
8519 return die_copy;
8521 die_copy = clone_die (die);
8522 add_child_die (type_node->root_die, die_copy);
8523 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8524 return die_copy;
8527 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8528 procedures in DIE's attributes. */
8530 static void
8531 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8532 comdat_type_node *type_node,
8533 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8535 dw_attr_node *a;
8536 unsigned i;
8538 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8540 dw_loc_descr_ref loc;
8542 if (a->dw_attr_val.val_class != dw_val_class_loc)
8543 continue;
8545 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8547 switch (loc->dw_loc_opc)
8549 case DW_OP_call2:
8550 case DW_OP_call4:
8551 case DW_OP_call_ref:
8552 gcc_assert (loc->dw_loc_oprnd1.val_class
8553 == dw_val_class_die_ref);
8554 loc->dw_loc_oprnd1.v.val_die_ref.die
8555 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8556 type_node,
8557 copied_dwarf_procs);
8559 default:
8560 break;
8566 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8567 rewrite references to point to the copies.
8569 References are looked for in DIE's attributes and recursively in all its
8570 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8571 mapping from old DWARF procedures to their copy. It is used not to copy
8572 twice the same DWARF procedure under TYPE_NODE. */
8574 static void
8575 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8576 comdat_type_node *type_node,
8577 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8579 dw_die_ref c;
8581 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8582 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8583 type_node,
8584 copied_dwarf_procs));
8587 /* Traverse the DIE and set up additional .debug_types or .debug_info
8588 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8589 section. */
8591 static void
8592 break_out_comdat_types (dw_die_ref die)
8594 dw_die_ref c;
8595 dw_die_ref first;
8596 dw_die_ref prev = NULL;
8597 dw_die_ref next = NULL;
8598 dw_die_ref unit = NULL;
8600 first = c = die->die_child;
8601 if (c)
8602 next = c->die_sib;
8603 if (c) do {
8604 if (prev == NULL || prev->die_sib == c)
8605 prev = c;
8606 c = next;
8607 next = (c == first ? NULL : c->die_sib);
8608 if (should_move_die_to_comdat (c))
8610 dw_die_ref replacement;
8611 comdat_type_node *type_node;
8613 /* Break out nested types into their own type units. */
8614 break_out_comdat_types (c);
8616 /* Create a new type unit DIE as the root for the new tree, and
8617 add it to the list of comdat types. */
8618 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8619 add_AT_unsigned (unit, DW_AT_language,
8620 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8621 type_node = ggc_cleared_alloc<comdat_type_node> ();
8622 type_node->root_die = unit;
8623 type_node->next = comdat_type_list;
8624 comdat_type_list = type_node;
8626 /* Generate the type signature. */
8627 generate_type_signature (c, type_node);
8629 /* Copy the declaration context, attributes, and children of the
8630 declaration into the new type unit DIE, then remove this DIE
8631 from the main CU (or replace it with a skeleton if necessary). */
8632 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8633 type_node->skeleton_die = replacement;
8635 /* Add the DIE to the new compunit. */
8636 add_child_die (unit, c);
8638 /* Types can reference DWARF procedures for type size or data location
8639 expressions. Calls in DWARF expressions cannot target procedures
8640 that are not in the same section. So we must copy DWARF procedures
8641 along with this type and then rewrite references to them. */
8642 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8643 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8645 if (replacement != NULL)
8646 c = replacement;
8648 else if (c->die_tag == DW_TAG_namespace
8649 || c->die_tag == DW_TAG_class_type
8650 || c->die_tag == DW_TAG_structure_type
8651 || c->die_tag == DW_TAG_union_type)
8653 /* Look for nested types that can be broken out. */
8654 break_out_comdat_types (c);
8656 } while (next != NULL);
8659 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8660 Enter all the cloned children into the hash table decl_table. */
8662 static dw_die_ref
8663 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8665 dw_die_ref c;
8666 dw_die_ref clone;
8667 struct decl_table_entry *entry;
8668 decl_table_entry **slot;
8670 if (die->die_tag == DW_TAG_subprogram)
8671 clone = clone_as_declaration (die);
8672 else
8673 clone = clone_die (die);
8675 slot = decl_table->find_slot_with_hash (die,
8676 htab_hash_pointer (die), INSERT);
8678 /* Assert that DIE isn't in the hash table yet. If it would be there
8679 before, the ancestors would be necessarily there as well, therefore
8680 clone_tree_partial wouldn't be called. */
8681 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8683 entry = XCNEW (struct decl_table_entry);
8684 entry->orig = die;
8685 entry->copy = clone;
8686 *slot = entry;
8688 if (die->die_tag != DW_TAG_subprogram)
8689 FOR_EACH_CHILD (die, c,
8690 add_child_die (clone, clone_tree_partial (c, decl_table)));
8692 return clone;
8695 /* Walk the DIE and its children, looking for references to incomplete
8696 or trivial types that are unmarked (i.e., that are not in the current
8697 type_unit). */
8699 static void
8700 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8702 dw_die_ref c;
8703 dw_attr_node *a;
8704 unsigned ix;
8706 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8708 if (AT_class (a) == dw_val_class_die_ref)
8710 dw_die_ref targ = AT_ref (a);
8711 decl_table_entry **slot;
8712 struct decl_table_entry *entry;
8714 if (targ->die_mark != 0 || targ->comdat_type_p)
8715 continue;
8717 slot = decl_table->find_slot_with_hash (targ,
8718 htab_hash_pointer (targ),
8719 INSERT);
8721 if (*slot != HTAB_EMPTY_ENTRY)
8723 /* TARG has already been copied, so we just need to
8724 modify the reference to point to the copy. */
8725 entry = *slot;
8726 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8728 else
8730 dw_die_ref parent = unit;
8731 dw_die_ref copy = clone_die (targ);
8733 /* Record in DECL_TABLE that TARG has been copied.
8734 Need to do this now, before the recursive call,
8735 because DECL_TABLE may be expanded and SLOT
8736 would no longer be a valid pointer. */
8737 entry = XCNEW (struct decl_table_entry);
8738 entry->orig = targ;
8739 entry->copy = copy;
8740 *slot = entry;
8742 /* If TARG is not a declaration DIE, we need to copy its
8743 children. */
8744 if (!is_declaration_die (targ))
8746 FOR_EACH_CHILD (
8747 targ, c,
8748 add_child_die (copy,
8749 clone_tree_partial (c, decl_table)));
8752 /* Make sure the cloned tree is marked as part of the
8753 type unit. */
8754 mark_dies (copy);
8756 /* If TARG has surrounding context, copy its ancestor tree
8757 into the new type unit. */
8758 if (targ->die_parent != NULL
8759 && !is_unit_die (targ->die_parent))
8760 parent = copy_ancestor_tree (unit, targ->die_parent,
8761 decl_table);
8763 add_child_die (parent, copy);
8764 a->dw_attr_val.v.val_die_ref.die = copy;
8766 /* Make sure the newly-copied DIE is walked. If it was
8767 installed in a previously-added context, it won't
8768 get visited otherwise. */
8769 if (parent != unit)
8771 /* Find the highest point of the newly-added tree,
8772 mark each node along the way, and walk from there. */
8773 parent->die_mark = 1;
8774 while (parent->die_parent
8775 && parent->die_parent->die_mark == 0)
8777 parent = parent->die_parent;
8778 parent->die_mark = 1;
8780 copy_decls_walk (unit, parent, decl_table);
8786 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8789 /* Copy declarations for "unworthy" types into the new comdat section.
8790 Incomplete types, modified types, and certain other types aren't broken
8791 out into comdat sections of their own, so they don't have a signature,
8792 and we need to copy the declaration into the same section so that we
8793 don't have an external reference. */
8795 static void
8796 copy_decls_for_unworthy_types (dw_die_ref unit)
8798 mark_dies (unit);
8799 decl_hash_type decl_table (10);
8800 copy_decls_walk (unit, unit, &decl_table);
8801 unmark_dies (unit);
8804 /* Traverse the DIE and add a sibling attribute if it may have the
8805 effect of speeding up access to siblings. To save some space,
8806 avoid generating sibling attributes for DIE's without children. */
8808 static void
8809 add_sibling_attributes (dw_die_ref die)
8811 dw_die_ref c;
8813 if (! die->die_child)
8814 return;
8816 if (die->die_parent && die != die->die_parent->die_child)
8817 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8819 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8822 /* Output all location lists for the DIE and its children. */
8824 static void
8825 output_location_lists (dw_die_ref die)
8827 dw_die_ref c;
8828 dw_attr_node *a;
8829 unsigned ix;
8831 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8832 if (AT_class (a) == dw_val_class_loc_list)
8833 output_loc_list (AT_loc_list (a));
8835 FOR_EACH_CHILD (die, c, output_location_lists (c));
8838 /* During assign_location_list_indexes and output_loclists_offset the
8839 current index, after it the number of assigned indexes (i.e. how
8840 large the .debug_loclists* offset table should be). */
8841 static unsigned int loc_list_idx;
8843 /* Output all location list offsets for the DIE and its children. */
8845 static void
8846 output_loclists_offsets (dw_die_ref die)
8848 dw_die_ref c;
8849 dw_attr_node *a;
8850 unsigned ix;
8852 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8853 if (AT_class (a) == dw_val_class_loc_list)
8855 dw_loc_list_ref l = AT_loc_list (a);
8856 if (l->offset_emitted)
8857 continue;
8858 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8859 loc_section_label, NULL);
8860 gcc_assert (l->hash == loc_list_idx);
8861 loc_list_idx++;
8862 l->offset_emitted = true;
8865 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8868 /* Recursively set indexes of location lists. */
8870 static void
8871 assign_location_list_indexes (dw_die_ref die)
8873 dw_die_ref c;
8874 dw_attr_node *a;
8875 unsigned ix;
8877 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8878 if (AT_class (a) == dw_val_class_loc_list)
8880 dw_loc_list_ref list = AT_loc_list (a);
8881 if (!list->num_assigned)
8883 list->num_assigned = true;
8884 list->hash = loc_list_idx++;
8888 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8891 /* We want to limit the number of external references, because they are
8892 larger than local references: a relocation takes multiple words, and
8893 even a sig8 reference is always eight bytes, whereas a local reference
8894 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8895 So if we encounter multiple external references to the same type DIE, we
8896 make a local typedef stub for it and redirect all references there.
8898 This is the element of the hash table for keeping track of these
8899 references. */
8901 struct external_ref
8903 dw_die_ref type;
8904 dw_die_ref stub;
8905 unsigned n_refs;
8908 /* Hashtable helpers. */
8910 struct external_ref_hasher : free_ptr_hash <external_ref>
8912 static inline hashval_t hash (const external_ref *);
8913 static inline bool equal (const external_ref *, const external_ref *);
8916 inline hashval_t
8917 external_ref_hasher::hash (const external_ref *r)
8919 dw_die_ref die = r->type;
8920 hashval_t h = 0;
8922 /* We can't use the address of the DIE for hashing, because
8923 that will make the order of the stub DIEs non-deterministic. */
8924 if (! die->comdat_type_p)
8925 /* We have a symbol; use it to compute a hash. */
8926 h = htab_hash_string (die->die_id.die_symbol);
8927 else
8929 /* We have a type signature; use a subset of the bits as the hash.
8930 The 8-byte signature is at least as large as hashval_t. */
8931 comdat_type_node *type_node = die->die_id.die_type_node;
8932 memcpy (&h, type_node->signature, sizeof (h));
8934 return h;
8937 inline bool
8938 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8940 return r1->type == r2->type;
8943 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8945 /* Return a pointer to the external_ref for references to DIE. */
8947 static struct external_ref *
8948 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8950 struct external_ref ref, *ref_p;
8951 external_ref **slot;
8953 ref.type = die;
8954 slot = map->find_slot (&ref, INSERT);
8955 if (*slot != HTAB_EMPTY_ENTRY)
8956 return *slot;
8958 ref_p = XCNEW (struct external_ref);
8959 ref_p->type = die;
8960 *slot = ref_p;
8961 return ref_p;
8964 /* Subroutine of optimize_external_refs, below.
8966 If we see a type skeleton, record it as our stub. If we see external
8967 references, remember how many we've seen. */
8969 static void
8970 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8972 dw_die_ref c;
8973 dw_attr_node *a;
8974 unsigned ix;
8975 struct external_ref *ref_p;
8977 if (is_type_die (die)
8978 && (c = get_AT_ref (die, DW_AT_signature)))
8980 /* This is a local skeleton; use it for local references. */
8981 ref_p = lookup_external_ref (map, c);
8982 ref_p->stub = die;
8985 /* Scan the DIE references, and remember any that refer to DIEs from
8986 other CUs (i.e. those which are not marked). */
8987 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8988 if (AT_class (a) == dw_val_class_die_ref
8989 && (c = AT_ref (a))->die_mark == 0
8990 && is_type_die (c))
8992 ref_p = lookup_external_ref (map, c);
8993 ref_p->n_refs++;
8996 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8999 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9000 points to an external_ref, DATA is the CU we're processing. If we don't
9001 already have a local stub, and we have multiple refs, build a stub. */
9004 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9006 struct external_ref *ref_p = *slot;
9008 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9010 /* We have multiple references to this type, so build a small stub.
9011 Both of these forms are a bit dodgy from the perspective of the
9012 DWARF standard, since technically they should have names. */
9013 dw_die_ref cu = data;
9014 dw_die_ref type = ref_p->type;
9015 dw_die_ref stub = NULL;
9017 if (type->comdat_type_p)
9019 /* If we refer to this type via sig8, use AT_signature. */
9020 stub = new_die (type->die_tag, cu, NULL_TREE);
9021 add_AT_die_ref (stub, DW_AT_signature, type);
9023 else
9025 /* Otherwise, use a typedef with no name. */
9026 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9027 add_AT_die_ref (stub, DW_AT_type, type);
9030 stub->die_mark++;
9031 ref_p->stub = stub;
9033 return 1;
9036 /* DIE is a unit; look through all the DIE references to see if there are
9037 any external references to types, and if so, create local stubs for
9038 them which will be applied in build_abbrev_table. This is useful because
9039 references to local DIEs are smaller. */
9041 static external_ref_hash_type *
9042 optimize_external_refs (dw_die_ref die)
9044 external_ref_hash_type *map = new external_ref_hash_type (10);
9045 optimize_external_refs_1 (die, map);
9046 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9047 return map;
9050 /* The following 3 variables are temporaries that are computed only during the
9051 build_abbrev_table call and used and released during the following
9052 optimize_abbrev_table call. */
9054 /* First abbrev_id that can be optimized based on usage. */
9055 static unsigned int abbrev_opt_start;
9057 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9058 abbrev_id smaller than this, because they must be already sized
9059 during build_abbrev_table). */
9060 static unsigned int abbrev_opt_base_type_end;
9062 /* Vector of usage counts during build_abbrev_table. Indexed by
9063 abbrev_id - abbrev_opt_start. */
9064 static vec<unsigned int> abbrev_usage_count;
9066 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9067 static vec<dw_die_ref> sorted_abbrev_dies;
9069 /* The format of each DIE (and its attribute value pairs) is encoded in an
9070 abbreviation table. This routine builds the abbreviation table and assigns
9071 a unique abbreviation id for each abbreviation entry. The children of each
9072 die are visited recursively. */
9074 static void
9075 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9077 unsigned int abbrev_id = 0;
9078 dw_die_ref c;
9079 dw_attr_node *a;
9080 unsigned ix;
9081 dw_die_ref abbrev;
9083 /* Scan the DIE references, and replace any that refer to
9084 DIEs from other CUs (i.e. those which are not marked) with
9085 the local stubs we built in optimize_external_refs. */
9086 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9087 if (AT_class (a) == dw_val_class_die_ref
9088 && (c = AT_ref (a))->die_mark == 0)
9090 struct external_ref *ref_p;
9091 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9093 ref_p = lookup_external_ref (extern_map, c);
9094 if (ref_p->stub && ref_p->stub != die)
9095 change_AT_die_ref (a, ref_p->stub);
9096 else
9097 /* We aren't changing this reference, so mark it external. */
9098 set_AT_ref_external (a, 1);
9101 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9103 dw_attr_node *die_a, *abbrev_a;
9104 unsigned ix;
9105 bool ok = true;
9107 if (abbrev_id == 0)
9108 continue;
9109 if (abbrev->die_tag != die->die_tag)
9110 continue;
9111 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9112 continue;
9114 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9115 continue;
9117 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9119 abbrev_a = &(*abbrev->die_attr)[ix];
9120 if ((abbrev_a->dw_attr != die_a->dw_attr)
9121 || (value_format (abbrev_a) != value_format (die_a)))
9123 ok = false;
9124 break;
9127 if (ok)
9128 break;
9131 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9133 vec_safe_push (abbrev_die_table, die);
9134 if (abbrev_opt_start)
9135 abbrev_usage_count.safe_push (0);
9137 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9139 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9140 sorted_abbrev_dies.safe_push (die);
9143 die->die_abbrev = abbrev_id;
9144 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9147 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9148 by die_abbrev's usage count, from the most commonly used
9149 abbreviation to the least. */
9151 static int
9152 die_abbrev_cmp (const void *p1, const void *p2)
9154 dw_die_ref die1 = *(const dw_die_ref *) p1;
9155 dw_die_ref die2 = *(const dw_die_ref *) p2;
9157 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9158 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9160 if (die1->die_abbrev >= abbrev_opt_base_type_end
9161 && die2->die_abbrev >= abbrev_opt_base_type_end)
9163 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9164 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9165 return -1;
9166 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9167 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9168 return 1;
9171 /* Stabilize the sort. */
9172 if (die1->die_abbrev < die2->die_abbrev)
9173 return -1;
9174 if (die1->die_abbrev > die2->die_abbrev)
9175 return 1;
9177 return 0;
9180 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9181 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9182 into dw_val_class_const_implicit or
9183 dw_val_class_unsigned_const_implicit. */
9185 static void
9186 optimize_implicit_const (unsigned int first_id, unsigned int end,
9187 vec<bool> &implicit_consts)
9189 /* It never makes sense if there is just one DIE using the abbreviation. */
9190 if (end < first_id + 2)
9191 return;
9193 dw_attr_node *a;
9194 unsigned ix, i;
9195 dw_die_ref die = sorted_abbrev_dies[first_id];
9196 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9197 if (implicit_consts[ix])
9199 enum dw_val_class new_class = dw_val_class_none;
9200 switch (AT_class (a))
9202 case dw_val_class_unsigned_const:
9203 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9204 continue;
9206 /* The .debug_abbrev section will grow by
9207 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9208 in all the DIEs using that abbreviation. */
9209 if (constant_size (AT_unsigned (a)) * (end - first_id)
9210 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9211 continue;
9213 new_class = dw_val_class_unsigned_const_implicit;
9214 break;
9216 case dw_val_class_const:
9217 new_class = dw_val_class_const_implicit;
9218 break;
9220 case dw_val_class_file:
9221 new_class = dw_val_class_file_implicit;
9222 break;
9224 default:
9225 continue;
9227 for (i = first_id; i < end; i++)
9228 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9229 = new_class;
9233 /* Attempt to optimize abbreviation table from abbrev_opt_start
9234 abbreviation above. */
9236 static void
9237 optimize_abbrev_table (void)
9239 if (abbrev_opt_start
9240 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9241 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9243 auto_vec<bool, 32> implicit_consts;
9244 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9246 unsigned int abbrev_id = abbrev_opt_start - 1;
9247 unsigned int first_id = ~0U;
9248 unsigned int last_abbrev_id = 0;
9249 unsigned int i;
9250 dw_die_ref die;
9251 if (abbrev_opt_base_type_end > abbrev_opt_start)
9252 abbrev_id = abbrev_opt_base_type_end - 1;
9253 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9254 most commonly used abbreviations come first. */
9255 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9257 dw_attr_node *a;
9258 unsigned ix;
9260 /* If calc_base_type_die_sizes has been called, the CU and
9261 base types after it can't be optimized, because we've already
9262 calculated their DIE offsets. We've sorted them first. */
9263 if (die->die_abbrev < abbrev_opt_base_type_end)
9264 continue;
9265 if (die->die_abbrev != last_abbrev_id)
9267 last_abbrev_id = die->die_abbrev;
9268 if (dwarf_version >= 5 && first_id != ~0U)
9269 optimize_implicit_const (first_id, i, implicit_consts);
9270 abbrev_id++;
9271 (*abbrev_die_table)[abbrev_id] = die;
9272 if (dwarf_version >= 5)
9274 first_id = i;
9275 implicit_consts.truncate (0);
9277 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9278 switch (AT_class (a))
9280 case dw_val_class_const:
9281 case dw_val_class_unsigned_const:
9282 case dw_val_class_file:
9283 implicit_consts.safe_push (true);
9284 break;
9285 default:
9286 implicit_consts.safe_push (false);
9287 break;
9291 else if (dwarf_version >= 5)
9293 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9294 if (!implicit_consts[ix])
9295 continue;
9296 else
9298 dw_attr_node *other_a
9299 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9300 if (!dw_val_equal_p (&a->dw_attr_val,
9301 &other_a->dw_attr_val))
9302 implicit_consts[ix] = false;
9305 die->die_abbrev = abbrev_id;
9307 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9308 if (dwarf_version >= 5 && first_id != ~0U)
9309 optimize_implicit_const (first_id, i, implicit_consts);
9312 abbrev_opt_start = 0;
9313 abbrev_opt_base_type_end = 0;
9314 abbrev_usage_count.release ();
9315 sorted_abbrev_dies.release ();
9318 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9320 static int
9321 constant_size (unsigned HOST_WIDE_INT value)
9323 int log;
9325 if (value == 0)
9326 log = 0;
9327 else
9328 log = floor_log2 (value);
9330 log = log / 8;
9331 log = 1 << (floor_log2 (log) + 1);
9333 return log;
9336 /* Return the size of a DIE as it is represented in the
9337 .debug_info section. */
9339 static unsigned long
9340 size_of_die (dw_die_ref die)
9342 unsigned long size = 0;
9343 dw_attr_node *a;
9344 unsigned ix;
9345 enum dwarf_form form;
9347 size += size_of_uleb128 (die->die_abbrev);
9348 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9350 switch (AT_class (a))
9352 case dw_val_class_addr:
9353 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9355 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9356 size += size_of_uleb128 (AT_index (a));
9358 else
9359 size += DWARF2_ADDR_SIZE;
9360 break;
9361 case dw_val_class_offset:
9362 size += DWARF_OFFSET_SIZE;
9363 break;
9364 case dw_val_class_loc:
9366 unsigned long lsize = size_of_locs (AT_loc (a));
9368 /* Block length. */
9369 if (dwarf_version >= 4)
9370 size += size_of_uleb128 (lsize);
9371 else
9372 size += constant_size (lsize);
9373 size += lsize;
9375 break;
9376 case dw_val_class_loc_list:
9377 case dw_val_class_view_list:
9378 if (dwarf_split_debug_info && dwarf_version >= 5)
9380 gcc_assert (AT_loc_list (a)->num_assigned);
9381 size += size_of_uleb128 (AT_loc_list (a)->hash);
9383 else
9384 size += DWARF_OFFSET_SIZE;
9385 break;
9386 case dw_val_class_range_list:
9387 if (value_format (a) == DW_FORM_rnglistx)
9389 gcc_assert (rnglist_idx);
9390 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9391 size += size_of_uleb128 (r->idx);
9393 else
9394 size += DWARF_OFFSET_SIZE;
9395 break;
9396 case dw_val_class_const:
9397 size += size_of_sleb128 (AT_int (a));
9398 break;
9399 case dw_val_class_unsigned_const:
9401 int csize = constant_size (AT_unsigned (a));
9402 if (dwarf_version == 3
9403 && a->dw_attr == DW_AT_data_member_location
9404 && csize >= 4)
9405 size += size_of_uleb128 (AT_unsigned (a));
9406 else
9407 size += csize;
9409 break;
9410 case dw_val_class_symview:
9411 if (symview_upper_bound <= 0xff)
9412 size += 1;
9413 else if (symview_upper_bound <= 0xffff)
9414 size += 2;
9415 else if (symview_upper_bound <= 0xffffffff)
9416 size += 4;
9417 else
9418 size += 8;
9419 break;
9420 case dw_val_class_const_implicit:
9421 case dw_val_class_unsigned_const_implicit:
9422 case dw_val_class_file_implicit:
9423 /* These occupy no size in the DIE, just an extra sleb128 in
9424 .debug_abbrev. */
9425 break;
9426 case dw_val_class_const_double:
9427 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9428 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9429 size++; /* block */
9430 break;
9431 case dw_val_class_wide_int:
9432 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9433 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9434 if (get_full_len (*a->dw_attr_val.v.val_wide)
9435 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9436 size++; /* block */
9437 break;
9438 case dw_val_class_vec:
9439 size += constant_size (a->dw_attr_val.v.val_vec.length
9440 * a->dw_attr_val.v.val_vec.elt_size)
9441 + a->dw_attr_val.v.val_vec.length
9442 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9443 break;
9444 case dw_val_class_flag:
9445 if (dwarf_version >= 4)
9446 /* Currently all add_AT_flag calls pass in 1 as last argument,
9447 so DW_FORM_flag_present can be used. If that ever changes,
9448 we'll need to use DW_FORM_flag and have some optimization
9449 in build_abbrev_table that will change those to
9450 DW_FORM_flag_present if it is set to 1 in all DIEs using
9451 the same abbrev entry. */
9452 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9453 else
9454 size += 1;
9455 break;
9456 case dw_val_class_die_ref:
9457 if (AT_ref_external (a))
9459 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9460 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9461 is sized by target address length, whereas in DWARF3
9462 it's always sized as an offset. */
9463 if (use_debug_types)
9464 size += DWARF_TYPE_SIGNATURE_SIZE;
9465 else if (dwarf_version == 2)
9466 size += DWARF2_ADDR_SIZE;
9467 else
9468 size += DWARF_OFFSET_SIZE;
9470 else
9471 size += DWARF_OFFSET_SIZE;
9472 break;
9473 case dw_val_class_fde_ref:
9474 size += DWARF_OFFSET_SIZE;
9475 break;
9476 case dw_val_class_lbl_id:
9477 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9479 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9480 size += size_of_uleb128 (AT_index (a));
9482 else
9483 size += DWARF2_ADDR_SIZE;
9484 break;
9485 case dw_val_class_lineptr:
9486 case dw_val_class_macptr:
9487 case dw_val_class_loclistsptr:
9488 size += DWARF_OFFSET_SIZE;
9489 break;
9490 case dw_val_class_str:
9491 form = AT_string_form (a);
9492 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9493 size += DWARF_OFFSET_SIZE;
9494 else if (form == dwarf_FORM (DW_FORM_strx))
9495 size += size_of_uleb128 (AT_index (a));
9496 else
9497 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9498 break;
9499 case dw_val_class_file:
9500 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9501 break;
9502 case dw_val_class_data8:
9503 size += 8;
9504 break;
9505 case dw_val_class_vms_delta:
9506 size += DWARF_OFFSET_SIZE;
9507 break;
9508 case dw_val_class_high_pc:
9509 size += DWARF2_ADDR_SIZE;
9510 break;
9511 case dw_val_class_discr_value:
9512 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9513 break;
9514 case dw_val_class_discr_list:
9516 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9518 /* This is a block, so we have the block length and then its
9519 data. */
9520 size += constant_size (block_size) + block_size;
9522 break;
9523 default:
9524 gcc_unreachable ();
9528 return size;
9531 /* Size the debugging information associated with a given DIE. Visits the
9532 DIE's children recursively. Updates the global variable next_die_offset, on
9533 each time through. Uses the current value of next_die_offset to update the
9534 die_offset field in each DIE. */
9536 static void
9537 calc_die_sizes (dw_die_ref die)
9539 dw_die_ref c;
9541 gcc_assert (die->die_offset == 0
9542 || (unsigned long int) die->die_offset == next_die_offset);
9543 die->die_offset = next_die_offset;
9544 next_die_offset += size_of_die (die);
9546 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9548 if (die->die_child != NULL)
9549 /* Count the null byte used to terminate sibling lists. */
9550 next_die_offset += 1;
9553 /* Size just the base type children at the start of the CU.
9554 This is needed because build_abbrev needs to size locs
9555 and sizing of type based stack ops needs to know die_offset
9556 values for the base types. */
9558 static void
9559 calc_base_type_die_sizes (void)
9561 unsigned long die_offset = (dwarf_split_debug_info
9562 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9563 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9564 unsigned int i;
9565 dw_die_ref base_type;
9566 #if ENABLE_ASSERT_CHECKING
9567 dw_die_ref prev = comp_unit_die ()->die_child;
9568 #endif
9570 die_offset += size_of_die (comp_unit_die ());
9571 for (i = 0; base_types.iterate (i, &base_type); i++)
9573 #if ENABLE_ASSERT_CHECKING
9574 gcc_assert (base_type->die_offset == 0
9575 && prev->die_sib == base_type
9576 && base_type->die_child == NULL
9577 && base_type->die_abbrev);
9578 prev = base_type;
9579 #endif
9580 if (abbrev_opt_start
9581 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9582 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9583 base_type->die_offset = die_offset;
9584 die_offset += size_of_die (base_type);
9588 /* Set the marks for a die and its children. We do this so
9589 that we know whether or not a reference needs to use FORM_ref_addr; only
9590 DIEs in the same CU will be marked. We used to clear out the offset
9591 and use that as the flag, but ran into ordering problems. */
9593 static void
9594 mark_dies (dw_die_ref die)
9596 dw_die_ref c;
9598 gcc_assert (!die->die_mark);
9600 die->die_mark = 1;
9601 FOR_EACH_CHILD (die, c, mark_dies (c));
9604 /* Clear the marks for a die and its children. */
9606 static void
9607 unmark_dies (dw_die_ref die)
9609 dw_die_ref c;
9611 if (! use_debug_types)
9612 gcc_assert (die->die_mark);
9614 die->die_mark = 0;
9615 FOR_EACH_CHILD (die, c, unmark_dies (c));
9618 /* Clear the marks for a die, its children and referred dies. */
9620 static void
9621 unmark_all_dies (dw_die_ref die)
9623 dw_die_ref c;
9624 dw_attr_node *a;
9625 unsigned ix;
9627 if (!die->die_mark)
9628 return;
9629 die->die_mark = 0;
9631 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9633 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9634 if (AT_class (a) == dw_val_class_die_ref)
9635 unmark_all_dies (AT_ref (a));
9638 /* Calculate if the entry should appear in the final output file. It may be
9639 from a pruned a type. */
9641 static bool
9642 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9644 /* By limiting gnu pubnames to definitions only, gold can generate a
9645 gdb index without entries for declarations, which don't include
9646 enough information to be useful. */
9647 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9648 return false;
9650 if (table == pubname_table)
9652 /* Enumerator names are part of the pubname table, but the
9653 parent DW_TAG_enumeration_type die may have been pruned.
9654 Don't output them if that is the case. */
9655 if (p->die->die_tag == DW_TAG_enumerator &&
9656 (p->die->die_parent == NULL
9657 || !p->die->die_parent->die_perennial_p))
9658 return false;
9660 /* Everything else in the pubname table is included. */
9661 return true;
9664 /* The pubtypes table shouldn't include types that have been
9665 pruned. */
9666 return (p->die->die_offset != 0
9667 || !flag_eliminate_unused_debug_types);
9670 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9671 generated for the compilation unit. */
9673 static unsigned long
9674 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9676 unsigned long size;
9677 unsigned i;
9678 pubname_entry *p;
9679 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9681 size = DWARF_PUBNAMES_HEADER_SIZE;
9682 FOR_EACH_VEC_ELT (*names, i, p)
9683 if (include_pubname_in_output (names, p))
9684 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9686 size += DWARF_OFFSET_SIZE;
9687 return size;
9690 /* Return the size of the information in the .debug_aranges section. */
9692 static unsigned long
9693 size_of_aranges (void)
9695 unsigned long size;
9697 size = DWARF_ARANGES_HEADER_SIZE;
9699 /* Count the address/length pair for this compilation unit. */
9700 if (text_section_used)
9701 size += 2 * DWARF2_ADDR_SIZE;
9702 if (cold_text_section_used)
9703 size += 2 * DWARF2_ADDR_SIZE;
9704 if (have_multiple_function_sections)
9706 unsigned fde_idx;
9707 dw_fde_ref fde;
9709 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9711 if (DECL_IGNORED_P (fde->decl))
9712 continue;
9713 if (!fde->in_std_section)
9714 size += 2 * DWARF2_ADDR_SIZE;
9715 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9716 size += 2 * DWARF2_ADDR_SIZE;
9720 /* Count the two zero words used to terminated the address range table. */
9721 size += 2 * DWARF2_ADDR_SIZE;
9722 return size;
9725 /* Select the encoding of an attribute value. */
9727 static enum dwarf_form
9728 value_format (dw_attr_node *a)
9730 switch (AT_class (a))
9732 case dw_val_class_addr:
9733 /* Only very few attributes allow DW_FORM_addr. */
9734 switch (a->dw_attr)
9736 case DW_AT_low_pc:
9737 case DW_AT_high_pc:
9738 case DW_AT_entry_pc:
9739 case DW_AT_trampoline:
9740 return (AT_index (a) == NOT_INDEXED
9741 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9742 default:
9743 break;
9745 switch (DWARF2_ADDR_SIZE)
9747 case 1:
9748 return DW_FORM_data1;
9749 case 2:
9750 return DW_FORM_data2;
9751 case 4:
9752 return DW_FORM_data4;
9753 case 8:
9754 return DW_FORM_data8;
9755 default:
9756 gcc_unreachable ();
9758 case dw_val_class_loc_list:
9759 case dw_val_class_view_list:
9760 if (dwarf_split_debug_info
9761 && dwarf_version >= 5
9762 && AT_loc_list (a)->num_assigned)
9763 return DW_FORM_loclistx;
9764 /* FALLTHRU */
9765 case dw_val_class_range_list:
9766 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9767 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9768 care about sizes of .debug* sections in shared libraries and
9769 executables and don't take into account relocations that affect just
9770 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9771 table in the .debug_rnglists section. */
9772 if (dwarf_split_debug_info
9773 && dwarf_version >= 5
9774 && AT_class (a) == dw_val_class_range_list
9775 && rnglist_idx
9776 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9777 return DW_FORM_rnglistx;
9778 if (dwarf_version >= 4)
9779 return DW_FORM_sec_offset;
9780 /* FALLTHRU */
9781 case dw_val_class_vms_delta:
9782 case dw_val_class_offset:
9783 switch (DWARF_OFFSET_SIZE)
9785 case 4:
9786 return DW_FORM_data4;
9787 case 8:
9788 return DW_FORM_data8;
9789 default:
9790 gcc_unreachable ();
9792 case dw_val_class_loc:
9793 if (dwarf_version >= 4)
9794 return DW_FORM_exprloc;
9795 switch (constant_size (size_of_locs (AT_loc (a))))
9797 case 1:
9798 return DW_FORM_block1;
9799 case 2:
9800 return DW_FORM_block2;
9801 case 4:
9802 return DW_FORM_block4;
9803 default:
9804 gcc_unreachable ();
9806 case dw_val_class_const:
9807 return DW_FORM_sdata;
9808 case dw_val_class_unsigned_const:
9809 switch (constant_size (AT_unsigned (a)))
9811 case 1:
9812 return DW_FORM_data1;
9813 case 2:
9814 return DW_FORM_data2;
9815 case 4:
9816 /* In DWARF3 DW_AT_data_member_location with
9817 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9818 constant, so we need to use DW_FORM_udata if we need
9819 a large constant. */
9820 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9821 return DW_FORM_udata;
9822 return DW_FORM_data4;
9823 case 8:
9824 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9825 return DW_FORM_udata;
9826 return DW_FORM_data8;
9827 default:
9828 gcc_unreachable ();
9830 case dw_val_class_const_implicit:
9831 case dw_val_class_unsigned_const_implicit:
9832 case dw_val_class_file_implicit:
9833 return DW_FORM_implicit_const;
9834 case dw_val_class_const_double:
9835 switch (HOST_BITS_PER_WIDE_INT)
9837 case 8:
9838 return DW_FORM_data2;
9839 case 16:
9840 return DW_FORM_data4;
9841 case 32:
9842 return DW_FORM_data8;
9843 case 64:
9844 if (dwarf_version >= 5)
9845 return DW_FORM_data16;
9846 /* FALLTHRU */
9847 default:
9848 return DW_FORM_block1;
9850 case dw_val_class_wide_int:
9851 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9853 case 8:
9854 return DW_FORM_data1;
9855 case 16:
9856 return DW_FORM_data2;
9857 case 32:
9858 return DW_FORM_data4;
9859 case 64:
9860 return DW_FORM_data8;
9861 case 128:
9862 if (dwarf_version >= 5)
9863 return DW_FORM_data16;
9864 /* FALLTHRU */
9865 default:
9866 return DW_FORM_block1;
9868 case dw_val_class_symview:
9869 /* ??? We might use uleb128, but then we'd have to compute
9870 .debug_info offsets in the assembler. */
9871 if (symview_upper_bound <= 0xff)
9872 return DW_FORM_data1;
9873 else if (symview_upper_bound <= 0xffff)
9874 return DW_FORM_data2;
9875 else if (symview_upper_bound <= 0xffffffff)
9876 return DW_FORM_data4;
9877 else
9878 return DW_FORM_data8;
9879 case dw_val_class_vec:
9880 switch (constant_size (a->dw_attr_val.v.val_vec.length
9881 * a->dw_attr_val.v.val_vec.elt_size))
9883 case 1:
9884 return DW_FORM_block1;
9885 case 2:
9886 return DW_FORM_block2;
9887 case 4:
9888 return DW_FORM_block4;
9889 default:
9890 gcc_unreachable ();
9892 case dw_val_class_flag:
9893 if (dwarf_version >= 4)
9895 /* Currently all add_AT_flag calls pass in 1 as last argument,
9896 so DW_FORM_flag_present can be used. If that ever changes,
9897 we'll need to use DW_FORM_flag and have some optimization
9898 in build_abbrev_table that will change those to
9899 DW_FORM_flag_present if it is set to 1 in all DIEs using
9900 the same abbrev entry. */
9901 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9902 return DW_FORM_flag_present;
9904 return DW_FORM_flag;
9905 case dw_val_class_die_ref:
9906 if (AT_ref_external (a))
9907 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9908 else
9909 return DW_FORM_ref;
9910 case dw_val_class_fde_ref:
9911 return DW_FORM_data;
9912 case dw_val_class_lbl_id:
9913 return (AT_index (a) == NOT_INDEXED
9914 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9915 case dw_val_class_lineptr:
9916 case dw_val_class_macptr:
9917 case dw_val_class_loclistsptr:
9918 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9919 case dw_val_class_str:
9920 return AT_string_form (a);
9921 case dw_val_class_file:
9922 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9924 case 1:
9925 return DW_FORM_data1;
9926 case 2:
9927 return DW_FORM_data2;
9928 case 4:
9929 return DW_FORM_data4;
9930 default:
9931 gcc_unreachable ();
9934 case dw_val_class_data8:
9935 return DW_FORM_data8;
9937 case dw_val_class_high_pc:
9938 switch (DWARF2_ADDR_SIZE)
9940 case 1:
9941 return DW_FORM_data1;
9942 case 2:
9943 return DW_FORM_data2;
9944 case 4:
9945 return DW_FORM_data4;
9946 case 8:
9947 return DW_FORM_data8;
9948 default:
9949 gcc_unreachable ();
9952 case dw_val_class_discr_value:
9953 return (a->dw_attr_val.v.val_discr_value.pos
9954 ? DW_FORM_udata
9955 : DW_FORM_sdata);
9956 case dw_val_class_discr_list:
9957 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9959 case 1:
9960 return DW_FORM_block1;
9961 case 2:
9962 return DW_FORM_block2;
9963 case 4:
9964 return DW_FORM_block4;
9965 default:
9966 gcc_unreachable ();
9969 default:
9970 gcc_unreachable ();
9974 /* Output the encoding of an attribute value. */
9976 static void
9977 output_value_format (dw_attr_node *a)
9979 enum dwarf_form form = value_format (a);
9981 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9984 /* Given a die and id, produce the appropriate abbreviations. */
9986 static void
9987 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9989 unsigned ix;
9990 dw_attr_node *a_attr;
9992 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9993 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9994 dwarf_tag_name (abbrev->die_tag));
9996 if (abbrev->die_child != NULL)
9997 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9998 else
9999 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10001 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10003 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10004 dwarf_attr_name (a_attr->dw_attr));
10005 output_value_format (a_attr);
10006 if (value_format (a_attr) == DW_FORM_implicit_const)
10008 if (AT_class (a_attr) == dw_val_class_file_implicit)
10010 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10011 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10012 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10014 else
10015 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10019 dw2_asm_output_data (1, 0, NULL);
10020 dw2_asm_output_data (1, 0, NULL);
10024 /* Output the .debug_abbrev section which defines the DIE abbreviation
10025 table. */
10027 static void
10028 output_abbrev_section (void)
10030 unsigned int abbrev_id;
10031 dw_die_ref abbrev;
10033 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10034 if (abbrev_id != 0)
10035 output_die_abbrevs (abbrev_id, abbrev);
10037 /* Terminate the table. */
10038 dw2_asm_output_data (1, 0, NULL);
10041 /* Return a new location list, given the begin and end range, and the
10042 expression. */
10044 static inline dw_loc_list_ref
10045 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10046 const char *end, var_loc_view vend,
10047 const char *section)
10049 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10051 retlist->begin = begin;
10052 retlist->begin_entry = NULL;
10053 retlist->end = end;
10054 retlist->expr = expr;
10055 retlist->section = section;
10056 retlist->vbegin = vbegin;
10057 retlist->vend = vend;
10059 return retlist;
10062 /* Return true iff there's any nonzero view number in the loc list.
10064 ??? When views are not enabled, we'll often extend a single range
10065 to the entire function, so that we emit a single location
10066 expression rather than a location list. With views, even with a
10067 single range, we'll output a list if start or end have a nonzero
10068 view. If we change this, we may want to stop splitting a single
10069 range in dw_loc_list just because of a nonzero view, even if it
10070 straddles across hot/cold partitions. */
10072 static bool
10073 loc_list_has_views (dw_loc_list_ref list)
10075 if (!debug_variable_location_views)
10076 return false;
10078 for (dw_loc_list_ref loc = list;
10079 loc != NULL; loc = loc->dw_loc_next)
10080 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10081 return true;
10083 return false;
10086 /* Generate a new internal symbol for this location list node, if it
10087 hasn't got one yet. */
10089 static inline void
10090 gen_llsym (dw_loc_list_ref list)
10092 gcc_assert (!list->ll_symbol);
10093 list->ll_symbol = gen_internal_sym ("LLST");
10095 if (!loc_list_has_views (list))
10096 return;
10098 if (dwarf2out_locviews_in_attribute ())
10100 /* Use the same label_num for the view list. */
10101 label_num--;
10102 list->vl_symbol = gen_internal_sym ("LVUS");
10104 else
10105 list->vl_symbol = list->ll_symbol;
10108 /* Generate a symbol for the list, but only if we really want to emit
10109 it as a list. */
10111 static inline void
10112 maybe_gen_llsym (dw_loc_list_ref list)
10114 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10115 return;
10117 gen_llsym (list);
10120 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10121 NULL, don't consider size of the location expression. If we're not
10122 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10123 representation in *SIZEP. */
10125 static bool
10126 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10128 /* Don't output an entry that starts and ends at the same address. */
10129 if (strcmp (curr->begin, curr->end) == 0
10130 && curr->vbegin == curr->vend && !curr->force)
10131 return true;
10133 if (!sizep)
10134 return false;
10136 unsigned long size = size_of_locs (curr->expr);
10138 /* If the expression is too large, drop it on the floor. We could
10139 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10140 in the expression, but >= 64KB expressions for a single value
10141 in a single range are unlikely very useful. */
10142 if (dwarf_version < 5 && size > 0xffff)
10143 return true;
10145 *sizep = size;
10147 return false;
10150 /* Output a view pair loclist entry for CURR, if it requires one. */
10152 static void
10153 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10155 if (!dwarf2out_locviews_in_loclist ())
10156 return;
10158 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10159 return;
10161 #ifdef DW_LLE_view_pair
10162 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10164 if (dwarf2out_as_locview_support)
10166 if (ZERO_VIEW_P (curr->vbegin))
10167 dw2_asm_output_data_uleb128 (0, "Location view begin");
10168 else
10170 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10171 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10172 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10175 if (ZERO_VIEW_P (curr->vend))
10176 dw2_asm_output_data_uleb128 (0, "Location view end");
10177 else
10179 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10180 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10181 dw2_asm_output_symname_uleb128 (label, "Location view end");
10184 else
10186 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10187 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10189 #endif /* DW_LLE_view_pair */
10191 return;
10194 /* Output the location list given to us. */
10196 static void
10197 output_loc_list (dw_loc_list_ref list_head)
10199 int vcount = 0, lcount = 0;
10201 if (list_head->emitted)
10202 return;
10203 list_head->emitted = true;
10205 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10207 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10209 for (dw_loc_list_ref curr = list_head; curr != NULL;
10210 curr = curr->dw_loc_next)
10212 unsigned long size;
10214 if (skip_loc_list_entry (curr, &size))
10215 continue;
10217 vcount++;
10219 /* ?? dwarf_split_debug_info? */
10220 if (dwarf2out_as_locview_support)
10222 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10224 if (!ZERO_VIEW_P (curr->vbegin))
10226 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10227 dw2_asm_output_symname_uleb128 (label,
10228 "View list begin (%s)",
10229 list_head->vl_symbol);
10231 else
10232 dw2_asm_output_data_uleb128 (0,
10233 "View list begin (%s)",
10234 list_head->vl_symbol);
10236 if (!ZERO_VIEW_P (curr->vend))
10238 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10239 dw2_asm_output_symname_uleb128 (label,
10240 "View list end (%s)",
10241 list_head->vl_symbol);
10243 else
10244 dw2_asm_output_data_uleb128 (0,
10245 "View list end (%s)",
10246 list_head->vl_symbol);
10248 else
10250 dw2_asm_output_data_uleb128 (curr->vbegin,
10251 "View list begin (%s)",
10252 list_head->vl_symbol);
10253 dw2_asm_output_data_uleb128 (curr->vend,
10254 "View list end (%s)",
10255 list_head->vl_symbol);
10260 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10262 const char *last_section = NULL;
10263 const char *base_label = NULL;
10265 /* Walk the location list, and output each range + expression. */
10266 for (dw_loc_list_ref curr = list_head; curr != NULL;
10267 curr = curr->dw_loc_next)
10269 unsigned long size;
10271 /* Skip this entry? If we skip it here, we must skip it in the
10272 view list above as well. */
10273 if (skip_loc_list_entry (curr, &size))
10274 continue;
10276 lcount++;
10278 if (dwarf_version >= 5)
10280 if (dwarf_split_debug_info)
10282 dwarf2out_maybe_output_loclist_view_pair (curr);
10283 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
10284 uleb128 index into .debug_addr and uleb128 length. */
10285 dw2_asm_output_data (1, DW_LLE_startx_length,
10286 "DW_LLE_startx_length (%s)",
10287 list_head->ll_symbol);
10288 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10289 "Location list range start index "
10290 "(%s)", curr->begin);
10291 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
10292 For that case we probably need to emit DW_LLE_startx_endx,
10293 but we'd need 2 .debug_addr entries rather than just one. */
10294 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10295 "Location list length (%s)",
10296 list_head->ll_symbol);
10298 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10300 dwarf2out_maybe_output_loclist_view_pair (curr);
10301 /* If all code is in .text section, the base address is
10302 already provided by the CU attributes. Use
10303 DW_LLE_offset_pair where both addresses are uleb128 encoded
10304 offsets against that base. */
10305 dw2_asm_output_data (1, DW_LLE_offset_pair,
10306 "DW_LLE_offset_pair (%s)",
10307 list_head->ll_symbol);
10308 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10309 "Location list begin address (%s)",
10310 list_head->ll_symbol);
10311 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10312 "Location list end address (%s)",
10313 list_head->ll_symbol);
10315 else if (HAVE_AS_LEB128)
10317 /* Otherwise, find out how many consecutive entries could share
10318 the same base entry. If just one, emit DW_LLE_start_length,
10319 otherwise emit DW_LLE_base_address for the base address
10320 followed by a series of DW_LLE_offset_pair. */
10321 if (last_section == NULL || curr->section != last_section)
10323 dw_loc_list_ref curr2;
10324 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10325 curr2 = curr2->dw_loc_next)
10327 if (strcmp (curr2->begin, curr2->end) == 0
10328 && !curr2->force)
10329 continue;
10330 break;
10332 if (curr2 == NULL || curr->section != curr2->section)
10333 last_section = NULL;
10334 else
10336 last_section = curr->section;
10337 base_label = curr->begin;
10338 dw2_asm_output_data (1, DW_LLE_base_address,
10339 "DW_LLE_base_address (%s)",
10340 list_head->ll_symbol);
10341 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10342 "Base address (%s)",
10343 list_head->ll_symbol);
10346 /* Only one entry with the same base address. Use
10347 DW_LLE_start_length with absolute address and uleb128
10348 length. */
10349 if (last_section == NULL)
10351 dwarf2out_maybe_output_loclist_view_pair (curr);
10352 dw2_asm_output_data (1, DW_LLE_start_length,
10353 "DW_LLE_start_length (%s)",
10354 list_head->ll_symbol);
10355 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10356 "Location list begin address (%s)",
10357 list_head->ll_symbol);
10358 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10359 "Location list length "
10360 "(%s)", list_head->ll_symbol);
10362 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10363 DW_LLE_base_address. */
10364 else
10366 dwarf2out_maybe_output_loclist_view_pair (curr);
10367 dw2_asm_output_data (1, DW_LLE_offset_pair,
10368 "DW_LLE_offset_pair (%s)",
10369 list_head->ll_symbol);
10370 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10371 "Location list begin address "
10372 "(%s)", list_head->ll_symbol);
10373 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10374 "Location list end address "
10375 "(%s)", list_head->ll_symbol);
10378 /* The assembler does not support .uleb128 directive. Emit
10379 DW_LLE_start_end with a pair of absolute addresses. */
10380 else
10382 dwarf2out_maybe_output_loclist_view_pair (curr);
10383 dw2_asm_output_data (1, DW_LLE_start_end,
10384 "DW_LLE_start_end (%s)",
10385 list_head->ll_symbol);
10386 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10387 "Location list begin address (%s)",
10388 list_head->ll_symbol);
10389 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10390 "Location list end address (%s)",
10391 list_head->ll_symbol);
10394 else if (dwarf_split_debug_info)
10396 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10397 and 4 byte length. */
10398 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10399 "Location list start/length entry (%s)",
10400 list_head->ll_symbol);
10401 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10402 "Location list range start index (%s)",
10403 curr->begin);
10404 /* The length field is 4 bytes. If we ever need to support
10405 an 8-byte length, we can add a new DW_LLE code or fall back
10406 to DW_LLE_GNU_start_end_entry. */
10407 dw2_asm_output_delta (4, curr->end, curr->begin,
10408 "Location list range length (%s)",
10409 list_head->ll_symbol);
10411 else if (!have_multiple_function_sections)
10413 /* Pair of relative addresses against start of text section. */
10414 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10415 "Location list begin address (%s)",
10416 list_head->ll_symbol);
10417 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10418 "Location list end address (%s)",
10419 list_head->ll_symbol);
10421 else
10423 /* Pair of absolute addresses. */
10424 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10425 "Location list begin address (%s)",
10426 list_head->ll_symbol);
10427 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10428 "Location list end address (%s)",
10429 list_head->ll_symbol);
10432 /* Output the block length for this list of location operations. */
10433 if (dwarf_version >= 5)
10434 dw2_asm_output_data_uleb128 (size, "Location expression size");
10435 else
10437 gcc_assert (size <= 0xffff);
10438 dw2_asm_output_data (2, size, "Location expression size");
10441 output_loc_sequence (curr->expr, -1);
10444 /* And finally list termination. */
10445 if (dwarf_version >= 5)
10446 dw2_asm_output_data (1, DW_LLE_end_of_list,
10447 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10448 else if (dwarf_split_debug_info)
10449 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10450 "Location list terminator (%s)",
10451 list_head->ll_symbol);
10452 else
10454 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10455 "Location list terminator begin (%s)",
10456 list_head->ll_symbol);
10457 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10458 "Location list terminator end (%s)",
10459 list_head->ll_symbol);
10462 gcc_assert (!list_head->vl_symbol
10463 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10466 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10467 section. Emit a relocated reference if val_entry is NULL, otherwise,
10468 emit an indirect reference. */
10470 static void
10471 output_range_list_offset (dw_attr_node *a)
10473 const char *name = dwarf_attr_name (a->dw_attr);
10475 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10477 if (dwarf_version >= 5)
10479 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10480 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
10481 debug_ranges_section, "%s", name);
10483 else
10485 char *p = strchr (ranges_section_label, '\0');
10486 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10487 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10488 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10489 debug_ranges_section, "%s", name);
10490 *p = '\0';
10493 else if (dwarf_version >= 5)
10495 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10496 gcc_assert (rnglist_idx);
10497 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10499 else
10500 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10501 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10502 "%s (offset from %s)", name, ranges_section_label);
10505 /* Output the offset into the debug_loc section. */
10507 static void
10508 output_loc_list_offset (dw_attr_node *a)
10510 char *sym = AT_loc_list (a)->ll_symbol;
10512 gcc_assert (sym);
10513 if (!dwarf_split_debug_info)
10514 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10515 "%s", dwarf_attr_name (a->dw_attr));
10516 else if (dwarf_version >= 5)
10518 gcc_assert (AT_loc_list (a)->num_assigned);
10519 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10520 dwarf_attr_name (a->dw_attr),
10521 sym);
10523 else
10524 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10525 "%s", dwarf_attr_name (a->dw_attr));
10528 /* Output the offset into the debug_loc section. */
10530 static void
10531 output_view_list_offset (dw_attr_node *a)
10533 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10535 gcc_assert (sym);
10536 if (dwarf_split_debug_info)
10537 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10538 "%s", dwarf_attr_name (a->dw_attr));
10539 else
10540 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10541 "%s", dwarf_attr_name (a->dw_attr));
10544 /* Output an attribute's index or value appropriately. */
10546 static void
10547 output_attr_index_or_value (dw_attr_node *a)
10549 const char *name = dwarf_attr_name (a->dw_attr);
10551 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10553 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10554 return;
10556 switch (AT_class (a))
10558 case dw_val_class_addr:
10559 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10560 break;
10561 case dw_val_class_high_pc:
10562 case dw_val_class_lbl_id:
10563 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10564 break;
10565 default:
10566 gcc_unreachable ();
10570 /* Output a type signature. */
10572 static inline void
10573 output_signature (const char *sig, const char *name)
10575 int i;
10577 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10578 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10581 /* Output a discriminant value. */
10583 static inline void
10584 output_discr_value (dw_discr_value *discr_value, const char *name)
10586 if (discr_value->pos)
10587 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10588 else
10589 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10592 /* Output the DIE and its attributes. Called recursively to generate
10593 the definitions of each child DIE. */
10595 static void
10596 output_die (dw_die_ref die)
10598 dw_attr_node *a;
10599 dw_die_ref c;
10600 unsigned long size;
10601 unsigned ix;
10603 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10604 (unsigned long)die->die_offset,
10605 dwarf_tag_name (die->die_tag));
10607 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10609 const char *name = dwarf_attr_name (a->dw_attr);
10611 switch (AT_class (a))
10613 case dw_val_class_addr:
10614 output_attr_index_or_value (a);
10615 break;
10617 case dw_val_class_offset:
10618 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10619 "%s", name);
10620 break;
10622 case dw_val_class_range_list:
10623 output_range_list_offset (a);
10624 break;
10626 case dw_val_class_loc:
10627 size = size_of_locs (AT_loc (a));
10629 /* Output the block length for this list of location operations. */
10630 if (dwarf_version >= 4)
10631 dw2_asm_output_data_uleb128 (size, "%s", name);
10632 else
10633 dw2_asm_output_data (constant_size (size), size, "%s", name);
10635 output_loc_sequence (AT_loc (a), -1);
10636 break;
10638 case dw_val_class_const:
10639 /* ??? It would be slightly more efficient to use a scheme like is
10640 used for unsigned constants below, but gdb 4.x does not sign
10641 extend. Gdb 5.x does sign extend. */
10642 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10643 break;
10645 case dw_val_class_unsigned_const:
10647 int csize = constant_size (AT_unsigned (a));
10648 if (dwarf_version == 3
10649 && a->dw_attr == DW_AT_data_member_location
10650 && csize >= 4)
10651 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10652 else
10653 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10655 break;
10657 case dw_val_class_symview:
10659 int vsize;
10660 if (symview_upper_bound <= 0xff)
10661 vsize = 1;
10662 else if (symview_upper_bound <= 0xffff)
10663 vsize = 2;
10664 else if (symview_upper_bound <= 0xffffffff)
10665 vsize = 4;
10666 else
10667 vsize = 8;
10668 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10669 "%s", name);
10671 break;
10673 case dw_val_class_const_implicit:
10674 if (flag_debug_asm)
10675 fprintf (asm_out_file, "\t\t\t%s %s ("
10676 HOST_WIDE_INT_PRINT_DEC ")\n",
10677 ASM_COMMENT_START, name, AT_int (a));
10678 break;
10680 case dw_val_class_unsigned_const_implicit:
10681 if (flag_debug_asm)
10682 fprintf (asm_out_file, "\t\t\t%s %s ("
10683 HOST_WIDE_INT_PRINT_HEX ")\n",
10684 ASM_COMMENT_START, name, AT_unsigned (a));
10685 break;
10687 case dw_val_class_const_double:
10689 unsigned HOST_WIDE_INT first, second;
10691 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10692 dw2_asm_output_data (1,
10693 HOST_BITS_PER_DOUBLE_INT
10694 / HOST_BITS_PER_CHAR,
10695 NULL);
10697 if (WORDS_BIG_ENDIAN)
10699 first = a->dw_attr_val.v.val_double.high;
10700 second = a->dw_attr_val.v.val_double.low;
10702 else
10704 first = a->dw_attr_val.v.val_double.low;
10705 second = a->dw_attr_val.v.val_double.high;
10708 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10709 first, "%s", name);
10710 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10711 second, NULL);
10713 break;
10715 case dw_val_class_wide_int:
10717 int i;
10718 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10719 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10720 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10721 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10722 * l, NULL);
10724 if (WORDS_BIG_ENDIAN)
10725 for (i = len - 1; i >= 0; --i)
10727 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10728 "%s", name);
10729 name = "";
10731 else
10732 for (i = 0; i < len; ++i)
10734 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10735 "%s", name);
10736 name = "";
10739 break;
10741 case dw_val_class_vec:
10743 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10744 unsigned int len = a->dw_attr_val.v.val_vec.length;
10745 unsigned int i;
10746 unsigned char *p;
10748 dw2_asm_output_data (constant_size (len * elt_size),
10749 len * elt_size, "%s", name);
10750 if (elt_size > sizeof (HOST_WIDE_INT))
10752 elt_size /= 2;
10753 len *= 2;
10755 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10756 i < len;
10757 i++, p += elt_size)
10758 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10759 "fp or vector constant word %u", i);
10760 break;
10763 case dw_val_class_flag:
10764 if (dwarf_version >= 4)
10766 /* Currently all add_AT_flag calls pass in 1 as last argument,
10767 so DW_FORM_flag_present can be used. If that ever changes,
10768 we'll need to use DW_FORM_flag and have some optimization
10769 in build_abbrev_table that will change those to
10770 DW_FORM_flag_present if it is set to 1 in all DIEs using
10771 the same abbrev entry. */
10772 gcc_assert (AT_flag (a) == 1);
10773 if (flag_debug_asm)
10774 fprintf (asm_out_file, "\t\t\t%s %s\n",
10775 ASM_COMMENT_START, name);
10776 break;
10778 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10779 break;
10781 case dw_val_class_loc_list:
10782 output_loc_list_offset (a);
10783 break;
10785 case dw_val_class_view_list:
10786 output_view_list_offset (a);
10787 break;
10789 case dw_val_class_die_ref:
10790 if (AT_ref_external (a))
10792 if (AT_ref (a)->comdat_type_p)
10794 comdat_type_node *type_node
10795 = AT_ref (a)->die_id.die_type_node;
10797 gcc_assert (type_node);
10798 output_signature (type_node->signature, name);
10800 else
10802 const char *sym = AT_ref (a)->die_id.die_symbol;
10803 int size;
10805 gcc_assert (sym);
10806 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10807 length, whereas in DWARF3 it's always sized as an
10808 offset. */
10809 if (dwarf_version == 2)
10810 size = DWARF2_ADDR_SIZE;
10811 else
10812 size = DWARF_OFFSET_SIZE;
10813 /* ??? We cannot unconditionally output die_offset if
10814 non-zero - others might create references to those
10815 DIEs via symbols.
10816 And we do not clear its DIE offset after outputting it
10817 (and the label refers to the actual DIEs, not the
10818 DWARF CU unit header which is when using label + offset
10819 would be the correct thing to do).
10820 ??? This is the reason for the with_offset flag. */
10821 if (AT_ref (a)->with_offset)
10822 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10823 debug_info_section, "%s", name);
10824 else
10825 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10826 name);
10829 else
10831 gcc_assert (AT_ref (a)->die_offset);
10832 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10833 "%s", name);
10835 break;
10837 case dw_val_class_fde_ref:
10839 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10841 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10842 a->dw_attr_val.v.val_fde_index * 2);
10843 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10844 "%s", name);
10846 break;
10848 case dw_val_class_vms_delta:
10849 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10850 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10851 AT_vms_delta2 (a), AT_vms_delta1 (a),
10852 "%s", name);
10853 #else
10854 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10855 AT_vms_delta2 (a), AT_vms_delta1 (a),
10856 "%s", name);
10857 #endif
10858 break;
10860 case dw_val_class_lbl_id:
10861 output_attr_index_or_value (a);
10862 break;
10864 case dw_val_class_lineptr:
10865 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10866 debug_line_section, "%s", name);
10867 break;
10869 case dw_val_class_macptr:
10870 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10871 debug_macinfo_section, "%s", name);
10872 break;
10874 case dw_val_class_loclistsptr:
10875 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10876 debug_loc_section, "%s", name);
10877 break;
10879 case dw_val_class_str:
10880 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10881 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10882 a->dw_attr_val.v.val_str->label,
10883 debug_str_section,
10884 "%s: \"%s\"", name, AT_string (a));
10885 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10886 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10887 a->dw_attr_val.v.val_str->label,
10888 debug_line_str_section,
10889 "%s: \"%s\"", name, AT_string (a));
10890 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
10891 dw2_asm_output_data_uleb128 (AT_index (a),
10892 "%s: \"%s\"", name, AT_string (a));
10893 else
10894 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10895 break;
10897 case dw_val_class_file:
10899 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10901 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10902 a->dw_attr_val.v.val_file->filename);
10903 break;
10906 case dw_val_class_file_implicit:
10907 if (flag_debug_asm)
10908 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10909 ASM_COMMENT_START, name,
10910 maybe_emit_file (a->dw_attr_val.v.val_file),
10911 a->dw_attr_val.v.val_file->filename);
10912 break;
10914 case dw_val_class_data8:
10916 int i;
10918 for (i = 0; i < 8; i++)
10919 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10920 i == 0 ? "%s" : NULL, name);
10921 break;
10924 case dw_val_class_high_pc:
10925 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10926 get_AT_low_pc (die), "DW_AT_high_pc");
10927 break;
10929 case dw_val_class_discr_value:
10930 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10931 break;
10933 case dw_val_class_discr_list:
10935 dw_discr_list_ref list = AT_discr_list (a);
10936 const int size = size_of_discr_list (list);
10938 /* This is a block, so output its length first. */
10939 dw2_asm_output_data (constant_size (size), size,
10940 "%s: block size", name);
10942 for (; list != NULL; list = list->dw_discr_next)
10944 /* One byte for the discriminant value descriptor, and then as
10945 many LEB128 numbers as required. */
10946 if (list->dw_discr_range)
10947 dw2_asm_output_data (1, DW_DSC_range,
10948 "%s: DW_DSC_range", name);
10949 else
10950 dw2_asm_output_data (1, DW_DSC_label,
10951 "%s: DW_DSC_label", name);
10953 output_discr_value (&list->dw_discr_lower_bound, name);
10954 if (list->dw_discr_range)
10955 output_discr_value (&list->dw_discr_upper_bound, name);
10957 break;
10960 default:
10961 gcc_unreachable ();
10965 FOR_EACH_CHILD (die, c, output_die (c));
10967 /* Add null byte to terminate sibling list. */
10968 if (die->die_child != NULL)
10969 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10970 (unsigned long) die->die_offset);
10973 /* Output the dwarf version number. */
10975 static void
10976 output_dwarf_version ()
10978 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
10979 views in loclist. That will change eventually. */
10980 if (dwarf_version == 6)
10982 static bool once;
10983 if (!once)
10985 warning (0,
10986 "-gdwarf-6 is output as version 5 with incompatibilities");
10987 once = true;
10989 dw2_asm_output_data (2, 5, "DWARF version number");
10991 else
10992 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10995 /* Output the compilation unit that appears at the beginning of the
10996 .debug_info section, and precedes the DIE descriptions. */
10998 static void
10999 output_compilation_unit_header (enum dwarf_unit_type ut)
11001 if (!XCOFF_DEBUGGING_INFO)
11003 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11004 dw2_asm_output_data (4, 0xffffffff,
11005 "Initial length escape value indicating 64-bit DWARF extension");
11006 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11007 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11008 "Length of Compilation Unit Info");
11011 output_dwarf_version ();
11012 if (dwarf_version >= 5)
11014 const char *name;
11015 switch (ut)
11017 case DW_UT_compile: name = "DW_UT_compile"; break;
11018 case DW_UT_type: name = "DW_UT_type"; break;
11019 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11020 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11021 default: gcc_unreachable ();
11023 dw2_asm_output_data (1, ut, "%s", name);
11024 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11026 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11027 debug_abbrev_section,
11028 "Offset Into Abbrev. Section");
11029 if (dwarf_version < 5)
11030 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11033 /* Output the compilation unit DIE and its children. */
11035 static void
11036 output_comp_unit (dw_die_ref die, int output_if_empty,
11037 const unsigned char *dwo_id)
11039 const char *secname, *oldsym;
11040 char *tmp;
11042 /* Unless we are outputting main CU, we may throw away empty ones. */
11043 if (!output_if_empty && die->die_child == NULL)
11044 return;
11046 /* Even if there are no children of this DIE, we must output the information
11047 about the compilation unit. Otherwise, on an empty translation unit, we
11048 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11049 will then complain when examining the file. First mark all the DIEs in
11050 this CU so we know which get local refs. */
11051 mark_dies (die);
11053 external_ref_hash_type *extern_map = optimize_external_refs (die);
11055 /* For now, optimize only the main CU, in order to optimize the rest
11056 we'd need to see all of them earlier. Leave the rest for post-linking
11057 tools like DWZ. */
11058 if (die == comp_unit_die ())
11059 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11061 build_abbrev_table (die, extern_map);
11063 optimize_abbrev_table ();
11065 delete extern_map;
11067 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11068 next_die_offset = (dwo_id
11069 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11070 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11071 calc_die_sizes (die);
11073 oldsym = die->die_id.die_symbol;
11074 if (oldsym && die->comdat_type_p)
11076 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11078 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11079 secname = tmp;
11080 die->die_id.die_symbol = NULL;
11081 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11083 else
11085 switch_to_section (debug_info_section);
11086 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11087 info_section_emitted = true;
11090 /* For LTO cross unit DIE refs we want a symbol on the start of the
11091 debuginfo section, not on the CU DIE. */
11092 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11094 /* ??? No way to get visibility assembled without a decl. */
11095 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11096 get_identifier (oldsym), char_type_node);
11097 TREE_PUBLIC (decl) = true;
11098 TREE_STATIC (decl) = true;
11099 DECL_ARTIFICIAL (decl) = true;
11100 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11101 DECL_VISIBILITY_SPECIFIED (decl) = true;
11102 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11103 #ifdef ASM_WEAKEN_LABEL
11104 /* We prefer a .weak because that handles duplicates from duplicate
11105 archive members in a graceful way. */
11106 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11107 #else
11108 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11109 #endif
11110 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11113 /* Output debugging information. */
11114 output_compilation_unit_header (dwo_id
11115 ? DW_UT_split_compile : DW_UT_compile);
11116 if (dwarf_version >= 5)
11118 if (dwo_id != NULL)
11119 for (int i = 0; i < 8; i++)
11120 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11122 output_die (die);
11124 /* Leave the marks on the main CU, so we can check them in
11125 output_pubnames. */
11126 if (oldsym)
11128 unmark_dies (die);
11129 die->die_id.die_symbol = oldsym;
11133 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11134 and .debug_pubtypes. This is configured per-target, but can be
11135 overridden by the -gpubnames or -gno-pubnames options. */
11137 static inline bool
11138 want_pubnames (void)
11140 if (debug_info_level <= DINFO_LEVEL_TERSE)
11141 return false;
11142 if (debug_generate_pub_sections != -1)
11143 return debug_generate_pub_sections;
11144 return targetm.want_debug_pub_sections;
11147 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11149 static void
11150 add_AT_pubnames (dw_die_ref die)
11152 if (want_pubnames ())
11153 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11156 /* Add a string attribute value to a skeleton DIE. */
11158 static inline void
11159 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11160 const char *str)
11162 dw_attr_node attr;
11163 struct indirect_string_node *node;
11165 if (! skeleton_debug_str_hash)
11166 skeleton_debug_str_hash
11167 = hash_table<indirect_string_hasher>::create_ggc (10);
11169 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11170 find_string_form (node);
11171 if (node->form == dwarf_FORM (DW_FORM_strx))
11172 node->form = DW_FORM_strp;
11174 attr.dw_attr = attr_kind;
11175 attr.dw_attr_val.val_class = dw_val_class_str;
11176 attr.dw_attr_val.val_entry = NULL;
11177 attr.dw_attr_val.v.val_str = node;
11178 add_dwarf_attr (die, &attr);
11181 /* Helper function to generate top-level dies for skeleton debug_info and
11182 debug_types. */
11184 static void
11185 add_top_level_skeleton_die_attrs (dw_die_ref die)
11187 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11188 const char *comp_dir = comp_dir_string ();
11190 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11191 if (comp_dir != NULL)
11192 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11193 add_AT_pubnames (die);
11194 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11197 /* Output skeleton debug sections that point to the dwo file. */
11199 static void
11200 output_skeleton_debug_sections (dw_die_ref comp_unit,
11201 const unsigned char *dwo_id)
11203 /* These attributes will be found in the full debug_info section. */
11204 remove_AT (comp_unit, DW_AT_producer);
11205 remove_AT (comp_unit, DW_AT_language);
11207 switch_to_section (debug_skeleton_info_section);
11208 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11210 /* Produce the skeleton compilation-unit header. This one differs enough from
11211 a normal CU header that it's better not to call output_compilation_unit
11212 header. */
11213 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11214 dw2_asm_output_data (4, 0xffffffff,
11215 "Initial length escape value indicating 64-bit "
11216 "DWARF extension");
11218 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11219 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11220 - DWARF_INITIAL_LENGTH_SIZE
11221 + size_of_die (comp_unit),
11222 "Length of Compilation Unit Info");
11223 output_dwarf_version ();
11224 if (dwarf_version >= 5)
11226 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11227 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11229 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
11230 debug_skeleton_abbrev_section,
11231 "Offset Into Abbrev. Section");
11232 if (dwarf_version < 5)
11233 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11234 else
11235 for (int i = 0; i < 8; i++)
11236 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11238 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11239 output_die (comp_unit);
11241 /* Build the skeleton debug_abbrev section. */
11242 switch_to_section (debug_skeleton_abbrev_section);
11243 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11245 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11247 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11250 /* Output a comdat type unit DIE and its children. */
11252 static void
11253 output_comdat_type_unit (comdat_type_node *node)
11255 const char *secname;
11256 char *tmp;
11257 int i;
11258 #if defined (OBJECT_FORMAT_ELF)
11259 tree comdat_key;
11260 #endif
11262 /* First mark all the DIEs in this CU so we know which get local refs. */
11263 mark_dies (node->root_die);
11265 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11267 build_abbrev_table (node->root_die, extern_map);
11269 delete extern_map;
11270 extern_map = NULL;
11272 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11273 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11274 calc_die_sizes (node->root_die);
11276 #if defined (OBJECT_FORMAT_ELF)
11277 if (dwarf_version >= 5)
11279 if (!dwarf_split_debug_info)
11280 secname = ".debug_info";
11281 else
11282 secname = ".debug_info.dwo";
11284 else if (!dwarf_split_debug_info)
11285 secname = ".debug_types";
11286 else
11287 secname = ".debug_types.dwo";
11289 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11290 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11291 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11292 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11293 comdat_key = get_identifier (tmp);
11294 targetm.asm_out.named_section (secname,
11295 SECTION_DEBUG | SECTION_LINKONCE,
11296 comdat_key);
11297 #else
11298 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11299 sprintf (tmp, (dwarf_version >= 5
11300 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11301 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11302 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11303 secname = tmp;
11304 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11305 #endif
11307 /* Output debugging information. */
11308 output_compilation_unit_header (dwarf_split_debug_info
11309 ? DW_UT_split_type : DW_UT_type);
11310 output_signature (node->signature, "Type Signature");
11311 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11312 "Offset to Type DIE");
11313 output_die (node->root_die);
11315 unmark_dies (node->root_die);
11318 /* Return the DWARF2/3 pubname associated with a decl. */
11320 static const char *
11321 dwarf2_name (tree decl, int scope)
11323 if (DECL_NAMELESS (decl))
11324 return NULL;
11325 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11328 /* Add a new entry to .debug_pubnames if appropriate. */
11330 static void
11331 add_pubname_string (const char *str, dw_die_ref die)
11333 pubname_entry e;
11335 e.die = die;
11336 e.name = xstrdup (str);
11337 vec_safe_push (pubname_table, e);
11340 static void
11341 add_pubname (tree decl, dw_die_ref die)
11343 if (!want_pubnames ())
11344 return;
11346 /* Don't add items to the table when we expect that the consumer will have
11347 just read the enclosing die. For example, if the consumer is looking at a
11348 class_member, it will either be inside the class already, or will have just
11349 looked up the class to find the member. Either way, searching the class is
11350 faster than searching the index. */
11351 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11352 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11354 const char *name = dwarf2_name (decl, 1);
11356 if (name)
11357 add_pubname_string (name, die);
11361 /* Add an enumerator to the pubnames section. */
11363 static void
11364 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11366 pubname_entry e;
11368 gcc_assert (scope_name);
11369 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11370 e.die = die;
11371 vec_safe_push (pubname_table, e);
11374 /* Add a new entry to .debug_pubtypes if appropriate. */
11376 static void
11377 add_pubtype (tree decl, dw_die_ref die)
11379 pubname_entry e;
11381 if (!want_pubnames ())
11382 return;
11384 if ((TREE_PUBLIC (decl)
11385 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11386 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11388 tree scope = NULL;
11389 const char *scope_name = "";
11390 const char *sep = is_cxx () ? "::" : ".";
11391 const char *name;
11393 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11394 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11396 scope_name = lang_hooks.dwarf_name (scope, 1);
11397 if (scope_name != NULL && scope_name[0] != '\0')
11398 scope_name = concat (scope_name, sep, NULL);
11399 else
11400 scope_name = "";
11403 if (TYPE_P (decl))
11404 name = type_tag (decl);
11405 else
11406 name = lang_hooks.dwarf_name (decl, 1);
11408 /* If we don't have a name for the type, there's no point in adding
11409 it to the table. */
11410 if (name != NULL && name[0] != '\0')
11412 e.die = die;
11413 e.name = concat (scope_name, name, NULL);
11414 vec_safe_push (pubtype_table, e);
11417 /* Although it might be more consistent to add the pubinfo for the
11418 enumerators as their dies are created, they should only be added if the
11419 enum type meets the criteria above. So rather than re-check the parent
11420 enum type whenever an enumerator die is created, just output them all
11421 here. This isn't protected by the name conditional because anonymous
11422 enums don't have names. */
11423 if (die->die_tag == DW_TAG_enumeration_type)
11425 dw_die_ref c;
11427 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11432 /* Output a single entry in the pubnames table. */
11434 static void
11435 output_pubname (dw_offset die_offset, pubname_entry *entry)
11437 dw_die_ref die = entry->die;
11438 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11440 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
11442 if (debug_generate_pub_sections == 2)
11444 /* This logic follows gdb's method for determining the value of the flag
11445 byte. */
11446 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11447 switch (die->die_tag)
11449 case DW_TAG_typedef:
11450 case DW_TAG_base_type:
11451 case DW_TAG_subrange_type:
11452 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11453 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11454 break;
11455 case DW_TAG_enumerator:
11456 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11457 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11458 if (!is_cxx ())
11459 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11460 break;
11461 case DW_TAG_subprogram:
11462 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11463 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11464 if (!is_ada ())
11465 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11466 break;
11467 case DW_TAG_constant:
11468 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11469 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11470 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11471 break;
11472 case DW_TAG_variable:
11473 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11474 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11475 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11476 break;
11477 case DW_TAG_namespace:
11478 case DW_TAG_imported_declaration:
11479 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11480 break;
11481 case DW_TAG_class_type:
11482 case DW_TAG_interface_type:
11483 case DW_TAG_structure_type:
11484 case DW_TAG_union_type:
11485 case DW_TAG_enumeration_type:
11486 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11487 if (!is_cxx ())
11488 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11489 break;
11490 default:
11491 /* An unusual tag. Leave the flag-byte empty. */
11492 break;
11494 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11495 "GDB-index flags");
11498 dw2_asm_output_nstring (entry->name, -1, "external name");
11502 /* Output the public names table used to speed up access to externally
11503 visible names; or the public types table used to find type definitions. */
11505 static void
11506 output_pubnames (vec<pubname_entry, va_gc> *names)
11508 unsigned i;
11509 unsigned long pubnames_length = size_of_pubnames (names);
11510 pubname_entry *pub;
11512 if (!XCOFF_DEBUGGING_INFO)
11514 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11515 dw2_asm_output_data (4, 0xffffffff,
11516 "Initial length escape value indicating 64-bit DWARF extension");
11517 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11518 "Pub Info Length");
11521 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11522 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11524 if (dwarf_split_debug_info)
11525 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11526 debug_skeleton_info_section,
11527 "Offset of Compilation Unit Info");
11528 else
11529 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11530 debug_info_section,
11531 "Offset of Compilation Unit Info");
11532 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11533 "Compilation Unit Length");
11535 FOR_EACH_VEC_ELT (*names, i, pub)
11537 if (include_pubname_in_output (names, pub))
11539 dw_offset die_offset = pub->die->die_offset;
11541 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11542 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11543 gcc_assert (pub->die->die_mark);
11545 /* If we're putting types in their own .debug_types sections,
11546 the .debug_pubtypes table will still point to the compile
11547 unit (not the type unit), so we want to use the offset of
11548 the skeleton DIE (if there is one). */
11549 if (pub->die->comdat_type_p && names == pubtype_table)
11551 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11553 if (type_node != NULL)
11554 die_offset = (type_node->skeleton_die != NULL
11555 ? type_node->skeleton_die->die_offset
11556 : comp_unit_die ()->die_offset);
11559 output_pubname (die_offset, pub);
11563 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11566 /* Output public names and types tables if necessary. */
11568 static void
11569 output_pubtables (void)
11571 if (!want_pubnames () || !info_section_emitted)
11572 return;
11574 switch_to_section (debug_pubnames_section);
11575 output_pubnames (pubname_table);
11576 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11577 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11578 simply won't look for the section. */
11579 switch_to_section (debug_pubtypes_section);
11580 output_pubnames (pubtype_table);
11584 /* Output the information that goes into the .debug_aranges table.
11585 Namely, define the beginning and ending address range of the
11586 text section generated for this compilation unit. */
11588 static void
11589 output_aranges (void)
11591 unsigned i;
11592 unsigned long aranges_length = size_of_aranges ();
11594 if (!XCOFF_DEBUGGING_INFO)
11596 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11597 dw2_asm_output_data (4, 0xffffffff,
11598 "Initial length escape value indicating 64-bit DWARF extension");
11599 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11600 "Length of Address Ranges Info");
11603 /* Version number for aranges is still 2, even up to DWARF5. */
11604 dw2_asm_output_data (2, 2, "DWARF aranges version");
11605 if (dwarf_split_debug_info)
11606 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11607 debug_skeleton_info_section,
11608 "Offset of Compilation Unit Info");
11609 else
11610 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11611 debug_info_section,
11612 "Offset of Compilation Unit Info");
11613 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11614 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11616 /* We need to align to twice the pointer size here. */
11617 if (DWARF_ARANGES_PAD_SIZE)
11619 /* Pad using a 2 byte words so that padding is correct for any
11620 pointer size. */
11621 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11622 2 * DWARF2_ADDR_SIZE);
11623 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11624 dw2_asm_output_data (2, 0, NULL);
11627 /* It is necessary not to output these entries if the sections were
11628 not used; if the sections were not used, the length will be 0 and
11629 the address may end up as 0 if the section is discarded by ld
11630 --gc-sections, leaving an invalid (0, 0) entry that can be
11631 confused with the terminator. */
11632 if (text_section_used)
11634 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11635 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11636 text_section_label, "Length");
11638 if (cold_text_section_used)
11640 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11641 "Address");
11642 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11643 cold_text_section_label, "Length");
11646 if (have_multiple_function_sections)
11648 unsigned fde_idx;
11649 dw_fde_ref fde;
11651 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11653 if (DECL_IGNORED_P (fde->decl))
11654 continue;
11655 if (!fde->in_std_section)
11657 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11658 "Address");
11659 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11660 fde->dw_fde_begin, "Length");
11662 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11664 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11665 "Address");
11666 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11667 fde->dw_fde_second_begin, "Length");
11672 /* Output the terminator words. */
11673 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11674 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11677 /* Add a new entry to .debug_ranges. Return its index into
11678 ranges_table vector. */
11680 static unsigned int
11681 add_ranges_num (int num, bool maybe_new_sec)
11683 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11684 vec_safe_push (ranges_table, r);
11685 return vec_safe_length (ranges_table) - 1;
11688 /* Add a new entry to .debug_ranges corresponding to a block, or a
11689 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11690 this entry might be in a different section from previous range. */
11692 static unsigned int
11693 add_ranges (const_tree block, bool maybe_new_sec)
11695 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11698 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11699 chain, or middle entry of a chain that will be directly referred to. */
11701 static void
11702 note_rnglist_head (unsigned int offset)
11704 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11705 return;
11706 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11709 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11710 When using dwarf_split_debug_info, address attributes in dies destined
11711 for the final executable should be direct references--setting the
11712 parameter force_direct ensures this behavior. */
11714 static void
11715 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11716 bool *added, bool force_direct)
11718 unsigned int in_use = vec_safe_length (ranges_by_label);
11719 unsigned int offset;
11720 dw_ranges_by_label rbl = { begin, end };
11721 vec_safe_push (ranges_by_label, rbl);
11722 offset = add_ranges_num (-(int)in_use - 1, true);
11723 if (!*added)
11725 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11726 *added = true;
11727 note_rnglist_head (offset);
11731 /* Emit .debug_ranges section. */
11733 static void
11734 output_ranges (void)
11736 unsigned i;
11737 static const char *const start_fmt = "Offset %#x";
11738 const char *fmt = start_fmt;
11739 dw_ranges *r;
11741 switch_to_section (debug_ranges_section);
11742 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11743 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11745 int block_num = r->num;
11747 if (block_num > 0)
11749 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11750 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11752 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11753 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11755 /* If all code is in the text section, then the compilation
11756 unit base address defaults to DW_AT_low_pc, which is the
11757 base of the text section. */
11758 if (!have_multiple_function_sections)
11760 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11761 text_section_label,
11762 fmt, i * 2 * DWARF2_ADDR_SIZE);
11763 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11764 text_section_label, NULL);
11767 /* Otherwise, the compilation unit base address is zero,
11768 which allows us to use absolute addresses, and not worry
11769 about whether the target supports cross-section
11770 arithmetic. */
11771 else
11773 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11774 fmt, i * 2 * DWARF2_ADDR_SIZE);
11775 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11778 fmt = NULL;
11781 /* Negative block_num stands for an index into ranges_by_label. */
11782 else if (block_num < 0)
11784 int lab_idx = - block_num - 1;
11786 if (!have_multiple_function_sections)
11788 gcc_unreachable ();
11789 #if 0
11790 /* If we ever use add_ranges_by_labels () for a single
11791 function section, all we have to do is to take out
11792 the #if 0 above. */
11793 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11794 (*ranges_by_label)[lab_idx].begin,
11795 text_section_label,
11796 fmt, i * 2 * DWARF2_ADDR_SIZE);
11797 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11798 (*ranges_by_label)[lab_idx].end,
11799 text_section_label, NULL);
11800 #endif
11802 else
11804 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11805 (*ranges_by_label)[lab_idx].begin,
11806 fmt, i * 2 * DWARF2_ADDR_SIZE);
11807 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11808 (*ranges_by_label)[lab_idx].end,
11809 NULL);
11812 else
11814 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11815 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11816 fmt = start_fmt;
11821 /* Non-zero if .debug_line_str should be used for .debug_line section
11822 strings or strings that are likely shareable with those. */
11823 #define DWARF5_USE_DEBUG_LINE_STR \
11824 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11825 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11826 /* FIXME: there is no .debug_line_str.dwo section, \
11827 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11828 && !dwarf_split_debug_info)
11830 /* Assign .debug_rnglists indexes. */
11832 static void
11833 index_rnglists (void)
11835 unsigned i;
11836 dw_ranges *r;
11838 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11839 if (r->label)
11840 r->idx = rnglist_idx++;
11843 /* Emit .debug_rnglists section. */
11845 static void
11846 output_rnglists (unsigned generation)
11848 unsigned i;
11849 dw_ranges *r;
11850 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11851 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11852 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11854 switch_to_section (debug_ranges_section);
11855 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11856 /* There are up to 4 unique ranges labels per generation.
11857 See also init_sections_and_labels. */
11858 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11859 2 + generation * 4);
11860 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11861 3 + generation * 4);
11862 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11863 dw2_asm_output_data (4, 0xffffffff,
11864 "Initial length escape value indicating "
11865 "64-bit DWARF extension");
11866 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11867 "Length of Range Lists");
11868 ASM_OUTPUT_LABEL (asm_out_file, l1);
11869 output_dwarf_version ();
11870 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11871 dw2_asm_output_data (1, 0, "Segment Size");
11872 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11873 about relocation sizes and primarily care about the size of .debug*
11874 sections in linked shared libraries and executables, then
11875 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11876 into it are usually larger than just DW_FORM_sec_offset offsets
11877 into the .debug_rnglists section. */
11878 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11879 "Offset Entry Count");
11880 if (dwarf_split_debug_info)
11882 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11883 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11884 if (r->label)
11885 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11886 ranges_base_label, NULL);
11889 const char *lab = "";
11890 unsigned int len = vec_safe_length (ranges_table);
11891 const char *base = NULL;
11892 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11894 int block_num = r->num;
11896 if (r->label)
11898 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11899 lab = r->label;
11901 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11902 base = NULL;
11903 if (block_num > 0)
11905 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11906 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11908 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11909 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11911 if (HAVE_AS_LEB128)
11913 /* If all code is in the text section, then the compilation
11914 unit base address defaults to DW_AT_low_pc, which is the
11915 base of the text section. */
11916 if (!have_multiple_function_sections)
11918 dw2_asm_output_data (1, DW_RLE_offset_pair,
11919 "DW_RLE_offset_pair (%s)", lab);
11920 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11921 "Range begin address (%s)", lab);
11922 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11923 "Range end address (%s)", lab);
11924 continue;
11926 if (base == NULL)
11928 dw_ranges *r2 = NULL;
11929 if (i < len - 1)
11930 r2 = &(*ranges_table)[i + 1];
11931 if (r2
11932 && r2->num != 0
11933 && r2->label == NULL
11934 && !r2->maybe_new_sec)
11936 dw2_asm_output_data (1, DW_RLE_base_address,
11937 "DW_RLE_base_address (%s)", lab);
11938 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11939 "Base address (%s)", lab);
11940 strcpy (basebuf, blabel);
11941 base = basebuf;
11944 if (base)
11946 dw2_asm_output_data (1, DW_RLE_offset_pair,
11947 "DW_RLE_offset_pair (%s)", lab);
11948 dw2_asm_output_delta_uleb128 (blabel, base,
11949 "Range begin address (%s)", lab);
11950 dw2_asm_output_delta_uleb128 (elabel, base,
11951 "Range end address (%s)", lab);
11952 continue;
11954 dw2_asm_output_data (1, DW_RLE_start_length,
11955 "DW_RLE_start_length (%s)", lab);
11956 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11957 "Range begin address (%s)", lab);
11958 dw2_asm_output_delta_uleb128 (elabel, blabel,
11959 "Range length (%s)", lab);
11961 else
11963 dw2_asm_output_data (1, DW_RLE_start_end,
11964 "DW_RLE_start_end (%s)", lab);
11965 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11966 "Range begin address (%s)", lab);
11967 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11968 "Range end address (%s)", lab);
11972 /* Negative block_num stands for an index into ranges_by_label. */
11973 else if (block_num < 0)
11975 int lab_idx = - block_num - 1;
11976 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11977 const char *elabel = (*ranges_by_label)[lab_idx].end;
11979 if (!have_multiple_function_sections)
11980 gcc_unreachable ();
11981 if (HAVE_AS_LEB128)
11983 dw2_asm_output_data (1, DW_RLE_start_length,
11984 "DW_RLE_start_length (%s)", lab);
11985 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11986 "Range begin address (%s)", lab);
11987 dw2_asm_output_delta_uleb128 (elabel, blabel,
11988 "Range length (%s)", lab);
11990 else
11992 dw2_asm_output_data (1, DW_RLE_start_end,
11993 "DW_RLE_start_end (%s)", lab);
11994 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11995 "Range begin address (%s)", lab);
11996 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11997 "Range end address (%s)", lab);
12000 else
12001 dw2_asm_output_data (1, DW_RLE_end_of_list,
12002 "DW_RLE_end_of_list (%s)", lab);
12004 ASM_OUTPUT_LABEL (asm_out_file, l2);
12007 /* Data structure containing information about input files. */
12008 struct file_info
12010 const char *path; /* Complete file name. */
12011 const char *fname; /* File name part. */
12012 int length; /* Length of entire string. */
12013 struct dwarf_file_data * file_idx; /* Index in input file table. */
12014 int dir_idx; /* Index in directory table. */
12017 /* Data structure containing information about directories with source
12018 files. */
12019 struct dir_info
12021 const char *path; /* Path including directory name. */
12022 int length; /* Path length. */
12023 int prefix; /* Index of directory entry which is a prefix. */
12024 int count; /* Number of files in this directory. */
12025 int dir_idx; /* Index of directory used as base. */
12028 /* Callback function for file_info comparison. We sort by looking at
12029 the directories in the path. */
12031 static int
12032 file_info_cmp (const void *p1, const void *p2)
12034 const struct file_info *const s1 = (const struct file_info *) p1;
12035 const struct file_info *const s2 = (const struct file_info *) p2;
12036 const unsigned char *cp1;
12037 const unsigned char *cp2;
12039 /* Take care of file names without directories. We need to make sure that
12040 we return consistent values to qsort since some will get confused if
12041 we return the same value when identical operands are passed in opposite
12042 orders. So if neither has a directory, return 0 and otherwise return
12043 1 or -1 depending on which one has the directory. We want the one with
12044 the directory to sort after the one without, so all no directory files
12045 are at the start (normally only the compilation unit file). */
12046 if ((s1->path == s1->fname || s2->path == s2->fname))
12047 return (s2->path == s2->fname) - (s1->path == s1->fname);
12049 cp1 = (const unsigned char *) s1->path;
12050 cp2 = (const unsigned char *) s2->path;
12052 while (1)
12054 ++cp1;
12055 ++cp2;
12056 /* Reached the end of the first path? If so, handle like above,
12057 but now we want longer directory prefixes before shorter ones. */
12058 if ((cp1 == (const unsigned char *) s1->fname)
12059 || (cp2 == (const unsigned char *) s2->fname))
12060 return ((cp1 == (const unsigned char *) s1->fname)
12061 - (cp2 == (const unsigned char *) s2->fname));
12063 /* Character of current path component the same? */
12064 else if (*cp1 != *cp2)
12065 return *cp1 - *cp2;
12069 struct file_name_acquire_data
12071 struct file_info *files;
12072 int used_files;
12073 int max_files;
12076 /* Traversal function for the hash table. */
12079 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12081 struct dwarf_file_data *d = *slot;
12082 struct file_info *fi;
12083 const char *f;
12085 gcc_assert (fnad->max_files >= d->emitted_number);
12087 if (! d->emitted_number)
12088 return 1;
12090 gcc_assert (fnad->max_files != fnad->used_files);
12092 fi = fnad->files + fnad->used_files++;
12094 /* Skip all leading "./". */
12095 f = d->filename;
12096 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12097 f += 2;
12099 /* Create a new array entry. */
12100 fi->path = f;
12101 fi->length = strlen (f);
12102 fi->file_idx = d;
12104 /* Search for the file name part. */
12105 f = strrchr (f, DIR_SEPARATOR);
12106 #if defined (DIR_SEPARATOR_2)
12108 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12110 if (g != NULL)
12112 if (f == NULL || f < g)
12113 f = g;
12116 #endif
12118 fi->fname = f == NULL ? fi->path : f + 1;
12119 return 1;
12122 /* Helper function for output_file_names. Emit a FORM encoded
12123 string STR, with assembly comment start ENTRY_KIND and
12124 index IDX */
12126 static void
12127 output_line_string (enum dwarf_form form, const char *str,
12128 const char *entry_kind, unsigned int idx)
12130 switch (form)
12132 case DW_FORM_string:
12133 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12134 break;
12135 case DW_FORM_line_strp:
12136 if (!debug_line_str_hash)
12137 debug_line_str_hash
12138 = hash_table<indirect_string_hasher>::create_ggc (10);
12140 struct indirect_string_node *node;
12141 node = find_AT_string_in_table (str, debug_line_str_hash);
12142 set_indirect_string (node);
12143 node->form = form;
12144 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
12145 debug_line_str_section, "%s: %#x: \"%s\"",
12146 entry_kind, 0, node->str);
12147 break;
12148 default:
12149 gcc_unreachable ();
12153 /* Output the directory table and the file name table. We try to minimize
12154 the total amount of memory needed. A heuristic is used to avoid large
12155 slowdowns with many input files. */
12157 static void
12158 output_file_names (void)
12160 struct file_name_acquire_data fnad;
12161 int numfiles;
12162 struct file_info *files;
12163 struct dir_info *dirs;
12164 int *saved;
12165 int *savehere;
12166 int *backmap;
12167 int ndirs;
12168 int idx_offset;
12169 int i;
12171 if (!last_emitted_file)
12173 if (dwarf_version >= 5)
12175 dw2_asm_output_data (1, 0, "Directory entry format count");
12176 dw2_asm_output_data_uleb128 (0, "Directories count");
12177 dw2_asm_output_data (1, 0, "File name entry format count");
12178 dw2_asm_output_data_uleb128 (0, "File names count");
12180 else
12182 dw2_asm_output_data (1, 0, "End directory table");
12183 dw2_asm_output_data (1, 0, "End file name table");
12185 return;
12188 numfiles = last_emitted_file->emitted_number;
12190 /* Allocate the various arrays we need. */
12191 files = XALLOCAVEC (struct file_info, numfiles);
12192 dirs = XALLOCAVEC (struct dir_info, numfiles);
12194 fnad.files = files;
12195 fnad.used_files = 0;
12196 fnad.max_files = numfiles;
12197 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12198 gcc_assert (fnad.used_files == fnad.max_files);
12200 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12202 /* Find all the different directories used. */
12203 dirs[0].path = files[0].path;
12204 dirs[0].length = files[0].fname - files[0].path;
12205 dirs[0].prefix = -1;
12206 dirs[0].count = 1;
12207 dirs[0].dir_idx = 0;
12208 files[0].dir_idx = 0;
12209 ndirs = 1;
12211 for (i = 1; i < numfiles; i++)
12212 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12213 && memcmp (dirs[ndirs - 1].path, files[i].path,
12214 dirs[ndirs - 1].length) == 0)
12216 /* Same directory as last entry. */
12217 files[i].dir_idx = ndirs - 1;
12218 ++dirs[ndirs - 1].count;
12220 else
12222 int j;
12224 /* This is a new directory. */
12225 dirs[ndirs].path = files[i].path;
12226 dirs[ndirs].length = files[i].fname - files[i].path;
12227 dirs[ndirs].count = 1;
12228 dirs[ndirs].dir_idx = ndirs;
12229 files[i].dir_idx = ndirs;
12231 /* Search for a prefix. */
12232 dirs[ndirs].prefix = -1;
12233 for (j = 0; j < ndirs; j++)
12234 if (dirs[j].length < dirs[ndirs].length
12235 && dirs[j].length > 1
12236 && (dirs[ndirs].prefix == -1
12237 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12238 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12239 dirs[ndirs].prefix = j;
12241 ++ndirs;
12244 /* Now to the actual work. We have to find a subset of the directories which
12245 allow expressing the file name using references to the directory table
12246 with the least amount of characters. We do not do an exhaustive search
12247 where we would have to check out every combination of every single
12248 possible prefix. Instead we use a heuristic which provides nearly optimal
12249 results in most cases and never is much off. */
12250 saved = XALLOCAVEC (int, ndirs);
12251 savehere = XALLOCAVEC (int, ndirs);
12253 memset (saved, '\0', ndirs * sizeof (saved[0]));
12254 for (i = 0; i < ndirs; i++)
12256 int j;
12257 int total;
12259 /* We can always save some space for the current directory. But this
12260 does not mean it will be enough to justify adding the directory. */
12261 savehere[i] = dirs[i].length;
12262 total = (savehere[i] - saved[i]) * dirs[i].count;
12264 for (j = i + 1; j < ndirs; j++)
12266 savehere[j] = 0;
12267 if (saved[j] < dirs[i].length)
12269 /* Determine whether the dirs[i] path is a prefix of the
12270 dirs[j] path. */
12271 int k;
12273 k = dirs[j].prefix;
12274 while (k != -1 && k != (int) i)
12275 k = dirs[k].prefix;
12277 if (k == (int) i)
12279 /* Yes it is. We can possibly save some memory by
12280 writing the filenames in dirs[j] relative to
12281 dirs[i]. */
12282 savehere[j] = dirs[i].length;
12283 total += (savehere[j] - saved[j]) * dirs[j].count;
12288 /* Check whether we can save enough to justify adding the dirs[i]
12289 directory. */
12290 if (total > dirs[i].length + 1)
12292 /* It's worthwhile adding. */
12293 for (j = i; j < ndirs; j++)
12294 if (savehere[j] > 0)
12296 /* Remember how much we saved for this directory so far. */
12297 saved[j] = savehere[j];
12299 /* Remember the prefix directory. */
12300 dirs[j].dir_idx = i;
12305 /* Emit the directory name table. */
12306 idx_offset = dirs[0].length > 0 ? 1 : 0;
12307 enum dwarf_form str_form = DW_FORM_string;
12308 enum dwarf_form idx_form = DW_FORM_udata;
12309 if (dwarf_version >= 5)
12311 const char *comp_dir = comp_dir_string ();
12312 if (comp_dir == NULL)
12313 comp_dir = "";
12314 dw2_asm_output_data (1, 1, "Directory entry format count");
12315 if (DWARF5_USE_DEBUG_LINE_STR)
12316 str_form = DW_FORM_line_strp;
12317 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12318 dw2_asm_output_data_uleb128 (str_form, "%s",
12319 get_DW_FORM_name (str_form));
12320 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12321 if (str_form == DW_FORM_string)
12323 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12324 for (i = 1 - idx_offset; i < ndirs; i++)
12325 dw2_asm_output_nstring (dirs[i].path,
12326 dirs[i].length
12327 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12328 "Directory Entry: %#x", i + idx_offset);
12330 else
12332 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12333 for (i = 1 - idx_offset; i < ndirs; i++)
12335 const char *str
12336 = ggc_alloc_string (dirs[i].path,
12337 dirs[i].length
12338 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12339 output_line_string (str_form, str, "Directory Entry",
12340 (unsigned) i + idx_offset);
12344 else
12346 for (i = 1 - idx_offset; i < ndirs; i++)
12347 dw2_asm_output_nstring (dirs[i].path,
12348 dirs[i].length
12349 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12350 "Directory Entry: %#x", i + idx_offset);
12352 dw2_asm_output_data (1, 0, "End directory table");
12355 /* We have to emit them in the order of emitted_number since that's
12356 used in the debug info generation. To do this efficiently we
12357 generate a back-mapping of the indices first. */
12358 backmap = XALLOCAVEC (int, numfiles);
12359 for (i = 0; i < numfiles; i++)
12360 backmap[files[i].file_idx->emitted_number - 1] = i;
12362 if (dwarf_version >= 5)
12364 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12365 if (filename0 == NULL)
12366 filename0 = "";
12367 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12368 DW_FORM_data2. Choose one based on the number of directories
12369 and how much space would they occupy in each encoding.
12370 If we have at most 256 directories, all indexes fit into
12371 a single byte, so DW_FORM_data1 is most compact (if there
12372 are at most 128 directories, DW_FORM_udata would be as
12373 compact as that, but not shorter and slower to decode). */
12374 if (ndirs + idx_offset <= 256)
12375 idx_form = DW_FORM_data1;
12376 /* If there are more than 65536 directories, we have to use
12377 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12378 Otherwise, compute what space would occupy if all the indexes
12379 used DW_FORM_udata - sum - and compare that to how large would
12380 be DW_FORM_data2 encoding, and pick the more efficient one. */
12381 else if (ndirs + idx_offset <= 65536)
12383 unsigned HOST_WIDE_INT sum = 1;
12384 for (i = 0; i < numfiles; i++)
12386 int file_idx = backmap[i];
12387 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12388 sum += size_of_uleb128 (dir_idx);
12390 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12391 idx_form = DW_FORM_data2;
12393 #ifdef VMS_DEBUGGING_INFO
12394 dw2_asm_output_data (1, 4, "File name entry format count");
12395 #else
12396 dw2_asm_output_data (1, 2, "File name entry format count");
12397 #endif
12398 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12399 dw2_asm_output_data_uleb128 (str_form, "%s",
12400 get_DW_FORM_name (str_form));
12401 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12402 "DW_LNCT_directory_index");
12403 dw2_asm_output_data_uleb128 (idx_form, "%s",
12404 get_DW_FORM_name (idx_form));
12405 #ifdef VMS_DEBUGGING_INFO
12406 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12407 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12408 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12409 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12410 #endif
12411 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12413 output_line_string (str_form, filename0, "File Entry", 0);
12415 /* Include directory index. */
12416 if (idx_form != DW_FORM_udata)
12417 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12418 0, NULL);
12419 else
12420 dw2_asm_output_data_uleb128 (0, NULL);
12422 #ifdef VMS_DEBUGGING_INFO
12423 dw2_asm_output_data_uleb128 (0, NULL);
12424 dw2_asm_output_data_uleb128 (0, NULL);
12425 #endif
12428 /* Now write all the file names. */
12429 for (i = 0; i < numfiles; i++)
12431 int file_idx = backmap[i];
12432 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12434 #ifdef VMS_DEBUGGING_INFO
12435 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12437 /* Setting these fields can lead to debugger miscomparisons,
12438 but VMS Debug requires them to be set correctly. */
12440 int ver;
12441 long long cdt;
12442 long siz;
12443 int maxfilelen = (strlen (files[file_idx].path)
12444 + dirs[dir_idx].length
12445 + MAX_VMS_VERSION_LEN + 1);
12446 char *filebuf = XALLOCAVEC (char, maxfilelen);
12448 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12449 snprintf (filebuf, maxfilelen, "%s;%d",
12450 files[file_idx].path + dirs[dir_idx].length, ver);
12452 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12454 /* Include directory index. */
12455 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12456 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12457 dir_idx + idx_offset, NULL);
12458 else
12459 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12461 /* Modification time. */
12462 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12463 &cdt, 0, 0, 0) == 0)
12464 ? cdt : 0, NULL);
12466 /* File length in bytes. */
12467 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12468 0, &siz, 0, 0) == 0)
12469 ? siz : 0, NULL);
12470 #else
12471 output_line_string (str_form,
12472 files[file_idx].path + dirs[dir_idx].length,
12473 "File Entry", (unsigned) i + 1);
12475 /* Include directory index. */
12476 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12477 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12478 dir_idx + idx_offset, NULL);
12479 else
12480 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12482 if (dwarf_version >= 5)
12483 continue;
12485 /* Modification time. */
12486 dw2_asm_output_data_uleb128 (0, NULL);
12488 /* File length in bytes. */
12489 dw2_asm_output_data_uleb128 (0, NULL);
12490 #endif /* VMS_DEBUGGING_INFO */
12493 if (dwarf_version < 5)
12494 dw2_asm_output_data (1, 0, "End file name table");
12498 /* Output one line number table into the .debug_line section. */
12500 static void
12501 output_one_line_info_table (dw_line_info_table *table)
12503 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12504 unsigned int current_line = 1;
12505 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12506 dw_line_info_entry *ent, *prev_addr;
12507 size_t i;
12508 unsigned int view;
12510 view = 0;
12512 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12514 switch (ent->opcode)
12516 case LI_set_address:
12517 /* ??? Unfortunately, we have little choice here currently, and
12518 must always use the most general form. GCC does not know the
12519 address delta itself, so we can't use DW_LNS_advance_pc. Many
12520 ports do have length attributes which will give an upper bound
12521 on the address range. We could perhaps use length attributes
12522 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12523 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12525 view = 0;
12527 /* This can handle any delta. This takes
12528 4+DWARF2_ADDR_SIZE bytes. */
12529 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12530 debug_variable_location_views
12531 ? ", reset view to 0" : "");
12532 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12533 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12534 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12536 prev_addr = ent;
12537 break;
12539 case LI_adv_address:
12541 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12542 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12543 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12545 view++;
12547 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12548 dw2_asm_output_delta (2, line_label, prev_label,
12549 "from %s to %s", prev_label, line_label);
12551 prev_addr = ent;
12552 break;
12555 case LI_set_line:
12556 if (ent->val == current_line)
12558 /* We still need to start a new row, so output a copy insn. */
12559 dw2_asm_output_data (1, DW_LNS_copy,
12560 "copy line %u", current_line);
12562 else
12564 int line_offset = ent->val - current_line;
12565 int line_delta = line_offset - DWARF_LINE_BASE;
12567 current_line = ent->val;
12568 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12570 /* This can handle deltas from -10 to 234, using the current
12571 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12572 This takes 1 byte. */
12573 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12574 "line %u", current_line);
12576 else
12578 /* This can handle any delta. This takes at least 4 bytes,
12579 depending on the value being encoded. */
12580 dw2_asm_output_data (1, DW_LNS_advance_line,
12581 "advance to line %u", current_line);
12582 dw2_asm_output_data_sleb128 (line_offset, NULL);
12583 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12586 break;
12588 case LI_set_file:
12589 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12590 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12591 break;
12593 case LI_set_column:
12594 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12595 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12596 break;
12598 case LI_negate_stmt:
12599 current_is_stmt = !current_is_stmt;
12600 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12601 "is_stmt %d", current_is_stmt);
12602 break;
12604 case LI_set_prologue_end:
12605 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12606 "set prologue end");
12607 break;
12609 case LI_set_epilogue_begin:
12610 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12611 "set epilogue begin");
12612 break;
12614 case LI_set_discriminator:
12615 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12616 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12617 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12618 dw2_asm_output_data_uleb128 (ent->val, NULL);
12619 break;
12623 /* Emit debug info for the address of the end of the table. */
12624 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12625 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12626 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12627 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12629 dw2_asm_output_data (1, 0, "end sequence");
12630 dw2_asm_output_data_uleb128 (1, NULL);
12631 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12634 /* Output the source line number correspondence information. This
12635 information goes into the .debug_line section. */
12637 static void
12638 output_line_info (bool prologue_only)
12640 static unsigned int generation;
12641 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
12642 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
12643 bool saw_one = false;
12644 int opc;
12646 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
12647 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
12648 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
12649 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
12651 if (!XCOFF_DEBUGGING_INFO)
12653 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12654 dw2_asm_output_data (4, 0xffffffff,
12655 "Initial length escape value indicating 64-bit DWARF extension");
12656 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12657 "Length of Source Line Info");
12660 ASM_OUTPUT_LABEL (asm_out_file, l1);
12662 output_dwarf_version ();
12663 if (dwarf_version >= 5)
12665 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12666 dw2_asm_output_data (1, 0, "Segment Size");
12668 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12669 ASM_OUTPUT_LABEL (asm_out_file, p1);
12671 /* Define the architecture-dependent minimum instruction length (in bytes).
12672 In this implementation of DWARF, this field is used for information
12673 purposes only. Since GCC generates assembly language, we have no
12674 a priori knowledge of how many instruction bytes are generated for each
12675 source line, and therefore can use only the DW_LNE_set_address and
12676 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12677 this as '1', which is "correct enough" for all architectures,
12678 and don't let the target override. */
12679 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12681 if (dwarf_version >= 4)
12682 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12683 "Maximum Operations Per Instruction");
12684 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12685 "Default is_stmt_start flag");
12686 dw2_asm_output_data (1, DWARF_LINE_BASE,
12687 "Line Base Value (Special Opcodes)");
12688 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12689 "Line Range Value (Special Opcodes)");
12690 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12691 "Special Opcode Base");
12693 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12695 int n_op_args;
12696 switch (opc)
12698 case DW_LNS_advance_pc:
12699 case DW_LNS_advance_line:
12700 case DW_LNS_set_file:
12701 case DW_LNS_set_column:
12702 case DW_LNS_fixed_advance_pc:
12703 case DW_LNS_set_isa:
12704 n_op_args = 1;
12705 break;
12706 default:
12707 n_op_args = 0;
12708 break;
12711 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12712 opc, n_op_args);
12715 /* Write out the information about the files we use. */
12716 output_file_names ();
12717 ASM_OUTPUT_LABEL (asm_out_file, p2);
12718 if (prologue_only)
12720 /* Output the marker for the end of the line number info. */
12721 ASM_OUTPUT_LABEL (asm_out_file, l2);
12722 return;
12725 if (separate_line_info)
12727 dw_line_info_table *table;
12728 size_t i;
12730 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12731 if (table->in_use)
12733 output_one_line_info_table (table);
12734 saw_one = true;
12737 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12739 output_one_line_info_table (cold_text_section_line_info);
12740 saw_one = true;
12743 /* ??? Some Darwin linkers crash on a .debug_line section with no
12744 sequences. Further, merely a DW_LNE_end_sequence entry is not
12745 sufficient -- the address column must also be initialized.
12746 Make sure to output at least one set_address/end_sequence pair,
12747 choosing .text since that section is always present. */
12748 if (text_section_line_info->in_use || !saw_one)
12749 output_one_line_info_table (text_section_line_info);
12751 /* Output the marker for the end of the line number info. */
12752 ASM_OUTPUT_LABEL (asm_out_file, l2);
12755 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12757 static inline bool
12758 need_endianity_attribute_p (bool reverse)
12760 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12763 /* Given a pointer to a tree node for some base type, return a pointer to
12764 a DIE that describes the given type. REVERSE is true if the type is
12765 to be interpreted in the reverse storage order wrt the target order.
12767 This routine must only be called for GCC type nodes that correspond to
12768 Dwarf base (fundamental) types. */
12770 static dw_die_ref
12771 base_type_die (tree type, bool reverse)
12773 dw_die_ref base_type_result;
12774 enum dwarf_type encoding;
12775 bool fpt_used = false;
12776 struct fixed_point_type_info fpt_info;
12777 tree type_bias = NULL_TREE;
12779 /* If this is a subtype that should not be emitted as a subrange type,
12780 use the base type. See subrange_type_for_debug_p. */
12781 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12782 type = TREE_TYPE (type);
12784 switch (TREE_CODE (type))
12786 case INTEGER_TYPE:
12787 if ((dwarf_version >= 4 || !dwarf_strict)
12788 && TYPE_NAME (type)
12789 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12790 && DECL_IS_BUILTIN (TYPE_NAME (type))
12791 && DECL_NAME (TYPE_NAME (type)))
12793 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12794 if (strcmp (name, "char16_t") == 0
12795 || strcmp (name, "char32_t") == 0)
12797 encoding = DW_ATE_UTF;
12798 break;
12801 if ((dwarf_version >= 3 || !dwarf_strict)
12802 && lang_hooks.types.get_fixed_point_type_info)
12804 memset (&fpt_info, 0, sizeof (fpt_info));
12805 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12807 fpt_used = true;
12808 encoding = ((TYPE_UNSIGNED (type))
12809 ? DW_ATE_unsigned_fixed
12810 : DW_ATE_signed_fixed);
12811 break;
12814 if (TYPE_STRING_FLAG (type))
12816 if (TYPE_UNSIGNED (type))
12817 encoding = DW_ATE_unsigned_char;
12818 else
12819 encoding = DW_ATE_signed_char;
12821 else if (TYPE_UNSIGNED (type))
12822 encoding = DW_ATE_unsigned;
12823 else
12824 encoding = DW_ATE_signed;
12826 if (!dwarf_strict
12827 && lang_hooks.types.get_type_bias)
12828 type_bias = lang_hooks.types.get_type_bias (type);
12829 break;
12831 case REAL_TYPE:
12832 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12834 if (dwarf_version >= 3 || !dwarf_strict)
12835 encoding = DW_ATE_decimal_float;
12836 else
12837 encoding = DW_ATE_lo_user;
12839 else
12840 encoding = DW_ATE_float;
12841 break;
12843 case FIXED_POINT_TYPE:
12844 if (!(dwarf_version >= 3 || !dwarf_strict))
12845 encoding = DW_ATE_lo_user;
12846 else if (TYPE_UNSIGNED (type))
12847 encoding = DW_ATE_unsigned_fixed;
12848 else
12849 encoding = DW_ATE_signed_fixed;
12850 break;
12852 /* Dwarf2 doesn't know anything about complex ints, so use
12853 a user defined type for it. */
12854 case COMPLEX_TYPE:
12855 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12856 encoding = DW_ATE_complex_float;
12857 else
12858 encoding = DW_ATE_lo_user;
12859 break;
12861 case BOOLEAN_TYPE:
12862 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12863 encoding = DW_ATE_boolean;
12864 break;
12866 default:
12867 /* No other TREE_CODEs are Dwarf fundamental types. */
12868 gcc_unreachable ();
12871 base_type_result = new_die_raw (DW_TAG_base_type);
12873 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12874 int_size_in_bytes (type));
12875 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12877 if (need_endianity_attribute_p (reverse))
12878 add_AT_unsigned (base_type_result, DW_AT_endianity,
12879 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12881 add_alignment_attribute (base_type_result, type);
12883 if (fpt_used)
12885 switch (fpt_info.scale_factor_kind)
12887 case fixed_point_scale_factor_binary:
12888 add_AT_int (base_type_result, DW_AT_binary_scale,
12889 fpt_info.scale_factor.binary);
12890 break;
12892 case fixed_point_scale_factor_decimal:
12893 add_AT_int (base_type_result, DW_AT_decimal_scale,
12894 fpt_info.scale_factor.decimal);
12895 break;
12897 case fixed_point_scale_factor_arbitrary:
12898 /* Arbitrary scale factors cannot be described in standard DWARF,
12899 yet. */
12900 if (!dwarf_strict)
12902 /* Describe the scale factor as a rational constant. */
12903 const dw_die_ref scale_factor
12904 = new_die (DW_TAG_constant, comp_unit_die (), type);
12906 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12907 fpt_info.scale_factor.arbitrary.numerator);
12908 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12909 fpt_info.scale_factor.arbitrary.denominator);
12911 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12913 break;
12915 default:
12916 gcc_unreachable ();
12920 if (type_bias)
12921 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12922 dw_scalar_form_constant
12923 | dw_scalar_form_exprloc
12924 | dw_scalar_form_reference,
12925 NULL);
12927 return base_type_result;
12930 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12931 named 'auto' in its type: return true for it, false otherwise. */
12933 static inline bool
12934 is_cxx_auto (tree type)
12936 if (is_cxx ())
12938 tree name = TYPE_IDENTIFIER (type);
12939 if (name == get_identifier ("auto")
12940 || name == get_identifier ("decltype(auto)"))
12941 return true;
12943 return false;
12946 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12947 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12949 static inline int
12950 is_base_type (tree type)
12952 switch (TREE_CODE (type))
12954 case INTEGER_TYPE:
12955 case REAL_TYPE:
12956 case FIXED_POINT_TYPE:
12957 case COMPLEX_TYPE:
12958 case BOOLEAN_TYPE:
12959 return 1;
12961 case VOID_TYPE:
12962 case ARRAY_TYPE:
12963 case RECORD_TYPE:
12964 case UNION_TYPE:
12965 case QUAL_UNION_TYPE:
12966 case ENUMERAL_TYPE:
12967 case FUNCTION_TYPE:
12968 case METHOD_TYPE:
12969 case POINTER_TYPE:
12970 case REFERENCE_TYPE:
12971 case NULLPTR_TYPE:
12972 case OFFSET_TYPE:
12973 case LANG_TYPE:
12974 case VECTOR_TYPE:
12975 return 0;
12977 default:
12978 if (is_cxx_auto (type))
12979 return 0;
12980 gcc_unreachable ();
12983 return 0;
12986 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12987 node, return the size in bits for the type if it is a constant, or else
12988 return the alignment for the type if the type's size is not constant, or
12989 else return BITS_PER_WORD if the type actually turns out to be an
12990 ERROR_MARK node. */
12992 static inline unsigned HOST_WIDE_INT
12993 simple_type_size_in_bits (const_tree type)
12995 if (TREE_CODE (type) == ERROR_MARK)
12996 return BITS_PER_WORD;
12997 else if (TYPE_SIZE (type) == NULL_TREE)
12998 return 0;
12999 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13000 return tree_to_uhwi (TYPE_SIZE (type));
13001 else
13002 return TYPE_ALIGN (type);
13005 /* Similarly, but return an offset_int instead of UHWI. */
13007 static inline offset_int
13008 offset_int_type_size_in_bits (const_tree type)
13010 if (TREE_CODE (type) == ERROR_MARK)
13011 return BITS_PER_WORD;
13012 else if (TYPE_SIZE (type) == NULL_TREE)
13013 return 0;
13014 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13015 return wi::to_offset (TYPE_SIZE (type));
13016 else
13017 return TYPE_ALIGN (type);
13020 /* Given a pointer to a tree node for a subrange type, return a pointer
13021 to a DIE that describes the given type. */
13023 static dw_die_ref
13024 subrange_type_die (tree type, tree low, tree high, tree bias,
13025 dw_die_ref context_die)
13027 dw_die_ref subrange_die;
13028 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13030 if (context_die == NULL)
13031 context_die = comp_unit_die ();
13033 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13035 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13037 /* The size of the subrange type and its base type do not match,
13038 so we need to generate a size attribute for the subrange type. */
13039 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13042 add_alignment_attribute (subrange_die, type);
13044 if (low)
13045 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13046 if (high)
13047 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13048 if (bias && !dwarf_strict)
13049 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13050 dw_scalar_form_constant
13051 | dw_scalar_form_exprloc
13052 | dw_scalar_form_reference,
13053 NULL);
13055 return subrange_die;
13058 /* Returns the (const and/or volatile) cv_qualifiers associated with
13059 the decl node. This will normally be augmented with the
13060 cv_qualifiers of the underlying type in add_type_attribute. */
13062 static int
13063 decl_quals (const_tree decl)
13065 return ((TREE_READONLY (decl)
13066 /* The C++ front-end correctly marks reference-typed
13067 variables as readonly, but from a language (and debug
13068 info) standpoint they are not const-qualified. */
13069 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13070 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13071 | (TREE_THIS_VOLATILE (decl)
13072 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13075 /* Determine the TYPE whose qualifiers match the largest strict subset
13076 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13077 qualifiers outside QUAL_MASK. */
13079 static int
13080 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13082 tree t;
13083 int best_rank = 0, best_qual = 0, max_rank;
13085 type_quals &= qual_mask;
13086 max_rank = popcount_hwi (type_quals) - 1;
13088 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13089 t = TYPE_NEXT_VARIANT (t))
13091 int q = TYPE_QUALS (t) & qual_mask;
13093 if ((q & type_quals) == q && q != type_quals
13094 && check_base_type (t, type))
13096 int rank = popcount_hwi (q);
13098 if (rank > best_rank)
13100 best_rank = rank;
13101 best_qual = q;
13106 return best_qual;
13109 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13110 static const dwarf_qual_info_t dwarf_qual_info[] =
13112 { TYPE_QUAL_CONST, DW_TAG_const_type },
13113 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13114 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13115 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13117 static const unsigned int dwarf_qual_info_size
13118 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13120 /* If DIE is a qualified DIE of some base DIE with the same parent,
13121 return the base DIE, otherwise return NULL. Set MASK to the
13122 qualifiers added compared to the returned DIE. */
13124 static dw_die_ref
13125 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13127 unsigned int i;
13128 for (i = 0; i < dwarf_qual_info_size; i++)
13129 if (die->die_tag == dwarf_qual_info[i].t)
13130 break;
13131 if (i == dwarf_qual_info_size)
13132 return NULL;
13133 if (vec_safe_length (die->die_attr) != 1)
13134 return NULL;
13135 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13136 if (type == NULL || type->die_parent != die->die_parent)
13137 return NULL;
13138 *mask |= dwarf_qual_info[i].q;
13139 if (depth)
13141 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13142 if (ret)
13143 return ret;
13145 return type;
13148 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13149 entry that chains the modifiers specified by CV_QUALS in front of the
13150 given type. REVERSE is true if the type is to be interpreted in the
13151 reverse storage order wrt the target order. */
13153 static dw_die_ref
13154 modified_type_die (tree type, int cv_quals, bool reverse,
13155 dw_die_ref context_die)
13157 enum tree_code code = TREE_CODE (type);
13158 dw_die_ref mod_type_die;
13159 dw_die_ref sub_die = NULL;
13160 tree item_type = NULL;
13161 tree qualified_type;
13162 tree name, low, high;
13163 dw_die_ref mod_scope;
13164 /* Only these cv-qualifiers are currently handled. */
13165 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13166 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13167 ENCODE_QUAL_ADDR_SPACE(~0U));
13168 const bool reverse_base_type
13169 = need_endianity_attribute_p (reverse) && is_base_type (type);
13171 if (code == ERROR_MARK)
13172 return NULL;
13174 if (lang_hooks.types.get_debug_type)
13176 tree debug_type = lang_hooks.types.get_debug_type (type);
13178 if (debug_type != NULL_TREE && debug_type != type)
13179 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13182 cv_quals &= cv_qual_mask;
13184 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13185 tag modifier (and not an attribute) old consumers won't be able
13186 to handle it. */
13187 if (dwarf_version < 3)
13188 cv_quals &= ~TYPE_QUAL_RESTRICT;
13190 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13191 if (dwarf_version < 5)
13192 cv_quals &= ~TYPE_QUAL_ATOMIC;
13194 /* See if we already have the appropriately qualified variant of
13195 this type. */
13196 qualified_type = get_qualified_type (type, cv_quals);
13198 if (qualified_type == sizetype)
13200 /* Try not to expose the internal sizetype type's name. */
13201 if (TYPE_NAME (qualified_type)
13202 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13204 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13206 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13207 && (TYPE_PRECISION (t)
13208 == TYPE_PRECISION (qualified_type))
13209 && (TYPE_UNSIGNED (t)
13210 == TYPE_UNSIGNED (qualified_type)));
13211 qualified_type = t;
13213 else if (qualified_type == sizetype
13214 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13215 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13216 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13217 qualified_type = size_type_node;
13220 /* If we do, then we can just use its DIE, if it exists. */
13221 if (qualified_type)
13223 mod_type_die = lookup_type_die (qualified_type);
13225 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13226 dealt with specially: the DIE with the attribute, if it exists, is
13227 placed immediately after the regular DIE for the same base type. */
13228 if (mod_type_die
13229 && (!reverse_base_type
13230 || ((mod_type_die = mod_type_die->die_sib) != NULL
13231 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13232 return mod_type_die;
13235 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13237 /* Handle C typedef types. */
13238 if (name
13239 && TREE_CODE (name) == TYPE_DECL
13240 && DECL_ORIGINAL_TYPE (name)
13241 && !DECL_ARTIFICIAL (name))
13243 tree dtype = TREE_TYPE (name);
13245 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13246 if (qualified_type == dtype && !reverse_base_type)
13248 tree origin = decl_ultimate_origin (name);
13250 /* Typedef variants that have an abstract origin don't get their own
13251 type DIE (see gen_typedef_die), so fall back on the ultimate
13252 abstract origin instead. */
13253 if (origin != NULL && origin != name)
13254 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13255 context_die);
13257 /* For a named type, use the typedef. */
13258 gen_type_die (qualified_type, context_die);
13259 return lookup_type_die (qualified_type);
13261 else
13263 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13264 dquals &= cv_qual_mask;
13265 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13266 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13267 /* cv-unqualified version of named type. Just use
13268 the unnamed type to which it refers. */
13269 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13270 reverse, context_die);
13271 /* Else cv-qualified version of named type; fall through. */
13275 mod_scope = scope_die_for (type, context_die);
13277 if (cv_quals)
13279 int sub_quals = 0, first_quals = 0;
13280 unsigned i;
13281 dw_die_ref first = NULL, last = NULL;
13283 /* Determine a lesser qualified type that most closely matches
13284 this one. Then generate DW_TAG_* entries for the remaining
13285 qualifiers. */
13286 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13287 cv_qual_mask);
13288 if (sub_quals && use_debug_types)
13290 bool needed = false;
13291 /* If emitting type units, make sure the order of qualifiers
13292 is canonical. Thus, start from unqualified type if
13293 an earlier qualifier is missing in sub_quals, but some later
13294 one is present there. */
13295 for (i = 0; i < dwarf_qual_info_size; i++)
13296 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13297 needed = true;
13298 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13300 sub_quals = 0;
13301 break;
13304 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13305 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13307 /* As not all intermediate qualified DIEs have corresponding
13308 tree types, ensure that qualified DIEs in the same scope
13309 as their DW_AT_type are emitted after their DW_AT_type,
13310 only with other qualified DIEs for the same type possibly
13311 in between them. Determine the range of such qualified
13312 DIEs now (first being the base type, last being corresponding
13313 last qualified DIE for it). */
13314 unsigned int count = 0;
13315 first = qualified_die_p (mod_type_die, &first_quals,
13316 dwarf_qual_info_size);
13317 if (first == NULL)
13318 first = mod_type_die;
13319 gcc_assert ((first_quals & ~sub_quals) == 0);
13320 for (count = 0, last = first;
13321 count < (1U << dwarf_qual_info_size);
13322 count++, last = last->die_sib)
13324 int quals = 0;
13325 if (last == mod_scope->die_child)
13326 break;
13327 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13328 != first)
13329 break;
13333 for (i = 0; i < dwarf_qual_info_size; i++)
13334 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13336 dw_die_ref d;
13337 if (first && first != last)
13339 for (d = first->die_sib; ; d = d->die_sib)
13341 int quals = 0;
13342 qualified_die_p (d, &quals, dwarf_qual_info_size);
13343 if (quals == (first_quals | dwarf_qual_info[i].q))
13344 break;
13345 if (d == last)
13347 d = NULL;
13348 break;
13351 if (d)
13353 mod_type_die = d;
13354 continue;
13357 if (first)
13359 d = new_die_raw (dwarf_qual_info[i].t);
13360 add_child_die_after (mod_scope, d, last);
13361 last = d;
13363 else
13364 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13365 if (mod_type_die)
13366 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13367 mod_type_die = d;
13368 first_quals |= dwarf_qual_info[i].q;
13371 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13373 dwarf_tag tag = DW_TAG_pointer_type;
13374 if (code == REFERENCE_TYPE)
13376 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13377 tag = DW_TAG_rvalue_reference_type;
13378 else
13379 tag = DW_TAG_reference_type;
13381 mod_type_die = new_die (tag, mod_scope, type);
13383 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13384 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13385 add_alignment_attribute (mod_type_die, type);
13386 item_type = TREE_TYPE (type);
13388 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13389 if (!ADDR_SPACE_GENERIC_P (as))
13391 int action = targetm.addr_space.debug (as);
13392 if (action >= 0)
13394 /* Positive values indicate an address_class. */
13395 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13397 else
13399 /* Negative values indicate an (inverted) segment base reg. */
13400 dw_loc_descr_ref d
13401 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13402 add_AT_loc (mod_type_die, DW_AT_segment, d);
13406 else if (code == INTEGER_TYPE
13407 && TREE_TYPE (type) != NULL_TREE
13408 && subrange_type_for_debug_p (type, &low, &high))
13410 tree bias = NULL_TREE;
13411 if (lang_hooks.types.get_type_bias)
13412 bias = lang_hooks.types.get_type_bias (type);
13413 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13414 item_type = TREE_TYPE (type);
13416 else if (is_base_type (type))
13418 mod_type_die = base_type_die (type, reverse);
13420 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13421 if (reverse_base_type)
13423 dw_die_ref after_die
13424 = modified_type_die (type, cv_quals, false, context_die);
13425 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13427 else
13428 add_child_die (comp_unit_die (), mod_type_die);
13430 add_pubtype (type, mod_type_die);
13432 else
13434 gen_type_die (type, context_die);
13436 /* We have to get the type_main_variant here (and pass that to the
13437 `lookup_type_die' routine) because the ..._TYPE node we have
13438 might simply be a *copy* of some original type node (where the
13439 copy was created to help us keep track of typedef names) and
13440 that copy might have a different TYPE_UID from the original
13441 ..._TYPE node. */
13442 if (TREE_CODE (type) == FUNCTION_TYPE
13443 || TREE_CODE (type) == METHOD_TYPE)
13445 /* For function/method types, can't just use type_main_variant here,
13446 because that can have different ref-qualifiers for C++,
13447 but try to canonicalize. */
13448 tree main = TYPE_MAIN_VARIANT (type);
13449 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13450 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13451 && check_base_type (t, main)
13452 && check_lang_type (t, type))
13453 return lookup_type_die (t);
13454 return lookup_type_die (type);
13456 else if (TREE_CODE (type) != VECTOR_TYPE
13457 && TREE_CODE (type) != ARRAY_TYPE)
13458 return lookup_type_die (type_main_variant (type));
13459 else
13460 /* Vectors have the debugging information in the type,
13461 not the main variant. */
13462 return lookup_type_die (type);
13465 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13466 don't output a DW_TAG_typedef, since there isn't one in the
13467 user's program; just attach a DW_AT_name to the type.
13468 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13469 if the base type already has the same name. */
13470 if (name
13471 && ((TREE_CODE (name) != TYPE_DECL
13472 && (qualified_type == TYPE_MAIN_VARIANT (type)
13473 || (cv_quals == TYPE_UNQUALIFIED)))
13474 || (TREE_CODE (name) == TYPE_DECL
13475 && TREE_TYPE (name) == qualified_type
13476 && DECL_NAME (name))))
13478 if (TREE_CODE (name) == TYPE_DECL)
13479 /* Could just call add_name_and_src_coords_attributes here,
13480 but since this is a builtin type it doesn't have any
13481 useful source coordinates anyway. */
13482 name = DECL_NAME (name);
13483 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13485 /* This probably indicates a bug. */
13486 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13488 name = TYPE_IDENTIFIER (type);
13489 add_name_attribute (mod_type_die,
13490 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13493 if (qualified_type && !reverse_base_type)
13494 equate_type_number_to_die (qualified_type, mod_type_die);
13496 if (item_type)
13497 /* We must do this after the equate_type_number_to_die call, in case
13498 this is a recursive type. This ensures that the modified_type_die
13499 recursion will terminate even if the type is recursive. Recursive
13500 types are possible in Ada. */
13501 sub_die = modified_type_die (item_type,
13502 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13503 reverse,
13504 context_die);
13506 if (sub_die != NULL)
13507 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13509 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13510 if (TYPE_ARTIFICIAL (type))
13511 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13513 return mod_type_die;
13516 /* Generate DIEs for the generic parameters of T.
13517 T must be either a generic type or a generic function.
13518 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13520 static void
13521 gen_generic_params_dies (tree t)
13523 tree parms, args;
13524 int parms_num, i;
13525 dw_die_ref die = NULL;
13526 int non_default;
13528 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13529 return;
13531 if (TYPE_P (t))
13532 die = lookup_type_die (t);
13533 else if (DECL_P (t))
13534 die = lookup_decl_die (t);
13536 gcc_assert (die);
13538 parms = lang_hooks.get_innermost_generic_parms (t);
13539 if (!parms)
13540 /* T has no generic parameter. It means T is neither a generic type
13541 or function. End of story. */
13542 return;
13544 parms_num = TREE_VEC_LENGTH (parms);
13545 args = lang_hooks.get_innermost_generic_args (t);
13546 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13547 non_default = int_cst_value (TREE_CHAIN (args));
13548 else
13549 non_default = TREE_VEC_LENGTH (args);
13550 for (i = 0; i < parms_num; i++)
13552 tree parm, arg, arg_pack_elems;
13553 dw_die_ref parm_die;
13555 parm = TREE_VEC_ELT (parms, i);
13556 arg = TREE_VEC_ELT (args, i);
13557 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13558 gcc_assert (parm && TREE_VALUE (parm) && arg);
13560 if (parm && TREE_VALUE (parm) && arg)
13562 /* If PARM represents a template parameter pack,
13563 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13564 by DW_TAG_template_*_parameter DIEs for the argument
13565 pack elements of ARG. Note that ARG would then be
13566 an argument pack. */
13567 if (arg_pack_elems)
13568 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13569 arg_pack_elems,
13570 die);
13571 else
13572 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13573 true /* emit name */, die);
13574 if (i >= non_default)
13575 add_AT_flag (parm_die, DW_AT_default_value, 1);
13580 /* Create and return a DIE for PARM which should be
13581 the representation of a generic type parameter.
13582 For instance, in the C++ front end, PARM would be a template parameter.
13583 ARG is the argument to PARM.
13584 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13585 name of the PARM.
13586 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13587 as a child node. */
13589 static dw_die_ref
13590 generic_parameter_die (tree parm, tree arg,
13591 bool emit_name_p,
13592 dw_die_ref parent_die)
13594 dw_die_ref tmpl_die = NULL;
13595 const char *name = NULL;
13597 if (!parm || !DECL_NAME (parm) || !arg)
13598 return NULL;
13600 /* We support non-type generic parameters and arguments,
13601 type generic parameters and arguments, as well as
13602 generic generic parameters (a.k.a. template template parameters in C++)
13603 and arguments. */
13604 if (TREE_CODE (parm) == PARM_DECL)
13605 /* PARM is a nontype generic parameter */
13606 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13607 else if (TREE_CODE (parm) == TYPE_DECL)
13608 /* PARM is a type generic parameter. */
13609 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13610 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13611 /* PARM is a generic generic parameter.
13612 Its DIE is a GNU extension. It shall have a
13613 DW_AT_name attribute to represent the name of the template template
13614 parameter, and a DW_AT_GNU_template_name attribute to represent the
13615 name of the template template argument. */
13616 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13617 parent_die, parm);
13618 else
13619 gcc_unreachable ();
13621 if (tmpl_die)
13623 tree tmpl_type;
13625 /* If PARM is a generic parameter pack, it means we are
13626 emitting debug info for a template argument pack element.
13627 In other terms, ARG is a template argument pack element.
13628 In that case, we don't emit any DW_AT_name attribute for
13629 the die. */
13630 if (emit_name_p)
13632 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13633 gcc_assert (name);
13634 add_AT_string (tmpl_die, DW_AT_name, name);
13637 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13639 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13640 TMPL_DIE should have a child DW_AT_type attribute that is set
13641 to the type of the argument to PARM, which is ARG.
13642 If PARM is a type generic parameter, TMPL_DIE should have a
13643 child DW_AT_type that is set to ARG. */
13644 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13645 add_type_attribute (tmpl_die, tmpl_type,
13646 (TREE_THIS_VOLATILE (tmpl_type)
13647 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
13648 false, parent_die);
13650 else
13652 /* So TMPL_DIE is a DIE representing a
13653 a generic generic template parameter, a.k.a template template
13654 parameter in C++ and arg is a template. */
13656 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13657 to the name of the argument. */
13658 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13659 if (name)
13660 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13663 if (TREE_CODE (parm) == PARM_DECL)
13664 /* So PARM is a non-type generic parameter.
13665 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13666 attribute of TMPL_DIE which value represents the value
13667 of ARG.
13668 We must be careful here:
13669 The value of ARG might reference some function decls.
13670 We might currently be emitting debug info for a generic
13671 type and types are emitted before function decls, we don't
13672 know if the function decls referenced by ARG will actually be
13673 emitted after cgraph computations.
13674 So must defer the generation of the DW_AT_const_value to
13675 after cgraph is ready. */
13676 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13679 return tmpl_die;
13682 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13683 PARM_PACK must be a template parameter pack. The returned DIE
13684 will be child DIE of PARENT_DIE. */
13686 static dw_die_ref
13687 template_parameter_pack_die (tree parm_pack,
13688 tree parm_pack_args,
13689 dw_die_ref parent_die)
13691 dw_die_ref die;
13692 int j;
13694 gcc_assert (parent_die && parm_pack);
13696 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13697 add_name_and_src_coords_attributes (die, parm_pack);
13698 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13699 generic_parameter_die (parm_pack,
13700 TREE_VEC_ELT (parm_pack_args, j),
13701 false /* Don't emit DW_AT_name */,
13702 die);
13703 return die;
13706 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13707 an enumerated type. */
13709 static inline int
13710 type_is_enum (const_tree type)
13712 return TREE_CODE (type) == ENUMERAL_TYPE;
13715 /* Return the DBX register number described by a given RTL node. */
13717 static unsigned int
13718 dbx_reg_number (const_rtx rtl)
13720 unsigned regno = REGNO (rtl);
13722 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13724 #ifdef LEAF_REG_REMAP
13725 if (crtl->uses_only_leaf_regs)
13727 int leaf_reg = LEAF_REG_REMAP (regno);
13728 if (leaf_reg != -1)
13729 regno = (unsigned) leaf_reg;
13731 #endif
13733 regno = DBX_REGISTER_NUMBER (regno);
13734 gcc_assert (regno != INVALID_REGNUM);
13735 return regno;
13738 /* Optionally add a DW_OP_piece term to a location description expression.
13739 DW_OP_piece is only added if the location description expression already
13740 doesn't end with DW_OP_piece. */
13742 static void
13743 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13745 dw_loc_descr_ref loc;
13747 if (*list_head != NULL)
13749 /* Find the end of the chain. */
13750 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13753 if (loc->dw_loc_opc != DW_OP_piece)
13754 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13758 /* Return a location descriptor that designates a machine register or
13759 zero if there is none. */
13761 static dw_loc_descr_ref
13762 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13764 rtx regs;
13766 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13767 return 0;
13769 /* We only use "frame base" when we're sure we're talking about the
13770 post-prologue local stack frame. We do this by *not* running
13771 register elimination until this point, and recognizing the special
13772 argument pointer and soft frame pointer rtx's.
13773 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13774 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13775 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13777 dw_loc_descr_ref result = NULL;
13779 if (dwarf_version >= 4 || !dwarf_strict)
13781 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13782 initialized);
13783 if (result)
13784 add_loc_descr (&result,
13785 new_loc_descr (DW_OP_stack_value, 0, 0));
13787 return result;
13790 regs = targetm.dwarf_register_span (rtl);
13792 if (REG_NREGS (rtl) > 1 || regs)
13793 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13794 else
13796 unsigned int dbx_regnum = dbx_reg_number (rtl);
13797 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13798 return 0;
13799 return one_reg_loc_descriptor (dbx_regnum, initialized);
13803 /* Return a location descriptor that designates a machine register for
13804 a given hard register number. */
13806 static dw_loc_descr_ref
13807 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13809 dw_loc_descr_ref reg_loc_descr;
13811 if (regno <= 31)
13812 reg_loc_descr
13813 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13814 else
13815 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13817 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13818 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13820 return reg_loc_descr;
13823 /* Given an RTL of a register, return a location descriptor that
13824 designates a value that spans more than one register. */
13826 static dw_loc_descr_ref
13827 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13828 enum var_init_status initialized)
13830 int size, i;
13831 dw_loc_descr_ref loc_result = NULL;
13833 /* Simple, contiguous registers. */
13834 if (regs == NULL_RTX)
13836 unsigned reg = REGNO (rtl);
13837 int nregs;
13839 #ifdef LEAF_REG_REMAP
13840 if (crtl->uses_only_leaf_regs)
13842 int leaf_reg = LEAF_REG_REMAP (reg);
13843 if (leaf_reg != -1)
13844 reg = (unsigned) leaf_reg;
13846 #endif
13848 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13849 nregs = REG_NREGS (rtl);
13851 /* At present we only track constant-sized pieces. */
13852 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
13853 return NULL;
13854 size /= nregs;
13856 loc_result = NULL;
13857 while (nregs--)
13859 dw_loc_descr_ref t;
13861 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13862 VAR_INIT_STATUS_INITIALIZED);
13863 add_loc_descr (&loc_result, t);
13864 add_loc_descr_op_piece (&loc_result, size);
13865 ++reg;
13867 return loc_result;
13870 /* Now onto stupid register sets in non contiguous locations. */
13872 gcc_assert (GET_CODE (regs) == PARALLEL);
13874 /* At present we only track constant-sized pieces. */
13875 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
13876 return NULL;
13877 loc_result = NULL;
13879 for (i = 0; i < XVECLEN (regs, 0); ++i)
13881 dw_loc_descr_ref t;
13883 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13884 VAR_INIT_STATUS_INITIALIZED);
13885 add_loc_descr (&loc_result, t);
13886 add_loc_descr_op_piece (&loc_result, size);
13889 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13890 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13891 return loc_result;
13894 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13896 /* Return a location descriptor that designates a constant i,
13897 as a compound operation from constant (i >> shift), constant shift
13898 and DW_OP_shl. */
13900 static dw_loc_descr_ref
13901 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13903 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13904 add_loc_descr (&ret, int_loc_descriptor (shift));
13905 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13906 return ret;
13909 /* Return a location descriptor that designates constant POLY_I. */
13911 static dw_loc_descr_ref
13912 int_loc_descriptor (poly_int64 poly_i)
13914 enum dwarf_location_atom op;
13916 HOST_WIDE_INT i;
13917 if (!poly_i.is_constant (&i))
13919 /* Create location descriptions for the non-constant part and
13920 add any constant offset at the end. */
13921 dw_loc_descr_ref ret = NULL;
13922 HOST_WIDE_INT constant = poly_i.coeffs[0];
13923 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
13925 HOST_WIDE_INT coeff = poly_i.coeffs[j];
13926 if (coeff != 0)
13928 dw_loc_descr_ref start = ret;
13929 unsigned int factor;
13930 int bias;
13931 unsigned int regno = targetm.dwarf_poly_indeterminate_value
13932 (j, &factor, &bias);
13934 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
13935 add COEFF * (REGNO / FACTOR) now and subtract
13936 COEFF * BIAS from the final constant part. */
13937 constant -= coeff * bias;
13938 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
13939 if (coeff % factor == 0)
13940 coeff /= factor;
13941 else
13943 int amount = exact_log2 (factor);
13944 gcc_assert (amount >= 0);
13945 add_loc_descr (&ret, int_loc_descriptor (amount));
13946 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13948 if (coeff != 1)
13950 add_loc_descr (&ret, int_loc_descriptor (coeff));
13951 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13953 if (start)
13954 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
13957 loc_descr_plus_const (&ret, constant);
13958 return ret;
13961 /* Pick the smallest representation of a constant, rather than just
13962 defaulting to the LEB encoding. */
13963 if (i >= 0)
13965 int clz = clz_hwi (i);
13966 int ctz = ctz_hwi (i);
13967 if (i <= 31)
13968 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13969 else if (i <= 0xff)
13970 op = DW_OP_const1u;
13971 else if (i <= 0xffff)
13972 op = DW_OP_const2u;
13973 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13974 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13975 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13976 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13977 while DW_OP_const4u is 5 bytes. */
13978 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13979 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13980 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13981 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13982 while DW_OP_const4u is 5 bytes. */
13983 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13985 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13986 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13987 <= 4)
13989 /* As i >= 2**31, the double cast above will yield a negative number.
13990 Since wrapping is defined in DWARF expressions we can output big
13991 positive integers as small negative ones, regardless of the size
13992 of host wide ints.
13994 Here, since the evaluator will handle 32-bit values and since i >=
13995 2**31, we know it's going to be interpreted as a negative literal:
13996 store it this way if we can do better than 5 bytes this way. */
13997 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13999 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14000 op = DW_OP_const4u;
14002 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14003 least 6 bytes: see if we can do better before falling back to it. */
14004 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14005 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14006 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14007 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14008 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14009 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14010 >= HOST_BITS_PER_WIDE_INT)
14011 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14012 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14013 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14014 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14015 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14016 && size_of_uleb128 (i) > 6)
14017 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14018 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14019 else
14020 op = DW_OP_constu;
14022 else
14024 if (i >= -0x80)
14025 op = DW_OP_const1s;
14026 else if (i >= -0x8000)
14027 op = DW_OP_const2s;
14028 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14030 if (size_of_int_loc_descriptor (i) < 5)
14032 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14033 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14034 return ret;
14036 op = DW_OP_const4s;
14038 else
14040 if (size_of_int_loc_descriptor (i)
14041 < (unsigned long) 1 + size_of_sleb128 (i))
14043 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14044 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14045 return ret;
14047 op = DW_OP_consts;
14051 return new_loc_descr (op, i, 0);
14054 /* Likewise, for unsigned constants. */
14056 static dw_loc_descr_ref
14057 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14059 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14060 const unsigned HOST_WIDE_INT max_uint
14061 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14063 /* If possible, use the clever signed constants handling. */
14064 if (i <= max_int)
14065 return int_loc_descriptor ((HOST_WIDE_INT) i);
14067 /* Here, we are left with positive numbers that cannot be represented as
14068 HOST_WIDE_INT, i.e.:
14069 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14071 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14072 whereas may be better to output a negative integer: thanks to integer
14073 wrapping, we know that:
14074 x = x - 2 ** DWARF2_ADDR_SIZE
14075 = x - 2 * (max (HOST_WIDE_INT) + 1)
14076 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14077 small negative integers. Let's try that in cases it will clearly improve
14078 the encoding: there is no gain turning DW_OP_const4u into
14079 DW_OP_const4s. */
14080 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14081 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14082 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14084 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14086 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14087 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14088 const HOST_WIDE_INT second_shift
14089 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14091 /* So we finally have:
14092 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14093 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14094 return int_loc_descriptor (second_shift);
14097 /* Last chance: fallback to a simple constant operation. */
14098 return new_loc_descr
14099 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14100 ? DW_OP_const4u
14101 : DW_OP_const8u,
14102 i, 0);
14105 /* Generate and return a location description that computes the unsigned
14106 comparison of the two stack top entries (a OP b where b is the top-most
14107 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14108 LE_EXPR, GT_EXPR or GE_EXPR. */
14110 static dw_loc_descr_ref
14111 uint_comparison_loc_list (enum tree_code kind)
14113 enum dwarf_location_atom op, flip_op;
14114 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14116 switch (kind)
14118 case LT_EXPR:
14119 op = DW_OP_lt;
14120 break;
14121 case LE_EXPR:
14122 op = DW_OP_le;
14123 break;
14124 case GT_EXPR:
14125 op = DW_OP_gt;
14126 break;
14127 case GE_EXPR:
14128 op = DW_OP_ge;
14129 break;
14130 default:
14131 gcc_unreachable ();
14134 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14135 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14137 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14138 possible to perform unsigned comparisons: we just have to distinguish
14139 three cases:
14141 1. when a and b have the same sign (as signed integers); then we should
14142 return: a OP(signed) b;
14144 2. when a is a negative signed integer while b is a positive one, then a
14145 is a greater unsigned integer than b; likewise when a and b's roles
14146 are flipped.
14148 So first, compare the sign of the two operands. */
14149 ret = new_loc_descr (DW_OP_over, 0, 0);
14150 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14151 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14152 /* If they have different signs (i.e. they have different sign bits), then
14153 the stack top value has now the sign bit set and thus it's smaller than
14154 zero. */
14155 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14156 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14157 add_loc_descr (&ret, bra_node);
14159 /* We are in case 1. At this point, we know both operands have the same
14160 sign, to it's safe to use the built-in signed comparison. */
14161 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14162 add_loc_descr (&ret, jmp_node);
14164 /* We are in case 2. Here, we know both operands do not have the same sign,
14165 so we have to flip the signed comparison. */
14166 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14167 tmp = new_loc_descr (flip_op, 0, 0);
14168 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14169 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14170 add_loc_descr (&ret, tmp);
14172 /* This dummy operation is necessary to make the two branches join. */
14173 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14174 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14175 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14176 add_loc_descr (&ret, tmp);
14178 return ret;
14181 /* Likewise, but takes the location description lists (might be destructive on
14182 them). Return NULL if either is NULL or if concatenation fails. */
14184 static dw_loc_list_ref
14185 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14186 enum tree_code kind)
14188 if (left == NULL || right == NULL)
14189 return NULL;
14191 add_loc_list (&left, right);
14192 if (left == NULL)
14193 return NULL;
14195 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14196 return left;
14199 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14200 without actually allocating it. */
14202 static unsigned long
14203 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14205 return size_of_int_loc_descriptor (i >> shift)
14206 + size_of_int_loc_descriptor (shift)
14207 + 1;
14210 /* Return size_of_locs (int_loc_descriptor (i)) without
14211 actually allocating it. */
14213 static unsigned long
14214 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14216 unsigned long s;
14218 if (i >= 0)
14220 int clz, ctz;
14221 if (i <= 31)
14222 return 1;
14223 else if (i <= 0xff)
14224 return 2;
14225 else if (i <= 0xffff)
14226 return 3;
14227 clz = clz_hwi (i);
14228 ctz = ctz_hwi (i);
14229 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14230 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14231 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14232 - clz - 5);
14233 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14234 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14235 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14236 - clz - 8);
14237 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14238 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14239 <= 4)
14240 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14241 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14242 return 5;
14243 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14244 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14245 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14246 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14247 - clz - 8);
14248 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14249 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14250 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14251 - clz - 16);
14252 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14253 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14254 && s > 6)
14255 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14256 - clz - 32);
14257 else
14258 return 1 + s;
14260 else
14262 if (i >= -0x80)
14263 return 2;
14264 else if (i >= -0x8000)
14265 return 3;
14266 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14268 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14270 s = size_of_int_loc_descriptor (-i) + 1;
14271 if (s < 5)
14272 return s;
14274 return 5;
14276 else
14278 unsigned long r = 1 + size_of_sleb128 (i);
14279 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14281 s = size_of_int_loc_descriptor (-i) + 1;
14282 if (s < r)
14283 return s;
14285 return r;
14290 /* Return loc description representing "address" of integer value.
14291 This can appear only as toplevel expression. */
14293 static dw_loc_descr_ref
14294 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14296 int litsize;
14297 dw_loc_descr_ref loc_result = NULL;
14299 if (!(dwarf_version >= 4 || !dwarf_strict))
14300 return NULL;
14302 litsize = size_of_int_loc_descriptor (i);
14303 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14304 is more compact. For DW_OP_stack_value we need:
14305 litsize + 1 (DW_OP_stack_value)
14306 and for DW_OP_implicit_value:
14307 1 (DW_OP_implicit_value) + 1 (length) + size. */
14308 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14310 loc_result = int_loc_descriptor (i);
14311 add_loc_descr (&loc_result,
14312 new_loc_descr (DW_OP_stack_value, 0, 0));
14313 return loc_result;
14316 loc_result = new_loc_descr (DW_OP_implicit_value,
14317 size, 0);
14318 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14319 loc_result->dw_loc_oprnd2.v.val_int = i;
14320 return loc_result;
14323 /* Return a location descriptor that designates a base+offset location. */
14325 static dw_loc_descr_ref
14326 based_loc_descr (rtx reg, poly_int64 offset,
14327 enum var_init_status initialized)
14329 unsigned int regno;
14330 dw_loc_descr_ref result;
14331 dw_fde_ref fde = cfun->fde;
14333 /* We only use "frame base" when we're sure we're talking about the
14334 post-prologue local stack frame. We do this by *not* running
14335 register elimination until this point, and recognizing the special
14336 argument pointer and soft frame pointer rtx's. */
14337 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14339 rtx elim = (ira_use_lra_p
14340 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14341 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14343 if (elim != reg)
14345 /* Allow hard frame pointer here even if frame pointer
14346 isn't used since hard frame pointer is encoded with
14347 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14348 not hard frame pointer directly. */
14349 elim = strip_offset_and_add (elim, &offset);
14350 gcc_assert (elim == hard_frame_pointer_rtx
14351 || elim == stack_pointer_rtx);
14353 /* If drap register is used to align stack, use frame
14354 pointer + offset to access stack variables. If stack
14355 is aligned without drap, use stack pointer + offset to
14356 access stack variables. */
14357 if (crtl->stack_realign_tried
14358 && reg == frame_pointer_rtx)
14360 int base_reg
14361 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14362 ? HARD_FRAME_POINTER_REGNUM
14363 : REGNO (elim));
14364 return new_reg_loc_descr (base_reg, offset);
14367 gcc_assert (frame_pointer_fb_offset_valid);
14368 offset += frame_pointer_fb_offset;
14369 HOST_WIDE_INT const_offset;
14370 if (offset.is_constant (&const_offset))
14371 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14372 else
14374 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14375 loc_descr_plus_const (&ret, offset);
14376 return ret;
14381 regno = REGNO (reg);
14382 #ifdef LEAF_REG_REMAP
14383 if (crtl->uses_only_leaf_regs)
14385 int leaf_reg = LEAF_REG_REMAP (regno);
14386 if (leaf_reg != -1)
14387 regno = (unsigned) leaf_reg;
14389 #endif
14390 regno = DWARF_FRAME_REGNUM (regno);
14392 HOST_WIDE_INT const_offset;
14393 if (!optimize && fde
14394 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14395 && offset.is_constant (&const_offset))
14397 /* Use cfa+offset to represent the location of arguments passed
14398 on the stack when drap is used to align stack.
14399 Only do this when not optimizing, for optimized code var-tracking
14400 is supposed to track where the arguments live and the register
14401 used as vdrap or drap in some spot might be used for something
14402 else in other part of the routine. */
14403 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14406 result = new_reg_loc_descr (regno, offset);
14408 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14409 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14411 return result;
14414 /* Return true if this RTL expression describes a base+offset calculation. */
14416 static inline int
14417 is_based_loc (const_rtx rtl)
14419 return (GET_CODE (rtl) == PLUS
14420 && ((REG_P (XEXP (rtl, 0))
14421 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14422 && CONST_INT_P (XEXP (rtl, 1)))));
14425 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14426 failed. */
14428 static dw_loc_descr_ref
14429 tls_mem_loc_descriptor (rtx mem)
14431 tree base;
14432 dw_loc_descr_ref loc_result;
14434 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14435 return NULL;
14437 base = get_base_address (MEM_EXPR (mem));
14438 if (base == NULL
14439 || !VAR_P (base)
14440 || !DECL_THREAD_LOCAL_P (base))
14441 return NULL;
14443 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14444 if (loc_result == NULL)
14445 return NULL;
14447 if (maybe_ne (MEM_OFFSET (mem), 0))
14448 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14450 return loc_result;
14453 /* Output debug info about reason why we failed to expand expression as dwarf
14454 expression. */
14456 static void
14457 expansion_failed (tree expr, rtx rtl, char const *reason)
14459 if (dump_file && (dump_flags & TDF_DETAILS))
14461 fprintf (dump_file, "Failed to expand as dwarf: ");
14462 if (expr)
14463 print_generic_expr (dump_file, expr, dump_flags);
14464 if (rtl)
14466 fprintf (dump_file, "\n");
14467 print_rtl (dump_file, rtl);
14469 fprintf (dump_file, "\nReason: %s\n", reason);
14473 /* Helper function for const_ok_for_output. */
14475 static bool
14476 const_ok_for_output_1 (rtx rtl)
14478 if (targetm.const_not_ok_for_debug_p (rtl))
14480 if (GET_CODE (rtl) != UNSPEC)
14482 expansion_failed (NULL_TREE, rtl,
14483 "Expression rejected for debug by the backend.\n");
14484 return false;
14487 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14488 the target hook doesn't explicitly allow it in debug info, assume
14489 we can't express it in the debug info. */
14490 /* Don't complain about TLS UNSPECs, those are just too hard to
14491 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14492 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14493 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14494 if (flag_checking
14495 && (XVECLEN (rtl, 0) == 0
14496 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14497 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14498 inform (current_function_decl
14499 ? DECL_SOURCE_LOCATION (current_function_decl)
14500 : UNKNOWN_LOCATION,
14501 #if NUM_UNSPEC_VALUES > 0
14502 "non-delegitimized UNSPEC %s (%d) found in variable location",
14503 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14504 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14505 XINT (rtl, 1));
14506 #else
14507 "non-delegitimized UNSPEC %d found in variable location",
14508 XINT (rtl, 1));
14509 #endif
14510 expansion_failed (NULL_TREE, rtl,
14511 "UNSPEC hasn't been delegitimized.\n");
14512 return false;
14515 if (CONST_POLY_INT_P (rtl))
14516 return false;
14518 if (targetm.const_not_ok_for_debug_p (rtl))
14520 expansion_failed (NULL_TREE, rtl,
14521 "Expression rejected for debug by the backend.\n");
14522 return false;
14525 /* FIXME: Refer to PR60655. It is possible for simplification
14526 of rtl expressions in var tracking to produce such expressions.
14527 We should really identify / validate expressions
14528 enclosed in CONST that can be handled by assemblers on various
14529 targets and only handle legitimate cases here. */
14530 switch (GET_CODE (rtl))
14532 case SYMBOL_REF:
14533 break;
14534 case NOT:
14535 case NEG:
14536 return false;
14537 default:
14538 return true;
14541 if (CONSTANT_POOL_ADDRESS_P (rtl))
14543 bool marked;
14544 get_pool_constant_mark (rtl, &marked);
14545 /* If all references to this pool constant were optimized away,
14546 it was not output and thus we can't represent it. */
14547 if (!marked)
14549 expansion_failed (NULL_TREE, rtl,
14550 "Constant was removed from constant pool.\n");
14551 return false;
14555 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14556 return false;
14558 /* Avoid references to external symbols in debug info, on several targets
14559 the linker might even refuse to link when linking a shared library,
14560 and in many other cases the relocations for .debug_info/.debug_loc are
14561 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14562 to be defined within the same shared library or executable are fine. */
14563 if (SYMBOL_REF_EXTERNAL_P (rtl))
14565 tree decl = SYMBOL_REF_DECL (rtl);
14567 if (decl == NULL || !targetm.binds_local_p (decl))
14569 expansion_failed (NULL_TREE, rtl,
14570 "Symbol not defined in current TU.\n");
14571 return false;
14575 return true;
14578 /* Return true if constant RTL can be emitted in DW_OP_addr or
14579 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14580 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14582 static bool
14583 const_ok_for_output (rtx rtl)
14585 if (GET_CODE (rtl) == SYMBOL_REF)
14586 return const_ok_for_output_1 (rtl);
14588 if (GET_CODE (rtl) == CONST)
14590 subrtx_var_iterator::array_type array;
14591 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14592 if (!const_ok_for_output_1 (*iter))
14593 return false;
14594 return true;
14597 return true;
14600 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
14601 if possible, NULL otherwise. */
14603 static dw_die_ref
14604 base_type_for_mode (machine_mode mode, bool unsignedp)
14606 dw_die_ref type_die;
14607 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
14609 if (type == NULL)
14610 return NULL;
14611 switch (TREE_CODE (type))
14613 case INTEGER_TYPE:
14614 case REAL_TYPE:
14615 break;
14616 default:
14617 return NULL;
14619 type_die = lookup_type_die (type);
14620 if (!type_die)
14621 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
14622 comp_unit_die ());
14623 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
14624 return NULL;
14625 return type_die;
14628 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
14629 type matching MODE, or, if MODE is narrower than or as wide as
14630 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
14631 possible. */
14633 static dw_loc_descr_ref
14634 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
14636 machine_mode outer_mode = mode;
14637 dw_die_ref type_die;
14638 dw_loc_descr_ref cvt;
14640 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14642 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
14643 return op;
14645 type_die = base_type_for_mode (outer_mode, 1);
14646 if (type_die == NULL)
14647 return NULL;
14648 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14649 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14650 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14651 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14652 add_loc_descr (&op, cvt);
14653 return op;
14656 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
14658 static dw_loc_descr_ref
14659 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
14660 dw_loc_descr_ref op1)
14662 dw_loc_descr_ref ret = op0;
14663 add_loc_descr (&ret, op1);
14664 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14665 if (STORE_FLAG_VALUE != 1)
14667 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14668 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14670 return ret;
14673 /* Subroutine of scompare_loc_descriptor for the case in which we're
14674 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14675 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
14677 static dw_loc_descr_ref
14678 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14679 scalar_int_mode op_mode,
14680 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14682 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14683 dw_loc_descr_ref cvt;
14685 if (type_die == NULL)
14686 return NULL;
14687 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14688 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14689 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14690 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14691 add_loc_descr (&op0, cvt);
14692 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14693 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14694 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14695 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14696 add_loc_descr (&op1, cvt);
14697 return compare_loc_descriptor (op, op0, op1);
14700 /* Subroutine of scompare_loc_descriptor for the case in which we're
14701 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14702 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
14704 static dw_loc_descr_ref
14705 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
14706 scalar_int_mode op_mode,
14707 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14709 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14710 /* For eq/ne, if the operands are known to be zero-extended,
14711 there is no need to do the fancy shifting up. */
14712 if (op == DW_OP_eq || op == DW_OP_ne)
14714 dw_loc_descr_ref last0, last1;
14715 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14717 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14719 /* deref_size zero extends, and for constants we can check
14720 whether they are zero extended or not. */
14721 if (((last0->dw_loc_opc == DW_OP_deref_size
14722 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14723 || (CONST_INT_P (XEXP (rtl, 0))
14724 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14725 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14726 && ((last1->dw_loc_opc == DW_OP_deref_size
14727 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14728 || (CONST_INT_P (XEXP (rtl, 1))
14729 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14730 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14731 return compare_loc_descriptor (op, op0, op1);
14733 /* EQ/NE comparison against constant in narrower type than
14734 DWARF2_ADDR_SIZE can be performed either as
14735 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14736 DW_OP_{eq,ne}
14738 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14739 DW_OP_{eq,ne}. Pick whatever is shorter. */
14740 if (CONST_INT_P (XEXP (rtl, 1))
14741 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14742 && (size_of_int_loc_descriptor (shift) + 1
14743 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14744 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14745 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14746 & GET_MODE_MASK (op_mode))))
14748 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14749 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14750 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14751 & GET_MODE_MASK (op_mode));
14752 return compare_loc_descriptor (op, op0, op1);
14755 add_loc_descr (&op0, int_loc_descriptor (shift));
14756 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14757 if (CONST_INT_P (XEXP (rtl, 1)))
14758 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14759 else
14761 add_loc_descr (&op1, int_loc_descriptor (shift));
14762 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14764 return compare_loc_descriptor (op, op0, op1);
14767 /* Return location descriptor for unsigned comparison OP RTL. */
14769 static dw_loc_descr_ref
14770 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14771 machine_mode mem_mode)
14773 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14774 dw_loc_descr_ref op0, op1;
14776 if (op_mode == VOIDmode)
14777 op_mode = GET_MODE (XEXP (rtl, 1));
14778 if (op_mode == VOIDmode)
14779 return NULL;
14781 scalar_int_mode int_op_mode;
14782 if (dwarf_strict
14783 && dwarf_version < 5
14784 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14785 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14786 return NULL;
14788 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14789 VAR_INIT_STATUS_INITIALIZED);
14790 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14791 VAR_INIT_STATUS_INITIALIZED);
14793 if (op0 == NULL || op1 == NULL)
14794 return NULL;
14796 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14798 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14799 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14801 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14802 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14804 return compare_loc_descriptor (op, op0, op1);
14807 /* Return location descriptor for unsigned comparison OP RTL. */
14809 static dw_loc_descr_ref
14810 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14811 machine_mode mem_mode)
14813 dw_loc_descr_ref op0, op1;
14815 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14816 if (test_op_mode == VOIDmode)
14817 test_op_mode = GET_MODE (XEXP (rtl, 1));
14819 scalar_int_mode op_mode;
14820 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14821 return NULL;
14823 if (dwarf_strict
14824 && dwarf_version < 5
14825 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14826 return NULL;
14828 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14829 VAR_INIT_STATUS_INITIALIZED);
14830 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14831 VAR_INIT_STATUS_INITIALIZED);
14833 if (op0 == NULL || op1 == NULL)
14834 return NULL;
14836 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14838 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14839 dw_loc_descr_ref last0, last1;
14840 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14842 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14844 if (CONST_INT_P (XEXP (rtl, 0)))
14845 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14846 /* deref_size zero extends, so no need to mask it again. */
14847 else if (last0->dw_loc_opc != DW_OP_deref_size
14848 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14850 add_loc_descr (&op0, int_loc_descriptor (mask));
14851 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14853 if (CONST_INT_P (XEXP (rtl, 1)))
14854 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14855 /* deref_size zero extends, so no need to mask it again. */
14856 else if (last1->dw_loc_opc != DW_OP_deref_size
14857 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14859 add_loc_descr (&op1, int_loc_descriptor (mask));
14860 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14863 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14865 HOST_WIDE_INT bias = 1;
14866 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14867 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14868 if (CONST_INT_P (XEXP (rtl, 1)))
14869 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14870 + INTVAL (XEXP (rtl, 1)));
14871 else
14872 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14873 bias, 0));
14875 return compare_loc_descriptor (op, op0, op1);
14878 /* Return location descriptor for {U,S}{MIN,MAX}. */
14880 static dw_loc_descr_ref
14881 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14882 machine_mode mem_mode)
14884 enum dwarf_location_atom op;
14885 dw_loc_descr_ref op0, op1, ret;
14886 dw_loc_descr_ref bra_node, drop_node;
14888 scalar_int_mode int_mode;
14889 if (dwarf_strict
14890 && dwarf_version < 5
14891 && (!is_a <scalar_int_mode> (mode, &int_mode)
14892 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14893 return NULL;
14895 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14896 VAR_INIT_STATUS_INITIALIZED);
14897 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14898 VAR_INIT_STATUS_INITIALIZED);
14900 if (op0 == NULL || op1 == NULL)
14901 return NULL;
14903 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14904 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14905 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14906 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14908 /* Checked by the caller. */
14909 int_mode = as_a <scalar_int_mode> (mode);
14910 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14912 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14913 add_loc_descr (&op0, int_loc_descriptor (mask));
14914 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14915 add_loc_descr (&op1, int_loc_descriptor (mask));
14916 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14918 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14920 HOST_WIDE_INT bias = 1;
14921 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14922 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14923 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14926 else if (is_a <scalar_int_mode> (mode, &int_mode)
14927 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14929 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14930 add_loc_descr (&op0, int_loc_descriptor (shift));
14931 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14932 add_loc_descr (&op1, int_loc_descriptor (shift));
14933 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14935 else if (is_a <scalar_int_mode> (mode, &int_mode)
14936 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14938 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14939 dw_loc_descr_ref cvt;
14940 if (type_die == NULL)
14941 return NULL;
14942 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14943 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14944 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14945 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14946 add_loc_descr (&op0, cvt);
14947 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14948 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14949 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14950 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14951 add_loc_descr (&op1, cvt);
14954 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14955 op = DW_OP_lt;
14956 else
14957 op = DW_OP_gt;
14958 ret = op0;
14959 add_loc_descr (&ret, op1);
14960 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14961 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14962 add_loc_descr (&ret, bra_node);
14963 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14964 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14965 add_loc_descr (&ret, drop_node);
14966 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14967 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14968 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14969 && is_a <scalar_int_mode> (mode, &int_mode)
14970 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14971 ret = convert_descriptor_to_mode (int_mode, ret);
14972 return ret;
14975 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14976 but after converting arguments to type_die, afterwards
14977 convert back to unsigned. */
14979 static dw_loc_descr_ref
14980 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14981 scalar_int_mode mode, machine_mode mem_mode)
14983 dw_loc_descr_ref cvt, op0, op1;
14985 if (type_die == NULL)
14986 return NULL;
14987 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14988 VAR_INIT_STATUS_INITIALIZED);
14989 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14990 VAR_INIT_STATUS_INITIALIZED);
14991 if (op0 == NULL || op1 == NULL)
14992 return NULL;
14993 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14994 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14995 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14996 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14997 add_loc_descr (&op0, cvt);
14998 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14999 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15000 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15001 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15002 add_loc_descr (&op1, cvt);
15003 add_loc_descr (&op0, op1);
15004 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15005 return convert_descriptor_to_mode (mode, op0);
15008 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15009 const0 is DW_OP_lit0 or corresponding typed constant,
15010 const1 is DW_OP_lit1 or corresponding typed constant
15011 and constMSB is constant with just the MSB bit set
15012 for the mode):
15013 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15014 L1: const0 DW_OP_swap
15015 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15016 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15017 L3: DW_OP_drop
15018 L4: DW_OP_nop
15020 CTZ is similar:
15021 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15022 L1: const0 DW_OP_swap
15023 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15024 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15025 L3: DW_OP_drop
15026 L4: DW_OP_nop
15028 FFS is similar:
15029 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15030 L1: const1 DW_OP_swap
15031 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15032 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15033 L3: DW_OP_drop
15034 L4: DW_OP_nop */
15036 static dw_loc_descr_ref
15037 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15038 machine_mode mem_mode)
15040 dw_loc_descr_ref op0, ret, tmp;
15041 HOST_WIDE_INT valv;
15042 dw_loc_descr_ref l1jump, l1label;
15043 dw_loc_descr_ref l2jump, l2label;
15044 dw_loc_descr_ref l3jump, l3label;
15045 dw_loc_descr_ref l4jump, l4label;
15046 rtx msb;
15048 if (GET_MODE (XEXP (rtl, 0)) != mode)
15049 return NULL;
15051 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15052 VAR_INIT_STATUS_INITIALIZED);
15053 if (op0 == NULL)
15054 return NULL;
15055 ret = op0;
15056 if (GET_CODE (rtl) == CLZ)
15058 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15059 valv = GET_MODE_BITSIZE (mode);
15061 else if (GET_CODE (rtl) == FFS)
15062 valv = 0;
15063 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15064 valv = GET_MODE_BITSIZE (mode);
15065 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15066 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15067 add_loc_descr (&ret, l1jump);
15068 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15069 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15070 VAR_INIT_STATUS_INITIALIZED);
15071 if (tmp == NULL)
15072 return NULL;
15073 add_loc_descr (&ret, tmp);
15074 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15075 add_loc_descr (&ret, l4jump);
15076 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15077 ? const1_rtx : const0_rtx,
15078 mode, mem_mode,
15079 VAR_INIT_STATUS_INITIALIZED);
15080 if (l1label == NULL)
15081 return NULL;
15082 add_loc_descr (&ret, l1label);
15083 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15084 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15085 add_loc_descr (&ret, l2label);
15086 if (GET_CODE (rtl) != CLZ)
15087 msb = const1_rtx;
15088 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15089 msb = GEN_INT (HOST_WIDE_INT_1U
15090 << (GET_MODE_BITSIZE (mode) - 1));
15091 else
15092 msb = immed_wide_int_const
15093 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15094 GET_MODE_PRECISION (mode)), mode);
15095 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15096 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15097 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15098 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15099 else
15100 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15101 VAR_INIT_STATUS_INITIALIZED);
15102 if (tmp == NULL)
15103 return NULL;
15104 add_loc_descr (&ret, tmp);
15105 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15106 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15107 add_loc_descr (&ret, l3jump);
15108 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15109 VAR_INIT_STATUS_INITIALIZED);
15110 if (tmp == NULL)
15111 return NULL;
15112 add_loc_descr (&ret, tmp);
15113 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15114 ? DW_OP_shl : DW_OP_shr, 0, 0));
15115 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15116 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15117 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15118 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15119 add_loc_descr (&ret, l2jump);
15120 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15121 add_loc_descr (&ret, l3label);
15122 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15123 add_loc_descr (&ret, l4label);
15124 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15125 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15126 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15127 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15128 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15129 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15130 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15131 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15132 return ret;
15135 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15136 const1 is DW_OP_lit1 or corresponding typed constant):
15137 const0 DW_OP_swap
15138 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15139 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15140 L2: DW_OP_drop
15142 PARITY is similar:
15143 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15144 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15145 L2: DW_OP_drop */
15147 static dw_loc_descr_ref
15148 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15149 machine_mode mem_mode)
15151 dw_loc_descr_ref op0, ret, tmp;
15152 dw_loc_descr_ref l1jump, l1label;
15153 dw_loc_descr_ref l2jump, l2label;
15155 if (GET_MODE (XEXP (rtl, 0)) != mode)
15156 return NULL;
15158 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15159 VAR_INIT_STATUS_INITIALIZED);
15160 if (op0 == NULL)
15161 return NULL;
15162 ret = op0;
15163 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15164 VAR_INIT_STATUS_INITIALIZED);
15165 if (tmp == NULL)
15166 return NULL;
15167 add_loc_descr (&ret, tmp);
15168 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15169 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15170 add_loc_descr (&ret, l1label);
15171 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15172 add_loc_descr (&ret, l2jump);
15173 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15174 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15175 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15176 VAR_INIT_STATUS_INITIALIZED);
15177 if (tmp == NULL)
15178 return NULL;
15179 add_loc_descr (&ret, tmp);
15180 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15181 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15182 ? DW_OP_plus : DW_OP_xor, 0, 0));
15183 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15184 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15185 VAR_INIT_STATUS_INITIALIZED);
15186 add_loc_descr (&ret, tmp);
15187 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15188 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15189 add_loc_descr (&ret, l1jump);
15190 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15191 add_loc_descr (&ret, l2label);
15192 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15193 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15194 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15195 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15196 return ret;
15199 /* BSWAP (constS is initial shift count, either 56 or 24):
15200 constS const0
15201 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15202 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15203 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15204 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15205 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15207 static dw_loc_descr_ref
15208 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15209 machine_mode mem_mode)
15211 dw_loc_descr_ref op0, ret, tmp;
15212 dw_loc_descr_ref l1jump, l1label;
15213 dw_loc_descr_ref l2jump, l2label;
15215 if (BITS_PER_UNIT != 8
15216 || (GET_MODE_BITSIZE (mode) != 32
15217 && GET_MODE_BITSIZE (mode) != 64))
15218 return NULL;
15220 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15221 VAR_INIT_STATUS_INITIALIZED);
15222 if (op0 == NULL)
15223 return NULL;
15225 ret = op0;
15226 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15227 mode, mem_mode,
15228 VAR_INIT_STATUS_INITIALIZED);
15229 if (tmp == NULL)
15230 return NULL;
15231 add_loc_descr (&ret, tmp);
15232 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15233 VAR_INIT_STATUS_INITIALIZED);
15234 if (tmp == NULL)
15235 return NULL;
15236 add_loc_descr (&ret, tmp);
15237 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15238 add_loc_descr (&ret, l1label);
15239 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15240 mode, mem_mode,
15241 VAR_INIT_STATUS_INITIALIZED);
15242 add_loc_descr (&ret, tmp);
15243 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15244 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15245 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15246 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15247 VAR_INIT_STATUS_INITIALIZED);
15248 if (tmp == NULL)
15249 return NULL;
15250 add_loc_descr (&ret, tmp);
15251 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15252 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15253 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15254 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15255 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15256 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15257 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15258 VAR_INIT_STATUS_INITIALIZED);
15259 add_loc_descr (&ret, tmp);
15260 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15261 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15262 add_loc_descr (&ret, l2jump);
15263 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15264 VAR_INIT_STATUS_INITIALIZED);
15265 add_loc_descr (&ret, tmp);
15266 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15267 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15268 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15269 add_loc_descr (&ret, l1jump);
15270 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15271 add_loc_descr (&ret, l2label);
15272 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15273 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15274 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15275 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15276 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15277 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15278 return ret;
15281 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15282 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15283 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15284 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15286 ROTATERT is similar:
15287 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15288 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15289 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15291 static dw_loc_descr_ref
15292 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15293 machine_mode mem_mode)
15295 rtx rtlop1 = XEXP (rtl, 1);
15296 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15297 int i;
15299 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15300 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15301 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15302 VAR_INIT_STATUS_INITIALIZED);
15303 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15304 VAR_INIT_STATUS_INITIALIZED);
15305 if (op0 == NULL || op1 == NULL)
15306 return NULL;
15307 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15308 for (i = 0; i < 2; i++)
15310 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15311 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15312 mode, mem_mode,
15313 VAR_INIT_STATUS_INITIALIZED);
15314 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15315 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15316 ? DW_OP_const4u
15317 : HOST_BITS_PER_WIDE_INT == 64
15318 ? DW_OP_const8u : DW_OP_constu,
15319 GET_MODE_MASK (mode), 0);
15320 else
15321 mask[i] = NULL;
15322 if (mask[i] == NULL)
15323 return NULL;
15324 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15326 ret = op0;
15327 add_loc_descr (&ret, op1);
15328 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15329 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15330 if (GET_CODE (rtl) == ROTATERT)
15332 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15333 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15334 GET_MODE_BITSIZE (mode), 0));
15336 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15337 if (mask[0] != NULL)
15338 add_loc_descr (&ret, mask[0]);
15339 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15340 if (mask[1] != NULL)
15342 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15343 add_loc_descr (&ret, mask[1]);
15344 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15346 if (GET_CODE (rtl) == ROTATE)
15348 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15349 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15350 GET_MODE_BITSIZE (mode), 0));
15352 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15353 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15354 return ret;
15357 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15358 for DEBUG_PARAMETER_REF RTL. */
15360 static dw_loc_descr_ref
15361 parameter_ref_descriptor (rtx rtl)
15363 dw_loc_descr_ref ret;
15364 dw_die_ref ref;
15366 if (dwarf_strict)
15367 return NULL;
15368 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15369 /* With LTO during LTRANS we get the late DIE that refers to the early
15370 DIE, thus we add another indirection here. This seems to confuse
15371 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15372 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15373 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15374 if (ref)
15376 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15377 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15378 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15380 else
15382 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15383 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15385 return ret;
15388 /* The following routine converts the RTL for a variable or parameter
15389 (resident in memory) into an equivalent Dwarf representation of a
15390 mechanism for getting the address of that same variable onto the top of a
15391 hypothetical "address evaluation" stack.
15393 When creating memory location descriptors, we are effectively transforming
15394 the RTL for a memory-resident object into its Dwarf postfix expression
15395 equivalent. This routine recursively descends an RTL tree, turning
15396 it into Dwarf postfix code as it goes.
15398 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15400 MEM_MODE is the mode of the memory reference, needed to handle some
15401 autoincrement addressing modes.
15403 Return 0 if we can't represent the location. */
15405 dw_loc_descr_ref
15406 mem_loc_descriptor (rtx rtl, machine_mode mode,
15407 machine_mode mem_mode,
15408 enum var_init_status initialized)
15410 dw_loc_descr_ref mem_loc_result = NULL;
15411 enum dwarf_location_atom op;
15412 dw_loc_descr_ref op0, op1;
15413 rtx inner = NULL_RTX;
15414 poly_int64 offset;
15416 if (mode == VOIDmode)
15417 mode = GET_MODE (rtl);
15419 /* Note that for a dynamically sized array, the location we will generate a
15420 description of here will be the lowest numbered location which is
15421 actually within the array. That's *not* necessarily the same as the
15422 zeroth element of the array. */
15424 rtl = targetm.delegitimize_address (rtl);
15426 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15427 return NULL;
15429 scalar_int_mode int_mode, inner_mode, op1_mode;
15430 switch (GET_CODE (rtl))
15432 case POST_INC:
15433 case POST_DEC:
15434 case POST_MODIFY:
15435 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15437 case SUBREG:
15438 /* The case of a subreg may arise when we have a local (register)
15439 variable or a formal (register) parameter which doesn't quite fill
15440 up an entire register. For now, just assume that it is
15441 legitimate to make the Dwarf info refer to the whole register which
15442 contains the given subreg. */
15443 if (!subreg_lowpart_p (rtl))
15444 break;
15445 inner = SUBREG_REG (rtl);
15446 /* FALLTHRU */
15447 case TRUNCATE:
15448 if (inner == NULL_RTX)
15449 inner = XEXP (rtl, 0);
15450 if (is_a <scalar_int_mode> (mode, &int_mode)
15451 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15452 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15453 #ifdef POINTERS_EXTEND_UNSIGNED
15454 || (int_mode == Pmode && mem_mode != VOIDmode)
15455 #endif
15457 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15459 mem_loc_result = mem_loc_descriptor (inner,
15460 inner_mode,
15461 mem_mode, initialized);
15462 break;
15464 if (dwarf_strict && dwarf_version < 5)
15465 break;
15466 if (is_a <scalar_int_mode> (mode, &int_mode)
15467 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15468 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15469 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15471 dw_die_ref type_die;
15472 dw_loc_descr_ref cvt;
15474 mem_loc_result = mem_loc_descriptor (inner,
15475 GET_MODE (inner),
15476 mem_mode, initialized);
15477 if (mem_loc_result == NULL)
15478 break;
15479 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15480 if (type_die == NULL)
15482 mem_loc_result = NULL;
15483 break;
15485 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15486 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15487 else
15488 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15489 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15490 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15491 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15492 add_loc_descr (&mem_loc_result, cvt);
15493 if (is_a <scalar_int_mode> (mode, &int_mode)
15494 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15496 /* Convert it to untyped afterwards. */
15497 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15498 add_loc_descr (&mem_loc_result, cvt);
15501 break;
15503 case REG:
15504 if (!is_a <scalar_int_mode> (mode, &int_mode)
15505 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15506 && rtl != arg_pointer_rtx
15507 && rtl != frame_pointer_rtx
15508 #ifdef POINTERS_EXTEND_UNSIGNED
15509 && (int_mode != Pmode || mem_mode == VOIDmode)
15510 #endif
15513 dw_die_ref type_die;
15514 unsigned int dbx_regnum;
15516 if (dwarf_strict && dwarf_version < 5)
15517 break;
15518 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
15519 break;
15520 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15521 if (type_die == NULL)
15522 break;
15524 dbx_regnum = dbx_reg_number (rtl);
15525 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15526 break;
15527 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15528 dbx_regnum, 0);
15529 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15530 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15531 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15532 break;
15534 /* Whenever a register number forms a part of the description of the
15535 method for calculating the (dynamic) address of a memory resident
15536 object, DWARF rules require the register number be referred to as
15537 a "base register". This distinction is not based in any way upon
15538 what category of register the hardware believes the given register
15539 belongs to. This is strictly DWARF terminology we're dealing with
15540 here. Note that in cases where the location of a memory-resident
15541 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15542 OP_CONST (0)) the actual DWARF location descriptor that we generate
15543 may just be OP_BASEREG (basereg). This may look deceptively like
15544 the object in question was allocated to a register (rather than in
15545 memory) so DWARF consumers need to be aware of the subtle
15546 distinction between OP_REG and OP_BASEREG. */
15547 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15548 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15549 else if (stack_realign_drap
15550 && crtl->drap_reg
15551 && crtl->args.internal_arg_pointer == rtl
15552 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15554 /* If RTL is internal_arg_pointer, which has been optimized
15555 out, use DRAP instead. */
15556 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15557 VAR_INIT_STATUS_INITIALIZED);
15559 break;
15561 case SIGN_EXTEND:
15562 case ZERO_EXTEND:
15563 if (!is_a <scalar_int_mode> (mode, &int_mode)
15564 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15565 break;
15566 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15567 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15568 if (op0 == 0)
15569 break;
15570 else if (GET_CODE (rtl) == ZERO_EXTEND
15571 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15572 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15573 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15574 to expand zero extend as two shifts instead of
15575 masking. */
15576 && GET_MODE_SIZE (inner_mode) <= 4)
15578 mem_loc_result = op0;
15579 add_loc_descr (&mem_loc_result,
15580 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15581 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15583 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15585 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15586 shift *= BITS_PER_UNIT;
15587 if (GET_CODE (rtl) == SIGN_EXTEND)
15588 op = DW_OP_shra;
15589 else
15590 op = DW_OP_shr;
15591 mem_loc_result = op0;
15592 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15593 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15594 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15595 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15597 else if (!dwarf_strict || dwarf_version >= 5)
15599 dw_die_ref type_die1, type_die2;
15600 dw_loc_descr_ref cvt;
15602 type_die1 = base_type_for_mode (inner_mode,
15603 GET_CODE (rtl) == ZERO_EXTEND);
15604 if (type_die1 == NULL)
15605 break;
15606 type_die2 = base_type_for_mode (int_mode, 1);
15607 if (type_die2 == NULL)
15608 break;
15609 mem_loc_result = op0;
15610 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15611 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15612 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
15613 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15614 add_loc_descr (&mem_loc_result, cvt);
15615 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15616 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15617 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
15618 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15619 add_loc_descr (&mem_loc_result, cvt);
15621 break;
15623 case MEM:
15625 rtx new_rtl = avoid_constant_pool_reference (rtl);
15626 if (new_rtl != rtl)
15628 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
15629 initialized);
15630 if (mem_loc_result != NULL)
15631 return mem_loc_result;
15634 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
15635 get_address_mode (rtl), mode,
15636 VAR_INIT_STATUS_INITIALIZED);
15637 if (mem_loc_result == NULL)
15638 mem_loc_result = tls_mem_loc_descriptor (rtl);
15639 if (mem_loc_result != NULL)
15641 if (!is_a <scalar_int_mode> (mode, &int_mode)
15642 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15644 dw_die_ref type_die;
15645 dw_loc_descr_ref deref;
15646 HOST_WIDE_INT size;
15648 if (dwarf_strict && dwarf_version < 5)
15649 return NULL;
15650 if (!GET_MODE_SIZE (mode).is_constant (&size))
15651 return NULL;
15652 type_die
15653 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15654 if (type_die == NULL)
15655 return NULL;
15656 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
15657 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15658 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15659 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
15660 add_loc_descr (&mem_loc_result, deref);
15662 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15663 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15664 else
15665 add_loc_descr (&mem_loc_result,
15666 new_loc_descr (DW_OP_deref_size,
15667 GET_MODE_SIZE (int_mode), 0));
15669 break;
15671 case LO_SUM:
15672 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15674 case LABEL_REF:
15675 /* Some ports can transform a symbol ref into a label ref, because
15676 the symbol ref is too far away and has to be dumped into a constant
15677 pool. */
15678 case CONST:
15679 case SYMBOL_REF:
15680 if (!is_a <scalar_int_mode> (mode, &int_mode)
15681 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15682 #ifdef POINTERS_EXTEND_UNSIGNED
15683 && (int_mode != Pmode || mem_mode == VOIDmode)
15684 #endif
15686 break;
15687 if (GET_CODE (rtl) == SYMBOL_REF
15688 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15690 dw_loc_descr_ref temp;
15692 /* If this is not defined, we have no way to emit the data. */
15693 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15694 break;
15696 temp = new_addr_loc_descr (rtl, dtprel_true);
15698 /* We check for DWARF 5 here because gdb did not implement
15699 DW_OP_form_tls_address until after 7.12. */
15700 mem_loc_result = new_loc_descr ((dwarf_version >= 5
15701 ? DW_OP_form_tls_address
15702 : DW_OP_GNU_push_tls_address),
15703 0, 0);
15704 add_loc_descr (&mem_loc_result, temp);
15706 break;
15709 if (!const_ok_for_output (rtl))
15711 if (GET_CODE (rtl) == CONST)
15712 switch (GET_CODE (XEXP (rtl, 0)))
15714 case NOT:
15715 op = DW_OP_not;
15716 goto try_const_unop;
15717 case NEG:
15718 op = DW_OP_neg;
15719 goto try_const_unop;
15720 try_const_unop:
15721 rtx arg;
15722 arg = XEXP (XEXP (rtl, 0), 0);
15723 if (!CONSTANT_P (arg))
15724 arg = gen_rtx_CONST (int_mode, arg);
15725 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
15726 initialized);
15727 if (op0)
15729 mem_loc_result = op0;
15730 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15732 break;
15733 default:
15734 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
15735 mem_mode, initialized);
15736 break;
15738 break;
15741 symref:
15742 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15743 vec_safe_push (used_rtx_array, rtl);
15744 break;
15746 case CONCAT:
15747 case CONCATN:
15748 case VAR_LOCATION:
15749 case DEBUG_IMPLICIT_PTR:
15750 expansion_failed (NULL_TREE, rtl,
15751 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15752 return 0;
15754 case ENTRY_VALUE:
15755 if (dwarf_strict && dwarf_version < 5)
15756 return NULL;
15757 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15759 if (!is_a <scalar_int_mode> (mode, &int_mode)
15760 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15761 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15762 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15763 else
15765 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15766 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15767 return NULL;
15768 op0 = one_reg_loc_descriptor (dbx_regnum,
15769 VAR_INIT_STATUS_INITIALIZED);
15772 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15773 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15775 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15776 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15777 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15778 return NULL;
15780 else
15781 gcc_unreachable ();
15782 if (op0 == NULL)
15783 return NULL;
15784 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15785 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15786 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15787 break;
15789 case DEBUG_PARAMETER_REF:
15790 mem_loc_result = parameter_ref_descriptor (rtl);
15791 break;
15793 case PRE_MODIFY:
15794 /* Extract the PLUS expression nested inside and fall into
15795 PLUS code below. */
15796 rtl = XEXP (rtl, 1);
15797 goto plus;
15799 case PRE_INC:
15800 case PRE_DEC:
15801 /* Turn these into a PLUS expression and fall into the PLUS code
15802 below. */
15803 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15804 gen_int_mode (GET_CODE (rtl) == PRE_INC
15805 ? GET_MODE_UNIT_SIZE (mem_mode)
15806 : -GET_MODE_UNIT_SIZE (mem_mode),
15807 mode));
15809 /* fall through */
15811 case PLUS:
15812 plus:
15813 if (is_based_loc (rtl)
15814 && is_a <scalar_int_mode> (mode, &int_mode)
15815 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15816 || XEXP (rtl, 0) == arg_pointer_rtx
15817 || XEXP (rtl, 0) == frame_pointer_rtx))
15818 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15819 INTVAL (XEXP (rtl, 1)),
15820 VAR_INIT_STATUS_INITIALIZED);
15821 else
15823 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15824 VAR_INIT_STATUS_INITIALIZED);
15825 if (mem_loc_result == 0)
15826 break;
15828 if (CONST_INT_P (XEXP (rtl, 1))
15829 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15830 <= DWARF2_ADDR_SIZE))
15831 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15832 else
15834 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15835 VAR_INIT_STATUS_INITIALIZED);
15836 if (op1 == 0)
15837 return NULL;
15838 add_loc_descr (&mem_loc_result, op1);
15839 add_loc_descr (&mem_loc_result,
15840 new_loc_descr (DW_OP_plus, 0, 0));
15843 break;
15845 /* If a pseudo-reg is optimized away, it is possible for it to
15846 be replaced with a MEM containing a multiply or shift. */
15847 case MINUS:
15848 op = DW_OP_minus;
15849 goto do_binop;
15851 case MULT:
15852 op = DW_OP_mul;
15853 goto do_binop;
15855 case DIV:
15856 if ((!dwarf_strict || dwarf_version >= 5)
15857 && is_a <scalar_int_mode> (mode, &int_mode)
15858 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15860 mem_loc_result = typed_binop (DW_OP_div, rtl,
15861 base_type_for_mode (mode, 0),
15862 int_mode, mem_mode);
15863 break;
15865 op = DW_OP_div;
15866 goto do_binop;
15868 case UMOD:
15869 op = DW_OP_mod;
15870 goto do_binop;
15872 case ASHIFT:
15873 op = DW_OP_shl;
15874 goto do_shift;
15876 case ASHIFTRT:
15877 op = DW_OP_shra;
15878 goto do_shift;
15880 case LSHIFTRT:
15881 op = DW_OP_shr;
15882 goto do_shift;
15884 do_shift:
15885 if (!is_a <scalar_int_mode> (mode, &int_mode))
15886 break;
15887 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15888 VAR_INIT_STATUS_INITIALIZED);
15890 rtx rtlop1 = XEXP (rtl, 1);
15891 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15892 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15893 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15894 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15895 VAR_INIT_STATUS_INITIALIZED);
15898 if (op0 == 0 || op1 == 0)
15899 break;
15901 mem_loc_result = op0;
15902 add_loc_descr (&mem_loc_result, op1);
15903 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15904 break;
15906 case AND:
15907 op = DW_OP_and;
15908 goto do_binop;
15910 case IOR:
15911 op = DW_OP_or;
15912 goto do_binop;
15914 case XOR:
15915 op = DW_OP_xor;
15916 goto do_binop;
15918 do_binop:
15919 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15920 VAR_INIT_STATUS_INITIALIZED);
15921 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15922 VAR_INIT_STATUS_INITIALIZED);
15924 if (op0 == 0 || op1 == 0)
15925 break;
15927 mem_loc_result = op0;
15928 add_loc_descr (&mem_loc_result, op1);
15929 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15930 break;
15932 case MOD:
15933 if ((!dwarf_strict || dwarf_version >= 5)
15934 && is_a <scalar_int_mode> (mode, &int_mode)
15935 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15937 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15938 base_type_for_mode (mode, 0),
15939 int_mode, mem_mode);
15940 break;
15943 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15944 VAR_INIT_STATUS_INITIALIZED);
15945 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15946 VAR_INIT_STATUS_INITIALIZED);
15948 if (op0 == 0 || op1 == 0)
15949 break;
15951 mem_loc_result = op0;
15952 add_loc_descr (&mem_loc_result, op1);
15953 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15954 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15955 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15956 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15957 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15958 break;
15960 case UDIV:
15961 if ((!dwarf_strict || dwarf_version >= 5)
15962 && is_a <scalar_int_mode> (mode, &int_mode))
15964 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15966 op = DW_OP_div;
15967 goto do_binop;
15969 mem_loc_result = typed_binop (DW_OP_div, rtl,
15970 base_type_for_mode (int_mode, 1),
15971 int_mode, mem_mode);
15973 break;
15975 case NOT:
15976 op = DW_OP_not;
15977 goto do_unop;
15979 case ABS:
15980 op = DW_OP_abs;
15981 goto do_unop;
15983 case NEG:
15984 op = DW_OP_neg;
15985 goto do_unop;
15987 do_unop:
15988 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15989 VAR_INIT_STATUS_INITIALIZED);
15991 if (op0 == 0)
15992 break;
15994 mem_loc_result = op0;
15995 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15996 break;
15998 case CONST_INT:
15999 if (!is_a <scalar_int_mode> (mode, &int_mode)
16000 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16001 #ifdef POINTERS_EXTEND_UNSIGNED
16002 || (int_mode == Pmode
16003 && mem_mode != VOIDmode
16004 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16005 #endif
16008 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16009 break;
16011 if ((!dwarf_strict || dwarf_version >= 5)
16012 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16013 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16015 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16016 scalar_int_mode amode;
16017 if (type_die == NULL)
16018 return NULL;
16019 if (INTVAL (rtl) >= 0
16020 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16021 .exists (&amode))
16022 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16023 /* const DW_OP_convert <XXX> vs.
16024 DW_OP_const_type <XXX, 1, const>. */
16025 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16026 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16028 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16029 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16030 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16031 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16032 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16033 add_loc_descr (&mem_loc_result, op0);
16034 return mem_loc_result;
16036 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16037 INTVAL (rtl));
16038 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16039 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16040 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16041 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16042 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16043 else
16045 mem_loc_result->dw_loc_oprnd2.val_class
16046 = dw_val_class_const_double;
16047 mem_loc_result->dw_loc_oprnd2.v.val_double
16048 = double_int::from_shwi (INTVAL (rtl));
16051 break;
16053 case CONST_DOUBLE:
16054 if (!dwarf_strict || dwarf_version >= 5)
16056 dw_die_ref type_die;
16058 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16059 CONST_DOUBLE rtx could represent either a large integer
16060 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16061 the value is always a floating point constant.
16063 When it is an integer, a CONST_DOUBLE is used whenever
16064 the constant requires 2 HWIs to be adequately represented.
16065 We output CONST_DOUBLEs as blocks. */
16066 if (mode == VOIDmode
16067 || (GET_MODE (rtl) == VOIDmode
16068 && maybe_ne (GET_MODE_BITSIZE (mode),
16069 HOST_BITS_PER_DOUBLE_INT)))
16070 break;
16071 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16072 if (type_die == NULL)
16073 return NULL;
16074 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16075 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16076 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16077 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16078 #if TARGET_SUPPORTS_WIDE_INT == 0
16079 if (!SCALAR_FLOAT_MODE_P (mode))
16081 mem_loc_result->dw_loc_oprnd2.val_class
16082 = dw_val_class_const_double;
16083 mem_loc_result->dw_loc_oprnd2.v.val_double
16084 = rtx_to_double_int (rtl);
16086 else
16087 #endif
16089 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16090 unsigned int length = GET_MODE_SIZE (float_mode);
16091 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16093 insert_float (rtl, array);
16094 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16095 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16096 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16097 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16100 break;
16102 case CONST_WIDE_INT:
16103 if (!dwarf_strict || dwarf_version >= 5)
16105 dw_die_ref type_die;
16107 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16108 if (type_die == NULL)
16109 return NULL;
16110 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16111 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16112 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16113 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16114 mem_loc_result->dw_loc_oprnd2.val_class
16115 = dw_val_class_wide_int;
16116 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16117 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16119 break;
16121 case CONST_POLY_INT:
16122 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16123 break;
16125 case EQ:
16126 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16127 break;
16129 case GE:
16130 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16131 break;
16133 case GT:
16134 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16135 break;
16137 case LE:
16138 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16139 break;
16141 case LT:
16142 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16143 break;
16145 case NE:
16146 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16147 break;
16149 case GEU:
16150 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16151 break;
16153 case GTU:
16154 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16155 break;
16157 case LEU:
16158 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16159 break;
16161 case LTU:
16162 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16163 break;
16165 case UMIN:
16166 case UMAX:
16167 if (!SCALAR_INT_MODE_P (mode))
16168 break;
16169 /* FALLTHRU */
16170 case SMIN:
16171 case SMAX:
16172 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16173 break;
16175 case ZERO_EXTRACT:
16176 case SIGN_EXTRACT:
16177 if (CONST_INT_P (XEXP (rtl, 1))
16178 && CONST_INT_P (XEXP (rtl, 2))
16179 && is_a <scalar_int_mode> (mode, &int_mode)
16180 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16181 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16182 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16183 && ((unsigned) INTVAL (XEXP (rtl, 1))
16184 + (unsigned) INTVAL (XEXP (rtl, 2))
16185 <= GET_MODE_BITSIZE (int_mode)))
16187 int shift, size;
16188 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16189 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16190 if (op0 == 0)
16191 break;
16192 if (GET_CODE (rtl) == SIGN_EXTRACT)
16193 op = DW_OP_shra;
16194 else
16195 op = DW_OP_shr;
16196 mem_loc_result = op0;
16197 size = INTVAL (XEXP (rtl, 1));
16198 shift = INTVAL (XEXP (rtl, 2));
16199 if (BITS_BIG_ENDIAN)
16200 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16201 if (shift + size != (int) DWARF2_ADDR_SIZE)
16203 add_loc_descr (&mem_loc_result,
16204 int_loc_descriptor (DWARF2_ADDR_SIZE
16205 - shift - size));
16206 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16208 if (size != (int) DWARF2_ADDR_SIZE)
16210 add_loc_descr (&mem_loc_result,
16211 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16212 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16215 break;
16217 case IF_THEN_ELSE:
16219 dw_loc_descr_ref op2, bra_node, drop_node;
16220 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16221 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16222 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16223 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16224 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16225 VAR_INIT_STATUS_INITIALIZED);
16226 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16227 VAR_INIT_STATUS_INITIALIZED);
16228 if (op0 == NULL || op1 == NULL || op2 == NULL)
16229 break;
16231 mem_loc_result = op1;
16232 add_loc_descr (&mem_loc_result, op2);
16233 add_loc_descr (&mem_loc_result, op0);
16234 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16235 add_loc_descr (&mem_loc_result, bra_node);
16236 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16237 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16238 add_loc_descr (&mem_loc_result, drop_node);
16239 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16240 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16242 break;
16244 case FLOAT_EXTEND:
16245 case FLOAT_TRUNCATE:
16246 case FLOAT:
16247 case UNSIGNED_FLOAT:
16248 case FIX:
16249 case UNSIGNED_FIX:
16250 if (!dwarf_strict || dwarf_version >= 5)
16252 dw_die_ref type_die;
16253 dw_loc_descr_ref cvt;
16255 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16256 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16257 if (op0 == NULL)
16258 break;
16259 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16260 && (GET_CODE (rtl) == FLOAT
16261 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16263 type_die = base_type_for_mode (int_mode,
16264 GET_CODE (rtl) == UNSIGNED_FLOAT);
16265 if (type_die == NULL)
16266 break;
16267 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16268 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16269 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16270 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16271 add_loc_descr (&op0, cvt);
16273 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16274 if (type_die == NULL)
16275 break;
16276 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16277 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16278 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16279 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16280 add_loc_descr (&op0, cvt);
16281 if (is_a <scalar_int_mode> (mode, &int_mode)
16282 && (GET_CODE (rtl) == FIX
16283 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16285 op0 = convert_descriptor_to_mode (int_mode, op0);
16286 if (op0 == NULL)
16287 break;
16289 mem_loc_result = op0;
16291 break;
16293 case CLZ:
16294 case CTZ:
16295 case FFS:
16296 if (is_a <scalar_int_mode> (mode, &int_mode))
16297 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16298 break;
16300 case POPCOUNT:
16301 case PARITY:
16302 if (is_a <scalar_int_mode> (mode, &int_mode))
16303 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16304 break;
16306 case BSWAP:
16307 if (is_a <scalar_int_mode> (mode, &int_mode))
16308 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16309 break;
16311 case ROTATE:
16312 case ROTATERT:
16313 if (is_a <scalar_int_mode> (mode, &int_mode))
16314 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16315 break;
16317 case COMPARE:
16318 /* In theory, we could implement the above. */
16319 /* DWARF cannot represent the unsigned compare operations
16320 natively. */
16321 case SS_MULT:
16322 case US_MULT:
16323 case SS_DIV:
16324 case US_DIV:
16325 case SS_PLUS:
16326 case US_PLUS:
16327 case SS_MINUS:
16328 case US_MINUS:
16329 case SS_NEG:
16330 case US_NEG:
16331 case SS_ABS:
16332 case SS_ASHIFT:
16333 case US_ASHIFT:
16334 case SS_TRUNCATE:
16335 case US_TRUNCATE:
16336 case UNORDERED:
16337 case ORDERED:
16338 case UNEQ:
16339 case UNGE:
16340 case UNGT:
16341 case UNLE:
16342 case UNLT:
16343 case LTGT:
16344 case FRACT_CONVERT:
16345 case UNSIGNED_FRACT_CONVERT:
16346 case SAT_FRACT:
16347 case UNSIGNED_SAT_FRACT:
16348 case SQRT:
16349 case ASM_OPERANDS:
16350 case VEC_MERGE:
16351 case VEC_SELECT:
16352 case VEC_CONCAT:
16353 case VEC_DUPLICATE:
16354 case VEC_SERIES:
16355 case UNSPEC:
16356 case HIGH:
16357 case FMA:
16358 case STRICT_LOW_PART:
16359 case CONST_VECTOR:
16360 case CONST_FIXED:
16361 case CLRSB:
16362 case CLOBBER:
16363 case CLOBBER_HIGH:
16364 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16365 can't express it in the debug info. This can happen e.g. with some
16366 TLS UNSPECs. */
16367 break;
16369 case CONST_STRING:
16370 resolve_one_addr (&rtl);
16371 goto symref;
16373 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16374 the expression. An UNSPEC rtx represents a raw DWARF operation,
16375 new_loc_descr is called for it to build the operation directly.
16376 Otherwise mem_loc_descriptor is called recursively. */
16377 case PARALLEL:
16379 int index = 0;
16380 dw_loc_descr_ref exp_result = NULL;
16382 for (; index < XVECLEN (rtl, 0); index++)
16384 rtx elem = XVECEXP (rtl, 0, index);
16385 if (GET_CODE (elem) == UNSPEC)
16387 /* Each DWARF operation UNSPEC contain two operands, if
16388 one operand is not used for the operation, const0_rtx is
16389 passed. */
16390 gcc_assert (XVECLEN (elem, 0) == 2);
16392 HOST_WIDE_INT dw_op = XINT (elem, 1);
16393 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16394 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16395 exp_result
16396 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16397 oprnd2);
16399 else
16400 exp_result
16401 = mem_loc_descriptor (elem, mode, mem_mode,
16402 VAR_INIT_STATUS_INITIALIZED);
16404 if (!mem_loc_result)
16405 mem_loc_result = exp_result;
16406 else
16407 add_loc_descr (&mem_loc_result, exp_result);
16410 break;
16413 default:
16414 if (flag_checking)
16416 print_rtl (stderr, rtl);
16417 gcc_unreachable ();
16419 break;
16422 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16423 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16425 return mem_loc_result;
16428 /* Return a descriptor that describes the concatenation of two locations.
16429 This is typically a complex variable. */
16431 static dw_loc_descr_ref
16432 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16434 /* At present we only track constant-sized pieces. */
16435 unsigned int size0, size1;
16436 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16437 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16438 return 0;
16440 dw_loc_descr_ref cc_loc_result = NULL;
16441 dw_loc_descr_ref x0_ref
16442 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16443 dw_loc_descr_ref x1_ref
16444 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16446 if (x0_ref == 0 || x1_ref == 0)
16447 return 0;
16449 cc_loc_result = x0_ref;
16450 add_loc_descr_op_piece (&cc_loc_result, size0);
16452 add_loc_descr (&cc_loc_result, x1_ref);
16453 add_loc_descr_op_piece (&cc_loc_result, size1);
16455 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16456 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16458 return cc_loc_result;
16461 /* Return a descriptor that describes the concatenation of N
16462 locations. */
16464 static dw_loc_descr_ref
16465 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16467 unsigned int i;
16468 dw_loc_descr_ref cc_loc_result = NULL;
16469 unsigned int n = XVECLEN (concatn, 0);
16470 unsigned int size;
16472 for (i = 0; i < n; ++i)
16474 dw_loc_descr_ref ref;
16475 rtx x = XVECEXP (concatn, 0, i);
16477 /* At present we only track constant-sized pieces. */
16478 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16479 return NULL;
16481 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16482 if (ref == NULL)
16483 return NULL;
16485 add_loc_descr (&cc_loc_result, ref);
16486 add_loc_descr_op_piece (&cc_loc_result, size);
16489 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16490 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16492 return cc_loc_result;
16495 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16496 for DEBUG_IMPLICIT_PTR RTL. */
16498 static dw_loc_descr_ref
16499 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16501 dw_loc_descr_ref ret;
16502 dw_die_ref ref;
16504 if (dwarf_strict && dwarf_version < 5)
16505 return NULL;
16506 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16507 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16508 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16509 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16510 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16511 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16512 if (ref)
16514 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16515 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16516 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16518 else
16520 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16521 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16523 return ret;
16526 /* Output a proper Dwarf location descriptor for a variable or parameter
16527 which is either allocated in a register or in a memory location. For a
16528 register, we just generate an OP_REG and the register number. For a
16529 memory location we provide a Dwarf postfix expression describing how to
16530 generate the (dynamic) address of the object onto the address stack.
16532 MODE is mode of the decl if this loc_descriptor is going to be used in
16533 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16534 allowed, VOIDmode otherwise.
16536 If we don't know how to describe it, return 0. */
16538 static dw_loc_descr_ref
16539 loc_descriptor (rtx rtl, machine_mode mode,
16540 enum var_init_status initialized)
16542 dw_loc_descr_ref loc_result = NULL;
16543 scalar_int_mode int_mode;
16545 switch (GET_CODE (rtl))
16547 case SUBREG:
16548 /* The case of a subreg may arise when we have a local (register)
16549 variable or a formal (register) parameter which doesn't quite fill
16550 up an entire register. For now, just assume that it is
16551 legitimate to make the Dwarf info refer to the whole register which
16552 contains the given subreg. */
16553 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16554 loc_result = loc_descriptor (SUBREG_REG (rtl),
16555 GET_MODE (SUBREG_REG (rtl)), initialized);
16556 else
16557 goto do_default;
16558 break;
16560 case REG:
16561 loc_result = reg_loc_descriptor (rtl, initialized);
16562 break;
16564 case MEM:
16565 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16566 GET_MODE (rtl), initialized);
16567 if (loc_result == NULL)
16568 loc_result = tls_mem_loc_descriptor (rtl);
16569 if (loc_result == NULL)
16571 rtx new_rtl = avoid_constant_pool_reference (rtl);
16572 if (new_rtl != rtl)
16573 loc_result = loc_descriptor (new_rtl, mode, initialized);
16575 break;
16577 case CONCAT:
16578 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
16579 initialized);
16580 break;
16582 case CONCATN:
16583 loc_result = concatn_loc_descriptor (rtl, initialized);
16584 break;
16586 case VAR_LOCATION:
16587 /* Single part. */
16588 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
16590 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
16591 if (GET_CODE (loc) == EXPR_LIST)
16592 loc = XEXP (loc, 0);
16593 loc_result = loc_descriptor (loc, mode, initialized);
16594 break;
16597 rtl = XEXP (rtl, 1);
16598 /* FALLTHRU */
16600 case PARALLEL:
16602 rtvec par_elems = XVEC (rtl, 0);
16603 int num_elem = GET_NUM_ELEM (par_elems);
16604 machine_mode mode;
16605 int i, size;
16607 /* Create the first one, so we have something to add to. */
16608 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
16609 VOIDmode, initialized);
16610 if (loc_result == NULL)
16611 return NULL;
16612 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
16613 /* At present we only track constant-sized pieces. */
16614 if (!GET_MODE_SIZE (mode).is_constant (&size))
16615 return NULL;
16616 add_loc_descr_op_piece (&loc_result, size);
16617 for (i = 1; i < num_elem; i++)
16619 dw_loc_descr_ref temp;
16621 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
16622 VOIDmode, initialized);
16623 if (temp == NULL)
16624 return NULL;
16625 add_loc_descr (&loc_result, temp);
16626 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
16627 /* At present we only track constant-sized pieces. */
16628 if (!GET_MODE_SIZE (mode).is_constant (&size))
16629 return NULL;
16630 add_loc_descr_op_piece (&loc_result, size);
16633 break;
16635 case CONST_INT:
16636 if (mode != VOIDmode && mode != BLKmode)
16638 int_mode = as_a <scalar_int_mode> (mode);
16639 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
16640 INTVAL (rtl));
16642 break;
16644 case CONST_DOUBLE:
16645 if (mode == VOIDmode)
16646 mode = GET_MODE (rtl);
16648 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16650 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16652 /* Note that a CONST_DOUBLE rtx could represent either an integer
16653 or a floating-point constant. A CONST_DOUBLE is used whenever
16654 the constant requires more than one word in order to be
16655 adequately represented. We output CONST_DOUBLEs as blocks. */
16656 scalar_mode smode = as_a <scalar_mode> (mode);
16657 loc_result = new_loc_descr (DW_OP_implicit_value,
16658 GET_MODE_SIZE (smode), 0);
16659 #if TARGET_SUPPORTS_WIDE_INT == 0
16660 if (!SCALAR_FLOAT_MODE_P (smode))
16662 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
16663 loc_result->dw_loc_oprnd2.v.val_double
16664 = rtx_to_double_int (rtl);
16666 else
16667 #endif
16669 unsigned int length = GET_MODE_SIZE (smode);
16670 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16672 insert_float (rtl, array);
16673 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16674 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16675 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16676 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16679 break;
16681 case CONST_WIDE_INT:
16682 if (mode == VOIDmode)
16683 mode = GET_MODE (rtl);
16685 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16687 int_mode = as_a <scalar_int_mode> (mode);
16688 loc_result = new_loc_descr (DW_OP_implicit_value,
16689 GET_MODE_SIZE (int_mode), 0);
16690 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16691 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16692 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
16694 break;
16696 case CONST_VECTOR:
16697 if (mode == VOIDmode)
16698 mode = GET_MODE (rtl);
16700 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16702 unsigned int length;
16703 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
16704 return NULL;
16706 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16707 unsigned char *array
16708 = ggc_vec_alloc<unsigned char> (length * elt_size);
16709 unsigned int i;
16710 unsigned char *p;
16711 machine_mode imode = GET_MODE_INNER (mode);
16713 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16714 switch (GET_MODE_CLASS (mode))
16716 case MODE_VECTOR_INT:
16717 for (i = 0, p = array; i < length; i++, p += elt_size)
16719 rtx elt = CONST_VECTOR_ELT (rtl, i);
16720 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16722 break;
16724 case MODE_VECTOR_FLOAT:
16725 for (i = 0, p = array; i < length; i++, p += elt_size)
16727 rtx elt = CONST_VECTOR_ELT (rtl, i);
16728 insert_float (elt, p);
16730 break;
16732 default:
16733 gcc_unreachable ();
16736 loc_result = new_loc_descr (DW_OP_implicit_value,
16737 length * elt_size, 0);
16738 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16739 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16740 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16741 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16743 break;
16745 case CONST:
16746 if (mode == VOIDmode
16747 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16748 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16749 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16751 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16752 break;
16754 /* FALLTHROUGH */
16755 case SYMBOL_REF:
16756 if (!const_ok_for_output (rtl))
16757 break;
16758 /* FALLTHROUGH */
16759 case LABEL_REF:
16760 if (is_a <scalar_int_mode> (mode, &int_mode)
16761 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
16762 && (dwarf_version >= 4 || !dwarf_strict))
16764 loc_result = new_addr_loc_descr (rtl, dtprel_false);
16765 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16766 vec_safe_push (used_rtx_array, rtl);
16768 break;
16770 case DEBUG_IMPLICIT_PTR:
16771 loc_result = implicit_ptr_descriptor (rtl, 0);
16772 break;
16774 case PLUS:
16775 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16776 && CONST_INT_P (XEXP (rtl, 1)))
16778 loc_result
16779 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16780 break;
16782 /* FALLTHRU */
16783 do_default:
16784 default:
16785 if ((is_a <scalar_int_mode> (mode, &int_mode)
16786 && GET_MODE (rtl) == int_mode
16787 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16788 && dwarf_version >= 4)
16789 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16791 /* Value expression. */
16792 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16793 if (loc_result)
16794 add_loc_descr (&loc_result,
16795 new_loc_descr (DW_OP_stack_value, 0, 0));
16797 break;
16800 return loc_result;
16803 /* We need to figure out what section we should use as the base for the
16804 address ranges where a given location is valid.
16805 1. If this particular DECL has a section associated with it, use that.
16806 2. If this function has a section associated with it, use that.
16807 3. Otherwise, use the text section.
16808 XXX: If you split a variable across multiple sections, we won't notice. */
16810 static const char *
16811 secname_for_decl (const_tree decl)
16813 const char *secname;
16815 if (VAR_OR_FUNCTION_DECL_P (decl)
16816 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16817 && DECL_SECTION_NAME (decl))
16818 secname = DECL_SECTION_NAME (decl);
16819 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16820 secname = DECL_SECTION_NAME (current_function_decl);
16821 else if (cfun && in_cold_section_p)
16822 secname = crtl->subsections.cold_section_label;
16823 else
16824 secname = text_section_label;
16826 return secname;
16829 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16831 static bool
16832 decl_by_reference_p (tree decl)
16834 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16835 || VAR_P (decl))
16836 && DECL_BY_REFERENCE (decl));
16839 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16840 for VARLOC. */
16842 static dw_loc_descr_ref
16843 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16844 enum var_init_status initialized)
16846 int have_address = 0;
16847 dw_loc_descr_ref descr;
16848 machine_mode mode;
16850 if (want_address != 2)
16852 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16853 /* Single part. */
16854 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16856 varloc = PAT_VAR_LOCATION_LOC (varloc);
16857 if (GET_CODE (varloc) == EXPR_LIST)
16858 varloc = XEXP (varloc, 0);
16859 mode = GET_MODE (varloc);
16860 if (MEM_P (varloc))
16862 rtx addr = XEXP (varloc, 0);
16863 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16864 mode, initialized);
16865 if (descr)
16866 have_address = 1;
16867 else
16869 rtx x = avoid_constant_pool_reference (varloc);
16870 if (x != varloc)
16871 descr = mem_loc_descriptor (x, mode, VOIDmode,
16872 initialized);
16875 else
16876 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16878 else
16879 return 0;
16881 else
16883 if (GET_CODE (varloc) == VAR_LOCATION)
16884 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16885 else
16886 mode = DECL_MODE (loc);
16887 descr = loc_descriptor (varloc, mode, initialized);
16888 have_address = 1;
16891 if (!descr)
16892 return 0;
16894 if (want_address == 2 && !have_address
16895 && (dwarf_version >= 4 || !dwarf_strict))
16897 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16899 expansion_failed (loc, NULL_RTX,
16900 "DWARF address size mismatch");
16901 return 0;
16903 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16904 have_address = 1;
16906 /* Show if we can't fill the request for an address. */
16907 if (want_address && !have_address)
16909 expansion_failed (loc, NULL_RTX,
16910 "Want address and only have value");
16911 return 0;
16914 /* If we've got an address and don't want one, dereference. */
16915 if (!want_address && have_address)
16917 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16918 enum dwarf_location_atom op;
16920 if (size > DWARF2_ADDR_SIZE || size == -1)
16922 expansion_failed (loc, NULL_RTX,
16923 "DWARF address size mismatch");
16924 return 0;
16926 else if (size == DWARF2_ADDR_SIZE)
16927 op = DW_OP_deref;
16928 else
16929 op = DW_OP_deref_size;
16931 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16934 return descr;
16937 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16938 if it is not possible. */
16940 static dw_loc_descr_ref
16941 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16943 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16944 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16945 else if (dwarf_version >= 3 || !dwarf_strict)
16946 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16947 else
16948 return NULL;
16951 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16952 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16954 static dw_loc_descr_ref
16955 dw_sra_loc_expr (tree decl, rtx loc)
16957 rtx p;
16958 unsigned HOST_WIDE_INT padsize = 0;
16959 dw_loc_descr_ref descr, *descr_tail;
16960 unsigned HOST_WIDE_INT decl_size;
16961 rtx varloc;
16962 enum var_init_status initialized;
16964 if (DECL_SIZE (decl) == NULL
16965 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16966 return NULL;
16968 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16969 descr = NULL;
16970 descr_tail = &descr;
16972 for (p = loc; p; p = XEXP (p, 1))
16974 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16975 rtx loc_note = *decl_piece_varloc_ptr (p);
16976 dw_loc_descr_ref cur_descr;
16977 dw_loc_descr_ref *tail, last = NULL;
16978 unsigned HOST_WIDE_INT opsize = 0;
16980 if (loc_note == NULL_RTX
16981 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16983 padsize += bitsize;
16984 continue;
16986 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16987 varloc = NOTE_VAR_LOCATION (loc_note);
16988 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16989 if (cur_descr == NULL)
16991 padsize += bitsize;
16992 continue;
16995 /* Check that cur_descr either doesn't use
16996 DW_OP_*piece operations, or their sum is equal
16997 to bitsize. Otherwise we can't embed it. */
16998 for (tail = &cur_descr; *tail != NULL;
16999 tail = &(*tail)->dw_loc_next)
17000 if ((*tail)->dw_loc_opc == DW_OP_piece)
17002 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17003 * BITS_PER_UNIT;
17004 last = *tail;
17006 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17008 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17009 last = *tail;
17012 if (last != NULL && opsize != bitsize)
17014 padsize += bitsize;
17015 /* Discard the current piece of the descriptor and release any
17016 addr_table entries it uses. */
17017 remove_loc_list_addr_table_entries (cur_descr);
17018 continue;
17021 /* If there is a hole, add DW_OP_*piece after empty DWARF
17022 expression, which means that those bits are optimized out. */
17023 if (padsize)
17025 if (padsize > decl_size)
17027 remove_loc_list_addr_table_entries (cur_descr);
17028 goto discard_descr;
17030 decl_size -= padsize;
17031 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17032 if (*descr_tail == NULL)
17034 remove_loc_list_addr_table_entries (cur_descr);
17035 goto discard_descr;
17037 descr_tail = &(*descr_tail)->dw_loc_next;
17038 padsize = 0;
17040 *descr_tail = cur_descr;
17041 descr_tail = tail;
17042 if (bitsize > decl_size)
17043 goto discard_descr;
17044 decl_size -= bitsize;
17045 if (last == NULL)
17047 HOST_WIDE_INT offset = 0;
17048 if (GET_CODE (varloc) == VAR_LOCATION
17049 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17051 varloc = PAT_VAR_LOCATION_LOC (varloc);
17052 if (GET_CODE (varloc) == EXPR_LIST)
17053 varloc = XEXP (varloc, 0);
17057 if (GET_CODE (varloc) == CONST
17058 || GET_CODE (varloc) == SIGN_EXTEND
17059 || GET_CODE (varloc) == ZERO_EXTEND)
17060 varloc = XEXP (varloc, 0);
17061 else if (GET_CODE (varloc) == SUBREG)
17062 varloc = SUBREG_REG (varloc);
17063 else
17064 break;
17066 while (1);
17067 /* DW_OP_bit_size offset should be zero for register
17068 or implicit location descriptions and empty location
17069 descriptions, but for memory addresses needs big endian
17070 adjustment. */
17071 if (MEM_P (varloc))
17073 unsigned HOST_WIDE_INT memsize;
17074 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17075 goto discard_descr;
17076 memsize *= BITS_PER_UNIT;
17077 if (memsize != bitsize)
17079 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17080 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17081 goto discard_descr;
17082 if (memsize < bitsize)
17083 goto discard_descr;
17084 if (BITS_BIG_ENDIAN)
17085 offset = memsize - bitsize;
17089 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17090 if (*descr_tail == NULL)
17091 goto discard_descr;
17092 descr_tail = &(*descr_tail)->dw_loc_next;
17096 /* If there were any non-empty expressions, add padding till the end of
17097 the decl. */
17098 if (descr != NULL && decl_size != 0)
17100 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17101 if (*descr_tail == NULL)
17102 goto discard_descr;
17104 return descr;
17106 discard_descr:
17107 /* Discard the descriptor and release any addr_table entries it uses. */
17108 remove_loc_list_addr_table_entries (descr);
17109 return NULL;
17112 /* Return the dwarf representation of the location list LOC_LIST of
17113 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17114 function. */
17116 static dw_loc_list_ref
17117 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17119 const char *endname, *secname;
17120 var_loc_view endview;
17121 rtx varloc;
17122 enum var_init_status initialized;
17123 struct var_loc_node *node;
17124 dw_loc_descr_ref descr;
17125 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17126 dw_loc_list_ref list = NULL;
17127 dw_loc_list_ref *listp = &list;
17129 /* Now that we know what section we are using for a base,
17130 actually construct the list of locations.
17131 The first location information is what is passed to the
17132 function that creates the location list, and the remaining
17133 locations just get added on to that list.
17134 Note that we only know the start address for a location
17135 (IE location changes), so to build the range, we use
17136 the range [current location start, next location start].
17137 This means we have to special case the last node, and generate
17138 a range of [last location start, end of function label]. */
17140 if (cfun && crtl->has_bb_partition)
17142 bool save_in_cold_section_p = in_cold_section_p;
17143 in_cold_section_p = first_function_block_is_cold;
17144 if (loc_list->last_before_switch == NULL)
17145 in_cold_section_p = !in_cold_section_p;
17146 secname = secname_for_decl (decl);
17147 in_cold_section_p = save_in_cold_section_p;
17149 else
17150 secname = secname_for_decl (decl);
17152 for (node = loc_list->first; node; node = node->next)
17154 bool range_across_switch = false;
17155 if (GET_CODE (node->loc) == EXPR_LIST
17156 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17158 if (GET_CODE (node->loc) == EXPR_LIST)
17160 descr = NULL;
17161 /* This requires DW_OP_{,bit_}piece, which is not usable
17162 inside DWARF expressions. */
17163 if (want_address == 2)
17164 descr = dw_sra_loc_expr (decl, node->loc);
17166 else
17168 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17169 varloc = NOTE_VAR_LOCATION (node->loc);
17170 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17172 if (descr)
17174 /* If section switch happens in between node->label
17175 and node->next->label (or end of function) and
17176 we can't emit it as a single entry list,
17177 emit two ranges, first one ending at the end
17178 of first partition and second one starting at the
17179 beginning of second partition. */
17180 if (node == loc_list->last_before_switch
17181 && (node != loc_list->first || loc_list->first->next
17182 /* If we are to emit a view number, we will emit
17183 a loclist rather than a single location
17184 expression for the entire function (see
17185 loc_list_has_views), so we have to split the
17186 range that straddles across partitions. */
17187 || !ZERO_VIEW_P (node->view))
17188 && current_function_decl)
17190 endname = cfun->fde->dw_fde_end;
17191 endview = 0;
17192 range_across_switch = true;
17194 /* The variable has a location between NODE->LABEL and
17195 NODE->NEXT->LABEL. */
17196 else if (node->next)
17197 endname = node->next->label, endview = node->next->view;
17198 /* If the variable has a location at the last label
17199 it keeps its location until the end of function. */
17200 else if (!current_function_decl)
17201 endname = text_end_label, endview = 0;
17202 else
17204 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17205 current_function_funcdef_no);
17206 endname = ggc_strdup (label_id);
17207 endview = 0;
17210 *listp = new_loc_list (descr, node->label, node->view,
17211 endname, endview, secname);
17212 if (TREE_CODE (decl) == PARM_DECL
17213 && node == loc_list->first
17214 && NOTE_P (node->loc)
17215 && strcmp (node->label, endname) == 0)
17216 (*listp)->force = true;
17217 listp = &(*listp)->dw_loc_next;
17221 if (cfun
17222 && crtl->has_bb_partition
17223 && node == loc_list->last_before_switch)
17225 bool save_in_cold_section_p = in_cold_section_p;
17226 in_cold_section_p = !first_function_block_is_cold;
17227 secname = secname_for_decl (decl);
17228 in_cold_section_p = save_in_cold_section_p;
17231 if (range_across_switch)
17233 if (GET_CODE (node->loc) == EXPR_LIST)
17234 descr = dw_sra_loc_expr (decl, node->loc);
17235 else
17237 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17238 varloc = NOTE_VAR_LOCATION (node->loc);
17239 descr = dw_loc_list_1 (decl, varloc, want_address,
17240 initialized);
17242 gcc_assert (descr);
17243 /* The variable has a location between NODE->LABEL and
17244 NODE->NEXT->LABEL. */
17245 if (node->next)
17246 endname = node->next->label, endview = node->next->view;
17247 else
17248 endname = cfun->fde->dw_fde_second_end, endview = 0;
17249 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17250 endname, endview, secname);
17251 listp = &(*listp)->dw_loc_next;
17255 /* Try to avoid the overhead of a location list emitting a location
17256 expression instead, but only if we didn't have more than one
17257 location entry in the first place. If some entries were not
17258 representable, we don't want to pretend a single entry that was
17259 applies to the entire scope in which the variable is
17260 available. */
17261 if (list && loc_list->first->next)
17262 gen_llsym (list);
17263 else
17264 maybe_gen_llsym (list);
17266 return list;
17269 /* Return if the loc_list has only single element and thus can be represented
17270 as location description. */
17272 static bool
17273 single_element_loc_list_p (dw_loc_list_ref list)
17275 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17276 return !list->ll_symbol;
17279 /* Duplicate a single element of location list. */
17281 static inline dw_loc_descr_ref
17282 copy_loc_descr (dw_loc_descr_ref ref)
17284 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17285 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17286 return copy;
17289 /* To each location in list LIST append loc descr REF. */
17291 static void
17292 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17294 dw_loc_descr_ref copy;
17295 add_loc_descr (&list->expr, ref);
17296 list = list->dw_loc_next;
17297 while (list)
17299 copy = copy_loc_descr (ref);
17300 add_loc_descr (&list->expr, copy);
17301 while (copy->dw_loc_next)
17302 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17303 list = list->dw_loc_next;
17307 /* To each location in list LIST prepend loc descr REF. */
17309 static void
17310 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17312 dw_loc_descr_ref copy;
17313 dw_loc_descr_ref ref_end = list->expr;
17314 add_loc_descr (&ref, list->expr);
17315 list->expr = ref;
17316 list = list->dw_loc_next;
17317 while (list)
17319 dw_loc_descr_ref end = list->expr;
17320 list->expr = copy = copy_loc_descr (ref);
17321 while (copy->dw_loc_next != ref_end)
17322 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17323 copy->dw_loc_next = end;
17324 list = list->dw_loc_next;
17328 /* Given two lists RET and LIST
17329 produce location list that is result of adding expression in LIST
17330 to expression in RET on each position in program.
17331 Might be destructive on both RET and LIST.
17333 TODO: We handle only simple cases of RET or LIST having at most one
17334 element. General case would involve sorting the lists in program order
17335 and merging them that will need some additional work.
17336 Adding that will improve quality of debug info especially for SRA-ed
17337 structures. */
17339 static void
17340 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17342 if (!list)
17343 return;
17344 if (!*ret)
17346 *ret = list;
17347 return;
17349 if (!list->dw_loc_next)
17351 add_loc_descr_to_each (*ret, list->expr);
17352 return;
17354 if (!(*ret)->dw_loc_next)
17356 prepend_loc_descr_to_each (list, (*ret)->expr);
17357 *ret = list;
17358 return;
17360 expansion_failed (NULL_TREE, NULL_RTX,
17361 "Don't know how to merge two non-trivial"
17362 " location lists.\n");
17363 *ret = NULL;
17364 return;
17367 /* LOC is constant expression. Try a luck, look it up in constant
17368 pool and return its loc_descr of its address. */
17370 static dw_loc_descr_ref
17371 cst_pool_loc_descr (tree loc)
17373 /* Get an RTL for this, if something has been emitted. */
17374 rtx rtl = lookup_constant_def (loc);
17376 if (!rtl || !MEM_P (rtl))
17378 gcc_assert (!rtl);
17379 return 0;
17381 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17383 /* TODO: We might get more coverage if we was actually delaying expansion
17384 of all expressions till end of compilation when constant pools are fully
17385 populated. */
17386 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17388 expansion_failed (loc, NULL_RTX,
17389 "CST value in contant pool but not marked.");
17390 return 0;
17392 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17393 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17396 /* Return dw_loc_list representing address of addr_expr LOC
17397 by looking for inner INDIRECT_REF expression and turning
17398 it into simple arithmetics.
17400 See loc_list_from_tree for the meaning of CONTEXT. */
17402 static dw_loc_list_ref
17403 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17404 loc_descr_context *context)
17406 tree obj, offset;
17407 poly_int64 bitsize, bitpos, bytepos;
17408 machine_mode mode;
17409 int unsignedp, reversep, volatilep = 0;
17410 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17412 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17413 &bitsize, &bitpos, &offset, &mode,
17414 &unsignedp, &reversep, &volatilep);
17415 STRIP_NOPS (obj);
17416 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17418 expansion_failed (loc, NULL_RTX, "bitfield access");
17419 return 0;
17421 if (!INDIRECT_REF_P (obj))
17423 expansion_failed (obj,
17424 NULL_RTX, "no indirect ref in inner refrence");
17425 return 0;
17427 if (!offset && known_eq (bitpos, 0))
17428 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17429 context);
17430 else if (toplev
17431 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17432 && (dwarf_version >= 4 || !dwarf_strict))
17434 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17435 if (!list_ret)
17436 return 0;
17437 if (offset)
17439 /* Variable offset. */
17440 list_ret1 = loc_list_from_tree (offset, 0, context);
17441 if (list_ret1 == 0)
17442 return 0;
17443 add_loc_list (&list_ret, list_ret1);
17444 if (!list_ret)
17445 return 0;
17446 add_loc_descr_to_each (list_ret,
17447 new_loc_descr (DW_OP_plus, 0, 0));
17449 HOST_WIDE_INT value;
17450 if (bytepos.is_constant (&value) && value > 0)
17451 add_loc_descr_to_each (list_ret,
17452 new_loc_descr (DW_OP_plus_uconst, value, 0));
17453 else if (maybe_ne (bytepos, 0))
17454 loc_list_plus_const (list_ret, bytepos);
17455 add_loc_descr_to_each (list_ret,
17456 new_loc_descr (DW_OP_stack_value, 0, 0));
17458 return list_ret;
17461 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17462 all operations from LOC are nops, move to the last one. Insert in NOPS all
17463 operations that are skipped. */
17465 static void
17466 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17467 hash_set<dw_loc_descr_ref> &nops)
17469 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17471 nops.add (loc);
17472 loc = loc->dw_loc_next;
17476 /* Helper for loc_descr_without_nops: free the location description operation
17477 P. */
17479 bool
17480 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17482 ggc_free (loc);
17483 return true;
17486 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17487 finishes LOC. */
17489 static void
17490 loc_descr_without_nops (dw_loc_descr_ref &loc)
17492 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17493 return;
17495 /* Set of all DW_OP_nop operations we remove. */
17496 hash_set<dw_loc_descr_ref> nops;
17498 /* First, strip all prefix NOP operations in order to keep the head of the
17499 operations list. */
17500 loc_descr_to_next_no_nop (loc, nops);
17502 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17504 /* For control flow operations: strip "prefix" nops in destination
17505 labels. */
17506 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17507 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17508 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17509 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17511 /* Do the same for the operations that follow, then move to the next
17512 iteration. */
17513 if (cur->dw_loc_next != NULL)
17514 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17515 cur = cur->dw_loc_next;
17518 nops.traverse<void *, free_loc_descr> (NULL);
17522 struct dwarf_procedure_info;
17524 /* Helper structure for location descriptions generation. */
17525 struct loc_descr_context
17527 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17528 NULL_TREE if DW_OP_push_object_address in invalid for this location
17529 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17530 tree context_type;
17531 /* The ..._DECL node that should be translated as a
17532 DW_OP_push_object_address operation. */
17533 tree base_decl;
17534 /* Information about the DWARF procedure we are currently generating. NULL if
17535 we are not generating a DWARF procedure. */
17536 struct dwarf_procedure_info *dpi;
17537 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17538 by consumer. Used for DW_TAG_generic_subrange attributes. */
17539 bool placeholder_arg;
17540 /* True if PLACEHOLDER_EXPR has been seen. */
17541 bool placeholder_seen;
17544 /* DWARF procedures generation
17546 DWARF expressions (aka. location descriptions) are used to encode variable
17547 things such as sizes or offsets. Such computations can have redundant parts
17548 that can be factorized in order to reduce the size of the output debug
17549 information. This is the whole point of DWARF procedures.
17551 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
17552 already factorized into functions ("size functions") in order to handle very
17553 big and complex types. Such functions are quite simple: they have integral
17554 arguments, they return an integral result and their body contains only a
17555 return statement with arithmetic expressions. This is the only kind of
17556 function we are interested in translating into DWARF procedures, here.
17558 DWARF expressions and DWARF procedure are executed using a stack, so we have
17559 to define some calling convention for them to interact. Let's say that:
17561 - Before calling a DWARF procedure, DWARF expressions must push on the stack
17562 all arguments in reverse order (right-to-left) so that when the DWARF
17563 procedure execution starts, the first argument is the top of the stack.
17565 - Then, when returning, the DWARF procedure must have consumed all arguments
17566 on the stack, must have pushed the result and touched nothing else.
17568 - Each integral argument and the result are integral types can be hold in a
17569 single stack slot.
17571 - We call "frame offset" the number of stack slots that are "under DWARF
17572 procedure control": it includes the arguments slots, the temporaries and
17573 the result slot. Thus, it is equal to the number of arguments when the
17574 procedure execution starts and must be equal to one (the result) when it
17575 returns. */
17577 /* Helper structure used when generating operations for a DWARF procedure. */
17578 struct dwarf_procedure_info
17580 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
17581 currently translated. */
17582 tree fndecl;
17583 /* The number of arguments FNDECL takes. */
17584 unsigned args_count;
17587 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
17588 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
17589 equate it to this DIE. */
17591 static dw_die_ref
17592 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
17593 dw_die_ref parent_die)
17595 dw_die_ref dwarf_proc_die;
17597 if ((dwarf_version < 3 && dwarf_strict)
17598 || location == NULL)
17599 return NULL;
17601 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
17602 if (fndecl)
17603 equate_decl_number_to_die (fndecl, dwarf_proc_die);
17604 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
17605 return dwarf_proc_die;
17608 /* Return whether TYPE is a supported type as a DWARF procedure argument
17609 type or return type (we handle only scalar types and pointer types that
17610 aren't wider than the DWARF expression evaluation stack. */
17612 static bool
17613 is_handled_procedure_type (tree type)
17615 return ((INTEGRAL_TYPE_P (type)
17616 || TREE_CODE (type) == OFFSET_TYPE
17617 || TREE_CODE (type) == POINTER_TYPE)
17618 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
17621 /* Helper for resolve_args_picking: do the same but stop when coming across
17622 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
17623 offset *before* evaluating the corresponding operation. */
17625 static bool
17626 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17627 struct dwarf_procedure_info *dpi,
17628 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
17630 /* The "frame_offset" identifier is already used to name a macro... */
17631 unsigned frame_offset_ = initial_frame_offset;
17632 dw_loc_descr_ref l;
17634 for (l = loc; l != NULL;)
17636 bool existed;
17637 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
17639 /* If we already met this node, there is nothing to compute anymore. */
17640 if (existed)
17642 /* Make sure that the stack size is consistent wherever the execution
17643 flow comes from. */
17644 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
17645 break;
17647 l_frame_offset = frame_offset_;
17649 /* If needed, relocate the picking offset with respect to the frame
17650 offset. */
17651 if (l->frame_offset_rel)
17653 unsigned HOST_WIDE_INT off;
17654 switch (l->dw_loc_opc)
17656 case DW_OP_pick:
17657 off = l->dw_loc_oprnd1.v.val_unsigned;
17658 break;
17659 case DW_OP_dup:
17660 off = 0;
17661 break;
17662 case DW_OP_over:
17663 off = 1;
17664 break;
17665 default:
17666 gcc_unreachable ();
17668 /* frame_offset_ is the size of the current stack frame, including
17669 incoming arguments. Besides, the arguments are pushed
17670 right-to-left. Thus, in order to access the Nth argument from
17671 this operation node, the picking has to skip temporaries *plus*
17672 one stack slot per argument (0 for the first one, 1 for the second
17673 one, etc.).
17675 The targetted argument number (N) is already set as the operand,
17676 and the number of temporaries can be computed with:
17677 frame_offsets_ - dpi->args_count */
17678 off += frame_offset_ - dpi->args_count;
17680 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
17681 if (off > 255)
17682 return false;
17684 if (off == 0)
17686 l->dw_loc_opc = DW_OP_dup;
17687 l->dw_loc_oprnd1.v.val_unsigned = 0;
17689 else if (off == 1)
17691 l->dw_loc_opc = DW_OP_over;
17692 l->dw_loc_oprnd1.v.val_unsigned = 0;
17694 else
17696 l->dw_loc_opc = DW_OP_pick;
17697 l->dw_loc_oprnd1.v.val_unsigned = off;
17701 /* Update frame_offset according to the effect the current operation has
17702 on the stack. */
17703 switch (l->dw_loc_opc)
17705 case DW_OP_deref:
17706 case DW_OP_swap:
17707 case DW_OP_rot:
17708 case DW_OP_abs:
17709 case DW_OP_neg:
17710 case DW_OP_not:
17711 case DW_OP_plus_uconst:
17712 case DW_OP_skip:
17713 case DW_OP_reg0:
17714 case DW_OP_reg1:
17715 case DW_OP_reg2:
17716 case DW_OP_reg3:
17717 case DW_OP_reg4:
17718 case DW_OP_reg5:
17719 case DW_OP_reg6:
17720 case DW_OP_reg7:
17721 case DW_OP_reg8:
17722 case DW_OP_reg9:
17723 case DW_OP_reg10:
17724 case DW_OP_reg11:
17725 case DW_OP_reg12:
17726 case DW_OP_reg13:
17727 case DW_OP_reg14:
17728 case DW_OP_reg15:
17729 case DW_OP_reg16:
17730 case DW_OP_reg17:
17731 case DW_OP_reg18:
17732 case DW_OP_reg19:
17733 case DW_OP_reg20:
17734 case DW_OP_reg21:
17735 case DW_OP_reg22:
17736 case DW_OP_reg23:
17737 case DW_OP_reg24:
17738 case DW_OP_reg25:
17739 case DW_OP_reg26:
17740 case DW_OP_reg27:
17741 case DW_OP_reg28:
17742 case DW_OP_reg29:
17743 case DW_OP_reg30:
17744 case DW_OP_reg31:
17745 case DW_OP_bregx:
17746 case DW_OP_piece:
17747 case DW_OP_deref_size:
17748 case DW_OP_nop:
17749 case DW_OP_bit_piece:
17750 case DW_OP_implicit_value:
17751 case DW_OP_stack_value:
17752 break;
17754 case DW_OP_addr:
17755 case DW_OP_const1u:
17756 case DW_OP_const1s:
17757 case DW_OP_const2u:
17758 case DW_OP_const2s:
17759 case DW_OP_const4u:
17760 case DW_OP_const4s:
17761 case DW_OP_const8u:
17762 case DW_OP_const8s:
17763 case DW_OP_constu:
17764 case DW_OP_consts:
17765 case DW_OP_dup:
17766 case DW_OP_over:
17767 case DW_OP_pick:
17768 case DW_OP_lit0:
17769 case DW_OP_lit1:
17770 case DW_OP_lit2:
17771 case DW_OP_lit3:
17772 case DW_OP_lit4:
17773 case DW_OP_lit5:
17774 case DW_OP_lit6:
17775 case DW_OP_lit7:
17776 case DW_OP_lit8:
17777 case DW_OP_lit9:
17778 case DW_OP_lit10:
17779 case DW_OP_lit11:
17780 case DW_OP_lit12:
17781 case DW_OP_lit13:
17782 case DW_OP_lit14:
17783 case DW_OP_lit15:
17784 case DW_OP_lit16:
17785 case DW_OP_lit17:
17786 case DW_OP_lit18:
17787 case DW_OP_lit19:
17788 case DW_OP_lit20:
17789 case DW_OP_lit21:
17790 case DW_OP_lit22:
17791 case DW_OP_lit23:
17792 case DW_OP_lit24:
17793 case DW_OP_lit25:
17794 case DW_OP_lit26:
17795 case DW_OP_lit27:
17796 case DW_OP_lit28:
17797 case DW_OP_lit29:
17798 case DW_OP_lit30:
17799 case DW_OP_lit31:
17800 case DW_OP_breg0:
17801 case DW_OP_breg1:
17802 case DW_OP_breg2:
17803 case DW_OP_breg3:
17804 case DW_OP_breg4:
17805 case DW_OP_breg5:
17806 case DW_OP_breg6:
17807 case DW_OP_breg7:
17808 case DW_OP_breg8:
17809 case DW_OP_breg9:
17810 case DW_OP_breg10:
17811 case DW_OP_breg11:
17812 case DW_OP_breg12:
17813 case DW_OP_breg13:
17814 case DW_OP_breg14:
17815 case DW_OP_breg15:
17816 case DW_OP_breg16:
17817 case DW_OP_breg17:
17818 case DW_OP_breg18:
17819 case DW_OP_breg19:
17820 case DW_OP_breg20:
17821 case DW_OP_breg21:
17822 case DW_OP_breg22:
17823 case DW_OP_breg23:
17824 case DW_OP_breg24:
17825 case DW_OP_breg25:
17826 case DW_OP_breg26:
17827 case DW_OP_breg27:
17828 case DW_OP_breg28:
17829 case DW_OP_breg29:
17830 case DW_OP_breg30:
17831 case DW_OP_breg31:
17832 case DW_OP_fbreg:
17833 case DW_OP_push_object_address:
17834 case DW_OP_call_frame_cfa:
17835 case DW_OP_GNU_variable_value:
17836 ++frame_offset_;
17837 break;
17839 case DW_OP_drop:
17840 case DW_OP_xderef:
17841 case DW_OP_and:
17842 case DW_OP_div:
17843 case DW_OP_minus:
17844 case DW_OP_mod:
17845 case DW_OP_mul:
17846 case DW_OP_or:
17847 case DW_OP_plus:
17848 case DW_OP_shl:
17849 case DW_OP_shr:
17850 case DW_OP_shra:
17851 case DW_OP_xor:
17852 case DW_OP_bra:
17853 case DW_OP_eq:
17854 case DW_OP_ge:
17855 case DW_OP_gt:
17856 case DW_OP_le:
17857 case DW_OP_lt:
17858 case DW_OP_ne:
17859 case DW_OP_regx:
17860 case DW_OP_xderef_size:
17861 --frame_offset_;
17862 break;
17864 case DW_OP_call2:
17865 case DW_OP_call4:
17866 case DW_OP_call_ref:
17868 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17869 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17871 if (stack_usage == NULL)
17872 return false;
17873 frame_offset_ += *stack_usage;
17874 break;
17877 case DW_OP_implicit_pointer:
17878 case DW_OP_entry_value:
17879 case DW_OP_const_type:
17880 case DW_OP_regval_type:
17881 case DW_OP_deref_type:
17882 case DW_OP_convert:
17883 case DW_OP_reinterpret:
17884 case DW_OP_form_tls_address:
17885 case DW_OP_GNU_push_tls_address:
17886 case DW_OP_GNU_uninit:
17887 case DW_OP_GNU_encoded_addr:
17888 case DW_OP_GNU_implicit_pointer:
17889 case DW_OP_GNU_entry_value:
17890 case DW_OP_GNU_const_type:
17891 case DW_OP_GNU_regval_type:
17892 case DW_OP_GNU_deref_type:
17893 case DW_OP_GNU_convert:
17894 case DW_OP_GNU_reinterpret:
17895 case DW_OP_GNU_parameter_ref:
17896 /* loc_list_from_tree will probably not output these operations for
17897 size functions, so assume they will not appear here. */
17898 /* Fall through... */
17900 default:
17901 gcc_unreachable ();
17904 /* Now, follow the control flow (except subroutine calls). */
17905 switch (l->dw_loc_opc)
17907 case DW_OP_bra:
17908 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17909 frame_offsets))
17910 return false;
17911 /* Fall through. */
17913 case DW_OP_skip:
17914 l = l->dw_loc_oprnd1.v.val_loc;
17915 break;
17917 case DW_OP_stack_value:
17918 return true;
17920 default:
17921 l = l->dw_loc_next;
17922 break;
17926 return true;
17929 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17930 operations) in order to resolve the operand of DW_OP_pick operations that
17931 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17932 offset *before* LOC is executed. Return if all relocations were
17933 successful. */
17935 static bool
17936 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17937 struct dwarf_procedure_info *dpi)
17939 /* Associate to all visited operations the frame offset *before* evaluating
17940 this operation. */
17941 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17943 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17944 frame_offsets);
17947 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17948 Return NULL if it is not possible. */
17950 static dw_die_ref
17951 function_to_dwarf_procedure (tree fndecl)
17953 struct loc_descr_context ctx;
17954 struct dwarf_procedure_info dpi;
17955 dw_die_ref dwarf_proc_die;
17956 tree tree_body = DECL_SAVED_TREE (fndecl);
17957 dw_loc_descr_ref loc_body, epilogue;
17959 tree cursor;
17960 unsigned i;
17962 /* Do not generate multiple DWARF procedures for the same function
17963 declaration. */
17964 dwarf_proc_die = lookup_decl_die (fndecl);
17965 if (dwarf_proc_die != NULL)
17966 return dwarf_proc_die;
17968 /* DWARF procedures are available starting with the DWARFv3 standard. */
17969 if (dwarf_version < 3 && dwarf_strict)
17970 return NULL;
17972 /* We handle only functions for which we still have a body, that return a
17973 supported type and that takes arguments with supported types. Note that
17974 there is no point translating functions that return nothing. */
17975 if (tree_body == NULL_TREE
17976 || DECL_RESULT (fndecl) == NULL_TREE
17977 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17978 return NULL;
17980 for (cursor = DECL_ARGUMENTS (fndecl);
17981 cursor != NULL_TREE;
17982 cursor = TREE_CHAIN (cursor))
17983 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17984 return NULL;
17986 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17987 if (TREE_CODE (tree_body) != RETURN_EXPR)
17988 return NULL;
17989 tree_body = TREE_OPERAND (tree_body, 0);
17990 if (TREE_CODE (tree_body) != MODIFY_EXPR
17991 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17992 return NULL;
17993 tree_body = TREE_OPERAND (tree_body, 1);
17995 /* Try to translate the body expression itself. Note that this will probably
17996 cause an infinite recursion if its call graph has a cycle. This is very
17997 unlikely for size functions, however, so don't bother with such things at
17998 the moment. */
17999 ctx.context_type = NULL_TREE;
18000 ctx.base_decl = NULL_TREE;
18001 ctx.dpi = &dpi;
18002 ctx.placeholder_arg = false;
18003 ctx.placeholder_seen = false;
18004 dpi.fndecl = fndecl;
18005 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18006 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18007 if (!loc_body)
18008 return NULL;
18010 /* After evaluating all operands in "loc_body", we should still have on the
18011 stack all arguments plus the desired function result (top of the stack).
18012 Generate code in order to keep only the result in our stack frame. */
18013 epilogue = NULL;
18014 for (i = 0; i < dpi.args_count; ++i)
18016 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18017 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18018 op_couple->dw_loc_next->dw_loc_next = epilogue;
18019 epilogue = op_couple;
18021 add_loc_descr (&loc_body, epilogue);
18022 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18023 return NULL;
18025 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18026 because they are considered useful. Now there is an epilogue, they are
18027 not anymore, so give it another try. */
18028 loc_descr_without_nops (loc_body);
18030 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18031 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18032 though, given that size functions do not come from source, so they should
18033 not have a dedicated DW_TAG_subprogram DIE. */
18034 dwarf_proc_die
18035 = new_dwarf_proc_die (loc_body, fndecl,
18036 get_context_die (DECL_CONTEXT (fndecl)));
18038 /* The called DWARF procedure consumes one stack slot per argument and
18039 returns one stack slot. */
18040 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18042 return dwarf_proc_die;
18046 /* Generate Dwarf location list representing LOC.
18047 If WANT_ADDRESS is false, expression computing LOC will be computed
18048 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18049 if WANT_ADDRESS is 2, expression computing address useable in location
18050 will be returned (i.e. DW_OP_reg can be used
18051 to refer to register values).
18053 CONTEXT provides information to customize the location descriptions
18054 generation. Its context_type field specifies what type is implicitly
18055 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18056 will not be generated.
18058 Its DPI field determines whether we are generating a DWARF expression for a
18059 DWARF procedure, so PARM_DECL references are processed specifically.
18061 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18062 and dpi fields were null. */
18064 static dw_loc_list_ref
18065 loc_list_from_tree_1 (tree loc, int want_address,
18066 struct loc_descr_context *context)
18068 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18069 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18070 int have_address = 0;
18071 enum dwarf_location_atom op;
18073 /* ??? Most of the time we do not take proper care for sign/zero
18074 extending the values properly. Hopefully this won't be a real
18075 problem... */
18077 if (context != NULL
18078 && context->base_decl == loc
18079 && want_address == 0)
18081 if (dwarf_version >= 3 || !dwarf_strict)
18082 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18083 NULL, 0, NULL, 0, NULL);
18084 else
18085 return NULL;
18088 switch (TREE_CODE (loc))
18090 case ERROR_MARK:
18091 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18092 return 0;
18094 case PLACEHOLDER_EXPR:
18095 /* This case involves extracting fields from an object to determine the
18096 position of other fields. It is supposed to appear only as the first
18097 operand of COMPONENT_REF nodes and to reference precisely the type
18098 that the context allows. */
18099 if (context != NULL
18100 && TREE_TYPE (loc) == context->context_type
18101 && want_address >= 1)
18103 if (dwarf_version >= 3 || !dwarf_strict)
18105 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18106 have_address = 1;
18107 break;
18109 else
18110 return NULL;
18112 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18113 the single argument passed by consumer. */
18114 else if (context != NULL
18115 && context->placeholder_arg
18116 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18117 && want_address == 0)
18119 ret = new_loc_descr (DW_OP_pick, 0, 0);
18120 ret->frame_offset_rel = 1;
18121 context->placeholder_seen = true;
18122 break;
18124 else
18125 expansion_failed (loc, NULL_RTX,
18126 "PLACEHOLDER_EXPR for an unexpected type");
18127 break;
18129 case CALL_EXPR:
18131 const int nargs = call_expr_nargs (loc);
18132 tree callee = get_callee_fndecl (loc);
18133 int i;
18134 dw_die_ref dwarf_proc;
18136 if (callee == NULL_TREE)
18137 goto call_expansion_failed;
18139 /* We handle only functions that return an integer. */
18140 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
18141 goto call_expansion_failed;
18143 dwarf_proc = function_to_dwarf_procedure (callee);
18144 if (dwarf_proc == NULL)
18145 goto call_expansion_failed;
18147 /* Evaluate arguments right-to-left so that the first argument will
18148 be the top-most one on the stack. */
18149 for (i = nargs - 1; i >= 0; --i)
18151 dw_loc_descr_ref loc_descr
18152 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
18153 context);
18155 if (loc_descr == NULL)
18156 goto call_expansion_failed;
18158 add_loc_descr (&ret, loc_descr);
18161 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18162 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18163 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18164 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18165 add_loc_descr (&ret, ret1);
18166 break;
18168 call_expansion_failed:
18169 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
18170 /* There are no opcodes for these operations. */
18171 return 0;
18174 case PREINCREMENT_EXPR:
18175 case PREDECREMENT_EXPR:
18176 case POSTINCREMENT_EXPR:
18177 case POSTDECREMENT_EXPR:
18178 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18179 /* There are no opcodes for these operations. */
18180 return 0;
18182 case ADDR_EXPR:
18183 /* If we already want an address, see if there is INDIRECT_REF inside
18184 e.g. for &this->field. */
18185 if (want_address)
18187 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18188 (loc, want_address == 2, context);
18189 if (list_ret)
18190 have_address = 1;
18191 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18192 && (ret = cst_pool_loc_descr (loc)))
18193 have_address = 1;
18195 /* Otherwise, process the argument and look for the address. */
18196 if (!list_ret && !ret)
18197 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18198 else
18200 if (want_address)
18201 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18202 return NULL;
18204 break;
18206 case VAR_DECL:
18207 if (DECL_THREAD_LOCAL_P (loc))
18209 rtx rtl;
18210 enum dwarf_location_atom tls_op;
18211 enum dtprel_bool dtprel = dtprel_false;
18213 if (targetm.have_tls)
18215 /* If this is not defined, we have no way to emit the
18216 data. */
18217 if (!targetm.asm_out.output_dwarf_dtprel)
18218 return 0;
18220 /* The way DW_OP_GNU_push_tls_address is specified, we
18221 can only look up addresses of objects in the current
18222 module. We used DW_OP_addr as first op, but that's
18223 wrong, because DW_OP_addr is relocated by the debug
18224 info consumer, while DW_OP_GNU_push_tls_address
18225 operand shouldn't be. */
18226 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18227 return 0;
18228 dtprel = dtprel_true;
18229 /* We check for DWARF 5 here because gdb did not implement
18230 DW_OP_form_tls_address until after 7.12. */
18231 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18232 : DW_OP_GNU_push_tls_address);
18234 else
18236 if (!targetm.emutls.debug_form_tls_address
18237 || !(dwarf_version >= 3 || !dwarf_strict))
18238 return 0;
18239 /* We stuffed the control variable into the DECL_VALUE_EXPR
18240 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18241 no longer appear in gimple code. We used the control
18242 variable in specific so that we could pick it up here. */
18243 loc = DECL_VALUE_EXPR (loc);
18244 tls_op = DW_OP_form_tls_address;
18247 rtl = rtl_for_decl_location (loc);
18248 if (rtl == NULL_RTX)
18249 return 0;
18251 if (!MEM_P (rtl))
18252 return 0;
18253 rtl = XEXP (rtl, 0);
18254 if (! CONSTANT_P (rtl))
18255 return 0;
18257 ret = new_addr_loc_descr (rtl, dtprel);
18258 ret1 = new_loc_descr (tls_op, 0, 0);
18259 add_loc_descr (&ret, ret1);
18261 have_address = 1;
18262 break;
18264 /* FALLTHRU */
18266 case PARM_DECL:
18267 if (context != NULL && context->dpi != NULL
18268 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18270 /* We are generating code for a DWARF procedure and we want to access
18271 one of its arguments: find the appropriate argument offset and let
18272 the resolve_args_picking pass compute the offset that complies
18273 with the stack frame size. */
18274 unsigned i = 0;
18275 tree cursor;
18277 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18278 cursor != NULL_TREE && cursor != loc;
18279 cursor = TREE_CHAIN (cursor), ++i)
18281 /* If we are translating a DWARF procedure, all referenced parameters
18282 must belong to the current function. */
18283 gcc_assert (cursor != NULL_TREE);
18285 ret = new_loc_descr (DW_OP_pick, i, 0);
18286 ret->frame_offset_rel = 1;
18287 break;
18289 /* FALLTHRU */
18291 case RESULT_DECL:
18292 if (DECL_HAS_VALUE_EXPR_P (loc))
18293 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
18294 want_address, context);
18295 /* FALLTHRU */
18297 case FUNCTION_DECL:
18299 rtx rtl;
18300 var_loc_list *loc_list = lookup_decl_loc (loc);
18302 if (loc_list && loc_list->first)
18304 list_ret = dw_loc_list (loc_list, loc, want_address);
18305 have_address = want_address != 0;
18306 break;
18308 rtl = rtl_for_decl_location (loc);
18309 if (rtl == NULL_RTX)
18311 if (TREE_CODE (loc) != FUNCTION_DECL
18312 && early_dwarf
18313 && current_function_decl
18314 && want_address != 1
18315 && ! DECL_IGNORED_P (loc)
18316 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18317 || POINTER_TYPE_P (TREE_TYPE (loc)))
18318 && DECL_CONTEXT (loc) == current_function_decl
18319 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18320 <= DWARF2_ADDR_SIZE))
18322 dw_die_ref ref = lookup_decl_die (loc);
18323 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18324 if (ref)
18326 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18327 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18328 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18330 else
18332 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18333 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18335 break;
18337 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18338 return 0;
18340 else if (CONST_INT_P (rtl))
18342 HOST_WIDE_INT val = INTVAL (rtl);
18343 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18344 val &= GET_MODE_MASK (DECL_MODE (loc));
18345 ret = int_loc_descriptor (val);
18347 else if (GET_CODE (rtl) == CONST_STRING)
18349 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18350 return 0;
18352 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18353 ret = new_addr_loc_descr (rtl, dtprel_false);
18354 else
18356 machine_mode mode, mem_mode;
18358 /* Certain constructs can only be represented at top-level. */
18359 if (want_address == 2)
18361 ret = loc_descriptor (rtl, VOIDmode,
18362 VAR_INIT_STATUS_INITIALIZED);
18363 have_address = 1;
18365 else
18367 mode = GET_MODE (rtl);
18368 mem_mode = VOIDmode;
18369 if (MEM_P (rtl))
18371 mem_mode = mode;
18372 mode = get_address_mode (rtl);
18373 rtl = XEXP (rtl, 0);
18374 have_address = 1;
18376 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18377 VAR_INIT_STATUS_INITIALIZED);
18379 if (!ret)
18380 expansion_failed (loc, rtl,
18381 "failed to produce loc descriptor for rtl");
18384 break;
18386 case MEM_REF:
18387 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18389 have_address = 1;
18390 goto do_plus;
18392 /* Fallthru. */
18393 case INDIRECT_REF:
18394 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18395 have_address = 1;
18396 break;
18398 case TARGET_MEM_REF:
18399 case SSA_NAME:
18400 case DEBUG_EXPR_DECL:
18401 return NULL;
18403 case COMPOUND_EXPR:
18404 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18405 context);
18407 CASE_CONVERT:
18408 case VIEW_CONVERT_EXPR:
18409 case SAVE_EXPR:
18410 case MODIFY_EXPR:
18411 case NON_LVALUE_EXPR:
18412 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18413 context);
18415 case COMPONENT_REF:
18416 case BIT_FIELD_REF:
18417 case ARRAY_REF:
18418 case ARRAY_RANGE_REF:
18419 case REALPART_EXPR:
18420 case IMAGPART_EXPR:
18422 tree obj, offset;
18423 poly_int64 bitsize, bitpos, bytepos;
18424 machine_mode mode;
18425 int unsignedp, reversep, volatilep = 0;
18427 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18428 &unsignedp, &reversep, &volatilep);
18430 gcc_assert (obj != loc);
18432 list_ret = loc_list_from_tree_1 (obj,
18433 want_address == 2
18434 && known_eq (bitpos, 0)
18435 && !offset ? 2 : 1,
18436 context);
18437 /* TODO: We can extract value of the small expression via shifting even
18438 for nonzero bitpos. */
18439 if (list_ret == 0)
18440 return 0;
18441 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18442 || !multiple_p (bitsize, BITS_PER_UNIT))
18444 expansion_failed (loc, NULL_RTX,
18445 "bitfield access");
18446 return 0;
18449 if (offset != NULL_TREE)
18451 /* Variable offset. */
18452 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18453 if (list_ret1 == 0)
18454 return 0;
18455 add_loc_list (&list_ret, list_ret1);
18456 if (!list_ret)
18457 return 0;
18458 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18461 HOST_WIDE_INT value;
18462 if (bytepos.is_constant (&value) && value > 0)
18463 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18464 value, 0));
18465 else if (maybe_ne (bytepos, 0))
18466 loc_list_plus_const (list_ret, bytepos);
18468 have_address = 1;
18469 break;
18472 case INTEGER_CST:
18473 if ((want_address || !tree_fits_shwi_p (loc))
18474 && (ret = cst_pool_loc_descr (loc)))
18475 have_address = 1;
18476 else if (want_address == 2
18477 && tree_fits_shwi_p (loc)
18478 && (ret = address_of_int_loc_descriptor
18479 (int_size_in_bytes (TREE_TYPE (loc)),
18480 tree_to_shwi (loc))))
18481 have_address = 1;
18482 else if (tree_fits_shwi_p (loc))
18483 ret = int_loc_descriptor (tree_to_shwi (loc));
18484 else if (tree_fits_uhwi_p (loc))
18485 ret = uint_loc_descriptor (tree_to_uhwi (loc));
18486 else
18488 expansion_failed (loc, NULL_RTX,
18489 "Integer operand is not host integer");
18490 return 0;
18492 break;
18494 case CONSTRUCTOR:
18495 case REAL_CST:
18496 case STRING_CST:
18497 case COMPLEX_CST:
18498 if ((ret = cst_pool_loc_descr (loc)))
18499 have_address = 1;
18500 else if (TREE_CODE (loc) == CONSTRUCTOR)
18502 tree type = TREE_TYPE (loc);
18503 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
18504 unsigned HOST_WIDE_INT offset = 0;
18505 unsigned HOST_WIDE_INT cnt;
18506 constructor_elt *ce;
18508 if (TREE_CODE (type) == RECORD_TYPE)
18510 /* This is very limited, but it's enough to output
18511 pointers to member functions, as long as the
18512 referenced function is defined in the current
18513 translation unit. */
18514 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
18516 tree val = ce->value;
18518 tree field = ce->index;
18520 if (val)
18521 STRIP_NOPS (val);
18523 if (!field || DECL_BIT_FIELD (field))
18525 expansion_failed (loc, NULL_RTX,
18526 "bitfield in record type constructor");
18527 size = offset = (unsigned HOST_WIDE_INT)-1;
18528 ret = NULL;
18529 break;
18532 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
18533 unsigned HOST_WIDE_INT pos = int_byte_position (field);
18534 gcc_assert (pos + fieldsize <= size);
18535 if (pos < offset)
18537 expansion_failed (loc, NULL_RTX,
18538 "out-of-order fields in record constructor");
18539 size = offset = (unsigned HOST_WIDE_INT)-1;
18540 ret = NULL;
18541 break;
18543 if (pos > offset)
18545 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
18546 add_loc_descr (&ret, ret1);
18547 offset = pos;
18549 if (val && fieldsize != 0)
18551 ret1 = loc_descriptor_from_tree (val, want_address, context);
18552 if (!ret1)
18554 expansion_failed (loc, NULL_RTX,
18555 "unsupported expression in field");
18556 size = offset = (unsigned HOST_WIDE_INT)-1;
18557 ret = NULL;
18558 break;
18560 add_loc_descr (&ret, ret1);
18562 if (fieldsize)
18564 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
18565 add_loc_descr (&ret, ret1);
18566 offset = pos + fieldsize;
18570 if (offset != size)
18572 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
18573 add_loc_descr (&ret, ret1);
18574 offset = size;
18577 have_address = !!want_address;
18579 else
18580 expansion_failed (loc, NULL_RTX,
18581 "constructor of non-record type");
18583 else
18584 /* We can construct small constants here using int_loc_descriptor. */
18585 expansion_failed (loc, NULL_RTX,
18586 "constructor or constant not in constant pool");
18587 break;
18589 case TRUTH_AND_EXPR:
18590 case TRUTH_ANDIF_EXPR:
18591 case BIT_AND_EXPR:
18592 op = DW_OP_and;
18593 goto do_binop;
18595 case TRUTH_XOR_EXPR:
18596 case BIT_XOR_EXPR:
18597 op = DW_OP_xor;
18598 goto do_binop;
18600 case TRUTH_OR_EXPR:
18601 case TRUTH_ORIF_EXPR:
18602 case BIT_IOR_EXPR:
18603 op = DW_OP_or;
18604 goto do_binop;
18606 case FLOOR_DIV_EXPR:
18607 case CEIL_DIV_EXPR:
18608 case ROUND_DIV_EXPR:
18609 case TRUNC_DIV_EXPR:
18610 case EXACT_DIV_EXPR:
18611 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18612 return 0;
18613 op = DW_OP_div;
18614 goto do_binop;
18616 case MINUS_EXPR:
18617 op = DW_OP_minus;
18618 goto do_binop;
18620 case FLOOR_MOD_EXPR:
18621 case CEIL_MOD_EXPR:
18622 case ROUND_MOD_EXPR:
18623 case TRUNC_MOD_EXPR:
18624 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18626 op = DW_OP_mod;
18627 goto do_binop;
18629 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18630 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18631 if (list_ret == 0 || list_ret1 == 0)
18632 return 0;
18634 add_loc_list (&list_ret, list_ret1);
18635 if (list_ret == 0)
18636 return 0;
18637 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18638 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18639 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
18640 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
18641 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
18642 break;
18644 case MULT_EXPR:
18645 op = DW_OP_mul;
18646 goto do_binop;
18648 case LSHIFT_EXPR:
18649 op = DW_OP_shl;
18650 goto do_binop;
18652 case RSHIFT_EXPR:
18653 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
18654 goto do_binop;
18656 case POINTER_PLUS_EXPR:
18657 case PLUS_EXPR:
18658 do_plus:
18659 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
18661 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
18662 smarter to encode their opposite. The DW_OP_plus_uconst operation
18663 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
18664 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
18665 bytes, Y being the size of the operation that pushes the opposite
18666 of the addend. So let's choose the smallest representation. */
18667 const tree tree_addend = TREE_OPERAND (loc, 1);
18668 offset_int wi_addend;
18669 HOST_WIDE_INT shwi_addend;
18670 dw_loc_descr_ref loc_naddend;
18672 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18673 if (list_ret == 0)
18674 return 0;
18676 /* Try to get the literal to push. It is the opposite of the addend,
18677 so as we rely on wrapping during DWARF evaluation, first decode
18678 the literal as a "DWARF-sized" signed number. */
18679 wi_addend = wi::to_offset (tree_addend);
18680 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
18681 shwi_addend = wi_addend.to_shwi ();
18682 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
18683 ? int_loc_descriptor (-shwi_addend)
18684 : NULL;
18686 if (loc_naddend != NULL
18687 && ((unsigned) size_of_uleb128 (shwi_addend)
18688 > size_of_loc_descr (loc_naddend)))
18690 add_loc_descr_to_each (list_ret, loc_naddend);
18691 add_loc_descr_to_each (list_ret,
18692 new_loc_descr (DW_OP_minus, 0, 0));
18694 else
18696 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
18698 loc_naddend = loc_cur;
18699 loc_cur = loc_cur->dw_loc_next;
18700 ggc_free (loc_naddend);
18702 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
18704 break;
18707 op = DW_OP_plus;
18708 goto do_binop;
18710 case LE_EXPR:
18711 op = DW_OP_le;
18712 goto do_comp_binop;
18714 case GE_EXPR:
18715 op = DW_OP_ge;
18716 goto do_comp_binop;
18718 case LT_EXPR:
18719 op = DW_OP_lt;
18720 goto do_comp_binop;
18722 case GT_EXPR:
18723 op = DW_OP_gt;
18724 goto do_comp_binop;
18726 do_comp_binop:
18727 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18729 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18730 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18731 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18732 TREE_CODE (loc));
18733 break;
18735 else
18736 goto do_binop;
18738 case EQ_EXPR:
18739 op = DW_OP_eq;
18740 goto do_binop;
18742 case NE_EXPR:
18743 op = DW_OP_ne;
18744 goto do_binop;
18746 do_binop:
18747 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18748 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18749 if (list_ret == 0 || list_ret1 == 0)
18750 return 0;
18752 add_loc_list (&list_ret, list_ret1);
18753 if (list_ret == 0)
18754 return 0;
18755 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18756 break;
18758 case TRUTH_NOT_EXPR:
18759 case BIT_NOT_EXPR:
18760 op = DW_OP_not;
18761 goto do_unop;
18763 case ABS_EXPR:
18764 op = DW_OP_abs;
18765 goto do_unop;
18767 case NEGATE_EXPR:
18768 op = DW_OP_neg;
18769 goto do_unop;
18771 do_unop:
18772 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18773 if (list_ret == 0)
18774 return 0;
18776 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18777 break;
18779 case MIN_EXPR:
18780 case MAX_EXPR:
18782 const enum tree_code code =
18783 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18785 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18786 build2 (code, integer_type_node,
18787 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18788 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18791 /* fall through */
18793 case COND_EXPR:
18795 dw_loc_descr_ref lhs
18796 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18797 dw_loc_list_ref rhs
18798 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18799 dw_loc_descr_ref bra_node, jump_node, tmp;
18801 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18802 if (list_ret == 0 || lhs == 0 || rhs == 0)
18803 return 0;
18805 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18806 add_loc_descr_to_each (list_ret, bra_node);
18808 add_loc_list (&list_ret, rhs);
18809 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18810 add_loc_descr_to_each (list_ret, jump_node);
18812 add_loc_descr_to_each (list_ret, lhs);
18813 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18814 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18816 /* ??? Need a node to point the skip at. Use a nop. */
18817 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18818 add_loc_descr_to_each (list_ret, tmp);
18819 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18820 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18822 break;
18824 case FIX_TRUNC_EXPR:
18825 return 0;
18827 default:
18828 /* Leave front-end specific codes as simply unknown. This comes
18829 up, for instance, with the C STMT_EXPR. */
18830 if ((unsigned int) TREE_CODE (loc)
18831 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18833 expansion_failed (loc, NULL_RTX,
18834 "language specific tree node");
18835 return 0;
18838 /* Otherwise this is a generic code; we should just lists all of
18839 these explicitly. We forgot one. */
18840 if (flag_checking)
18841 gcc_unreachable ();
18843 /* In a release build, we want to degrade gracefully: better to
18844 generate incomplete debugging information than to crash. */
18845 return NULL;
18848 if (!ret && !list_ret)
18849 return 0;
18851 if (want_address == 2 && !have_address
18852 && (dwarf_version >= 4 || !dwarf_strict))
18854 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18856 expansion_failed (loc, NULL_RTX,
18857 "DWARF address size mismatch");
18858 return 0;
18860 if (ret)
18861 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18862 else
18863 add_loc_descr_to_each (list_ret,
18864 new_loc_descr (DW_OP_stack_value, 0, 0));
18865 have_address = 1;
18867 /* Show if we can't fill the request for an address. */
18868 if (want_address && !have_address)
18870 expansion_failed (loc, NULL_RTX,
18871 "Want address and only have value");
18872 return 0;
18875 gcc_assert (!ret || !list_ret);
18877 /* If we've got an address and don't want one, dereference. */
18878 if (!want_address && have_address)
18880 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18882 if (size > DWARF2_ADDR_SIZE || size == -1)
18884 expansion_failed (loc, NULL_RTX,
18885 "DWARF address size mismatch");
18886 return 0;
18888 else if (size == DWARF2_ADDR_SIZE)
18889 op = DW_OP_deref;
18890 else
18891 op = DW_OP_deref_size;
18893 if (ret)
18894 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18895 else
18896 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18898 if (ret)
18899 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
18901 return list_ret;
18904 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18905 expressions. */
18907 static dw_loc_list_ref
18908 loc_list_from_tree (tree loc, int want_address,
18909 struct loc_descr_context *context)
18911 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18913 for (dw_loc_list_ref loc_cur = result;
18914 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18915 loc_descr_without_nops (loc_cur->expr);
18916 return result;
18919 /* Same as above but return only single location expression. */
18920 static dw_loc_descr_ref
18921 loc_descriptor_from_tree (tree loc, int want_address,
18922 struct loc_descr_context *context)
18924 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18925 if (!ret)
18926 return NULL;
18927 if (ret->dw_loc_next)
18929 expansion_failed (loc, NULL_RTX,
18930 "Location list where only loc descriptor needed");
18931 return NULL;
18933 return ret->expr;
18936 /* Given a value, round it up to the lowest multiple of `boundary'
18937 which is not less than the value itself. */
18939 static inline HOST_WIDE_INT
18940 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18942 return (((value + boundary - 1) / boundary) * boundary);
18945 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18946 pointer to the declared type for the relevant field variable, or return
18947 `integer_type_node' if the given node turns out to be an
18948 ERROR_MARK node. */
18950 static inline tree
18951 field_type (const_tree decl)
18953 tree type;
18955 if (TREE_CODE (decl) == ERROR_MARK)
18956 return integer_type_node;
18958 type = DECL_BIT_FIELD_TYPE (decl);
18959 if (type == NULL_TREE)
18960 type = TREE_TYPE (decl);
18962 return type;
18965 /* Given a pointer to a tree node, return the alignment in bits for
18966 it, or else return BITS_PER_WORD if the node actually turns out to
18967 be an ERROR_MARK node. */
18969 static inline unsigned
18970 simple_type_align_in_bits (const_tree type)
18972 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18975 static inline unsigned
18976 simple_decl_align_in_bits (const_tree decl)
18978 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18981 /* Return the result of rounding T up to ALIGN. */
18983 static inline offset_int
18984 round_up_to_align (const offset_int &t, unsigned int align)
18986 return wi::udiv_trunc (t + align - 1, align) * align;
18989 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18990 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18991 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18992 if we fail to return the size in one of these two forms. */
18994 static dw_loc_descr_ref
18995 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18997 tree tree_size;
18998 struct loc_descr_context ctx;
19000 /* Return a constant integer in priority, if possible. */
19001 *cst_size = int_size_in_bytes (type);
19002 if (*cst_size != -1)
19003 return NULL;
19005 ctx.context_type = const_cast<tree> (type);
19006 ctx.base_decl = NULL_TREE;
19007 ctx.dpi = NULL;
19008 ctx.placeholder_arg = false;
19009 ctx.placeholder_seen = false;
19011 type = TYPE_MAIN_VARIANT (type);
19012 tree_size = TYPE_SIZE_UNIT (type);
19013 return ((tree_size != NULL_TREE)
19014 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
19015 : NULL);
19018 /* Helper structure for RECORD_TYPE processing. */
19019 struct vlr_context
19021 /* Root RECORD_TYPE. It is needed to generate data member location
19022 descriptions in variable-length records (VLR), but also to cope with
19023 variants, which are composed of nested structures multiplexed with
19024 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19025 function processing a FIELD_DECL, it is required to be non null. */
19026 tree struct_type;
19027 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19028 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19029 this variant part as part of the root record (in storage units). For
19030 regular records, it must be NULL_TREE. */
19031 tree variant_part_offset;
19034 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19035 addressed byte of the "containing object" for the given FIELD_DECL. If
19036 possible, return a native constant through CST_OFFSET (in which case NULL is
19037 returned); otherwise return a DWARF expression that computes the offset.
19039 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19040 that offset is, either because the argument turns out to be a pointer to an
19041 ERROR_MARK node, or because the offset expression is too complex for us.
19043 CTX is required: see the comment for VLR_CONTEXT. */
19045 static dw_loc_descr_ref
19046 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19047 HOST_WIDE_INT *cst_offset)
19049 tree tree_result;
19050 dw_loc_list_ref loc_result;
19052 *cst_offset = 0;
19054 if (TREE_CODE (decl) == ERROR_MARK)
19055 return NULL;
19056 else
19057 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19059 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19060 case. */
19061 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19062 return NULL;
19064 #ifdef PCC_BITFIELD_TYPE_MATTERS
19065 /* We used to handle only constant offsets in all cases. Now, we handle
19066 properly dynamic byte offsets only when PCC bitfield type doesn't
19067 matter. */
19068 if (PCC_BITFIELD_TYPE_MATTERS
19069 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19071 offset_int object_offset_in_bits;
19072 offset_int object_offset_in_bytes;
19073 offset_int bitpos_int;
19074 tree type;
19075 tree field_size_tree;
19076 offset_int deepest_bitpos;
19077 offset_int field_size_in_bits;
19078 unsigned int type_align_in_bits;
19079 unsigned int decl_align_in_bits;
19080 offset_int type_size_in_bits;
19082 bitpos_int = wi::to_offset (bit_position (decl));
19083 type = field_type (decl);
19084 type_size_in_bits = offset_int_type_size_in_bits (type);
19085 type_align_in_bits = simple_type_align_in_bits (type);
19087 field_size_tree = DECL_SIZE (decl);
19089 /* The size could be unspecified if there was an error, or for
19090 a flexible array member. */
19091 if (!field_size_tree)
19092 field_size_tree = bitsize_zero_node;
19094 /* If the size of the field is not constant, use the type size. */
19095 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19096 field_size_in_bits = wi::to_offset (field_size_tree);
19097 else
19098 field_size_in_bits = type_size_in_bits;
19100 decl_align_in_bits = simple_decl_align_in_bits (decl);
19102 /* The GCC front-end doesn't make any attempt to keep track of the
19103 starting bit offset (relative to the start of the containing
19104 structure type) of the hypothetical "containing object" for a
19105 bit-field. Thus, when computing the byte offset value for the
19106 start of the "containing object" of a bit-field, we must deduce
19107 this information on our own. This can be rather tricky to do in
19108 some cases. For example, handling the following structure type
19109 definition when compiling for an i386/i486 target (which only
19110 aligns long long's to 32-bit boundaries) can be very tricky:
19112 struct S { int field1; long long field2:31; };
19114 Fortunately, there is a simple rule-of-thumb which can be used
19115 in such cases. When compiling for an i386/i486, GCC will
19116 allocate 8 bytes for the structure shown above. It decides to
19117 do this based upon one simple rule for bit-field allocation.
19118 GCC allocates each "containing object" for each bit-field at
19119 the first (i.e. lowest addressed) legitimate alignment boundary
19120 (based upon the required minimum alignment for the declared
19121 type of the field) which it can possibly use, subject to the
19122 condition that there is still enough available space remaining
19123 in the containing object (when allocated at the selected point)
19124 to fully accommodate all of the bits of the bit-field itself.
19126 This simple rule makes it obvious why GCC allocates 8 bytes for
19127 each object of the structure type shown above. When looking
19128 for a place to allocate the "containing object" for `field2',
19129 the compiler simply tries to allocate a 64-bit "containing
19130 object" at each successive 32-bit boundary (starting at zero)
19131 until it finds a place to allocate that 64- bit field such that
19132 at least 31 contiguous (and previously unallocated) bits remain
19133 within that selected 64 bit field. (As it turns out, for the
19134 example above, the compiler finds it is OK to allocate the
19135 "containing object" 64-bit field at bit-offset zero within the
19136 structure type.)
19138 Here we attempt to work backwards from the limited set of facts
19139 we're given, and we try to deduce from those facts, where GCC
19140 must have believed that the containing object started (within
19141 the structure type). The value we deduce is then used (by the
19142 callers of this routine) to generate DW_AT_location and
19143 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19144 the case of DW_AT_location, regular fields as well). */
19146 /* Figure out the bit-distance from the start of the structure to
19147 the "deepest" bit of the bit-field. */
19148 deepest_bitpos = bitpos_int + field_size_in_bits;
19150 /* This is the tricky part. Use some fancy footwork to deduce
19151 where the lowest addressed bit of the containing object must
19152 be. */
19153 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19155 /* Round up to type_align by default. This works best for
19156 bitfields. */
19157 object_offset_in_bits
19158 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19160 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19162 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19164 /* Round up to decl_align instead. */
19165 object_offset_in_bits
19166 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19169 object_offset_in_bytes
19170 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19171 if (ctx->variant_part_offset == NULL_TREE)
19173 *cst_offset = object_offset_in_bytes.to_shwi ();
19174 return NULL;
19176 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19178 else
19179 #endif /* PCC_BITFIELD_TYPE_MATTERS */
19180 tree_result = byte_position (decl);
19182 if (ctx->variant_part_offset != NULL_TREE)
19183 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19184 ctx->variant_part_offset, tree_result);
19186 /* If the byte offset is a constant, it's simplier to handle a native
19187 constant rather than a DWARF expression. */
19188 if (TREE_CODE (tree_result) == INTEGER_CST)
19190 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19191 return NULL;
19193 struct loc_descr_context loc_ctx = {
19194 ctx->struct_type, /* context_type */
19195 NULL_TREE, /* base_decl */
19196 NULL, /* dpi */
19197 false, /* placeholder_arg */
19198 false /* placeholder_seen */
19200 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19202 /* We want a DWARF expression: abort if we only have a location list with
19203 multiple elements. */
19204 if (!loc_result || !single_element_loc_list_p (loc_result))
19205 return NULL;
19206 else
19207 return loc_result->expr;
19210 /* The following routines define various Dwarf attributes and any data
19211 associated with them. */
19213 /* Add a location description attribute value to a DIE.
19215 This emits location attributes suitable for whole variables and
19216 whole parameters. Note that the location attributes for struct fields are
19217 generated by the routine `data_member_location_attribute' below. */
19219 static inline void
19220 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19221 dw_loc_list_ref descr)
19223 bool check_no_locviews = true;
19224 if (descr == 0)
19225 return;
19226 if (single_element_loc_list_p (descr))
19227 add_AT_loc (die, attr_kind, descr->expr);
19228 else
19230 add_AT_loc_list (die, attr_kind, descr);
19231 gcc_assert (descr->ll_symbol);
19232 if (attr_kind == DW_AT_location && descr->vl_symbol
19233 && dwarf2out_locviews_in_attribute ())
19235 add_AT_view_list (die, DW_AT_GNU_locviews);
19236 check_no_locviews = false;
19240 if (check_no_locviews)
19241 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19244 /* Add DW_AT_accessibility attribute to DIE if needed. */
19246 static void
19247 add_accessibility_attribute (dw_die_ref die, tree decl)
19249 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19250 children, otherwise the default is DW_ACCESS_public. In DWARF2
19251 the default has always been DW_ACCESS_public. */
19252 if (TREE_PROTECTED (decl))
19253 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19254 else if (TREE_PRIVATE (decl))
19256 if (dwarf_version == 2
19257 || die->die_parent == NULL
19258 || die->die_parent->die_tag != DW_TAG_class_type)
19259 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19261 else if (dwarf_version > 2
19262 && die->die_parent
19263 && die->die_parent->die_tag == DW_TAG_class_type)
19264 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19267 /* Attach the specialized form of location attribute used for data members of
19268 struct and union types. In the special case of a FIELD_DECL node which
19269 represents a bit-field, the "offset" part of this special location
19270 descriptor must indicate the distance in bytes from the lowest-addressed
19271 byte of the containing struct or union type to the lowest-addressed byte of
19272 the "containing object" for the bit-field. (See the `field_byte_offset'
19273 function above).
19275 For any given bit-field, the "containing object" is a hypothetical object
19276 (of some integral or enum type) within which the given bit-field lives. The
19277 type of this hypothetical "containing object" is always the same as the
19278 declared type of the individual bit-field itself (for GCC anyway... the
19279 DWARF spec doesn't actually mandate this). Note that it is the size (in
19280 bytes) of the hypothetical "containing object" which will be given in the
19281 DW_AT_byte_size attribute for this bit-field. (See the
19282 `byte_size_attribute' function below.) It is also used when calculating the
19283 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19284 function below.)
19286 CTX is required: see the comment for VLR_CONTEXT. */
19288 static void
19289 add_data_member_location_attribute (dw_die_ref die,
19290 tree decl,
19291 struct vlr_context *ctx)
19293 HOST_WIDE_INT offset;
19294 dw_loc_descr_ref loc_descr = 0;
19296 if (TREE_CODE (decl) == TREE_BINFO)
19298 /* We're working on the TAG_inheritance for a base class. */
19299 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19301 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19302 aren't at a fixed offset from all (sub)objects of the same
19303 type. We need to extract the appropriate offset from our
19304 vtable. The following dwarf expression means
19306 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19308 This is specific to the V3 ABI, of course. */
19310 dw_loc_descr_ref tmp;
19312 /* Make a copy of the object address. */
19313 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19314 add_loc_descr (&loc_descr, tmp);
19316 /* Extract the vtable address. */
19317 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19318 add_loc_descr (&loc_descr, tmp);
19320 /* Calculate the address of the offset. */
19321 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19322 gcc_assert (offset < 0);
19324 tmp = int_loc_descriptor (-offset);
19325 add_loc_descr (&loc_descr, tmp);
19326 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19327 add_loc_descr (&loc_descr, tmp);
19329 /* Extract the offset. */
19330 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19331 add_loc_descr (&loc_descr, tmp);
19333 /* Add it to the object address. */
19334 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19335 add_loc_descr (&loc_descr, tmp);
19337 else
19338 offset = tree_to_shwi (BINFO_OFFSET (decl));
19340 else
19342 loc_descr = field_byte_offset (decl, ctx, &offset);
19344 /* If loc_descr is available then we know the field offset is dynamic.
19345 However, GDB does not handle dynamic field offsets very well at the
19346 moment. */
19347 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
19349 loc_descr = NULL;
19350 offset = 0;
19353 /* Data member location evalutation starts with the base address on the
19354 stack. Compute the field offset and add it to this base address. */
19355 else if (loc_descr != NULL)
19356 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19359 if (! loc_descr)
19361 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19362 e.g. GDB only added support to it in November 2016. For DWARF5
19363 we need newer debug info consumers anyway. We might change this
19364 to dwarf_version >= 4 once most consumers catched up. */
19365 if (dwarf_version >= 5
19366 && TREE_CODE (decl) == FIELD_DECL
19367 && DECL_BIT_FIELD_TYPE (decl))
19369 tree off = bit_position (decl);
19370 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19372 remove_AT (die, DW_AT_byte_size);
19373 remove_AT (die, DW_AT_bit_offset);
19374 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19375 return;
19378 if (dwarf_version > 2)
19380 /* Don't need to output a location expression, just the constant. */
19381 if (offset < 0)
19382 add_AT_int (die, DW_AT_data_member_location, offset);
19383 else
19384 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19385 return;
19387 else
19389 enum dwarf_location_atom op;
19391 /* The DWARF2 standard says that we should assume that the structure
19392 address is already on the stack, so we can specify a structure
19393 field address by using DW_OP_plus_uconst. */
19394 op = DW_OP_plus_uconst;
19395 loc_descr = new_loc_descr (op, offset, 0);
19399 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19402 /* Writes integer values to dw_vec_const array. */
19404 static void
19405 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19407 while (size != 0)
19409 *dest++ = val & 0xff;
19410 val >>= 8;
19411 --size;
19415 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
19417 static HOST_WIDE_INT
19418 extract_int (const unsigned char *src, unsigned int size)
19420 HOST_WIDE_INT val = 0;
19422 src += size;
19423 while (size != 0)
19425 val <<= 8;
19426 val |= *--src & 0xff;
19427 --size;
19429 return val;
19432 /* Writes wide_int values to dw_vec_const array. */
19434 static void
19435 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
19437 int i;
19439 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
19441 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
19442 return;
19445 /* We'd have to extend this code to support odd sizes. */
19446 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
19448 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
19450 if (WORDS_BIG_ENDIAN)
19451 for (i = n - 1; i >= 0; i--)
19453 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19454 dest += sizeof (HOST_WIDE_INT);
19456 else
19457 for (i = 0; i < n; i++)
19459 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19460 dest += sizeof (HOST_WIDE_INT);
19464 /* Writes floating point values to dw_vec_const array. */
19466 static void
19467 insert_float (const_rtx rtl, unsigned char *array)
19469 long val[4];
19470 int i;
19471 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19473 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
19475 /* real_to_target puts 32-bit pieces in each long. Pack them. */
19476 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
19478 insert_int (val[i], 4, array);
19479 array += 4;
19483 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
19484 does not have a "location" either in memory or in a register. These
19485 things can arise in GNU C when a constant is passed as an actual parameter
19486 to an inlined function. They can also arise in C++ where declared
19487 constants do not necessarily get memory "homes". */
19489 static bool
19490 add_const_value_attribute (dw_die_ref die, rtx rtl)
19492 switch (GET_CODE (rtl))
19494 case CONST_INT:
19496 HOST_WIDE_INT val = INTVAL (rtl);
19498 if (val < 0)
19499 add_AT_int (die, DW_AT_const_value, val);
19500 else
19501 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
19503 return true;
19505 case CONST_WIDE_INT:
19507 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
19508 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
19509 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
19510 wide_int w = wi::zext (w1, prec);
19511 add_AT_wide (die, DW_AT_const_value, w);
19513 return true;
19515 case CONST_DOUBLE:
19516 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
19517 floating-point constant. A CONST_DOUBLE is used whenever the
19518 constant requires more than one word in order to be adequately
19519 represented. */
19520 if (TARGET_SUPPORTS_WIDE_INT == 0
19521 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
19522 add_AT_double (die, DW_AT_const_value,
19523 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
19524 else
19526 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19527 unsigned int length = GET_MODE_SIZE (mode);
19528 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
19530 insert_float (rtl, array);
19531 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
19533 return true;
19535 case CONST_VECTOR:
19537 unsigned int length;
19538 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
19539 return false;
19541 machine_mode mode = GET_MODE (rtl);
19542 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
19543 unsigned char *array
19544 = ggc_vec_alloc<unsigned char> (length * elt_size);
19545 unsigned int i;
19546 unsigned char *p;
19547 machine_mode imode = GET_MODE_INNER (mode);
19549 switch (GET_MODE_CLASS (mode))
19551 case MODE_VECTOR_INT:
19552 for (i = 0, p = array; i < length; i++, p += elt_size)
19554 rtx elt = CONST_VECTOR_ELT (rtl, i);
19555 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
19557 break;
19559 case MODE_VECTOR_FLOAT:
19560 for (i = 0, p = array; i < length; i++, p += elt_size)
19562 rtx elt = CONST_VECTOR_ELT (rtl, i);
19563 insert_float (elt, p);
19565 break;
19567 default:
19568 gcc_unreachable ();
19571 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
19573 return true;
19575 case CONST_STRING:
19576 if (dwarf_version >= 4 || !dwarf_strict)
19578 dw_loc_descr_ref loc_result;
19579 resolve_one_addr (&rtl);
19580 rtl_addr:
19581 loc_result = new_addr_loc_descr (rtl, dtprel_false);
19582 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
19583 add_AT_loc (die, DW_AT_location, loc_result);
19584 vec_safe_push (used_rtx_array, rtl);
19585 return true;
19587 return false;
19589 case CONST:
19590 if (CONSTANT_P (XEXP (rtl, 0)))
19591 return add_const_value_attribute (die, XEXP (rtl, 0));
19592 /* FALLTHROUGH */
19593 case SYMBOL_REF:
19594 if (!const_ok_for_output (rtl))
19595 return false;
19596 /* FALLTHROUGH */
19597 case LABEL_REF:
19598 if (dwarf_version >= 4 || !dwarf_strict)
19599 goto rtl_addr;
19600 return false;
19602 case PLUS:
19603 /* In cases where an inlined instance of an inline function is passed
19604 the address of an `auto' variable (which is local to the caller) we
19605 can get a situation where the DECL_RTL of the artificial local
19606 variable (for the inlining) which acts as a stand-in for the
19607 corresponding formal parameter (of the inline function) will look
19608 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
19609 exactly a compile-time constant expression, but it isn't the address
19610 of the (artificial) local variable either. Rather, it represents the
19611 *value* which the artificial local variable always has during its
19612 lifetime. We currently have no way to represent such quasi-constant
19613 values in Dwarf, so for now we just punt and generate nothing. */
19614 return false;
19616 case HIGH:
19617 case CONST_FIXED:
19618 return false;
19620 case MEM:
19621 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
19622 && MEM_READONLY_P (rtl)
19623 && GET_MODE (rtl) == BLKmode)
19625 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
19626 return true;
19628 return false;
19630 default:
19631 /* No other kinds of rtx should be possible here. */
19632 gcc_unreachable ();
19634 return false;
19637 /* Determine whether the evaluation of EXPR references any variables
19638 or functions which aren't otherwise used (and therefore may not be
19639 output). */
19640 static tree
19641 reference_to_unused (tree * tp, int * walk_subtrees,
19642 void * data ATTRIBUTE_UNUSED)
19644 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
19645 *walk_subtrees = 0;
19647 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
19648 && ! TREE_ASM_WRITTEN (*tp))
19649 return *tp;
19650 /* ??? The C++ FE emits debug information for using decls, so
19651 putting gcc_unreachable here falls over. See PR31899. For now
19652 be conservative. */
19653 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
19654 return *tp;
19655 else if (VAR_P (*tp))
19657 varpool_node *node = varpool_node::get (*tp);
19658 if (!node || !node->definition)
19659 return *tp;
19661 else if (TREE_CODE (*tp) == FUNCTION_DECL
19662 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
19664 /* The call graph machinery must have finished analyzing,
19665 optimizing and gimplifying the CU by now.
19666 So if *TP has no call graph node associated
19667 to it, it means *TP will not be emitted. */
19668 if (!cgraph_node::get (*tp))
19669 return *tp;
19671 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
19672 return *tp;
19674 return NULL_TREE;
19677 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
19678 for use in a later add_const_value_attribute call. */
19680 static rtx
19681 rtl_for_decl_init (tree init, tree type)
19683 rtx rtl = NULL_RTX;
19685 STRIP_NOPS (init);
19687 /* If a variable is initialized with a string constant without embedded
19688 zeros, build CONST_STRING. */
19689 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
19691 tree enttype = TREE_TYPE (type);
19692 tree domain = TYPE_DOMAIN (type);
19693 scalar_int_mode mode;
19695 if (is_int_mode (TYPE_MODE (enttype), &mode)
19696 && GET_MODE_SIZE (mode) == 1
19697 && domain
19698 && TYPE_MAX_VALUE (domain)
19699 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
19700 && integer_zerop (TYPE_MIN_VALUE (domain))
19701 && compare_tree_int (TYPE_MAX_VALUE (domain),
19702 TREE_STRING_LENGTH (init) - 1) == 0
19703 && ((size_t) TREE_STRING_LENGTH (init)
19704 == strlen (TREE_STRING_POINTER (init)) + 1))
19706 rtl = gen_rtx_CONST_STRING (VOIDmode,
19707 ggc_strdup (TREE_STRING_POINTER (init)));
19708 rtl = gen_rtx_MEM (BLKmode, rtl);
19709 MEM_READONLY_P (rtl) = 1;
19712 /* Other aggregates, and complex values, could be represented using
19713 CONCAT: FIXME! */
19714 else if (AGGREGATE_TYPE_P (type)
19715 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
19716 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
19717 || TREE_CODE (type) == COMPLEX_TYPE)
19719 /* Vectors only work if their mode is supported by the target.
19720 FIXME: generic vectors ought to work too. */
19721 else if (TREE_CODE (type) == VECTOR_TYPE
19722 && !VECTOR_MODE_P (TYPE_MODE (type)))
19724 /* If the initializer is something that we know will expand into an
19725 immediate RTL constant, expand it now. We must be careful not to
19726 reference variables which won't be output. */
19727 else if (initializer_constant_valid_p (init, type)
19728 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19730 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19731 possible. */
19732 if (TREE_CODE (type) == VECTOR_TYPE)
19733 switch (TREE_CODE (init))
19735 case VECTOR_CST:
19736 break;
19737 case CONSTRUCTOR:
19738 if (TREE_CONSTANT (init))
19740 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19741 bool constant_p = true;
19742 tree value;
19743 unsigned HOST_WIDE_INT ix;
19745 /* Even when ctor is constant, it might contain non-*_CST
19746 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19747 belong into VECTOR_CST nodes. */
19748 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19749 if (!CONSTANT_CLASS_P (value))
19751 constant_p = false;
19752 break;
19755 if (constant_p)
19757 init = build_vector_from_ctor (type, elts);
19758 break;
19761 /* FALLTHRU */
19763 default:
19764 return NULL;
19767 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19769 /* If expand_expr returns a MEM, it wasn't immediate. */
19770 gcc_assert (!rtl || !MEM_P (rtl));
19773 return rtl;
19776 /* Generate RTL for the variable DECL to represent its location. */
19778 static rtx
19779 rtl_for_decl_location (tree decl)
19781 rtx rtl;
19783 /* Here we have to decide where we are going to say the parameter "lives"
19784 (as far as the debugger is concerned). We only have a couple of
19785 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19787 DECL_RTL normally indicates where the parameter lives during most of the
19788 activation of the function. If optimization is enabled however, this
19789 could be either NULL or else a pseudo-reg. Both of those cases indicate
19790 that the parameter doesn't really live anywhere (as far as the code
19791 generation parts of GCC are concerned) during most of the function's
19792 activation. That will happen (for example) if the parameter is never
19793 referenced within the function.
19795 We could just generate a location descriptor here for all non-NULL
19796 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19797 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19798 where DECL_RTL is NULL or is a pseudo-reg.
19800 Note however that we can only get away with using DECL_INCOMING_RTL as
19801 a backup substitute for DECL_RTL in certain limited cases. In cases
19802 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19803 we can be sure that the parameter was passed using the same type as it is
19804 declared to have within the function, and that its DECL_INCOMING_RTL
19805 points us to a place where a value of that type is passed.
19807 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19808 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19809 because in these cases DECL_INCOMING_RTL points us to a value of some
19810 type which is *different* from the type of the parameter itself. Thus,
19811 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19812 such cases, the debugger would end up (for example) trying to fetch a
19813 `float' from a place which actually contains the first part of a
19814 `double'. That would lead to really incorrect and confusing
19815 output at debug-time.
19817 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19818 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19819 are a couple of exceptions however. On little-endian machines we can
19820 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19821 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19822 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19823 when (on a little-endian machine) a non-prototyped function has a
19824 parameter declared to be of type `short' or `char'. In such cases,
19825 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19826 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19827 passed `int' value. If the debugger then uses that address to fetch
19828 a `short' or a `char' (on a little-endian machine) the result will be
19829 the correct data, so we allow for such exceptional cases below.
19831 Note that our goal here is to describe the place where the given formal
19832 parameter lives during most of the function's activation (i.e. between the
19833 end of the prologue and the start of the epilogue). We'll do that as best
19834 as we can. Note however that if the given formal parameter is modified
19835 sometime during the execution of the function, then a stack backtrace (at
19836 debug-time) will show the function as having been called with the *new*
19837 value rather than the value which was originally passed in. This happens
19838 rarely enough that it is not a major problem, but it *is* a problem, and
19839 I'd like to fix it.
19841 A future version of dwarf2out.c may generate two additional attributes for
19842 any given DW_TAG_formal_parameter DIE which will describe the "passed
19843 type" and the "passed location" for the given formal parameter in addition
19844 to the attributes we now generate to indicate the "declared type" and the
19845 "active location" for each parameter. This additional set of attributes
19846 could be used by debuggers for stack backtraces. Separately, note that
19847 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19848 This happens (for example) for inlined-instances of inline function formal
19849 parameters which are never referenced. This really shouldn't be
19850 happening. All PARM_DECL nodes should get valid non-NULL
19851 DECL_INCOMING_RTL values. FIXME. */
19853 /* Use DECL_RTL as the "location" unless we find something better. */
19854 rtl = DECL_RTL_IF_SET (decl);
19856 /* When generating abstract instances, ignore everything except
19857 constants, symbols living in memory, and symbols living in
19858 fixed registers. */
19859 if (! reload_completed)
19861 if (rtl
19862 && (CONSTANT_P (rtl)
19863 || (MEM_P (rtl)
19864 && CONSTANT_P (XEXP (rtl, 0)))
19865 || (REG_P (rtl)
19866 && VAR_P (decl)
19867 && TREE_STATIC (decl))))
19869 rtl = targetm.delegitimize_address (rtl);
19870 return rtl;
19872 rtl = NULL_RTX;
19874 else if (TREE_CODE (decl) == PARM_DECL)
19876 if (rtl == NULL_RTX
19877 || is_pseudo_reg (rtl)
19878 || (MEM_P (rtl)
19879 && is_pseudo_reg (XEXP (rtl, 0))
19880 && DECL_INCOMING_RTL (decl)
19881 && MEM_P (DECL_INCOMING_RTL (decl))
19882 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19884 tree declared_type = TREE_TYPE (decl);
19885 tree passed_type = DECL_ARG_TYPE (decl);
19886 machine_mode dmode = TYPE_MODE (declared_type);
19887 machine_mode pmode = TYPE_MODE (passed_type);
19889 /* This decl represents a formal parameter which was optimized out.
19890 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19891 all cases where (rtl == NULL_RTX) just below. */
19892 if (dmode == pmode)
19893 rtl = DECL_INCOMING_RTL (decl);
19894 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19895 && SCALAR_INT_MODE_P (dmode)
19896 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
19897 && DECL_INCOMING_RTL (decl))
19899 rtx inc = DECL_INCOMING_RTL (decl);
19900 if (REG_P (inc))
19901 rtl = inc;
19902 else if (MEM_P (inc))
19904 if (BYTES_BIG_ENDIAN)
19905 rtl = adjust_address_nv (inc, dmode,
19906 GET_MODE_SIZE (pmode)
19907 - GET_MODE_SIZE (dmode));
19908 else
19909 rtl = inc;
19914 /* If the parm was passed in registers, but lives on the stack, then
19915 make a big endian correction if the mode of the type of the
19916 parameter is not the same as the mode of the rtl. */
19917 /* ??? This is the same series of checks that are made in dbxout.c before
19918 we reach the big endian correction code there. It isn't clear if all
19919 of these checks are necessary here, but keeping them all is the safe
19920 thing to do. */
19921 else if (MEM_P (rtl)
19922 && XEXP (rtl, 0) != const0_rtx
19923 && ! CONSTANT_P (XEXP (rtl, 0))
19924 /* Not passed in memory. */
19925 && !MEM_P (DECL_INCOMING_RTL (decl))
19926 /* Not passed by invisible reference. */
19927 && (!REG_P (XEXP (rtl, 0))
19928 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19929 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19930 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19931 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19932 #endif
19934 /* Big endian correction check. */
19935 && BYTES_BIG_ENDIAN
19936 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19937 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
19938 UNITS_PER_WORD))
19940 machine_mode addr_mode = get_address_mode (rtl);
19941 poly_int64 offset = (UNITS_PER_WORD
19942 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19944 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19945 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19948 else if (VAR_P (decl)
19949 && rtl
19950 && MEM_P (rtl)
19951 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19953 machine_mode addr_mode = get_address_mode (rtl);
19954 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19955 GET_MODE (rtl));
19957 /* If a variable is declared "register" yet is smaller than
19958 a register, then if we store the variable to memory, it
19959 looks like we're storing a register-sized value, when in
19960 fact we are not. We need to adjust the offset of the
19961 storage location to reflect the actual value's bytes,
19962 else gdb will not be able to display it. */
19963 if (maybe_ne (offset, 0))
19964 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19965 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19968 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19969 and will have been substituted directly into all expressions that use it.
19970 C does not have such a concept, but C++ and other languages do. */
19971 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19972 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19974 if (rtl)
19975 rtl = targetm.delegitimize_address (rtl);
19977 /* If we don't look past the constant pool, we risk emitting a
19978 reference to a constant pool entry that isn't referenced from
19979 code, and thus is not emitted. */
19980 if (rtl)
19981 rtl = avoid_constant_pool_reference (rtl);
19983 /* Try harder to get a rtl. If this symbol ends up not being emitted
19984 in the current CU, resolve_addr will remove the expression referencing
19985 it. */
19986 if (rtl == NULL_RTX
19987 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
19988 && VAR_P (decl)
19989 && !DECL_EXTERNAL (decl)
19990 && TREE_STATIC (decl)
19991 && DECL_NAME (decl)
19992 && !DECL_HARD_REGISTER (decl)
19993 && DECL_MODE (decl) != VOIDmode)
19995 rtl = make_decl_rtl_for_debug (decl);
19996 if (!MEM_P (rtl)
19997 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19998 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19999 rtl = NULL_RTX;
20002 return rtl;
20005 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20006 returned. If so, the decl for the COMMON block is returned, and the
20007 value is the offset into the common block for the symbol. */
20009 static tree
20010 fortran_common (tree decl, HOST_WIDE_INT *value)
20012 tree val_expr, cvar;
20013 machine_mode mode;
20014 poly_int64 bitsize, bitpos;
20015 tree offset;
20016 HOST_WIDE_INT cbitpos;
20017 int unsignedp, reversep, volatilep = 0;
20019 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20020 it does not have a value (the offset into the common area), or if it
20021 is thread local (as opposed to global) then it isn't common, and shouldn't
20022 be handled as such. */
20023 if (!VAR_P (decl)
20024 || !TREE_STATIC (decl)
20025 || !DECL_HAS_VALUE_EXPR_P (decl)
20026 || !is_fortran ())
20027 return NULL_TREE;
20029 val_expr = DECL_VALUE_EXPR (decl);
20030 if (TREE_CODE (val_expr) != COMPONENT_REF)
20031 return NULL_TREE;
20033 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20034 &unsignedp, &reversep, &volatilep);
20036 if (cvar == NULL_TREE
20037 || !VAR_P (cvar)
20038 || DECL_ARTIFICIAL (cvar)
20039 || !TREE_PUBLIC (cvar)
20040 /* We don't expect to have to cope with variable offsets,
20041 since at present all static data must have a constant size. */
20042 || !bitpos.is_constant (&cbitpos))
20043 return NULL_TREE;
20045 *value = 0;
20046 if (offset != NULL)
20048 if (!tree_fits_shwi_p (offset))
20049 return NULL_TREE;
20050 *value = tree_to_shwi (offset);
20052 if (cbitpos != 0)
20053 *value += cbitpos / BITS_PER_UNIT;
20055 return cvar;
20058 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20059 data attribute for a variable or a parameter. We generate the
20060 DW_AT_const_value attribute only in those cases where the given variable
20061 or parameter does not have a true "location" either in memory or in a
20062 register. This can happen (for example) when a constant is passed as an
20063 actual argument in a call to an inline function. (It's possible that
20064 these things can crop up in other ways also.) Note that one type of
20065 constant value which can be passed into an inlined function is a constant
20066 pointer. This can happen for example if an actual argument in an inlined
20067 function call evaluates to a compile-time constant address.
20069 CACHE_P is true if it is worth caching the location list for DECL,
20070 so that future calls can reuse it rather than regenerate it from scratch.
20071 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20072 since we will need to refer to them each time the function is inlined. */
20074 static bool
20075 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20077 rtx rtl;
20078 dw_loc_list_ref list;
20079 var_loc_list *loc_list;
20080 cached_dw_loc_list *cache;
20082 if (early_dwarf)
20083 return false;
20085 if (TREE_CODE (decl) == ERROR_MARK)
20086 return false;
20088 if (get_AT (die, DW_AT_location)
20089 || get_AT (die, DW_AT_const_value))
20090 return true;
20092 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20093 || TREE_CODE (decl) == RESULT_DECL);
20095 /* Try to get some constant RTL for this decl, and use that as the value of
20096 the location. */
20098 rtl = rtl_for_decl_location (decl);
20099 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20100 && add_const_value_attribute (die, rtl))
20101 return true;
20103 /* See if we have single element location list that is equivalent to
20104 a constant value. That way we are better to use add_const_value_attribute
20105 rather than expanding constant value equivalent. */
20106 loc_list = lookup_decl_loc (decl);
20107 if (loc_list
20108 && loc_list->first
20109 && loc_list->first->next == NULL
20110 && NOTE_P (loc_list->first->loc)
20111 && NOTE_VAR_LOCATION (loc_list->first->loc)
20112 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20114 struct var_loc_node *node;
20116 node = loc_list->first;
20117 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20118 if (GET_CODE (rtl) == EXPR_LIST)
20119 rtl = XEXP (rtl, 0);
20120 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20121 && add_const_value_attribute (die, rtl))
20122 return true;
20124 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20125 list several times. See if we've already cached the contents. */
20126 list = NULL;
20127 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20128 cache_p = false;
20129 if (cache_p)
20131 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20132 if (cache)
20133 list = cache->loc_list;
20135 if (list == NULL)
20137 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20138 NULL);
20139 /* It is usually worth caching this result if the decl is from
20140 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20141 if (cache_p && list && list->dw_loc_next)
20143 cached_dw_loc_list **slot
20144 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20145 DECL_UID (decl),
20146 INSERT);
20147 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20148 cache->decl_id = DECL_UID (decl);
20149 cache->loc_list = list;
20150 *slot = cache;
20153 if (list)
20155 add_AT_location_description (die, DW_AT_location, list);
20156 return true;
20158 /* None of that worked, so it must not really have a location;
20159 try adding a constant value attribute from the DECL_INITIAL. */
20160 return tree_add_const_value_attribute_for_decl (die, decl);
20163 /* Helper function for tree_add_const_value_attribute. Natively encode
20164 initializer INIT into an array. Return true if successful. */
20166 static bool
20167 native_encode_initializer (tree init, unsigned char *array, int size)
20169 tree type;
20171 if (init == NULL_TREE)
20172 return false;
20174 STRIP_NOPS (init);
20175 switch (TREE_CODE (init))
20177 case STRING_CST:
20178 type = TREE_TYPE (init);
20179 if (TREE_CODE (type) == ARRAY_TYPE)
20181 tree enttype = TREE_TYPE (type);
20182 scalar_int_mode mode;
20184 if (!is_int_mode (TYPE_MODE (enttype), &mode)
20185 || GET_MODE_SIZE (mode) != 1)
20186 return false;
20187 if (int_size_in_bytes (type) != size)
20188 return false;
20189 if (size > TREE_STRING_LENGTH (init))
20191 memcpy (array, TREE_STRING_POINTER (init),
20192 TREE_STRING_LENGTH (init));
20193 memset (array + TREE_STRING_LENGTH (init),
20194 '\0', size - TREE_STRING_LENGTH (init));
20196 else
20197 memcpy (array, TREE_STRING_POINTER (init), size);
20198 return true;
20200 return false;
20201 case CONSTRUCTOR:
20202 type = TREE_TYPE (init);
20203 if (int_size_in_bytes (type) != size)
20204 return false;
20205 if (TREE_CODE (type) == ARRAY_TYPE)
20207 HOST_WIDE_INT min_index;
20208 unsigned HOST_WIDE_INT cnt;
20209 int curpos = 0, fieldsize;
20210 constructor_elt *ce;
20212 if (TYPE_DOMAIN (type) == NULL_TREE
20213 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
20214 return false;
20216 fieldsize = int_size_in_bytes (TREE_TYPE (type));
20217 if (fieldsize <= 0)
20218 return false;
20220 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
20221 memset (array, '\0', size);
20222 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20224 tree val = ce->value;
20225 tree index = ce->index;
20226 int pos = curpos;
20227 if (index && TREE_CODE (index) == RANGE_EXPR)
20228 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
20229 * fieldsize;
20230 else if (index)
20231 pos = (tree_to_shwi (index) - min_index) * fieldsize;
20233 if (val)
20235 STRIP_NOPS (val);
20236 if (!native_encode_initializer (val, array + pos, fieldsize))
20237 return false;
20239 curpos = pos + fieldsize;
20240 if (index && TREE_CODE (index) == RANGE_EXPR)
20242 int count = tree_to_shwi (TREE_OPERAND (index, 1))
20243 - tree_to_shwi (TREE_OPERAND (index, 0));
20244 while (count-- > 0)
20246 if (val)
20247 memcpy (array + curpos, array + pos, fieldsize);
20248 curpos += fieldsize;
20251 gcc_assert (curpos <= size);
20253 return true;
20255 else if (TREE_CODE (type) == RECORD_TYPE
20256 || TREE_CODE (type) == UNION_TYPE)
20258 tree field = NULL_TREE;
20259 unsigned HOST_WIDE_INT cnt;
20260 constructor_elt *ce;
20262 if (int_size_in_bytes (type) != size)
20263 return false;
20265 if (TREE_CODE (type) == RECORD_TYPE)
20266 field = TYPE_FIELDS (type);
20268 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20270 tree val = ce->value;
20271 int pos, fieldsize;
20273 if (ce->index != 0)
20274 field = ce->index;
20276 if (val)
20277 STRIP_NOPS (val);
20279 if (field == NULL_TREE || DECL_BIT_FIELD (field))
20280 return false;
20282 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
20283 && TYPE_DOMAIN (TREE_TYPE (field))
20284 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
20285 return false;
20286 else if (DECL_SIZE_UNIT (field) == NULL_TREE
20287 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
20288 return false;
20289 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
20290 pos = int_byte_position (field);
20291 gcc_assert (pos + fieldsize <= size);
20292 if (val && fieldsize != 0
20293 && !native_encode_initializer (val, array + pos, fieldsize))
20294 return false;
20296 return true;
20298 return false;
20299 case VIEW_CONVERT_EXPR:
20300 case NON_LVALUE_EXPR:
20301 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
20302 default:
20303 return native_encode_expr (init, array, size) == size;
20307 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20308 attribute is the const value T. */
20310 static bool
20311 tree_add_const_value_attribute (dw_die_ref die, tree t)
20313 tree init;
20314 tree type = TREE_TYPE (t);
20315 rtx rtl;
20317 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20318 return false;
20320 init = t;
20321 gcc_assert (!DECL_P (init));
20323 if (TREE_CODE (init) == INTEGER_CST)
20325 if (tree_fits_uhwi_p (init))
20327 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20328 return true;
20330 if (tree_fits_shwi_p (init))
20332 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20333 return true;
20336 if (! early_dwarf)
20338 rtl = rtl_for_decl_init (init, type);
20339 if (rtl)
20340 return add_const_value_attribute (die, rtl);
20342 /* If the host and target are sane, try harder. */
20343 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20344 && initializer_constant_valid_p (init, type))
20346 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20347 if (size > 0 && (int) size == size)
20349 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20351 if (native_encode_initializer (init, array, size))
20353 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20354 return true;
20356 ggc_free (array);
20359 return false;
20362 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20363 attribute is the const value of T, where T is an integral constant
20364 variable with static storage duration
20365 (so it can't be a PARM_DECL or a RESULT_DECL). */
20367 static bool
20368 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20371 if (!decl
20372 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20373 || (VAR_P (decl) && !TREE_STATIC (decl)))
20374 return false;
20376 if (TREE_READONLY (decl)
20377 && ! TREE_THIS_VOLATILE (decl)
20378 && DECL_INITIAL (decl))
20379 /* OK */;
20380 else
20381 return false;
20383 /* Don't add DW_AT_const_value if abstract origin already has one. */
20384 if (get_AT (var_die, DW_AT_const_value))
20385 return false;
20387 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20390 /* Convert the CFI instructions for the current function into a
20391 location list. This is used for DW_AT_frame_base when we targeting
20392 a dwarf2 consumer that does not support the dwarf3
20393 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20394 expressions. */
20396 static dw_loc_list_ref
20397 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20399 int ix;
20400 dw_fde_ref fde;
20401 dw_loc_list_ref list, *list_tail;
20402 dw_cfi_ref cfi;
20403 dw_cfa_location last_cfa, next_cfa;
20404 const char *start_label, *last_label, *section;
20405 dw_cfa_location remember;
20407 fde = cfun->fde;
20408 gcc_assert (fde != NULL);
20410 section = secname_for_decl (current_function_decl);
20411 list_tail = &list;
20412 list = NULL;
20414 memset (&next_cfa, 0, sizeof (next_cfa));
20415 next_cfa.reg = INVALID_REGNUM;
20416 remember = next_cfa;
20418 start_label = fde->dw_fde_begin;
20420 /* ??? Bald assumption that the CIE opcode list does not contain
20421 advance opcodes. */
20422 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20423 lookup_cfa_1 (cfi, &next_cfa, &remember);
20425 last_cfa = next_cfa;
20426 last_label = start_label;
20428 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20430 /* If the first partition contained no CFI adjustments, the
20431 CIE opcodes apply to the whole first partition. */
20432 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20433 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20434 list_tail =&(*list_tail)->dw_loc_next;
20435 start_label = last_label = fde->dw_fde_second_begin;
20438 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20440 switch (cfi->dw_cfi_opc)
20442 case DW_CFA_set_loc:
20443 case DW_CFA_advance_loc1:
20444 case DW_CFA_advance_loc2:
20445 case DW_CFA_advance_loc4:
20446 if (!cfa_equal_p (&last_cfa, &next_cfa))
20448 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20449 start_label, 0, last_label, 0, section);
20451 list_tail = &(*list_tail)->dw_loc_next;
20452 last_cfa = next_cfa;
20453 start_label = last_label;
20455 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20456 break;
20458 case DW_CFA_advance_loc:
20459 /* The encoding is complex enough that we should never emit this. */
20460 gcc_unreachable ();
20462 default:
20463 lookup_cfa_1 (cfi, &next_cfa, &remember);
20464 break;
20466 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20468 if (!cfa_equal_p (&last_cfa, &next_cfa))
20470 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20471 start_label, 0, last_label, 0, section);
20473 list_tail = &(*list_tail)->dw_loc_next;
20474 last_cfa = next_cfa;
20475 start_label = last_label;
20477 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20478 start_label, 0, fde->dw_fde_end, 0, section);
20479 list_tail = &(*list_tail)->dw_loc_next;
20480 start_label = last_label = fde->dw_fde_second_begin;
20484 if (!cfa_equal_p (&last_cfa, &next_cfa))
20486 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20487 start_label, 0, last_label, 0, section);
20488 list_tail = &(*list_tail)->dw_loc_next;
20489 start_label = last_label;
20492 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20493 start_label, 0,
20494 fde->dw_fde_second_begin
20495 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20496 section);
20498 maybe_gen_llsym (list);
20500 return list;
20503 /* Compute a displacement from the "steady-state frame pointer" to the
20504 frame base (often the same as the CFA), and store it in
20505 frame_pointer_fb_offset. OFFSET is added to the displacement
20506 before the latter is negated. */
20508 static void
20509 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20511 rtx reg, elim;
20513 #ifdef FRAME_POINTER_CFA_OFFSET
20514 reg = frame_pointer_rtx;
20515 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20516 #else
20517 reg = arg_pointer_rtx;
20518 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20519 #endif
20521 elim = (ira_use_lra_p
20522 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20523 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20524 elim = strip_offset_and_add (elim, &offset);
20526 frame_pointer_fb_offset = -offset;
20528 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20529 in which to eliminate. This is because it's stack pointer isn't
20530 directly accessible as a register within the ISA. To work around
20531 this, assume that while we cannot provide a proper value for
20532 frame_pointer_fb_offset, we won't need one either. We can use
20533 hard frame pointer in debug info even if frame pointer isn't used
20534 since hard frame pointer in debug info is encoded with DW_OP_fbreg
20535 which uses the DW_AT_frame_base attribute, not hard frame pointer
20536 directly. */
20537 frame_pointer_fb_offset_valid
20538 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
20541 /* Generate a DW_AT_name attribute given some string value to be included as
20542 the value of the attribute. */
20544 static void
20545 add_name_attribute (dw_die_ref die, const char *name_string)
20547 if (name_string != NULL && *name_string != 0)
20549 if (demangle_name_func)
20550 name_string = (*demangle_name_func) (name_string);
20552 add_AT_string (die, DW_AT_name, name_string);
20556 /* Generate a DW_AT_description attribute given some string value to be included
20557 as the value of the attribute. */
20559 static void
20560 add_desc_attribute (dw_die_ref die, const char *name_string)
20562 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20563 return;
20565 if (name_string == NULL || *name_string == 0)
20566 return;
20568 if (demangle_name_func)
20569 name_string = (*demangle_name_func) (name_string);
20571 add_AT_string (die, DW_AT_description, name_string);
20574 /* Generate a DW_AT_description attribute given some decl to be included
20575 as the value of the attribute. */
20577 static void
20578 add_desc_attribute (dw_die_ref die, tree decl)
20580 tree decl_name;
20582 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20583 return;
20585 if (decl == NULL_TREE || !DECL_P (decl))
20586 return;
20587 decl_name = DECL_NAME (decl);
20589 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20591 const char *name = dwarf2_name (decl, 0);
20592 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
20594 else
20596 char *desc = print_generic_expr_to_str (decl);
20597 add_desc_attribute (die, desc);
20598 free (desc);
20602 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
20603 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
20604 of TYPE accordingly.
20606 ??? This is a temporary measure until after we're able to generate
20607 regular DWARF for the complex Ada type system. */
20609 static void
20610 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
20611 dw_die_ref context_die)
20613 tree dtype;
20614 dw_die_ref dtype_die;
20616 if (!lang_hooks.types.descriptive_type)
20617 return;
20619 dtype = lang_hooks.types.descriptive_type (type);
20620 if (!dtype)
20621 return;
20623 dtype_die = lookup_type_die (dtype);
20624 if (!dtype_die)
20626 gen_type_die (dtype, context_die);
20627 dtype_die = lookup_type_die (dtype);
20628 gcc_assert (dtype_die);
20631 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
20634 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
20636 static const char *
20637 comp_dir_string (void)
20639 const char *wd;
20640 char *wd1;
20641 static const char *cached_wd = NULL;
20643 if (cached_wd != NULL)
20644 return cached_wd;
20646 wd = get_src_pwd ();
20647 if (wd == NULL)
20648 return NULL;
20650 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
20652 int wdlen;
20654 wdlen = strlen (wd);
20655 wd1 = ggc_vec_alloc<char> (wdlen + 2);
20656 strcpy (wd1, wd);
20657 wd1 [wdlen] = DIR_SEPARATOR;
20658 wd1 [wdlen + 1] = 0;
20659 wd = wd1;
20662 cached_wd = remap_debug_filename (wd);
20663 return cached_wd;
20666 /* Generate a DW_AT_comp_dir attribute for DIE. */
20668 static void
20669 add_comp_dir_attribute (dw_die_ref die)
20671 const char * wd = comp_dir_string ();
20672 if (wd != NULL)
20673 add_AT_string (die, DW_AT_comp_dir, wd);
20676 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
20677 pointer computation, ...), output a representation for that bound according
20678 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
20679 loc_list_from_tree for the meaning of CONTEXT. */
20681 static void
20682 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
20683 int forms, struct loc_descr_context *context)
20685 dw_die_ref context_die, decl_die = NULL;
20686 dw_loc_list_ref list;
20687 bool strip_conversions = true;
20688 bool placeholder_seen = false;
20690 while (strip_conversions)
20691 switch (TREE_CODE (value))
20693 case ERROR_MARK:
20694 case SAVE_EXPR:
20695 return;
20697 CASE_CONVERT:
20698 case VIEW_CONVERT_EXPR:
20699 value = TREE_OPERAND (value, 0);
20700 break;
20702 default:
20703 strip_conversions = false;
20704 break;
20707 /* If possible and permitted, output the attribute as a constant. */
20708 if ((forms & dw_scalar_form_constant) != 0
20709 && TREE_CODE (value) == INTEGER_CST)
20711 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
20713 /* If HOST_WIDE_INT is big enough then represent the bound as
20714 a constant value. We need to choose a form based on
20715 whether the type is signed or unsigned. We cannot just
20716 call add_AT_unsigned if the value itself is positive
20717 (add_AT_unsigned might add the unsigned value encoded as
20718 DW_FORM_data[1248]). Some DWARF consumers will lookup the
20719 bounds type and then sign extend any unsigned values found
20720 for signed types. This is needed only for
20721 DW_AT_{lower,upper}_bound, since for most other attributes,
20722 consumers will treat DW_FORM_data[1248] as unsigned values,
20723 regardless of the underlying type. */
20724 if (prec <= HOST_BITS_PER_WIDE_INT
20725 || tree_fits_uhwi_p (value))
20727 if (TYPE_UNSIGNED (TREE_TYPE (value)))
20728 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
20729 else
20730 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
20732 else
20733 /* Otherwise represent the bound as an unsigned value with
20734 the precision of its type. The precision and signedness
20735 of the type will be necessary to re-interpret it
20736 unambiguously. */
20737 add_AT_wide (die, attr, wi::to_wide (value));
20738 return;
20741 /* Otherwise, if it's possible and permitted too, output a reference to
20742 another DIE. */
20743 if ((forms & dw_scalar_form_reference) != 0)
20745 tree decl = NULL_TREE;
20747 /* Some type attributes reference an outer type. For instance, the upper
20748 bound of an array may reference an embedding record (this happens in
20749 Ada). */
20750 if (TREE_CODE (value) == COMPONENT_REF
20751 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
20752 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
20753 decl = TREE_OPERAND (value, 1);
20755 else if (VAR_P (value)
20756 || TREE_CODE (value) == PARM_DECL
20757 || TREE_CODE (value) == RESULT_DECL)
20758 decl = value;
20760 if (decl != NULL_TREE)
20762 decl_die = lookup_decl_die (decl);
20764 /* ??? Can this happen, or should the variable have been bound
20765 first? Probably it can, since I imagine that we try to create
20766 the types of parameters in the order in which they exist in
20767 the list, and won't have created a forward reference to a
20768 later parameter. */
20769 if (decl_die != NULL)
20771 if (get_AT (decl_die, DW_AT_location)
20772 || get_AT (decl_die, DW_AT_const_value))
20774 add_AT_die_ref (die, attr, decl_die);
20775 return;
20781 /* Last chance: try to create a stack operation procedure to evaluate the
20782 value. Do nothing if even that is not possible or permitted. */
20783 if ((forms & dw_scalar_form_exprloc) == 0)
20784 return;
20786 list = loc_list_from_tree (value, 2, context);
20787 if (context && context->placeholder_arg)
20789 placeholder_seen = context->placeholder_seen;
20790 context->placeholder_seen = false;
20792 if (list == NULL || single_element_loc_list_p (list))
20794 /* If this attribute is not a reference nor constant, it is
20795 a DWARF expression rather than location description. For that
20796 loc_list_from_tree (value, 0, &context) is needed. */
20797 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20798 if (list2 && single_element_loc_list_p (list2))
20800 if (placeholder_seen)
20802 struct dwarf_procedure_info dpi;
20803 dpi.fndecl = NULL_TREE;
20804 dpi.args_count = 1;
20805 if (!resolve_args_picking (list2->expr, 1, &dpi))
20806 return;
20808 add_AT_loc (die, attr, list2->expr);
20809 return;
20813 /* If that failed to give a single element location list, fall back to
20814 outputting this as a reference... still if permitted. */
20815 if (list == NULL
20816 || (forms & dw_scalar_form_reference) == 0
20817 || placeholder_seen)
20818 return;
20820 if (!decl_die)
20822 if (current_function_decl == 0)
20823 context_die = comp_unit_die ();
20824 else
20825 context_die = lookup_decl_die (current_function_decl);
20827 decl_die = new_die (DW_TAG_variable, context_die, value);
20828 add_AT_flag (decl_die, DW_AT_artificial, 1);
20829 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20830 context_die);
20833 add_AT_location_description (decl_die, DW_AT_location, list);
20834 add_AT_die_ref (die, attr, decl_die);
20837 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20838 default. */
20840 static int
20841 lower_bound_default (void)
20843 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20845 case DW_LANG_C:
20846 case DW_LANG_C89:
20847 case DW_LANG_C99:
20848 case DW_LANG_C11:
20849 case DW_LANG_C_plus_plus:
20850 case DW_LANG_C_plus_plus_11:
20851 case DW_LANG_C_plus_plus_14:
20852 case DW_LANG_ObjC:
20853 case DW_LANG_ObjC_plus_plus:
20854 return 0;
20855 case DW_LANG_Fortran77:
20856 case DW_LANG_Fortran90:
20857 case DW_LANG_Fortran95:
20858 case DW_LANG_Fortran03:
20859 case DW_LANG_Fortran08:
20860 return 1;
20861 case DW_LANG_UPC:
20862 case DW_LANG_D:
20863 case DW_LANG_Python:
20864 return dwarf_version >= 4 ? 0 : -1;
20865 case DW_LANG_Ada95:
20866 case DW_LANG_Ada83:
20867 case DW_LANG_Cobol74:
20868 case DW_LANG_Cobol85:
20869 case DW_LANG_Modula2:
20870 case DW_LANG_PLI:
20871 return dwarf_version >= 4 ? 1 : -1;
20872 default:
20873 return -1;
20877 /* Given a tree node describing an array bound (either lower or upper) output
20878 a representation for that bound. */
20880 static void
20881 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20882 tree bound, struct loc_descr_context *context)
20884 int dflt;
20886 while (1)
20887 switch (TREE_CODE (bound))
20889 /* Strip all conversions. */
20890 CASE_CONVERT:
20891 case VIEW_CONVERT_EXPR:
20892 bound = TREE_OPERAND (bound, 0);
20893 break;
20895 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20896 are even omitted when they are the default. */
20897 case INTEGER_CST:
20898 /* If the value for this bound is the default one, we can even omit the
20899 attribute. */
20900 if (bound_attr == DW_AT_lower_bound
20901 && tree_fits_shwi_p (bound)
20902 && (dflt = lower_bound_default ()) != -1
20903 && tree_to_shwi (bound) == dflt)
20904 return;
20906 /* FALLTHRU */
20908 default:
20909 /* Because of the complex interaction there can be with other GNAT
20910 encodings, GDB isn't ready yet to handle proper DWARF description
20911 for self-referencial subrange bounds: let GNAT encodings do the
20912 magic in such a case. */
20913 if (is_ada ()
20914 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20915 && contains_placeholder_p (bound))
20916 return;
20918 add_scalar_info (subrange_die, bound_attr, bound,
20919 dw_scalar_form_constant
20920 | dw_scalar_form_exprloc
20921 | dw_scalar_form_reference,
20922 context);
20923 return;
20927 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20928 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20929 Note that the block of subscript information for an array type also
20930 includes information about the element type of the given array type.
20932 This function reuses previously set type and bound information if
20933 available. */
20935 static void
20936 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20938 unsigned dimension_number;
20939 tree lower, upper;
20940 dw_die_ref child = type_die->die_child;
20942 for (dimension_number = 0;
20943 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20944 type = TREE_TYPE (type), dimension_number++)
20946 tree domain = TYPE_DOMAIN (type);
20948 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20949 break;
20951 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20952 and (in GNU C only) variable bounds. Handle all three forms
20953 here. */
20955 /* Find and reuse a previously generated DW_TAG_subrange_type if
20956 available.
20958 For multi-dimensional arrays, as we iterate through the
20959 various dimensions in the enclosing for loop above, we also
20960 iterate through the DIE children and pick at each
20961 DW_TAG_subrange_type previously generated (if available).
20962 Each child DW_TAG_subrange_type DIE describes the range of
20963 the current dimension. At this point we should have as many
20964 DW_TAG_subrange_type's as we have dimensions in the
20965 array. */
20966 dw_die_ref subrange_die = NULL;
20967 if (child)
20968 while (1)
20970 child = child->die_sib;
20971 if (child->die_tag == DW_TAG_subrange_type)
20972 subrange_die = child;
20973 if (child == type_die->die_child)
20975 /* If we wrapped around, stop looking next time. */
20976 child = NULL;
20977 break;
20979 if (child->die_tag == DW_TAG_subrange_type)
20980 break;
20982 if (!subrange_die)
20983 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20985 if (domain)
20987 /* We have an array type with specified bounds. */
20988 lower = TYPE_MIN_VALUE (domain);
20989 upper = TYPE_MAX_VALUE (domain);
20991 /* Define the index type. */
20992 if (TREE_TYPE (domain)
20993 && !get_AT (subrange_die, DW_AT_type))
20995 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20996 TREE_TYPE field. We can't emit debug info for this
20997 because it is an unnamed integral type. */
20998 if (TREE_CODE (domain) == INTEGER_TYPE
20999 && TYPE_NAME (domain) == NULL_TREE
21000 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
21001 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
21003 else
21004 add_type_attribute (subrange_die, TREE_TYPE (domain),
21005 TYPE_UNQUALIFIED, false, type_die);
21008 /* ??? If upper is NULL, the array has unspecified length,
21009 but it does have a lower bound. This happens with Fortran
21010 dimension arr(N:*)
21011 Since the debugger is definitely going to need to know N
21012 to produce useful results, go ahead and output the lower
21013 bound solo, and hope the debugger can cope. */
21015 if (!get_AT (subrange_die, DW_AT_lower_bound))
21016 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21017 if (!get_AT (subrange_die, DW_AT_upper_bound)
21018 && !get_AT (subrange_die, DW_AT_count))
21020 if (upper)
21021 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21022 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21023 /* Zero-length array. */
21024 add_bound_info (subrange_die, DW_AT_count,
21025 build_int_cst (TREE_TYPE (lower), 0), NULL);
21029 /* Otherwise we have an array type with an unspecified length. The
21030 DWARF-2 spec does not say how to handle this; let's just leave out the
21031 bounds. */
21035 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21037 static void
21038 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21040 dw_die_ref decl_die;
21041 HOST_WIDE_INT size;
21042 dw_loc_descr_ref size_expr = NULL;
21044 switch (TREE_CODE (tree_node))
21046 case ERROR_MARK:
21047 size = 0;
21048 break;
21049 case ENUMERAL_TYPE:
21050 case RECORD_TYPE:
21051 case UNION_TYPE:
21052 case QUAL_UNION_TYPE:
21053 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21054 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21056 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21057 return;
21059 size_expr = type_byte_size (tree_node, &size);
21060 break;
21061 case FIELD_DECL:
21062 /* For a data member of a struct or union, the DW_AT_byte_size is
21063 generally given as the number of bytes normally allocated for an
21064 object of the *declared* type of the member itself. This is true
21065 even for bit-fields. */
21066 size = int_size_in_bytes (field_type (tree_node));
21067 break;
21068 default:
21069 gcc_unreachable ();
21072 /* Support for dynamically-sized objects was introduced by DWARFv3.
21073 At the moment, GDB does not handle variable byte sizes very well,
21074 though. */
21075 if ((dwarf_version >= 3 || !dwarf_strict)
21076 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
21077 && size_expr != NULL)
21078 add_AT_loc (die, DW_AT_byte_size, size_expr);
21080 /* Note that `size' might be -1 when we get to this point. If it is, that
21081 indicates that the byte size of the entity in question is variable and
21082 that we could not generate a DWARF expression that computes it. */
21083 if (size >= 0)
21084 add_AT_unsigned (die, DW_AT_byte_size, size);
21087 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21088 alignment. */
21090 static void
21091 add_alignment_attribute (dw_die_ref die, tree tree_node)
21093 if (dwarf_version < 5 && dwarf_strict)
21094 return;
21096 unsigned align;
21098 if (DECL_P (tree_node))
21100 if (!DECL_USER_ALIGN (tree_node))
21101 return;
21103 align = DECL_ALIGN_UNIT (tree_node);
21105 else if (TYPE_P (tree_node))
21107 if (!TYPE_USER_ALIGN (tree_node))
21108 return;
21110 align = TYPE_ALIGN_UNIT (tree_node);
21112 else
21113 gcc_unreachable ();
21115 add_AT_unsigned (die, DW_AT_alignment, align);
21118 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21119 which specifies the distance in bits from the highest order bit of the
21120 "containing object" for the bit-field to the highest order bit of the
21121 bit-field itself.
21123 For any given bit-field, the "containing object" is a hypothetical object
21124 (of some integral or enum type) within which the given bit-field lives. The
21125 type of this hypothetical "containing object" is always the same as the
21126 declared type of the individual bit-field itself. The determination of the
21127 exact location of the "containing object" for a bit-field is rather
21128 complicated. It's handled by the `field_byte_offset' function (above).
21130 CTX is required: see the comment for VLR_CONTEXT.
21132 Note that it is the size (in bytes) of the hypothetical "containing object"
21133 which will be given in the DW_AT_byte_size attribute for this bit-field.
21134 (See `byte_size_attribute' above). */
21136 static inline void
21137 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
21139 HOST_WIDE_INT object_offset_in_bytes;
21140 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21141 HOST_WIDE_INT bitpos_int;
21142 HOST_WIDE_INT highest_order_object_bit_offset;
21143 HOST_WIDE_INT highest_order_field_bit_offset;
21144 HOST_WIDE_INT bit_offset;
21146 field_byte_offset (decl, ctx, &object_offset_in_bytes);
21148 /* Must be a field and a bit field. */
21149 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21151 /* We can't yet handle bit-fields whose offsets are variable, so if we
21152 encounter such things, just return without generating any attribute
21153 whatsoever. Likewise for variable or too large size. */
21154 if (! tree_fits_shwi_p (bit_position (decl))
21155 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21156 return;
21158 bitpos_int = int_bit_position (decl);
21160 /* Note that the bit offset is always the distance (in bits) from the
21161 highest-order bit of the "containing object" to the highest-order bit of
21162 the bit-field itself. Since the "high-order end" of any object or field
21163 is different on big-endian and little-endian machines, the computation
21164 below must take account of these differences. */
21165 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21166 highest_order_field_bit_offset = bitpos_int;
21168 if (! BYTES_BIG_ENDIAN)
21170 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21171 highest_order_object_bit_offset +=
21172 simple_type_size_in_bits (original_type);
21175 bit_offset
21176 = (! BYTES_BIG_ENDIAN
21177 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21178 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21180 if (bit_offset < 0)
21181 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21182 else
21183 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21186 /* For a FIELD_DECL node which represents a bit field, output an attribute
21187 which specifies the length in bits of the given field. */
21189 static inline void
21190 add_bit_size_attribute (dw_die_ref die, tree decl)
21192 /* Must be a field and a bit field. */
21193 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21194 && DECL_BIT_FIELD_TYPE (decl));
21196 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21197 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21200 /* If the compiled language is ANSI C, then add a 'prototyped'
21201 attribute, if arg types are given for the parameters of a function. */
21203 static inline void
21204 add_prototyped_attribute (dw_die_ref die, tree func_type)
21206 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21208 case DW_LANG_C:
21209 case DW_LANG_C89:
21210 case DW_LANG_C99:
21211 case DW_LANG_C11:
21212 case DW_LANG_ObjC:
21213 if (prototype_p (func_type))
21214 add_AT_flag (die, DW_AT_prototyped, 1);
21215 break;
21216 default:
21217 break;
21221 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21222 by looking in the type declaration, the object declaration equate table or
21223 the block mapping. */
21225 static inline dw_die_ref
21226 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21228 dw_die_ref origin_die = NULL;
21230 if (DECL_P (origin))
21232 dw_die_ref c;
21233 origin_die = lookup_decl_die (origin);
21234 /* "Unwrap" the decls DIE which we put in the imported unit context.
21235 We are looking for the abstract copy here. */
21236 if (in_lto_p
21237 && origin_die
21238 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
21239 /* ??? Identify this better. */
21240 && c->with_offset)
21241 origin_die = c;
21243 else if (TYPE_P (origin))
21244 origin_die = lookup_type_die (origin);
21245 else if (TREE_CODE (origin) == BLOCK)
21246 origin_die = BLOCK_DIE (origin);
21248 /* XXX: Functions that are never lowered don't always have correct block
21249 trees (in the case of java, they simply have no block tree, in some other
21250 languages). For these functions, there is nothing we can really do to
21251 output correct debug info for inlined functions in all cases. Rather
21252 than die, we'll just produce deficient debug info now, in that we will
21253 have variables without a proper abstract origin. In the future, when all
21254 functions are lowered, we should re-add a gcc_assert (origin_die)
21255 here. */
21257 if (origin_die)
21258 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21259 return origin_die;
21262 /* We do not currently support the pure_virtual attribute. */
21264 static inline void
21265 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21267 if (DECL_VINDEX (func_decl))
21269 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21271 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21272 add_AT_loc (die, DW_AT_vtable_elem_location,
21273 new_loc_descr (DW_OP_constu,
21274 tree_to_shwi (DECL_VINDEX (func_decl)),
21275 0));
21277 /* GNU extension: Record what type this method came from originally. */
21278 if (debug_info_level > DINFO_LEVEL_TERSE
21279 && DECL_CONTEXT (func_decl))
21280 add_AT_die_ref (die, DW_AT_containing_type,
21281 lookup_type_die (DECL_CONTEXT (func_decl)));
21285 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21286 given decl. This used to be a vendor extension until after DWARF 4
21287 standardized it. */
21289 static void
21290 add_linkage_attr (dw_die_ref die, tree decl)
21292 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21294 /* Mimic what assemble_name_raw does with a leading '*'. */
21295 if (name[0] == '*')
21296 name = &name[1];
21298 if (dwarf_version >= 4)
21299 add_AT_string (die, DW_AT_linkage_name, name);
21300 else
21301 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21304 /* Add source coordinate attributes for the given decl. */
21306 static void
21307 add_src_coords_attributes (dw_die_ref die, tree decl)
21309 expanded_location s;
21311 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21312 return;
21313 s = expand_location (DECL_SOURCE_LOCATION (decl));
21314 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21315 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21316 if (debug_column_info && s.column)
21317 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21320 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21322 static void
21323 add_linkage_name_raw (dw_die_ref die, tree decl)
21325 /* Defer until we have an assembler name set. */
21326 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21328 limbo_die_node *asm_name;
21330 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21331 asm_name->die = die;
21332 asm_name->created_for = decl;
21333 asm_name->next = deferred_asm_name;
21334 deferred_asm_name = asm_name;
21336 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21337 add_linkage_attr (die, decl);
21340 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21342 static void
21343 add_linkage_name (dw_die_ref die, tree decl)
21345 if (debug_info_level > DINFO_LEVEL_NONE
21346 && VAR_OR_FUNCTION_DECL_P (decl)
21347 && TREE_PUBLIC (decl)
21348 && !(VAR_P (decl) && DECL_REGISTER (decl))
21349 && die->die_tag != DW_TAG_member)
21350 add_linkage_name_raw (die, decl);
21353 /* Add a DW_AT_name attribute and source coordinate attribute for the
21354 given decl, but only if it actually has a name. */
21356 static void
21357 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21358 bool no_linkage_name)
21360 tree decl_name;
21362 decl_name = DECL_NAME (decl);
21363 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21365 const char *name = dwarf2_name (decl, 0);
21366 if (name)
21367 add_name_attribute (die, name);
21368 else
21369 add_desc_attribute (die, decl);
21371 if (! DECL_ARTIFICIAL (decl))
21372 add_src_coords_attributes (die, decl);
21374 if (!no_linkage_name)
21375 add_linkage_name (die, decl);
21377 else
21378 add_desc_attribute (die, decl);
21380 #ifdef VMS_DEBUGGING_INFO
21381 /* Get the function's name, as described by its RTL. This may be different
21382 from the DECL_NAME name used in the source file. */
21383 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21385 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21386 XEXP (DECL_RTL (decl), 0), false);
21387 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21389 #endif /* VMS_DEBUGGING_INFO */
21392 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21394 static void
21395 add_discr_value (dw_die_ref die, dw_discr_value *value)
21397 dw_attr_node attr;
21399 attr.dw_attr = DW_AT_discr_value;
21400 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21401 attr.dw_attr_val.val_entry = NULL;
21402 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21403 if (value->pos)
21404 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21405 else
21406 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21407 add_dwarf_attr (die, &attr);
21410 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21412 static void
21413 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21415 dw_attr_node attr;
21417 attr.dw_attr = DW_AT_discr_list;
21418 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21419 attr.dw_attr_val.val_entry = NULL;
21420 attr.dw_attr_val.v.val_discr_list = discr_list;
21421 add_dwarf_attr (die, &attr);
21424 static inline dw_discr_list_ref
21425 AT_discr_list (dw_attr_node *attr)
21427 return attr->dw_attr_val.v.val_discr_list;
21430 #ifdef VMS_DEBUGGING_INFO
21431 /* Output the debug main pointer die for VMS */
21433 void
21434 dwarf2out_vms_debug_main_pointer (void)
21436 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21437 dw_die_ref die;
21439 /* Allocate the VMS debug main subprogram die. */
21440 die = new_die_raw (DW_TAG_subprogram);
21441 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21442 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21443 current_function_funcdef_no);
21444 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21446 /* Make it the first child of comp_unit_die (). */
21447 die->die_parent = comp_unit_die ();
21448 if (comp_unit_die ()->die_child)
21450 die->die_sib = comp_unit_die ()->die_child->die_sib;
21451 comp_unit_die ()->die_child->die_sib = die;
21453 else
21455 die->die_sib = die;
21456 comp_unit_die ()->die_child = die;
21459 #endif /* VMS_DEBUGGING_INFO */
21461 /* walk_tree helper function for uses_local_type, below. */
21463 static tree
21464 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21466 if (!TYPE_P (*tp))
21467 *walk_subtrees = 0;
21468 else
21470 tree name = TYPE_NAME (*tp);
21471 if (name && DECL_P (name) && decl_function_context (name))
21472 return *tp;
21474 return NULL_TREE;
21477 /* If TYPE involves a function-local type (including a local typedef to a
21478 non-local type), returns that type; otherwise returns NULL_TREE. */
21480 static tree
21481 uses_local_type (tree type)
21483 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21484 return used;
21487 /* Return the DIE for the scope that immediately contains this type.
21488 Non-named types that do not involve a function-local type get global
21489 scope. Named types nested in namespaces or other types get their
21490 containing scope. All other types (i.e. function-local named types) get
21491 the current active scope. */
21493 static dw_die_ref
21494 scope_die_for (tree t, dw_die_ref context_die)
21496 dw_die_ref scope_die = NULL;
21497 tree containing_scope;
21499 /* Non-types always go in the current scope. */
21500 gcc_assert (TYPE_P (t));
21502 /* Use the scope of the typedef, rather than the scope of the type
21503 it refers to. */
21504 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
21505 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
21506 else
21507 containing_scope = TYPE_CONTEXT (t);
21509 /* Use the containing namespace if there is one. */
21510 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
21512 if (context_die == lookup_decl_die (containing_scope))
21513 /* OK */;
21514 else if (debug_info_level > DINFO_LEVEL_TERSE)
21515 context_die = get_context_die (containing_scope);
21516 else
21517 containing_scope = NULL_TREE;
21520 /* Ignore function type "scopes" from the C frontend. They mean that
21521 a tagged type is local to a parmlist of a function declarator, but
21522 that isn't useful to DWARF. */
21523 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
21524 containing_scope = NULL_TREE;
21526 if (SCOPE_FILE_SCOPE_P (containing_scope))
21528 /* If T uses a local type keep it local as well, to avoid references
21529 to function-local DIEs from outside the function. */
21530 if (current_function_decl && uses_local_type (t))
21531 scope_die = context_die;
21532 else
21533 scope_die = comp_unit_die ();
21535 else if (TYPE_P (containing_scope))
21537 /* For types, we can just look up the appropriate DIE. */
21538 if (debug_info_level > DINFO_LEVEL_TERSE)
21539 scope_die = get_context_die (containing_scope);
21540 else
21542 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
21543 if (scope_die == NULL)
21544 scope_die = comp_unit_die ();
21547 else
21548 scope_die = context_die;
21550 return scope_die;
21553 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
21555 static inline int
21556 local_scope_p (dw_die_ref context_die)
21558 for (; context_die; context_die = context_die->die_parent)
21559 if (context_die->die_tag == DW_TAG_inlined_subroutine
21560 || context_die->die_tag == DW_TAG_subprogram)
21561 return 1;
21563 return 0;
21566 /* Returns nonzero if CONTEXT_DIE is a class. */
21568 static inline int
21569 class_scope_p (dw_die_ref context_die)
21571 return (context_die
21572 && (context_die->die_tag == DW_TAG_structure_type
21573 || context_die->die_tag == DW_TAG_class_type
21574 || context_die->die_tag == DW_TAG_interface_type
21575 || context_die->die_tag == DW_TAG_union_type));
21578 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
21579 whether or not to treat a DIE in this context as a declaration. */
21581 static inline int
21582 class_or_namespace_scope_p (dw_die_ref context_die)
21584 return (class_scope_p (context_die)
21585 || (context_die && context_die->die_tag == DW_TAG_namespace));
21588 /* Many forms of DIEs require a "type description" attribute. This
21589 routine locates the proper "type descriptor" die for the type given
21590 by 'type' plus any additional qualifiers given by 'cv_quals', and
21591 adds a DW_AT_type attribute below the given die. */
21593 static void
21594 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
21595 bool reverse, dw_die_ref context_die)
21597 enum tree_code code = TREE_CODE (type);
21598 dw_die_ref type_die = NULL;
21600 /* ??? If this type is an unnamed subrange type of an integral, floating-point
21601 or fixed-point type, use the inner type. This is because we have no
21602 support for unnamed types in base_type_die. This can happen if this is
21603 an Ada subrange type. Correct solution is emit a subrange type die. */
21604 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
21605 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
21606 type = TREE_TYPE (type), code = TREE_CODE (type);
21608 if (code == ERROR_MARK
21609 /* Handle a special case. For functions whose return type is void, we
21610 generate *no* type attribute. (Note that no object may have type
21611 `void', so this only applies to function return types). */
21612 || code == VOID_TYPE)
21613 return;
21615 type_die = modified_type_die (type,
21616 cv_quals | TYPE_QUALS (type),
21617 reverse,
21618 context_die);
21620 if (type_die != NULL)
21621 add_AT_die_ref (object_die, DW_AT_type, type_die);
21624 /* Given an object die, add the calling convention attribute for the
21625 function call type. */
21626 static void
21627 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
21629 enum dwarf_calling_convention value = DW_CC_normal;
21631 value = ((enum dwarf_calling_convention)
21632 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
21634 if (is_fortran ()
21635 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
21637 /* DWARF 2 doesn't provide a way to identify a program's source-level
21638 entry point. DW_AT_calling_convention attributes are only meant
21639 to describe functions' calling conventions. However, lacking a
21640 better way to signal the Fortran main program, we used this for
21641 a long time, following existing custom. Now, DWARF 4 has
21642 DW_AT_main_subprogram, which we add below, but some tools still
21643 rely on the old way, which we thus keep. */
21644 value = DW_CC_program;
21646 if (dwarf_version >= 4 || !dwarf_strict)
21647 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
21650 /* Only add the attribute if the backend requests it, and
21651 is not DW_CC_normal. */
21652 if (value && (value != DW_CC_normal))
21653 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
21656 /* Given a tree pointer to a struct, class, union, or enum type node, return
21657 a pointer to the (string) tag name for the given type, or zero if the type
21658 was declared without a tag. */
21660 static const char *
21661 type_tag (const_tree type)
21663 const char *name = 0;
21665 if (TYPE_NAME (type) != 0)
21667 tree t = 0;
21669 /* Find the IDENTIFIER_NODE for the type name. */
21670 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
21671 && !TYPE_NAMELESS (type))
21672 t = TYPE_NAME (type);
21674 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
21675 a TYPE_DECL node, regardless of whether or not a `typedef' was
21676 involved. */
21677 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21678 && ! DECL_IGNORED_P (TYPE_NAME (type)))
21680 /* We want to be extra verbose. Don't call dwarf_name if
21681 DECL_NAME isn't set. The default hook for decl_printable_name
21682 doesn't like that, and in this context it's correct to return
21683 0, instead of "<anonymous>" or the like. */
21684 if (DECL_NAME (TYPE_NAME (type))
21685 && !DECL_NAMELESS (TYPE_NAME (type)))
21686 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
21689 /* Now get the name as a string, or invent one. */
21690 if (!name && t != 0)
21691 name = IDENTIFIER_POINTER (t);
21694 return (name == 0 || *name == '\0') ? 0 : name;
21697 /* Return the type associated with a data member, make a special check
21698 for bit field types. */
21700 static inline tree
21701 member_declared_type (const_tree member)
21703 return (DECL_BIT_FIELD_TYPE (member)
21704 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
21707 /* Get the decl's label, as described by its RTL. This may be different
21708 from the DECL_NAME name used in the source file. */
21710 #if 0
21711 static const char *
21712 decl_start_label (tree decl)
21714 rtx x;
21715 const char *fnname;
21717 x = DECL_RTL (decl);
21718 gcc_assert (MEM_P (x));
21720 x = XEXP (x, 0);
21721 gcc_assert (GET_CODE (x) == SYMBOL_REF);
21723 fnname = XSTR (x, 0);
21724 return fnname;
21726 #endif
21728 /* For variable-length arrays that have been previously generated, but
21729 may be incomplete due to missing subscript info, fill the subscript
21730 info. Return TRUE if this is one of those cases. */
21731 static bool
21732 fill_variable_array_bounds (tree type)
21734 if (TREE_ASM_WRITTEN (type)
21735 && TREE_CODE (type) == ARRAY_TYPE
21736 && variably_modified_type_p (type, NULL))
21738 dw_die_ref array_die = lookup_type_die (type);
21739 if (!array_die)
21740 return false;
21741 add_subscript_info (array_die, type, !is_ada ());
21742 return true;
21744 return false;
21747 /* These routines generate the internal representation of the DIE's for
21748 the compilation unit. Debugging information is collected by walking
21749 the declaration trees passed in from dwarf2out_decl(). */
21751 static void
21752 gen_array_type_die (tree type, dw_die_ref context_die)
21754 dw_die_ref array_die;
21756 /* GNU compilers represent multidimensional array types as sequences of one
21757 dimensional array types whose element types are themselves array types.
21758 We sometimes squish that down to a single array_type DIE with multiple
21759 subscripts in the Dwarf debugging info. The draft Dwarf specification
21760 say that we are allowed to do this kind of compression in C, because
21761 there is no difference between an array of arrays and a multidimensional
21762 array. We don't do this for Ada to remain as close as possible to the
21763 actual representation, which is especially important against the language
21764 flexibilty wrt arrays of variable size. */
21766 bool collapse_nested_arrays = !is_ada ();
21768 if (fill_variable_array_bounds (type))
21769 return;
21771 dw_die_ref scope_die = scope_die_for (type, context_die);
21772 tree element_type;
21774 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
21775 DW_TAG_string_type doesn't have DW_AT_type attribute). */
21776 if (TYPE_STRING_FLAG (type)
21777 && TREE_CODE (type) == ARRAY_TYPE
21778 && is_fortran ()
21779 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21781 HOST_WIDE_INT size;
21783 array_die = new_die (DW_TAG_string_type, scope_die, type);
21784 add_name_attribute (array_die, type_tag (type));
21785 equate_type_number_to_die (type, array_die);
21786 size = int_size_in_bytes (type);
21787 if (size >= 0)
21788 add_AT_unsigned (array_die, DW_AT_byte_size, size);
21789 /* ??? We can't annotate types late, but for LTO we may not
21790 generate a location early either (gfortran.dg/save_6.f90). */
21791 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
21792 && TYPE_DOMAIN (type) != NULL_TREE
21793 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21795 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21796 tree rszdecl = szdecl;
21798 size = int_size_in_bytes (TREE_TYPE (szdecl));
21799 if (!DECL_P (szdecl))
21801 if (TREE_CODE (szdecl) == INDIRECT_REF
21802 && DECL_P (TREE_OPERAND (szdecl, 0)))
21804 rszdecl = TREE_OPERAND (szdecl, 0);
21805 if (int_size_in_bytes (TREE_TYPE (rszdecl))
21806 != DWARF2_ADDR_SIZE)
21807 size = 0;
21809 else
21810 size = 0;
21812 if (size > 0)
21814 dw_loc_list_ref loc
21815 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21816 NULL);
21817 if (loc)
21819 add_AT_location_description (array_die, DW_AT_string_length,
21820 loc);
21821 if (size != DWARF2_ADDR_SIZE)
21822 add_AT_unsigned (array_die, dwarf_version >= 5
21823 ? DW_AT_string_length_byte_size
21824 : DW_AT_byte_size, size);
21828 return;
21831 array_die = new_die (DW_TAG_array_type, scope_die, type);
21832 add_name_attribute (array_die, type_tag (type));
21833 equate_type_number_to_die (type, array_die);
21835 if (TREE_CODE (type) == VECTOR_TYPE)
21836 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21838 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
21839 if (is_fortran ()
21840 && TREE_CODE (type) == ARRAY_TYPE
21841 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21842 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21843 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21845 #if 0
21846 /* We default the array ordering. Debuggers will probably do the right
21847 things even if DW_AT_ordering is not present. It's not even an issue
21848 until we start to get into multidimensional arrays anyway. If a debugger
21849 is ever caught doing the Wrong Thing for multi-dimensional arrays,
21850 then we'll have to put the DW_AT_ordering attribute back in. (But if
21851 and when we find out that we need to put these in, we will only do so
21852 for multidimensional arrays. */
21853 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21854 #endif
21856 if (TREE_CODE (type) == VECTOR_TYPE)
21858 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21859 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21860 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21861 add_bound_info (subrange_die, DW_AT_upper_bound,
21862 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21864 else
21865 add_subscript_info (array_die, type, collapse_nested_arrays);
21867 /* Add representation of the type of the elements of this array type and
21868 emit the corresponding DIE if we haven't done it already. */
21869 element_type = TREE_TYPE (type);
21870 if (collapse_nested_arrays)
21871 while (TREE_CODE (element_type) == ARRAY_TYPE)
21873 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21874 break;
21875 element_type = TREE_TYPE (element_type);
21878 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21879 TREE_CODE (type) == ARRAY_TYPE
21880 && TYPE_REVERSE_STORAGE_ORDER (type),
21881 context_die);
21883 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21884 if (TYPE_ARTIFICIAL (type))
21885 add_AT_flag (array_die, DW_AT_artificial, 1);
21887 if (get_AT (array_die, DW_AT_name))
21888 add_pubtype (type, array_die);
21890 add_alignment_attribute (array_die, type);
21893 /* This routine generates DIE for array with hidden descriptor, details
21894 are filled into *info by a langhook. */
21896 static void
21897 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21898 dw_die_ref context_die)
21900 const dw_die_ref scope_die = scope_die_for (type, context_die);
21901 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21902 struct loc_descr_context context = { type, info->base_decl, NULL,
21903 false, false };
21904 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21905 int dim;
21907 add_name_attribute (array_die, type_tag (type));
21908 equate_type_number_to_die (type, array_die);
21910 if (info->ndimensions > 1)
21911 switch (info->ordering)
21913 case array_descr_ordering_row_major:
21914 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21915 break;
21916 case array_descr_ordering_column_major:
21917 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21918 break;
21919 default:
21920 break;
21923 if (dwarf_version >= 3 || !dwarf_strict)
21925 if (info->data_location)
21926 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21927 dw_scalar_form_exprloc, &context);
21928 if (info->associated)
21929 add_scalar_info (array_die, DW_AT_associated, info->associated,
21930 dw_scalar_form_constant
21931 | dw_scalar_form_exprloc
21932 | dw_scalar_form_reference, &context);
21933 if (info->allocated)
21934 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21935 dw_scalar_form_constant
21936 | dw_scalar_form_exprloc
21937 | dw_scalar_form_reference, &context);
21938 if (info->stride)
21940 const enum dwarf_attribute attr
21941 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21942 const int forms
21943 = (info->stride_in_bits)
21944 ? dw_scalar_form_constant
21945 : (dw_scalar_form_constant
21946 | dw_scalar_form_exprloc
21947 | dw_scalar_form_reference);
21949 add_scalar_info (array_die, attr, info->stride, forms, &context);
21952 if (dwarf_version >= 5)
21954 if (info->rank)
21956 add_scalar_info (array_die, DW_AT_rank, info->rank,
21957 dw_scalar_form_constant
21958 | dw_scalar_form_exprloc, &context);
21959 subrange_tag = DW_TAG_generic_subrange;
21960 context.placeholder_arg = true;
21964 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21966 for (dim = 0; dim < info->ndimensions; dim++)
21968 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21970 if (info->dimen[dim].bounds_type)
21971 add_type_attribute (subrange_die,
21972 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21973 false, context_die);
21974 if (info->dimen[dim].lower_bound)
21975 add_bound_info (subrange_die, DW_AT_lower_bound,
21976 info->dimen[dim].lower_bound, &context);
21977 if (info->dimen[dim].upper_bound)
21978 add_bound_info (subrange_die, DW_AT_upper_bound,
21979 info->dimen[dim].upper_bound, &context);
21980 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21981 add_scalar_info (subrange_die, DW_AT_byte_stride,
21982 info->dimen[dim].stride,
21983 dw_scalar_form_constant
21984 | dw_scalar_form_exprloc
21985 | dw_scalar_form_reference,
21986 &context);
21989 gen_type_die (info->element_type, context_die);
21990 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21991 TREE_CODE (type) == ARRAY_TYPE
21992 && TYPE_REVERSE_STORAGE_ORDER (type),
21993 context_die);
21995 if (get_AT (array_die, DW_AT_name))
21996 add_pubtype (type, array_die);
21998 add_alignment_attribute (array_die, type);
22001 #if 0
22002 static void
22003 gen_entry_point_die (tree decl, dw_die_ref context_die)
22005 tree origin = decl_ultimate_origin (decl);
22006 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22008 if (origin != NULL)
22009 add_abstract_origin_attribute (decl_die, origin);
22010 else
22012 add_name_and_src_coords_attributes (decl_die, decl);
22013 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22014 TYPE_UNQUALIFIED, false, context_die);
22017 if (DECL_ABSTRACT_P (decl))
22018 equate_decl_number_to_die (decl, decl_die);
22019 else
22020 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22022 #endif
22024 /* Walk through the list of incomplete types again, trying once more to
22025 emit full debugging info for them. */
22027 static void
22028 retry_incomplete_types (void)
22030 set_early_dwarf s;
22031 int i;
22033 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22034 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22035 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22036 vec_safe_truncate (incomplete_types, 0);
22039 /* Determine what tag to use for a record type. */
22041 static enum dwarf_tag
22042 record_type_tag (tree type)
22044 if (! lang_hooks.types.classify_record)
22045 return DW_TAG_structure_type;
22047 switch (lang_hooks.types.classify_record (type))
22049 case RECORD_IS_STRUCT:
22050 return DW_TAG_structure_type;
22052 case RECORD_IS_CLASS:
22053 return DW_TAG_class_type;
22055 case RECORD_IS_INTERFACE:
22056 if (dwarf_version >= 3 || !dwarf_strict)
22057 return DW_TAG_interface_type;
22058 return DW_TAG_structure_type;
22060 default:
22061 gcc_unreachable ();
22065 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22066 include all of the information about the enumeration values also. Each
22067 enumerated type name/value is listed as a child of the enumerated type
22068 DIE. */
22070 static dw_die_ref
22071 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22073 dw_die_ref type_die = lookup_type_die (type);
22074 dw_die_ref orig_type_die = type_die;
22076 if (type_die == NULL)
22078 type_die = new_die (DW_TAG_enumeration_type,
22079 scope_die_for (type, context_die), type);
22080 equate_type_number_to_die (type, type_die);
22081 add_name_attribute (type_die, type_tag (type));
22082 if ((dwarf_version >= 4 || !dwarf_strict)
22083 && ENUM_IS_SCOPED (type))
22084 add_AT_flag (type_die, DW_AT_enum_class, 1);
22085 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22086 add_AT_flag (type_die, DW_AT_declaration, 1);
22087 if (!dwarf_strict)
22088 add_AT_unsigned (type_die, DW_AT_encoding,
22089 TYPE_UNSIGNED (type)
22090 ? DW_ATE_unsigned
22091 : DW_ATE_signed);
22093 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22094 return type_die;
22095 else
22096 remove_AT (type_die, DW_AT_declaration);
22098 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22099 given enum type is incomplete, do not generate the DW_AT_byte_size
22100 attribute or the DW_AT_element_list attribute. */
22101 if (TYPE_SIZE (type))
22103 tree link;
22105 if (!ENUM_IS_OPAQUE (type))
22106 TREE_ASM_WRITTEN (type) = 1;
22107 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22108 add_byte_size_attribute (type_die, type);
22109 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22110 add_alignment_attribute (type_die, type);
22111 if ((dwarf_version >= 3 || !dwarf_strict)
22112 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22114 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22115 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22116 context_die);
22118 if (TYPE_STUB_DECL (type) != NULL_TREE)
22120 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22121 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22122 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22123 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22126 /* If the first reference to this type was as the return type of an
22127 inline function, then it may not have a parent. Fix this now. */
22128 if (type_die->die_parent == NULL)
22129 add_child_die (scope_die_for (type, context_die), type_die);
22131 for (link = TYPE_VALUES (type);
22132 link != NULL; link = TREE_CHAIN (link))
22134 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22135 tree value = TREE_VALUE (link);
22137 gcc_assert (!ENUM_IS_OPAQUE (type));
22138 add_name_attribute (enum_die,
22139 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22141 if (TREE_CODE (value) == CONST_DECL)
22142 value = DECL_INITIAL (value);
22144 if (simple_type_size_in_bits (TREE_TYPE (value))
22145 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22147 /* For constant forms created by add_AT_unsigned DWARF
22148 consumers (GDB, elfutils, etc.) always zero extend
22149 the value. Only when the actual value is negative
22150 do we need to use add_AT_int to generate a constant
22151 form that can represent negative values. */
22152 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22153 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22154 add_AT_unsigned (enum_die, DW_AT_const_value,
22155 (unsigned HOST_WIDE_INT) val);
22156 else
22157 add_AT_int (enum_die, DW_AT_const_value, val);
22159 else
22160 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22161 that here. TODO: This should be re-worked to use correct
22162 signed/unsigned double tags for all cases. */
22163 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22166 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22167 if (TYPE_ARTIFICIAL (type)
22168 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22169 add_AT_flag (type_die, DW_AT_artificial, 1);
22171 else
22172 add_AT_flag (type_die, DW_AT_declaration, 1);
22174 add_pubtype (type, type_die);
22176 return type_die;
22179 /* Generate a DIE to represent either a real live formal parameter decl or to
22180 represent just the type of some formal parameter position in some function
22181 type.
22183 Note that this routine is a bit unusual because its argument may be a
22184 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22185 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22186 node. If it's the former then this function is being called to output a
22187 DIE to represent a formal parameter object (or some inlining thereof). If
22188 it's the latter, then this function is only being called to output a
22189 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22190 argument type of some subprogram type.
22191 If EMIT_NAME_P is true, name and source coordinate attributes
22192 are emitted. */
22194 static dw_die_ref
22195 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22196 dw_die_ref context_die)
22198 tree node_or_origin = node ? node : origin;
22199 tree ultimate_origin;
22200 dw_die_ref parm_die = NULL;
22202 if (DECL_P (node_or_origin))
22204 parm_die = lookup_decl_die (node);
22206 /* If the contexts differ, we may not be talking about the same
22207 thing.
22208 ??? When in LTO the DIE parent is the "abstract" copy and the
22209 context_die is the specification "copy". But this whole block
22210 should eventually be no longer needed. */
22211 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
22213 if (!DECL_ABSTRACT_P (node))
22215 /* This can happen when creating an inlined instance, in
22216 which case we need to create a new DIE that will get
22217 annotated with DW_AT_abstract_origin. */
22218 parm_die = NULL;
22220 else
22221 gcc_unreachable ();
22224 if (parm_die && parm_die->die_parent == NULL)
22226 /* Check that parm_die already has the right attributes that
22227 we would have added below. If any attributes are
22228 missing, fall through to add them. */
22229 if (! DECL_ABSTRACT_P (node_or_origin)
22230 && !get_AT (parm_die, DW_AT_location)
22231 && !get_AT (parm_die, DW_AT_const_value))
22232 /* We are missing location info, and are about to add it. */
22234 else
22236 add_child_die (context_die, parm_die);
22237 return parm_die;
22242 /* If we have a previously generated DIE, use it, unless this is an
22243 concrete instance (origin != NULL), in which case we need a new
22244 DIE with a corresponding DW_AT_abstract_origin. */
22245 bool reusing_die;
22246 if (parm_die && origin == NULL)
22247 reusing_die = true;
22248 else
22250 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22251 reusing_die = false;
22254 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22256 case tcc_declaration:
22257 ultimate_origin = decl_ultimate_origin (node_or_origin);
22258 if (node || ultimate_origin)
22259 origin = ultimate_origin;
22261 if (reusing_die)
22262 goto add_location;
22264 if (origin != NULL)
22265 add_abstract_origin_attribute (parm_die, origin);
22266 else if (emit_name_p)
22267 add_name_and_src_coords_attributes (parm_die, node);
22268 if (origin == NULL
22269 || (! DECL_ABSTRACT_P (node_or_origin)
22270 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22271 decl_function_context
22272 (node_or_origin))))
22274 tree type = TREE_TYPE (node_or_origin);
22275 if (decl_by_reference_p (node_or_origin))
22276 add_type_attribute (parm_die, TREE_TYPE (type),
22277 TYPE_UNQUALIFIED,
22278 false, context_die);
22279 else
22280 add_type_attribute (parm_die, type,
22281 decl_quals (node_or_origin),
22282 false, context_die);
22284 if (origin == NULL && DECL_ARTIFICIAL (node))
22285 add_AT_flag (parm_die, DW_AT_artificial, 1);
22286 add_location:
22287 if (node && node != origin)
22288 equate_decl_number_to_die (node, parm_die);
22289 if (! DECL_ABSTRACT_P (node_or_origin))
22290 add_location_or_const_value_attribute (parm_die, node_or_origin,
22291 node == NULL);
22293 break;
22295 case tcc_type:
22296 /* We were called with some kind of a ..._TYPE node. */
22297 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22298 context_die);
22299 break;
22301 default:
22302 gcc_unreachable ();
22305 return parm_die;
22308 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22309 children DW_TAG_formal_parameter DIEs representing the arguments of the
22310 parameter pack.
22312 PARM_PACK must be a function parameter pack.
22313 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22314 must point to the subsequent arguments of the function PACK_ARG belongs to.
22315 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22316 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22317 following the last one for which a DIE was generated. */
22319 static dw_die_ref
22320 gen_formal_parameter_pack_die (tree parm_pack,
22321 tree pack_arg,
22322 dw_die_ref subr_die,
22323 tree *next_arg)
22325 tree arg;
22326 dw_die_ref parm_pack_die;
22328 gcc_assert (parm_pack
22329 && lang_hooks.function_parameter_pack_p (parm_pack)
22330 && subr_die);
22332 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22333 add_src_coords_attributes (parm_pack_die, parm_pack);
22335 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22337 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22338 parm_pack))
22339 break;
22340 gen_formal_parameter_die (arg, NULL,
22341 false /* Don't emit name attribute. */,
22342 parm_pack_die);
22344 if (next_arg)
22345 *next_arg = arg;
22346 return parm_pack_die;
22349 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22350 at the end of an (ANSI prototyped) formal parameters list. */
22352 static void
22353 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22355 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22358 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22359 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22360 parameters as specified in some function type specification (except for
22361 those which appear as part of a function *definition*). */
22363 static void
22364 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22366 tree link;
22367 tree formal_type = NULL;
22368 tree first_parm_type;
22369 tree arg;
22371 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22373 arg = DECL_ARGUMENTS (function_or_method_type);
22374 function_or_method_type = TREE_TYPE (function_or_method_type);
22376 else
22377 arg = NULL_TREE;
22379 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22381 /* Make our first pass over the list of formal parameter types and output a
22382 DW_TAG_formal_parameter DIE for each one. */
22383 for (link = first_parm_type; link; )
22385 dw_die_ref parm_die;
22387 formal_type = TREE_VALUE (link);
22388 if (formal_type == void_type_node)
22389 break;
22391 /* Output a (nameless) DIE to represent the formal parameter itself. */
22392 parm_die = gen_formal_parameter_die (formal_type, NULL,
22393 true /* Emit name attribute. */,
22394 context_die);
22395 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22396 && link == first_parm_type)
22398 add_AT_flag (parm_die, DW_AT_artificial, 1);
22399 if (dwarf_version >= 3 || !dwarf_strict)
22400 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22402 else if (arg && DECL_ARTIFICIAL (arg))
22403 add_AT_flag (parm_die, DW_AT_artificial, 1);
22405 link = TREE_CHAIN (link);
22406 if (arg)
22407 arg = DECL_CHAIN (arg);
22410 /* If this function type has an ellipsis, add a
22411 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22412 if (formal_type != void_type_node)
22413 gen_unspecified_parameters_die (function_or_method_type, context_die);
22415 /* Make our second (and final) pass over the list of formal parameter types
22416 and output DIEs to represent those types (as necessary). */
22417 for (link = TYPE_ARG_TYPES (function_or_method_type);
22418 link && TREE_VALUE (link);
22419 link = TREE_CHAIN (link))
22420 gen_type_die (TREE_VALUE (link), context_die);
22423 /* We want to generate the DIE for TYPE so that we can generate the
22424 die for MEMBER, which has been defined; we will need to refer back
22425 to the member declaration nested within TYPE. If we're trying to
22426 generate minimal debug info for TYPE, processing TYPE won't do the
22427 trick; we need to attach the member declaration by hand. */
22429 static void
22430 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22432 gen_type_die (type, context_die);
22434 /* If we're trying to avoid duplicate debug info, we may not have
22435 emitted the member decl for this function. Emit it now. */
22436 if (TYPE_STUB_DECL (type)
22437 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22438 && ! lookup_decl_die (member))
22440 dw_die_ref type_die;
22441 gcc_assert (!decl_ultimate_origin (member));
22443 type_die = lookup_type_die_strip_naming_typedef (type);
22444 if (TREE_CODE (member) == FUNCTION_DECL)
22445 gen_subprogram_die (member, type_die);
22446 else if (TREE_CODE (member) == FIELD_DECL)
22448 /* Ignore the nameless fields that are used to skip bits but handle
22449 C++ anonymous unions and structs. */
22450 if (DECL_NAME (member) != NULL_TREE
22451 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22452 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22454 struct vlr_context vlr_ctx = {
22455 DECL_CONTEXT (member), /* struct_type */
22456 NULL_TREE /* variant_part_offset */
22458 gen_type_die (member_declared_type (member), type_die);
22459 gen_field_die (member, &vlr_ctx, type_die);
22462 else
22463 gen_variable_die (member, NULL_TREE, type_die);
22467 /* Forward declare these functions, because they are mutually recursive
22468 with their set_block_* pairing functions. */
22469 static void set_decl_origin_self (tree);
22471 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22472 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22473 that it points to the node itself, thus indicating that the node is its
22474 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
22475 the given node is NULL, recursively descend the decl/block tree which
22476 it is the root of, and for each other ..._DECL or BLOCK node contained
22477 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
22478 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
22479 values to point to themselves. */
22481 static void
22482 set_block_origin_self (tree stmt)
22484 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
22486 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
22489 tree local_decl;
22491 for (local_decl = BLOCK_VARS (stmt);
22492 local_decl != NULL_TREE;
22493 local_decl = DECL_CHAIN (local_decl))
22494 /* Do not recurse on nested functions since the inlining status
22495 of parent and child can be different as per the DWARF spec. */
22496 if (TREE_CODE (local_decl) != FUNCTION_DECL
22497 && !DECL_EXTERNAL (local_decl))
22498 set_decl_origin_self (local_decl);
22502 tree subblock;
22504 for (subblock = BLOCK_SUBBLOCKS (stmt);
22505 subblock != NULL_TREE;
22506 subblock = BLOCK_CHAIN (subblock))
22507 set_block_origin_self (subblock); /* Recurse. */
22512 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
22513 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
22514 node to so that it points to the node itself, thus indicating that the
22515 node represents its own (abstract) origin. Additionally, if the
22516 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
22517 the decl/block tree of which the given node is the root of, and for
22518 each other ..._DECL or BLOCK node contained therein whose
22519 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
22520 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
22521 point to themselves. */
22523 static void
22524 set_decl_origin_self (tree decl)
22526 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
22528 DECL_ABSTRACT_ORIGIN (decl) = decl;
22529 if (TREE_CODE (decl) == FUNCTION_DECL)
22531 tree arg;
22533 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
22534 DECL_ABSTRACT_ORIGIN (arg) = arg;
22535 if (DECL_INITIAL (decl) != NULL_TREE
22536 && DECL_INITIAL (decl) != error_mark_node)
22537 set_block_origin_self (DECL_INITIAL (decl));
22542 /* Mark the early DIE for DECL as the abstract instance. */
22544 static void
22545 dwarf2out_abstract_function (tree decl)
22547 dw_die_ref old_die;
22549 /* Make sure we have the actual abstract inline, not a clone. */
22550 decl = DECL_ORIGIN (decl);
22552 if (DECL_IGNORED_P (decl))
22553 return;
22555 old_die = lookup_decl_die (decl);
22556 /* With early debug we always have an old DIE unless we are in LTO
22557 and the user did not compile but only link with debug. */
22558 if (in_lto_p && ! old_die)
22559 return;
22560 gcc_assert (old_die != NULL);
22561 if (get_AT (old_die, DW_AT_inline)
22562 || get_AT (old_die, DW_AT_abstract_origin))
22563 /* We've already generated the abstract instance. */
22564 return;
22566 /* Go ahead and put DW_AT_inline on the DIE. */
22567 if (DECL_DECLARED_INLINE_P (decl))
22569 if (cgraph_function_possibly_inlined_p (decl))
22570 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
22571 else
22572 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
22574 else
22576 if (cgraph_function_possibly_inlined_p (decl))
22577 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
22578 else
22579 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
22582 if (DECL_DECLARED_INLINE_P (decl)
22583 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22584 add_AT_flag (old_die, DW_AT_artificial, 1);
22586 set_decl_origin_self (decl);
22589 /* Helper function of premark_used_types() which gets called through
22590 htab_traverse.
22592 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22593 marked as unused by prune_unused_types. */
22595 bool
22596 premark_used_types_helper (tree const &type, void *)
22598 dw_die_ref die;
22600 die = lookup_type_die (type);
22601 if (die != NULL)
22602 die->die_perennial_p = 1;
22603 return true;
22606 /* Helper function of premark_types_used_by_global_vars which gets called
22607 through htab_traverse.
22609 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22610 marked as unused by prune_unused_types. The DIE of the type is marked
22611 only if the global variable using the type will actually be emitted. */
22614 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
22615 void *)
22617 struct types_used_by_vars_entry *entry;
22618 dw_die_ref die;
22620 entry = (struct types_used_by_vars_entry *) *slot;
22621 gcc_assert (entry->type != NULL
22622 && entry->var_decl != NULL);
22623 die = lookup_type_die (entry->type);
22624 if (die)
22626 /* Ask cgraph if the global variable really is to be emitted.
22627 If yes, then we'll keep the DIE of ENTRY->TYPE. */
22628 varpool_node *node = varpool_node::get (entry->var_decl);
22629 if (node && node->definition)
22631 die->die_perennial_p = 1;
22632 /* Keep the parent DIEs as well. */
22633 while ((die = die->die_parent) && die->die_perennial_p == 0)
22634 die->die_perennial_p = 1;
22637 return 1;
22640 /* Mark all members of used_types_hash as perennial. */
22642 static void
22643 premark_used_types (struct function *fun)
22645 if (fun && fun->used_types_hash)
22646 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
22649 /* Mark all members of types_used_by_vars_entry as perennial. */
22651 static void
22652 premark_types_used_by_global_vars (void)
22654 if (types_used_by_vars_hash)
22655 types_used_by_vars_hash
22656 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
22659 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
22660 for CA_LOC call arg loc node. */
22662 static dw_die_ref
22663 gen_call_site_die (tree decl, dw_die_ref subr_die,
22664 struct call_arg_loc_node *ca_loc)
22666 dw_die_ref stmt_die = NULL, die;
22667 tree block = ca_loc->block;
22669 while (block
22670 && block != DECL_INITIAL (decl)
22671 && TREE_CODE (block) == BLOCK)
22673 stmt_die = BLOCK_DIE (block);
22674 if (stmt_die)
22675 break;
22676 block = BLOCK_SUPERCONTEXT (block);
22678 if (stmt_die == NULL)
22679 stmt_die = subr_die;
22680 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
22681 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
22682 if (ca_loc->tail_call_p)
22683 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
22684 if (ca_loc->symbol_ref)
22686 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
22687 if (tdie)
22688 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
22689 else
22690 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
22691 false);
22693 return die;
22696 /* Generate a DIE to represent a declared function (either file-scope or
22697 block-local). */
22699 static void
22700 gen_subprogram_die (tree decl, dw_die_ref context_die)
22702 tree origin = decl_ultimate_origin (decl);
22703 dw_die_ref subr_die;
22704 dw_die_ref old_die = lookup_decl_die (decl);
22706 /* This function gets called multiple times for different stages of
22707 the debug process. For example, for func() in this code:
22709 namespace S
22711 void func() { ... }
22714 ...we get called 4 times. Twice in early debug and twice in
22715 late debug:
22717 Early debug
22718 -----------
22720 1. Once while generating func() within the namespace. This is
22721 the declaration. The declaration bit below is set, as the
22722 context is the namespace.
22724 A new DIE will be generated with DW_AT_declaration set.
22726 2. Once for func() itself. This is the specification. The
22727 declaration bit below is clear as the context is the CU.
22729 We will use the cached DIE from (1) to create a new DIE with
22730 DW_AT_specification pointing to the declaration in (1).
22732 Late debug via rest_of_handle_final()
22733 -------------------------------------
22735 3. Once generating func() within the namespace. This is also the
22736 declaration, as in (1), but this time we will early exit below
22737 as we have a cached DIE and a declaration needs no additional
22738 annotations (no locations), as the source declaration line
22739 info is enough.
22741 4. Once for func() itself. As in (2), this is the specification,
22742 but this time we will re-use the cached DIE, and just annotate
22743 it with the location information that should now be available.
22745 For something without namespaces, but with abstract instances, we
22746 are also called a multiple times:
22748 class Base
22750 public:
22751 Base (); // constructor declaration (1)
22754 Base::Base () { } // constructor specification (2)
22756 Early debug
22757 -----------
22759 1. Once for the Base() constructor by virtue of it being a
22760 member of the Base class. This is done via
22761 rest_of_type_compilation.
22763 This is a declaration, so a new DIE will be created with
22764 DW_AT_declaration.
22766 2. Once for the Base() constructor definition, but this time
22767 while generating the abstract instance of the base
22768 constructor (__base_ctor) which is being generated via early
22769 debug of reachable functions.
22771 Even though we have a cached version of the declaration (1),
22772 we will create a DW_AT_specification of the declaration DIE
22773 in (1).
22775 3. Once for the __base_ctor itself, but this time, we generate
22776 an DW_AT_abstract_origin version of the DW_AT_specification in
22777 (2).
22779 Late debug via rest_of_handle_final
22780 -----------------------------------
22782 4. One final time for the __base_ctor (which will have a cached
22783 DIE with DW_AT_abstract_origin created in (3). This time,
22784 we will just annotate the location information now
22785 available.
22787 int declaration = (current_function_decl != decl
22788 || class_or_namespace_scope_p (context_die));
22790 /* A declaration that has been previously dumped needs no
22791 additional information. */
22792 if (old_die && declaration)
22793 return;
22795 /* Now that the C++ front end lazily declares artificial member fns, we
22796 might need to retrofit the declaration into its class. */
22797 if (!declaration && !origin && !old_die
22798 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22799 && !class_or_namespace_scope_p (context_die)
22800 && debug_info_level > DINFO_LEVEL_TERSE)
22801 old_die = force_decl_die (decl);
22803 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
22804 if (origin != NULL)
22806 gcc_assert (!declaration || local_scope_p (context_die));
22808 /* Fixup die_parent for the abstract instance of a nested
22809 inline function. */
22810 if (old_die && old_die->die_parent == NULL)
22811 add_child_die (context_die, old_die);
22813 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22815 /* If we have a DW_AT_abstract_origin we have a working
22816 cached version. */
22817 subr_die = old_die;
22819 else
22821 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22822 add_abstract_origin_attribute (subr_die, origin);
22823 /* This is where the actual code for a cloned function is.
22824 Let's emit linkage name attribute for it. This helps
22825 debuggers to e.g, set breakpoints into
22826 constructors/destructors when the user asks "break
22827 K::K". */
22828 add_linkage_name (subr_die, decl);
22831 /* A cached copy, possibly from early dwarf generation. Reuse as
22832 much as possible. */
22833 else if (old_die)
22835 if (!get_AT_flag (old_die, DW_AT_declaration)
22836 /* We can have a normal definition following an inline one in the
22837 case of redefinition of GNU C extern inlines.
22838 It seems reasonable to use AT_specification in this case. */
22839 && !get_AT (old_die, DW_AT_inline))
22841 /* Detect and ignore this case, where we are trying to output
22842 something we have already output. */
22843 if (get_AT (old_die, DW_AT_low_pc)
22844 || get_AT (old_die, DW_AT_ranges))
22845 return;
22847 /* If we have no location information, this must be a
22848 partially generated DIE from early dwarf generation.
22849 Fall through and generate it. */
22852 /* If the definition comes from the same place as the declaration,
22853 maybe use the old DIE. We always want the DIE for this function
22854 that has the *_pc attributes to be under comp_unit_die so the
22855 debugger can find it. We also need to do this for abstract
22856 instances of inlines, since the spec requires the out-of-line copy
22857 to have the same parent. For local class methods, this doesn't
22858 apply; we just use the old DIE. */
22859 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22860 struct dwarf_file_data * file_index = lookup_filename (s.file);
22861 if (((is_unit_die (old_die->die_parent)
22862 /* This condition fixes the inconsistency/ICE with the
22863 following Fortran test (or some derivative thereof) while
22864 building libgfortran:
22866 module some_m
22867 contains
22868 logical function funky (FLAG)
22869 funky = .true.
22870 end function
22871 end module
22873 || (old_die->die_parent
22874 && old_die->die_parent->die_tag == DW_TAG_module)
22875 || local_scope_p (old_die->die_parent)
22876 || context_die == NULL)
22877 && (DECL_ARTIFICIAL (decl)
22878 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22879 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22880 == (unsigned) s.line)
22881 && (!debug_column_info
22882 || s.column == 0
22883 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22884 == (unsigned) s.column)))))
22885 /* With LTO if there's an abstract instance for
22886 the old DIE, this is a concrete instance and
22887 thus re-use the DIE. */
22888 || get_AT (old_die, DW_AT_abstract_origin))
22890 subr_die = old_die;
22892 /* Clear out the declaration attribute, but leave the
22893 parameters so they can be augmented with location
22894 information later. Unless this was a declaration, in
22895 which case, wipe out the nameless parameters and recreate
22896 them further down. */
22897 if (remove_AT (subr_die, DW_AT_declaration))
22900 remove_AT (subr_die, DW_AT_object_pointer);
22901 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22904 /* Make a specification pointing to the previously built
22905 declaration. */
22906 else
22908 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22909 add_AT_specification (subr_die, old_die);
22910 add_pubname (decl, subr_die);
22911 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22912 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22913 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22914 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22915 if (debug_column_info
22916 && s.column
22917 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22918 != (unsigned) s.column))
22919 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22921 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22922 emit the real type on the definition die. */
22923 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22925 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22926 if (die == auto_die || die == decltype_auto_die)
22927 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22928 TYPE_UNQUALIFIED, false, context_die);
22931 /* When we process the method declaration, we haven't seen
22932 the out-of-class defaulted definition yet, so we have to
22933 recheck now. */
22934 if ((dwarf_version >= 5 || ! dwarf_strict)
22935 && !get_AT (subr_die, DW_AT_defaulted))
22937 int defaulted
22938 = lang_hooks.decls.decl_dwarf_attribute (decl,
22939 DW_AT_defaulted);
22940 if (defaulted != -1)
22942 /* Other values must have been handled before. */
22943 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22944 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22949 /* Create a fresh DIE for anything else. */
22950 else
22952 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22954 if (TREE_PUBLIC (decl))
22955 add_AT_flag (subr_die, DW_AT_external, 1);
22957 add_name_and_src_coords_attributes (subr_die, decl);
22958 add_pubname (decl, subr_die);
22959 if (debug_info_level > DINFO_LEVEL_TERSE)
22961 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22962 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22963 TYPE_UNQUALIFIED, false, context_die);
22966 add_pure_or_virtual_attribute (subr_die, decl);
22967 if (DECL_ARTIFICIAL (decl))
22968 add_AT_flag (subr_die, DW_AT_artificial, 1);
22970 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22971 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22973 add_alignment_attribute (subr_die, decl);
22975 add_accessibility_attribute (subr_die, decl);
22978 /* Unless we have an existing non-declaration DIE, equate the new
22979 DIE. */
22980 if (!old_die || is_declaration_die (old_die))
22981 equate_decl_number_to_die (decl, subr_die);
22983 if (declaration)
22985 if (!old_die || !get_AT (old_die, DW_AT_inline))
22987 add_AT_flag (subr_die, DW_AT_declaration, 1);
22989 /* If this is an explicit function declaration then generate
22990 a DW_AT_explicit attribute. */
22991 if ((dwarf_version >= 3 || !dwarf_strict)
22992 && lang_hooks.decls.decl_dwarf_attribute (decl,
22993 DW_AT_explicit) == 1)
22994 add_AT_flag (subr_die, DW_AT_explicit, 1);
22996 /* If this is a C++11 deleted special function member then generate
22997 a DW_AT_deleted attribute. */
22998 if ((dwarf_version >= 5 || !dwarf_strict)
22999 && lang_hooks.decls.decl_dwarf_attribute (decl,
23000 DW_AT_deleted) == 1)
23001 add_AT_flag (subr_die, DW_AT_deleted, 1);
23003 /* If this is a C++11 defaulted special function member then
23004 generate a DW_AT_defaulted attribute. */
23005 if (dwarf_version >= 5 || !dwarf_strict)
23007 int defaulted
23008 = lang_hooks.decls.decl_dwarf_attribute (decl,
23009 DW_AT_defaulted);
23010 if (defaulted != -1)
23011 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23014 /* If this is a C++11 non-static member function with & ref-qualifier
23015 then generate a DW_AT_reference attribute. */
23016 if ((dwarf_version >= 5 || !dwarf_strict)
23017 && lang_hooks.decls.decl_dwarf_attribute (decl,
23018 DW_AT_reference) == 1)
23019 add_AT_flag (subr_die, DW_AT_reference, 1);
23021 /* If this is a C++11 non-static member function with &&
23022 ref-qualifier then generate a DW_AT_reference attribute. */
23023 if ((dwarf_version >= 5 || !dwarf_strict)
23024 && lang_hooks.decls.decl_dwarf_attribute (decl,
23025 DW_AT_rvalue_reference)
23026 == 1)
23027 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23030 /* For non DECL_EXTERNALs, if range information is available, fill
23031 the DIE with it. */
23032 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23034 HOST_WIDE_INT cfa_fb_offset;
23036 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23038 if (!crtl->has_bb_partition)
23040 dw_fde_ref fde = fun->fde;
23041 if (fde->dw_fde_begin)
23043 /* We have already generated the labels. */
23044 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23045 fde->dw_fde_end, false);
23047 else
23049 /* Create start/end labels and add the range. */
23050 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23051 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23052 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23053 current_function_funcdef_no);
23054 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23055 current_function_funcdef_no);
23056 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23057 false);
23060 #if VMS_DEBUGGING_INFO
23061 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23062 Section 2.3 Prologue and Epilogue Attributes:
23063 When a breakpoint is set on entry to a function, it is generally
23064 desirable for execution to be suspended, not on the very first
23065 instruction of the function, but rather at a point after the
23066 function's frame has been set up, after any language defined local
23067 declaration processing has been completed, and before execution of
23068 the first statement of the function begins. Debuggers generally
23069 cannot properly determine where this point is. Similarly for a
23070 breakpoint set on exit from a function. The prologue and epilogue
23071 attributes allow a compiler to communicate the location(s) to use. */
23074 if (fde->dw_fde_vms_end_prologue)
23075 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23076 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23078 if (fde->dw_fde_vms_begin_epilogue)
23079 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23080 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23082 #endif
23085 else
23087 /* Generate pubnames entries for the split function code ranges. */
23088 dw_fde_ref fde = fun->fde;
23090 if (fde->dw_fde_second_begin)
23092 if (dwarf_version >= 3 || !dwarf_strict)
23094 /* We should use ranges for non-contiguous code section
23095 addresses. Use the actual code range for the initial
23096 section, since the HOT/COLD labels might precede an
23097 alignment offset. */
23098 bool range_list_added = false;
23099 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23100 fde->dw_fde_end, &range_list_added,
23101 false);
23102 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23103 fde->dw_fde_second_end,
23104 &range_list_added, false);
23105 if (range_list_added)
23106 add_ranges (NULL);
23108 else
23110 /* There is no real support in DW2 for this .. so we make
23111 a work-around. First, emit the pub name for the segment
23112 containing the function label. Then make and emit a
23113 simplified subprogram DIE for the second segment with the
23114 name pre-fixed by __hot/cold_sect_of_. We use the same
23115 linkage name for the second die so that gdb will find both
23116 sections when given "b foo". */
23117 const char *name = NULL;
23118 tree decl_name = DECL_NAME (decl);
23119 dw_die_ref seg_die;
23121 /* Do the 'primary' section. */
23122 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23123 fde->dw_fde_end, false);
23125 /* Build a minimal DIE for the secondary section. */
23126 seg_die = new_die (DW_TAG_subprogram,
23127 subr_die->die_parent, decl);
23129 if (TREE_PUBLIC (decl))
23130 add_AT_flag (seg_die, DW_AT_external, 1);
23132 if (decl_name != NULL
23133 && IDENTIFIER_POINTER (decl_name) != NULL)
23135 name = dwarf2_name (decl, 1);
23136 if (! DECL_ARTIFICIAL (decl))
23137 add_src_coords_attributes (seg_die, decl);
23139 add_linkage_name (seg_die, decl);
23141 gcc_assert (name != NULL);
23142 add_pure_or_virtual_attribute (seg_die, decl);
23143 if (DECL_ARTIFICIAL (decl))
23144 add_AT_flag (seg_die, DW_AT_artificial, 1);
23146 name = concat ("__second_sect_of_", name, NULL);
23147 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23148 fde->dw_fde_second_end, false);
23149 add_name_attribute (seg_die, name);
23150 if (want_pubnames ())
23151 add_pubname_string (name, seg_die);
23154 else
23155 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23156 false);
23159 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23161 /* We define the "frame base" as the function's CFA. This is more
23162 convenient for several reasons: (1) It's stable across the prologue
23163 and epilogue, which makes it better than just a frame pointer,
23164 (2) With dwarf3, there exists a one-byte encoding that allows us
23165 to reference the .debug_frame data by proxy, but failing that,
23166 (3) We can at least reuse the code inspection and interpretation
23167 code that determines the CFA position at various points in the
23168 function. */
23169 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23171 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23172 add_AT_loc (subr_die, DW_AT_frame_base, op);
23174 else
23176 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23177 if (list->dw_loc_next)
23178 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23179 else
23180 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23183 /* Compute a displacement from the "steady-state frame pointer" to
23184 the CFA. The former is what all stack slots and argument slots
23185 will reference in the rtl; the latter is what we've told the
23186 debugger about. We'll need to adjust all frame_base references
23187 by this displacement. */
23188 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23190 if (fun->static_chain_decl)
23192 /* DWARF requires here a location expression that computes the
23193 address of the enclosing subprogram's frame base. The machinery
23194 in tree-nested.c is supposed to store this specific address in the
23195 last field of the FRAME record. */
23196 const tree frame_type
23197 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23198 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23200 tree fb_expr
23201 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23202 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23203 fb_expr, fb_decl, NULL_TREE);
23205 add_AT_location_description (subr_die, DW_AT_static_link,
23206 loc_list_from_tree (fb_expr, 0, NULL));
23209 resolve_variable_values ();
23212 /* Generate child dies for template paramaters. */
23213 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23214 gen_generic_params_dies (decl);
23216 /* Now output descriptions of the arguments for this function. This gets
23217 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23218 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23219 `...' at the end of the formal parameter list. In order to find out if
23220 there was a trailing ellipsis or not, we must instead look at the type
23221 associated with the FUNCTION_DECL. This will be a node of type
23222 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23223 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23224 an ellipsis at the end. */
23226 /* In the case where we are describing a mere function declaration, all we
23227 need to do here (and all we *can* do here) is to describe the *types* of
23228 its formal parameters. */
23229 if (debug_info_level <= DINFO_LEVEL_TERSE)
23231 else if (declaration)
23232 gen_formal_types_die (decl, subr_die);
23233 else
23235 /* Generate DIEs to represent all known formal parameters. */
23236 tree parm = DECL_ARGUMENTS (decl);
23237 tree generic_decl = early_dwarf
23238 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23239 tree generic_decl_parm = generic_decl
23240 ? DECL_ARGUMENTS (generic_decl)
23241 : NULL;
23243 /* Now we want to walk the list of parameters of the function and
23244 emit their relevant DIEs.
23246 We consider the case of DECL being an instance of a generic function
23247 as well as it being a normal function.
23249 If DECL is an instance of a generic function we walk the
23250 parameters of the generic function declaration _and_ the parameters of
23251 DECL itself. This is useful because we want to emit specific DIEs for
23252 function parameter packs and those are declared as part of the
23253 generic function declaration. In that particular case,
23254 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23255 That DIE has children DIEs representing the set of arguments
23256 of the pack. Note that the set of pack arguments can be empty.
23257 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23258 children DIE.
23260 Otherwise, we just consider the parameters of DECL. */
23261 while (generic_decl_parm || parm)
23263 if (generic_decl_parm
23264 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23265 gen_formal_parameter_pack_die (generic_decl_parm,
23266 parm, subr_die,
23267 &parm);
23268 else if (parm)
23270 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23272 if (early_dwarf
23273 && parm == DECL_ARGUMENTS (decl)
23274 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23275 && parm_die
23276 && (dwarf_version >= 3 || !dwarf_strict))
23277 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23279 parm = DECL_CHAIN (parm);
23281 else if (parm)
23282 parm = DECL_CHAIN (parm);
23284 if (generic_decl_parm)
23285 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23288 /* Decide whether we need an unspecified_parameters DIE at the end.
23289 There are 2 more cases to do this for: 1) the ansi ... declaration -
23290 this is detectable when the end of the arg list is not a
23291 void_type_node 2) an unprototyped function declaration (not a
23292 definition). This just means that we have no info about the
23293 parameters at all. */
23294 if (early_dwarf)
23296 if (prototype_p (TREE_TYPE (decl)))
23298 /* This is the prototyped case, check for.... */
23299 if (stdarg_p (TREE_TYPE (decl)))
23300 gen_unspecified_parameters_die (decl, subr_die);
23302 else if (DECL_INITIAL (decl) == NULL_TREE)
23303 gen_unspecified_parameters_die (decl, subr_die);
23307 if (subr_die != old_die)
23308 /* Add the calling convention attribute if requested. */
23309 add_calling_convention_attribute (subr_die, decl);
23311 /* Output Dwarf info for all of the stuff within the body of the function
23312 (if it has one - it may be just a declaration).
23314 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23315 a function. This BLOCK actually represents the outermost binding contour
23316 for the function, i.e. the contour in which the function's formal
23317 parameters and labels get declared. Curiously, it appears that the front
23318 end doesn't actually put the PARM_DECL nodes for the current function onto
23319 the BLOCK_VARS list for this outer scope, but are strung off of the
23320 DECL_ARGUMENTS list for the function instead.
23322 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23323 the LABEL_DECL nodes for the function however, and we output DWARF info
23324 for those in decls_for_scope. Just within the `outer_scope' there will be
23325 a BLOCK node representing the function's outermost pair of curly braces,
23326 and any blocks used for the base and member initializers of a C++
23327 constructor function. */
23328 tree outer_scope = DECL_INITIAL (decl);
23329 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23331 int call_site_note_count = 0;
23332 int tail_call_site_note_count = 0;
23334 /* Emit a DW_TAG_variable DIE for a named return value. */
23335 if (DECL_NAME (DECL_RESULT (decl)))
23336 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23338 /* The first time through decls_for_scope we will generate the
23339 DIEs for the locals. The second time, we fill in the
23340 location info. */
23341 decls_for_scope (outer_scope, subr_die);
23343 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23345 struct call_arg_loc_node *ca_loc;
23346 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23348 dw_die_ref die = NULL;
23349 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23350 rtx arg, next_arg;
23351 tree arg_decl = NULL_TREE;
23353 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23354 ? XEXP (ca_loc->call_arg_loc_note, 0)
23355 : NULL_RTX);
23356 arg; arg = next_arg)
23358 dw_loc_descr_ref reg, val;
23359 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23360 dw_die_ref cdie, tdie = NULL;
23362 next_arg = XEXP (arg, 1);
23363 if (REG_P (XEXP (XEXP (arg, 0), 0))
23364 && next_arg
23365 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23366 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23367 && REGNO (XEXP (XEXP (arg, 0), 0))
23368 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23369 next_arg = XEXP (next_arg, 1);
23370 if (mode == VOIDmode)
23372 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23373 if (mode == VOIDmode)
23374 mode = GET_MODE (XEXP (arg, 0));
23376 if (mode == VOIDmode || mode == BLKmode)
23377 continue;
23378 /* Get dynamic information about call target only if we
23379 have no static information: we cannot generate both
23380 DW_AT_call_origin and DW_AT_call_target
23381 attributes. */
23382 if (ca_loc->symbol_ref == NULL_RTX)
23384 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23386 tloc = XEXP (XEXP (arg, 0), 1);
23387 continue;
23389 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23390 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23392 tlocc = XEXP (XEXP (arg, 0), 1);
23393 continue;
23396 reg = NULL;
23397 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23398 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23399 VAR_INIT_STATUS_INITIALIZED);
23400 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23402 rtx mem = XEXP (XEXP (arg, 0), 0);
23403 reg = mem_loc_descriptor (XEXP (mem, 0),
23404 get_address_mode (mem),
23405 GET_MODE (mem),
23406 VAR_INIT_STATUS_INITIALIZED);
23408 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23409 == DEBUG_PARAMETER_REF)
23411 tree tdecl
23412 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23413 tdie = lookup_decl_die (tdecl);
23414 if (tdie == NULL)
23415 continue;
23416 arg_decl = tdecl;
23418 else
23419 continue;
23420 if (reg == NULL
23421 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23422 != DEBUG_PARAMETER_REF)
23423 continue;
23424 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23425 VOIDmode,
23426 VAR_INIT_STATUS_INITIALIZED);
23427 if (val == NULL)
23428 continue;
23429 if (die == NULL)
23430 die = gen_call_site_die (decl, subr_die, ca_loc);
23431 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23432 NULL_TREE);
23433 add_desc_attribute (cdie, arg_decl);
23434 if (reg != NULL)
23435 add_AT_loc (cdie, DW_AT_location, reg);
23436 else if (tdie != NULL)
23437 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23438 tdie);
23439 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23440 if (next_arg != XEXP (arg, 1))
23442 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
23443 if (mode == VOIDmode)
23444 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
23445 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
23446 0), 1),
23447 mode, VOIDmode,
23448 VAR_INIT_STATUS_INITIALIZED);
23449 if (val != NULL)
23450 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
23451 val);
23454 if (die == NULL
23455 && (ca_loc->symbol_ref || tloc))
23456 die = gen_call_site_die (decl, subr_die, ca_loc);
23457 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
23459 dw_loc_descr_ref tval = NULL;
23461 if (tloc != NULL_RTX)
23462 tval = mem_loc_descriptor (tloc,
23463 GET_MODE (tloc) == VOIDmode
23464 ? Pmode : GET_MODE (tloc),
23465 VOIDmode,
23466 VAR_INIT_STATUS_INITIALIZED);
23467 if (tval)
23468 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
23469 else if (tlocc != NULL_RTX)
23471 tval = mem_loc_descriptor (tlocc,
23472 GET_MODE (tlocc) == VOIDmode
23473 ? Pmode : GET_MODE (tlocc),
23474 VOIDmode,
23475 VAR_INIT_STATUS_INITIALIZED);
23476 if (tval)
23477 add_AT_loc (die,
23478 dwarf_AT (DW_AT_call_target_clobbered),
23479 tval);
23482 if (die != NULL)
23484 call_site_note_count++;
23485 if (ca_loc->tail_call_p)
23486 tail_call_site_note_count++;
23490 call_arg_locations = NULL;
23491 call_arg_loc_last = NULL;
23492 if (tail_call_site_count >= 0
23493 && tail_call_site_count == tail_call_site_note_count
23494 && (!dwarf_strict || dwarf_version >= 5))
23496 if (call_site_count >= 0
23497 && call_site_count == call_site_note_count)
23498 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
23499 else
23500 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
23502 call_site_count = -1;
23503 tail_call_site_count = -1;
23506 /* Mark used types after we have created DIEs for the functions scopes. */
23507 premark_used_types (DECL_STRUCT_FUNCTION (decl));
23510 /* Returns a hash value for X (which really is a die_struct). */
23512 hashval_t
23513 block_die_hasher::hash (die_struct *d)
23515 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
23518 /* Return nonzero if decl_id and die_parent of die_struct X is the same
23519 as decl_id and die_parent of die_struct Y. */
23521 bool
23522 block_die_hasher::equal (die_struct *x, die_struct *y)
23524 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
23527 /* Hold information about markers for inlined entry points. */
23528 struct GTY ((for_user)) inline_entry_data
23530 /* The block that's the inlined_function_outer_scope for an inlined
23531 function. */
23532 tree block;
23534 /* The label at the inlined entry point. */
23535 const char *label_pfx;
23536 unsigned int label_num;
23538 /* The view number to be used as the inlined entry point. */
23539 var_loc_view view;
23542 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
23544 typedef tree compare_type;
23545 static inline hashval_t hash (const inline_entry_data *);
23546 static inline bool equal (const inline_entry_data *, const_tree);
23549 /* Hash table routines for inline_entry_data. */
23551 inline hashval_t
23552 inline_entry_data_hasher::hash (const inline_entry_data *data)
23554 return htab_hash_pointer (data->block);
23557 inline bool
23558 inline_entry_data_hasher::equal (const inline_entry_data *data,
23559 const_tree block)
23561 return data->block == block;
23564 /* Inlined entry points pending DIE creation in this compilation unit. */
23566 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
23569 /* Return TRUE if DECL, which may have been previously generated as
23570 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
23571 true if decl (or its origin) is either an extern declaration or a
23572 class/namespace scoped declaration.
23574 The declare_in_namespace support causes us to get two DIEs for one
23575 variable, both of which are declarations. We want to avoid
23576 considering one to be a specification, so we must test for
23577 DECLARATION and DW_AT_declaration. */
23578 static inline bool
23579 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
23581 return (old_die && TREE_STATIC (decl) && !declaration
23582 && get_AT_flag (old_die, DW_AT_declaration) == 1);
23585 /* Return true if DECL is a local static. */
23587 static inline bool
23588 local_function_static (tree decl)
23590 gcc_assert (VAR_P (decl));
23591 return TREE_STATIC (decl)
23592 && DECL_CONTEXT (decl)
23593 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
23596 /* Generate a DIE to represent a declared data object.
23597 Either DECL or ORIGIN must be non-null. */
23599 static void
23600 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
23602 HOST_WIDE_INT off = 0;
23603 tree com_decl;
23604 tree decl_or_origin = decl ? decl : origin;
23605 tree ultimate_origin;
23606 dw_die_ref var_die;
23607 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
23608 bool declaration = (DECL_EXTERNAL (decl_or_origin)
23609 || class_or_namespace_scope_p (context_die));
23610 bool specialization_p = false;
23611 bool no_linkage_name = false;
23613 /* While C++ inline static data members have definitions inside of the
23614 class, force the first DIE to be a declaration, then let gen_member_die
23615 reparent it to the class context and call gen_variable_die again
23616 to create the outside of the class DIE for the definition. */
23617 if (!declaration
23618 && old_die == NULL
23619 && decl
23620 && DECL_CONTEXT (decl)
23621 && TYPE_P (DECL_CONTEXT (decl))
23622 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
23624 declaration = true;
23625 if (dwarf_version < 5)
23626 no_linkage_name = true;
23629 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23630 if (decl || ultimate_origin)
23631 origin = ultimate_origin;
23632 com_decl = fortran_common (decl_or_origin, &off);
23634 /* Symbol in common gets emitted as a child of the common block, in the form
23635 of a data member. */
23636 if (com_decl)
23638 dw_die_ref com_die;
23639 dw_loc_list_ref loc = NULL;
23640 die_node com_die_arg;
23642 var_die = lookup_decl_die (decl_or_origin);
23643 if (var_die)
23645 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
23647 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
23648 if (loc)
23650 if (off)
23652 /* Optimize the common case. */
23653 if (single_element_loc_list_p (loc)
23654 && loc->expr->dw_loc_opc == DW_OP_addr
23655 && loc->expr->dw_loc_next == NULL
23656 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
23657 == SYMBOL_REF)
23659 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23660 loc->expr->dw_loc_oprnd1.v.val_addr
23661 = plus_constant (GET_MODE (x), x , off);
23663 else
23664 loc_list_plus_const (loc, off);
23666 add_AT_location_description (var_die, DW_AT_location, loc);
23667 remove_AT (var_die, DW_AT_declaration);
23670 return;
23673 if (common_block_die_table == NULL)
23674 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
23676 com_die_arg.decl_id = DECL_UID (com_decl);
23677 com_die_arg.die_parent = context_die;
23678 com_die = common_block_die_table->find (&com_die_arg);
23679 if (! early_dwarf)
23680 loc = loc_list_from_tree (com_decl, 2, NULL);
23681 if (com_die == NULL)
23683 const char *cnam
23684 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
23685 die_node **slot;
23687 com_die = new_die (DW_TAG_common_block, context_die, decl);
23688 add_name_and_src_coords_attributes (com_die, com_decl);
23689 if (loc)
23691 add_AT_location_description (com_die, DW_AT_location, loc);
23692 /* Avoid sharing the same loc descriptor between
23693 DW_TAG_common_block and DW_TAG_variable. */
23694 loc = loc_list_from_tree (com_decl, 2, NULL);
23696 else if (DECL_EXTERNAL (decl_or_origin))
23697 add_AT_flag (com_die, DW_AT_declaration, 1);
23698 if (want_pubnames ())
23699 add_pubname_string (cnam, com_die); /* ??? needed? */
23700 com_die->decl_id = DECL_UID (com_decl);
23701 slot = common_block_die_table->find_slot (com_die, INSERT);
23702 *slot = com_die;
23704 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
23706 add_AT_location_description (com_die, DW_AT_location, loc);
23707 loc = loc_list_from_tree (com_decl, 2, NULL);
23708 remove_AT (com_die, DW_AT_declaration);
23710 var_die = new_die (DW_TAG_variable, com_die, decl);
23711 add_name_and_src_coords_attributes (var_die, decl_or_origin);
23712 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
23713 decl_quals (decl_or_origin), false,
23714 context_die);
23715 add_alignment_attribute (var_die, decl);
23716 add_AT_flag (var_die, DW_AT_external, 1);
23717 if (loc)
23719 if (off)
23721 /* Optimize the common case. */
23722 if (single_element_loc_list_p (loc)
23723 && loc->expr->dw_loc_opc == DW_OP_addr
23724 && loc->expr->dw_loc_next == NULL
23725 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
23727 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23728 loc->expr->dw_loc_oprnd1.v.val_addr
23729 = plus_constant (GET_MODE (x), x, off);
23731 else
23732 loc_list_plus_const (loc, off);
23734 add_AT_location_description (var_die, DW_AT_location, loc);
23736 else if (DECL_EXTERNAL (decl_or_origin))
23737 add_AT_flag (var_die, DW_AT_declaration, 1);
23738 if (decl)
23739 equate_decl_number_to_die (decl, var_die);
23740 return;
23743 if (old_die)
23745 if (declaration)
23747 /* A declaration that has been previously dumped, needs no
23748 further annotations, since it doesn't need location on
23749 the second pass. */
23750 return;
23752 else if (decl_will_get_specification_p (old_die, decl, declaration)
23753 && !get_AT (old_die, DW_AT_specification))
23755 /* Fall-thru so we can make a new variable die along with a
23756 DW_AT_specification. */
23758 else if (origin && old_die->die_parent != context_die)
23760 /* If we will be creating an inlined instance, we need a
23761 new DIE that will get annotated with
23762 DW_AT_abstract_origin. */
23763 gcc_assert (!DECL_ABSTRACT_P (decl));
23765 else
23767 /* If a DIE was dumped early, it still needs location info.
23768 Skip to where we fill the location bits. */
23769 var_die = old_die;
23771 /* ??? In LTRANS we cannot annotate early created variably
23772 modified type DIEs without copying them and adjusting all
23773 references to them. Thus we dumped them again. Also add a
23774 reference to them but beware of -g0 compile and -g link
23775 in which case the reference will be already present. */
23776 tree type = TREE_TYPE (decl_or_origin);
23777 if (in_lto_p
23778 && ! get_AT (var_die, DW_AT_type)
23779 && variably_modified_type_p
23780 (type, decl_function_context (decl_or_origin)))
23782 if (decl_by_reference_p (decl_or_origin))
23783 add_type_attribute (var_die, TREE_TYPE (type),
23784 TYPE_UNQUALIFIED, false, context_die);
23785 else
23786 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
23787 false, context_die);
23790 goto gen_variable_die_location;
23794 /* For static data members, the declaration in the class is supposed
23795 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
23796 also in DWARF2; the specification should still be DW_TAG_variable
23797 referencing the DW_TAG_member DIE. */
23798 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
23799 var_die = new_die (DW_TAG_member, context_die, decl);
23800 else
23801 var_die = new_die (DW_TAG_variable, context_die, decl);
23803 if (origin != NULL)
23804 add_abstract_origin_attribute (var_die, origin);
23806 /* Loop unrolling can create multiple blocks that refer to the same
23807 static variable, so we must test for the DW_AT_declaration flag.
23809 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
23810 copy decls and set the DECL_ABSTRACT_P flag on them instead of
23811 sharing them.
23813 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
23814 else if (decl_will_get_specification_p (old_die, decl, declaration))
23816 /* This is a definition of a C++ class level static. */
23817 add_AT_specification (var_die, old_die);
23818 specialization_p = true;
23819 if (DECL_NAME (decl))
23821 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23822 struct dwarf_file_data * file_index = lookup_filename (s.file);
23824 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23825 add_AT_file (var_die, DW_AT_decl_file, file_index);
23827 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23828 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23830 if (debug_column_info
23831 && s.column
23832 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23833 != (unsigned) s.column))
23834 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23836 if (old_die->die_tag == DW_TAG_member)
23837 add_linkage_name (var_die, decl);
23840 else
23841 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23843 if ((origin == NULL && !specialization_p)
23844 || (origin != NULL
23845 && !DECL_ABSTRACT_P (decl_or_origin)
23846 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23847 decl_function_context
23848 (decl_or_origin))))
23850 tree type = TREE_TYPE (decl_or_origin);
23852 if (decl_by_reference_p (decl_or_origin))
23853 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23854 context_die);
23855 else
23856 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23857 context_die);
23860 if (origin == NULL && !specialization_p)
23862 if (TREE_PUBLIC (decl))
23863 add_AT_flag (var_die, DW_AT_external, 1);
23865 if (DECL_ARTIFICIAL (decl))
23866 add_AT_flag (var_die, DW_AT_artificial, 1);
23868 add_alignment_attribute (var_die, decl);
23870 add_accessibility_attribute (var_die, decl);
23873 if (declaration)
23874 add_AT_flag (var_die, DW_AT_declaration, 1);
23876 if (decl && (DECL_ABSTRACT_P (decl)
23877 || !old_die || is_declaration_die (old_die)))
23878 equate_decl_number_to_die (decl, var_die);
23880 gen_variable_die_location:
23881 if (! declaration
23882 && (! DECL_ABSTRACT_P (decl_or_origin)
23883 /* Local static vars are shared between all clones/inlines,
23884 so emit DW_AT_location on the abstract DIE if DECL_RTL is
23885 already set. */
23886 || (VAR_P (decl_or_origin)
23887 && TREE_STATIC (decl_or_origin)
23888 && DECL_RTL_SET_P (decl_or_origin))))
23890 if (early_dwarf)
23891 add_pubname (decl_or_origin, var_die);
23892 else
23893 add_location_or_const_value_attribute (var_die, decl_or_origin,
23894 decl == NULL);
23896 else
23897 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
23899 if ((dwarf_version >= 4 || !dwarf_strict)
23900 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23901 DW_AT_const_expr) == 1
23902 && !get_AT (var_die, DW_AT_const_expr)
23903 && !specialization_p)
23904 add_AT_flag (var_die, DW_AT_const_expr, 1);
23906 if (!dwarf_strict)
23908 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23909 DW_AT_inline);
23910 if (inl != -1
23911 && !get_AT (var_die, DW_AT_inline)
23912 && !specialization_p)
23913 add_AT_unsigned (var_die, DW_AT_inline, inl);
23917 /* Generate a DIE to represent a named constant. */
23919 static void
23920 gen_const_die (tree decl, dw_die_ref context_die)
23922 dw_die_ref const_die;
23923 tree type = TREE_TYPE (decl);
23925 const_die = lookup_decl_die (decl);
23926 if (const_die)
23927 return;
23929 const_die = new_die (DW_TAG_constant, context_die, decl);
23930 equate_decl_number_to_die (decl, const_die);
23931 add_name_and_src_coords_attributes (const_die, decl);
23932 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23933 if (TREE_PUBLIC (decl))
23934 add_AT_flag (const_die, DW_AT_external, 1);
23935 if (DECL_ARTIFICIAL (decl))
23936 add_AT_flag (const_die, DW_AT_artificial, 1);
23937 tree_add_const_value_attribute_for_decl (const_die, decl);
23940 /* Generate a DIE to represent a label identifier. */
23942 static void
23943 gen_label_die (tree decl, dw_die_ref context_die)
23945 tree origin = decl_ultimate_origin (decl);
23946 dw_die_ref lbl_die = lookup_decl_die (decl);
23947 rtx insn;
23948 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23950 if (!lbl_die)
23952 lbl_die = new_die (DW_TAG_label, context_die, decl);
23953 equate_decl_number_to_die (decl, lbl_die);
23955 if (origin != NULL)
23956 add_abstract_origin_attribute (lbl_die, origin);
23957 else
23958 add_name_and_src_coords_attributes (lbl_die, decl);
23961 if (DECL_ABSTRACT_P (decl))
23962 equate_decl_number_to_die (decl, lbl_die);
23963 else if (! early_dwarf)
23965 insn = DECL_RTL_IF_SET (decl);
23967 /* Deleted labels are programmer specified labels which have been
23968 eliminated because of various optimizations. We still emit them
23969 here so that it is possible to put breakpoints on them. */
23970 if (insn
23971 && (LABEL_P (insn)
23972 || ((NOTE_P (insn)
23973 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23975 /* When optimization is enabled (via -O) some parts of the compiler
23976 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23977 represent source-level labels which were explicitly declared by
23978 the user. This really shouldn't be happening though, so catch
23979 it if it ever does happen. */
23980 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23982 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23983 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23985 else if (insn
23986 && NOTE_P (insn)
23987 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23988 && CODE_LABEL_NUMBER (insn) != -1)
23990 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23991 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23996 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23997 attributes to the DIE for a block STMT, to describe where the inlined
23998 function was called from. This is similar to add_src_coords_attributes. */
24000 static inline void
24001 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24003 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
24005 if (dwarf_version >= 3 || !dwarf_strict)
24007 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24008 add_AT_unsigned (die, DW_AT_call_line, s.line);
24009 if (debug_column_info && s.column)
24010 add_AT_unsigned (die, DW_AT_call_column, s.column);
24015 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24016 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24018 static inline void
24019 add_high_low_attributes (tree stmt, dw_die_ref die)
24021 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24023 if (inline_entry_data **iedp
24024 = !inline_entry_data_table ? NULL
24025 : inline_entry_data_table->find_slot_with_hash (stmt,
24026 htab_hash_pointer (stmt),
24027 NO_INSERT))
24029 inline_entry_data *ied = *iedp;
24030 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24031 gcc_assert (debug_inline_points);
24032 gcc_assert (inlined_function_outer_scope_p (stmt));
24034 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24035 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24037 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24038 && !dwarf_strict)
24040 if (!output_asm_line_debug_info ())
24041 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24042 else
24044 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24045 /* FIXME: this will resolve to a small number. Could we
24046 possibly emit smaller data? Ideally we'd emit a
24047 uleb128, but that would make the size of DIEs
24048 impossible for the compiler to compute, since it's
24049 the assembler that computes the value of the view
24050 label in this case. Ideally, we'd have a single form
24051 encompassing both the address and the view, and
24052 indirecting them through a table might make things
24053 easier, but even that would be more wasteful,
24054 space-wise, than what we have now. */
24055 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24059 inline_entry_data_table->clear_slot (iedp);
24062 if (BLOCK_FRAGMENT_CHAIN (stmt)
24063 && (dwarf_version >= 3 || !dwarf_strict))
24065 tree chain, superblock = NULL_TREE;
24066 dw_die_ref pdie;
24067 dw_attr_node *attr = NULL;
24069 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24071 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24072 BLOCK_NUMBER (stmt));
24073 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24076 /* Optimize duplicate .debug_ranges lists or even tails of
24077 lists. If this BLOCK has same ranges as its supercontext,
24078 lookup DW_AT_ranges attribute in the supercontext (and
24079 recursively so), verify that the ranges_table contains the
24080 right values and use it instead of adding a new .debug_range. */
24081 for (chain = stmt, pdie = die;
24082 BLOCK_SAME_RANGE (chain);
24083 chain = BLOCK_SUPERCONTEXT (chain))
24085 dw_attr_node *new_attr;
24087 pdie = pdie->die_parent;
24088 if (pdie == NULL)
24089 break;
24090 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24091 break;
24092 new_attr = get_AT (pdie, DW_AT_ranges);
24093 if (new_attr == NULL
24094 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24095 break;
24096 attr = new_attr;
24097 superblock = BLOCK_SUPERCONTEXT (chain);
24099 if (attr != NULL
24100 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24101 == BLOCK_NUMBER (superblock))
24102 && BLOCK_FRAGMENT_CHAIN (superblock))
24104 unsigned long off = attr->dw_attr_val.v.val_offset;
24105 unsigned long supercnt = 0, thiscnt = 0;
24106 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24107 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24109 ++supercnt;
24110 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24111 == BLOCK_NUMBER (chain));
24113 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24114 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24115 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24116 ++thiscnt;
24117 gcc_assert (supercnt >= thiscnt);
24118 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24119 false);
24120 note_rnglist_head (off + supercnt - thiscnt);
24121 return;
24124 unsigned int offset = add_ranges (stmt, true);
24125 add_AT_range_list (die, DW_AT_ranges, offset, false);
24126 note_rnglist_head (offset);
24128 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24129 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24132 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24133 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24134 chain = BLOCK_FRAGMENT_CHAIN (chain);
24136 while (chain);
24137 add_ranges (NULL);
24139 else
24141 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24142 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24143 BLOCK_NUMBER (stmt));
24144 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24145 BLOCK_NUMBER (stmt));
24146 add_AT_low_high_pc (die, label, label_high, false);
24150 /* Generate a DIE for a lexical block. */
24152 static void
24153 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24155 dw_die_ref old_die = BLOCK_DIE (stmt);
24156 dw_die_ref stmt_die = NULL;
24157 if (!old_die)
24159 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24160 BLOCK_DIE (stmt) = stmt_die;
24163 if (BLOCK_ABSTRACT (stmt))
24165 if (old_die)
24167 /* This must have been generated early and it won't even
24168 need location information since it's a DW_AT_inline
24169 function. */
24170 if (flag_checking)
24171 for (dw_die_ref c = context_die; c; c = c->die_parent)
24172 if (c->die_tag == DW_TAG_inlined_subroutine
24173 || c->die_tag == DW_TAG_subprogram)
24175 gcc_assert (get_AT (c, DW_AT_inline));
24176 break;
24178 return;
24181 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
24183 /* If this is an inlined instance, create a new lexical die for
24184 anything below to attach DW_AT_abstract_origin to. */
24185 if (old_die)
24187 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24188 BLOCK_DIE (stmt) = stmt_die;
24189 old_die = NULL;
24192 tree origin = block_ultimate_origin (stmt);
24193 if (origin != NULL_TREE && origin != stmt)
24194 add_abstract_origin_attribute (stmt_die, origin);
24197 if (old_die)
24198 stmt_die = old_die;
24200 /* A non abstract block whose blocks have already been reordered
24201 should have the instruction range for this block. If so, set the
24202 high/low attributes. */
24203 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
24205 gcc_assert (stmt_die);
24206 add_high_low_attributes (stmt, stmt_die);
24209 decls_for_scope (stmt, stmt_die);
24212 /* Generate a DIE for an inlined subprogram. */
24214 static void
24215 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24217 tree decl;
24219 /* The instance of function that is effectively being inlined shall not
24220 be abstract. */
24221 gcc_assert (! BLOCK_ABSTRACT (stmt));
24223 decl = block_ultimate_origin (stmt);
24225 /* Make sure any inlined functions are known to be inlineable. */
24226 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24227 || cgraph_function_possibly_inlined_p (decl));
24229 if (! BLOCK_ABSTRACT (stmt))
24231 dw_die_ref subr_die
24232 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24234 if (call_arg_locations || debug_inline_points)
24235 BLOCK_DIE (stmt) = subr_die;
24236 add_abstract_origin_attribute (subr_die, decl);
24237 if (TREE_ASM_WRITTEN (stmt))
24238 add_high_low_attributes (stmt, subr_die);
24239 add_call_src_coords_attributes (stmt, subr_die);
24241 decls_for_scope (stmt, subr_die);
24245 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24246 the comment for VLR_CONTEXT. */
24248 static void
24249 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24251 dw_die_ref decl_die;
24253 if (TREE_TYPE (decl) == error_mark_node)
24254 return;
24256 decl_die = new_die (DW_TAG_member, context_die, decl);
24257 add_name_and_src_coords_attributes (decl_die, decl);
24258 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24259 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24260 context_die);
24262 if (DECL_BIT_FIELD_TYPE (decl))
24264 add_byte_size_attribute (decl_die, decl);
24265 add_bit_size_attribute (decl_die, decl);
24266 add_bit_offset_attribute (decl_die, decl, ctx);
24269 add_alignment_attribute (decl_die, decl);
24271 /* If we have a variant part offset, then we are supposed to process a member
24272 of a QUAL_UNION_TYPE, which is how we represent variant parts in
24273 trees. */
24274 gcc_assert (ctx->variant_part_offset == NULL_TREE
24275 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
24276 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24277 add_data_member_location_attribute (decl_die, decl, ctx);
24279 if (DECL_ARTIFICIAL (decl))
24280 add_AT_flag (decl_die, DW_AT_artificial, 1);
24282 add_accessibility_attribute (decl_die, decl);
24284 /* Equate decl number to die, so that we can look up this decl later on. */
24285 equate_decl_number_to_die (decl, decl_die);
24288 /* Generate a DIE for a pointer to a member type. TYPE can be an
24289 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24290 pointer to member function. */
24292 static void
24293 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24295 if (lookup_type_die (type))
24296 return;
24298 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24299 scope_die_for (type, context_die), type);
24301 equate_type_number_to_die (type, ptr_die);
24302 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24303 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24304 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24305 context_die);
24306 add_alignment_attribute (ptr_die, type);
24308 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24309 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24311 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24312 add_AT_loc (ptr_die, DW_AT_use_location, op);
24316 static char *producer_string;
24318 /* Return a heap allocated producer string including command line options
24319 if -grecord-gcc-switches. */
24321 static char *
24322 gen_producer_string (void)
24324 size_t j;
24325 auto_vec<const char *> switches;
24326 const char *language_string = lang_hooks.name;
24327 char *producer, *tail;
24328 const char *p;
24329 size_t len = dwarf_record_gcc_switches ? 0 : 3;
24330 size_t plen = strlen (language_string) + 1 + strlen (version_string);
24332 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
24333 switch (save_decoded_options[j].opt_index)
24335 case OPT_o:
24336 case OPT_d:
24337 case OPT_dumpbase:
24338 case OPT_dumpdir:
24339 case OPT_auxbase:
24340 case OPT_auxbase_strip:
24341 case OPT_quiet:
24342 case OPT_version:
24343 case OPT_v:
24344 case OPT_w:
24345 case OPT_L:
24346 case OPT_D:
24347 case OPT_I:
24348 case OPT_U:
24349 case OPT_SPECIAL_unknown:
24350 case OPT_SPECIAL_ignore:
24351 case OPT_SPECIAL_deprecated:
24352 case OPT_SPECIAL_program_name:
24353 case OPT_SPECIAL_input_file:
24354 case OPT_grecord_gcc_switches:
24355 case OPT__output_pch_:
24356 case OPT_fdiagnostics_show_location_:
24357 case OPT_fdiagnostics_show_option:
24358 case OPT_fdiagnostics_show_caret:
24359 case OPT_fdiagnostics_show_labels:
24360 case OPT_fdiagnostics_show_line_numbers:
24361 case OPT_fdiagnostics_color_:
24362 case OPT_fverbose_asm:
24363 case OPT____:
24364 case OPT__sysroot_:
24365 case OPT_nostdinc:
24366 case OPT_nostdinc__:
24367 case OPT_fpreprocessed:
24368 case OPT_fltrans_output_list_:
24369 case OPT_fresolution_:
24370 case OPT_fdebug_prefix_map_:
24371 case OPT_fmacro_prefix_map_:
24372 case OPT_ffile_prefix_map_:
24373 case OPT_fcompare_debug:
24374 case OPT_fchecking:
24375 case OPT_fchecking_:
24376 /* Ignore these. */
24377 continue;
24378 default:
24379 if (cl_options[save_decoded_options[j].opt_index].flags
24380 & CL_NO_DWARF_RECORD)
24381 continue;
24382 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
24383 == '-');
24384 switch (save_decoded_options[j].canonical_option[0][1])
24386 case 'M':
24387 case 'i':
24388 case 'W':
24389 continue;
24390 case 'f':
24391 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
24392 "dump", 4) == 0)
24393 continue;
24394 break;
24395 default:
24396 break;
24398 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
24399 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
24400 break;
24403 producer = XNEWVEC (char, plen + 1 + len + 1);
24404 tail = producer;
24405 sprintf (tail, "%s %s", language_string, version_string);
24406 tail += plen;
24408 FOR_EACH_VEC_ELT (switches, j, p)
24410 len = strlen (p);
24411 *tail = ' ';
24412 memcpy (tail + 1, p, len);
24413 tail += len + 1;
24416 *tail = '\0';
24417 return producer;
24420 /* Given a C and/or C++ language/version string return the "highest".
24421 C++ is assumed to be "higher" than C in this case. Used for merging
24422 LTO translation unit languages. */
24423 static const char *
24424 highest_c_language (const char *lang1, const char *lang2)
24426 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24427 return "GNU C++17";
24428 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24429 return "GNU C++14";
24430 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24431 return "GNU C++11";
24432 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24433 return "GNU C++98";
24435 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24436 return "GNU C17";
24437 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24438 return "GNU C11";
24439 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24440 return "GNU C99";
24441 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24442 return "GNU C89";
24444 gcc_unreachable ();
24448 /* Generate the DIE for the compilation unit. */
24450 static dw_die_ref
24451 gen_compile_unit_die (const char *filename)
24453 dw_die_ref die;
24454 const char *language_string = lang_hooks.name;
24455 int language;
24457 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24459 if (filename)
24461 add_name_attribute (die, filename);
24462 /* Don't add cwd for <built-in>. */
24463 if (filename[0] != '<')
24464 add_comp_dir_attribute (die);
24467 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24469 /* If our producer is LTO try to figure out a common language to use
24470 from the global list of translation units. */
24471 if (strcmp (language_string, "GNU GIMPLE") == 0)
24473 unsigned i;
24474 tree t;
24475 const char *common_lang = NULL;
24477 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24479 if (!TRANSLATION_UNIT_LANGUAGE (t))
24480 continue;
24481 if (!common_lang)
24482 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24483 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24485 else if (strncmp (common_lang, "GNU C", 5) == 0
24486 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
24487 /* Mixing C and C++ is ok, use C++ in that case. */
24488 common_lang = highest_c_language (common_lang,
24489 TRANSLATION_UNIT_LANGUAGE (t));
24490 else
24492 /* Fall back to C. */
24493 common_lang = NULL;
24494 break;
24498 if (common_lang)
24499 language_string = common_lang;
24502 language = DW_LANG_C;
24503 if (strncmp (language_string, "GNU C", 5) == 0
24504 && ISDIGIT (language_string[5]))
24506 language = DW_LANG_C89;
24507 if (dwarf_version >= 3 || !dwarf_strict)
24509 if (strcmp (language_string, "GNU C89") != 0)
24510 language = DW_LANG_C99;
24512 if (dwarf_version >= 5 /* || !dwarf_strict */)
24513 if (strcmp (language_string, "GNU C11") == 0
24514 || strcmp (language_string, "GNU C17") == 0)
24515 language = DW_LANG_C11;
24518 else if (strncmp (language_string, "GNU C++", 7) == 0)
24520 language = DW_LANG_C_plus_plus;
24521 if (dwarf_version >= 5 /* || !dwarf_strict */)
24523 if (strcmp (language_string, "GNU C++11") == 0)
24524 language = DW_LANG_C_plus_plus_11;
24525 else if (strcmp (language_string, "GNU C++14") == 0)
24526 language = DW_LANG_C_plus_plus_14;
24527 else if (strcmp (language_string, "GNU C++17") == 0)
24528 /* For now. */
24529 language = DW_LANG_C_plus_plus_14;
24532 else if (strcmp (language_string, "GNU F77") == 0)
24533 language = DW_LANG_Fortran77;
24534 else if (dwarf_version >= 3 || !dwarf_strict)
24536 if (strcmp (language_string, "GNU Ada") == 0)
24537 language = DW_LANG_Ada95;
24538 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24540 language = DW_LANG_Fortran95;
24541 if (dwarf_version >= 5 /* || !dwarf_strict */)
24543 if (strcmp (language_string, "GNU Fortran2003") == 0)
24544 language = DW_LANG_Fortran03;
24545 else if (strcmp (language_string, "GNU Fortran2008") == 0)
24546 language = DW_LANG_Fortran08;
24549 else if (strcmp (language_string, "GNU Objective-C") == 0)
24550 language = DW_LANG_ObjC;
24551 else if (strcmp (language_string, "GNU Objective-C++") == 0)
24552 language = DW_LANG_ObjC_plus_plus;
24553 else if (dwarf_version >= 5 || !dwarf_strict)
24555 if (strcmp (language_string, "GNU Go") == 0)
24556 language = DW_LANG_Go;
24559 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
24560 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24561 language = DW_LANG_Fortran90;
24562 /* Likewise for Ada. */
24563 else if (strcmp (language_string, "GNU Ada") == 0)
24564 language = DW_LANG_Ada83;
24566 add_AT_unsigned (die, DW_AT_language, language);
24568 switch (language)
24570 case DW_LANG_Fortran77:
24571 case DW_LANG_Fortran90:
24572 case DW_LANG_Fortran95:
24573 case DW_LANG_Fortran03:
24574 case DW_LANG_Fortran08:
24575 /* Fortran has case insensitive identifiers and the front-end
24576 lowercases everything. */
24577 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
24578 break;
24579 default:
24580 /* The default DW_ID_case_sensitive doesn't need to be specified. */
24581 break;
24583 return die;
24586 /* Generate the DIE for a base class. */
24588 static void
24589 gen_inheritance_die (tree binfo, tree access, tree type,
24590 dw_die_ref context_die)
24592 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
24593 struct vlr_context ctx = { type, NULL };
24595 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
24596 context_die);
24597 add_data_member_location_attribute (die, binfo, &ctx);
24599 if (BINFO_VIRTUAL_P (binfo))
24600 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
24602 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
24603 children, otherwise the default is DW_ACCESS_public. In DWARF2
24604 the default has always been DW_ACCESS_private. */
24605 if (access == access_public_node)
24607 if (dwarf_version == 2
24608 || context_die->die_tag == DW_TAG_class_type)
24609 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
24611 else if (access == access_protected_node)
24612 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
24613 else if (dwarf_version > 2
24614 && context_die->die_tag != DW_TAG_class_type)
24615 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
24618 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
24619 structure. */
24620 static bool
24621 is_variant_part (tree decl)
24623 return (TREE_CODE (decl) == FIELD_DECL
24624 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
24627 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
24628 return the FIELD_DECL. Return NULL_TREE otherwise. */
24630 static tree
24631 analyze_discr_in_predicate (tree operand, tree struct_type)
24633 bool continue_stripping = true;
24634 while (continue_stripping)
24635 switch (TREE_CODE (operand))
24637 CASE_CONVERT:
24638 operand = TREE_OPERAND (operand, 0);
24639 break;
24640 default:
24641 continue_stripping = false;
24642 break;
24645 /* Match field access to members of struct_type only. */
24646 if (TREE_CODE (operand) == COMPONENT_REF
24647 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
24648 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
24649 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
24650 return TREE_OPERAND (operand, 1);
24651 else
24652 return NULL_TREE;
24655 /* Check that SRC is a constant integer that can be represented as a native
24656 integer constant (either signed or unsigned). If so, store it into DEST and
24657 return true. Return false otherwise. */
24659 static bool
24660 get_discr_value (tree src, dw_discr_value *dest)
24662 tree discr_type = TREE_TYPE (src);
24664 if (lang_hooks.types.get_debug_type)
24666 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
24667 if (debug_type != NULL)
24668 discr_type = debug_type;
24671 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
24672 return false;
24674 /* Signedness can vary between the original type and the debug type. This
24675 can happen for character types in Ada for instance: the character type
24676 used for code generation can be signed, to be compatible with the C one,
24677 but from a debugger point of view, it must be unsigned. */
24678 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
24679 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
24681 if (is_orig_unsigned != is_debug_unsigned)
24682 src = fold_convert (discr_type, src);
24684 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
24685 return false;
24687 dest->pos = is_debug_unsigned;
24688 if (is_debug_unsigned)
24689 dest->v.uval = tree_to_uhwi (src);
24690 else
24691 dest->v.sval = tree_to_shwi (src);
24693 return true;
24696 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
24697 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
24698 store NULL_TREE in DISCR_DECL. Otherwise:
24700 - store the discriminant field in STRUCT_TYPE that controls the variant
24701 part to *DISCR_DECL
24703 - put in *DISCR_LISTS_P an array where for each variant, the item
24704 represents the corresponding matching list of discriminant values.
24706 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
24707 the above array.
24709 Note that when the array is allocated (i.e. when the analysis is
24710 successful), it is up to the caller to free the array. */
24712 static void
24713 analyze_variants_discr (tree variant_part_decl,
24714 tree struct_type,
24715 tree *discr_decl,
24716 dw_discr_list_ref **discr_lists_p,
24717 unsigned *discr_lists_length)
24719 tree variant_part_type = TREE_TYPE (variant_part_decl);
24720 tree variant;
24721 dw_discr_list_ref *discr_lists;
24722 unsigned i;
24724 /* Compute how many variants there are in this variant part. */
24725 *discr_lists_length = 0;
24726 for (variant = TYPE_FIELDS (variant_part_type);
24727 variant != NULL_TREE;
24728 variant = DECL_CHAIN (variant))
24729 ++*discr_lists_length;
24731 *discr_decl = NULL_TREE;
24732 *discr_lists_p
24733 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
24734 sizeof (**discr_lists_p));
24735 discr_lists = *discr_lists_p;
24737 /* And then analyze all variants to extract discriminant information for all
24738 of them. This analysis is conservative: as soon as we detect something we
24739 do not support, abort everything and pretend we found nothing. */
24740 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
24741 variant != NULL_TREE;
24742 variant = DECL_CHAIN (variant), ++i)
24744 tree match_expr = DECL_QUALIFIER (variant);
24746 /* Now, try to analyze the predicate and deduce a discriminant for
24747 it. */
24748 if (match_expr == boolean_true_node)
24749 /* Typically happens for the default variant: it matches all cases that
24750 previous variants rejected. Don't output any matching value for
24751 this one. */
24752 continue;
24754 /* The following loop tries to iterate over each discriminant
24755 possibility: single values or ranges. */
24756 while (match_expr != NULL_TREE)
24758 tree next_round_match_expr;
24759 tree candidate_discr = NULL_TREE;
24760 dw_discr_list_ref new_node = NULL;
24762 /* Possibilities are matched one after the other by nested
24763 TRUTH_ORIF_EXPR expressions. Process the current possibility and
24764 continue with the rest at next iteration. */
24765 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
24767 next_round_match_expr = TREE_OPERAND (match_expr, 0);
24768 match_expr = TREE_OPERAND (match_expr, 1);
24770 else
24771 next_round_match_expr = NULL_TREE;
24773 if (match_expr == boolean_false_node)
24774 /* This sub-expression matches nothing: just wait for the next
24775 one. */
24778 else if (TREE_CODE (match_expr) == EQ_EXPR)
24780 /* We are matching: <discr_field> == <integer_cst>
24781 This sub-expression matches a single value. */
24782 tree integer_cst = TREE_OPERAND (match_expr, 1);
24784 candidate_discr
24785 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
24786 struct_type);
24788 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24789 if (!get_discr_value (integer_cst,
24790 &new_node->dw_discr_lower_bound))
24791 goto abort;
24792 new_node->dw_discr_range = false;
24795 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
24797 /* We are matching:
24798 <discr_field> > <integer_cst>
24799 && <discr_field> < <integer_cst>.
24800 This sub-expression matches the range of values between the
24801 two matched integer constants. Note that comparisons can be
24802 inclusive or exclusive. */
24803 tree candidate_discr_1, candidate_discr_2;
24804 tree lower_cst, upper_cst;
24805 bool lower_cst_included, upper_cst_included;
24806 tree lower_op = TREE_OPERAND (match_expr, 0);
24807 tree upper_op = TREE_OPERAND (match_expr, 1);
24809 /* When the comparison is exclusive, the integer constant is not
24810 the discriminant range bound we are looking for: we will have
24811 to increment or decrement it. */
24812 if (TREE_CODE (lower_op) == GE_EXPR)
24813 lower_cst_included = true;
24814 else if (TREE_CODE (lower_op) == GT_EXPR)
24815 lower_cst_included = false;
24816 else
24817 goto abort;
24819 if (TREE_CODE (upper_op) == LE_EXPR)
24820 upper_cst_included = true;
24821 else if (TREE_CODE (upper_op) == LT_EXPR)
24822 upper_cst_included = false;
24823 else
24824 goto abort;
24826 /* Extract the discriminant from the first operand and check it
24827 is consistant with the same analysis in the second
24828 operand. */
24829 candidate_discr_1
24830 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
24831 struct_type);
24832 candidate_discr_2
24833 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
24834 struct_type);
24835 if (candidate_discr_1 == candidate_discr_2)
24836 candidate_discr = candidate_discr_1;
24837 else
24838 goto abort;
24840 /* Extract bounds from both. */
24841 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24842 lower_cst = TREE_OPERAND (lower_op, 1);
24843 upper_cst = TREE_OPERAND (upper_op, 1);
24845 if (!lower_cst_included)
24846 lower_cst
24847 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
24848 build_int_cst (TREE_TYPE (lower_cst), 1));
24849 if (!upper_cst_included)
24850 upper_cst
24851 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
24852 build_int_cst (TREE_TYPE (upper_cst), 1));
24854 if (!get_discr_value (lower_cst,
24855 &new_node->dw_discr_lower_bound)
24856 || !get_discr_value (upper_cst,
24857 &new_node->dw_discr_upper_bound))
24858 goto abort;
24860 new_node->dw_discr_range = true;
24863 else
24864 /* Unsupported sub-expression: we cannot determine the set of
24865 matching discriminant values. Abort everything. */
24866 goto abort;
24868 /* If the discriminant info is not consistant with what we saw so
24869 far, consider the analysis failed and abort everything. */
24870 if (candidate_discr == NULL_TREE
24871 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
24872 goto abort;
24873 else
24874 *discr_decl = candidate_discr;
24876 if (new_node != NULL)
24878 new_node->dw_discr_next = discr_lists[i];
24879 discr_lists[i] = new_node;
24881 match_expr = next_round_match_expr;
24885 /* If we reach this point, we could match everything we were interested
24886 in. */
24887 return;
24889 abort:
24890 /* Clean all data structure and return no result. */
24891 free (*discr_lists_p);
24892 *discr_lists_p = NULL;
24893 *discr_decl = NULL_TREE;
24896 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
24897 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
24898 under CONTEXT_DIE.
24900 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
24901 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
24902 this type, which are record types, represent the available variants and each
24903 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
24904 values are inferred from these attributes.
24906 In trees, the offsets for the fields inside these sub-records are relative
24907 to the variant part itself, whereas the corresponding DIEs should have
24908 offset attributes that are relative to the embedding record base address.
24909 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
24910 must be an expression that computes the offset of the variant part to
24911 describe in DWARF. */
24913 static void
24914 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
24915 dw_die_ref context_die)
24917 const tree variant_part_type = TREE_TYPE (variant_part_decl);
24918 tree variant_part_offset = vlr_ctx->variant_part_offset;
24919 struct loc_descr_context ctx = {
24920 vlr_ctx->struct_type, /* context_type */
24921 NULL_TREE, /* base_decl */
24922 NULL, /* dpi */
24923 false, /* placeholder_arg */
24924 false /* placeholder_seen */
24927 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
24928 NULL_TREE if there is no such field. */
24929 tree discr_decl = NULL_TREE;
24930 dw_discr_list_ref *discr_lists;
24931 unsigned discr_lists_length = 0;
24932 unsigned i;
24934 dw_die_ref dwarf_proc_die = NULL;
24935 dw_die_ref variant_part_die
24936 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
24938 equate_decl_number_to_die (variant_part_decl, variant_part_die);
24940 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
24941 &discr_decl, &discr_lists, &discr_lists_length);
24943 if (discr_decl != NULL_TREE)
24945 dw_die_ref discr_die = lookup_decl_die (discr_decl);
24947 if (discr_die)
24948 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
24949 else
24950 /* We have no DIE for the discriminant, so just discard all
24951 discrimimant information in the output. */
24952 discr_decl = NULL_TREE;
24955 /* If the offset for this variant part is more complex than a constant,
24956 create a DWARF procedure for it so that we will not have to generate DWARF
24957 expressions for it for each member. */
24958 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24959 && (dwarf_version >= 3 || !dwarf_strict))
24961 const tree dwarf_proc_fndecl
24962 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24963 build_function_type (TREE_TYPE (variant_part_offset),
24964 NULL_TREE));
24965 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24966 const dw_loc_descr_ref dwarf_proc_body
24967 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24969 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24970 dwarf_proc_fndecl, context_die);
24971 if (dwarf_proc_die != NULL)
24972 variant_part_offset = dwarf_proc_call;
24975 /* Output DIEs for all variants. */
24976 i = 0;
24977 for (tree variant = TYPE_FIELDS (variant_part_type);
24978 variant != NULL_TREE;
24979 variant = DECL_CHAIN (variant), ++i)
24981 tree variant_type = TREE_TYPE (variant);
24982 dw_die_ref variant_die;
24984 /* All variants (i.e. members of a variant part) are supposed to be
24985 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24986 under these records. */
24987 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24989 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24990 equate_decl_number_to_die (variant, variant_die);
24992 /* Output discriminant values this variant matches, if any. */
24993 if (discr_decl == NULL || discr_lists[i] == NULL)
24994 /* In the case we have discriminant information at all, this is
24995 probably the default variant: as the standard says, don't
24996 output any discriminant value/list attribute. */
24998 else if (discr_lists[i]->dw_discr_next == NULL
24999 && !discr_lists[i]->dw_discr_range)
25000 /* If there is only one accepted value, don't bother outputting a
25001 list. */
25002 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25003 else
25004 add_discr_list (variant_die, discr_lists[i]);
25006 for (tree member = TYPE_FIELDS (variant_type);
25007 member != NULL_TREE;
25008 member = DECL_CHAIN (member))
25010 struct vlr_context vlr_sub_ctx = {
25011 vlr_ctx->struct_type, /* struct_type */
25012 NULL /* variant_part_offset */
25014 if (is_variant_part (member))
25016 /* All offsets for fields inside variant parts are relative to
25017 the top-level embedding RECORD_TYPE's base address. On the
25018 other hand, offsets in GCC's types are relative to the
25019 nested-most variant part. So we have to sum offsets each time
25020 we recurse. */
25022 vlr_sub_ctx.variant_part_offset
25023 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25024 variant_part_offset, byte_position (member));
25025 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25027 else
25029 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25030 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25035 free (discr_lists);
25038 /* Generate a DIE for a class member. */
25040 static void
25041 gen_member_die (tree type, dw_die_ref context_die)
25043 tree member;
25044 tree binfo = TYPE_BINFO (type);
25046 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25048 /* If this is not an incomplete type, output descriptions of each of its
25049 members. Note that as we output the DIEs necessary to represent the
25050 members of this record or union type, we will also be trying to output
25051 DIEs to represent the *types* of those members. However the `type'
25052 function (above) will specifically avoid generating type DIEs for member
25053 types *within* the list of member DIEs for this (containing) type except
25054 for those types (of members) which are explicitly marked as also being
25055 members of this (containing) type themselves. The g++ front- end can
25056 force any given type to be treated as a member of some other (containing)
25057 type by setting the TYPE_CONTEXT of the given (member) type to point to
25058 the TREE node representing the appropriate (containing) type. */
25060 /* First output info about the base classes. */
25061 if (binfo)
25063 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25064 int i;
25065 tree base;
25067 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25068 gen_inheritance_die (base,
25069 (accesses ? (*accesses)[i] : access_public_node),
25070 type,
25071 context_die);
25074 /* Now output info about the data members and type members. */
25075 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25077 struct vlr_context vlr_ctx = { type, NULL_TREE };
25078 bool static_inline_p
25079 = (TREE_STATIC (member)
25080 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25081 != -1));
25083 /* Ignore clones. */
25084 if (DECL_ABSTRACT_ORIGIN (member))
25085 continue;
25087 /* If we thought we were generating minimal debug info for TYPE
25088 and then changed our minds, some of the member declarations
25089 may have already been defined. Don't define them again, but
25090 do put them in the right order. */
25092 if (dw_die_ref child = lookup_decl_die (member))
25094 /* Handle inline static data members, which only have in-class
25095 declarations. */
25096 dw_die_ref ref = NULL;
25097 if (child->die_tag == DW_TAG_variable
25098 && child->die_parent == comp_unit_die ())
25100 ref = get_AT_ref (child, DW_AT_specification);
25101 /* For C++17 inline static data members followed by redundant
25102 out of class redeclaration, we might get here with
25103 child being the DIE created for the out of class
25104 redeclaration and with its DW_AT_specification being
25105 the DIE created for in-class definition. We want to
25106 reparent the latter, and don't want to create another
25107 DIE with DW_AT_specification in that case, because
25108 we already have one. */
25109 if (ref
25110 && static_inline_p
25111 && ref->die_tag == DW_TAG_variable
25112 && ref->die_parent == comp_unit_die ()
25113 && get_AT (ref, DW_AT_specification) == NULL)
25115 child = ref;
25116 ref = NULL;
25117 static_inline_p = false;
25121 if (child->die_tag == DW_TAG_variable
25122 && child->die_parent == comp_unit_die ()
25123 && ref == NULL)
25125 reparent_child (child, context_die);
25126 if (dwarf_version < 5)
25127 child->die_tag = DW_TAG_member;
25129 else
25130 splice_child_die (context_die, child);
25133 /* Do not generate standard DWARF for variant parts if we are generating
25134 the corresponding GNAT encodings: DIEs generated for both would
25135 conflict in our mappings. */
25136 else if (is_variant_part (member)
25137 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
25139 vlr_ctx.variant_part_offset = byte_position (member);
25140 gen_variant_part (member, &vlr_ctx, context_die);
25142 else
25144 vlr_ctx.variant_part_offset = NULL_TREE;
25145 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25148 /* For C++ inline static data members emit immediately a DW_TAG_variable
25149 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25150 DW_AT_specification. */
25151 if (static_inline_p)
25153 int old_extern = DECL_EXTERNAL (member);
25154 DECL_EXTERNAL (member) = 0;
25155 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25156 DECL_EXTERNAL (member) = old_extern;
25161 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25162 is set, we pretend that the type was never defined, so we only get the
25163 member DIEs needed by later specification DIEs. */
25165 static void
25166 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25167 enum debug_info_usage usage)
25169 if (TREE_ASM_WRITTEN (type))
25171 /* Fill in the bound of variable-length fields in late dwarf if
25172 still incomplete. */
25173 if (!early_dwarf && variably_modified_type_p (type, NULL))
25174 for (tree member = TYPE_FIELDS (type);
25175 member;
25176 member = DECL_CHAIN (member))
25177 fill_variable_array_bounds (TREE_TYPE (member));
25178 return;
25181 dw_die_ref type_die = lookup_type_die (type);
25182 dw_die_ref scope_die = 0;
25183 int nested = 0;
25184 int complete = (TYPE_SIZE (type)
25185 && (! TYPE_STUB_DECL (type)
25186 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25187 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25188 complete = complete && should_emit_struct_debug (type, usage);
25190 if (type_die && ! complete)
25191 return;
25193 if (TYPE_CONTEXT (type) != NULL_TREE
25194 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25195 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25196 nested = 1;
25198 scope_die = scope_die_for (type, context_die);
25200 /* Generate child dies for template paramaters. */
25201 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25202 schedule_generic_params_dies_gen (type);
25204 if (! type_die || (nested && is_cu_die (scope_die)))
25205 /* First occurrence of type or toplevel definition of nested class. */
25207 dw_die_ref old_die = type_die;
25209 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25210 ? record_type_tag (type) : DW_TAG_union_type,
25211 scope_die, type);
25212 equate_type_number_to_die (type, type_die);
25213 if (old_die)
25214 add_AT_specification (type_die, old_die);
25215 else
25216 add_name_attribute (type_die, type_tag (type));
25218 else
25219 remove_AT (type_die, DW_AT_declaration);
25221 /* If this type has been completed, then give it a byte_size attribute and
25222 then give a list of members. */
25223 if (complete && !ns_decl)
25225 /* Prevent infinite recursion in cases where the type of some member of
25226 this type is expressed in terms of this type itself. */
25227 TREE_ASM_WRITTEN (type) = 1;
25228 add_byte_size_attribute (type_die, type);
25229 add_alignment_attribute (type_die, type);
25230 if (TYPE_STUB_DECL (type) != NULL_TREE)
25232 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25233 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25236 /* If the first reference to this type was as the return type of an
25237 inline function, then it may not have a parent. Fix this now. */
25238 if (type_die->die_parent == NULL)
25239 add_child_die (scope_die, type_die);
25241 gen_member_die (type, type_die);
25243 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25244 if (TYPE_ARTIFICIAL (type))
25245 add_AT_flag (type_die, DW_AT_artificial, 1);
25247 /* GNU extension: Record what type our vtable lives in. */
25248 if (TYPE_VFIELD (type))
25250 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25252 gen_type_die (vtype, context_die);
25253 add_AT_die_ref (type_die, DW_AT_containing_type,
25254 lookup_type_die (vtype));
25257 else
25259 add_AT_flag (type_die, DW_AT_declaration, 1);
25261 /* We don't need to do this for function-local types. */
25262 if (TYPE_STUB_DECL (type)
25263 && ! decl_function_context (TYPE_STUB_DECL (type)))
25264 vec_safe_push (incomplete_types, type);
25267 if (get_AT (type_die, DW_AT_name))
25268 add_pubtype (type, type_die);
25271 /* Generate a DIE for a subroutine _type_. */
25273 static void
25274 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25276 tree return_type = TREE_TYPE (type);
25277 dw_die_ref subr_die
25278 = new_die (DW_TAG_subroutine_type,
25279 scope_die_for (type, context_die), type);
25281 equate_type_number_to_die (type, subr_die);
25282 add_prototyped_attribute (subr_die, type);
25283 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25284 context_die);
25285 add_alignment_attribute (subr_die, type);
25286 gen_formal_types_die (type, subr_die);
25288 if (get_AT (subr_die, DW_AT_name))
25289 add_pubtype (type, subr_die);
25290 if ((dwarf_version >= 5 || !dwarf_strict)
25291 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25292 add_AT_flag (subr_die, DW_AT_reference, 1);
25293 if ((dwarf_version >= 5 || !dwarf_strict)
25294 && lang_hooks.types.type_dwarf_attribute (type,
25295 DW_AT_rvalue_reference) != -1)
25296 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25299 /* Generate a DIE for a type definition. */
25301 static void
25302 gen_typedef_die (tree decl, dw_die_ref context_die)
25304 dw_die_ref type_die;
25305 tree type;
25307 if (TREE_ASM_WRITTEN (decl))
25309 if (DECL_ORIGINAL_TYPE (decl))
25310 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25311 return;
25314 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25315 checks in process_scope_var and modified_type_die), this should be called
25316 only for original types. */
25317 gcc_assert (decl_ultimate_origin (decl) == NULL
25318 || decl_ultimate_origin (decl) == decl);
25320 TREE_ASM_WRITTEN (decl) = 1;
25321 type_die = new_die (DW_TAG_typedef, context_die, decl);
25323 add_name_and_src_coords_attributes (type_die, decl);
25324 if (DECL_ORIGINAL_TYPE (decl))
25326 type = DECL_ORIGINAL_TYPE (decl);
25327 if (type == error_mark_node)
25328 return;
25330 gcc_assert (type != TREE_TYPE (decl));
25331 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25333 else
25335 type = TREE_TYPE (decl);
25336 if (type == error_mark_node)
25337 return;
25339 if (is_naming_typedef_decl (TYPE_NAME (type)))
25341 /* Here, we are in the case of decl being a typedef naming
25342 an anonymous type, e.g:
25343 typedef struct {...} foo;
25344 In that case TREE_TYPE (decl) is not a typedef variant
25345 type and TYPE_NAME of the anonymous type is set to the
25346 TYPE_DECL of the typedef. This construct is emitted by
25347 the C++ FE.
25349 TYPE is the anonymous struct named by the typedef
25350 DECL. As we need the DW_AT_type attribute of the
25351 DW_TAG_typedef to point to the DIE of TYPE, let's
25352 generate that DIE right away. add_type_attribute
25353 called below will then pick (via lookup_type_die) that
25354 anonymous struct DIE. */
25355 if (!TREE_ASM_WRITTEN (type))
25356 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25358 /* This is a GNU Extension. We are adding a
25359 DW_AT_linkage_name attribute to the DIE of the
25360 anonymous struct TYPE. The value of that attribute
25361 is the name of the typedef decl naming the anonymous
25362 struct. This greatly eases the work of consumers of
25363 this debug info. */
25364 add_linkage_name_raw (lookup_type_die (type), decl);
25368 add_type_attribute (type_die, type, decl_quals (decl), false,
25369 context_die);
25371 if (is_naming_typedef_decl (decl))
25372 /* We want that all subsequent calls to lookup_type_die with
25373 TYPE in argument yield the DW_TAG_typedef we have just
25374 created. */
25375 equate_type_number_to_die (type, type_die);
25377 add_alignment_attribute (type_die, TREE_TYPE (decl));
25379 add_accessibility_attribute (type_die, decl);
25381 if (DECL_ABSTRACT_P (decl))
25382 equate_decl_number_to_die (decl, type_die);
25384 if (get_AT (type_die, DW_AT_name))
25385 add_pubtype (decl, type_die);
25388 /* Generate a DIE for a struct, class, enum or union type. */
25390 static void
25391 gen_tagged_type_die (tree type,
25392 dw_die_ref context_die,
25393 enum debug_info_usage usage)
25395 if (type == NULL_TREE
25396 || !is_tagged_type (type))
25397 return;
25399 if (TREE_ASM_WRITTEN (type))
25401 /* If this is a nested type whose containing class hasn't been written
25402 out yet, writing it out will cover this one, too. This does not apply
25403 to instantiations of member class templates; they need to be added to
25404 the containing class as they are generated. FIXME: This hurts the
25405 idea of combining type decls from multiple TUs, since we can't predict
25406 what set of template instantiations we'll get. */
25407 else if (TYPE_CONTEXT (type)
25408 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25409 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25411 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25413 if (TREE_ASM_WRITTEN (type))
25414 return;
25416 /* If that failed, attach ourselves to the stub. */
25417 context_die = lookup_type_die (TYPE_CONTEXT (type));
25419 else if (TYPE_CONTEXT (type) != NULL_TREE
25420 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25422 /* If this type is local to a function that hasn't been written
25423 out yet, use a NULL context for now; it will be fixed up in
25424 decls_for_scope. */
25425 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25426 /* A declaration DIE doesn't count; nested types need to go in the
25427 specification. */
25428 if (context_die && is_declaration_die (context_die))
25429 context_die = NULL;
25431 else
25432 context_die = declare_in_namespace (type, context_die);
25434 if (TREE_CODE (type) == ENUMERAL_TYPE)
25436 /* This might have been written out by the call to
25437 declare_in_namespace. */
25438 if (!TREE_ASM_WRITTEN (type))
25439 gen_enumeration_type_die (type, context_die);
25441 else
25442 gen_struct_or_union_type_die (type, context_die, usage);
25444 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25445 it up if it is ever completed. gen_*_type_die will set it for us
25446 when appropriate. */
25449 /* Generate a type description DIE. */
25451 static void
25452 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25453 enum debug_info_usage usage)
25455 struct array_descr_info info;
25457 if (type == NULL_TREE || type == error_mark_node)
25458 return;
25460 if (flag_checking && type)
25461 verify_type (type);
25463 if (TYPE_NAME (type) != NULL_TREE
25464 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25465 && is_redundant_typedef (TYPE_NAME (type))
25466 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25467 /* The DECL of this type is a typedef we don't want to emit debug
25468 info for but we want debug info for its underlying typedef.
25469 This can happen for e.g, the injected-class-name of a C++
25470 type. */
25471 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25473 /* If TYPE is a typedef type variant, let's generate debug info
25474 for the parent typedef which TYPE is a type of. */
25475 if (typedef_variant_p (type))
25477 if (TREE_ASM_WRITTEN (type))
25478 return;
25480 tree name = TYPE_NAME (type);
25481 tree origin = decl_ultimate_origin (name);
25482 if (origin != NULL && origin != name)
25484 gen_decl_die (origin, NULL, NULL, context_die);
25485 return;
25488 /* Prevent broken recursion; we can't hand off to the same type. */
25489 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25491 /* Give typedefs the right scope. */
25492 context_die = scope_die_for (type, context_die);
25494 TREE_ASM_WRITTEN (type) = 1;
25496 gen_decl_die (name, NULL, NULL, context_die);
25497 return;
25500 /* If type is an anonymous tagged type named by a typedef, let's
25501 generate debug info for the typedef. */
25502 if (is_naming_typedef_decl (TYPE_NAME (type)))
25504 /* Give typedefs the right scope. */
25505 context_die = scope_die_for (type, context_die);
25507 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
25508 return;
25511 if (lang_hooks.types.get_debug_type)
25513 tree debug_type = lang_hooks.types.get_debug_type (type);
25515 if (debug_type != NULL_TREE && debug_type != type)
25517 gen_type_die_with_usage (debug_type, context_die, usage);
25518 return;
25522 /* We are going to output a DIE to represent the unqualified version
25523 of this type (i.e. without any const or volatile qualifiers) so
25524 get the main variant (i.e. the unqualified version) of this type
25525 now. (Vectors and arrays are special because the debugging info is in the
25526 cloned type itself. Similarly function/method types can contain extra
25527 ref-qualification). */
25528 if (TREE_CODE (type) == FUNCTION_TYPE
25529 || TREE_CODE (type) == METHOD_TYPE)
25531 /* For function/method types, can't use type_main_variant here,
25532 because that can have different ref-qualifiers for C++,
25533 but try to canonicalize. */
25534 tree main = TYPE_MAIN_VARIANT (type);
25535 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
25536 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
25537 && check_base_type (t, main)
25538 && check_lang_type (t, type))
25540 type = t;
25541 break;
25544 else if (TREE_CODE (type) != VECTOR_TYPE
25545 && TREE_CODE (type) != ARRAY_TYPE)
25546 type = type_main_variant (type);
25548 /* If this is an array type with hidden descriptor, handle it first. */
25549 if (!TREE_ASM_WRITTEN (type)
25550 && lang_hooks.types.get_array_descr_info)
25552 memset (&info, 0, sizeof (info));
25553 if (lang_hooks.types.get_array_descr_info (type, &info))
25555 /* Fortran sometimes emits array types with no dimension. */
25556 gcc_assert (info.ndimensions >= 0
25557 && (info.ndimensions
25558 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
25559 gen_descr_array_type_die (type, &info, context_die);
25560 TREE_ASM_WRITTEN (type) = 1;
25561 return;
25565 if (TREE_ASM_WRITTEN (type))
25567 /* Variable-length types may be incomplete even if
25568 TREE_ASM_WRITTEN. For such types, fall through to
25569 gen_array_type_die() and possibly fill in
25570 DW_AT_{upper,lower}_bound attributes. */
25571 if ((TREE_CODE (type) != ARRAY_TYPE
25572 && TREE_CODE (type) != RECORD_TYPE
25573 && TREE_CODE (type) != UNION_TYPE
25574 && TREE_CODE (type) != QUAL_UNION_TYPE)
25575 || !variably_modified_type_p (type, NULL))
25576 return;
25579 switch (TREE_CODE (type))
25581 case ERROR_MARK:
25582 break;
25584 case POINTER_TYPE:
25585 case REFERENCE_TYPE:
25586 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
25587 ensures that the gen_type_die recursion will terminate even if the
25588 type is recursive. Recursive types are possible in Ada. */
25589 /* ??? We could perhaps do this for all types before the switch
25590 statement. */
25591 TREE_ASM_WRITTEN (type) = 1;
25593 /* For these types, all that is required is that we output a DIE (or a
25594 set of DIEs) to represent the "basis" type. */
25595 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25596 DINFO_USAGE_IND_USE);
25597 break;
25599 case OFFSET_TYPE:
25600 /* This code is used for C++ pointer-to-data-member types.
25601 Output a description of the relevant class type. */
25602 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
25603 DINFO_USAGE_IND_USE);
25605 /* Output a description of the type of the object pointed to. */
25606 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25607 DINFO_USAGE_IND_USE);
25609 /* Now output a DIE to represent this pointer-to-data-member type
25610 itself. */
25611 gen_ptr_to_mbr_type_die (type, context_die);
25612 break;
25614 case FUNCTION_TYPE:
25615 /* Force out return type (in case it wasn't forced out already). */
25616 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25617 DINFO_USAGE_DIR_USE);
25618 gen_subroutine_type_die (type, context_die);
25619 break;
25621 case METHOD_TYPE:
25622 /* Force out return type (in case it wasn't forced out already). */
25623 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25624 DINFO_USAGE_DIR_USE);
25625 gen_subroutine_type_die (type, context_die);
25626 break;
25628 case ARRAY_TYPE:
25629 case VECTOR_TYPE:
25630 gen_array_type_die (type, context_die);
25631 break;
25633 case ENUMERAL_TYPE:
25634 case RECORD_TYPE:
25635 case UNION_TYPE:
25636 case QUAL_UNION_TYPE:
25637 gen_tagged_type_die (type, context_die, usage);
25638 return;
25640 case VOID_TYPE:
25641 case INTEGER_TYPE:
25642 case REAL_TYPE:
25643 case FIXED_POINT_TYPE:
25644 case COMPLEX_TYPE:
25645 case BOOLEAN_TYPE:
25646 /* No DIEs needed for fundamental types. */
25647 break;
25649 case NULLPTR_TYPE:
25650 case LANG_TYPE:
25651 /* Just use DW_TAG_unspecified_type. */
25653 dw_die_ref type_die = lookup_type_die (type);
25654 if (type_die == NULL)
25656 tree name = TYPE_IDENTIFIER (type);
25657 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
25658 type);
25659 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
25660 equate_type_number_to_die (type, type_die);
25663 break;
25665 default:
25666 if (is_cxx_auto (type))
25668 tree name = TYPE_IDENTIFIER (type);
25669 dw_die_ref *die = (name == get_identifier ("auto")
25670 ? &auto_die : &decltype_auto_die);
25671 if (!*die)
25673 *die = new_die (DW_TAG_unspecified_type,
25674 comp_unit_die (), NULL_TREE);
25675 add_name_attribute (*die, IDENTIFIER_POINTER (name));
25677 equate_type_number_to_die (type, *die);
25678 break;
25680 gcc_unreachable ();
25683 TREE_ASM_WRITTEN (type) = 1;
25686 static void
25687 gen_type_die (tree type, dw_die_ref context_die)
25689 if (type != error_mark_node)
25691 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
25692 if (flag_checking)
25694 dw_die_ref die = lookup_type_die (type);
25695 if (die)
25696 check_die (die);
25701 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
25702 things which are local to the given block. */
25704 static void
25705 gen_block_die (tree stmt, dw_die_ref context_die)
25707 int must_output_die = 0;
25708 bool inlined_func;
25710 /* Ignore blocks that are NULL. */
25711 if (stmt == NULL_TREE)
25712 return;
25714 inlined_func = inlined_function_outer_scope_p (stmt);
25716 /* If the block is one fragment of a non-contiguous block, do not
25717 process the variables, since they will have been done by the
25718 origin block. Do process subblocks. */
25719 if (BLOCK_FRAGMENT_ORIGIN (stmt))
25721 tree sub;
25723 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
25724 gen_block_die (sub, context_die);
25726 return;
25729 /* Determine if we need to output any Dwarf DIEs at all to represent this
25730 block. */
25731 if (inlined_func)
25732 /* The outer scopes for inlinings *must* always be represented. We
25733 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
25734 must_output_die = 1;
25735 else if (BLOCK_DIE (stmt))
25736 /* If we already have a DIE then it was filled early. Meanwhile
25737 we might have pruned all BLOCK_VARS as optimized out but we
25738 still want to generate high/low PC attributes so output it. */
25739 must_output_die = 1;
25740 else if (TREE_USED (stmt)
25741 || TREE_ASM_WRITTEN (stmt)
25742 || BLOCK_ABSTRACT (stmt))
25744 /* Determine if this block directly contains any "significant"
25745 local declarations which we will need to output DIEs for. */
25746 if (debug_info_level > DINFO_LEVEL_TERSE)
25748 /* We are not in terse mode so any local declaration that
25749 is not ignored for debug purposes counts as being a
25750 "significant" one. */
25751 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
25752 must_output_die = 1;
25753 else
25754 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
25755 if (!DECL_IGNORED_P (var))
25757 must_output_die = 1;
25758 break;
25761 else if (!dwarf2out_ignore_block (stmt))
25762 must_output_die = 1;
25765 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
25766 DIE for any block which contains no significant local declarations at
25767 all. Rather, in such cases we just call `decls_for_scope' so that any
25768 needed Dwarf info for any sub-blocks will get properly generated. Note
25769 that in terse mode, our definition of what constitutes a "significant"
25770 local declaration gets restricted to include only inlined function
25771 instances and local (nested) function definitions. */
25772 if (must_output_die)
25774 if (inlined_func)
25776 /* If STMT block is abstract, that means we have been called
25777 indirectly from dwarf2out_abstract_function.
25778 That function rightfully marks the descendent blocks (of
25779 the abstract function it is dealing with) as being abstract,
25780 precisely to prevent us from emitting any
25781 DW_TAG_inlined_subroutine DIE as a descendent
25782 of an abstract function instance. So in that case, we should
25783 not call gen_inlined_subroutine_die.
25785 Later though, when cgraph asks dwarf2out to emit info
25786 for the concrete instance of the function decl into which
25787 the concrete instance of STMT got inlined, the later will lead
25788 to the generation of a DW_TAG_inlined_subroutine DIE. */
25789 if (! BLOCK_ABSTRACT (stmt))
25790 gen_inlined_subroutine_die (stmt, context_die);
25792 else
25793 gen_lexical_block_die (stmt, context_die);
25795 else
25796 decls_for_scope (stmt, context_die);
25799 /* Process variable DECL (or variable with origin ORIGIN) within
25800 block STMT and add it to CONTEXT_DIE. */
25801 static void
25802 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
25804 dw_die_ref die;
25805 tree decl_or_origin = decl ? decl : origin;
25807 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
25808 die = lookup_decl_die (decl_or_origin);
25809 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
25811 if (TYPE_DECL_IS_STUB (decl_or_origin))
25812 die = lookup_type_die (TREE_TYPE (decl_or_origin));
25813 else
25814 die = lookup_decl_die (decl_or_origin);
25815 /* Avoid re-creating the DIE late if it was optimized as unused early. */
25816 if (! die && ! early_dwarf)
25817 return;
25819 else
25820 die = NULL;
25822 /* Avoid creating DIEs for local typedefs and concrete static variables that
25823 will only be pruned later. */
25824 if ((origin || decl_ultimate_origin (decl))
25825 && (TREE_CODE (decl_or_origin) == TYPE_DECL
25826 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
25828 origin = decl_ultimate_origin (decl_or_origin);
25829 if (decl && VAR_P (decl) && die != NULL)
25831 die = lookup_decl_die (origin);
25832 if (die != NULL)
25833 equate_decl_number_to_die (decl, die);
25835 return;
25838 if (die != NULL && die->die_parent == NULL)
25839 add_child_die (context_die, die);
25840 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
25842 if (early_dwarf)
25843 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
25844 stmt, context_die);
25846 else
25848 if (decl && DECL_P (decl))
25850 die = lookup_decl_die (decl);
25852 /* Early created DIEs do not have a parent as the decls refer
25853 to the function as DECL_CONTEXT rather than the BLOCK. */
25854 if (die && die->die_parent == NULL)
25856 gcc_assert (in_lto_p);
25857 add_child_die (context_die, die);
25861 gen_decl_die (decl, origin, NULL, context_die);
25865 /* Generate all of the decls declared within a given scope and (recursively)
25866 all of its sub-blocks. */
25868 static void
25869 decls_for_scope (tree stmt, dw_die_ref context_die)
25871 tree decl;
25872 unsigned int i;
25873 tree subblocks;
25875 /* Ignore NULL blocks. */
25876 if (stmt == NULL_TREE)
25877 return;
25879 /* Output the DIEs to represent all of the data objects and typedefs
25880 declared directly within this block but not within any nested
25881 sub-blocks. Also, nested function and tag DIEs have been
25882 generated with a parent of NULL; fix that up now. We don't
25883 have to do this if we're at -g1. */
25884 if (debug_info_level > DINFO_LEVEL_TERSE)
25886 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25887 process_scope_var (stmt, decl, NULL_TREE, context_die);
25888 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25889 origin - avoid doing this twice as we have no good way to see
25890 if we've done it once already. */
25891 if (! early_dwarf)
25892 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25894 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25895 if (decl == current_function_decl)
25896 /* Ignore declarations of the current function, while they
25897 are declarations, gen_subprogram_die would treat them
25898 as definitions again, because they are equal to
25899 current_function_decl and endlessly recurse. */;
25900 else if (TREE_CODE (decl) == FUNCTION_DECL)
25901 process_scope_var (stmt, decl, NULL_TREE, context_die);
25902 else
25903 process_scope_var (stmt, NULL_TREE, decl, context_die);
25907 /* Even if we're at -g1, we need to process the subblocks in order to get
25908 inlined call information. */
25910 /* Output the DIEs to represent all sub-blocks (and the items declared
25911 therein) of this block. */
25912 for (subblocks = BLOCK_SUBBLOCKS (stmt);
25913 subblocks != NULL;
25914 subblocks = BLOCK_CHAIN (subblocks))
25915 gen_block_die (subblocks, context_die);
25918 /* Is this a typedef we can avoid emitting? */
25920 static bool
25921 is_redundant_typedef (const_tree decl)
25923 if (TYPE_DECL_IS_STUB (decl))
25924 return true;
25926 if (DECL_ARTIFICIAL (decl)
25927 && DECL_CONTEXT (decl)
25928 && is_tagged_type (DECL_CONTEXT (decl))
25929 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
25930 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
25931 /* Also ignore the artificial member typedef for the class name. */
25932 return true;
25934 return false;
25937 /* Return TRUE if TYPE is a typedef that names a type for linkage
25938 purposes. This kind of typedefs is produced by the C++ FE for
25939 constructs like:
25941 typedef struct {...} foo;
25943 In that case, there is no typedef variant type produced for foo.
25944 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
25945 struct type. */
25947 static bool
25948 is_naming_typedef_decl (const_tree decl)
25950 if (decl == NULL_TREE
25951 || TREE_CODE (decl) != TYPE_DECL
25952 || DECL_NAMELESS (decl)
25953 || !is_tagged_type (TREE_TYPE (decl))
25954 || DECL_IS_BUILTIN (decl)
25955 || is_redundant_typedef (decl)
25956 /* It looks like Ada produces TYPE_DECLs that are very similar
25957 to C++ naming typedefs but that have different
25958 semantics. Let's be specific to c++ for now. */
25959 || !is_cxx (decl))
25960 return FALSE;
25962 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25963 && TYPE_NAME (TREE_TYPE (decl)) == decl
25964 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25965 != TYPE_NAME (TREE_TYPE (decl))));
25968 /* Looks up the DIE for a context. */
25970 static inline dw_die_ref
25971 lookup_context_die (tree context)
25973 if (context)
25975 /* Find die that represents this context. */
25976 if (TYPE_P (context))
25978 context = TYPE_MAIN_VARIANT (context);
25979 dw_die_ref ctx = lookup_type_die (context);
25980 if (!ctx)
25981 return NULL;
25982 return strip_naming_typedef (context, ctx);
25984 else
25985 return lookup_decl_die (context);
25987 return comp_unit_die ();
25990 /* Returns the DIE for a context. */
25992 static inline dw_die_ref
25993 get_context_die (tree context)
25995 if (context)
25997 /* Find die that represents this context. */
25998 if (TYPE_P (context))
26000 context = TYPE_MAIN_VARIANT (context);
26001 return strip_naming_typedef (context, force_type_die (context));
26003 else
26004 return force_decl_die (context);
26006 return comp_unit_die ();
26009 /* Returns the DIE for decl. A DIE will always be returned. */
26011 static dw_die_ref
26012 force_decl_die (tree decl)
26014 dw_die_ref decl_die;
26015 unsigned saved_external_flag;
26016 tree save_fn = NULL_TREE;
26017 decl_die = lookup_decl_die (decl);
26018 if (!decl_die)
26020 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26022 decl_die = lookup_decl_die (decl);
26023 if (decl_die)
26024 return decl_die;
26026 switch (TREE_CODE (decl))
26028 case FUNCTION_DECL:
26029 /* Clear current_function_decl, so that gen_subprogram_die thinks
26030 that this is a declaration. At this point, we just want to force
26031 declaration die. */
26032 save_fn = current_function_decl;
26033 current_function_decl = NULL_TREE;
26034 gen_subprogram_die (decl, context_die);
26035 current_function_decl = save_fn;
26036 break;
26038 case VAR_DECL:
26039 /* Set external flag to force declaration die. Restore it after
26040 gen_decl_die() call. */
26041 saved_external_flag = DECL_EXTERNAL (decl);
26042 DECL_EXTERNAL (decl) = 1;
26043 gen_decl_die (decl, NULL, NULL, context_die);
26044 DECL_EXTERNAL (decl) = saved_external_flag;
26045 break;
26047 case NAMESPACE_DECL:
26048 if (dwarf_version >= 3 || !dwarf_strict)
26049 dwarf2out_decl (decl);
26050 else
26051 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26052 decl_die = comp_unit_die ();
26053 break;
26055 case TRANSLATION_UNIT_DECL:
26056 decl_die = comp_unit_die ();
26057 break;
26059 default:
26060 gcc_unreachable ();
26063 /* We should be able to find the DIE now. */
26064 if (!decl_die)
26065 decl_die = lookup_decl_die (decl);
26066 gcc_assert (decl_die);
26069 return decl_die;
26072 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26073 always returned. */
26075 static dw_die_ref
26076 force_type_die (tree type)
26078 dw_die_ref type_die;
26080 type_die = lookup_type_die (type);
26081 if (!type_die)
26083 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26085 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26086 false, context_die);
26087 gcc_assert (type_die);
26089 return type_die;
26092 /* Force out any required namespaces to be able to output DECL,
26093 and return the new context_die for it, if it's changed. */
26095 static dw_die_ref
26096 setup_namespace_context (tree thing, dw_die_ref context_die)
26098 tree context = (DECL_P (thing)
26099 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26100 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26101 /* Force out the namespace. */
26102 context_die = force_decl_die (context);
26104 return context_die;
26107 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26108 type) within its namespace, if appropriate.
26110 For compatibility with older debuggers, namespace DIEs only contain
26111 declarations; all definitions are emitted at CU scope, with
26112 DW_AT_specification pointing to the declaration (like with class
26113 members). */
26115 static dw_die_ref
26116 declare_in_namespace (tree thing, dw_die_ref context_die)
26118 dw_die_ref ns_context;
26120 if (debug_info_level <= DINFO_LEVEL_TERSE)
26121 return context_die;
26123 /* External declarations in the local scope only need to be emitted
26124 once, not once in the namespace and once in the scope.
26126 This avoids declaring the `extern' below in the
26127 namespace DIE as well as in the innermost scope:
26129 namespace S
26131 int i=5;
26132 int foo()
26134 int i=8;
26135 extern int i;
26136 return i;
26140 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26141 return context_die;
26143 /* If this decl is from an inlined function, then don't try to emit it in its
26144 namespace, as we will get confused. It would have already been emitted
26145 when the abstract instance of the inline function was emitted anyways. */
26146 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26147 return context_die;
26149 ns_context = setup_namespace_context (thing, context_die);
26151 if (ns_context != context_die)
26153 if (is_fortran ())
26154 return ns_context;
26155 if (DECL_P (thing))
26156 gen_decl_die (thing, NULL, NULL, ns_context);
26157 else
26158 gen_type_die (thing, ns_context);
26160 return context_die;
26163 /* Generate a DIE for a namespace or namespace alias. */
26165 static void
26166 gen_namespace_die (tree decl, dw_die_ref context_die)
26168 dw_die_ref namespace_die;
26170 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26171 they are an alias of. */
26172 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26174 /* Output a real namespace or module. */
26175 context_die = setup_namespace_context (decl, comp_unit_die ());
26176 namespace_die = new_die (is_fortran ()
26177 ? DW_TAG_module : DW_TAG_namespace,
26178 context_die, decl);
26179 /* For Fortran modules defined in different CU don't add src coords. */
26180 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26182 const char *name = dwarf2_name (decl, 0);
26183 if (name)
26184 add_name_attribute (namespace_die, name);
26186 else
26187 add_name_and_src_coords_attributes (namespace_die, decl);
26188 if (DECL_EXTERNAL (decl))
26189 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26190 equate_decl_number_to_die (decl, namespace_die);
26192 else
26194 /* Output a namespace alias. */
26196 /* Force out the namespace we are an alias of, if necessary. */
26197 dw_die_ref origin_die
26198 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26200 if (DECL_FILE_SCOPE_P (decl)
26201 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26202 context_die = setup_namespace_context (decl, comp_unit_die ());
26203 /* Now create the namespace alias DIE. */
26204 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26205 add_name_and_src_coords_attributes (namespace_die, decl);
26206 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26207 equate_decl_number_to_die (decl, namespace_die);
26209 if ((dwarf_version >= 5 || !dwarf_strict)
26210 && lang_hooks.decls.decl_dwarf_attribute (decl,
26211 DW_AT_export_symbols) == 1)
26212 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26214 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26215 if (want_pubnames ())
26216 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26219 /* Generate Dwarf debug information for a decl described by DECL.
26220 The return value is currently only meaningful for PARM_DECLs,
26221 for all other decls it returns NULL.
26223 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26224 It can be NULL otherwise. */
26226 static dw_die_ref
26227 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26228 dw_die_ref context_die)
26230 tree decl_or_origin = decl ? decl : origin;
26231 tree class_origin = NULL, ultimate_origin;
26233 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26234 return NULL;
26236 switch (TREE_CODE (decl_or_origin))
26238 case ERROR_MARK:
26239 break;
26241 case CONST_DECL:
26242 if (!is_fortran () && !is_ada ())
26244 /* The individual enumerators of an enum type get output when we output
26245 the Dwarf representation of the relevant enum type itself. */
26246 break;
26249 /* Emit its type. */
26250 gen_type_die (TREE_TYPE (decl), context_die);
26252 /* And its containing namespace. */
26253 context_die = declare_in_namespace (decl, context_die);
26255 gen_const_die (decl, context_die);
26256 break;
26258 case FUNCTION_DECL:
26259 #if 0
26260 /* FIXME */
26261 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26262 on local redeclarations of global functions. That seems broken. */
26263 if (current_function_decl != decl)
26264 /* This is only a declaration. */;
26265 #endif
26267 /* We should have abstract copies already and should not generate
26268 stray type DIEs in late LTO dumping. */
26269 if (! early_dwarf)
26272 /* If we're emitting a clone, emit info for the abstract instance. */
26273 else if (origin || DECL_ORIGIN (decl) != decl)
26274 dwarf2out_abstract_function (origin
26275 ? DECL_ORIGIN (origin)
26276 : DECL_ABSTRACT_ORIGIN (decl));
26278 /* If we're emitting a possibly inlined function emit it as
26279 abstract instance. */
26280 else if (cgraph_function_possibly_inlined_p (decl)
26281 && ! DECL_ABSTRACT_P (decl)
26282 && ! class_or_namespace_scope_p (context_die)
26283 /* dwarf2out_abstract_function won't emit a die if this is just
26284 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26285 that case, because that works only if we have a die. */
26286 && DECL_INITIAL (decl) != NULL_TREE)
26287 dwarf2out_abstract_function (decl);
26289 /* Otherwise we're emitting the primary DIE for this decl. */
26290 else if (debug_info_level > DINFO_LEVEL_TERSE)
26292 /* Before we describe the FUNCTION_DECL itself, make sure that we
26293 have its containing type. */
26294 if (!origin)
26295 origin = decl_class_context (decl);
26296 if (origin != NULL_TREE)
26297 gen_type_die (origin, context_die);
26299 /* And its return type. */
26300 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26302 /* And its virtual context. */
26303 if (DECL_VINDEX (decl) != NULL_TREE)
26304 gen_type_die (DECL_CONTEXT (decl), context_die);
26306 /* Make sure we have a member DIE for decl. */
26307 if (origin != NULL_TREE)
26308 gen_type_die_for_member (origin, decl, context_die);
26310 /* And its containing namespace. */
26311 context_die = declare_in_namespace (decl, context_die);
26314 /* Now output a DIE to represent the function itself. */
26315 if (decl)
26316 gen_subprogram_die (decl, context_die);
26317 break;
26319 case TYPE_DECL:
26320 /* If we are in terse mode, don't generate any DIEs to represent any
26321 actual typedefs. */
26322 if (debug_info_level <= DINFO_LEVEL_TERSE)
26323 break;
26325 /* In the special case of a TYPE_DECL node representing the declaration
26326 of some type tag, if the given TYPE_DECL is marked as having been
26327 instantiated from some other (original) TYPE_DECL node (e.g. one which
26328 was generated within the original definition of an inline function) we
26329 used to generate a special (abbreviated) DW_TAG_structure_type,
26330 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26331 should be actually referencing those DIEs, as variable DIEs with that
26332 type would be emitted already in the abstract origin, so it was always
26333 removed during unused type prunning. Don't add anything in this
26334 case. */
26335 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26336 break;
26338 if (is_redundant_typedef (decl))
26339 gen_type_die (TREE_TYPE (decl), context_die);
26340 else
26341 /* Output a DIE to represent the typedef itself. */
26342 gen_typedef_die (decl, context_die);
26343 break;
26345 case LABEL_DECL:
26346 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26347 gen_label_die (decl, context_die);
26348 break;
26350 case VAR_DECL:
26351 case RESULT_DECL:
26352 /* If we are in terse mode, don't generate any DIEs to represent any
26353 variable declarations or definitions. */
26354 if (debug_info_level <= DINFO_LEVEL_TERSE)
26355 break;
26357 /* Avoid generating stray type DIEs during late dwarf dumping.
26358 All types have been dumped early. */
26359 if (early_dwarf
26360 /* ??? But in LTRANS we cannot annotate early created variably
26361 modified type DIEs without copying them and adjusting all
26362 references to them. Dump them again as happens for inlining
26363 which copies both the decl and the types. */
26364 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26365 in VLA bound information for example. */
26366 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26367 current_function_decl)))
26369 /* Output any DIEs that are needed to specify the type of this data
26370 object. */
26371 if (decl_by_reference_p (decl_or_origin))
26372 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26373 else
26374 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26377 if (early_dwarf)
26379 /* And its containing type. */
26380 class_origin = decl_class_context (decl_or_origin);
26381 if (class_origin != NULL_TREE)
26382 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26384 /* And its containing namespace. */
26385 context_die = declare_in_namespace (decl_or_origin, context_die);
26388 /* Now output the DIE to represent the data object itself. This gets
26389 complicated because of the possibility that the VAR_DECL really
26390 represents an inlined instance of a formal parameter for an inline
26391 function. */
26392 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26393 if (ultimate_origin != NULL_TREE
26394 && TREE_CODE (ultimate_origin) == PARM_DECL)
26395 gen_formal_parameter_die (decl, origin,
26396 true /* Emit name attribute. */,
26397 context_die);
26398 else
26399 gen_variable_die (decl, origin, context_die);
26400 break;
26402 case FIELD_DECL:
26403 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26404 /* Ignore the nameless fields that are used to skip bits but handle C++
26405 anonymous unions and structs. */
26406 if (DECL_NAME (decl) != NULL_TREE
26407 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26408 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26410 gen_type_die (member_declared_type (decl), context_die);
26411 gen_field_die (decl, ctx, context_die);
26413 break;
26415 case PARM_DECL:
26416 /* Avoid generating stray type DIEs during late dwarf dumping.
26417 All types have been dumped early. */
26418 if (early_dwarf
26419 /* ??? But in LTRANS we cannot annotate early created variably
26420 modified type DIEs without copying them and adjusting all
26421 references to them. Dump them again as happens for inlining
26422 which copies both the decl and the types. */
26423 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26424 in VLA bound information for example. */
26425 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26426 current_function_decl)))
26428 if (DECL_BY_REFERENCE (decl_or_origin))
26429 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26430 else
26431 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26433 return gen_formal_parameter_die (decl, origin,
26434 true /* Emit name attribute. */,
26435 context_die);
26437 case NAMESPACE_DECL:
26438 if (dwarf_version >= 3 || !dwarf_strict)
26439 gen_namespace_die (decl, context_die);
26440 break;
26442 case IMPORTED_DECL:
26443 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26444 DECL_CONTEXT (decl), context_die);
26445 break;
26447 case NAMELIST_DECL:
26448 gen_namelist_decl (DECL_NAME (decl), context_die,
26449 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26450 break;
26452 default:
26453 /* Probably some frontend-internal decl. Assume we don't care. */
26454 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26455 break;
26458 return NULL;
26461 /* Output initial debug information for global DECL. Called at the
26462 end of the parsing process.
26464 This is the initial debug generation process. As such, the DIEs
26465 generated may be incomplete. A later debug generation pass
26466 (dwarf2out_late_global_decl) will augment the information generated
26467 in this pass (e.g., with complete location info). */
26469 static void
26470 dwarf2out_early_global_decl (tree decl)
26472 set_early_dwarf s;
26474 /* gen_decl_die() will set DECL_ABSTRACT because
26475 cgraph_function_possibly_inlined_p() returns true. This is in
26476 turn will cause DW_AT_inline attributes to be set.
26478 This happens because at early dwarf generation, there is no
26479 cgraph information, causing cgraph_function_possibly_inlined_p()
26480 to return true. Trick cgraph_function_possibly_inlined_p()
26481 while we generate dwarf early. */
26482 bool save = symtab->global_info_ready;
26483 symtab->global_info_ready = true;
26485 /* We don't handle TYPE_DECLs. If required, they'll be reached via
26486 other DECLs and they can point to template types or other things
26487 that dwarf2out can't handle when done via dwarf2out_decl. */
26488 if (TREE_CODE (decl) != TYPE_DECL
26489 && TREE_CODE (decl) != PARM_DECL)
26491 if (TREE_CODE (decl) == FUNCTION_DECL)
26493 tree save_fndecl = current_function_decl;
26495 /* For nested functions, make sure we have DIEs for the parents first
26496 so that all nested DIEs are generated at the proper scope in the
26497 first shot. */
26498 tree context = decl_function_context (decl);
26499 if (context != NULL)
26501 dw_die_ref context_die = lookup_decl_die (context);
26502 current_function_decl = context;
26504 /* Avoid emitting DIEs multiple times, but still process CONTEXT
26505 enough so that it lands in its own context. This avoids type
26506 pruning issues later on. */
26507 if (context_die == NULL || is_declaration_die (context_die))
26508 dwarf2out_decl (context);
26511 /* Emit an abstract origin of a function first. This happens
26512 with C++ constructor clones for example and makes
26513 dwarf2out_abstract_function happy which requires the early
26514 DIE of the abstract instance to be present. */
26515 tree origin = DECL_ABSTRACT_ORIGIN (decl);
26516 dw_die_ref origin_die;
26517 if (origin != NULL
26518 /* Do not emit the DIE multiple times but make sure to
26519 process it fully here in case we just saw a declaration. */
26520 && ((origin_die = lookup_decl_die (origin)) == NULL
26521 || is_declaration_die (origin_die)))
26523 current_function_decl = origin;
26524 dwarf2out_decl (origin);
26527 /* Emit the DIE for decl but avoid doing that multiple times. */
26528 dw_die_ref old_die;
26529 if ((old_die = lookup_decl_die (decl)) == NULL
26530 || is_declaration_die (old_die))
26532 current_function_decl = decl;
26533 dwarf2out_decl (decl);
26536 current_function_decl = save_fndecl;
26538 else
26539 dwarf2out_decl (decl);
26541 symtab->global_info_ready = save;
26544 /* Return whether EXPR is an expression with the following pattern:
26545 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
26547 static bool
26548 is_trivial_indirect_ref (tree expr)
26550 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
26551 return false;
26553 tree nop = TREE_OPERAND (expr, 0);
26554 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
26555 return false;
26557 tree int_cst = TREE_OPERAND (nop, 0);
26558 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
26561 /* Output debug information for global decl DECL. Called from
26562 toplev.c after compilation proper has finished. */
26564 static void
26565 dwarf2out_late_global_decl (tree decl)
26567 /* Fill-in any location information we were unable to determine
26568 on the first pass. */
26569 if (VAR_P (decl))
26571 dw_die_ref die = lookup_decl_die (decl);
26573 /* We may have to generate early debug late for LTO in case debug
26574 was not enabled at compile-time or the target doesn't support
26575 the LTO early debug scheme. */
26576 if (! die && in_lto_p)
26578 dwarf2out_decl (decl);
26579 die = lookup_decl_die (decl);
26582 if (die)
26584 /* We get called via the symtab code invoking late_global_decl
26585 for symbols that are optimized out.
26587 Do not add locations for those, except if they have a
26588 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
26589 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
26590 INDIRECT_REF expression, as this could generate relocations to
26591 text symbols in LTO object files, which is invalid. */
26592 varpool_node *node = varpool_node::get (decl);
26593 if ((! node || ! node->definition)
26594 && ! (DECL_HAS_VALUE_EXPR_P (decl)
26595 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
26596 tree_add_const_value_attribute_for_decl (die, decl);
26597 else
26598 add_location_or_const_value_attribute (die, decl, false);
26603 /* Output debug information for type decl DECL. Called from toplev.c
26604 and from language front ends (to record built-in types). */
26605 static void
26606 dwarf2out_type_decl (tree decl, int local)
26608 if (!local)
26610 set_early_dwarf s;
26611 dwarf2out_decl (decl);
26615 /* Output debug information for imported module or decl DECL.
26616 NAME is non-NULL name in the lexical block if the decl has been renamed.
26617 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
26618 that DECL belongs to.
26619 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
26620 static void
26621 dwarf2out_imported_module_or_decl_1 (tree decl,
26622 tree name,
26623 tree lexical_block,
26624 dw_die_ref lexical_block_die)
26626 expanded_location xloc;
26627 dw_die_ref imported_die = NULL;
26628 dw_die_ref at_import_die;
26630 if (TREE_CODE (decl) == IMPORTED_DECL)
26632 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
26633 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
26634 gcc_assert (decl);
26636 else
26637 xloc = expand_location (input_location);
26639 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
26641 at_import_die = force_type_die (TREE_TYPE (decl));
26642 /* For namespace N { typedef void T; } using N::T; base_type_die
26643 returns NULL, but DW_TAG_imported_declaration requires
26644 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
26645 if (!at_import_die)
26647 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
26648 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
26649 at_import_die = lookup_type_die (TREE_TYPE (decl));
26650 gcc_assert (at_import_die);
26653 else
26655 at_import_die = lookup_decl_die (decl);
26656 if (!at_import_die)
26658 /* If we're trying to avoid duplicate debug info, we may not have
26659 emitted the member decl for this field. Emit it now. */
26660 if (TREE_CODE (decl) == FIELD_DECL)
26662 tree type = DECL_CONTEXT (decl);
26664 if (TYPE_CONTEXT (type)
26665 && TYPE_P (TYPE_CONTEXT (type))
26666 && !should_emit_struct_debug (TYPE_CONTEXT (type),
26667 DINFO_USAGE_DIR_USE))
26668 return;
26669 gen_type_die_for_member (type, decl,
26670 get_context_die (TYPE_CONTEXT (type)));
26672 if (TREE_CODE (decl) == NAMELIST_DECL)
26673 at_import_die = gen_namelist_decl (DECL_NAME (decl),
26674 get_context_die (DECL_CONTEXT (decl)),
26675 NULL_TREE);
26676 else
26677 at_import_die = force_decl_die (decl);
26681 if (TREE_CODE (decl) == NAMESPACE_DECL)
26683 if (dwarf_version >= 3 || !dwarf_strict)
26684 imported_die = new_die (DW_TAG_imported_module,
26685 lexical_block_die,
26686 lexical_block);
26687 else
26688 return;
26690 else
26691 imported_die = new_die (DW_TAG_imported_declaration,
26692 lexical_block_die,
26693 lexical_block);
26695 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
26696 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
26697 if (debug_column_info && xloc.column)
26698 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
26699 if (name)
26700 add_AT_string (imported_die, DW_AT_name,
26701 IDENTIFIER_POINTER (name));
26702 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
26705 /* Output debug information for imported module or decl DECL.
26706 NAME is non-NULL name in context if the decl has been renamed.
26707 CHILD is true if decl is one of the renamed decls as part of
26708 importing whole module.
26709 IMPLICIT is set if this hook is called for an implicit import
26710 such as inline namespace. */
26712 static void
26713 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
26714 bool child, bool implicit)
26716 /* dw_die_ref at_import_die; */
26717 dw_die_ref scope_die;
26719 if (debug_info_level <= DINFO_LEVEL_TERSE)
26720 return;
26722 gcc_assert (decl);
26724 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
26725 should be enough, for DWARF4 and older even if we emit as extension
26726 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
26727 for the benefit of consumers unaware of DW_AT_export_symbols. */
26728 if (implicit
26729 && dwarf_version >= 5
26730 && lang_hooks.decls.decl_dwarf_attribute (decl,
26731 DW_AT_export_symbols) == 1)
26732 return;
26734 set_early_dwarf s;
26736 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
26737 We need decl DIE for reference and scope die. First, get DIE for the decl
26738 itself. */
26740 /* Get the scope die for decl context. Use comp_unit_die for global module
26741 or decl. If die is not found for non globals, force new die. */
26742 if (context
26743 && TYPE_P (context)
26744 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
26745 return;
26747 scope_die = get_context_die (context);
26749 if (child)
26751 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
26752 there is nothing we can do, here. */
26753 if (dwarf_version < 3 && dwarf_strict)
26754 return;
26756 gcc_assert (scope_die->die_child);
26757 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
26758 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
26759 scope_die = scope_die->die_child;
26762 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
26763 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
26766 /* Output debug information for namelists. */
26768 static dw_die_ref
26769 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
26771 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
26772 tree value;
26773 unsigned i;
26775 if (debug_info_level <= DINFO_LEVEL_TERSE)
26776 return NULL;
26778 gcc_assert (scope_die != NULL);
26779 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
26780 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
26782 /* If there are no item_decls, we have a nondefining namelist, e.g.
26783 with USE association; hence, set DW_AT_declaration. */
26784 if (item_decls == NULL_TREE)
26786 add_AT_flag (nml_die, DW_AT_declaration, 1);
26787 return nml_die;
26790 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
26792 nml_item_ref_die = lookup_decl_die (value);
26793 if (!nml_item_ref_die)
26794 nml_item_ref_die = force_decl_die (value);
26796 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
26797 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
26799 return nml_die;
26803 /* Write the debugging output for DECL and return the DIE. */
26805 static void
26806 dwarf2out_decl (tree decl)
26808 dw_die_ref context_die = comp_unit_die ();
26810 switch (TREE_CODE (decl))
26812 case ERROR_MARK:
26813 return;
26815 case FUNCTION_DECL:
26816 /* If we're a nested function, initially use a parent of NULL; if we're
26817 a plain function, this will be fixed up in decls_for_scope. If
26818 we're a method, it will be ignored, since we already have a DIE.
26819 Avoid doing this late though since clones of class methods may
26820 otherwise end up in limbo and create type DIEs late. */
26821 if (early_dwarf
26822 && decl_function_context (decl)
26823 /* But if we're in terse mode, we don't care about scope. */
26824 && debug_info_level > DINFO_LEVEL_TERSE)
26825 context_die = NULL;
26826 break;
26828 case VAR_DECL:
26829 /* For local statics lookup proper context die. */
26830 if (local_function_static (decl))
26831 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26833 /* If we are in terse mode, don't generate any DIEs to represent any
26834 variable declarations or definitions. */
26835 if (debug_info_level <= DINFO_LEVEL_TERSE)
26836 return;
26837 break;
26839 case CONST_DECL:
26840 if (debug_info_level <= DINFO_LEVEL_TERSE)
26841 return;
26842 if (!is_fortran () && !is_ada ())
26843 return;
26844 if (TREE_STATIC (decl) && decl_function_context (decl))
26845 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26846 break;
26848 case NAMESPACE_DECL:
26849 case IMPORTED_DECL:
26850 if (debug_info_level <= DINFO_LEVEL_TERSE)
26851 return;
26852 if (lookup_decl_die (decl) != NULL)
26853 return;
26854 break;
26856 case TYPE_DECL:
26857 /* Don't emit stubs for types unless they are needed by other DIEs. */
26858 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
26859 return;
26861 /* Don't bother trying to generate any DIEs to represent any of the
26862 normal built-in types for the language we are compiling. */
26863 if (DECL_IS_BUILTIN (decl))
26864 return;
26866 /* If we are in terse mode, don't generate any DIEs for types. */
26867 if (debug_info_level <= DINFO_LEVEL_TERSE)
26868 return;
26870 /* If we're a function-scope tag, initially use a parent of NULL;
26871 this will be fixed up in decls_for_scope. */
26872 if (decl_function_context (decl))
26873 context_die = NULL;
26875 break;
26877 case NAMELIST_DECL:
26878 break;
26880 default:
26881 return;
26884 gen_decl_die (decl, NULL, NULL, context_die);
26886 if (flag_checking)
26888 dw_die_ref die = lookup_decl_die (decl);
26889 if (die)
26890 check_die (die);
26894 /* Write the debugging output for DECL. */
26896 static void
26897 dwarf2out_function_decl (tree decl)
26899 dwarf2out_decl (decl);
26900 call_arg_locations = NULL;
26901 call_arg_loc_last = NULL;
26902 call_site_count = -1;
26903 tail_call_site_count = -1;
26904 decl_loc_table->empty ();
26905 cached_dw_loc_list_table->empty ();
26908 /* Output a marker (i.e. a label) for the beginning of the generated code for
26909 a lexical block. */
26911 static void
26912 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
26913 unsigned int blocknum)
26915 switch_to_section (current_function_section ());
26916 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
26919 /* Output a marker (i.e. a label) for the end of the generated code for a
26920 lexical block. */
26922 static void
26923 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
26925 switch_to_section (current_function_section ());
26926 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
26929 /* Returns nonzero if it is appropriate not to emit any debugging
26930 information for BLOCK, because it doesn't contain any instructions.
26932 Don't allow this for blocks with nested functions or local classes
26933 as we would end up with orphans, and in the presence of scheduling
26934 we may end up calling them anyway. */
26936 static bool
26937 dwarf2out_ignore_block (const_tree block)
26939 tree decl;
26940 unsigned int i;
26942 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
26943 if (TREE_CODE (decl) == FUNCTION_DECL
26944 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26945 return 0;
26946 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
26948 decl = BLOCK_NONLOCALIZED_VAR (block, i);
26949 if (TREE_CODE (decl) == FUNCTION_DECL
26950 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26951 return 0;
26954 return 1;
26957 /* Hash table routines for file_hash. */
26959 bool
26960 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
26962 return filename_cmp (p1->filename, p2) == 0;
26965 hashval_t
26966 dwarf_file_hasher::hash (dwarf_file_data *p)
26968 return htab_hash_string (p->filename);
26971 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26972 dwarf2out.c) and return its "index". The index of each (known) filename is
26973 just a unique number which is associated with only that one filename. We
26974 need such numbers for the sake of generating labels (in the .debug_sfnames
26975 section) and references to those files numbers (in the .debug_srcinfo
26976 and .debug_macinfo sections). If the filename given as an argument is not
26977 found in our current list, add it to the list and assign it the next
26978 available unique index number. */
26980 static struct dwarf_file_data *
26981 lookup_filename (const char *file_name)
26983 struct dwarf_file_data * created;
26985 if (!file_name)
26986 return NULL;
26988 dwarf_file_data **slot
26989 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26990 INSERT);
26991 if (*slot)
26992 return *slot;
26994 created = ggc_alloc<dwarf_file_data> ();
26995 created->filename = file_name;
26996 created->emitted_number = 0;
26997 *slot = created;
26998 return created;
27001 /* If the assembler will construct the file table, then translate the compiler
27002 internal file table number into the assembler file table number, and emit
27003 a .file directive if we haven't already emitted one yet. The file table
27004 numbers are different because we prune debug info for unused variables and
27005 types, which may include filenames. */
27007 static int
27008 maybe_emit_file (struct dwarf_file_data * fd)
27010 if (! fd->emitted_number)
27012 if (last_emitted_file)
27013 fd->emitted_number = last_emitted_file->emitted_number + 1;
27014 else
27015 fd->emitted_number = 1;
27016 last_emitted_file = fd;
27018 if (output_asm_line_debug_info ())
27020 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27021 output_quoted_string (asm_out_file,
27022 remap_debug_filename (fd->filename));
27023 fputc ('\n', asm_out_file);
27027 return fd->emitted_number;
27030 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27031 That generation should happen after function debug info has been
27032 generated. The value of the attribute is the constant value of ARG. */
27034 static void
27035 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27037 die_arg_entry entry;
27039 if (!die || !arg)
27040 return;
27042 gcc_assert (early_dwarf);
27044 if (!tmpl_value_parm_die_table)
27045 vec_alloc (tmpl_value_parm_die_table, 32);
27047 entry.die = die;
27048 entry.arg = arg;
27049 vec_safe_push (tmpl_value_parm_die_table, entry);
27052 /* Return TRUE if T is an instance of generic type, FALSE
27053 otherwise. */
27055 static bool
27056 generic_type_p (tree t)
27058 if (t == NULL_TREE || !TYPE_P (t))
27059 return false;
27060 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27063 /* Schedule the generation of the generic parameter dies for the
27064 instance of generic type T. The proper generation itself is later
27065 done by gen_scheduled_generic_parms_dies. */
27067 static void
27068 schedule_generic_params_dies_gen (tree t)
27070 if (!generic_type_p (t))
27071 return;
27073 gcc_assert (early_dwarf);
27075 if (!generic_type_instances)
27076 vec_alloc (generic_type_instances, 256);
27078 vec_safe_push (generic_type_instances, t);
27081 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27082 by append_entry_to_tmpl_value_parm_die_table. This function must
27083 be called after function DIEs have been generated. */
27085 static void
27086 gen_remaining_tmpl_value_param_die_attribute (void)
27088 if (tmpl_value_parm_die_table)
27090 unsigned i, j;
27091 die_arg_entry *e;
27093 /* We do this in two phases - first get the cases we can
27094 handle during early-finish, preserving those we cannot
27095 (containing symbolic constants where we don't yet know
27096 whether we are going to output the referenced symbols).
27097 For those we try again at late-finish. */
27098 j = 0;
27099 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27101 if (!e->die->removed
27102 && !tree_add_const_value_attribute (e->die, e->arg))
27104 dw_loc_descr_ref loc = NULL;
27105 if (! early_dwarf
27106 && (dwarf_version >= 5 || !dwarf_strict))
27107 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27108 if (loc)
27109 add_AT_loc (e->die, DW_AT_location, loc);
27110 else
27111 (*tmpl_value_parm_die_table)[j++] = *e;
27114 tmpl_value_parm_die_table->truncate (j);
27118 /* Generate generic parameters DIEs for instances of generic types
27119 that have been previously scheduled by
27120 schedule_generic_params_dies_gen. This function must be called
27121 after all the types of the CU have been laid out. */
27123 static void
27124 gen_scheduled_generic_parms_dies (void)
27126 unsigned i;
27127 tree t;
27129 if (!generic_type_instances)
27130 return;
27132 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27133 if (COMPLETE_TYPE_P (t))
27134 gen_generic_params_dies (t);
27136 generic_type_instances = NULL;
27140 /* Replace DW_AT_name for the decl with name. */
27142 static void
27143 dwarf2out_set_name (tree decl, tree name)
27145 dw_die_ref die;
27146 dw_attr_node *attr;
27147 const char *dname;
27149 die = TYPE_SYMTAB_DIE (decl);
27150 if (!die)
27151 return;
27153 dname = dwarf2_name (name, 0);
27154 if (!dname)
27155 return;
27157 attr = get_AT (die, DW_AT_name);
27158 if (attr)
27160 struct indirect_string_node *node;
27162 node = find_AT_string (dname);
27163 /* replace the string. */
27164 attr->dw_attr_val.v.val_str = node;
27167 else
27168 add_name_attribute (die, dname);
27171 /* True if before or during processing of the first function being emitted. */
27172 static bool in_first_function_p = true;
27173 /* True if loc_note during dwarf2out_var_location call might still be
27174 before first real instruction at address equal to .Ltext0. */
27175 static bool maybe_at_text_label_p = true;
27176 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27177 static unsigned int first_loclabel_num_not_at_text_label;
27179 /* Look ahead for a real insn, or for a begin stmt marker. */
27181 static rtx_insn *
27182 dwarf2out_next_real_insn (rtx_insn *loc_note)
27184 rtx_insn *next_real = NEXT_INSN (loc_note);
27186 while (next_real)
27187 if (INSN_P (next_real))
27188 break;
27189 else
27190 next_real = NEXT_INSN (next_real);
27192 return next_real;
27195 /* Called by the final INSN scan whenever we see a var location. We
27196 use it to drop labels in the right places, and throw the location in
27197 our lookup table. */
27199 static void
27200 dwarf2out_var_location (rtx_insn *loc_note)
27202 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27203 struct var_loc_node *newloc;
27204 rtx_insn *next_real, *next_note;
27205 rtx_insn *call_insn = NULL;
27206 static const char *last_label;
27207 static const char *last_postcall_label;
27208 static bool last_in_cold_section_p;
27209 static rtx_insn *expected_next_loc_note;
27210 tree decl;
27211 bool var_loc_p;
27212 var_loc_view view = 0;
27214 if (!NOTE_P (loc_note))
27216 if (CALL_P (loc_note))
27218 maybe_reset_location_view (loc_note, cur_line_info_table);
27219 call_site_count++;
27220 if (SIBLING_CALL_P (loc_note))
27221 tail_call_site_count++;
27222 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27224 call_insn = loc_note;
27225 loc_note = NULL;
27226 var_loc_p = false;
27228 next_real = dwarf2out_next_real_insn (call_insn);
27229 next_note = NULL;
27230 cached_next_real_insn = NULL;
27231 goto create_label;
27233 if (optimize == 0 && !flag_var_tracking)
27235 /* When the var-tracking pass is not running, there is no note
27236 for indirect calls whose target is compile-time known. In this
27237 case, process such calls specifically so that we generate call
27238 sites for them anyway. */
27239 rtx x = PATTERN (loc_note);
27240 if (GET_CODE (x) == PARALLEL)
27241 x = XVECEXP (x, 0, 0);
27242 if (GET_CODE (x) == SET)
27243 x = SET_SRC (x);
27244 if (GET_CODE (x) == CALL)
27245 x = XEXP (x, 0);
27246 if (!MEM_P (x)
27247 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27248 || !SYMBOL_REF_DECL (XEXP (x, 0))
27249 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27250 != FUNCTION_DECL))
27252 call_insn = loc_note;
27253 loc_note = NULL;
27254 var_loc_p = false;
27256 next_real = dwarf2out_next_real_insn (call_insn);
27257 next_note = NULL;
27258 cached_next_real_insn = NULL;
27259 goto create_label;
27263 else if (!debug_variable_location_views)
27264 gcc_unreachable ();
27265 else
27266 maybe_reset_location_view (loc_note, cur_line_info_table);
27268 return;
27271 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27272 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27273 return;
27275 /* Optimize processing a large consecutive sequence of location
27276 notes so we don't spend too much time in next_real_insn. If the
27277 next insn is another location note, remember the next_real_insn
27278 calculation for next time. */
27279 next_real = cached_next_real_insn;
27280 if (next_real)
27282 if (expected_next_loc_note != loc_note)
27283 next_real = NULL;
27286 next_note = NEXT_INSN (loc_note);
27287 if (! next_note
27288 || next_note->deleted ()
27289 || ! NOTE_P (next_note)
27290 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
27291 && NOTE_KIND (next_note) != NOTE_INSN_BEGIN_STMT
27292 && NOTE_KIND (next_note) != NOTE_INSN_INLINE_ENTRY))
27293 next_note = NULL;
27295 if (! next_real)
27296 next_real = dwarf2out_next_real_insn (loc_note);
27298 if (next_note)
27300 expected_next_loc_note = next_note;
27301 cached_next_real_insn = next_real;
27303 else
27304 cached_next_real_insn = NULL;
27306 /* If there are no instructions which would be affected by this note,
27307 don't do anything. */
27308 if (var_loc_p
27309 && next_real == NULL_RTX
27310 && !NOTE_DURING_CALL_P (loc_note))
27311 return;
27313 create_label:
27315 if (next_real == NULL_RTX)
27316 next_real = get_last_insn ();
27318 /* If there were any real insns between note we processed last time
27319 and this note (or if it is the first note), clear
27320 last_{,postcall_}label so that they are not reused this time. */
27321 if (last_var_location_insn == NULL_RTX
27322 || last_var_location_insn != next_real
27323 || last_in_cold_section_p != in_cold_section_p)
27325 last_label = NULL;
27326 last_postcall_label = NULL;
27329 if (var_loc_p)
27331 const char *label
27332 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27333 view = cur_line_info_table->view;
27334 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27335 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27336 if (newloc == NULL)
27337 return;
27339 else
27341 decl = NULL_TREE;
27342 newloc = NULL;
27345 /* If there were no real insns between note we processed last time
27346 and this note, use the label we emitted last time. Otherwise
27347 create a new label and emit it. */
27348 if (last_label == NULL)
27350 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27351 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27352 loclabel_num++;
27353 last_label = ggc_strdup (loclabel);
27354 /* See if loclabel might be equal to .Ltext0. If yes,
27355 bump first_loclabel_num_not_at_text_label. */
27356 if (!have_multiple_function_sections
27357 && in_first_function_p
27358 && maybe_at_text_label_p)
27360 static rtx_insn *last_start;
27361 rtx_insn *insn;
27362 for (insn = loc_note; insn; insn = previous_insn (insn))
27363 if (insn == last_start)
27364 break;
27365 else if (!NONDEBUG_INSN_P (insn))
27366 continue;
27367 else
27369 rtx body = PATTERN (insn);
27370 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27371 continue;
27372 /* Inline asm could occupy zero bytes. */
27373 else if (GET_CODE (body) == ASM_INPUT
27374 || asm_noperands (body) >= 0)
27375 continue;
27376 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27377 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27378 continue;
27379 #endif
27380 else
27382 /* Assume insn has non-zero length. */
27383 maybe_at_text_label_p = false;
27384 break;
27387 if (maybe_at_text_label_p)
27389 last_start = loc_note;
27390 first_loclabel_num_not_at_text_label = loclabel_num;
27395 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27396 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27398 if (!var_loc_p)
27400 struct call_arg_loc_node *ca_loc
27401 = ggc_cleared_alloc<call_arg_loc_node> ();
27402 rtx_insn *prev = call_insn;
27404 ca_loc->call_arg_loc_note
27405 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27406 ca_loc->next = NULL;
27407 ca_loc->label = last_label;
27408 gcc_assert (prev
27409 && (CALL_P (prev)
27410 || (NONJUMP_INSN_P (prev)
27411 && GET_CODE (PATTERN (prev)) == SEQUENCE
27412 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27413 if (!CALL_P (prev))
27414 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27415 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27417 /* Look for a SYMBOL_REF in the "prev" instruction. */
27418 rtx x = get_call_rtx_from (PATTERN (prev));
27419 if (x)
27421 /* Try to get the call symbol, if any. */
27422 if (MEM_P (XEXP (x, 0)))
27423 x = XEXP (x, 0);
27424 /* First, look for a memory access to a symbol_ref. */
27425 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27426 && SYMBOL_REF_DECL (XEXP (x, 0))
27427 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27428 ca_loc->symbol_ref = XEXP (x, 0);
27429 /* Otherwise, look at a compile-time known user-level function
27430 declaration. */
27431 else if (MEM_P (x)
27432 && MEM_EXPR (x)
27433 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27434 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27437 ca_loc->block = insn_scope (prev);
27438 if (call_arg_locations)
27439 call_arg_loc_last->next = ca_loc;
27440 else
27441 call_arg_locations = ca_loc;
27442 call_arg_loc_last = ca_loc;
27444 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27446 newloc->label = last_label;
27447 newloc->view = view;
27449 else
27451 if (!last_postcall_label)
27453 sprintf (loclabel, "%s-1", last_label);
27454 last_postcall_label = ggc_strdup (loclabel);
27456 newloc->label = last_postcall_label;
27457 /* ??? This view is at last_label, not last_label-1, but we
27458 could only assume view at last_label-1 is zero if we could
27459 assume calls always have length greater than one. This is
27460 probably true in general, though there might be a rare
27461 exception to this rule, e.g. if a call insn is optimized out
27462 by target magic. Then, even the -1 in the label will be
27463 wrong, which might invalidate the range. Anyway, using view,
27464 though technically possibly incorrect, will work as far as
27465 ranges go: since L-1 is in the middle of the call insn,
27466 (L-1).0 and (L-1).V shouldn't make any difference, and having
27467 the loclist entry refer to the .loc entry might be useful, so
27468 leave it like this. */
27469 newloc->view = view;
27472 if (var_loc_p && flag_debug_asm)
27474 const char *name, *sep, *patstr;
27475 if (decl && DECL_NAME (decl))
27476 name = IDENTIFIER_POINTER (DECL_NAME (decl));
27477 else
27478 name = "";
27479 if (NOTE_VAR_LOCATION_LOC (loc_note))
27481 sep = " => ";
27482 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27484 else
27486 sep = " ";
27487 patstr = "RESET";
27489 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27490 name, sep, patstr);
27493 last_var_location_insn = next_real;
27494 last_in_cold_section_p = in_cold_section_p;
27497 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
27498 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
27499 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
27500 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
27501 BLOCK_FRAGMENT_ORIGIN links. */
27502 static bool
27503 block_within_block_p (tree block, tree outer, bool bothways)
27505 if (block == outer)
27506 return true;
27508 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
27509 for (tree context = BLOCK_SUPERCONTEXT (block);
27510 context != outer;
27511 context = BLOCK_SUPERCONTEXT (context))
27512 if (!context || TREE_CODE (context) != BLOCK)
27513 return false;
27515 if (!bothways)
27516 return true;
27518 /* Now check that each block is actually referenced by its
27519 parent. */
27520 for (tree context = BLOCK_SUPERCONTEXT (block); ;
27521 context = BLOCK_SUPERCONTEXT (context))
27523 if (BLOCK_FRAGMENT_ORIGIN (context))
27525 gcc_assert (!BLOCK_SUBBLOCKS (context));
27526 context = BLOCK_FRAGMENT_ORIGIN (context);
27528 for (tree sub = BLOCK_SUBBLOCKS (context);
27529 sub != block;
27530 sub = BLOCK_CHAIN (sub))
27531 if (!sub)
27532 return false;
27533 if (context == outer)
27534 return true;
27535 else
27536 block = context;
27540 /* Called during final while assembling the marker of the entry point
27541 for an inlined function. */
27543 static void
27544 dwarf2out_inline_entry (tree block)
27546 gcc_assert (debug_inline_points);
27548 /* If we can't represent it, don't bother. */
27549 if (!(dwarf_version >= 3 || !dwarf_strict))
27550 return;
27552 gcc_assert (DECL_P (block_ultimate_origin (block)));
27554 /* Sanity check the block tree. This would catch a case in which
27555 BLOCK got removed from the tree reachable from the outermost
27556 lexical block, but got retained in markers. It would still link
27557 back to its parents, but some ancestor would be missing a link
27558 down the path to the sub BLOCK. If the block got removed, its
27559 BLOCK_NUMBER will not be a usable value. */
27560 if (flag_checking)
27561 gcc_assert (block_within_block_p (block,
27562 DECL_INITIAL (current_function_decl),
27563 true));
27565 gcc_assert (inlined_function_outer_scope_p (block));
27566 gcc_assert (!BLOCK_DIE (block));
27568 if (BLOCK_FRAGMENT_ORIGIN (block))
27569 block = BLOCK_FRAGMENT_ORIGIN (block);
27570 /* Can the entry point ever not be at the beginning of an
27571 unfragmented lexical block? */
27572 else if (!(BLOCK_FRAGMENT_CHAIN (block)
27573 || (cur_line_info_table
27574 && !ZERO_VIEW_P (cur_line_info_table->view))))
27575 return;
27577 if (!inline_entry_data_table)
27578 inline_entry_data_table
27579 = hash_table<inline_entry_data_hasher>::create_ggc (10);
27582 inline_entry_data **iedp
27583 = inline_entry_data_table->find_slot_with_hash (block,
27584 htab_hash_pointer (block),
27585 INSERT);
27586 if (*iedp)
27587 /* ??? Ideally, we'd record all entry points for the same inlined
27588 function (some may have been duplicated by e.g. unrolling), but
27589 we have no way to represent that ATM. */
27590 return;
27592 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
27593 ied->block = block;
27594 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
27595 ied->label_num = BLOCK_NUMBER (block);
27596 if (cur_line_info_table)
27597 ied->view = cur_line_info_table->view;
27599 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27601 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_INLINE_ENTRY_LABEL,
27602 BLOCK_NUMBER (block));
27603 ASM_OUTPUT_LABEL (asm_out_file, label);
27606 /* Called from finalize_size_functions for size functions so that their body
27607 can be encoded in the debug info to describe the layout of variable-length
27608 structures. */
27610 static void
27611 dwarf2out_size_function (tree decl)
27613 function_to_dwarf_procedure (decl);
27616 /* Note in one location list that text section has changed. */
27619 var_location_switch_text_section_1 (var_loc_list **slot, void *)
27621 var_loc_list *list = *slot;
27622 if (list->first)
27623 list->last_before_switch
27624 = list->last->next ? list->last->next : list->last;
27625 return 1;
27628 /* Note in all location lists that text section has changed. */
27630 static void
27631 var_location_switch_text_section (void)
27633 if (decl_loc_table == NULL)
27634 return;
27636 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
27639 /* Create a new line number table. */
27641 static dw_line_info_table *
27642 new_line_info_table (void)
27644 dw_line_info_table *table;
27646 table = ggc_cleared_alloc<dw_line_info_table> ();
27647 table->file_num = 1;
27648 table->line_num = 1;
27649 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
27650 FORCE_RESET_NEXT_VIEW (table->view);
27651 table->symviews_since_reset = 0;
27653 return table;
27656 /* Lookup the "current" table into which we emit line info, so
27657 that we don't have to do it for every source line. */
27659 static void
27660 set_cur_line_info_table (section *sec)
27662 dw_line_info_table *table;
27664 if (sec == text_section)
27665 table = text_section_line_info;
27666 else if (sec == cold_text_section)
27668 table = cold_text_section_line_info;
27669 if (!table)
27671 cold_text_section_line_info = table = new_line_info_table ();
27672 table->end_label = cold_end_label;
27675 else
27677 const char *end_label;
27679 if (crtl->has_bb_partition)
27681 if (in_cold_section_p)
27682 end_label = crtl->subsections.cold_section_end_label;
27683 else
27684 end_label = crtl->subsections.hot_section_end_label;
27686 else
27688 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27689 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
27690 current_function_funcdef_no);
27691 end_label = ggc_strdup (label);
27694 table = new_line_info_table ();
27695 table->end_label = end_label;
27697 vec_safe_push (separate_line_info, table);
27700 if (output_asm_line_debug_info ())
27701 table->is_stmt = (cur_line_info_table
27702 ? cur_line_info_table->is_stmt
27703 : DWARF_LINE_DEFAULT_IS_STMT_START);
27704 cur_line_info_table = table;
27708 /* We need to reset the locations at the beginning of each
27709 function. We can't do this in the end_function hook, because the
27710 declarations that use the locations won't have been output when
27711 that hook is called. Also compute have_multiple_function_sections here. */
27713 static void
27714 dwarf2out_begin_function (tree fun)
27716 section *sec = function_section (fun);
27718 if (sec != text_section)
27719 have_multiple_function_sections = true;
27721 if (crtl->has_bb_partition && !cold_text_section)
27723 gcc_assert (current_function_decl == fun);
27724 cold_text_section = unlikely_text_section ();
27725 switch_to_section (cold_text_section);
27726 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
27727 switch_to_section (sec);
27730 dwarf2out_note_section_used ();
27731 call_site_count = 0;
27732 tail_call_site_count = 0;
27734 set_cur_line_info_table (sec);
27735 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
27738 /* Helper function of dwarf2out_end_function, called only after emitting
27739 the very first function into assembly. Check if some .debug_loc range
27740 might end with a .LVL* label that could be equal to .Ltext0.
27741 In that case we must force using absolute addresses in .debug_loc ranges,
27742 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
27743 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
27744 list terminator.
27745 Set have_multiple_function_sections to true in that case and
27746 terminate htab traversal. */
27749 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
27751 var_loc_list *entry = *slot;
27752 struct var_loc_node *node;
27754 node = entry->first;
27755 if (node && node->next && node->next->label)
27757 unsigned int i;
27758 const char *label = node->next->label;
27759 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
27761 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
27763 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
27764 if (strcmp (label, loclabel) == 0)
27766 have_multiple_function_sections = true;
27767 return 0;
27771 return 1;
27774 /* Hook called after emitting a function into assembly.
27775 This does something only for the very first function emitted. */
27777 static void
27778 dwarf2out_end_function (unsigned int)
27780 if (in_first_function_p
27781 && !have_multiple_function_sections
27782 && first_loclabel_num_not_at_text_label
27783 && decl_loc_table)
27784 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
27785 in_first_function_p = false;
27786 maybe_at_text_label_p = false;
27789 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
27790 front-ends register a translation unit even before dwarf2out_init is
27791 called. */
27792 static tree main_translation_unit = NULL_TREE;
27794 /* Hook called by front-ends after they built their main translation unit.
27795 Associate comp_unit_die to UNIT. */
27797 static void
27798 dwarf2out_register_main_translation_unit (tree unit)
27800 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
27801 && main_translation_unit == NULL_TREE);
27802 main_translation_unit = unit;
27803 /* If dwarf2out_init has not been called yet, it will perform the association
27804 itself looking at main_translation_unit. */
27805 if (decl_die_table != NULL)
27806 equate_decl_number_to_die (unit, comp_unit_die ());
27809 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
27811 static void
27812 push_dw_line_info_entry (dw_line_info_table *table,
27813 enum dw_line_info_opcode opcode, unsigned int val)
27815 dw_line_info_entry e;
27816 e.opcode = opcode;
27817 e.val = val;
27818 vec_safe_push (table->entries, e);
27821 /* Output a label to mark the beginning of a source code line entry
27822 and record information relating to this source line, in
27823 'line_info_table' for later output of the .debug_line section. */
27824 /* ??? The discriminator parameter ought to be unsigned. */
27826 static void
27827 dwarf2out_source_line (unsigned int line, unsigned int column,
27828 const char *filename,
27829 int discriminator, bool is_stmt)
27831 unsigned int file_num;
27832 dw_line_info_table *table;
27833 static var_loc_view lvugid;
27835 if (debug_info_level < DINFO_LEVEL_TERSE)
27836 return;
27838 table = cur_line_info_table;
27840 if (line == 0)
27842 if (debug_variable_location_views
27843 && output_asm_line_debug_info ()
27844 && table && !RESETTING_VIEW_P (table->view))
27846 /* If we're using the assembler to compute view numbers, we
27847 can't issue a .loc directive for line zero, so we can't
27848 get a view number at this point. We might attempt to
27849 compute it from the previous view, or equate it to a
27850 subsequent view (though it might not be there!), but
27851 since we're omitting the line number entry, we might as
27852 well omit the view number as well. That means pretending
27853 it's a view number zero, which might very well turn out
27854 to be correct. ??? Extend the assembler so that the
27855 compiler could emit e.g. ".locview .LVU#", to output a
27856 view without changing line number information. We'd then
27857 have to count it in symviews_since_reset; when it's omitted,
27858 it doesn't count. */
27859 if (!zero_view_p)
27860 zero_view_p = BITMAP_GGC_ALLOC ();
27861 bitmap_set_bit (zero_view_p, table->view);
27862 if (flag_debug_asm)
27864 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27865 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27866 fprintf (asm_out_file, "\t%s line 0, omitted view ",
27867 ASM_COMMENT_START);
27868 assemble_name (asm_out_file, label);
27869 putc ('\n', asm_out_file);
27871 table->view = ++lvugid;
27873 return;
27876 /* The discriminator column was added in dwarf4. Simplify the below
27877 by simply removing it if we're not supposed to output it. */
27878 if (dwarf_version < 4 && dwarf_strict)
27879 discriminator = 0;
27881 if (!debug_column_info)
27882 column = 0;
27884 file_num = maybe_emit_file (lookup_filename (filename));
27886 /* ??? TODO: Elide duplicate line number entries. Traditionally,
27887 the debugger has used the second (possibly duplicate) line number
27888 at the beginning of the function to mark the end of the prologue.
27889 We could eliminate any other duplicates within the function. For
27890 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
27891 that second line number entry. */
27892 /* Recall that this end-of-prologue indication is *not* the same thing
27893 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
27894 to which the hook corresponds, follows the last insn that was
27895 emitted by gen_prologue. What we need is to precede the first insn
27896 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
27897 insn that corresponds to something the user wrote. These may be
27898 very different locations once scheduling is enabled. */
27900 if (0 && file_num == table->file_num
27901 && line == table->line_num
27902 && column == table->column_num
27903 && discriminator == table->discrim_num
27904 && is_stmt == table->is_stmt)
27905 return;
27907 switch_to_section (current_function_section ());
27909 /* If requested, emit something human-readable. */
27910 if (flag_debug_asm)
27912 if (debug_column_info)
27913 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
27914 filename, line, column);
27915 else
27916 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
27917 filename, line);
27920 if (output_asm_line_debug_info ())
27922 /* Emit the .loc directive understood by GNU as. */
27923 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
27924 file_num, line, is_stmt, discriminator */
27925 fputs ("\t.loc ", asm_out_file);
27926 fprint_ul (asm_out_file, file_num);
27927 putc (' ', asm_out_file);
27928 fprint_ul (asm_out_file, line);
27929 putc (' ', asm_out_file);
27930 fprint_ul (asm_out_file, column);
27932 if (is_stmt != table->is_stmt)
27934 fputs (" is_stmt ", asm_out_file);
27935 putc (is_stmt ? '1' : '0', asm_out_file);
27937 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
27939 gcc_assert (discriminator > 0);
27940 fputs (" discriminator ", asm_out_file);
27941 fprint_ul (asm_out_file, (unsigned long) discriminator);
27943 if (debug_variable_location_views)
27945 if (!RESETTING_VIEW_P (table->view))
27947 table->symviews_since_reset++;
27948 if (table->symviews_since_reset > symview_upper_bound)
27949 symview_upper_bound = table->symviews_since_reset;
27950 /* When we're using the assembler to compute view
27951 numbers, we output symbolic labels after "view" in
27952 .loc directives, and the assembler will set them for
27953 us, so that we can refer to the view numbers in
27954 location lists. The only exceptions are when we know
27955 a view will be zero: "-0" is a forced reset, used
27956 e.g. in the beginning of functions, whereas "0" tells
27957 the assembler to check that there was a PC change
27958 since the previous view, in a way that implicitly
27959 resets the next view. */
27960 fputs (" view ", asm_out_file);
27961 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27962 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27963 assemble_name (asm_out_file, label);
27964 table->view = ++lvugid;
27966 else
27968 table->symviews_since_reset = 0;
27969 if (FORCE_RESETTING_VIEW_P (table->view))
27970 fputs (" view -0", asm_out_file);
27971 else
27972 fputs (" view 0", asm_out_file);
27973 /* Mark the present view as a zero view. Earlier debug
27974 binds may have already added its id to loclists to be
27975 emitted later, so we can't reuse the id for something
27976 else. However, it's good to know whether a view is
27977 known to be zero, because then we may be able to
27978 optimize out locviews that are all zeros, so take
27979 note of it in zero_view_p. */
27980 if (!zero_view_p)
27981 zero_view_p = BITMAP_GGC_ALLOC ();
27982 bitmap_set_bit (zero_view_p, lvugid);
27983 table->view = ++lvugid;
27986 putc ('\n', asm_out_file);
27988 else
27990 unsigned int label_num = ++line_info_label_num;
27992 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
27994 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
27995 push_dw_line_info_entry (table, LI_adv_address, label_num);
27996 else
27997 push_dw_line_info_entry (table, LI_set_address, label_num);
27998 if (debug_variable_location_views)
28000 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28001 if (resetting)
28002 table->view = 0;
28004 if (flag_debug_asm)
28005 fprintf (asm_out_file, "\t%s view %s%d\n",
28006 ASM_COMMENT_START,
28007 resetting ? "-" : "",
28008 table->view);
28010 table->view++;
28012 if (file_num != table->file_num)
28013 push_dw_line_info_entry (table, LI_set_file, file_num);
28014 if (discriminator != table->discrim_num)
28015 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28016 if (is_stmt != table->is_stmt)
28017 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28018 push_dw_line_info_entry (table, LI_set_line, line);
28019 if (debug_column_info)
28020 push_dw_line_info_entry (table, LI_set_column, column);
28023 table->file_num = file_num;
28024 table->line_num = line;
28025 table->column_num = column;
28026 table->discrim_num = discriminator;
28027 table->is_stmt = is_stmt;
28028 table->in_use = true;
28031 /* Record the beginning of a new source file. */
28033 static void
28034 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28036 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28038 macinfo_entry e;
28039 e.code = DW_MACINFO_start_file;
28040 e.lineno = lineno;
28041 e.info = ggc_strdup (filename);
28042 vec_safe_push (macinfo_table, e);
28046 /* Record the end of a source file. */
28048 static void
28049 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28051 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28053 macinfo_entry e;
28054 e.code = DW_MACINFO_end_file;
28055 e.lineno = lineno;
28056 e.info = NULL;
28057 vec_safe_push (macinfo_table, e);
28061 /* Called from debug_define in toplev.c. The `buffer' parameter contains
28062 the tail part of the directive line, i.e. the part which is past the
28063 initial whitespace, #, whitespace, directive-name, whitespace part. */
28065 static void
28066 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28067 const char *buffer ATTRIBUTE_UNUSED)
28069 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28071 macinfo_entry e;
28072 /* Insert a dummy first entry to be able to optimize the whole
28073 predefined macro block using DW_MACRO_import. */
28074 if (macinfo_table->is_empty () && lineno <= 1)
28076 e.code = 0;
28077 e.lineno = 0;
28078 e.info = NULL;
28079 vec_safe_push (macinfo_table, e);
28081 e.code = DW_MACINFO_define;
28082 e.lineno = lineno;
28083 e.info = ggc_strdup (buffer);
28084 vec_safe_push (macinfo_table, e);
28088 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
28089 the tail part of the directive line, i.e. the part which is past the
28090 initial whitespace, #, whitespace, directive-name, whitespace part. */
28092 static void
28093 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28094 const char *buffer ATTRIBUTE_UNUSED)
28096 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28098 macinfo_entry e;
28099 /* Insert a dummy first entry to be able to optimize the whole
28100 predefined macro block using DW_MACRO_import. */
28101 if (macinfo_table->is_empty () && lineno <= 1)
28103 e.code = 0;
28104 e.lineno = 0;
28105 e.info = NULL;
28106 vec_safe_push (macinfo_table, e);
28108 e.code = DW_MACINFO_undef;
28109 e.lineno = lineno;
28110 e.info = ggc_strdup (buffer);
28111 vec_safe_push (macinfo_table, e);
28115 /* Helpers to manipulate hash table of CUs. */
28117 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28119 static inline hashval_t hash (const macinfo_entry *);
28120 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28123 inline hashval_t
28124 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28126 return htab_hash_string (entry->info);
28129 inline bool
28130 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28131 const macinfo_entry *entry2)
28133 return !strcmp (entry1->info, entry2->info);
28136 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28138 /* Output a single .debug_macinfo entry. */
28140 static void
28141 output_macinfo_op (macinfo_entry *ref)
28143 int file_num;
28144 size_t len;
28145 struct indirect_string_node *node;
28146 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28147 struct dwarf_file_data *fd;
28149 switch (ref->code)
28151 case DW_MACINFO_start_file:
28152 fd = lookup_filename (ref->info);
28153 file_num = maybe_emit_file (fd);
28154 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28155 dw2_asm_output_data_uleb128 (ref->lineno,
28156 "Included from line number %lu",
28157 (unsigned long) ref->lineno);
28158 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28159 break;
28160 case DW_MACINFO_end_file:
28161 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28162 break;
28163 case DW_MACINFO_define:
28164 case DW_MACINFO_undef:
28165 len = strlen (ref->info) + 1;
28166 if (!dwarf_strict
28167 && len > DWARF_OFFSET_SIZE
28168 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28169 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28171 ref->code = ref->code == DW_MACINFO_define
28172 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28173 output_macinfo_op (ref);
28174 return;
28176 dw2_asm_output_data (1, ref->code,
28177 ref->code == DW_MACINFO_define
28178 ? "Define macro" : "Undefine macro");
28179 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28180 (unsigned long) ref->lineno);
28181 dw2_asm_output_nstring (ref->info, -1, "The macro");
28182 break;
28183 case DW_MACRO_define_strp:
28184 case DW_MACRO_undef_strp:
28185 node = find_AT_string (ref->info);
28186 gcc_assert (node
28187 && (node->form == DW_FORM_strp
28188 || node->form == dwarf_FORM (DW_FORM_strx)));
28189 dw2_asm_output_data (1, ref->code,
28190 ref->code == DW_MACRO_define_strp
28191 ? "Define macro strp"
28192 : "Undefine macro strp");
28193 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28194 (unsigned long) ref->lineno);
28195 if (node->form == DW_FORM_strp)
28196 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
28197 debug_str_section, "The macro: \"%s\"",
28198 ref->info);
28199 else
28200 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28201 ref->info);
28202 break;
28203 case DW_MACRO_import:
28204 dw2_asm_output_data (1, ref->code, "Import");
28205 ASM_GENERATE_INTERNAL_LABEL (label,
28206 DEBUG_MACRO_SECTION_LABEL,
28207 ref->lineno + macinfo_label_base);
28208 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
28209 break;
28210 default:
28211 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28212 ASM_COMMENT_START, (unsigned long) ref->code);
28213 break;
28217 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28218 other compilation unit .debug_macinfo sections. IDX is the first
28219 index of a define/undef, return the number of ops that should be
28220 emitted in a comdat .debug_macinfo section and emit
28221 a DW_MACRO_import entry referencing it.
28222 If the define/undef entry should be emitted normally, return 0. */
28224 static unsigned
28225 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28226 macinfo_hash_type **macinfo_htab)
28228 macinfo_entry *first, *second, *cur, *inc;
28229 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28230 unsigned char checksum[16];
28231 struct md5_ctx ctx;
28232 char *grp_name, *tail;
28233 const char *base;
28234 unsigned int i, count, encoded_filename_len, linebuf_len;
28235 macinfo_entry **slot;
28237 first = &(*macinfo_table)[idx];
28238 second = &(*macinfo_table)[idx + 1];
28240 /* Optimize only if there are at least two consecutive define/undef ops,
28241 and either all of them are before first DW_MACINFO_start_file
28242 with lineno {0,1} (i.e. predefined macro block), or all of them are
28243 in some included header file. */
28244 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28245 return 0;
28246 if (vec_safe_is_empty (files))
28248 if (first->lineno > 1 || second->lineno > 1)
28249 return 0;
28251 else if (first->lineno == 0)
28252 return 0;
28254 /* Find the last define/undef entry that can be grouped together
28255 with first and at the same time compute md5 checksum of their
28256 codes, linenumbers and strings. */
28257 md5_init_ctx (&ctx);
28258 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28259 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28260 break;
28261 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28262 break;
28263 else
28265 unsigned char code = cur->code;
28266 md5_process_bytes (&code, 1, &ctx);
28267 checksum_uleb128 (cur->lineno, &ctx);
28268 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28270 md5_finish_ctx (&ctx, checksum);
28271 count = i - idx;
28273 /* From the containing include filename (if any) pick up just
28274 usable characters from its basename. */
28275 if (vec_safe_is_empty (files))
28276 base = "";
28277 else
28278 base = lbasename (files->last ().info);
28279 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28280 if (ISIDNUM (base[i]) || base[i] == '.')
28281 encoded_filename_len++;
28282 /* Count . at the end. */
28283 if (encoded_filename_len)
28284 encoded_filename_len++;
28286 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28287 linebuf_len = strlen (linebuf);
28289 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28290 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28291 + 16 * 2 + 1);
28292 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
28293 tail = grp_name + 4;
28294 if (encoded_filename_len)
28296 for (i = 0; base[i]; i++)
28297 if (ISIDNUM (base[i]) || base[i] == '.')
28298 *tail++ = base[i];
28299 *tail++ = '.';
28301 memcpy (tail, linebuf, linebuf_len);
28302 tail += linebuf_len;
28303 *tail++ = '.';
28304 for (i = 0; i < 16; i++)
28305 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28307 /* Construct a macinfo_entry for DW_MACRO_import
28308 in the empty vector entry before the first define/undef. */
28309 inc = &(*macinfo_table)[idx - 1];
28310 inc->code = DW_MACRO_import;
28311 inc->lineno = 0;
28312 inc->info = ggc_strdup (grp_name);
28313 if (!*macinfo_htab)
28314 *macinfo_htab = new macinfo_hash_type (10);
28315 /* Avoid emitting duplicates. */
28316 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28317 if (*slot != NULL)
28319 inc->code = 0;
28320 inc->info = NULL;
28321 /* If such an entry has been used before, just emit
28322 a DW_MACRO_import op. */
28323 inc = *slot;
28324 output_macinfo_op (inc);
28325 /* And clear all macinfo_entry in the range to avoid emitting them
28326 in the second pass. */
28327 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28329 cur->code = 0;
28330 cur->info = NULL;
28333 else
28335 *slot = inc;
28336 inc->lineno = (*macinfo_htab)->elements ();
28337 output_macinfo_op (inc);
28339 return count;
28342 /* Save any strings needed by the macinfo table in the debug str
28343 table. All strings must be collected into the table by the time
28344 index_string is called. */
28346 static void
28347 save_macinfo_strings (void)
28349 unsigned len;
28350 unsigned i;
28351 macinfo_entry *ref;
28353 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28355 switch (ref->code)
28357 /* Match the logic in output_macinfo_op to decide on
28358 indirect strings. */
28359 case DW_MACINFO_define:
28360 case DW_MACINFO_undef:
28361 len = strlen (ref->info) + 1;
28362 if (!dwarf_strict
28363 && len > DWARF_OFFSET_SIZE
28364 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28365 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28366 set_indirect_string (find_AT_string (ref->info));
28367 break;
28368 case DW_MACINFO_start_file:
28369 /* -gsplit-dwarf -g3 will also output filename as indirect
28370 string. */
28371 if (!dwarf_split_debug_info)
28372 break;
28373 /* Fall through. */
28374 case DW_MACRO_define_strp:
28375 case DW_MACRO_undef_strp:
28376 set_indirect_string (find_AT_string (ref->info));
28377 break;
28378 default:
28379 break;
28384 /* Output macinfo section(s). */
28386 static void
28387 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28389 unsigned i;
28390 unsigned long length = vec_safe_length (macinfo_table);
28391 macinfo_entry *ref;
28392 vec<macinfo_entry, va_gc> *files = NULL;
28393 macinfo_hash_type *macinfo_htab = NULL;
28394 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28396 if (! length)
28397 return;
28399 /* output_macinfo* uses these interchangeably. */
28400 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28401 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28402 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28403 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28405 /* AIX Assembler inserts the length, so adjust the reference to match the
28406 offset expected by debuggers. */
28407 strcpy (dl_section_ref, debug_line_label);
28408 if (XCOFF_DEBUGGING_INFO)
28409 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28411 /* For .debug_macro emit the section header. */
28412 if (!dwarf_strict || dwarf_version >= 5)
28414 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28415 "DWARF macro version number");
28416 if (DWARF_OFFSET_SIZE == 8)
28417 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28418 else
28419 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28420 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
28421 debug_line_section, NULL);
28424 /* In the first loop, it emits the primary .debug_macinfo section
28425 and after each emitted op the macinfo_entry is cleared.
28426 If a longer range of define/undef ops can be optimized using
28427 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28428 the vector before the first define/undef in the range and the
28429 whole range of define/undef ops is not emitted and kept. */
28430 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28432 switch (ref->code)
28434 case DW_MACINFO_start_file:
28435 vec_safe_push (files, *ref);
28436 break;
28437 case DW_MACINFO_end_file:
28438 if (!vec_safe_is_empty (files))
28439 files->pop ();
28440 break;
28441 case DW_MACINFO_define:
28442 case DW_MACINFO_undef:
28443 if ((!dwarf_strict || dwarf_version >= 5)
28444 && HAVE_COMDAT_GROUP
28445 && vec_safe_length (files) != 1
28446 && i > 0
28447 && i + 1 < length
28448 && (*macinfo_table)[i - 1].code == 0)
28450 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28451 if (count)
28453 i += count - 1;
28454 continue;
28457 break;
28458 case 0:
28459 /* A dummy entry may be inserted at the beginning to be able
28460 to optimize the whole block of predefined macros. */
28461 if (i == 0)
28462 continue;
28463 default:
28464 break;
28466 output_macinfo_op (ref);
28467 ref->info = NULL;
28468 ref->code = 0;
28471 if (!macinfo_htab)
28472 return;
28474 /* Save the number of transparent includes so we can adjust the
28475 label number for the fat LTO object DWARF. */
28476 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
28478 delete macinfo_htab;
28479 macinfo_htab = NULL;
28481 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
28482 terminate the current chain and switch to a new comdat .debug_macinfo
28483 section and emit the define/undef entries within it. */
28484 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28485 switch (ref->code)
28487 case 0:
28488 continue;
28489 case DW_MACRO_import:
28491 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28492 tree comdat_key = get_identifier (ref->info);
28493 /* Terminate the previous .debug_macinfo section. */
28494 dw2_asm_output_data (1, 0, "End compilation unit");
28495 targetm.asm_out.named_section (debug_macinfo_section_name,
28496 SECTION_DEBUG
28497 | SECTION_LINKONCE
28498 | (early_lto_debug
28499 ? SECTION_EXCLUDE : 0),
28500 comdat_key);
28501 ASM_GENERATE_INTERNAL_LABEL (label,
28502 DEBUG_MACRO_SECTION_LABEL,
28503 ref->lineno + macinfo_label_base);
28504 ASM_OUTPUT_LABEL (asm_out_file, label);
28505 ref->code = 0;
28506 ref->info = NULL;
28507 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28508 "DWARF macro version number");
28509 if (DWARF_OFFSET_SIZE == 8)
28510 dw2_asm_output_data (1, 1, "Flags: 64-bit");
28511 else
28512 dw2_asm_output_data (1, 0, "Flags: 32-bit");
28514 break;
28515 case DW_MACINFO_define:
28516 case DW_MACINFO_undef:
28517 output_macinfo_op (ref);
28518 ref->code = 0;
28519 ref->info = NULL;
28520 break;
28521 default:
28522 gcc_unreachable ();
28525 macinfo_label_base += macinfo_label_base_adj;
28528 /* Initialize the various sections and labels for dwarf output and prefix
28529 them with PREFIX if non-NULL. Returns the generation (zero based
28530 number of times function was called). */
28532 static unsigned
28533 init_sections_and_labels (bool early_lto_debug)
28535 /* As we may get called multiple times have a generation count for
28536 labels. */
28537 static unsigned generation = 0;
28539 if (early_lto_debug)
28541 if (!dwarf_split_debug_info)
28543 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28544 SECTION_DEBUG | SECTION_EXCLUDE,
28545 NULL);
28546 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
28547 SECTION_DEBUG | SECTION_EXCLUDE,
28548 NULL);
28549 debug_macinfo_section_name
28550 = ((dwarf_strict && dwarf_version < 5)
28551 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
28552 debug_macinfo_section = get_section (debug_macinfo_section_name,
28553 SECTION_DEBUG
28554 | SECTION_EXCLUDE, NULL);
28556 else
28558 /* ??? Which of the following do we need early? */
28559 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
28560 SECTION_DEBUG | SECTION_EXCLUDE,
28561 NULL);
28562 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
28563 SECTION_DEBUG | SECTION_EXCLUDE,
28564 NULL);
28565 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28566 SECTION_DEBUG
28567 | SECTION_EXCLUDE, NULL);
28568 debug_skeleton_abbrev_section
28569 = get_section (DEBUG_LTO_ABBREV_SECTION,
28570 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28571 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28572 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28573 generation);
28575 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28576 stay in the main .o, but the skeleton_line goes into the split
28577 off dwo. */
28578 debug_skeleton_line_section
28579 = get_section (DEBUG_LTO_LINE_SECTION,
28580 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28581 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28582 DEBUG_SKELETON_LINE_SECTION_LABEL,
28583 generation);
28584 debug_str_offsets_section
28585 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
28586 SECTION_DEBUG | SECTION_EXCLUDE,
28587 NULL);
28588 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28589 DEBUG_SKELETON_INFO_SECTION_LABEL,
28590 generation);
28591 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
28592 DEBUG_STR_DWO_SECTION_FLAGS,
28593 NULL);
28594 debug_macinfo_section_name
28595 = ((dwarf_strict && dwarf_version < 5)
28596 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
28597 debug_macinfo_section = get_section (debug_macinfo_section_name,
28598 SECTION_DEBUG | SECTION_EXCLUDE,
28599 NULL);
28601 /* For macro info and the file table we have to refer to a
28602 debug_line section. */
28603 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
28604 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28605 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28606 DEBUG_LINE_SECTION_LABEL, generation);
28608 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
28609 DEBUG_STR_SECTION_FLAGS
28610 | SECTION_EXCLUDE, NULL);
28611 if (!dwarf_split_debug_info)
28612 debug_line_str_section
28613 = get_section (DEBUG_LTO_LINE_STR_SECTION,
28614 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
28616 else
28618 if (!dwarf_split_debug_info)
28620 debug_info_section = get_section (DEBUG_INFO_SECTION,
28621 SECTION_DEBUG, NULL);
28622 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28623 SECTION_DEBUG, NULL);
28624 debug_loc_section = get_section (dwarf_version >= 5
28625 ? DEBUG_LOCLISTS_SECTION
28626 : DEBUG_LOC_SECTION,
28627 SECTION_DEBUG, NULL);
28628 debug_macinfo_section_name
28629 = ((dwarf_strict && dwarf_version < 5)
28630 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
28631 debug_macinfo_section = get_section (debug_macinfo_section_name,
28632 SECTION_DEBUG, NULL);
28634 else
28636 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
28637 SECTION_DEBUG | SECTION_EXCLUDE,
28638 NULL);
28639 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
28640 SECTION_DEBUG | SECTION_EXCLUDE,
28641 NULL);
28642 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
28643 SECTION_DEBUG, NULL);
28644 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
28645 SECTION_DEBUG, NULL);
28646 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28647 SECTION_DEBUG, NULL);
28648 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28649 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28650 generation);
28652 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28653 stay in the main .o, but the skeleton_line goes into the
28654 split off dwo. */
28655 debug_skeleton_line_section
28656 = get_section (DEBUG_DWO_LINE_SECTION,
28657 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28658 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28659 DEBUG_SKELETON_LINE_SECTION_LABEL,
28660 generation);
28661 debug_str_offsets_section
28662 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
28663 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28664 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28665 DEBUG_SKELETON_INFO_SECTION_LABEL,
28666 generation);
28667 debug_loc_section = get_section (dwarf_version >= 5
28668 ? DEBUG_DWO_LOCLISTS_SECTION
28669 : DEBUG_DWO_LOC_SECTION,
28670 SECTION_DEBUG | SECTION_EXCLUDE,
28671 NULL);
28672 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
28673 DEBUG_STR_DWO_SECTION_FLAGS,
28674 NULL);
28675 debug_macinfo_section_name
28676 = ((dwarf_strict && dwarf_version < 5)
28677 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
28678 debug_macinfo_section = get_section (debug_macinfo_section_name,
28679 SECTION_DEBUG | SECTION_EXCLUDE,
28680 NULL);
28682 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
28683 SECTION_DEBUG, NULL);
28684 debug_line_section = get_section (DEBUG_LINE_SECTION,
28685 SECTION_DEBUG, NULL);
28686 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
28687 SECTION_DEBUG, NULL);
28688 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
28689 SECTION_DEBUG, NULL);
28690 debug_str_section = get_section (DEBUG_STR_SECTION,
28691 DEBUG_STR_SECTION_FLAGS, NULL);
28692 if (!dwarf_split_debug_info && !output_asm_line_debug_info ())
28693 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
28694 DEBUG_STR_SECTION_FLAGS, NULL);
28696 debug_ranges_section = get_section (dwarf_version >= 5
28697 ? DEBUG_RNGLISTS_SECTION
28698 : DEBUG_RANGES_SECTION,
28699 SECTION_DEBUG, NULL);
28700 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
28701 SECTION_DEBUG, NULL);
28704 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
28705 DEBUG_ABBREV_SECTION_LABEL, generation);
28706 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
28707 DEBUG_INFO_SECTION_LABEL, generation);
28708 info_section_emitted = false;
28709 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28710 DEBUG_LINE_SECTION_LABEL, generation);
28711 /* There are up to 4 unique ranges labels per generation.
28712 See also output_rnglists. */
28713 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
28714 DEBUG_RANGES_SECTION_LABEL, generation * 4);
28715 if (dwarf_version >= 5 && dwarf_split_debug_info)
28716 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
28717 DEBUG_RANGES_SECTION_LABEL,
28718 1 + generation * 4);
28719 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
28720 DEBUG_ADDR_SECTION_LABEL, generation);
28721 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
28722 (dwarf_strict && dwarf_version < 5)
28723 ? DEBUG_MACINFO_SECTION_LABEL
28724 : DEBUG_MACRO_SECTION_LABEL, generation);
28725 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
28726 generation);
28728 ++generation;
28729 return generation - 1;
28732 /* Set up for Dwarf output at the start of compilation. */
28734 static void
28735 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
28737 /* Allocate the file_table. */
28738 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
28740 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28741 /* Allocate the decl_die_table. */
28742 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
28744 /* Allocate the decl_loc_table. */
28745 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
28747 /* Allocate the cached_dw_loc_list_table. */
28748 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
28750 /* Allocate the initial hunk of the abbrev_die_table. */
28751 vec_alloc (abbrev_die_table, 256);
28752 /* Zero-th entry is allocated, but unused. */
28753 abbrev_die_table->quick_push (NULL);
28755 /* Allocate the dwarf_proc_stack_usage_map. */
28756 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
28758 /* Allocate the pubtypes and pubnames vectors. */
28759 vec_alloc (pubname_table, 32);
28760 vec_alloc (pubtype_table, 32);
28762 vec_alloc (incomplete_types, 64);
28764 vec_alloc (used_rtx_array, 32);
28766 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28767 vec_alloc (macinfo_table, 64);
28768 #endif
28770 /* If front-ends already registered a main translation unit but we were not
28771 ready to perform the association, do this now. */
28772 if (main_translation_unit != NULL_TREE)
28773 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
28776 /* Called before compile () starts outputtting functions, variables
28777 and toplevel asms into assembly. */
28779 static void
28780 dwarf2out_assembly_start (void)
28782 if (text_section_line_info)
28783 return;
28785 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28786 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
28787 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
28788 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
28789 COLD_TEXT_SECTION_LABEL, 0);
28790 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
28792 switch_to_section (text_section);
28793 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
28794 #endif
28796 /* Make sure the line number table for .text always exists. */
28797 text_section_line_info = new_line_info_table ();
28798 text_section_line_info->end_label = text_end_label;
28800 #ifdef DWARF2_LINENO_DEBUGGING_INFO
28801 cur_line_info_table = text_section_line_info;
28802 #endif
28804 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
28805 && dwarf2out_do_cfi_asm ()
28806 && !dwarf2out_do_eh_frame ())
28807 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
28810 /* A helper function for dwarf2out_finish called through
28811 htab_traverse. Assign a string its index. All strings must be
28812 collected into the table by the time index_string is called,
28813 because the indexing code relies on htab_traverse to traverse nodes
28814 in the same order for each run. */
28817 index_string (indirect_string_node **h, unsigned int *index)
28819 indirect_string_node *node = *h;
28821 find_string_form (node);
28822 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28824 gcc_assert (node->index == NO_INDEX_ASSIGNED);
28825 node->index = *index;
28826 *index += 1;
28828 return 1;
28831 /* A helper function for output_indirect_strings called through
28832 htab_traverse. Output the offset to a string and update the
28833 current offset. */
28836 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
28838 indirect_string_node *node = *h;
28840 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28842 /* Assert that this node has been assigned an index. */
28843 gcc_assert (node->index != NO_INDEX_ASSIGNED
28844 && node->index != NOT_INDEXED);
28845 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
28846 "indexed string 0x%x: %s", node->index, node->str);
28847 *offset += strlen (node->str) + 1;
28849 return 1;
28852 /* A helper function for dwarf2out_finish called through
28853 htab_traverse. Output the indexed string. */
28856 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
28858 struct indirect_string_node *node = *h;
28860 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28862 /* Assert that the strings are output in the same order as their
28863 indexes were assigned. */
28864 gcc_assert (*cur_idx == node->index);
28865 assemble_string (node->str, strlen (node->str) + 1);
28866 *cur_idx += 1;
28868 return 1;
28871 /* A helper function for output_indirect_strings. Counts the number
28872 of index strings offsets. Must match the logic of the functions
28873 output_index_string[_offsets] above. */
28875 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
28877 struct indirect_string_node *node = *h;
28879 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28880 *last_idx += 1;
28881 return 1;
28884 /* A helper function for dwarf2out_finish called through
28885 htab_traverse. Emit one queued .debug_str string. */
28888 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
28890 struct indirect_string_node *node = *h;
28892 node->form = find_string_form (node);
28893 if (node->form == form && node->refcount > 0)
28895 ASM_OUTPUT_LABEL (asm_out_file, node->label);
28896 assemble_string (node->str, strlen (node->str) + 1);
28899 return 1;
28902 /* Output the indexed string table. */
28904 static void
28905 output_indirect_strings (void)
28907 switch_to_section (debug_str_section);
28908 if (!dwarf_split_debug_info)
28909 debug_str_hash->traverse<enum dwarf_form,
28910 output_indirect_string> (DW_FORM_strp);
28911 else
28913 unsigned int offset = 0;
28914 unsigned int cur_idx = 0;
28916 if (skeleton_debug_str_hash)
28917 skeleton_debug_str_hash->traverse<enum dwarf_form,
28918 output_indirect_string> (DW_FORM_strp);
28920 switch_to_section (debug_str_offsets_section);
28921 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
28922 header. Note that we don't need to generate a label to the
28923 actual index table following the header here, because this is
28924 for the split dwarf case only. In an .dwo file there is only
28925 one string offsets table (and one debug info section). But
28926 if we would start using string offset tables for the main (or
28927 skeleton) unit, then we have to add a DW_AT_str_offsets_base
28928 pointing to the actual index after the header. Split dwarf
28929 units will never have a string offsets base attribute. When
28930 a split unit is moved into a .dwp file the string offsets can
28931 be found through the .debug_cu_index section table. */
28932 if (dwarf_version >= 5)
28934 unsigned int last_idx = 0;
28935 unsigned long str_offsets_length;
28937 debug_str_hash->traverse_noresize
28938 <unsigned int *, count_index_strings> (&last_idx);
28939 str_offsets_length = last_idx * DWARF_OFFSET_SIZE + 4;
28940 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
28941 dw2_asm_output_data (4, 0xffffffff,
28942 "Escape value for 64-bit DWARF extension");
28943 dw2_asm_output_data (DWARF_OFFSET_SIZE, str_offsets_length,
28944 "Length of string offsets unit");
28945 dw2_asm_output_data (2, 5, "DWARF string offsets version");
28946 dw2_asm_output_data (2, 0, "Header zero padding");
28948 debug_str_hash->traverse_noresize
28949 <unsigned int *, output_index_string_offset> (&offset);
28950 switch_to_section (debug_str_dwo_section);
28951 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
28952 (&cur_idx);
28956 /* Callback for htab_traverse to assign an index to an entry in the
28957 table, and to write that entry to the .debug_addr section. */
28960 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
28962 addr_table_entry *entry = *slot;
28964 if (entry->refcount == 0)
28966 gcc_assert (entry->index == NO_INDEX_ASSIGNED
28967 || entry->index == NOT_INDEXED);
28968 return 1;
28971 gcc_assert (entry->index == *cur_index);
28972 (*cur_index)++;
28974 switch (entry->kind)
28976 case ate_kind_rtx:
28977 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
28978 "0x%x", entry->index);
28979 break;
28980 case ate_kind_rtx_dtprel:
28981 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
28982 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
28983 DWARF2_ADDR_SIZE,
28984 entry->addr.rtl);
28985 fputc ('\n', asm_out_file);
28986 break;
28987 case ate_kind_label:
28988 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
28989 "0x%x", entry->index);
28990 break;
28991 default:
28992 gcc_unreachable ();
28994 return 1;
28997 /* A helper function for dwarf2out_finish. Counts the number
28998 of indexed addresses. Must match the logic of the functions
28999 output_addr_table_entry above. */
29001 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29003 addr_table_entry *entry = *slot;
29005 if (entry->refcount > 0)
29006 *last_idx += 1;
29007 return 1;
29010 /* Produce the .debug_addr section. */
29012 static void
29013 output_addr_table (void)
29015 unsigned int index = 0;
29016 if (addr_index_table == NULL || addr_index_table->size () == 0)
29017 return;
29019 switch_to_section (debug_addr_section);
29020 addr_index_table
29021 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29024 #if ENABLE_ASSERT_CHECKING
29025 /* Verify that all marks are clear. */
29027 static void
29028 verify_marks_clear (dw_die_ref die)
29030 dw_die_ref c;
29032 gcc_assert (! die->die_mark);
29033 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29035 #endif /* ENABLE_ASSERT_CHECKING */
29037 /* Clear the marks for a die and its children.
29038 Be cool if the mark isn't set. */
29040 static void
29041 prune_unmark_dies (dw_die_ref die)
29043 dw_die_ref c;
29045 if (die->die_mark)
29046 die->die_mark = 0;
29047 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29050 /* Given LOC that is referenced by a DIE we're marking as used, find all
29051 referenced DWARF procedures it references and mark them as used. */
29053 static void
29054 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29056 for (; loc != NULL; loc = loc->dw_loc_next)
29057 switch (loc->dw_loc_opc)
29059 case DW_OP_implicit_pointer:
29060 case DW_OP_convert:
29061 case DW_OP_reinterpret:
29062 case DW_OP_GNU_implicit_pointer:
29063 case DW_OP_GNU_convert:
29064 case DW_OP_GNU_reinterpret:
29065 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29066 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29067 break;
29068 case DW_OP_GNU_variable_value:
29069 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29071 dw_die_ref ref
29072 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29073 if (ref == NULL)
29074 break;
29075 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29076 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29077 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29079 /* FALLTHRU */
29080 case DW_OP_call2:
29081 case DW_OP_call4:
29082 case DW_OP_call_ref:
29083 case DW_OP_const_type:
29084 case DW_OP_GNU_const_type:
29085 case DW_OP_GNU_parameter_ref:
29086 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29087 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29088 break;
29089 case DW_OP_regval_type:
29090 case DW_OP_deref_type:
29091 case DW_OP_GNU_regval_type:
29092 case DW_OP_GNU_deref_type:
29093 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29094 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29095 break;
29096 case DW_OP_entry_value:
29097 case DW_OP_GNU_entry_value:
29098 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29099 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29100 break;
29101 default:
29102 break;
29106 /* Given DIE that we're marking as used, find any other dies
29107 it references as attributes and mark them as used. */
29109 static void
29110 prune_unused_types_walk_attribs (dw_die_ref die)
29112 dw_attr_node *a;
29113 unsigned ix;
29115 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29117 switch (AT_class (a))
29119 /* Make sure DWARF procedures referenced by location descriptions will
29120 get emitted. */
29121 case dw_val_class_loc:
29122 prune_unused_types_walk_loc_descr (AT_loc (a));
29123 break;
29124 case dw_val_class_loc_list:
29125 for (dw_loc_list_ref list = AT_loc_list (a);
29126 list != NULL;
29127 list = list->dw_loc_next)
29128 prune_unused_types_walk_loc_descr (list->expr);
29129 break;
29131 case dw_val_class_view_list:
29132 /* This points to a loc_list in another attribute, so it's
29133 already covered. */
29134 break;
29136 case dw_val_class_die_ref:
29137 /* A reference to another DIE.
29138 Make sure that it will get emitted.
29139 If it was broken out into a comdat group, don't follow it. */
29140 if (! AT_ref (a)->comdat_type_p
29141 || a->dw_attr == DW_AT_specification)
29142 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29143 break;
29145 case dw_val_class_str:
29146 /* Set the string's refcount to 0 so that prune_unused_types_mark
29147 accounts properly for it. */
29148 a->dw_attr_val.v.val_str->refcount = 0;
29149 break;
29151 default:
29152 break;
29157 /* Mark the generic parameters and arguments children DIEs of DIE. */
29159 static void
29160 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29162 dw_die_ref c;
29164 if (die == NULL || die->die_child == NULL)
29165 return;
29166 c = die->die_child;
29169 if (is_template_parameter (c))
29170 prune_unused_types_mark (c, 1);
29171 c = c->die_sib;
29172 } while (c && c != die->die_child);
29175 /* Mark DIE as being used. If DOKIDS is true, then walk down
29176 to DIE's children. */
29178 static void
29179 prune_unused_types_mark (dw_die_ref die, int dokids)
29181 dw_die_ref c;
29183 if (die->die_mark == 0)
29185 /* We haven't done this node yet. Mark it as used. */
29186 die->die_mark = 1;
29187 /* If this is the DIE of a generic type instantiation,
29188 mark the children DIEs that describe its generic parms and
29189 args. */
29190 prune_unused_types_mark_generic_parms_dies (die);
29192 /* We also have to mark its parents as used.
29193 (But we don't want to mark our parent's kids due to this,
29194 unless it is a class.) */
29195 if (die->die_parent)
29196 prune_unused_types_mark (die->die_parent,
29197 class_scope_p (die->die_parent));
29199 /* Mark any referenced nodes. */
29200 prune_unused_types_walk_attribs (die);
29202 /* If this node is a specification,
29203 also mark the definition, if it exists. */
29204 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29205 prune_unused_types_mark (die->die_definition, 1);
29208 if (dokids && die->die_mark != 2)
29210 /* We need to walk the children, but haven't done so yet.
29211 Remember that we've walked the kids. */
29212 die->die_mark = 2;
29214 /* If this is an array type, we need to make sure our
29215 kids get marked, even if they're types. If we're
29216 breaking out types into comdat sections, do this
29217 for all type definitions. */
29218 if (die->die_tag == DW_TAG_array_type
29219 || (use_debug_types
29220 && is_type_die (die) && ! is_declaration_die (die)))
29221 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29222 else
29223 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29227 /* For local classes, look if any static member functions were emitted
29228 and if so, mark them. */
29230 static void
29231 prune_unused_types_walk_local_classes (dw_die_ref die)
29233 dw_die_ref c;
29235 if (die->die_mark == 2)
29236 return;
29238 switch (die->die_tag)
29240 case DW_TAG_structure_type:
29241 case DW_TAG_union_type:
29242 case DW_TAG_class_type:
29243 break;
29245 case DW_TAG_subprogram:
29246 if (!get_AT_flag (die, DW_AT_declaration)
29247 || die->die_definition != NULL)
29248 prune_unused_types_mark (die, 1);
29249 return;
29251 default:
29252 return;
29255 /* Mark children. */
29256 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29259 /* Walk the tree DIE and mark types that we actually use. */
29261 static void
29262 prune_unused_types_walk (dw_die_ref die)
29264 dw_die_ref c;
29266 /* Don't do anything if this node is already marked and
29267 children have been marked as well. */
29268 if (die->die_mark == 2)
29269 return;
29271 switch (die->die_tag)
29273 case DW_TAG_structure_type:
29274 case DW_TAG_union_type:
29275 case DW_TAG_class_type:
29276 if (die->die_perennial_p)
29277 break;
29279 for (c = die->die_parent; c; c = c->die_parent)
29280 if (c->die_tag == DW_TAG_subprogram)
29281 break;
29283 /* Finding used static member functions inside of classes
29284 is needed just for local classes, because for other classes
29285 static member function DIEs with DW_AT_specification
29286 are emitted outside of the DW_TAG_*_type. If we ever change
29287 it, we'd need to call this even for non-local classes. */
29288 if (c)
29289 prune_unused_types_walk_local_classes (die);
29291 /* It's a type node --- don't mark it. */
29292 return;
29294 case DW_TAG_const_type:
29295 case DW_TAG_packed_type:
29296 case DW_TAG_pointer_type:
29297 case DW_TAG_reference_type:
29298 case DW_TAG_rvalue_reference_type:
29299 case DW_TAG_volatile_type:
29300 case DW_TAG_typedef:
29301 case DW_TAG_array_type:
29302 case DW_TAG_interface_type:
29303 case DW_TAG_friend:
29304 case DW_TAG_enumeration_type:
29305 case DW_TAG_subroutine_type:
29306 case DW_TAG_string_type:
29307 case DW_TAG_set_type:
29308 case DW_TAG_subrange_type:
29309 case DW_TAG_ptr_to_member_type:
29310 case DW_TAG_file_type:
29311 /* Type nodes are useful only when other DIEs reference them --- don't
29312 mark them. */
29313 /* FALLTHROUGH */
29315 case DW_TAG_dwarf_procedure:
29316 /* Likewise for DWARF procedures. */
29318 if (die->die_perennial_p)
29319 break;
29321 return;
29323 default:
29324 /* Mark everything else. */
29325 break;
29328 if (die->die_mark == 0)
29330 die->die_mark = 1;
29332 /* Now, mark any dies referenced from here. */
29333 prune_unused_types_walk_attribs (die);
29336 die->die_mark = 2;
29338 /* Mark children. */
29339 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29342 /* Increment the string counts on strings referred to from DIE's
29343 attributes. */
29345 static void
29346 prune_unused_types_update_strings (dw_die_ref die)
29348 dw_attr_node *a;
29349 unsigned ix;
29351 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29352 if (AT_class (a) == dw_val_class_str)
29354 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
29355 s->refcount++;
29356 /* Avoid unnecessarily putting strings that are used less than
29357 twice in the hash table. */
29358 if (s->refcount
29359 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
29361 indirect_string_node **slot
29362 = debug_str_hash->find_slot_with_hash (s->str,
29363 htab_hash_string (s->str),
29364 INSERT);
29365 gcc_assert (*slot == NULL);
29366 *slot = s;
29371 /* Mark DIE and its children as removed. */
29373 static void
29374 mark_removed (dw_die_ref die)
29376 dw_die_ref c;
29377 die->removed = true;
29378 FOR_EACH_CHILD (die, c, mark_removed (c));
29381 /* Remove from the tree DIE any dies that aren't marked. */
29383 static void
29384 prune_unused_types_prune (dw_die_ref die)
29386 dw_die_ref c;
29388 gcc_assert (die->die_mark);
29389 prune_unused_types_update_strings (die);
29391 if (! die->die_child)
29392 return;
29394 c = die->die_child;
29395 do {
29396 dw_die_ref prev = c, next;
29397 for (c = c->die_sib; ! c->die_mark; c = next)
29398 if (c == die->die_child)
29400 /* No marked children between 'prev' and the end of the list. */
29401 if (prev == c)
29402 /* No marked children at all. */
29403 die->die_child = NULL;
29404 else
29406 prev->die_sib = c->die_sib;
29407 die->die_child = prev;
29409 c->die_sib = NULL;
29410 mark_removed (c);
29411 return;
29413 else
29415 next = c->die_sib;
29416 c->die_sib = NULL;
29417 mark_removed (c);
29420 if (c != prev->die_sib)
29421 prev->die_sib = c;
29422 prune_unused_types_prune (c);
29423 } while (c != die->die_child);
29426 /* Remove dies representing declarations that we never use. */
29428 static void
29429 prune_unused_types (void)
29431 unsigned int i;
29432 limbo_die_node *node;
29433 comdat_type_node *ctnode;
29434 pubname_entry *pub;
29435 dw_die_ref base_type;
29437 #if ENABLE_ASSERT_CHECKING
29438 /* All the marks should already be clear. */
29439 verify_marks_clear (comp_unit_die ());
29440 for (node = limbo_die_list; node; node = node->next)
29441 verify_marks_clear (node->die);
29442 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29443 verify_marks_clear (ctnode->root_die);
29444 #endif /* ENABLE_ASSERT_CHECKING */
29446 /* Mark types that are used in global variables. */
29447 premark_types_used_by_global_vars ();
29449 /* Set the mark on nodes that are actually used. */
29450 prune_unused_types_walk (comp_unit_die ());
29451 for (node = limbo_die_list; node; node = node->next)
29452 prune_unused_types_walk (node->die);
29453 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29455 prune_unused_types_walk (ctnode->root_die);
29456 prune_unused_types_mark (ctnode->type_die, 1);
29459 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
29460 are unusual in that they are pubnames that are the children of pubtypes.
29461 They should only be marked via their parent DW_TAG_enumeration_type die,
29462 not as roots in themselves. */
29463 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
29464 if (pub->die->die_tag != DW_TAG_enumerator)
29465 prune_unused_types_mark (pub->die, 1);
29466 for (i = 0; base_types.iterate (i, &base_type); i++)
29467 prune_unused_types_mark (base_type, 1);
29469 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
29470 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
29471 callees). */
29472 cgraph_node *cnode;
29473 FOR_EACH_FUNCTION (cnode)
29474 if (cnode->referred_to_p (false))
29476 dw_die_ref die = lookup_decl_die (cnode->decl);
29477 if (die == NULL || die->die_mark)
29478 continue;
29479 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
29480 if (e->caller != cnode
29481 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
29483 prune_unused_types_mark (die, 1);
29484 break;
29488 if (debug_str_hash)
29489 debug_str_hash->empty ();
29490 if (skeleton_debug_str_hash)
29491 skeleton_debug_str_hash->empty ();
29492 prune_unused_types_prune (comp_unit_die ());
29493 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
29495 node = *pnode;
29496 if (!node->die->die_mark)
29497 *pnode = node->next;
29498 else
29500 prune_unused_types_prune (node->die);
29501 pnode = &node->next;
29504 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29505 prune_unused_types_prune (ctnode->root_die);
29507 /* Leave the marks clear. */
29508 prune_unmark_dies (comp_unit_die ());
29509 for (node = limbo_die_list; node; node = node->next)
29510 prune_unmark_dies (node->die);
29511 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29512 prune_unmark_dies (ctnode->root_die);
29515 /* Helpers to manipulate hash table of comdat type units. */
29517 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
29519 static inline hashval_t hash (const comdat_type_node *);
29520 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
29523 inline hashval_t
29524 comdat_type_hasher::hash (const comdat_type_node *type_node)
29526 hashval_t h;
29527 memcpy (&h, type_node->signature, sizeof (h));
29528 return h;
29531 inline bool
29532 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
29533 const comdat_type_node *type_node_2)
29535 return (! memcmp (type_node_1->signature, type_node_2->signature,
29536 DWARF_TYPE_SIGNATURE_SIZE));
29539 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
29540 to the location it would have been added, should we know its
29541 DECL_ASSEMBLER_NAME when we added other attributes. This will
29542 probably improve compactness of debug info, removing equivalent
29543 abbrevs, and hide any differences caused by deferring the
29544 computation of the assembler name, triggered by e.g. PCH. */
29546 static inline void
29547 move_linkage_attr (dw_die_ref die)
29549 unsigned ix = vec_safe_length (die->die_attr);
29550 dw_attr_node linkage = (*die->die_attr)[ix - 1];
29552 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
29553 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
29555 while (--ix > 0)
29557 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
29559 if (prev->dw_attr == DW_AT_decl_line
29560 || prev->dw_attr == DW_AT_decl_column
29561 || prev->dw_attr == DW_AT_name)
29562 break;
29565 if (ix != vec_safe_length (die->die_attr) - 1)
29567 die->die_attr->pop ();
29568 die->die_attr->quick_insert (ix, linkage);
29572 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
29573 referenced from typed stack ops and count how often they are used. */
29575 static void
29576 mark_base_types (dw_loc_descr_ref loc)
29578 dw_die_ref base_type = NULL;
29580 for (; loc; loc = loc->dw_loc_next)
29582 switch (loc->dw_loc_opc)
29584 case DW_OP_regval_type:
29585 case DW_OP_deref_type:
29586 case DW_OP_GNU_regval_type:
29587 case DW_OP_GNU_deref_type:
29588 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
29589 break;
29590 case DW_OP_convert:
29591 case DW_OP_reinterpret:
29592 case DW_OP_GNU_convert:
29593 case DW_OP_GNU_reinterpret:
29594 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
29595 continue;
29596 /* FALLTHRU */
29597 case DW_OP_const_type:
29598 case DW_OP_GNU_const_type:
29599 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
29600 break;
29601 case DW_OP_entry_value:
29602 case DW_OP_GNU_entry_value:
29603 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
29604 continue;
29605 default:
29606 continue;
29608 gcc_assert (base_type->die_parent == comp_unit_die ());
29609 if (base_type->die_mark)
29610 base_type->die_mark++;
29611 else
29613 base_types.safe_push (base_type);
29614 base_type->die_mark = 1;
29619 /* Comparison function for sorting marked base types. */
29621 static int
29622 base_type_cmp (const void *x, const void *y)
29624 dw_die_ref dx = *(const dw_die_ref *) x;
29625 dw_die_ref dy = *(const dw_die_ref *) y;
29626 unsigned int byte_size1, byte_size2;
29627 unsigned int encoding1, encoding2;
29628 unsigned int align1, align2;
29629 if (dx->die_mark > dy->die_mark)
29630 return -1;
29631 if (dx->die_mark < dy->die_mark)
29632 return 1;
29633 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
29634 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
29635 if (byte_size1 < byte_size2)
29636 return 1;
29637 if (byte_size1 > byte_size2)
29638 return -1;
29639 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
29640 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
29641 if (encoding1 < encoding2)
29642 return 1;
29643 if (encoding1 > encoding2)
29644 return -1;
29645 align1 = get_AT_unsigned (dx, DW_AT_alignment);
29646 align2 = get_AT_unsigned (dy, DW_AT_alignment);
29647 if (align1 < align2)
29648 return 1;
29649 if (align1 > align2)
29650 return -1;
29651 return 0;
29654 /* Move base types marked by mark_base_types as early as possible
29655 in the CU, sorted by decreasing usage count both to make the
29656 uleb128 references as small as possible and to make sure they
29657 will have die_offset already computed by calc_die_sizes when
29658 sizes of typed stack loc ops is computed. */
29660 static void
29661 move_marked_base_types (void)
29663 unsigned int i;
29664 dw_die_ref base_type, die, c;
29666 if (base_types.is_empty ())
29667 return;
29669 /* Sort by decreasing usage count, they will be added again in that
29670 order later on. */
29671 base_types.qsort (base_type_cmp);
29672 die = comp_unit_die ();
29673 c = die->die_child;
29676 dw_die_ref prev = c;
29677 c = c->die_sib;
29678 while (c->die_mark)
29680 remove_child_with_prev (c, prev);
29681 /* As base types got marked, there must be at least
29682 one node other than DW_TAG_base_type. */
29683 gcc_assert (die->die_child != NULL);
29684 c = prev->die_sib;
29687 while (c != die->die_child);
29688 gcc_assert (die->die_child);
29689 c = die->die_child;
29690 for (i = 0; base_types.iterate (i, &base_type); i++)
29692 base_type->die_mark = 0;
29693 base_type->die_sib = c->die_sib;
29694 c->die_sib = base_type;
29695 c = base_type;
29699 /* Helper function for resolve_addr, attempt to resolve
29700 one CONST_STRING, return true if successful. Similarly verify that
29701 SYMBOL_REFs refer to variables emitted in the current CU. */
29703 static bool
29704 resolve_one_addr (rtx *addr)
29706 rtx rtl = *addr;
29708 if (GET_CODE (rtl) == CONST_STRING)
29710 size_t len = strlen (XSTR (rtl, 0)) + 1;
29711 tree t = build_string (len, XSTR (rtl, 0));
29712 tree tlen = size_int (len - 1);
29713 TREE_TYPE (t)
29714 = build_array_type (char_type_node, build_index_type (tlen));
29715 rtl = lookup_constant_def (t);
29716 if (!rtl || !MEM_P (rtl))
29717 return false;
29718 rtl = XEXP (rtl, 0);
29719 if (GET_CODE (rtl) == SYMBOL_REF
29720 && SYMBOL_REF_DECL (rtl)
29721 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29722 return false;
29723 vec_safe_push (used_rtx_array, rtl);
29724 *addr = rtl;
29725 return true;
29728 if (GET_CODE (rtl) == SYMBOL_REF
29729 && SYMBOL_REF_DECL (rtl))
29731 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
29733 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
29734 return false;
29736 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29737 return false;
29740 if (GET_CODE (rtl) == CONST)
29742 subrtx_ptr_iterator::array_type array;
29743 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
29744 if (!resolve_one_addr (*iter))
29745 return false;
29748 return true;
29751 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
29752 if possible, and create DW_TAG_dwarf_procedure that can be referenced
29753 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
29755 static rtx
29756 string_cst_pool_decl (tree t)
29758 rtx rtl = output_constant_def (t, 1);
29759 unsigned char *array;
29760 dw_loc_descr_ref l;
29761 tree decl;
29762 size_t len;
29763 dw_die_ref ref;
29765 if (!rtl || !MEM_P (rtl))
29766 return NULL_RTX;
29767 rtl = XEXP (rtl, 0);
29768 if (GET_CODE (rtl) != SYMBOL_REF
29769 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
29770 return NULL_RTX;
29772 decl = SYMBOL_REF_DECL (rtl);
29773 if (!lookup_decl_die (decl))
29775 len = TREE_STRING_LENGTH (t);
29776 vec_safe_push (used_rtx_array, rtl);
29777 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
29778 array = ggc_vec_alloc<unsigned char> (len);
29779 memcpy (array, TREE_STRING_POINTER (t), len);
29780 l = new_loc_descr (DW_OP_implicit_value, len, 0);
29781 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
29782 l->dw_loc_oprnd2.v.val_vec.length = len;
29783 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
29784 l->dw_loc_oprnd2.v.val_vec.array = array;
29785 add_AT_loc (ref, DW_AT_location, l);
29786 equate_decl_number_to_die (decl, ref);
29788 return rtl;
29791 /* Helper function of resolve_addr_in_expr. LOC is
29792 a DW_OP_addr followed by DW_OP_stack_value, either at the start
29793 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
29794 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
29795 with DW_OP_implicit_pointer if possible
29796 and return true, if unsuccessful, return false. */
29798 static bool
29799 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
29801 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
29802 HOST_WIDE_INT offset = 0;
29803 dw_die_ref ref = NULL;
29804 tree decl;
29806 if (GET_CODE (rtl) == CONST
29807 && GET_CODE (XEXP (rtl, 0)) == PLUS
29808 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
29810 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
29811 rtl = XEXP (XEXP (rtl, 0), 0);
29813 if (GET_CODE (rtl) == CONST_STRING)
29815 size_t len = strlen (XSTR (rtl, 0)) + 1;
29816 tree t = build_string (len, XSTR (rtl, 0));
29817 tree tlen = size_int (len - 1);
29819 TREE_TYPE (t)
29820 = build_array_type (char_type_node, build_index_type (tlen));
29821 rtl = string_cst_pool_decl (t);
29822 if (!rtl)
29823 return false;
29825 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
29827 decl = SYMBOL_REF_DECL (rtl);
29828 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
29830 ref = lookup_decl_die (decl);
29831 if (ref && (get_AT (ref, DW_AT_location)
29832 || get_AT (ref, DW_AT_const_value)))
29834 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
29835 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29836 loc->dw_loc_oprnd1.val_entry = NULL;
29837 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29838 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29839 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29840 loc->dw_loc_oprnd2.v.val_int = offset;
29841 return true;
29845 return false;
29848 /* Helper function for resolve_addr, handle one location
29849 expression, return false if at least one CONST_STRING or SYMBOL_REF in
29850 the location list couldn't be resolved. */
29852 static bool
29853 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
29855 dw_loc_descr_ref keep = NULL;
29856 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
29857 switch (loc->dw_loc_opc)
29859 case DW_OP_addr:
29860 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29862 if ((prev == NULL
29863 || prev->dw_loc_opc == DW_OP_piece
29864 || prev->dw_loc_opc == DW_OP_bit_piece)
29865 && loc->dw_loc_next
29866 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
29867 && (!dwarf_strict || dwarf_version >= 5)
29868 && optimize_one_addr_into_implicit_ptr (loc))
29869 break;
29870 return false;
29872 break;
29873 case DW_OP_GNU_addr_index:
29874 case DW_OP_addrx:
29875 case DW_OP_GNU_const_index:
29876 case DW_OP_constx:
29877 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
29878 || loc->dw_loc_opc == DW_OP_addrx)
29879 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
29880 || loc->dw_loc_opc == DW_OP_constx)
29881 && loc->dtprel))
29883 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
29884 if (!resolve_one_addr (&rtl))
29885 return false;
29886 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
29887 loc->dw_loc_oprnd1.val_entry
29888 = add_addr_table_entry (rtl, ate_kind_rtx);
29890 break;
29891 case DW_OP_const4u:
29892 case DW_OP_const8u:
29893 if (loc->dtprel
29894 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29895 return false;
29896 break;
29897 case DW_OP_plus_uconst:
29898 if (size_of_loc_descr (loc)
29899 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
29901 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
29903 dw_loc_descr_ref repl
29904 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
29905 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
29906 add_loc_descr (&repl, loc->dw_loc_next);
29907 *loc = *repl;
29909 break;
29910 case DW_OP_implicit_value:
29911 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
29912 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
29913 return false;
29914 break;
29915 case DW_OP_implicit_pointer:
29916 case DW_OP_GNU_implicit_pointer:
29917 case DW_OP_GNU_parameter_ref:
29918 case DW_OP_GNU_variable_value:
29919 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29921 dw_die_ref ref
29922 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29923 if (ref == NULL)
29924 return false;
29925 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29926 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29927 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29929 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
29931 if (prev == NULL
29932 && loc->dw_loc_next == NULL
29933 && AT_class (a) == dw_val_class_loc)
29934 switch (a->dw_attr)
29936 /* Following attributes allow both exprloc and reference,
29937 so if the whole expression is DW_OP_GNU_variable_value
29938 alone we could transform it into reference. */
29939 case DW_AT_byte_size:
29940 case DW_AT_bit_size:
29941 case DW_AT_lower_bound:
29942 case DW_AT_upper_bound:
29943 case DW_AT_bit_stride:
29944 case DW_AT_count:
29945 case DW_AT_allocated:
29946 case DW_AT_associated:
29947 case DW_AT_byte_stride:
29948 a->dw_attr_val.val_class = dw_val_class_die_ref;
29949 a->dw_attr_val.val_entry = NULL;
29950 a->dw_attr_val.v.val_die_ref.die
29951 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29952 a->dw_attr_val.v.val_die_ref.external = 0;
29953 return true;
29954 default:
29955 break;
29957 if (dwarf_strict)
29958 return false;
29960 break;
29961 case DW_OP_const_type:
29962 case DW_OP_regval_type:
29963 case DW_OP_deref_type:
29964 case DW_OP_convert:
29965 case DW_OP_reinterpret:
29966 case DW_OP_GNU_const_type:
29967 case DW_OP_GNU_regval_type:
29968 case DW_OP_GNU_deref_type:
29969 case DW_OP_GNU_convert:
29970 case DW_OP_GNU_reinterpret:
29971 while (loc->dw_loc_next
29972 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
29973 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
29975 dw_die_ref base1, base2;
29976 unsigned enc1, enc2, size1, size2;
29977 if (loc->dw_loc_opc == DW_OP_regval_type
29978 || loc->dw_loc_opc == DW_OP_deref_type
29979 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29980 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29981 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
29982 else if (loc->dw_loc_oprnd1.val_class
29983 == dw_val_class_unsigned_const)
29984 break;
29985 else
29986 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29987 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
29988 == dw_val_class_unsigned_const)
29989 break;
29990 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
29991 gcc_assert (base1->die_tag == DW_TAG_base_type
29992 && base2->die_tag == DW_TAG_base_type);
29993 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
29994 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
29995 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
29996 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
29997 if (size1 == size2
29998 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
29999 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30000 && loc != keep)
30001 || enc1 == enc2))
30003 /* Optimize away next DW_OP_convert after
30004 adjusting LOC's base type die reference. */
30005 if (loc->dw_loc_opc == DW_OP_regval_type
30006 || loc->dw_loc_opc == DW_OP_deref_type
30007 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30008 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30009 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30010 else
30011 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30012 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30013 continue;
30015 /* Don't change integer DW_OP_convert after e.g. floating
30016 point typed stack entry. */
30017 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30018 keep = loc->dw_loc_next;
30019 break;
30021 break;
30022 default:
30023 break;
30025 return true;
30028 /* Helper function of resolve_addr. DIE had DW_AT_location of
30029 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30030 and DW_OP_addr couldn't be resolved. resolve_addr has already
30031 removed the DW_AT_location attribute. This function attempts to
30032 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30033 to it or DW_AT_const_value attribute, if possible. */
30035 static void
30036 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30038 if (!VAR_P (decl)
30039 || lookup_decl_die (decl) != die
30040 || DECL_EXTERNAL (decl)
30041 || !TREE_STATIC (decl)
30042 || DECL_INITIAL (decl) == NULL_TREE
30043 || DECL_P (DECL_INITIAL (decl))
30044 || get_AT (die, DW_AT_const_value))
30045 return;
30047 tree init = DECL_INITIAL (decl);
30048 HOST_WIDE_INT offset = 0;
30049 /* For variables that have been optimized away and thus
30050 don't have a memory location, see if we can emit
30051 DW_AT_const_value instead. */
30052 if (tree_add_const_value_attribute (die, init))
30053 return;
30054 if (dwarf_strict && dwarf_version < 5)
30055 return;
30056 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30057 and ADDR_EXPR refers to a decl that has DW_AT_location or
30058 DW_AT_const_value (but isn't addressable, otherwise
30059 resolving the original DW_OP_addr wouldn't fail), see if
30060 we can add DW_OP_implicit_pointer. */
30061 STRIP_NOPS (init);
30062 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30063 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30065 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30066 init = TREE_OPERAND (init, 0);
30067 STRIP_NOPS (init);
30069 if (TREE_CODE (init) != ADDR_EXPR)
30070 return;
30071 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30072 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30073 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30074 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30075 && TREE_OPERAND (init, 0) != decl))
30077 dw_die_ref ref;
30078 dw_loc_descr_ref l;
30080 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30082 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30083 if (!rtl)
30084 return;
30085 decl = SYMBOL_REF_DECL (rtl);
30087 else
30088 decl = TREE_OPERAND (init, 0);
30089 ref = lookup_decl_die (decl);
30090 if (ref == NULL
30091 || (!get_AT (ref, DW_AT_location)
30092 && !get_AT (ref, DW_AT_const_value)))
30093 return;
30094 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30095 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30096 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30097 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30098 add_AT_loc (die, DW_AT_location, l);
30102 /* Return NULL if l is a DWARF expression, or first op that is not
30103 valid DWARF expression. */
30105 static dw_loc_descr_ref
30106 non_dwarf_expression (dw_loc_descr_ref l)
30108 while (l)
30110 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30111 return l;
30112 switch (l->dw_loc_opc)
30114 case DW_OP_regx:
30115 case DW_OP_implicit_value:
30116 case DW_OP_stack_value:
30117 case DW_OP_implicit_pointer:
30118 case DW_OP_GNU_implicit_pointer:
30119 case DW_OP_GNU_parameter_ref:
30120 case DW_OP_piece:
30121 case DW_OP_bit_piece:
30122 return l;
30123 default:
30124 break;
30126 l = l->dw_loc_next;
30128 return NULL;
30131 /* Return adjusted copy of EXPR:
30132 If it is empty DWARF expression, return it.
30133 If it is valid non-empty DWARF expression,
30134 return copy of EXPR with DW_OP_deref appended to it.
30135 If it is DWARF expression followed by DW_OP_reg{N,x}, return
30136 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30137 If it is DWARF expression followed by DW_OP_stack_value, return
30138 copy of the DWARF expression without anything appended.
30139 Otherwise, return NULL. */
30141 static dw_loc_descr_ref
30142 copy_deref_exprloc (dw_loc_descr_ref expr)
30144 dw_loc_descr_ref tail = NULL;
30146 if (expr == NULL)
30147 return NULL;
30149 dw_loc_descr_ref l = non_dwarf_expression (expr);
30150 if (l && l->dw_loc_next)
30151 return NULL;
30153 if (l)
30155 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30156 tail = new_loc_descr ((enum dwarf_location_atom)
30157 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30158 0, 0);
30159 else
30160 switch (l->dw_loc_opc)
30162 case DW_OP_regx:
30163 tail = new_loc_descr (DW_OP_bregx,
30164 l->dw_loc_oprnd1.v.val_unsigned, 0);
30165 break;
30166 case DW_OP_stack_value:
30167 break;
30168 default:
30169 return NULL;
30172 else
30173 tail = new_loc_descr (DW_OP_deref, 0, 0);
30175 dw_loc_descr_ref ret = NULL, *p = &ret;
30176 while (expr != l)
30178 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30179 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30180 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30181 p = &(*p)->dw_loc_next;
30182 expr = expr->dw_loc_next;
30184 *p = tail;
30185 return ret;
30188 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30189 reference to a variable or argument, adjust it if needed and return:
30190 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30191 attribute if present should be removed
30192 0 keep the attribute perhaps with minor modifications, no need to rescan
30193 1 if the attribute has been successfully adjusted. */
30195 static int
30196 optimize_string_length (dw_attr_node *a)
30198 dw_loc_descr_ref l = AT_loc (a), lv;
30199 dw_die_ref die;
30200 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30202 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30203 die = lookup_decl_die (decl);
30204 if (die)
30206 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30207 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30208 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30210 else
30211 return -1;
30213 else
30214 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30216 /* DWARF5 allows reference class, so we can then reference the DIE.
30217 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30218 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30220 a->dw_attr_val.val_class = dw_val_class_die_ref;
30221 a->dw_attr_val.val_entry = NULL;
30222 a->dw_attr_val.v.val_die_ref.die = die;
30223 a->dw_attr_val.v.val_die_ref.external = 0;
30224 return 0;
30227 dw_attr_node *av = get_AT (die, DW_AT_location);
30228 dw_loc_list_ref d;
30229 bool non_dwarf_expr = false;
30231 if (av == NULL)
30232 return dwarf_strict ? -1 : 0;
30233 switch (AT_class (av))
30235 case dw_val_class_loc_list:
30236 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30237 if (d->expr && non_dwarf_expression (d->expr))
30238 non_dwarf_expr = true;
30239 break;
30240 case dw_val_class_view_list:
30241 gcc_unreachable ();
30242 case dw_val_class_loc:
30243 lv = AT_loc (av);
30244 if (lv == NULL)
30245 return dwarf_strict ? -1 : 0;
30246 if (non_dwarf_expression (lv))
30247 non_dwarf_expr = true;
30248 break;
30249 default:
30250 return dwarf_strict ? -1 : 0;
30253 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30254 into DW_OP_call4 or DW_OP_GNU_variable_value into
30255 DW_OP_call4 DW_OP_deref, do so. */
30256 if (!non_dwarf_expr
30257 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30259 l->dw_loc_opc = DW_OP_call4;
30260 if (l->dw_loc_next)
30261 l->dw_loc_next = NULL;
30262 else
30263 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30264 return 0;
30267 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30268 copy over the DW_AT_location attribute from die to a. */
30269 if (l->dw_loc_next != NULL)
30271 a->dw_attr_val = av->dw_attr_val;
30272 return 1;
30275 dw_loc_list_ref list, *p;
30276 switch (AT_class (av))
30278 case dw_val_class_loc_list:
30279 p = &list;
30280 list = NULL;
30281 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30283 lv = copy_deref_exprloc (d->expr);
30284 if (lv)
30286 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
30287 p = &(*p)->dw_loc_next;
30289 else if (!dwarf_strict && d->expr)
30290 return 0;
30292 if (list == NULL)
30293 return dwarf_strict ? -1 : 0;
30294 a->dw_attr_val.val_class = dw_val_class_loc_list;
30295 gen_llsym (list);
30296 *AT_loc_list_ptr (a) = list;
30297 return 1;
30298 case dw_val_class_loc:
30299 lv = copy_deref_exprloc (AT_loc (av));
30300 if (lv == NULL)
30301 return dwarf_strict ? -1 : 0;
30302 a->dw_attr_val.v.val_loc = lv;
30303 return 1;
30304 default:
30305 gcc_unreachable ();
30309 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
30310 an address in .rodata section if the string literal is emitted there,
30311 or remove the containing location list or replace DW_AT_const_value
30312 with DW_AT_location and empty location expression, if it isn't found
30313 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
30314 to something that has been emitted in the current CU. */
30316 static void
30317 resolve_addr (dw_die_ref die)
30319 dw_die_ref c;
30320 dw_attr_node *a;
30321 dw_loc_list_ref *curr, *start, loc;
30322 unsigned ix;
30323 bool remove_AT_byte_size = false;
30325 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30326 switch (AT_class (a))
30328 case dw_val_class_loc_list:
30329 start = curr = AT_loc_list_ptr (a);
30330 loc = *curr;
30331 gcc_assert (loc);
30332 /* The same list can be referenced more than once. See if we have
30333 already recorded the result from a previous pass. */
30334 if (loc->replaced)
30335 *curr = loc->dw_loc_next;
30336 else if (!loc->resolved_addr)
30338 /* As things stand, we do not expect or allow one die to
30339 reference a suffix of another die's location list chain.
30340 References must be identical or completely separate.
30341 There is therefore no need to cache the result of this
30342 pass on any list other than the first; doing so
30343 would lead to unnecessary writes. */
30344 while (*curr)
30346 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
30347 if (!resolve_addr_in_expr (a, (*curr)->expr))
30349 dw_loc_list_ref next = (*curr)->dw_loc_next;
30350 dw_loc_descr_ref l = (*curr)->expr;
30352 if (next && (*curr)->ll_symbol)
30354 gcc_assert (!next->ll_symbol);
30355 next->ll_symbol = (*curr)->ll_symbol;
30356 next->vl_symbol = (*curr)->vl_symbol;
30358 if (dwarf_split_debug_info)
30359 remove_loc_list_addr_table_entries (l);
30360 *curr = next;
30362 else
30364 mark_base_types ((*curr)->expr);
30365 curr = &(*curr)->dw_loc_next;
30368 if (loc == *start)
30369 loc->resolved_addr = 1;
30370 else
30372 loc->replaced = 1;
30373 loc->dw_loc_next = *start;
30376 if (!*start)
30378 remove_AT (die, a->dw_attr);
30379 ix--;
30381 break;
30382 case dw_val_class_view_list:
30384 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30385 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
30386 dw_val_node *llnode
30387 = view_list_to_loc_list_val_node (&a->dw_attr_val);
30388 /* If we no longer have a loclist, or it no longer needs
30389 views, drop this attribute. */
30390 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
30392 remove_AT (die, a->dw_attr);
30393 ix--;
30395 break;
30397 case dw_val_class_loc:
30399 dw_loc_descr_ref l = AT_loc (a);
30400 /* DW_OP_GNU_variable_value DW_OP_stack_value or
30401 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
30402 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
30403 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
30404 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
30405 with DW_FORM_ref referencing the same DIE as
30406 DW_OP_GNU_variable_value used to reference. */
30407 if (a->dw_attr == DW_AT_string_length
30408 && l
30409 && l->dw_loc_opc == DW_OP_GNU_variable_value
30410 && (l->dw_loc_next == NULL
30411 || (l->dw_loc_next->dw_loc_next == NULL
30412 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
30414 switch (optimize_string_length (a))
30416 case -1:
30417 remove_AT (die, a->dw_attr);
30418 ix--;
30419 /* If we drop DW_AT_string_length, we need to drop also
30420 DW_AT_{string_length_,}byte_size. */
30421 remove_AT_byte_size = true;
30422 continue;
30423 default:
30424 break;
30425 case 1:
30426 /* Even if we keep the optimized DW_AT_string_length,
30427 it might have changed AT_class, so process it again. */
30428 ix--;
30429 continue;
30432 /* For -gdwarf-2 don't attempt to optimize
30433 DW_AT_data_member_location containing
30434 DW_OP_plus_uconst - older consumers might
30435 rely on it being that op instead of a more complex,
30436 but shorter, location description. */
30437 if ((dwarf_version > 2
30438 || a->dw_attr != DW_AT_data_member_location
30439 || l == NULL
30440 || l->dw_loc_opc != DW_OP_plus_uconst
30441 || l->dw_loc_next != NULL)
30442 && !resolve_addr_in_expr (a, l))
30444 if (dwarf_split_debug_info)
30445 remove_loc_list_addr_table_entries (l);
30446 if (l != NULL
30447 && l->dw_loc_next == NULL
30448 && l->dw_loc_opc == DW_OP_addr
30449 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
30450 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
30451 && a->dw_attr == DW_AT_location)
30453 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
30454 remove_AT (die, a->dw_attr);
30455 ix--;
30456 optimize_location_into_implicit_ptr (die, decl);
30457 break;
30459 if (a->dw_attr == DW_AT_string_length)
30460 /* If we drop DW_AT_string_length, we need to drop also
30461 DW_AT_{string_length_,}byte_size. */
30462 remove_AT_byte_size = true;
30463 remove_AT (die, a->dw_attr);
30464 ix--;
30466 else
30467 mark_base_types (l);
30469 break;
30470 case dw_val_class_addr:
30471 if (a->dw_attr == DW_AT_const_value
30472 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
30474 if (AT_index (a) != NOT_INDEXED)
30475 remove_addr_table_entry (a->dw_attr_val.val_entry);
30476 remove_AT (die, a->dw_attr);
30477 ix--;
30479 if ((die->die_tag == DW_TAG_call_site
30480 && a->dw_attr == DW_AT_call_origin)
30481 || (die->die_tag == DW_TAG_GNU_call_site
30482 && a->dw_attr == DW_AT_abstract_origin))
30484 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
30485 dw_die_ref tdie = lookup_decl_die (tdecl);
30486 dw_die_ref cdie;
30487 if (tdie == NULL
30488 && DECL_EXTERNAL (tdecl)
30489 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
30490 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
30492 dw_die_ref pdie = cdie;
30493 /* Make sure we don't add these DIEs into type units.
30494 We could emit skeleton DIEs for context (namespaces,
30495 outer structs/classes) and a skeleton DIE for the
30496 innermost context with DW_AT_signature pointing to the
30497 type unit. See PR78835. */
30498 while (pdie && pdie->die_tag != DW_TAG_type_unit)
30499 pdie = pdie->die_parent;
30500 if (pdie == NULL)
30502 /* Creating a full DIE for tdecl is overly expensive and
30503 at this point even wrong when in the LTO phase
30504 as it can end up generating new type DIEs we didn't
30505 output and thus optimize_external_refs will crash. */
30506 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
30507 add_AT_flag (tdie, DW_AT_external, 1);
30508 add_AT_flag (tdie, DW_AT_declaration, 1);
30509 add_linkage_attr (tdie, tdecl);
30510 add_name_and_src_coords_attributes (tdie, tdecl, true);
30511 equate_decl_number_to_die (tdecl, tdie);
30514 if (tdie)
30516 a->dw_attr_val.val_class = dw_val_class_die_ref;
30517 a->dw_attr_val.v.val_die_ref.die = tdie;
30518 a->dw_attr_val.v.val_die_ref.external = 0;
30520 else
30522 if (AT_index (a) != NOT_INDEXED)
30523 remove_addr_table_entry (a->dw_attr_val.val_entry);
30524 remove_AT (die, a->dw_attr);
30525 ix--;
30528 break;
30529 default:
30530 break;
30533 if (remove_AT_byte_size)
30534 remove_AT (die, dwarf_version >= 5
30535 ? DW_AT_string_length_byte_size
30536 : DW_AT_byte_size);
30538 FOR_EACH_CHILD (die, c, resolve_addr (c));
30541 /* Helper routines for optimize_location_lists.
30542 This pass tries to share identical local lists in .debug_loc
30543 section. */
30545 /* Iteratively hash operands of LOC opcode into HSTATE. */
30547 static void
30548 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
30550 dw_val_ref val1 = &loc->dw_loc_oprnd1;
30551 dw_val_ref val2 = &loc->dw_loc_oprnd2;
30553 switch (loc->dw_loc_opc)
30555 case DW_OP_const4u:
30556 case DW_OP_const8u:
30557 if (loc->dtprel)
30558 goto hash_addr;
30559 /* FALLTHRU */
30560 case DW_OP_const1u:
30561 case DW_OP_const1s:
30562 case DW_OP_const2u:
30563 case DW_OP_const2s:
30564 case DW_OP_const4s:
30565 case DW_OP_const8s:
30566 case DW_OP_constu:
30567 case DW_OP_consts:
30568 case DW_OP_pick:
30569 case DW_OP_plus_uconst:
30570 case DW_OP_breg0:
30571 case DW_OP_breg1:
30572 case DW_OP_breg2:
30573 case DW_OP_breg3:
30574 case DW_OP_breg4:
30575 case DW_OP_breg5:
30576 case DW_OP_breg6:
30577 case DW_OP_breg7:
30578 case DW_OP_breg8:
30579 case DW_OP_breg9:
30580 case DW_OP_breg10:
30581 case DW_OP_breg11:
30582 case DW_OP_breg12:
30583 case DW_OP_breg13:
30584 case DW_OP_breg14:
30585 case DW_OP_breg15:
30586 case DW_OP_breg16:
30587 case DW_OP_breg17:
30588 case DW_OP_breg18:
30589 case DW_OP_breg19:
30590 case DW_OP_breg20:
30591 case DW_OP_breg21:
30592 case DW_OP_breg22:
30593 case DW_OP_breg23:
30594 case DW_OP_breg24:
30595 case DW_OP_breg25:
30596 case DW_OP_breg26:
30597 case DW_OP_breg27:
30598 case DW_OP_breg28:
30599 case DW_OP_breg29:
30600 case DW_OP_breg30:
30601 case DW_OP_breg31:
30602 case DW_OP_regx:
30603 case DW_OP_fbreg:
30604 case DW_OP_piece:
30605 case DW_OP_deref_size:
30606 case DW_OP_xderef_size:
30607 hstate.add_object (val1->v.val_int);
30608 break;
30609 case DW_OP_skip:
30610 case DW_OP_bra:
30612 int offset;
30614 gcc_assert (val1->val_class == dw_val_class_loc);
30615 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
30616 hstate.add_object (offset);
30618 break;
30619 case DW_OP_implicit_value:
30620 hstate.add_object (val1->v.val_unsigned);
30621 switch (val2->val_class)
30623 case dw_val_class_const:
30624 hstate.add_object (val2->v.val_int);
30625 break;
30626 case dw_val_class_vec:
30628 unsigned int elt_size = val2->v.val_vec.elt_size;
30629 unsigned int len = val2->v.val_vec.length;
30631 hstate.add_int (elt_size);
30632 hstate.add_int (len);
30633 hstate.add (val2->v.val_vec.array, len * elt_size);
30635 break;
30636 case dw_val_class_const_double:
30637 hstate.add_object (val2->v.val_double.low);
30638 hstate.add_object (val2->v.val_double.high);
30639 break;
30640 case dw_val_class_wide_int:
30641 hstate.add (val2->v.val_wide->get_val (),
30642 get_full_len (*val2->v.val_wide)
30643 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30644 break;
30645 case dw_val_class_addr:
30646 inchash::add_rtx (val2->v.val_addr, hstate);
30647 break;
30648 default:
30649 gcc_unreachable ();
30651 break;
30652 case DW_OP_bregx:
30653 case DW_OP_bit_piece:
30654 hstate.add_object (val1->v.val_int);
30655 hstate.add_object (val2->v.val_int);
30656 break;
30657 case DW_OP_addr:
30658 hash_addr:
30659 if (loc->dtprel)
30661 unsigned char dtprel = 0xd1;
30662 hstate.add_object (dtprel);
30664 inchash::add_rtx (val1->v.val_addr, hstate);
30665 break;
30666 case DW_OP_GNU_addr_index:
30667 case DW_OP_addrx:
30668 case DW_OP_GNU_const_index:
30669 case DW_OP_constx:
30671 if (loc->dtprel)
30673 unsigned char dtprel = 0xd1;
30674 hstate.add_object (dtprel);
30676 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
30678 break;
30679 case DW_OP_implicit_pointer:
30680 case DW_OP_GNU_implicit_pointer:
30681 hstate.add_int (val2->v.val_int);
30682 break;
30683 case DW_OP_entry_value:
30684 case DW_OP_GNU_entry_value:
30685 hstate.add_object (val1->v.val_loc);
30686 break;
30687 case DW_OP_regval_type:
30688 case DW_OP_deref_type:
30689 case DW_OP_GNU_regval_type:
30690 case DW_OP_GNU_deref_type:
30692 unsigned int byte_size
30693 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
30694 unsigned int encoding
30695 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
30696 hstate.add_object (val1->v.val_int);
30697 hstate.add_object (byte_size);
30698 hstate.add_object (encoding);
30700 break;
30701 case DW_OP_convert:
30702 case DW_OP_reinterpret:
30703 case DW_OP_GNU_convert:
30704 case DW_OP_GNU_reinterpret:
30705 if (val1->val_class == dw_val_class_unsigned_const)
30707 hstate.add_object (val1->v.val_unsigned);
30708 break;
30710 /* FALLTHRU */
30711 case DW_OP_const_type:
30712 case DW_OP_GNU_const_type:
30714 unsigned int byte_size
30715 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
30716 unsigned int encoding
30717 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
30718 hstate.add_object (byte_size);
30719 hstate.add_object (encoding);
30720 if (loc->dw_loc_opc != DW_OP_const_type
30721 && loc->dw_loc_opc != DW_OP_GNU_const_type)
30722 break;
30723 hstate.add_object (val2->val_class);
30724 switch (val2->val_class)
30726 case dw_val_class_const:
30727 hstate.add_object (val2->v.val_int);
30728 break;
30729 case dw_val_class_vec:
30731 unsigned int elt_size = val2->v.val_vec.elt_size;
30732 unsigned int len = val2->v.val_vec.length;
30734 hstate.add_object (elt_size);
30735 hstate.add_object (len);
30736 hstate.add (val2->v.val_vec.array, len * elt_size);
30738 break;
30739 case dw_val_class_const_double:
30740 hstate.add_object (val2->v.val_double.low);
30741 hstate.add_object (val2->v.val_double.high);
30742 break;
30743 case dw_val_class_wide_int:
30744 hstate.add (val2->v.val_wide->get_val (),
30745 get_full_len (*val2->v.val_wide)
30746 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30747 break;
30748 default:
30749 gcc_unreachable ();
30752 break;
30754 default:
30755 /* Other codes have no operands. */
30756 break;
30760 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
30762 static inline void
30763 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
30765 dw_loc_descr_ref l;
30766 bool sizes_computed = false;
30767 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
30768 size_of_locs (loc);
30770 for (l = loc; l != NULL; l = l->dw_loc_next)
30772 enum dwarf_location_atom opc = l->dw_loc_opc;
30773 hstate.add_object (opc);
30774 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
30776 size_of_locs (loc);
30777 sizes_computed = true;
30779 hash_loc_operands (l, hstate);
30783 /* Compute hash of the whole location list LIST_HEAD. */
30785 static inline void
30786 hash_loc_list (dw_loc_list_ref list_head)
30788 dw_loc_list_ref curr = list_head;
30789 inchash::hash hstate;
30791 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
30793 hstate.add (curr->begin, strlen (curr->begin) + 1);
30794 hstate.add (curr->end, strlen (curr->end) + 1);
30795 hstate.add_object (curr->vbegin);
30796 hstate.add_object (curr->vend);
30797 if (curr->section)
30798 hstate.add (curr->section, strlen (curr->section) + 1);
30799 hash_locs (curr->expr, hstate);
30801 list_head->hash = hstate.end ();
30804 /* Return true if X and Y opcodes have the same operands. */
30806 static inline bool
30807 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
30809 dw_val_ref valx1 = &x->dw_loc_oprnd1;
30810 dw_val_ref valx2 = &x->dw_loc_oprnd2;
30811 dw_val_ref valy1 = &y->dw_loc_oprnd1;
30812 dw_val_ref valy2 = &y->dw_loc_oprnd2;
30814 switch (x->dw_loc_opc)
30816 case DW_OP_const4u:
30817 case DW_OP_const8u:
30818 if (x->dtprel)
30819 goto hash_addr;
30820 /* FALLTHRU */
30821 case DW_OP_const1u:
30822 case DW_OP_const1s:
30823 case DW_OP_const2u:
30824 case DW_OP_const2s:
30825 case DW_OP_const4s:
30826 case DW_OP_const8s:
30827 case DW_OP_constu:
30828 case DW_OP_consts:
30829 case DW_OP_pick:
30830 case DW_OP_plus_uconst:
30831 case DW_OP_breg0:
30832 case DW_OP_breg1:
30833 case DW_OP_breg2:
30834 case DW_OP_breg3:
30835 case DW_OP_breg4:
30836 case DW_OP_breg5:
30837 case DW_OP_breg6:
30838 case DW_OP_breg7:
30839 case DW_OP_breg8:
30840 case DW_OP_breg9:
30841 case DW_OP_breg10:
30842 case DW_OP_breg11:
30843 case DW_OP_breg12:
30844 case DW_OP_breg13:
30845 case DW_OP_breg14:
30846 case DW_OP_breg15:
30847 case DW_OP_breg16:
30848 case DW_OP_breg17:
30849 case DW_OP_breg18:
30850 case DW_OP_breg19:
30851 case DW_OP_breg20:
30852 case DW_OP_breg21:
30853 case DW_OP_breg22:
30854 case DW_OP_breg23:
30855 case DW_OP_breg24:
30856 case DW_OP_breg25:
30857 case DW_OP_breg26:
30858 case DW_OP_breg27:
30859 case DW_OP_breg28:
30860 case DW_OP_breg29:
30861 case DW_OP_breg30:
30862 case DW_OP_breg31:
30863 case DW_OP_regx:
30864 case DW_OP_fbreg:
30865 case DW_OP_piece:
30866 case DW_OP_deref_size:
30867 case DW_OP_xderef_size:
30868 return valx1->v.val_int == valy1->v.val_int;
30869 case DW_OP_skip:
30870 case DW_OP_bra:
30871 /* If splitting debug info, the use of DW_OP_GNU_addr_index
30872 can cause irrelevant differences in dw_loc_addr. */
30873 gcc_assert (valx1->val_class == dw_val_class_loc
30874 && valy1->val_class == dw_val_class_loc
30875 && (dwarf_split_debug_info
30876 || x->dw_loc_addr == y->dw_loc_addr));
30877 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
30878 case DW_OP_implicit_value:
30879 if (valx1->v.val_unsigned != valy1->v.val_unsigned
30880 || valx2->val_class != valy2->val_class)
30881 return false;
30882 switch (valx2->val_class)
30884 case dw_val_class_const:
30885 return valx2->v.val_int == valy2->v.val_int;
30886 case dw_val_class_vec:
30887 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30888 && valx2->v.val_vec.length == valy2->v.val_vec.length
30889 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30890 valx2->v.val_vec.elt_size
30891 * valx2->v.val_vec.length) == 0;
30892 case dw_val_class_const_double:
30893 return valx2->v.val_double.low == valy2->v.val_double.low
30894 && valx2->v.val_double.high == valy2->v.val_double.high;
30895 case dw_val_class_wide_int:
30896 return *valx2->v.val_wide == *valy2->v.val_wide;
30897 case dw_val_class_addr:
30898 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
30899 default:
30900 gcc_unreachable ();
30902 case DW_OP_bregx:
30903 case DW_OP_bit_piece:
30904 return valx1->v.val_int == valy1->v.val_int
30905 && valx2->v.val_int == valy2->v.val_int;
30906 case DW_OP_addr:
30907 hash_addr:
30908 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
30909 case DW_OP_GNU_addr_index:
30910 case DW_OP_addrx:
30911 case DW_OP_GNU_const_index:
30912 case DW_OP_constx:
30914 rtx ax1 = valx1->val_entry->addr.rtl;
30915 rtx ay1 = valy1->val_entry->addr.rtl;
30916 return rtx_equal_p (ax1, ay1);
30918 case DW_OP_implicit_pointer:
30919 case DW_OP_GNU_implicit_pointer:
30920 return valx1->val_class == dw_val_class_die_ref
30921 && valx1->val_class == valy1->val_class
30922 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
30923 && valx2->v.val_int == valy2->v.val_int;
30924 case DW_OP_entry_value:
30925 case DW_OP_GNU_entry_value:
30926 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
30927 case DW_OP_const_type:
30928 case DW_OP_GNU_const_type:
30929 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
30930 || valx2->val_class != valy2->val_class)
30931 return false;
30932 switch (valx2->val_class)
30934 case dw_val_class_const:
30935 return valx2->v.val_int == valy2->v.val_int;
30936 case dw_val_class_vec:
30937 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30938 && valx2->v.val_vec.length == valy2->v.val_vec.length
30939 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30940 valx2->v.val_vec.elt_size
30941 * valx2->v.val_vec.length) == 0;
30942 case dw_val_class_const_double:
30943 return valx2->v.val_double.low == valy2->v.val_double.low
30944 && valx2->v.val_double.high == valy2->v.val_double.high;
30945 case dw_val_class_wide_int:
30946 return *valx2->v.val_wide == *valy2->v.val_wide;
30947 default:
30948 gcc_unreachable ();
30950 case DW_OP_regval_type:
30951 case DW_OP_deref_type:
30952 case DW_OP_GNU_regval_type:
30953 case DW_OP_GNU_deref_type:
30954 return valx1->v.val_int == valy1->v.val_int
30955 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
30956 case DW_OP_convert:
30957 case DW_OP_reinterpret:
30958 case DW_OP_GNU_convert:
30959 case DW_OP_GNU_reinterpret:
30960 if (valx1->val_class != valy1->val_class)
30961 return false;
30962 if (valx1->val_class == dw_val_class_unsigned_const)
30963 return valx1->v.val_unsigned == valy1->v.val_unsigned;
30964 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30965 case DW_OP_GNU_parameter_ref:
30966 return valx1->val_class == dw_val_class_die_ref
30967 && valx1->val_class == valy1->val_class
30968 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30969 default:
30970 /* Other codes have no operands. */
30971 return true;
30975 /* Return true if DWARF location expressions X and Y are the same. */
30977 static inline bool
30978 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
30980 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
30981 if (x->dw_loc_opc != y->dw_loc_opc
30982 || x->dtprel != y->dtprel
30983 || !compare_loc_operands (x, y))
30984 break;
30985 return x == NULL && y == NULL;
30988 /* Hashtable helpers. */
30990 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
30992 static inline hashval_t hash (const dw_loc_list_struct *);
30993 static inline bool equal (const dw_loc_list_struct *,
30994 const dw_loc_list_struct *);
30997 /* Return precomputed hash of location list X. */
30999 inline hashval_t
31000 loc_list_hasher::hash (const dw_loc_list_struct *x)
31002 return x->hash;
31005 /* Return true if location lists A and B are the same. */
31007 inline bool
31008 loc_list_hasher::equal (const dw_loc_list_struct *a,
31009 const dw_loc_list_struct *b)
31011 if (a == b)
31012 return 1;
31013 if (a->hash != b->hash)
31014 return 0;
31015 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31016 if (strcmp (a->begin, b->begin) != 0
31017 || strcmp (a->end, b->end) != 0
31018 || (a->section == NULL) != (b->section == NULL)
31019 || (a->section && strcmp (a->section, b->section) != 0)
31020 || a->vbegin != b->vbegin || a->vend != b->vend
31021 || !compare_locs (a->expr, b->expr))
31022 break;
31023 return a == NULL && b == NULL;
31026 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31029 /* Recursively optimize location lists referenced from DIE
31030 children and share them whenever possible. */
31032 static void
31033 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31035 dw_die_ref c;
31036 dw_attr_node *a;
31037 unsigned ix;
31038 dw_loc_list_struct **slot;
31039 bool drop_locviews = false;
31040 bool has_locviews = false;
31042 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31043 if (AT_class (a) == dw_val_class_loc_list)
31045 dw_loc_list_ref list = AT_loc_list (a);
31046 /* TODO: perform some optimizations here, before hashing
31047 it and storing into the hash table. */
31048 hash_loc_list (list);
31049 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31050 if (*slot == NULL)
31052 *slot = list;
31053 if (loc_list_has_views (list))
31054 gcc_assert (list->vl_symbol);
31055 else if (list->vl_symbol)
31057 drop_locviews = true;
31058 list->vl_symbol = NULL;
31061 else
31063 if (list->vl_symbol && !(*slot)->vl_symbol)
31064 drop_locviews = true;
31065 a->dw_attr_val.v.val_loc_list = *slot;
31068 else if (AT_class (a) == dw_val_class_view_list)
31070 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31071 has_locviews = true;
31075 if (drop_locviews && has_locviews)
31076 remove_AT (die, DW_AT_GNU_locviews);
31078 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31082 /* Recursively assign each location list a unique index into the debug_addr
31083 section. */
31085 static void
31086 index_location_lists (dw_die_ref die)
31088 dw_die_ref c;
31089 dw_attr_node *a;
31090 unsigned ix;
31092 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31093 if (AT_class (a) == dw_val_class_loc_list)
31095 dw_loc_list_ref list = AT_loc_list (a);
31096 dw_loc_list_ref curr;
31097 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31099 /* Don't index an entry that has already been indexed
31100 or won't be output. Make sure skip_loc_list_entry doesn't
31101 call size_of_locs, because that might cause circular dependency,
31102 index_location_lists requiring address table indexes to be
31103 computed, but adding new indexes through add_addr_table_entry
31104 and address table index computation requiring no new additions
31105 to the hash table. In the rare case of DWARF[234] >= 64KB
31106 location expression, we'll just waste unused address table entry
31107 for it. */
31108 if (curr->begin_entry != NULL
31109 || skip_loc_list_entry (curr))
31110 continue;
31112 curr->begin_entry
31113 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31117 FOR_EACH_CHILD (die, c, index_location_lists (c));
31120 /* Optimize location lists referenced from DIE
31121 children and share them whenever possible. */
31123 static void
31124 optimize_location_lists (dw_die_ref die)
31126 loc_list_hash_type htab (500);
31127 optimize_location_lists_1 (die, &htab);
31130 /* Traverse the limbo die list, and add parent/child links. The only
31131 dies without parents that should be here are concrete instances of
31132 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
31133 For concrete instances, we can get the parent die from the abstract
31134 instance. */
31136 static void
31137 flush_limbo_die_list (void)
31139 limbo_die_node *node;
31141 /* get_context_die calls force_decl_die, which can put new DIEs on the
31142 limbo list in LTO mode when nested functions are put in a different
31143 partition than that of their parent function. */
31144 while ((node = limbo_die_list))
31146 dw_die_ref die = node->die;
31147 limbo_die_list = node->next;
31149 if (die->die_parent == NULL)
31151 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31153 if (origin && origin->die_parent)
31154 add_child_die (origin->die_parent, die);
31155 else if (is_cu_die (die))
31157 else if (seen_error ())
31158 /* It's OK to be confused by errors in the input. */
31159 add_child_die (comp_unit_die (), die);
31160 else
31162 /* In certain situations, the lexical block containing a
31163 nested function can be optimized away, which results
31164 in the nested function die being orphaned. Likewise
31165 with the return type of that nested function. Force
31166 this to be a child of the containing function.
31168 It may happen that even the containing function got fully
31169 inlined and optimized out. In that case we are lost and
31170 assign the empty child. This should not be big issue as
31171 the function is likely unreachable too. */
31172 gcc_assert (node->created_for);
31174 if (DECL_P (node->created_for))
31175 origin = get_context_die (DECL_CONTEXT (node->created_for));
31176 else if (TYPE_P (node->created_for))
31177 origin = scope_die_for (node->created_for, comp_unit_die ());
31178 else
31179 origin = comp_unit_die ();
31181 add_child_die (origin, die);
31187 /* Reset DIEs so we can output them again. */
31189 static void
31190 reset_dies (dw_die_ref die)
31192 dw_die_ref c;
31194 /* Remove stuff we re-generate. */
31195 die->die_mark = 0;
31196 die->die_offset = 0;
31197 die->die_abbrev = 0;
31198 remove_AT (die, DW_AT_sibling);
31200 FOR_EACH_CHILD (die, c, reset_dies (c));
31203 /* Output stuff that dwarf requires at the end of every file,
31204 and generate the DWARF-2 debugging info. */
31206 static void
31207 dwarf2out_finish (const char *filename)
31209 comdat_type_node *ctnode;
31210 dw_die_ref main_comp_unit_die;
31211 unsigned char checksum[16];
31212 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31214 /* Flush out any latecomers to the limbo party. */
31215 flush_limbo_die_list ();
31217 if (inline_entry_data_table)
31218 gcc_assert (inline_entry_data_table->elements () == 0);
31220 if (flag_checking)
31222 verify_die (comp_unit_die ());
31223 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31224 verify_die (node->die);
31227 /* We shouldn't have any symbols with delayed asm names for
31228 DIEs generated after early finish. */
31229 gcc_assert (deferred_asm_name == NULL);
31231 gen_remaining_tmpl_value_param_die_attribute ();
31233 if (flag_generate_lto || flag_generate_offload)
31235 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31237 /* Prune stuff so that dwarf2out_finish runs successfully
31238 for the fat part of the object. */
31239 reset_dies (comp_unit_die ());
31240 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31241 reset_dies (node->die);
31243 hash_table<comdat_type_hasher> comdat_type_table (100);
31244 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31246 comdat_type_node **slot
31247 = comdat_type_table.find_slot (ctnode, INSERT);
31249 /* Don't reset types twice. */
31250 if (*slot != HTAB_EMPTY_ENTRY)
31251 continue;
31253 /* Remove the pointer to the line table. */
31254 remove_AT (ctnode->root_die, DW_AT_stmt_list);
31256 if (debug_info_level >= DINFO_LEVEL_TERSE)
31257 reset_dies (ctnode->root_die);
31259 *slot = ctnode;
31262 /* Reset die CU symbol so we don't output it twice. */
31263 comp_unit_die ()->die_id.die_symbol = NULL;
31265 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
31266 remove_AT (comp_unit_die (), DW_AT_stmt_list);
31267 if (have_macinfo)
31268 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
31270 /* Remove indirect string decisions. */
31271 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
31272 if (debug_line_str_hash)
31274 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
31275 debug_line_str_hash = NULL;
31279 #if ENABLE_ASSERT_CHECKING
31281 dw_die_ref die = comp_unit_die (), c;
31282 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
31284 #endif
31285 resolve_addr (comp_unit_die ());
31286 move_marked_base_types ();
31288 if (dump_file)
31290 fprintf (dump_file, "DWARF for %s\n", filename);
31291 print_die (comp_unit_die (), dump_file);
31294 /* Initialize sections and labels used for actual assembler output. */
31295 unsigned generation = init_sections_and_labels (false);
31297 /* Traverse the DIE's and add sibling attributes to those DIE's that
31298 have children. */
31299 add_sibling_attributes (comp_unit_die ());
31300 limbo_die_node *node;
31301 for (node = cu_die_list; node; node = node->next)
31302 add_sibling_attributes (node->die);
31303 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31304 add_sibling_attributes (ctnode->root_die);
31306 /* When splitting DWARF info, we put some attributes in the
31307 skeleton compile_unit DIE that remains in the .o, while
31308 most attributes go in the DWO compile_unit_die. */
31309 if (dwarf_split_debug_info)
31311 limbo_die_node *cu;
31312 main_comp_unit_die = gen_compile_unit_die (NULL);
31313 if (dwarf_version >= 5)
31314 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
31315 cu = limbo_die_list;
31316 gcc_assert (cu->die == main_comp_unit_die);
31317 limbo_die_list = limbo_die_list->next;
31318 cu->next = cu_die_list;
31319 cu_die_list = cu;
31321 else
31322 main_comp_unit_die = comp_unit_die ();
31324 /* Output a terminator label for the .text section. */
31325 switch_to_section (text_section);
31326 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
31327 if (cold_text_section)
31329 switch_to_section (cold_text_section);
31330 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
31333 /* We can only use the low/high_pc attributes if all of the code was
31334 in .text. */
31335 if (!have_multiple_function_sections
31336 || (dwarf_version < 3 && dwarf_strict))
31338 /* Don't add if the CU has no associated code. */
31339 if (text_section_used)
31340 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
31341 text_end_label, true);
31343 else
31345 unsigned fde_idx;
31346 dw_fde_ref fde;
31347 bool range_list_added = false;
31349 if (text_section_used)
31350 add_ranges_by_labels (main_comp_unit_die, text_section_label,
31351 text_end_label, &range_list_added, true);
31352 if (cold_text_section_used)
31353 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
31354 cold_end_label, &range_list_added, true);
31356 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
31358 if (DECL_IGNORED_P (fde->decl))
31359 continue;
31360 if (!fde->in_std_section)
31361 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
31362 fde->dw_fde_end, &range_list_added,
31363 true);
31364 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
31365 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
31366 fde->dw_fde_second_end, &range_list_added,
31367 true);
31370 if (range_list_added)
31372 /* We need to give .debug_loc and .debug_ranges an appropriate
31373 "base address". Use zero so that these addresses become
31374 absolute. Historically, we've emitted the unexpected
31375 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
31376 Emit both to give time for other tools to adapt. */
31377 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
31378 if (! dwarf_strict && dwarf_version < 4)
31379 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
31381 add_ranges (NULL);
31385 /* AIX Assembler inserts the length, so adjust the reference to match the
31386 offset expected by debuggers. */
31387 strcpy (dl_section_ref, debug_line_section_label);
31388 if (XCOFF_DEBUGGING_INFO)
31389 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
31391 if (debug_info_level >= DINFO_LEVEL_TERSE)
31392 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
31393 dl_section_ref);
31395 if (have_macinfo)
31396 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31397 macinfo_section_label);
31399 if (dwarf_split_debug_info)
31401 if (have_location_lists)
31403 /* Since we generate the loclists in the split DWARF .dwo
31404 file itself, we don't need to generate a loclists_base
31405 attribute for the split compile unit DIE. That attribute
31406 (and using relocatable sec_offset FORMs) isn't allowed
31407 for a split compile unit. Only if the .debug_loclists
31408 section was in the main file, would we need to generate a
31409 loclists_base attribute here (for the full or skeleton
31410 unit DIE). */
31412 /* optimize_location_lists calculates the size of the lists,
31413 so index them first, and assign indices to the entries.
31414 Although optimize_location_lists will remove entries from
31415 the table, it only does so for duplicates, and therefore
31416 only reduces ref_counts to 1. */
31417 index_location_lists (comp_unit_die ());
31420 if (addr_index_table != NULL)
31422 unsigned int index = 0;
31423 addr_index_table
31424 ->traverse_noresize<unsigned int *, index_addr_table_entry>
31425 (&index);
31429 loc_list_idx = 0;
31430 if (have_location_lists)
31432 optimize_location_lists (comp_unit_die ());
31433 /* And finally assign indexes to the entries for -gsplit-dwarf. */
31434 if (dwarf_version >= 5 && dwarf_split_debug_info)
31435 assign_location_list_indexes (comp_unit_die ());
31438 save_macinfo_strings ();
31440 if (dwarf_split_debug_info)
31442 unsigned int index = 0;
31444 /* Add attributes common to skeleton compile_units and
31445 type_units. Because these attributes include strings, it
31446 must be done before freezing the string table. Top-level
31447 skeleton die attrs are added when the skeleton type unit is
31448 created, so ensure it is created by this point. */
31449 add_top_level_skeleton_die_attrs (main_comp_unit_die);
31450 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31453 /* Output all of the compilation units. We put the main one last so that
31454 the offsets are available to output_pubnames. */
31455 for (node = cu_die_list; node; node = node->next)
31456 output_comp_unit (node->die, 0, NULL);
31458 hash_table<comdat_type_hasher> comdat_type_table (100);
31459 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31461 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31463 /* Don't output duplicate types. */
31464 if (*slot != HTAB_EMPTY_ENTRY)
31465 continue;
31467 /* Add a pointer to the line table for the main compilation unit
31468 so that the debugger can make sense of DW_AT_decl_file
31469 attributes. */
31470 if (debug_info_level >= DINFO_LEVEL_TERSE)
31471 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31472 (!dwarf_split_debug_info
31473 ? dl_section_ref
31474 : debug_skeleton_line_section_label));
31476 output_comdat_type_unit (ctnode);
31477 *slot = ctnode;
31480 if (dwarf_split_debug_info)
31482 int mark;
31483 struct md5_ctx ctx;
31485 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
31486 index_rnglists ();
31488 /* Compute a checksum of the comp_unit to use as the dwo_id. */
31489 md5_init_ctx (&ctx);
31490 mark = 0;
31491 die_checksum (comp_unit_die (), &ctx, &mark);
31492 unmark_all_dies (comp_unit_die ());
31493 md5_finish_ctx (&ctx, checksum);
31495 if (dwarf_version < 5)
31497 /* Use the first 8 bytes of the checksum as the dwo_id,
31498 and add it to both comp-unit DIEs. */
31499 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
31500 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
31503 /* Add the base offset of the ranges table to the skeleton
31504 comp-unit DIE. */
31505 if (!vec_safe_is_empty (ranges_table))
31507 if (dwarf_version >= 5)
31508 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
31509 ranges_base_label);
31510 else
31511 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
31512 ranges_section_label);
31515 switch_to_section (debug_addr_section);
31516 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
31517 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
31518 before DWARF5, didn't have a header for .debug_addr units.
31519 DWARF5 specifies a small header when address tables are used. */
31520 if (dwarf_version >= 5)
31522 unsigned int last_idx = 0;
31523 unsigned long addrs_length;
31525 addr_index_table->traverse_noresize
31526 <unsigned int *, count_index_addrs> (&last_idx);
31527 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
31529 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31530 dw2_asm_output_data (4, 0xffffffff,
31531 "Escape value for 64-bit DWARF extension");
31532 dw2_asm_output_data (DWARF_OFFSET_SIZE, addrs_length,
31533 "Length of Address Unit");
31534 dw2_asm_output_data (2, 5, "DWARF addr version");
31535 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
31536 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
31538 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
31539 output_addr_table ();
31542 /* Output the main compilation unit if non-empty or if .debug_macinfo
31543 or .debug_macro will be emitted. */
31544 output_comp_unit (comp_unit_die (), have_macinfo,
31545 dwarf_split_debug_info ? checksum : NULL);
31547 if (dwarf_split_debug_info && info_section_emitted)
31548 output_skeleton_debug_sections (main_comp_unit_die, checksum);
31550 /* Output the abbreviation table. */
31551 if (vec_safe_length (abbrev_die_table) != 1)
31553 switch_to_section (debug_abbrev_section);
31554 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31555 output_abbrev_section ();
31558 /* Output location list section if necessary. */
31559 if (have_location_lists)
31561 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
31562 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
31563 /* Output the location lists info. */
31564 switch_to_section (debug_loc_section);
31565 if (dwarf_version >= 5)
31567 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
31568 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
31569 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31570 dw2_asm_output_data (4, 0xffffffff,
31571 "Initial length escape value indicating "
31572 "64-bit DWARF extension");
31573 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
31574 "Length of Location Lists");
31575 ASM_OUTPUT_LABEL (asm_out_file, l1);
31576 output_dwarf_version ();
31577 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
31578 dw2_asm_output_data (1, 0, "Segment Size");
31579 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
31580 "Offset Entry Count");
31582 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
31583 if (dwarf_version >= 5 && dwarf_split_debug_info)
31585 unsigned int save_loc_list_idx = loc_list_idx;
31586 loc_list_idx = 0;
31587 output_loclists_offsets (comp_unit_die ());
31588 gcc_assert (save_loc_list_idx == loc_list_idx);
31590 output_location_lists (comp_unit_die ());
31591 if (dwarf_version >= 5)
31592 ASM_OUTPUT_LABEL (asm_out_file, l2);
31595 output_pubtables ();
31597 /* Output the address range information if a CU (.debug_info section)
31598 was emitted. We output an empty table even if we had no functions
31599 to put in it. This because the consumer has no way to tell the
31600 difference between an empty table that we omitted and failure to
31601 generate a table that would have contained data. */
31602 if (info_section_emitted)
31604 switch_to_section (debug_aranges_section);
31605 output_aranges ();
31608 /* Output ranges section if necessary. */
31609 if (!vec_safe_is_empty (ranges_table))
31611 if (dwarf_version >= 5)
31612 output_rnglists (generation);
31613 else
31614 output_ranges ();
31617 /* Have to end the macro section. */
31618 if (have_macinfo)
31620 switch_to_section (debug_macinfo_section);
31621 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31622 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
31623 : debug_skeleton_line_section_label, false);
31624 dw2_asm_output_data (1, 0, "End compilation unit");
31627 /* Output the source line correspondence table. We must do this
31628 even if there is no line information. Otherwise, on an empty
31629 translation unit, we will generate a present, but empty,
31630 .debug_info section. IRIX 6.5 `nm' will then complain when
31631 examining the file. This is done late so that any filenames
31632 used by the debug_info section are marked as 'used'. */
31633 switch_to_section (debug_line_section);
31634 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
31635 if (! output_asm_line_debug_info ())
31636 output_line_info (false);
31638 if (dwarf_split_debug_info && info_section_emitted)
31640 switch_to_section (debug_skeleton_line_section);
31641 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31642 output_line_info (true);
31645 /* If we emitted any indirect strings, output the string table too. */
31646 if (debug_str_hash || skeleton_debug_str_hash)
31647 output_indirect_strings ();
31648 if (debug_line_str_hash)
31650 switch_to_section (debug_line_str_section);
31651 const enum dwarf_form form = DW_FORM_line_strp;
31652 debug_line_str_hash->traverse<enum dwarf_form,
31653 output_indirect_string> (form);
31656 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
31657 symview_upper_bound = 0;
31658 if (zero_view_p)
31659 bitmap_clear (zero_view_p);
31662 /* Returns a hash value for X (which really is a variable_value_struct). */
31664 inline hashval_t
31665 variable_value_hasher::hash (variable_value_struct *x)
31667 return (hashval_t) x->decl_id;
31670 /* Return nonzero if decl_id of variable_value_struct X is the same as
31671 UID of decl Y. */
31673 inline bool
31674 variable_value_hasher::equal (variable_value_struct *x, tree y)
31676 return x->decl_id == DECL_UID (y);
31679 /* Helper function for resolve_variable_value, handle
31680 DW_OP_GNU_variable_value in one location expression.
31681 Return true if exprloc has been changed into loclist. */
31683 static bool
31684 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31686 dw_loc_descr_ref next;
31687 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
31689 next = loc->dw_loc_next;
31690 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
31691 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
31692 continue;
31694 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31695 if (DECL_CONTEXT (decl) != current_function_decl)
31696 continue;
31698 dw_die_ref ref = lookup_decl_die (decl);
31699 if (ref)
31701 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31702 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31703 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31704 continue;
31706 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
31707 if (l == NULL)
31708 continue;
31709 if (l->dw_loc_next)
31711 if (AT_class (a) != dw_val_class_loc)
31712 continue;
31713 switch (a->dw_attr)
31715 /* Following attributes allow both exprloc and loclist
31716 classes, so we can change them into a loclist. */
31717 case DW_AT_location:
31718 case DW_AT_string_length:
31719 case DW_AT_return_addr:
31720 case DW_AT_data_member_location:
31721 case DW_AT_frame_base:
31722 case DW_AT_segment:
31723 case DW_AT_static_link:
31724 case DW_AT_use_location:
31725 case DW_AT_vtable_elem_location:
31726 if (prev)
31728 prev->dw_loc_next = NULL;
31729 prepend_loc_descr_to_each (l, AT_loc (a));
31731 if (next)
31732 add_loc_descr_to_each (l, next);
31733 a->dw_attr_val.val_class = dw_val_class_loc_list;
31734 a->dw_attr_val.val_entry = NULL;
31735 a->dw_attr_val.v.val_loc_list = l;
31736 have_location_lists = true;
31737 return true;
31738 /* Following attributes allow both exprloc and reference,
31739 so if the whole expression is DW_OP_GNU_variable_value alone
31740 we could transform it into reference. */
31741 case DW_AT_byte_size:
31742 case DW_AT_bit_size:
31743 case DW_AT_lower_bound:
31744 case DW_AT_upper_bound:
31745 case DW_AT_bit_stride:
31746 case DW_AT_count:
31747 case DW_AT_allocated:
31748 case DW_AT_associated:
31749 case DW_AT_byte_stride:
31750 if (prev == NULL && next == NULL)
31751 break;
31752 /* FALLTHRU */
31753 default:
31754 if (dwarf_strict)
31755 continue;
31756 break;
31758 /* Create DW_TAG_variable that we can refer to. */
31759 gen_decl_die (decl, NULL_TREE, NULL,
31760 lookup_decl_die (current_function_decl));
31761 ref = lookup_decl_die (decl);
31762 if (ref)
31764 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31765 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31766 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31768 continue;
31770 if (prev)
31772 prev->dw_loc_next = l->expr;
31773 add_loc_descr (&prev->dw_loc_next, next);
31774 free_loc_descr (loc, NULL);
31775 next = prev->dw_loc_next;
31777 else
31779 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
31780 add_loc_descr (&loc, next);
31781 next = loc;
31783 loc = prev;
31785 return false;
31788 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
31790 static void
31791 resolve_variable_value (dw_die_ref die)
31793 dw_attr_node *a;
31794 dw_loc_list_ref loc;
31795 unsigned ix;
31797 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31798 switch (AT_class (a))
31800 case dw_val_class_loc:
31801 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
31802 break;
31803 /* FALLTHRU */
31804 case dw_val_class_loc_list:
31805 loc = AT_loc_list (a);
31806 gcc_assert (loc);
31807 for (; loc; loc = loc->dw_loc_next)
31808 resolve_variable_value_in_expr (a, loc->expr);
31809 break;
31810 default:
31811 break;
31815 /* Attempt to optimize DW_OP_GNU_variable_value refering to
31816 temporaries in the current function. */
31818 static void
31819 resolve_variable_values (void)
31821 if (!variable_value_hash || !current_function_decl)
31822 return;
31824 struct variable_value_struct *node
31825 = variable_value_hash->find_with_hash (current_function_decl,
31826 DECL_UID (current_function_decl));
31828 if (node == NULL)
31829 return;
31831 unsigned int i;
31832 dw_die_ref die;
31833 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
31834 resolve_variable_value (die);
31837 /* Helper function for note_variable_value, handle one location
31838 expression. */
31840 static void
31841 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
31843 for (; loc; loc = loc->dw_loc_next)
31844 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
31845 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31847 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31848 dw_die_ref ref = lookup_decl_die (decl);
31849 if (! ref && (flag_generate_lto || flag_generate_offload))
31851 /* ??? This is somewhat a hack because we do not create DIEs
31852 for variables not in BLOCK trees early but when generating
31853 early LTO output we need the dw_val_class_decl_ref to be
31854 fully resolved. For fat LTO objects we'd also like to
31855 undo this after LTO dwarf output. */
31856 gcc_assert (DECL_CONTEXT (decl));
31857 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
31858 gcc_assert (ctx != NULL);
31859 gen_decl_die (decl, NULL_TREE, NULL, ctx);
31860 ref = lookup_decl_die (decl);
31861 gcc_assert (ref != NULL);
31863 if (ref)
31865 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31866 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31867 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31868 continue;
31870 if (VAR_P (decl)
31871 && DECL_CONTEXT (decl)
31872 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
31873 && lookup_decl_die (DECL_CONTEXT (decl)))
31875 if (!variable_value_hash)
31876 variable_value_hash
31877 = hash_table<variable_value_hasher>::create_ggc (10);
31879 tree fndecl = DECL_CONTEXT (decl);
31880 struct variable_value_struct *node;
31881 struct variable_value_struct **slot
31882 = variable_value_hash->find_slot_with_hash (fndecl,
31883 DECL_UID (fndecl),
31884 INSERT);
31885 if (*slot == NULL)
31887 node = ggc_cleared_alloc<variable_value_struct> ();
31888 node->decl_id = DECL_UID (fndecl);
31889 *slot = node;
31891 else
31892 node = *slot;
31894 vec_safe_push (node->dies, die);
31899 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
31900 with dw_val_class_decl_ref operand. */
31902 static void
31903 note_variable_value (dw_die_ref die)
31905 dw_die_ref c;
31906 dw_attr_node *a;
31907 dw_loc_list_ref loc;
31908 unsigned ix;
31910 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31911 switch (AT_class (a))
31913 case dw_val_class_loc_list:
31914 loc = AT_loc_list (a);
31915 gcc_assert (loc);
31916 if (!loc->noted_variable_value)
31918 loc->noted_variable_value = 1;
31919 for (; loc; loc = loc->dw_loc_next)
31920 note_variable_value_in_expr (die, loc->expr);
31922 break;
31923 case dw_val_class_loc:
31924 note_variable_value_in_expr (die, AT_loc (a));
31925 break;
31926 default:
31927 break;
31930 /* Mark children. */
31931 FOR_EACH_CHILD (die, c, note_variable_value (c));
31934 /* Perform any cleanups needed after the early debug generation pass
31935 has run. */
31937 static void
31938 dwarf2out_early_finish (const char *filename)
31940 set_early_dwarf s;
31941 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31943 /* PCH might result in DW_AT_producer string being restored from the
31944 header compilation, so always fill it with empty string initially
31945 and overwrite only here. */
31946 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
31947 producer_string = gen_producer_string ();
31948 producer->dw_attr_val.v.val_str->refcount--;
31949 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
31951 /* Add the name for the main input file now. We delayed this from
31952 dwarf2out_init to avoid complications with PCH. */
31953 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
31954 add_comp_dir_attribute (comp_unit_die ());
31956 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
31957 DW_AT_comp_dir into .debug_line_str section. */
31958 if (!output_asm_line_debug_info ()
31959 && dwarf_version >= 5
31960 && DWARF5_USE_DEBUG_LINE_STR)
31962 for (int i = 0; i < 2; i++)
31964 dw_attr_node *a = get_AT (comp_unit_die (),
31965 i ? DW_AT_comp_dir : DW_AT_name);
31966 if (a == NULL
31967 || AT_class (a) != dw_val_class_str
31968 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
31969 continue;
31971 if (! debug_line_str_hash)
31972 debug_line_str_hash
31973 = hash_table<indirect_string_hasher>::create_ggc (10);
31975 struct indirect_string_node *node
31976 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
31977 set_indirect_string (node);
31978 node->form = DW_FORM_line_strp;
31979 a->dw_attr_val.v.val_str->refcount--;
31980 a->dw_attr_val.v.val_str = node;
31984 /* With LTO early dwarf was really finished at compile-time, so make
31985 sure to adjust the phase after annotating the LTRANS CU DIE. */
31986 if (in_lto_p)
31988 early_dwarf_finished = true;
31989 if (dump_file)
31991 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
31992 print_die (comp_unit_die (), dump_file);
31994 return;
31997 /* Walk through the list of incomplete types again, trying once more to
31998 emit full debugging info for them. */
31999 retry_incomplete_types ();
32001 /* The point here is to flush out the limbo list so that it is empty
32002 and we don't need to stream it for LTO. */
32003 flush_limbo_die_list ();
32005 gen_scheduled_generic_parms_dies ();
32006 gen_remaining_tmpl_value_param_die_attribute ();
32008 /* Add DW_AT_linkage_name for all deferred DIEs. */
32009 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32011 tree decl = node->created_for;
32012 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32013 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32014 ended up in deferred_asm_name before we knew it was
32015 constant and never written to disk. */
32016 && DECL_ASSEMBLER_NAME (decl))
32018 add_linkage_attr (node->die, decl);
32019 move_linkage_attr (node->die);
32022 deferred_asm_name = NULL;
32024 if (flag_eliminate_unused_debug_types)
32025 prune_unused_types ();
32027 /* Generate separate COMDAT sections for type DIEs. */
32028 if (use_debug_types)
32030 break_out_comdat_types (comp_unit_die ());
32032 /* Each new type_unit DIE was added to the limbo die list when created.
32033 Since these have all been added to comdat_type_list, clear the
32034 limbo die list. */
32035 limbo_die_list = NULL;
32037 /* For each new comdat type unit, copy declarations for incomplete
32038 types to make the new unit self-contained (i.e., no direct
32039 references to the main compile unit). */
32040 for (comdat_type_node *ctnode = comdat_type_list;
32041 ctnode != NULL; ctnode = ctnode->next)
32042 copy_decls_for_unworthy_types (ctnode->root_die);
32043 copy_decls_for_unworthy_types (comp_unit_die ());
32045 /* In the process of copying declarations from one unit to another,
32046 we may have left some declarations behind that are no longer
32047 referenced. Prune them. */
32048 prune_unused_types ();
32051 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
32052 with dw_val_class_decl_ref operand. */
32053 note_variable_value (comp_unit_die ());
32054 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32055 note_variable_value (node->die);
32056 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
32057 ctnode = ctnode->next)
32058 note_variable_value (ctnode->root_die);
32059 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32060 note_variable_value (node->die);
32062 /* The AT_pubnames attribute needs to go in all skeleton dies, including
32063 both the main_cu and all skeleton TUs. Making this call unconditional
32064 would end up either adding a second copy of the AT_pubnames attribute, or
32065 requiring a special case in add_top_level_skeleton_die_attrs. */
32066 if (!dwarf_split_debug_info)
32067 add_AT_pubnames (comp_unit_die ());
32069 /* The early debug phase is now finished. */
32070 early_dwarf_finished = true;
32071 if (dump_file)
32073 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
32074 print_die (comp_unit_die (), dump_file);
32077 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
32078 if ((!flag_generate_lto && !flag_generate_offload)
32079 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
32080 copy_lto_debug_sections operation of the simple object support in
32081 libiberty is not implemented for them yet. */
32082 || TARGET_PECOFF || TARGET_COFF)
32083 return;
32085 /* Now as we are going to output for LTO initialize sections and labels
32086 to the LTO variants. We don't need a random-seed postfix as other
32087 LTO sections as linking the LTO debug sections into one in a partial
32088 link is fine. */
32089 init_sections_and_labels (true);
32091 /* The output below is modeled after dwarf2out_finish with all
32092 location related output removed and some LTO specific changes.
32093 Some refactoring might make both smaller and easier to match up. */
32095 /* Traverse the DIE's and add add sibling attributes to those DIE's
32096 that have children. */
32097 add_sibling_attributes (comp_unit_die ());
32098 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32099 add_sibling_attributes (node->die);
32100 for (comdat_type_node *ctnode = comdat_type_list;
32101 ctnode != NULL; ctnode = ctnode->next)
32102 add_sibling_attributes (ctnode->root_die);
32104 /* AIX Assembler inserts the length, so adjust the reference to match the
32105 offset expected by debuggers. */
32106 strcpy (dl_section_ref, debug_line_section_label);
32107 if (XCOFF_DEBUGGING_INFO)
32108 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32110 if (debug_info_level >= DINFO_LEVEL_TERSE)
32111 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32113 if (have_macinfo)
32114 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32115 macinfo_section_label);
32117 save_macinfo_strings ();
32119 if (dwarf_split_debug_info)
32121 unsigned int index = 0;
32122 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32125 /* Output all of the compilation units. We put the main one last so that
32126 the offsets are available to output_pubnames. */
32127 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32128 output_comp_unit (node->die, 0, NULL);
32130 hash_table<comdat_type_hasher> comdat_type_table (100);
32131 for (comdat_type_node *ctnode = comdat_type_list;
32132 ctnode != NULL; ctnode = ctnode->next)
32134 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32136 /* Don't output duplicate types. */
32137 if (*slot != HTAB_EMPTY_ENTRY)
32138 continue;
32140 /* Add a pointer to the line table for the main compilation unit
32141 so that the debugger can make sense of DW_AT_decl_file
32142 attributes. */
32143 if (debug_info_level >= DINFO_LEVEL_TERSE)
32144 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32145 (!dwarf_split_debug_info
32146 ? debug_line_section_label
32147 : debug_skeleton_line_section_label));
32149 output_comdat_type_unit (ctnode);
32150 *slot = ctnode;
32153 /* Stick a unique symbol to the main debuginfo section. */
32154 compute_comp_unit_symbol (comp_unit_die ());
32156 /* Output the main compilation unit. We always need it if only for
32157 the CU symbol. */
32158 output_comp_unit (comp_unit_die (), true, NULL);
32160 /* Output the abbreviation table. */
32161 if (vec_safe_length (abbrev_die_table) != 1)
32163 switch_to_section (debug_abbrev_section);
32164 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32165 output_abbrev_section ();
32168 /* Have to end the macro section. */
32169 if (have_macinfo)
32171 /* We have to save macinfo state if we need to output it again
32172 for the FAT part of the object. */
32173 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32174 if (flag_fat_lto_objects)
32175 macinfo_table = macinfo_table->copy ();
32177 switch_to_section (debug_macinfo_section);
32178 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32179 output_macinfo (debug_line_section_label, true);
32180 dw2_asm_output_data (1, 0, "End compilation unit");
32182 if (flag_fat_lto_objects)
32184 vec_free (macinfo_table);
32185 macinfo_table = saved_macinfo_table;
32189 /* Emit a skeleton debug_line section. */
32190 switch_to_section (debug_line_section);
32191 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32192 output_line_info (true);
32194 /* If we emitted any indirect strings, output the string table too. */
32195 if (debug_str_hash || skeleton_debug_str_hash)
32196 output_indirect_strings ();
32197 if (debug_line_str_hash)
32199 switch_to_section (debug_line_str_section);
32200 const enum dwarf_form form = DW_FORM_line_strp;
32201 debug_line_str_hash->traverse<enum dwarf_form,
32202 output_indirect_string> (form);
32205 /* Switch back to the text section. */
32206 switch_to_section (text_section);
32209 /* Reset all state within dwarf2out.c so that we can rerun the compiler
32210 within the same process. For use by toplev::finalize. */
32212 void
32213 dwarf2out_c_finalize (void)
32215 last_var_location_insn = NULL;
32216 cached_next_real_insn = NULL;
32217 used_rtx_array = NULL;
32218 incomplete_types = NULL;
32219 debug_info_section = NULL;
32220 debug_skeleton_info_section = NULL;
32221 debug_abbrev_section = NULL;
32222 debug_skeleton_abbrev_section = NULL;
32223 debug_aranges_section = NULL;
32224 debug_addr_section = NULL;
32225 debug_macinfo_section = NULL;
32226 debug_line_section = NULL;
32227 debug_skeleton_line_section = NULL;
32228 debug_loc_section = NULL;
32229 debug_pubnames_section = NULL;
32230 debug_pubtypes_section = NULL;
32231 debug_str_section = NULL;
32232 debug_line_str_section = NULL;
32233 debug_str_dwo_section = NULL;
32234 debug_str_offsets_section = NULL;
32235 debug_ranges_section = NULL;
32236 debug_frame_section = NULL;
32237 fde_vec = NULL;
32238 debug_str_hash = NULL;
32239 debug_line_str_hash = NULL;
32240 skeleton_debug_str_hash = NULL;
32241 dw2_string_counter = 0;
32242 have_multiple_function_sections = false;
32243 text_section_used = false;
32244 cold_text_section_used = false;
32245 cold_text_section = NULL;
32246 current_unit_personality = NULL;
32248 early_dwarf = false;
32249 early_dwarf_finished = false;
32251 next_die_offset = 0;
32252 single_comp_unit_die = NULL;
32253 comdat_type_list = NULL;
32254 limbo_die_list = NULL;
32255 file_table = NULL;
32256 decl_die_table = NULL;
32257 common_block_die_table = NULL;
32258 decl_loc_table = NULL;
32259 call_arg_locations = NULL;
32260 call_arg_loc_last = NULL;
32261 call_site_count = -1;
32262 tail_call_site_count = -1;
32263 cached_dw_loc_list_table = NULL;
32264 abbrev_die_table = NULL;
32265 delete dwarf_proc_stack_usage_map;
32266 dwarf_proc_stack_usage_map = NULL;
32267 line_info_label_num = 0;
32268 cur_line_info_table = NULL;
32269 text_section_line_info = NULL;
32270 cold_text_section_line_info = NULL;
32271 separate_line_info = NULL;
32272 info_section_emitted = false;
32273 pubname_table = NULL;
32274 pubtype_table = NULL;
32275 macinfo_table = NULL;
32276 ranges_table = NULL;
32277 ranges_by_label = NULL;
32278 rnglist_idx = 0;
32279 have_location_lists = false;
32280 loclabel_num = 0;
32281 poc_label_num = 0;
32282 last_emitted_file = NULL;
32283 label_num = 0;
32284 tmpl_value_parm_die_table = NULL;
32285 generic_type_instances = NULL;
32286 frame_pointer_fb_offset = 0;
32287 frame_pointer_fb_offset_valid = false;
32288 base_types.release ();
32289 XDELETEVEC (producer_string);
32290 producer_string = NULL;
32293 #include "gt-dwarf2out.h"